Rizin
unix-like reverse engineering framework and cli tools
crca.c File Reference
#include "crca.h"

Go to the source code of this file.

Macros

#define CRC_PRESET(crc, size, reflect, poly, xout)    { UTCRC_C(crc), (size), (reflect), UTCRC_C(poly), UTCRC_C(xout) }
 

Functions

void crc_init_custom (RzCrc *ctx, utcrc crc, ut32 size, int reflect, utcrc poly, utcrc xout)
 
void crc_update (RzCrc *ctx, const ut8 *data, ut32 sz)
 
void crc_final (RzCrc *ctx, utcrc *r)
 
void crc_init_preset (RzCrc *ctx, RzCrcPresets preset)
 
utcrc rz_hash_crc_preset (const ut8 *data, ut32 size, RzCrcPresets preset)
 

Variables

RzCrc crc_presets []
 

Macro Definition Documentation

◆ CRC_PRESET

#define CRC_PRESET (   crc,
  size,
  reflect,
  poly,
  xout 
)     { UTCRC_C(crc), (size), (reflect), UTCRC_C(poly), UTCRC_C(xout) }

Definition at line 56 of file crca.c.

Function Documentation

◆ crc_final()

void crc_final ( RzCrc ctx,
utcrc r 
)

Definition at line 38 of file crca.c.

38  {
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 }
lzma_index ** i
Definition: index.h:629
ut64 utcrc
Definition: crca.h:9
#define UTCRC_C(x)
Definition: crca.h:10
#define r
Definition: crypto_rc6.c:12

References i, r, and UTCRC_C.

Referenced by plugin_crca_final(), and rz_hash_crc_preset().

◆ crc_init_custom()

void crc_init_custom ( RzCrc ctx,
utcrc  crc,
ut32  size,
int  reflect,
utcrc  poly,
utcrc  xout 
)

Definition at line 8 of file crca.c.

8  {
9  ctx->crc = crc;
10  ctx->size = size;
11  ctx->reflect = reflect;
12  ctx->poly = poly;
13  ctx->xout = xout;
14 }
voidpf void uLong size
Definition: ioapi.h:138

◆ crc_init_preset()

void crc_init_preset ( RzCrc ctx,
RzCrcPresets  preset 
)

Definition at line 114 of file crca.c.

114  {
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 }
uint32_t preset
Definition: container.h:259
RzCrc crc_presets[]
Definition: crca.c:60
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

References RzCrc::crc, crc_presets, RzCrc::poly, preset, RzCrc::reflect, RzCrc::size, and RzCrc::xout.

Referenced by rz_hash_crc_preset().

◆ crc_update()

void crc_update ( RzCrc ctx,
const ut8 data,
ut32  sz 
)

Definition at line 16 of file crca.c.

16  {
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 }
#define d(i)
Definition: sha256.c:44

References d, and i.

Referenced by plugin_crca_update(), and rz_hash_crc_preset().

◆ rz_hash_crc_preset()

utcrc rz_hash_crc_preset ( const ut8 data,
ut32  size,
RzCrcPresets  preset 
)

Definition at line 122 of file crca.c.

122  {
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 }
void crc_final(RzCrc *ctx, utcrc *r)
Definition: crca.c:38
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
@ CRC_PRESET_SIZE
Definition: crca.h:65
Definition: crca.h:68

References crc_final(), crc_init_preset(), CRC_PRESET_SIZE, crc_update(), preset, and r.

Variable Documentation

◆ crc_presets

RzCrc crc_presets[]

Definition at line 60 of file crca.c.

Referenced by crc_init_preset().