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