RPS: Forgot to add header
[oweals/gnunet.git] / src / my / my_query_helper.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 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      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 /**
21  * @file my/my_query_helper.c
22  * @brief library to help with access to a MySQL database
23  * @author Christian Grothoff
24  * @author Christophe Genevey
25  */
26 #include "platform.h"
27 #include <mysql/mysql.h>
28 #include "gnunet_my_lib.h"
29
30
31 /**
32  * Function called to clean up memory allocated
33  * by a #GNUNET_MY_QueryConverter.
34  *
35  * @param cls closure
36  * @param qbind array of parameter to clean up
37  */
38 static void
39 my_clean_query (void *cls,
40                 MYSQL_BIND *qbind)
41 {
42   (void) cls;
43   GNUNET_free (qbind[0].buffer);
44 }
45
46
47 /**
48  * Function called to convert input argument into SQL parameters.
49  *
50  * @param cls closure
51  * @param pq data about the query
52  * @param qbind array of parameters to initialize
53  * @return -1 on error
54  */
55 static int
56 my_conv_fixed_size (void *cls,
57                     const struct GNUNET_MY_QueryParam *qp,
58                     MYSQL_BIND *qbind)
59 {
60   (void) cls;
61   GNUNET_assert (1 == qp->num_params);
62   qbind->buffer = (void *) qp->data;
63   qbind->buffer_length = qp->data_len;
64   qbind->buffer_type = MYSQL_TYPE_BLOB;
65
66   return 1;
67 }
68
69
70 /**
71  * Generate query parameter for a buffer @a ptr of
72  * @a ptr_size bytes.
73  *
74  * @param ptr pointer to the query parameter to pass
75  * @param ptr_size number of bytes in @a ptr
76  */
77 struct GNUNET_MY_QueryParam
78 GNUNET_MY_query_param_fixed_size (const void *ptr,
79                                   size_t ptr_size)
80 {
81   struct GNUNET_MY_QueryParam qp = {
82     .conv = &my_conv_fixed_size,
83     .cleaner = NULL,
84     .conv_cls = NULL,
85     .num_params = 1,
86     .data = ptr,
87     .data_len = (unsigned long) ptr_size
88   };
89   return qp;
90 }
91
92
93 /**
94  * Function called to convert input argument into SQL parameters.
95  *
96  * @param cls closure
97  * @param pq data about the query
98  * @param qbind array of parameters to initialize
99  * @return -1 on error
100  */
101 static int
102 my_conv_string (void *cls,
103                 const struct GNUNET_MY_QueryParam *qp,
104                 MYSQL_BIND *qbind)
105 {
106   (void) cls;
107   GNUNET_assert (1 == qp->num_params);
108   qbind->buffer = (void *) qp->data;
109   qbind->buffer_length = qp->data_len;
110   qbind->buffer_type = MYSQL_TYPE_STRING;
111   return 1;
112 }
113
114
115 /**
116  * Generate query parameter for a string
117  *
118  * @param ptr pointer to the string query parameter to pass
119  */
120 struct GNUNET_MY_QueryParam
121 GNUNET_MY_query_param_string (const char *ptr)
122 {
123   struct GNUNET_MY_QueryParam qp = {
124     .conv = &my_conv_string,
125     .cleaner = NULL,
126     .conv_cls = NULL,
127     .num_params = 1,
128     .data = ptr,
129     .data_len = strlen (ptr)
130   };
131   return qp;
132 }
133
134
135 /**
136  * Function called to convert input argument into SQL parameters
137  *
138  * @param cls closure
139  * @param pq data about the query
140  * @param qbind array of parameters to initialize
141  * @return -1 on error
142  */
143 static int
144 my_conv_uint16 (void *cls,
145                 const struct GNUNET_MY_QueryParam *qp,
146                 MYSQL_BIND *qbind)
147 {
148   (void) cls;
149   GNUNET_assert (1 == qp->num_params);
150   qbind->buffer = (void *) qp->data;
151   qbind->buffer_length = sizeof (uint16_t);
152   qbind->buffer_type = MYSQL_TYPE_SHORT;
153   qbind->is_unsigned = 1;
154   return 1;
155 }
156
157
158 /**
159  * Generate query parameter for an uint16_t in host byte order.
160  *
161  * @param x pointer to the query parameter to pass
162  */
163 struct GNUNET_MY_QueryParam
164 GNUNET_MY_query_param_uint16 (const uint16_t *x)
165 {
166   struct GNUNET_MY_QueryParam res = {
167     .conv = &my_conv_uint16,
168     .cleaner = NULL,
169     .conv_cls = NULL,
170     .num_params = 1,
171     .data = x,
172     .data_len = sizeof (*x)
173   };
174
175   return res;
176 }
177
178
179 /**
180  * Function called to convert input argument into SQL parameters
181  *
182  * @param cls closure
183  * @param pq data about the query
184  * @param qbind array of parameters to initialize
185  * @return -1 on error
186  */
187 static int
188 my_conv_uint32 (void *cls,
189                 const struct GNUNET_MY_QueryParam *qp,
190                 MYSQL_BIND *qbind)
191 {
192   (void) cls;
193   GNUNET_assert (1 == qp->num_params);
194   qbind->buffer = (void *) qp->data;
195   qbind->buffer_length = sizeof(uint32_t);
196   qbind->buffer_type = MYSQL_TYPE_LONG;
197   qbind->is_unsigned = 1;
198   return 1;
199 }
200
201
202 /**
203  * Generate query parameter for an uint32_t in host byte order
204  *
205  * @param x pointer to the query parameter to pass
206  */
207 struct GNUNET_MY_QueryParam
208 GNUNET_MY_query_param_uint32 (const uint32_t *x)
209 {
210   struct GNUNET_MY_QueryParam res = {
211     .conv = &my_conv_uint32,
212     .cleaner = NULL,
213     .conv_cls = NULL,
214     .num_params = 1,
215     .data = x,
216     .data_len = sizeof (*x)
217   };
218
219   return res;
220 }
221
222
223 /**
224  * Function called to convert input argument into SQL parameters
225  *
226  * @param cls closure
227  * @param pq data about the query
228  * @param qbind array of parameters to initialize
229  * @return -1 on error
230  */
231 static int
232 my_conv_uint64 (void *cls,
233                 const struct GNUNET_MY_QueryParam *qp,
234                 MYSQL_BIND * qbind)
235 {
236   (void) cls;
237   GNUNET_assert (1 == qp->num_params);
238   qbind->buffer = (void *) qp->data;
239   qbind->buffer_length = sizeof (uint64_t);
240   qbind->buffer_type = MYSQL_TYPE_LONGLONG;
241   qbind->is_unsigned = 1;
242   return 1;
243 }
244
245
246 /**
247  * Generate query parameter for an uint64_t in host byte order
248  *
249  * @param x pointer to the query parameter to pass
250  */
251 struct GNUNET_MY_QueryParam
252 GNUNET_MY_query_param_uint64 (const uint64_t *x)
253 {
254   struct GNUNET_MY_QueryParam res = {
255     .conv = &my_conv_uint64,
256     .cleaner = NULL,
257     .conv_cls = NULL,
258     .num_params = 1,
259     .data = x,
260     .data_len = sizeof(*x)
261   };
262
263   return res;
264 }
265
266
267 /**
268  * Function called to convert input argument into SQL parameters
269  *
270  * @param cls closure
271  * @param pq data about the query
272  * @param qbind array of parameters to initialize
273  * @return -1 on error
274  */
275 static int
276 my_conv_rsa_public_key (void *cls,
277                         const struct GNUNET_MY_QueryParam *qp,
278                         MYSQL_BIND * qbind)
279 {
280   const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
281   char *buf;
282   size_t buf_size;
283
284   (void) cls;
285   GNUNET_assert(1 == qp->num_params);
286   buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
287                                                   &buf);
288   qbind->buffer = (void *) buf;
289   qbind->buffer_length = buf_size;
290   qbind->buffer_type = MYSQL_TYPE_BLOB;
291   return 1;
292 }
293
294
295 /**
296  * Generate query parameter for an RSA public key. The
297  * database must contain a BLOB type in the respective position.
298  *
299  * @param x the query parameter to pass
300  * @return array entry for the query parameters to use
301  */
302 struct GNUNET_MY_QueryParam
303 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
304 {
305   struct GNUNET_MY_QueryParam res = {
306     .conv = &my_conv_rsa_public_key,
307     .cleaner = &my_clean_query,
308     .conv_cls = NULL,
309     .num_params = 1,
310     .data = x,
311     .data_len = 0
312   };
313
314   return res;
315 }
316
317
318 /**
319   * Function called to convert input argument into SQL parameters
320   *
321   *@param cls closure
322   *@param pq data about the query
323   *@param qbind array of parameters to initialize
324   *@return -1 on error
325   */
326 static int
327 my_conv_rsa_signature (void *cls,
328                        const struct GNUNET_MY_QueryParam *qp,
329                        MYSQL_BIND *qbind)
330 {
331   const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
332   char *buf;
333   size_t buf_size;
334
335   (void) cls;
336   GNUNET_assert(1 == qp->num_params);
337   buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
338                                                  &buf);
339   qbind->buffer = (void *) buf;
340   qbind->buffer_length = buf_size;
341   qbind->buffer_type = MYSQL_TYPE_BLOB;
342   return 1;
343 }
344
345
346 /**
347  * Generate query parameter for an RSA signature. The
348  * database must contain a BLOB type in the respective position
349  *
350  * @param x the query parameter to pass
351  * @return array entry for the query parameters to use
352  */
353 struct GNUNET_MY_QueryParam
354 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
355 {
356   struct GNUNET_MY_QueryParam res = {
357     .conv = &my_conv_rsa_signature,
358     .cleaner = &my_clean_query,
359     .conv_cls = NULL,
360     .num_params = 1,
361     .data = (x),
362     .data_len = 0
363   };
364   return res;
365 }
366
367
368 /**
369  * Generate query parameter for an absolute time value.
370  * The database must store a 64-bit integer.
371  *
372  * @param x pointer to the query parameter to pass
373  * @return array entry for the query parameters to use
374  */
375 struct GNUNET_MY_QueryParam
376 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
377 {
378   return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
379 }
380
381
382 /**
383  * Generate query parameter for an absolute time value.
384  * The database must store a 64-bit integer.
385  *
386  * @param x pointer to the query parameter to pass
387  */
388 struct GNUNET_MY_QueryParam
389 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
390 {
391   return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
392 }
393
394
395 /* end of my_query_helper.c */