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,
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,
160 return GNUNET_SYSERR;
162 if (PQgetisnull (result,
167 return GNUNET_SYSERR;
170 /* if a field is null, continue but
171 * remember that we now return a different result */
172 len = PQgetlength (result,
175 if (*dst_size != len)
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,
244 return GNUNET_SYSERR;
246 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,
266 return GNUNET_SYSERR;
273 * Function called to clean up memory allocated
274 * by a #GNUNET_PQ_ResultConverter.
277 * @param rd result data to clean up
280 clean_rsa_public_key (void *cls,
283 struct GNUNET_CRYPTO_RsaPublicKey **pk = rd;
287 GNUNET_CRYPTO_rsa_public_key_free (*pk);
294 * RSA public key expected.
296 * @param name name of the field in the table
297 * @param[out] rsa where to store the result
298 * @return array entry for the result specification to use
300 struct GNUNET_PQ_ResultSpec
301 GNUNET_PQ_result_spec_rsa_public_key (const char *name,
302 struct GNUNET_CRYPTO_RsaPublicKey **rsa)
304 struct GNUNET_PQ_ResultSpec res =
305 { &extract_rsa_public_key,
306 &clean_rsa_public_key,
308 (void *) rsa, 0, name, NULL };
314 * Extract data from a Postgres database @a result at row @a row.
317 * @param result where to extract data from
318 * @param int row to extract data from
319 * @param fname name (or prefix) of the fields to extract from
320 * @param[in,out] dst_size where to store size of result, may be NULL
321 * @param[out] dst where to store the result
323 * #GNUNET_YES if all results could be extracted
324 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
327 extract_rsa_signature (void *cls,
334 struct GNUNET_CRYPTO_RsaSignature **sig = dst;
340 fnum = PQfnumber (result,
345 return GNUNET_SYSERR;
347 if (PQgetisnull (result,
352 return GNUNET_SYSERR;
354 /* if a field is null, continue but
355 * remember that we now return a different result */
356 len = PQgetlength (result,
359 res = PQgetvalue (result,
362 *sig = GNUNET_CRYPTO_rsa_signature_decode (res,
367 return GNUNET_SYSERR;
374 * Function called to clean up memory allocated
375 * by a #GNUNET_PQ_ResultConverter.
378 * @param rd result data to clean up
381 clean_rsa_signature (void *cls,
384 struct GNUNET_CRYPTO_RsaSignature **sig = rd;
388 GNUNET_CRYPTO_rsa_signature_free (*sig);
395 * RSA signature expected.
397 * @param name name of the field in the table
398 * @param[out] sig where to store the result;
399 * @return array entry for the result specification to use
401 struct GNUNET_PQ_ResultSpec
402 GNUNET_PQ_result_spec_rsa_signature (const char *name,
403 struct GNUNET_CRYPTO_RsaSignature **sig)
405 struct GNUNET_PQ_ResultSpec res =
406 { &extract_rsa_signature,
407 &clean_rsa_signature,
409 (void *) sig, 0, (name), NULL };
415 * Extract data from a Postgres database @a result at row @a row.
418 * @param result where to extract data from
419 * @param int row to extract data from
420 * @param fname name (or prefix) of the fields to extract from
421 * @param[in,out] dst_size where to store size of result, may be NULL
422 * @param[out] dst where to store the result
424 * #GNUNET_YES if all results could be extracted
425 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
428 extract_string (void *cls,
441 fnum = PQfnumber (result,
446 return GNUNET_SYSERR;
448 if (PQgetisnull (result,
453 return GNUNET_SYSERR;
455 /* if a field is null, continue but
456 * remember that we now return a different result */
457 len = PQgetlength (result,
460 res = PQgetvalue (result,
463 *str = GNUNET_strndup (res,
468 return GNUNET_SYSERR;
475 * Function called to clean up memory allocated
476 * by a #GNUNET_PQ_ResultConverter.
479 * @param rd result data to clean up
482 clean_string (void *cls,
496 * 0-terminated string expected.
498 * @param name name of the field in the table
499 * @param[out] dst where to store the result, allocated
500 * @return array entry for the result specification to use
502 struct GNUNET_PQ_ResultSpec
503 GNUNET_PQ_result_spec_string (const char *name,
506 struct GNUNET_PQ_ResultSpec res =
510 (void *) dst, 0, (name), NULL };
516 * Absolute time expected.
518 * @param name name of the field in the table
519 * @param[out] at where to store the result
520 * @return array entry for the result specification to use
522 struct GNUNET_PQ_ResultSpec
523 GNUNET_PQ_result_spec_absolute_time (const char *name,
524 struct GNUNET_TIME_Absolute *at)
526 return GNUNET_PQ_result_spec_uint64 (name,
532 * Absolute time in network byte order expected.
534 * @param name name of the field in the table
535 * @param[out] at where to store the result
536 * @return array entry for the result specification to use
538 struct GNUNET_PQ_ResultSpec
539 GNUNET_PQ_result_spec_absolute_time_nbo (const char *name,
540 struct GNUNET_TIME_AbsoluteNBO *at)
542 struct GNUNET_PQ_ResultSpec res =
543 GNUNET_PQ_result_spec_auto_from_type(name, &at->abs_value_us__);
549 * Extract data from a Postgres database @a result at row @a row.
552 * @param result where to extract data from
553 * @param int row to extract data from
554 * @param fname name (or prefix) of the fields to extract from
555 * @param[in,out] dst_size where to store size of result, may be NULL
556 * @param[out] dst where to store the result
558 * #GNUNET_YES if all results could be extracted
559 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
562 extract_uint16 (void *cls,
569 uint16_t *udst = dst;
573 fnum = PQfnumber (result,
578 return GNUNET_SYSERR;
580 if (PQgetisnull (result,
585 return GNUNET_SYSERR;
587 GNUNET_assert (NULL != dst);
588 if (sizeof (uint16_t) != *dst_size)
591 return GNUNET_SYSERR;
593 res = (uint16_t *) PQgetvalue (result,
596 *udst = ntohs (*res);
604 * @param name name of the field in the table
605 * @param[out] u16 where to store the result
606 * @return array entry for the result specification to use
608 struct GNUNET_PQ_ResultSpec
609 GNUNET_PQ_result_spec_uint16 (const char *name,
612 struct GNUNET_PQ_ResultSpec res =
616 (void *) u16, sizeof (*u16), (name), NULL };
622 * Extract data from a Postgres database @a result at row @a row.
625 * @param result where to extract data from
626 * @param int row to extract data from
627 * @param fname name (or prefix) of the fields to extract from
628 * @param[in,out] dst_size where to store size of result, may be NULL
629 * @param[out] dst where to store the result
631 * #GNUNET_YES if all results could be extracted
632 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
635 extract_uint32 (void *cls,
642 uint32_t *udst = dst;
646 fnum = PQfnumber (result,
651 return GNUNET_SYSERR;
653 if (PQgetisnull (result,
658 return GNUNET_SYSERR;
660 GNUNET_assert (NULL != dst);
661 if (sizeof (uint32_t) != *dst_size)
664 return GNUNET_SYSERR;
666 res = (uint32_t *) PQgetvalue (result,
669 *udst = ntohl (*res);
677 * @param name name of the field in the table
678 * @param[out] u32 where to store the result
679 * @return array entry for the result specification to use
681 struct GNUNET_PQ_ResultSpec
682 GNUNET_PQ_result_spec_uint32 (const char *name,
685 struct GNUNET_PQ_ResultSpec res =
689 (void *) u32, sizeof (*u32), (name), NULL };
695 * Extract data from a Postgres database @a result at row @a row.
698 * @param result where to extract data from
699 * @param int row to extract data from
700 * @param fname name (or prefix) of the fields to extract from
701 * @param[in,out] dst_size where to store size of result, may be NULL
702 * @param[out] dst where to store the result
704 * #GNUNET_YES if all results could be extracted
705 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
708 extract_uint64 (void *cls,
715 uint64_t *udst = dst;
719 fnum = PQfnumber (result,
724 return GNUNET_SYSERR;
726 if (PQgetisnull (result,
731 return GNUNET_SYSERR;
733 GNUNET_assert (NULL != dst);
734 if (sizeof (uint64_t) != *dst_size)
737 return GNUNET_SYSERR;
739 res = (uint64_t *) PQgetvalue (result,
742 *udst = GNUNET_ntohll (*res);
750 * @param name name of the field in the table
751 * @param[out] u64 where to store the result
752 * @return array entry for the result specification to use
754 struct GNUNET_PQ_ResultSpec
755 GNUNET_PQ_result_spec_uint64 (const char *name,
758 struct GNUNET_PQ_ResultSpec res =
762 (void *) u64, sizeof (*u64), (name), NULL };
767 /* end of pq_result_helper.c */