Rizin
unix-like reverse engineering framework and cli tools
crca.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2009 Victor Mu~noz <vmunoz@ingenieria-inversa.cl>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 // some definitions and test cases borrowed from http://www.nightmare.com/~ryb/code/CrcMoose.py (Ray Burr)
5 
6 #include "crca.h"
7 
8 void crc_init_custom(RzCrc *ctx, utcrc crc, ut32 size, int reflect, utcrc poly, utcrc xout) {
9  ctx->crc = crc;
10  ctx->size = size;
11  ctx->reflect = reflect;
12  ctx->poly = poly;
13  ctx->xout = xout;
14 }
15 
16 void crc_update(RzCrc *ctx, const ut8 *data, ut32 sz) {
17  utcrc crc, d;
18  int i, j;
19 
20  crc = ctx->crc;
21  for (i = 0; i < sz; i++) {
22  d = data[i];
23  if (ctx->reflect) {
24  for (j = 0; j < 4; j++) {
25  if (((d >> j) ^ (d >> (7 - j))) & 1) {
26  d ^= (1 << j) ^ (1 << (7 - j));
27  }
28  }
29  }
30  crc ^= d << (ctx->size - 8);
31  for (j = 0; j < 8; j++) {
32  crc = ((crc >> (ctx->size - 1)) & 1 ? ctx->poly : 0) ^ (crc << 1);
33  }
34  }
35  ctx->crc = crc;
36 }
37 
39  utcrc crc;
40  int i;
41 
42  crc = ctx->crc;
43  crc &= (((UTCRC_C(1) << (ctx->size - 1)) - 1) << 1) | 1;
44  if (ctx->reflect) {
45  for (i = 0; i < (ctx->size >> 1); i++) {
46  if (((crc >> i) ^ (crc >> (ctx->size - 1 - i))) & 1) {
47  crc ^= (UTCRC_C(1) << i) ^ (UTCRC_C(1) << (ctx->size - 1 - i));
48  }
49  }
50  }
51 
52  *r = crc ^ ctx->xout;
53 }
54 
55 /* preset initializer to provide compatibility */
56 #define CRC_PRESET(crc, size, reflect, poly, xout) \
57  { UTCRC_C(crc), (size), (reflect), UTCRC_C(poly), UTCRC_C(xout) }
58 
59 /* NOTE: Run `rz-hash -a <algo> -s 123456789` to test CRC. */
61  CRC_PRESET(0x00, 8, 0, 0x07, 0x00), // CRC-8-SMBUS, test vector for "1234567892: f4
62  CRC_PRESET(0xFF, 8, 0, 0x9B, 0x00), // CRC-8/CDMA2000, test vector for "123456789": 0xda
63  CRC_PRESET(0x00, 8, 1, 0x39, 0x00), // CRC-8/DARC, test vector for "123456789": 0x15
64  CRC_PRESET(0x00, 8, 0, 0xD5, 0x00), // CRC-8/DVB-S2, test vector for "123456789": 0xbc
65  CRC_PRESET(0xFF, 8, 1, 0x1D, 0x00), // CRC-8/EBU, test vector for "123456789": 0x97
66  CRC_PRESET(0xFD, 8, 0, 0x1D, 0x00), // CRC-8/I-CODE, test vector for "123456789": 0x7e
67  CRC_PRESET(0x00, 8, 0, 0x07, 0x55), // CRC-8/ITU, test vector for "123456789": 0xa1
68  CRC_PRESET(0x00, 8, 1, 0x31, 0x00), // CRC-8/MAXIM, test vector for "123456789": 0xa1
69  CRC_PRESET(0xFF, 8, 1, 0x07, 0x00), // CRC-8/ROHC, test vector for "123456789": 0xd0
70  CRC_PRESET(0x00, 8, 1, 0x9B, 0x00), // CRC-8/WCDMA, test vector for "123456789": 0x25
71  CRC_PRESET(0x0000, 15, 0, 0x4599, 0x0000), // CRC-15-CAN, test vector for "1234567892: 059e
72  CRC_PRESET(0x0000, 16, 1, 0x8005, 0x0000), // CRC-16-IBM (CRC-16/ARC), test vector for "1234567892: bb3d
73  CRC_PRESET(0xFFFF, 16, 0, 0x1021, 0x0000), // CRC-16-CITT (CRC-16/CCITT-FALSE), test vector for "1234567892: 29b1
74  CRC_PRESET(0xFFFF, 16, 1, 0x8005, 0xFFFF), // CRC-16-USB, test vector for "1234567892: b4c8
75  CRC_PRESET(0xFFFF, 16, 1, 0x1021, 0xFFFF), // CRC-HDLC, test vector for "1234567892: 906e
76  CRC_PRESET(0x1D0F, 16, 0, 0x1021, 0x0000), // CRC-16/AUG-CCITT, test vector for "123456789": 0xe5cc
77  CRC_PRESET(0x0000, 16, 0, 0x8005, 0x0000), // CRC-16/BUYPASS, test vector for "123456789": 0xfee8
78  CRC_PRESET(0xFFFF, 16, 0, 0xC867, 0x0000), // CRC-16/CDMA2000, test vector for "123456789": 0x4c06
79  CRC_PRESET(0x800D, 16, 0, 0x8005, 0x0000), // CRC-16/DDS110, test vector for "123456789": 0x9ecf
80  CRC_PRESET(0x0000, 16, 0, 0x0589, 0x0001), // CRC-16/DECT-R, test vector for "123456789": 0x007e
81  CRC_PRESET(0x0000, 16, 0, 0x0589, 0x0000), // CRC-16/DECT-X, test vector for "123456789": 0x007f
82  CRC_PRESET(0x0000, 16, 1, 0x3D65, 0xFFFF), // CRC-16/DNP, test vector for "123456789": 0xea82
83  CRC_PRESET(0x0000, 16, 0, 0x3D65, 0xFFFF), // CRC-16/EN-13757, test vector for "123456789": 0xc2b7
84  CRC_PRESET(0xFFFF, 16, 0, 0x1021, 0xFFFF), // CRC-16/GENIBUS, test vector for "123456789": 0xd64e
85  CRC_PRESET(0x0000, 16, 1, 0x8005, 0xFFFF), // CRC-16/MAXIM, test vector for "123456789": 0x44c2
86  CRC_PRESET(0xFFFF, 16, 1, 0x1021, 0x0000), // CRC-16/MCRF4XX, test vector for "123456789": 0x6f91
87  CRC_PRESET(0xB2AA, 16, 1, 0x1021, 0x0000), // CRC-16/RIELLO, test vector for "123456789": 0x63d0
88  CRC_PRESET(0x0000, 16, 0, 0x8BB7, 0x0000), // CRC-16/T10-DIF, test vector for "123456789": 0xd0db
89  CRC_PRESET(0x0000, 16, 0, 0xA097, 0x0000), // CRC-16/TELEDISK, test vector for "123456789": 0x0fb3
90  CRC_PRESET(0x89EC, 16, 1, 0x1021, 0x0000), // CRC-16/TMS37157, test vector for "123456789": 0x26b1
91  CRC_PRESET(0xC6C6, 16, 1, 0x1021, 0x0000), // CRC-A, test vector for "123456789": 0xbf05
92  CRC_PRESET(0x0000, 16, 1, 0x1021, 0x0000), // CRC-16/KERMIT, test vector for "123456789": 0x2189
93  CRC_PRESET(0xFFFF, 16, 1, 0x8005, 0x0000), // CRC-16/MODBUS, test vector for "123456789": 0x4b37
94  CRC_PRESET(0xFFFF, 16, 1, 0x1021, 0xFFFF), // CRC-16/X-25, test vector for "123456789": 0x906e
95  CRC_PRESET(0x0000, 16, 0, 0x1021, 0x0000), // CRC-16/XMODEM, test vector for "123456789": 0x31c3
96  CRC_PRESET(0xB704CE, 24, 0, 0x864CFB, 0x000000), // CRC-24, test vector for "1234567892: 21cf02
97  CRC_PRESET(0xFFFFFFFF, 32, 1, 0x04C11DB7, 0xFFFFFFFF), // CRC-32, test vector for "1234567892: cbf43926
98  CRC_PRESET(0x00000000, 32, 0, 0x80000011, 0x00000000), // CRC-32-ECMA-267 (EDC for DVD sectors), test vector for "1234567892: b27ce117
99  CRC_PRESET(0xFFFFFFFF, 32, 1, 0x1EDC6F41, 0xFFFFFFFF), // CRC-32C, test vector for "1234567892: e3069283
100  CRC_PRESET(0xFFFFFFFF, 32, 0, 0x04C11DB7, 0xFFFFFFFF), // CRC-32/BZIP2, test vector for "123456789": 0xfc891918
101  CRC_PRESET(0xFFFFFFFF, 32, 1, 0xA833982B, 0xFFFFFFFF), // CRC-32D, test vector for "123456789": 0x87315576
102  CRC_PRESET(0xFFFFFFFF, 32, 0, 0x04C11DB7, 0x00000000), // CRC-32/MPEG2, test vector for "123456789": 0x0376e6e7
103  CRC_PRESET(0x00000000, 32, 0, 0x04C11DB7, 0xFFFFFFFF), // CRC-32/POSIX, test vector for "123456789": 0x765e7680
104  CRC_PRESET(0x00000000, 32, 0, 0x814141AB, 0x00000000), // CRC-32Q, test vector for "123456789": 0x3010bf7f
105  CRC_PRESET(0xFFFFFFFF, 32, 1, 0x04C11DB7, 0x00000000), // CRC-32/JAMCRC, test vector for "123456789": 0x340bc6d9
106  CRC_PRESET(0x00000000, 32, 0, 0x000000AF, 0x00000000), // CRC-32/XFER, test vector for "123456789": 0xbd0be338
107  CRC_PRESET(0x0000000000000000, 64, 0, 0x42F0E1EBA9EA3693, 0x0000000000000000), // CRC-64, check: 0x6c40df5f0b497347
108  CRC_PRESET(0x0000000000000000, 64, 0, 0x42F0E1EBA9EA3693, 0x0000000000000000), // CRC-64/ECMA-182, check: 0x6c40df5f0b497347
109  CRC_PRESET(0xFFFFFFFFFFFFFFFF, 64, 0, 0x42F0E1EBA9EA3693, 0xFFFFFFFFFFFFFFFF), // CRC-64/WE, check: 0x62ec59e3f1a4f00a
110  CRC_PRESET(0xFFFFFFFFFFFFFFFF, 64, 1, 0x42F0E1EBA9EA3693, 0xFFFFFFFFFFFFFFFF), // CRC-64/XZ, check: 0x995dc9bbdf1939fa
111  CRC_PRESET(0xFFFFFFFFFFFFFFFF, 64, 1, 0x000000000000001b, 0xFFFFFFFFFFFFFFFF), // CRC-64/ISO, check: 0xb90956c775a41001
112 };
113 
115  ctx->crc = crc_presets[preset].crc;
116  ctx->size = crc_presets[preset].size;
117  ctx->reflect = crc_presets[preset].reflect;
118  ctx->poly = crc_presets[preset].poly;
119  ctx->xout = crc_presets[preset].xout;
120 }
121 
123  if (!data || !size || preset >= CRC_PRESET_SIZE) {
124  return 0;
125  }
126  utcrc r;
127  RzCrc crcctx;
128  crc_init_preset(&crcctx, preset);
129  crc_update(&crcctx, data, size);
130  crc_final(&crcctx, &r);
131  return r;
132 }
lzma_index ** i
Definition: index.h:629
uint32_t preset
Definition: container.h:259
utcrc rz_hash_crc_preset(const ut8 *data, ut32 size, RzCrcPresets preset)
Definition: crca.c:122
#define CRC_PRESET(crc, size, reflect, poly, xout)
Definition: crca.c:56
void crc_final(RzCrc *ctx, utcrc *r)
Definition: crca.c:38
RzCrc crc_presets[]
Definition: crca.c:60
void crc_init_preset(RzCrc *ctx, RzCrcPresets preset)
Definition: crca.c:114
void crc_update(RzCrc *ctx, const ut8 *data, ut32 sz)
Definition: crca.c:16
void crc_init_custom(RzCrc *ctx, utcrc crc, ut32 size, int reflect, utcrc poly, utcrc xout)
Definition: crca.c:8
ut64 utcrc
Definition: crca.h:9
#define UTCRC_C(x)
Definition: crca.h:10
RzCrcPresets
Definition: crca.h:12
@ CRC_PRESET_SIZE
Definition: crca.h:65
#define r
Definition: crypto_rc6.c:12
uint32_t ut32
voidpf void uLong size
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
#define d(i)
Definition: sha256.c:44
Definition: crca.h:68
int reflect
Definition: crca.h:71
utcrc poly
Definition: crca.h:72
utcrc crc
Definition: crca.h:69
ut32 size
Definition: crca.h:70
utcrc xout
Definition: crca.h:73