Evince
Evince is a document viewer capable of displaying multiple and single page document formats like PDF and Postscript.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
crc32.c
Go to the documentation of this file.
1 /* Copyright 2015 the unarr project authors (see AUTHORS file).
2  License: LGPLv3 */
3 
4 #include "unarr-imp.h"
5 
6 #ifndef HAVE_ZLIB
7 
8 /* code adapted from https://gnunet.org/svn/gnunet/src/util/crypto_crc.c (public domain) */
9 
10 static bool crc_table_ready = false;
11 static uint32_t crc_table[256];
12 
13 uint32_t ar_crc32(uint32_t crc32, const unsigned char *data, size_t data_len)
14 {
15  if (!crc_table_ready) {
16  uint32_t i, j;
17  uint32_t h = 1;
18  crc_table[0] = 0;
19  for (i = 128; i; i >>= 1) {
20  h = (h >> 1) ^ ((h & 1) ? 0xEDB88320 : 0);
21  for (j = 0; j < 256; j += 2 * i) {
22  crc_table[i + j] = crc_table[j] ^ h;
23  }
24  }
25  crc_table_ready = true;
26  }
27 
28  crc32 = crc32 ^ 0xFFFFFFFF;
29  while (data_len-- > 0) {
30  crc32 = (crc32 >> 8) ^ crc_table[(crc32 ^ *data++) & 0xFF];
31  }
32  return crc32 ^ 0xFFFFFFFF;
33 }
34 
35 #else
36 
37 #include <zlib.h>
38 
39 uint32_t ar_crc32(uint32_t crc, const unsigned char *data, size_t data_len)
40 {
41 #if SIZE_MAX > UINT32_MAX
42  while (data_len > UINT32_MAX) {
43  crc = crc32(crc, data, UINT32_MAX);
44  data += UINT32_MAX;
45  data_len -= UINT32_MAX;
46  }
47 #endif
48  return crc32(crc, data, (uint32_t)data_len);
49 }
50 
51 #endif