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
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file my/my_result_helper.c
22 * @brief functions to extract result values
23 * @author Christophe Genevey
27 #include "gnunet_util_lib.h"
28 #include "gnunet_my_lib.h"
32 * extract data from a Mysql database @a result at row @a row
36 * @param stmt the mysql statement that is being run
37 * @param column the column that is being processed
38 * @param[out] result mysql result
40 * #GNUNET_OK if all results could be extracted
41 * #GNUNET_SYSERR if a result was invalid
44 pre_extract_varsize_blob(void *cls,
45 struct GNUNET_MY_ResultSpec *rs,
50 results[0].buffer = NULL;
51 results[0].buffer_length = 0;
52 results[0].length = &rs->mysql_bind_output_length;
53 results[0].is_null = &rs->is_null;
61 * extract data from a Mysql database @a result at row @a row
65 * @param stmt the mysql statement that is being run
66 * @param column the column that is being processed
69 * #GNUNET_OK if all results could be extracted
70 * #GNUNET_SYSERR if a result was invalid
73 post_extract_varsize_blob(void *cls,
74 struct GNUNET_MY_ResultSpec *rs,
82 if (*results->is_null)
84 size = (size_t)rs->mysql_bind_output_length;
86 if (rs->mysql_bind_output_length != size)
87 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
89 buf = GNUNET_malloc(size);
91 results[0].buffer = buf;
92 results[0].buffer_length = size;
93 results[0].buffer_type = MYSQL_TYPE_BLOB;
96 mysql_stmt_fetch_column(stmt,
102 return GNUNET_SYSERR;
105 *(void **)rs->dst = buf;
106 *rs->result_size = size;
113 * extract data from a Mysql database @a result at row @a row
119 cleanup_varsize_blob(void *cls,
120 struct GNUNET_MY_ResultSpec *rs)
122 void **ptr = (void **)rs->dst;
133 * Variable-size result expected
135 * @param[out] dst where to store the result, allocated
136 * @param[out] ptr_size where to store the size of @a dst
137 * @return array entru for the result specification to use
139 struct GNUNET_MY_ResultSpec
140 GNUNET_MY_result_spec_variable_size(void **dst,
143 struct GNUNET_MY_ResultSpec res =
145 .pre_conv = &pre_extract_varsize_blob,
146 .post_conv = &post_extract_varsize_blob,
147 .cleaner = &cleanup_varsize_blob,
148 .dst = (void *)(dst),
149 .result_size = ptr_size,
158 * Extract data from a Mysql database @a result at row @a row
162 * @param stmt the mysql statement that is being run
163 * @param column the column that is being processed
164 * @param[out] results
166 * #GNUNET_OK if all results could be extracted
167 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
170 pre_extract_fixed_blob(void *cls,
171 struct GNUNET_MY_ResultSpec *rs,
176 results[0].buffer = rs->dst;
177 results[0].buffer_length = rs->dst_size;
178 results[0].length = &rs->mysql_bind_output_length;
179 results[0].buffer_type = MYSQL_TYPE_BLOB;
180 results[0].is_null = &rs->is_null;
188 * Check size of extracted fixed size data from a Mysql database @a
189 * result at row @a row
193 * @param stmt the mysql statement that is being run
194 * @param column the column that is being processed
195 * @param[out] results
197 * #GNUNET_OK if all results could be extracted
198 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
201 post_extract_fixed_blob(void *cls,
202 struct GNUNET_MY_ResultSpec *rs,
207 if (*results->is_null)
208 return GNUNET_SYSERR;
209 if (rs->dst_size != rs->mysql_bind_output_length)
210 return GNUNET_SYSERR;
216 * Fixed-size result expected.
218 * @param name name of the field in the table
219 * @param[out] dst where to store the result
220 * @param ptr_size number of bytes in @a dst
221 * @return array entry for the result specification to use
223 struct GNUNET_MY_ResultSpec
224 GNUNET_MY_result_spec_fixed_size(void *ptr,
227 struct GNUNET_MY_ResultSpec res =
229 .pre_conv = &pre_extract_fixed_blob,
230 .post_conv = &post_extract_fixed_blob,
232 .dst = (void *)(ptr),
233 .dst_size = ptr_size,
242 * Extract data from a Mysql database @a result at row @a row
246 * @param stmt the mysql statement that is being run
247 * @param column the column that is being processed
248 * @param[out] results
250 * #GNUNET_OK if all results could be extracted
251 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
254 pre_extract_rsa_public_key(void *cls,
255 struct GNUNET_MY_ResultSpec *rs,
260 results[0].buffer = NULL;
261 results[0].buffer_length = 0;
262 results[0].length = &rs->mysql_bind_output_length;
263 results[0].buffer_type = MYSQL_TYPE_BLOB;
264 results[0].is_null = &rs->is_null;
272 * Check size of extracted fixed size data from a Mysql database @a
273 * result at row @a row
277 * @param stmt the mysql statement that is being run
278 * @param column the column that is being processed
279 * @param[out] results
281 * #GNUNET_OK if all results could be extracted
282 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
285 post_extract_rsa_public_key(void *cls,
286 struct GNUNET_MY_ResultSpec *rs,
292 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
296 if (*results->is_null)
297 return GNUNET_SYSERR;
298 size = (size_t)rs->mysql_bind_output_length;
300 if (rs->mysql_bind_output_length != size)
301 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
302 buf = GNUNET_malloc(size);
304 results[0].buffer = buf;
305 results[0].buffer_length = size;
306 results[0].buffer_type = MYSQL_TYPE_BLOB;
308 mysql_stmt_fetch_column(stmt,
314 return GNUNET_SYSERR;
317 *pk = GNUNET_CRYPTO_rsa_public_key_decode(buf,
322 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
323 "Results contains bogus public key value (fail to decode)\n");
324 return GNUNET_SYSERR;
332 * Function called to clean up memory allocated
333 * by a #GNUNET_MY_ResultConverter.
336 * @param rs result data to clean up
339 clean_rsa_public_key(void *cls,
340 struct GNUNET_MY_ResultSpec *rs)
342 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
346 GNUNET_CRYPTO_rsa_public_key_free(*pk);
353 * RSA public key expected
355 * @param name name of the field in the table
356 * @param[out] rsa where to store the result
357 * @return array entry for the result specification to use
359 struct GNUNET_MY_ResultSpec
360 GNUNET_MY_result_spec_rsa_public_key(struct GNUNET_CRYPTO_RsaPublicKey **rsa)
362 struct GNUNET_MY_ResultSpec res = {
363 .pre_conv = &pre_extract_rsa_public_key,
364 .post_conv = &post_extract_rsa_public_key,
365 .cleaner = &clean_rsa_public_key,
376 * Extract data from a Mysql database @a result at row @a row.
380 * @param stmt the mysql statement that is being run
381 * @param column the column that is being processed
382 * @param[out] results
384 * #GNUNET_OK if all results could be extracted
385 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
388 pre_extract_rsa_signature(void *cls,
389 struct GNUNET_MY_ResultSpec *rs,
394 results[0].buffer = 0;
395 results[0].buffer_length = 0;
396 results[0].length = &rs->mysql_bind_output_length;
397 results[0].buffer_type = MYSQL_TYPE_BLOB;
398 results[0].is_null = &rs->is_null;
406 * Extract data from a Mysql database @a result at row @a row.
410 * @param stmt the mysql statement that is being run
411 * @param column the column that is being processed
412 * @param[out] results
414 * #GNUNET_OK if all results could be extracted
415 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
418 post_extract_rsa_signature(void *cls,
419 struct GNUNET_MY_ResultSpec *rs,
424 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
428 if (*results->is_null)
429 return GNUNET_SYSERR;
430 size = (size_t)rs->mysql_bind_output_length;
432 if (rs->mysql_bind_output_length != size)
433 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
434 buf = GNUNET_malloc(size);
436 results[0].buffer = buf;
437 results[0].buffer_length = size;
438 results[0].buffer_type = MYSQL_TYPE_BLOB;
440 mysql_stmt_fetch_column(stmt,
446 return GNUNET_SYSERR;
449 *sig = GNUNET_CRYPTO_rsa_signature_decode(buf,
454 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
455 "Resuls contains bogus signature value (fails to decode)\n");
456 return GNUNET_SYSERR;
463 * Function called to clean up memory allocated
464 * by a #GNUNET_MY_ResultConverter.
467 * @param rd result data to clean up
470 clean_rsa_signature(void *cls,
471 struct GNUNET_MY_ResultSpec *rs)
473 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
477 GNUNET_CRYPTO_rsa_signature_free(*sig);
484 * RSA signature expected.
486 * @param[out] sig where to store the result;
487 * @return array entry for the result specification to use
489 struct GNUNET_MY_ResultSpec
490 GNUNET_MY_result_spec_rsa_signature(struct GNUNET_CRYPTO_RsaSignature **sig)
492 struct GNUNET_MY_ResultSpec res =
494 .pre_conv = &pre_extract_rsa_signature,
495 .post_conv = &post_extract_rsa_signature,
496 .cleaner = &clean_rsa_signature,
507 * Extract data from a Mysql database @a result at row @a row
511 * @param stmt the mysql statement that is being run
512 * @param column the column that is being processed
513 * @param[out] results
515 * #GNUNET_OK if all results could be extracted
516 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
519 pre_extract_string(void * cls,
520 struct GNUNET_MY_ResultSpec *rs,
525 results[0].buffer = NULL;
526 results[0].buffer_length = 0;
527 results[0].length = &rs->mysql_bind_output_length;
528 results[0].buffer_type = MYSQL_TYPE_BLOB;
529 results[0].is_null = &rs->is_null;
537 * Check size of extracted fixed size data from a Mysql database @a
541 * @param stmt the mysql statement that is being run
542 * @param column the column that is being processed
543 * @param[out] results
545 * #GNUNET_OK if all results could be extracted
546 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
549 post_extract_string(void * cls,
550 struct GNUNET_MY_ResultSpec *rs,
555 size_t size = (size_t)rs->mysql_bind_output_length;
558 if (rs->mysql_bind_output_length != size)
559 return GNUNET_SYSERR;
560 if (*results->is_null)
562 *(void **)rs->dst = NULL;
566 buf = GNUNET_malloc(size);
567 results[0].buffer = buf;
568 results[0].buffer_length = size;
569 results[0].buffer_type = MYSQL_TYPE_BLOB;
572 mysql_stmt_fetch_column(stmt,
578 return GNUNET_SYSERR;
581 *(void **)rs->dst = buf;
587 * 0- terminated string exprected.
589 * @param[out] dst where to store the result, allocated
590 * @return array entry for the result specification to use
592 struct GNUNET_MY_ResultSpec
593 GNUNET_MY_result_spec_string(char **dst)
595 struct GNUNET_MY_ResultSpec res = {
596 .pre_conv = &pre_extract_string,
597 .post_conv = &post_extract_string,
609 * Absolute time expected
611 * @param name name of the field in the table
612 * @param[out] at where to store the result
613 * @return array entry for the result specification to use
615 struct GNUNET_MY_ResultSpec
616 GNUNET_MY_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
618 return GNUNET_MY_result_spec_uint64(&at->abs_value_us);
623 * Absolute time in network byte order expected
625 * @param[out] at where to store the result
626 * @return array entry for the result specification to use
628 struct GNUNET_MY_ResultSpec
629 GNUNET_MY_result_spec_absolute_time_nbo(struct GNUNET_TIME_AbsoluteNBO *at)
631 struct GNUNET_MY_ResultSpec res =
632 GNUNET_MY_result_spec_auto_from_type(&at->abs_value_us__);
639 * Extract data from a Postgres database @a result at row @a row.
643 * @param stmt the mysql statement that is being run
644 * @param column the column that is being processed
645 * @param[out] results
647 * #GNUNET_YES if all results could be extracted
648 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
651 pre_extract_uint16(void *cls,
652 struct GNUNET_MY_ResultSpec *rs,
657 results[0].buffer = rs->dst;
658 results[0].buffer_length = rs->dst_size;
659 results[0].length = &rs->mysql_bind_output_length;
660 results[0].buffer_type = MYSQL_TYPE_SHORT;
661 results[0].is_null = &rs->is_null;
669 * Check size of extracted fixed size data from a Mysql datbase.
673 * @param stmt the mysql statement that is being run
674 * @param column the column that is being processed
675 * @param[out] results
677 * #GNUNET_YES if all results could be extracted
678 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
681 post_extract_uint16(void *cls,
682 struct GNUNET_MY_ResultSpec *rs,
687 if (rs->dst_size != rs->mysql_bind_output_length)
688 return GNUNET_SYSERR;
689 if (*results->is_null)
690 return GNUNET_SYSERR;
698 * @param[out] u16 where to store the result
699 * @return array entry for the result specification to use
701 struct GNUNET_MY_ResultSpec
702 GNUNET_MY_result_spec_uint16(uint16_t *u16)
704 struct GNUNET_MY_ResultSpec res = {
705 .pre_conv = &pre_extract_uint16,
706 .post_conv = &post_extract_uint16,
709 .dst_size = sizeof(*u16),
718 * Extrac data from a MYSQL database @a result at row @a row
723 * @param stmt the mysql statement that is being run
724 * @param column the column that is being processed
725 * @param[out] results
727 * #GNUNET_OK if all results could be extracted
728 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
731 pre_extract_uint32(void *cls,
732 struct GNUNET_MY_ResultSpec *rs,
737 results[0].buffer = rs->dst;
738 results[0].buffer_length = rs->dst_size;
739 results[0].length = &rs->mysql_bind_output_length;
740 results[0].buffer_type = MYSQL_TYPE_LONG;
741 results[0].is_null = &rs->is_null;
749 * Extrac data from a MYSQL database @a result at row @a row
754 * @param stmt the mysql statement that is being run
755 * @param column the column that is being processed
756 * @param[out] results
758 * #GNUNET_OK if all results could be extracted
759 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
762 post_extract_uint32(void *cls,
763 struct GNUNET_MY_ResultSpec *rs,
768 if (rs->dst_size != rs->mysql_bind_output_length)
769 return GNUNET_SYSERR;
770 if (*results->is_null)
771 return GNUNET_SYSERR;
779 * @param[out] u32 where to store the result
780 * @return array entry for the result specification to use
782 struct GNUNET_MY_ResultSpec
783 GNUNET_MY_result_spec_uint32(uint32_t *u32)
785 struct GNUNET_MY_ResultSpec res = {
786 .pre_conv = &pre_extract_uint32,
787 .post_conv = &post_extract_uint32,
790 .dst_size = sizeof(*u32),
799 * Extract data from a MYSQL database @a result at row @a row
803 * @param stmt the mysql statement that is being run
804 * @param column the column that is being processed
805 * @param[out] results
807 * #GNUNET_OK if all results could be extracted
808 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
811 pre_extract_uint64(void *cls,
812 struct GNUNET_MY_ResultSpec *rs,
817 if (sizeof(uint64_t) != rs->dst_size)
818 return GNUNET_SYSERR;
819 results[0].buffer = rs->dst;
820 results[0].buffer_length = rs->dst_size;
821 results[0].length = &rs->mysql_bind_output_length;
822 results[0].buffer_type = MYSQL_TYPE_LONGLONG;
823 results[0].is_null = &rs->is_null;
831 * Check size of extracted fixe size data from a Mysql database
835 * @param stmt the mysql statement that is being run
836 * @param column the column that is being processed
837 * @param[out] results
839 * #GNUNET_OK if all results could be extracted
840 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
843 post_extract_uint64(void *cls,
844 struct GNUNET_MY_ResultSpec *rs,
849 if (sizeof(uint64_t) != rs->dst_size)
850 return GNUNET_SYSERR;
851 if (*results->is_null)
852 return GNUNET_SYSERR;
860 * @param[out] u64 where to store the result
861 * @return array entry for the result specification to use
863 struct GNUNET_MY_ResultSpec
864 GNUNET_MY_result_spec_uint64(uint64_t *u64)
866 struct GNUNET_MY_ResultSpec res = {
867 .pre_conv = &pre_extract_uint64,
868 .post_conv = &post_extract_uint64,
871 .dst_size = sizeof(*u64),
879 /* end of my_result_helper.c */