Age Owner Branch data TLA 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
2339 tgl@sss.pgh.pa.us 22 :CBC 172777 : ltree_crc32_sz(const char *buf, int size)
23 : : {
24 : : pg_crc32 crc;
25 : 172777 : const char *p = buf;
232 tmunro@postgresql.or 26 : 172777 : const char *end = buf + size;
27 : : static pg_locale_t locale = NULL;
28 : :
254 jdavis@postgresql.or 29 [ + + ]: 172777 : if (!locale)
30 : 1 : locale = pg_database_locale();
31 : :
4314 heikki.linnakangas@i 32 : 172777 : INIT_TRADITIONAL_CRC32(crc);
33 [ + + ]: 470855 : while (size > 0)
34 : : {
35 : : /* max space required to map single codepoint, including NUL */
36 : : char foldstr[PG_CASEMAP_BUFSZ];
232 tmunro@postgresql.or 37 : 298078 : int srclen = pg_mblen_range(p, end);
38 : : size_t foldlen;
39 : :
40 : : /* fold one codepoint at a time */
3 jdavis@postgresql.or 41 : 298078 : foldlen = pg_strfold(foldstr, sizeof(foldstr), p, srclen, locale);
42 [ - + ]: 298078 : Assert(foldlen < sizeof(foldstr));
43 : :
254 44 [ + + ]: 596156 : COMP_TRADITIONAL_CRC32(crc, foldstr, foldlen);
45 : :
46 : 298078 : size -= srclen;
47 : 298078 : p += srclen;
48 : : }
49 : 172777 : FIN_TRADITIONAL_CRC32(crc);
50 : 172777 : return (unsigned int) crc;
51 : : }
52 : :
53 : : #else
54 : :
55 : : unsigned int
56 : : ltree_crc32_sz(const char *buf, int size)
57 : : {
58 : : pg_crc32 crc;
59 : : const char *p = buf;
60 : :
61 : : INIT_TRADITIONAL_CRC32(crc);
62 : : while (size > 0)
63 : : {
64 : : COMP_TRADITIONAL_CRC32(crc, p, 1);
65 : : size--;
66 : : p++;
67 : : }
68 : : FIN_TRADITIONAL_CRC32(crc);
69 : : return (unsigned int) crc;
70 : : }
71 : :
72 : : #endif /* !LOWER_NODE */
|