Debian Codegraph @

common/crc.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
4 *
5 * CRC functions.
6 */
7
8 #include "dynamips_common.h"
9
10 #define CRC12_POLY 0x0f01
11 #define CRC16_POLY 0xa001
12 #define CRC32_POLY 0xedb88320L
13
14 /* CRC tables */
15 m_uint16_t crc12_array[256],crc16_array[256];
16 m_uint32_t crc32_array[256];
17
18 /* Initialize CRC-12 algorithm */
19 static void crc12_init(void)
20 {
21 m_uint16_t crc,c;
22 int i,j;
23
24 for(i=0;i<256;i++) {
25 crc = 0;
26 c = (m_uint16_t)i;
27
28 for(j=0;j<8;j++) {
29 if ((crc ^ c) & 0x0001)
30 crc = (crc >> 1) ^ CRC12_POLY;
31 else
32 crc = crc >> 1;
33
34 c = c >> 1;
35 }
36
37 crc12_array[i] = crc;
38 }
39 }
40
41 /* Initialize CRC-16 algorithm */
42 static void crc16_init(void)
43 {
44 m_uint16_t crc,c;
45 int i,j;
46
47 for(i=0;i<256;i++) {
48 crc = 0;
49 c = (m_uint16_t)i;
50
51 for(j=0;j<8;j++) {
52 if ((crc ^ c) & 0x0001)
53 crc = (crc >> 1) ^ CRC16_POLY;
54 else
55 crc = crc >> 1;
56
57 c = c >> 1;
58 }
59
60 crc16_array[i] = crc;
61 }
62 }
63
64 /* Initialize CRC-32 algorithm */
65 static void crc32_init(void)
66 {
67 unsigned long c;
68 int n, k;
69
70 for (n=0;n<256;n++) {
71 c = (unsigned long) n;
72 for (k = 0; k < 8; k++) {
73 if (c & 1)
74 c = CRC32_POLY ^ (c >> 1);
75 else
76 c = c >> 1;
77 }
78 crc32_array[n] = c;
79 }
80 }
81
82 /* Initialize CRC algorithms */
83 void crc_init(void)
84 {
85 crc12_init();
86 crc16_init();
87 crc32_init();
88 }