my_result_helper.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  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
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file my/my_result_helper.c
  18. * @brief functions to extract result values
  19. * @author Christophe Genevey
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_my_lib.h"
  24. /**
  25. * extract data from a Mysql database @a result at row @a row
  26. *
  27. * @param cls closure
  28. * @param[in,out] rs
  29. * @param stmt the mysql statement that is being run
  30. * @param column the column that is being processed
  31. * @param[out] result mysql result
  32. * @return
  33. * #GNUNET_OK if all results could be extracted
  34. * #GNUNET_SYSERR if a result was invalid
  35. */
  36. static int
  37. pre_extract_varsize_blob (void *cls,
  38. struct GNUNET_MY_ResultSpec *rs,
  39. MYSQL_STMT *stmt,
  40. unsigned int column,
  41. MYSQL_BIND *results)
  42. {
  43. results[0].buffer = NULL;
  44. results[0].buffer_length = 0;
  45. results[0].length = &rs->mysql_bind_output_length;
  46. results[0].is_null = &rs->is_null;
  47. rs->is_null = 0;
  48. return GNUNET_OK;
  49. }
  50. /**
  51. * extract data from a Mysql database @a result at row @a row
  52. *
  53. * @param cls closure
  54. * @param[in,out] rs
  55. * @param stmt the mysql statement that is being run
  56. * @param column the column that is being processed
  57. * @param[out] results
  58. * @return
  59. * #GNUNET_OK if all results could be extracted
  60. * #GNUNET_SYSERR if a result was invalid
  61. */
  62. static int
  63. post_extract_varsize_blob (void *cls,
  64. struct GNUNET_MY_ResultSpec *rs,
  65. MYSQL_STMT *stmt,
  66. unsigned int column,
  67. MYSQL_BIND *results)
  68. {
  69. void *buf;
  70. size_t size;
  71. if (*results->is_null)
  72. return GNUNET_SYSERR;
  73. size = (size_t) rs->mysql_bind_output_length;
  74. if (rs->mysql_bind_output_length != size)
  75. return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
  76. buf = GNUNET_malloc (size);
  77. results[0].buffer = buf;
  78. results[0].buffer_length = size;
  79. results[0].buffer_type = MYSQL_TYPE_BLOB;
  80. if (0 !=
  81. mysql_stmt_fetch_column (stmt,
  82. results,
  83. column,
  84. 0))
  85. {
  86. GNUNET_free (buf);
  87. return GNUNET_SYSERR;
  88. }
  89. *(void **) rs->dst = buf;
  90. *rs->result_size = size;
  91. return GNUNET_OK;
  92. }
  93. /**
  94. * extract data from a Mysql database @a result at row @a row
  95. *
  96. * @param cls closure
  97. * @param[in,out] rs
  98. */
  99. static void
  100. cleanup_varsize_blob (void *cls,
  101. struct GNUNET_MY_ResultSpec *rs)
  102. {
  103. void **ptr = (void **) rs->dst;
  104. if (NULL != *ptr)
  105. {
  106. GNUNET_free (*ptr);
  107. *ptr = NULL;
  108. }
  109. }
  110. /**
  111. * Variable-size result expected
  112. *
  113. * @param[out] dst where to store the result, allocated
  114. * @param[out] ptr_size where to store the size of @a dst
  115. * @return array entru for the result specification to use
  116. */
  117. struct GNUNET_MY_ResultSpec
  118. GNUNET_MY_result_spec_variable_size (void **dst,
  119. size_t *ptr_size)
  120. {
  121. 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,
  127. .num_fields = 1
  128. };
  129. return res;
  130. }
  131. /**
  132. * Extract data from a Mysql database @a result at row @a row
  133. *
  134. * @param cls closure
  135. * @param[in,out] rs
  136. * @param stmt the mysql statement that is being run
  137. * @param column the column that is being processed
  138. * @param[out] results
  139. * @return
  140. * #GNUNET_OK if all results could be extracted
  141. * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
  142. */
  143. static int
  144. pre_extract_fixed_blob (void *cls,
  145. struct GNUNET_MY_ResultSpec *rs,
  146. MYSQL_STMT *stmt,
  147. unsigned int column,
  148. MYSQL_BIND *results)
  149. {
  150. results[0].buffer = rs->dst;
  151. results[0].buffer_length = rs->dst_size;
  152. results[0].length = &rs->mysql_bind_output_length;
  153. results[0].buffer_type = MYSQL_TYPE_BLOB;
  154. results[0].is_null = &rs->is_null;
  155. rs->is_null = 0;
  156. return GNUNET_OK;
  157. }
  158. /**
  159. * Check size of extracted fixed size data from a Mysql database @a
  160. * result at row @a row
  161. *
  162. * @param cls closure
  163. * @param[in,out] rs
  164. * @param stmt the mysql statement that is being run
  165. * @param column the column that is being processed
  166. * @param[out] results
  167. * @return
  168. * #GNUNET_OK if all results could be extracted
  169. * #GNUNET_SYSERR if a result was invalid(non-existing field or NULL)
  170. */
  171. static int
  172. post_extract_fixed_blob (void *cls,
  173. struct GNUNET_MY_ResultSpec *rs,
  174. MYSQL_STMT *stmt,
  175. unsigned int column,
  176. MYSQL_BIND *results)
  177. {
  178. if (*results->is_null)
  179. return GNUNET_SYSERR;
  180. if (rs->dst_size != rs->mysql_bind_output_length)
  181. return GNUNET_SYSERR;
  182. return GNUNET_OK;
  183. }
  184. /**
  185. * Fixed-size result expected.
  186. *
  187. * @param name name of the field in the table
  188. * @param[out] dst where to store the result
  189. * @param ptr_size number of bytes in @a dst
  190. * @return array entry for the result specification to use
  191. */
  192. struct GNUNET_MY_ResultSpec
  193. GNUNET_MY_result_spec_fixed_size (void *ptr,
  194. size_t ptr_size)
  195. {
  196. struct GNUNET_MY_ResultSpec res = {
  197. .pre_conv = &pre_extract_fixed_blob,
  198. .post_conv = &post_extract_fixed_blob,
  199. .cleaner = NULL,
  200. .dst = (void *) (ptr),
  201. .dst_size = ptr_size,
  202. .num_fields = 1
  203. };
  204. return res;
  205. }
  206. /**
  207. * Extract data from a Mysql database @a result at row @a row
  208. *
  209. * @param cls closure
  210. * @param[in,out] rs
  211. * @param stmt the mysql statement that is being run
  212. * @param column the column that is being processed
  213. * @param[out] results
  214. * @return
  215. * #GNUNET_OK if all results could be extracted
  216. * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
  217. */
  218. static int
  219. pre_extract_rsa_public_key (void *cls,
  220. struct GNUNET_MY_ResultSpec *rs,
  221. MYSQL_STMT *stmt,
  222. unsigned int column,
  223. MYSQL_BIND *results)
  224. {
  225. results[0].buffer = NULL;
  226. results[0].buffer_length = 0;
  227. results[0].length = &rs->mysql_bind_output_length;
  228. results[0].buffer_type = MYSQL_TYPE_BLOB;
  229. results[0].is_null = &rs->is_null;
  230. rs->is_null = 0;
  231. return GNUNET_OK;
  232. }
  233. /**
  234. * Check size of extracted fixed size data from a Mysql database @a
  235. * result at row @a row
  236. *
  237. * @param cls closure
  238. * @param[in,out] rs
  239. * @param stmt the mysql statement that is being run
  240. * @param column the column that is being processed
  241. * @param[out] results
  242. * @return
  243. * #GNUNET_OK if all results could be extracted
  244. * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
  245. */
  246. static int
  247. post_extract_rsa_public_key (void *cls,
  248. struct GNUNET_MY_ResultSpec *rs,
  249. MYSQL_STMT *stmt,
  250. unsigned int column,
  251. MYSQL_BIND *results)
  252. {
  253. struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
  254. void *buf;
  255. size_t size;
  256. if (*results->is_null)
  257. return GNUNET_SYSERR;
  258. size = (size_t) rs->mysql_bind_output_length;
  259. if (rs->mysql_bind_output_length != size)
  260. return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
  261. buf = GNUNET_malloc (size);
  262. results[0].buffer = buf;
  263. results[0].buffer_length = size;
  264. results[0].buffer_type = MYSQL_TYPE_BLOB;
  265. if (0 !=
  266. mysql_stmt_fetch_column (stmt,
  267. results,
  268. column,
  269. 0))
  270. {
  271. GNUNET_free (buf);
  272. return GNUNET_SYSERR;
  273. }
  274. *pk = GNUNET_CRYPTO_rsa_public_key_decode (buf,
  275. size);
  276. GNUNET_free (buf);
  277. if (NULL == *pk)
  278. {
  279. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  280. "Results contains bogus public key value (fail to decode)\n");
  281. return GNUNET_SYSERR;
  282. }
  283. return GNUNET_OK;
  284. }
  285. /**
  286. * Function called to clean up memory allocated
  287. * by a #GNUNET_MY_ResultConverter.
  288. *
  289. * @param cls closure
  290. * @param rs result data to clean up
  291. */
  292. static void
  293. clean_rsa_public_key (void *cls,
  294. struct GNUNET_MY_ResultSpec *rs)
  295. {
  296. struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst;
  297. if (NULL != *pk)
  298. {
  299. GNUNET_CRYPTO_rsa_public_key_free (*pk);
  300. *pk = NULL;
  301. }
  302. }
  303. /**
  304. * RSA public key expected
  305. *
  306. * @param name name of the field in the table
  307. * @param[out] rsa where to store the result
  308. * @return array entry for the result specification to use
  309. */
  310. struct GNUNET_MY_ResultSpec
  311. GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa)
  312. {
  313. struct GNUNET_MY_ResultSpec res = {
  314. .pre_conv = &pre_extract_rsa_public_key,
  315. .post_conv = &post_extract_rsa_public_key,
  316. .cleaner = &clean_rsa_public_key,
  317. .dst = (void *) rsa,
  318. .dst_size = 0,
  319. .num_fields = 1
  320. };
  321. return res;
  322. }
  323. /**
  324. * Extract data from a Mysql database @a result at row @a row.
  325. *
  326. * @param cls closure
  327. * @param[in,out] rs
  328. * @param stmt the mysql statement that is being run
  329. * @param column the column that is being processed
  330. * @param[out] results
  331. * @return
  332. * #GNUNET_OK if all results could be extracted
  333. * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
  334. */
  335. static int
  336. pre_extract_rsa_signature (void *cls,
  337. struct GNUNET_MY_ResultSpec *rs,
  338. MYSQL_STMT *stmt,
  339. unsigned int column,
  340. MYSQL_BIND *results)
  341. {
  342. results[0].buffer = 0;
  343. results[0].buffer_length = 0;
  344. results[0].length = &rs->mysql_bind_output_length;
  345. results[0].buffer_type = MYSQL_TYPE_BLOB;
  346. results[0].is_null = &rs->is_null;
  347. rs->is_null = 0;
  348. return GNUNET_OK;
  349. }
  350. /**
  351. * Extract data from a Mysql database @a result at row @a row.
  352. *
  353. * @param cls closure
  354. * @param[in,out] rs
  355. * @param stmt the mysql statement that is being run
  356. * @param column the column that is being processed
  357. * @param[out] results
  358. * @return
  359. * #GNUNET_OK if all results could be extracted
  360. * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
  361. */
  362. static int
  363. post_extract_rsa_signature (void *cls,
  364. struct GNUNET_MY_ResultSpec *rs,
  365. MYSQL_STMT *stmt,
  366. unsigned int column,
  367. MYSQL_BIND *results)
  368. {
  369. struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
  370. void *buf;
  371. size_t size;
  372. if (*results->is_null)
  373. return GNUNET_SYSERR;
  374. size = (size_t) rs->mysql_bind_output_length;
  375. if (rs->mysql_bind_output_length != size)
  376. return GNUNET_SYSERR; /* 'unsigned long' does not fit in size_t!? */
  377. buf = GNUNET_malloc (size);
  378. results[0].buffer = buf;
  379. results[0].buffer_length = size;
  380. results[0].buffer_type = MYSQL_TYPE_BLOB;
  381. if (0 !=
  382. mysql_stmt_fetch_column (stmt,
  383. results,
  384. column,
  385. 0))
  386. {
  387. GNUNET_free (buf);
  388. return GNUNET_SYSERR;
  389. }
  390. *sig = GNUNET_CRYPTO_rsa_signature_decode (buf,
  391. size);
  392. GNUNET_free (buf);
  393. if (NULL == *sig)
  394. {
  395. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  396. "Resuls contains bogus signature value (fails to decode)\n");
  397. return GNUNET_SYSERR;
  398. }
  399. return GNUNET_OK;
  400. }
  401. /**
  402. * Function called to clean up memory allocated
  403. * by a #GNUNET_MY_ResultConverter.
  404. *
  405. * @param cls closure
  406. * @param rd result data to clean up
  407. */
  408. static void
  409. clean_rsa_signature (void *cls,
  410. struct GNUNET_MY_ResultSpec *rs)
  411. {
  412. struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst;
  413. if (NULL != *sig)
  414. {
  415. GNUNET_CRYPTO_rsa_signature_free (*sig);
  416. *sig = NULL;
  417. }
  418. }
  419. /**
  420. * RSA signature expected.
  421. *
  422. * @param[out] sig where to store the result;
  423. * @return array entry for the result specification to use
  424. */
  425. struct GNUNET_MY_ResultSpec
  426. GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
  427. {
  428. struct GNUNET_MY_ResultSpec res = {
  429. .pre_conv = &pre_extract_rsa_signature,
  430. .post_conv = &post_extract_rsa_signature,
  431. .cleaner = &clean_rsa_signature,
  432. .dst = (void *) sig,
  433. .dst_size = 0,
  434. .num_fields = 1
  435. };
  436. return res;
  437. }
  438. /**
  439. * Extract data from a Mysql database @a result at row @a row
  440. *
  441. * @param cls closure
  442. * @param[in,out] rs
  443. * @param stmt the mysql statement that is being run
  444. * @param column the column that is being processed
  445. * @param[out] results
  446. * @return
  447. * #GNUNET_OK if all results could be extracted
  448. * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
  449. */
  450. static int
  451. pre_extract_string (void *cls,
  452. struct GNUNET_MY_ResultSpec *rs,
  453. MYSQL_STMT *stmt,
  454. unsigned int column,
  455. MYSQL_BIND *results)
  456. {
  457. results[0].buffer = NULL;
  458. results[0].buffer_length = 0;
  459. results[0].length = &rs->mysql_bind_output_length;
  460. results[0].buffer_type = MYSQL_TYPE_BLOB;
  461. results[0].is_null = &rs->is_null;
  462. rs->is_null = 0;
  463. return GNUNET_OK;
  464. }
  465. /**
  466. * Check size of extracted fixed size data from a Mysql database @a
  467. *
  468. * @param cls closure
  469. * @param[in,out] rs
  470. * @param stmt the mysql statement that is being run
  471. * @param column the column that is being processed
  472. * @param[out] results
  473. * @return
  474. * #GNUNET_OK if all results could be extracted
  475. * #GNUNET_SYSERR if a result was invalid (non existing field or NULL)
  476. */
  477. static int
  478. post_extract_string (void *cls,
  479. struct GNUNET_MY_ResultSpec *rs,
  480. MYSQL_STMT *stmt,
  481. unsigned int column,
  482. MYSQL_BIND *results)
  483. {
  484. size_t size = (size_t) rs->mysql_bind_output_length;
  485. char *buf;
  486. if (rs->mysql_bind_output_length != size)
  487. return GNUNET_SYSERR;
  488. if (*results->is_null)
  489. {
  490. *(void **) rs->dst = NULL;
  491. return GNUNET_OK;
  492. }
  493. buf = GNUNET_malloc (size);
  494. results[0].buffer = buf;
  495. results[0].buffer_length = size;
  496. results[0].buffer_type = MYSQL_TYPE_BLOB;
  497. if (0 !=
  498. mysql_stmt_fetch_column (stmt,
  499. results,
  500. column,
  501. 0))
  502. {
  503. GNUNET_free (buf);
  504. return GNUNET_SYSERR;
  505. }
  506. buf[size] = '\0';
  507. *(void **) rs->dst = buf;
  508. return GNUNET_OK;
  509. }
  510. /**
  511. * 0- terminated string exprected.
  512. *
  513. * @param[out] dst where to store the result, allocated
  514. * @return array entry for the result specification to use
  515. */
  516. struct GNUNET_MY_ResultSpec
  517. GNUNET_MY_result_spec_string (char **dst)
  518. {
  519. struct GNUNET_MY_ResultSpec res = {
  520. .pre_conv = &pre_extract_string,
  521. .post_conv = &post_extract_string,
  522. .cleaner = NULL,
  523. .dst = (void *) dst,
  524. .dst_size = 0,
  525. .num_fields = 1
  526. };
  527. return res;
  528. }
  529. /**
  530. * Absolute time expected
  531. *
  532. * @param name name of the field in the table
  533. * @param[out] at where to store the result
  534. * @return array entry for the result specification to use
  535. */
  536. struct GNUNET_MY_ResultSpec
  537. GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
  538. {
  539. return GNUNET_MY_result_spec_uint64 (&at->abs_value_us);
  540. }
  541. /**
  542. * Absolute time in network byte order expected
  543. *
  544. * @param[out] at where to store the result
  545. * @return array entry for the result specification to use
  546. */
  547. struct GNUNET_MY_ResultSpec
  548. GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at)
  549. {
  550. struct GNUNET_MY_ResultSpec res =
  551. GNUNET_MY_result_spec_auto_from_type (&at->abs_value_us__);
  552. return res;
  553. }
  554. /**
  555. * Extract data from a Postgres database @a result at row @a row.
  556. *
  557. * @param cls closure
  558. * @param[in,out] rs
  559. * @param stmt the mysql statement that is being run
  560. * @param column the column that is being processed
  561. * @param[out] results
  562. * @return
  563. * #GNUNET_YES if all results could be extracted
  564. * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
  565. */
  566. static int
  567. pre_extract_uint16 (void *cls,
  568. struct GNUNET_MY_ResultSpec *rs,
  569. MYSQL_STMT *stmt,
  570. unsigned int column,
  571. MYSQL_BIND *results)
  572. {
  573. results[0].buffer = rs->dst;
  574. results[0].buffer_length = rs->dst_size;
  575. results[0].length = &rs->mysql_bind_output_length;
  576. results[0].buffer_type = MYSQL_TYPE_SHORT;
  577. results[0].is_null = &rs->is_null;
  578. rs->is_null = 0;
  579. return GNUNET_OK;
  580. }
  581. /**
  582. * Check size of extracted fixed size data from a Mysql database.
  583. *
  584. * @param cls closure
  585. * @param[in,out] rs
  586. * @param stmt the mysql statement that is being run
  587. * @param column the column that is being processed
  588. * @param[out] results
  589. * @return
  590. * #GNUNET_YES if all results could be extracted
  591. * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
  592. */
  593. static int
  594. post_extract_uint16 (void *cls,
  595. struct GNUNET_MY_ResultSpec *rs,
  596. MYSQL_STMT *stmt,
  597. unsigned int column,
  598. MYSQL_BIND *results)
  599. {
  600. if (rs->dst_size != rs->mysql_bind_output_length)
  601. return GNUNET_SYSERR;
  602. if (*results->is_null)
  603. return GNUNET_SYSERR;
  604. return GNUNET_OK;
  605. }
  606. /**
  607. * uint16_t expected
  608. *
  609. * @param[out] u16 where to store the result
  610. * @return array entry for the result specification to use
  611. */
  612. struct GNUNET_MY_ResultSpec
  613. GNUNET_MY_result_spec_uint16 (uint16_t *u16)
  614. {
  615. struct GNUNET_MY_ResultSpec res = {
  616. .pre_conv = &pre_extract_uint16,
  617. .post_conv = &post_extract_uint16,
  618. .cleaner = NULL,
  619. .dst = (void *) u16,
  620. .dst_size = sizeof(*u16),
  621. .num_fields = 1
  622. };
  623. return res;
  624. }
  625. /**
  626. * Extract data from a MYSQL database @a result at row @a row
  627. *
  628. * @param cls closure
  629. * @param cls closure
  630. * @param[in,out] rs
  631. * @param stmt the mysql statement that is being run
  632. * @param column the column that is being processed
  633. * @param[out] results
  634. * @return
  635. * #GNUNET_OK if all results could be extracted
  636. * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
  637. */
  638. static int
  639. pre_extract_uint32 (void *cls,
  640. struct GNUNET_MY_ResultSpec *rs,
  641. MYSQL_STMT *stmt,
  642. unsigned int column,
  643. MYSQL_BIND *results)
  644. {
  645. results[0].buffer = rs->dst;
  646. results[0].buffer_length = rs->dst_size;
  647. results[0].length = &rs->mysql_bind_output_length;
  648. results[0].buffer_type = MYSQL_TYPE_LONG;
  649. results[0].is_null = &rs->is_null;
  650. rs->is_null = 0;
  651. return GNUNET_OK;
  652. }
  653. /**
  654. * Extract data from a MYSQL database @a result at row @a row
  655. *
  656. * @param cls closure
  657. * @param cls closure
  658. * @param[in,out] rs
  659. * @param stmt the mysql statement that is being run
  660. * @param column the column that is being processed
  661. * @param[out] results
  662. * @return
  663. * #GNUNET_OK if all results could be extracted
  664. * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
  665. */
  666. static int
  667. post_extract_uint32 (void *cls,
  668. struct GNUNET_MY_ResultSpec *rs,
  669. MYSQL_STMT *stmt,
  670. unsigned int column,
  671. MYSQL_BIND *results)
  672. {
  673. if (rs->dst_size != rs->mysql_bind_output_length)
  674. return GNUNET_SYSERR;
  675. if (*results->is_null)
  676. return GNUNET_SYSERR;
  677. return GNUNET_OK;
  678. }
  679. /**
  680. * uint32_t expected
  681. *
  682. * @param[out] u32 where to store the result
  683. * @return array entry for the result specification to use
  684. */
  685. struct GNUNET_MY_ResultSpec
  686. GNUNET_MY_result_spec_uint32 (uint32_t *u32)
  687. {
  688. struct GNUNET_MY_ResultSpec res = {
  689. .pre_conv = &pre_extract_uint32,
  690. .post_conv = &post_extract_uint32,
  691. .cleaner = NULL,
  692. .dst = (void *) u32,
  693. .dst_size = sizeof(*u32),
  694. .num_fields = 1
  695. };
  696. return res;
  697. }
  698. /**
  699. * Extract data from a MYSQL database @a result at row @a row
  700. *
  701. * @param cls closure
  702. * @param[in,out] rs
  703. * @param stmt the mysql statement that is being run
  704. * @param column the column that is being processed
  705. * @param[out] results
  706. * @return
  707. * #GNUNET_OK if all results could be extracted
  708. * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
  709. */
  710. static int
  711. pre_extract_uint64 (void *cls,
  712. struct GNUNET_MY_ResultSpec *rs,
  713. MYSQL_STMT *stmt,
  714. unsigned int column,
  715. MYSQL_BIND *results)
  716. {
  717. if (sizeof(uint64_t) != rs->dst_size)
  718. return GNUNET_SYSERR;
  719. results[0].buffer = rs->dst;
  720. results[0].buffer_length = rs->dst_size;
  721. results[0].length = &rs->mysql_bind_output_length;
  722. results[0].buffer_type = MYSQL_TYPE_LONGLONG;
  723. results[0].is_null = &rs->is_null;
  724. rs->is_null = 0;
  725. return GNUNET_OK;
  726. }
  727. /**
  728. * Check size of extracted fixed-size data from a Mysql database
  729. *
  730. * @param cls closure
  731. * @param[in,out] rs
  732. * @param stmt the mysql statement that is being run
  733. * @param column the column that is being processed
  734. * @param[out] results
  735. * @return
  736. * #GNUNET_OK if all results could be extracted
  737. * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
  738. */
  739. static int
  740. post_extract_uint64 (void *cls,
  741. struct GNUNET_MY_ResultSpec *rs,
  742. MYSQL_STMT *stmt,
  743. unsigned int column,
  744. MYSQL_BIND *results)
  745. {
  746. if (sizeof(uint64_t) != rs->dst_size)
  747. return GNUNET_SYSERR;
  748. if (*results->is_null)
  749. return GNUNET_SYSERR;
  750. return GNUNET_OK;
  751. }
  752. /**
  753. * uint64_t expected.
  754. *
  755. * @param[out] u64 where to store the result
  756. * @return array entry for the result specification to use
  757. */
  758. struct GNUNET_MY_ResultSpec
  759. GNUNET_MY_result_spec_uint64 (uint64_t *u64)
  760. {
  761. struct GNUNET_MY_ResultSpec res = {
  762. .pre_conv = &pre_extract_uint64,
  763. .post_conv = &post_extract_uint64,
  764. .cleaner = NULL,
  765. .dst = (void *) u64,
  766. .dst_size = sizeof(*u64),
  767. .num_fields = 1
  768. };
  769. return res;
  770. }
  771. /* end of my_result_helper.c */