sq_query_helper.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. char *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 GNUNET_CRYPTO_RsaPublicKey *x)
  164. {
  165. struct GNUNET_SQ_QueryParam qp = {
  166. .conv = &bind_rsa_pub,
  167. .data = x,
  168. .num_params = 1
  169. };
  170. return qp;
  171. }
  172. /**
  173. * Function called to convert input argument into SQL parameters.
  174. *
  175. * @param cls closure
  176. * @param data pointer to input argument
  177. * @param data_len number of bytes in @a data (if applicable)
  178. * @param stmt sqlite statement to bind parameters for
  179. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  180. * so immediately suitable for passing to `sqlite3_bind`-functions.
  181. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  182. */
  183. static int
  184. bind_rsa_sig (void *cls,
  185. const void *data,
  186. size_t data_len,
  187. sqlite3_stmt *stmt,
  188. unsigned int off)
  189. {
  190. const struct GNUNET_CRYPTO_RsaSignature *sig = data;
  191. char *buf;
  192. size_t buf_size;
  193. GNUNET_break (NULL == cls);
  194. buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
  195. &buf);
  196. if (SQLITE_OK !=
  197. sqlite3_bind_blob64 (stmt,
  198. (int) off,
  199. buf,
  200. (sqlite3_uint64) buf_size,
  201. SQLITE_TRANSIENT))
  202. {
  203. GNUNET_free (buf);
  204. return GNUNET_SYSERR;
  205. }
  206. GNUNET_free (buf);
  207. return GNUNET_OK;
  208. }
  209. /**
  210. * Generate query parameter for an RSA signature. The
  211. * database must contain a BLOB type in the respective position.
  212. *
  213. * @param x the query parameter to pass
  214. */
  215. struct GNUNET_SQ_QueryParam
  216. GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
  217. {
  218. struct GNUNET_SQ_QueryParam qp = {
  219. .conv = &bind_rsa_sig,
  220. .data = x,
  221. .num_params = 1
  222. };
  223. return qp;
  224. }
  225. /**
  226. * Function called to convert input argument into SQL parameters.
  227. *
  228. * @param cls closure
  229. * @param data pointer to input argument
  230. * @param data_len number of bytes in @a data (if applicable)
  231. * @param stmt sqlite statement to bind parameters for
  232. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  233. * so immediately suitable for passing to `sqlite3_bind`-functions.
  234. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  235. */
  236. static int
  237. bind_abstime (void *cls,
  238. const void *data,
  239. size_t data_len,
  240. sqlite3_stmt *stmt,
  241. unsigned int off)
  242. {
  243. const struct GNUNET_TIME_Absolute *u = data;
  244. struct GNUNET_TIME_Absolute abs;
  245. abs = *u;
  246. if (abs.abs_value_us > INT64_MAX)
  247. abs.abs_value_us = INT64_MAX;
  248. GNUNET_assert (sizeof (uint64_t) == data_len);
  249. if (SQLITE_OK !=
  250. sqlite3_bind_int64 (stmt,
  251. (int) off,
  252. (sqlite3_int64) abs.abs_value_us))
  253. return GNUNET_SYSERR;
  254. return GNUNET_OK;
  255. }
  256. /**
  257. * Generate query parameter for an absolute time value.
  258. * The database must store a 64-bit integer.
  259. *
  260. * @param x pointer to the query parameter to pass
  261. */
  262. struct GNUNET_SQ_QueryParam
  263. GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
  264. {
  265. struct GNUNET_SQ_QueryParam qp = {
  266. .conv = &bind_abstime,
  267. .data = x,
  268. .size = sizeof (struct GNUNET_TIME_Absolute),
  269. .num_params = 1
  270. };
  271. return qp;
  272. }
  273. /**
  274. * Function called to convert input argument into SQL parameters.
  275. *
  276. * @param cls closure
  277. * @param data pointer to input argument
  278. * @param data_len number of bytes in @a data (if applicable)
  279. * @param stmt sqlite statement to bind parameters for
  280. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  281. * so immediately suitable for passing to `sqlite3_bind`-functions.
  282. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  283. */
  284. static int
  285. bind_nbotime (void *cls,
  286. const void *data,
  287. size_t data_len,
  288. sqlite3_stmt *stmt,
  289. unsigned int off)
  290. {
  291. const struct GNUNET_TIME_AbsoluteNBO *u = data;
  292. struct GNUNET_TIME_Absolute abs;
  293. abs = GNUNET_TIME_absolute_ntoh (*u);
  294. if (abs.abs_value_us > INT64_MAX)
  295. abs.abs_value_us = INT64_MAX;
  296. GNUNET_assert (sizeof (uint64_t) == data_len);
  297. if (SQLITE_OK !=
  298. sqlite3_bind_int64 (stmt,
  299. (int) off,
  300. (sqlite3_int64) abs.abs_value_us))
  301. return GNUNET_SYSERR;
  302. return GNUNET_OK;
  303. }
  304. /**
  305. * Generate query parameter for an absolute time value.
  306. * The database must store a 64-bit integer.
  307. *
  308. * @param x pointer to the query parameter to pass
  309. */
  310. struct GNUNET_SQ_QueryParam
  311. GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
  312. {
  313. struct GNUNET_SQ_QueryParam qp = {
  314. .conv = &bind_nbotime,
  315. .data = x,
  316. .size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
  317. .num_params = 1
  318. };
  319. return qp;
  320. }
  321. /**
  322. * Function called to convert input argument into SQL parameters.
  323. *
  324. * @param cls closure
  325. * @param data pointer to input argument
  326. * @param data_len number of bytes in @a data (if applicable)
  327. * @param stmt sqlite statement to bind parameters for
  328. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  329. * so immediately suitable for passing to `sqlite3_bind`-functions.
  330. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  331. */
  332. static int
  333. bind_u16 (void *cls,
  334. const void *data,
  335. size_t data_len,
  336. sqlite3_stmt *stmt,
  337. unsigned int off)
  338. {
  339. const uint16_t *u = data;
  340. GNUNET_assert (sizeof (uint16_t) == data_len);
  341. if (SQLITE_OK !=
  342. sqlite3_bind_int (stmt,
  343. (int) off,
  344. (int) *u))
  345. return GNUNET_SYSERR;
  346. return GNUNET_OK;
  347. }
  348. /**
  349. * Generate query parameter for an uint16_t in host byte order.
  350. *
  351. * @param x pointer to the query parameter to pass
  352. */
  353. struct GNUNET_SQ_QueryParam
  354. GNUNET_SQ_query_param_uint16 (const uint16_t *x)
  355. {
  356. struct GNUNET_SQ_QueryParam qp = {
  357. .conv = &bind_u16,
  358. .data = x,
  359. .size = sizeof (uint16_t),
  360. .num_params = 1
  361. };
  362. return qp;
  363. }
  364. /**
  365. * Function called to convert input argument into SQL parameters.
  366. *
  367. * @param cls closure
  368. * @param data pointer to input argument
  369. * @param data_len number of bytes in @a data (if applicable)
  370. * @param stmt sqlite statement to bind parameters for
  371. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  372. * so immediately suitable for passing to `sqlite3_bind`-functions.
  373. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  374. */
  375. static int
  376. bind_u32 (void *cls,
  377. const void *data,
  378. size_t data_len,
  379. sqlite3_stmt *stmt,
  380. unsigned int off)
  381. {
  382. const uint32_t *u = data;
  383. GNUNET_assert (sizeof (uint32_t) == data_len);
  384. if (SQLITE_OK !=
  385. sqlite3_bind_int64 (stmt,
  386. (int) off,
  387. (sqlite3_int64) *u))
  388. return GNUNET_SYSERR;
  389. return GNUNET_OK;
  390. }
  391. /**
  392. * Generate query parameter for an uint32_t in host byte order.
  393. *
  394. * @param x pointer to the query parameter to pass
  395. */
  396. struct GNUNET_SQ_QueryParam
  397. GNUNET_SQ_query_param_uint32 (const uint32_t *x)
  398. {
  399. struct GNUNET_SQ_QueryParam qp = {
  400. .conv = &bind_u32,
  401. .data = x,
  402. .size = sizeof (uint32_t),
  403. .num_params = 1
  404. };
  405. return qp;
  406. }
  407. /**
  408. * Function called to convert input argument into SQL parameters.
  409. *
  410. * @param cls closure
  411. * @param data pointer to input argument
  412. * @param data_len number of bytes in @a data (if applicable)
  413. * @param stmt sqlite statement to bind parameters for
  414. * @param off offset of the argument to bind in @a stmt, numbered from 1,
  415. * so immediately suitable for passing to `sqlite3_bind`-functions.
  416. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  417. */
  418. static int
  419. bind_u64 (void *cls,
  420. const void *data,
  421. size_t data_len,
  422. sqlite3_stmt *stmt,
  423. unsigned int off)
  424. {
  425. const uint64_t *u = data;
  426. GNUNET_assert (sizeof (uint64_t) == data_len);
  427. if (SQLITE_OK !=
  428. sqlite3_bind_int64 (stmt,
  429. (int) off,
  430. (sqlite3_int64) *u))
  431. return GNUNET_SYSERR;
  432. return GNUNET_OK;
  433. }
  434. /**
  435. * Generate query parameter for an uint16_t in host byte order.
  436. *
  437. * @param x pointer to the query parameter to pass
  438. */
  439. struct GNUNET_SQ_QueryParam
  440. GNUNET_SQ_query_param_uint64 (const uint64_t *x)
  441. {
  442. struct GNUNET_SQ_QueryParam qp = {
  443. .conv = &bind_u64,
  444. .data = x,
  445. .size = sizeof (uint64_t),
  446. .num_params = 1
  447. };
  448. return qp;
  449. }
  450. /* end of sq_query_helper.c */