2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file pq/pq_query_helper.c
22 * @brief functions to initialize parameter arrays
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_pq_lib.h"
31 * Function called to convert input argument into SQL parameters.
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
45 qconv_fixed (void *cls,
51 unsigned int param_length,
53 unsigned int scratch_length)
56 (void) scratch_length;
57 GNUNET_break (NULL == cls);
58 if (1 != param_length)
60 param_values[0] = (void *) data;
61 param_lengths[0] = data_len;
68 * Generate query parameter for a buffer @a ptr of
71 * @param ptr pointer to the query parameter to pass
72 * @oaran ptr_size number of bytes in @a ptr
74 struct GNUNET_PQ_QueryParam
75 GNUNET_PQ_query_param_fixed_size (const void *ptr,
78 struct GNUNET_PQ_QueryParam res =
79 { &qconv_fixed, NULL, ptr, ptr_size, 1 };
86 * Generate query parameter for a string.
88 * @param ptr pointer to the string query parameter to pass
90 struct GNUNET_PQ_QueryParam
91 GNUNET_PQ_query_param_string (const char *ptr)
93 return GNUNET_PQ_query_param_fixed_size (ptr, strlen (ptr));
98 * Function called to convert input argument into SQL parameters.
101 * @param data pointer to input argument
102 * @param data_len number of bytes in @a data (if applicable)
103 * @param[out] param_values SQL data to set
104 * @param[out] param_lengths SQL length data to set
105 * @param[out] param_formats SQL format data to set
106 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
107 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
108 * @param scratch_length number of entries left in @a scratch
109 * @return -1 on error, number of offsets used in @a scratch otherwise
112 qconv_uint16 (void *cls,
115 void *param_values[],
118 unsigned int param_length,
120 unsigned int scratch_length)
122 const uint16_t *u_hbo = data;
126 (void) scratch_length;
127 GNUNET_break (NULL == cls);
128 if (1 != param_length)
130 u_nbo = GNUNET_new (uint16_t);
132 *u_nbo = htons (*u_hbo);
133 param_values[0] = (void *) u_nbo;
134 param_lengths[0] = sizeof(uint16_t);
135 param_formats[0] = 1;
141 * Generate query parameter for an uint16_t in host byte order.
143 * @param x pointer to the query parameter to pass
145 struct GNUNET_PQ_QueryParam
146 GNUNET_PQ_query_param_uint16 (const uint16_t *x)
148 struct GNUNET_PQ_QueryParam res =
149 { &qconv_uint16, NULL, x, sizeof(*x), 1 };
156 * Function called to convert input argument into SQL parameters.
159 * @param data pointer to input argument
160 * @param data_len number of bytes in @a data (if applicable)
161 * @param[out] param_values SQL data to set
162 * @param[out] param_lengths SQL length data to set
163 * @param[out] param_formats SQL format data to set
164 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
165 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
166 * @param scratch_length number of entries left in @a scratch
167 * @return -1 on error, number of offsets used in @a scratch otherwise
170 qconv_uint32 (void *cls,
173 void *param_values[],
176 unsigned int param_length,
178 unsigned int scratch_length)
180 const uint32_t *u_hbo = data;
184 (void) scratch_length;
185 GNUNET_break (NULL == cls);
186 if (1 != param_length)
188 u_nbo = GNUNET_new (uint32_t);
190 *u_nbo = htonl (*u_hbo);
191 param_values[0] = (void *) u_nbo;
192 param_lengths[0] = sizeof(uint32_t);
193 param_formats[0] = 1;
199 * Generate query parameter for an uint32_t in host byte order.
201 * @param x pointer to the query parameter to pass
203 struct GNUNET_PQ_QueryParam
204 GNUNET_PQ_query_param_uint32 (const uint32_t *x)
206 struct GNUNET_PQ_QueryParam res =
207 { &qconv_uint32, NULL, x, sizeof(*x), 1 };
214 * Function called to convert input argument into SQL parameters.
217 * @param data pointer to input argument
218 * @param data_len number of bytes in @a data (if applicable)
219 * @param[out] param_values SQL data to set
220 * @param[out] param_lengths SQL length data to set
221 * @param[out] param_formats SQL format data to set
222 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
223 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
224 * @param scratch_length number of entries left in @a scratch
225 * @return -1 on error, number of offsets used in @a scratch otherwise
228 qconv_uint64 (void *cls,
231 void *param_values[],
234 unsigned int param_length,
236 unsigned int scratch_length)
238 const uint64_t *u_hbo = data;
242 (void) scratch_length;
243 GNUNET_break (NULL == cls);
244 if (1 != param_length)
246 u_nbo = GNUNET_new (uint64_t);
248 *u_nbo = GNUNET_htonll (*u_hbo);
249 param_values[0] = (void *) u_nbo;
250 param_lengths[0] = sizeof(uint64_t);
251 param_formats[0] = 1;
257 * Generate query parameter for an uint64_t in host byte order.
259 * @param x pointer to the query parameter to pass
261 struct GNUNET_PQ_QueryParam
262 GNUNET_PQ_query_param_uint64 (const uint64_t *x)
264 struct GNUNET_PQ_QueryParam res =
265 { &qconv_uint64, NULL, x, sizeof(*x), 1 };
272 * Function called to convert input argument into SQL parameters.
275 * @param data pointer to input argument
276 * @param data_len number of bytes in @a data (if applicable)
277 * @param[out] param_values SQL data to set
278 * @param[out] param_lengths SQL length data to set
279 * @param[out] param_formats SQL format data to set
280 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
281 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
282 * @param scratch_length number of entries left in @a scratch
283 * @return -1 on error, number of offsets used in @a scratch otherwise
286 qconv_rsa_public_key (void *cls,
289 void *param_values[],
292 unsigned int param_length,
294 unsigned int scratch_length)
296 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
300 GNUNET_break (NULL == cls);
301 if (1 != param_length)
303 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
306 param_values[0] = (void *) buf;
307 param_lengths[0] = buf_size;
308 param_formats[0] = 1;
314 * Generate query parameter for an RSA public key. The
315 * database must contain a BLOB type in the respective position.
317 * @param x the query parameter to pass
318 * @return array entry for the query parameters to use
320 struct GNUNET_PQ_QueryParam
321 GNUNET_PQ_query_param_rsa_public_key (const struct
322 GNUNET_CRYPTO_RsaPublicKey *x)
324 struct GNUNET_PQ_QueryParam res =
325 { &qconv_rsa_public_key, NULL, (x), 0, 1 };
332 * Function called to convert input argument into SQL parameters.
335 * @param data pointer to input argument
336 * @param data_len number of bytes in @a data (if applicable)
337 * @param[out] param_values SQL data to set
338 * @param[out] param_lengths SQL length data to set
339 * @param[out] param_formats SQL format data to set
340 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
341 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
342 * @param scratch_length number of entries left in @a scratch
343 * @return -1 on error, number of offsets used in @a scratch otherwise
346 qconv_rsa_signature (void *cls,
349 void *param_values[],
352 unsigned int param_length,
354 unsigned int scratch_length)
356 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
360 GNUNET_break (NULL == cls);
361 if (1 != param_length)
363 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
366 param_values[0] = (void *) buf;
367 param_lengths[0] = buf_size;
368 param_formats[0] = 1;
374 * Generate query parameter for an RSA signature. The
375 * database must contain a BLOB type in the respective position.
377 * @param x the query parameter to pass
378 * @return array entry for the query parameters to use
380 struct GNUNET_PQ_QueryParam
381 GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
383 struct GNUNET_PQ_QueryParam res =
384 { &qconv_rsa_signature, NULL, (x), 0, 1 };
391 * Function called to convert input argument into SQL parameters.
394 * @param data pointer to input argument
395 * @param data_len number of bytes in @a data (if applicable)
396 * @param[out] param_values SQL data to set
397 * @param[out] param_lengths SQL length data to set
398 * @param[out] param_formats SQL format data to set
399 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
400 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
401 * @param scratch_length number of entries left in @a scratch
402 * @return -1 on error, number of offsets used in @a scratch otherwise
405 qconv_rel_time (void *cls,
408 void *param_values[],
411 unsigned int param_length,
413 unsigned int scratch_length)
415 const struct GNUNET_TIME_Relative *u = data;
416 struct GNUNET_TIME_Relative rel;
419 GNUNET_break (NULL == cls);
420 if (1 != param_length)
423 if (rel.rel_value_us > INT64_MAX)
424 rel.rel_value_us = INT64_MAX;
425 u_nbo = GNUNET_new (uint64_t);
427 *u_nbo = GNUNET_htonll (rel.rel_value_us);
428 param_values[0] = (void *) u_nbo;
429 param_lengths[0] = sizeof(uint64_t);
430 param_formats[0] = 1;
436 * Generate query parameter for a relative time value.
437 * The database must store a 64-bit integer.
439 * @param x pointer to the query parameter to pass
440 * @return array entry for the query parameters to use
442 struct GNUNET_PQ_QueryParam
443 GNUNET_PQ_query_param_relative_time (const struct GNUNET_TIME_Relative *x)
445 struct GNUNET_PQ_QueryParam res =
446 { &qconv_rel_time, NULL, x, sizeof(*x), 1 };
453 * Function called to convert input argument into SQL parameters.
456 * @param data pointer to input argument
457 * @param data_len number of bytes in @a data (if applicable)
458 * @param[out] param_values SQL data to set
459 * @param[out] param_lengths SQL length data to set
460 * @param[out] param_formats SQL format data to set
461 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
462 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
463 * @param scratch_length number of entries left in @a scratch
464 * @return -1 on error, number of offsets used in @a scratch otherwise
467 qconv_abs_time (void *cls,
470 void *param_values[],
473 unsigned int param_length,
475 unsigned int scratch_length)
477 const struct GNUNET_TIME_Absolute *u = data;
478 struct GNUNET_TIME_Absolute abs;
481 GNUNET_break (NULL == cls);
482 if (1 != param_length)
485 if (abs.abs_value_us > INT64_MAX)
486 abs.abs_value_us = INT64_MAX;
487 u_nbo = GNUNET_new (uint64_t);
489 *u_nbo = GNUNET_htonll (abs.abs_value_us);
490 param_values[0] = (void *) u_nbo;
491 param_lengths[0] = sizeof(uint64_t);
492 param_formats[0] = 1;
498 * Generate query parameter for an absolute time value.
499 * The database must store a 64-bit integer.
501 * @param x pointer to the query parameter to pass
502 * @return array entry for the query parameters to use
504 struct GNUNET_PQ_QueryParam
505 GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
507 struct GNUNET_PQ_QueryParam res =
508 { &qconv_abs_time, NULL, x, sizeof(*x), 1 };
515 * Generate query parameter for an absolute time value.
516 * The database must store a 64-bit integer.
518 * @param x pointer to the query parameter to pass
520 struct GNUNET_PQ_QueryParam
521 GNUNET_PQ_query_param_absolute_time_nbo (const struct
522 GNUNET_TIME_AbsoluteNBO *x)
524 return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
528 /* end of pq_query_helper.c */