Debian Codegraph @

contrib/ltree/crc32.c

raw
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 "catalog/pg_collation.h"
17 #include "utils/pg_locale.h"
18 #endif
19
20 #ifdef LOWER_NODE
21
22 unsigned int
23 ltree_crc32_sz(const char *buf, int size)
24 {
25 pg_crc32 crc;
26 const char *p = buf;
27 const char *end = buf + size;
28 static pg_locale_t locale = NULL;
29
30 if (!locale)
31 locale = pg_newlocale_from_collation(DEFAULT_COLLATION_OID);
32
33 INIT_TRADITIONAL_CRC32(crc);
34 if (locale->ctype_is_c)
35 {
36 while (size > 0)
37 {
38 char c = pg_ascii_tolower(*p);
39
40 COMP_TRADITIONAL_CRC32(crc, &c, 1);
41 size--;
42 p++;
43 }
44 }
45 else
46 {
47 while (size > 0)
48 {
49 char foldstr[UNICODE_CASEMAP_BUFSZ];
50 int srclen = pg_mblen_range(p, end);
51 size_t foldlen;
52
53 /* fold one codepoint at a time */
54 foldlen = pg_strfold(foldstr, UNICODE_CASEMAP_BUFSZ, p, srclen,
55 locale);
56
57 COMP_TRADITIONAL_CRC32(crc, foldstr, foldlen);
58
59 size -= srclen;
60 p += srclen;
61 }
62 }
63 FIN_TRADITIONAL_CRC32(crc);
64 return (unsigned int) crc;
65 }
66
67 #else
68
69 unsigned int
70 ltree_crc32_sz(const char *buf, int size)
71 {
72 pg_crc32 crc;
73 const char *p = buf;
74
75 INIT_TRADITIONAL_CRC32(crc);
76 while (size > 0)
77 {
78 COMP_TRADITIONAL_CRC32(crc, p, 1);
79 size--;
80 p++;
81 }
82 FIN_TRADITIONAL_CRC32(crc);
83 return (unsigned int) crc;
84 }
85
86 #endif /* !LOWER_NODE */