diff options
Diffstat (limited to 'src/sq/sq_query_helper.c')
-rw-r--r-- | src/sq/sq_query_helper.c | 510 |
1 files changed, 0 insertions, 510 deletions
diff --git a/src/sq/sq_query_helper.c b/src/sq/sq_query_helper.c deleted file mode 100644 index 1f7662b95..000000000 --- a/src/sq/sq_query_helper.c +++ /dev/null | |||
@@ -1,510 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2017 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file sq/sq_query_helper.c | ||
22 | * @brief helper functions for queries | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_sq_lib.h" | ||
27 | |||
28 | |||
29 | /** | ||
30 | * Function called to convert input argument into SQL parameters. | ||
31 | * | ||
32 | * @param cls closure | ||
33 | * @param data pointer to input argument | ||
34 | * @param data_len number of bytes in @a data (if applicable) | ||
35 | * @param stmt sqlite statement to bind parameters for | ||
36 | * @param off offset of the argument to bind in @a stmt, numbered from 1, | ||
37 | * so immediately suitable for passing to `sqlite3_bind`-functions. | ||
38 | * @return #GNUNET_SYSERR on error, #GNUNET_OK on success | ||
39 | */ | ||
40 | static int | ||
41 | bind_fixed_blob (void *cls, | ||
42 | const void *data, | ||
43 | size_t data_len, | ||
44 | sqlite3_stmt *stmt, | ||
45 | unsigned int off) | ||
46 | { | ||
47 | if (SQLITE_OK != | ||
48 | sqlite3_bind_blob64 (stmt, | ||
49 | (int) off, | ||
50 | data, | ||
51 | (sqlite3_uint64) data_len, | ||
52 | SQLITE_TRANSIENT)) | ||
53 | return GNUNET_SYSERR; | ||
54 | return GNUNET_OK; | ||
55 | } | ||
56 | |||
57 | |||
58 | /** | ||
59 | * Generate query parameter for a buffer @a ptr of | ||
60 | * @a ptr_size bytes. | ||
61 | * | ||
62 | * @param ptr pointer to the query parameter to pass | ||
63 | * @oaran ptr_size number of bytes in @a ptr | ||
64 | */ | ||
65 | struct GNUNET_SQ_QueryParam | ||
66 | GNUNET_SQ_query_param_fixed_size (const void *ptr, | ||
67 | size_t ptr_size) | ||
68 | { | ||
69 | struct GNUNET_SQ_QueryParam qp = { | ||
70 | .conv = &bind_fixed_blob, | ||
71 | .data = ptr, | ||
72 | .size = ptr_size, | ||
73 | .num_params = 1 | ||
74 | }; | ||
75 | |||
76 | return qp; | ||
77 | } | ||
78 | |||
79 | |||
80 | /** | ||
81 | * Function called to convert input argument into SQL parameters. | ||
82 | * | ||
83 | * @param cls closure | ||
84 | * @param data pointer to input argument | ||
85 | * @param data_len number of bytes in @a data (if applicable) | ||
86 | * @param stmt sqlite statement to bind parameters for | ||
87 | * @param off offset of the argument to bind in @a stmt, numbered from 1, | ||
88 | * so immediately suitable for passing to `sqlite3_bind`-functions. | ||
89 | * @return #GNUNET_SYSERR on error, #GNUNET_OK on success | ||
90 | */ | ||
91 | static int | ||
92 | bind_string (void *cls, | ||
93 | const void *data, | ||
94 | size_t data_len, | ||
95 | sqlite3_stmt *stmt, | ||
96 | unsigned int off) | ||
97 | { | ||
98 | if (NULL == data) | ||
99 | { | ||
100 | if (SQLITE_OK != | ||
101 | sqlite3_bind_null (stmt, | ||
102 | (int) off)) | ||
103 | return GNUNET_SYSERR; | ||
104 | return GNUNET_OK; | ||
105 | } | ||
106 | if (SQLITE_OK != | ||
107 | sqlite3_bind_text (stmt, | ||
108 | (int) off, | ||
109 | (const char *) data, | ||
110 | -1, | ||
111 | SQLITE_TRANSIENT)) | ||
112 | return GNUNET_SYSERR; | ||
113 | return GNUNET_OK; | ||
114 | } | ||
115 | |||
116 | |||
117 | /** | ||
118 | * Generate query parameter for a string. | ||
119 | * | ||
120 | * @param ptr pointer to the string query parameter to pass | ||
121 | */ | ||
122 | struct GNUNET_SQ_QueryParam | ||
123 | GNUNET_SQ_query_param_string (const char *ptr) | ||
124 | { | ||
125 | struct GNUNET_SQ_QueryParam qp = { | ||
126 | .conv = &bind_string, | ||
127 | .data = ptr, | ||
128 | .num_params = 1 | ||
129 | }; | ||
130 | |||
131 | return qp; | ||
132 | } | ||
133 | |||
134 | |||
135 | /** | ||
136 | * Function called to convert input argument into SQL parameters. | ||
137 | * | ||
138 | * @param cls closure | ||
139 | * @param data pointer to input argument | ||
140 | * @param data_len number of bytes in @a data (if applicable) | ||
141 | * @param stmt sqlite statement to bind parameters for | ||
142 | * @param off offset of the argument to bind in @a stmt, numbered from 1, | ||
143 | * so immediately suitable for passing to `sqlite3_bind`-functions. | ||
144 | * @return #GNUNET_SYSERR on error, #GNUNET_OK on success | ||
145 | */ | ||
146 | static int | ||
147 | bind_rsa_pub (void *cls, | ||
148 | const void *data, | ||
149 | size_t data_len, | ||
150 | sqlite3_stmt *stmt, | ||
151 | unsigned int off) | ||
152 | { | ||
153 | const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data; | ||
154 | void *buf; | ||
155 | size_t buf_size; | ||
156 | |||
157 | GNUNET_break (NULL == cls); | ||
158 | buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, | ||
159 | &buf); | ||
160 | if (SQLITE_OK != | ||
161 | sqlite3_bind_blob64 (stmt, | ||
162 | (int) off, | ||
163 | buf, | ||
164 | (sqlite3_uint64) buf_size, | ||
165 | SQLITE_TRANSIENT)) | ||
166 | { | ||
167 | GNUNET_free (buf); | ||
168 | return GNUNET_SYSERR; | ||
169 | } | ||
170 | GNUNET_free (buf); | ||
171 | return GNUNET_OK; | ||
172 | } | ||
173 | |||
174 | |||
175 | /** | ||
176 | * Generate query parameter for an RSA public key. The | ||
177 | * database must contain a BLOB type in the respective position. | ||
178 | * | ||
179 | * @param x the query parameter to pass. | ||
180 | */ | ||
181 | struct GNUNET_SQ_QueryParam | ||
182 | GNUNET_SQ_query_param_rsa_public_key (const struct | ||
183 | GNUNET_CRYPTO_RsaPublicKey *x) | ||
184 | { | ||
185 | struct GNUNET_SQ_QueryParam qp = { | ||
186 | .conv = &bind_rsa_pub, | ||
187 | .data = x, | ||
188 | .num_params = 1 | ||
189 | }; | ||
190 | |||
191 | return qp; | ||
192 | } | ||
193 | |||
194 | |||
195 | /** | ||
196 | * Function called to convert input argument into SQL parameters. | ||
197 | * | ||
198 | * @param cls closure | ||
199 | * @param data pointer to input argument | ||
200 | * @param data_len number of bytes in @a data (if applicable) | ||
201 | * @param stmt sqlite statement to bind parameters for | ||
202 | * @param off offset of the argument to bind in @a stmt, numbered from 1, | ||
203 | * so immediately suitable for passing to `sqlite3_bind`-functions. | ||
204 | * @return #GNUNET_SYSERR on error, #GNUNET_OK on success | ||
205 | */ | ||
206 | static int | ||
207 | bind_rsa_sig (void *cls, | ||
208 | const void *data, | ||
209 | size_t data_len, | ||
210 | sqlite3_stmt *stmt, | ||
211 | unsigned int off) | ||
212 | { | ||
213 | const struct GNUNET_CRYPTO_RsaSignature *sig = data; | ||
214 | void *buf; | ||
215 | size_t buf_size; | ||
216 | |||
217 | GNUNET_break (NULL == cls); | ||
218 | buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig, | ||
219 | &buf); | ||
220 | if (SQLITE_OK != | ||
221 | sqlite3_bind_blob64 (stmt, | ||
222 | (int) off, | ||
223 | buf, | ||
224 | (sqlite3_uint64) buf_size, | ||
225 | SQLITE_TRANSIENT)) | ||
226 | { | ||
227 | GNUNET_free (buf); | ||
228 | return GNUNET_SYSERR; | ||
229 | } | ||
230 | GNUNET_free (buf); | ||
231 | return GNUNET_OK; | ||
232 | } | ||
233 | |||
234 | |||
235 | /** | ||
236 | * Generate query parameter for an RSA signature. The | ||
237 | * database must contain a BLOB type in the respective position. | ||
238 | * | ||
239 | * @param x the query parameter to pass | ||
240 | */ | ||
241 | struct GNUNET_SQ_QueryParam | ||
242 | GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x) | ||
243 | { | ||
244 | struct GNUNET_SQ_QueryParam qp = { | ||
245 | .conv = &bind_rsa_sig, | ||
246 | .data = x, | ||
247 | .num_params = 1 | ||
248 | }; | ||
249 | |||
250 | return qp; | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * Function called to convert input argument into SQL parameters. | ||
256 | * | ||
257 | * @param cls closure | ||
258 | * @param data pointer to input argument | ||
259 | * @param data_len number of bytes in @a data (if applicable) | ||
260 | * @param stmt sqlite statement to bind parameters for | ||
261 | * @param off offset of the argument to bind in @a stmt, numbered from 1, | ||
262 | * so immediately suitable for passing to `sqlite3_bind`-functions. | ||
263 | * @return #GNUNET_SYSERR on error, #GNUNET_OK on success | ||
264 | */ | ||
265 | static int | ||
266 | bind_abstime (void *cls, | ||
267 | const void *data, | ||
268 | size_t data_len, | ||
269 | sqlite3_stmt *stmt, | ||
270 | unsigned int off) | ||
271 | { | ||
272 | const struct GNUNET_TIME_Absolute *u = data; | ||
273 | struct GNUNET_TIME_Absolute abs; | ||
274 | |||
275 | abs = *u; | ||
276 | if (abs.abs_value_us > INT64_MAX) | ||
277 | abs.abs_value_us = INT64_MAX; | ||
278 | GNUNET_assert (sizeof(uint64_t) == data_len); | ||
279 | if (SQLITE_OK != | ||
280 | sqlite3_bind_int64 (stmt, | ||
281 | (int) off, | ||
282 | (sqlite3_int64) abs.abs_value_us)) | ||
283 | return GNUNET_SYSERR; | ||
284 | return GNUNET_OK; | ||
285 | } | ||
286 | |||
287 | |||
288 | /** | ||
289 | * Generate query parameter for an absolute time value. | ||
290 | * The database must store a 64-bit integer. | ||
291 | * | ||
292 | * @param x pointer to the query parameter to pass | ||
293 | */ | ||
294 | struct GNUNET_SQ_QueryParam | ||
295 | GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x) | ||
296 | { | ||
297 | struct GNUNET_SQ_QueryParam qp = { | ||
298 | .conv = &bind_abstime, | ||
299 | .data = x, | ||
300 | .size = sizeof(struct GNUNET_TIME_Absolute), | ||
301 | .num_params = 1 | ||
302 | }; | ||
303 | |||
304 | return qp; | ||
305 | } | ||
306 | |||
307 | |||
308 | /** | ||
309 | * Function called to convert input argument into SQL parameters. | ||
310 | * | ||
311 | * @param cls closure | ||
312 | * @param data pointer to input argument | ||
313 | * @param data_len number of bytes in @a data (if applicable) | ||
314 | * @param stmt sqlite statement to bind parameters for | ||
315 | * @param off offset of the argument to bind in @a stmt, numbered from 1, | ||
316 | * so immediately suitable for passing to `sqlite3_bind`-functions. | ||
317 | * @return #GNUNET_SYSERR on error, #GNUNET_OK on success | ||
318 | */ | ||
319 | static int | ||
320 | bind_nbotime (void *cls, | ||
321 | const void *data, | ||
322 | size_t data_len, | ||
323 | sqlite3_stmt *stmt, | ||
324 | unsigned int off) | ||
325 | { | ||
326 | const struct GNUNET_TIME_AbsoluteNBO *u = data; | ||
327 | struct GNUNET_TIME_Absolute abs; | ||
328 | |||
329 | abs = GNUNET_TIME_absolute_ntoh (*u); | ||
330 | if (abs.abs_value_us > INT64_MAX) | ||
331 | abs.abs_value_us = INT64_MAX; | ||
332 | GNUNET_assert (sizeof(uint64_t) == data_len); | ||
333 | if (SQLITE_OK != | ||
334 | sqlite3_bind_int64 (stmt, | ||
335 | (int) off, | ||
336 | (sqlite3_int64) abs.abs_value_us)) | ||
337 | return GNUNET_SYSERR; | ||
338 | return GNUNET_OK; | ||
339 | } | ||
340 | |||
341 | |||
342 | /** | ||
343 | * Generate query parameter for an absolute time value. | ||
344 | * The database must store a 64-bit integer. | ||
345 | * | ||
346 | * @param x pointer to the query parameter to pass | ||
347 | */ | ||
348 | struct GNUNET_SQ_QueryParam | ||
349 | GNUNET_SQ_query_param_absolute_time_nbo (const struct | ||
350 | GNUNET_TIME_AbsoluteNBO *x) | ||
351 | { | ||
352 | struct GNUNET_SQ_QueryParam qp = { | ||
353 | .conv = &bind_nbotime, | ||
354 | .data = x, | ||
355 | .size = sizeof(struct GNUNET_TIME_AbsoluteNBO), | ||
356 | .num_params = 1 | ||
357 | }; | ||
358 | |||
359 | return qp; | ||
360 | } | ||
361 | |||
362 | |||
363 | /** | ||
364 | * Function called to convert input argument into SQL parameters. | ||
365 | * | ||
366 | * @param cls closure | ||
367 | * @param data pointer to input argument | ||
368 | * @param data_len number of bytes in @a data (if applicable) | ||
369 | * @param stmt sqlite statement to bind parameters for | ||
370 | * @param off offset of the argument to bind in @a stmt, numbered from 1, | ||
371 | * so immediately suitable for passing to `sqlite3_bind`-functions. | ||
372 | * @return #GNUNET_SYSERR on error, #GNUNET_OK on success | ||
373 | */ | ||
374 | static int | ||
375 | bind_u16 (void *cls, | ||
376 | const void *data, | ||
377 | size_t data_len, | ||
378 | sqlite3_stmt *stmt, | ||
379 | unsigned int off) | ||
380 | { | ||
381 | const uint16_t *u = data; | ||
382 | |||
383 | GNUNET_assert (sizeof(uint16_t) == data_len); | ||
384 | if (SQLITE_OK != | ||
385 | sqlite3_bind_int (stmt, | ||
386 | (int) off, | ||
387 | (int) *u)) | ||
388 | return GNUNET_SYSERR; | ||
389 | return GNUNET_OK; | ||
390 | } | ||
391 | |||
392 | |||
393 | /** | ||
394 | * Generate query parameter for an uint16_t in host byte order. | ||
395 | * | ||
396 | * @param x pointer to the query parameter to pass | ||
397 | */ | ||
398 | struct GNUNET_SQ_QueryParam | ||
399 | GNUNET_SQ_query_param_uint16 (const uint16_t *x) | ||
400 | { | ||
401 | struct GNUNET_SQ_QueryParam qp = { | ||
402 | .conv = &bind_u16, | ||
403 | .data = x, | ||
404 | .size = sizeof(uint16_t), | ||
405 | .num_params = 1 | ||
406 | }; | ||
407 | |||
408 | return qp; | ||
409 | } | ||
410 | |||
411 | |||
412 | /** | ||
413 | * Function called to convert input argument into SQL parameters. | ||
414 | * | ||
415 | * @param cls closure | ||
416 | * @param data pointer to input argument | ||
417 | * @param data_len number of bytes in @a data (if applicable) | ||
418 | * @param stmt sqlite statement to bind parameters for | ||
419 | * @param off offset of the argument to bind in @a stmt, numbered from 1, | ||
420 | * so immediately suitable for passing to `sqlite3_bind`-functions. | ||
421 | * @return #GNUNET_SYSERR on error, #GNUNET_OK on success | ||
422 | */ | ||
423 | static int | ||
424 | bind_u32 (void *cls, | ||
425 | const void *data, | ||
426 | size_t data_len, | ||
427 | sqlite3_stmt *stmt, | ||
428 | unsigned int off) | ||
429 | { | ||
430 | const uint32_t *u = data; | ||
431 | |||
432 | GNUNET_assert (sizeof(uint32_t) == data_len); | ||
433 | if (SQLITE_OK != | ||
434 | sqlite3_bind_int64 (stmt, | ||
435 | (int) off, | ||
436 | (sqlite3_int64) * u)) | ||
437 | return GNUNET_SYSERR; | ||
438 | return GNUNET_OK; | ||
439 | } | ||
440 | |||
441 | |||
442 | /** | ||
443 | * Generate query parameter for an uint32_t in host byte order. | ||
444 | * | ||
445 | * @param x pointer to the query parameter to pass | ||
446 | */ | ||
447 | struct GNUNET_SQ_QueryParam | ||
448 | GNUNET_SQ_query_param_uint32 (const uint32_t *x) | ||
449 | { | ||
450 | struct GNUNET_SQ_QueryParam qp = { | ||
451 | .conv = &bind_u32, | ||
452 | .data = x, | ||
453 | .size = sizeof(uint32_t), | ||
454 | .num_params = 1 | ||
455 | }; | ||
456 | |||
457 | return qp; | ||
458 | } | ||
459 | |||
460 | |||
461 | /** | ||
462 | * Function called to convert input argument into SQL parameters. | ||
463 | * | ||
464 | * @param cls closure | ||
465 | * @param data pointer to input argument | ||
466 | * @param data_len number of bytes in @a data (if applicable) | ||
467 | * @param stmt sqlite statement to bind parameters for | ||
468 | * @param off offset of the argument to bind in @a stmt, numbered from 1, | ||
469 | * so immediately suitable for passing to `sqlite3_bind`-functions. | ||
470 | * @return #GNUNET_SYSERR on error, #GNUNET_OK on success | ||
471 | */ | ||
472 | static int | ||
473 | bind_u64 (void *cls, | ||
474 | const void *data, | ||
475 | size_t data_len, | ||
476 | sqlite3_stmt *stmt, | ||
477 | unsigned int off) | ||
478 | { | ||
479 | const uint64_t *u = data; | ||
480 | |||
481 | GNUNET_assert (sizeof(uint64_t) == data_len); | ||
482 | if (SQLITE_OK != | ||
483 | sqlite3_bind_int64 (stmt, | ||
484 | (int) off, | ||
485 | (sqlite3_int64) * u)) | ||
486 | return GNUNET_SYSERR; | ||
487 | return GNUNET_OK; | ||
488 | } | ||
489 | |||
490 | |||
491 | /** | ||
492 | * Generate query parameter for an uint16_t in host byte order. | ||
493 | * | ||
494 | * @param x pointer to the query parameter to pass | ||
495 | */ | ||
496 | struct GNUNET_SQ_QueryParam | ||
497 | GNUNET_SQ_query_param_uint64 (const uint64_t *x) | ||
498 | { | ||
499 | struct GNUNET_SQ_QueryParam qp = { | ||
500 | .conv = &bind_u64, | ||
501 | .data = x, | ||
502 | .size = sizeof(uint64_t), | ||
503 | .num_params = 1 | ||
504 | }; | ||
505 | |||
506 | return qp; | ||
507 | } | ||
508 | |||
509 | |||
510 | /* end of sq_query_helper.c */ | ||