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 };
85 * Generate query parameter for a string.
87 * @param ptr pointer to the string query parameter to pass
89 struct GNUNET_PQ_QueryParam
90 GNUNET_PQ_query_param_string (const char *ptr)
92 return GNUNET_PQ_query_param_fixed_size (ptr, strlen (ptr));
97 * Function called to convert input argument into SQL parameters.
100 * @param data pointer to input argument
101 * @param data_len number of bytes in @a data (if applicable)
102 * @param[out] param_values SQL data to set
103 * @param[out] param_lengths SQL length data to set
104 * @param[out] param_formats SQL format data to set
105 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
106 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
107 * @param scratch_length number of entries left in @a scratch
108 * @return -1 on error, number of offsets used in @a scratch otherwise
111 qconv_uint16 (void *cls,
114 void *param_values[],
117 unsigned int param_length,
119 unsigned int scratch_length)
121 const uint16_t *u_hbo = data;
125 (void) scratch_length;
126 GNUNET_break (NULL == cls);
127 if (1 != param_length)
129 u_nbo = GNUNET_new (uint16_t);
131 *u_nbo = htons (*u_hbo);
132 param_values[0] = (void *) u_nbo;
133 param_lengths[0] = sizeof (uint16_t);
134 param_formats[0] = 1;
140 * Generate query parameter for an uint16_t in host byte order.
142 * @param x pointer to the query parameter to pass
144 struct GNUNET_PQ_QueryParam
145 GNUNET_PQ_query_param_uint16 (const uint16_t *x)
147 struct GNUNET_PQ_QueryParam res =
148 { &qconv_uint16, NULL, x, sizeof (*x), 1 };
154 * Function called to convert input argument into SQL parameters.
157 * @param data pointer to input argument
158 * @param data_len number of bytes in @a data (if applicable)
159 * @param[out] param_values SQL data to set
160 * @param[out] param_lengths SQL length data to set
161 * @param[out] param_formats SQL format data to set
162 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
163 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
164 * @param scratch_length number of entries left in @a scratch
165 * @return -1 on error, number of offsets used in @a scratch otherwise
168 qconv_uint32 (void *cls,
171 void *param_values[],
174 unsigned int param_length,
176 unsigned int scratch_length)
178 const uint32_t *u_hbo = data;
182 (void) scratch_length;
183 GNUNET_break (NULL == cls);
184 if (1 != param_length)
186 u_nbo = GNUNET_new (uint32_t);
188 *u_nbo = htonl (*u_hbo);
189 param_values[0] = (void *) u_nbo;
190 param_lengths[0] = sizeof (uint32_t);
191 param_formats[0] = 1;
197 * Generate query parameter for an uint32_t in host byte order.
199 * @param x pointer to the query parameter to pass
201 struct GNUNET_PQ_QueryParam
202 GNUNET_PQ_query_param_uint32 (const uint32_t *x)
204 struct GNUNET_PQ_QueryParam res =
205 { &qconv_uint32, NULL, x, sizeof (*x), 1 };
211 * Function called to convert input argument into SQL parameters.
214 * @param data pointer to input argument
215 * @param data_len number of bytes in @a data (if applicable)
216 * @param[out] param_values SQL data to set
217 * @param[out] param_lengths SQL length data to set
218 * @param[out] param_formats SQL format data to set
219 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
220 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
221 * @param scratch_length number of entries left in @a scratch
222 * @return -1 on error, number of offsets used in @a scratch otherwise
225 qconv_uint64 (void *cls,
228 void *param_values[],
231 unsigned int param_length,
233 unsigned int scratch_length)
235 const uint64_t *u_hbo = data;
239 (void) scratch_length;
240 GNUNET_break (NULL == cls);
241 if (1 != param_length)
243 u_nbo = GNUNET_new (uint64_t);
245 *u_nbo = GNUNET_htonll (*u_hbo);
246 param_values[0] = (void *) u_nbo;
247 param_lengths[0] = sizeof (uint64_t);
248 param_formats[0] = 1;
254 * Generate query parameter for an uint64_t in host byte order.
256 * @param x pointer to the query parameter to pass
258 struct GNUNET_PQ_QueryParam
259 GNUNET_PQ_query_param_uint64 (const uint64_t *x)
261 struct GNUNET_PQ_QueryParam res =
262 { &qconv_uint64, NULL, x, sizeof (*x), 1 };
268 * Function called to convert input argument into SQL parameters.
271 * @param data pointer to input argument
272 * @param data_len number of bytes in @a data (if applicable)
273 * @param[out] param_values SQL data to set
274 * @param[out] param_lengths SQL length data to set
275 * @param[out] param_formats SQL format data to set
276 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
277 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
278 * @param scratch_length number of entries left in @a scratch
279 * @return -1 on error, number of offsets used in @a scratch otherwise
282 qconv_rsa_public_key (void *cls,
285 void *param_values[],
288 unsigned int param_length,
290 unsigned int scratch_length)
292 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
296 GNUNET_break (NULL == cls);
297 if (1 != param_length)
299 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
302 param_values[0] = (void *) buf;
303 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
304 param_formats[0] = 1;
310 * Generate query parameter for an RSA public key. The
311 * database must contain a BLOB type in the respective position.
313 * @param x the query parameter to pass
314 * @return array entry for the query parameters to use
316 struct GNUNET_PQ_QueryParam
317 GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
319 struct GNUNET_PQ_QueryParam res =
320 { &qconv_rsa_public_key, NULL, (x), 0, 1 };
326 * Function called to convert input argument into SQL parameters.
329 * @param data pointer to input argument
330 * @param data_len number of bytes in @a data (if applicable)
331 * @param[out] param_values SQL data to set
332 * @param[out] param_lengths SQL length data to set
333 * @param[out] param_formats SQL format data to set
334 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
335 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
336 * @param scratch_length number of entries left in @a scratch
337 * @return -1 on error, number of offsets used in @a scratch otherwise
340 qconv_rsa_signature (void *cls,
343 void *param_values[],
346 unsigned int param_length,
348 unsigned int scratch_length)
350 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
354 GNUNET_break (NULL == cls);
355 if (1 != param_length)
357 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
360 param_values[0] = (void *) buf;
361 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
362 param_formats[0] = 1;
368 * Generate query parameter for an RSA signature. The
369 * database must contain a BLOB type in the respective position.
371 * @param x the query parameter to pass
372 * @return array entry for the query parameters to use
374 struct GNUNET_PQ_QueryParam
375 GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
377 struct GNUNET_PQ_QueryParam res =
378 { &qconv_rsa_signature, NULL, (x), 0, 1 };
384 * Function called to convert input argument into SQL parameters.
387 * @param data pointer to input argument
388 * @param data_len number of bytes in @a data (if applicable)
389 * @param[out] param_values SQL data to set
390 * @param[out] param_lengths SQL length data to set
391 * @param[out] param_formats SQL format data to set
392 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
393 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
394 * @param scratch_length number of entries left in @a scratch
395 * @return -1 on error, number of offsets used in @a scratch otherwise
398 qconv_abs_time (void *cls,
401 void *param_values[],
404 unsigned int param_length,
406 unsigned int scratch_length)
408 const struct GNUNET_TIME_Absolute *u = data;
409 struct GNUNET_TIME_Absolute abs;
412 GNUNET_break (NULL == cls);
413 if (1 != param_length)
416 if (abs.abs_value_us > INT64_MAX)
417 abs.abs_value_us = INT64_MAX;
418 u_nbo = GNUNET_new (uint64_t);
420 *u_nbo = GNUNET_htonll (abs.abs_value_us);
421 param_values[0] = (void *) u_nbo;
422 param_lengths[0] = sizeof (uint64_t);
423 param_formats[0] = 1;
429 * Generate query parameter for an absolute time value.
430 * The database must store a 64-bit integer.
432 * @param x pointer to the query parameter to pass
433 * @return array entry for the query parameters to use
435 struct GNUNET_PQ_QueryParam
436 GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
438 struct GNUNET_PQ_QueryParam res =
439 { &qconv_abs_time, NULL, x, sizeof (*x), 1 };
446 * Generate query parameter for an absolute time value.
447 * The database must store a 64-bit integer.
449 * @param x pointer to the query parameter to pass
451 struct GNUNET_PQ_QueryParam
452 GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
454 return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
458 /* end of pq_query_helper.c */