Line data Source code
1 : /* contrib/ltree/crc32.c */ 2 : 3 : /* 4 : * Implements CRC-32, as used in ltree. 5 : * 6 : * Note that the CRC is used in the on-disk format of GiST indexes, so we 7 : * must stay backwards-compatible! 8 : */ 9 : 10 : #include "postgres.h" 11 : #include "ltree.h" 12 : 13 : #include "crc32.h" 14 : #include "utils/pg_crc.h" 15 : #ifdef LOWER_NODE 16 : #include "utils/pg_locale.h" 17 : #endif 18 : 19 : #ifdef LOWER_NODE 20 : 21 : unsigned int 22 313708 : ltree_crc32_sz(const char *buf, int size) 23 : { 24 : pg_crc32 crc; 25 313708 : const char *p = buf; 26 : static pg_locale_t locale = NULL; 27 : 28 313708 : if (!locale) 29 2 : locale = pg_database_locale(); 30 : 31 313708 : INIT_TRADITIONAL_CRC32(crc); 32 752894 : while (size > 0) 33 : { 34 : char foldstr[UNICODE_CASEMAP_BUFSZ]; 35 439186 : int srclen = pg_mblen(p); 36 : size_t foldlen; 37 : 38 : /* fold one codepoint at a time */ 39 439186 : foldlen = pg_strfold(foldstr, UNICODE_CASEMAP_BUFSZ, p, srclen, 40 : locale); 41 : 42 878372 : COMP_TRADITIONAL_CRC32(crc, foldstr, foldlen); 43 : 44 439186 : size -= srclen; 45 439186 : p += srclen; 46 : } 47 313708 : FIN_TRADITIONAL_CRC32(crc); 48 313708 : return (unsigned int) crc; 49 : } 50 : 51 : #else 52 : 53 : unsigned int 54 : ltree_crc32_sz(const char *buf, int size) 55 : { 56 : pg_crc32 crc; 57 : const char *p = buf; 58 : 59 : INIT_TRADITIONAL_CRC32(crc); 60 : while (size > 0) 61 : { 62 : COMP_TRADITIONAL_CRC32(crc, p, 1); 63 : size--; 64 : p++; 65 : } 66 : FIN_TRADITIONAL_CRC32(crc); 67 : return (unsigned int) crc; 68 : } 69 : 70 : #endif /* !LOWER_NODE */