-
Notifications
You must be signed in to change notification settings - Fork 0
/
inout.c
327 lines (259 loc) · 7.84 KB
/
inout.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <postgres.h>
#include <fmgr.h>
#include <libpq/pqformat.h>
#include <utils/builtins.h>
#include "crockford.h"
#include <inttypes.h>
#include <limits.h>
#define BASE32_RADIX 32
#define BASE32UINT8_LENGTH 13
#define BAD_CHAR -1
#define IGNORE_CHAR -2
#define MAX_BASE32_UINT16 65535ULL
#define MAX_BASE32_UINT32 4294967295ULL
#define MAX_BASE32_UINT64 18446744073709551615ULL
static const char
crockford_values[] = {
// 0..47
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, IGNORE_CHAR, BAD_CHAR, BAD_CHAR,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // '0'..'9'
// 58..64
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR,
10, 11, 12, 13, 14, 15, 16, 17, // 'A'..'H'
1, // 'I'
18, 19, // 'J'..'K'
1, // 'L'
20, 21, // 'M'..'N'
0, // 'O'
22, 23, 24, 25, 26, // 'P'..'T'
BAD_CHAR, // 'U'
27, 28, 29, 30, 31, // 'V'..'Z'
// 91..96
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR,
10, 11, 12, 13, 14, 15, 16, 17, // 'a'..'h'
1, // 'i'
18, 19, // 'j'..'k'
1, // 'l'
20, 21, // 'm'..'n'
0, // 'o'
22, 23, 24, 25, 26, // 'p'..'t'
BAD_CHAR, // 'u'
27, 28, 29, 30, 31, // 'v'..'z'
// 123..255
BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BAD_CHAR, BAD_CHAR,
BAD_CHAR, BAD_CHAR, BA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, BAD_CHAR, BAD_CHAR, BAD_CHAR
}; // crockford_values
static uint64
crockford_atou(const char *s, int size)
{
unsigned long long int result = 0;
bool out_of_range = false;
const bool check_uint64 = (sizeof(uint64) == size);
bool saw_digit = false;
int i = 0;
if (s == NULL)
elog(ERROR, "NULL pointer");
if (*s == 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for crockford value: \"%s\"", s)));
errno = 0;
while (s[i]) {
char c = crockford_values[(size_t)s[i]];
if (BAD_CHAR == c)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for crockford value: \"%s\"", s),
errdetail("symbol \"%c\" is invalid", s[i])));
if (IGNORE_CHAR != c) {
saw_digit = true;
if (check_uint64
&& ((MAX_BASE32_UINT64 - c)/BASE32_RADIX < result)
)
{
out_of_range = true;
break;
}
result = result * BASE32_RADIX + c;
}
i++;
}
if (!saw_digit)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for crockford value: \"%s\"", s)));
switch (size)
{
case sizeof(uint64):
if (errno == ERANGE || out_of_range)
out_of_range = true;
break;
case sizeof(uint32):
if (errno == ERANGE || result > MAX_BASE32_UINT32)
out_of_range = true;
break;
case sizeof(uint16):
if (errno == ERANGE || result > MAX_BASE32_UINT16)
out_of_range = true;
break;
default:
elog(ERROR, "unsupported result size: %d", size);
}
if (out_of_range)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%s\" is out of range for type crockfordint%d", s, size)));
return result;
}
static char*
crockford_utoa(uint64 value)
{
char *ptr;
const char *crockford_digits = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
char buf[BASE32UINT8_LENGTH + 1];
ptr = buf + sizeof(buf) - 1;
*ptr = '\0';
do {
*--ptr = crockford_digits[value % BASE32_RADIX];
value /= BASE32_RADIX;
} while (ptr > buf && value);
return pstrdup(ptr);
}
// crockford2
PG_FUNCTION_INFO_V1(crockford2in);
Datum
crockford2in(PG_FUNCTION_ARGS)
{
char *s = PG_GETARG_CSTRING(0);
PG_RETURN_UINT16(crockford_atou(s, sizeof(uint16)));
}
PG_FUNCTION_INFO_V1(crockford2out);
Datum
crockford2out(PG_FUNCTION_ARGS)
{
PG_RETURN_CSTRING(crockford_utoa((uint64)PG_GETARG_UINT16(0)));
}
PG_FUNCTION_INFO_V1(crockford2recv);
Datum
crockford2recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
PG_RETURN_UINT16((uint16) pq_getmsgint(buf, sizeof(uint16)));
}
PG_FUNCTION_INFO_V1(crockford2send);
Datum
crockford2send(PG_FUNCTION_ARGS)
{
uint16 arg1 = PG_GETARG_UINT16(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint16(&buf, arg1);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
// crockford4
PG_FUNCTION_INFO_V1(crockford4in);
Datum
crockford4in(PG_FUNCTION_ARGS)
{
char *s = PG_GETARG_CSTRING(0);
PG_RETURN_UINT32(crockford_atou(s, sizeof(uint32)));
}
PG_FUNCTION_INFO_V1(crockford4out);
Datum
crockford4out(PG_FUNCTION_ARGS)
{
PG_RETURN_CSTRING(crockford_utoa((uint64)PG_GETARG_UINT32(0)));
}
PG_FUNCTION_INFO_V1(crockford4recv);
Datum
crockford4recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
PG_RETURN_UINT32((uint32) pq_getmsgint(buf, sizeof(uint32)));
}
PG_FUNCTION_INFO_V1(crockford4send);
Datum
crockford4send(PG_FUNCTION_ARGS)
{
uint32 arg1 = PG_GETARG_UINT32(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint32(&buf, arg1);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}
// crockford8
PG_FUNCTION_INFO_V1(crockford8in);
Datum
crockford8in(PG_FUNCTION_ARGS)
{
char *s = PG_GETARG_CSTRING(0);
PG_RETURN_UINT64(crockford_atou(s, sizeof(uint64)));
}
PG_FUNCTION_INFO_V1(crockford8out);
Datum
crockford8out(PG_FUNCTION_ARGS)
{
PG_RETURN_CSTRING(crockford_utoa(PG_GETARG_UINT64(0)));
}
PG_FUNCTION_INFO_V1(crockford8recv);
Datum
crockford8recv(PG_FUNCTION_ARGS)
{
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
PG_RETURN_UINT64((uint16) pq_getmsgint64(buf));
}
PG_FUNCTION_INFO_V1(crockford8send);
Datum
crockford8send(PG_FUNCTION_ARGS)
{
uint64 arg1 = PG_GETARG_UINT64(0);
StringInfoData buf;
pq_begintypsend(&buf);
pq_sendint64(&buf, arg1);
PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}