summaryrefslogtreecommitdiff
path: root/contrib/ltree/crc32.c
blob: 3918d4a0ec251a1196ced515a3bb5a1bb4e91e34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* contrib/ltree/crc32.c */

/*
 * Implements CRC-32, as used in ltree.
 *
 * Note that the CRC is used in the on-disk format of GiST indexes, so we
 * must stay backwards-compatible!
 */

#include "postgres.h"
#include "ltree.h"

#include "crc32.h"
#include "utils/pg_crc.h"
#ifdef LOWER_NODE
#include "utils/pg_locale.h"
#endif

#ifdef LOWER_NODE

unsigned int
ltree_crc32_sz(const char *buf, int size)
{
	pg_crc32	crc;
	const char *p = buf;
	static pg_locale_t locale = NULL;

	if (!locale)
		locale = pg_database_locale();

	INIT_TRADITIONAL_CRC32(crc);
	while (size > 0)
	{
		char		foldstr[UNICODE_CASEMAP_BUFSZ];
		int			srclen = pg_mblen(p);
		size_t		foldlen;

		/* fold one codepoint at a time */
		foldlen = pg_strfold(foldstr, UNICODE_CASEMAP_BUFSZ, p, srclen,
							 locale);

		COMP_TRADITIONAL_CRC32(crc, foldstr, foldlen);

		size -= srclen;
		p += srclen;
	}
	FIN_TRADITIONAL_CRC32(crc);
	return (unsigned int) crc;
}

#else

unsigned int
ltree_crc32_sz(const char *buf, int size)
{
	pg_crc32	crc;
	const char *p = buf;

	INIT_TRADITIONAL_CRC32(crc);
	while (size > 0)
	{
		COMP_TRADITIONAL_CRC32(crc, p, 1);
		size--;
		p++;
	}
	FIN_TRADITIONAL_CRC32(crc);
	return (unsigned int) crc;
}

#endif							/* !LOWER_NODE */