multicast: fix replay_req_remove_cadet()
[oweals/gnunet.git] / src / sq / sq_query_helper.c
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2017 GNUnet e.V.
4
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.
8
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.
12
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/>
15 */
16 /**
17  * @file sq/sq_query_helper.c
18  * @brief helper functions for queries
19  * @author Christian Grothoff
20  */
21 #include "platform.h"
22 #include "gnunet_sq_lib.h"
23
24
25 /**
26  * Function called to convert input argument into SQL parameters.
27  *
28  * @param cls closure
29  * @param data pointer to input argument
30  * @param data_len number of bytes in @a data (if applicable)
31  * @param stmt sqlite statement to bind parameters for
32  * @param off offset of the argument to bind in @a stmt, numbered from 1,
33  *            so immediately suitable for passing to `sqlite3_bind`-functions.
34  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
35  */
36 static int
37 bind_fixed_blob (void *cls,
38                  const void *data,
39                  size_t data_len,
40                  sqlite3_stmt *stmt,
41                  unsigned int off)
42 {
43   if (SQLITE_OK !=
44       sqlite3_bind_blob64 (stmt,
45                            (int) off,
46                            data,
47                            (sqlite3_uint64) data_len,
48                            SQLITE_TRANSIENT))
49     return GNUNET_SYSERR;
50   return GNUNET_OK;
51 }
52
53
54 /**
55  * Generate query parameter for a buffer @a ptr of
56  * @a ptr_size bytes.
57  *
58  * @param ptr pointer to the query parameter to pass
59  * @oaran ptr_size number of bytes in @a ptr
60  */
61 struct GNUNET_SQ_QueryParam
62 GNUNET_SQ_query_param_fixed_size (const void *ptr,
63                                   size_t ptr_size)
64 {
65   struct GNUNET_SQ_QueryParam qp = {
66     .conv = &bind_fixed_blob,
67     .data = ptr,
68     .size = ptr_size,
69     .num_params = 1
70   };
71   return qp;
72 }
73
74
75 /**
76  * Function called to convert input argument into SQL parameters.
77  *
78  * @param cls closure
79  * @param data pointer to input argument
80  * @param data_len number of bytes in @a data (if applicable)
81  * @param stmt sqlite statement to bind parameters for
82  * @param off offset of the argument to bind in @a stmt, numbered from 1,
83  *            so immediately suitable for passing to `sqlite3_bind`-functions.
84  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
85  */
86 static int
87 bind_string (void *cls,
88              const void *data,
89              size_t data_len,
90              sqlite3_stmt *stmt,
91              unsigned int off)
92 {
93   if (SQLITE_OK !=
94       sqlite3_bind_text (stmt,
95                          (int) off,
96                          (const char *) data,
97                          -1,
98                          SQLITE_TRANSIENT))
99     return GNUNET_SYSERR;
100   return GNUNET_OK;
101 }
102
103
104 /**
105  * Generate query parameter for a string.
106  *
107  * @param ptr pointer to the string query parameter to pass
108  */
109 struct GNUNET_SQ_QueryParam
110 GNUNET_SQ_query_param_string (const char *ptr)
111 {
112   struct GNUNET_SQ_QueryParam qp = {
113     .conv = &bind_string,
114     .data = ptr,
115     .num_params = 1
116   };
117   return qp;
118 }
119
120
121 /**
122  * Function called to convert input argument into SQL parameters.
123  *
124  * @param cls closure
125  * @param data pointer to input argument
126  * @param data_len number of bytes in @a data (if applicable)
127  * @param stmt sqlite statement to bind parameters for
128  * @param off offset of the argument to bind in @a stmt, numbered from 1,
129  *            so immediately suitable for passing to `sqlite3_bind`-functions.
130  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
131  */
132 static int
133 bind_rsa_pub (void *cls,
134               const void *data,
135               size_t data_len,
136               sqlite3_stmt *stmt,
137               unsigned int off)
138 {
139   const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
140   char *buf;
141   size_t buf_size;
142
143   GNUNET_break (NULL == cls);
144   buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
145                                                   &buf);
146   if (SQLITE_OK !=
147       sqlite3_bind_blob64 (stmt,
148                            (int) off,
149                            buf,
150                            (sqlite3_uint64) buf_size,
151                            SQLITE_TRANSIENT))
152   {
153     GNUNET_free (buf);
154     return GNUNET_SYSERR;
155   }
156   GNUNET_free (buf);
157   return GNUNET_OK;
158 }
159
160
161 /**
162  * Generate query parameter for an RSA public key.  The
163  * database must contain a BLOB type in the respective position.
164  *
165  * @param x the query parameter to pass.
166  */
167 struct GNUNET_SQ_QueryParam
168 GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
169 {
170  struct GNUNET_SQ_QueryParam qp = {
171     .conv = &bind_rsa_pub,
172     .data = x,
173     .num_params = 1
174   };
175  return qp;
176 }
177
178
179 /**
180  * Function called to convert input argument into SQL parameters.
181  *
182  * @param cls closure
183  * @param data pointer to input argument
184  * @param data_len number of bytes in @a data (if applicable)
185  * @param stmt sqlite statement to bind parameters for
186  * @param off offset of the argument to bind in @a stmt, numbered from 1,
187  *            so immediately suitable for passing to `sqlite3_bind`-functions.
188  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
189  */
190 static int
191 bind_rsa_sig (void *cls,
192               const void *data,
193               size_t data_len,
194               sqlite3_stmt *stmt,
195               unsigned int off)
196 {
197   const struct GNUNET_CRYPTO_RsaSignature *sig = data;
198   char *buf;
199   size_t buf_size;
200
201   GNUNET_break (NULL == cls);
202   buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
203                                                  &buf);
204   if (SQLITE_OK !=
205       sqlite3_bind_blob64 (stmt,
206                            (int) off,
207                            buf,
208                            (sqlite3_uint64) buf_size,
209                            SQLITE_TRANSIENT))
210   {
211     GNUNET_free (buf);
212     return GNUNET_SYSERR;
213   }
214   GNUNET_free (buf);
215   return GNUNET_OK;
216 }
217
218
219 /**
220  * Generate query parameter for an RSA signature.  The
221  * database must contain a BLOB type in the respective position.
222  *
223  * @param x the query parameter to pass
224  */
225 struct GNUNET_SQ_QueryParam
226 GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
227 {
228  struct GNUNET_SQ_QueryParam qp = {
229     .conv = &bind_rsa_sig,
230     .data = x,
231     .num_params = 1
232   };
233  return qp;
234 }
235
236
237 /**
238  * Generate query parameter for an absolute time value.
239  * The database must store a 64-bit integer.
240  *
241  * @param x pointer to the query parameter to pass
242  */
243 struct GNUNET_SQ_QueryParam
244 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
245 {
246   return GNUNET_SQ_query_param_uint64 (&x->abs_value_us);
247 }
248
249
250 /**
251  * Function called to convert input argument into SQL parameters.
252  *
253  * @param cls closure
254  * @param data pointer to input argument
255  * @param data_len number of bytes in @a data (if applicable)
256  * @param stmt sqlite statement to bind parameters for
257  * @param off offset of the argument to bind in @a stmt, numbered from 1,
258  *            so immediately suitable for passing to `sqlite3_bind`-functions.
259  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
260  */
261 static int
262 bind_nbotime (void *cls,
263               const void *data,
264               size_t data_len,
265               sqlite3_stmt *stmt,
266               unsigned int off)
267 {
268   const struct GNUNET_TIME_AbsoluteNBO *u = data;
269   struct GNUNET_TIME_Absolute abs;
270
271   abs = GNUNET_TIME_absolute_ntoh (*u);
272   GNUNET_assert (sizeof (uint64_t) == data_len);
273   if (SQLITE_OK !=
274       sqlite3_bind_int64 (stmt,
275                           (int) off,
276                           (sqlite3_int64) abs.abs_value_us))
277     return GNUNET_SYSERR;
278   return GNUNET_OK;
279 }
280
281
282 /**
283  * Generate query parameter for an absolute time value.
284  * The database must store a 64-bit integer.
285  *
286  * @param x pointer to the query parameter to pass
287  */
288 struct GNUNET_SQ_QueryParam
289 GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
290 {
291  struct GNUNET_SQ_QueryParam qp = {
292     .conv = &bind_nbotime,
293     .data = x,
294     .size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
295     .num_params = 1
296   };
297   return qp;
298 }
299
300
301 /**
302  * Function called to convert input argument into SQL parameters.
303  *
304  * @param cls closure
305  * @param data pointer to input argument
306  * @param data_len number of bytes in @a data (if applicable)
307  * @param stmt sqlite statement to bind parameters for
308  * @param off offset of the argument to bind in @a stmt, numbered from 1,
309  *            so immediately suitable for passing to `sqlite3_bind`-functions.
310  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
311  */
312 static int
313 bind_u16 (void *cls,
314           const void *data,
315           size_t data_len,
316           sqlite3_stmt *stmt,
317           unsigned int off)
318 {
319   const uint16_t *u = data;
320
321   GNUNET_assert (sizeof (uint16_t) == data_len);
322   if (SQLITE_OK !=
323       sqlite3_bind_int (stmt,
324                         (int) off,
325                         (int) *u))
326     return GNUNET_SYSERR;
327   return GNUNET_OK;
328 }
329
330
331 /**
332  * Generate query parameter for an uint16_t in host byte order.
333  *
334  * @param x pointer to the query parameter to pass
335  */
336 struct GNUNET_SQ_QueryParam
337 GNUNET_SQ_query_param_uint16 (const uint16_t *x)
338 {
339  struct GNUNET_SQ_QueryParam qp = {
340     .conv = &bind_u16,
341     .data = x,
342     .size = sizeof (uint16_t),
343     .num_params = 1
344   };
345   return qp;
346 }
347
348
349 /**
350  * Function called to convert input argument into SQL parameters.
351  *
352  * @param cls closure
353  * @param data pointer to input argument
354  * @param data_len number of bytes in @a data (if applicable)
355  * @param stmt sqlite statement to bind parameters for
356  * @param off offset of the argument to bind in @a stmt, numbered from 1,
357  *            so immediately suitable for passing to `sqlite3_bind`-functions.
358  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
359  */
360 static int
361 bind_u32 (void *cls,
362           const void *data,
363           size_t data_len,
364           sqlite3_stmt *stmt,
365           unsigned int off)
366 {
367   const uint32_t *u = data;
368
369   GNUNET_assert (sizeof (uint32_t) == data_len);
370   if (SQLITE_OK !=
371       sqlite3_bind_int64 (stmt,
372                           (int) off,
373                           (sqlite3_int64) *u))
374     return GNUNET_SYSERR;
375   return GNUNET_OK;
376 }
377
378 /**
379  * Generate query parameter for an uint32_t in host byte order.
380  *
381  * @param x pointer to the query parameter to pass
382  */
383 struct GNUNET_SQ_QueryParam
384 GNUNET_SQ_query_param_uint32 (const uint32_t *x)
385 {
386  struct GNUNET_SQ_QueryParam qp = {
387     .conv = &bind_u32,
388     .data = x,
389     .size = sizeof (uint32_t),
390     .num_params = 1
391   };
392   return qp;
393 }
394
395
396 /**
397  * Function called to convert input argument into SQL parameters.
398  *
399  * @param cls closure
400  * @param data pointer to input argument
401  * @param data_len number of bytes in @a data (if applicable)
402  * @param stmt sqlite statement to bind parameters for
403  * @param off offset of the argument to bind in @a stmt, numbered from 1,
404  *            so immediately suitable for passing to `sqlite3_bind`-functions.
405  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
406  */
407 static int
408 bind_u64 (void *cls,
409           const void *data,
410           size_t data_len,
411           sqlite3_stmt *stmt,
412           unsigned int off)
413 {
414   const uint64_t *u = data;
415
416   GNUNET_assert (sizeof (uint64_t) == data_len);
417   if (SQLITE_OK !=
418       sqlite3_bind_int64 (stmt,
419                           (int) off,
420                           (sqlite3_int64) *u))
421     return GNUNET_SYSERR;
422   return GNUNET_OK;
423 }
424
425
426 /**
427  * Generate query parameter for an uint16_t in host byte order.
428  *
429  * @param x pointer to the query parameter to pass
430  */
431 struct GNUNET_SQ_QueryParam
432 GNUNET_SQ_query_param_uint64 (const uint64_t *x)
433 {
434   struct GNUNET_SQ_QueryParam qp = {
435     .conv = &bind_u64,
436     .data = x,
437     .size = sizeof (uint64_t),
438     .num_params = 1
439   };
440   return qp;
441 }
442
443 /* end of sq_query_helper.c */