diff options
Diffstat (limited to 'src/my/my_result_helper.c')
-rw-r--r-- | src/my/my_result_helper.c | 332 |
1 files changed, 81 insertions, 251 deletions
diff --git a/src/my/my_result_helper.c b/src/my/my_result_helper.c index cced22482..a5c39913c 100644 --- a/src/my/my_result_helper.c +++ b/src/my/my_result_helper.c | |||
@@ -24,64 +24,42 @@ | |||
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 result where to extract data from | 27 | * @param qp data about the query |
28 | * @param int row to extract data from | 28 | * @param result mysql result |
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 | ||
32 | * @return | 29 | * @return |
33 | * #GNUNET_OK if all results could be extracted | 30 | * #GNUNET_OK if all results could be extracted |
34 | * #GNUNET_SYSERR if a result was invalid | 31 | * #GNUNET_SYSERR if a result was invalid |
35 | */ | 32 | */ |
33 | |||
36 | static int | 34 | static int |
37 | extract_varsize_blob (void *cls, | 35 | extract_varsize_blob (void *cls, |
38 | MYSQL_RES * result, | 36 | struct GNUNET_MY_ResultSpec *rs, |
39 | int row, | 37 | MYSQL_BIND *results) |
40 | const char *fname, | ||
41 | size_t *dst_size, | ||
42 | void *dst) | ||
43 | { | 38 | { |
44 | const char *res; | ||
45 | void *idst; | ||
46 | size_t len; | 39 | size_t len; |
40 | void *idst; | ||
41 | char * res; | ||
47 | 42 | ||
48 | MYSQL_ROW rows; | 43 | if (results->is_null) |
49 | MYSQL_FIELD *field; | ||
50 | |||
51 | rows = mysql_fetch_row (result); | ||
52 | |||
53 | field = mysql_fetch_field (result); | ||
54 | |||
55 | //If it's the correct field | ||
56 | if (field->name != fname) | ||
57 | { | 44 | { |
58 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
59 | "Field '%s' does not exist in result", | ||
60 | fname); | ||
61 | |||
62 | return GNUNET_SYSERR; | 45 | return GNUNET_SYSERR; |
63 | } | 46 | } |
64 | 47 | ||
48 | len = results->buffer_length; | ||
49 | res = results->buffer; | ||
65 | 50 | ||
66 | if (rows[row] == NULL) | 51 | GNUNET_assert (NULL != res); |
67 | { | ||
68 | return GNUNET_SYSERR; | ||
69 | } | ||
70 | |||
71 | res = rows[row]; | ||
72 | 52 | ||
73 | len = strlen(res); | 53 | rs->dst_size = len; |
74 | 54 | ||
75 | GNUNET_assert (NULL != res); | ||
76 | |||
77 | *dst_size = len; | ||
78 | idst = GNUNET_malloc (len); | 55 | idst = GNUNET_malloc (len); |
79 | *((void **) dst) = idst; | 56 | *(void **)rs->dst = idst; |
57 | |||
80 | memcpy (idst, | 58 | memcpy (idst, |
81 | res, | 59 | res, |
82 | len); | 60 | len); |
83 | 61 | ||
84 | return GNUNET_OK; | 62 | return GNUNET_OK; |
85 | } | 63 | } |
86 | 64 | ||
87 | /** | 65 | /** |
@@ -95,7 +73,8 @@ struct GNUNET_MY_ResultSpec | |||
95 | GNUNET_MY_result_spec_variable_size (void **dst, | 73 | GNUNET_MY_result_spec_variable_size (void **dst, |
96 | size_t *ptr_size) | 74 | size_t *ptr_size) |
97 | { | 75 | { |
98 | struct GNUNET_MY_ResultSpec res = { | 76 | struct GNUNET_MY_ResultSpec res = |
77 | { | ||
99 | &extract_varsize_blob, | 78 | &extract_varsize_blob, |
100 | NULL, | 79 | NULL, |
101 | (void *)(dst), | 80 | (void *)(dst), |
@@ -122,57 +101,34 @@ GNUNET_MY_result_spec_variable_size (void **dst, | |||
122 | */ | 101 | */ |
123 | static int | 102 | static int |
124 | extract_fixed_blob (void *cls, | 103 | extract_fixed_blob (void *cls, |
125 | MYSQL_RES * result, | 104 | struct GNUNET_MY_ResultSpec *rs, |
126 | int row, | 105 | MYSQL_BIND *results) |
127 | const char * fname, | ||
128 | size_t * dst_size, | ||
129 | void *dst) | ||
130 | { | 106 | { |
131 | size_t len; | 107 | size_t len; |
132 | const char *res; | 108 | const char *res; |
133 | 109 | ||
134 | MYSQL_ROW rows; | 110 | if (results->is_null) |
135 | MYSQL_FIELD * field; | ||
136 | |||
137 | rows = mysql_fetch_row (result); | ||
138 | |||
139 | field = mysql_fetch_field (result); | ||
140 | |||
141 | //If it's the correct field | ||
142 | if (field->name != fname) | ||
143 | { | ||
144 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
145 | "Field '%s' does not exist in result", | ||
146 | fname); | ||
147 | |||
148 | return GNUNET_SYSERR; | ||
149 | } | ||
150 | |||
151 | |||
152 | if (rows[row] == NULL) | ||
153 | { | 111 | { |
154 | return GNUNET_SYSERR; | 112 | return GNUNET_SYSERR; |
155 | } | 113 | } |
156 | 114 | ||
157 | res = rows[row]; | 115 | len = results->buffer_length; |
158 | 116 | if (rs->dst_size != len) | |
159 | len = strlen (res); | ||
160 | if (*dst_size != len) | ||
161 | { | 117 | { |
162 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 118 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
163 | "Field '%s' has wrong size (got %u, expected %u)\n", | 119 | "Results has wrong size (got %u, expected %u)\n", |
164 | fname, | ||
165 | (unsigned int)len, | 120 | (unsigned int)len, |
166 | (unsigned int) *dst_size); | 121 | (unsigned int)rs->dst_size); |
167 | return GNUNET_SYSERR; | 122 | return GNUNET_SYSERR; |
168 | } | 123 | } |
169 | 124 | ||
125 | res = results->buffer; | ||
126 | |||
170 | GNUNET_assert (NULL != res); | 127 | GNUNET_assert (NULL != res); |
171 | 128 | memcpy (rs->dst, | |
172 | memcpy (dst, | ||
173 | res, | 129 | res, |
174 | len); | 130 | len); |
175 | 131 | ||
176 | return GNUNET_OK; | 132 | return GNUNET_OK; |
177 | } | 133 | } |
178 | /** | 134 | /** |
@@ -187,7 +143,8 @@ struct GNUNET_MY_ResultSpec | |||
187 | GNUNET_MY_result_spec_fixed_size (void *ptr, | 143 | GNUNET_MY_result_spec_fixed_size (void *ptr, |
188 | size_t ptr_size) | 144 | size_t ptr_size) |
189 | { | 145 | { |
190 | struct GNUNET_MY_ResultSpec res = { | 146 | struct GNUNET_MY_ResultSpec res = |
147 | { | ||
191 | &extract_fixed_blob, | 148 | &extract_fixed_blob, |
192 | NULL, | 149 | NULL, |
193 | (void *)(ptr), | 150 | (void *)(ptr), |
@@ -213,54 +170,33 @@ GNUNET_MY_result_spec_fixed_size (void *ptr, | |||
213 | */ | 170 | */ |
214 | static int | 171 | static int |
215 | extract_rsa_public_key (void *cls, | 172 | extract_rsa_public_key (void *cls, |
216 | MYSQL_RES *result, | 173 | struct GNUNET_MY_ResultSpec *rs, |
217 | int row, | 174 | MYSQL_BIND *results) |
218 | const char *fname, | 175 | |
219 | size_t *dst_size, | ||
220 | void *dst) | ||
221 | { | 176 | { |
222 | struct GNUNET_CRYPTO_RsaPublicKey **pk = dst; | 177 | struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst; |
178 | |||
223 | size_t len; | 179 | size_t len; |
224 | const char *res; | 180 | const char *res; |
225 | 181 | ||
226 | MYSQL_ROW rows; | 182 | if (results->is_null) |
227 | MYSQL_FIELD * field; | ||
228 | |||
229 | *pk = NULL; | ||
230 | |||
231 | rows = mysql_fetch_row (result); | ||
232 | |||
233 | field = mysql_fetch_field (result); | ||
234 | |||
235 | //If it's the correct field | ||
236 | if (field->name != fname) | ||
237 | { | ||
238 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
239 | "Field '%s' does not exist in result", | ||
240 | fname); | ||
241 | return GNUNET_SYSERR; | ||
242 | } | ||
243 | |||
244 | |||
245 | if (rows[row] == NULL) | ||
246 | { | 183 | { |
247 | return GNUNET_SYSERR; | 184 | return GNUNET_SYSERR; |
248 | } | 185 | } |
249 | 186 | ||
250 | res = rows[row]; | 187 | len = results->buffer_length; |
188 | res = results->buffer; | ||
251 | 189 | ||
252 | len = strlen (res); | 190 | *pk = GNUNET_CRYPTO_rsa_public_key_decode (res, |
253 | |||
254 | *pk = GNUNET_CRYPTO_rsa_public_key_decode (res, | ||
255 | len); | 191 | len); |
256 | 192 | ||
257 | if (NULL == *pk) | 193 | if (NULL == *pk) |
258 | { | 194 | { |
259 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 195 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
260 | "Field '%s' contains bogus value (fails to decode\n", | 196 | "Results contains bogus value (fail to decode)\n"); |
261 | fname); | ||
262 | return GNUNET_SYSERR; | 197 | return GNUNET_SYSERR; |
263 | } | 198 | } |
199 | |||
264 | return GNUNET_OK; | 200 | return GNUNET_OK; |
265 | } | 201 | } |
266 | 202 | ||
@@ -300,51 +236,28 @@ GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa) | |||
300 | */ | 236 | */ |
301 | static int | 237 | static int |
302 | extract_rsa_signature (void *cls, | 238 | extract_rsa_signature (void *cls, |
303 | MYSQL_RES * result, | 239 | struct GNUNET_MY_ResultSpec *rs, |
304 | int row, const char *fname, | 240 | MYSQL_BIND *results) |
305 | size_t * dst_size, | ||
306 | void *dst) | ||
307 | { | 241 | { |
308 | struct GNUNET_CRYPTO_RsaSignature **sig = dst; | 242 | struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst; |
309 | size_t len; | 243 | size_t len; |
310 | const char *res; | 244 | const char *res; |
311 | 245 | ||
312 | 246 | if (results->is_null) | |
313 | MYSQL_ROW rows; | ||
314 | MYSQL_FIELD * field; | ||
315 | |||
316 | *sig = NULL; | ||
317 | |||
318 | rows = mysql_fetch_row (result); | ||
319 | |||
320 | field = mysql_fetch_field (result); | ||
321 | |||
322 | //If it's the correct field | ||
323 | if (field->name == fname) | ||
324 | { | 247 | { |
325 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
326 | "Field '%s' does not exist in result", | ||
327 | fname); | ||
328 | return GNUNET_SYSERR; | 248 | return GNUNET_SYSERR; |
329 | } | 249 | } |
330 | 250 | ||
331 | 251 | len = results->buffer_length; | |
332 | if (rows[row] == NULL) | 252 | res = results->buffer; |
333 | { | ||
334 | return GNUNET_SYSERR; | ||
335 | } | ||
336 | |||
337 | res = rows[row]; | ||
338 | len = strlen (res); | ||
339 | 253 | ||
340 | *sig = GNUNET_CRYPTO_rsa_signature_decode (res, | 254 | *sig = GNUNET_CRYPTO_rsa_signature_decode (res, |
341 | len); | 255 | len); |
342 | 256 | ||
343 | if (NULL == *sig) | 257 | if (NULL != *sig) |
344 | { | 258 | { |
345 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 259 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
346 | "Field '%s' contains bogus value (fails to decode)\n", | 260 | "Resuls contains bogus value (fails to decode)\n"); |
347 | fname); | ||
348 | return GNUNET_SYSERR; | 261 | return GNUNET_SYSERR; |
349 | } | 262 | } |
350 | 263 | ||
@@ -386,51 +299,30 @@ GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig) | |||
386 | */ | 299 | */ |
387 | static int | 300 | static int |
388 | extract_string (void * cls, | 301 | extract_string (void * cls, |
389 | MYSQL_RES * result, | 302 | struct GNUNET_MY_ResultSpec *rs, |
390 | int row, | 303 | MYSQL_BIND *results) |
391 | const char * fname, | ||
392 | size_t *dst_size, | ||
393 | void *dst) | ||
394 | { | 304 | { |
395 | char **str = dst; | 305 | char **str = rs->dst; |
396 | size_t len; | 306 | size_t len; |
397 | const char *res; | 307 | const char *res; |
398 | 308 | ||
399 | MYSQL_ROW rows; | ||
400 | MYSQL_FIELD * field; | ||
401 | |||
402 | *str = NULL; | 309 | *str = NULL; |
403 | 310 | ||
404 | rows = mysql_fetch_row (result); | 311 | if (results->is_null) |
405 | |||
406 | field = mysql_fetch_field (result); | ||
407 | |||
408 | //If it's the correct field | ||
409 | if (field->name == fname) | ||
410 | { | 312 | { |
411 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
412 | "Field '%s' does not exist in result", | ||
413 | fname); | ||
414 | return GNUNET_SYSERR; | 313 | return GNUNET_SYSERR; |
415 | } | 314 | } |
416 | 315 | ||
316 | len = results->buffer_length; | ||
317 | res = results->buffer; | ||
417 | 318 | ||
418 | if (rows[row] == NULL) | ||
419 | { | ||
420 | return GNUNET_SYSERR; | ||
421 | } | ||
422 | |||
423 | res = rows[row]; | ||
424 | len = strlen (res); | ||
425 | |||
426 | *str = GNUNET_strndup (res, | 319 | *str = GNUNET_strndup (res, |
427 | len); | 320 | len); |
428 | 321 | ||
429 | if (NULL == *str) | 322 | if (NULL == *str) |
430 | { | 323 | { |
431 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 324 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
432 | "Field '%s' contains bogus value (fails to decode) \n", | 325 | "Results contains bogus value (fail to decode)\n"); |
433 | fname); | ||
434 | return GNUNET_SYSERR; | 326 | return GNUNET_SYSERR; |
435 | } | 327 | } |
436 | return GNUNET_OK; | 328 | return GNUNET_OK; |
@@ -496,47 +388,25 @@ GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at) | |||
496 | */ | 388 | */ |
497 | static int | 389 | static int |
498 | extract_uint16 (void *cls, | 390 | extract_uint16 (void *cls, |
499 | MYSQL_RES * result, | 391 | struct GNUNET_MY_ResultSpec *rs, |
500 | int row, | 392 | MYSQL_BIND *results) |
501 | const char *fname, | 393 | { |
502 | size_t *dst_size, | 394 | uint16_t *udst = rs->dst; |
503 | void *dst) | 395 | const uint16_t *res; |
504 | { | ||
505 | //TO COMPLETE | ||
506 | uint16_t *udst = dst; | ||
507 | uint16_t *res; | ||
508 | |||
509 | MYSQL_ROW rows; | ||
510 | MYSQL_FIELD * field; | ||
511 | |||
512 | rows = mysql_fetch_row (result); | ||
513 | |||
514 | field = mysql_fetch_field (result); | ||
515 | 396 | ||
516 | //If it's the correct field | 397 | if(results->is_null) |
517 | if (field->name == fname) | ||
518 | { | 398 | { |
519 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
520 | "Field '%s' does not exist in result", | ||
521 | fname); | ||
522 | return GNUNET_SYSERR; | 399 | return GNUNET_SYSERR; |
523 | } | 400 | } |
524 | 401 | ||
525 | 402 | GNUNET_assert (NULL != rs->dst); | |
526 | if (rows[row] == NULL) | 403 | if (sizeof (uint16_t) != rs->dst_size) |
527 | { | ||
528 | return GNUNET_SYSERR; | ||
529 | } | ||
530 | |||
531 | GNUNET_assert (NULL != dst); | ||
532 | |||
533 | if (sizeof (uint16_t) != *dst_size) | ||
534 | { | 404 | { |
535 | GNUNET_break (0); | 405 | GNUNET_break (0); |
536 | return GNUNET_SYSERR; | 406 | return GNUNET_SYSERR; |
537 | } | 407 | } |
538 | 408 | ||
539 | res = atoi (rows[row]); | 409 | res = (uint16_t *)results->buffer; |
540 | *udst = ntohs (*res); | 410 | *udst = ntohs (*res); |
541 | 411 | ||
542 | return GNUNET_OK; | 412 | return GNUNET_OK; |
@@ -576,48 +446,28 @@ GNUNET_MY_result_spec_uint16 (uint16_t *u16) | |||
576 | */ | 446 | */ |
577 | static int | 447 | static int |
578 | extract_uint32 (void *cls, | 448 | extract_uint32 (void *cls, |
579 | MYSQL_RES * result, | 449 | struct GNUNET_MY_ResultSpec *rs, |
580 | int row, | 450 | MYSQL_BIND *results) |
581 | const char *fname, | ||
582 | size_t *dst_size, | ||
583 | void *dst) | ||
584 | { | 451 | { |
585 | uint32_t *udst = dst; | 452 | uint32_t *udst = rs->dst; |
586 | const uint32_t *res; | 453 | const uint32_t *res; |
587 | 454 | ||
588 | MYSQL_ROW rows; | 455 | if(results->is_null) |
589 | MYSQL_FIELD * field; | ||
590 | |||
591 | rows = mysql_fetch_row (result); | ||
592 | |||
593 | field = mysql_fetch_field (result); | ||
594 | |||
595 | //If it's the correct field | ||
596 | if (field->name == fname) | ||
597 | { | ||
598 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
599 | "Field '%s' does not exist in result", | ||
600 | fname); | ||
601 | return GNUNET_SYSERR; | ||
602 | } | ||
603 | |||
604 | |||
605 | if (rows[row] == NULL) | ||
606 | { | 456 | { |
607 | return GNUNET_SYSERR; | 457 | return GNUNET_SYSERR; |
608 | } | 458 | } |
609 | 459 | ||
610 | GNUNET_assert (NULL != dst); | 460 | GNUNET_assert (NULL != rs->dst); |
611 | 461 | if (sizeof (uint32_t) != rs->dst_size) | |
612 | if (sizeof (uint32_t) != *dst_size) | ||
613 | { | 462 | { |
614 | GNUNET_break (0); | 463 | GNUNET_break (0); |
615 | return GNUNET_SYSERR; | 464 | return GNUNET_SYSERR; |
616 | } | 465 | } |
617 | 466 | ||
618 | res = (uint32_t) rows[row]; | 467 | res = (uint32_t *)results->buffer; |
619 | 468 | ||
620 | *udst = ntohl (*res); | 469 | *udst = ntohl (*res); |
470 | |||
621 | return GNUNET_OK; | 471 | return GNUNET_OK; |
622 | } | 472 | } |
623 | 473 | ||
@@ -655,45 +505,25 @@ GNUNET_MY_result_spec_uint32 (uint32_t *u32) | |||
655 | */ | 505 | */ |
656 | static int | 506 | static int |
657 | extract_uint64 (void *cls, | 507 | extract_uint64 (void *cls, |
658 | MYSQL_RES * result, | 508 | struct GNUNET_MY_ResultSpec *rs, |
659 | int row, | 509 | MYSQL_BIND *results) |
660 | const char *fname, | ||
661 | size_t *dst_size, | ||
662 | void *dst) | ||
663 | { | 510 | { |
664 | uint64_t *udst = dst; | 511 | uint64_t *udst = rs->dst; |
665 | const uint64_t *res; | 512 | const uint64_t *res; |
666 | 513 | ||
667 | MYSQL_ROW rows; | 514 | if (results->is_null) |
668 | MYSQL_FIELD * field; | ||
669 | |||
670 | rows = mysql_fetch_row (result); | ||
671 | |||
672 | field = mysql_fetch_field (result); | ||
673 | |||
674 | //If it's the correct field | ||
675 | if (field->name == fname) | ||
676 | { | 515 | { |
677 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
678 | "Field '%s' does not exist in result", | ||
679 | fname); | ||
680 | return GNUNET_SYSERR; | 516 | return GNUNET_SYSERR; |
681 | } | 517 | } |
682 | 518 | ||
683 | 519 | GNUNET_assert (NULL != rs->dst); | |
684 | if (rows[row] == NULL) | 520 | if (sizeof (uint64_t) != rs->dst_size) |
685 | { | 521 | { |
522 | GNUNET_break (0); | ||
686 | return GNUNET_SYSERR; | 523 | return GNUNET_SYSERR; |
687 | } | 524 | } |
688 | 525 | ||
689 | GNUNET_assert (NULL != dst); | 526 | res = (uint64_t *)results->buffer; |
690 | if (sizeof (uint64_t) != *dst_size) | ||
691 | { | ||
692 | GNUNET_break (0); | ||
693 | return GNUNET_SYSERR; | ||
694 | } | ||
695 | |||
696 | res = (uint64_t) rows[row]; | ||
697 | *udst = GNUNET_ntohll (*res); | 527 | *udst = GNUNET_ntohll (*res); |
698 | 528 | ||
699 | return GNUNET_OK; | 529 | return GNUNET_OK; |