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
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 
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  return qp;
90 }
91 
92 
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 
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 
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 
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 
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 
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 
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 
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 
275 static int
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 
304 {
305  struct GNUNET_MY_QueryParam res = {
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 
326 static int
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 
355 {
356  struct GNUNET_MY_QueryParam res = {
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 
377 {
378  return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
379 }
380 
381 
390 {
391  return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
392 }
393 
394 
395 /* 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:63
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:318
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:966
#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:51
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:90
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.