aboutsummaryrefslogtreecommitdiff
path: root/src/pq/pq_query_helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq/pq_query_helper.c')
-rw-r--r--src/pq/pq_query_helper.c572
1 files changed, 0 insertions, 572 deletions
diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c
deleted file mode 100644
index ce8ce8f87..000000000
--- a/src/pq/pq_query_helper.c
+++ /dev/null
@@ -1,572 +0,0 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016, 2020 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 pq/pq_query_helper.c
22 * @brief functions to initialize parameter arrays
23 * @author Christian Grothoff
24 */
25#include "platform.h"
26#include "gnunet_util_lib.h"
27#include "gnunet_pq_lib.h"
28
29
30/**
31 * Function called to convert input argument into SQL parameters.
32 *
33 * @param cls closure
34 * @param data pointer to input argument
35 * @param data_len number of bytes in @a data (if applicable)
36 * @param[out] param_values SQL data to set
37 * @param[out] param_lengths SQL length data to set
38 * @param[out] param_formats SQL format data to set
39 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
40 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
41 * @param scratch_length number of entries left in @a scratch
42 * @return -1 on error, number of offsets used in @a scratch otherwise
43 */
44static int
45qconv_null (void *cls,
46 const void *data,
47 size_t data_len,
48 void *param_values[],
49 int param_lengths[],
50 int param_formats[],
51 unsigned int param_length,
52 void *scratch[],
53 unsigned int scratch_length)
54{
55 (void) scratch;
56 (void) scratch_length;
57 (void) data;
58 (void) data_len;
59 GNUNET_break (NULL == cls);
60 if (1 != param_length)
61 return -1;
62 param_values[0] = NULL;
63 param_lengths[0] = 0;
64 param_formats[0] = 1;
65 return 0;
66}
67
68
69struct GNUNET_PQ_QueryParam
70GNUNET_PQ_query_param_null (void)
71{
72 struct GNUNET_PQ_QueryParam res = {
73 .conv = &qconv_null,
74 .num_params = 1
75 };
76
77 return res;
78}
79
80
81/**
82 * Function called to convert input argument into SQL parameters.
83 *
84 * @param cls closure
85 * @param data pointer to input argument
86 * @param data_len number of bytes in @a data (if applicable)
87 * @param[out] param_values SQL data to set
88 * @param[out] param_lengths SQL length data to set
89 * @param[out] param_formats SQL format data to set
90 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
91 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
92 * @param scratch_length number of entries left in @a scratch
93 * @return -1 on error, number of offsets used in @a scratch otherwise
94 */
95static int
96qconv_fixed (void *cls,
97 const void *data,
98 size_t data_len,
99 void *param_values[],
100 int param_lengths[],
101 int param_formats[],
102 unsigned int param_length,
103 void *scratch[],
104 unsigned int scratch_length)
105{
106 (void) scratch;
107 (void) scratch_length;
108 GNUNET_break (NULL == cls);
109 if (1 != param_length)
110 return -1;
111 param_values[0] = (void *) data;
112 param_lengths[0] = data_len;
113 param_formats[0] = 1;
114 return 0;
115}
116
117
118struct GNUNET_PQ_QueryParam
119GNUNET_PQ_query_param_fixed_size (const void *ptr,
120 size_t ptr_size)
121{
122 struct GNUNET_PQ_QueryParam res = {
123 &qconv_fixed, NULL, ptr, ptr_size, 1
124 };
125
126 return res;
127}
128
129
130struct GNUNET_PQ_QueryParam
131GNUNET_PQ_query_param_string (const char *ptr)
132{
133 return GNUNET_PQ_query_param_fixed_size (ptr,
134 strlen (ptr));
135}
136
137
138struct GNUNET_PQ_QueryParam
139GNUNET_PQ_query_param_bool (bool b)
140{
141 static uint8_t bt = 1;
142 static uint8_t bf = 0;
143
144 return GNUNET_PQ_query_param_fixed_size (b ? &bt : &bf,
145 sizeof (uint8_t));
146}
147
148
149/**
150 * Function called to convert input argument into SQL parameters.
151 *
152 * @param cls closure
153 * @param data pointer to input argument
154 * @param data_len number of bytes in @a data (if applicable)
155 * @param[out] param_values SQL data to set
156 * @param[out] param_lengths SQL length data to set
157 * @param[out] param_formats SQL format data to set
158 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
159 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
160 * @param scratch_length number of entries left in @a scratch
161 * @return -1 on error, number of offsets used in @a scratch otherwise
162 */
163static int
164qconv_uint16 (void *cls,
165 const void *data,
166 size_t data_len,
167 void *param_values[],
168 int param_lengths[],
169 int param_formats[],
170 unsigned int param_length,
171 void *scratch[],
172 unsigned int scratch_length)
173{
174 const uint16_t *u_hbo = data;
175 uint16_t *u_nbo;
176
177 (void) scratch;
178 (void) scratch_length;
179 GNUNET_break (NULL == cls);
180 if (1 != param_length)
181 return -1;
182 u_nbo = GNUNET_new (uint16_t);
183 scratch[0] = u_nbo;
184 *u_nbo = htons (*u_hbo);
185 param_values[0] = (void *) u_nbo;
186 param_lengths[0] = sizeof(uint16_t);
187 param_formats[0] = 1;
188 return 1;
189}
190
191
192struct GNUNET_PQ_QueryParam
193GNUNET_PQ_query_param_uint16 (const uint16_t *x)
194{
195 struct GNUNET_PQ_QueryParam res = {
196 .conv = &qconv_uint16,
197 .data = x,
198 .size = sizeof(*x),
199 .num_params = 1
200 };
201
202 return res;
203}
204
205
206/**
207 * Function called to convert input argument into SQL parameters.
208 *
209 * @param cls closure
210 * @param data pointer to input argument
211 * @param data_len number of bytes in @a data (if applicable)
212 * @param[out] param_values SQL data to set
213 * @param[out] param_lengths SQL length data to set
214 * @param[out] param_formats SQL format data to set
215 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
216 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
217 * @param scratch_length number of entries left in @a scratch
218 * @return -1 on error, number of offsets used in @a scratch otherwise
219 */
220static int
221qconv_uint32 (void *cls,
222 const void *data,
223 size_t data_len,
224 void *param_values[],
225 int param_lengths[],
226 int param_formats[],
227 unsigned int param_length,
228 void *scratch[],
229 unsigned int scratch_length)
230{
231 const uint32_t *u_hbo = data;
232 uint32_t *u_nbo;
233
234 (void) scratch;
235 (void) scratch_length;
236 GNUNET_break (NULL == cls);
237 if (1 != param_length)
238 return -1;
239 u_nbo = GNUNET_new (uint32_t);
240 scratch[0] = u_nbo;
241 *u_nbo = htonl (*u_hbo);
242 param_values[0] = (void *) u_nbo;
243 param_lengths[0] = sizeof(uint32_t);
244 param_formats[0] = 1;
245 return 1;
246}
247
248
249struct GNUNET_PQ_QueryParam
250GNUNET_PQ_query_param_uint32 (const uint32_t *x)
251{
252 struct GNUNET_PQ_QueryParam res = {
253 .conv = &qconv_uint32,
254 .data = x,
255 .size = sizeof(*x),
256 .num_params = 1
257 };
258
259 return res;
260}
261
262
263/**
264 * Function called to convert input argument into SQL parameters.
265 *
266 * @param cls closure
267 * @param data pointer to input argument
268 * @param data_len number of bytes in @a data (if applicable)
269 * @param[out] param_values SQL data to set
270 * @param[out] param_lengths SQL length data to set
271 * @param[out] param_formats SQL format data to set
272 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
273 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
274 * @param scratch_length number of entries left in @a scratch
275 * @return -1 on error, number of offsets used in @a scratch otherwise
276 */
277static int
278qconv_uint64 (void *cls,
279 const void *data,
280 size_t data_len,
281 void *param_values[],
282 int param_lengths[],
283 int param_formats[],
284 unsigned int param_length,
285 void *scratch[],
286 unsigned int scratch_length)
287{
288 const uint64_t *u_hbo = data;
289 uint64_t *u_nbo;
290
291 (void) scratch;
292 (void) scratch_length;
293 GNUNET_break (NULL == cls);
294 if (1 != param_length)
295 return -1;
296 u_nbo = GNUNET_new (uint64_t);
297 scratch[0] = u_nbo;
298 *u_nbo = GNUNET_htonll (*u_hbo);
299 param_values[0] = (void *) u_nbo;
300 param_lengths[0] = sizeof(uint64_t);
301 param_formats[0] = 1;
302 return 1;
303}
304
305
306struct GNUNET_PQ_QueryParam
307GNUNET_PQ_query_param_uint64 (const uint64_t *x)
308{
309 struct GNUNET_PQ_QueryParam res = {
310 .conv = &qconv_uint64,
311 .data = x,
312 .size = sizeof(*x),
313 .num_params = 1
314 };
315
316 return res;
317}
318
319
320/**
321 * Function called to convert input argument into SQL parameters.
322 *
323 * @param cls closure
324 * @param data pointer to input argument
325 * @param data_len number of bytes in @a data (if applicable)
326 * @param[out] param_values SQL data to set
327 * @param[out] param_lengths SQL length data to set
328 * @param[out] param_formats SQL format data to set
329 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
330 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
331 * @param scratch_length number of entries left in @a scratch
332 * @return -1 on error, number of offsets used in @a scratch otherwise
333 */
334static int
335qconv_rsa_public_key (void *cls,
336 const void *data,
337 size_t data_len,
338 void *param_values[],
339 int param_lengths[],
340 int param_formats[],
341 unsigned int param_length,
342 void *scratch[],
343 unsigned int scratch_length)
344{
345 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
346 void *buf;
347 size_t buf_size;
348
349 GNUNET_break (NULL == cls);
350 if (1 != param_length)
351 return -1;
352 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
353 &buf);
354 scratch[0] = buf;
355 param_values[0] = (void *) buf;
356 param_lengths[0] = buf_size;
357 param_formats[0] = 1;
358 return 1;
359}
360
361
362struct GNUNET_PQ_QueryParam
363GNUNET_PQ_query_param_rsa_public_key (
364 const struct GNUNET_CRYPTO_RsaPublicKey *x)
365{
366 struct GNUNET_PQ_QueryParam res = {
367 .conv = &qconv_rsa_public_key,
368 .data = x,
369 .num_params = 1
370 };
371
372 return res;
373}
374
375
376/**
377 * Function called to convert input argument into SQL parameters.
378 *
379 * @param cls closure
380 * @param data pointer to input argument
381 * @param data_len number of bytes in @a data (if applicable)
382 * @param[out] param_values SQL data to set
383 * @param[out] param_lengths SQL length data to set
384 * @param[out] param_formats SQL format data to set
385 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
386 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
387 * @param scratch_length number of entries left in @a scratch
388 * @return -1 on error, number of offsets used in @a scratch otherwise
389 */
390static int
391qconv_rsa_signature (void *cls,
392 const void *data,
393 size_t data_len,
394 void *param_values[],
395 int param_lengths[],
396 int param_formats[],
397 unsigned int param_length,
398 void *scratch[],
399 unsigned int scratch_length)
400{
401 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
402 void *buf;
403 size_t buf_size;
404
405 GNUNET_break (NULL == cls);
406 if (1 != param_length)
407 return -1;
408 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
409 &buf);
410 scratch[0] = buf;
411 param_values[0] = (void *) buf;
412 param_lengths[0] = buf_size;
413 param_formats[0] = 1;
414 return 1;
415}
416
417
418struct GNUNET_PQ_QueryParam
419GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
420{
421 struct GNUNET_PQ_QueryParam res = {
422 .conv = &qconv_rsa_signature,
423 .data = x,
424 .num_params = 1
425 };
426
427 return res;
428}
429
430
431/**
432 * Function called to convert input argument into SQL parameters.
433 *
434 * @param cls closure
435 * @param data pointer to input argument
436 * @param data_len number of bytes in @a data (if applicable)
437 * @param[out] param_values SQL data to set
438 * @param[out] param_lengths SQL length data to set
439 * @param[out] param_formats SQL format data to set
440 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
441 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
442 * @param scratch_length number of entries left in @a scratch
443 * @return -1 on error, number of offsets used in @a scratch otherwise
444 */
445static int
446qconv_rel_time (void *cls,
447 const void *data,
448 size_t data_len,
449 void *param_values[],
450 int param_lengths[],
451 int param_formats[],
452 unsigned int param_length,
453 void *scratch[],
454 unsigned int scratch_length)
455{
456 const struct GNUNET_TIME_Relative *u = data;
457 struct GNUNET_TIME_Relative rel;
458 uint64_t *u_nbo;
459
460 GNUNET_break (NULL == cls);
461 if (1 != param_length)
462 return -1;
463 rel = *u;
464 if (rel.rel_value_us > INT64_MAX)
465 rel.rel_value_us = INT64_MAX;
466 u_nbo = GNUNET_new (uint64_t);
467 scratch[0] = u_nbo;
468 *u_nbo = GNUNET_htonll (rel.rel_value_us);
469 param_values[0] = (void *) u_nbo;
470 param_lengths[0] = sizeof(uint64_t);
471 param_formats[0] = 1;
472 return 1;
473}
474
475
476struct GNUNET_PQ_QueryParam
477GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x)
478{
479 struct GNUNET_PQ_QueryParam res = {
480 .conv = &qconv_rel_time,
481 .data = x,
482 .size = sizeof(*x),
483 .num_params = 1
484 };
485
486 return res;
487}
488
489
490/**
491 * Function called to convert input argument into SQL parameters.
492 *
493 * @param cls closure
494 * @param data pointer to input argument
495 * @param data_len number of bytes in @a data (if applicable)
496 * @param[out] param_values SQL data to set
497 * @param[out] param_lengths SQL length data to set
498 * @param[out] param_formats SQL format data to set
499 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
500 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
501 * @param scratch_length number of entries left in @a scratch
502 * @return -1 on error, number of offsets used in @a scratch otherwise
503 */
504static int
505qconv_abs_time (void *cls,
506 const void *data,
507 size_t data_len,
508 void *param_values[],
509 int param_lengths[],
510 int param_formats[],
511 unsigned int param_length,
512 void *scratch[],
513 unsigned int scratch_length)
514{
515 const struct GNUNET_TIME_Absolute *u = data;
516 struct GNUNET_TIME_Absolute abs;
517 uint64_t *u_nbo;
518
519 GNUNET_break (NULL == cls);
520 if (1 != param_length)
521 return -1;
522 abs = *u;
523 if (abs.abs_value_us > INT64_MAX)
524 abs.abs_value_us = INT64_MAX;
525 u_nbo = GNUNET_new (uint64_t);
526 scratch[0] = u_nbo;
527 *u_nbo = GNUNET_htonll (abs.abs_value_us);
528 param_values[0] = (void *) u_nbo;
529 param_lengths[0] = sizeof(uint64_t);
530 param_formats[0] = 1;
531 return 1;
532}
533
534
535struct GNUNET_PQ_QueryParam
536GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
537{
538 struct GNUNET_PQ_QueryParam res = {
539 .conv = &qconv_abs_time,
540 .data = x,
541 .size = sizeof(*x),
542 .num_params = 1
543 };
544
545 return res;
546}
547
548
549struct GNUNET_PQ_QueryParam
550GNUNET_PQ_query_param_absolute_time_nbo (
551 const struct GNUNET_TIME_AbsoluteNBO *x)
552{
553 return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
554}
555
556
557struct GNUNET_PQ_QueryParam
558GNUNET_PQ_query_param_timestamp (const struct GNUNET_TIME_Timestamp *x)
559{
560 return GNUNET_PQ_query_param_absolute_time (&x->abs_time);
561}
562
563
564struct GNUNET_PQ_QueryParam
565GNUNET_PQ_query_param_timestamp_nbo (
566 const struct GNUNET_TIME_TimestampNBO *x)
567{
568 return GNUNET_PQ_query_param_absolute_time_nbo (&x->abs_time_nbo);
569}
570
571
572/* end of pq_query_helper.c */