summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libmusepack/crc32.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libmusepack/crc32.c')
-rw-r--r--lib/rbcodec/codecs/libmusepack/crc32.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libmusepack/crc32.c b/lib/rbcodec/codecs/libmusepack/crc32.c
new file mode 100644
index 0000000000..7613f1c2c8
--- /dev/null
+++ b/lib/rbcodec/codecs/libmusepack/crc32.c
@@ -0,0 +1,57 @@
1/*
2* C Implementation: crc32
3*
4* code from http://www.w3.org/TR/PNG/#D-CRCAppendix
5*
6*/
7#include "internal.h"
8
9/* Table of CRCs of all 8-bit messages. */
10static unsigned long crc_table[256];
11
12/* Flag: has the table been computed? Initially false. */
13static int crc_table_computed = 0;
14
15/* Make the table for a fast CRC. */
16static void make_crc_table(void)
17{
18 unsigned long c;
19 int n, k;
20
21 for (n = 0; n < 256; n++) {
22 c = (unsigned long) n;
23 for (k = 0; k < 8; k++) {
24 if (c & 1)
25 c = 0xedb88320L ^ (c >> 1);
26 else
27 c = c >> 1;
28 }
29 crc_table[n] = c;
30 }
31 crc_table_computed = 1;
32}
33
34
35/* Update a running CRC with the bytes buf[0..len-1]--the CRC
36 should be initialized to all 1's, and the transmitted value
37 is the 1's complement of the final running CRC (see the
38 crc() routine below). */
39
40static unsigned long update_crc(unsigned long crc, unsigned char *buf, int len)
41{
42 unsigned long c = crc;
43 int n;
44
45 if (!crc_table_computed)
46 make_crc_table();
47 for (n = 0; n < len; n++) {
48 c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
49 }
50 return c;
51}
52
53/* Return the CRC of the bytes buf[0..len-1]. */
54unsigned long mpc_crc32(unsigned char *buf, int len)
55{
56 return update_crc(0xffffffffL, buf, len) ^ 0xffffffffL;
57}