4
* PostgreSQL CRC support
6
* See Ross Williams' excellent introduction
7
* A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS, available from
8
* http://www.ross.net/crc/ or several other net sites.
10
* We use a normal (not "reflected", in Williams' terms) CRC, using initial
11
* all-ones register contents and a final bit inversion.
13
* The 64-bit variant is not used as of PostgreSQL 8.1, but we retain the
14
* code for possible future use.
17
* Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
18
* Portions Copyright (c) 1994, Regents of the University of California
20
* src/include/utils/pg_crc.h
25
/* ugly hack to let this be used in frontend and backend code on Cygwin */
29
#define CRCDLLIMPORT PGDLLIMPORT
32
typedef uint32 pg_crc32;
34
/* Initialize a CRC accumulator */
35
#define INIT_CRC32(crc) ((crc) = 0xFFFFFFFF)
37
/* Finish a CRC calculation */
38
#define FIN_CRC32(crc) ((crc) ^= 0xFFFFFFFF)
40
/* Accumulate some (more) bytes into a CRC */
41
#define COMP_CRC32(crc, data, len) \
43
unsigned char *__data = (unsigned char *) (data); \
44
uint32 __len = (len); \
48
int __tab_index = ((int) ((crc) >> 24) ^ *__data++) & 0xFF; \
49
(crc) = pg_crc32_table[__tab_index] ^ ((crc) << 8); \
53
/* Check for equality of two CRCs */
54
#define EQ_CRC32(c1,c2) ((c1) == (c2))
56
/* Constant table for CRC calculation */
57
extern CRCDLLIMPORT const uint32 pg_crc32_table[];
60
#ifdef PROVIDE_64BIT_CRC
63
* If we use a 64-bit integer type, then a 64-bit CRC looks just like the
64
* usual sort of implementation. However, we can also fake it with two
65
* 32-bit registers. Experience has shown that the two-32-bit-registers code
66
* is as fast as, or even much faster than, the 64-bit code on all but true
67
* 64-bit machines. We use SIZEOF_VOID_P to check the native word width.
73
* crc0 represents the LSBs of the 64-bit value, crc1 the MSBs. Note that
74
* with crc0 placed first, the output of 32-bit and 64-bit implementations
75
* will be bit-compatible only on little-endian architectures. If it were
76
* important to make the two possible implementations bit-compatible on
77
* all machines, we could do a configure test to decide how to order the
78
* two fields, but it seems not worth the trouble.
80
typedef struct pg_crc64
86
/* Initialize a CRC accumulator */
87
#define INIT_CRC64(crc) ((crc).crc0 = 0xffffffff, (crc).crc1 = 0xffffffff)
89
/* Finish a CRC calculation */
90
#define FIN_CRC64(crc) ((crc).crc0 ^= 0xffffffff, (crc).crc1 ^= 0xffffffff)
92
/* Accumulate some (more) bytes into a CRC */
93
#define COMP_CRC64(crc, data, len) \
95
uint32 __crc0 = (crc).crc0; \
96
uint32 __crc1 = (crc).crc1; \
97
unsigned char *__data = (unsigned char *) (data); \
98
uint32 __len = (len); \
100
while (__len-- > 0) \
102
int __tab_index = ((int) (__crc1 >> 24) ^ *__data++) & 0xFF; \
103
__crc1 = pg_crc64_table1[__tab_index] ^ ((__crc1 << 8) | (__crc0 >> 24)); \
104
__crc0 = pg_crc64_table0[__tab_index] ^ (__crc0 << 8); \
106
(crc).crc0 = __crc0; \
107
(crc).crc1 = __crc1; \
110
/* Check for equality of two CRCs */
111
#define EQ_CRC64(c1,c2) ((c1).crc0 == (c2).crc0 && (c1).crc1 == (c2).crc1)
113
/* Constant table for CRC calculation */
114
extern CRCDLLIMPORT const uint32 pg_crc64_table0[];
115
extern CRCDLLIMPORT const uint32 pg_crc64_table1[];
116
#else /* use int64 implementation */
118
typedef struct pg_crc64
123
/* Initialize a CRC accumulator */
124
#define INIT_CRC64(crc) ((crc).crc0 = UINT64CONST(0xffffffffffffffff))
126
/* Finish a CRC calculation */
127
#define FIN_CRC64(crc) ((crc).crc0 ^= UINT64CONST(0xffffffffffffffff))
129
/* Accumulate some (more) bytes into a CRC */
130
#define COMP_CRC64(crc, data, len) \
132
uint64 __crc0 = (crc).crc0; \
133
unsigned char *__data = (unsigned char *) (data); \
134
uint32 __len = (len); \
136
while (__len-- > 0) \
138
int __tab_index = ((int) (__crc0 >> 56) ^ *__data++) & 0xFF; \
139
__crc0 = pg_crc64_table[__tab_index] ^ (__crc0 << 8); \
141
(crc).crc0 = __crc0; \
144
/* Check for equality of two CRCs */
145
#define EQ_CRC64(c1,c2) ((c1).crc0 == (c2).crc0)
147
/* Constant table for CRC calculation */
148
extern CRCDLLIMPORT const uint64 pg_crc64_table[];
149
#endif /* SIZEOF_VOID_P < 8 */
150
#endif /* PROVIDE_64BIT_CRC */
152
#endif /* PG_CRC_H */