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;
46 // results[0].buffer_type = MYSQL_TYPE_BLOB;
53 * extract data from a Mysql database @a result at row @a row
57 * @param stmt the mysql statement that is being run
58 * @param column the column that is being processed
61 * #GNUNET_OK if all results could be extracted
62 * #GNUNET_SYSERR if a result was invalid
65 post_extract_varsize_blob (void *cls,
66 struct GNUNET_MY_ResultSpec *rs,
74 size = (size_t) rs->mysql_bind_output_length;
76 if (rs->mysql_bind_output_length != size)
77 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;
84 fprintf(stderr, "size : %d\n", size);
87 mysql_stmt_fetch_column (stmt,
96 printf("buf : %s\n", (char*)buf);
98 *(void **) rs->dst = buf;
99 *rs->result_size = size;
106 * extract data from a Mysql database @a result at row @a row
112 cleanup_varsize_blob (void *cls,
113 struct GNUNET_MY_ResultSpec *rs)
117 ptr = * (void **) rs->dst;
121 *(void **) rs->dst = NULL;
122 *rs->result_size = 0;
127 * Variable-size result expected
129 * @param[out] dst where to store the result, allocated
130 * @param[out] sptr where to store the size of @a dst
131 * @return array entru for the result specification to use
133 struct GNUNET_MY_ResultSpec
134 GNUNET_MY_result_spec_variable_size (void **dst,
137 struct GNUNET_MY_ResultSpec res =
139 .pre_conv = &pre_extract_varsize_blob,
140 .post_conv = &post_extract_varsize_blob,
141 .cleaner = &cleanup_varsize_blob,
142 .dst = (void *)(dst),
143 .result_size = ptr_size,
152 * Extract data from a Mysql database @a result at row @a row
155 * @param result where to extract data from
156 * @param int row to extract data from
157 * @param fname name (or prefix) of the fields to extract from
158 * @param[in] dst_size desired size, never NULL
159 * @param[out] dst where to store the result
161 * #GNUNET_OK if all results could be extracted
162 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
165 pre_extract_fixed_blob (void *cls,
166 struct GNUNET_MY_ResultSpec *rs,
171 results[0].buffer = rs->dst;
172 results[0].buffer_length = rs->dst_size;
173 results[0].length = &rs->mysql_bind_output_length;
174 results[0].buffer_type = MYSQL_TYPE_BLOB;
181 * Check size of extracted fixed size data from a Mysql database @a
182 * result at row @a row
185 * @param result where to extract data from
186 * @param int row to extract data from
187 * @param fname name (or prefix) of the fields to extract from
188 * @param[in] dst_size desired size, never NULL
189 * @param[out] dst where to store the result
191 * #GNUNET_OK if all results could be extracted
192 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
195 post_extract_fixed_blob (void *cls,
196 struct GNUNET_MY_ResultSpec *rs,
201 if (rs->dst_size != rs->mysql_bind_output_length)
202 return GNUNET_SYSERR;
208 * Fixed-size result expected.
210 * @param name name of the field in the table
211 * @param[out] dst where to store the result
212 * @param dst_size number of bytes in @a dst
213 * @return array entry for the result specification to use
215 struct GNUNET_MY_ResultSpec
216 GNUNET_MY_result_spec_fixed_size (void *ptr,
219 struct GNUNET_MY_ResultSpec res =
221 .pre_conv = &pre_extract_fixed_blob,
222 .post_conv = &post_extract_fixed_blob,
223 .dst = (void *)(ptr),
224 .dst_size = ptr_size,
233 * Extract data from a Mysql database @a result at row @a row
236 * @param result where to extract data from
237 * @param int row to extract data from
238 * @param fname name (or prefix) of the fields to extract from
239 * @param[in, out] dst_size where to store size of result, may be NULL
240 * @param[out] dst where to store the result
242 * #GNUNET_OK if all results could be extracted
243 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
246 pre_extract_rsa_public_key (void *cls,
247 struct GNUNET_MY_ResultSpec *rs,
252 results[0].buffer = NULL;
253 results[0].buffer_length = 0;
254 results[0].length = &rs->mysql_bind_output_length;
255 results[0].buffer_type = MYSQL_TYPE_BLOB;
262 * Check size of extracted fixed size data from a Mysql database @a
263 * result at row @a row
266 * @param result where to extract data from
267 * @param int row to extract data from
268 * @param fname name (or prefix) of the fields to extract from
269 * @param[in, out] dst_size where to store size of result, may be NULL
270 * @param[out] dst where to store the result
272 * #GNUNET_OK if all results could be extracted
273 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
276 post_extract_rsa_public_key (void *cls,
277 struct GNUNET_MY_ResultSpec *rs,
283 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
287 size = (size_t) rs->mysql_bind_output_length;
289 if (rs->mysql_bind_output_length != size)
290 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
291 buf = GNUNET_malloc (size);
293 results[0].buffer = buf;
294 results[0].buffer_length = size;
295 results[0].buffer_type = MYSQL_TYPE_BLOB;
297 mysql_stmt_fetch_column (stmt,
303 return GNUNET_SYSERR;
305 *pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
311 "Results contains bogus public key value (fail to decode)\n");
312 return GNUNET_SYSERR;
320 * Function called to clean up memory allocated
321 * by a #GNUNET_MY_ResultConverter.
324 * @param rd result data to clean up
327 clean_rsa_public_key (void *cls,
328 struct GNUNET_MY_ResultSpec *rs)
330 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
334 GNUNET_CRYPTO_rsa_public_key_free (*pk);
341 * RSA public key expected
343 * @param name name of the field in the table
344 * @param[out] rsa where to store the result
345 * @return array entry for the result specification to use
347 struct GNUNET_MY_ResultSpec
348 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
350 struct GNUNET_MY_ResultSpec res = {
351 .pre_conv = &pre_extract_rsa_public_key,
352 .post_conv = &post_extract_rsa_public_key,
353 .cleaner = &clean_rsa_public_key,
363 * Extract data from a Mysql database @a result at row @a row.
366 * @param result where to extract data from
367 * @param int row to extract data from
368 * @param fname name (or prefix) of the fields to extract from
369 * @param[in,out] dst_size where to store size of result, may be NULL
370 * @param[out] dst where to store the result
372 * #GNUNET_OK if all results could be extracted
373 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
376 pre_extract_rsa_signature (void *cls,
377 struct GNUNET_MY_ResultSpec *rs,
382 results[0].buffer = 0;
383 results[0].buffer_length = 0;
384 results[0].length = &rs->mysql_bind_output_length;
385 results[0].buffer_type = MYSQL_TYPE_BLOB;
392 * Extract data from a Mysql database @a result at row @a row.
395 * @param result where to extract data from
396 * @param int row to extract data from
397 * @param fname name (or prefix) of the fields to extract from
398 * @param[in,out] dst_size where to store size of result, may be NULL
399 * @param[out] dst where to store the result
401 * #GNUNET_OK if all results could be extracted
402 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
405 post_extract_rsa_signature (void *cls,
406 struct GNUNET_MY_ResultSpec *rs,
411 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
415 size = (size_t) rs->mysql_bind_output_length;
417 if (rs->mysql_bind_output_length != size)
418 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
419 buf = GNUNET_malloc (size);
421 results[0].buffer = buf;
422 results[0].buffer_length = size;
423 results[0].buffer_type = MYSQL_TYPE_BLOB;
425 mysql_stmt_fetch_column (stmt,
431 return GNUNET_SYSERR;
434 *sig = GNUNET_CRYPTO_rsa_signature_decode (buf,
439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
440 "Resuls contains bogus signature value (fails to decode)\n");
441 return GNUNET_SYSERR;
448 * Function called to clean up memory allocated
449 * by a #GNUNET_MY_ResultConverter.
452 * @param rd result data to clean up
455 clean_rsa_signature (void *cls,
456 struct GNUNET_MY_ResultSpec *rs)
458 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
462 GNUNET_CRYPTO_rsa_signature_free (*sig);
469 * RSA signature expected.
471 * @param[out] sig where to store the result;
472 * @return array entry for the result specification to use
474 struct GNUNET_MY_ResultSpec
475 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
477 struct GNUNET_MY_ResultSpec res =
479 .pre_conv = &pre_extract_rsa_signature,
480 .post_conv = &post_extract_rsa_signature,
481 .cleaner = &clean_rsa_signature,
490 * Extract data from a Mysql database @a result at row @a row
493 * @param result where to extract data from
494 * @param int row to extract data from
495 * @param fname name (or prefix) of the fields to extract from
496 * @param[in, out] dst_size where to store size of result, may be NULL
497 * @param[out] dst where to store the result
499 * #GNUNET_OK if all results could be extracted
500 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
503 pre_extract_string (void * cls,
504 struct GNUNET_MY_ResultSpec *rs,
509 results[0].buffer = (char *)rs->dst;
510 results[0].buffer_length = rs->dst_size;
511 results[0].length = &rs->mysql_bind_output_length;
513 char **str = rs->dst;
519 if (results->is_null)
521 return GNUNET_SYSERR;
524 len = results->buffer_length;
525 res = results->buffer;
527 *str = GNUNET_strndup (res,
532 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
533 "Results contains bogus value (fail to decode)\n");
534 return GNUNET_SYSERR;
541 * Check size of extracted fixed size data from a Mysql database @a
544 * @param result where to extract data from
545 * @param int row to extract data from
546 * @param fname name (or prefix) of the fields to extract from
547 * @param[in, out] dst_size where to store size of result, may be NULL
548 * @param[out] dst where to store the result
550 * #GNUNET_OK if all results could be extracted
551 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
554 post_extract_string (void * cls,
555 struct GNUNET_MY_ResultSpec *rs,
560 if (rs->dst_size != rs->mysql_bind_output_length)
561 return GNUNET_SYSERR;
564 char **str = rs->dst;
570 if (results->is_null)
572 return GNUNET_SYSERR;
575 len = results->buffer_length;
576 res = results->buffer;
578 *str = GNUNET_strndup (res,
583 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
584 "Results contains bogus value (fail to decode)\n");
585 return GNUNET_SYSERR;
593 * 0- terminated string exprected.
595 * @param[out] dst where to store the result, allocated
596 * @return array entry for the result specification to use
598 struct GNUNET_MY_ResultSpec
599 GNUNET_MY_result_spec_string (char **dst)
601 struct GNUNET_MY_ResultSpec res = {
602 .pre_conv = &pre_extract_string,
603 .post_conv = &post_extract_string,
613 * Absolute time expected
615 * @param name name of the field in the table
616 * @param[out] at where to store the result
617 * @return array entry for the result specification to use
619 struct GNUNET_MY_ResultSpec
620 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
622 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
627 * Absolute time in network byte order expected
629 * @param[out] at where to store the result
630 * @return array entry for the result specification to use
632 struct GNUNET_MY_ResultSpec
633 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
635 struct GNUNET_MY_ResultSpec res =
636 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
642 * Extract data from a Postgres database @a result at row @a row.
645 * @param result where to extract data from
646 * @param int row to extract data from
647 * @param fname name (or prefix) of the fields to extract from
648 * @param[in,out] dst_size where to store size of result, may be NULL
649 * @param[out] dst where to store the result
651 * #GNUNET_YES if all results could be extracted
652 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
655 pre_extract_uint16 (void *cls,
656 struct GNUNET_MY_ResultSpec *rs,
661 results[0].buffer = (char *)rs->dst;
662 results[0].buffer_length = rs->dst_size;
663 results[0].length = &rs->mysql_bind_output_length;
664 results[0].buffer_type = MYSQL_TYPE_SHORT;
671 * Check size of extracted fixed size data from a Mysql datbase.
674 * @param result where to extract data from
675 * @param int row to extract data from
676 * @param fname name (or prefix) of the fields to extract from
677 * @param[in,out] dst_size where to store size of result, may be NULL
678 * @param[out] dst where to store the result
680 * #GNUNET_YES if all results could be extracted
681 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
684 post_extract_uint16 (void *cls,
685 struct GNUNET_MY_ResultSpec *rs,
690 if (rs->dst_size != rs->mysql_bind_output_length)
691 return GNUNET_SYSERR;
699 * @param[out] u16 where to store the result
700 * @return array entry for the result specification to use
702 struct GNUNET_MY_ResultSpec
703 GNUNET_MY_result_spec_uint16 (uint16_t *u16)
705 struct GNUNET_MY_ResultSpec res = {
706 .pre_conv = &pre_extract_uint16,
707 .post_conv = &post_extract_uint16,
709 .dst_size = sizeof (*u16),
716 * Extrac data from a MYSQL database @a result at row @a row
719 * @param result where to extract data from
720 * @param int row to extract data from
721 * @param fname name (or prefix) of the fields to extract from
722 * @param[in, out] dst_size where to store size of result, may be NULL
723 * @param[out] dst where to store the result
725 * #GNUNET_OK if all results could be extracted
726 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
729 pre_extract_uint32 (void *cls,
730 struct GNUNET_MY_ResultSpec *rs,
735 results[0].buffer = (int *)rs->dst;
736 results[0].buffer_length = rs->dst_size;
737 results[0].length = &rs->mysql_bind_output_length;
738 results[0].buffer_type = MYSQL_TYPE_LONG;
745 * Extrac data from a MYSQL database @a result at row @a row
748 * @param result where to extract data from
749 * @param int row to extract data from
750 * @param fname name (or prefix) of the fields to extract from
751 * @param[in, out] dst_size where to store size of result, may be NULL
752 * @param[out] dst where to store the result
754 * #GNUNET_OK if all results could be extracted
755 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
758 post_extract_uint32 (void *cls,
759 struct GNUNET_MY_ResultSpec *rs,
764 if (rs->dst_size != rs->mysql_bind_output_length)
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,
783 .dst_size = sizeof (*u32),
791 * Extract data from a MYSQL database @a result at row @a row
794 * @param result where to extract data from
795 * @param int row to extract data from
796 * @param fname name (or prefix) of the fields to extract from
797 * @param[in, out] dst_size where to store size of result, may be null
798 * @param[out] dst where to store the result
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 results[0].buffer = rs->dst;
811 results[0].buffer_length = rs->dst_size;
812 results[0].length = &rs->mysql_bind_output_length;
813 results[0].buffer_type = MYSQL_TYPE_LONGLONG;
821 * Check size of extracted fixe size data from a Mysql database
824 * @param result where to extract data from
825 * @param int row to extract data from
826 * @param fname name (or prefix) of the fields to extract from
827 * @param[in, out] dst_size where to store size of result, may be null
828 * @param[out] dst where to store the result
830 * #GNUNET_OK if all results could be extracted
831 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
834 post_extract_uint64 (void *cls,
835 struct GNUNET_MY_ResultSpec *rs,
840 if (rs->dst_size != rs->mysql_bind_output_length)
841 return GNUNET_SYSERR;
849 * @param[out] u64 where to store the result
850 * @return array entry for the result specification to use
852 struct GNUNET_MY_ResultSpec
853 GNUNET_MY_result_spec_uint64 (uint64_t *u64)
855 struct GNUNET_MY_ResultSpec res = {
856 .pre_conv = &pre_extract_uint64,
857 .post_conv = &post_extract_uint64,
859 .dst_size = sizeof (*u64),
865 /* end of pq_result_helper.c */