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/>.
19 * @file my/my_result_helper.c
20 * @brief functions to extract result values
21 * @author Christophe Genevey
25 #include "gnunet_util_lib.h"
26 #include "gnunet_my_lib.h"
30 * extract data from a Mysql database @a result at row @a row
34 * @param stmt the mysql statement that is being run
35 * @param column the column that is being processed
36 * @param[out] result mysql result
38 * #GNUNET_OK if all results could be extracted
39 * #GNUNET_SYSERR if a result was invalid
42 pre_extract_varsize_blob (void *cls,
43 struct GNUNET_MY_ResultSpec *rs,
48 results[0].buffer = NULL;
49 results[0].buffer_length = 0;
50 results[0].length = &rs->mysql_bind_output_length;
51 results[0].is_null = &rs->is_null;
59 * extract data from a Mysql database @a result at row @a row
63 * @param stmt the mysql statement that is being run
64 * @param column the column that is being processed
67 * #GNUNET_OK if all results could be extracted
68 * #GNUNET_SYSERR if a result was invalid
71 post_extract_varsize_blob (void *cls,
72 struct GNUNET_MY_ResultSpec *rs,
80 if (*results->is_null)
82 size = (size_t) rs->mysql_bind_output_length;
84 if (rs->mysql_bind_output_length != size)
85 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
87 buf = GNUNET_malloc (size);
89 results[0].buffer = buf;
90 results[0].buffer_length = size;
91 results[0].buffer_type = MYSQL_TYPE_BLOB;
94 mysql_stmt_fetch_column (stmt,
100 return GNUNET_SYSERR;
103 *(void **) rs->dst = buf;
104 *rs->result_size = size;
111 * extract data from a Mysql database @a result at row @a row
117 cleanup_varsize_blob (void *cls,
118 struct GNUNET_MY_ResultSpec *rs)
120 void **ptr = (void **)rs->dst;
131 * Variable-size result expected
133 * @param[out] dst where to store the result, allocated
134 * @param[out] ptr_size where to store the size of @a dst
135 * @return array entru for the result specification to use
137 struct GNUNET_MY_ResultSpec
138 GNUNET_MY_result_spec_variable_size (void **dst,
141 struct GNUNET_MY_ResultSpec res =
143 .pre_conv = &pre_extract_varsize_blob,
144 .post_conv = &post_extract_varsize_blob,
145 .cleaner = &cleanup_varsize_blob,
146 .dst = (void *)(dst),
147 .result_size = ptr_size,
156 * Extract data from a Mysql database @a result at row @a row
160 * @param stmt the mysql statement that is being run
161 * @param column the column that is being processed
162 * @param[out] results
164 * #GNUNET_OK if all results could be extracted
165 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
168 pre_extract_fixed_blob (void *cls,
169 struct GNUNET_MY_ResultSpec *rs,
174 results[0].buffer = rs->dst;
175 results[0].buffer_length = rs->dst_size;
176 results[0].length = &rs->mysql_bind_output_length;
177 results[0].buffer_type = MYSQL_TYPE_BLOB;
178 results[0].is_null = &rs->is_null;
186 * Check size of extracted fixed size data from a Mysql database @a
187 * result at row @a row
191 * @param stmt the mysql statement that is being run
192 * @param column the column that is being processed
193 * @param[out] results
195 * #GNUNET_OK if all results could be extracted
196 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
199 post_extract_fixed_blob (void *cls,
200 struct GNUNET_MY_ResultSpec *rs,
205 if (*results->is_null)
206 return GNUNET_SYSERR;
207 if (rs->dst_size != rs->mysql_bind_output_length)
208 return GNUNET_SYSERR;
214 * Fixed-size result expected.
216 * @param name name of the field in the table
217 * @param[out] dst where to store the result
218 * @param ptr_size number of bytes in @a dst
219 * @return array entry for the result specification to use
221 struct GNUNET_MY_ResultSpec
222 GNUNET_MY_result_spec_fixed_size (void *ptr,
225 struct GNUNET_MY_ResultSpec res =
227 .pre_conv = &pre_extract_fixed_blob,
228 .post_conv = &post_extract_fixed_blob,
230 .dst = (void *)(ptr),
231 .dst_size = ptr_size,
240 * Extract data from a Mysql database @a result at row @a row
244 * @param stmt the mysql statement that is being run
245 * @param column the column that is being processed
246 * @param[out] results
248 * #GNUNET_OK if all results could be extracted
249 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
252 pre_extract_rsa_public_key (void *cls,
253 struct GNUNET_MY_ResultSpec *rs,
258 results[0].buffer = NULL;
259 results[0].buffer_length = 0;
260 results[0].length = &rs->mysql_bind_output_length;
261 results[0].buffer_type = MYSQL_TYPE_BLOB;
262 results[0].is_null = &rs->is_null;
270 * Check size of extracted fixed size data from a Mysql database @a
271 * result at row @a row
275 * @param stmt the mysql statement that is being run
276 * @param column the column that is being processed
277 * @param[out] results
279 * #GNUNET_OK if all results could be extracted
280 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
283 post_extract_rsa_public_key (void *cls,
284 struct GNUNET_MY_ResultSpec *rs,
290 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
294 if (*results->is_null)
295 return GNUNET_SYSERR;
296 size = (size_t) rs->mysql_bind_output_length;
298 if (rs->mysql_bind_output_length != size)
299 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
300 buf = GNUNET_malloc (size);
302 results[0].buffer = buf;
303 results[0].buffer_length = size;
304 results[0].buffer_type = MYSQL_TYPE_BLOB;
306 mysql_stmt_fetch_column (stmt,
312 return GNUNET_SYSERR;
315 *pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
321 "Results contains bogus public key value (fail to decode)\n");
322 return GNUNET_SYSERR;
330 * Function called to clean up memory allocated
331 * by a #GNUNET_MY_ResultConverter.
334 * @param rs result data to clean up
337 clean_rsa_public_key (void *cls,
338 struct GNUNET_MY_ResultSpec *rs)
340 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
344 GNUNET_CRYPTO_rsa_public_key_free (*pk);
351 * RSA public key expected
353 * @param name name of the field in the table
354 * @param[out] rsa where to store the result
355 * @return array entry for the result specification to use
357 struct GNUNET_MY_ResultSpec
358 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
360 struct GNUNET_MY_ResultSpec res = {
361 .pre_conv = &pre_extract_rsa_public_key,
362 .post_conv = &post_extract_rsa_public_key,
363 .cleaner = &clean_rsa_public_key,
374 * Extract data from a Mysql database @a result at row @a row.
378 * @param stmt the mysql statement that is being run
379 * @param column the column that is being processed
380 * @param[out] results
382 * #GNUNET_OK if all results could be extracted
383 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
386 pre_extract_rsa_signature (void *cls,
387 struct GNUNET_MY_ResultSpec *rs,
392 results[0].buffer = 0;
393 results[0].buffer_length = 0;
394 results[0].length = &rs->mysql_bind_output_length;
395 results[0].buffer_type = MYSQL_TYPE_BLOB;
396 results[0].is_null = &rs->is_null;
404 * Extract data from a Mysql database @a result at row @a row.
408 * @param stmt the mysql statement that is being run
409 * @param column the column that is being processed
410 * @param[out] results
412 * #GNUNET_OK if all results could be extracted
413 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
416 post_extract_rsa_signature (void *cls,
417 struct GNUNET_MY_ResultSpec *rs,
422 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
426 if (*results->is_null)
427 return GNUNET_SYSERR;
428 size = (size_t) rs->mysql_bind_output_length;
430 if (rs->mysql_bind_output_length != size)
431 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
432 buf = GNUNET_malloc (size);
434 results[0].buffer = buf;
435 results[0].buffer_length = size;
436 results[0].buffer_type = MYSQL_TYPE_BLOB;
438 mysql_stmt_fetch_column (stmt,
444 return GNUNET_SYSERR;
447 *sig = GNUNET_CRYPTO_rsa_signature_decode (buf,
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
453 "Resuls contains bogus signature value (fails to decode)\n");
454 return GNUNET_SYSERR;
461 * Function called to clean up memory allocated
462 * by a #GNUNET_MY_ResultConverter.
465 * @param rd result data to clean up
468 clean_rsa_signature (void *cls,
469 struct GNUNET_MY_ResultSpec *rs)
471 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
475 GNUNET_CRYPTO_rsa_signature_free (*sig);
482 * RSA signature expected.
484 * @param[out] sig where to store the result;
485 * @return array entry for the result specification to use
487 struct GNUNET_MY_ResultSpec
488 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
490 struct GNUNET_MY_ResultSpec res =
492 .pre_conv = &pre_extract_rsa_signature,
493 .post_conv = &post_extract_rsa_signature,
494 .cleaner = &clean_rsa_signature,
504 * Extract data from a Mysql database @a result at row @a row
508 * @param stmt the mysql statement that is being run
509 * @param column the column that is being processed
510 * @param[out] results
512 * #GNUNET_OK if all results could be extracted
513 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
516 pre_extract_string (void * cls,
517 struct GNUNET_MY_ResultSpec *rs,
522 results[0].buffer = NULL;
523 results[0].buffer_length = 0;
524 results[0].length = &rs->mysql_bind_output_length;
525 results[0].buffer_type = MYSQL_TYPE_BLOB;
526 results[0].is_null = &rs->is_null;
534 * Check size of extracted fixed size data from a Mysql database @a
538 * @param stmt the mysql statement that is being run
539 * @param column the column that is being processed
540 * @param[out] results
542 * #GNUNET_OK if all results could be extracted
543 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
546 post_extract_string (void * cls,
547 struct GNUNET_MY_ResultSpec *rs,
552 size_t size = (size_t) rs->mysql_bind_output_length;
555 if (rs->mysql_bind_output_length != size)
556 return GNUNET_SYSERR;
557 if (*results->is_null)
559 *(void **) rs->dst = NULL;
563 buf = GNUNET_malloc (size);
564 results[0].buffer = buf;
565 results[0].buffer_length = size;
566 results[0].buffer_type = MYSQL_TYPE_BLOB;
569 mysql_stmt_fetch_column (stmt,
575 return GNUNET_SYSERR;
578 *(void **) rs->dst = buf;
584 * 0- terminated string exprected.
586 * @param[out] dst where to store the result, allocated
587 * @return array entry for the result specification to use
589 struct GNUNET_MY_ResultSpec
590 GNUNET_MY_result_spec_string (char **dst)
592 struct GNUNET_MY_ResultSpec res = {
593 .pre_conv = &pre_extract_string,
594 .post_conv = &post_extract_string,
605 * Absolute time expected
607 * @param name name of the field in the table
608 * @param[out] at where to store the result
609 * @return array entry for the result specification to use
611 struct GNUNET_MY_ResultSpec
612 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
614 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
619 * Absolute time in network byte order expected
621 * @param[out] at where to store the result
622 * @return array entry for the result specification to use
624 struct GNUNET_MY_ResultSpec
625 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
627 struct GNUNET_MY_ResultSpec res =
628 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
634 * Extract data from a Postgres database @a result at row @a row.
638 * @param stmt the mysql statement that is being run
639 * @param column the column that is being processed
640 * @param[out] results
642 * #GNUNET_YES if all results could be extracted
643 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
646 pre_extract_uint16 (void *cls,
647 struct GNUNET_MY_ResultSpec *rs,
652 results[0].buffer = rs->dst;
653 results[0].buffer_length = rs->dst_size;
654 results[0].length = &rs->mysql_bind_output_length;
655 results[0].buffer_type = MYSQL_TYPE_SHORT;
656 results[0].is_null = &rs->is_null;
664 * Check size of extracted fixed size data from a Mysql datbase.
668 * @param stmt the mysql statement that is being run
669 * @param column the column that is being processed
670 * @param[out] results
672 * #GNUNET_YES if all results could be extracted
673 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
676 post_extract_uint16 (void *cls,
677 struct GNUNET_MY_ResultSpec *rs,
682 if (rs->dst_size != rs->mysql_bind_output_length)
683 return GNUNET_SYSERR;
684 if (*results->is_null)
685 return GNUNET_SYSERR;
693 * @param[out] u16 where to store the result
694 * @return array entry for the result specification to use
696 struct GNUNET_MY_ResultSpec
697 GNUNET_MY_result_spec_uint16 (uint16_t *u16)
699 struct GNUNET_MY_ResultSpec res = {
700 .pre_conv = &pre_extract_uint16,
701 .post_conv = &post_extract_uint16,
704 .dst_size = sizeof (*u16),
712 * Extrac data from a MYSQL database @a result at row @a row
717 * @param stmt the mysql statement that is being run
718 * @param column the column that is being processed
719 * @param[out] results
721 * #GNUNET_OK if all results could be extracted
722 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
725 pre_extract_uint32 (void *cls,
726 struct GNUNET_MY_ResultSpec *rs,
731 results[0].buffer = rs->dst;
732 results[0].buffer_length = rs->dst_size;
733 results[0].length = &rs->mysql_bind_output_length;
734 results[0].buffer_type = MYSQL_TYPE_LONG;
735 results[0].is_null = &rs->is_null;
743 * Extrac data from a MYSQL database @a result at row @a row
748 * @param stmt the mysql statement that is being run
749 * @param column the column that is being processed
750 * @param[out] results
752 * #GNUNET_OK if all results could be extracted
753 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
756 post_extract_uint32 (void *cls,
757 struct GNUNET_MY_ResultSpec *rs,
762 if (rs->dst_size != rs->mysql_bind_output_length)
763 return GNUNET_SYSERR;
764 if (*results->is_null)
765 return GNUNET_SYSERR;
773 * @param[out] u32 where to store the result
774 * @return array entry for the result specification to use
776 struct GNUNET_MY_ResultSpec
777 GNUNET_MY_result_spec_uint32 (uint32_t *u32)
779 struct GNUNET_MY_ResultSpec res = {
780 .pre_conv = &pre_extract_uint32,
781 .post_conv = &post_extract_uint32,
784 .dst_size = sizeof (*u32),
792 * Extract data from a MYSQL database @a result at row @a row
796 * @param stmt the mysql statement that is being run
797 * @param column the column that is being processed
798 * @param[out] results
800 * #GNUNET_OK if all results could be extracted
801 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
804 pre_extract_uint64 (void *cls,
805 struct GNUNET_MY_ResultSpec *rs,
810 if (sizeof (uint64_t) != rs->dst_size)
811 return GNUNET_SYSERR;
812 results[0].buffer = rs->dst;
813 results[0].buffer_length = rs->dst_size;
814 results[0].length = &rs->mysql_bind_output_length;
815 results[0].buffer_type = MYSQL_TYPE_LONGLONG;
816 results[0].is_null = &rs->is_null;
824 * Check size of extracted fixe size data from a Mysql database
828 * @param stmt the mysql statement that is being run
829 * @param column the column that is being processed
830 * @param[out] results
832 * #GNUNET_OK if all results could be extracted
833 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
836 post_extract_uint64 (void *cls,
837 struct GNUNET_MY_ResultSpec *rs,
842 if (sizeof (uint64_t) != rs->dst_size)
843 return GNUNET_SYSERR;
844 if (*results->is_null)
845 return GNUNET_SYSERR;
853 * @param[out] u64 where to store the result
854 * @return array entry for the result specification to use
856 struct GNUNET_MY_ResultSpec
857 GNUNET_MY_result_spec_uint64 (uint64_t *u64)
859 struct GNUNET_MY_ResultSpec res = {
860 .pre_conv = &pre_extract_uint64,
861 .post_conv = &post_extract_uint64,
864 .dst_size = sizeof (*u64),
871 /* end of my_result_helper.c */