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,
80 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
81 "Field `%s' does not exist in result\n",
85 if (PQgetisnull (result,
90 /* if a field is null, continue but
91 * remember that we now return a different result */
92 len = PQgetlength (result,
95 res = PQgetvalue (result,
98 GNUNET_assert (NULL != res);
100 idst = GNUNET_malloc (len);
101 *((void **) dst) = idst;
110 * Variable-size result expected.
112 * @param name name of the field in the table
113 * @param[out] dst where to store the result, allocated
114 * @param[out] sptr where to store the size of @a dst
115 * @return array entry for the result specification to use
117 struct GNUNET_PQ_ResultSpec
118 GNUNET_PQ_result_spec_variable_size (const char *name,
122 struct GNUNET_PQ_ResultSpec res =
123 { &extract_varsize_blob,
124 &clean_varsize_blob, NULL,
125 (void *) (dst), 0, name, sptr };
131 * Extract data from a Postgres database @a result at row @a row.
134 * @param result where to extract data from
135 * @param int row to extract data from
136 * @param fname name (or prefix) of the fields to extract from
137 * @param[in] dst_size desired size, never NULL
138 * @param[out] dst where to store the result
140 * #GNUNET_YES if all results could be extracted
141 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
144 extract_fixed_blob (void *cls,
155 fnum = PQfnumber (result,
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160 "Field `%s' does not exist in result\n",
162 return GNUNET_SYSERR;
164 if (PQgetisnull (result,
167 return GNUNET_SYSERR;
169 /* if a field is null, continue but
170 * remember that we now return a different result */
171 len = PQgetlength (result,
174 if (*dst_size != len)
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
177 "Field `%s' has wrong size (got %u, expected %u)\n",
180 (unsigned int) *dst_size);
181 return GNUNET_SYSERR;
183 res = PQgetvalue (result,
186 GNUNET_assert (NULL != res);
195 * Fixed-size result expected.
197 * @param name name of the field in the table
198 * @param[out] dst where to store the result
199 * @param dst_size number of bytes in @a dst
200 * @return array entry for the result specification to use
202 struct GNUNET_PQ_ResultSpec
203 GNUNET_PQ_result_spec_fixed_size (const char *name,
207 struct GNUNET_PQ_ResultSpec res =
208 { &extract_fixed_blob,
210 (dst), dst_size, name, NULL };
216 * Extract data from a Postgres database @a result at row @a row.
219 * @param result where to extract data from
220 * @param int row to extract data from
221 * @param fname name (or prefix) of the fields to extract from
222 * @param[in,out] dst_size where to store size of result, may be NULL
223 * @param[out] dst where to store the result
225 * #GNUNET_YES if all results could be extracted
226 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
229 extract_rsa_public_key (void *cls,
236 struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
242 fnum = PQfnumber (result,
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
247 "Field `%s' does not exist in result\n",
249 return GNUNET_SYSERR;
251 if (PQgetisnull (result,
254 return GNUNET_SYSERR;
256 /* if a field is null, continue but
257 * remember that we now return a different result */
258 len = PQgetlength (result,
261 res = PQgetvalue (result,
264 *pk = GNUNET_CRYPTO_rsa_public_key_decode (res,
268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
269 "Field `%s' contains bogus value (fails to decode)\n",
271 return GNUNET_SYSERR;
278 * Function called to clean up memory allocated
279 * by a #GNUNET_PQ_ResultConverter.
282 * @param rd result data to clean up
285 clean_rsa_public_key (void *cls,
288 struct GNUNET_CRYPTO_RsaPublicKey **pk = rd;
292 GNUNET_CRYPTO_rsa_public_key_free (*pk);
299 * RSA public key expected.
301 * @param name name of the field in the table
302 * @param[out] rsa where to store the result
303 * @return array entry for the result specification to use
305 struct GNUNET_PQ_ResultSpec
306 GNUNET_PQ_result_spec_rsa_public_key (const char *name,
307 struct GNUNET_CRYPTO_RsaPublicKey **rsa)
309 struct GNUNET_PQ_ResultSpec res =
310 { &extract_rsa_public_key,
311 &clean_rsa_public_key,
313 (void *) rsa, 0, name, NULL };
319 * Extract data from a Postgres database @a result at row @a row.
322 * @param result where to extract data from
323 * @param int row to extract data from
324 * @param fname name (or prefix) of the fields to extract from
325 * @param[in,out] dst_size where to store size of result, may be NULL
326 * @param[out] dst where to store the result
328 * #GNUNET_YES if all results could be extracted
329 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
332 extract_rsa_signature (void *cls,
339 struct GNUNET_CRYPTO_RsaSignature **sig = dst;
345 fnum = PQfnumber (result,
349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
350 "Field `%s' does not exist in result\n",
352 return GNUNET_SYSERR;
354 if (PQgetisnull (result,
357 return GNUNET_SYSERR;
359 /* if a field is null, continue but
360 * remember that we now return a different result */
361 len = PQgetlength (result,
364 res = PQgetvalue (result,
367 *sig = GNUNET_CRYPTO_rsa_signature_decode (res,
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372 "Field `%s' contains bogus value (fails to decode)\n",
374 return GNUNET_SYSERR;
381 * Function called to clean up memory allocated
382 * by a #GNUNET_PQ_ResultConverter.
385 * @param rd result data to clean up
388 clean_rsa_signature (void *cls,
391 struct GNUNET_CRYPTO_RsaSignature **sig = rd;
395 GNUNET_CRYPTO_rsa_signature_free (*sig);
402 * RSA signature expected.
404 * @param name name of the field in the table
405 * @param[out] sig where to store the result;
406 * @return array entry for the result specification to use
408 struct GNUNET_PQ_ResultSpec
409 GNUNET_PQ_result_spec_rsa_signature (const char *name,
410 struct GNUNET_CRYPTO_RsaSignature **sig)
412 struct GNUNET_PQ_ResultSpec res =
413 { &extract_rsa_signature,
414 &clean_rsa_signature,
416 (void *) sig, 0, (name), NULL };
422 * Extract data from a Postgres database @a result at row @a row.
425 * @param result where to extract data from
426 * @param int row to extract data from
427 * @param fname name (or prefix) of the fields to extract from
428 * @param[in,out] dst_size where to store size of result, may be NULL
429 * @param[out] dst where to store the result
431 * #GNUNET_YES if all results could be extracted
432 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
435 extract_string (void *cls,
448 fnum = PQfnumber (result,
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
453 "Field `%s' does not exist in result\n",
455 return GNUNET_SYSERR;
457 if (PQgetisnull (result,
460 return GNUNET_SYSERR;
462 /* if a field is null, continue but
463 * remember that we now return a different result */
464 len = PQgetlength (result,
467 res = PQgetvalue (result,
470 *str = GNUNET_strndup (res,
474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
475 "Field `%s' contains bogus value (fails to decode)\n",
477 return GNUNET_SYSERR;
484 * Function called to clean up memory allocated
485 * by a #GNUNET_PQ_ResultConverter.
488 * @param rd result data to clean up
491 clean_string (void *cls,
505 * 0-terminated string expected.
507 * @param name name of the field in the table
508 * @param[out] dst where to store the result, allocated
509 * @return array entry for the result specification to use
511 struct GNUNET_PQ_ResultSpec
512 GNUNET_PQ_result_spec_string (const char *name,
515 struct GNUNET_PQ_ResultSpec res =
519 (void *) dst, 0, (name), NULL };
525 * Absolute time expected.
527 * @param name name of the field in the table
528 * @param[out] at where to store the result
529 * @return array entry for the result specification to use
531 struct GNUNET_PQ_ResultSpec
532 GNUNET_PQ_result_spec_absolute_time (const char *name,
533 struct GNUNET_TIME_Absolute *at)
535 return GNUNET_PQ_result_spec_uint64 (name,
541 * Absolute time in network byte order expected.
543 * @param name name of the field in the table
544 * @param[out] at where to store the result
545 * @return array entry for the result specification to use
547 struct GNUNET_PQ_ResultSpec
548 GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
549 struct GNUNET_TIME_AbsoluteNBO *at)
551 struct GNUNET_PQ_ResultSpec res =
552 GNUNET_PQ_result_spec_auto_from_type(name, &at->abs_value_us__);
558 * Extract data from a Postgres database @a result at row @a row.
561 * @param result where to extract data from
562 * @param int row to extract data from
563 * @param fname name (or prefix) of the fields to extract from
564 * @param[in,out] dst_size where to store size of result, may be NULL
565 * @param[out] dst where to store the result
567 * #GNUNET_YES if all results could be extracted
568 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
571 extract_uint16 (void *cls,
578 uint16_t *udst = dst;
582 fnum = PQfnumber (result,
586 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
587 "Field `%s' does not exist in result\n",
589 return GNUNET_SYSERR;
591 if (PQgetisnull (result,
594 return GNUNET_SYSERR;
595 GNUNET_assert (NULL != dst);
596 if (sizeof (uint16_t) != *dst_size)
599 return GNUNET_SYSERR;
601 res = (uint16_t *) PQgetvalue (result,
604 *udst = ntohs (*res);
612 * @param name name of the field in the table
613 * @param[out] u16 where to store the result
614 * @return array entry for the result specification to use
616 struct GNUNET_PQ_ResultSpec
617 GNUNET_PQ_result_spec_uint16 (const char *name,
620 struct GNUNET_PQ_ResultSpec res =
624 (void *) u16, sizeof (*u16), (name), NULL };
630 * Extract data from a Postgres database @a result at row @a row.
633 * @param result where to extract data from
634 * @param int row to extract data from
635 * @param fname name (or prefix) of the fields to extract from
636 * @param[in,out] dst_size where to store size of result, may be NULL
637 * @param[out] dst where to store the result
639 * #GNUNET_YES if all results could be extracted
640 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
643 extract_uint32 (void *cls,
650 uint32_t *udst = dst;
654 fnum = PQfnumber (result,
658 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
659 "Field `%s' does not exist in result\n",
661 return GNUNET_SYSERR;
663 if (PQgetisnull (result,
666 return GNUNET_SYSERR;
667 GNUNET_assert (NULL != dst);
668 if (sizeof (uint32_t) != *dst_size)
671 return GNUNET_SYSERR;
673 res = (uint32_t *) PQgetvalue (result,
676 *udst = ntohl (*res);
684 * @param name name of the field in the table
685 * @param[out] u32 where to store the result
686 * @return array entry for the result specification to use
688 struct GNUNET_PQ_ResultSpec
689 GNUNET_PQ_result_spec_uint32 (const char *name,
692 struct GNUNET_PQ_ResultSpec res =
696 (void *) u32, sizeof (*u32), (name), NULL };
702 * Extract data from a Postgres database @a result at row @a row.
705 * @param result where to extract data from
706 * @param int row to extract data from
707 * @param fname name (or prefix) of the fields to extract from
708 * @param[in,out] dst_size where to store size of result, may be NULL
709 * @param[out] dst where to store the result
711 * #GNUNET_YES if all results could be extracted
712 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
715 extract_uint64 (void *cls,
722 uint64_t *udst = dst;
726 fnum = PQfnumber (result,
730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
731 "Field `%s' does not exist in result\n",
733 return GNUNET_SYSERR;
735 if (PQgetisnull (result,
738 return GNUNET_SYSERR;
739 GNUNET_assert (NULL != dst);
740 if (sizeof (uint64_t) != *dst_size)
743 return GNUNET_SYSERR;
745 res = (uint64_t *) PQgetvalue (result,
748 *udst = GNUNET_ntohll (*res);
756 * @param name name of the field in the table
757 * @param[out] u64 where to store the result
758 * @return array entry for the result specification to use
760 struct GNUNET_PQ_ResultSpec
761 GNUNET_PQ_result_spec_uint64 (const char *name,
764 struct GNUNET_PQ_ResultSpec res =
768 (void *) u64, sizeof (*u64), (name), NULL };
773 /* end of pq_result_helper.c */