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)
127 {
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)
156 {
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)
175 {
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)
220 {
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)
239 {
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)
288 {
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)
346 {
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 assert(s1 && s2);
395 new = malloc(len1 + len2 + 1);
396 if (! new)
397 return -1;
399 if (len1)
400 memcpy(new, s1, len1);
401 if (len2)
402 memcpy(new + len1, s2, len2);
403 new[len1 + len2] = '\0';
405 /* element types match and if either datum is an array,
406 * the result is an array as well */
407 res->type = d1->type | d2->type;
408 if (res->type == SDB_TYPE_STRING) {
409 res->data.string = (char *)new;
410 }
411 else if (res->type == SDB_TYPE_BINARY) {
412 res->data.binary.datum = new;
413 res->data.binary.length = len1 + len2;
414 }
415 else if (res->type & SDB_TYPE_ARRAY) {
416 res->data.array.values = new;
417 res->data.array.length = array1_len + array2_len;
418 if (copy_array_values(res, res, sdb_data_sizeof(res->type & 0xff))) {
419 /* this leaks already copied values but there's not much we can
420 * do and this should only happen if we're in trouble anyway */
421 free(new);
422 res->data.array.values = NULL;
423 res->data.array.length = 0;
424 return -1;
425 }
426 }
427 return 0;
428 } /* data_concat */
430 /*
431 * public API
432 */
434 const sdb_data_t SDB_DATA_NULL = SDB_DATA_INIT;
436 int
437 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
438 {
439 sdb_data_t tmp;
441 if ((! dst) || (! src))
442 return -1;
444 tmp = *src;
445 if (src->type == SDB_TYPE_STRING) {
446 if (src->data.string) {
447 tmp.data.string = strdup(src->data.string);
448 if (! tmp.data.string)
449 return -1;
450 }
451 }
452 else if (src->type == SDB_TYPE_BINARY) {
453 if (src->data.binary.datum) {
454 tmp.data.binary.datum = malloc(src->data.binary.length);
455 if (! tmp.data.binary.datum)
456 return -1;
457 memcpy(tmp.data.binary.datum, src->data.binary.datum,
458 src->data.binary.length);
459 }
460 }
461 else if (src->type == SDB_TYPE_REGEX) {
462 if (src->data.re.raw) {
463 tmp.data.re.raw = strdup(src->data.re.raw);
464 if (! tmp.data.re.raw)
465 return -1;
466 /* we need to recompile because the regex might point to
467 * dynamically allocated memory */
468 if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
469 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
470 free(tmp.data.re.raw);
471 return -1;
472 }
473 }
474 else
475 memset(&tmp.data.re.regex, 0, sizeof(tmp.data.re.regex));
476 }
477 else if (src->type & SDB_TYPE_ARRAY) {
478 if (src->data.array.values) {
479 size_t elem_size = sdb_data_sizeof(src->type & 0xff);
480 tmp.data.array.values = calloc(src->data.array.length, elem_size);
481 if (! tmp.data.array.values)
482 return -1;
483 if (copy_array_values(&tmp, src, elem_size)) {
484 sdb_data_free_datum(&tmp);
485 return -1;
486 }
487 }
488 }
490 sdb_data_free_datum(dst);
491 *dst = tmp;
492 return 0;
493 } /* sdb_data_copy */
495 void
496 sdb_data_free_datum(sdb_data_t *datum)
497 {
498 if (! datum)
499 return;
501 if (datum->type == SDB_TYPE_STRING) {
502 if (datum->data.string)
503 free(datum->data.string);
504 datum->data.string = NULL;
505 }
506 else if (datum->type == SDB_TYPE_BINARY) {
507 if (datum->data.binary.datum)
508 free(datum->data.binary.datum);
509 datum->data.binary.datum = NULL;
510 datum->data.binary.length = 0;
511 }
512 else if (datum->type == SDB_TYPE_REGEX) {
513 if (datum->data.re.raw) {
514 free(datum->data.re.raw);
515 regfree(&datum->data.re.regex);
516 }
517 datum->data.re.raw = NULL;
518 memset(&datum->data.re.regex, 0, sizeof(datum->data.re.regex));
519 }
520 else if (datum->type & SDB_TYPE_ARRAY) {
521 free_array_values(datum);
522 if (datum->data.array.values)
523 free(datum->data.array.values);
524 datum->data.array.values = NULL;
525 datum->data.array.length = 0;
526 }
527 } /* sdb_data_free_datum */
529 int
530 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
531 {
532 #define CMP_NULL(a, b) \
533 do { \
534 if (!(a) && !(b)) return 0; \
535 if (!(a)) return -1; \
536 if (!(b)) return 1; \
537 } while (0)
539 CMP_NULL(d1, d2);
541 if (d1->type != d2->type)
542 return SDB_CMP(d1->type, d2->type);
544 if (d1->type == SDB_TYPE_INTEGER)
545 return SDB_CMP(d1->data.integer, d2->data.integer);
546 else if (d1->type == SDB_TYPE_DECIMAL)
547 return SDB_CMP(d1->data.decimal, d2->data.decimal);
548 else if (d1->type == SDB_TYPE_STRING) {
549 CMP_NULL(d1->data.string, d2->data.string);
550 return strcasecmp(d1->data.string, d2->data.string);
551 }
552 else if (d1->type == SDB_TYPE_DATETIME)
553 return SDB_CMP(d1->data.datetime, d2->data.datetime);
554 else if (d1->type == SDB_TYPE_BINARY) {
555 int diff;
557 CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
559 /* on a common prefix, the shorter datum sorts less */
560 if (d1->data.binary.length < d2->data.binary.length) {
561 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
562 d1->data.binary.length);
563 diff = diff ? diff : -1;
564 }
565 else if (d1->data.binary.length > d2->data.binary.length) {
566 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
567 d2->data.binary.length);
568 diff = diff ? diff : 1;
569 }
570 else
571 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
572 d1->data.binary.length);
574 return diff;
575 }
576 else if (d1->type == SDB_TYPE_REGEX) {
577 CMP_NULL(d1->data.re.raw, d2->data.re.raw);
578 return strcmp(d1->data.re.raw, d2->data.re.raw);
579 }
580 else if (d1->type & SDB_TYPE_ARRAY) {
581 CMP_NULL(d1->data.array.values, d2->data.array.values);
582 return array_cmp(d1, d2);
583 }
584 return -1;
585 } /* sdb_data_cmp */
587 int
588 sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2)
589 {
590 char d1_str[sdb_data_strlen(d1) + 1];
591 char d2_str[sdb_data_strlen(d2) + 1];
593 if ((d1->type & SDB_TYPE_ARRAY) || (d2->type & SDB_TYPE_ARRAY)) {
594 /* TODO */
595 errno = ENOTSUP;
596 return -1;
597 }
599 if (sdb_data_isnull(d1))
600 d1 = NULL;
601 if (sdb_data_isnull(d2))
602 d2 = NULL;
604 CMP_NULL(d1, d2);
606 if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0)
607 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
608 if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0)
609 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
611 return strcasecmp(d1_str, d2_str);
612 #undef CMP_NULL
613 } /* sdb_data_strcmp */
615 _Bool
616 sdb_data_isnull(const sdb_data_t *datum)
617 {
618 if (! datum)
619 return 1;
620 if (datum->type == SDB_TYPE_NULL)
621 return 1;
622 if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string))
623 return 1;
624 if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
625 return 1;
626 if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw))
627 return 1;
628 return 0;
629 } /* sdb_data_isnull */
631 _Bool
632 sdb_data_inarray(const sdb_data_t *value, const sdb_data_t *array)
633 {
634 size_t i;
636 if (sdb_data_isnull(value) || sdb_data_isnull(array))
637 return 0;
638 if ((value->type & SDB_TYPE_ARRAY) || (! (array->type & SDB_TYPE_ARRAY)))
639 return 0;
640 if (value->type != (array->type & 0xff))
641 return 0;
643 if (value->type == SDB_TYPE_INTEGER) {
644 int64_t *v = array->data.array.values;
645 for (i = 0; i < array->data.array.length; ++i)
646 if (value->data.integer == v[i])
647 return 1;
648 }
649 else if (value->type == SDB_TYPE_DECIMAL) {
650 double *v = array->data.array.values;
651 for (i = 0; i < array->data.array.length; ++i)
652 if (value->data.decimal == v[i])
653 return 1;
654 }
655 else if (value->type == SDB_TYPE_STRING) {
656 char **v = array->data.array.values;
657 for (i = 0; i < array->data.array.length; ++i)
658 if (!strcasecmp(value->data.string, v[i]))
659 return 1;
660 }
661 else {
662 /* TODO */
663 errno = ENOTSUP;
664 return 0;
665 }
666 return 0;
667 } /* sdb_data_inarray */
669 int
670 sdb_data_array_get(const sdb_data_t *array, size_t i, sdb_data_t *value)
671 {
672 sdb_data_t tmp = SDB_DATA_INIT;
673 int type;
675 if ((! array) || (! (array->type & SDB_TYPE_ARRAY)))
676 return -1;
677 if (i >= array->data.array.length)
678 return -1;
680 type = array->type & 0xff;
681 if (type == SDB_TYPE_INTEGER) {
682 int64_t *v = array->data.array.values;
683 tmp.data.integer = v[i];
684 }
685 else if (type == SDB_TYPE_DECIMAL) {
686 double *v = array->data.array.values;
687 tmp.data.decimal = v[i];
688 }
689 else if (type == SDB_TYPE_STRING) {
690 char **v = array->data.array.values;
691 tmp.data.string = v[i];
692 }
693 else {
694 /* TODO */
695 errno = ENOTSUP;
696 return -1;
697 }
699 if (value) {
700 *value = tmp;
701 value->type = type;
702 }
703 return 0;
704 } /* sdb_data_array_get */
706 int
707 sdb_data_parse_op(const char *op)
708 {
709 if (! strcmp(op, "+"))
710 return SDB_DATA_ADD;
711 else if (! strcmp(op, "-"))
712 return SDB_DATA_SUB;
713 else if (! strcmp(op, "*"))
714 return SDB_DATA_MUL;
715 else if (! strcmp(op, "/"))
716 return SDB_DATA_DIV;
717 else if (! strcmp(op, "%"))
718 return SDB_DATA_MOD;
719 else if (! strcmp(op, "||"))
720 return SDB_DATA_CONCAT;
721 return -1;
722 } /* sdb_data_parse_op */
724 int
725 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
726 sdb_data_t *res)
727 {
728 if ((! d1) || (! d2) || (! res))
729 return -1;
730 if (sdb_data_isnull(d1) || sdb_data_isnull(d2)) {
731 *res = SDB_DATA_NULL;
732 return 0;
733 }
734 switch (op) {
735 case SDB_DATA_CONCAT: return data_concat(d1, d2, res);
736 case SDB_DATA_ADD: return data_lin(d1, 1, d2, res);
737 case SDB_DATA_SUB: return data_lin(d1, -1, d2, res);
738 case SDB_DATA_MUL: return data_mul(d1, d2, res);
739 case SDB_DATA_DIV: return data_div(d1, d2, res, NULL);
740 case SDB_DATA_MOD: return data_div(d1, d2, NULL, res);
741 }
742 return -1;
743 } /* sdb_data_expr_eval */
745 int
746 sdb_data_expr_type(int op, int type1, int type2)
747 {
748 int types_num = (int)SDB_STATIC_ARRAY_LEN(op_matrix[0]);
750 assert(SDB_STATIC_ARRAY_LEN(op_matrix[0])
751 == SDB_STATIC_ARRAY_LEN(op_matrix[0][0]));
753 if ((op <= 0) || (SDB_STATIC_ARRAY_LEN(op_matrix) < (size_t)op))
754 return -1;
756 /* arrays only support concat; element type has to match */
757 if ((type1 & SDB_TYPE_ARRAY) || (type2 & SDB_TYPE_ARRAY)) {
758 if (((type1 & 0xff) != (type2 & 0xff)) || (op != SDB_DATA_CONCAT))
759 return -1;
760 return type1 | SDB_TYPE_ARRAY;
761 }
762 if ((type1 < 0) || (types_num < type1)
763 || (type2 < 0) || (types_num < type2))
764 return -1;
766 if ((type1 == SDB_TYPE_NULL) || (type2 == SDB_TYPE_NULL))
767 return SDB_TYPE_NULL;
768 return op_matrix[op - 1][type1 - 1][type2 - 1];
769 } /* sdb_data_expr_type */
771 size_t
772 sdb_data_strlen(const sdb_data_t *datum)
773 {
774 if (! datum)
775 return 0;
777 if (datum->type == SDB_TYPE_INTEGER) {
778 /* log(64) */
779 return 20;
780 }
781 else if (datum->type == SDB_TYPE_DECIMAL) {
782 /* XXX: -d.dddddde+dd or -ddddd.dddddd */
783 return 42;
784 }
785 else if (datum->type == SDB_TYPE_STRING) {
786 if (! datum->data.string)
787 return 6; /* NULL */
788 /* in the worst case, each character needs to be escaped */
789 return 2 * strlen(datum->data.string) + 2;
790 }
791 else if (datum->type == SDB_TYPE_DATETIME) {
792 /* "YYYY-MM-DD HH:MM:SS +zzzz" */
793 return 27;
794 }
795 else if (datum->type == SDB_TYPE_BINARY) {
796 if (! datum->data.binary.datum)
797 return 6; /* NULL */
798 /* "\xNN" */
799 return 4 * datum->data.binary.length + 2;
800 }
801 else if (datum->type == SDB_TYPE_REGEX) {
802 if (! datum->data.re.raw)
803 return 6; /* NULL */
804 /* "/.../" */
805 return strlen(datum->data.re.raw) + 4;
806 }
807 else if (datum->type & SDB_TYPE_ARRAY) {
808 size_t len = 2; /* [] */
809 size_t i;
810 for (i = 0; i < datum->data.array.length; ++i) {
811 sdb_data_t v = SDB_DATA_INIT;
812 sdb_data_array_get(datum, i, &v);
813 len += sdb_data_strlen(&v) + 1;
814 }
815 return len;
816 }
817 return 0;
818 } /* sdb_data_strlen */
820 int
821 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
822 {
823 char tmp[sdb_data_strlen(datum) + 1];
824 char *data = NULL;
825 _Bool is_null = 0;
826 int ret = -1;
828 size_t i, pos;
830 if ((! datum) || (! buf) || (! buflen))
831 return -1;
833 if (datum->type == SDB_TYPE_INTEGER) {
834 ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
835 }
836 else if (datum->type == SDB_TYPE_DECIMAL) {
837 ret = snprintf(buf, buflen, "%g", datum->data.decimal);
838 }
839 else if (datum->type == SDB_TYPE_STRING) {
840 if (! datum->data.string)
841 is_null = 1;
842 else {
843 pos = 0;
844 for (i = 0; i < strlen(datum->data.string); ++i) {
845 char byte = datum->data.string[i];
847 if ((byte == '\\') || (byte == '"')) {
848 tmp[pos] = '\\';
849 ++pos;
850 }
851 tmp[pos] = byte;
852 ++pos;
853 }
854 tmp[pos] = '\0';
855 data = tmp;
856 }
857 }
858 else if (datum->type == SDB_TYPE_DATETIME) {
859 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
860 datum->data.datetime))
861 return -1;
862 tmp[sizeof(tmp) - 1] = '\0';
863 data = tmp;
864 }
865 else if (datum->type == SDB_TYPE_BINARY) {
866 pos = 0;
867 for (i = 0; i < datum->data.binary.length; ++i) {
868 int byte = (int)datum->data.binary.datum[i];
869 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
870 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
872 tmp[pos] = '\\';
873 tmp[pos + 1] = 'x';
874 pos += 2;
876 if (byte > 0xf) {
877 tmp[pos] = hex[byte >> 4];
878 ++pos;
879 }
880 tmp[pos] = hex[byte & 0xf];
881 ++pos;
882 }
883 if (datum->data.binary.datum) {
884 tmp[pos] = '\0';
885 data = tmp;
886 }
887 else
888 is_null = 1;
889 }
890 else if (datum->type == SDB_TYPE_REGEX) {
891 if (! datum->data.re.raw)
892 is_null = 1;
893 else {
894 snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
895 data = tmp;
896 }
897 }
898 else if (datum->type & SDB_TYPE_ARRAY) {
899 ret = 1;
900 buf[0] = '[';
901 for (i = 0; i < datum->data.array.length; ++i) {
902 sdb_data_t v = SDB_DATA_INIT;
903 int n;
904 if ((size_t)ret >= buflen - 1)
905 break;
907 if (ret > 1) {
908 buf[ret] = ',';
909 buf[ret + 1] = ' ';
910 ret += 2;
911 }
913 sdb_data_array_get(datum, i, &v);
914 n = sdb_data_format(&v, buf + ret, buflen - ret, quoted);
915 if (n > 0)
916 ret += n;
917 else
918 break;
919 }
920 if ((size_t)ret < buflen - 1) {
921 buf[ret] = ']';
922 buf[ret + 1] = '\0';
923 ++ret;
924 }
925 }
927 if (is_null) {
928 /* never quote NULL */
929 strncpy(buf, "NULL", buflen);
930 ret = (int)SDB_MIN(buflen, 4);
931 }
932 else if (data) {
933 if (quoted == SDB_UNQUOTED)
934 ret = snprintf(buf, buflen, "%s", data);
935 else if (quoted == SDB_SINGLE_QUOTED)
936 ret = snprintf(buf, buflen, "'%s'", data);
937 else
938 ret = snprintf(buf, buflen, "\"%s\"", data);
939 }
940 buf[buflen - 1] = '\0';
941 return ret;
942 } /* sdb_data_format */
944 int
945 sdb_data_parse(char *str, int type, sdb_data_t *data)
946 {
947 sdb_data_t tmp;
949 char *endptr = NULL;
951 errno = 0;
952 if (type == SDB_TYPE_INTEGER) {
953 tmp.data.integer = strtoll(str, &endptr, 0);
954 }
955 else if (type == SDB_TYPE_DECIMAL) {
956 tmp.data.decimal = strtod(str, &endptr);
957 }
958 else if (type == SDB_TYPE_STRING) {
959 tmp.data.string = str;
960 }
961 else if (type == SDB_TYPE_DATETIME) {
962 double datetime = strtod(str, &endptr);
963 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
964 }
965 else if (type == SDB_TYPE_BINARY) {
966 /* we don't support any binary information containing 0-bytes */
967 tmp.data.binary.length = strlen(str);
968 tmp.data.binary.datum = (unsigned char *)str;
969 }
970 else if (type == SDB_TYPE_REGEX) {
971 tmp.data.re.raw = strdup(str);
972 if (! tmp.data.re.raw)
973 return -1;
974 if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
975 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
976 sdb_log(SDB_LOG_ERR, "core: Failed to compile regular "
977 "expression '%s'", tmp.data.re.raw);
978 free(tmp.data.re.raw);
979 return -1;
980 }
981 if (! data) {
982 tmp.type = SDB_TYPE_REGEX;
983 sdb_data_free_datum(&tmp);
984 }
985 }
986 else if (type & SDB_TYPE_ARRAY) {
987 /* TODO */
988 errno = ENOTSUP;
989 return -1;
990 }
991 else {
992 errno = EINVAL;
993 return -1;
994 }
996 if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
997 || (type == SDB_TYPE_DATETIME)) {
998 if (errno || (str == endptr)) {
999 char errbuf[1024];
1000 sdb_log(SDB_LOG_ERR, "core: Failed to parse string "
1001 "'%s' as numeric value (type %i): %s", str, type,
1002 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1003 return -1;
1004 }
1005 else if (endptr && (*endptr != '\0'))
1006 sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after "
1007 "number while parsing numeric value (type %i): %s.",
1008 type, endptr);
1009 }
1011 if (data) {
1012 *data = tmp;
1013 data->type = type;
1014 }
1015 return 0;
1016 } /* sdb_data_parse */
1018 size_t
1019 sdb_data_sizeof(int type)
1020 {
1021 sdb_data_t v;
1022 if (type == SDB_TYPE_INTEGER)
1023 return sizeof(v.data.integer);
1024 else if (type == SDB_TYPE_DECIMAL)
1025 return sizeof(v.data.decimal);
1026 else if (type == SDB_TYPE_STRING)
1027 return sizeof(v.data.string);
1028 else if (type == SDB_TYPE_DATETIME)
1029 return sizeof(v.data.datetime);
1030 else if (type == SDB_TYPE_BINARY)
1031 return sizeof(v.data.binary);
1032 else if (type == SDB_TYPE_REGEX)
1033 return sizeof(v.data.re);
1034 return 0;
1035 } /* sdb_data_sizeof */
1037 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */