ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / Soft / Lib / utils / ardrone_crc_32.c
1 /**
2  * Based on PNG CRC implementation
3  */
4
5 #include <utils/ardrone_crc_32.h>
6
7 /* Table of CRCs of all 8-bit messages. */
8 uint32_t crc_table[256];
9
10 /* Flag: has the table been computed? Initially false. */
11 int32_t crc_table_computed = 0;
12
13 /* Make the table for a fast CRC. */
14 void ardrone_make_crc_table(void)
15 {
16   uint32_t c;
17   int32_t n, k;
18    
19   for (n = 0; n < 256; n++)
20     {
21       c = n;
22       for (k = 0; k < 8; k++)
23         {
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 /* Update a running CRC with the bytes buf[0..len-1]--the CRC
35    should be initialized to all 1's, and the transmitted value
36    is the 1's complement of the final running CRC (see the
37    crc() routine below). */
38    
39 uint32_t ardrone_update_crc(uint32_t crc, const uint8_t *buf,
40                          int32_t len)
41 {
42   uint32_t c = crc;
43   int32_t n;
44    
45   if (!crc_table_computed)
46     ardrone_make_crc_table();
47   for (n = 0; n < len; n++)
48     {
49       c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
50     }
51   return c;
52 }
53    
54 /* Return the CRC of the bytes buf[0..len-1]. */
55 uint32_t ardrone_crc_32(const uint8_t *buf, int32_t len)
56 {
57   return ardrone_update_crc(0xffffffffL, buf, len) ^ 0xffffffffL;
58 }