~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/include/utils/pg_crc.h

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * pg_crc.h
 
3
 *
 
4
 * PostgreSQL 64-bit CRC support
 
5
 *
 
6
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
7
 * Portions Copyright (c) 1994, Regents of the University of California
 
8
 *
 
9
 * $PostgreSQL: pgsql/src/include/utils/pg_crc.h,v 1.12 2004-12-31 22:03:46 pgsql Exp $
 
10
 */
 
11
#ifndef PG_CRC_H
 
12
#define PG_CRC_H
 
13
 
 
14
/*
 
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.
 
20
 *
 
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):
 
25
 *
 
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 +
 
29
 * x^7 + x^4 + x + 1
 
30
 */
 
31
 
 
32
#ifdef INT64_IS_BUSTED
 
33
 
 
34
/*
 
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.
 
41
 */
 
42
typedef struct crc64
 
43
{
 
44
        uint32          crc0;
 
45
        uint32          crc1;
 
46
} crc64;
 
47
 
 
48
/* Initialize a CRC accumulator */
 
49
#define INIT_CRC64(crc) ((crc).crc0 = 0xffffffff, (crc).crc1 = 0xffffffff)
 
50
 
 
51
/* Finish a CRC calculation */
 
52
#define FIN_CRC64(crc)  ((crc).crc0 ^= 0xffffffff, (crc).crc1 ^= 0xffffffff)
 
53
 
 
54
/* Accumulate some (more) bytes into a CRC */
 
55
#define COMP_CRC64(crc, data, len)      \
 
56
do { \
 
57
        uint32          __crc0 = (crc).crc0; \
 
58
        uint32          __crc1 = (crc).crc1; \
 
59
        unsigned char *__data = (unsigned char *) (data); \
 
60
        uint32          __len = (len); \
 
61
\
 
62
        while (__len-- > 0) \
 
63
        { \
 
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); \
 
67
        } \
 
68
        (crc).crc0 = __crc0; \
 
69
        (crc).crc1 = __crc1; \
 
70
} while (0)
 
71
 
 
72
/* Check for equality of two CRCs */
 
73
#define EQ_CRC64(c1,c2)  ((c1).crc0 == (c2).crc0 && (c1).crc1 == (c2).crc1)
 
74
 
 
75
/* Constant table for CRC calculation */
 
76
extern const uint32 crc_table0[];
 
77
extern const uint32 crc_table1[];
 
78
 
 
79
#else                                                   /* int64 works */
 
80
 
 
81
typedef struct crc64
 
82
{
 
83
        uint64          crc0;
 
84
} crc64;
 
85
 
 
86
/* Initialize a CRC accumulator */
 
87
#define INIT_CRC64(crc) ((crc).crc0 = UINT64CONST(0xffffffffffffffff))
 
88
 
 
89
/* Finish a CRC calculation */
 
90
#define FIN_CRC64(crc)  ((crc).crc0 ^= UINT64CONST(0xffffffffffffffff))
 
91
 
 
92
/* Accumulate some (more) bytes into a CRC */
 
93
#define COMP_CRC64(crc, data, len)      \
 
94
do { \
 
95
        uint64          __crc0 = (crc).crc0; \
 
96
        unsigned char *__data = (unsigned char *) (data); \
 
97
        uint32          __len = (len); \
 
98
\
 
99
        while (__len-- > 0) \
 
100
        { \
 
101
                int             __tab_index = ((int) (__crc0 >> 56) ^ *__data++) & 0xFF; \
 
102
                __crc0 = crc_table[__tab_index] ^ (__crc0 << 8); \
 
103
        } \
 
104
        (crc).crc0 = __crc0; \
 
105
} while (0)
 
106
 
 
107
/* Check for equality of two CRCs */
 
108
#define EQ_CRC64(c1,c2)  ((c1).crc0 == (c2).crc0)
 
109
 
 
110
/* Constant table for CRC calculation */
 
111
extern const uint64 crc_table[];
 
112
#endif   /* INT64_IS_BUSTED */
 
113
 
 
114
#endif   /* PG_CRC_H */