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 0 : ltree_crc32_sz(const char *buf, int size)
23 : {
24 0 : pg_crc32 crc;
25 0 : const char *p = buf;
26 : static pg_locale_t locale = NULL;
27 :
28 0 : if (!locale)
29 0 : locale = pg_database_locale();
30 :
31 0 : INIT_TRADITIONAL_CRC32(crc);
32 0 : while (size > 0)
33 : {
34 0 : char foldstr[UNICODE_CASEMAP_BUFSZ];
35 0 : int srclen = pg_mblen(p);
36 0 : size_t foldlen;
37 :
38 : /* fold one codepoint at a time */
39 0 : foldlen = pg_strfold(foldstr, UNICODE_CASEMAP_BUFSZ, p, srclen,
40 0 : locale);
41 :
42 0 : COMP_TRADITIONAL_CRC32(crc, foldstr, foldlen);
43 :
44 0 : size -= srclen;
45 0 : p += srclen;
46 0 : }
47 0 : FIN_TRADITIONAL_CRC32(crc);
48 0 : return (unsigned int) crc;
49 0 : }
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 */
|