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,
73 fnum = PQfnumber (result,
77 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
78 "Field `%s' does not exist in result\n",
82 if (PQgetisnull (result,
87 /* if a field is null, continue but
88 * remember that we now return a different result */
89 len = PQgetlength (result,
92 res = PQgetvalue (result,
95 GNUNET_assert (NULL != res);
97 idst = GNUNET_malloc (len);
98 *((void **) dst) = idst;
107 * Variable-size result expected.
109 * @param name name of the field in the table
110 * @param[out] dst where to store the result, allocated
111 * @param[out] sptr where to store the size of @a dst
112 * @return array entry for the result specification to use
114 struct GNUNET_PQ_ResultSpec
115 GNUNET_PQ_result_spec_variable_size (const char *name,
119 struct GNUNET_PQ_ResultSpec res =
120 { &extract_varsize_blob,
121 &clean_varsize_blob, NULL,
122 (void *) (dst), 0, name, sptr };
128 * Extract data from a Postgres database @a result at row @a row.
131 * @param result where to extract data from
132 * @param int row to extract data from
133 * @param fname name (or prefix) of the fields to extract from
134 * @param[in] dst_size desired size, never NULL
135 * @param[out] dst where to store the result
137 * #GNUNET_YES if all results could be extracted
138 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
141 extract_fixed_blob (void *cls,
152 fnum = PQfnumber (result,
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
157 "Field `%s' does not exist in result\n",
159 return GNUNET_SYSERR;
161 if (PQgetisnull (result,
164 return GNUNET_SYSERR;
166 /* if a field is null, continue but
167 * remember that we now return a different result */
168 len = PQgetlength (result,
171 if (*dst_size != len)
173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
174 "Field `%s' has wrong size (got %u, expected %u)\n",
177 (unsigned int) *dst_size);
178 return GNUNET_SYSERR;
180 res = PQgetvalue (result,
183 GNUNET_assert (NULL != res);
192 * Fixed-size result expected.
194 * @param name name of the field in the table
195 * @param[out] dst where to store the result
196 * @param dst_size number of bytes in @a dst
197 * @return array entry for the result specification to use
199 struct GNUNET_PQ_ResultSpec
200 GNUNET_PQ_result_spec_fixed_size (const char *name,
204 struct GNUNET_PQ_ResultSpec res =
205 { &extract_fixed_blob,
207 (dst), dst_size, name, NULL };
213 * Extract data from a Postgres database @a result at row @a row.
216 * @param result where to extract data from
217 * @param int row to extract data from
218 * @param fname name (or prefix) of the fields to extract from
219 * @param[in,out] dst_size where to store size of result, may be NULL
220 * @param[out] dst where to store the result
222 * #GNUNET_YES if all results could be extracted
223 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
226 extract_rsa_public_key (void *cls,
233 struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
239 fnum = PQfnumber (result,
243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
244 "Field `%s' does not exist in result\n",
246 return GNUNET_SYSERR;
248 if (PQgetisnull (result,
251 return GNUNET_SYSERR;
253 /* if a field is null, continue but
254 * remember that we now return a different result */
255 len = PQgetlength (result,
258 res = PQgetvalue (result,
261 *pk = GNUNET_CRYPTO_rsa_public_key_decode (res,
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
266 "Field `%s' contains bogus value (fails to decode)\n",
268 return GNUNET_SYSERR;
275 * Function called to clean up memory allocated
276 * by a #GNUNET_PQ_ResultConverter.
279 * @param rd result data to clean up
282 clean_rsa_public_key (void *cls,
285 struct GNUNET_CRYPTO_RsaPublicKey **pk = rd;
289 GNUNET_CRYPTO_rsa_public_key_free (*pk);
296 * RSA public key expected.
298 * @param name name of the field in the table
299 * @param[out] rsa where to store the result
300 * @return array entry for the result specification to use
302 struct GNUNET_PQ_ResultSpec
303 GNUNET_PQ_result_spec_rsa_public_key (const char *name,
304 struct GNUNET_CRYPTO_RsaPublicKey **rsa)
306 struct GNUNET_PQ_ResultSpec res =
307 { &extract_rsa_public_key,
308 &clean_rsa_public_key,
310 (void *) rsa, 0, name, NULL };
316 * Extract data from a Postgres database @a result at row @a row.
319 * @param result where to extract data from
320 * @param int row to extract data from
321 * @param fname name (or prefix) of the fields to extract from
322 * @param[in,out] dst_size where to store size of result, may be NULL
323 * @param[out] dst where to store the result
325 * #GNUNET_YES if all results could be extracted
326 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
329 extract_rsa_signature (void *cls,
336 struct GNUNET_CRYPTO_RsaSignature **sig = dst;
342 fnum = PQfnumber (result,
346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
347 "Field `%s' does not exist in result\n",
349 return GNUNET_SYSERR;
351 if (PQgetisnull (result,
354 return GNUNET_SYSERR;
356 /* if a field is null, continue but
357 * remember that we now return a different result */
358 len = PQgetlength (result,
361 res = PQgetvalue (result,
364 *sig = GNUNET_CRYPTO_rsa_signature_decode (res,
368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
369 "Field `%s' contains bogus value (fails to decode)\n",
371 return GNUNET_SYSERR;
378 * Function called to clean up memory allocated
379 * by a #GNUNET_PQ_ResultConverter.
382 * @param rd result data to clean up
385 clean_rsa_signature (void *cls,
388 struct GNUNET_CRYPTO_RsaSignature **sig = rd;
392 GNUNET_CRYPTO_rsa_signature_free (*sig);
399 * RSA signature expected.
401 * @param name name of the field in the table
402 * @param[out] sig where to store the result;
403 * @return array entry for the result specification to use
405 struct GNUNET_PQ_ResultSpec
406 GNUNET_PQ_result_spec_rsa_signature (const char *name,
407 struct GNUNET_CRYPTO_RsaSignature **sig)
409 struct GNUNET_PQ_ResultSpec res =
410 { &extract_rsa_signature,
411 &clean_rsa_signature,
413 (void *) sig, 0, (name), NULL };
419 * Extract data from a Postgres database @a result at row @a row.
422 * @param result where to extract data from
423 * @param int row to extract data from
424 * @param fname name (or prefix) of the fields to extract from
425 * @param[in,out] dst_size where to store size of result, may be NULL
426 * @param[out] dst where to store the result
428 * #GNUNET_YES if all results could be extracted
429 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
432 extract_string (void *cls,
445 fnum = PQfnumber (result,
449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
450 "Field `%s' does not exist in result\n",
452 return GNUNET_SYSERR;
454 if (PQgetisnull (result,
457 return GNUNET_SYSERR;
459 /* if a field is null, continue but
460 * remember that we now return a different result */
461 len = PQgetlength (result,
464 res = PQgetvalue (result,
467 *str = GNUNET_strndup (res,
471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
472 "Field `%s' contains bogus value (fails to decode)\n",
474 return GNUNET_SYSERR;
481 * Function called to clean up memory allocated
482 * by a #GNUNET_PQ_ResultConverter.
485 * @param rd result data to clean up
488 clean_string (void *cls,
502 * 0-terminated string expected.
504 * @param name name of the field in the table
505 * @param[out] dst where to store the result, allocated
506 * @return array entry for the result specification to use
508 struct GNUNET_PQ_ResultSpec
509 GNUNET_PQ_result_spec_string (const char *name,
512 struct GNUNET_PQ_ResultSpec res =
516 (void *) dst, 0, (name), NULL };
522 * Absolute time expected.
524 * @param name name of the field in the table
525 * @param[out] at where to store the result
526 * @return array entry for the result specification to use
528 struct GNUNET_PQ_ResultSpec
529 GNUNET_PQ_result_spec_absolute_time (const char *name,
530 struct GNUNET_TIME_Absolute *at)
532 return GNUNET_PQ_result_spec_uint64 (name,
538 * Absolute time in network byte order expected.
540 * @param name name of the field in the table
541 * @param[out] at where to store the result
542 * @return array entry for the result specification to use
544 struct GNUNET_PQ_ResultSpec
545 GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
546 struct GNUNET_TIME_AbsoluteNBO *at)
548 struct GNUNET_PQ_ResultSpec res =
549 GNUNET_PQ_result_spec_auto_from_type(name, &at->abs_value_us__);
555 * Extract data from a Postgres database @a result at row @a row.
558 * @param result where to extract data from
559 * @param int row to extract data from
560 * @param fname name (or prefix) of the fields to extract from
561 * @param[in,out] dst_size where to store size of result, may be NULL
562 * @param[out] dst where to store the result
564 * #GNUNET_YES if all results could be extracted
565 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
568 extract_uint16 (void *cls,
575 uint16_t *udst = dst;
579 fnum = PQfnumber (result,
583 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
584 "Field `%s' does not exist in result\n",
586 return GNUNET_SYSERR;
588 if (PQgetisnull (result,
591 return GNUNET_SYSERR;
592 GNUNET_assert (NULL != dst);
593 if (sizeof (uint16_t) != *dst_size)
596 return GNUNET_SYSERR;
598 res = (uint16_t *) PQgetvalue (result,
601 *udst = ntohs (*res);
609 * @param name name of the field in the table
610 * @param[out] u16 where to store the result
611 * @return array entry for the result specification to use
613 struct GNUNET_PQ_ResultSpec
614 GNUNET_PQ_result_spec_uint16 (const char *name,
617 struct GNUNET_PQ_ResultSpec res =
621 (void *) u16, sizeof (*u16), (name), NULL };
627 * Extract data from a Postgres database @a result at row @a row.
630 * @param result where to extract data from
631 * @param int row to extract data from
632 * @param fname name (or prefix) of the fields to extract from
633 * @param[in,out] dst_size where to store size of result, may be NULL
634 * @param[out] dst where to store the result
636 * #GNUNET_YES if all results could be extracted
637 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
640 extract_uint32 (void *cls,
647 uint32_t *udst = dst;
651 fnum = PQfnumber (result,
655 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
656 "Field `%s' does not exist in result\n",
658 return GNUNET_SYSERR;
660 if (PQgetisnull (result,
663 return GNUNET_SYSERR;
664 GNUNET_assert (NULL != dst);
665 if (sizeof (uint32_t) != *dst_size)
668 return GNUNET_SYSERR;
670 res = (uint32_t *) PQgetvalue (result,
673 *udst = ntohl (*res);
681 * @param name name of the field in the table
682 * @param[out] u32 where to store the result
683 * @return array entry for the result specification to use
685 struct GNUNET_PQ_ResultSpec
686 GNUNET_PQ_result_spec_uint32 (const char *name,
689 struct GNUNET_PQ_ResultSpec res =
693 (void *) u32, sizeof (*u32), (name), NULL };
699 * Extract data from a Postgres database @a result at row @a row.
702 * @param result where to extract data from
703 * @param int row to extract data from
704 * @param fname name (or prefix) of the fields to extract from
705 * @param[in,out] dst_size where to store size of result, may be NULL
706 * @param[out] dst where to store the result
708 * #GNUNET_YES if all results could be extracted
709 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
712 extract_uint64 (void *cls,
719 uint64_t *udst = dst;
723 fnum = PQfnumber (result,
727 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
728 "Field `%s' does not exist in result\n",
730 return GNUNET_SYSERR;
732 if (PQgetisnull (result,
735 return GNUNET_SYSERR;
736 GNUNET_assert (NULL != dst);
737 if (sizeof (uint64_t) != *dst_size)
740 return GNUNET_SYSERR;
742 res = (uint64_t *) PQgetvalue (result,
745 *udst = GNUNET_ntohll (*res);
753 * @param name name of the field in the table
754 * @param[out] u64 where to store the result
755 * @return array entry for the result specification to use
757 struct GNUNET_PQ_ResultSpec
758 GNUNET_PQ_result_spec_uint64 (const char *name,
761 struct GNUNET_PQ_ResultSpec res =
765 (void *) u64, sizeof (*u64), (name), NULL };
770 /* end of pq_result_helper.c */