pq_query_helper.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 pq/pq_query_helper.c
  18. * @brief functions to initialize parameter arrays
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_pq_lib.h"
  24. /**
  25. * Function called to convert input argument into SQL parameters.
  26. *
  27. * @param cls closure
  28. * @param data pointer to input argument
  29. * @param data_len number of bytes in @a data (if applicable)
  30. * @param[out] param_values SQL data to set
  31. * @param[out] param_lengths SQL length data to set
  32. * @param[out] param_formats SQL format data to set
  33. * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
  34. * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
  35. * @param scratch_length number of entries left in @a scratch
  36. * @return -1 on error, number of offsets used in @a scratch otherwise
  37. */
  38. static int
  39. qconv_fixed (void *cls,
  40. const void *data,
  41. size_t data_len,
  42. void *param_values[],
  43. int param_lengths[],
  44. int param_formats[],
  45. unsigned int param_length,
  46. void *scratch[],
  47. unsigned int scratch_length)
  48. {
  49. (void) scratch;
  50. (void) scratch_length;
  51. GNUNET_break (NULL == cls);
  52. if (1 != param_length)
  53. return -1;
  54. param_values[0] = (void *) data;
  55. param_lengths[0] = data_len;
  56. param_formats[0] = 1;
  57. return 0;
  58. }
  59. /**
  60. * Generate query parameter for a buffer @a ptr of
  61. * @a ptr_size bytes.
  62. *
  63. * @param ptr pointer to the query parameter to pass
  64. * @oaran ptr_size number of bytes in @a ptr
  65. */
  66. struct GNUNET_PQ_QueryParam
  67. GNUNET_PQ_query_param_fixed_size (const void *ptr,
  68. size_t ptr_size)
  69. {
  70. struct GNUNET_PQ_QueryParam res =
  71. { &qconv_fixed, NULL, ptr, ptr_size, 1 };
  72. return res;
  73. }
  74. /**
  75. * Generate query parameter for a string.
  76. *
  77. * @param ptr pointer to the string query parameter to pass
  78. */
  79. struct GNUNET_PQ_QueryParam
  80. GNUNET_PQ_query_param_string (const char *ptr)
  81. {
  82. return GNUNET_PQ_query_param_fixed_size (ptr, strlen (ptr));
  83. }
  84. /**
  85. * Function called to convert input argument into SQL parameters.
  86. *
  87. * @param cls closure
  88. * @param data pointer to input argument
  89. * @param data_len number of bytes in @a data (if applicable)
  90. * @param[out] param_values SQL data to set
  91. * @param[out] param_lengths SQL length data to set
  92. * @param[out] param_formats SQL format data to set
  93. * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
  94. * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
  95. * @param scratch_length number of entries left in @a scratch
  96. * @return -1 on error, number of offsets used in @a scratch otherwise
  97. */
  98. static int
  99. qconv_uint16 (void *cls,
  100. const void *data,
  101. size_t data_len,
  102. void *param_values[],
  103. int param_lengths[],
  104. int param_formats[],
  105. unsigned int param_length,
  106. void *scratch[],
  107. unsigned int scratch_length)
  108. {
  109. const uint16_t *u_hbo = data;
  110. uint16_t *u_nbo;
  111. (void) scratch;
  112. (void) scratch_length;
  113. GNUNET_break (NULL == cls);
  114. if (1 != param_length)
  115. return -1;
  116. u_nbo = GNUNET_new (uint16_t);
  117. scratch[0] = u_nbo;
  118. *u_nbo = htons (*u_hbo);
  119. param_values[0] = (void *) u_nbo;
  120. param_lengths[0] = sizeof (uint16_t);
  121. param_formats[0] = 1;
  122. return 1;
  123. }
  124. /**
  125. * Generate query parameter for an uint16_t in host byte order.
  126. *
  127. * @param x pointer to the query parameter to pass
  128. */
  129. struct GNUNET_PQ_QueryParam
  130. GNUNET_PQ_query_param_uint16 (const uint16_t *x)
  131. {
  132. struct GNUNET_PQ_QueryParam res =
  133. { &qconv_uint16, NULL, x, sizeof (*x), 1 };
  134. return res;
  135. }
  136. /**
  137. * Function called to convert input argument into SQL parameters.
  138. *
  139. * @param cls closure
  140. * @param data pointer to input argument
  141. * @param data_len number of bytes in @a data (if applicable)
  142. * @param[out] param_values SQL data to set
  143. * @param[out] param_lengths SQL length data to set
  144. * @param[out] param_formats SQL format data to set
  145. * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
  146. * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
  147. * @param scratch_length number of entries left in @a scratch
  148. * @return -1 on error, number of offsets used in @a scratch otherwise
  149. */
  150. static int
  151. qconv_uint32 (void *cls,
  152. const void *data,
  153. size_t data_len,
  154. void *param_values[],
  155. int param_lengths[],
  156. int param_formats[],
  157. unsigned int param_length,
  158. void *scratch[],
  159. unsigned int scratch_length)
  160. {
  161. const uint32_t *u_hbo = data;
  162. uint32_t *u_nbo;
  163. (void) scratch;
  164. (void) scratch_length;
  165. GNUNET_break (NULL == cls);
  166. if (1 != param_length)
  167. return -1;
  168. u_nbo = GNUNET_new (uint32_t);
  169. scratch[0] = u_nbo;
  170. *u_nbo = htonl (*u_hbo);
  171. param_values[0] = (void *) u_nbo;
  172. param_lengths[0] = sizeof (uint32_t);
  173. param_formats[0] = 1;
  174. return 1;
  175. }
  176. /**
  177. * Generate query parameter for an uint32_t in host byte order.
  178. *
  179. * @param x pointer to the query parameter to pass
  180. */
  181. struct GNUNET_PQ_QueryParam
  182. GNUNET_PQ_query_param_uint32 (const uint32_t *x)
  183. {
  184. struct GNUNET_PQ_QueryParam res =
  185. { &qconv_uint32, NULL, x, sizeof (*x), 1 };
  186. return res;
  187. }
  188. /**
  189. * Function called to convert input argument into SQL parameters.
  190. *
  191. * @param cls closure
  192. * @param data pointer to input argument
  193. * @param data_len number of bytes in @a data (if applicable)
  194. * @param[out] param_values SQL data to set
  195. * @param[out] param_lengths SQL length data to set
  196. * @param[out] param_formats SQL format data to set
  197. * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
  198. * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
  199. * @param scratch_length number of entries left in @a scratch
  200. * @return -1 on error, number of offsets used in @a scratch otherwise
  201. */
  202. static int
  203. qconv_uint64 (void *cls,
  204. const void *data,
  205. size_t data_len,
  206. void *param_values[],
  207. int param_lengths[],
  208. int param_formats[],
  209. unsigned int param_length,
  210. void *scratch[],
  211. unsigned int scratch_length)
  212. {
  213. const uint64_t *u_hbo = data;
  214. uint64_t *u_nbo;
  215. (void) scratch;
  216. (void) scratch_length;
  217. GNUNET_break (NULL == cls);
  218. if (1 != param_length)
  219. return -1;
  220. u_nbo = GNUNET_new (uint64_t);
  221. scratch[0] = u_nbo;
  222. *u_nbo = GNUNET_htonll (*u_hbo);
  223. param_values[0] = (void *) u_nbo;
  224. param_lengths[0] = sizeof (uint64_t);
  225. param_formats[0] = 1;
  226. return 1;
  227. }
  228. /**
  229. * Generate query parameter for an uint64_t in host byte order.
  230. *
  231. * @param x pointer to the query parameter to pass
  232. */
  233. struct GNUNET_PQ_QueryParam
  234. GNUNET_PQ_query_param_uint64 (const uint64_t *x)
  235. {
  236. struct GNUNET_PQ_QueryParam res =
  237. { &qconv_uint64, NULL, x, sizeof (*x), 1 };
  238. return res;
  239. }
  240. /**
  241. * Function called to convert input argument into SQL parameters.
  242. *
  243. * @param cls closure
  244. * @param data pointer to input argument
  245. * @param data_len number of bytes in @a data (if applicable)
  246. * @param[out] param_values SQL data to set
  247. * @param[out] param_lengths SQL length data to set
  248. * @param[out] param_formats SQL format data to set
  249. * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
  250. * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
  251. * @param scratch_length number of entries left in @a scratch
  252. * @return -1 on error, number of offsets used in @a scratch otherwise
  253. */
  254. static int
  255. qconv_rsa_public_key (void *cls,
  256. const void *data,
  257. size_t data_len,
  258. void *param_values[],
  259. int param_lengths[],
  260. int param_formats[],
  261. unsigned int param_length,
  262. void *scratch[],
  263. unsigned int scratch_length)
  264. {
  265. const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
  266. char *buf;
  267. size_t buf_size;
  268. GNUNET_break (NULL == cls);
  269. if (1 != param_length)
  270. return -1;
  271. buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
  272. &buf);
  273. scratch[0] = buf;
  274. param_values[0] = (void *) buf;
  275. param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
  276. param_formats[0] = 1;
  277. return 1;
  278. }
  279. /**
  280. * Generate query parameter for an RSA public key. The
  281. * database must contain a BLOB type in the respective position.
  282. *
  283. * @param x the query parameter to pass
  284. * @return array entry for the query parameters to use
  285. */
  286. struct GNUNET_PQ_QueryParam
  287. GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
  288. {
  289. struct GNUNET_PQ_QueryParam res =
  290. { &qconv_rsa_public_key, NULL, (x), 0, 1 };
  291. return res;
  292. }
  293. /**
  294. * Function called to convert input argument into SQL parameters.
  295. *
  296. * @param cls closure
  297. * @param data pointer to input argument
  298. * @param data_len number of bytes in @a data (if applicable)
  299. * @param[out] param_values SQL data to set
  300. * @param[out] param_lengths SQL length data to set
  301. * @param[out] param_formats SQL format data to set
  302. * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
  303. * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
  304. * @param scratch_length number of entries left in @a scratch
  305. * @return -1 on error, number of offsets used in @a scratch otherwise
  306. */
  307. static int
  308. qconv_rsa_signature (void *cls,
  309. const void *data,
  310. size_t data_len,
  311. void *param_values[],
  312. int param_lengths[],
  313. int param_formats[],
  314. unsigned int param_length,
  315. void *scratch[],
  316. unsigned int scratch_length)
  317. {
  318. const struct GNUNET_CRYPTO_RsaSignature *sig = data;
  319. char *buf;
  320. size_t buf_size;
  321. GNUNET_break (NULL == cls);
  322. if (1 != param_length)
  323. return -1;
  324. buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
  325. &buf);
  326. scratch[0] = buf;
  327. param_values[0] = (void *) buf;
  328. param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
  329. param_formats[0] = 1;
  330. return 1;
  331. }
  332. /**
  333. * Generate query parameter for an RSA signature. The
  334. * database must contain a BLOB type in the respective position.
  335. *
  336. * @param x the query parameter to pass
  337. * @return array entry for the query parameters to use
  338. */
  339. struct GNUNET_PQ_QueryParam
  340. GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
  341. {
  342. struct GNUNET_PQ_QueryParam res =
  343. { &qconv_rsa_signature, NULL, (x), 0, 1 };
  344. return res;
  345. }
  346. /**
  347. * Function called to convert input argument into SQL parameters.
  348. *
  349. * @param cls closure
  350. * @param data pointer to input argument
  351. * @param data_len number of bytes in @a data (if applicable)
  352. * @param[out] param_values SQL data to set
  353. * @param[out] param_lengths SQL length data to set
  354. * @param[out] param_formats SQL format data to set
  355. * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
  356. * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
  357. * @param scratch_length number of entries left in @a scratch
  358. * @return -1 on error, number of offsets used in @a scratch otherwise
  359. */
  360. static int
  361. qconv_abs_time (void *cls,
  362. const void *data,
  363. size_t data_len,
  364. void *param_values[],
  365. int param_lengths[],
  366. int param_formats[],
  367. unsigned int param_length,
  368. void *scratch[],
  369. unsigned int scratch_length)
  370. {
  371. const struct GNUNET_TIME_Absolute *u = data;
  372. struct GNUNET_TIME_Absolute abs;
  373. uint64_t *u_nbo;
  374. GNUNET_break (NULL == cls);
  375. if (1 != param_length)
  376. return -1;
  377. abs = *u;
  378. if (abs.abs_value_us > INT64_MAX)
  379. abs.abs_value_us = INT64_MAX;
  380. u_nbo = GNUNET_new (uint64_t);
  381. scratch[0] = u_nbo;
  382. *u_nbo = GNUNET_htonll (abs.abs_value_us);
  383. param_values[0] = (void *) u_nbo;
  384. param_lengths[0] = sizeof (uint64_t);
  385. param_formats[0] = 1;
  386. return 1;
  387. }
  388. /**
  389. * Generate query parameter for an absolute time value.
  390. * The database must store a 64-bit integer.
  391. *
  392. * @param x pointer to the query parameter to pass
  393. * @return array entry for the query parameters to use
  394. */
  395. struct GNUNET_PQ_QueryParam
  396. GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
  397. {
  398. struct GNUNET_PQ_QueryParam res =
  399. { &qconv_abs_time, NULL, x, sizeof (*x), 1 };
  400. return res;
  401. }
  402. /**
  403. * Generate query parameter for an absolute time value.
  404. * The database must store a 64-bit integer.
  405. *
  406. * @param x pointer to the query parameter to pass
  407. */
  408. struct GNUNET_PQ_QueryParam
  409. GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
  410. {
  411. return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
  412. }
  413. /* end of pq_query_helper.c */