GNUnet  0.20.0
pq_query_helper.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2014, 2015, 2016, 2020 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_common.h"
27 #include "gnunet_pq_lib.h"
28 #include "gnunet_time_lib.h"
29 #include "pq.h"
30 
31 
46 static int
47 qconv_null (void *cls,
48  const void *data,
49  size_t data_len,
50  void *param_values[],
51  int param_lengths[],
52  int param_formats[],
53  unsigned int param_length,
54  void *scratch[],
55  unsigned int scratch_length)
56 {
57  (void) scratch;
58  (void) scratch_length;
59  (void) data;
60  (void) data_len;
61  GNUNET_break (NULL == cls);
62  if (1 != param_length)
63  return -1;
64  param_values[0] = NULL;
65  param_lengths[0] = 0;
66  param_formats[0] = 1;
67  return 0;
68 }
69 
70 
73 {
74  struct GNUNET_PQ_QueryParam res = {
75  .conv = &qconv_null,
76  .num_params = 1
77  };
78 
79  return res;
80 }
81 
82 
97 static int
98 qconv_fixed (void *cls,
99  const void *data,
100  size_t data_len,
101  void *param_values[],
102  int param_lengths[],
103  int param_formats[],
104  unsigned int param_length,
105  void *scratch[],
106  unsigned int scratch_length)
107 {
108  (void) scratch;
109  (void) scratch_length;
110  GNUNET_break (NULL == cls);
111  if (1 != param_length)
112  return -1;
113  param_values[0] = (void *) data;
114  param_lengths[0] = data_len;
115  param_formats[0] = 1;
116  return 0;
117 }
118 
119 
121 GNUNET_PQ_query_param_fixed_size (const void *ptr,
122  size_t ptr_size)
123 {
124  struct GNUNET_PQ_QueryParam res = {
125  .conv = &qconv_fixed,
126  .conv_cls = NULL,
127  .data = ptr,
128  .size = ptr_size,
129  .num_params = 1
130  };
131 
132  return res;
133 }
134 
135 
137 GNUNET_PQ_query_param_string (const char *ptr)
138 {
140  strlen (ptr));
141 }
142 
143 
146 {
147  static uint8_t bt = 1;
148  static uint8_t bf = 0;
149 
150  return GNUNET_PQ_query_param_fixed_size (b ? &bt : &bf,
151  sizeof (uint8_t));
152 }
153 
154 
169 static int
170 qconv_uint16 (void *cls,
171  const void *data,
172  size_t data_len,
173  void *param_values[],
174  int param_lengths[],
175  int param_formats[],
176  unsigned int param_length,
177  void *scratch[],
178  unsigned int scratch_length)
179 {
180  const uint16_t *u_hbo = data;
181  uint16_t *u_nbo;
182 
183  (void) scratch;
184  (void) scratch_length;
185  GNUNET_break (NULL == cls);
186  if (1 != param_length)
187  return -1;
188  u_nbo = GNUNET_new (uint16_t);
189  scratch[0] = u_nbo;
190  *u_nbo = htons (*u_hbo);
191  param_values[0] = (void *) u_nbo;
192  param_lengths[0] = sizeof(uint16_t);
193  param_formats[0] = 1;
194  return 1;
195 }
196 
197 
199 GNUNET_PQ_query_param_uint16 (const uint16_t *x)
200 {
201  struct GNUNET_PQ_QueryParam res = {
202  .conv = &qconv_uint16,
203  .data = x,
204  .size = sizeof(*x),
205  .num_params = 1
206  };
207 
208  return res;
209 }
210 
211 
226 static int
227 qconv_uint32 (void *cls,
228  const void *data,
229  size_t data_len,
230  void *param_values[],
231  int param_lengths[],
232  int param_formats[],
233  unsigned int param_length,
234  void *scratch[],
235  unsigned int scratch_length)
236 {
237  const uint32_t *u_hbo = data;
238  uint32_t *u_nbo;
239 
240  (void) scratch;
241  (void) scratch_length;
242  GNUNET_break (NULL == cls);
243  if (1 != param_length)
244  return -1;
245  u_nbo = GNUNET_new (uint32_t);
246  scratch[0] = u_nbo;
247  *u_nbo = htonl (*u_hbo);
248  param_values[0] = (void *) u_nbo;
249  param_lengths[0] = sizeof(uint32_t);
250  param_formats[0] = 1;
251  return 1;
252 }
253 
254 
256 GNUNET_PQ_query_param_uint32 (const uint32_t *x)
257 {
258  struct GNUNET_PQ_QueryParam res = {
259  .conv = &qconv_uint32,
260  .data = x,
261  .size = sizeof(*x),
262  .num_params = 1
263  };
264 
265  return res;
266 }
267 
268 
283 static int
284 qconv_uint64 (void *cls,
285  const void *data,
286  size_t data_len,
287  void *param_values[],
288  int param_lengths[],
289  int param_formats[],
290  unsigned int param_length,
291  void *scratch[],
292  unsigned int scratch_length)
293 {
294  const uint64_t *u_hbo = data;
295  uint64_t *u_nbo;
296 
297  (void) scratch;
298  (void) scratch_length;
299  GNUNET_break (NULL == cls);
300  if (1 != param_length)
301  return -1;
302  u_nbo = GNUNET_new (uint64_t);
303  scratch[0] = u_nbo;
304  *u_nbo = GNUNET_htonll (*u_hbo);
305  param_values[0] = (void *) u_nbo;
306  param_lengths[0] = sizeof(uint64_t);
307  param_formats[0] = 1;
308  return 1;
309 }
310 
311 
313 GNUNET_PQ_query_param_uint64 (const uint64_t *x)
314 {
315  struct GNUNET_PQ_QueryParam res = {
316  .conv = &qconv_uint64,
317  .data = x,
318  .size = sizeof(*x),
319  .num_params = 1
320  };
321 
322  return res;
323 }
324 
325 
340 static int
342  const void *data,
343  size_t data_len,
344  void *param_values[],
345  int param_lengths[],
346  int param_formats[],
347  unsigned int param_length,
348  void *scratch[],
349  unsigned int scratch_length)
350 {
351  const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
352  void *buf;
353  size_t buf_size;
354 
355  GNUNET_break (NULL == cls);
356  if (1 != param_length)
357  return -1;
358  buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
359  &buf);
360  scratch[0] = buf;
361  param_values[0] = (void *) buf;
362  param_lengths[0] = buf_size;
363  param_formats[0] = 1;
364  return 1;
365 }
366 
367 
370  const struct GNUNET_CRYPTO_RsaPublicKey *x)
371 {
372  struct GNUNET_PQ_QueryParam res = {
373  .conv = &qconv_rsa_public_key,
374  .data = x,
375  .num_params = 1
376  };
377 
378  return res;
379 }
380 
381 
396 static int
398  const void *data,
399  size_t data_len,
400  void *param_values[],
401  int param_lengths[],
402  int param_formats[],
403  unsigned int param_length,
404  void *scratch[],
405  unsigned int scratch_length)
406 {
407  const struct GNUNET_CRYPTO_RsaSignature *sig = data;
408  void *buf;
409  size_t buf_size;
410 
411  GNUNET_break (NULL == cls);
412  if (1 != param_length)
413  return -1;
414  buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
415  &buf);
416  scratch[0] = buf;
417  param_values[0] = (void *) buf;
418  param_lengths[0] = buf_size;
419  param_formats[0] = 1;
420  return 1;
421 }
422 
423 
426 {
427  struct GNUNET_PQ_QueryParam res = {
428  .conv = &qconv_rsa_signature,
429  .data = x,
430  .num_params = 1
431  };
432 
433  return res;
434 }
435 
436 
451 static int
452 qconv_rel_time (void *cls,
453  const void *data,
454  size_t data_len,
455  void *param_values[],
456  int param_lengths[],
457  int param_formats[],
458  unsigned int param_length,
459  void *scratch[],
460  unsigned int scratch_length)
461 {
462  const struct GNUNET_TIME_Relative *u = data;
463  struct GNUNET_TIME_Relative rel;
464  uint64_t *u_nbo;
465 
466  GNUNET_break (NULL == cls);
467  if (1 != param_length)
468  return -1;
469  rel = *u;
470  if (rel.rel_value_us > INT64_MAX)
471  rel.rel_value_us = INT64_MAX;
472  u_nbo = GNUNET_new (uint64_t);
473  scratch[0] = u_nbo;
474  *u_nbo = GNUNET_htonll (rel.rel_value_us);
475  param_values[0] = (void *) u_nbo;
476  param_lengths[0] = sizeof(uint64_t);
477  param_formats[0] = 1;
478  return 1;
479 }
480 
481 
484 {
485  struct GNUNET_PQ_QueryParam res = {
486  .conv = &qconv_rel_time,
487  .data = x,
488  .size = sizeof(*x),
489  .num_params = 1
490  };
491 
492  return res;
493 }
494 
495 
510 static int
511 qconv_abs_time (void *cls,
512  const void *data,
513  size_t data_len,
514  void *param_values[],
515  int param_lengths[],
516  int param_formats[],
517  unsigned int param_length,
518  void *scratch[],
519  unsigned int scratch_length)
520 {
521  const struct GNUNET_TIME_Absolute *u = data;
522  struct GNUNET_TIME_Absolute abs;
523  uint64_t *u_nbo;
524 
525  GNUNET_break (NULL == cls);
526  if (1 != param_length)
527  return -1;
528  abs = *u;
529  if (abs.abs_value_us > INT64_MAX)
530  abs.abs_value_us = INT64_MAX;
531  u_nbo = GNUNET_new (uint64_t);
532  scratch[0] = u_nbo;
533  *u_nbo = GNUNET_htonll (abs.abs_value_us);
534  param_values[0] = (void *) u_nbo;
535  param_lengths[0] = sizeof(uint64_t);
536  param_formats[0] = 1;
537  return 1;
538 }
539 
540 
543 {
544  struct GNUNET_PQ_QueryParam res = {
545  .conv = &qconv_abs_time,
546  .data = x,
547  .size = sizeof(*x),
548  .num_params = 1
549  };
550 
551  return res;
552 }
553 
554 
557  const struct GNUNET_TIME_AbsoluteNBO *x)
558 {
559  return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
560 }
561 
562 
565 {
566  return GNUNET_PQ_query_param_absolute_time (&x->abs_time);
567 }
568 
569 
572  const struct GNUNET_TIME_TimestampNBO *x)
573 {
574  return GNUNET_PQ_query_param_absolute_time_nbo (&x->abs_time_nbo);
575 }
576 
577 
585 {
592  const size_t *sizes;
593 
598  size_t same_size;
599 
606 
610  enum array_types typ;
611 
615  Oid oid;
616 };
617 
622 static void
624 {
625  GNUNET_free (cls);
626 }
627 
628 
647 static int
649  void *cls,
650  const void *data,
651  size_t data_len,
652  void *param_values[],
653  int param_lengths[],
654  int param_formats[],
655  unsigned int param_length,
656  void *scratch[],
657  unsigned int scratch_length)
658 {
659  struct qconv_array_cls *meta = cls;
660  size_t num = data_len;
661  size_t total_size;
662  const size_t *sizes;
663  bool same_sized;
664  size_t *string_lengths = NULL;
665  void *elements = NULL;
666  bool noerror = true;
667 
668  (void) (param_length);
669  (void) (scratch_length);
670 
671  GNUNET_assert (NULL != meta);
672  GNUNET_assert (num < INT_MAX);
673 
674  sizes = meta->sizes;
675  same_sized = (0 != meta->same_size);
676 
677 #define RETURN_UNLESS(cond) \
678  do { \
679  if (! (cond)) \
680  { \
681  GNUNET_break ((cond)); \
682  noerror = false; \
683  goto DONE; \
684  } \
685  } while (0)
686 
687  /* Calculate sizes and check bounds */
688  {
689  /* num * length-field */
690  size_t x = sizeof(uint32_t);
691  size_t y = x * num;
692  RETURN_UNLESS ((0 == num) || (y / num == x));
693 
694  /* size of header */
695  total_size = x = sizeof(struct pq_array_header);
696  total_size += y;
697  RETURN_UNLESS (total_size >= x);
698 
699  /* sizes of elements */
700  if (same_sized)
701  {
702  x = num * meta->same_size;
703  RETURN_UNLESS ((0 == num) || (x / num == meta->same_size));
704 
705  y = total_size;
706  total_size += x;
707  RETURN_UNLESS (total_size >= y);
708  }
709  else /* sizes are different per element */
710  {
711  /* for an array of strings we need to get their length's first */
712  if (array_of_string == meta->typ)
713  {
714  string_lengths = GNUNET_new_array (num, size_t);
715 
716  if (meta->continuous)
717  {
718  const char *ptr = data;
719  for (unsigned int i = 0; i < num; i++)
720  {
721  size_t len = strlen (ptr);
722  string_lengths[i] = len;
723  ptr += len + 1;
724  }
725  }
726  else
727  {
728  const char **str = (const char **) data;
729  for (unsigned int i = 0; i < num; i++)
730  string_lengths[i] = strlen (str[i]);
731  }
732 
733  sizes = string_lengths;
734  }
735 
736  for (unsigned int i = 0; i < num; i++)
737  {
738  x = total_size;
739  total_size += sizes[i];
740  RETURN_UNLESS (total_size >= x);
741  }
742  }
743 
744  RETURN_UNLESS (total_size < INT_MAX);
745 
746  elements = GNUNET_malloc (total_size);
747  }
748 
749  /* Write data */
750  {
751  char *in = (char *) data;
752  char *out = elements;
753  size_t nullbyte = (array_of_string == meta->typ) ? 1 : 0;
754  struct pq_array_header h = {
755  .ndim = htonl (1), /* We only support one-dimensional arrays */
756  .has_null = htonl (0), /* We do not support NULL entries in arrays */
757  .lbound = htonl (1), /* Default start index value */
758  .dim = htonl (num),
759  .oid = htonl (meta->oid),
760  };
761 
762  /* Write header */
763  GNUNET_memcpy (out, &h, sizeof(h));
764  out += sizeof(h);
765 
766 
767  /* Write elements */
768  for (unsigned int i = 0; i < num; i++)
769  {
770  size_t sz = same_sized ? meta->same_size : sizes[i];
771  size_t hsz = htonl (sz);
772 
773  GNUNET_memcpy (out,
774  &hsz,
775  sizeof(hsz));
776  out += sizeof(uint32_t);
777 
778  switch (meta->typ)
779  {
780  case array_of_bool:
781  {
782  GNUNET_assert (sizeof(bool) == sz);
783  *(bool *) out = (*(bool *) in);
784  in += sz;
785  break;
786  }
787  case array_of_uint16:
788  {
789  GNUNET_assert (sizeof(uint16_t) == sz);
790  *(uint16_t *) out = htons (*(uint16_t *) in);
791  in += sz;
792  break;
793  }
794  case array_of_uint32:
795  {
796  uint32_t v;
797  GNUNET_assert (sizeof(uint32_t) == sz);
798 
799  v = htonl (*(uint32_t *) in);
800  GNUNET_memcpy (out,
801  &v,
802  sizeof(v));
803  in += sz;
804  break;
805  }
806  case array_of_uint64:
807  {
808  uint64_t tmp;
809  GNUNET_assert (sizeof(uint64_t) == sz);
810 
811  tmp = GNUNET_htonll (*(uint64_t *) in);
812  GNUNET_memcpy (out,
813  &tmp,
814  sizeof(tmp));
815  in += sz;
816  break;
817  }
818  case array_of_byte:
819  case array_of_string:
820  {
821  const void *ptr;
822 
823  if (meta->continuous)
824  {
825  ptr = in;
826  in += sz + nullbyte;
827  }
828  else
829  ptr = ((const void **) data)[i];
830 
831  GNUNET_memcpy (out,
832  ptr,
833  sz);
834  break;
835  }
836  case array_of_abs_time:
837  case array_of_rel_time:
838  case array_of_timestamp:
839  {
840  uint64_t val;
841 
842  switch (meta->typ)
843  {
844  case array_of_abs_time:
845  {
846  const struct GNUNET_TIME_Absolute *abs =
847  (const struct GNUNET_TIME_Absolute *) in;
848 
849  GNUNET_assert (sizeof(struct GNUNET_TIME_Absolute) == sz);
850 
851  if (! meta->continuous)
852  abs = ((const struct GNUNET_TIME_Absolute **) data)[i];
853 
854  val = abs->abs_value_us;
855  break;
856  }
857  case array_of_rel_time:
858  {
859  const struct GNUNET_TIME_Relative *rel =
860  (const struct GNUNET_TIME_Relative *) in;
861 
862  GNUNET_assert (sizeof(struct GNUNET_TIME_Relative) == sz);
863 
864  if (! meta->continuous)
865  rel = ((const struct GNUNET_TIME_Relative **) data)[i];
866 
867  val = rel->rel_value_us;
868  break;
869  }
870  case array_of_timestamp:
871  {
872  const struct GNUNET_TIME_Timestamp *ts =
873  (const struct GNUNET_TIME_Timestamp *) in;
874 
875  GNUNET_assert (sizeof(struct GNUNET_TIME_Timestamp) == sz);
876 
877  if (! meta->continuous)
878  ts = ((const struct GNUNET_TIME_Timestamp **) data)[i];
879 
880  val = ts->abs_time.abs_value_us;
881  break;
882  }
883  default:
884  {
885  GNUNET_assert (0);
886  }
887  }
888 
889  if (val > INT64_MAX)
890  val = INT64_MAX;
891 
892  val = GNUNET_htonll (val);
893  GNUNET_memcpy (out,
894  &val,
895  sizeof(val));
896 
897  if (meta->continuous)
898  in += sz;
899 
900  break;
901  }
902  default:
903  {
904  GNUNET_assert (0);
905  break;
906  }
907  }
908  out += sz;
909  }
910  }
911 
912  param_values[0] = elements;
913  param_lengths[0] = total_size;
914  param_formats[0] = 1;
915  scratch[0] = elements;
916 
917 DONE:
918  GNUNET_free (string_lengths);
919 
920  if (noerror)
921  return 1;
922 
923  return -1;
924 }
925 
926 
939 static struct GNUNET_PQ_QueryParam
941  unsigned int num,
942  bool continuous,
943  const void *elements,
944  const size_t *sizes,
945  size_t same_size,
946  enum array_types typ,
947  Oid oid)
948 {
949  struct qconv_array_cls *meta = GNUNET_new (struct qconv_array_cls);
950 
951  meta->typ = typ;
952  meta->oid = oid;
953  meta->sizes = sizes;
954  meta->same_size = same_size;
955  meta->continuous = continuous;
956 
957  {
958  struct GNUNET_PQ_QueryParam res = {
959  .conv = qconv_array,
960  .conv_cls = meta,
961  .conv_cls_cleanup = &qconv_array_cls_cleanup,
962  .data = elements,
963  .size = num,
964  .num_params = 1,
965  };
966 
967  return res;
968  }
969 }
970 
971 
974  unsigned int num,
975  const bool *elements,
976  struct GNUNET_PQ_Context *db)
977 {
978  Oid oid;
979 
982  "bool",
983  &oid));
985  true,
986  elements,
987  NULL,
988  sizeof(bool),
990  oid);
991 }
992 
993 
996  unsigned int num,
997  const uint16_t *elements,
998  struct GNUNET_PQ_Context *db)
999 {
1000  Oid oid;
1001 
1004  "int2",
1005  &oid));
1007  true,
1008  elements,
1009  NULL,
1010  sizeof(uint16_t),
1012  oid);
1013 }
1014 
1015 
1016 struct GNUNET_PQ_QueryParam
1018  unsigned int num,
1019  const uint32_t *elements,
1020  struct GNUNET_PQ_Context *db)
1021 {
1022  Oid oid;
1023 
1026  "int4",
1027  &oid));
1029  true,
1030  elements,
1031  NULL,
1032  sizeof(uint32_t),
1034  oid);
1035 }
1036 
1037 
1038 struct GNUNET_PQ_QueryParam
1040  unsigned int num,
1041  const uint64_t *elements,
1042  struct GNUNET_PQ_Context *db)
1043 {
1044  Oid oid;
1045 
1048  "int8",
1049  &oid));
1051  true,
1052  elements,
1053  NULL,
1054  sizeof(uint64_t),
1056  oid);
1057 }
1058 
1059 
1060 struct GNUNET_PQ_QueryParam
1062  unsigned int num,
1063  const void *elements,
1064  const size_t *sizes,
1065  struct GNUNET_PQ_Context *db)
1066 {
1067  Oid oid;
1068 
1071  "bytea",
1072  &oid));
1074  true,
1075  elements,
1076  sizes,
1077  0,
1078  array_of_byte,
1079  oid);
1080 }
1081 
1082 
1083 struct GNUNET_PQ_QueryParam
1085  unsigned int num,
1086  const void *elements[static num],
1087  const size_t *sizes,
1088  struct GNUNET_PQ_Context *db)
1089 {
1090  Oid oid;
1091 
1094  "bytea",
1095  &oid));
1097  false,
1098  elements,
1099  sizes,
1100  0,
1101  array_of_byte,
1102  oid);
1103 }
1104 
1105 
1106 struct GNUNET_PQ_QueryParam
1108  unsigned int num,
1109  const void *elements,
1110  size_t same_size,
1111  struct GNUNET_PQ_Context *db)
1112 {
1113  Oid oid;
1114 
1117  "bytea",
1118  &oid));
1120  true,
1121  elements,
1122  NULL,
1123  same_size,
1124  array_of_byte,
1125  oid);
1126 }
1127 
1128 
1129 struct GNUNET_PQ_QueryParam
1131  unsigned int num,
1132  const void *elements[static num],
1133  size_t same_size,
1134  struct GNUNET_PQ_Context *db)
1135 {
1136  Oid oid;
1137 
1140  "bytea",
1141  &oid));
1143  false,
1144  elements,
1145  NULL,
1146  same_size,
1147  array_of_byte,
1148  oid);
1149 }
1150 
1151 
1152 struct GNUNET_PQ_QueryParam
1154  unsigned int num,
1155  const char *elements,
1156  struct GNUNET_PQ_Context *db)
1157 {
1158  Oid oid;
1159 
1162  "text",
1163  &oid));
1165  true,
1166  elements,
1167  NULL,
1168  0,
1170  oid);
1171 }
1172 
1173 
1174 struct GNUNET_PQ_QueryParam
1176  unsigned int num,
1177  const char *elements[static num],
1178  struct GNUNET_PQ_Context *db)
1179 {
1180  Oid oid;
1181 
1184  "text",
1185  &oid));
1187  false,
1188  elements,
1189  NULL,
1190  0,
1192  oid);
1193 }
1194 
1195 
1196 struct GNUNET_PQ_QueryParam
1198  unsigned int num,
1199  const struct GNUNET_TIME_Absolute *elements,
1200  struct GNUNET_PQ_Context *db)
1201 {
1202  Oid oid;
1203 
1206  "int8",
1207  &oid));
1209  true,
1210  elements,
1211  NULL,
1212  sizeof(struct GNUNET_TIME_Absolute),
1214  oid);
1215 }
1216 
1217 
1218 struct GNUNET_PQ_QueryParam
1220  unsigned int num,
1221  const struct GNUNET_TIME_Absolute *elements[],
1222  struct GNUNET_PQ_Context *db)
1223 {
1224  Oid oid;
1225 
1228  "int8",
1229  &oid));
1231  false,
1232  elements,
1233  NULL,
1234  sizeof(struct GNUNET_TIME_Absolute),
1236  oid);
1237 }
1238 
1239 
1240 struct GNUNET_PQ_QueryParam
1242  unsigned int num,
1243  const struct GNUNET_TIME_Relative *elements,
1244  struct GNUNET_PQ_Context *db)
1245 {
1246  Oid oid;
1247 
1250  "int8",
1251  &oid));
1253  true,
1254  elements,
1255  NULL,
1256  sizeof(struct GNUNET_TIME_Relative),
1258  oid);
1259 }
1260 
1261 
1262 struct GNUNET_PQ_QueryParam
1264  unsigned int num,
1265  const struct GNUNET_TIME_Relative *elements[],
1266  struct GNUNET_PQ_Context *db)
1267 {
1268  Oid oid;
1269 
1272  "int8",
1273  &oid));
1275  false,
1276  elements,
1277  NULL,
1278  sizeof(struct GNUNET_TIME_Relative),
1280  oid);
1281 }
1282 
1283 
1284 struct GNUNET_PQ_QueryParam
1286  unsigned int num,
1287  const struct GNUNET_TIME_Timestamp *elements,
1288  struct GNUNET_PQ_Context *db)
1289 {
1290  Oid oid;
1291 
1294  "int8",
1295  &oid));
1297  true,
1298  elements,
1299  NULL,
1300  sizeof(struct GNUNET_TIME_Timestamp),
1302  oid);
1303 }
1304 
1305 
1306 struct GNUNET_PQ_QueryParam
1308  unsigned int num,
1309  const struct GNUNET_TIME_Timestamp *elements[],
1310  struct GNUNET_PQ_Context *db)
1311 {
1312  Oid oid;
1313 
1316  "int8",
1317  &oid));
1319  false,
1320  elements,
1321  NULL,
1322  sizeof(struct GNUNET_TIME_Timestamp),
1324  oid);
1325 }
1326 
1327 
1328 /* end of pq_query_helper.c */
#define INT_MAX
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static int res
uint32_t data
The data value.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static struct GNUNET_FS_MetaData * meta
Meta-data provided via command-line option.
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:97
static char buf[2048]
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
helper functions for Postgres DB interactions
#define GNUNET_PQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
uint32_t oid
Definition: gnunet_pq_lib.h:2
enum GNUNET_GenericReturnValue GNUNET_PQ_get_oid_by_name(struct GNUNET_PQ_Context *db, const char *name, Oid *oid)
Returns the oid for a given datatype by name.
Definition: pq_connect.c:326
Functions related to time.
size_t GNUNET_CRYPTO_rsa_public_key_encode(const struct GNUNET_CRYPTO_RsaPublicKey *key, void **buffer)
Encode the public key in a format suitable for storing it into a file.
Definition: crypto_rsa.c:325
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:37
size_t GNUNET_CRYPTO_rsa_signature_encode(const struct GNUNET_CRYPTO_RsaSignature *sig, void **buffer)
Encode the given signature in a format suitable for storing it into a file.
Definition: crypto_rsa.c:999
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
#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_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
shared internal data structures of libgnunetpq
array_types
Internal types that are supported as array types.
Definition: pq.h:132
@ array_of_byte
Definition: pq.h:137
@ array_of_uint32
Definition: pq.h:135
@ array_of_string
Definition: pq.h:138
@ array_of_rel_time
Definition: pq.h:140
@ array_of_uint16
Definition: pq.h:134
@ array_of_uint64
Definition: pq.h:136
@ array_of_abs_time
Definition: pq.h:139
@ array_of_timestamp
Definition: pq.h:141
@ array_of_bool
Definition: pq.h:133
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
static int qconv_fixed(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_rel_time(unsigned int num, const struct GNUNET_TIME_Relative *elements[], struct GNUNET_PQ_Context *db)
Generate query parameter for an array of relative time stamps (pointers)
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
static int qconv_rel_time(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_abs_time(unsigned int num, const struct GNUNET_TIME_Absolute *elements, struct GNUNET_PQ_Context *db)
Generate query parameter for an array of absolute time stamps (continuous)
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_null(void)
Generate query parameter to create a NULL value.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_timestamp(unsigned int num, const struct GNUNET_TIME_Timestamp *elements[], struct GNUNET_PQ_Context *db)
Generate query parameter for an array of time stamps (pointers)
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_abs_time(unsigned int num, const struct GNUNET_TIME_Absolute *elements[], struct GNUNET_PQ_Context *db)
Generate query parameter for an array of absolute time stamps (pointers)
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_uint32(unsigned int num, const uint32_t *elements, struct GNUNET_PQ_Context *db)
Generate query parameter for an array of uint32_t in host byte order.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_bytes(unsigned int num, const void *elements[static num], const size_t *sizes, struct GNUNET_PQ_Context *db)
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_relative_time(const struct GNUNET_TIME_Relative *x)
Generate query parameter for a relative time value.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_uint64(unsigned int num, const uint64_t *elements, struct GNUNET_PQ_Context *db)
Generate query parameter for an array of uint64 in host byte order.
#define RETURN_UNLESS(cond)
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint16(const uint16_t *x)
Generate query parameter for an uint16_t in host byte order.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *x)
Generate query parameter for an RSA public key.
static int qconv_null(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bool(unsigned int num, const bool *elements, struct GNUNET_PQ_Context *db)
Generate query parameter for an array of bool in host byte order.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
Generate query parameter for an absolute time value.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_string(unsigned int num, const char *elements[static num], struct GNUNET_PQ_Context *db)
Generate query parameter for an array of strings (pointers)
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bytes(unsigned int num, const void *elements, const size_t *sizes, struct GNUNET_PQ_Context *db)
Generate query parameter for an array of buffers elements, each of corresponding size given in sizes.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_bool(bool b)
Pass a boolean into a query.
static void qconv_array_cls_cleanup(void *cls)
Callback to cleanup a qconv_array_cls to be used during GNUNET_PQ_cleanup_query_params_closures.
static int qconv_uint64(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
static int qconv_uint32(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
static int qconv_array(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters for arrays.
static int qconv_abs_time(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
static int qconv_rsa_signature(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_timestamp(unsigned int num, const struct GNUNET_TIME_Timestamp *elements, struct GNUNET_PQ_Context *db)
Generate query parameter for an array of time stamps (continuous)
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_rel_time(unsigned int num, const struct GNUNET_TIME_Relative *elements, struct GNUNET_PQ_Context *db)
Generate query parameter for an array of relative time stamps (continuous)
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_uint16(unsigned int num, const uint16_t *elements, struct GNUNET_PQ_Context *db)
Generate query parameter for an array of uint16_t in host byte order.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
static int qconv_uint16(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_timestamp(const struct GNUNET_TIME_Timestamp *x)
Generate query parameter for a timestamp.
static int qconv_rsa_public_key(void *cls, const void *data, size_t data_len, void *param_values[], int param_lengths[], int param_formats[], unsigned int param_length, void *scratch[], unsigned int scratch_length)
Function called to convert input argument into SQL parameters.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_timestamp_nbo(const struct GNUNET_TIME_TimestampNBO *x)
Generate query parameter for a timestamp in NBO.
static struct GNUNET_PQ_QueryParam query_param_array_generic(unsigned int num, bool continuous, const void *elements, const size_t *sizes, size_t same_size, enum array_types typ, Oid oid)
Function to genreate a typ specific query parameter and corresponding closure.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_ptrs_bytes_same_size(unsigned int num, const void *elements[static num], size_t same_size, struct GNUNET_PQ_Context *db)
Generate query parameter for an array of pointers to buffers elements, each of the same size size.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_string(unsigned int num, const char *elements, struct GNUNET_PQ_Context *db)
Generate query parameter for an array of strings (continuous)
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string(const char *ptr)
Generate query parameter for a string.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_array_bytes_same_size(unsigned int num, const void *elements, size_t same_size, struct GNUNET_PQ_Context *db)
Generate query parameter for an array of buffers elements, each of the same size size.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
Generate query parameter for an RSA signature.
The public information of an RSA key pair.
Definition: crypto_rsa.c:53
an RSA signature
Definition: crypto_rsa.c:65
Handle to Postgres database.
Definition: pq.h:36
unsigned int num
Definition: pq.h:112
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:83
size_t size
Size of data.
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.
Time for timestamps used by GNUnet, in seconds and in network byte order.
Rounded time for timestamps used by GNUnet, in seconds.
struct GNUNET_TIME_Absolute abs_time
The actual value.
the header for a postgresql array in binary format.
Definition: pq.h:151
Closure for the array type handlers.
const size_t * sizes
If not null, contains the array of sizes (the size of the array is the .size field in the ambient GNU...
enum array_types typ
Type of the array elements.
size_t same_size
If size and c_sizes are NULL, this field defines the same size for each element in the array.
Oid oid
Oid of the array elements.
bool continuous
If true, the array parameter to the data pointer to the qconv_array is a continuous byte array of dat...