C2A_Core
crc.c
[詳解]
1 #pragma section REPRO
6 #include "crc.h"
7 
8 static const uint16_t CRC_kCrc16IbmRightTable_[256] = {
9  0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
10  0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
11  0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
12  0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
13  0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
14  0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
15  0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
16  0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
17  0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
18  0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
19  0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
20  0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
21  0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
22  0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
23  0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
24  0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
25  0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
26  0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
27  0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
28  0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
29  0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
30  0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
31  0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
32  0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
33  0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
34  0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
35  0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
36  0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
37  0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
38  0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
39  0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
40  0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
41 };
42 
43 static const uint16_t CRC_kCrc16CcittLeftTable_[256] = {
44  0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
45  0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
46  0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
47  0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
48  0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
49  0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
50  0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
51  0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
52  0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
53  0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
54  0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
55  0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
56  0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
57  0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
58  0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
59  0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
60  0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
61  0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
62  0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
63  0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
64  0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
65  0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
66  0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
67  0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
68  0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
69  0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
70  0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
71  0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
72  0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
73  0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
74  0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
75  0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
76 };
77 
78 static const uint16_t CRC_kCrc16CcittRightTable_[256] = {
79  0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
80  0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
81  0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
82  0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
83  0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
84  0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
85  0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
86  0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
87  0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
88  0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
89  0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
90  0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
91  0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
92  0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
93  0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
94  0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
95  0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
96  0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
97  0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
98  0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
99  0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
100  0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
101  0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
102  0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
103  0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
104  0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
105  0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
106  0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
107  0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
108  0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
109  0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
110  0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
111 };
112 
113 uint16_t CRC_calc_crc_16_ibm_right(uint16_t crc, const uint8_t* c, size_t n, int rev_flag)
114 {
115  size_t i;
116 
117  for (i = 0; i < n; ++i)
118  {
119  crc = (uint16_t)(CRC_kCrc16IbmRightTable_[(crc ^ c[i]) & 0xff] ^ (crc >> 8));
120  }
121 
122  if (rev_flag) return (uint16_t)~crc;
123  else return crc;
124 }
125 
126 uint16_t CRC_calc_crc_16_ccitt_left(uint16_t crc, const uint8_t* c, size_t n, int rev_flag)
127 {
128  size_t i;
129 
130  for (i = 0; i < n; ++i)
131  {
132  crc = (uint16_t)(CRC_kCrc16CcittLeftTable_[((crc >> 8) ^ c[i]) & 0xff] ^ (crc << 8));
133  }
134 
135  if (rev_flag) return (uint16_t)~crc;
136  else return crc;
137 }
138 
139 uint16_t CRC_calc_crc_16_ccitt_right(uint16_t crc, const uint8_t* c, size_t n, int rev_flag)
140 {
141  size_t i;
142 
143  for (i = 0; i < n; ++i)
144  {
145  crc = (uint16_t)(CRC_kCrc16CcittRightTable_[(crc ^ c[i]) & 0xff] ^ (crc >> 8));
146  }
147 
148  if (rev_flag) return (uint16_t)~crc;
149  else return crc;
150 }
151 
152 void CRC_make_crc_8_table(uint8_t* table, uint8_t crc_poly, uint8_t shift)
153 {
154  int i, j;
155 
156  for (i = 0; i < 256; ++i)
157  {
158  uint8_t c = (uint8_t)i;
159  for (j = 0; j < 8; ++j)
160  {
161  if (shift)
162  {
163  if (c & 1) c = (uint8_t)(crc_poly ^ (c >> 1));
164  else c >>= 1;
165  }
166  else
167  {
168  if (c & 0x80) c = (uint8_t)(crc_poly ^ (c << 1));
169  else c <<= 1;
170  }
171  }
172  table[i] = c;
173  }
174 }
175 
176 void CRC_make_crc_16_table(uint16_t* table, uint16_t crc_poly, uint8_t shift)
177 {
178  int i, j;
179 
180  for (i = 0; i < 256; ++i)
181  {
182  uint16_t c = (uint16_t)i;
183  if (!shift) c <<= 8;
184  for (j = 0; j < 8; ++j)
185  {
186  if (shift)
187  {
188  if (c & 1) c = (uint16_t)(crc_poly ^ (c >> 1));
189  else c >>= 1;
190  }
191  else
192  {
193  if (c & 0x8000) c = (uint16_t)(crc_poly ^ (c << 1));
194  else c <<= 1;
195  }
196  }
197  table[i] = c;
198  }
199 }
200 
201 void CRC_make_crc_32_table(uint32_t* table, uint32_t crc_poly, uint8_t shift)
202 {
203  int i, j;
204 
205  for (i = 0; i < 255; ++i)
206  {
207  uint32_t c = (uint32_t)i;
208  if (!shift) c <<= 24;
209  for (j = 0; j < 8; ++j)
210  {
211  if (shift)
212  {
213  if (c & 1) c = crc_poly ^ (c >> 1);
214  else c >>= 1;
215  }
216  else
217  {
218  if (c & 0x80000000) c = crc_poly ^ (c << 1);
219  else c <<= 1;
220  }
221  }
222  table[i] = c;
223  }
224 }
225 
226 /*
227 uint16_t CRC_calc_crc_16_right(uint16_t crc, uint16_t crc_poly, const uint8_t* c, size_t n, int rev_flag)
228 {
229  size_t i;
230  uint8_t j;
231  for (i = 0; i < n; ++i)
232  {
233  crc ^= c[i];
234  for (j = 0; j < 8; ++j)
235  {
236  if (crc & 1) crc = (crc >> 1) ^ crc_poly;
237  else crc >>= 1;
238  }
239  }
240 
241  if (rev_flag) return (uint16_t)~crc;
242  else return crc;
243 }
244 
245 uint16_t CRC_calc_crc_16_left(uint16_t crc, uint16_t crc_poly, const uint8_t* c, size_t n, int rev_flag)
246 {
247  size_t i;
248  uint8_t j;
249  for (i = 0; i < n; ++i)
250  {
251  crc ^= (c[i] << 8);
252  for (j = 0; j < 8; ++j)
253  {
254  if (crc & 0x8000) crc = (crc << 1) ^ crc_poly;
255  else crc <<= 1;
256  }
257  }
258 
259  if (rev_flag) return (uint16_t)~crc;
260  else return crc;
261 }
262 */
263 
264 #pragma section
void CRC_make_crc_8_table(uint8_t *table, uint8_t crc_poly, uint8_t shift)
crc用のテーブル作成
Definition: crc.c:152
static const uint16_t CRC_kCrc16CcittLeftTable_[256]
Definition: crc.c:43
static const uint16_t CRC_kCrc16IbmRightTable_[256]
Definition: crc.c:8
uint16_t CRC_calc_crc_16_ccitt_left(uint16_t crc, const uint8_t *c, size_t n, int rev_flag)
CRC-16-CCITT
Definition: crc.c:126
uint16_t CRC_calc_crc_16_ibm_right(uint16_t crc, const uint8_t *c, size_t n, int rev_flag)
CRC-16-IBM
Definition: crc.c:113
void CRC_make_crc_16_table(uint16_t *table, uint16_t crc_poly, uint8_t shift)
Definition: crc.c:176
uint16_t CRC_calc_crc_16_ccitt_right(uint16_t crc, const uint8_t *c, size_t n, int rev_flag)
CRC-16-CCITT
Definition: crc.c:139
void CRC_make_crc_32_table(uint32_t *table, uint32_t crc_poly, uint8_t shift)
Definition: crc.c:201
static const uint16_t CRC_kCrc16CcittRightTable_[256]
Definition: crc.c:78
CRC用のライブラリ