-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite_checksum.c
187 lines (167 loc) · 4.5 KB
/
sqlite_checksum.c
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* $Id: sqlite_checksum.c 1460 2012-11-02 18:21:27Z fabien $ */
/*
* SQLite extensions for pg_comparator.
*
* provide checksum functions: cksum2, cksum4 and cksum8.
* provide integer aggregates: xor and isum.
*/
#include <stdio.h>
#include <assert.h>
#define COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE 1
#ifdef COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE
// compile as loadable module
#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT1
#else // not loadable
#include <sqlite3.h>
#endif // COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE
// plain C implementations
#include "jenkins.c"
/******************************************************* CHECKSUMS FUNCTIONS */
static void sqlite_checksum_int2(
sqlite3_context * ctx,
int argc,
sqlite3_value ** argv)
{
assert(argc==1);
const unsigned char * txt;
size_t len;
switch (sqlite3_value_type(argv[0])) {
case SQLITE_NULL:
txt = NULL;
len = 0;
break;
case SQLITE_TEXT:
txt = sqlite3_value_text(argv[0]);
len = sqlite3_value_bytes(argv[0]);
break;
// hmmm... should I do something else?
case SQLITE_INTEGER:
case SQLITE_FLOAT:
case SQLITE_BLOB:
default:
sqlite3_result_error(ctx, "expecting TEXT or NULL", -1);
return;
}
sqlite3_result_int(ctx, checksum_int2(txt, len));
}
static void sqlite_checksum_int4(
sqlite3_context * ctx,
int argc,
sqlite3_value ** argv)
{
assert(argc==1);
const unsigned char * txt;
size_t len;
switch (sqlite3_value_type(argv[0])) {
case SQLITE_NULL:
txt = NULL;
len = 0;
break;
case SQLITE_TEXT:
txt = sqlite3_value_text(argv[0]);
len = sqlite3_value_bytes(argv[0]);
break;
// hmmm... should I do something else?
case SQLITE_INTEGER:
case SQLITE_FLOAT:
case SQLITE_BLOB:
default:
sqlite3_result_error(ctx, "expecting TEXT or NULL", -1);
return;
}
sqlite3_result_int(ctx, checksum_int4(txt, len));
}
static void sqlite_checksum_int8(
sqlite3_context * ctx,
int argc,
sqlite3_value ** argv)
{
assert(argc==1);
const unsigned char * txt;
size_t len;
switch (sqlite3_value_type(argv[0])) {
case SQLITE_NULL:
txt = NULL;
len = 0;
break;
case SQLITE_TEXT:
txt = sqlite3_value_text(argv[0]);
len = sqlite3_value_bytes(argv[0]);
break;
// hmmm... should I do something else?
case SQLITE_INTEGER:
case SQLITE_FLOAT:
case SQLITE_BLOB:
default:
sqlite3_result_error(ctx, "expecting TEXT or NULL", -1);
return;
}
sqlite3_result_int64(ctx, checksum_int8(txt, len));
}
/***************************************************** INTEGER XOR AGGREGATE */
static void ixor_step(
sqlite3_context * ctx,
int argc,
sqlite3_value ** argv)
{
assert(argc==1);
int64_t * val = sqlite3_aggregate_context(ctx, sizeof(int64_t));
if (sqlite3_value_type(argv[0])==SQLITE_INTEGER)
*val ^= sqlite3_value_int64(argv[0]);
// else just ignore...
}
// integer SUM without fancy overflow handling
static void isum_step(
sqlite3_context * ctx,
int argc,
sqlite3_value ** argv)
{
assert(argc==1);
int64_t * val = sqlite3_aggregate_context(ctx, sizeof(int64_t));
if (sqlite3_value_type(argv[0])==SQLITE_INTEGER)
*val += sqlite3_value_int64(argv[0]);
// else just ignore...
}
static void int_finalize(sqlite3_context * ctx)
{
int64_t * val = sqlite3_aggregate_context(ctx, 0);
sqlite3_result_int64(ctx, val? *val: 0);
}
/***************************************************************** AUTO LOAD */
#ifdef COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE
// autoload checksum functions
int sqlite3_extension_init(
sqlite3 * db,
char ** pzErrMsg,
const sqlite3_api_routines *pApi)
{
SQLITE_EXTENSION_INIT2(pApi);
sqlite3_create_function(db,
// name, #arg, txt, data,
"cksum2", 1, SQLITE_UTF8, NULL,
// func, step, final
sqlite_checksum_int2, NULL, NULL);
sqlite3_create_function(db,
// name, #arg, txt, data,
"cksum4", 1, SQLITE_UTF8, NULL,
// func, step, final
sqlite_checksum_int4, NULL, NULL);
sqlite3_create_function(db,
// name, #arg, txt, data,
"cksum8", 1, SQLITE_UTF8, NULL,
// func, step, final
sqlite_checksum_int8, NULL, NULL);
sqlite3_create_function(db,
// name, #args, txt, data,
"xor", 1, SQLITE_UTF8, NULL,
// func, step, final
NULL, ixor_step, int_finalize);
sqlite3_create_function(db,
// name, #args, txt, data,
"isum", 1, SQLITE_UTF8, NULL,
// func, step, final
NULL, isum_step, int_finalize);
return 0;
}
#endif // COMPILE_SQLITE_EXTENSIONS_AS_LOADABLE_MODULE