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
35 extract_varsize_blob (void *cls,
36 struct GNUNET_MY_ResultSpec *rs,
48 len = results->buffer_length;
49 res = results->buffer;
51 GNUNET_assert (NULL != res);
55 idst = GNUNET_malloc (len);
56 *(void **)rs->dst = idst;
66 * Variable-size result expected
68 * @param[out] dst where to store the result, allocated
69 * @param[out] sptr where to store the size of @a dst
70 * @return array entru for the result specification to use
72 struct GNUNET_MY_ResultSpec
73 GNUNET_MY_result_spec_variable_size (void **dst,
76 struct GNUNET_MY_ResultSpec res =
78 &extract_varsize_blob,
89 * Extract data from a Mysql database @a result at row @a row
92 * @param result where to extract data from
93 * @param int row to extract data from
94 * @param fname name (or prefix) of the fields to extract from
95 * @param[in] dst_size desired size, never NULL
96 * @param[out] dst where to store the result
98 * #GNUNET_OK if all results could be extracted
99 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
103 extract_fixed_blob (void *cls,
104 struct GNUNET_MY_ResultSpec *rs,
110 if (results->is_null)
112 return GNUNET_SYSERR;
115 len = results->buffer_length;
116 if (rs->dst_size != len)
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119 "Results has wrong size (got %u, expected %u)\n",
121 (unsigned int)rs->dst_size);
122 return GNUNET_SYSERR;
125 res = results->buffer;
127 GNUNET_assert (NULL != res);
135 * Fixed-size result expected.
137 * @param name name of the field in the table
138 * @param[out] dst where to store the result
139 * @param dst_size number of bytes in @a dst
140 * @return array entry for the result specification to use
142 struct GNUNET_MY_ResultSpec
143 GNUNET_MY_result_spec_fixed_size (void *ptr,
146 struct GNUNET_MY_ResultSpec res =
159 * Extract data from a Mysql database @a result at row @a row
162 * @param result where to extract data from
163 * @param int row to extract data from
164 * @param fname name (or prefix) of the fields to extract from
165 * @param[in, out] dst_size where to store size of result, may be NULL
166 * @param[out] dst where to store the result
168 * #GNUNET_OK if all results could be extracted
169 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
172 extract_rsa_public_key (void *cls,
173 struct GNUNET_MY_ResultSpec *rs,
177 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
182 if (results->is_null)
184 return GNUNET_SYSERR;
187 len = results->buffer_length;
188 res = results->buffer;
190 *pk = GNUNET_CRYPTO_rsa_public_key_decode (res,
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196 "Results contains bogus value (fail to decode)\n");
197 return GNUNET_SYSERR;
204 * RSA public key expected
206 * @param name name of the field in the table
207 * @param[out] rsa where to store the result
208 * @return array entry for the result specification to use
210 struct GNUNET_MY_ResultSpec
211 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
213 struct GNUNET_MY_ResultSpec res = {
214 &extract_rsa_public_key,
225 * Extract data from a Mysql database @a result at row @a row.
228 * @param result where to extract data from
229 * @param int row to extract data from
230 * @param fname name (or prefix) of the fields to extract from
231 * @param[in,out] dst_size where to store size of result, may be NULL
232 * @param[out] dst where to store the result
234 * #GNUNET_OK if all results could be extracted
235 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
238 extract_rsa_signature (void *cls,
239 struct GNUNET_MY_ResultSpec *rs,
242 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
246 if (results->is_null)
248 return GNUNET_SYSERR;
251 len = results->buffer_length;
252 res = results->buffer;
254 *sig = GNUNET_CRYPTO_rsa_signature_decode (res,
259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260 "Resuls contains bogus value (fails to decode)\n");
261 return GNUNET_SYSERR;
268 * RSA signature expected.
270 * @param[out] sig where to store the result;
271 * @return array entry for the result specification to use
273 struct GNUNET_MY_ResultSpec
274 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
276 struct GNUNET_MY_ResultSpec res =
278 &extract_rsa_signature,
288 * Extract data from a Mysql database @a result at row @a row
291 * @param result where to extract data from
292 * @param int row to extract data from
293 * @param fname name (or prefix) of the fields to extract from
294 * @param[in, out] dst_size where to store size of result, may be NULL
295 * @param[out] dst where to store the result
297 * #GNUNET_OK if all results could be extracted
298 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
301 extract_string (void * cls,
302 struct GNUNET_MY_ResultSpec *rs,
305 char **str = rs->dst;
311 if (results->is_null)
313 return GNUNET_SYSERR;
316 len = results->buffer_length;
317 res = results->buffer;
319 *str = GNUNET_strndup (res,
324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
325 "Results contains bogus value (fail to decode)\n");
326 return GNUNET_SYSERR;
331 * 0- terminated string exprected.
333 * @param[out] dst where to store the result, allocated
334 * @return array entry for the result specification to use
336 struct GNUNET_MY_ResultSpec
337 GNUNET_MY_result_spec_string (char **dst)
339 struct GNUNET_MY_ResultSpec res = {
350 * Absolute time expected
352 * @param name name of the field in the table
353 * @param[out] at where to store the result
354 * @return array entry for the result specification to use
356 struct GNUNET_MY_ResultSpec
357 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
359 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
363 * Absolute time in network byte order expected
365 * @param[out] at where to store the result
366 * @return array entry for the result specification to use
368 struct GNUNET_MY_ResultSpec
369 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
371 struct GNUNET_MY_ResultSpec res =
372 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
377 * Extract data from a Postgres database @a result at row @a row.
380 * @param result where to extract data from
381 * @param int row to extract data from
382 * @param fname name (or prefix) of the fields to extract from
383 * @param[in,out] dst_size where to store size of result, may be NULL
384 * @param[out] dst where to store the result
386 * #GNUNET_YES if all results could be extracted
387 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
390 extract_uint16 (void *cls,
391 struct GNUNET_MY_ResultSpec *rs,
394 uint16_t *udst = rs->dst;
399 return GNUNET_SYSERR;
402 GNUNET_assert (NULL != rs->dst);
403 if (sizeof (uint16_t) != rs->dst_size)
406 return GNUNET_SYSERR;
409 res = (uint16_t *)results->buffer;
410 *udst = ntohs (*res);
418 * @param[out] u16 where to store the result
419 * @return array entry for the result specification to use
421 struct GNUNET_MY_ResultSpec
422 GNUNET_MY_result_spec_uint16 (uint16_t *u16)
424 struct GNUNET_MY_ResultSpec res = {
435 * Extrac data from a MYSQL database @a result at row @a row
438 * @param result where to extract data from
439 * @param int row to extract data from
440 * @param fname name (or prefix) of the fields to extract from
441 * @param[in, out] dst_size where to store size of result, may be NULL
442 * @param[out] dst where to store the result
444 * #GNUNET_OK if all results could be extracted
445 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
448 extract_uint32 (void *cls,
449 struct GNUNET_MY_ResultSpec *rs,
452 uint32_t *udst = rs->dst;
457 return GNUNET_SYSERR;
460 GNUNET_assert (NULL != rs->dst);
461 if (sizeof (uint32_t) != rs->dst_size)
464 return GNUNET_SYSERR;
467 res = (uint32_t *)results->buffer;
469 *udst = ntohl (*res);
477 * @param[out] u32 where to store the result
478 * @return array entry for the result specification to use
480 struct GNUNET_MY_ResultSpec
481 GNUNET_MY_result_spec_uint32 (uint32_t *u32)
483 struct GNUNET_MY_ResultSpec res = {
494 * Extract data from a MYSQL database @a result at row @a row
497 * @param result where to extract data from
498 * @param int row to extract data from
499 * @param fname name (or prefix) of the fields to extract from
500 * @param[in, out] dst_size where to store size of result, may be null
501 * @param[out] dst where to store the result
503 * #GNUNET_OK if all results could be extracted
504 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
507 extract_uint64 (void *cls,
508 struct GNUNET_MY_ResultSpec *rs,
511 uint64_t *udst = rs->dst;
514 if (results->is_null)
516 return GNUNET_SYSERR;
519 GNUNET_assert (NULL != rs->dst);
520 if (sizeof (uint64_t) != rs->dst_size)
523 return GNUNET_SYSERR;
526 res = (uint64_t *)results->buffer;
527 *udst = GNUNET_ntohll (*res);
536 * @param[out] u64 where to store the result
537 * @return array entry for the result specification to use
539 struct GNUNET_MY_ResultSpec
540 GNUNET_MY_result_spec_uint64 (uint64_t *u64)
542 struct GNUNET_MY_ResultSpec res = {
552 /* end of pq_result_helper.c */