finish to fix memory leak
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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   GNUNET_free (qbind[0].buffer);
43 }
44
45
46 /**
47  * Function called to convert input argument into SQL parameters.
48  *
49  * @param cls closure
50  * @param pq data about the query
51  * @param qbind array of parameters to initialize
52  * @return -1 on error
53  */
54 static int
55 my_conv_fixed_size (void *cls,
56                     const struct GNUNET_MY_QueryParam *qp,
57                     MYSQL_BIND *qbind)
58 {
59   GNUNET_assert (1 == qp->num_params);
60
61   qbind->buffer = (void *) qp->data;
62   qbind->buffer_length = qp->data_len;
63   qbind->buffer_type = MYSQL_TYPE_BLOB;
64
65   return 1;
66 }
67
68
69 /**
70  * Generate query parameter for a buffer @a ptr of
71  * @a ptr_size bytes.
72  *
73  * @param ptr pointer to the query parameter to pass
74  * @param ptr_size number of bytes in @a ptr
75  */
76 struct GNUNET_MY_QueryParam
77 GNUNET_MY_query_param_fixed_size (const void *ptr,
78                                   size_t ptr_size)
79 {
80   struct GNUNET_MY_QueryParam qp = {
81     .conv = &my_conv_fixed_size,
82     .cleaner = NULL,
83     .conv_cls = NULL,
84     .num_params = 1,
85     .data = ptr,
86     .data_len = (unsigned long) ptr_size
87   };
88   return qp;
89 }
90
91
92 /**
93   * Generate query parameter for a string
94   *
95   *@param ptr pointer to the string query parameter to pass
96   */
97 struct GNUNET_MY_QueryParam
98 GNUNET_MY_query_param_string (const char *ptr)
99 {
100   return GNUNET_MY_query_param_fixed_size(ptr,
101                                          strlen(ptr));
102 }
103
104
105 /**
106   * Function called to convert input argument into SQL parameters
107   *
108   *@param cls closure
109   *@param pq data about the query
110  * @param qbind array of parameters to initialize
111   *@return -1 on error
112   */
113 static int
114 my_conv_uint16 (void *cls,
115                 const struct GNUNET_MY_QueryParam * qp,
116                 MYSQL_BIND *qbind)
117 {
118   const uint16_t *u_hbo = qp->data;
119   uint16_t *u_nbo;
120
121   GNUNET_assert (1 == qp->num_params);
122
123   u_nbo = GNUNET_new (uint16_t);
124   if (NULL == u_nbo)
125     return -1;
126
127   *u_nbo = *u_hbo;
128
129   qbind->buffer = (void *) u_nbo;
130   qbind->buffer_length = sizeof(uint16_t);
131   qbind->buffer_type = MYSQL_TYPE_SHORT;
132
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_MY_QueryParam
143 GNUNET_MY_query_param_uint16 (const uint16_t *x)
144 {
145   struct GNUNET_MY_QueryParam res = {
146     .conv = &my_conv_uint16,
147     .cleaner = &my_clean_query,
148     .conv_cls = NULL,
149     .num_params = 1,
150     .data = x,
151     .data_len = sizeof (*x)
152   };
153
154   return res;
155 }
156
157
158 /**
159   * Function called to convert input argument into SQL parameters
160   *
161   *@param cls closure
162   *@param pq data about the query
163  * @param qbind array of parameters to initialize
164   *@return -1 on error
165   */
166 static int
167 my_conv_uint32 (void *cls,
168                 const struct GNUNET_MY_QueryParam *qp,
169                 MYSQL_BIND *qbind)
170 {
171   const uint32_t *u_hbo = qp->data;
172   uint32_t * u_nbo;
173
174   GNUNET_assert (1 == qp->num_params);
175
176   u_nbo = GNUNET_new (uint32_t);
177
178   *u_nbo = *u_hbo;
179
180   qbind->buffer = (void *) u_nbo;
181   qbind->buffer_length = sizeof(uint32_t);
182   qbind->buffer_type = MYSQL_TYPE_LONG;
183
184   return 1;
185 }
186
187
188 /**
189   * Generate query parameter for an uint32_t in host byte order
190   *
191   *@param x pointer to the query parameter to pass
192   */
193 struct GNUNET_MY_QueryParam
194 GNUNET_MY_query_param_uint32 (const uint32_t *x)
195 {
196   struct GNUNET_MY_QueryParam res = {
197     .conv = &my_conv_uint32,
198     .cleaner = &my_clean_query,
199     .conv_cls = NULL,
200     .num_params = 1,
201     .data = x,
202     .data_len = sizeof (*x)
203   };
204
205   return res;
206 }
207
208
209 /**
210   * Function called to convert input argument into SQL parameters
211   *
212   *@param cls closure
213   *@param pq data about the query
214  * @param qbind array of parameters to initialize
215   *@return -1 on error
216   */
217 static int
218 my_conv_uint64 (void *cls,
219               const struct GNUNET_MY_QueryParam *qp,
220               MYSQL_BIND * qbind)
221 {
222   const uint64_t * u_hbo = qp->data;
223   uint64_t *u_nbo;
224
225   GNUNET_assert (1 == qp->num_params);
226
227   u_nbo = GNUNET_new(uint64_t);
228
229   *u_nbo = *u_hbo;
230
231   qbind->buffer = (void *) u_nbo;
232   qbind->buffer_length = sizeof (uint64_t);
233   qbind->buffer_type = MYSQL_TYPE_LONGLONG;
234
235   return 1;
236 }
237
238
239 /**
240   * Generate query parameter for an uint64_t in host byte order
241   *
242   *@param x pointer to the query parameter to pass
243   */
244 struct GNUNET_MY_QueryParam
245 GNUNET_MY_query_param_uint64 (const uint64_t *x)
246 {
247   struct GNUNET_MY_QueryParam res = {
248     .conv = &my_conv_uint64,
249     .cleaner = &my_clean_query,
250     .conv_cls = NULL,
251     .num_params = 1,
252     .data = x,
253     .data_len = sizeof(*x)
254   };
255
256   return res;
257 }
258
259
260 /**
261   * Function called to convert input argument into SQL parameters
262   *
263   *@param cls closure
264   *@param pq data about the query
265  * @param qbind array of parameters to initialize
266   *@return -1 on error
267   */
268 static int
269 my_conv_rsa_public_key (void *cls,
270                         const struct GNUNET_MY_QueryParam *qp,
271                         MYSQL_BIND * qbind)
272 {
273   const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
274   char *buf;
275   size_t buf_size;
276
277   GNUNET_assert(1 == qp->num_params);
278
279   buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
280
281   qbind->buffer = (void *) buf;
282   qbind->buffer_length = buf_size;
283   qbind->buffer_type = MYSQL_TYPE_BLOB;
284
285   return 1;
286 }
287
288
289 /**
290   * Generate query parameter for an RSA public key. The
291   * database must contain a BLOB type in the respective position.
292   *
293   * @param x the query parameter to pass
294   * @return array entry for the query parameters to use
295   */
296 struct GNUNET_MY_QueryParam
297 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
298 {
299   struct GNUNET_MY_QueryParam res = {
300     .conv = &my_conv_rsa_public_key,
301     .cleaner = &my_clean_query,
302     .conv_cls = NULL,
303     .num_params = 1,
304     .data = x,
305     .data_len = 0
306   };
307
308   return res;
309 }
310
311
312 /**
313   * Function called to convert input argument into SQL parameters
314   *
315   *@param cls closure
316   *@param pq data about the query
317   *@param qbind array of parameters to initialize
318   *@return -1 on error
319   */
320 static int
321 my_conv_rsa_signature (void *cls,
322                        const struct GNUNET_MY_QueryParam *qp,
323                        MYSQL_BIND *qbind)
324 {
325   const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
326   char *buf;
327   size_t buf_size;
328
329   GNUNET_assert(1 == qp->num_params);
330
331   buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
332                                                  &buf);
333   qbind->buffer = (void *) buf;
334   qbind->buffer_length = buf_size;
335   qbind->buffer_type = MYSQL_TYPE_BLOB;
336
337   return 1;
338 }
339
340
341 /**
342   * Generate query parameter for an RSA signature. The
343   * database must contain a BLOB type in the respective position
344   *
345   *@param x the query parameter to pass
346   *@return array entry for the query parameters to use
347   */
348 struct GNUNET_MY_QueryParam
349 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
350 {
351   struct GNUNET_MY_QueryParam res = {
352     .conv = &my_conv_rsa_signature,
353     .cleaner = &my_clean_query,
354     .conv_cls = NULL,
355     .num_params = 1,
356     .data = (x),
357     .data_len = 0
358   };
359   return res;
360 }
361
362
363 /**
364   * Generate query parameter for an absolute time value.
365   * The database must store a 64-bit integer.
366   *
367   *@param x pointer to the query parameter to pass
368   *@return array entry for the query parameters to use
369   */
370 struct GNUNET_MY_QueryParam
371 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
372 {
373   return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
374 }
375
376
377 /**
378   * Generate query parameter for an absolute time value.
379   * The database must store a 64-bit integer.
380   *
381   *@param x pointer to the query parameter to pass
382   */
383 struct GNUNET_MY_QueryParam
384 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
385 {
386   return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
387 }
388
389
390 /* end of my_query_helper.c */