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 result where to extract data from
28 * @param int row to extract data from
29 * @param fname name (or prefix) of the fields to extract from
30 * @param[in, out] dst_size where to store size of result, may be NULL
31 * @param[out] dst where to store the result
33 * #GNUNET_OK if all results could be extracted
34 * #GNUNET_SYSERR if a result was invalid
37 extract_varsize_blob (void *cls,
51 rows = mysql_fetch_row (result);
53 field = mysql_fetch_field (result);
55 //If it's the correct field
56 if (field->name != fname)
58 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
59 "Field '%s' does not exist in result",
66 if (rows[row] == NULL)
75 GNUNET_assert (NULL != res);
78 idst = GNUNET_malloc (len);
79 *((void **) dst) = idst;
88 * Variable-size result expected
90 * @param[out] dst where to store the result, allocated
91 * @param[out] sptr where to store the size of @a dst
92 * @return array entru for the result specification to use
94 struct GNUNET_MY_ResultSpec
95 GNUNET_MY_result_spec_variable_size (void **dst,
98 struct GNUNET_MY_ResultSpec res = {
99 &extract_varsize_blob,
110 * Extract data from a Mysql database @a result at row @a row
113 * @param result where to extract data from
114 * @param int row to extract data from
115 * @param fname name (or prefix) of the fields to extract from
116 * @param[in] dst_size desired size, never NULL
117 * @param[out] dst where to store the result
119 * #GNUNET_OK if all results could be extracted
120 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
124 extract_fixed_blob (void *cls,
137 rows = mysql_fetch_row (result);
139 field = mysql_fetch_field (result);
141 //If it's the correct field
142 if (field->name != fname)
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
145 "Field '%s' does not exist in result",
148 return GNUNET_SYSERR;
152 if (rows[row] == NULL)
154 return GNUNET_SYSERR;
160 if (*dst_size != len)
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163 "Field '%s' has wrong size (got %u, expected %u)\n",
166 (unsigned int) *dst_size);
167 return GNUNET_SYSERR;
170 GNUNET_assert (NULL != res);
179 * Fixed-size result expected.
181 * @param name name of the field in the table
182 * @param[out] dst where to store the result
183 * @param dst_size number of bytes in @a dst
184 * @return array entry for the result specification to use
186 struct GNUNET_MY_ResultSpec
187 GNUNET_MY_result_spec_fixed_size (void *ptr,
190 struct GNUNET_MY_ResultSpec res = {
202 * Extract data from a Mysql database @a result at row @a row
205 * @param result where to extract data from
206 * @param int row to extract data from
207 * @param fname name (or prefix) of the fields to extract from
208 * @param[in, out] dst_size where to store size of result, may be NULL
209 * @param[out] dst where to store the result
211 * #GNUNET_OK if all results could be extracted
212 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
215 extract_rsa_public_key (void *cls,
222 struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
231 rows = mysql_fetch_row (result);
233 field = mysql_fetch_field (result);
235 //If it's the correct field
236 if (field->name != fname)
238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
239 "Field '%s' does not exist in result",
241 return GNUNET_SYSERR;
245 if (rows[row] == NULL)
247 return GNUNET_SYSERR;
254 *pk = GNUNET_CRYPTO_rsa_public_key_decode (res,
259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260 "Field '%s' contains bogus value (fails to decode\n",
262 return GNUNET_SYSERR;
268 * RSA public key expected
270 * @param name name of the field in the table
271 * @param[out] rsa where to store the result
272 * @return array entry for the result specification to use
274 struct GNUNET_MY_ResultSpec
275 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
277 struct GNUNET_MY_ResultSpec res = {
278 &extract_rsa_public_key,
289 * Extract data from a Mysql database @a result at row @a row.
292 * @param result where to extract data from
293 * @param int row to extract data from
294 * @param fname name (or prefix) of the fields to extract from
295 * @param[in,out] dst_size where to store size of result, may be NULL
296 * @param[out] dst where to store the result
298 * #GNUNET_OK if all results could be extracted
299 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
302 extract_rsa_signature (void *cls,
304 int row, const char *fname,
308 struct GNUNET_CRYPTO_RsaSignature **sig = dst;
318 rows = mysql_fetch_row (result);
320 field = mysql_fetch_field (result);
322 //If it's the correct field
323 if (field->name == fname)
325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
326 "Field '%s' does not exist in result",
328 return GNUNET_SYSERR;
332 if (rows[row] == NULL)
334 return GNUNET_SYSERR;
340 *sig = GNUNET_CRYPTO_rsa_signature_decode (res,
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346 "Field '%s' contains bogus value (fails to decode)\n",
348 return GNUNET_SYSERR;
355 * RSA signature expected.
357 * @param[out] sig where to store the result;
358 * @return array entry for the result specification to use
360 struct GNUNET_MY_ResultSpec
361 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
363 struct GNUNET_MY_ResultSpec res =
365 &extract_rsa_signature,
375 * Extract data from a Mysql database @a result at row @a row
378 * @param result where to extract data from
379 * @param int row to extract data from
380 * @param fname name (or prefix) of the fields to extract from
381 * @param[in, out] dst_size where to store size of result, may be NULL
382 * @param[out] dst where to store the result
384 * #GNUNET_OK if all results could be extracted
385 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
388 extract_string (void * cls,
404 rows = mysql_fetch_row (result);
406 field = mysql_fetch_field (result);
408 //If it's the correct field
411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
412 "Field '%s' does not exist in result",
414 return GNUNET_SYSERR;
418 if (rows[row] == NULL)
420 return GNUNET_SYSERR;
426 *str = GNUNET_strndup (res,
431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
432 "Field '%s' contains bogus value (fails to decode) \n",
434 return GNUNET_SYSERR;
439 * 0- terminated string exprected.
441 * @param[out] dst where to store the result, allocated
442 * @return array entry for the result specification to use
444 struct GNUNET_MY_ResultSpec
445 GNUNET_MY_result_spec_string (char **dst)
447 struct GNUNET_MY_ResultSpec res = {
458 * Absolute time expected
460 * @param name name of the field in the table
461 * @param[out] at where to store the result
462 * @return array entry for the result specification to use
464 struct GNUNET_MY_ResultSpec
465 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
467 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
471 * Absolute time in network byte order expected
473 * @param[out] at where to store the result
474 * @return array entry for the result specification to use
476 struct GNUNET_MY_ResultSpec
477 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
479 struct GNUNET_MY_ResultSpec res =
480 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
485 * Extract data from a Postgres database @a result at row @a row.
488 * @param result where to extract data from
489 * @param int row to extract data from
490 * @param fname name (or prefix) of the fields to extract from
491 * @param[in,out] dst_size where to store size of result, may be NULL
492 * @param[out] dst where to store the result
494 * #GNUNET_YES if all results could be extracted
495 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
498 extract_uint16 (void *cls,
506 uint16_t *udst = dst;
512 rows = mysql_fetch_row (result);
514 field = mysql_fetch_field (result);
516 //If it's the correct field
517 if (field->name == fname)
519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
520 "Field '%s' does not exist in result",
522 return GNUNET_SYSERR;
526 if (rows[row] == NULL)
528 return GNUNET_SYSERR;
531 GNUNET_assert (NULL != dst);
533 if (sizeof (uint16_t) != *dst_size)
536 return GNUNET_SYSERR;
539 res = (uint16_t) rows[row];
540 *udst = ntohs (*res);
548 * @param[out] u16 where to store the result
549 * @return array entry for the result specification to use
551 struct GNUNET_MY_ResultSpec
552 GNUNET_MY_result_spec_uint16 (uint16_t *u16)
554 struct GNUNET_MY_ResultSpec res = {
565 * Extrac data from a MYSQL database @a result at row @a row
568 * @param result where to extract data from
569 * @param int row to extract data from
570 * @param fname name (or prefix) of the fields to extract from
571 * @param[in, out] dst_size where to store size of result, may be NULL
572 * @param[out] dst where to store the result
574 * #GNUNET_OK if all results could be extracted
575 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
578 extract_uint32 (void *cls,
585 uint32_t *udst = dst;
591 rows = mysql_fetch_row (result);
593 field = mysql_fetch_field (result);
595 //If it's the correct field
596 if (field->name == fname)
598 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
599 "Field '%s' does not exist in result",
601 return GNUNET_SYSERR;
605 if (rows[row] == NULL)
607 return GNUNET_SYSERR;
610 GNUNET_assert (NULL != dst);
612 if (sizeof (uint32_t) != *dst_size)
615 return GNUNET_SYSERR;
618 res = (uint32_t) rows[row];
620 *udst = ntohl (*res);
627 * @param[out] u32 where to store the result
628 * @return array entry for the result specification to use
630 struct GNUNET_MY_ResultSpec
631 GNUNET_MY_result_spec_uint32 (uint32_t *u32)
633 struct GNUNET_MY_ResultSpec res = {
644 * Extract data from a MYSQL database @a result at row @a row
647 * @param result where to extract data from
648 * @param int row to extract data from
649 * @param fname name (or prefix) of the fields to extract from
650 * @param[in, out] dst_size where to store size of result, may be null
651 * @param[out] dst where to store the result
653 * #GNUNET_OK if all results could be extracted
654 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
657 extract_uint64 (void *cls,
664 uint64_t *udst = dst;
670 rows = mysql_fetch_row (result);
672 field = mysql_fetch_field (result);
674 //If it's the correct field
675 if (field->name == fname)
677 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
678 "Field '%s' does not exist in result",
680 return GNUNET_SYSERR;
684 if (rows[row] == NULL)
686 return GNUNET_SYSERR;
689 GNUNET_assert (NULL != dst);
690 if (sizeof (uint64_t) != *dst_size)
693 return GNUNET_SYSERR;
696 res = (uint64_t) rows[row];
697 *udst = GNUNET_ntohll (*res);
706 * @param[out] u64 where to store the result
707 * @return array entry for the result specification to use
709 struct GNUNET_MY_ResultSpec
710 GNUNET_MY_result_spec_uint64 (uint64_t *u64)
712 struct GNUNET_MY_ResultSpec res = {
722 /* end of pq_result_helper.c */