KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
checksum.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : checksum.c
11  * Created : 18 feb. 2014
12  * Author : Vincent van Beveren
13  */
14 #include "util/checksum.h"
15 
16 
17 // ADLER32 has been adapted from Linux net/sctp/sctp.h,
18 
19 #define ADLER32_BASE 65521 /* largest prime smaller than 65536 */
20 
21 
22 
23 uint32_t adler32(uint32_t adler, const uint8_t *buf, int len)
24 {
25  // initialize the two totals
26  uint32_t s1 = adler & 0xffff;
27  uint32_t s2 = (adler >> 16) & 0xffff;
28  int n;
29 
30  for (n = 0; n < len; ++n, ++buf) {
31  // add to the first total
32  s1 = (s1 + *buf);
33  // wrap if required
34  if (s1 >= ADLER32_BASE) s1 -= ADLER32_BASE;
35 
36  // add to the second total
37  s2 = (s2 + s1);
38  // wrap if required
39  if (s2 >= ADLER32_BASE) s2 -= ADLER32_BASE;
40  }
41  // return the adler32 checksum
42  return (s2 << 16) + s1;
43 }
44 
45 /* CRC-32, eg. ethernet */
46 
47 // Copied from
48 // https://gist.github.com/notwa/5689243
49 // Thanks!
50 static const uint32_t crc32_tbl[] = {
51  0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
52  0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
53  0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
54  0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
55 };
56 
57 uint32_t crc32(uint32_t crc, const uint8_t *ptr, int cnt, bool dbg)
58 {
59  while (cnt--) {
60  if (dbg) printf("%02x ", *ptr);
61 
62  crc = (crc >> 4) ^ crc32_tbl[(crc & 0xf) ^ (*ptr & 0xf)];
63  crc = (crc >> 4) ^ crc32_tbl[(crc & 0xf) ^ (*(ptr++) >> 4)];
64  }
65  if (dbg) puts("");
66  return crc;
67 }
68 
uint32_t adler32(uint32_t adler, const uint8_t *buf, int len)
Update the adler32 checkum with the new data.
Definition: checksum.c:23
uint32_t crc32(uint32_t crc, const uint8_t *ptr, int cnt, bool dbg)
Lightweight CRC32 algorithm, using Ethernet polynomial.
Definition: checksum.c:57
This module provides checksum functions.