98f697b5d6905676990ce3927e85d593a6ac52b4
[oweals/gnunet.git] / src / pq / pq_query_helper.c
1  /*
2   This file is part of GNUnet
3   Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4
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.
9
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.
14  
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/>.
17 */
18 /**
19  * @file pq/pq_query_helper.c
20  * @brief functions to initialize parameter arrays
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_pq_lib.h"
26
27
28 /**
29  * Function called to convert input argument into SQL parameters.
30  *
31  * @param cls closure
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
41  */
42 static int
43 qconv_fixed (void *cls,
44              const void *data,
45              size_t data_len,
46              void *param_values[],
47              int param_lengths[],
48              int param_formats[],
49              unsigned int param_length,
50              void *scratch[],
51              unsigned int scratch_length)
52 {
53   (void) scratch;
54   (void) scratch_length;
55   GNUNET_break (NULL == cls);
56   if (1 != param_length)
57     return -1;
58   param_values[0] = (void *) data;
59   param_lengths[0] = data_len;
60   param_formats[0] = 1;
61   return 0;
62 }
63
64
65 /**
66  * Generate query parameter for a buffer @a ptr of
67  * @a ptr_size bytes.
68  *
69  * @param ptr pointer to the query parameter to pass
70  * @oaran ptr_size number of bytes in @a ptr
71  */
72 struct GNUNET_PQ_QueryParam
73 GNUNET_PQ_query_param_fixed_size (const void *ptr,
74                                   size_t ptr_size)
75 {
76   struct GNUNET_PQ_QueryParam res =
77     { &qconv_fixed, NULL, ptr, ptr_size, 1 };
78   return res;
79 }
80
81
82 /**
83  * Generate query parameter for a string.
84  *
85  * @param ptr pointer to the string query parameter to pass
86  */
87 struct GNUNET_PQ_QueryParam
88 GNUNET_PQ_query_param_string (const char *ptr)
89 {
90   return GNUNET_PQ_query_param_fixed_size (ptr, strlen (ptr));
91 }
92
93
94 /**
95  * Function called to convert input argument into SQL parameters.
96  *
97  * @param cls closure
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
107  */
108 static int
109 qconv_uint16 (void *cls,
110               const void *data,
111               size_t data_len,
112               void *param_values[],
113               int param_lengths[],
114               int param_formats[],
115               unsigned int param_length,
116               void *scratch[],
117               unsigned int scratch_length)
118 {
119   const uint16_t *u_hbo = data;
120   uint16_t *u_nbo;
121
122   (void) scratch;
123   (void) scratch_length;
124   GNUNET_break (NULL == cls);
125   if (1 != param_length)
126     return -1;
127   u_nbo = GNUNET_new (uint16_t);
128   scratch[0] = u_nbo;
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;
133   return 1;
134 }
135
136
137 /**
138  * Generate query parameter for an uint16_t in host byte order.
139  *
140  * @param x pointer to the query parameter to pass
141  */
142 struct GNUNET_PQ_QueryParam
143 GNUNET_PQ_query_param_uint16 (const uint16_t *x)
144 {
145   struct GNUNET_PQ_QueryParam res =
146     { &qconv_uint16, NULL, x, sizeof (*x), 1 };
147   return res;
148 }
149
150
151 /**
152  * Function called to convert input argument into SQL parameters.
153  *
154  * @param cls closure
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
164  */
165 static int
166 qconv_uint32 (void *cls,
167               const void *data,
168               size_t data_len,
169               void *param_values[],
170               int param_lengths[],
171               int param_formats[],
172               unsigned int param_length,
173               void *scratch[],
174               unsigned int scratch_length)
175 {
176   const uint32_t *u_hbo = data;
177   uint32_t *u_nbo;
178
179   (void) scratch;
180   (void) scratch_length;
181   GNUNET_break (NULL == cls);
182   if (1 != param_length)
183     return -1;
184   u_nbo = GNUNET_new (uint32_t);
185   scratch[0] = u_nbo;
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;
190   return 1;
191 }
192
193
194 /**
195  * Generate query parameter for an uint32_t in host byte order.
196  *
197  * @param x pointer to the query parameter to pass
198  */
199 struct GNUNET_PQ_QueryParam
200 GNUNET_PQ_query_param_uint32 (const uint32_t *x)
201 {
202   struct GNUNET_PQ_QueryParam res =
203     { &qconv_uint32, NULL, x, sizeof (*x), 1 };
204   return res;
205 }
206
207
208 /**
209  * Function called to convert input argument into SQL parameters.
210  *
211  * @param cls closure
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
221  */
222 static int
223 qconv_uint64 (void *cls,
224               const void *data,
225               size_t data_len,
226               void *param_values[],
227               int param_lengths[],
228               int param_formats[],
229               unsigned int param_length,
230               void *scratch[],
231               unsigned int scratch_length)
232 {
233   const uint64_t *u_hbo = data;
234   uint64_t *u_nbo;
235
236   (void) scratch;
237   (void) scratch_length;
238   GNUNET_break (NULL == cls);
239   if (1 != param_length)
240     return -1;
241   u_nbo = GNUNET_new (uint64_t);
242   scratch[0] = u_nbo;
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;
247   return 1;
248 }
249
250
251 /**
252  * Generate query parameter for an uint64_t in host byte order.
253  *
254  * @param x pointer to the query parameter to pass
255  */
256 struct GNUNET_PQ_QueryParam
257 GNUNET_PQ_query_param_uint64 (const uint64_t *x)
258 {
259   struct GNUNET_PQ_QueryParam res =
260     { &qconv_uint64, NULL, x, sizeof (*x), 1 };
261   return res;
262 }
263
264
265 /**
266  * Function called to convert input argument into SQL parameters.
267  *
268  * @param cls closure
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
278  */
279 static int
280 qconv_rsa_public_key (void *cls,
281                       const void *data,
282                       size_t data_len,
283                       void *param_values[],
284                       int param_lengths[],
285                       int param_formats[],
286                       unsigned int param_length,
287                       void *scratch[],
288                       unsigned int scratch_length)
289 {
290   const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
291   char *buf;
292   size_t buf_size;
293
294   GNUNET_break (NULL == cls);
295   if (1 != param_length)
296     return -1;
297   buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
298                                                   &buf);
299   scratch[0] = buf;
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;
303   return 1;
304 }
305
306
307 /**
308  * Generate query parameter for an RSA public key.  The
309  * database must contain a BLOB type in the respective position.
310  *
311  * @param x the query parameter to pass
312  * @return array entry for the query parameters to use
313  */
314 struct GNUNET_PQ_QueryParam
315 GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
316 {
317   struct GNUNET_PQ_QueryParam res =
318     { &qconv_rsa_public_key, NULL, (x), 0, 1 };
319   return res;
320 }
321
322
323 /**
324  * Function called to convert input argument into SQL parameters.
325  *
326  * @param cls closure
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
336  */
337 static int
338 qconv_rsa_signature (void *cls,
339                      const void *data,
340                      size_t data_len,
341                      void *param_values[],
342                      int param_lengths[],
343                      int param_formats[],
344                      unsigned int param_length,
345                      void *scratch[],
346                      unsigned int scratch_length)
347 {
348   const struct GNUNET_CRYPTO_RsaSignature *sig = data;
349   char *buf;
350   size_t buf_size;
351
352   GNUNET_break (NULL == cls);
353   if (1 != param_length)
354     return -1;
355   buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
356                                                  &buf);
357   scratch[0] = buf;
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;
361   return 1;
362 }
363
364
365 /**
366  * Generate query parameter for an RSA signature.  The
367  * database must contain a BLOB type in the respective position.
368  *
369  * @param x the query parameter to pass
370  * @return array entry for the query parameters to use
371  */
372 struct GNUNET_PQ_QueryParam
373 GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
374 {
375   struct GNUNET_PQ_QueryParam res =
376     { &qconv_rsa_signature, NULL, (x), 0, 1 };
377   return res;
378 }
379
380
381 /**
382  * Function called to convert input argument into SQL parameters.
383  *
384  * @param cls closure
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
394  */
395 static int
396 qconv_abs_time (void *cls,
397                 const void *data,
398                 size_t data_len,
399                 void *param_values[],
400                 int param_lengths[],
401                 int param_formats[],
402                 unsigned int param_length,
403                 void *scratch[],
404                 unsigned int scratch_length)
405 {
406   const struct GNUNET_TIME_Absolute *u = data;
407   struct GNUNET_TIME_Absolute abs;
408   uint64_t *u_nbo;
409
410   GNUNET_break (NULL == cls);
411   if (1 != param_length)
412     return -1;
413   abs = *u;
414   if (abs.abs_value_us > INT64_MAX)
415     abs.abs_value_us = INT64_MAX;
416   u_nbo = GNUNET_new (uint64_t);
417   scratch[0] = u_nbo;
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;
422   return 1;
423 }
424
425
426 /**
427  * Generate query parameter for an absolute time value.
428  * The database must store a 64-bit integer.
429  *
430  * @param x pointer to the query parameter to pass
431  * @return array entry for the query parameters to use
432  */
433 struct GNUNET_PQ_QueryParam
434 GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
435 {
436   struct GNUNET_PQ_QueryParam res =
437     { &qconv_abs_time, NULL, x, sizeof (*x), 1 };
438
439   return res;
440 }
441
442
443 /**
444  * Generate query parameter for an absolute time value.
445  * The database must store a 64-bit integer.
446  *
447  * @param x pointer to the query parameter to pass
448  */
449 struct GNUNET_PQ_QueryParam
450 GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
451 {
452   return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
453 }
454
455
456 /* end of pq_query_helper.c */