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 my/my_result_helper.c
18 * @brief functions to extract result values
19 * @author Christophe Genevey
23 #include "gnunet_util_lib.h"
24 #include "gnunet_my_lib.h"
27 * extract data from a Mysql database @a result at row @a row
30 * @param qp data about the query
31 * @param result mysql result
33 * #GNUNET_OK if all results could be extracted
34 * #GNUNET_SYSERR if a result was invalid
37 pre_extract_varsize_blob (void *cls,
38 struct GNUNET_MY_ResultSpec *rs,
43 results[0].buffer = NULL;
44 results[0].buffer_length = 0;
45 results[0].length = &rs->mysql_bind_output_length;
52 * extract data from a Mysql database @a result at row @a row
56 * @param stmt the mysql statement that is being run
57 * @param column the column that is being processed
60 * #GNUNET_OK if all results could be extracted
61 * #GNUNET_SYSERR if a result was invalid
64 post_extract_varsize_blob (void *cls,
65 struct GNUNET_MY_ResultSpec *rs,
73 size = (size_t) rs->mysql_bind_output_length;
75 if (rs->mysql_bind_output_length != size)
76 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
78 buf = GNUNET_malloc (size);
80 results[0].buffer = buf;
81 results[0].buffer_length = size;
82 results[0].buffer_type = MYSQL_TYPE_BLOB;
85 mysql_stmt_fetch_column (stmt,
94 *(void **) rs->dst = buf;
95 *rs->result_size = size;
102 * extract data from a Mysql database @a result at row @a row
108 cleanup_varsize_blob (void *cls,
109 struct GNUNET_MY_ResultSpec *rs)
111 void **ptr = (void **)rs->dst;
121 * Variable-size result expected
123 * @param[out] dst where to store the result, allocated
124 * @param[out] sptr where to store the size of @a dst
125 * @return array entru for the result specification to use
127 struct GNUNET_MY_ResultSpec
128 GNUNET_MY_result_spec_variable_size (void **dst,
131 struct GNUNET_MY_ResultSpec res =
133 .pre_conv = &pre_extract_varsize_blob,
134 .post_conv = &post_extract_varsize_blob,
135 .cleaner = &cleanup_varsize_blob,
136 .dst = (void *)(dst),
137 .result_size = ptr_size,
146 * Extract data from a Mysql database @a result at row @a row
149 * @param result where to extract data from
150 * @param int row to extract data from
151 * @param fname name (or prefix) of the fields to extract from
152 * @param[in] dst_size desired size, never NULL
153 * @param[out] dst where to store the result
155 * #GNUNET_OK if all results could be extracted
156 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
159 pre_extract_fixed_blob (void *cls,
160 struct GNUNET_MY_ResultSpec *rs,
165 results[0].buffer = rs->dst;
166 results[0].buffer_length = rs->dst_size;
167 results[0].length = &rs->mysql_bind_output_length;
168 results[0].buffer_type = MYSQL_TYPE_BLOB;
175 * Check size of extracted fixed size data from a Mysql database @a
176 * result at row @a row
179 * @param result where to extract data from
180 * @param int row to extract data from
181 * @param fname name (or prefix) of the fields to extract from
182 * @param[in] dst_size desired size, never NULL
183 * @param[out] dst where to store the result
185 * #GNUNET_OK if all results could be extracted
186 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
189 post_extract_fixed_blob (void *cls,
190 struct GNUNET_MY_ResultSpec *rs,
195 if (rs->dst_size != rs->mysql_bind_output_length)
196 return GNUNET_SYSERR;
202 * Fixed-size result expected.
204 * @param name name of the field in the table
205 * @param[out] dst where to store the result
206 * @param dst_size number of bytes in @a dst
207 * @return array entry for the result specification to use
209 struct GNUNET_MY_ResultSpec
210 GNUNET_MY_result_spec_fixed_size (void *ptr,
213 struct GNUNET_MY_ResultSpec res =
215 .pre_conv = &pre_extract_fixed_blob,
216 .post_conv = &post_extract_fixed_blob,
218 .dst = (void *)(ptr),
219 .dst_size = ptr_size,
228 * Extract data from a Mysql database @a result at row @a row
231 * @param result where to extract data from
232 * @param int row to extract data from
233 * @param fname name (or prefix) of the fields to extract from
234 * @param[in, out] dst_size where to store size of result, may be NULL
235 * @param[out] dst where to store the result
237 * #GNUNET_OK if all results could be extracted
238 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
241 pre_extract_rsa_public_key (void *cls,
242 struct GNUNET_MY_ResultSpec *rs,
247 results[0].buffer = NULL;
248 results[0].buffer_length = 0;
249 results[0].length = &rs->mysql_bind_output_length;
250 results[0].buffer_type = MYSQL_TYPE_BLOB;
257 * Check size of extracted fixed size data from a Mysql database @a
258 * result at row @a row
261 * @param result where to extract data from
262 * @param int row to extract data from
263 * @param fname name (or prefix) of the fields to extract from
264 * @param[in, out] dst_size where to store size of result, may be NULL
265 * @param[out] dst where to store the result
267 * #GNUNET_OK if all results could be extracted
268 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
271 post_extract_rsa_public_key (void *cls,
272 struct GNUNET_MY_ResultSpec *rs,
278 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
282 size = (size_t) rs->mysql_bind_output_length;
284 if (rs->mysql_bind_output_length != size)
285 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
286 buf = GNUNET_malloc (size);
288 results[0].buffer = buf;
289 results[0].buffer_length = size;
290 results[0].buffer_type = MYSQL_TYPE_BLOB;
292 mysql_stmt_fetch_column (stmt,
298 return GNUNET_SYSERR;
300 *pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
306 "Results contains bogus public key value (fail to decode)\n");
307 return GNUNET_SYSERR;
315 * Function called to clean up memory allocated
316 * by a #GNUNET_MY_ResultConverter.
319 * @param rd result data to clean up
322 clean_rsa_public_key (void *cls,
323 struct GNUNET_MY_ResultSpec *rs)
325 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
329 GNUNET_CRYPTO_rsa_public_key_free (*pk);
336 * RSA public key expected
338 * @param name name of the field in the table
339 * @param[out] rsa where to store the result
340 * @return array entry for the result specification to use
342 struct GNUNET_MY_ResultSpec
343 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
345 struct GNUNET_MY_ResultSpec res = {
346 .pre_conv = &pre_extract_rsa_public_key,
347 .post_conv = &post_extract_rsa_public_key,
348 .cleaner = &clean_rsa_public_key,
358 * Extract data from a Mysql database @a result at row @a row.
361 * @param result where to extract data from
362 * @param int row to extract data from
363 * @param fname name (or prefix) of the fields to extract from
364 * @param[in,out] dst_size where to store size of result, may be NULL
365 * @param[out] dst where to store the result
367 * #GNUNET_OK if all results could be extracted
368 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
371 pre_extract_rsa_signature (void *cls,
372 struct GNUNET_MY_ResultSpec *rs,
377 results[0].buffer = 0;
378 results[0].buffer_length = 0;
379 results[0].length = &rs->mysql_bind_output_length;
380 results[0].buffer_type = MYSQL_TYPE_BLOB;
387 * Extract data from a Mysql database @a result at row @a row.
390 * @param result where to extract data from
391 * @param int row to extract data from
392 * @param fname name (or prefix) of the fields to extract from
393 * @param[in,out] dst_size where to store size of result, may be NULL
394 * @param[out] dst where to store the result
396 * #GNUNET_OK if all results could be extracted
397 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
400 post_extract_rsa_signature (void *cls,
401 struct GNUNET_MY_ResultSpec *rs,
406 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
410 size = (size_t) rs->mysql_bind_output_length;
412 if (rs->mysql_bind_output_length != size)
413 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
414 buf = GNUNET_malloc (size);
416 results[0].buffer = buf;
417 results[0].buffer_length = size;
418 results[0].buffer_type = MYSQL_TYPE_BLOB;
420 mysql_stmt_fetch_column (stmt,
426 return GNUNET_SYSERR;
429 *sig = GNUNET_CRYPTO_rsa_signature_decode (buf,
434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
435 "Resuls contains bogus signature value (fails to decode)\n");
436 return GNUNET_SYSERR;
443 * Function called to clean up memory allocated
444 * by a #GNUNET_MY_ResultConverter.
447 * @param rd result data to clean up
450 clean_rsa_signature (void *cls,
451 struct GNUNET_MY_ResultSpec *rs)
453 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
457 GNUNET_CRYPTO_rsa_signature_free (*sig);
464 * RSA signature expected.
466 * @param[out] sig where to store the result;
467 * @return array entry for the result specification to use
469 struct GNUNET_MY_ResultSpec
470 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
472 struct GNUNET_MY_ResultSpec res =
474 .pre_conv = &pre_extract_rsa_signature,
475 .post_conv = &post_extract_rsa_signature,
476 .cleaner = &clean_rsa_signature,
485 * Extract data from a Mysql database @a result at row @a row
488 * @param result where to extract data from
489 * @param int row to extract data from
490 * @param fname name (or prefix) of the fields to extract from
491 * @param[in, out] dst_size where to store size of result, may be NULL
492 * @param[out] dst where to store the result
494 * #GNUNET_OK if all results could be extracted
495 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
498 pre_extract_string (void * cls,
499 struct GNUNET_MY_ResultSpec *rs,
504 results[0].buffer = (char *)rs->dst;
505 results[0].buffer_length = rs->dst_size;
506 results[0].length = &rs->mysql_bind_output_length;
513 * Check size of extracted fixed size data from a Mysql database @a
516 * @param result where to extract data from
517 * @param int row to extract data from
518 * @param fname name (or prefix) of the fields to extract from
519 * @param[in, out] dst_size where to store size of result, may be NULL
520 * @param[out] dst where to store the result
522 * #GNUNET_OK if all results could be extracted
523 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
526 post_extract_string (void * cls,
527 struct GNUNET_MY_ResultSpec *rs,
532 if (rs->dst_size != rs->mysql_bind_output_length)
533 return GNUNET_SYSERR;
539 * 0- terminated string exprected.
541 * @param[out] dst where to store the result, allocated
542 * @return array entry for the result specification to use
544 struct GNUNET_MY_ResultSpec
545 GNUNET_MY_result_spec_string (char **dst)
547 struct GNUNET_MY_ResultSpec res = {
548 .pre_conv = &pre_extract_string,
549 .post_conv = &post_extract_string,
560 * Absolute time expected
562 * @param name name of the field in the table
563 * @param[out] at where to store the result
564 * @return array entry for the result specification to use
566 struct GNUNET_MY_ResultSpec
567 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
569 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
574 * Absolute time in network byte order expected
576 * @param[out] at where to store the result
577 * @return array entry for the result specification to use
579 struct GNUNET_MY_ResultSpec
580 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
582 struct GNUNET_MY_ResultSpec res =
583 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
589 * Extract data from a Postgres database @a result at row @a row.
592 * @param result where to extract data from
593 * @param int row to extract data from
594 * @param fname name (or prefix) of the fields to extract from
595 * @param[in,out] dst_size where to store size of result, may be NULL
596 * @param[out] dst where to store the result
598 * #GNUNET_YES if all results could be extracted
599 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
602 pre_extract_uint16 (void *cls,
603 struct GNUNET_MY_ResultSpec *rs,
608 results[0].buffer = (char *)rs->dst;
609 results[0].buffer_length = rs->dst_size;
610 results[0].length = &rs->mysql_bind_output_length;
611 results[0].buffer_type = MYSQL_TYPE_SHORT;
618 * Check size of extracted fixed size data from a Mysql datbase.
621 * @param result where to extract data from
622 * @param int row to extract data from
623 * @param fname name (or prefix) of the fields to extract from
624 * @param[in,out] dst_size where to store size of result, may be NULL
625 * @param[out] dst where to store the result
627 * #GNUNET_YES if all results could be extracted
628 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
631 post_extract_uint16 (void *cls,
632 struct GNUNET_MY_ResultSpec *rs,
637 if (rs->dst_size != rs->mysql_bind_output_length)
638 return GNUNET_SYSERR;
646 * @param[out] u16 where to store the result
647 * @return array entry for the result specification to use
649 struct GNUNET_MY_ResultSpec
650 GNUNET_MY_result_spec_uint16 (uint16_t *u16)
652 struct GNUNET_MY_ResultSpec res = {
653 .pre_conv = &pre_extract_uint16,
654 .post_conv = &post_extract_uint16,
657 .dst_size = sizeof (*u16),
664 * Extrac data from a MYSQL database @a result at row @a row
667 * @param result where to extract data from
668 * @param int row to extract data from
669 * @param fname name (or prefix) of the fields to extract from
670 * @param[in, out] dst_size where to store size of result, may be NULL
671 * @param[out] dst where to store the result
673 * #GNUNET_OK if all results could be extracted
674 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
677 pre_extract_uint32 (void *cls,
678 struct GNUNET_MY_ResultSpec *rs,
683 results[0].buffer = (int *)rs->dst;
684 results[0].buffer_length = rs->dst_size;
685 results[0].length = &rs->mysql_bind_output_length;
686 results[0].buffer_type = MYSQL_TYPE_LONG;
693 * Extrac data from a MYSQL database @a result at row @a row
696 * @param result where to extract data from
697 * @param int row to extract data from
698 * @param fname name (or prefix) of the fields to extract from
699 * @param[in, out] dst_size where to store size of result, may be NULL
700 * @param[out] dst where to store the result
702 * #GNUNET_OK if all results could be extracted
703 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
706 post_extract_uint32 (void *cls,
707 struct GNUNET_MY_ResultSpec *rs,
712 if (rs->dst_size != rs->mysql_bind_output_length)
713 return GNUNET_SYSERR;
721 * @param[out] u32 where to store the result
722 * @return array entry for the result specification to use
724 struct GNUNET_MY_ResultSpec
725 GNUNET_MY_result_spec_uint32 (uint32_t *u32)
727 struct GNUNET_MY_ResultSpec res = {
728 .pre_conv = &pre_extract_uint32,
729 .post_conv = &post_extract_uint32,
732 .dst_size = sizeof (*u32),
740 * Extract data from a MYSQL database @a result at row @a row
743 * @param result where to extract data from
744 * @param int row to extract data from
745 * @param fname name (or prefix) of the fields to extract from
746 * @param[in, out] dst_size where to store size of result, may be null
747 * @param[out] dst where to store the result
749 * #GNUNET_OK if all results could be extracted
750 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
753 pre_extract_uint64 (void *cls,
754 struct GNUNET_MY_ResultSpec *rs,
759 results[0].buffer = rs->dst;
760 results[0].buffer_length = rs->dst_size;
761 results[0].length = &rs->mysql_bind_output_length;
762 results[0].buffer_type = MYSQL_TYPE_LONGLONG;
770 * Check size of extracted fixe size data from a Mysql database
773 * @param result where to extract data from
774 * @param int row to extract data from
775 * @param fname name (or prefix) of the fields to extract from
776 * @param[in, out] dst_size where to store size of result, may be null
777 * @param[out] dst where to store the result
779 * #GNUNET_OK if all results could be extracted
780 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
783 post_extract_uint64 (void *cls,
784 struct GNUNET_MY_ResultSpec *rs,
789 if (rs->dst_size != rs->mysql_bind_output_length)
790 return GNUNET_SYSERR;
798 * @param[out] u64 where to store the result
799 * @return array entry for the result specification to use
801 struct GNUNET_MY_ResultSpec
802 GNUNET_MY_result_spec_uint64 (uint64_t *u64)
804 struct GNUNET_MY_ResultSpec res = {
805 .pre_conv = &pre_extract_uint64,
806 .post_conv = &post_extract_uint64,
809 .dst_size = sizeof (*u64),
815 /* end of pq_result_helper.c */