2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
17 * @file pq/pq_result_helper.c
18 * @brief functions to extract result values
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_pq_lib.h"
27 * Function called to clean up memory allocated
28 * by a #GNUNET_PQ_ResultConverter.
31 * @param rd result data to clean up
34 clean_varsize_blob (void *cls,
48 * Extract data from a Postgres database @a result at row @a row.
51 * @param result where to extract data from
52 * @param int row to extract data from
53 * @param fname name (or prefix) of the fields to extract from
54 * @param[in,out] dst_size where to store size of result, may be NULL
55 * @param[out] dst where to store the result
57 * #GNUNET_YES if all results could be extracted
58 * #GNUNET_SYSERR if a result was invalid (non-existing field)
61 extract_varsize_blob (void *cls,
74 *((void **) dst) = NULL;
76 fnum = PQfnumber (result,
83 if (PQgetisnull (result,
88 /* if a field is null, continue but
89 * remember that we now return a different result */
90 len = PQgetlength (result,
93 res = PQgetvalue (result,
96 GNUNET_assert (NULL != res);
98 idst = GNUNET_malloc (len);
99 *((void **) dst) = idst;
108 * Variable-size result expected.
110 * @param name name of the field in the table
111 * @param[out] dst where to store the result, allocated
112 * @param[out] sptr where to store the size of @a dst
113 * @return array entry for the result specification to use
115 struct GNUNET_PQ_ResultSpec
116 GNUNET_PQ_result_spec_variable_size (const char *name,
120 struct GNUNET_PQ_ResultSpec res =
121 { &extract_varsize_blob,
122 &clean_varsize_blob, NULL,
123 (void *) (dst), 0, name, sptr };
129 * Extract data from a Postgres database @a result at row @a row.
132 * @param result where to extract data from
133 * @param int row to extract data from
134 * @param fname name (or prefix) of the fields to extract from
135 * @param[in] dst_size desired size, never NULL
136 * @param[out] dst where to store the result
138 * #GNUNET_YES if all results could be extracted
139 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
142 extract_fixed_blob (void *cls,
153 fnum = PQfnumber (result,
158 return GNUNET_SYSERR;
160 if (PQgetisnull (result,
163 return GNUNET_SYSERR;
165 /* if a field is null, continue but
166 * remember that we now return a different result */
167 len = PQgetlength (result,
170 if (*dst_size != len)
173 return GNUNET_SYSERR;
175 res = PQgetvalue (result,
178 GNUNET_assert (NULL != res);
187 * Fixed-size result expected.
189 * @param name name of the field in the table
190 * @param[out] dst where to store the result
191 * @param dst_size number of bytes in @a dst
192 * @return array entry for the result specification to use
194 struct GNUNET_PQ_ResultSpec
195 GNUNET_PQ_result_spec_fixed_size (const char *name,
199 struct GNUNET_PQ_ResultSpec res =
200 { &extract_fixed_blob,
202 (dst), dst_size, name, NULL };
208 * Extract data from a Postgres database @a result at row @a row.
211 * @param result where to extract data from
212 * @param int row to extract data from
213 * @param fname name (or prefix) of the fields to extract from
214 * @param[in,out] dst_size where to store size of result, may be NULL
215 * @param[out] dst where to store the result
217 * #GNUNET_YES if all results could be extracted
218 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
221 extract_rsa_public_key (void *cls,
228 struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
234 fnum = PQfnumber (result,
239 return GNUNET_SYSERR;
241 if (PQgetisnull (result,
244 return GNUNET_SYSERR;
246 /* if a field is null, continue but
247 * remember that we now return a different result */
248 len = PQgetlength (result,
251 res = PQgetvalue (result,
254 *pk = GNUNET_CRYPTO_rsa_public_key_decode (res,
259 return GNUNET_SYSERR;
266 * Function called to clean up memory allocated
267 * by a #GNUNET_PQ_ResultConverter.
270 * @param rd result data to clean up
273 clean_rsa_public_key (void *cls,
276 struct GNUNET_CRYPTO_RsaPublicKey **pk = rd;
280 GNUNET_CRYPTO_rsa_public_key_free (*pk);
287 * RSA public key expected.
289 * @param name name of the field in the table
290 * @param[out] rsa where to store the result
291 * @return array entry for the result specification to use
293 struct GNUNET_PQ_ResultSpec
294 GNUNET_PQ_result_spec_rsa_public_key (const char *name,
295 struct GNUNET_CRYPTO_RsaPublicKey **rsa)
297 struct GNUNET_PQ_ResultSpec res =
298 { &extract_rsa_public_key,
299 &clean_rsa_public_key,
301 (void *) rsa, 0, name, NULL };
307 * Extract data from a Postgres database @a result at row @a row.
310 * @param result where to extract data from
311 * @param int row to extract data from
312 * @param fname name (or prefix) of the fields to extract from
313 * @param[in,out] dst_size where to store size of result, may be NULL
314 * @param[out] dst where to store the result
316 * #GNUNET_YES if all results could be extracted
317 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
320 extract_rsa_signature (void *cls,
327 struct GNUNET_CRYPTO_RsaSignature **sig = dst;
333 fnum = PQfnumber (result,
338 return GNUNET_SYSERR;
340 if (PQgetisnull (result,
343 return GNUNET_SYSERR;
345 /* if a field is null, continue but
346 * remember that we now return a different result */
347 len = PQgetlength (result,
350 res = PQgetvalue (result,
353 *sig = GNUNET_CRYPTO_rsa_signature_decode (res,
358 return GNUNET_SYSERR;
365 * Function called to clean up memory allocated
366 * by a #GNUNET_PQ_ResultConverter.
369 * @param rd result data to clean up
372 clean_rsa_signature (void *cls,
375 struct GNUNET_CRYPTO_RsaSignature **sig = rd;
379 GNUNET_CRYPTO_rsa_signature_free (*sig);
386 * RSA signature expected.
388 * @param name name of the field in the table
389 * @param[out] sig where to store the result;
390 * @return array entry for the result specification to use
392 struct GNUNET_PQ_ResultSpec
393 GNUNET_PQ_result_spec_rsa_signature (const char *name,
394 struct GNUNET_CRYPTO_RsaSignature **sig)
396 struct GNUNET_PQ_ResultSpec res =
397 { &extract_rsa_signature,
398 &clean_rsa_signature,
400 (void *) sig, 0, (name), NULL };
406 * Extract data from a Postgres database @a result at row @a row.
409 * @param result where to extract data from
410 * @param int row to extract data from
411 * @param fname name (or prefix) of the fields to extract from
412 * @param[in,out] dst_size where to store size of result, may be NULL
413 * @param[out] dst where to store the result
415 * #GNUNET_YES if all results could be extracted
416 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
419 extract_string (void *cls,
432 fnum = PQfnumber (result,
437 return GNUNET_SYSERR;
439 if (PQgetisnull (result,
442 return GNUNET_SYSERR;
444 /* if a field is null, continue but
445 * remember that we now return a different result */
446 len = PQgetlength (result,
449 res = PQgetvalue (result,
452 *str = GNUNET_strndup (res,
457 return GNUNET_SYSERR;
464 * Function called to clean up memory allocated
465 * by a #GNUNET_PQ_ResultConverter.
468 * @param rd result data to clean up
471 clean_string (void *cls,
485 * 0-terminated string expected.
487 * @param name name of the field in the table
488 * @param[out] dst where to store the result, allocated
489 * @return array entry for the result specification to use
491 struct GNUNET_PQ_ResultSpec
492 GNUNET_PQ_result_spec_string (const char *name,
495 struct GNUNET_PQ_ResultSpec res =
499 (void *) dst, 0, (name), NULL };
505 * Absolute time expected.
507 * @param name name of the field in the table
508 * @param[out] at where to store the result
509 * @return array entry for the result specification to use
511 struct GNUNET_PQ_ResultSpec
512 GNUNET_PQ_result_spec_absolute_time (const char *name,
513 struct GNUNET_TIME_Absolute *at)
515 return GNUNET_PQ_result_spec_uint64 (name,
521 * Absolute time in network byte order expected.
523 * @param name name of the field in the table
524 * @param[out] at where to store the result
525 * @return array entry for the result specification to use
527 struct GNUNET_PQ_ResultSpec
528 GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
529 struct GNUNET_TIME_AbsoluteNBO *at)
531 struct GNUNET_PQ_ResultSpec res =
532 GNUNET_PQ_result_spec_auto_from_type(name, &at->abs_value_us__);
538 * Extract data from a Postgres database @a result at row @a row.
541 * @param result where to extract data from
542 * @param int row to extract data from
543 * @param fname name (or prefix) of the fields to extract from
544 * @param[in,out] dst_size where to store size of result, may be NULL
545 * @param[out] dst where to store the result
547 * #GNUNET_YES if all results could be extracted
548 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
551 extract_uint16 (void *cls,
558 uint16_t *udst = dst;
562 fnum = PQfnumber (result,
567 return GNUNET_SYSERR;
569 if (PQgetisnull (result,
572 return GNUNET_SYSERR;
573 GNUNET_assert (NULL != dst);
574 if (sizeof (uint16_t) != *dst_size)
577 return GNUNET_SYSERR;
579 res = (uint16_t *) PQgetvalue (result,
582 *udst = ntohs (*res);
590 * @param name name of the field in the table
591 * @param[out] u16 where to store the result
592 * @return array entry for the result specification to use
594 struct GNUNET_PQ_ResultSpec
595 GNUNET_PQ_result_spec_uint16 (const char *name,
598 struct GNUNET_PQ_ResultSpec res =
602 (void *) u16, sizeof (*u16), (name), NULL };
608 * Extract data from a Postgres database @a result at row @a row.
611 * @param result where to extract data from
612 * @param int row to extract data from
613 * @param fname name (or prefix) of the fields to extract from
614 * @param[in,out] dst_size where to store size of result, may be NULL
615 * @param[out] dst where to store the result
617 * #GNUNET_YES if all results could be extracted
618 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
621 extract_uint32 (void *cls,
628 uint32_t *udst = dst;
632 fnum = PQfnumber (result,
637 return GNUNET_SYSERR;
639 if (PQgetisnull (result,
642 return GNUNET_SYSERR;
643 GNUNET_assert (NULL != dst);
644 if (sizeof (uint32_t) != *dst_size)
647 return GNUNET_SYSERR;
649 res = (uint32_t *) PQgetvalue (result,
652 *udst = ntohl (*res);
660 * @param name name of the field in the table
661 * @param[out] u32 where to store the result
662 * @return array entry for the result specification to use
664 struct GNUNET_PQ_ResultSpec
665 GNUNET_PQ_result_spec_uint32 (const char *name,
668 struct GNUNET_PQ_ResultSpec res =
672 (void *) u32, sizeof (*u32), (name), NULL };
678 * Extract data from a Postgres database @a result at row @a row.
681 * @param result where to extract data from
682 * @param int row to extract data from
683 * @param fname name (or prefix) of the fields to extract from
684 * @param[in,out] dst_size where to store size of result, may be NULL
685 * @param[out] dst where to store the result
687 * #GNUNET_YES if all results could be extracted
688 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
691 extract_uint64 (void *cls,
698 uint64_t *udst = dst;
702 fnum = PQfnumber (result,
707 return GNUNET_SYSERR;
709 if (PQgetisnull (result,
712 return GNUNET_SYSERR;
713 GNUNET_assert (NULL != dst);
714 if (sizeof (uint64_t) != *dst_size)
717 return GNUNET_SYSERR;
719 res = (uint64_t *) PQgetvalue (result,
722 *udst = GNUNET_ntohll (*res);
730 * @param name name of the field in the table
731 * @param[out] u64 where to store the result
732 * @return array entry for the result specification to use
734 struct GNUNET_PQ_ResultSpec
735 GNUNET_PQ_result_spec_uint64 (const char *name,
738 struct GNUNET_PQ_ResultSpec res =
742 (void *) u64, sizeof (*u64), (name), NULL };
747 /* end of pq_result_helper.c */