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  return res;
81 }
82 
83 
91 {
92  return GNUNET_PQ_query_param_fixed_size (ptr, strlen (ptr));
93 }
94 
95 
110 static int
111 qconv_uint16 (void *cls,
112  const void *data,
113  size_t data_len,
114  void *param_values[],
115  int param_lengths[],
116  int param_formats[],
117  unsigned int param_length,
118  void *scratch[],
119  unsigned int scratch_length)
120 {
121  const uint16_t *u_hbo = data;
122  uint16_t *u_nbo;
123 
124  (void) scratch;
125  (void) scratch_length;
126  GNUNET_break (NULL == cls);
127  if (1 != param_length)
128  return -1;
129  u_nbo = GNUNET_new (uint16_t);
130  scratch[0] = u_nbo;
131  *u_nbo = htons (*u_hbo);
132  param_values[0] = (void *) u_nbo;
133  param_lengths[0] = sizeof (uint16_t);
134  param_formats[0] = 1;
135  return 1;
136 }
137 
138 
145 GNUNET_PQ_query_param_uint16 (const uint16_t *x)
146 {
147  struct GNUNET_PQ_QueryParam res =
148  { &qconv_uint16, NULL, x, sizeof (*x), 1 };
149  return res;
150 }
151 
152 
167 static int
168 qconv_uint32 (void *cls,
169  const void *data,
170  size_t data_len,
171  void *param_values[],
172  int param_lengths[],
173  int param_formats[],
174  unsigned int param_length,
175  void *scratch[],
176  unsigned int scratch_length)
177 {
178  const uint32_t *u_hbo = data;
179  uint32_t *u_nbo;
180 
181  (void) scratch;
182  (void) scratch_length;
183  GNUNET_break (NULL == cls);
184  if (1 != param_length)
185  return -1;
186  u_nbo = GNUNET_new (uint32_t);
187  scratch[0] = u_nbo;
188  *u_nbo = htonl (*u_hbo);
189  param_values[0] = (void *) u_nbo;
190  param_lengths[0] = sizeof (uint32_t);
191  param_formats[0] = 1;
192  return 1;
193 }
194 
195 
202 GNUNET_PQ_query_param_uint32 (const uint32_t *x)
203 {
204  struct GNUNET_PQ_QueryParam res =
205  { &qconv_uint32, NULL, x, sizeof (*x), 1 };
206  return res;
207 }
208 
209 
224 static int
225 qconv_uint64 (void *cls,
226  const void *data,
227  size_t data_len,
228  void *param_values[],
229  int param_lengths[],
230  int param_formats[],
231  unsigned int param_length,
232  void *scratch[],
233  unsigned int scratch_length)
234 {
235  const uint64_t *u_hbo = data;
236  uint64_t *u_nbo;
237 
238  (void) scratch;
239  (void) scratch_length;
240  GNUNET_break (NULL == cls);
241  if (1 != param_length)
242  return -1;
243  u_nbo = GNUNET_new (uint64_t);
244  scratch[0] = u_nbo;
245  *u_nbo = GNUNET_htonll (*u_hbo);
246  param_values[0] = (void *) u_nbo;
247  param_lengths[0] = sizeof (uint64_t);
248  param_formats[0] = 1;
249  return 1;
250 }
251 
252 
259 GNUNET_PQ_query_param_uint64 (const uint64_t *x)
260 {
261  struct GNUNET_PQ_QueryParam res =
262  { &qconv_uint64, NULL, x, sizeof (*x), 1 };
263  return res;
264 }
265 
266 
281 static int
283  const void *data,
284  size_t data_len,
285  void *param_values[],
286  int param_lengths[],
287  int param_formats[],
288  unsigned int param_length,
289  void *scratch[],
290  unsigned int scratch_length)
291 {
292  const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
293  char *buf;
294  size_t buf_size;
295 
296  GNUNET_break (NULL == cls);
297  if (1 != param_length)
298  return -1;
299  buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
300  &buf);
301  scratch[0] = buf;
302  param_values[0] = (void *) buf;
303  param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
304  param_formats[0] = 1;
305  return 1;
306 }
307 
308 
318 {
319  struct GNUNET_PQ_QueryParam res =
320  { &qconv_rsa_public_key, NULL, (x), 0, 1 };
321  return res;
322 }
323 
324 
339 static int
341  const void *data,
342  size_t data_len,
343  void *param_values[],
344  int param_lengths[],
345  int param_formats[],
346  unsigned int param_length,
347  void *scratch[],
348  unsigned int scratch_length)
349 {
350  const struct GNUNET_CRYPTO_RsaSignature *sig = data;
351  char *buf;
352  size_t buf_size;
353 
354  GNUNET_break (NULL == cls);
355  if (1 != param_length)
356  return -1;
357  buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
358  &buf);
359  scratch[0] = buf;
360  param_values[0] = (void *) buf;
361  param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
362  param_formats[0] = 1;
363  return 1;
364 }
365 
366 
376 {
377  struct GNUNET_PQ_QueryParam res =
378  { &qconv_rsa_signature, NULL, (x), 0, 1 };
379  return res;
380 }
381 
382 
397 static int
398 qconv_abs_time (void *cls,
399  const void *data,
400  size_t data_len,
401  void *param_values[],
402  int param_lengths[],
403  int param_formats[],
404  unsigned int param_length,
405  void *scratch[],
406  unsigned int scratch_length)
407 {
408  const struct GNUNET_TIME_Absolute *u = data;
409  struct GNUNET_TIME_Absolute abs;
410  uint64_t *u_nbo;
411 
412  GNUNET_break (NULL == cls);
413  if (1 != param_length)
414  return -1;
415  abs = *u;
416  if (abs.abs_value_us > INT64_MAX)
417  abs.abs_value_us = INT64_MAX;
418  u_nbo = GNUNET_new (uint64_t);
419  scratch[0] = u_nbo;
420  *u_nbo = GNUNET_htonll (abs.abs_value_us);
421  param_values[0] = (void *) u_nbo;
422  param_lengths[0] = sizeof (uint64_t);
423  param_formats[0] = 1;
424  return 1;
425 }
426 
427 
437 {
438  struct GNUNET_PQ_QueryParam res =
439  { &qconv_abs_time, NULL, x, sizeof (*x), 1 };
440 
441  return res;
442 }
443 
444 
453 {
454  return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
455 }
456 
457 
458 /* 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:63
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:318
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:966
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:51
#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.