Code

Use stdbool.h's bool type instead of _Bool.
[sysdb.git] / src / core / data.c
1 /*
2  * SysDB - src/core/data.c
3  * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
34 #include "core/data.h"
35 #include "utils/error.h"
37 #include <assert.h>
39 #include <errno.h>
41 #include <inttypes.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
47 #include <math.h>
49 /*
50  * Operator support maxtrix.
51  * <type1> <op> <type2> -> op_matrix[<op>][<type1>][<type2>]
52  */
54 /* add, sub, mul, div, mod, concat */
56 /* integer, decimal, string, datetime, binary, regex */
58 static int op_matrix[6][6][6] = {
59         /* SDB_DATA_ADD */
60         {
61                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
62                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
63                 { -1, -1, -1, -1, -1, -1 },
64                 { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 },
65                 { -1, -1, -1, -1, -1, -1 },
66                 { -1, -1, -1, -1, -1, -1 },
67         },
69         /* SDB_DATA_SUB */
70         {
71                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
72                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
73                 { -1, -1, -1, -1, -1, -1 },
74                 { -1, -1, -1, SDB_TYPE_DATETIME, -1, -1 },
75                 { -1, -1, -1, -1, -1, -1 },
76                 { -1, -1, -1, -1, -1, -1 },
77         },
79         /* SDB_DATA_MUL */
80         {
81                 { SDB_TYPE_INTEGER, -1, -1, SDB_TYPE_DATETIME, -1, -1 },
82                 { -1, SDB_TYPE_DECIMAL, -1, SDB_TYPE_DATETIME, -1, -1 },
83                 { -1, -1, -1, -1, -1, -1 },
84                 { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 },
85                 { -1, -1, -1, -1, -1, -1 },
86                 { -1, -1, -1, -1, -1, -1 },
87         },
89         /* SDB_DATA_DIV */
90         {
91                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
92                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
93                 { -1, -1, -1, -1, -1, -1 },
94                 { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 },
95                 { -1, -1, -1, -1, -1, -1 },
96                 { -1, -1, -1, -1, -1, -1 },
97         },
99         /* SDB_DATA_MOD */
100         {
101                 { SDB_TYPE_INTEGER, -1, -1, -1, -1, -1 },
102                 { -1, SDB_TYPE_DECIMAL, -1, -1, -1, -1 },
103                 { -1, -1, -1, -1, -1, -1 },
104                 { SDB_TYPE_DATETIME, SDB_TYPE_DATETIME, -1, SDB_TYPE_DATETIME, -1, -1 },
105                 { -1, -1, -1, -1, -1, -1 },
106                 { -1, -1, -1, -1, -1, -1 },
107         },
109         /* SDB_DATA_CONCAT */
110         {
111                 { -1, -1, -1, -1, -1, -1 },
112                 { -1, -1, -1, -1, -1, -1 },
113                 { -1, -1, SDB_TYPE_STRING, -1, -1, -1 },
114                 { -1, -1, -1, -1, -1, -1 },
115                 { -1, -1, -1, -1, SDB_TYPE_BINARY, -1 },
116                 { -1, -1, -1, -1, -1, -1 },
117         },
118 };
120 /*
121  * private helper functions
122  */
124 /* this function supports in-place copies */
125 static int
126 copy_array_values(sdb_data_t *dst, const sdb_data_t *src, size_t elem_size)
128         int type = src->type & 0xff;
130         if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)) {
131                 if (dst != src)
132                         memcpy(dst->data.array.values, src->data.array.values,
133                                         src->data.array.length * elem_size);
134         }
135         else if (type == SDB_TYPE_STRING) {
136                 char **s = src->data.array.values;
137                 char **d = dst->data.array.values;
138                 size_t i;
140                 for (i = 0; i < src->data.array.length; ++i) {
141                         d[i] = strdup(s[i]);
142                         if (! d[i])
143                                 return -1;
144                 }
145         }
146         else {
147                 /* TODO */
148                 errno = ENOTSUP;
149                 return -1;
150         }
151         return 0;
152 } /* copy_array_values */
154 static void
155 free_array_values(sdb_data_t *datum)
157         int type = datum->type & 0xff;
159         if (type == SDB_TYPE_STRING) {
160                 char **v = datum->data.array.values;
161                 size_t i;
163                 for (i = 0; i < datum->data.array.length; ++i) {
164                         if (v[i])
165                                 free(v[i]);
166                         v[i] = NULL;
167                 }
168         }
169 } /* free_array_values */
171 /* compare two arrays element-by-element returning how the first non-equal
172  * elements compare to each other */
173 static int
174 array_cmp(const sdb_data_t *a1, const sdb_data_t *a2)
176         int type = a1->type & 0xff;
177         size_t len, i;
179         assert((a1->type == a2->type) && (a1->type & SDB_TYPE_ARRAY));
181         len = SDB_MIN(a1->data.array.length, a2->data.array.length);
183         if (type == SDB_TYPE_INTEGER) {
184                 int64_t *v1 = a1->data.array.values;
185                 int64_t *v2 = a2->data.array.values;
187                 for (i = 0; i < len; ++i)
188                         if (v1[i] != v2[i])
189                                 return SDB_CMP(v1[i], v2[i]);
190         }
191         else if (type == SDB_TYPE_DECIMAL) {
192                 double *v1 = a1->data.array.values;
193                 double *v2 = a2->data.array.values;
195                 for (i = 0; i < len; ++i)
196                         if (v1[i] != v2[i])
197                                 return SDB_CMP(v1[i], v2[i]);
198         }
199         else if (type == SDB_TYPE_STRING) {
200                 char **v1 = a1->data.array.values;
201                 char **v2 = a2->data.array.values;
203                 for (i = 0; i < len; ++i) {
204                         int diff = strcasecmp(v1[i], v2[i]);
205                         if (diff)
206                                 return diff;
207                 }
208         }
209         else {
210                 /* TODO */
211                 errno = ENOTSUP;
212                 /* but fall through to ensure stable sorting: */
213         }
214         return SDB_CMP(a1->data.array.length, a2->data.array.length);
215 } /* array_cmp */
217 /* Calculate the linear function 'd1 + n * d2'. */
218 static int
219 data_lin(const sdb_data_t *d1, int n, const sdb_data_t *d2, sdb_data_t *res)
221         if (d1->type != d2->type)
222                 return -1;
224         if (d1->type == SDB_TYPE_INTEGER)
225                 res->data.integer = d1->data.integer + (int64_t)n * d2->data.integer;
226         else if (d1->type == SDB_TYPE_DECIMAL)
227                 res->data.decimal = d1->data.decimal + (double)n * d2->data.decimal;
228         else if (d1->type ==  SDB_TYPE_DATETIME)
229                 res->data.datetime = d1->data.datetime + (sdb_time_t)n * d2->data.datetime;
230         else
231                 return -1;
232         res->type = d1->type;
233         return 0;
234 } /* data_lin */
236 /* Multiply d1 with d2. */
237 static int
238 data_mul(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
240         if (d1->type == SDB_TYPE_INTEGER) {
241                 if (d2->type == SDB_TYPE_INTEGER)
242                         res->data.integer = d1->data.integer * d2->data.integer;
243                 else if (d2->type == SDB_TYPE_DATETIME) {
244                         res->data.datetime = (sdb_time_t)d1->data.integer
245                                 * d2->data.datetime;
246                         res->type = SDB_TYPE_DATETIME;
247                         return 0;
248                 }
249                 else
250                         return -1;
251         }
252         else if (d1->type == SDB_TYPE_DECIMAL) {
253                 if (d2->type == SDB_TYPE_DECIMAL)
254                         res->data.decimal = d1->data.decimal * d2->data.decimal;
255                 else if (d2->type == SDB_TYPE_DATETIME) {
256                         res->data.datetime = (sdb_time_t)(d1->data.decimal
257                                         * (double)d2->data.datetime);
258                         res->type = SDB_TYPE_DATETIME;
259                         return 0;
260                 }
261                 else
262                         return -1;
263         }
264         else if (d1->type == SDB_TYPE_DATETIME) {
265                 if (d2->type == SDB_TYPE_DATETIME)
266                         res->data.datetime = d1->data.datetime
267                                 * d2->data.datetime;
268                 else if (d2->type == SDB_TYPE_INTEGER)
269                         res->data.datetime = d1->data.datetime
270                                 * (sdb_time_t)d2->data.integer;
271                 else if (d2->type == SDB_TYPE_DECIMAL)
272                         res->data.datetime = (sdb_time_t)((double)d1->data.datetime
273                                         * d2->data.decimal);
274                 else
275                         return -1;
276         }
277         else
278                 return -1;
280         res->type = d1->type;
281         return 0;
282 } /* data_mul */
284 /* Device d1 by d2 and return the result and the remainder. */
285 static int
286 data_div(const sdb_data_t *d1, const sdb_data_t *d2,
287                 sdb_data_t *res, sdb_data_t *rem)
289         if (d1->type == SDB_TYPE_INTEGER) {
290                 if (d2->type != SDB_TYPE_INTEGER)
291                         return -1;
292                 if (res)
293                         res->data.integer = d1->data.integer / d2->data.integer;
294                 if (rem)
295                         rem->data.integer = d1->data.integer % d2->data.integer;
296         }
297         else if (d1->type == SDB_TYPE_DECIMAL) {
298                 if (d2->type != SDB_TYPE_DECIMAL)
299                         return -1;
300                 if (res)
301                         res->data.decimal = d1->data.decimal / d2->data.decimal;
302                 if (rem)
303                         rem->data.decimal = fmod(d1->data.decimal, d2->data.decimal);
304         }
305         else if (d1->type == SDB_TYPE_DATETIME) {
306                 if (d2->type == SDB_TYPE_DECIMAL) {
307                         if (res)
308                                 res->data.datetime = (sdb_time_t)((double)d1->data.datetime
309                                                 / d2->data.decimal);
310                         if (rem) {
311                                 double tmp = fmod((double)d1->data.datetime, d2->data.decimal);
312                                 rem->data.datetime = (sdb_time_t)tmp;
313                         }
314                 }
315                 else {
316                         sdb_time_t a, b;
317                         if (d2->type == SDB_TYPE_DATETIME) {
318                                 a = d1->data.datetime;
319                                 b = d2->data.datetime;
320                         }
321                         else if (d2->type == SDB_TYPE_INTEGER) {
322                                 a = d1->data.datetime;
323                                 b = (sdb_time_t)d2->data.integer;
324                         }
325                         else
326                                 return -1;
327                         if (res)
328                                 res->data.datetime = a / b;
329                         if (rem)
330                                 rem->data.datetime = a % b;
331                 }
332         }
333         else
334                 return -1;
336         if (res)
337                 res->type = d1->type;
338         if (rem)
339                 rem->type = d1->type;
340         return 0;
341 } /* data_div */
343 /* Concatenate d1 and d2. */
344 static int
345 data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
347         unsigned char *new;
348         const unsigned char *s1, *s2;
349         size_t len1, len2, array1_len = 0, array2_len = 0;
351         if ((d1->type & 0xff) != (d2->type & 0xff))
352                 return -1;
354         if ((d1->type & SDB_TYPE_ARRAY) || (d2->type & SDB_TYPE_ARRAY)) {
355                 size_t elem_size = sdb_data_sizeof(d1->type & 0xff);
356                 if (d1->type & SDB_TYPE_ARRAY) {
357                         s1 = (const unsigned char *)d1->data.array.values;
358                         array1_len = d1->data.array.length;
359                 }
360                 else {
361                         /* As per C99, section 6.7.2.1, paragraph 14:
362                          * "A pointer to a union object, suitably converted, points to
363                          * each of its members" */
364                         s1 = (const unsigned char *)&d1->data;
365                         array1_len = 1;
366                 }
367                 if (d2->type & SDB_TYPE_ARRAY) {
368                         s2 = (const unsigned char *)d2->data.array.values;
369                         array2_len = d2->data.array.length;
370                 }
371                 else {
372                         s2 = (const unsigned char *)&d2->data;
373                         array2_len = 1;
374                 }
375                 len1 = array1_len * elem_size;
376                 len2 = array2_len * elem_size;
377         }
378         else if (d1->type == SDB_TYPE_STRING) {
379                 s1 = (unsigned char *)d1->data.string;
380                 s2 = (unsigned char *)d2->data.string;
381                 len1 = s1 ? strlen((const char *)s1) : 0;
382                 len2 = s2 ? strlen((const char *)s2) : 0;
383         }
384         else if (d1->type == SDB_TYPE_BINARY) {
385                 s1 = d1->data.binary.datum;
386                 s2 = d2->data.binary.datum;
387                 len1 = d1->data.binary.length;
388                 len2 = d2->data.binary.length;
389         }
390         else
391                 return -1;
393         new = malloc(len1 + len2 + 1);
394         if (! new)
395                 return -1;
397         if (len1)
398                 memcpy(new, s1, len1);
399         if (len2)
400                 memcpy(new + len1, s2, len2);
401         new[len1 + len2] = '\0';
403         /* element types match and if either datum is an array,
404          * the result is an array as well */
405         res->type = d1->type | d2->type;
406         if (res->type == SDB_TYPE_STRING) {
407                 res->data.string = (char *)new;
408         }
409         else if (res->type == SDB_TYPE_BINARY) {
410                 res->data.binary.datum = new;
411                 res->data.binary.length = len1 + len2;
412         }
413         else if (res->type & SDB_TYPE_ARRAY) {
414                 res->data.array.values = new;
415                 res->data.array.length = array1_len + array2_len;
416                 if (copy_array_values(res, res, sdb_data_sizeof(res->type & 0xff))) {
417                         /* this leaks already copied values but there's not much we can
418                          * do and this should only happen if we're in trouble anyway */
419                         free(new);
420                         res->data.array.values = NULL;
421                         res->data.array.length = 0;
422                         return -1;
423                 }
424         }
425         return 0;
426 } /* data_concat */
428 /*
429  * public API
430  */
432 const sdb_data_t SDB_DATA_NULL = SDB_DATA_INIT;
434 int
435 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
437         sdb_data_t tmp;
439         if ((! dst) || (! src))
440                 return -1;
442         tmp = *src;
443         if (src->type == SDB_TYPE_STRING) {
444                 if (src->data.string) {
445                         tmp.data.string = strdup(src->data.string);
446                         if (! tmp.data.string)
447                                 return -1;
448                 }
449         }
450         else if (src->type == SDB_TYPE_BINARY) {
451                 if (src->data.binary.datum) {
452                         tmp.data.binary.datum = malloc(src->data.binary.length);
453                         if (! tmp.data.binary.datum)
454                                 return -1;
455                         memcpy(tmp.data.binary.datum, src->data.binary.datum,
456                                         src->data.binary.length);
457                 }
458         }
459         else if (src->type == SDB_TYPE_REGEX) {
460                 if (src->data.re.raw) {
461                         tmp.data.re.raw = strdup(src->data.re.raw);
462                         if (! tmp.data.re.raw)
463                                 return -1;
464                         /* we need to recompile because the regex might point to
465                          * dynamically allocated memory */
466                         if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
467                                                 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
468                                 free(tmp.data.re.raw);
469                                 return -1;
470                         }
471                 }
472                 else
473                         memset(&tmp.data.re.regex, 0, sizeof(tmp.data.re.regex));
474         }
475         else if (src->type & SDB_TYPE_ARRAY) {
476                 if (src->data.array.values) {
477                         size_t elem_size = sdb_data_sizeof(src->type & 0xff);
478                         tmp.data.array.values = calloc(src->data.array.length, elem_size);
479                         if (! tmp.data.array.values)
480                                 return -1;
481                         if (copy_array_values(&tmp, src, elem_size)) {
482                                 sdb_data_free_datum(&tmp);
483                                 return -1;
484                         }
485                 }
486         }
488         sdb_data_free_datum(dst);
489         *dst = tmp;
490         return 0;
491 } /* sdb_data_copy */
493 void
494 sdb_data_free_datum(sdb_data_t *datum)
496         if (! datum)
497                 return;
499         if (datum->type == SDB_TYPE_STRING) {
500                 if (datum->data.string)
501                         free(datum->data.string);
502                 datum->data.string = NULL;
503         }
504         else if (datum->type == SDB_TYPE_BINARY) {
505                 if (datum->data.binary.datum)
506                         free(datum->data.binary.datum);
507                 datum->data.binary.datum = NULL;
508                 datum->data.binary.length = 0;
509         }
510         else if (datum->type == SDB_TYPE_REGEX) {
511                 if (datum->data.re.raw) {
512                         free(datum->data.re.raw);
513                         regfree(&datum->data.re.regex);
514                 }
515                 datum->data.re.raw = NULL;
516                 memset(&datum->data.re.regex, 0, sizeof(datum->data.re.regex));
517         }
518         else if (datum->type & SDB_TYPE_ARRAY) {
519                 free_array_values(datum);
520                 if (datum->data.array.values)
521                         free(datum->data.array.values);
522                 datum->data.array.values = NULL;
523                 datum->data.array.length = 0;
524         }
525 } /* sdb_data_free_datum */
527 int
528 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
530 #define CMP_NULL(a, b) \
531         do { \
532                 if (!(a) && !(b)) return 0; \
533                 if (!(a)) return -1; \
534                 if (!(b)) return 1; \
535         } while (0)
537         CMP_NULL(d1, d2);
539         if (d1->type != d2->type)
540                 return SDB_CMP(d1->type, d2->type);
542         if (d1->type == SDB_TYPE_INTEGER)
543                 return SDB_CMP(d1->data.integer, d2->data.integer);
544         else if (d1->type == SDB_TYPE_DECIMAL)
545                 return SDB_CMP(d1->data.decimal, d2->data.decimal);
546         else if (d1->type == SDB_TYPE_STRING) {
547                 CMP_NULL(d1->data.string, d2->data.string);
548                 return strcasecmp(d1->data.string, d2->data.string);
549         }
550         else if (d1->type == SDB_TYPE_DATETIME)
551                 return SDB_CMP(d1->data.datetime, d2->data.datetime);
552         else if (d1->type == SDB_TYPE_BINARY) {
553                 int diff;
555                 CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
557                 /* on a common prefix, the shorter datum sorts less */
558                 if (d1->data.binary.length < d2->data.binary.length) {
559                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
560                                         d1->data.binary.length);
561                         diff = diff ? diff : -1;
562                 }
563                 else if (d1->data.binary.length > d2->data.binary.length) {
564                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
565                                         d2->data.binary.length);
566                         diff = diff ? diff : 1;
567                 }
568                 else
569                         diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
570                                         d1->data.binary.length);
572                 return diff;
573         }
574         else if (d1->type == SDB_TYPE_REGEX) {
575                 CMP_NULL(d1->data.re.raw, d2->data.re.raw);
576                 return strcmp(d1->data.re.raw, d2->data.re.raw);
577         }
578         else if (d1->type & SDB_TYPE_ARRAY) {
579                 CMP_NULL(d1->data.array.values, d2->data.array.values);
580                 return array_cmp(d1, d2);
581         }
582         return -1;
583 } /* sdb_data_cmp */
585 int
586 sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2)
588         char d1_str[sdb_data_strlen(d1) + 1];
589         char d2_str[sdb_data_strlen(d2) + 1];
591         if ((d1->type & SDB_TYPE_ARRAY) || (d2->type & SDB_TYPE_ARRAY)) {
592                 /* TODO */
593                 errno = ENOTSUP;
594                 return -1;
595         }
597         if (sdb_data_isnull(d1))
598                 d1 = NULL;
599         if (sdb_data_isnull(d2))
600                 d2 = NULL;
602         CMP_NULL(d1, d2);
604         if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0)
605                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
606         if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0)
607                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
609         return strcasecmp(d1_str, d2_str);
610 #undef CMP_NULL
611 } /* sdb_data_strcmp */
613 bool
614 sdb_data_isnull(const sdb_data_t *datum)
616         if (! datum)
617                 return 1;
618         if (datum->type == SDB_TYPE_NULL)
619                 return 1;
620         if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string))
621                 return 1;
622         if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
623                 return 1;
624         if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw))
625                 return 1;
626         return 0;
627 } /* sdb_data_isnull */
629 bool
630 sdb_data_inarray(const sdb_data_t *value, const sdb_data_t *array)
632         const void *values;
633         size_t length, i;
634         int type = value->type & 0xff;
636         if (sdb_data_isnull(value) || sdb_data_isnull(array))
637                 return 0;
638         if (! (array->type & SDB_TYPE_ARRAY))
639                 return 0;
640         if ((value->type & 0xff) != (array->type & 0xff))
641                 return 0;
643         if (value->type & SDB_TYPE_ARRAY) {
644                 values = value->data.array.values;
645                 length = value->data.array.length;
646         }
647         else {
648                 values = &value->data;
649                 length = 1;
650         }
652         for (i = 0; i < length; ++i) {
653                 size_t j;
655                 if (type == SDB_TYPE_INTEGER) {
656                         int64_t *v = array->data.array.values;
657                         for (j = 0; j < array->data.array.length; ++j)
658                                 if (((const int64_t *)values)[i] == v[j])
659                                         break;
660                 }
661                 else if (type == SDB_TYPE_DECIMAL) {
662                         double *v = array->data.array.values;
663                         for (j = 0; j < array->data.array.length; ++j)
664                                 if (((const double *)values)[i] == v[j])
665                                         break;
666                 }
667                 else if (type == SDB_TYPE_STRING) {
668                         char **v = array->data.array.values;
669                         for (j = 0; j < array->data.array.length; ++j)
670                                 if (!strcasecmp(((const char * const*)values)[i], v[j]))
671                                         break;
672                 }
673                 else {
674                         /* TODO */
675                         errno = ENOTSUP;
676                         return 0;
677                 }
679                 if (j >= array->data.array.length)
680                         /* value not found */
681                         return 0;
682         }
683         return 1;
684 } /* sdb_data_inarray */
686 int
687 sdb_data_array_get(const sdb_data_t *array, size_t i, sdb_data_t *value)
689         sdb_data_t tmp = SDB_DATA_INIT;
690         int type;
692         if ((! array) || (! (array->type & SDB_TYPE_ARRAY)))
693                 return -1;
694         if (i >= array->data.array.length)
695                 return -1;
697         type = array->type & 0xff;
698         if (type == SDB_TYPE_INTEGER) {
699                 int64_t *v = array->data.array.values;
700                 tmp.data.integer = v[i];
701         }
702         else if (type == SDB_TYPE_DECIMAL) {
703                 double *v = array->data.array.values;
704                 tmp.data.decimal = v[i];
705         }
706         else if (type == SDB_TYPE_STRING) {
707                 char **v = array->data.array.values;
708                 tmp.data.string = v[i];
709         }
710         else {
711                 /* TODO */
712                 errno = ENOTSUP;
713                 return -1;
714         }
716         if (value) {
717                 *value = tmp;
718                 value->type = type;
719         }
720         return 0;
721 } /* sdb_data_array_get */
723 int
724 sdb_data_parse_op(const char *op)
726         if (! strcmp(op, "+"))
727                 return SDB_DATA_ADD;
728         else if (! strcmp(op, "-"))
729                 return SDB_DATA_SUB;
730         else if (! strcmp(op, "*"))
731                 return SDB_DATA_MUL;
732         else if (! strcmp(op, "/"))
733                 return SDB_DATA_DIV;
734         else if (! strcmp(op, "%"))
735                 return SDB_DATA_MOD;
736         else if (! strcmp(op, "||"))
737                 return SDB_DATA_CONCAT;
738         return -1;
739 } /* sdb_data_parse_op */
741 int
742 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
743                 sdb_data_t *res)
745         if ((! d1) || (! d2) || (! res))
746                 return -1;
747         if (sdb_data_isnull(d1) || sdb_data_isnull(d2)) {
748                 *res = SDB_DATA_NULL;
749                 return 0;
750         }
751         switch (op) {
752                 case SDB_DATA_CONCAT: return data_concat(d1, d2, res);
753                 case SDB_DATA_ADD: return data_lin(d1, 1, d2, res);
754                 case SDB_DATA_SUB: return data_lin(d1, -1, d2, res);
755                 case SDB_DATA_MUL: return data_mul(d1, d2, res);
756                 case SDB_DATA_DIV: return data_div(d1, d2, res, NULL);
757                 case SDB_DATA_MOD: return data_div(d1, d2, NULL, res);
758         }
759         return -1;
760 } /* sdb_data_expr_eval */
762 int
763 sdb_data_expr_type(int op, int type1, int type2)
765         int types_num = (int)SDB_STATIC_ARRAY_LEN(op_matrix[0]);
767         assert(SDB_STATIC_ARRAY_LEN(op_matrix[0])
768                         == SDB_STATIC_ARRAY_LEN(op_matrix[0][0]));
770         if ((op <= 0) || (SDB_STATIC_ARRAY_LEN(op_matrix) < (size_t)op))
771                 return -1;
773         /* arrays only support concat; element type has to match */
774         if ((type1 & SDB_TYPE_ARRAY) || (type2 & SDB_TYPE_ARRAY)) {
775                 if (((type1 & 0xff) != (type2 & 0xff)) || (op != SDB_DATA_CONCAT))
776                         return -1;
777                 return type1 | SDB_TYPE_ARRAY;
778         }
779         if ((type1 < 0) || (types_num < type1)
780                         || (type2 < 0) || (types_num < type2))
781                 return -1;
783         if ((type1 == SDB_TYPE_NULL) || (type2 == SDB_TYPE_NULL))
784                 return SDB_TYPE_NULL;
785         return op_matrix[op - 1][type1 - 1][type2 - 1];
786 } /* sdb_data_expr_type */
788 size_t
789 sdb_data_strlen(const sdb_data_t *datum)
791         if (! datum)
792                 return 0;
794         if (datum->type == SDB_TYPE_INTEGER) {
795                 /* log(64) */
796                 return 20;
797         }
798         else if (datum->type == SDB_TYPE_DECIMAL) {
799                 /* XXX: -d.dddddde+dd or -ddddd.dddddd */
800                 return 42;
801         }
802         else if (datum->type == SDB_TYPE_STRING) {
803                 if (! datum->data.string)
804                         return 6; /* NULL */
805                 /* in the worst case, each character needs to be escaped */
806                 return 2 * strlen(datum->data.string) + 2;
807         }
808         else if (datum->type == SDB_TYPE_DATETIME) {
809                 /* "YYYY-MM-DD HH:MM:SS +zzzz" */
810                 return 27;
811         }
812         else if (datum->type == SDB_TYPE_BINARY) {
813                 if (! datum->data.binary.datum)
814                         return 6; /* NULL */
815                 /* "\xNN" */
816                 return 4 * datum->data.binary.length + 2;
817         }
818         else if (datum->type == SDB_TYPE_REGEX) {
819                 if (! datum->data.re.raw)
820                         return 6; /* NULL */
821                 /* "/.../" */
822                 return strlen(datum->data.re.raw) + 4;
823         }
824         else if (datum->type & SDB_TYPE_ARRAY) {
825                 size_t len = 2; /* [] */
826                 size_t i;
827                 for (i = 0; i < datum->data.array.length; ++i) {
828                         sdb_data_t v = SDB_DATA_INIT;
829                         sdb_data_array_get(datum, i, &v);
830                         len += sdb_data_strlen(&v) + 1;
831                 }
832                 return len;
833         }
834         return 0;
835 } /* sdb_data_strlen */
837 int
838 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
840         char tmp[sdb_data_strlen(datum) + 1];
841         char *data = NULL;
842         bool is_null = 0;
843         int ret = -1;
845         size_t i, pos;
847         if ((! datum) || (! buf) || (! buflen))
848                 return -1;
850         if (datum->type == SDB_TYPE_INTEGER) {
851                 ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
852         }
853         else if (datum->type == SDB_TYPE_DECIMAL) {
854                 if (isnan(datum->data.decimal))
855                         ret = snprintf(buf, buflen, "nan");
856                 else
857                         ret = snprintf(buf, buflen, "%g", datum->data.decimal);
858         }
859         else if (datum->type == SDB_TYPE_STRING) {
860                 if (! datum->data.string)
861                         is_null = 1;
862                 else {
863                         pos = 0;
864                         for (i = 0; i < strlen(datum->data.string); ++i) {
865                                 char byte = datum->data.string[i];
867                                 if ((byte == '\\') || (byte == '"')) {
868                                         tmp[pos] = '\\';
869                                         ++pos;
870                                 }
871                                 tmp[pos] = byte;
872                                 ++pos;
873                         }
874                         tmp[pos] = '\0';
875                         data = tmp;
876                 }
877         }
878         else if (datum->type == SDB_TYPE_DATETIME) {
879                 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
880                                         datum->data.datetime))
881                         return -1;
882                 tmp[sizeof(tmp) - 1] = '\0';
883                 data = tmp;
884         }
885         else if (datum->type == SDB_TYPE_BINARY) {
886                 pos = 0;
887                 for (i = 0; i < datum->data.binary.length; ++i) {
888                         int byte = (int)datum->data.binary.datum[i];
889                         char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
890                                 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
892                         tmp[pos] = '\\';
893                         tmp[pos + 1] = 'x';
894                         pos += 2;
896                         if (byte > 0xf) {
897                                 tmp[pos] = hex[byte >> 4];
898                                 ++pos;
899                         }
900                         tmp[pos] = hex[byte & 0xf];
901                         ++pos;
902                 }
903                 if (datum->data.binary.datum) {
904                         tmp[pos] = '\0';
905                         data = tmp;
906                 }
907                 else
908                         is_null = 1;
909         }
910         else if (datum->type == SDB_TYPE_REGEX) {
911                 if (! datum->data.re.raw)
912                         is_null = 1;
913                 else {
914                         snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
915                         data = tmp;
916                 }
917         }
918         else if (datum->type & SDB_TYPE_ARRAY) {
919                 ret = 1;
920                 buf[0] = '[';
921                 for (i = 0; i < datum->data.array.length; ++i) {
922                         sdb_data_t v = SDB_DATA_INIT;
923                         int n;
924                         if ((size_t)ret >= buflen - 1)
925                                 break;
927                         if (ret > 1) {
928                                 buf[ret] = ',';
929                                 buf[ret + 1] = ' ';
930                                 ret += 2;
931                         }
933                         sdb_data_array_get(datum, i, &v);
934                         n = sdb_data_format(&v, buf + ret, buflen - ret, quoted);
935                         if (n > 0)
936                                 ret += n;
937                         else
938                                 break;
939                 }
940                 if ((size_t)ret < buflen - 1) {
941                         buf[ret] = ']';
942                         buf[ret + 1] = '\0';
943                         ++ret;
944                 }
945         }
947         if (is_null) {
948                 /* never quote NULL */
949                 strncpy(buf, "NULL", buflen);
950                 ret = (int)SDB_MIN(buflen, 4);
951         }
952         else if (data) {
953                 if (quoted == SDB_UNQUOTED)
954                         ret = snprintf(buf, buflen, "%s", data);
955                 else if (quoted == SDB_SINGLE_QUOTED)
956                         ret = snprintf(buf, buflen, "'%s'", data);
957                 else
958                         ret = snprintf(buf, buflen, "\"%s\"", data);
959         }
960         buf[buflen - 1] = '\0';
961         return ret;
962 } /* sdb_data_format */
964 int
965 sdb_data_parse(char *str, int type, sdb_data_t *data)
967         sdb_data_t tmp;
969         char *endptr = NULL;
971         errno = 0;
972         if (type == SDB_TYPE_INTEGER) {
973                 tmp.data.integer = strtoll(str, &endptr, 0);
974         }
975         else if (type == SDB_TYPE_DECIMAL) {
976                 tmp.data.decimal = strtod(str, &endptr);
977         }
978         else if (type == SDB_TYPE_STRING) {
979                 tmp.data.string = str;
980         }
981         else if (type == SDB_TYPE_DATETIME) {
982                 double datetime = strtod(str, &endptr);
983                 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
984         }
985         else if (type == SDB_TYPE_BINARY) {
986                 /* we don't support any binary information containing 0-bytes */
987                 tmp.data.binary.length = strlen(str);
988                 tmp.data.binary.datum = (unsigned char *)str;
989         }
990         else if (type == SDB_TYPE_REGEX) {
991                 tmp.data.re.raw = strdup(str);
992                 if (! tmp.data.re.raw)
993                         return -1;
994                 if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
995                                         REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
996                         sdb_log(SDB_LOG_ERR, "core: Failed to compile regular "
997                                         "expression '%s'", tmp.data.re.raw);
998                         free(tmp.data.re.raw);
999                         return -1;
1000                 }
1001                 if (! data) {
1002                         tmp.type = SDB_TYPE_REGEX;
1003                         sdb_data_free_datum(&tmp);
1004                 }
1005         }
1006         else if (type & SDB_TYPE_ARRAY) {
1007                 /* TODO */
1008                 errno = ENOTSUP;
1009                 return -1;
1010         }
1011         else {
1012                 errno = EINVAL;
1013                 return -1;
1014         }
1016         if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
1017                         || (type == SDB_TYPE_DATETIME)) {
1018                 if (errno || (str == endptr)) {
1019                         char errbuf[1024];
1020                         sdb_log(SDB_LOG_ERR, "core: Failed to parse string "
1021                                         "'%s' as numeric value (type %i): %s", str, type,
1022                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1023                         return -1;
1024                 }
1025                 else if (endptr && (*endptr != '\0'))
1026                         sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after "
1027                                         "number while parsing numeric value (type %i): %s.",
1028                                         type, endptr);
1029         }
1031         if (data) {
1032                 *data = tmp;
1033                 data->type = type;
1034         }
1035         return 0;
1036 } /* sdb_data_parse */
1038 size_t
1039 sdb_data_sizeof(int type)
1041         sdb_data_t v;
1042         if (type == SDB_TYPE_INTEGER)
1043                 return sizeof(v.data.integer);
1044         else if (type == SDB_TYPE_DECIMAL)
1045                 return sizeof(v.data.decimal);
1046         else if (type == SDB_TYPE_STRING)
1047                 return sizeof(v.data.string);
1048         else if (type == SDB_TYPE_DATETIME)
1049                 return sizeof(v.data.datetime);
1050         else if (type == SDB_TYPE_BINARY)
1051                 return sizeof(v.data.binary);
1052         else if (type == SDB_TYPE_REGEX)
1053                 return sizeof(v.data.re);
1054         return 0;
1055 } /* sdb_data_sizeof */
1057 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */