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,
38 results[0].buffer = NULL;
39 results[0].buffer_length = 0;
40 results[0].length = &rs->mysql_bind_output_length;
46 * extract data from a Mysql database @a result at row @a row
50 * @param stmt the mysql statement that is being run
51 * @param column the column that is being processed
54 * #GNUNET_OK if all results could be extracted
55 * #GNUNET_SYSERR if a result was invalid
58 post_extract_varsize_blob (void *cls,
59 struct GNUNET_MY_ResultSpec *rs,
67 size = (size_t) rs->mysql_bind_output_length;
68 if (rs->mysql_bind_output_length != size)
69 return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
70 buf = GNUNET_malloc (size);
71 results[0].buffer = buf;
72 results[0].buffer_length = size;
74 mysql_stmt_fetch_column (stmt,
82 *(void **) rs->dst = buf;
83 *rs->result_size = size;
89 * extract data from a Mysql database @a result at row @a row
95 cleanup_varsize_blob (void *cls,
96 struct GNUNET_MY_ResultSpec *rs)
100 ptr = * (void **) rs->dst;
104 *(void **) rs->dst = NULL;
105 *rs->result_size = 0;
110 * Variable-size result expected
112 * @param[out] dst where to store the result, allocated
113 * @param[out] sptr where to store the size of @a dst
114 * @return array entru for the result specification to use
116 struct GNUNET_MY_ResultSpec
117 GNUNET_MY_result_spec_variable_size (void **dst,
120 struct GNUNET_MY_ResultSpec res =
122 .pre_conv = &pre_extract_varsize_blob,
123 .post_conv = &post_extract_varsize_blob,
124 .cleaner = &cleanup_varsize_blob,
125 .dst = (void *)(dst),
126 .result_size = ptr_size,
135 * Extract data from a Mysql database @a result at row @a row
138 * @param result where to extract data from
139 * @param int row to extract data from
140 * @param fname name (or prefix) of the fields to extract from
141 * @param[in] dst_size desired size, never NULL
142 * @param[out] dst where to store the result
144 * #GNUNET_OK if all results could be extracted
145 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
148 pre_extract_fixed_blob (void *cls,
149 struct GNUNET_MY_ResultSpec *rs,
152 results[0].buffer = rs->dst;
153 results[0].buffer_length = rs->dst_size;
154 results[0].length = &rs->mysql_bind_output_length;
160 * Check size of extracted fixed size data from a Mysql database @a
161 * result at row @a row
164 * @param result where to extract data from
165 * @param int row to extract data from
166 * @param fname name (or prefix) of the fields to extract from
167 * @param[in] dst_size desired size, never NULL
168 * @param[out] dst where to store the result
170 * #GNUNET_OK if all results could be extracted
171 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
174 post_extract_fixed_blob (void *cls,
175 struct GNUNET_MY_ResultSpec *rs,
178 if (rs->dst_size != rs->mysql_bind_output_length)
179 return GNUNET_SYSERR;
185 * Fixed-size result expected.
187 * @param name name of the field in the table
188 * @param[out] dst where to store the result
189 * @param dst_size number of bytes in @a dst
190 * @return array entry for the result specification to use
192 struct GNUNET_MY_ResultSpec
193 GNUNET_MY_result_spec_fixed_size (void *ptr,
196 struct GNUNET_MY_ResultSpec res =
198 .pre_conv = &pre_extract_fixed_blob,
199 .post_conv = &post_extract_fixed_blob,
200 .dst = (void *)(ptr),
201 .dst_size = ptr_size,
209 * Extract data from a Mysql database @a result at row @a row
212 * @param result where to extract data from
213 * @param int row to extract data from
214 * @param fname name (or prefix) of the fields to extract from
215 * @param[in, out] dst_size where to store size of result, may be NULL
216 * @param[out] dst where to store the result
218 * #GNUNET_OK if all results could be extracted
219 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
222 extract_rsa_public_key (void *cls,
223 struct GNUNET_MY_ResultSpec *rs,
227 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
232 if (results->is_null)
234 return GNUNET_SYSERR;
237 len = results->buffer_length;
238 res = results->buffer;
240 *pk = GNUNET_CRYPTO_rsa_public_key_decode (res,
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
246 "Results contains bogus value (fail to decode)\n");
247 return GNUNET_SYSERR;
254 * RSA public key expected
256 * @param name name of the field in the table
257 * @param[out] rsa where to store the result
258 * @return array entry for the result specification to use
260 struct GNUNET_MY_ResultSpec
261 GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
263 struct GNUNET_MY_ResultSpec res = {
264 &extract_rsa_public_key,
276 * Extract data from a Mysql database @a result at row @a row.
279 * @param result where to extract data from
280 * @param int row to extract data from
281 * @param fname name (or prefix) of the fields to extract from
282 * @param[in,out] dst_size where to store size of result, may be NULL
283 * @param[out] dst where to store the result
285 * #GNUNET_OK if all results could be extracted
286 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
289 extract_rsa_signature (void *cls,
290 struct GNUNET_MY_ResultSpec *rs,
293 struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
297 if (results->is_null)
299 return GNUNET_SYSERR;
302 len = results->buffer_length;
303 res = results->buffer;
305 *sig = GNUNET_CRYPTO_rsa_signature_decode (res,
310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
311 "Resuls contains bogus value (fails to decode)\n");
312 return GNUNET_SYSERR;
319 * RSA signature expected.
321 * @param[out] sig where to store the result;
322 * @return array entry for the result specification to use
324 struct GNUNET_MY_ResultSpec
325 GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
327 struct GNUNET_MY_ResultSpec res =
329 &extract_rsa_signature,
340 * Extract data from a Mysql database @a result at row @a row
343 * @param result where to extract data from
344 * @param int row to extract data from
345 * @param fname name (or prefix) of the fields to extract from
346 * @param[in, out] dst_size where to store size of result, may be NULL
347 * @param[out] dst where to store the result
349 * #GNUNET_OK if all results could be extracted
350 * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
353 extract_string (void * cls,
354 struct GNUNET_MY_ResultSpec *rs,
357 char **str = rs->dst;
363 if (results->is_null)
365 return GNUNET_SYSERR;
368 len = results->buffer_length;
369 res = results->buffer;
371 *str = GNUNET_strndup (res,
376 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
377 "Results contains bogus value (fail to decode)\n");
378 return GNUNET_SYSERR;
385 * 0- terminated string exprected.
387 * @param[out] dst where to store the result, allocated
388 * @return array entry for the result specification to use
390 struct GNUNET_MY_ResultSpec
391 GNUNET_MY_result_spec_string (char **dst)
393 struct GNUNET_MY_ResultSpec res = {
406 * Absolute time expected
408 * @param name name of the field in the table
409 * @param[out] at where to store the result
410 * @return array entry for the result specification to use
412 struct GNUNET_MY_ResultSpec
413 GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
415 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
420 * Absolute time in network byte order expected
422 * @param[out] at where to store the result
423 * @return array entry for the result specification to use
425 struct GNUNET_MY_ResultSpec
426 GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
428 struct GNUNET_MY_ResultSpec res =
429 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
435 * Extract data from a Postgres 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_YES if all results could be extracted
445 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
448 extract_uint16 (void *cls,
449 struct GNUNET_MY_ResultSpec *rs,
452 uint16_t *udst = rs->dst;
457 return GNUNET_SYSERR;
460 GNUNET_assert (NULL != rs->dst);
461 if (sizeof (uint16_t) != rs->dst_size)
464 return GNUNET_SYSERR;
467 res = (uint16_t *)results->buffer;
468 *udst = ntohs (*res);
477 * @param[out] u16 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_uint16 (uint16_t *u16)
483 struct GNUNET_MY_ResultSpec res = {
495 * Extrac data from a MYSQL database @a result at row @a row
498 * @param result where to extract data from
499 * @param int row to extract data from
500 * @param fname name (or prefix) of the fields to extract from
501 * @param[in, out] dst_size where to store size of result, may be NULL
502 * @param[out] dst where to store the result
504 * #GNUNET_OK if all results could be extracted
505 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
508 extract_uint32 (void *cls,
509 struct GNUNET_MY_ResultSpec *rs,
512 uint32_t *udst = rs->dst;
517 return GNUNET_SYSERR;
520 GNUNET_assert (NULL != rs->dst);
521 if (sizeof (uint32_t) != rs->dst_size)
524 return GNUNET_SYSERR;
527 res = (uint32_t *)results->buffer;
529 *udst = ntohl (*res);
537 * @param[out] u32 where to store the result
538 * @return array entry for the result specification to use
540 struct GNUNET_MY_ResultSpec
541 GNUNET_MY_result_spec_uint32 (uint32_t *u32)
543 struct GNUNET_MY_ResultSpec res = {
555 * Extract data from a MYSQL database @a result at row @a row
558 * @param result where to extract data from
559 * @param int row to extract data from
560 * @param fname name (or prefix) of the fields to extract from
561 * @param[in, out] dst_size where to store size of result, may be null
562 * @param[out] dst where to store the result
564 * #GNUNET_OK if all results could be extracted
565 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
568 extract_uint64 (void *cls,
569 struct GNUNET_MY_ResultSpec *rs,
572 uint64_t *udst = rs->dst;
575 results[0].buffer = &rs->dst;
576 results[0].buffer_length = 42;
578 GNUNET_assert (NULL != rs->dst);
579 if (sizeof (uint64_t) != rs->dst_size)
582 return GNUNET_SYSERR;
585 res = (uint64_t *)results->buffer;
586 *udst = GNUNET_ntohll (*res);
595 * @param[out] u64 where to store the result
596 * @return array entry for the result specification to use
598 struct GNUNET_MY_ResultSpec
599 GNUNET_MY_result_spec_uint64 (uint64_t *u64)
601 struct GNUNET_MY_ResultSpec res = {
602 .pre_conv = &extract_uint64,
604 .dst_size = sizeof (*u64),
610 /* end of pq_result_helper.c */