aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-02-28 04:07:26 +0100
committerChristian Grothoff <christian@grothoff.org>2017-02-28 04:07:26 +0100
commit67935d7aa1090eed2eb00212741dcc4d13c0bb40 (patch)
tree6f261b7cda16c8a04ccec9d8763a3c0dbfaf5a95 /src
parent830137e312e8e565c9113a20411aa3073fbd1cbd (diff)
downloadgnunet-67935d7aa1090eed2eb00212741dcc4d13c0bb40.tar.gz
gnunet-67935d7aa1090eed2eb00212741dcc4d13c0bb40.zip
completing sq_result_helper
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_sq_lib.h4
-rw-r--r--src/pq/pq_result_helper.c50
-rw-r--r--src/sq/sq.c2
-rw-r--r--src/sq/sq_result_helper.c553
4 files changed, 561 insertions, 48 deletions
diff --git a/src/include/gnunet_sq_lib.h b/src/include/gnunet_sq_lib.h
index 4484c2851..c03f83e07 100644
--- a/src/include/gnunet_sq_lib.h
+++ b/src/include/gnunet_sq_lib.h
@@ -252,7 +252,9 @@ struct GNUNET_SQ_ResultSpec
252 size_t dst_size; 252 size_t dst_size;
253 253
254 /** 254 /**
255 * Where to store actual size of the result. 255 * Where to store actual size of the result. If left at
256 * NULL, will be made to point to @e dst_size before
257 * @a conv is called.
256 */ 258 */
257 size_t *result_size; 259 size_t *result_size;
258 260
diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c
index 180c5fc18..c5c8e56b9 100644
--- a/src/pq/pq_result_helper.c
+++ b/src/pq/pq_result_helper.c
@@ -77,9 +77,7 @@ extract_varsize_blob (void *cls,
77 fname); 77 fname);
78 if (fnum < 0) 78 if (fnum < 0)
79 { 79 {
80 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 80 GNUNET_break (0);
81 "Field `%s' does not exist in result\n",
82 fname);
83 return GNUNET_SYSERR; 81 return GNUNET_SYSERR;
84 } 82 }
85 if (PQgetisnull (result, 83 if (PQgetisnull (result,
@@ -156,9 +154,7 @@ extract_fixed_blob (void *cls,
156 fname); 154 fname);
157 if (fnum < 0) 155 if (fnum < 0)
158 { 156 {
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 157 GNUNET_break (0);
160 "Field `%s' does not exist in result\n",
161 fname);
162 return GNUNET_SYSERR; 158 return GNUNET_SYSERR;
163 } 159 }
164 if (PQgetisnull (result, 160 if (PQgetisnull (result,
@@ -173,11 +169,7 @@ extract_fixed_blob (void *cls,
173 fnum); 169 fnum);
174 if (*dst_size != len) 170 if (*dst_size != len)
175 { 171 {
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 172 GNUNET_break (0);
177 "Field `%s' has wrong size (got %u, expected %u)\n",
178 fname,
179 (unsigned int) len,
180 (unsigned int) *dst_size);
181 return GNUNET_SYSERR; 173 return GNUNET_SYSERR;
182 } 174 }
183 res = PQgetvalue (result, 175 res = PQgetvalue (result,
@@ -243,9 +235,7 @@ extract_rsa_public_key (void *cls,
243 fname); 235 fname);
244 if (fnum < 0) 236 if (fnum < 0)
245 { 237 {
246 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 238 GNUNET_break (0);
247 "Field `%s' does not exist in result\n",
248 fname);
249 return GNUNET_SYSERR; 239 return GNUNET_SYSERR;
250 } 240 }
251 if (PQgetisnull (result, 241 if (PQgetisnull (result,
@@ -265,9 +255,7 @@ extract_rsa_public_key (void *cls,
265 len); 255 len);
266 if (NULL == *pk) 256 if (NULL == *pk)
267 { 257 {
268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 258 GNUNET_break (0);
269 "Field `%s' contains bogus value (fails to decode)\n",
270 fname);
271 return GNUNET_SYSERR; 259 return GNUNET_SYSERR;
272 } 260 }
273 return GNUNET_OK; 261 return GNUNET_OK;
@@ -346,9 +334,7 @@ extract_rsa_signature (void *cls,
346 fname); 334 fname);
347 if (fnum < 0) 335 if (fnum < 0)
348 { 336 {
349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 337 GNUNET_break (0);
350 "Field `%s' does not exist in result\n",
351 fname);
352 return GNUNET_SYSERR; 338 return GNUNET_SYSERR;
353 } 339 }
354 if (PQgetisnull (result, 340 if (PQgetisnull (result,
@@ -368,9 +354,7 @@ extract_rsa_signature (void *cls,
368 len); 354 len);
369 if (NULL == *sig) 355 if (NULL == *sig)
370 { 356 {
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 357 GNUNET_break (0);
372 "Field `%s' contains bogus value (fails to decode)\n",
373 fname);
374 return GNUNET_SYSERR; 358 return GNUNET_SYSERR;
375 } 359 }
376 return GNUNET_OK; 360 return GNUNET_OK;
@@ -449,9 +433,7 @@ extract_string (void *cls,
449 fname); 433 fname);
450 if (fnum < 0) 434 if (fnum < 0)
451 { 435 {
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 436 GNUNET_break (0);
453 "Field `%s' does not exist in result\n",
454 fname);
455 return GNUNET_SYSERR; 437 return GNUNET_SYSERR;
456 } 438 }
457 if (PQgetisnull (result, 439 if (PQgetisnull (result,
@@ -471,9 +453,7 @@ extract_string (void *cls,
471 len); 453 len);
472 if (NULL == *str) 454 if (NULL == *str)
473 { 455 {
474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 456 GNUNET_break (0);
475 "Field `%s' contains bogus value (fails to decode)\n",
476 fname);
477 return GNUNET_SYSERR; 457 return GNUNET_SYSERR;
478 } 458 }
479 return GNUNET_OK; 459 return GNUNET_OK;
@@ -583,9 +563,7 @@ extract_uint16 (void *cls,
583 fname); 563 fname);
584 if (fnum < 0) 564 if (fnum < 0)
585 { 565 {
586 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 566 GNUNET_break (0);
587 "Field `%s' does not exist in result\n",
588 fname);
589 return GNUNET_SYSERR; 567 return GNUNET_SYSERR;
590 } 568 }
591 if (PQgetisnull (result, 569 if (PQgetisnull (result,
@@ -655,9 +633,7 @@ extract_uint32 (void *cls,
655 fname); 633 fname);
656 if (fnum < 0) 634 if (fnum < 0)
657 { 635 {
658 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 636 GNUNET_break (0);
659 "Field `%s' does not exist in result\n",
660 fname);
661 return GNUNET_SYSERR; 637 return GNUNET_SYSERR;
662 } 638 }
663 if (PQgetisnull (result, 639 if (PQgetisnull (result,
@@ -727,9 +703,7 @@ extract_uint64 (void *cls,
727 fname); 703 fname);
728 if (fnum < 0) 704 if (fnum < 0)
729 { 705 {
730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 706 GNUNET_break (0);
731 "Field `%s' does not exist in result\n",
732 fname);
733 return GNUNET_SYSERR; 707 return GNUNET_SYSERR;
734 } 708 }
735 if (PQgetisnull (result, 709 if (PQgetisnull (result,
diff --git a/src/sq/sq.c b/src/sq/sq.c
index 47fcaf24e..dc4416761 100644
--- a/src/sq/sq.c
+++ b/src/sq/sq.c
@@ -75,6 +75,8 @@ GNUNET_SQ_extract_result (sqlite3_stmt *result,
75 75
76 for (unsigned int i=0;NULL != rs[i].conv; i++) 76 for (unsigned int i=0;NULL != rs[i].conv; i++)
77 { 77 {
78 if (NULL == rs[i].result_size)
79 rs[i].result_size = &rs[i].dst_size;
78 if (GNUNET_OK != 80 if (GNUNET_OK !=
79 rs[i].conv (rs[i].cls, 81 rs[i].conv (rs[i].cls,
80 result, 82 result,
diff --git a/src/sq/sq_result_helper.c b/src/sq/sq_result_helper.c
index 36ce53317..9ac2e2533 100644
--- a/src/sq/sq_result_helper.c
+++ b/src/sq/sq_result_helper.c
@@ -24,26 +24,75 @@
24 24
25 25
26/** 26/**
27 * Extract fixed-sized binary data from a Postgres database @a result at row @a row. 27 * Extract variable-sized binary data from a Postgres database @a result at row @a row.
28 * 28 *
29 * @param cls closure 29 * @param cls closure
30 * @param result where to extract data from 30 * @param result where to extract data from
31 * @param row row to extract data from
32 * @param column column to extract data from 31 * @param column column to extract data from
33 * @param[in,out] dst_size where to store size of result, may be NULL 32 * @param[in,out] dst_size where to store size of result, may be NULL
34 * @param[out] dst where to store the result 33 * @param[out] dst where to store the result (actually a `void **`)
35 * @return 34 * @return
36 * #GNUNET_YES if all results could be extracted 35 * #GNUNET_YES if all results could be extracted
37 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) 36 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
38 */ 37 */
39static int 38static int
40extract_fixed_blob (void *cls, 39extract_var_blob (void *cls,
41 sqlite3_stmt *result, 40 sqlite3_stmt *result,
42 int row, 41 unsigned int column,
43 unsigned int column, 42 size_t *dst_size,
44 size_t *dst_size, 43 void *dst)
45 void *dst)
46{ 44{
45 int have;
46 const void *ret;
47 void **rdst = (void **) dst;
48
49 if (SQLITE_BLOB !=
50 sqlite3_column_type (result,
51 column))
52 {
53 GNUNET_break (0);
54 return GNUNET_SYSERR;
55 }
56 /* sqlite manual says to invoke 'sqlite3_column_blob()'
57 before calling sqlite3_column_bytes() */
58 ret = sqlite3_column_blob (result,
59 column);
60 have = sqlite3_column_bytes (result,
61 column);
62 if (have < 0)
63 {
64 GNUNET_break (0);
65 return GNUNET_SYSERR;
66 }
67 *dst_size = have;
68 if (0 == have)
69 {
70 *rdst = NULL;
71 return GNUNET_OK;
72 }
73 *rdst = GNUNET_malloc (have);
74 GNUNET_memcpy (*rdst,
75 ret,
76 have);
77 return GNUNET_OK;
78}
79
80
81/**
82 * Cleanup memory allocated by #extract_var_blob().
83 *
84 * @param cls pointer to pointer of allocation
85 */
86static void
87clean_var_blob (void *cls)
88{
89 void **dptr = (void **) cls;
90
91 if (NULL != *dptr)
92 {
93 GNUNET_free (*dptr);
94 *dptr = NULL;
95 }
47} 96}
48 97
49 98
@@ -58,6 +107,62 @@ struct GNUNET_SQ_ResultSpec
58GNUNET_SQ_result_spec_variable_size (void **dst, 107GNUNET_SQ_result_spec_variable_size (void **dst,
59 size_t *sptr) 108 size_t *sptr)
60{ 109{
110 struct GNUNET_SQ_ResultSpec rs = {
111 .conv = &extract_var_blob,
112 .cleaner = &clean_var_blob,
113 .cls = dst,
114 .result_size = sptr,
115 .num_params = 1
116 };
117
118 return rs;
119}
120
121
122/**
123 * Extract fixed-sized binary data from a Postgres database @a result at row @a row.
124 *
125 * @param cls closure
126 * @param result where to extract data from
127 * @param column column to extract data from
128 * @param[in,out] dst_size where to store size of result, may be NULL
129 * @param[out] dst where to store the result
130 * @return
131 * #GNUNET_YES if all results could be extracted
132 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
133 */
134static int
135extract_fixed_blob (void *cls,
136 sqlite3_stmt *result,
137 unsigned int column,
138 size_t *dst_size,
139 void *dst)
140{
141 int have;
142 const void *ret;
143
144 if (SQLITE_BLOB !=
145 sqlite3_column_type (result,
146 column))
147 {
148 GNUNET_break (0);
149 return GNUNET_SYSERR;
150 }
151 /* sqlite manual says to invoke 'sqlite3_column_blob()'
152 before calling sqlite3_column_bytes() */
153 ret = sqlite3_column_blob (result,
154 column);
155 have = sqlite3_column_bytes (result,
156 column);
157 if (*dst_size != have)
158 {
159 GNUNET_break (0);
160 return GNUNET_SYSERR;
161 }
162 GNUNET_memcpy (dst,
163 ret,
164 have);
165 return GNUNET_OK;
61} 166}
62 167
63 168
@@ -72,6 +177,76 @@ struct GNUNET_SQ_ResultSpec
72GNUNET_SQ_result_spec_fixed_size (void *dst, 177GNUNET_SQ_result_spec_fixed_size (void *dst,
73 size_t dst_size) 178 size_t dst_size)
74{ 179{
180 struct GNUNET_SQ_ResultSpec rs = {
181 .conv = &extract_fixed_blob,
182 .dst = dst,
183 .dst_size = dst_size,
184 .num_params = 1
185 };
186
187 return rs;
188}
189
190
191/**
192 * Extract fixed-sized binary data from a Postgres database @a result at row @a row.
193 *
194 * @param cls closure
195 * @param result where to extract data from
196 * @param column column to extract data from
197 * @param[in,out] dst_size where to store size of result, may be NULL
198 * @param[out] dst where to store the result
199 * @return
200 * #GNUNET_YES if all results could be extracted
201 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
202 */
203static int
204extract_utf8_string (void *cls,
205 sqlite3_stmt *result,
206 unsigned int column,
207 size_t *dst_size,
208 void *dst)
209{
210 const char *text;
211 char **rdst = dst;
212
213 if (SQLITE_TEXT !=
214 sqlite3_column_type (result,
215 column))
216 {
217 GNUNET_break (0);
218 return GNUNET_SYSERR;
219 }
220 /* sqlite manual guarantees that 'sqlite3_column_text()'
221 is 0-terminated */
222 text = (const char *) sqlite3_column_text (result,
223 column);
224 if (NULL == text)
225 {
226 GNUNET_break (0);
227 return GNUNET_SYSERR;
228 }
229 *dst_size = strlen (text) + 1;
230 *rdst = GNUNET_strdup (text);
231 return GNUNET_OK;
232}
233
234
235/**
236 * Cleanup memory allocated by #extract_var_blob().
237 *
238 * @param cls pointer to pointer of allocation
239 */
240static void
241clean_utf8_string (void *cls)
242{
243 char **dptr = (char **) cls;
244
245 if (NULL != *dptr)
246 {
247 GNUNET_free (*dptr);
248 *dptr = NULL;
249 }
75} 250}
76 251
77 252
@@ -84,6 +259,87 @@ GNUNET_SQ_result_spec_fixed_size (void *dst,
84struct GNUNET_SQ_ResultSpec 259struct GNUNET_SQ_ResultSpec
85GNUNET_SQ_result_spec_string (char **dst) 260GNUNET_SQ_result_spec_string (char **dst)
86{ 261{
262 struct GNUNET_SQ_ResultSpec rs = {
263 .conv = &extract_utf8_string,
264 .cleaner = &clean_utf8_string,
265 .cls = dst,
266 .dst = dst,
267 .num_params = 1
268 };
269
270 return rs;
271}
272
273
274/**
275 * Extract data from a Postgres database @a result at row @a row.
276 *
277 * @param cls closure
278 * @param result where to extract data from
279 * @param column column to extract data from
280 * @param[in,out] dst_size where to store size of result, may be NULL
281 * @param[out] dst where to store the result
282 * @return
283 * #GNUNET_YES if all results could be extracted
284 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
285 */
286static int
287extract_rsa_pub (void *cls,
288 sqlite3_stmt *result,
289 unsigned int column,
290 size_t *dst_size,
291 void *dst)
292{
293 struct GNUNET_CRYPTO_RsaPublicKey **pk = dst;
294 int have;
295 const void *ret;
296
297 if (SQLITE_BLOB !=
298 sqlite3_column_type (result,
299 column))
300 {
301 GNUNET_break (0);
302 return GNUNET_SYSERR;
303 }
304 /* sqlite manual says to invoke 'sqlite3_column_blob()'
305 before calling sqlite3_column_bytes() */
306 ret = sqlite3_column_blob (result,
307 column);
308 have = sqlite3_column_bytes (result,
309 column);
310 if (have < 0)
311 {
312 GNUNET_break (0);
313 return GNUNET_SYSERR;
314 }
315
316 *pk = GNUNET_CRYPTO_rsa_public_key_decode (ret,
317 have);
318 if (NULL == *pk)
319 {
320 GNUNET_break (0);
321 return GNUNET_SYSERR;
322 }
323 return GNUNET_OK;
324}
325
326
327/**
328 * Function called to clean up memory allocated
329 * by a #GNUNET_PQ_ResultConverter.
330 *
331 * @param cls closure
332 */
333static void
334clean_rsa_pub (void *cls)
335{
336 struct GNUNET_CRYPTO_RsaPublicKey **pk = cls;
337
338 if (NULL != *pk)
339 {
340 GNUNET_CRYPTO_rsa_public_key_free (*pk);
341 *pk = NULL;
342 }
87} 343}
88 344
89 345
@@ -96,6 +352,87 @@ GNUNET_SQ_result_spec_string (char **dst)
96struct GNUNET_SQ_ResultSpec 352struct GNUNET_SQ_ResultSpec
97GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa) 353GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
98{ 354{
355 struct GNUNET_SQ_ResultSpec rs = {
356 .conv = &extract_rsa_pub,
357 .cleaner = &clean_rsa_pub,
358 .dst = rsa,
359 .cls = rsa,
360 .num_params = 1
361 };
362
363 return rs;
364}
365
366
367/**
368 * Extract data from a Postgres database @a result at row @a row.
369 *
370 * @param cls closure
371 * @param result where to extract data from
372 * @param column column to extract data from
373 * @param[in,out] dst_size where to store size of result, may be NULL
374 * @param[out] dst where to store the result
375 * @return
376 * #GNUNET_YES if all results could be extracted
377 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
378 */
379static int
380extract_rsa_sig (void *cls,
381 sqlite3_stmt *result,
382 unsigned int column,
383 size_t *dst_size,
384 void *dst)
385{
386 struct GNUNET_CRYPTO_RsaSignature **sig = dst;
387 int have;
388 const void *ret;
389
390 if (SQLITE_BLOB !=
391 sqlite3_column_type (result,
392 column))
393 {
394 GNUNET_break (0);
395 return GNUNET_SYSERR;
396 }
397 /* sqlite manual says to invoke 'sqlite3_column_blob()'
398 before calling sqlite3_column_bytes() */
399 ret = sqlite3_column_blob (result,
400 column);
401 have = sqlite3_column_bytes (result,
402 column);
403 if (have < 0)
404 {
405 GNUNET_break (0);
406 return GNUNET_SYSERR;
407 }
408
409 *sig = GNUNET_CRYPTO_rsa_signature_decode (ret,
410 have);
411 if (NULL == *sig)
412 {
413 GNUNET_break (0);
414 return GNUNET_SYSERR;
415 }
416 return GNUNET_OK;
417}
418
419
420/**
421 * Function called to clean up memory allocated
422 * by a #GNUNET_PQ_ResultConverter.
423 *
424 * @param cls result data to clean up
425 */
426static void
427clean_rsa_sig (void *cls)
428{
429 struct GNUNET_CRYPTO_RsaSignature **sig = cls;
430
431 if (NULL != *sig)
432 {
433 GNUNET_CRYPTO_rsa_signature_free (*sig);
434 *sig = NULL;
435 }
99} 436}
100 437
101 438
@@ -108,6 +445,15 @@ GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
108struct GNUNET_SQ_ResultSpec 445struct GNUNET_SQ_ResultSpec
109GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig) 446GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
110{ 447{
448 struct GNUNET_SQ_ResultSpec rs = {
449 .conv = &extract_rsa_sig,
450 .cleaner = &clean_rsa_sig,
451 .dst = sig,
452 .cls = sig,
453 .num_params = 1
454 };
455
456 return rs;
111} 457}
112 458
113 459
@@ -120,6 +466,44 @@ GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
120struct GNUNET_SQ_ResultSpec 466struct GNUNET_SQ_ResultSpec
121GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at) 467GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
122{ 468{
469 return GNUNET_SQ_result_spec_uint64 (&at->abs_value_us);
470}
471
472
473/**
474 * Extract absolute time value in NBO from a Postgres database @a result at row @a row.
475 *
476 * @param cls closure
477 * @param result where to extract data from
478 * @param column column to extract data from
479 * @param[in,out] dst_size where to store size of result, may be NULL
480 * @param[out] dst where to store the result
481 * @return
482 * #GNUNET_YES if all results could be extracted
483 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
484 */
485static int
486extract_abs_time_nbo (void *cls,
487 sqlite3_stmt *result,
488 unsigned int column,
489 size_t *dst_size,
490 void *dst)
491{
492 struct GNUNET_TIME_AbsoluteNBO *u = dst;
493 struct GNUNET_TIME_Absolute t;
494
495 GNUNET_assert (sizeof (uint64_t) == *dst_size);
496 if (SQLITE_INTEGER !=
497 sqlite3_column_type (result,
498 column))
499 {
500 GNUNET_break (0);
501 return GNUNET_SYSERR;
502 }
503 t.abs_value_us = (uint64_t) sqlite3_column_int64 (result,
504 column);
505 *u = GNUNET_TIME_absolute_hton (t);
506 return GNUNET_OK;
123} 507}
124 508
125 509
@@ -132,6 +516,56 @@ GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
132struct GNUNET_SQ_ResultSpec 516struct GNUNET_SQ_ResultSpec
133GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at) 517GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
134{ 518{
519 struct GNUNET_SQ_ResultSpec rs = {
520 .conv = &extract_abs_time_nbo,
521 .dst = at,
522 .dst_size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
523 .num_params = 1
524 };
525
526 return rs;
527}
528
529
530/**
531 * Extract 16-bit integer from a Postgres database @a result at row @a row.
532 *
533 * @param cls closure
534 * @param result where to extract data from
535 * @param column column to extract data from
536 * @param[in,out] dst_size where to store size of result, may be NULL
537 * @param[out] dst where to store the result
538 * @return
539 * #GNUNET_YES if all results could be extracted
540 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
541 */
542static int
543extract_uint16 (void *cls,
544 sqlite3_stmt *result,
545 unsigned int column,
546 size_t *dst_size,
547 void *dst)
548{
549 uint64_t v;
550 uint32_t *u = dst;
551
552 GNUNET_assert (sizeof (uint16_t) == *dst_size);
553 if (SQLITE_INTEGER !=
554 sqlite3_column_type (result,
555 column))
556 {
557 GNUNET_break (0);
558 return GNUNET_SYSERR;
559 }
560 v = (uint64_t) sqlite3_column_int64 (result,
561 column);
562 if (v > UINT16_MAX)
563 {
564 GNUNET_break (0);
565 return GNUNET_SYSERR;
566 }
567 *u = (uint16_t) v;
568 return GNUNET_OK;
135} 569}
136 570
137 571
@@ -144,6 +578,56 @@ GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
144struct GNUNET_SQ_ResultSpec 578struct GNUNET_SQ_ResultSpec
145GNUNET_SQ_result_spec_uint16 (uint16_t *u16) 579GNUNET_SQ_result_spec_uint16 (uint16_t *u16)
146{ 580{
581 struct GNUNET_SQ_ResultSpec rs = {
582 .conv = &extract_uint16,
583 .dst = u16,
584 .dst_size = sizeof (uint16_t),
585 .num_params = 1
586 };
587
588 return rs;
589}
590
591
592/**
593 * Extract 32-bit integer from a Postgres database @a result at row @a row.
594 *
595 * @param cls closure
596 * @param result where to extract data from
597 * @param column column to extract data from
598 * @param[in,out] dst_size where to store size of result, may be NULL
599 * @param[out] dst where to store the result
600 * @return
601 * #GNUNET_YES if all results could be extracted
602 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
603 */
604static int
605extract_uint32 (void *cls,
606 sqlite3_stmt *result,
607 unsigned int column,
608 size_t *dst_size,
609 void *dst)
610{
611 uint64_t v;
612 uint32_t *u = dst;
613
614 GNUNET_assert (sizeof (uint32_t) == *dst_size);
615 if (SQLITE_INTEGER !=
616 sqlite3_column_type (result,
617 column))
618 {
619 GNUNET_break (0);
620 return GNUNET_SYSERR;
621 }
622 v = (uint64_t) sqlite3_column_int64 (result,
623 column);
624 if (v > UINT32_MAX)
625 {
626 GNUNET_break (0);
627 return GNUNET_SYSERR;
628 }
629 *u = (uint32_t) v;
630 return GNUNET_OK;
147} 631}
148 632
149 633
@@ -156,6 +640,49 @@ GNUNET_SQ_result_spec_uint16 (uint16_t *u16)
156struct GNUNET_SQ_ResultSpec 640struct GNUNET_SQ_ResultSpec
157GNUNET_SQ_result_spec_uint32 (uint32_t *u32) 641GNUNET_SQ_result_spec_uint32 (uint32_t *u32)
158{ 642{
643 struct GNUNET_SQ_ResultSpec rs = {
644 .conv = &extract_uint32,
645 .dst = u32,
646 .dst_size = sizeof (uint32_t),
647 .num_params = 1
648 };
649
650 return rs;
651}
652
653
654/**
655 * Extract 64-bit integer from a Postgres database @a result at row @a row.
656 *
657 * @param cls closure
658 * @param result where to extract data from
659 * @param column column to extract data from
660 * @param[in,out] dst_size where to store size of result, may be NULL
661 * @param[out] dst where to store the result
662 * @return
663 * #GNUNET_YES if all results could be extracted
664 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
665 */
666static int
667extract_uint64 (void *cls,
668 sqlite3_stmt *result,
669 unsigned int column,
670 size_t *dst_size,
671 void *dst)
672{
673 uint64_t *u = dst;
674
675 GNUNET_assert (sizeof (uint64_t) == *dst_size);
676 if (SQLITE_INTEGER !=
677 sqlite3_column_type (result,
678 column))
679 {
680 GNUNET_break (0);
681 return GNUNET_SYSERR;
682 }
683 *u = (uint64_t) sqlite3_column_int64 (result,
684 column);
685 return GNUNET_OK;
159} 686}
160 687
161 688
@@ -168,6 +695,14 @@ GNUNET_SQ_result_spec_uint32 (uint32_t *u32)
168struct GNUNET_SQ_ResultSpec 695struct GNUNET_SQ_ResultSpec
169GNUNET_SQ_result_spec_uint64 (uint64_t *u64) 696GNUNET_SQ_result_spec_uint64 (uint64_t *u64)
170{ 697{
698 struct GNUNET_SQ_ResultSpec rs = {
699 .conv = &extract_uint64,
700 .dst = u64,
701 .dst_size = sizeof (uint64_t),
702 .num_params = 1
703 };
704
705 return rs;
171} 706}
172 707
173 708