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_pq_lib.h"
24 * Function called to clean up memory allocated
25 * by a #GNUNET_MY_ResultConverter.
28 * @param rd result data to clean up
31 clean_varsize_blob (void *cls,
45 * extract data from a Mysql database @a result at row @a row
48 * @param result where to extract data from
49 * @param int row to extract data from
50 * @param fname name (or prefix) of the fields to extract from
51 * @param[in, out] dst_size where to store size of result, may be NULL
52 * @param[out] dst where to store the result
54 * #GNUNET_OK if all results could be extracted
55 * #GNUNET_SYSERR if a result was invalid
58 extract_varsize_blob (void *cls,
72 rows = mysql_fetch_row (result);
74 field = mysql_fetch_field (result);
76 //If it's the correct field
79 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
80 "Field '%s' does not exist in result",
87 if (rows[row] == NULL)
96 GNUNET_assert (NULL != res);
99 idst = GNUNET_malloc (len);
100 *((void **) dst) = idst;
109 * Variable-size result expected
111 * @param[out] dst where to store the result, allocated
112 * @param[out] sptr where to store the size of @a dst
113 * @return array entru for the result specification to use
115 struct GNUNET_MY_ResultSpec
116 GNUNET_MY_result_spec_variable_size (void **dst,
119 struct GNUNET_MY_ResultSpec res = {
120 &extract_varsize_blob,
131 * Extract data from a Mysql database @a result at row @a row
134 * @param result where to extract data from
135 * @param int row to extract data from
136 * @param fname name (or prefix) of the fields to extract from
137 * @param[in] dst_size desired size, never NULL
138 * @param[out] dst where to store the result
140 * #GNUNET_OK if all results could be extracted
141 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
145 extracted_fixed_blob (void *cls,
158 rows = mysql_fetch_row (result);
160 field = mysql_fetch_field (result);
162 //If it's the correct field
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
166 "Field '%s' does not exist in result",
169 return GNUNET_SYSERR;
173 if (rows[row] == NULL)
175 return GNUNET_SYSERR;
181 if (*dst_size != len)
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
184 "Field '%s' has wrong size (got %u, expected %u)\n",
187 (unsigned int) *dst_size);
188 return GNUNET_SYSERR;
191 GNUNET_assert (NULL != res);
200 * Fixed-size result expected.
202 * @param name name of the field in the table
203 * @param[out] dst where to store the result
204 * @param dst_size number of bytes in @a dst
205 * @return array entry for the result specification to use
207 struct GNUNET_MY_ResultSpec
208 GNUNET_MY_result_spec_fixed_size (void *ptr,
211 struct GNUNET_MY_ResultSpec res = {
223 * Extract data from a Mysql database @a result at row @a row
226 * @param result where to extract data from
227 * @param int row to extract data from
228 * @param fname name (or prefix) of the fields to extract from
229 * @param[in, out] dst_size where to store size of result, may be NULL
230 * @param[out] dst where to store the result
232 * #GNUNET_OK if all results could be extracted
233 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
236 extract_rsa_public_key (void *cls,
243 struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
252 rows = mysql_fetch_row (result);
254 field = mysql_fetch_field (result);
256 //If it's the correct field
259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260 "Field '%s' does not exist in result",
262 return GNUNET_SYSERR;
266 if (rows[row] == NULL)
268 return GNUNET_SYSERR;
275 *pk = GNUNET_CRYPTO_rsa_public_key_decode (res,
280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
281 "Field '%s' contains bogus value (fails to decode\n",
283 return GNUNET_SYSERR;
289 * Function called to clean up memory allocated
290 * by a #GNUNET_MY_ResultConverter
293 * @param rd result data to clean up
296 clean_rsa_public_key (void *cls,
299 struct GNUNET_CRYPTO_RsaPublicKey **pk = rd;
302 GNUNET_CRYPTO_rsa_public_key_free (*pk);
309 * RSA public key expected
311 * @param name name of the field in the table
312 * @param[out] rsa where to store the result
313 * @return array entry for the result specification to use
315 struct GNUNET_MY_ResultSpec
316 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
318 struct GNUNET_MY_ResultSpec res = {
319 &extract_rsa_public_key,
330 * Extract data from a Mysql database @a result at row @a row.
333 * @param result where to extract data from
334 * @param int row to extract data from
335 * @param fname name (or prefix) of the fields to extract from
336 * @param[in,out] dst_size where to store size of result, may be NULL
337 * @param[out] dst where to store the result
339 * #GNUNET_OK if all results could be extracted
340 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
343 extract_rsa_signature (void *cls,
345 int row, const char *fname,
349 struct GNUNET_CRYPTO_RsaSignature **sig = dst;
359 rows = mysql_fetch_row (result);
361 field = mysql_fetch_field (result);
363 //If it's the correct field
366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
367 "Field '%s' does not exist in result",
369 return GNUNET_SYSERR;
373 if (rows[row] == NULL)
375 return GNUNET_SYSERR;
381 *sig = GNUNET_CRYPTO_rsa_signature_decode (res,
386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
387 "Field '%s' contains bogus value (fails to decode)\n",
389 return GNUNET_SYSERR;
396 * RSA signature expected.
398 * @param[out] sig where to store the result;
399 * @return array entry for the result specification to use
401 struct GNUNET_MY_ResultSpec
402 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
404 struct GNUNET_MY_ResultSpec res =
406 &extract_rsa_signature,
416 * Extract data from a Mysql database @a result at row @a row
419 * @param result where to extract data from
420 * @param int row to extract data from
421 * @param fname name (or prefix) of the fields to extract from
422 * @param[in, out] dst_size where to store size of result, may be NULL
423 * @param[out] dst where to store the result
425 * #GNUNET_OK if all results could be extracted
426 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
429 extract_string (void * cls,
445 rows = mysql_fetch_row (result);
447 field = mysql_fetch_field (result);
449 //If it's the correct field
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
453 "Field '%s' does not exist in result",
455 return GNUNET_SYSERR;
459 if (rows[row] == NULL)
461 return GNUNET_SYSERR;
467 *str = GNUNET_strndup (res,
472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
473 "Field '%s' contains bogus value (fails to decode) \n",
475 return GNUNET_SYSERR;
480 * 0- terminated string exprected.
482 * @param[out] dst where to store the result, allocated
483 * @return array entry for the result specification to use
485 struct GNUNET_MY_ResultSpec
486 GNUNET_MY_result_spec_string (char **dst)
488 struct GNUNET_MY_ResultSpec res = {
499 * Absolute time expected
501 * @param name name of the field in the table
502 * @param[out] at where to store the result
503 * @return array entry for the result specification to use
505 struct GNUNET_MY_ResultSpec
506 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
508 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
512 * Absolute time in network byte order expected
514 * @param[out] at where to store the result
515 * @return array entry for the result specification to use
517 struct GNUNET_MY_ResultSpec
518 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
520 struct GNUNET_MY_ResultSpec res =
521 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
526 * Extract data from a Postgres database @a result at row @a row.
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_YES if all results could be extracted
536 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
539 extract_uint16 (void *cls,
552 * @param[out] u16 where to store the result
553 * @return array entry for the result specification to use
555 struct GNUNET_MY_ResultSpec
556 GNUNET_MY_result_spec_uint16 (uint16_t *u16)
558 struct GNUNET_MY_ResultSpec res = {
569 /* end of pq_result_helper.c */