2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4 GNUnet is free software; you can redistribute it and/or modify it under the
5 terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 3, or (at your option) any later version.
7 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
8 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
9 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
10 You should have received a copy of the GNU General Public License along with
11 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
14 * @file my/my_result_helper.c
15 * @brief functions to extract result values
16 * @author Christophe Genevey
20 #include "gnunet_util_lib.h"
21 #include "gnunet_my_lib.h"
24 * extract data from a Mysql database @a result at row @a row
27 * @param qp data about the query
28 * @param result mysql result
30 * #GNUNET_OK if all results could be extracted
31 * #GNUNET_SYSERR if a result was invalid
34 pre_extract_varsize_blob (void *cls,
35 struct GNUNET_MY_ResultSpec *rs,
40 results[0].buffer = NULL;
41 results[0].buffer_length = 0;
42 results[0].length = &rs->mysql_bind_output_length;
43 // results[0].buffer_type = MYSQL_TYPE_BLOB;
50 * extract data from a Mysql database @a result at row @a row
54 * @param stmt the mysql statement that is being run
55 * @param column the column that is being processed
58 * #GNUNET_OK if all results could be extracted
59 * #GNUNET_SYSERR if a result was invalid
62 post_extract_varsize_blob (void *cls,
63 struct GNUNET_MY_ResultSpec *rs,
71 size = (size_t) rs->mysql_bind_output_length;
73 if (rs->mysql_bind_output_length != size)
74 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
75 buf = GNUNET_malloc (size);
77 results[0].buffer = buf;
78 results[0].buffer_length = size;
79 results[0].buffer_type = MYSQL_TYPE_BLOB;
81 fprintf(stderr, "size : %d\n", size);
84 mysql_stmt_fetch_column (stmt,
93 printf("buf : %s\n", (char*)buf);
95 *(void **) rs->dst = buf;
96 *rs->result_size = size;
103 * extract data from a Mysql database @a result at row @a row
109 cleanup_varsize_blob (void *cls,
110 struct GNUNET_MY_ResultSpec *rs)
114 ptr = * (void **) rs->dst;
118 *(void **) rs->dst = NULL;
119 *rs->result_size = 0;
124 * Variable-size result expected
126 * @param[out] dst where to store the result, allocated
127 * @param[out] sptr where to store the size of @a dst
128 * @return array entru for the result specification to use
130 struct GNUNET_MY_ResultSpec
131 GNUNET_MY_result_spec_variable_size (void **dst,
134 struct GNUNET_MY_ResultSpec res =
136 .pre_conv = &pre_extract_varsize_blob,
137 .post_conv = &post_extract_varsize_blob,
138 .cleaner = &cleanup_varsize_blob,
139 .dst = (void *)(dst),
140 .result_size = ptr_size,
149 * Extract data from a Mysql database @a result at row @a row
152 * @param result where to extract data from
153 * @param int row to extract data from
154 * @param fname name (or prefix) of the fields to extract from
155 * @param[in] dst_size desired size, never NULL
156 * @param[out] dst where to store the result
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
182 * @param result where to extract data from
183 * @param int row to extract data from
184 * @param fname name (or prefix) of the fields to extract from
185 * @param[in] dst_size desired size, never NULL
186 * @param[out] dst where to store the result
188 * #GNUNET_OK if all results could be extracted
189 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
192 post_extract_fixed_blob (void *cls,
193 struct GNUNET_MY_ResultSpec *rs,
198 if (rs->dst_size != rs->mysql_bind_output_length)
199 return GNUNET_SYSERR;
205 * Fixed-size result expected.
207 * @param name name of the field in the table
208 * @param[out] dst where to store the result
209 * @param dst_size number of bytes in @a dst
210 * @return array entry for the result specification to use
212 struct GNUNET_MY_ResultSpec
213 GNUNET_MY_result_spec_fixed_size (void *ptr,
216 struct GNUNET_MY_ResultSpec res =
218 .pre_conv = &pre_extract_fixed_blob,
219 .post_conv = &post_extract_fixed_blob,
220 .dst = (void *)(ptr),
221 .dst_size = ptr_size,
229 * Extract data from a Mysql database @a result at row @a row
232 * @param result where to extract data from
233 * @param int row to extract data from
234 * @param fname name (or prefix) of the fields to extract from
235 * @param[in, out] dst_size where to store size of result, may be NULL
236 * @param[out] dst where to store the result
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,
249 results[0].buffer = NULL;
250 results[0].buffer_length = 0;
251 results[0].length = &rs->mysql_bind_output_length;
252 results[0].buffer_type = MYSQL_TYPE_BLOB;
259 * Check size of extracted fixed size data from a Mysql database @a
260 * result at row @a row
263 * @param result where to extract data from
264 * @param int row to extract data from
265 * @param fname name (or prefix) of the fields to extract from
266 * @param[in, out] dst_size where to store size of result, may be NULL
267 * @param[out] dst where to store the result
269 * #GNUNET_OK if all results could be extracted
270 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
273 post_extract_rsa_public_key (void *cls,
274 struct GNUNET_MY_ResultSpec *rs,
280 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
285 results[0].buffer = res;
286 results[0].buffer_length = size;
289 mysql_stmt_fetch_column (stmt,
294 return GNUNET_SYSERR;
297 *pk = GNUNET_CRYPTO_rsa_public_key_decode (res,
301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
302 "Results contains bogus value (fail to decode)\n");
303 return GNUNET_SYSERR;
311 * Function called to clean up memory allocated
312 * by a #GNUNET_MY_ResultConverter.
315 * @param rd result data to clean up
318 clean_rsa_public_key (void *cls,
319 struct GNUNET_MY_ResultSpec *rs)
321 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
325 GNUNET_CRYPTO_rsa_public_key_free (*pk);
327 *rs->result_size = 0;
333 * RSA public key expected
335 * @param name name of the field in the table
336 * @param[out] rsa where to store the result
337 * @return array entry for the result specification to use
339 struct GNUNET_MY_ResultSpec
340 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
342 struct GNUNET_MY_ResultSpec res = {
343 .pre_conv = &pre_extract_rsa_public_key,
344 .post_conv = &post_extract_rsa_public_key,
345 .cleaner = &clean_rsa_public_key,
355 * Extract data from a Mysql database @a result at row @a row.
358 * @param result where to extract data from
359 * @param int row to extract data from
360 * @param fname name (or prefix) of the fields to extract from
361 * @param[in,out] dst_size where to store size of result, may be NULL
362 * @param[out] dst where to store the result
364 * #GNUNET_OK if all results could be extracted
365 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
368 pre_extract_rsa_signature (void *cls,
369 struct GNUNET_MY_ResultSpec *rs,
374 results[0].buffer = 0;
375 results[0].buffer_length = 0;
376 results[0].length = &rs->mysql_bind_output_length;
377 results[0].buffer_type = MYSQL_TYPE_BLOB;
382 * Extract data from a Mysql database @a result at row @a row.
385 * @param result where to extract data from
386 * @param int row to extract data from
387 * @param fname name (or prefix) of the fields to extract from
388 * @param[in,out] dst_size where to store size of result, may be NULL
389 * @param[out] dst where to store the result
391 * #GNUNET_OK if all results could be extracted
392 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
395 post_extract_rsa_signature (void *cls,
396 struct GNUNET_MY_ResultSpec *rs,
401 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
405 results[0].buffer = res;
406 results[0].buffer_length = size;
409 mysql_stmt_fetch_column (stmt,
414 return GNUNET_SYSERR;
417 *sig = GNUNET_CRYPTO_rsa_signature_decode (res,
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
423 "Resuls contains bogus value (fails to decode)\n");
424 return GNUNET_SYSERR;
432 * Function called to clean up memory allocated
433 * by a #GNUNET_MY_ResultConverter.
436 * @param rd result data to clean up
439 clean_rsa_signature (void *cls,
440 struct GNUNET_MY_ResultSpec *rs)
442 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
446 GNUNET_CRYPTO_rsa_signature_free (*sig);
454 * RSA signature expected.
456 * @param[out] sig where to store the result;
457 * @return array entry for the result specification to use
459 struct GNUNET_MY_ResultSpec
460 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
462 struct GNUNET_MY_ResultSpec res =
464 .pre_conv = &pre_extract_rsa_signature,
465 .post_conv = &post_extract_rsa_signature,
466 .cleaner = &clean_rsa_signature,
475 * Extract data from a Mysql database @a result at row @a row
478 * @param result where to extract data from
479 * @param int row to extract data from
480 * @param fname name (or prefix) of the fields to extract from
481 * @param[in, out] dst_size where to store size of result, may be NULL
482 * @param[out] dst where to store the result
484 * #GNUNET_OK if all results could be extracted
485 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
488 pre_extract_string (void * cls,
489 struct GNUNET_MY_ResultSpec *rs,
494 results[0].buffer = (char *)rs->dst;
495 results[0].buffer_length = rs->dst_size;
496 results[0].length = &rs->mysql_bind_output_length;
498 char **str = rs->dst;
504 if (results->is_null)
506 return GNUNET_SYSERR;
509 len = results->buffer_length;
510 res = results->buffer;
512 *str = GNUNET_strndup (res,
517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
518 "Results contains bogus value (fail to decode)\n");
519 return GNUNET_SYSERR;
526 * Check size of extracted fixed size data from a Mysql database @a
529 * @param result where to extract data from
530 * @param int row to extract data from
531 * @param fname name (or prefix) of the fields to extract from
532 * @param[in, out] dst_size where to store size of result, may be NULL
533 * @param[out] dst where to store the result
535 * #GNUNET_OK if all results could be extracted
536 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
539 post_extract_string (void * cls,
540 struct GNUNET_MY_ResultSpec *rs,
545 if (rs->dst_size != rs->mysql_bind_output_length)
546 return GNUNET_SYSERR;
549 char **str = rs->dst;
555 if (results->is_null)
557 return GNUNET_SYSERR;
560 len = results->buffer_length;
561 res = results->buffer;
563 *str = GNUNET_strndup (res,
568 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
569 "Results contains bogus value (fail to decode)\n");
570 return GNUNET_SYSERR;
578 * 0- terminated string exprected.
580 * @param[out] dst where to store the result, allocated
581 * @return array entry for the result specification to use
583 struct GNUNET_MY_ResultSpec
584 GNUNET_MY_result_spec_string (char **dst)
586 struct GNUNET_MY_ResultSpec res = {
587 .pre_conv = &pre_extract_string,
588 .post_conv = &post_extract_string,
598 * Absolute time expected
600 * @param name name of the field in the table
601 * @param[out] at where to store the result
602 * @return array entry for the result specification to use
604 struct GNUNET_MY_ResultSpec
605 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
607 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
612 * Absolute time in network byte order expected
614 * @param[out] at where to store the result
615 * @return array entry for the result specification to use
617 struct GNUNET_MY_ResultSpec
618 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
620 struct GNUNET_MY_ResultSpec res =
621 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
627 * Extract data from a Postgres database @a result at row @a row.
630 * @param result where to extract data from
631 * @param int row to extract data from
632 * @param fname name (or prefix) of the fields to extract from
633 * @param[in,out] dst_size where to store size of result, may be NULL
634 * @param[out] dst where to store the result
636 * #GNUNET_YES if all results could be extracted
637 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
640 pre_extract_uint16 (void *cls,
641 struct GNUNET_MY_ResultSpec *rs,
646 results[0].buffer = (char *)rs->dst;
647 results[0].buffer_length = rs->dst_size;
648 results[0].length = &rs->mysql_bind_output_length;
649 results[0].buffer_type = MYSQL_TYPE_SHORT;
656 * Check size of extracted fixed size data from a Mysql datbase.
659 * @param result where to extract data from
660 * @param int row to extract data from
661 * @param fname name (or prefix) of the fields to extract from
662 * @param[in,out] dst_size where to store size of result, may be NULL
663 * @param[out] dst where to store the result
665 * #GNUNET_YES if all results could be extracted
666 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
669 post_extract_uint16 (void *cls,
670 struct GNUNET_MY_ResultSpec *rs,
675 if (rs->dst_size != rs->mysql_bind_output_length)
676 return GNUNET_SYSERR;
684 * @param[out] u16 where to store the result
685 * @return array entry for the result specification to use
687 struct GNUNET_MY_ResultSpec
688 GNUNET_MY_result_spec_uint16 (uint16_t *u16)
690 struct GNUNET_MY_ResultSpec res = {
691 .pre_conv = &pre_extract_uint16,
692 .post_conv = &post_extract_uint16,
694 .dst_size = sizeof (*u16),
701 * Extrac data from a MYSQL database @a result at row @a row
704 * @param result where to extract data from
705 * @param int row to extract data from
706 * @param fname name (or prefix) of the fields to extract from
707 * @param[in, out] dst_size where to store size of result, may be NULL
708 * @param[out] dst where to store the result
710 * #GNUNET_OK if all results could be extracted
711 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
714 pre_extract_uint32 (void *cls,
715 struct GNUNET_MY_ResultSpec *rs,
720 results[0].buffer = (int *)rs->dst;
721 results[0].buffer_length = rs->dst_size;
722 results[0].length = &rs->mysql_bind_output_length;
723 results[0].buffer_type = MYSQL_TYPE_LONG;
730 * Extrac data from a MYSQL database @a result at row @a row
733 * @param result where to extract data from
734 * @param int row to extract data from
735 * @param fname name (or prefix) of the fields to extract from
736 * @param[in, out] dst_size where to store size of result, may be NULL
737 * @param[out] dst where to store the result
739 * #GNUNET_OK if all results could be extracted
740 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
743 post_extract_uint32 (void *cls,
744 struct GNUNET_MY_ResultSpec *rs,
749 if (rs->dst_size != rs->mysql_bind_output_length)
750 return GNUNET_SYSERR;
758 * @param[out] u32 where to store the result
759 * @return array entry for the result specification to use
761 struct GNUNET_MY_ResultSpec
762 GNUNET_MY_result_spec_uint32 (uint32_t *u32)
764 struct GNUNET_MY_ResultSpec res = {
765 .pre_conv = &pre_extract_uint32,
766 .post_conv = &post_extract_uint32,
768 .dst_size = sizeof (*u32),
776 * Extract data from a MYSQL database @a result at row @a row
779 * @param result where to extract data from
780 * @param int row to extract data from
781 * @param fname name (or prefix) of the fields to extract from
782 * @param[in, out] dst_size where to store size of result, may be null
783 * @param[out] dst where to store the result
785 * #GNUNET_OK if all results could be extracted
786 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
789 pre_extract_uint64 (void *cls,
790 struct GNUNET_MY_ResultSpec *rs,
795 results[0].buffer = rs->dst;
796 results[0].buffer_length = rs->dst_size;
797 results[0].length = &rs->mysql_bind_output_length;
798 results[0].buffer_type = MYSQL_TYPE_LONGLONG;
806 * Check size of extracted fixe size data from a Mysql database
809 * @param result where to extract data from
810 * @param int row to extract data from
811 * @param fname name (or prefix) of the fields to extract from
812 * @param[in, out] dst_size where to store size of result, may be null
813 * @param[out] dst where to store the result
815 * #GNUNET_OK if all results could be extracted
816 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
819 post_extract_uint64 (void *cls,
820 struct GNUNET_MY_ResultSpec *rs,
825 if (rs->dst_size != rs->mysql_bind_output_length)
826 return GNUNET_SYSERR;
834 * @param[out] u64 where to store the result
835 * @return array entry for the result specification to use
837 struct GNUNET_MY_ResultSpec
838 GNUNET_MY_result_spec_uint64 (uint64_t *u64)
840 struct GNUNET_MY_ResultSpec res = {
841 .pre_conv = &pre_extract_uint64,
842 .post_conv = &post_extract_uint64,
844 .dst_size = sizeof (*u64),
850 /* end of pq_result_helper.c */