pg_crc.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * pg_crc.h
  3. *
  4. * PostgreSQL CRC support
  5. *
  6. * See Ross Williams' excellent introduction
  7. * A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS, available from
  8. * http://ross.net/crc/ or several other net sites.
  9. *
  10. * We have three slightly different variants of a 32-bit CRC calculation:
  11. * CRC-32C (Castagnoli polynomial), CRC-32 (Ethernet polynomial), and a legacy
  12. * CRC-32 version that uses the lookup table in a funny way. They all consist
  13. * of four macros:
  14. *
  15. * INIT_<variant>(crc)
  16. * Initialize a CRC accumulator
  17. *
  18. * COMP_<variant>(crc, data, len)
  19. * Accumulate some (more) bytes into a CRC
  20. *
  21. * FIN_<variant>(crc)
  22. * Finish a CRC calculation
  23. *
  24. * EQ_<variant>(c1, c2)
  25. * Check for equality of two CRCs.
  26. *
  27. * The CRC-32C variant is in port/pg_crc32c.h.
  28. *
  29. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  30. * Portions Copyright (c) 1994, Regents of the University of California
  31. *
  32. * src/include/utils/pg_crc.h
  33. */
  34. #ifndef PG_CRC_H
  35. #define PG_CRC_H
  36. typedef uint32 pg_crc32;
  37. /*
  38. * CRC-32, the same used e.g. in Ethernet.
  39. *
  40. * This is currently only used in ltree and hstore contrib modules. It uses
  41. * the same lookup table as the legacy algorithm below. New code should
  42. * use the Castagnoli version instead.
  43. */
  44. #define INIT_TRADITIONAL_CRC32(crc) ((crc) = 0xFFFFFFFF)
  45. #define FIN_TRADITIONAL_CRC32(crc) ((crc) ^= 0xFFFFFFFF)
  46. #define COMP_TRADITIONAL_CRC32(crc, data, len) \
  47. COMP_CRC32_NORMAL_TABLE(crc, data, len, pg_crc32_table)
  48. #define EQ_TRADITIONAL_CRC32(c1, c2) ((c1) == (c2))
  49. /* Sarwate's algorithm, for use with a "normal" lookup table */
  50. #define COMP_CRC32_NORMAL_TABLE(crc, data, len, table) \
  51. do { \
  52. const unsigned char *__data = (const unsigned char *) (data); \
  53. uint32 __len = (len); \
  54. \
  55. while (__len-- > 0) \
  56. { \
  57. int __tab_index = ((int) (crc) ^ *__data++) & 0xFF; \
  58. (crc) = table[__tab_index] ^ ((crc) >> 8); \
  59. } \
  60. } while (0)
  61. /*
  62. * The CRC algorithm used for WAL et al in pre-9.5 versions.
  63. *
  64. * This closely resembles the normal CRC-32 algorithm, but is subtly
  65. * different. Using Williams' terms, we use the "normal" table, but with
  66. * "reflected" code. That's bogus, but it was like that for years before
  67. * anyone noticed. It does not correspond to any polynomial in a normal CRC
  68. * algorithm, so it's not clear what the error-detection properties of this
  69. * algorithm actually are.
  70. *
  71. * We still need to carry this around because it is used in a few on-disk
  72. * structures that need to be pg_upgradeable. It should not be used in new
  73. * code.
  74. */
  75. #define INIT_LEGACY_CRC32(crc) ((crc) = 0xFFFFFFFF)
  76. #define FIN_LEGACY_CRC32(crc) ((crc) ^= 0xFFFFFFFF)
  77. #define COMP_LEGACY_CRC32(crc, data, len) \
  78. COMP_CRC32_REFLECTED_TABLE(crc, data, len, pg_crc32_table)
  79. #define EQ_LEGACY_CRC32(c1, c2) ((c1) == (c2))
  80. /*
  81. * Sarwate's algorithm, for use with a "reflected" lookup table (but in the
  82. * legacy algorithm, we actually use it on a "normal" table, see above)
  83. */
  84. #define COMP_CRC32_REFLECTED_TABLE(crc, data, len, table) \
  85. do { \
  86. const unsigned char *__data = (const unsigned char *) (data); \
  87. uint32 __len = (len); \
  88. \
  89. while (__len-- > 0) \
  90. { \
  91. int __tab_index = ((int) ((crc) >> 24) ^ *__data++) & 0xFF; \
  92. (crc) = table[__tab_index] ^ ((crc) << 8); \
  93. } \
  94. } while (0)
  95. /*
  96. * Constant table for the CRC-32 polynomials. The same table is used by both
  97. * the normal and traditional variants.
  98. */
  99. extern PGDLLIMPORT const uint32 pg_crc32_table[256];
  100. #endif /* PG_CRC_H */