GNUnet  0.20.0
sq_result_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 
41 static int
42 extract_var_blob (void *cls,
43  sqlite3_stmt *result,
44  unsigned int column,
45  size_t *dst_size,
46  void *dst)
47 {
48  int have;
49  const void *ret;
50  void **rdst = (void **) dst;
51 
52  if (SQLITE_NULL ==
53  sqlite3_column_type (result,
54  column))
55  {
56  *rdst = NULL;
57  *dst_size = 0;
58  return GNUNET_YES;
59  }
60 
61  if (SQLITE_BLOB !=
62  sqlite3_column_type (result,
63  column))
64  {
65  GNUNET_break (0);
66  return GNUNET_SYSERR;
67  }
68  /* sqlite manual says to invoke 'sqlite3_column_blob()'
69  before calling sqlite3_column_bytes() */
70  ret = sqlite3_column_blob (result,
71  column);
72  have = sqlite3_column_bytes (result,
73  column);
74  if (have < 0)
75  {
76  GNUNET_break (0);
77  return GNUNET_SYSERR;
78  }
79  *dst_size = have;
80  if (0 == have)
81  {
82  *rdst = NULL;
83  return GNUNET_OK;
84  }
85  *rdst = GNUNET_malloc (have);
86  GNUNET_memcpy (*rdst,
87  ret,
88  have);
89  return GNUNET_OK;
90 }
91 
92 
98 static void
99 clean_var_blob (void *cls)
100 {
101  void **dptr = (void **) cls;
102 
103  if (NULL != *dptr)
104  {
105  GNUNET_free (*dptr);
106  *dptr = NULL;
107  }
108 }
109 
110 
120  size_t *sptr)
121 {
122  struct GNUNET_SQ_ResultSpec rs = {
124  .cleaner = &clean_var_blob,
125  .dst = dst,
126  .cls = dst,
127  .result_size = sptr,
128  .num_params = 1
129  };
130 
131  return rs;
132 }
133 
134 
147 static int
149  sqlite3_stmt *result,
150  unsigned int column,
151  size_t *dst_size,
152  void *dst)
153 {
154  int have;
155  const void *ret;
156 
157  if ((0 == *dst_size) &&
158  (SQLITE_NULL ==
159  sqlite3_column_type (result,
160  column)))
161  {
162  return GNUNET_YES;
163  }
164 
165  if (SQLITE_BLOB !=
166  sqlite3_column_type (result,
167  column))
168  {
169  GNUNET_break (0);
170  return GNUNET_SYSERR;
171  }
172  /* sqlite manual says to invoke 'sqlite3_column_blob()'
173  before calling sqlite3_column_bytes() */
174  ret = sqlite3_column_blob (result,
175  column);
176  have = sqlite3_column_bytes (result,
177  column);
178  if (*dst_size != have)
179  {
180  GNUNET_break (0);
181  return GNUNET_SYSERR;
182  }
184  ret,
185  have);
186  return GNUNET_OK;
187 }
188 
189 
199  size_t dst_size)
200 {
201  struct GNUNET_SQ_ResultSpec rs = {
203  .dst = dst,
204  .dst_size = dst_size,
205  .num_params = 1
206  };
207 
208  return rs;
209 }
210 
211 
224 static int
226  sqlite3_stmt *result,
227  unsigned int column,
228  size_t *dst_size,
229  void *dst)
230 {
231  const char *text;
232  char **rdst = dst;
233 
234  if (SQLITE_NULL ==
235  sqlite3_column_type (result,
236  column))
237  {
238  *rdst = NULL;
239  return GNUNET_OK;
240  }
241  if (SQLITE_TEXT !=
242  sqlite3_column_type (result,
243  column))
244  {
245  GNUNET_break (0);
246  return GNUNET_SYSERR;
247  }
248  /* sqlite manual guarantees that 'sqlite3_column_text()'
249  is 0-terminated */
250  text = (const char *) sqlite3_column_text (result,
251  column);
252  if (NULL == text)
253  {
254  GNUNET_break (0);
255  return GNUNET_SYSERR;
256  }
257  *dst_size = strlen (text) + 1;
258  *rdst = GNUNET_strdup (text);
259  return GNUNET_OK;
260 }
261 
262 
268 static void
270 {
271  char **dptr = (char **) cls;
272 
273  if (NULL != *dptr)
274  {
275  GNUNET_free (*dptr);
276  *dptr = NULL;
277  }
278 }
279 
280 
289 {
290  struct GNUNET_SQ_ResultSpec rs = {
292  .cleaner = &clean_utf8_string,
293  .cls = dst,
294  .dst = dst,
295  .num_params = 1
296  };
297 
298  return rs;
299 }
300 
301 
314 static int
316  sqlite3_stmt *result,
317  unsigned int column,
318  size_t *dst_size,
319  void *dst)
320 {
321  struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
322  int have;
323  const void *ret;
324 
325  if (SQLITE_BLOB !=
326  sqlite3_column_type (result,
327  column))
328  {
329  GNUNET_break (0);
330  return GNUNET_SYSERR;
331  }
332  /* sqlite manual says to invoke 'sqlite3_column_blob()'
333  before calling sqlite3_column_bytes() */
334  ret = sqlite3_column_blob (result,
335  column);
336  have = sqlite3_column_bytes (result,
337  column);
338  if (have < 0)
339  {
340  GNUNET_break (0);
341  return GNUNET_SYSERR;
342  }
343 
345  have);
346  if (NULL == *pk)
347  {
348  GNUNET_break (0);
349  return GNUNET_SYSERR;
350  }
351  return GNUNET_OK;
352 }
353 
354 
361 static void
362 clean_rsa_pub (void *cls)
363 {
364  struct GNUNET_CRYPTO_RsaPublicKey **pk = cls;
365 
366  if (NULL != *pk)
367  {
369  *pk = NULL;
370  }
371 }
372 
373 
382 {
383  struct GNUNET_SQ_ResultSpec rs = {
384  .conv = &extract_rsa_pub,
385  .cleaner = &clean_rsa_pub,
386  .dst = rsa,
387  .cls = rsa,
388  .num_params = 1
389  };
390 
391  return rs;
392 }
393 
394 
407 static int
409  sqlite3_stmt *result,
410  unsigned int column,
411  size_t *dst_size,
412  void *dst)
413 {
414  struct GNUNET_CRYPTO_RsaSignature **sig = dst;
415  int have;
416  const void *ret;
417 
418  if (SQLITE_BLOB !=
419  sqlite3_column_type (result,
420  column))
421  {
422  GNUNET_break (0);
423  return GNUNET_SYSERR;
424  }
425  /* sqlite manual says to invoke 'sqlite3_column_blob()'
426  before calling sqlite3_column_bytes() */
427  ret = sqlite3_column_blob (result,
428  column);
429  have = sqlite3_column_bytes (result,
430  column);
431  if (have < 0)
432  {
433  GNUNET_break (0);
434  return GNUNET_SYSERR;
435  }
436 
438  have);
439  if (NULL == *sig)
440  {
441  GNUNET_break (0);
442  return GNUNET_SYSERR;
443  }
444  return GNUNET_OK;
445 }
446 
447 
454 static void
455 clean_rsa_sig (void *cls)
456 {
457  struct GNUNET_CRYPTO_RsaSignature **sig = cls;
458 
459  if (NULL != *sig)
460  {
462  *sig = NULL;
463  }
464 }
465 
466 
475 {
476  struct GNUNET_SQ_ResultSpec rs = {
477  .conv = &extract_rsa_sig,
478  .cleaner = &clean_rsa_sig,
479  .dst = sig,
480  .cls = sig,
481  .num_params = 1
482  };
483 
484  return rs;
485 }
486 
487 
500 static int
502  sqlite3_stmt *result,
503  unsigned int column,
504  size_t *dst_size,
505  void *dst)
506 {
507  struct GNUNET_TIME_Absolute *u = dst;
508  struct GNUNET_TIME_Absolute t;
509 
510  GNUNET_assert (sizeof(uint64_t) == *dst_size);
511  if (SQLITE_INTEGER !=
512  sqlite3_column_type (result,
513  column))
514  {
515  GNUNET_break (0);
516  return GNUNET_SYSERR;
517  }
518  t.abs_value_us = (uint64_t) sqlite3_column_int64 (result,
519  column);
520  if (INT64_MAX == t.abs_value_us)
522  *u = t;
523  return GNUNET_OK;
524 }
525 
526 
535 {
536  struct GNUNET_SQ_ResultSpec rs = {
538  .dst = at,
539  .dst_size = sizeof(struct GNUNET_TIME_Absolute),
540  .num_params = 1
541  };
542 
543  return rs;
544 }
545 
546 
559 static int
561  sqlite3_stmt *result,
562  unsigned int column,
563  size_t *dst_size,
564  void *dst)
565 {
566  struct GNUNET_TIME_AbsoluteNBO *u = dst;
567  struct GNUNET_TIME_Absolute t;
568 
569  GNUNET_assert (sizeof(uint64_t) == *dst_size);
570  if (SQLITE_INTEGER !=
571  sqlite3_column_type (result,
572  column))
573  {
574  GNUNET_break (0);
575  return GNUNET_SYSERR;
576  }
577  t.abs_value_us = (uint64_t) sqlite3_column_int64 (result,
578  column);
579  if (INT64_MAX == t.abs_value_us)
582  return GNUNET_OK;
583 }
584 
585 
594 {
595  struct GNUNET_SQ_ResultSpec rs = {
597  .dst = at,
598  .dst_size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
599  .num_params = 1
600  };
601 
602  return rs;
603 }
604 
605 
618 static int
619 extract_uint16 (void *cls,
620  sqlite3_stmt *result,
621  unsigned int column,
622  size_t *dst_size,
623  void *dst)
624 {
625  uint64_t v;
626  uint16_t *u = dst;
627 
628  GNUNET_assert (sizeof(uint16_t) == *dst_size);
629  if (SQLITE_INTEGER !=
630  sqlite3_column_type (result,
631  column))
632  {
633  GNUNET_break (0);
634  return GNUNET_SYSERR;
635  }
636  v = (uint64_t) sqlite3_column_int64 (result,
637  column);
638  if (v > UINT16_MAX)
639  {
640  GNUNET_break (0);
641  return GNUNET_SYSERR;
642  }
643  *u = (uint16_t) v;
644  return GNUNET_OK;
645 }
646 
647 
655 GNUNET_SQ_result_spec_uint16 (uint16_t *u16)
656 {
657  struct GNUNET_SQ_ResultSpec rs = {
658  .conv = &extract_uint16,
659  .dst = u16,
660  .dst_size = sizeof(uint16_t),
661  .num_params = 1
662  };
663 
664  return rs;
665 }
666 
667 
680 static int
682  sqlite3_stmt *result,
683  unsigned int column,
684  size_t *dst_size,
685  void *dst)
686 {
687  uint64_t v;
688  uint32_t *u = dst;
689 
690  GNUNET_assert (sizeof(uint32_t) == *dst_size);
691  if (SQLITE_INTEGER !=
692  sqlite3_column_type (result,
693  column))
694  {
695  GNUNET_break (0);
696  return GNUNET_SYSERR;
697  }
698  v = (uint64_t) sqlite3_column_int64 (result,
699  column);
700  if (v > UINT32_MAX)
701  {
702  GNUNET_break (0);
703  return GNUNET_SYSERR;
704  }
705  *u = (uint32_t) v;
706  return GNUNET_OK;
707 }
708 
709 
717 GNUNET_SQ_result_spec_uint32 (uint32_t *u32)
718 {
719  struct GNUNET_SQ_ResultSpec rs = {
720  .conv = &extract_uint32,
721  .dst = u32,
722  .dst_size = sizeof(uint32_t),
723  .num_params = 1
724  };
725 
726  return rs;
727 }
728 
729 
742 static int
744  sqlite3_stmt *result,
745  unsigned int column,
746  size_t *dst_size,
747  void *dst)
748 {
749  uint64_t *u = dst;
750 
751  GNUNET_assert (sizeof(uint64_t) == *dst_size);
752  if (SQLITE_INTEGER !=
753  sqlite3_column_type (result,
754  column))
755  {
756  GNUNET_break (0);
757  return GNUNET_SYSERR;
758  }
759  *u = (uint64_t) sqlite3_column_int64 (result,
760  column);
761  return GNUNET_OK;
762 }
763 
764 
772 GNUNET_SQ_result_spec_uint64 (uint64_t *u64)
773 {
774  struct GNUNET_SQ_ResultSpec rs = {
775  .conv = &extract_uint64,
776  .dst = u64,
777  .dst_size = sizeof(uint64_t),
778  .num_params = 1
779  };
780 
781  return rs;
782 }
783 
784 
785 /* end of sq_result_helper.c */
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
static int result
Global testing status.
static struct GNUNET_SCHEDULER_Task * t
Main task.
helper functions for Sqlite3 DB interactions
struct GNUNET_CRYPTO_RsaSignature * GNUNET_CRYPTO_rsa_signature_decode(const void *buf, size_t buf_size)
Decode the signature from the data-format back to the "normal", internal format.
Definition: crypto_rsa.c:1039
void GNUNET_CRYPTO_rsa_signature_free(struct GNUNET_CRYPTO_RsaSignature *sig)
Free memory occupied by signature.
Definition: crypto_rsa.c:991
void GNUNET_CRYPTO_rsa_public_key_free(struct GNUNET_CRYPTO_RsaPublicKey *key)
Free memory occupied by the public key.
Definition: crypto_rsa.c:268
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:423
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:638
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
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.
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_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.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time_nbo(struct GNUNET_TIME_AbsoluteNBO *at)
Absolute time expected.
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.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_string(char **dst)
0-terminated string expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint16(uint16_t *u16)
uint16_t expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
Absolute time expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint32(uint32_t *u32)
uint32_t expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_fixed_size(void *dst, size_t dst_size)
Fixed-size result expected.
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 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.
static void clean_var_blob(void *cls)
Cleanup memory allocated by extract_var_blob().
static void clean_rsa_pub(void *cls)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_variable_size(void **dst, size_t *sptr)
Variable-size result expected.
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.
static void clean_utf8_string(void *cls)
Cleanup memory allocated by extract_var_blob().
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 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.
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.
static void clean_rsa_sig(void *cls)
Function called to clean up memory allocated by a GNUNET_PQ_ResultConverter.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint64(uint64_t *u64)
uint64_t expected.
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.
The public information of an RSA key pair.
Definition: crypto_rsa.c:53
an RSA signature
Definition: crypto_rsa.c:65
Description of a DB result cell.
GNUNET_SQ_ResultConverter conv
What is the format of the result?
void * cls
Closure for conv and cleaner.
unsigned int num_params
Number of parameters (columns) eaten by this operation.
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...
void * dst
Destination for the data.
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.