aboutsummaryrefslogtreecommitdiff
path: root/src/my/my_result_helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/my/my_result_helper.c')
-rw-r--r--src/my/my_result_helper.c310
1 files changed, 184 insertions, 126 deletions
diff --git a/src/my/my_result_helper.c b/src/my/my_result_helper.c
index a5c39913c..108edb62d 100644
--- a/src/my/my_result_helper.c
+++ b/src/my/my_result_helper.c
@@ -21,116 +21,166 @@
21#include "gnunet_my_lib.h" 21#include "gnunet_my_lib.h"
22 22
23/** 23/**
24 * extract data from a Mysql database @a result at row @a row 24 * extract data from a Mysql database @a result at row @a row
25 * 25 *
26 * @param cls closure 26 * @param cls closure
27 * @param qp data about the query 27 * @param qp data about the query
28 * @param result mysql result 28 * @param result mysql result
29 * @return 29 * @return
30 * #GNUNET_OK if all results could be extracted 30 * #GNUNET_OK if all results could be extracted
31 * #GNUNET_SYSERR if a result was invalid 31 * #GNUNET_SYSERR if a result was invalid
32 */ 32 */
33
34static int 33static int
35extract_varsize_blob (void *cls, 34pre_extract_varsize_blob (void *cls,
36 struct GNUNET_MY_ResultSpec *rs, 35 struct GNUNET_MY_ResultSpec *rs,
37 MYSQL_BIND *results) 36 MYSQL_BIND *results)
38{ 37{
39 size_t len; 38 results[0].buffer = NULL;
40 void *idst; 39 results[0].buffer_length = 0;
41 char * res; 40 results[0].length = &rs->mysql_bind_output_length;
41 return GNUNET_OK;
42}
42 43
43 if (results->is_null) 44
45/**
46 * extract data from a Mysql database @a result at row @a row
47 *
48 * @param cls closure
49 * @param[in,out] rs
50 * @param stmt the mysql statement that is being run
51 * @param column the column that is being processed
52 * @param[out] results
53 * @return
54 * #GNUNET_OK if all results could be extracted
55 * #GNUNET_SYSERR if a result was invalid
56 */
57static int
58post_extract_varsize_blob (void *cls,
59 struct GNUNET_MY_ResultSpec *rs,
60 MYSQL_STMT *stmt,
61 unsigned int column,
62 MYSQL_BIND *results)
63{
64 void *buf;
65 size_t size;
66
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;
73 if (0 !=
74 mysql_stmt_fetch_column (stmt,
75 results,
76 column,
77 0))
44 { 78 {
79 GNUNET_free (buf);
45 return GNUNET_SYSERR; 80 return GNUNET_SYSERR;
46 } 81 }
82 *(void **) rs->dst = buf;
83 *rs->result_size = size;
84 return GNUNET_OK;
85}
47 86
48 len = results->buffer_length;
49 res = results->buffer;
50
51 GNUNET_assert (NULL != res);
52
53 rs->dst_size = len;
54
55 idst = GNUNET_malloc (len);
56 *(void **)rs->dst = idst;
57
58 memcpy (idst,
59 res,
60 len);
61 87
62 return GNUNET_OK; 88/**
89 * extract data from a Mysql database @a result at row @a row
90 *
91 * @param cls closure
92 * @param[in,out] rs
93 */
94static void
95cleanup_varsize_blob (void *cls,
96 struct GNUNET_MY_ResultSpec *rs)
97{
98 void *ptr;
99
100 ptr = * (void **) rs->dst;
101 if (NULL == ptr)
102 return;
103 GNUNET_free (ptr);
104 *(void **) rs->dst = NULL;
105 *rs->result_size = 0;
63} 106}
64 107
108
65/** 109/**
66 * Variable-size result expected 110 * Variable-size result expected
67 * 111 *
68 * @param[out] dst where to store the result, allocated 112 * @param[out] dst where to store the result, allocated
69 * @param[out] sptr where to store the size of @a dst 113 * @param[out] sptr where to store the size of @a dst
70 * @return array entru for the result specification to use 114 * @return array entru for the result specification to use
71 */ 115 */
72struct GNUNET_MY_ResultSpec 116struct GNUNET_MY_ResultSpec
73GNUNET_MY_result_spec_variable_size (void **dst, 117GNUNET_MY_result_spec_variable_size (void **dst,
74 size_t *ptr_size) 118 size_t *ptr_size)
75{ 119{
76 struct GNUNET_MY_ResultSpec res = 120 struct GNUNET_MY_ResultSpec res =
77 { 121 {
78 &extract_varsize_blob, 122 .pre_conv = &pre_extract_varsize_blob,
79 NULL, 123 .post_conv = &post_extract_varsize_blob,
80 (void *)(dst), 124 .cleaner = &cleanup_varsize_blob,
81 0, 125 .dst = (void *)(dst),
82 ptr_size 126 .result_size = ptr_size,
127 .num_fields = 1
83 }; 128 };
84 129
85 return res; 130 return res;
86} 131}
87 132
133
88/** 134/**
89 * Extract data from a Mysql database @a result at row @a row 135 * Extract data from a Mysql database @a result at row @a row
90 * 136 *
91 * @param cls closure 137 * @param cls closure
92 * @param result where to extract data from 138 * @param result where to extract data from
93 * @param int row to extract data from 139 * @param int row to extract data from
94 * @param fname name (or prefix) of the fields to extract from 140 * @param fname name (or prefix) of the fields to extract from
95 * @param[in] dst_size desired size, never NULL 141 * @param[in] dst_size desired size, never NULL
96 * @param[out] dst where to store the result 142 * @param[out] dst where to store the result
97 * @return 143 * @return
98 * #GNUNET_OK if all results could be extracted 144 * #GNUNET_OK if all results could be extracted
99 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL) 145 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
100 * 146 */
101 */
102static int 147static int
103extract_fixed_blob (void *cls, 148pre_extract_fixed_blob (void *cls,
104 struct GNUNET_MY_ResultSpec *rs, 149 struct GNUNET_MY_ResultSpec *rs,
105 MYSQL_BIND *results) 150 MYSQL_BIND *results)
106{ 151{
107 size_t len; 152 results[0].buffer = rs->dst;
108 const char *res; 153 results[0].buffer_length = rs->dst_size;
154 results[0].length = &rs->mysql_bind_output_length;
155 return GNUNET_OK;
156}
109 157
110 if (results->is_null)
111 {
112 return GNUNET_SYSERR;
113 }
114 158
115 len = results->buffer_length; 159/**
116 if (rs->dst_size != len) 160 * Check size of extracted fixed size data from a Mysql database @a
117 { 161 * result at row @a row
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 162 *
119 "Results has wrong size (got %u, expected %u)\n", 163 * @param cls closure
120 (unsigned int)len, 164 * @param result where to extract data from
121 (unsigned int)rs->dst_size); 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
169 * @return
170 * #GNUNET_OK if all results could be extracted
171 * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
172 */
173static int
174post_extract_fixed_blob (void *cls,
175 struct GNUNET_MY_ResultSpec *rs,
176 MYSQL_BIND *results)
177{
178 if (rs->dst_size != rs->mysql_bind_output_length)
122 return GNUNET_SYSERR; 179 return GNUNET_SYSERR;
123 }
124
125 res = results->buffer;
126
127 GNUNET_assert (NULL != res);
128 memcpy (rs->dst,
129 res,
130 len);
131
132 return GNUNET_OK; 180 return GNUNET_OK;
133} 181}
182
183
134/** 184/**
135 * Fixed-size result expected. 185 * Fixed-size result expected.
136 * 186 *
@@ -143,15 +193,15 @@ struct GNUNET_MY_ResultSpec
143GNUNET_MY_result_spec_fixed_size (void *ptr, 193GNUNET_MY_result_spec_fixed_size (void *ptr,
144 size_t ptr_size) 194 size_t ptr_size)
145{ 195{
146 struct GNUNET_MY_ResultSpec res = 196 struct GNUNET_MY_ResultSpec res =
147 { 197 {
148 &extract_fixed_blob, 198 .pre_conv = &pre_extract_fixed_blob,
149 NULL, 199 .post_conv = &post_extract_fixed_blob,
150 (void *)(ptr), 200 .dst = (void *)(ptr),
151 ptr_size, 201 .dst_size = ptr_size,
152 NULL 202 .num_fields = 1
153 }; 203 };
154 204
155 return res; 205 return res;
156} 206}
157 207
@@ -175,7 +225,7 @@ extract_rsa_public_key (void *cls,
175 225
176{ 226{
177 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst; 227 struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
178 228
179 size_t len; 229 size_t len;
180 const char *res; 230 const char *res;
181 231
@@ -215,7 +265,8 @@ GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
215 NULL, 265 NULL,
216 (void *) rsa, 266 (void *) rsa,
217 0, 267 0,
218 NULL 268 NULL,
269 1
219 }; 270 };
220 271
221 return res; 272 return res;
@@ -273,13 +324,14 @@ extract_rsa_signature (void *cls,
273struct GNUNET_MY_ResultSpec 324struct GNUNET_MY_ResultSpec
274GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig) 325GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
275{ 326{
276 struct GNUNET_MY_ResultSpec res = 327 struct GNUNET_MY_ResultSpec res =
277 { 328 {
278 &extract_rsa_signature, 329 &extract_rsa_signature,
279 NULL, 330 NULL,
280 (void *)sig, 331 (void *)sig,
281 0, 332 0,
282 NULL 333 NULL,
334 1
283 }; 335 };
284 return res; 336 return res;
285} 337}
@@ -326,13 +378,15 @@ extract_string (void * cls,
326 return GNUNET_SYSERR; 378 return GNUNET_SYSERR;
327 } 379 }
328 return GNUNET_OK; 380 return GNUNET_OK;
329} 381}
382
383
330/** 384/**
331 * 0- terminated string exprected. 385 * 0- terminated string exprected.
332 * 386 *
333 * @param[out] dst where to store the result, allocated 387 * @param[out] dst where to store the result, allocated
334 * @return array entry for the result specification to use 388 * @return array entry for the result specification to use
335 */ 389 */
336struct GNUNET_MY_ResultSpec 390struct GNUNET_MY_ResultSpec
337GNUNET_MY_result_spec_string (char **dst) 391GNUNET_MY_result_spec_string (char **dst)
338{ 392{
@@ -341,17 +395,19 @@ GNUNET_MY_result_spec_string (char **dst)
341 NULL, 395 NULL,
342 (void *) dst, 396 (void *) dst,
343 0, 397 0,
344 NULL 398 NULL,
399 1
345 }; 400 };
346 return res; 401 return res;
347} 402}
348 403
404
349/** 405/**
350 * Absolute time expected 406 * Absolute time expected
351 * 407 *
352 * @param name name of the field in the table 408 * @param name name of the field in the table
353 * @param[out] at where to store the result 409 * @param[out] at where to store the result
354 * @return array entry for the result specification to use 410 * @return array entry for the result specification to use
355 */ 411 */
356struct GNUNET_MY_ResultSpec 412struct GNUNET_MY_ResultSpec
357GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at) 413GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
@@ -359,6 +415,7 @@ GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
359 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us); 415 return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
360} 416}
361 417
418
362/** 419/**
363 * Absolute time in network byte order expected 420 * Absolute time in network byte order expected
364 * 421 *
@@ -368,11 +425,12 @@ GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
368struct GNUNET_MY_ResultSpec 425struct GNUNET_MY_ResultSpec
369GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at) 426GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
370{ 427{
371 struct GNUNET_MY_ResultSpec res = 428 struct GNUNET_MY_ResultSpec res =
372 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__); 429 GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
373 return res; 430 return res;
374} 431}
375 432
433
376/** 434/**
377 * Extract data from a Postgres database @a result at row @a row. 435 * Extract data from a Postgres database @a result at row @a row.
378 * 436 *
@@ -390,7 +448,7 @@ static int
390extract_uint16 (void *cls, 448extract_uint16 (void *cls,
391 struct GNUNET_MY_ResultSpec *rs, 449 struct GNUNET_MY_ResultSpec *rs,
392 MYSQL_BIND *results) 450 MYSQL_BIND *results)
393{ 451{
394 uint16_t *udst = rs->dst; 452 uint16_t *udst = rs->dst;
395 const uint16_t *res; 453 const uint16_t *res;
396 454
@@ -412,12 +470,13 @@ extract_uint16 (void *cls,
412 return GNUNET_OK; 470 return GNUNET_OK;
413} 471}
414 472
473
415/** 474/**
416 * uint16_t expected 475 * uint16_t expected
417 * 476 *
418 * @param[out] u16 where to store the result 477 * @param[out] u16 where to store the result
419 * @return array entry for the result specification to use 478 * @return array entry for the result specification to use
420 */ 479 */
421struct GNUNET_MY_ResultSpec 480struct GNUNET_MY_ResultSpec
422GNUNET_MY_result_spec_uint16 (uint16_t *u16) 481GNUNET_MY_result_spec_uint16 (uint16_t *u16)
423{ 482{
@@ -426,7 +485,8 @@ GNUNET_MY_result_spec_uint16 (uint16_t *u16)
426 NULL, 485 NULL,
427 (void *) u16, 486 (void *) u16,
428 sizeof (*u16), 487 sizeof (*u16),
429 NULL 488 NULL,
489 1
430 }; 490 };
431 return res; 491 return res;
432} 492}
@@ -467,7 +527,7 @@ extract_uint32 (void *cls,
467 res = (uint32_t *)results->buffer; 527 res = (uint32_t *)results->buffer;
468 528
469 *udst = ntohl (*res); 529 *udst = ntohl (*res);
470 530
471 return GNUNET_OK; 531 return GNUNET_OK;
472} 532}
473 533
@@ -485,7 +545,8 @@ GNUNET_MY_result_spec_uint32 (uint32_t *u32)
485 NULL, 545 NULL,
486 (void *) u32, 546 (void *) u32,
487 sizeof (*u32), 547 sizeof (*u32),
488 NULL 548 NULL,
549 1
489 }; 550 };
490 return res; 551 return res;
491} 552}
@@ -511,10 +572,8 @@ extract_uint64 (void *cls,
511 uint64_t *udst = rs->dst; 572 uint64_t *udst = rs->dst;
512 const uint64_t *res; 573 const uint64_t *res;
513 574
514 if (results->is_null) 575 results[0].buffer = &rs->dst;
515 { 576 results[0].buffer_length = 42;
516 return GNUNET_SYSERR;
517 }
518 577
519 GNUNET_assert (NULL != rs->dst); 578 GNUNET_assert (NULL != rs->dst);
520 if (sizeof (uint64_t) != rs->dst_size) 579 if (sizeof (uint64_t) != rs->dst_size)
@@ -540,13 +599,12 @@ struct GNUNET_MY_ResultSpec
540GNUNET_MY_result_spec_uint64 (uint64_t *u64) 599GNUNET_MY_result_spec_uint64 (uint64_t *u64)
541{ 600{
542 struct GNUNET_MY_ResultSpec res = { 601 struct GNUNET_MY_ResultSpec res = {
543 &extract_uint64, 602 .pre_conv = &extract_uint64,
544 NULL, 603 .dst = (void *) u64,
545 (void *) u64, 604 .dst_size = sizeof (*u64),
546 sizeof (*u64), 605 .num_fields = 1
547 NULL
548 }; 606 };
549 return res; 607 return res;
550} 608}
551 609
552/* end of pq_result_helper.c */ \ No newline at end of file 610/* end of pq_result_helper.c */