abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
crc32.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "misc/util/abc_global.h"
#include "zutil.h"
#include "crc32.h"

Go to the source code of this file.

Macros

#define local   static
 
#define TBLS   1
 
#define DO1   crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)
 
#define DO8   DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1
 
#define GF2_DIM   32 /* dimension of GF(2) vectors (length of CRC) */
 

Functions

local unsigned long
gf2_matrix_times 
OF ((unsigned long *mat, unsigned long vec))
 
local void gf2_matrix_square OF ((unsigned long *square, unsigned long *mat))
 
local uLong crc32_combine_ (uLong crc1, uLong crc2, z_off64_t len2)
 
ABC_NAMESPACE_IMPL_END
ABC_NAMESPACE_IMPL_START const
unsigned long FAR *ZEXPORT 
get_crc_table ()
 
unsigned long ZEXPORT crc32 (unsigned long crc, const unsigned char FAR *buf, uInt len)
 
local unsigned long gf2_matrix_times (unsigned long *mat, unsigned long vec)
 
local void gf2_matrix_square (unsigned long *square, unsigned long *mat)
 
uLong ZEXPORT crc32_combine (uLong crc1, uLong crc2, z_off_t len2)
 
uLong ZEXPORT crc32_combine64 (uLong crc1, uLong crc2, z_off64_t len2)
 

Macro Definition Documentation

#define DO1   crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8)

Definition at line 226 of file crc32.c.

#define DO8   DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1

Definition at line 227 of file crc32.c.

#define GF2_DIM   32 /* dimension of GF(2) vectors (length of CRC) */

Definition at line 341 of file crc32.c.

#define local   static

Definition at line 38 of file crc32.c.

#define TBLS   1

Definition at line 73 of file crc32.c.

Function Documentation

unsigned long ZEXPORT crc32 ( unsigned long  crc,
const unsigned char FAR buf,
uInt  len 
)

Definition at line 230 of file crc32.c.

231 {
232  if (buf == Z_NULL) return 0UL;
233 
234 #ifdef DYNAMIC_CRC_TABLE
235  if (crc_table_empty)
236  make_crc_table();
237 #endif /* DYNAMIC_CRC_TABLE */
238 
239 #ifdef BYFOUR
240  if (sizeof(void *) == sizeof(ptrdiff_t)) {
241  u4 endian;
242 
243  endian = 1;
244  if (*((unsigned char *)(&endian)))
245  return crc32_little(crc, buf, len);
246  else
247  return crc32_big(crc, buf, len);
248  }
249 #endif /* BYFOUR */
250  crc = crc ^ 0xffffffffUL;
251  while (len >= 8) {
252  DO8;
253  len -= 8;
254  }
255  if (len) do {
256  DO1;
257  } while (--len);
258  return crc ^ 0xffffffffUL;
259 }
#define DO1
Definition: crc32.c:226
#define DO8
Definition: crc32.c:227
#define Z_NULL
Definition: zlib.h:216
uLong ZEXPORT crc32_combine ( uLong  crc1,
uLong  crc2,
z_off_t  len2 
)

Definition at line 421 of file crc32.c.

422 {
423  return crc32_combine_(crc1, crc2, len2);
424 }
local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2)
Definition: crc32.c:368
uLong ZEXPORT crc32_combine64 ( uLong  crc1,
uLong  crc2,
z_off64_t  len2 
)

Definition at line 426 of file crc32.c.

427 {
428  return crc32_combine_(crc1, crc2, len2);
429 }
local uLong crc32_combine_(uLong crc1, uLong crc2, z_off64_t len2)
Definition: crc32.c:368
local uLong crc32_combine_ ( uLong  crc1,
uLong  crc2,
z_off64_t  len2 
)

Definition at line 368 of file crc32.c.

369 {
370  int n;
371  unsigned long row;
372  unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */
373  unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */
374 
375  /* degenerate case (also disallow negative lengths) */
376  if (len2 <= 0)
377  return crc1;
378 
379  /* put operator for one zero bit in odd */
380  odd[0] = 0xedb88320UL; /* CRC-32 polynomial */
381  row = 1;
382  for (n = 1; n < GF2_DIM; n++) {
383  odd[n] = row;
384  row <<= 1;
385  }
386 
387  /* put operator for two zero bits in even */
388  gf2_matrix_square(even, odd);
389 
390  /* put operator for four zero bits in odd */
391  gf2_matrix_square(odd, even);
392 
393  /* apply len2 zeros to crc1 (first square will put the operator for one
394  zero byte, eight zero bits, in even) */
395  do {
396  /* apply zeros operator for this bit of len2 */
397  gf2_matrix_square(even, odd);
398  if (len2 & 1)
399  crc1 = gf2_matrix_times(even, crc1);
400  len2 >>= 1;
401 
402  /* if no more bits set, then done */
403  if (len2 == 0)
404  break;
405 
406  /* another iteration of the loop with odd and even swapped */
407  gf2_matrix_square(odd, even);
408  if (len2 & 1)
409  crc1 = gf2_matrix_times(odd, crc1);
410  len2 >>= 1;
411 
412  /* if no more bits set, then done */
413  } while (len2 != 0);
414 
415  /* return combined crc */
416  crc1 ^= crc2;
417  return crc1;
418 }
local void gf2_matrix_square(unsigned long *square, unsigned long *mat)
Definition: crc32.c:359
#define GF2_DIM
Definition: crc32.c:341
local unsigned long gf2_matrix_times(unsigned long *mat, unsigned long vec)
Definition: crc32.c:344

Definition at line 216 of file crc32.c.

217 {
218 #ifdef DYNAMIC_CRC_TABLE
219  if (crc_table_empty)
220  make_crc_table();
221 #endif /* DYNAMIC_CRC_TABLE */
222  return (const unsigned long FAR *)crc_table;
223 }
ABC_NAMESPACE_HEADER_START local const unsigned long FAR crc_table[TBLS][256]
Definition: crc32.h:7
#define FAR
Definition: zconf.h:329
local void gf2_matrix_square ( unsigned long *  square,
unsigned long *  mat 
)

Definition at line 359 of file crc32.c.

360 {
361  int n;
362 
363  for (n = 0; n < GF2_DIM; n++)
364  square[n] = gf2_matrix_times(mat, mat[n]);
365 }
#define GF2_DIM
Definition: crc32.c:341
local unsigned long gf2_matrix_times(unsigned long *mat, unsigned long vec)
Definition: crc32.c:344
local unsigned long gf2_matrix_times ( unsigned long *  mat,
unsigned long  vec 
)

Definition at line 344 of file crc32.c.

345 {
346  unsigned long sum;
347 
348  sum = 0;
349  while (vec) {
350  if (vec & 1)
351  sum ^= *mat;
352  vec >>= 1;
353  mat++;
354  }
355  return sum;
356 }
local unsigned long gf2_matrix_times OF ( (unsigned long *mat, unsigned long vec)  )
local void gf2_matrix_square OF ( (unsigned long *square, unsigned long *mat)  )