-fix time assertion introduce in last patch
[oweals/gnunet.git] / src / util / crypto_crc.c
index c6cd83a7494d65d363045758e6c6fcb56e8e8eb8..db0b5bf63880099dbe6c7318a526c49835f11d37 100644 (file)
@@ -4,7 +4,7 @@
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
  * @brief implementation of CRC16 and CRC32
  * @author Christian Grothoff
  */
-
 #include "platform.h"
-#include "gnunet_common.h"
-#include "gnunet_crypto_lib.h"
+#include "gnunet_util_lib.h"
 
 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
 
 /* Avoid wasting space on 8-byte longs. */
 #if UINT_MAX >= 0xffffffff
-typedef unsigned int uLong;
+typedef unsigned int GNUNET_uLong;
 #elif ULONG_MAX >= 0xffffffff
-typedef unsigned long uLong;
+typedef unsigned long GNUNET_uLong;
 #else
 #error This compiler is not ANSI-compliant!
 #endif
@@ -46,12 +44,12 @@ typedef unsigned long uLong;
 #define Z_NULL  0
 
 
-#define POLYNOMIAL (uLong)0xedb88320
-static uLong crc_table[256];
+#define POLYNOMIAL (GNUNET_uLong)0xedb88320
+static GNUNET_uLong crc_table[256];
 
 /*
  * This routine writes each crc_table entry exactly once,
- * with the ccorrect final value.  Thus, it is safe to call
+ * with the correct final value.  Thus, it is safe to call
  * even on a table that someone else is using concurrently.
  */
 static void
@@ -59,7 +57,7 @@ crc_init ()
 {
   static int once;
   unsigned int i, j;
-  uLong h = 1;
+  GNUNET_uLong h = 1;
 
   if (once)
     return;
@@ -84,8 +82,8 @@ crc_init ()
  * to data in little-endian byte and bit order to preserve the
  * property of detecting all burst errors of length 32 bits or less.
  */
-static uLong
-crc32 (uLong crc, const char *buf, size_t len)
+static GNUNET_uLong
+crc32 (GNUNET_uLong crc, const char *buf, size_t len)
 {
   crc_init ();
   GNUNET_assert (crc_table[255] != 0);
@@ -106,7 +104,7 @@ crc32 (uLong crc, const char *buf, size_t len)
 int32_t
 GNUNET_CRYPTO_crc32_n (const void *buf, size_t len)
 {
-  uLong crc;
+  GNUNET_uLong crc;
 
   crc = crc32 (0L, Z_NULL, 0);
   crc = crc32 (crc, (char *) buf, len);
@@ -118,20 +116,22 @@ GNUNET_CRYPTO_crc32_n (const void *buf, size_t len)
  * Perform an incremental step in a CRC16 (for TCP/IP) calculation.
  *
  * @param sum current sum, initially 0
- * @param hdr buffer to calculate CRC over (must be 16-bit aligned)
+ * @param buf buffer to calculate CRC over (must be 16-bit aligned)
  * @param len number of bytes in hdr, must be multiple of 2
  * @return updated crc sum (must be subjected to GNUNET_CRYPTO_crc16_finish to get actual crc16)
  */
 uint32_t
-GNUNET_CRYPTO_crc16_step (uint32_t sum, uint16_t * hdr, size_t len)
+GNUNET_CRYPTO_crc16_step (uint32_t sum, const void *buf, size_t len)
 {
+  const uint16_t *hdr = buf;
   for (; len >= 2; len -= 2)
     sum += *(hdr++);
   if (len == 1)
-    sum += *((unsigned char *) hdr);
+    sum += (*hdr) & ntohs(0xFF00);
   return sum;
 }
 
+
 /**
  * Convert results from GNUNET_CRYPTO_crc16_step to final crc16.
  *
@@ -151,13 +151,14 @@ GNUNET_CRYPTO_crc16_finish (uint32_t sum)
 /**
  * Calculate the checksum of a buffer in one step.
  *
- * @param hdr buffer to  calculate CRC over (must be 16-bit aligned)
+ * @param buf buffer to  calculate CRC over (must be 16-bit aligned)
  * @param len number of bytes in hdr, must be multiple of 2
  * @return crc16 value
  */
 uint16_t
-GNUNET_CRYPTO_crc16_n (uint16_t *hdr, size_t len)
+GNUNET_CRYPTO_crc16_n (const void *buf, size_t len)
 {
+  const uint16_t *hdr = buf;
   uint32_t sum = GNUNET_CRYPTO_crc16_step (0, hdr, len);
 
   return GNUNET_CRYPTO_crc16_finish (sum);