crc.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
32 #include <atlasmv_base/crc.h>
33 
34 
35 
36 /*******************************************************************\
37 * *
38 * Library : lib_crc *
39 * File : lib_crc.c *
40 * Author : Lammert Bies 1999-2008 *
41 * E-mail : info@lammertbies.nl *
42 * Language : ANSI C *
43 * *
44 * *
45 * Description *
46 * =========== *
47 * *
48 * The file lib_crc.c contains the private and public func- *
49 * tions used for the calculation of CRC-16, CRC-CCITT and *
50 * CRC-32 cyclic redundancy values. *
51 * *
52 * *
53 * Dependencies *
54 * ============ *
55 * *
56 * lib_crc.h CRC definitions and prototypes *
57 * *
58 * *
59 * Modification history *
60 * ==================== *
61 * *
62 * Date Version Comment *
63 * *
64 * 2008-04-20 1.16 Added CRC-CCITT calculation for Kermit *
65 * *
66 * 2007-04-01 1.15 Added CRC16 calculation for Modbus *
67 * *
68 * 2007-03-28 1.14 Added CRC16 routine for Sick devices *
69 * *
70 * 2005-12-17 1.13 Added CRC-CCITT with initial 0x1D0F *
71 * *
72 * 2005-05-14 1.12 Added CRC-CCITT with start value 0 *
73 * *
74 * 2005-02-05 1.11 Fixed bug in CRC-DNP routine *
75 * *
76 * 2005-02-04 1.10 Added CRC-DNP routines *
77 * *
78 * 1999-02-21 1.01 Added FALSE and TRUE mnemonics *
79 * *
80 * 1999-01-22 1.00 Initial source *
81 * *
82 \*******************************************************************/
83 
84 
85 
86 /*******************************************************************\
87 * *
88 * #define P_xxxx *
89 * *
90 * The CRC's are computed using polynomials. The coefficients *
91 * for the algorithms are defined by the following constants. *
92 * *
93 \*******************************************************************/
94 
95 #define P_16 0xA001
96 #define P_32 0xEDB88320L
97 #define P_CCITT 0x1021
98 #define P_DNP 0xA6BC
99 #define P_KERMIT 0x8408
100 #define P_SICK 0x8005
101 
102 
103 
104 /*******************************************************************\
105 * *
106 * static int crc_tab...init *
107 * static unsigned ... crc_tab...[] *
108 * *
109 * The algorithms use tables with precalculated values. This *
110 * speeds up the calculation dramaticaly. The first time the *
111 * CRC function is called, the table for that specific calcu- *
112 * lation is set up. The ...init variables are used to deter- *
113 * mine if the initialization has taken place. The calculated *
114 * values are stored in the crc_tab... arrays. *
115 * *
116 * The variables are declared static. This makes them invisi- *
117 * ble for other modules of the program. *
118 * *
119 \*******************************************************************/
120 
121 static int crc_tab16_init = FALSE;
122 static int crc_tab32_init = FALSE;
124 static int crc_tabdnp_init = FALSE;
126 
127 static unsigned short crc_tab16[256];
128 static unsigned long crc_tab32[256];
129 static unsigned short crc_tabccitt[256];
130 static unsigned short crc_tabdnp[256];
131 static unsigned short crc_tabkermit[256];
132 
133 
134 
135 /*******************************************************************\
136 * *
137 * static void init_crc...tab(); *
138 * *
139 * Three local functions are used to initialize the tables *
140 * with values for the algorithm. *
141 * *
142 \*******************************************************************/
143 
144 static void init_crc16_tab( void );
145 static void init_crc32_tab( void );
146 static void init_crcccitt_tab( void );
147 static void init_crcdnp_tab( void );
148 static void init_crckermit_tab( void );
149 
150 
151 
152 /*******************************************************************\
153 * *
154 * unsigned short update_crc_ccitt( unsigned long crc, char c ); *
155 * *
156 * The function update_crc_ccitt calculates a new CRC-CCITT *
157 * value based on the previous value of the CRC and the next *
158 * byte of the data to be checked. *
159 * *
160 \*******************************************************************/
161 
162 unsigned short update_crc_ccitt(unsigned short crc, unsigned char c) {
163 
164  unsigned short tmp, short_c;
165 
166  short_c = 0x00ff & (unsigned short) c;
167 
169 
170  tmp = (crc >> 8) ^ short_c;
171  crc = (crc << 8) ^ crc_tabccitt[tmp];
172 
173  return crc;
174 
175 } /* update_crc_ccitt */
176 
177 
178 
179 /*******************************************************************\
180 * *
181 * unsigned short update_crc_sick( *
182 * unsigned long crc, char c, char prev_byte ); *
183 * *
184 * The function update_crc_sick calculates a new CRC-SICK *
185 * value based on the previous value of the CRC and the next *
186 * byte of the data to be checked. *
187 * *
188 \*******************************************************************/
189 
190 unsigned short update_crc_sick( unsigned short crc, char c, char prev_byte ) {
191 
192  unsigned short short_c, short_p;
193 
194  short_c = 0x00ff & (unsigned short) c;
195  short_p = ( 0x00ff & (unsigned short) prev_byte ) << 8;
196 
197  if ( crc & 0x8000 ) crc = ( crc << 1 ) ^ P_SICK;
198  else crc = crc << 1;
199 
200  crc &= 0xffff;
201  crc ^= ( short_c | short_p );
202 
203  return crc;
204 
205 } /* update_crc_sick */
206 
207 
208 
209 /*******************************************************************\
210 * *
211 * unsigned short update_crc_16( unsigned short crc, char c ); *
212 * *
213 * The function update_crc_16 calculates a new CRC-16 value *
214 * based on the previous value of the CRC and the next byte *
215 * of the data to be checked. *
216 * *
217 \*******************************************************************/
218 
219 unsigned short update_crc_16( unsigned short crc, char c ) {
220 
221  unsigned short tmp, short_c;
222 
223  short_c = 0x00ff & (unsigned short) c;
224 
225  if ( ! crc_tab16_init ) init_crc16_tab();
226 
227  tmp = crc ^ short_c;
228  crc = (crc >> 8) ^ crc_tab16[ tmp & 0xff ];
229 
230  return crc;
231 
232 } /* update_crc_16 */
233 
234 
235 
236 /*******************************************************************\
237 * *
238 * unsigned short update_crc_kermit( unsigned short crc, char c ); *
239 * *
240 * The function update_crc_kermit calculates a new CRC value *
241 * based on the previous value of the CRC and the next byte *
242 * of the data to be checked. *
243 * *
244 \*******************************************************************/
245 
246 unsigned short update_crc_kermit( unsigned short crc, char c ) {
247 
248  unsigned short tmp, short_c;
249 
250  short_c = 0x00ff & (unsigned short) c;
251 
253 
254  tmp = crc ^ short_c;
255  crc = (crc >> 8) ^ crc_tabkermit[ tmp & 0xff ];
256 
257  return crc;
258 
259 } /* update_crc_kermit */
260 
261 
262 
263 /*******************************************************************\
264 * *
265 * unsigned short update_crc_dnp( unsigned short crc, char c ); *
266 * *
267 * The function update_crc_dnp calculates a new CRC-DNP value *
268 * based on the previous value of the CRC and the next byte *
269 * of the data to be checked. *
270 * *
271 \*******************************************************************/
272 
273 unsigned short update_crc_dnp( unsigned short crc, char c ) {
274 
275  unsigned short tmp, short_c;
276 
277  short_c = 0x00ff & (unsigned short) c;
278 
279  if ( ! crc_tabdnp_init ) init_crcdnp_tab();
280 
281  tmp = crc ^ short_c;
282  crc = (crc >> 8) ^ crc_tabdnp[ tmp & 0xff ];
283 
284  return crc;
285 
286 } /* update_crc_dnp */
287 
288 
289 
290 /*******************************************************************\
291 * *
292 * unsigned long update_crc_32( unsigned long crc, char c ); *
293 * *
294 * The function update_crc_32 calculates a new CRC-32 value *
295 * based on the previous value of the CRC and the next byte *
296 * of the data to be checked. *
297 * *
298 \*******************************************************************/
299 
300 unsigned long update_crc_32( unsigned long crc, char c ) {
301 
302  unsigned long tmp, long_c;
303 
304  long_c = 0x000000ffL & (unsigned long) c;
305 
306  if ( ! crc_tab32_init ) init_crc32_tab();
307 
308  tmp = crc ^ long_c;
309  crc = (crc >> 8) ^ crc_tab32[ tmp & 0xff ];
310 
311  return crc;
312 
313 } /* update_crc_32 */
314 
315 
316 
317 /*******************************************************************\
318 * *
319 * static void init_crc16_tab( void ); *
320 * *
321 * The function init_crc16_tab() is used to fill the array *
322 * for calculation of the CRC-16 with values. *
323 * *
324 \*******************************************************************/
325 
326 static void init_crc16_tab( void ) {
327 
328  int i, j;
329  unsigned short crc, c;
330 
331  for (i=0; i<256; i++) {
332 
333  crc = 0;
334  c = (unsigned short) i;
335 
336  for (j=0; j<8; j++) {
337 
338  if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ P_16;
339  else crc = crc >> 1;
340 
341  c = c >> 1;
342  }
343 
344  crc_tab16[i] = crc;
345  }
346 
348 
349 } /* init_crc16_tab */
350 
351 
352 
353 /*******************************************************************\
354 * *
355 * static void init_crckermit_tab( void ); *
356 * *
357 * The function init_crckermit_tab() is used to fill the array *
358 * for calculation of the CRC Kermit with values. *
359 * *
360 \*******************************************************************/
361 
362 static void init_crckermit_tab( void ) {
363 
364  int i, j;
365  unsigned short crc, c;
366 
367  for (i=0; i<256; i++) {
368 
369  crc = 0;
370  c = (unsigned short) i;
371 
372  for (j=0; j<8; j++) {
373 
374  if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ P_KERMIT;
375  else crc = crc >> 1;
376 
377  c = c >> 1;
378  }
379 
380  crc_tabkermit[i] = crc;
381  }
382 
384 
385 } /* init_crckermit_tab */
386 
387 
388 
389 /*******************************************************************\
390 * *
391 * static void init_crcdnp_tab( void ); *
392 * *
393 * The function init_crcdnp_tab() is used to fill the array *
394 * for calculation of the CRC-DNP with values. *
395 * *
396 \*******************************************************************/
397 
398 static void init_crcdnp_tab( void ) {
399 
400  int i, j;
401  unsigned short crc, c;
402 
403  for (i=0; i<256; i++) {
404 
405  crc = 0;
406  c = (unsigned short) i;
407 
408  for (j=0; j<8; j++) {
409 
410  if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ P_DNP;
411  else crc = crc >> 1;
412 
413  c = c >> 1;
414  }
415 
416  crc_tabdnp[i] = crc;
417  }
418 
420 
421 } /* init_crcdnp_tab */
422 
423 
424 
425 /*******************************************************************\
426 * *
427 * static void init_crc32_tab( void ); *
428 * *
429 * The function init_crc32_tab() is used to fill the array *
430 * for calculation of the CRC-32 with values. *
431 * *
432 \*******************************************************************/
433 
434 static void init_crc32_tab( void ) {
435 
436  int i, j;
437  unsigned long crc;
438 
439  for (i=0; i<256; i++) {
440 
441  crc = (unsigned long) i;
442 
443  for (j=0; j<8; j++) {
444 
445  if ( crc & 0x00000001L ) crc = ( crc >> 1 ) ^ P_32;
446  else crc = crc >> 1;
447  }
448 
449  crc_tab32[i] = crc;
450  }
451 
453 
454 } /* init_crc32_tab */
455 
456 
457 
458 /*******************************************************************\
459 * *
460 * static void init_crcccitt_tab( void ); *
461 * *
462 * The function init_crcccitt_tab() is used to fill the array *
463 * for calculation of the CRC-CCITT with values. *
464 * *
465 \*******************************************************************/
466 
467 static void init_crcccitt_tab( void ) {
468 
469  int i, j;
470  unsigned short crc, c;
471 
472  for (i=0; i<256; i++) {
473 
474  crc = 0;
475  c = ((unsigned short) i) << 8;
476 
477  for (j=0; j<8; j++) {
478 
479  if ( (crc ^ c) & 0x8000 ) crc = ( crc << 1 ) ^ P_CCITT;
480  else crc = crc << 1;
481 
482  c = c << 1;
483  }
484 
485  crc_tabccitt[i] = crc;
486  }
487 
489 
490 } /* init_crcccitt_tab */
static int crc_tab32_init
Definition: crc.cpp:122
static unsigned short crc_tab16[256]
Definition: crc.cpp:127
static unsigned short crc_tabkermit[256]
Definition: crc.cpp:131
unsigned short update_crc_sick(unsigned short crc, char c, char prev_byte)
Definition: crc.cpp:190
#define P_32
Definition: crc.cpp:96
static int crc_tab16_init
Definition: crc.cpp:121
#define P_SICK
Definition: crc.cpp:100
header for this library. Defines public funtions prototypes this library makes available to other mod...
unsigned short update_crc_dnp(unsigned short crc, char c)
Definition: crc.cpp:273
#define P_DNP
Definition: crc.cpp:98
unsigned short update_crc_kermit(unsigned short crc, char c)
Definition: crc.cpp:246
#define P_16
Definition: crc.cpp:95
static void init_crckermit_tab(void)
Definition: crc.cpp:362
static int crc_tabdnp_init
Definition: crc.cpp:124
unsigned long update_crc_32(unsigned long crc, char c)
Definition: crc.cpp:300
#define P_CCITT
Definition: crc.cpp:97
unsigned short update_crc_ccitt(unsigned short crc, unsigned char c)
Definition: crc.cpp:162
static void init_crcdnp_tab(void)
Definition: crc.cpp:398
#define TRUE
Definition: crc.h:74
static void init_crcccitt_tab(void)
Definition: crc.cpp:467
static int crc_tabccitt_init
Definition: crc.cpp:123
static unsigned long crc_tab32[256]
Definition: crc.cpp:128
static void init_crc16_tab(void)
Definition: crc.cpp:326
static unsigned short crc_tabdnp[256]
Definition: crc.cpp:130
static void init_crc32_tab(void)
Definition: crc.cpp:434
static unsigned short crc_tabccitt[256]
Definition: crc.cpp:129
unsigned short update_crc_16(unsigned short crc, char c)
Definition: crc.cpp:219
#define FALSE
Definition: crc.h:73
#define P_KERMIT
Definition: crc.cpp:99
static int crc_tabkermit_init
Definition: crc.cpp:125


atlasmv_base
Author(s): David Gameiro, Jorge Almeida
autogenerated on Mon Mar 2 2015 01:31:28