GNUnet  0.10.x
sq_result_helper.c
Go to the documentation of this file.
1 
2 /*
3  This file is part of GNUnet
4  Copyright (C) 2017 GNUnet e.V.
5 
6  GNUnet is free software: you can redistribute it and/or modify it
7  under the terms of the GNU Affero General Public License as published
8  by the Free Software Foundation, either version 3 of the License,
9  or (at your option) any later version.
10 
11  GNUnet is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19  SPDX-License-Identifier: AGPL3.0-or-later
20  */
26 #include "platform.h"
27 #include "gnunet_sq_lib.h"
28 
29 
42 static int
43 extract_var_blob(void *cls,
44  sqlite3_stmt *result,
45  unsigned int column,
46  size_t *dst_size,
47  void *dst)
48 {
49  int have;
50  const void *ret;
51  void **rdst = (void **)dst;
52 
53  if (SQLITE_NULL ==
54  sqlite3_column_type(result,
55  column))
56  {
57  *rdst = NULL;
58  *dst_size = 0;
59  return GNUNET_YES;
60  }
61 
62  if (SQLITE_BLOB !=
63  sqlite3_column_type(result,
64  column))
65  {
66  GNUNET_break(0);
67  return GNUNET_SYSERR;
68  }
69  /* sqlite manual says to invoke 'sqlite3_column_blob()'
70  before calling sqlite3_column_bytes() */
71  ret = sqlite3_column_blob(result,
72  column);
73  have = sqlite3_column_bytes(result,
74  column);
75  if (have < 0)
76  {
77  GNUNET_break(0);
78  return GNUNET_SYSERR;
79  }
80  *dst_size = have;
81  if (0 == have)
82  {
83  *rdst = NULL;
84  return GNUNET_OK;
85  }
86  *rdst = GNUNET_malloc(have);
87  GNUNET_memcpy(*rdst,
88  ret,
89  have);
90  return GNUNET_OK;
91 }
92 
93 
99 static void
100 clean_var_blob(void *cls)
101 {
102  void **dptr = (void **)cls;
103 
104  if (NULL != *dptr)
105  {
106  GNUNET_free(*dptr);
107  *dptr = NULL;
108  }
109 }
110 
111 
121  size_t *sptr)
122 {
123  struct GNUNET_SQ_ResultSpec rs = {
125  .cleaner = &clean_var_blob,
126  .dst = dst,
127  .cls = dst,
128  .result_size = sptr,
129  .num_params = 1
130  };
131 
132  return rs;
133 }
134 
135 
148 static int
150  sqlite3_stmt *result,
151  unsigned int column,
152  size_t *dst_size,
153  void *dst)
154 {
155  int have;
156  const void *ret;
157 
158  if ((0 == *dst_size) &&
159  (SQLITE_NULL ==
160  sqlite3_column_type(result,
161  column)))
162  {
163  return GNUNET_YES;
164  }
165 
166  if (SQLITE_BLOB !=
167  sqlite3_column_type(result,
168  column))
169  {
170  GNUNET_break(0);
171  return GNUNET_SYSERR;
172  }
173  /* sqlite manual says to invoke 'sqlite3_column_blob()'
174  before calling sqlite3_column_bytes() */
175  ret = sqlite3_column_blob(result,
176  column);
177  have = sqlite3_column_bytes(result,
178  column);
179  if (*dst_size != have)
180  {
181  GNUNET_break(0);
182  return GNUNET_SYSERR;
183  }
184  GNUNET_memcpy(dst,
185  ret,
186  have);
187  return GNUNET_OK;
188 }
189 
190 
200  size_t dst_size)
201 {
202  struct GNUNET_SQ_ResultSpec rs = {
204  .dst = dst,
205  .dst_size = dst_size,
206  .num_params = 1
207  };
208 
209  return rs;
210 }
211 
212 
225 static int
227  sqlite3_stmt *result,
228  unsigned int column,
229  size_t *dst_size,
230  void *dst)
231 {
232  const char *text;
233  char **rdst = dst;
234 
235  if (SQLITE_NULL ==
236  sqlite3_column_type(result,
237  column))
238  {
239  *rdst = NULL;
240  return GNUNET_OK;
241  }
242  if (SQLITE_TEXT !=
243  sqlite3_column_type(result,
244  column))
245  {
246  GNUNET_break(0);
247  return GNUNET_SYSERR;
248  }
249  /* sqlite manual guarantees that 'sqlite3_column_text()'
250  is 0-terminated */
251  text = (const char *)sqlite3_column_text(result,
252  column);
253  if (NULL == text)
254  {
255  GNUNET_break(0);
256  return GNUNET_SYSERR;
257  }
258  *dst_size = strlen(text) + 1;
259  *rdst = GNUNET_strdup(text);
260  return GNUNET_OK;
261 }
262 
263 
269 static void
271 {
272  char **dptr = (char **)cls;
273 
274  if (NULL != *dptr)
275  {
276  GNUNET_free(*dptr);
277  *dptr = NULL;
278  }
279 }
280 
281 
290 {
291  struct GNUNET_SQ_ResultSpec rs = {
293  .cleaner = &clean_utf8_string,
294  .cls = dst,
295  .dst = dst,
296  .num_params = 1
297  };
298 
299  return rs;
300 }
301 
302 
315 static int
317  sqlite3_stmt *result,
318  unsigned int column,
319  size_t *dst_size,
320  void *dst)
321 {
322  struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
323  int have;
324  const void *ret;
325 
326  if (SQLITE_BLOB !=
327  sqlite3_column_type(result,
328  column))
329  {
330  GNUNET_break(0);
331  return GNUNET_SYSERR;
332  }
333  /* sqlite manual says to invoke 'sqlite3_column_blob()'
334  before calling sqlite3_column_bytes() */
335  ret = sqlite3_column_blob(result,
336  column);
337  have = sqlite3_column_bytes(result,
338  column);
339  if (have < 0)
340  {
341  GNUNET_break(0);
342  return GNUNET_SYSERR;
343  }
344 
346  have);
347  if (NULL == *pk)
348  {
349  GNUNET_break(0);
350  return GNUNET_SYSERR;
351  }
352  return GNUNET_OK;
353 }
354 
355 
362 static void
363 clean_rsa_pub(void *cls)
364 {
365  struct GNUNET_CRYPTO_RsaPublicKey **pk = cls;
366 
367  if (NULL != *pk)
368  {
370  *pk = NULL;
371  }
372 }
373 
374 
383 {
384  struct GNUNET_SQ_ResultSpec rs = {
385  .conv = &extract_rsa_pub,
386  .cleaner = &clean_rsa_pub,
387  .dst = rsa,
388  .cls = rsa,
389  .num_params = 1
390  };
391 
392  return rs;
393 }
394 
395 
408 static int
410  sqlite3_stmt *result,
411  unsigned int column,
412  size_t *dst_size,
413  void *dst)
414 {
415  struct GNUNET_CRYPTO_RsaSignature **sig = dst;
416  int have;
417  const void *ret;
418 
419  if (SQLITE_BLOB !=
420  sqlite3_column_type(result,
421  column))
422  {
423  GNUNET_break(0);
424  return GNUNET_SYSERR;
425  }
426  /* sqlite manual says to invoke 'sqlite3_column_blob()'
427  before calling sqlite3_column_bytes() */
428  ret = sqlite3_column_blob(result,
429  column);
430  have = sqlite3_column_bytes(result,
431  column);
432  if (have < 0)
433  {
434  GNUNET_break(0);
435  return GNUNET_SYSERR;
436  }
437 
439  have);
440  if (NULL == *sig)
441  {
442  GNUNET_break(0);
443  return GNUNET_SYSERR;
444  }
445  return GNUNET_OK;
446 }
447 
448 
455 static void
456 clean_rsa_sig(void *cls)
457 {
458  struct GNUNET_CRYPTO_RsaSignature **sig = cls;
459 
460  if (NULL != *sig)
461  {
463  *sig = NULL;
464  }
465 }
466 
467 
476 {
477  struct GNUNET_SQ_ResultSpec rs = {
478  .conv = &extract_rsa_sig,
479  .cleaner = &clean_rsa_sig,
480  .dst = sig,
481  .cls = sig,
482  .num_params = 1
483  };
484 
485  return rs;
486 }
487 
488 
501 static int
503  sqlite3_stmt *result,
504  unsigned int column,
505  size_t *dst_size,
506  void *dst)
507 {
508  struct GNUNET_TIME_Absolute *u = dst;
509  struct GNUNET_TIME_Absolute t;
510 
511  GNUNET_assert(sizeof(uint64_t) == *dst_size);
512  if (SQLITE_INTEGER !=
513  sqlite3_column_type(result,
514  column))
515  {
516  GNUNET_break(0);
517  return GNUNET_SYSERR;
518  }
519  t.abs_value_us = (uint64_t)sqlite3_column_int64(result,
520  column);
521  if (INT64_MAX == t.abs_value_us)
523  *u = t;
524  return GNUNET_OK;
525 }
526 
527 
536 {
537  struct GNUNET_SQ_ResultSpec rs = {
539  .dst = at,
540  .dst_size = sizeof(struct GNUNET_TIME_Absolute),
541  .num_params = 1
542  };
543 
544  return rs;
545 }
546 
547 
560 static int
562  sqlite3_stmt *result,
563  unsigned int column,
564  size_t *dst_size,
565  void *dst)
566 {
567  struct GNUNET_TIME_AbsoluteNBO *u = dst;
568  struct GNUNET_TIME_Absolute t;
569 
570  GNUNET_assert(sizeof(uint64_t) == *dst_size);
571  if (SQLITE_INTEGER !=
572  sqlite3_column_type(result,
573  column))
574  {
575  GNUNET_break(0);
576  return GNUNET_SYSERR;
577  }
578  t.abs_value_us = (uint64_t)sqlite3_column_int64(result,
579  column);
580  if (INT64_MAX == t.abs_value_us)
583  return GNUNET_OK;
584 }
585 
586 
595 {
596  struct GNUNET_SQ_ResultSpec rs = {
598  .dst = at,
599  .dst_size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
600  .num_params = 1
601  };
602 
603  return rs;
604 }
605 
606 
619 static int
620 extract_uint16(void *cls,
621  sqlite3_stmt *result,
622  unsigned int column,
623  size_t *dst_size,
624  void *dst)
625 {
626  uint64_t v;
627  uint16_t *u = dst;
628 
629  GNUNET_assert(sizeof(uint16_t) == *dst_size);
630  if (SQLITE_INTEGER !=
631  sqlite3_column_type(result,
632  column))
633  {
634  GNUNET_break(0);
635  return GNUNET_SYSERR;
636  }
637  v = (uint64_t)sqlite3_column_int64(result,
638  column);
639  if (v > UINT16_MAX)
640  {
641  GNUNET_break(0);
642  return GNUNET_SYSERR;
643  }
644  *u = (uint16_t)v;
645  return GNUNET_OK;
646 }
647 
648 
657 {
658  struct GNUNET_SQ_ResultSpec rs = {
659  .conv = &extract_uint16,
660  .dst = u16,
661  .dst_size = sizeof(uint16_t),
662  .num_params = 1
663  };
664 
665  return rs;
666 }
667 
668 
681 static int
683  sqlite3_stmt *result,
684  unsigned int column,
685  size_t *dst_size,
686  void *dst)
687 {
688  uint64_t v;
689  uint32_t *u = dst;
690 
691  GNUNET_assert(sizeof(uint32_t) == *dst_size);
692  if (SQLITE_INTEGER !=
693  sqlite3_column_type(result,
694  column))
695  {
696  GNUNET_break(0);
697  return GNUNET_SYSERR;
698  }
699  v = (uint64_t)sqlite3_column_int64(result,
700  column);
701  if (v > UINT32_MAX)
702  {
703  GNUNET_break(0);
704  return GNUNET_SYSERR;
705  }
706  *u = (uint32_t)v;
707  return GNUNET_OK;
708 }
709 
710 
719 {
720  struct GNUNET_SQ_ResultSpec rs = {
721  .conv = &extract_uint32,
722  .dst = u32,
723  .dst_size = sizeof(uint32_t),
724  .num_params = 1
725  };
726 
727  return rs;
728 }
729 
730 
743 static int
745  sqlite3_stmt *result,
746  unsigned int column,
747  size_t *dst_size,
748  void *dst)
749 {
750  uint64_t *u = dst;
751 
752  GNUNET_assert(sizeof(uint64_t) == *dst_size);
753  if (SQLITE_INTEGER !=
754  sqlite3_column_type(result,
755  column))
756  {
757  GNUNET_break(0);
758  return GNUNET_SYSERR;
759  }
760  *u = (uint64_t)sqlite3_column_int64(result,
761  column);
762  return GNUNET_OK;
763 }
764 
765 
774 {
775  struct GNUNET_SQ_ResultSpec rs = {
776  .conv = &extract_uint64,
777  .dst = u64,
778  .dst_size = sizeof(uint64_t),
779  .num_params = 1
780  };
781 
782  return rs;
783 }
784 
785 
786 /* end of sq_result_helper.c */
static int extract_uint16(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract 16-bit integer from a Postgres database result at row row.
static void clean_rsa_pub(void *cls)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
static void clean_var_blob(void *cls)
Cleanup memory allocated by extract_var_blob().
static struct GNUNET_CRYPTO_EddsaPrivateKey * pk
Private key of this peer.
Description of a DB result cell.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time_nbo(struct GNUNET_TIME_AbsoluteNBO *at)
Absolute time expected.
an RSA signature
Definition: crypto_rsa.c:61
void GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig)
Free memory occupied by signature.
Definition: crypto_rsa.c:950
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_fixed_size(void *dst, size_t dst_size)
Fixed-size result expected.
GNUNET_SQ_ResultConverter conv
What is the format of the result?
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int extract_var_blob(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract variable-sized binary data from a Postgres database result at row row.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
Absolute time expected.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static struct GNUNET_SCHEDULER_Task * t
Main task.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_variable_size(void **dst, size_t *sptr)
Variable-size result expected.
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
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
static int extract_uint64(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract 64-bit integer from a Postgres database result at row row.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
void * cls
Closure for conv and cleaner.
static int extract_abs_time_nbo(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract absolute time value in NBO from a Postgres database result at row row.
static int extract_abs_time(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract absolute time value from a Postgres database result at row row.
unsigned int num_params
Number of parameters (columns) eaten by this operation.
struct GNUNET_CRYPTO_RsaPublicKey * GNUNET_CRYPTO_rsa_public_key_decode(const char *buf, size_t len)
Decode the public key from the data-format back to the "normal", internal format. ...
Definition: crypto_rsa.c:367
static int result
Global testing status.
void * dst
Destination for the data.
size_t dst_size
Allowed size for the data, 0 for variable-size (in this case, the type of dst is a void ** and we nee...
static int extract_fixed_blob(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract fixed-sized binary data from a Postgres database result at row row.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
helper functions for Sqlite3 DB interactions
static int extract_rsa_pub(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint64(uint64_t *u64)
uint64_t expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint32(uint32_t *u32)
uint32_t expected.
static void clean_rsa_sig(void *cls)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
struct GNUNET_CRYPTO_RsaSignature * GNUNET_CRYPTO_rsa_signature_decode(const char *buf, size_t len)
Decode the signature from the data-format back to the "normal", internal format.
Definition: crypto_rsa.c:995
static int extract_rsa_sig(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract data from a Postgres database result at row row.
static void clean_utf8_string(void *cls)
Cleanup memory allocated by extract_var_blob().
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_rsa_signature(struct GNUNET_CRYPTO_RsaSignature **sig)
RSA signature expected.
static int extract_uint32(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract 32-bit integer from a Postgres database result at row row.
void GNUNET_CRYPTO_rsa_public_key_free(struct GNUNET_CRYPTO_RsaPublicKey *key)
Free memory occupied by the public key.
Definition: crypto_rsa.c:299
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:77
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint16(uint16_t *u16)
uint16_t expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_rsa_public_key(struct GNUNET_CRYPTO_RsaPublicKey **rsa)
RSA public key expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_string(char **dst)
0-terminated string expected.
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:655
static int extract_utf8_string(void *cls, sqlite3_stmt *result, unsigned int column, size_t *dst_size, void *dst)
Extract fixed-sized binary data from a Postgres database result at row row.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.