4
* PostgreSQL 64-bit CRC support
6
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7
* Portions Copyright (c) 1994, Regents of the University of California
9
* $PostgreSQL: pgsql/src/include/utils/pg_crc.h,v 1.12 2004-12-31 22:03:46 pgsql Exp $
15
* If we have a 64-bit integer type, then a 64-bit CRC looks just like the
16
* usual sort of implementation. (See Ross Williams' excellent introduction
17
* A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS, available from
18
* ftp://ftp.rocksoft.com/papers/crc_v3.txt or several other net sites.)
19
* If we have no working 64-bit type, then fake it with two 32-bit registers.
21
* The present implementation is a normal (not "reflected", in Williams'
22
* terms) 64-bit CRC, using initial all-ones register contents and a final
23
* bit inversion. The chosen polynomial is borrowed from the DLT1 spec
24
* (ECMA-182, available from http://www.ecma.ch/ecma1/STAND/ECMA-182.HTM):
26
* x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 +
27
* x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 +
28
* x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +
32
#ifdef INT64_IS_BUSTED
35
* crc0 represents the LSBs of the 64-bit value, crc1 the MSBs. Note that
36
* with crc0 placed first, the output of 32-bit and 64-bit implementations
37
* will be bit-compatible only on little-endian architectures. If it were
38
* important to make the two possible implementations bit-compatible on
39
* all machines, we could do a configure test to decide how to order the
40
* two fields, but it seems not worth the trouble.
48
/* Initialize a CRC accumulator */
49
#define INIT_CRC64(crc) ((crc).crc0 = 0xffffffff, (crc).crc1 = 0xffffffff)
51
/* Finish a CRC calculation */
52
#define FIN_CRC64(crc) ((crc).crc0 ^= 0xffffffff, (crc).crc1 ^= 0xffffffff)
54
/* Accumulate some (more) bytes into a CRC */
55
#define COMP_CRC64(crc, data, len) \
57
uint32 __crc0 = (crc).crc0; \
58
uint32 __crc1 = (crc).crc1; \
59
unsigned char *__data = (unsigned char *) (data); \
60
uint32 __len = (len); \
64
int __tab_index = ((int) (__crc1 >> 24) ^ *__data++) & 0xFF; \
65
__crc1 = crc_table1[__tab_index] ^ ((__crc1 << 8) | (__crc0 >> 24)); \
66
__crc0 = crc_table0[__tab_index] ^ (__crc0 << 8); \
68
(crc).crc0 = __crc0; \
69
(crc).crc1 = __crc1; \
72
/* Check for equality of two CRCs */
73
#define EQ_CRC64(c1,c2) ((c1).crc0 == (c2).crc0 && (c1).crc1 == (c2).crc1)
75
/* Constant table for CRC calculation */
76
extern const uint32 crc_table0[];
77
extern const uint32 crc_table1[];
79
#else /* int64 works */
86
/* Initialize a CRC accumulator */
87
#define INIT_CRC64(crc) ((crc).crc0 = UINT64CONST(0xffffffffffffffff))
89
/* Finish a CRC calculation */
90
#define FIN_CRC64(crc) ((crc).crc0 ^= UINT64CONST(0xffffffffffffffff))
92
/* Accumulate some (more) bytes into a CRC */
93
#define COMP_CRC64(crc, data, len) \
95
uint64 __crc0 = (crc).crc0; \
96
unsigned char *__data = (unsigned char *) (data); \
97
uint32 __len = (len); \
101
int __tab_index = ((int) (__crc0 >> 56) ^ *__data++) & 0xFF; \
102
__crc0 = crc_table[__tab_index] ^ (__crc0 << 8); \
104
(crc).crc0 = __crc0; \
107
/* Check for equality of two CRCs */
108
#define EQ_CRC64(c1,c2) ((c1).crc0 == (c2).crc0)
110
/* Constant table for CRC calculation */
111
extern const uint64 crc_table[];
112
#endif /* INT64_IS_BUSTED */
114
#endif /* PG_CRC_H */