aboutsummaryrefslogtreecommitdiff
path: root/src/pq/pq_query_helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pq/pq_query_helper.c')
-rw-r--r--src/pq/pq_query_helper.c386
1 files changed, 386 insertions, 0 deletions
diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c
new file mode 100644
index 000000000..5f2764b62
--- /dev/null
+++ b/src/pq/pq_query_helper.c
@@ -0,0 +1,386 @@
1 /*
2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
8
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
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/gnunet_util_lib.h>
23#include "gnunet_pq_lib.h"
24
25
26/**
27 * Function called to convert input argument into SQL parameters.
28 *
29 * @param cls closure
30 * @param data pointer to input argument
31 * @param data_len number of bytes in @a data (if applicable)
32 * @param[out] param_values SQL data to set
33 * @param[out] param_lengths SQL length data to set
34 * @param[out] param_formats SQL format data to set
35 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
36 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
37 * @param scratch_length number of entries left in @a scratch
38 * @return -1 on error, number of offsets used in @a scratch otherwise
39 */
40static int
41qconv_fixed (void *cls,
42 const void *data,
43 size_t data_len,
44 void *param_values[],
45 int param_lengths[],
46 int param_formats[],
47 unsigned int param_length,
48 void *scratch[],
49 unsigned int scratch_length)
50{
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
61/**
62 * Generate query parameter for a buffer @a ptr of
63 * @a ptr_size bytes.
64 *
65 * @param ptr pointer to the query parameter to pass
66 * @oaran ptr_size number of bytes in @a ptr
67 */
68struct GNUNET_PQ_QueryParam
69GNUNET_PQ_query_param_fixed_size (const void *ptr,
70 size_t ptr_size)
71{
72 struct GNUNET_PQ_QueryParam res =
73 { &qconv_fixed, NULL, ptr, ptr_size, 1 };
74 return res;
75}
76
77
78/**
79 * Function called to convert input argument into SQL parameters.
80 *
81 * @param cls closure
82 * @param data pointer to input argument
83 * @param data_len number of bytes in @a data (if applicable)
84 * @param[out] param_values SQL data to set
85 * @param[out] param_lengths SQL length data to set
86 * @param[out] param_formats SQL format data to set
87 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
88 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
89 * @param scratch_length number of entries left in @a scratch
90 * @return -1 on error, number of offsets used in @a scratch otherwise
91 */
92static int
93qconv_uint16 (void *cls,
94 const void *data,
95 size_t data_len,
96 void *param_values[],
97 int param_lengths[],
98 int param_formats[],
99 unsigned int param_length,
100 void *scratch[],
101 unsigned int scratch_length)
102{
103 const uint16_t *u_hbo = data;
104 uint16_t *u_nbo;
105
106 GNUNET_break (NULL == cls);
107 if (1 != param_length)
108 return -1;
109 u_nbo = GNUNET_new (uint16_t);
110 scratch[0] = u_nbo;
111 *u_nbo = htons (*u_hbo);
112 param_values[0] = (void *) u_nbo;
113 param_lengths[0] = sizeof (uint16_t);
114 param_formats[0] = 1;
115 return 1;
116}
117
118
119/**
120 * Generate query parameter for an uint16_t in host byte order.
121 *
122 * @param x pointer to the query parameter to pass
123 */
124struct GNUNET_PQ_QueryParam
125GNUNET_PQ_query_param_uint16 (const uint16_t *x)
126{
127 struct GNUNET_PQ_QueryParam res =
128 { &qconv_uint16, NULL, x, sizeof (*x), 1 };
129 return res;
130}
131
132
133/**
134 * Function called to convert input argument into SQL parameters.
135 *
136 * @param cls closure
137 * @param data pointer to input argument
138 * @param data_len number of bytes in @a data (if applicable)
139 * @param[out] param_values SQL data to set
140 * @param[out] param_lengths SQL length data to set
141 * @param[out] param_formats SQL format data to set
142 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
143 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
144 * @param scratch_length number of entries left in @a scratch
145 * @return -1 on error, number of offsets used in @a scratch otherwise
146 */
147static int
148qconv_uint32 (void *cls,
149 const void *data,
150 size_t data_len,
151 void *param_values[],
152 int param_lengths[],
153 int param_formats[],
154 unsigned int param_length,
155 void *scratch[],
156 unsigned int scratch_length)
157{
158 const uint32_t *u_hbo = data;
159 uint32_t *u_nbo;
160
161 GNUNET_break (NULL == cls);
162 if (1 != param_length)
163 return -1;
164 u_nbo = GNUNET_new (uint32_t);
165 scratch[0] = u_nbo;
166 *u_nbo = htonl (*u_hbo);
167 param_values[0] = (void *) u_nbo;
168 param_lengths[0] = sizeof (uint32_t);
169 param_formats[0] = 1;
170 return 1;
171}
172
173
174/**
175 * Generate query parameter for an uint32_t in host byte order.
176 *
177 * @param x pointer to the query parameter to pass
178 */
179struct GNUNET_PQ_QueryParam
180GNUNET_PQ_query_param_uint32 (const uint32_t *x)
181{
182 struct GNUNET_PQ_QueryParam res =
183 { &qconv_uint32, NULL, x, sizeof (*x), 1 };
184 return res;
185}
186
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 */
202static int
203qconv_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
216 GNUNET_break (NULL == cls);
217 if (1 != param_length)
218 return -1;
219 u_nbo = GNUNET_new (uint64_t);
220 scratch[0] = u_nbo;
221 *u_nbo = GNUNET_htonll (*u_hbo);
222 param_values[0] = (void *) u_nbo;
223 param_lengths[0] = sizeof (uint64_t);
224 param_formats[0] = 1;
225 return 1;
226}
227
228
229/**
230 * Generate query parameter for an uint64_t in host byte order.
231 *
232 * @param x pointer to the query parameter to pass
233 */
234struct GNUNET_PQ_QueryParam
235GNUNET_PQ_query_param_uint64 (const uint64_t *x)
236{
237 struct GNUNET_PQ_QueryParam res =
238 { &qconv_uint64, NULL, x, sizeof (*x), 1 };
239 return res;
240}
241
242
243/**
244 * Function called to convert input argument into SQL parameters.
245 *
246 * @param cls closure
247 * @param data pointer to input argument
248 * @param data_len number of bytes in @a data (if applicable)
249 * @param[out] param_values SQL data to set
250 * @param[out] param_lengths SQL length data to set
251 * @param[out] param_formats SQL format data to set
252 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
253 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
254 * @param scratch_length number of entries left in @a scratch
255 * @return -1 on error, number of offsets used in @a scratch otherwise
256 */
257static int
258qconv_rsa_public_key (void *cls,
259 const void *data,
260 size_t data_len,
261 void *param_values[],
262 int param_lengths[],
263 int param_formats[],
264 unsigned int param_length,
265 void *scratch[],
266 unsigned int scratch_length)
267{
268 const struct GNUNET_CRYPTO_rsa_PublicKey *rsa = data;
269 char *buf;
270 size_t buf_size;
271
272 GNUNET_break (NULL == cls);
273 if (1 != param_length)
274 return -1;
275 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
276 &buf);
277 scratch[0] = buf;
278 param_values[0] = (void *) buf;
279 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
280 param_formats[0] = 1;
281 return 1;
282}
283
284
285/**
286 * Generate query parameter for an RSA public key. The
287 * database must contain a BLOB type in the respective position.
288 *
289 * @param x the query parameter to pass
290 * @return array entry for the query parameters to use
291 */
292struct GNUNET_PQ_QueryParam
293GNUNET_PQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_rsa_PublicKey *x)
294{
295 struct GNUNET_PQ_QueryParam res =
296 { &qconv_rsa_public_key, NULL, (x), 0, 1 };
297 return res;
298}
299
300
301/**
302 * Function called to convert input argument into SQL parameters.
303 *
304 * @param cls closure
305 * @param data pointer to input argument
306 * @param data_len number of bytes in @a data (if applicable)
307 * @param[out] param_values SQL data to set
308 * @param[out] param_lengths SQL length data to set
309 * @param[out] param_formats SQL format data to set
310 * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays
311 * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc()
312 * @param scratch_length number of entries left in @a scratch
313 * @return -1 on error, number of offsets used in @a scratch otherwise
314 */
315static int
316qconv_rsa_signature (void *cls,
317 const void *data,
318 size_t data_len,
319 void *param_values[],
320 int param_lengths[],
321 int param_formats[],
322 unsigned int param_length,
323 void *scratch[],
324 unsigned int scratch_length)
325{
326 const struct GNUNET_CRYPTO_rsa_Signature *sig = data;
327 char *buf;
328 size_t buf_size;
329
330 GNUNET_break (NULL == cls);
331 if (1 != param_length)
332 return -1;
333 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
334 &buf);
335 scratch[0] = buf;
336 param_values[0] = (void *) buf;
337 param_lengths[0] = buf_size - 1; /* DB doesn't like the trailing \0 */
338 param_formats[0] = 1;
339 return 1;
340}
341
342
343/**
344 * Generate query parameter for an RSA signature. The
345 * database must contain a BLOB type in the respective position.
346 *
347 * @param x the query parameter to pass
348 * @return array entry for the query parameters to use
349 */
350struct GNUNET_PQ_QueryParam
351GNUNET_PQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_rsa_Signature *x)
352{
353 struct GNUNET_PQ_QueryParam res =
354 { &qconv_rsa_signature, NULL, (x), 0, 1 };
355 return res;
356}
357
358
359/**
360 * Generate query parameter for an absolute time value.
361 * The database must store a 64-bit integer.
362 *
363 * @param x pointer to the query parameter to pass
364 * @return array entry for the query parameters to use
365 */
366struct GNUNET_PQ_QueryParam
367GNUNET_PQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
368{
369 return GNUNET_PQ_query_param_uint64 (&x->abs_value_us);
370}
371
372
373/**
374 * Generate query parameter for an absolute time value.
375 * The database must store a 64-bit integer.
376 *
377 * @param x pointer to the query parameter to pass
378 */
379struct GNUNET_PQ_QueryParam
380GNUNET_PQ_query_param_absolute_time_nbo(const struct GNUNET_TIME_AbsoluteNBO *x)
381{
382 return GNUNET_PQ_query_param_auto_from_type (&x->abs_value_us__);
383}
384
385
386/* end of pq_query_helper.c */