sq_query_helper.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2017 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 sq/sq_query_helper.c
  18. * @brief helper functions for queries
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_sq_lib.h"
  23. /**
  24. * Function called to convert input argument into SQL parameters.
  25. *
  26. * @param cls closure
  27. * @param data pointer to input argument
  28. * @param data_len number of bytes in @a data (if applicable)
  29. * @param stmt sqlite statement to bind parameters for
  30. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  31. * so immediately suitable for passing to `sqlite3_bind`-functions.
  32. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  33. */
  34. static int
  35. bind_fixed_blob (void *cls,
  36. const void *data,
  37. size_t data_len,
  38. sqlite3_stmt *stmt,
  39. unsigned int off)
  40. {
  41. if (SQLITE_OK !=
  42. sqlite3_bind_blob64 (stmt,
  43. (int) off,
  44. data,
  45. (sqlite3_uint64) data_len,
  46. SQLITE_TRANSIENT))
  47. return GNUNET_SYSERR;
  48. return GNUNET_OK;
  49. }
  50. /**
  51. * Generate query parameter for a buffer @a ptr of
  52. * @a ptr_size bytes.
  53. *
  54. * @param ptr pointer to the query parameter to pass
  55. * @oaran ptr_size number of bytes in @a ptr
  56. */
  57. struct GNUNET_SQ_QueryParam
  58. GNUNET_SQ_query_param_fixed_size (const void *ptr,
  59. size_t ptr_size)
  60. {
  61. struct GNUNET_SQ_QueryParam qp = {
  62. .conv = &bind_fixed_blob,
  63. .data = ptr,
  64. .size = ptr_size,
  65. .num_params = 1
  66. };
  67. return qp;
  68. }
  69. /**
  70. * Function called to convert input argument into SQL parameters.
  71. *
  72. * @param cls closure
  73. * @param data pointer to input argument
  74. * @param data_len number of bytes in @a data (if applicable)
  75. * @param stmt sqlite statement to bind parameters for
  76. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  77. * so immediately suitable for passing to `sqlite3_bind`-functions.
  78. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  79. */
  80. static int
  81. bind_string (void *cls,
  82. const void *data,
  83. size_t data_len,
  84. sqlite3_stmt *stmt,
  85. unsigned int off)
  86. {
  87. if (NULL == data)
  88. {
  89. if (SQLITE_OK !=
  90. sqlite3_bind_null (stmt,
  91. (int) off))
  92. return GNUNET_SYSERR;
  93. return GNUNET_OK;
  94. }
  95. if (SQLITE_OK !=
  96. sqlite3_bind_text (stmt,
  97. (int) off,
  98. (const char *) data,
  99. -1,
  100. SQLITE_TRANSIENT))
  101. return GNUNET_SYSERR;
  102. return GNUNET_OK;
  103. }
  104. /**
  105. * Generate query parameter for a string.
  106. *
  107. * @param ptr pointer to the string query parameter to pass
  108. */
  109. struct GNUNET_SQ_QueryParam
  110. GNUNET_SQ_query_param_string (const char *ptr)
  111. {
  112. struct GNUNET_SQ_QueryParam qp = {
  113. .conv = &bind_string,
  114. .data = ptr,
  115. .num_params = 1
  116. };
  117. return qp;
  118. }
  119. /**
  120. * Function called to convert input argument into SQL parameters.
  121. *
  122. * @param cls closure
  123. * @param data pointer to input argument
  124. * @param data_len number of bytes in @a data (if applicable)
  125. * @param stmt sqlite statement to bind parameters for
  126. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  127. * so immediately suitable for passing to `sqlite3_bind`-functions.
  128. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  129. */
  130. static int
  131. bind_rsa_pub (void *cls,
  132. const void *data,
  133. size_t data_len,
  134. sqlite3_stmt *stmt,
  135. unsigned int off)
  136. {
  137. const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
  138. void *buf;
  139. size_t buf_size;
  140. GNUNET_break (NULL == cls);
  141. buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
  142. &buf);
  143. if (SQLITE_OK !=
  144. sqlite3_bind_blob64 (stmt,
  145. (int) off,
  146. buf,
  147. (sqlite3_uint64) buf_size,
  148. SQLITE_TRANSIENT))
  149. {
  150. GNUNET_free (buf);
  151. return GNUNET_SYSERR;
  152. }
  153. GNUNET_free (buf);
  154. return GNUNET_OK;
  155. }
  156. /**
  157. * Generate query parameter for an RSA public key. The
  158. * database must contain a BLOB type in the respective position.
  159. *
  160. * @param x the query parameter to pass.
  161. */
  162. struct GNUNET_SQ_QueryParam
  163. GNUNET_SQ_query_param_rsa_public_key (const struct
  164. GNUNET_CRYPTO_RsaPublicKey *x)
  165. {
  166. struct GNUNET_SQ_QueryParam qp = {
  167. .conv = &bind_rsa_pub,
  168. .data = x,
  169. .num_params = 1
  170. };
  171. return qp;
  172. }
  173. /**
  174. * Function called to convert input argument into SQL parameters.
  175. *
  176. * @param cls closure
  177. * @param data pointer to input argument
  178. * @param data_len number of bytes in @a data (if applicable)
  179. * @param stmt sqlite statement to bind parameters for
  180. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  181. * so immediately suitable for passing to `sqlite3_bind`-functions.
  182. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  183. */
  184. static int
  185. bind_rsa_sig (void *cls,
  186. const void *data,
  187. size_t data_len,
  188. sqlite3_stmt *stmt,
  189. unsigned int off)
  190. {
  191. const struct GNUNET_CRYPTO_RsaSignature *sig = data;
  192. void *buf;
  193. size_t buf_size;
  194. GNUNET_break (NULL == cls);
  195. buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
  196. &buf);
  197. if (SQLITE_OK !=
  198. sqlite3_bind_blob64 (stmt,
  199. (int) off,
  200. buf,
  201. (sqlite3_uint64) buf_size,
  202. SQLITE_TRANSIENT))
  203. {
  204. GNUNET_free (buf);
  205. return GNUNET_SYSERR;
  206. }
  207. GNUNET_free (buf);
  208. return GNUNET_OK;
  209. }
  210. /**
  211. * Generate query parameter for an RSA signature. The
  212. * database must contain a BLOB type in the respective position.
  213. *
  214. * @param x the query parameter to pass
  215. */
  216. struct GNUNET_SQ_QueryParam
  217. GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
  218. {
  219. struct GNUNET_SQ_QueryParam qp = {
  220. .conv = &bind_rsa_sig,
  221. .data = x,
  222. .num_params = 1
  223. };
  224. return qp;
  225. }
  226. /**
  227. * Function called to convert input argument into SQL parameters.
  228. *
  229. * @param cls closure
  230. * @param data pointer to input argument
  231. * @param data_len number of bytes in @a data (if applicable)
  232. * @param stmt sqlite statement to bind parameters for
  233. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  234. * so immediately suitable for passing to `sqlite3_bind`-functions.
  235. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  236. */
  237. static int
  238. bind_abstime (void *cls,
  239. const void *data,
  240. size_t data_len,
  241. sqlite3_stmt *stmt,
  242. unsigned int off)
  243. {
  244. const struct GNUNET_TIME_Absolute *u = data;
  245. struct GNUNET_TIME_Absolute abs;
  246. abs = *u;
  247. if (abs.abs_value_us > INT64_MAX)
  248. abs.abs_value_us = INT64_MAX;
  249. GNUNET_assert (sizeof(uint64_t) == data_len);
  250. if (SQLITE_OK !=
  251. sqlite3_bind_int64 (stmt,
  252. (int) off,
  253. (sqlite3_int64) abs.abs_value_us))
  254. return GNUNET_SYSERR;
  255. return GNUNET_OK;
  256. }
  257. /**
  258. * Generate query parameter for an absolute time value.
  259. * The database must store a 64-bit integer.
  260. *
  261. * @param x pointer to the query parameter to pass
  262. */
  263. struct GNUNET_SQ_QueryParam
  264. GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
  265. {
  266. struct GNUNET_SQ_QueryParam qp = {
  267. .conv = &bind_abstime,
  268. .data = x,
  269. .size = sizeof(struct GNUNET_TIME_Absolute),
  270. .num_params = 1
  271. };
  272. return qp;
  273. }
  274. /**
  275. * Function called to convert input argument into SQL parameters.
  276. *
  277. * @param cls closure
  278. * @param data pointer to input argument
  279. * @param data_len number of bytes in @a data (if applicable)
  280. * @param stmt sqlite statement to bind parameters for
  281. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  282. * so immediately suitable for passing to `sqlite3_bind`-functions.
  283. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  284. */
  285. static int
  286. bind_nbotime (void *cls,
  287. const void *data,
  288. size_t data_len,
  289. sqlite3_stmt *stmt,
  290. unsigned int off)
  291. {
  292. const struct GNUNET_TIME_AbsoluteNBO *u = data;
  293. struct GNUNET_TIME_Absolute abs;
  294. abs = GNUNET_TIME_absolute_ntoh (*u);
  295. if (abs.abs_value_us > INT64_MAX)
  296. abs.abs_value_us = INT64_MAX;
  297. GNUNET_assert (sizeof(uint64_t) == data_len);
  298. if (SQLITE_OK !=
  299. sqlite3_bind_int64 (stmt,
  300. (int) off,
  301. (sqlite3_int64) abs.abs_value_us))
  302. return GNUNET_SYSERR;
  303. return GNUNET_OK;
  304. }
  305. /**
  306. * Generate query parameter for an absolute time value.
  307. * The database must store a 64-bit integer.
  308. *
  309. * @param x pointer to the query parameter to pass
  310. */
  311. struct GNUNET_SQ_QueryParam
  312. GNUNET_SQ_query_param_absolute_time_nbo (const struct
  313. GNUNET_TIME_AbsoluteNBO *x)
  314. {
  315. struct GNUNET_SQ_QueryParam qp = {
  316. .conv = &bind_nbotime,
  317. .data = x,
  318. .size = sizeof(struct GNUNET_TIME_AbsoluteNBO),
  319. .num_params = 1
  320. };
  321. return qp;
  322. }
  323. /**
  324. * Function called to convert input argument into SQL parameters.
  325. *
  326. * @param cls closure
  327. * @param data pointer to input argument
  328. * @param data_len number of bytes in @a data (if applicable)
  329. * @param stmt sqlite statement to bind parameters for
  330. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  331. * so immediately suitable for passing to `sqlite3_bind`-functions.
  332. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  333. */
  334. static int
  335. bind_u16 (void *cls,
  336. const void *data,
  337. size_t data_len,
  338. sqlite3_stmt *stmt,
  339. unsigned int off)
  340. {
  341. const uint16_t *u = data;
  342. GNUNET_assert (sizeof(uint16_t) == data_len);
  343. if (SQLITE_OK !=
  344. sqlite3_bind_int (stmt,
  345. (int) off,
  346. (int) *u))
  347. return GNUNET_SYSERR;
  348. return GNUNET_OK;
  349. }
  350. /**
  351. * Generate query parameter for an uint16_t in host byte order.
  352. *
  353. * @param x pointer to the query parameter to pass
  354. */
  355. struct GNUNET_SQ_QueryParam
  356. GNUNET_SQ_query_param_uint16 (const uint16_t *x)
  357. {
  358. struct GNUNET_SQ_QueryParam qp = {
  359. .conv = &bind_u16,
  360. .data = x,
  361. .size = sizeof(uint16_t),
  362. .num_params = 1
  363. };
  364. return qp;
  365. }
  366. /**
  367. * Function called to convert input argument into SQL parameters.
  368. *
  369. * @param cls closure
  370. * @param data pointer to input argument
  371. * @param data_len number of bytes in @a data (if applicable)
  372. * @param stmt sqlite statement to bind parameters for
  373. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  374. * so immediately suitable for passing to `sqlite3_bind`-functions.
  375. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  376. */
  377. static int
  378. bind_u32 (void *cls,
  379. const void *data,
  380. size_t data_len,
  381. sqlite3_stmt *stmt,
  382. unsigned int off)
  383. {
  384. const uint32_t *u = data;
  385. GNUNET_assert (sizeof(uint32_t) == data_len);
  386. if (SQLITE_OK !=
  387. sqlite3_bind_int64 (stmt,
  388. (int) off,
  389. (sqlite3_int64) * u))
  390. return GNUNET_SYSERR;
  391. return GNUNET_OK;
  392. }
  393. /**
  394. * Generate query parameter for an uint32_t in host byte order.
  395. *
  396. * @param x pointer to the query parameter to pass
  397. */
  398. struct GNUNET_SQ_QueryParam
  399. GNUNET_SQ_query_param_uint32 (const uint32_t *x)
  400. {
  401. struct GNUNET_SQ_QueryParam qp = {
  402. .conv = &bind_u32,
  403. .data = x,
  404. .size = sizeof(uint32_t),
  405. .num_params = 1
  406. };
  407. return qp;
  408. }
  409. /**
  410. * Function called to convert input argument into SQL parameters.
  411. *
  412. * @param cls closure
  413. * @param data pointer to input argument
  414. * @param data_len number of bytes in @a data (if applicable)
  415. * @param stmt sqlite statement to bind parameters for
  416. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  417. * so immediately suitable for passing to `sqlite3_bind`-functions.
  418. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  419. */
  420. static int
  421. bind_u64 (void *cls,
  422. const void *data,
  423. size_t data_len,
  424. sqlite3_stmt *stmt,
  425. unsigned int off)
  426. {
  427. const uint64_t *u = data;
  428. GNUNET_assert (sizeof(uint64_t) == data_len);
  429. if (SQLITE_OK !=
  430. sqlite3_bind_int64 (stmt,
  431. (int) off,
  432. (sqlite3_int64) * u))
  433. return GNUNET_SYSERR;
  434. return GNUNET_OK;
  435. }
  436. /**
  437. * Generate query parameter for an uint16_t in host byte order.
  438. *
  439. * @param x pointer to the query parameter to pass
  440. */
  441. struct GNUNET_SQ_QueryParam
  442. GNUNET_SQ_query_param_uint64 (const uint64_t *x)
  443. {
  444. struct GNUNET_SQ_QueryParam qp = {
  445. .conv = &bind_u64,
  446. .data = x,
  447. .size = sizeof(uint64_t),
  448. .num_params = 1
  449. };
  450. return qp;
  451. }
  452. /* end of sq_query_helper.c */