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/>.
19 * @file pq/pq_query_helper.c
20 * @brief functions to initialize parameter arrays
21 * @author Christian Grothoff
24 #include "gnunet_util_lib.h"
25 #include "gnunet_pq_lib.h"
29 * Function called to convert input argument into SQL parameters.
32 * @param data pointer to input argument
33 * @param data_len number of bytes in @a data (if applicable)
34 * @param[out] param_values SQL data to set
35 * @param[out] param_lengths SQL length data to set
36 * @param[out] param_formats SQL format data to set
37 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
38 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
39 * @param scratch_length number of entries left in @a scratch
40 * @return -1 on error, number of offsets used in @a scratch otherwise
43 qconv_fixed (void *cls,
49 unsigned int param_length,
51 unsigned int scratch_length)
54 (void) scratch_length;
55 GNUNET_break (NULL == cls);
56 if (1 != param_length)
58 param_values[0] = (void *) data;
59 param_lengths[0] = data_len;
66 * Generate query parameter for a buffer @a ptr of
69 * @param ptr pointer to the query parameter to pass
70 * @oaran ptr_size number of bytes in @a ptr
72 struct GNUNET_PQ_QueryParam
73 GNUNET_PQ_query_param_fixed_size (const void *ptr,
76 struct GNUNET_PQ_QueryParam res =
77 { &qconv_fixed, NULL, ptr, ptr_size, 1 };
83 * Generate query parameter for a string.
85 * @param ptr pointer to the string query parameter to pass
87 struct GNUNET_PQ_QueryParam
88 GNUNET_PQ_query_param_string (const char *ptr)
90 return GNUNET_PQ_query_param_fixed_size (ptr, strlen (ptr));
95 * Function called to convert input argument into SQL parameters.
98 * @param data pointer to input argument
99 * @param data_len number of bytes in @a data (if applicable)
100 * @param[out] param_values SQL data to set
101 * @param[out] param_lengths SQL length data to set
102 * @param[out] param_formats SQL format data to set
103 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
104 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
105 * @param scratch_length number of entries left in @a scratch
106 * @return -1 on error, number of offsets used in @a scratch otherwise
109 qconv_uint16 (void *cls,
112 void *param_values[],
115 unsigned int param_length,
117 unsigned int scratch_length)
119 const uint16_t *u_hbo = data;
123 (void) scratch_length;
124 GNUNET_break (NULL == cls);
125 if (1 != param_length)
127 u_nbo = GNUNET_new (uint16_t);
129 *u_nbo = htons (*u_hbo);
130 param_values[0] = (void *) u_nbo;
131 param_lengths[0] = sizeof (uint16_t);
132 param_formats[0] = 1;
138 * Generate query parameter for an uint16_t in host byte order.
140 * @param x pointer to the query parameter to pass
142 struct GNUNET_PQ_QueryParam
143 GNUNET_PQ_query_param_uint16 (const uint16_t *x)
145 struct GNUNET_PQ_QueryParam res =
146 { &qconv_uint16, NULL, x, sizeof (*x), 1 };
152 * Function called to convert input argument into SQL parameters.
155 * @param data pointer to input argument
156 * @param data_len number of bytes in @a data (if applicable)
157 * @param[out] param_values SQL data to set
158 * @param[out] param_lengths SQL length data to set
159 * @param[out] param_formats SQL format data to set
160 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
161 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
162 * @param scratch_length number of entries left in @a scratch
163 * @return -1 on error, number of offsets used in @a scratch otherwise
166 qconv_uint32 (void *cls,
169 void *param_values[],
172 unsigned int param_length,
174 unsigned int scratch_length)
176 const uint32_t *u_hbo = data;
180 (void) scratch_length;
181 GNUNET_break (NULL == cls);
182 if (1 != param_length)
184 u_nbo = GNUNET_new (uint32_t);
186 *u_nbo = htonl (*u_hbo);
187 param_values[0] = (void *) u_nbo;
188 param_lengths[0] = sizeof (uint32_t);
189 param_formats[0] = 1;
195 * Generate query parameter for an uint32_t in host byte order.
197 * @param x pointer to the query parameter to pass
199 struct GNUNET_PQ_QueryParam
200 GNUNET_PQ_query_param_uint32 (const uint32_t *x)
202 struct GNUNET_PQ_QueryParam res =
203 { &qconv_uint32, NULL, x, sizeof (*x), 1 };
209 * Function called to convert input argument into SQL parameters.
212 * @param data pointer to input argument
213 * @param data_len number of bytes in @a data (if applicable)
214 * @param[out] param_values SQL data to set
215 * @param[out] param_lengths SQL length data to set
216 * @param[out] param_formats SQL format data to set
217 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
218 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
219 * @param scratch_length number of entries left in @a scratch
220 * @return -1 on error, number of offsets used in @a scratch otherwise
223 qconv_uint64 (void *cls,
226 void *param_values[],
229 unsigned int param_length,
231 unsigned int scratch_length)
233 const uint64_t *u_hbo = data;
237 (void) scratch_length;
238 GNUNET_break (NULL == cls);
239 if (1 != param_length)
241 u_nbo = GNUNET_new (uint64_t);
243 *u_nbo = GNUNET_htonll (*u_hbo);
244 param_values[0] = (void *) u_nbo;
245 param_lengths[0] = sizeof (uint64_t);
246 param_formats[0] = 1;
252 * Generate query parameter for an uint64_t in host byte order.
254 * @param x pointer to the query parameter to pass
256 struct GNUNET_PQ_QueryParam
257 GNUNET_PQ_query_param_uint64 (const uint64_t *x)
259 struct GNUNET_PQ_QueryParam res =
260 { &qconv_uint64, NULL, x, sizeof (*x), 1 };
266 * Function called to convert input argument into SQL parameters.
269 * @param data pointer to input argument
270 * @param data_len number of bytes in @a data (if applicable)
271 * @param[out] param_values SQL data to set
272 * @param[out] param_lengths SQL length data to set
273 * @param[out] param_formats SQL format data to set
274 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
275 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
276 * @param scratch_length number of entries left in @a scratch
277 * @return -1 on error, number of offsets used in @a scratch otherwise
280 qconv_rsa_public_key (void *cls,
283 void *param_values[],
286 unsigned int param_length,
288 unsigned int scratch_length)
290 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
294 GNUNET_break (NULL == cls);
295 if (1 != param_length)
297 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
300 param_values[0] = (void *) buf;
301 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
302 param_formats[0] = 1;
308 * Generate query parameter for an RSA public key. The
309 * database must contain a BLOB type in the respective position.
311 * @param x the query parameter to pass
312 * @return array entry for the query parameters to use
314 struct GNUNET_PQ_QueryParam
315 GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
317 struct GNUNET_PQ_QueryParam res =
318 { &qconv_rsa_public_key, NULL, (x), 0, 1 };
324 * Function called to convert input argument into SQL parameters.
327 * @param data pointer to input argument
328 * @param data_len number of bytes in @a data (if applicable)
329 * @param[out] param_values SQL data to set
330 * @param[out] param_lengths SQL length data to set
331 * @param[out] param_formats SQL format data to set
332 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
333 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
334 * @param scratch_length number of entries left in @a scratch
335 * @return -1 on error, number of offsets used in @a scratch otherwise
338 qconv_rsa_signature (void *cls,
341 void *param_values[],
344 unsigned int param_length,
346 unsigned int scratch_length)
348 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
352 GNUNET_break (NULL == cls);
353 if (1 != param_length)
355 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
358 param_values[0] = (void *) buf;
359 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
360 param_formats[0] = 1;
366 * Generate query parameter for an RSA signature. The
367 * database must contain a BLOB type in the respective position.
369 * @param x the query parameter to pass
370 * @return array entry for the query parameters to use
372 struct GNUNET_PQ_QueryParam
373 GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
375 struct GNUNET_PQ_QueryParam res =
376 { &qconv_rsa_signature, NULL, (x), 0, 1 };
382 * Function called to convert input argument into SQL parameters.
385 * @param data pointer to input argument
386 * @param data_len number of bytes in @a data (if applicable)
387 * @param[out] param_values SQL data to set
388 * @param[out] param_lengths SQL length data to set
389 * @param[out] param_formats SQL format data to set
390 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
391 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
392 * @param scratch_length number of entries left in @a scratch
393 * @return -1 on error, number of offsets used in @a scratch otherwise
396 qconv_abs_time (void *cls,
399 void *param_values[],
402 unsigned int param_length,
404 unsigned int scratch_length)
406 const struct GNUNET_TIME_Absolute *u = data;
407 struct GNUNET_TIME_Absolute abs;
410 GNUNET_break (NULL == cls);
411 if (1 != param_length)
414 if (abs.abs_value_us > INT64_MAX)
415 abs.abs_value_us = INT64_MAX;
416 u_nbo = GNUNET_new (uint64_t);
418 *u_nbo = GNUNET_htonll (abs.abs_value_us);
419 param_values[0] = (void *) u_nbo;
420 param_lengths[0] = sizeof (uint64_t);
421 param_formats[0] = 1;
427 * Generate query parameter for an absolute time value.
428 * The database must store a 64-bit integer.
430 * @param x pointer to the query parameter to pass
431 * @return array entry for the query parameters to use
433 struct GNUNET_PQ_QueryParam
434 GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
436 struct GNUNET_PQ_QueryParam res =
437 { &qconv_abs_time, NULL, x, sizeof (*x), 1 };
444 * Generate query parameter for an absolute time value.
445 * The database must store a 64-bit integer.
447 * @param x pointer to the query parameter to pass
449 struct GNUNET_PQ_QueryParam
450 GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
452 return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
456 /* end of pq_query_helper.c */