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 under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
17 * @file pq/pq_query_helper.c
18 * @brief functions to initialize parameter arrays
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_pq_lib.h"
27 * Function called to convert input argument into SQL parameters.
30 * @param data pointer to input argument
31 * @param data_len number of bytes in @a data (if applicable)
32 * @param[out] param_values SQL data to set
33 * @param[out] param_lengths SQL length data to set
34 * @param[out] param_formats SQL format data to set
35 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
36 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
37 * @param scratch_length number of entries left in @a scratch
38 * @return -1 on error, number of offsets used in @a scratch otherwise
41 qconv_fixed (void *cls,
47 unsigned int param_length,
49 unsigned int scratch_length)
51 GNUNET_break (NULL == cls);
52 if (1 != param_length)
54 param_values[0] = (void *) data;
55 param_lengths[0] = data_len;
62 * Generate query parameter for a buffer @a ptr of
65 * @param ptr pointer to the query parameter to pass
66 * @oaran ptr_size number of bytes in @a ptr
68 struct GNUNET_PQ_QueryParam
69 GNUNET_PQ_query_param_fixed_size (const void *ptr,
72 struct GNUNET_PQ_QueryParam res =
73 { &qconv_fixed, NULL, ptr, ptr_size, 1 };
79 * Generate query parameter for a string.
81 * @param ptr pointer to the string query parameter to pass
83 struct GNUNET_PQ_QueryParam
84 GNUNET_PQ_query_param_string (const char *ptr)
86 return GNUNET_PQ_query_param_fixed_size (ptr, strlen (ptr));
91 * Function called to convert input argument into SQL parameters.
94 * @param data pointer to input argument
95 * @param data_len number of bytes in @a data (if applicable)
96 * @param[out] param_values SQL data to set
97 * @param[out] param_lengths SQL length data to set
98 * @param[out] param_formats SQL format data to set
99 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
100 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
101 * @param scratch_length number of entries left in @a scratch
102 * @return -1 on error, number of offsets used in @a scratch otherwise
105 qconv_uint16 (void *cls,
108 void *param_values[],
111 unsigned int param_length,
113 unsigned int scratch_length)
115 const uint16_t *u_hbo = data;
118 GNUNET_break (NULL == cls);
119 if (1 != param_length)
121 u_nbo = GNUNET_new (uint16_t);
123 *u_nbo = htons (*u_hbo);
124 param_values[0] = (void *) u_nbo;
125 param_lengths[0] = sizeof (uint16_t);
126 param_formats[0] = 1;
132 * Generate query parameter for an uint16_t in host byte order.
134 * @param x pointer to the query parameter to pass
136 struct GNUNET_PQ_QueryParam
137 GNUNET_PQ_query_param_uint16 (const uint16_t *x)
139 struct GNUNET_PQ_QueryParam res =
140 { &qconv_uint16, NULL, x, sizeof (*x), 1 };
146 * Function called to convert input argument into SQL parameters.
149 * @param data pointer to input argument
150 * @param data_len number of bytes in @a data (if applicable)
151 * @param[out] param_values SQL data to set
152 * @param[out] param_lengths SQL length data to set
153 * @param[out] param_formats SQL format data to set
154 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
155 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
156 * @param scratch_length number of entries left in @a scratch
157 * @return -1 on error, number of offsets used in @a scratch otherwise
160 qconv_uint32 (void *cls,
163 void *param_values[],
166 unsigned int param_length,
168 unsigned int scratch_length)
170 const uint32_t *u_hbo = data;
173 GNUNET_break (NULL == cls);
174 if (1 != param_length)
176 u_nbo = GNUNET_new (uint32_t);
178 *u_nbo = htonl (*u_hbo);
179 param_values[0] = (void *) u_nbo;
180 param_lengths[0] = sizeof (uint32_t);
181 param_formats[0] = 1;
187 * Generate query parameter for an uint32_t in host byte order.
189 * @param x pointer to the query parameter to pass
191 struct GNUNET_PQ_QueryParam
192 GNUNET_PQ_query_param_uint32 (const uint32_t *x)
194 struct GNUNET_PQ_QueryParam res =
195 { &qconv_uint32, NULL, x, sizeof (*x), 1 };
201 * Function called to convert input argument into SQL parameters.
204 * @param data pointer to input argument
205 * @param data_len number of bytes in @a data (if applicable)
206 * @param[out] param_values SQL data to set
207 * @param[out] param_lengths SQL length data to set
208 * @param[out] param_formats SQL format data to set
209 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
210 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
211 * @param scratch_length number of entries left in @a scratch
212 * @return -1 on error, number of offsets used in @a scratch otherwise
215 qconv_uint64 (void *cls,
218 void *param_values[],
221 unsigned int param_length,
223 unsigned int scratch_length)
225 const uint64_t *u_hbo = data;
228 GNUNET_break (NULL == cls);
229 if (1 != param_length)
231 u_nbo = GNUNET_new (uint64_t);
233 *u_nbo = GNUNET_htonll (*u_hbo);
234 param_values[0] = (void *) u_nbo;
235 param_lengths[0] = sizeof (uint64_t);
236 param_formats[0] = 1;
242 * Generate query parameter for an uint64_t in host byte order.
244 * @param x pointer to the query parameter to pass
246 struct GNUNET_PQ_QueryParam
247 GNUNET_PQ_query_param_uint64 (const uint64_t *x)
249 struct GNUNET_PQ_QueryParam res =
250 { &qconv_uint64, NULL, x, sizeof (*x), 1 };
256 * Function called to convert input argument into SQL parameters.
259 * @param data pointer to input argument
260 * @param data_len number of bytes in @a data (if applicable)
261 * @param[out] param_values SQL data to set
262 * @param[out] param_lengths SQL length data to set
263 * @param[out] param_formats SQL format data to set
264 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
265 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
266 * @param scratch_length number of entries left in @a scratch
267 * @return -1 on error, number of offsets used in @a scratch otherwise
270 qconv_rsa_public_key (void *cls,
273 void *param_values[],
276 unsigned int param_length,
278 unsigned int scratch_length)
280 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
284 GNUNET_break (NULL == cls);
285 if (1 != param_length)
287 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
290 param_values[0] = (void *) buf;
291 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
292 param_formats[0] = 1;
298 * Generate query parameter for an RSA public key. The
299 * database must contain a BLOB type in the respective position.
301 * @param x the query parameter to pass
302 * @return array entry for the query parameters to use
304 struct GNUNET_PQ_QueryParam
305 GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
307 struct GNUNET_PQ_QueryParam res =
308 { &qconv_rsa_public_key, NULL, (x), 0, 1 };
314 * Function called to convert input argument into SQL parameters.
317 * @param data pointer to input argument
318 * @param data_len number of bytes in @a data (if applicable)
319 * @param[out] param_values SQL data to set
320 * @param[out] param_lengths SQL length data to set
321 * @param[out] param_formats SQL format data to set
322 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
323 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
324 * @param scratch_length number of entries left in @a scratch
325 * @return -1 on error, number of offsets used in @a scratch otherwise
328 qconv_rsa_signature (void *cls,
331 void *param_values[],
334 unsigned int param_length,
336 unsigned int scratch_length)
338 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
342 GNUNET_break (NULL == cls);
343 if (1 != param_length)
345 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
348 param_values[0] = (void *) buf;
349 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
350 param_formats[0] = 1;
356 * Generate query parameter for an RSA signature. The
357 * database must contain a BLOB type in the respective position.
359 * @param x the query parameter to pass
360 * @return array entry for the query parameters to use
362 struct GNUNET_PQ_QueryParam
363 GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
365 struct GNUNET_PQ_QueryParam res =
366 { &qconv_rsa_signature, NULL, (x), 0, 1 };
372 * Generate query parameter for an absolute time value.
373 * The database must store a 64-bit integer.
375 * @param x pointer to the query parameter to pass
376 * @return array entry for the query parameters to use
378 struct GNUNET_PQ_QueryParam
379 GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
381 return GNUNET_PQ_query_param_uint64 (&x->abs_value_us);
386 * Generate query parameter for an absolute time value.
387 * The database must store a 64-bit integer.
389 * @param x pointer to the query parameter to pass
391 struct GNUNET_PQ_QueryParam
392 GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
394 return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
398 /* end of pq_query_helper.c */