-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathjtr_crc32.c
More file actions
83 lines (67 loc) · 1.61 KB
/
jtr_crc32.c
File metadata and controls
83 lines (67 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* This is a tiny implementation of CRC-32.
*
* This software was written by Solar Designer in 1998 and revised in 2005.
* No copyright is claimed, and the software is hereby placed in the public
* domain.
* In case this attempt to disclaim copyright and place the software in the
* public domain is deemed null and void, then the software is
* Copyright (c) 1998,2005 by Solar Designer and it is hereby released to the
* general public under the following terms:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*
* (This is a heavily cut-down "BSD license".)
*/
#include <stdio.h>
#include "jtr_crc32.h"
#define POLY 0xEDB88320
#define ALL1 0xFFFFFFFF
static CRC32_t table[256];
void CRC32_Init(CRC32_t *value)
{
*value = ALL1;
}
static int bInit = 0;
void CRC32_Table_Init()
{
unsigned int index, bit;
CRC32_t entry;
if (bInit) return;
bInit = 1;
for (index = 0; index < 0x100; index++) {
entry = index;
for (bit = 0; bit < 8; bit++)
if (entry & 1) {
entry >>= 1;
entry ^= POLY;
} else
entry >>= 1;
table[index] = entry;
}
}
void CRC32_Update(CRC32_t *value, void *data, unsigned int size)
{
unsigned char *ptr;
unsigned int count;
CRC32_t result;
result = *value;
ptr = data;
count = size;
if (count)
do {
result = (result >> 8) ^ table[(result ^ *ptr++) & 0xFF];
} while (--count);
*value = result;
}
void CRC32_Final(unsigned char *out, CRC32_t value)
{
value = ~value;
out[0] = value;
out[1] = value >> 8;
out[2] = value >> 16;
out[3] = value >> 24;
}