GNUnet  0.10.x
my_query_helper.c
Go to the documentation of this file.
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  */
26 #include "platform.h"
27 #include <mysql/mysql.h>
28 #include "gnunet_my_lib.h"
29 
30 
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 
55 static int
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 
79  size_t ptr_size)
80 {
81  struct GNUNET_MY_QueryParam qp = {
83  .cleaner = NULL,
84  .conv_cls = NULL,
85  .num_params = 1,
86  .data = ptr,
87  .data_len = (unsigned long)ptr_size
88  };
89 
90  return qp;
91 }
92 
93 
102 static int
103 my_conv_string(void *cls,
104  const struct GNUNET_MY_QueryParam *qp,
105  MYSQL_BIND *qbind)
106 {
107  (void)cls;
108  GNUNET_assert(1 == qp->num_params);
109  qbind->buffer = (void *)qp->data;
110  qbind->buffer_length = qp->data_len;
111  qbind->buffer_type = MYSQL_TYPE_STRING;
112  return 1;
113 }
114 
115 
123 {
124  struct GNUNET_MY_QueryParam qp = {
125  .conv = &my_conv_string,
126  .cleaner = NULL,
127  .conv_cls = NULL,
128  .num_params = 1,
129  .data = ptr,
130  .data_len = strlen(ptr)
131  };
132 
133  return qp;
134 }
135 
136 
145 static int
146 my_conv_uint16(void *cls,
147  const struct GNUNET_MY_QueryParam *qp,
148  MYSQL_BIND *qbind)
149 {
150  (void)cls;
151  GNUNET_assert(1 == qp->num_params);
152  qbind->buffer = (void *)qp->data;
153  qbind->buffer_length = sizeof(uint16_t);
154  qbind->buffer_type = MYSQL_TYPE_SHORT;
155  qbind->is_unsigned = 1;
156  return 1;
157 }
158 
159 
167 {
168  struct GNUNET_MY_QueryParam res = {
169  .conv = &my_conv_uint16,
170  .cleaner = NULL,
171  .conv_cls = NULL,
172  .num_params = 1,
173  .data = x,
174  .data_len = sizeof(*x)
175  };
176 
177  return res;
178 }
179 
180 
189 static int
190 my_conv_uint32(void *cls,
191  const struct GNUNET_MY_QueryParam *qp,
192  MYSQL_BIND *qbind)
193 {
194  (void)cls;
195  GNUNET_assert(1 == qp->num_params);
196  qbind->buffer = (void *)qp->data;
197  qbind->buffer_length = sizeof(uint32_t);
198  qbind->buffer_type = MYSQL_TYPE_LONG;
199  qbind->is_unsigned = 1;
200  return 1;
201 }
202 
203 
211 {
212  struct GNUNET_MY_QueryParam res = {
213  .conv = &my_conv_uint32,
214  .cleaner = NULL,
215  .conv_cls = NULL,
216  .num_params = 1,
217  .data = x,
218  .data_len = sizeof(*x)
219  };
220 
221  return res;
222 }
223 
224 
233 static int
234 my_conv_uint64(void *cls,
235  const struct GNUNET_MY_QueryParam *qp,
236  MYSQL_BIND * qbind)
237 {
238  (void)cls;
239  GNUNET_assert(1 == qp->num_params);
240  qbind->buffer = (void *)qp->data;
241  qbind->buffer_length = sizeof(uint64_t);
242  qbind->buffer_type = MYSQL_TYPE_LONGLONG;
243  qbind->is_unsigned = 1;
244  return 1;
245 }
246 
247 
255 {
256  struct GNUNET_MY_QueryParam res = {
257  .conv = &my_conv_uint64,
258  .cleaner = NULL,
259  .conv_cls = NULL,
260  .num_params = 1,
261  .data = x,
262  .data_len = sizeof(*x)
263  };
264 
265  return res;
266 }
267 
268 
277 static int
279  const struct GNUNET_MY_QueryParam *qp,
280  MYSQL_BIND * qbind)
281 {
282  const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
283  char *buf;
284  size_t buf_size;
285 
286  (void)cls;
287  GNUNET_assert(1 == qp->num_params);
289  &buf);
290  qbind->buffer = (void *)buf;
291  qbind->buffer_length = buf_size;
292  qbind->buffer_type = MYSQL_TYPE_BLOB;
293  return 1;
294 }
295 
296 
306 {
307  struct GNUNET_MY_QueryParam res = {
309  .cleaner = &my_clean_query,
310  .conv_cls = NULL,
311  .num_params = 1,
312  .data = x,
313  .data_len = 0
314  };
315 
316  return res;
317 }
318 
319 
328 static int
330  const struct GNUNET_MY_QueryParam *qp,
331  MYSQL_BIND *qbind)
332 {
333  const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
334  char *buf;
335  size_t buf_size;
336 
337  (void)cls;
338  GNUNET_assert(1 == qp->num_params);
339  buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
340  &buf);
341  qbind->buffer = (void *)buf;
342  qbind->buffer_length = buf_size;
343  qbind->buffer_type = MYSQL_TYPE_BLOB;
344  return 1;
345 }
346 
347 
357 {
358  struct GNUNET_MY_QueryParam res = {
360  .cleaner = &my_clean_query,
361  .conv_cls = NULL,
362  .num_params = 1,
363  .data = (x),
364  .data_len = 0
365  };
366 
367  return res;
368 }
369 
370 
380 {
381  return GNUNET_MY_query_param_uint64(&x->abs_value_us);
382 }
383 
384 
393 {
394  return GNUNET_MY_query_param_auto_from_type(&x->abs_value_us__);
395 }
396 
397 
398 /* end of my_query_helper.c */
#define GNUNET_MY_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
const void * data
Information to pass to conv.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint64_t in host byte order.
static int my_conv_uint16(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
an RSA signature
Definition: crypto_rsa.c:61
size_t GNUNET_CRYPTO_rsa_public_key_encode(const struct GNUNET_CRYPTO_RsaPublicKey *key, char **buffer)
Encode the public key in a format suitable for storing it into a file.
Definition: crypto_rsa.c:315
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
size_t GNUNET_CRYPTO_rsa_signature_encode(const struct GNUNET_CRYPTO_RsaSignature *sig, char **buffer)
Encode the given signature in a format suitable for storing it into a file.
Definition: crypto_rsa.c:965
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void my_clean_query(void *cls, MYSQL_BIND *qbind)
Function called to clean up memory allocated by a GNUNET_MY_QueryConverter.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
Information we pass to GNUNET_MY_exec_prepared() to initialize the arguments of the prepared statemen...
Definition: gnunet_my_lib.h:84
unsigned int num_params
Number of arguments the conv converter expects to initialize.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
The public information of an RSA key pair.
Definition: crypto_rsa.c:50
static int my_conv_uint64(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
static int my_conv_rsa_public_key(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
GNUNET_MY_QueryConverter conv
Function to call for the type conversion.
Definition: gnunet_my_lib.h:88
static int my_conv_fixed_size(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
Generate query parameter for an RSA signature.
static char buf[2048]
static int res
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
Generate query parameter for an absolute time value.
static int my_conv_uint32(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
unsigned long data_len
Information to pass to conv.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *x)
Generate query parameter for an RSA public key.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_uint16(const uint16_t *x)
Generate query parameter for an uint16_t in host byte order.
Time for absolute times used by GNUnet, in microseconds.
static int my_conv_string(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
struct GNUNET_MY_QueryParam GNUNET_MY_query_param_string(const char *ptr)
Generate query parameter for a string.
static int my_conv_rsa_signature(void *cls, const struct GNUNET_MY_QueryParam *qp, MYSQL_BIND *qbind)
Function called to convert input argument into SQL parameters.
#define GNUNET_free(ptr)
Wrapper around free.