GNUnet  0.10.x
sq_query_helper.c
Go to the documentation of this file.
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
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  */
25 #include "platform.h"
26 #include "gnunet_sq_lib.h"
27 
28 
40 static int
41 bind_fixed_blob(void *cls,
42  const void *data,
43  size_t data_len,
44  sqlite3_stmt *stmt,
45  unsigned int off)
46 {
47  if (SQLITE_OK !=
48  sqlite3_bind_blob64(stmt,
49  (int)off,
50  data,
51  (sqlite3_uint64)data_len,
52  SQLITE_TRANSIENT))
53  return GNUNET_SYSERR;
54  return GNUNET_OK;
55 }
56 
57 
67  size_t ptr_size)
68 {
69  struct GNUNET_SQ_QueryParam qp = {
71  .data = ptr,
72  .size = ptr_size,
73  .num_params = 1
74  };
75 
76  return qp;
77 }
78 
79 
91 static int
92 bind_string(void *cls,
93  const void *data,
94  size_t data_len,
95  sqlite3_stmt *stmt,
96  unsigned int off)
97 {
98  if (NULL == data)
99  {
100  if (SQLITE_OK !=
101  sqlite3_bind_null(stmt,
102  (int)off))
103  return GNUNET_SYSERR;
104  return GNUNET_OK;
105  }
106  if (SQLITE_OK !=
107  sqlite3_bind_text(stmt,
108  (int)off,
109  (const char *)data,
110  -1,
111  SQLITE_TRANSIENT))
112  return GNUNET_SYSERR;
113  return GNUNET_OK;
114 }
115 
116 
124 {
125  struct GNUNET_SQ_QueryParam qp = {
126  .conv = &bind_string,
127  .data = ptr,
128  .num_params = 1
129  };
130 
131  return qp;
132 }
133 
134 
146 static int
147 bind_rsa_pub(void *cls,
148  const void *data,
149  size_t data_len,
150  sqlite3_stmt *stmt,
151  unsigned int off)
152 {
153  const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
154  char *buf;
155  size_t buf_size;
156 
157  GNUNET_break(NULL == cls);
159  &buf);
160  if (SQLITE_OK !=
161  sqlite3_bind_blob64(stmt,
162  (int)off,
163  buf,
164  (sqlite3_uint64)buf_size,
165  SQLITE_TRANSIENT))
166  {
167  GNUNET_free(buf);
168  return GNUNET_SYSERR;
169  }
170  GNUNET_free(buf);
171  return GNUNET_OK;
172 }
173 
174 
183 {
184  struct GNUNET_SQ_QueryParam qp = {
185  .conv = &bind_rsa_pub,
186  .data = x,
187  .num_params = 1
188  };
189 
190  return qp;
191 }
192 
193 
205 static int
206 bind_rsa_sig(void *cls,
207  const void *data,
208  size_t data_len,
209  sqlite3_stmt *stmt,
210  unsigned int off)
211 {
212  const struct GNUNET_CRYPTO_RsaSignature *sig = data;
213  char *buf;
214  size_t buf_size;
215 
216  GNUNET_break(NULL == cls);
217  buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
218  &buf);
219  if (SQLITE_OK !=
220  sqlite3_bind_blob64(stmt,
221  (int)off,
222  buf,
223  (sqlite3_uint64)buf_size,
224  SQLITE_TRANSIENT))
225  {
226  GNUNET_free(buf);
227  return GNUNET_SYSERR;
228  }
229  GNUNET_free(buf);
230  return GNUNET_OK;
231 }
232 
233 
242 {
243  struct GNUNET_SQ_QueryParam qp = {
244  .conv = &bind_rsa_sig,
245  .data = x,
246  .num_params = 1
247  };
248 
249  return qp;
250 }
251 
252 
264 static int
265 bind_abstime(void *cls,
266  const void *data,
267  size_t data_len,
268  sqlite3_stmt *stmt,
269  unsigned int off)
270 {
271  const struct GNUNET_TIME_Absolute *u = data;
272  struct GNUNET_TIME_Absolute abs;
273 
274  abs = *u;
275  if (abs.abs_value_us > INT64_MAX)
276  abs.abs_value_us = INT64_MAX;
277  GNUNET_assert(sizeof(uint64_t) == data_len);
278  if (SQLITE_OK !=
279  sqlite3_bind_int64(stmt,
280  (int)off,
281  (sqlite3_int64)abs.abs_value_us))
282  return GNUNET_SYSERR;
283  return GNUNET_OK;
284 }
285 
286 
295 {
296  struct GNUNET_SQ_QueryParam qp = {
297  .conv = &bind_abstime,
298  .data = x,
299  .size = sizeof(struct GNUNET_TIME_Absolute),
300  .num_params = 1
301  };
302 
303  return qp;
304 }
305 
306 
318 static int
319 bind_nbotime(void *cls,
320  const void *data,
321  size_t data_len,
322  sqlite3_stmt *stmt,
323  unsigned int off)
324 {
325  const struct GNUNET_TIME_AbsoluteNBO *u = data;
326  struct GNUNET_TIME_Absolute abs;
327 
328  abs = GNUNET_TIME_absolute_ntoh(*u);
329  if (abs.abs_value_us > INT64_MAX)
330  abs.abs_value_us = INT64_MAX;
331  GNUNET_assert(sizeof(uint64_t) == data_len);
332  if (SQLITE_OK !=
333  sqlite3_bind_int64(stmt,
334  (int)off,
335  (sqlite3_int64)abs.abs_value_us))
336  return GNUNET_SYSERR;
337  return GNUNET_OK;
338 }
339 
340 
349 {
350  struct GNUNET_SQ_QueryParam qp = {
351  .conv = &bind_nbotime,
352  .data = x,
353  .size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
354  .num_params = 1
355  };
356 
357  return qp;
358 }
359 
360 
372 static int
373 bind_u16(void *cls,
374  const void *data,
375  size_t data_len,
376  sqlite3_stmt *stmt,
377  unsigned int off)
378 {
379  const uint16_t *u = data;
380 
381  GNUNET_assert(sizeof(uint16_t) == data_len);
382  if (SQLITE_OK !=
383  sqlite3_bind_int(stmt,
384  (int)off,
385  (int)*u))
386  return GNUNET_SYSERR;
387  return GNUNET_OK;
388 }
389 
390 
398 {
399  struct GNUNET_SQ_QueryParam qp = {
400  .conv = &bind_u16,
401  .data = x,
402  .size = sizeof(uint16_t),
403  .num_params = 1
404  };
405 
406  return qp;
407 }
408 
409 
421 static int
422 bind_u32(void *cls,
423  const void *data,
424  size_t data_len,
425  sqlite3_stmt *stmt,
426  unsigned int off)
427 {
428  const uint32_t *u = data;
429 
430  GNUNET_assert(sizeof(uint32_t) == data_len);
431  if (SQLITE_OK !=
432  sqlite3_bind_int64(stmt,
433  (int)off,
434  (sqlite3_int64) * u))
435  return GNUNET_SYSERR;
436  return GNUNET_OK;
437 }
438 
446 {
447  struct GNUNET_SQ_QueryParam qp = {
448  .conv = &bind_u32,
449  .data = x,
450  .size = sizeof(uint32_t),
451  .num_params = 1
452  };
453 
454  return qp;
455 }
456 
457 
469 static int
470 bind_u64(void *cls,
471  const void *data,
472  size_t data_len,
473  sqlite3_stmt *stmt,
474  unsigned int off)
475 {
476  const uint64_t *u = data;
477 
478  GNUNET_assert(sizeof(uint64_t) == data_len);
479  if (SQLITE_OK !=
480  sqlite3_bind_int64(stmt,
481  (int)off,
482  (sqlite3_int64) * u))
483  return GNUNET_SYSERR;
484  return GNUNET_OK;
485 }
486 
487 
495 {
496  struct GNUNET_SQ_QueryParam qp = {
497  .conv = &bind_u64,
498  .data = x,
499  .size = sizeof(uint64_t),
500  .num_params = 1
501  };
502 
503  return qp;
504 }
505 
506 /* end of sq_query_helper.c */
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:671
static int bind_fixed_blob(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
an RSA signature
Definition: crypto_rsa.c:61
static int bind_u16(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
unsigned int num_params
Number of parameters eaten by this operation.
Definition: gnunet_sq_lib.h:78
static int bind_rsa_sig(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
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_SQ_QueryParam GNUNET_SQ_query_param_string(const char *ptr)
Generate query parameter for a string.
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
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
Generate query parameter for an RSA signature.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint16(const uint16_t *x)
Generate query parameter for an uint16_t in host byte order.
static int bind_u32(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
uint64_t abs_value_us
The actual value.
The public information of an RSA key pair.
Definition: crypto_rsa.c:50
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
static int bind_nbotime(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *x)
Generate query parameter for an RSA public key.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
Generate query parameter for an absolute time value.
static char buf[2048]
static int bind_abstime(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
static int bind_u64(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
helper functions for Sqlite3 DB interactions
Description of a DB query parameter.
Definition: gnunet_sq_lib.h:54
static int bind_rsa_pub(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
size_t size
Size of data.
Definition: gnunet_sq_lib.h:73
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
GNUNET_SQ_QueryConverter conv
Function for how to handle this type of entry.
Definition: gnunet_sq_lib.h:58
Time for absolute times used by GNUnet, in microseconds.
uint32_t data
The data value.
static int bind_string(void *cls, const void *data, size_t data_len, sqlite3_stmt *stmt, unsigned int off)
Function called to convert input argument into SQL parameters.
#define GNUNET_free(ptr)
Wrapper around free.