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"
28 * extract data from a Mysql database @a result at row @a row
32 * @param stmt the mysql statement that is being run
33 * @param column the column that is being processed
34 * @param[out] result mysql result
36 * #GNUNET_OK if all results could be extracted
37 * #GNUNET_SYSERR if a result was invalid
40 pre_extract_varsize_blob (void *cls,
41 struct GNUNET_MY_ResultSpec *rs,
46 results[0].buffer = NULL;
47 results[0].buffer_length = 0;
48 results[0].length = &rs->mysql_bind_output_length;
55 * extract data from a Mysql database @a result at row @a row
59 * @param stmt the mysql statement that is being run
60 * @param column the column that is being processed
63 * #GNUNET_OK if all results could be extracted
64 * #GNUNET_SYSERR if a result was invalid
67 post_extract_varsize_blob (void *cls,
68 struct GNUNET_MY_ResultSpec *rs,
76 size = (size_t) rs->mysql_bind_output_length;
78 if (rs->mysql_bind_output_length != size)
79 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
81 buf = GNUNET_malloc (size);
83 results[0].buffer = buf;
84 results[0].buffer_length = size;
85 results[0].buffer_type = MYSQL_TYPE_BLOB;
88 mysql_stmt_fetch_column (stmt,
97 *(void **) rs->dst = buf;
98 *rs->result_size = size;
105 * extract data from a Mysql database @a result at row @a row
111 cleanup_varsize_blob (void *cls,
112 struct GNUNET_MY_ResultSpec *rs)
114 void **ptr = (void **)rs->dst;
125 * Variable-size result expected
127 * @param[out] dst where to store the result, allocated
128 * @param[out] ptr_size where to store the size of @a dst
129 * @return array entru for the result specification to use
131 struct GNUNET_MY_ResultSpec
132 GNUNET_MY_result_spec_variable_size (void **dst,
135 struct GNUNET_MY_ResultSpec res =
137 .pre_conv = &pre_extract_varsize_blob,
138 .post_conv = &post_extract_varsize_blob,
139 .cleaner = &cleanup_varsize_blob,
140 .dst = (void *)(dst),
141 .result_size = ptr_size,
150 * Extract data from a Mysql database @a result at row @a row
154 * @param stmt the mysql statement that is being run
155 * @param column the column that is being processed
156 * @param[out] results
158 * #GNUNET_OK if all results could be extracted
159 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
162 pre_extract_fixed_blob (void *cls,
163 struct GNUNET_MY_ResultSpec *rs,
168 results[0].buffer = rs->dst;
169 results[0].buffer_length = rs->dst_size;
170 results[0].length = &rs->mysql_bind_output_length;
171 results[0].buffer_type = MYSQL_TYPE_BLOB;
178 * Check size of extracted fixed size data from a Mysql database @a
179 * result at row @a row
183 * @param stmt the mysql statement that is being run
184 * @param column the column that is being processed
185 * @param[out] results
187 * #GNUNET_OK if all results could be extracted
188 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
191 post_extract_fixed_blob (void *cls,
192 struct GNUNET_MY_ResultSpec *rs,
197 if (rs->dst_size != rs->mysql_bind_output_length)
198 return GNUNET_SYSERR;
204 * Fixed-size result expected.
206 * @param name name of the field in the table
207 * @param[out] dst where to store the result
208 * @param ptr_size number of bytes in @a dst
209 * @return array entry for the result specification to use
211 struct GNUNET_MY_ResultSpec
212 GNUNET_MY_result_spec_fixed_size (void *ptr,
215 struct GNUNET_MY_ResultSpec res =
217 .pre_conv = &pre_extract_fixed_blob,
218 .post_conv = &post_extract_fixed_blob,
220 .dst = (void *)(ptr),
221 .dst_size = ptr_size,
230 * Extract data from a Mysql database @a result at row @a row
234 * @param stmt the mysql statement that is being run
235 * @param column the column that is being processed
236 * @param[out] results
238 * #GNUNET_OK if all results could be extracted
239 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
242 pre_extract_rsa_public_key (void *cls,
243 struct GNUNET_MY_ResultSpec *rs,
248 results[0].buffer = NULL;
249 results[0].buffer_length = 0;
250 results[0].length = &rs->mysql_bind_output_length;
251 results[0].buffer_type = MYSQL_TYPE_BLOB;
258 * Check size of extracted fixed size data from a Mysql database @a
259 * result at row @a row
263 * @param stmt the mysql statement that is being run
264 * @param column the column that is being processed
265 * @param[out] results
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;
301 *pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 "Results contains bogus public key value (fail to decode)\n");
308 return GNUNET_SYSERR;
316 * Function called to clean up memory allocated
317 * by a #GNUNET_MY_ResultConverter.
320 * @param rs result data to clean up
323 clean_rsa_public_key (void *cls,
324 struct GNUNET_MY_ResultSpec *rs)
326 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
330 GNUNET_CRYPTO_rsa_public_key_free (*pk);
337 * RSA public key expected
339 * @param name name of the field in the table
340 * @param[out] rsa where to store the result
341 * @return array entry for the result specification to use
343 struct GNUNET_MY_ResultSpec
344 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
346 struct GNUNET_MY_ResultSpec res = {
347 .pre_conv = &pre_extract_rsa_public_key,
348 .post_conv = &post_extract_rsa_public_key,
349 .cleaner = &clean_rsa_public_key,
360 * Extract data from a Mysql database @a result at row @a row.
364 * @param stmt the mysql statement that is being run
365 * @param column the column that is being processed
366 * @param[out] results
368 * #GNUNET_OK if all results could be extracted
369 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
372 pre_extract_rsa_signature (void *cls,
373 struct GNUNET_MY_ResultSpec *rs,
378 results[0].buffer = 0;
379 results[0].buffer_length = 0;
380 results[0].length = &rs->mysql_bind_output_length;
381 results[0].buffer_type = MYSQL_TYPE_BLOB;
388 * Extract data from a Mysql database @a result at row @a row.
392 * @param stmt the mysql statement that is being run
393 * @param column the column that is being processed
394 * @param[out] results
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,
486 * Extract data from a Mysql database @a result at row @a row
490 * @param stmt the mysql statement that is being run
491 * @param column the column that is being processed
492 * @param[out] results
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 = NULL;
505 results[0].buffer_length = 0;
506 results[0].length = &rs->mysql_bind_output_length;
507 results[0].buffer_type = MYSQL_TYPE_BLOB;
514 * Check size of extracted fixed size data from a Mysql database @a
518 * @param stmt the mysql statement that is being run
519 * @param column the column that is being processed
520 * @param[out] results
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 size_t size = (size_t) rs->mysql_bind_output_length;
535 if (rs->mysql_bind_output_length != size)
536 return GNUNET_SYSERR;
538 buf = GNUNET_malloc (size);
539 results[0].buffer = buf;
540 results[0].buffer_length = size;
541 results[0].buffer_type = MYSQL_TYPE_BLOB;
544 mysql_stmt_fetch_column (stmt,
550 return GNUNET_SYSERR;
558 * 0- terminated string exprected.
560 * @param[out] dst where to store the result, allocated
561 * @return array entry for the result specification to use
563 struct GNUNET_MY_ResultSpec
564 GNUNET_MY_result_spec_string (char **dst)
566 struct GNUNET_MY_ResultSpec res = {
567 .pre_conv = &pre_extract_string,
568 .post_conv = &post_extract_string,
579 * Absolute time expected
581 * @param name name of the field in the table
582 * @param[out] at where to store the result
583 * @return array entry for the result specification to use
585 struct GNUNET_MY_ResultSpec
586 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
588 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
593 * Absolute time in network byte order expected
595 * @param[out] at where to store the result
596 * @return array entry for the result specification to use
598 struct GNUNET_MY_ResultSpec
599 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
601 struct GNUNET_MY_ResultSpec res =
602 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
608 * Extract data from a Postgres database @a result at row @a row.
612 * @param stmt the mysql statement that is being run
613 * @param column the column that is being processed
614 * @param[out] results
616 * #GNUNET_YES if all results could be extracted
617 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
620 pre_extract_uint16 (void *cls,
621 struct GNUNET_MY_ResultSpec *rs,
626 results[0].buffer = rs->dst;
627 results[0].buffer_length = rs->dst_size;
628 results[0].length = &rs->mysql_bind_output_length;
629 results[0].buffer_type = MYSQL_TYPE_SHORT;
636 * Check size of extracted fixed size data from a Mysql datbase.
640 * @param stmt the mysql statement that is being run
641 * @param column the column that is being processed
642 * @param[out] results
644 * #GNUNET_YES if all results could be extracted
645 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
648 post_extract_uint16 (void *cls,
649 struct GNUNET_MY_ResultSpec *rs,
654 if (rs->dst_size != rs->mysql_bind_output_length)
655 return GNUNET_SYSERR;
663 * @param[out] u16 where to store the result
664 * @return array entry for the result specification to use
666 struct GNUNET_MY_ResultSpec
667 GNUNET_MY_result_spec_uint16 (uint16_t *u16)
669 struct GNUNET_MY_ResultSpec res = {
670 .pre_conv = &pre_extract_uint16,
671 .post_conv = &post_extract_uint16,
674 .dst_size = sizeof (*u16),
682 * Extrac data from a MYSQL database @a result at row @a row
687 * @param stmt the mysql statement that is being run
688 * @param column the column that is being processed
689 * @param[out] results
691 * #GNUNET_OK if all results could be extracted
692 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
695 pre_extract_uint32 (void *cls,
696 struct GNUNET_MY_ResultSpec *rs,
701 results[0].buffer = rs->dst;
702 results[0].buffer_length = rs->dst_size;
703 results[0].length = &rs->mysql_bind_output_length;
704 results[0].buffer_type = MYSQL_TYPE_LONG;
711 * Extrac data from a MYSQL database @a result at row @a row
716 * @param stmt the mysql statement that is being run
717 * @param column the column that is being processed
718 * @param[out] results
720 * #GNUNET_OK if all results could be extracted
721 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
724 post_extract_uint32 (void *cls,
725 struct GNUNET_MY_ResultSpec *rs,
730 if (rs->dst_size != rs->mysql_bind_output_length)
731 return GNUNET_SYSERR;
739 * @param[out] u32 where to store the result
740 * @return array entry for the result specification to use
742 struct GNUNET_MY_ResultSpec
743 GNUNET_MY_result_spec_uint32 (uint32_t *u32)
745 struct GNUNET_MY_ResultSpec res = {
746 .pre_conv = &pre_extract_uint32,
747 .post_conv = &post_extract_uint32,
750 .dst_size = sizeof (*u32),
758 * Extract data from a MYSQL database @a result at row @a row
762 * @param stmt the mysql statement that is being run
763 * @param column the column that is being processed
764 * @param[out] results
766 * #GNUNET_OK if all results could be extracted
767 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
770 pre_extract_uint64 (void *cls,
771 struct GNUNET_MY_ResultSpec *rs,
776 results[0].buffer = rs->dst;
777 results[0].buffer_length = rs->dst_size;
778 results[0].length = &rs->mysql_bind_output_length;
779 results[0].buffer_type = MYSQL_TYPE_LONGLONG;
786 * Check size of extracted fixe size data from a Mysql database
790 * @param stmt the mysql statement that is being run
791 * @param column the column that is being processed
792 * @param[out] results
794 * #GNUNET_OK if all results could be extracted
795 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
798 post_extract_uint64 (void *cls,
799 struct GNUNET_MY_ResultSpec *rs,
804 if (rs->dst_size != rs->mysql_bind_output_length)
805 return GNUNET_SYSERR;
813 * @param[out] u64 where to store the result
814 * @return array entry for the result specification to use
816 struct GNUNET_MY_ResultSpec
817 GNUNET_MY_result_spec_uint64 (uint64_t *u64)
819 struct GNUNET_MY_ResultSpec res = {
820 .pre_conv = &pre_extract_uint64,
821 .post_conv = &post_extract_uint64,
824 .dst_size = sizeof (*u64),
831 /* end of pq_result_helper.c */