GNUnet  0.10.x
Macros | Functions | Variables
crypto_crc.c File Reference

implementation of CRC16 and CRC32 More...

#include "platform.h"
#include "gnunet_crypto_lib.h"
Include dependency graph for crypto_crc.c:

Go to the source code of this file.

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-crypto-crc", __VA_ARGS__)
 
#define Z_NULL   0
 
#define POLYNOMIAL   (GNUNET_uLong)0xedb88320
 

Functions

static void crc_init ()
 
static GNUNET_uLong crc32 (GNUNET_uLong crc, const char *buf, size_t len)
 
int32_t GNUNET_CRYPTO_crc32_n (const void *buf, size_t len)
 Compute the CRC32 checksum for the first len bytes of the buffer. More...
 
uint32_t GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len)
 Perform an incremental step in a CRC16 (for TCP/IP) calculation. More...
 
uint16_t GNUNET_CRYPTO_crc16_finish (uint32_t sum)
 Convert results from GNUNET_CRYPTO_crc16_step() to final crc16. More...
 
uint16_t GNUNET_CRYPTO_crc16_n (const void *buf, size_t len)
 Calculate the checksum of a buffer in one step. More...
 
uint8_t GNUNET_CRYPTO_crc8_n (const void *buf, size_t len)
 Calculate the checksum of a buffer in one step. More...
 

Variables

static GNUNET_uLong crc_table [256]
 

Detailed Description

implementation of CRC16 and CRC32

Author
Christian Grothoff

Definition in file crypto_crc.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-crypto-crc", __VA_ARGS__)

Definition at line 33 of file crypto_crc.c.

◆ Z_NULL

#define Z_NULL   0

Definition at line 44 of file crypto_crc.c.

Referenced by GNUNET_CRYPTO_crc32_n().

◆ POLYNOMIAL

#define POLYNOMIAL   (GNUNET_uLong)0xedb88320

Definition at line 47 of file crypto_crc.c.

Referenced by crc_init().

Function Documentation

◆ crc_init()

static void crc_init ( )
static

Definition at line 56 of file crypto_crc.c.

References crc_table, h, once, and POLYNOMIAL.

Referenced by crc32().

57 {
58  static int once;
59  unsigned int i, j;
60  GNUNET_uLong h = 1;
61 
62  if (once)
63  return;
64  once = 1;
65  crc_table[0] = 0;
66  for (i = 128; i; i >>= 1)
67  {
68  h = (h >> 1) ^ ((h & 1) ? POLYNOMIAL : 0);
69  /* h is now crc_table[i] */
70  for (j = 0; j < 256; j += 2 * i)
71  crc_table[i + j] = crc_table[j] ^ h;
72  }
73 }
#define POLYNOMIAL
Definition: crypto_crc.c:47
static GNUNET_uLong crc_table[256]
Definition: crypto_crc.c:48
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
static int once
Global to mark if we&#39;ve run the initialization.
Definition: gnsrecord.c:68
Here is the caller graph for this function:

◆ crc32()

static GNUNET_uLong crc32 ( GNUNET_uLong  crc,
const char *  buf,
size_t  len 
)
static

Definition at line 86 of file crypto_crc.c.

References crc_init(), crc_table, and GNUNET_assert.

Referenced by GNUNET_CRYPTO_crc32_n().

87 {
88  crc_init ();
89  GNUNET_assert (crc_table[255] != 0);
90  crc ^= 0xffffffff;
91  while (len--)
92  crc = (crc >> 8) ^ crc_table[(crc ^ *buf++) & 0xff];
93  return crc ^ 0xffffffff;
94 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void crc_init()
Definition: crypto_crc.c:56
static GNUNET_uLong crc_table[256]
Definition: crypto_crc.c:48
static char buf[2048]
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_crc16_step()

uint32_t GNUNET_CRYPTO_crc16_step ( uint32_t  sum,
const void *  buf,
size_t  len 
)

Perform an incremental step in a CRC16 (for TCP/IP) calculation.

Parameters
sumcurrent sum, initially 0
bufbuffer to calculate CRC over (must be 16-bit aligned)
lennumber of bytes in hdr, must be multiple of 2
Returns
updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish() to get actual crc16)

Definition at line 124 of file crypto_crc.c.

References buf.

Referenced by GNUNET_CRYPTO_crc16_n(), GNUNET_TUN_calculate_icmp_checksum(), GNUNET_TUN_calculate_tcp4_checksum(), GNUNET_TUN_calculate_tcp6_checksum(), GNUNET_TUN_calculate_udp4_checksum(), and GNUNET_TUN_calculate_udp6_checksum().

125 {
126  const uint16_t *hdr = buf;
127  for (; len >= 2; len -= 2)
128  sum += *(hdr++);
129  if (len == 1)
130  sum += (*hdr) & ntohs(0xFF00);
131  return sum;
132 }
static char buf[2048]
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_crc16_finish()

uint16_t GNUNET_CRYPTO_crc16_finish ( uint32_t  sum)

Convert results from GNUNET_CRYPTO_crc16_step() to final crc16.

Convert results from GNUNET_CRYPTO_crc16_step to final crc16.

Parameters
sumcummulative sum
Returns
crc16 value

Definition at line 142 of file crypto_crc.c.

Referenced by GNUNET_CRYPTO_crc16_n(), GNUNET_TUN_calculate_icmp_checksum(), GNUNET_TUN_calculate_tcp4_checksum(), GNUNET_TUN_calculate_tcp6_checksum(), GNUNET_TUN_calculate_udp4_checksum(), and GNUNET_TUN_calculate_udp6_checksum().

143 {
144  sum = (sum >> 16) + (sum & 0xFFFF);
145  sum += (sum >> 16);
146 
147  return ~sum;
148 }
Here is the caller graph for this function:

Variable Documentation

◆ crc_table

GNUNET_uLong crc_table[256]
static

Definition at line 48 of file crypto_crc.c.

Referenced by crc32(), and crc_init().