librnr  1.14.5
RoadNarrows Robotics Common Library 1
checksum.c File Reference

Checksum algorithms. More...

#include <sys/types.h>
#include "rnr/rnrconfig.h"

Go to the source code of this file.

Functions

u8_t generate_checksum8 (byte_t buf[], size_t len)
 Computes the modular 8-bit checksum over buffer. More...
 
u16_t generate_checksum16 (byte_t buf[], size_t len)
 Computes the modular 16-bit checksum over buffer. More...
 
u32_t generate_checksum32 (byte_t buf[], size_t len)
 Computes the modular 32-bit checksum over buffer. More...
 
u32_t generate_crc32 (byte_t buf[], size_t len)
 Computes the 32-bit cyclic redundance check over buffer. More...
 

Variables

static u32_t crc32tbl [256]
 CRC-32 table generated from CRC32_POLY_NORM polygon.
 

Detailed Description

Checksum algorithms.

Author
Daniel Packard (danie.nosp@m.l@ro.nosp@m.adnar.nosp@m.rows.nosp@m..com)
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)
EULA
See the README and EULA files for any copyright and licensing information.

Definition in file checksum.c.

Function Documentation

u16_t generate_checksum16 ( byte_t  buf[],
size_t  len 
)

Computes the modular 16-bit checksum over buffer.

The two's complitment is not taken.

Parameters
bufBuffer to checksum.
lenNumber of bytes in buf.
Returns
16-bit sum of bytes, discarding overflow.

Definition at line 113 of file checksum.c.

Referenced by profile().

114 {
115  u16_t sum = 0;
116  size_t i;
117 
118  for(i=0; i<len; ++i)
119  {
120  sum = (u16_t)(sum + (u16_t)buf[i]);
121  }
122 
123  return sum;
124 }
__uint16_t u16_t
16-bit unsigned integer
Definition: rnrconfig.h:168
u32_t generate_checksum32 ( byte_t  buf[],
size_t  len 
)

Computes the modular 32-bit checksum over buffer.

The two's complitment is not taken.

Parameters
bufBuffer to checksum
lenNumber of bytes in buf.
Returns
32-bit sum of bytes, discarding overflow.

Definition at line 126 of file checksum.c.

Referenced by profile().

127 {
128  u32_t sum = 0;
129  size_t i;
130 
131  for(i=0; i<len; ++i)
132  {
133  sum += (u32_t)buf[i];
134  }
135 
136  return sum;
137 }
__uint32_t u32_t
32-bit unsigned integer
Definition: rnrconfig.h:170
u8_t generate_checksum8 ( byte_t  buf[],
size_t  len 
)

Computes the modular 8-bit checksum over buffer.

The two's complitment is not taken.

Parameters
bufBuffer to checksum.
lenNumber of bytes in buf.
Returns
8-bit sum of bytes, discarding overflow.

Definition at line 100 of file checksum.c.

Referenced by profile().

101 {
102  u8_t sum = 0;
103  size_t i;
104 
105  for(i=0; i<len; ++i)
106  {
107  sum = (u8_t)(sum + (u8_t)buf[i]);
108  }
109 
110  return sum;
111 }
__uint8_t u8_t
8-bit unsigned integer
Definition: rnrconfig.h:166
u32_t generate_crc32 ( byte_t  buf[],
size_t  len 
)

Computes the 32-bit cyclic redundance check over buffer.

CRC polynomial: 0x04C11DB7 (normal representation).

This CRC-32 is used by Ethernet, MPEG-2, gzip, gzip2, and others.

This CRC can be specified as:
width 32
poly 0x04c11db7
init 0xffffffff
refin false
refout false
xorout true
Parameters
bufBuffer to checksum
lenNumber of bytes in buf.
Returns
32-bit CRC

Definition at line 139 of file checksum.c.

Referenced by profile().

140 {
141  u32_t crc = 0xffffffff;
142  size_t i;
143 
144  for(i=0; i<len; ++i)
145  {
146  crc = (crc << 8) ^ crc32tbl[((crc >> 24) ^ buf[i]) & 0xff];
147  }
148 
149  return crc ^ 0xffffffff;
150 }
__uint32_t u32_t
32-bit unsigned integer
Definition: rnrconfig.h:170