File: crc32.c

package info (click to toggle)
e-uae 0.8.29-WIP4-10
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze, wheezy
  • size: 7,164 kB
  • ctags: 11,154
  • sloc: ansic: 96,763; sh: 3,353; cpp: 1,471; asm: 974; objc: 611; makefile: 474; perl: 216
file content (50 lines) | stat: -rwxr-xr-x 961 bytes parent folder | download | duplicates (2)
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

#include "sysconfig.h"
#include "sysdeps.h"

#include "crc32.h"

static uae_u32 crc_table32[256];
static uae_u16 crc_table16[256];

static void make_crc_table (void)
{
    uae_u32 c;
    uae_u16 w;
    unsigned int n, k;
    for (n = 0; n < 256; n++) {
	c = (uae_u32) n;
	w = n << 8;
	for (k = 0; k < 8; k++) {
	    c = (c >> 1) ^ (c & 1 ? 0xedb88320 : 0);
	    w = (w << 1) ^ ((w & 0x8000) ? 0x1021 : 0);
	}
	crc_table32[n] = c;
	crc_table16[n] = w;
    }
}

uae_u32 get_crc32 (const uae_u8 *buf, unsigned int len)
{
    uae_u32 crc;

    if (!crc_table32[1])
	make_crc_table ();

    crc = 0xffffffff;
    while (len-- > 0)
	crc = crc_table32[(crc ^ (*buf++)) & 0xff] ^ (crc >> 8);
    return crc ^ 0xffffffff;
}

uae_u16 get_crc16 (const uae_u8 *buf, unsigned int len)
{
    uae_u16 crc;
    if (!crc_table32[1])
	make_crc_table ();

    crc = 0xffff;
    while (len-- > 0)
	crc = (crc << 8) ^ crc_table16[((crc >> 8) ^ (*buf++)) & 0xff];
    return crc;
}