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)
53 GNUNET_break (NULL == cls);
54 if (1 != param_length)
56 param_values[0] = (void *) data;
57 param_lengths[0] = data_len;
64 * Generate query parameter for a buffer @a ptr of
67 * @param ptr pointer to the query parameter to pass
68 * @oaran ptr_size number of bytes in @a ptr
70 struct GNUNET_PQ_QueryParam
71 GNUNET_PQ_query_param_fixed_size (const void *ptr,
74 struct GNUNET_PQ_QueryParam res =
75 { &qconv_fixed, NULL, ptr, ptr_size, 1 };
81 * Generate query parameter for a string.
83 * @param ptr pointer to the string query parameter to pass
85 struct GNUNET_PQ_QueryParam
86 GNUNET_PQ_query_param_string (const char *ptr)
88 return GNUNET_PQ_query_param_fixed_size (ptr, strlen (ptr));
93 * Function called to convert input argument into SQL parameters.
96 * @param data pointer to input argument
97 * @param data_len number of bytes in @a data (if applicable)
98 * @param[out] param_values SQL data to set
99 * @param[out] param_lengths SQL length data to set
100 * @param[out] param_formats SQL format data to set
101 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
102 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
103 * @param scratch_length number of entries left in @a scratch
104 * @return -1 on error, number of offsets used in @a scratch otherwise
107 qconv_uint16 (void *cls,
110 void *param_values[],
113 unsigned int param_length,
115 unsigned int scratch_length)
117 const uint16_t *u_hbo = data;
120 GNUNET_break (NULL == cls);
121 if (1 != param_length)
123 u_nbo = GNUNET_new (uint16_t);
125 *u_nbo = htons (*u_hbo);
126 param_values[0] = (void *) u_nbo;
127 param_lengths[0] = sizeof (uint16_t);
128 param_formats[0] = 1;
134 * Generate query parameter for an uint16_t in host byte order.
136 * @param x pointer to the query parameter to pass
138 struct GNUNET_PQ_QueryParam
139 GNUNET_PQ_query_param_uint16 (const uint16_t *x)
141 struct GNUNET_PQ_QueryParam res =
142 { &qconv_uint16, NULL, x, sizeof (*x), 1 };
148 * Function called to convert input argument into SQL parameters.
151 * @param data pointer to input argument
152 * @param data_len number of bytes in @a data (if applicable)
153 * @param[out] param_values SQL data to set
154 * @param[out] param_lengths SQL length data to set
155 * @param[out] param_formats SQL format data to set
156 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
157 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
158 * @param scratch_length number of entries left in @a scratch
159 * @return -1 on error, number of offsets used in @a scratch otherwise
162 qconv_uint32 (void *cls,
165 void *param_values[],
168 unsigned int param_length,
170 unsigned int scratch_length)
172 const uint32_t *u_hbo = data;
175 GNUNET_break (NULL == cls);
176 if (1 != param_length)
178 u_nbo = GNUNET_new (uint32_t);
180 *u_nbo = htonl (*u_hbo);
181 param_values[0] = (void *) u_nbo;
182 param_lengths[0] = sizeof (uint32_t);
183 param_formats[0] = 1;
189 * Generate query parameter for an uint32_t in host byte order.
191 * @param x pointer to the query parameter to pass
193 struct GNUNET_PQ_QueryParam
194 GNUNET_PQ_query_param_uint32 (const uint32_t *x)
196 struct GNUNET_PQ_QueryParam res =
197 { &qconv_uint32, NULL, x, sizeof (*x), 1 };
203 * Function called to convert input argument into SQL parameters.
206 * @param data pointer to input argument
207 * @param data_len number of bytes in @a data (if applicable)
208 * @param[out] param_values SQL data to set
209 * @param[out] param_lengths SQL length data to set
210 * @param[out] param_formats SQL format data to set
211 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
212 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
213 * @param scratch_length number of entries left in @a scratch
214 * @return -1 on error, number of offsets used in @a scratch otherwise
217 qconv_uint64 (void *cls,
220 void *param_values[],
223 unsigned int param_length,
225 unsigned int scratch_length)
227 const uint64_t *u_hbo = data;
230 GNUNET_break (NULL == cls);
231 if (1 != param_length)
233 u_nbo = GNUNET_new (uint64_t);
235 *u_nbo = GNUNET_htonll (*u_hbo);
236 param_values[0] = (void *) u_nbo;
237 param_lengths[0] = sizeof (uint64_t);
238 param_formats[0] = 1;
244 * Generate query parameter for an uint64_t in host byte order.
246 * @param x pointer to the query parameter to pass
248 struct GNUNET_PQ_QueryParam
249 GNUNET_PQ_query_param_uint64 (const uint64_t *x)
251 struct GNUNET_PQ_QueryParam res =
252 { &qconv_uint64, NULL, x, sizeof (*x), 1 };
258 * Function called to convert input argument into SQL parameters.
261 * @param data pointer to input argument
262 * @param data_len number of bytes in @a data (if applicable)
263 * @param[out] param_values SQL data to set
264 * @param[out] param_lengths SQL length data to set
265 * @param[out] param_formats SQL format data to set
266 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
267 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
268 * @param scratch_length number of entries left in @a scratch
269 * @return -1 on error, number of offsets used in @a scratch otherwise
272 qconv_rsa_public_key (void *cls,
275 void *param_values[],
278 unsigned int param_length,
280 unsigned int scratch_length)
282 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
286 GNUNET_break (NULL == cls);
287 if (1 != param_length)
289 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
292 param_values[0] = (void *) buf;
293 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
294 param_formats[0] = 1;
300 * Generate query parameter for an RSA public key. The
301 * database must contain a BLOB type in the respective position.
303 * @param x the query parameter to pass
304 * @return array entry for the query parameters to use
306 struct GNUNET_PQ_QueryParam
307 GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
309 struct GNUNET_PQ_QueryParam res =
310 { &qconv_rsa_public_key, NULL, (x), 0, 1 };
316 * Function called to convert input argument into SQL parameters.
319 * @param data pointer to input argument
320 * @param data_len number of bytes in @a data (if applicable)
321 * @param[out] param_values SQL data to set
322 * @param[out] param_lengths SQL length data to set
323 * @param[out] param_formats SQL format data to set
324 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
325 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
326 * @param scratch_length number of entries left in @a scratch
327 * @return -1 on error, number of offsets used in @a scratch otherwise
330 qconv_rsa_signature (void *cls,
333 void *param_values[],
336 unsigned int param_length,
338 unsigned int scratch_length)
340 const struct GNUNET_CRYPTO_RsaSignature *sig = data;
344 GNUNET_break (NULL == cls);
345 if (1 != param_length)
347 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
350 param_values[0] = (void *) buf;
351 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
352 param_formats[0] = 1;
358 * Generate query parameter for an RSA signature. The
359 * database must contain a BLOB type in the respective position.
361 * @param x the query parameter to pass
362 * @return array entry for the query parameters to use
364 struct GNUNET_PQ_QueryParam
365 GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
367 struct GNUNET_PQ_QueryParam res =
368 { &qconv_rsa_signature, NULL, (x), 0, 1 };
374 * Generate query parameter for an absolute time value.
375 * The database must store a 64-bit integer.
377 * @param x pointer to 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_absolute_time (const struct GNUNET_TIME_Absolute *x)
383 return GNUNET_PQ_query_param_uint64 (&x->abs_value_us);
388 * Generate query parameter for an absolute time value.
389 * The database must store a 64-bit integer.
391 * @param x pointer to the query parameter to pass
393 struct GNUNET_PQ_QueryParam
394 GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
396 return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
400 /* end of pq_query_helper.c */