crc32.c

filter: Statementstmt 100% 26 / 26 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 100% 34 / 34 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/File/Raw/Hash.pm ↑ Summary
1 /*
2 * crc32.c - IEEE 802.3 reflected CRC-32 implementation.
3 *
4 * Table-driven, computed lazily on first use. Table holds 256 entries
5 * = 1 KiB of static data. Polynomial 0xEDB88320.
6 */
7  
8 #include "crc32.h"
9  
10 static uint32_t crc32_table[256];
11 static int crc32_table_built = 0;
12  
13 static void
14 7 crc32_build_table(void)
15 {
16 uint32_t c;
17 int i, j;
18 1799 ▶ for (i = 0; i < 256; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 256; i++) { 1792 7 1799
19 1792 c = (uint32_t)i;
20 16128 ▶ for (j = 0; j < 8; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 8; j++) { 14336 1792 16128
21 14336 c = (c & 1u) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1);
22 }
23 1792 crc32_table[i] = c;
24 }
25 7 crc32_table_built = 1;
26 7 }
27  
28 void
29 17 crc32_init(crc32_ctx_t *ctx)
30 {
31 17 ▶ if (!crc32_table_built) crc32_build_table();
Branch: 1 branch
BranchTrueFalseTotal
if (!crc32_table_built) crc32_build_table(); 7 10 17
32 17 ctx->state = 0xFFFFFFFFu;
33 17 }
34  
35 void
36 16 crc32_update(crc32_ctx_t *ctx, const void *data, size_t len)
37 {
38 16 const unsigned char *p = (const unsigned char *)data;
39 16 uint32_t c = ctx->state;
40 size_t i;
41 642927 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 642911 16 642927
42 642911 c = crc32_table[(c ^ p[i]) & 0xFFu] ^ (c >> 8);
43 }
44 16 ctx->state = c;
45 16 }
46  
47 void
48 17 crc32_final(crc32_ctx_t *ctx, unsigned char out[CRC32_DIGEST_SIZE])
49 {
50 17 uint32_t c = ctx->state ^ 0xFFFFFFFFu;
51 /* Big-endian output: matches the conventional "0xCBF43926" reading
52 * for the "123456789" test vector. */
53 17 out[0] = (unsigned char)((c >> 24) & 0xFFu);
54 17 out[1] = (unsigned char)((c >> 16) & 0xFFu);
55 17 out[2] = (unsigned char)((c >> 8) & 0xFFu);
56 17 out[3] = (unsigned char)( c & 0xFFu);
57 17 }
« blib/lib/File/Raw/Hash.pm ↑ Summary