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