Eneboo - Documentación para desarrolladores
src/libpq/include/utils/pg_crc.h
Ir a la documentación de este archivo.
00001 /*
00002  * pg_crc.h
00003  *
00004  * PostgreSQL CRC support
00005  *
00006  * See Ross Williams' excellent introduction
00007  * A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS, available from
00008  * ftp://ftp.rocksoft.com/papers/crc_v3.txt or several other net sites.
00009  *
00010  * We use a normal (not "reflected", in Williams' terms) CRC, using initial
00011  * all-ones register contents and a final bit inversion.
00012  *
00013  * The 64-bit variant is not used as of PostgreSQL 8.1, but we retain the
00014  * code for possible future use.
00015  *
00016  *
00017  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
00018  * Portions Copyright (c) 1994, Regents of the University of California
00019  *
00020  * $PostgreSQL: pgsql/src/include/utils/pg_crc.h,v 1.14 2005/10/15 02:49:46 momjian Exp $
00021  */
00022 #ifndef PG_CRC_H
00023 #define PG_CRC_H
00024 
00025 
00026 typedef uint32 pg_crc32;
00027 
00028 /* Initialize a CRC accumulator */
00029 #define INIT_CRC32(crc) ((crc) = 0xFFFFFFFF)
00030 
00031 /* Finish a CRC calculation */
00032 #define FIN_CRC32(crc)  ((crc) ^= 0xFFFFFFFF)
00033 
00034 /* Accumulate some (more) bytes into a CRC */
00035 #define COMP_CRC32(crc, data, len)      \
00036 do { \
00037         unsigned char *__data = (unsigned char *) (data); \
00038         uint32          __len = (len); \
00039 \
00040         while (__len-- > 0) \
00041         { \
00042                 int             __tab_index = ((int) ((crc) >> 24) ^ *__data++) & 0xFF; \
00043                 (crc) = pg_crc32_table[__tab_index] ^ ((crc) << 8); \
00044         } \
00045 } while (0)
00046 
00047 /* Check for equality of two CRCs */
00048 #define EQ_CRC32(c1,c2)  ((c1) == (c2))
00049 
00050 /* Constant table for CRC calculation */
00051 extern const uint32 pg_crc32_table[];
00052 
00053 
00054 #ifdef PROVIDE_64BIT_CRC
00055 
00056 /*
00057  * If we have a 64-bit integer type, then a 64-bit CRC looks just like the
00058  * usual sort of implementation.  If we have no working 64-bit type, then
00059  * fake it with two 32-bit registers.  (Note: experience has shown that the
00060  * two-32-bit-registers code is as fast as, or even much faster than, the
00061  * 64-bit code on all but true 64-bit machines.  INT64_IS_BUSTED is therefore
00062  * probably the wrong control symbol to use to select the implementation.)
00063  */
00064 
00065 #ifdef INT64_IS_BUSTED
00066 
00067 /*
00068  * crc0 represents the LSBs of the 64-bit value, crc1 the MSBs.  Note that
00069  * with crc0 placed first, the output of 32-bit and 64-bit implementations
00070  * will be bit-compatible only on little-endian architectures.  If it were
00071  * important to make the two possible implementations bit-compatible on
00072  * all machines, we could do a configure test to decide how to order the
00073  * two fields, but it seems not worth the trouble.
00074  */
00075 typedef struct pg_crc64
00076 {
00077         uint32          crc0;
00078         uint32          crc1;
00079 }       pg_crc64;
00080 
00081 /* Initialize a CRC accumulator */
00082 #define INIT_CRC64(crc) ((crc).crc0 = 0xffffffff, (crc).crc1 = 0xffffffff)
00083 
00084 /* Finish a CRC calculation */
00085 #define FIN_CRC64(crc)  ((crc).crc0 ^= 0xffffffff, (crc).crc1 ^= 0xffffffff)
00086 
00087 /* Accumulate some (more) bytes into a CRC */
00088 #define COMP_CRC64(crc, data, len)      \
00089 do { \
00090         uint32          __crc0 = (crc).crc0; \
00091         uint32          __crc1 = (crc).crc1; \
00092         unsigned char *__data = (unsigned char *) (data); \
00093         uint32          __len = (len); \
00094 \
00095         while (__len-- > 0) \
00096         { \
00097                 int             __tab_index = ((int) (__crc1 >> 24) ^ *__data++) & 0xFF; \
00098                 __crc1 = pg_crc64_table1[__tab_index] ^ ((__crc1 << 8) | (__crc0 >> 24)); \
00099                 __crc0 = pg_crc64_table0[__tab_index] ^ (__crc0 << 8); \
00100         } \
00101         (crc).crc0 = __crc0; \
00102         (crc).crc1 = __crc1; \
00103 } while (0)
00104 
00105 /* Check for equality of two CRCs */
00106 #define EQ_CRC64(c1,c2)  ((c1).crc0 == (c2).crc0 && (c1).crc1 == (c2).crc1)
00107 
00108 /* Constant table for CRC calculation */
00109 extern const uint32 pg_crc64_table0[];
00110 extern const uint32 pg_crc64_table1[];
00111 #else                                                   /* int64 works */
00112 
00113 typedef struct pg_crc64
00114 {
00115         uint64          crc0;
00116 }       pg_crc64;
00117 
00118 /* Initialize a CRC accumulator */
00119 #define INIT_CRC64(crc) ((crc).crc0 = UINT64CONST(0xffffffffffffffff))
00120 
00121 /* Finish a CRC calculation */
00122 #define FIN_CRC64(crc)  ((crc).crc0 ^= UINT64CONST(0xffffffffffffffff))
00123 
00124 /* Accumulate some (more) bytes into a CRC */
00125 #define COMP_CRC64(crc, data, len)      \
00126 do { \
00127         uint64          __crc0 = (crc).crc0; \
00128         unsigned char *__data = (unsigned char *) (data); \
00129         uint32          __len = (len); \
00130 \
00131         while (__len-- > 0) \
00132         { \
00133                 int             __tab_index = ((int) (__crc0 >> 56) ^ *__data++) & 0xFF; \
00134                 __crc0 = pg_crc64_table[__tab_index] ^ (__crc0 << 8); \
00135         } \
00136         (crc).crc0 = __crc0; \
00137 } while (0)
00138 
00139 /* Check for equality of two CRCs */
00140 #define EQ_CRC64(c1,c2)  ((c1).crc0 == (c2).crc0)
00141 
00142 /* Constant table for CRC calculation */
00143 extern const uint64 pg_crc64_table[];
00144 #endif   /* INT64_IS_BUSTED */
00145 #endif   /* PROVIDE_64BIT_CRC */
00146 
00147 #endif   /* PG_CRC_H */
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'