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 * private helper functions
51 */
53 /* this function supports in-place copies */
54 static int
55 copy_array_values(sdb_data_t *dst, const sdb_data_t *src, size_t elem_size)
56 {
57 int type = src->type & 0xff;
59 if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)) {
60 if (dst != src)
61 memcpy(dst->data.array.values, src->data.array.values,
62 src->data.array.length * elem_size);
63 }
64 else if (type == SDB_TYPE_STRING) {
65 char **s = src->data.array.values;
66 char **d = dst->data.array.values;
67 size_t i;
69 for (i = 0; i < src->data.array.length; ++i) {
70 d[i] = strdup(s[i]);
71 if (! d[i])
72 return -1;
73 }
74 }
75 else {
76 /* TODO */
77 errno = ENOTSUP;
78 return -1;
79 }
80 return 0;
81 } /* copy_array_values */
83 static void
84 free_array_values(sdb_data_t *datum)
85 {
86 int type = datum->type & 0xff;
88 if (type == SDB_TYPE_STRING) {
89 char **v = datum->data.array.values;
90 size_t i;
92 for (i = 0; i < datum->data.array.length; ++i) {
93 if (v[i])
94 free(v[i]);
95 v[i] = NULL;
96 }
97 }
98 } /* free_array_values */
100 /* compare two arrays element-by-element returning how the first non-equal
101 * elements compare to each other */
102 static int
103 array_cmp(const sdb_data_t *a1, const sdb_data_t *a2)
104 {
105 int type = a1->type & 0xff;
106 size_t len, i;
108 assert((a1->type == a2->type) && (a1->type & SDB_TYPE_ARRAY));
110 len = SDB_MIN(a1->data.array.length, a2->data.array.length);
112 if (type == SDB_TYPE_INTEGER) {
113 int64_t *v1 = a1->data.array.values;
114 int64_t *v2 = a2->data.array.values;
116 for (i = 0; i < len; ++i)
117 if (v1[i] != v2[i])
118 return SDB_CMP(v1[i], v2[i]);
119 }
120 else if (type == SDB_TYPE_DECIMAL) {
121 double *v1 = a1->data.array.values;
122 double *v2 = a2->data.array.values;
124 for (i = 0; i < len; ++i)
125 if (v1[i] != v2[i])
126 return SDB_CMP(v1[i], v2[i]);
127 }
128 else if (type == SDB_TYPE_STRING) {
129 char **v1 = a1->data.array.values;
130 char **v2 = a2->data.array.values;
132 for (i = 0; i < len; ++i) {
133 int diff = strcasecmp(v1[i], v2[i]);
134 if (diff)
135 return diff;
136 }
137 }
138 else {
139 /* TODO */
140 errno = ENOTSUP;
141 /* but fall through to ensure stable sorting: */
142 }
143 return SDB_CMP(a1->data.array.length, a2->data.array.length);
144 } /* array_cmp */
146 /* Calculate the linear function 'd1 + n * d2'. */
147 static int
148 data_lin(const sdb_data_t *d1, int n, const sdb_data_t *d2, sdb_data_t *res)
149 {
150 if (d1->type != d2->type)
151 return -1;
153 if (d1->type == SDB_TYPE_INTEGER)
154 res->data.integer = d1->data.integer + (int64_t)n * d2->data.integer;
155 else if (d1->type == SDB_TYPE_DECIMAL)
156 res->data.decimal = d1->data.decimal + (double)n * d2->data.decimal;
157 else if (d1->type == SDB_TYPE_DATETIME)
158 res->data.datetime = d1->data.datetime + (sdb_time_t)n * d2->data.datetime;
159 else
160 return -1;
161 res->type = d1->type;
162 return 0;
163 } /* data_lin */
165 /* Multiply d1 with d2. */
166 static int
167 data_mul(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
168 {
169 if (d1->type == SDB_TYPE_INTEGER) {
170 if (d2->type == SDB_TYPE_INTEGER)
171 res->data.integer = d1->data.integer * d2->data.integer;
172 else if (d2->type == SDB_TYPE_DATETIME) {
173 res->data.datetime = (sdb_time_t)d1->data.integer
174 * d2->data.datetime;
175 res->type = SDB_TYPE_DATETIME;
176 return 0;
177 }
178 else
179 return -1;
180 }
181 else if (d1->type == SDB_TYPE_DECIMAL) {
182 if (d2->type == SDB_TYPE_DECIMAL)
183 res->data.decimal = d1->data.decimal * d2->data.decimal;
184 else if (d2->type == SDB_TYPE_DATETIME) {
185 res->data.datetime = (sdb_time_t)(d1->data.decimal
186 * (double)d2->data.datetime);
187 res->type = SDB_TYPE_DATETIME;
188 return 0;
189 }
190 else
191 return -1;
192 }
193 else if (d1->type == SDB_TYPE_DATETIME) {
194 if (d2->type == SDB_TYPE_DATETIME)
195 res->data.datetime = d1->data.datetime
196 * d2->data.datetime;
197 else if (d2->type == SDB_TYPE_INTEGER)
198 res->data.datetime = d1->data.datetime
199 * (sdb_time_t)d2->data.integer;
200 else if (d2->type == SDB_TYPE_DECIMAL)
201 res->data.datetime = (sdb_time_t)((double)d1->data.datetime
202 * d2->data.decimal);
203 else
204 return -1;
205 }
206 else
207 return -1;
209 res->type = d1->type;
210 return 0;
211 } /* data_mul */
213 /* Device d1 by d2 and return the result and the remainder. */
214 static int
215 data_div(const sdb_data_t *d1, const sdb_data_t *d2,
216 sdb_data_t *res, sdb_data_t *rem)
217 {
218 if (d1->type == SDB_TYPE_INTEGER) {
219 if (d2->type != SDB_TYPE_INTEGER)
220 return -1;
221 if (res)
222 res->data.integer = d1->data.integer / d2->data.integer;
223 if (rem)
224 rem->data.integer = d1->data.integer % d2->data.integer;
225 }
226 else if (d1->type == SDB_TYPE_DECIMAL) {
227 if (d2->type != SDB_TYPE_DECIMAL)
228 return -1;
229 if (res)
230 res->data.decimal = d1->data.decimal / d2->data.decimal;
231 if (rem)
232 rem->data.decimal = fmod(d1->data.decimal, d2->data.decimal);
233 }
234 else if (d1->type == SDB_TYPE_DATETIME) {
235 if (d2->type == SDB_TYPE_DECIMAL) {
236 if (res)
237 res->data.datetime = (sdb_time_t)((double)d1->data.datetime
238 / d2->data.decimal);
239 if (rem) {
240 double tmp = fmod((double)d1->data.datetime, d2->data.decimal);
241 rem->data.datetime = (sdb_time_t)tmp;
242 }
243 }
244 else {
245 sdb_time_t a, b;
246 if (d2->type == SDB_TYPE_DATETIME) {
247 a = d1->data.datetime;
248 b = d2->data.datetime;
249 }
250 else if (d2->type == SDB_TYPE_INTEGER) {
251 a = d1->data.datetime;
252 b = (sdb_time_t)d2->data.integer;
253 }
254 else
255 return -1;
256 if (res)
257 res->data.datetime = a / b;
258 if (rem)
259 rem->data.datetime = a % b;
260 }
261 }
262 else
263 return -1;
265 if (res)
266 res->type = d1->type;
267 if (rem)
268 rem->type = d1->type;
269 return 0;
270 } /* data_div */
272 /* Concatenate d1 and d2. */
273 static int
274 data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
275 {
276 unsigned char *new;
277 unsigned char *s1, *s2;
278 size_t len1, len2;
280 /* TODO: support array plus element */
281 if (d1->type != d2->type)
282 return -1;
284 if (d1->type == SDB_TYPE_STRING) {
285 s1 = (unsigned char *)d1->data.string;
286 s2 = (unsigned char *)d2->data.string;
287 len1 = s1 ? strlen((char *)s1) : 0;
288 len2 = s2 ? strlen((char *)s2) : 0;
289 }
290 else if (d1->type == SDB_TYPE_BINARY) {
291 s1 = d1->data.binary.datum;
292 s2 = d2->data.binary.datum;
293 len1 = d1->data.binary.length;
294 len2 = d2->data.binary.length;
295 }
296 else if (d1->type & SDB_TYPE_ARRAY) {
297 size_t elem_size = sdb_data_sizeof(d1->type & 0xff);
298 s1 = (unsigned char *)d1->data.array.values;
299 s2 = (unsigned char *)d2->data.array.values;
300 len1 = d1->data.array.length * elem_size;
301 len2 = d2->data.array.length * elem_size;
302 }
303 else
304 return -1;
306 assert(s1 && s2);
308 new = malloc(len1 + len2 + 1);
309 if (! new)
310 return -1;
312 if (len1)
313 memcpy(new, s1, len1);
314 if (len2)
315 memcpy(new + len1, s2, len2);
316 new[len1 + len2] = '\0';
318 res->type = d1->type;
319 if (res->type == SDB_TYPE_STRING) {
320 res->data.string = (char *)new;
321 }
322 else if (res->type == SDB_TYPE_BINARY) {
323 res->data.binary.datum = new;
324 res->data.binary.length = len1 + len2;
325 }
326 else if (d1->type & SDB_TYPE_ARRAY) {
327 res->data.array.values = new;
328 res->data.array.length = d1->data.array.length + d2->data.array.length;
329 if (copy_array_values(res, res, sdb_data_sizeof(res->type & 0xff))) {
330 /* this leaks already copied values but there's not much we can
331 * do and this should only happen if we're in trouble anyway */
332 free(new);
333 res->data.array.values = NULL;
334 res->data.array.length = 0;
335 return -1;
336 }
337 }
338 return 0;
339 } /* data_concat */
341 /*
342 * public API
343 */
345 const sdb_data_t SDB_DATA_NULL = SDB_DATA_INIT;
347 int
348 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
349 {
350 sdb_data_t tmp;
352 if ((! dst) || (! src))
353 return -1;
355 tmp = *src;
356 if (src->type == SDB_TYPE_STRING) {
357 if (src->data.string) {
358 tmp.data.string = strdup(src->data.string);
359 if (! tmp.data.string)
360 return -1;
361 }
362 }
363 else if (src->type == SDB_TYPE_BINARY) {
364 if (src->data.binary.datum) {
365 tmp.data.binary.datum = malloc(src->data.binary.length);
366 if (! tmp.data.binary.datum)
367 return -1;
368 memcpy(tmp.data.binary.datum, src->data.binary.datum,
369 src->data.binary.length);
370 }
371 }
372 else if (src->type == SDB_TYPE_REGEX) {
373 if (src->data.re.raw) {
374 tmp.data.re.raw = strdup(src->data.re.raw);
375 if (! tmp.data.re.raw)
376 return -1;
377 /* we need to recompile because the regex might point to
378 * dynamically allocated memory */
379 if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
380 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
381 free(tmp.data.re.raw);
382 return -1;
383 }
384 }
385 else
386 memset(&tmp.data.re.regex, 0, sizeof(tmp.data.re.regex));
387 }
388 else if (src->type & SDB_TYPE_ARRAY) {
389 if (src->data.array.values) {
390 size_t elem_size = sdb_data_sizeof(src->type & 0xff);
391 tmp.data.array.values = calloc(src->data.array.length, elem_size);
392 if (! tmp.data.array.values)
393 return -1;
394 if (copy_array_values(&tmp, src, elem_size)) {
395 sdb_data_free_datum(&tmp);
396 return -1;
397 }
398 }
399 }
401 sdb_data_free_datum(dst);
402 *dst = tmp;
403 return 0;
404 } /* sdb_data_copy */
406 void
407 sdb_data_free_datum(sdb_data_t *datum)
408 {
409 if (! datum)
410 return;
412 if (datum->type == SDB_TYPE_STRING) {
413 if (datum->data.string)
414 free(datum->data.string);
415 datum->data.string = NULL;
416 }
417 else if (datum->type == SDB_TYPE_BINARY) {
418 if (datum->data.binary.datum)
419 free(datum->data.binary.datum);
420 datum->data.binary.datum = NULL;
421 datum->data.binary.length = 0;
422 }
423 else if (datum->type == SDB_TYPE_REGEX) {
424 if (datum->data.re.raw) {
425 free(datum->data.re.raw);
426 regfree(&datum->data.re.regex);
427 }
428 datum->data.re.raw = NULL;
429 memset(&datum->data.re.regex, 0, sizeof(datum->data.re.regex));
430 }
431 else if (datum->type & SDB_TYPE_ARRAY) {
432 free_array_values(datum);
433 if (datum->data.array.values)
434 free(datum->data.array.values);
435 datum->data.array.values = NULL;
436 datum->data.array.length = 0;
437 }
438 } /* sdb_data_free_datum */
440 int
441 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
442 {
443 #define CMP_NULL(a, b) \
444 do { \
445 if (!(a) && !(b)) return 0; \
446 if (!(a)) return -1; \
447 if (!(b)) return 1; \
448 } while (0)
450 CMP_NULL(d1, d2);
452 if (d1->type != d2->type)
453 return SDB_CMP(d1->type, d2->type);
455 if (d1->type == SDB_TYPE_INTEGER)
456 return SDB_CMP(d1->data.integer, d2->data.integer);
457 else if (d1->type == SDB_TYPE_DECIMAL)
458 return SDB_CMP(d1->data.decimal, d2->data.decimal);
459 else if (d1->type == SDB_TYPE_STRING) {
460 CMP_NULL(d1->data.string, d2->data.string);
461 return strcasecmp(d1->data.string, d2->data.string);
462 }
463 else if (d1->type == SDB_TYPE_DATETIME)
464 return SDB_CMP(d1->data.datetime, d2->data.datetime);
465 else if (d1->type == SDB_TYPE_BINARY) {
466 int diff;
468 CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
470 /* on a common prefix, the shorter datum sorts less */
471 if (d1->data.binary.length < d2->data.binary.length) {
472 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
473 d1->data.binary.length);
474 diff = diff ? diff : -1;
475 }
476 else if (d1->data.binary.length > d2->data.binary.length) {
477 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
478 d2->data.binary.length);
479 diff = diff ? diff : 1;
480 }
481 else
482 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
483 d1->data.binary.length);
485 return diff;
486 }
487 else if (d1->type == SDB_TYPE_REGEX) {
488 CMP_NULL(d1->data.re.raw, d2->data.re.raw);
489 return strcmp(d1->data.re.raw, d2->data.re.raw);
490 }
491 else if (d1->type & SDB_TYPE_ARRAY) {
492 CMP_NULL(d1->data.array.values, d2->data.array.values);
493 return array_cmp(d1, d2);
494 }
495 return -1;
496 } /* sdb_data_cmp */
498 int
499 sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2)
500 {
501 char d1_str[sdb_data_strlen(d1) + 1];
502 char d2_str[sdb_data_strlen(d2) + 1];
504 if ((d1->type & SDB_TYPE_ARRAY) || (d2->type & SDB_TYPE_ARRAY)) {
505 /* TODO */
506 errno = ENOTSUP;
507 return -1;
508 }
510 if (sdb_data_isnull(d1))
511 d1 = NULL;
512 if (sdb_data_isnull(d2))
513 d2 = NULL;
515 CMP_NULL(d1, d2);
517 if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0)
518 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
519 if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0)
520 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
522 return strcasecmp(d1_str, d2_str);
523 #undef CMP_NULL
524 } /* sdb_data_strcmp */
526 _Bool
527 sdb_data_isnull(const sdb_data_t *datum)
528 {
529 if (! datum)
530 return 1;
531 if (datum->type == SDB_TYPE_NULL)
532 return 1;
533 if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string))
534 return 1;
535 if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
536 return 1;
537 if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw))
538 return 1;
539 if ((datum->type & SDB_TYPE_ARRAY) && (! datum->data.array.values))
540 return 1;
541 return 0;
542 } /* sdb_data_isnull */
544 _Bool
545 sdb_data_inarray(const sdb_data_t *value, const sdb_data_t *array)
546 {
547 size_t i;
549 if (sdb_data_isnull(value) || sdb_data_isnull(array))
550 return 0;
551 if ((value->type & SDB_TYPE_ARRAY) || (! (array->type & SDB_TYPE_ARRAY)))
552 return 0;
553 if (value->type != (array->type & 0xff))
554 return 0;
556 if (value->type == SDB_TYPE_INTEGER) {
557 int64_t *v = array->data.array.values;
558 for (i = 0; i < array->data.array.length; ++i)
559 if (value->data.integer == v[i])
560 return 1;
561 }
562 else if (value->type == SDB_TYPE_DECIMAL) {
563 double *v = array->data.array.values;
564 for (i = 0; i < array->data.array.length; ++i)
565 if (value->data.decimal == v[i])
566 return 1;
567 }
568 else if (value->type == SDB_TYPE_STRING) {
569 char **v = array->data.array.values;
570 for (i = 0; i < array->data.array.length; ++i)
571 if (!strcasecmp(value->data.string, v[i]))
572 return 1;
573 }
574 else {
575 /* TODO */
576 errno = ENOTSUP;
577 return 0;
578 }
579 return 0;
580 } /* sdb_data_inarray */
582 int
583 sdb_data_parse_op(const char *op)
584 {
585 if (! strcmp(op, "+"))
586 return SDB_DATA_ADD;
587 else if (! strcmp(op, "-"))
588 return SDB_DATA_SUB;
589 else if (! strcmp(op, "*"))
590 return SDB_DATA_MUL;
591 else if (! strcmp(op, "/"))
592 return SDB_DATA_DIV;
593 else if (! strcmp(op, "%"))
594 return SDB_DATA_MOD;
595 else if (! strcmp(op, "||"))
596 return SDB_DATA_CONCAT;
597 return -1;
598 } /* sdb_data_parse_op */
600 int
601 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
602 sdb_data_t *res)
603 {
604 if ((! d1) || (! d2) || (! res))
605 return -1;
606 if (sdb_data_isnull(d1) || sdb_data_isnull(d2)) {
607 *res = SDB_DATA_NULL;
608 return 0;
609 }
610 switch (op) {
611 case SDB_DATA_CONCAT:
612 return data_concat(d1, d2, res);
613 case SDB_DATA_ADD:
614 return data_lin(d1, 1, d2, res);
615 case SDB_DATA_SUB:
616 return data_lin(d1, -1, d2, res);
617 case SDB_DATA_MUL:
618 return data_mul(d1, d2, res);
619 case SDB_DATA_DIV:
620 return data_div(d1, d2, res, NULL);
621 case SDB_DATA_MOD:
622 return data_div(d1, d2, NULL, res);
623 }
624 return -1;
625 } /* sdb_data_expr_eval */
627 size_t
628 sdb_data_strlen(const sdb_data_t *datum)
629 {
630 if (! datum)
631 return 0;
633 if (datum->type == SDB_TYPE_INTEGER) {
634 /* log(64) */
635 return 20;
636 }
637 else if (datum->type == SDB_TYPE_DECIMAL) {
638 /* XXX: -d.dddddde+dd or -ddddd.dddddd */
639 return 42;
640 }
641 else if (datum->type == SDB_TYPE_STRING) {
642 if (! datum->data.string)
643 return 8; /* "<NULL>" */
644 /* in the worst case, each character needs to be escaped */
645 return 2 * strlen(datum->data.string) + 2;
646 }
647 else if (datum->type == SDB_TYPE_DATETIME) {
648 /* "YYYY-MM-DD HH:MM:SS +zzzz" */
649 return 27;
650 }
651 else if (datum->type == SDB_TYPE_BINARY) {
652 if (! datum->data.binary.datum)
653 return 8; /* "<NULL>" */
654 /* "\xNN" */
655 return 4 * datum->data.binary.length + 2;
656 }
657 else if (datum->type == SDB_TYPE_REGEX) {
658 if (! datum->data.re.raw)
659 return 8; /* "<NULL>" */
660 /* "/.../" */
661 return strlen(datum->data.re.raw) + 4;
662 }
663 else if (datum->type & SDB_TYPE_ARRAY) {
664 /* TODO */
665 errno = ENOTSUP;
666 return 0;
667 }
668 return 0;
669 } /* sdb_data_strlen */
671 int
672 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
673 {
674 char tmp[sdb_data_strlen(datum) + 1];
675 char *data = NULL;
676 int ret = -1;
678 size_t i, pos;
680 if ((! datum) || (! buf))
681 return -1;
683 if (datum->type == SDB_TYPE_INTEGER) {
684 ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
685 }
686 else if (datum->type == SDB_TYPE_DECIMAL) {
687 ret = snprintf(buf, buflen, "%g", datum->data.decimal);
688 }
689 else if (datum->type == SDB_TYPE_STRING) {
690 if (! datum->data.string)
691 data = "<NULL>";
692 else {
693 pos = 0;
694 for (i = 0; i < strlen(datum->data.string); ++i) {
695 char byte = datum->data.string[i];
697 if ((byte == '\\') || (byte == '"')) {
698 tmp[pos] = '\\';
699 ++pos;
700 }
701 tmp[pos] = byte;
702 ++pos;
703 }
704 tmp[pos] = '\0';
705 data = tmp;
706 }
707 }
708 else if (datum->type == SDB_TYPE_DATETIME) {
709 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
710 datum->data.datetime))
711 return -1;
712 tmp[sizeof(tmp) - 1] = '\0';
713 data = tmp;
714 }
715 else if (datum->type == SDB_TYPE_BINARY) {
716 pos = 0;
717 for (i = 0; i < datum->data.binary.length; ++i) {
718 int byte = (int)datum->data.binary.datum[i];
719 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
720 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
722 tmp[pos] = '\\';
723 tmp[pos + 1] = 'x';
724 pos += 2;
726 if (byte > 0xf) {
727 tmp[pos] = hex[byte >> 4];
728 ++pos;
729 }
730 tmp[pos] = hex[byte & 0xf];
731 ++pos;
732 }
733 if (datum->data.binary.datum) {
734 tmp[pos] = '\0';
735 data = tmp;
736 }
737 else
738 data = "<NULL>";
739 }
740 else if (datum->type == SDB_TYPE_REGEX) {
741 if (! datum->data.re.raw)
742 data = "<NULL>";
743 else {
744 snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
745 data = tmp;
746 }
747 }
748 else if (datum->type & SDB_TYPE_ARRAY) {
749 /* TODO */
750 errno = ENOTSUP;
751 return -1;
752 }
754 if (data) {
755 if (quoted == SDB_UNQUOTED)
756 ret = snprintf(buf, buflen, "%s", data);
757 else if (quoted == SDB_SINGLE_QUOTED)
758 ret = snprintf(buf, buflen, "'%s'", data);
759 else
760 ret = snprintf(buf, buflen, "\"%s\"", data);
761 }
762 buf[buflen - 1] = '\0';
763 return ret;
764 } /* sdb_data_format */
766 int
767 sdb_data_parse(char *str, int type, sdb_data_t *data)
768 {
769 sdb_data_t tmp;
771 char *endptr = NULL;
773 errno = 0;
774 if (type == SDB_TYPE_INTEGER) {
775 tmp.data.integer = strtoll(str, &endptr, 0);
776 }
777 else if (type == SDB_TYPE_DECIMAL) {
778 tmp.data.decimal = strtod(str, &endptr);
779 }
780 else if (type == SDB_TYPE_STRING) {
781 tmp.data.string = str;
782 }
783 else if (type == SDB_TYPE_DATETIME) {
784 double datetime = strtod(str, &endptr);
785 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
786 }
787 else if (type == SDB_TYPE_BINARY) {
788 /* we don't support any binary information containing 0-bytes */
789 tmp.data.binary.length = strlen(str);
790 tmp.data.binary.datum = (unsigned char *)str;
791 }
792 else if (type == SDB_TYPE_REGEX) {
793 tmp.data.re.raw = strdup(str);
794 if (! tmp.data.re.raw)
795 return -1;
796 if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
797 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
798 sdb_log(SDB_LOG_ERR, "core: Failed to compile regular "
799 "expression '%s'", tmp.data.re.raw);
800 free(tmp.data.re.raw);
801 return -1;
802 }
803 if (! data) {
804 tmp.type = SDB_TYPE_REGEX;
805 sdb_data_free_datum(&tmp);
806 }
807 }
808 else if (type & SDB_TYPE_ARRAY) {
809 /* TODO */
810 errno = ENOTSUP;
811 return -1;
812 }
813 else {
814 errno = EINVAL;
815 return -1;
816 }
818 if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
819 || (type == SDB_TYPE_DATETIME)) {
820 if (errno || (str == endptr)) {
821 char errbuf[1024];
822 sdb_log(SDB_LOG_ERR, "core: Failed to parse string "
823 "'%s' as numeric value (type %i): %s", str, type,
824 sdb_strerror(errno, errbuf, sizeof(errbuf)));
825 return -1;
826 }
827 else if (endptr && (*endptr != '\0'))
828 sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after "
829 "number while parsing numeric value (type %i): %s.",
830 type, endptr);
831 }
833 if (data) {
834 *data = tmp;
835 data->type = type;
836 }
837 return 0;
838 } /* sdb_data_parse */
840 size_t
841 sdb_data_sizeof(int type)
842 {
843 sdb_data_t v;
844 if (type == SDB_TYPE_INTEGER)
845 return sizeof(v.data.integer);
846 else if (type == SDB_TYPE_DECIMAL)
847 return sizeof(v.data.decimal);
848 else if (type == SDB_TYPE_STRING)
849 return sizeof(v.data.string);
850 else if (type == SDB_TYPE_DATETIME)
851 return sizeof(v.data.datetime);
852 else if (type == SDB_TYPE_BINARY)
853 return sizeof(v.data.binary);
854 else if (type == SDB_TYPE_REGEX)
855 return sizeof(v.data.re);
856 return 0;
857 } /* sdb_data_sizeof */
859 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */