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.c520
1 files changed, 0 insertions, 520 deletions
diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c
deleted file mode 100644
index cee84d203..000000000
--- a/src/pq/pq_query_helper.c
+++ /dev/null
@@ -1,520 +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 &qconv_null, NULL, NULL, 0, 1
74 };
75
76 return res;
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[out] param_values SQL data to set
87 * @param[out] param_lengths SQL length data to set
88 * @param[out] param_formats SQL format data to set
89 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
90 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
91 * @param scratch_length number of entries left in @a scratch
92 * @return -1 on error, number of offsets used in @a scratch otherwise
93 */
94static int
95qconv_fixed (void *cls,
96 const void *data,
97 size_t data_len,
98 void *param_values[],
99 int param_lengths[],
100 int param_formats[],
101 unsigned int param_length,
102 void *scratch[],
103 unsigned int scratch_length)
104{
105 (void) scratch;
106 (void) scratch_length;
107 GNUNET_break (NULL == cls);
108 if (1 != param_length)
109 return -1;
110 param_values[0] = (void *) data;
111 param_lengths[0] = data_len;
112 param_formats[0] = 1;
113 return 0;
114}
115
116
117struct GNUNET_PQ_QueryParam
118GNUNET_PQ_query_param_fixed_size (const void *ptr,
119 size_t ptr_size)
120{
121 struct GNUNET_PQ_QueryParam res = {
122 &qconv_fixed, NULL, ptr, ptr_size, 1
123 };
124
125 return res;
126}
127
128
129struct GNUNET_PQ_QueryParam
130GNUNET_PQ_query_param_string (const char *ptr)
131{
132 return GNUNET_PQ_query_param_fixed_size (ptr,
133 strlen (ptr));
134}
135
136
137/**
138 * Function called to convert input argument into SQL parameters.
139 *
140 * @param cls closure
141 * @param data pointer to input argument
142 * @param data_len number of bytes in @a data (if applicable)
143 * @param[out] param_values SQL data to set
144 * @param[out] param_lengths SQL length data to set
145 * @param[out] param_formats SQL format data to set
146 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
147 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
148 * @param scratch_length number of entries left in @a scratch
149 * @return -1 on error, number of offsets used in @a scratch otherwise
150 */
151static int
152qconv_uint16 (void *cls,
153 const void *data,
154 size_t data_len,
155 void *param_values[],
156 int param_lengths[],
157 int param_formats[],
158 unsigned int param_length,
159 void *scratch[],
160 unsigned int scratch_length)
161{
162 const uint16_t *u_hbo = data;
163 uint16_t *u_nbo;
164
165 (void) scratch;
166 (void) scratch_length;
167 GNUNET_break (NULL == cls);
168 if (1 != param_length)
169 return -1;
170 u_nbo = GNUNET_new (uint16_t);
171 scratch[0] = u_nbo;
172 *u_nbo = htons (*u_hbo);
173 param_values[0] = (void *) u_nbo;
174 param_lengths[0] = sizeof(uint16_t);
175 param_formats[0] = 1;
176 return 1;
177}
178
179
180struct GNUNET_PQ_QueryParam
181GNUNET_PQ_query_param_uint16 (const uint16_t *x)
182{
183 struct GNUNET_PQ_QueryParam res =
184 { &qconv_uint16, NULL, x, sizeof(*x), 1 };
185
186 return res;
187}
188
189
190/**
191 * Function called to convert input argument into SQL parameters.
192 *
193 * @param cls closure
194 * @param data pointer to input argument
195 * @param data_len number of bytes in @a data (if applicable)
196 * @param[out] param_values SQL data to set
197 * @param[out] param_lengths SQL length data to set
198 * @param[out] param_formats SQL format data to set
199 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
200 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
201 * @param scratch_length number of entries left in @a scratch
202 * @return -1 on error, number of offsets used in @a scratch otherwise
203 */
204static int
205qconv_uint32 (void *cls,
206 const void *data,
207 size_t data_len,
208 void *param_values[],
209 int param_lengths[],
210 int param_formats[],
211 unsigned int param_length,
212 void *scratch[],
213 unsigned int scratch_length)
214{
215 const uint32_t *u_hbo = data;
216 uint32_t *u_nbo;
217
218 (void) scratch;
219 (void) scratch_length;
220 GNUNET_break (NULL == cls);
221 if (1 != param_length)
222 return -1;
223 u_nbo = GNUNET_new (uint32_t);
224 scratch[0] = u_nbo;
225 *u_nbo = htonl (*u_hbo);
226 param_values[0] = (void *) u_nbo;
227 param_lengths[0] = sizeof(uint32_t);
228 param_formats[0] = 1;
229 return 1;
230}
231
232
233struct GNUNET_PQ_QueryParam
234GNUNET_PQ_query_param_uint32 (const uint32_t *x)
235{
236 struct GNUNET_PQ_QueryParam res =
237 { &qconv_uint32, NULL, x, sizeof(*x), 1 };
238
239 return res;
240}
241
242
243/**
244 * Function called to convert input argument into SQL parameters.
245 *
246 * @param cls closure
247 * @param data pointer to input argument
248 * @param data_len number of bytes in @a data (if applicable)
249 * @param[out] param_values SQL data to set
250 * @param[out] param_lengths SQL length data to set
251 * @param[out] param_formats SQL format data to set
252 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
253 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
254 * @param scratch_length number of entries left in @a scratch
255 * @return -1 on error, number of offsets used in @a scratch otherwise
256 */
257static int
258qconv_uint64 (void *cls,
259 const void *data,
260 size_t data_len,
261 void *param_values[],
262 int param_lengths[],
263 int param_formats[],
264 unsigned int param_length,
265 void *scratch[],
266 unsigned int scratch_length)
267{
268 const uint64_t *u_hbo = data;
269 uint64_t *u_nbo;
270
271 (void) scratch;
272 (void) scratch_length;
273 GNUNET_break (NULL == cls);
274 if (1 != param_length)
275 return -1;
276 u_nbo = GNUNET_new (uint64_t);
277 scratch[0] = u_nbo;
278 *u_nbo = GNUNET_htonll (*u_hbo);
279 param_values[0] = (void *) u_nbo;
280 param_lengths[0] = sizeof(uint64_t);
281 param_formats[0] = 1;
282 return 1;
283}
284
285
286struct GNUNET_PQ_QueryParam
287GNUNET_PQ_query_param_uint64 (const uint64_t *x)
288{
289 struct GNUNET_PQ_QueryParam res =
290 { &qconv_uint64, NULL, x, sizeof(*x), 1 };
291
292 return res;
293}
294
295
296/**
297 * Function called to convert input argument into SQL parameters.
298 *
299 * @param cls closure
300 * @param data pointer to input argument
301 * @param data_len number of bytes in @a data (if applicable)
302 * @param[out] param_values SQL data to set
303 * @param[out] param_lengths SQL length data to set
304 * @param[out] param_formats SQL format data to set
305 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
306 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
307 * @param scratch_length number of entries left in @a scratch
308 * @return -1 on error, number of offsets used in @a scratch otherwise
309 */
310static int
311qconv_rsa_public_key (void *cls,
312 const void *data,
313 size_t data_len,
314 void *param_values[],
315 int param_lengths[],
316 int param_formats[],
317 unsigned int param_length,
318 void *scratch[],
319 unsigned int scratch_length)
320{
321 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
322 void *buf;
323 size_t buf_size;
324
325 GNUNET_break (NULL == cls);
326 if (1 != param_length)
327 return -1;
328 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
329 &buf);
330 scratch[0] = buf;
331 param_values[0] = (void *) buf;
332 param_lengths[0] = buf_size;
333 param_formats[0] = 1;
334 return 1;
335}
336
337
338struct GNUNET_PQ_QueryParam
339GNUNET_PQ_query_param_rsa_public_key (const struct
340 GNUNET_CRYPTO_RsaPublicKey *x)
341{
342 struct GNUNET_PQ_QueryParam res =
343 { &qconv_rsa_public_key, NULL, (x), 0, 1 };
344
345 return res;
346}
347
348
349/**
350 * Function called to convert input argument into SQL parameters.
351 *
352 * @param cls closure
353 * @param data pointer to input argument
354 * @param data_len number of bytes in @a data (if applicable)
355 * @param[out] param_values SQL data to set
356 * @param[out] param_lengths SQL length data to set
357 * @param[out] param_formats SQL format data to set
358 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
359 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
360 * @param scratch_length number of entries left in @a scratch
361 * @return -1 on error, number of offsets used in @a scratch otherwise
362 */
363static int
364qconv_rsa_signature (void *cls,
365 const void *data,
366 size_t data_len,
367 void *param_values[],
368 int param_lengths[],
369 int param_formats[],
370 unsigned int param_length,
371 void *scratch[],
372 unsigned int scratch_length)
373{
374 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
375 void *buf;
376 size_t buf_size;
377
378 GNUNET_break (NULL == cls);
379 if (1 != param_length)
380 return -1;
381 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
382 &buf);
383 scratch[0] = buf;
384 param_values[0] = (void *) buf;
385 param_lengths[0] = buf_size;
386 param_formats[0] = 1;
387 return 1;
388}
389
390
391struct GNUNET_PQ_QueryParam
392GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
393{
394 struct GNUNET_PQ_QueryParam res =
395 { &qconv_rsa_signature, NULL, (x), 0, 1 };
396
397 return res;
398}
399
400
401/**
402 * Function called to convert input argument into SQL parameters.
403 *
404 * @param cls closure
405 * @param data pointer to input argument
406 * @param data_len number of bytes in @a data (if applicable)
407 * @param[out] param_values SQL data to set
408 * @param[out] param_lengths SQL length data to set
409 * @param[out] param_formats SQL format data to set
410 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
411 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
412 * @param scratch_length number of entries left in @a scratch
413 * @return -1 on error, number of offsets used in @a scratch otherwise
414 */
415static int
416qconv_rel_time (void *cls,
417 const void *data,
418 size_t data_len,
419 void *param_values[],
420 int param_lengths[],
421 int param_formats[],
422 unsigned int param_length,
423 void *scratch[],
424 unsigned int scratch_length)
425{
426 const struct GNUNET_TIME_Relative *u = data;
427 struct GNUNET_TIME_Relative rel;
428 uint64_t *u_nbo;
429
430 GNUNET_break (NULL == cls);
431 if (1 != param_length)
432 return -1;
433 rel = *u;
434 if (rel.rel_value_us > INT64_MAX)
435 rel.rel_value_us = INT64_MAX;
436 u_nbo = GNUNET_new (uint64_t);
437 scratch[0] = u_nbo;
438 *u_nbo = GNUNET_htonll (rel.rel_value_us);
439 param_values[0] = (void *) u_nbo;
440 param_lengths[0] = sizeof(uint64_t);
441 param_formats[0] = 1;
442 return 1;
443}
444
445
446struct GNUNET_PQ_QueryParam
447GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x)
448{
449 struct GNUNET_PQ_QueryParam res =
450 { &qconv_rel_time, NULL, x, sizeof(*x), 1 };
451
452 return res;
453}
454
455
456/**
457 * Function called to convert input argument into SQL parameters.
458 *
459 * @param cls closure
460 * @param data pointer to input argument
461 * @param data_len number of bytes in @a data (if applicable)
462 * @param[out] param_values SQL data to set
463 * @param[out] param_lengths SQL length data to set
464 * @param[out] param_formats SQL format data to set
465 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
466 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
467 * @param scratch_length number of entries left in @a scratch
468 * @return -1 on error, number of offsets used in @a scratch otherwise
469 */
470static int
471qconv_abs_time (void *cls,
472 const void *data,
473 size_t data_len,
474 void *param_values[],
475 int param_lengths[],
476 int param_formats[],
477 unsigned int param_length,
478 void *scratch[],
479 unsigned int scratch_length)
480{
481 const struct GNUNET_TIME_Absolute *u = data;
482 struct GNUNET_TIME_Absolute abs;
483 uint64_t *u_nbo;
484
485 GNUNET_break (NULL == cls);
486 if (1 != param_length)
487 return -1;
488 abs = *u;
489 if (abs.abs_value_us > INT64_MAX)
490 abs.abs_value_us = INT64_MAX;
491 u_nbo = GNUNET_new (uint64_t);
492 scratch[0] = u_nbo;
493 *u_nbo = GNUNET_htonll (abs.abs_value_us);
494 param_values[0] = (void *) u_nbo;
495 param_lengths[0] = sizeof(uint64_t);
496 param_formats[0] = 1;
497 return 1;
498}
499
500
501struct GNUNET_PQ_QueryParam
502GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
503{
504 struct GNUNET_PQ_QueryParam res = {
505 &qconv_abs_time, NULL, x, sizeof(*x), 1
506 };
507
508 return res;
509}
510
511
512struct GNUNET_PQ_QueryParam
513GNUNET_PQ_query_param_absolute_time_nbo (const struct
514 GNUNET_TIME_AbsoluteNBO *x)
515{
516 return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
517}
518
519
520/* end of pq_query_helper.c */