GNUnet  0.11.x
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_util_lib.h"
27 #include "gnunet_pq_lib.h"
28 
29 
44 static int
45 qconv_null (void *cls,
46  const void *data,
47  size_t data_len,
48  void *param_values[],
49  int param_lengths[],
50  int param_formats[],
51  unsigned int param_length,
52  void *scratch[],
53  unsigned int scratch_length)
54 {
55  (void) scratch;
56  (void) scratch_length;
57  (void) data;
58  (void) data_len;
59  GNUNET_break (NULL == cls);
60  if (1 != param_length)
61  return -1;
62  param_values[0] = NULL;
63  param_lengths[0] = 0;
64  param_formats[0] = 1;
65  return 0;
66 }
67 
68 
71 {
72  struct GNUNET_PQ_QueryParam res = {
73  &qconv_null, NULL, NULL, 0, 1
74  };
75 
76  return res;
77 }
78 
79 
94 static int
95 qconv_fixed (void *cls,
96  const void *data,
97  size_t data_len,
98  void *param_values[],
99  int param_lengths[],
100  int param_formats[],
101  unsigned int param_length,
102  void *scratch[],
103  unsigned int scratch_length)
104 {
105  (void) scratch;
106  (void) scratch_length;
107  GNUNET_break (NULL == cls);
108  if (1 != param_length)
109  return -1;
110  param_values[0] = (void *) data;
111  param_lengths[0] = data_len;
112  param_formats[0] = 1;
113  return 0;
114 }
115 
116 
118 GNUNET_PQ_query_param_fixed_size (const void *ptr,
119  size_t ptr_size)
120 {
121  struct GNUNET_PQ_QueryParam res = {
122  &qconv_fixed, NULL, ptr, ptr_size, 1
123  };
124 
125  return res;
126 }
127 
128 
130 GNUNET_PQ_query_param_string (const char *ptr)
131 {
133  strlen (ptr));
134 }
135 
136 
151 static int
152 qconv_uint16 (void *cls,
153  const void *data,
154  size_t data_len,
155  void *param_values[],
156  int param_lengths[],
157  int param_formats[],
158  unsigned int param_length,
159  void *scratch[],
160  unsigned int scratch_length)
161 {
162  const uint16_t *u_hbo = data;
163  uint16_t *u_nbo;
164 
165  (void) scratch;
166  (void) scratch_length;
167  GNUNET_break (NULL == cls);
168  if (1 != param_length)
169  return -1;
170  u_nbo = GNUNET_new (uint16_t);
171  scratch[0] = u_nbo;
172  *u_nbo = htons (*u_hbo);
173  param_values[0] = (void *) u_nbo;
174  param_lengths[0] = sizeof(uint16_t);
175  param_formats[0] = 1;
176  return 1;
177 }
178 
179 
181 GNUNET_PQ_query_param_uint16 (const uint16_t *x)
182 {
183  struct GNUNET_PQ_QueryParam res =
184  { &qconv_uint16, NULL, x, sizeof(*x), 1 };
185 
186  return res;
187 }
188 
189 
204 static int
205 qconv_uint32 (void *cls,
206  const void *data,
207  size_t data_len,
208  void *param_values[],
209  int param_lengths[],
210  int param_formats[],
211  unsigned int param_length,
212  void *scratch[],
213  unsigned int scratch_length)
214 {
215  const uint32_t *u_hbo = data;
216  uint32_t *u_nbo;
217 
218  (void) scratch;
219  (void) scratch_length;
220  GNUNET_break (NULL == cls);
221  if (1 != param_length)
222  return -1;
223  u_nbo = GNUNET_new (uint32_t);
224  scratch[0] = u_nbo;
225  *u_nbo = htonl (*u_hbo);
226  param_values[0] = (void *) u_nbo;
227  param_lengths[0] = sizeof(uint32_t);
228  param_formats[0] = 1;
229  return 1;
230 }
231 
232 
234 GNUNET_PQ_query_param_uint32 (const uint32_t *x)
235 {
236  struct GNUNET_PQ_QueryParam res =
237  { &qconv_uint32, NULL, x, sizeof(*x), 1 };
238 
239  return res;
240 }
241 
242 
257 static int
258 qconv_uint64 (void *cls,
259  const void *data,
260  size_t data_len,
261  void *param_values[],
262  int param_lengths[],
263  int param_formats[],
264  unsigned int param_length,
265  void *scratch[],
266  unsigned int scratch_length)
267 {
268  const uint64_t *u_hbo = data;
269  uint64_t *u_nbo;
270 
271  (void) scratch;
272  (void) scratch_length;
273  GNUNET_break (NULL == cls);
274  if (1 != param_length)
275  return -1;
276  u_nbo = GNUNET_new (uint64_t);
277  scratch[0] = u_nbo;
278  *u_nbo = GNUNET_htonll (*u_hbo);
279  param_values[0] = (void *) u_nbo;
280  param_lengths[0] = sizeof(uint64_t);
281  param_formats[0] = 1;
282  return 1;
283 }
284 
285 
287 GNUNET_PQ_query_param_uint64 (const uint64_t *x)
288 {
289  struct GNUNET_PQ_QueryParam res =
290  { &qconv_uint64, NULL, x, sizeof(*x), 1 };
291 
292  return res;
293 }
294 
295 
310 static int
312  const void *data,
313  size_t data_len,
314  void *param_values[],
315  int param_lengths[],
316  int param_formats[],
317  unsigned int param_length,
318  void *scratch[],
319  unsigned int scratch_length)
320 {
321  const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
322  void *buf;
323  size_t buf_size;
324 
325  GNUNET_break (NULL == cls);
326  if (1 != param_length)
327  return -1;
328  buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
329  &buf);
330  scratch[0] = buf;
331  param_values[0] = (void *) buf;
332  param_lengths[0] = buf_size;
333  param_formats[0] = 1;
334  return 1;
335 }
336 
337 
341 {
342  struct GNUNET_PQ_QueryParam res =
343  { &qconv_rsa_public_key, NULL, (x), 0, 1 };
344 
345  return res;
346 }
347 
348 
363 static int
365  const void *data,
366  size_t data_len,
367  void *param_values[],
368  int param_lengths[],
369  int param_formats[],
370  unsigned int param_length,
371  void *scratch[],
372  unsigned int scratch_length)
373 {
374  const struct GNUNET_CRYPTO_RsaSignature *sig = data;
375  void *buf;
376  size_t buf_size;
377 
378  GNUNET_break (NULL == cls);
379  if (1 != param_length)
380  return -1;
381  buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
382  &buf);
383  scratch[0] = buf;
384  param_values[0] = (void *) buf;
385  param_lengths[0] = buf_size;
386  param_formats[0] = 1;
387  return 1;
388 }
389 
390 
393 {
394  struct GNUNET_PQ_QueryParam res =
395  { &qconv_rsa_signature, NULL, (x), 0, 1 };
396 
397  return res;
398 }
399 
400 
415 static int
416 qconv_rel_time (void *cls,
417  const void *data,
418  size_t data_len,
419  void *param_values[],
420  int param_lengths[],
421  int param_formats[],
422  unsigned int param_length,
423  void *scratch[],
424  unsigned int scratch_length)
425 {
426  const struct GNUNET_TIME_Relative *u = data;
427  struct GNUNET_TIME_Relative rel;
428  uint64_t *u_nbo;
429 
430  GNUNET_break (NULL == cls);
431  if (1 != param_length)
432  return -1;
433  rel = *u;
434  if (rel.rel_value_us > INT64_MAX)
435  rel.rel_value_us = INT64_MAX;
436  u_nbo = GNUNET_new (uint64_t);
437  scratch[0] = u_nbo;
438  *u_nbo = GNUNET_htonll (rel.rel_value_us);
439  param_values[0] = (void *) u_nbo;
440  param_lengths[0] = sizeof(uint64_t);
441  param_formats[0] = 1;
442  return 1;
443 }
444 
445 
448 {
449  struct GNUNET_PQ_QueryParam res =
450  { &qconv_rel_time, NULL, x, sizeof(*x), 1 };
451 
452  return res;
453 }
454 
455 
470 static int
471 qconv_abs_time (void *cls,
472  const void *data,
473  size_t data_len,
474  void *param_values[],
475  int param_lengths[],
476  int param_formats[],
477  unsigned int param_length,
478  void *scratch[],
479  unsigned int scratch_length)
480 {
481  const struct GNUNET_TIME_Absolute *u = data;
482  struct GNUNET_TIME_Absolute abs;
483  uint64_t *u_nbo;
484 
485  GNUNET_break (NULL == cls);
486  if (1 != param_length)
487  return -1;
488  abs = *u;
489  if (abs.abs_value_us > INT64_MAX)
490  abs.abs_value_us = INT64_MAX;
491  u_nbo = GNUNET_new (uint64_t);
492  scratch[0] = u_nbo;
493  *u_nbo = GNUNET_htonll (abs.abs_value_us);
494  param_values[0] = (void *) u_nbo;
495  param_lengths[0] = sizeof(uint64_t);
496  param_formats[0] = 1;
497  return 1;
498 }
499 
500 
503 {
504  struct GNUNET_PQ_QueryParam res = {
505  &qconv_abs_time, NULL, x, sizeof(*x), 1
506  };
507 
508  return res;
509 }
510 
511 
515 {
516  return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
517 }
518 
519 
520 /* end of pq_query_helper.c */
static int res
uint32_t data
The data value.
static char buf[2048]
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:36
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:300
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:953
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.
#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.
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_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_null(void)
Generate query parameter to create a NULL value.
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_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_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
Generate query parameter for an absolute time value.
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_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_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.
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_string(const char *ptr)
Generate query parameter for a string.
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:52
an RSA signature
Definition: crypto_rsa.c:64
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:65
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.