GNUnet  0.10.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 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_fixed(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  GNUNET_break(NULL == cls);
58  if (1 != param_length)
59  return -1;
60  param_values[0] = (void *)data;
61  param_lengths[0] = data_len;
62  param_formats[0] = 1;
63  return 0;
64 }
65 
66 
76  size_t ptr_size)
77 {
78  struct GNUNET_PQ_QueryParam res =
79  { &qconv_fixed, NULL, ptr, ptr_size, 1 };
80 
81  return res;
82 }
83 
84 
92 {
93  return GNUNET_PQ_query_param_fixed_size(ptr, strlen(ptr));
94 }
95 
96 
111 static int
112 qconv_uint16(void *cls,
113  const void *data,
114  size_t data_len,
115  void *param_values[],
116  int param_lengths[],
117  int param_formats[],
118  unsigned int param_length,
119  void *scratch[],
120  unsigned int scratch_length)
121 {
122  const uint16_t *u_hbo = data;
123  uint16_t *u_nbo;
124 
125  (void)scratch;
126  (void)scratch_length;
127  GNUNET_break(NULL == cls);
128  if (1 != param_length)
129  return -1;
130  u_nbo = GNUNET_new(uint16_t);
131  scratch[0] = u_nbo;
132  *u_nbo = htons(*u_hbo);
133  param_values[0] = (void *)u_nbo;
134  param_lengths[0] = sizeof(uint16_t);
135  param_formats[0] = 1;
136  return 1;
137 }
138 
139 
147 {
148  struct GNUNET_PQ_QueryParam res =
149  { &qconv_uint16, NULL, x, sizeof(*x), 1 };
150 
151  return res;
152 }
153 
154 
169 static int
170 qconv_uint32(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 uint32_t *u_hbo = data;
181  uint32_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(uint32_t);
189  scratch[0] = u_nbo;
190  *u_nbo = htonl(*u_hbo);
191  param_values[0] = (void *)u_nbo;
192  param_lengths[0] = sizeof(uint32_t);
193  param_formats[0] = 1;
194  return 1;
195 }
196 
197 
205 {
206  struct GNUNET_PQ_QueryParam res =
207  { &qconv_uint32, NULL, x, sizeof(*x), 1 };
208 
209  return res;
210 }
211 
212 
227 static int
228 qconv_uint64(void *cls,
229  const void *data,
230  size_t data_len,
231  void *param_values[],
232  int param_lengths[],
233  int param_formats[],
234  unsigned int param_length,
235  void *scratch[],
236  unsigned int scratch_length)
237 {
238  const uint64_t *u_hbo = data;
239  uint64_t *u_nbo;
240 
241  (void)scratch;
242  (void)scratch_length;
243  GNUNET_break(NULL == cls);
244  if (1 != param_length)
245  return -1;
246  u_nbo = GNUNET_new(uint64_t);
247  scratch[0] = u_nbo;
248  *u_nbo = GNUNET_htonll(*u_hbo);
249  param_values[0] = (void *)u_nbo;
250  param_lengths[0] = sizeof(uint64_t);
251  param_formats[0] = 1;
252  return 1;
253 }
254 
255 
263 {
264  struct GNUNET_PQ_QueryParam res =
265  { &qconv_uint64, NULL, x, sizeof(*x), 1 };
266 
267  return res;
268 }
269 
270 
285 static int
287  const void *data,
288  size_t data_len,
289  void *param_values[],
290  int param_lengths[],
291  int param_formats[],
292  unsigned int param_length,
293  void *scratch[],
294  unsigned int scratch_length)
295 {
296  const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
297  char *buf;
298  size_t buf_size;
299 
300  GNUNET_break(NULL == cls);
301  if (1 != param_length)
302  return -1;
304  &buf);
305  scratch[0] = buf;
306  param_values[0] = (void *)buf;
307  param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
308  param_formats[0] = 1;
309  return 1;
310 }
311 
312 
322 {
323  struct GNUNET_PQ_QueryParam res =
324  { &qconv_rsa_public_key, NULL, (x), 0, 1 };
325 
326  return res;
327 }
328 
329 
344 static int
346  const void *data,
347  size_t data_len,
348  void *param_values[],
349  int param_lengths[],
350  int param_formats[],
351  unsigned int param_length,
352  void *scratch[],
353  unsigned int scratch_length)
354 {
355  const struct GNUNET_CRYPTO_RsaSignature *sig = data;
356  char *buf;
357  size_t buf_size;
358 
359  GNUNET_break(NULL == cls);
360  if (1 != param_length)
361  return -1;
362  buf_size = GNUNET_CRYPTO_rsa_signature_encode(sig,
363  &buf);
364  scratch[0] = buf;
365  param_values[0] = (void *)buf;
366  param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
367  param_formats[0] = 1;
368  return 1;
369 }
370 
371 
381 {
382  struct GNUNET_PQ_QueryParam res =
383  { &qconv_rsa_signature, NULL, (x), 0, 1 };
384 
385  return res;
386 }
387 
388 
403 static int
404 qconv_abs_time(void *cls,
405  const void *data,
406  size_t data_len,
407  void *param_values[],
408  int param_lengths[],
409  int param_formats[],
410  unsigned int param_length,
411  void *scratch[],
412  unsigned int scratch_length)
413 {
414  const struct GNUNET_TIME_Absolute *u = data;
415  struct GNUNET_TIME_Absolute abs;
416  uint64_t *u_nbo;
417 
418  GNUNET_break(NULL == cls);
419  if (1 != param_length)
420  return -1;
421  abs = *u;
422  if (abs.abs_value_us > INT64_MAX)
423  abs.abs_value_us = INT64_MAX;
424  u_nbo = GNUNET_new(uint64_t);
425  scratch[0] = u_nbo;
426  *u_nbo = GNUNET_htonll(abs.abs_value_us);
427  param_values[0] = (void *)u_nbo;
428  param_lengths[0] = sizeof(uint64_t);
429  param_formats[0] = 1;
430  return 1;
431 }
432 
433 
443 {
444  struct GNUNET_PQ_QueryParam res =
445  { &qconv_abs_time, NULL, x, sizeof(*x), 1 };
446 
447  return res;
448 }
449 
450 
459 {
460  return GNUNET_PQ_query_param_auto_from_type(&x->abs_value_us__);
461 }
462 
463 
464 /* end of pq_query_helper.c */
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint64_t in host byte order.
an RSA signature
Definition: crypto_rsa.c:61
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
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
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
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.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
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...
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.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *x)
Generate query parameter for an RSA signature.
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.
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.
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.
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.
#define GNUNET_PQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
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 char buf[2048]
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
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.
static int res
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint16(const uint16_t *x)
Generate query parameter for an uint16_t in host byte order.
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:35
helper functions for Postgres DB interactions
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_string(const char *ptr)
Generate query parameter for a string.
Time for absolute times used by GNUnet, in microseconds.
uint32_t data
The data 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_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.