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 /* Calculate the linear function 'd1 + n * d2'. */
54 static int
55 data_lin(const sdb_data_t *d1, int n, const sdb_data_t *d2, sdb_data_t *res)
56 {
57 if (d1->type != d2->type)
58 return -1;
60 if (d1->type == SDB_TYPE_INTEGER)
61 res->data.integer = d1->data.integer + (int64_t)n * d2->data.integer;
62 else if (d1->type == SDB_TYPE_DECIMAL)
63 res->data.decimal = d1->data.decimal + (double)n * d2->data.decimal;
64 else if (d1->type == SDB_TYPE_DATETIME)
65 res->data.datetime = d1->data.datetime + (sdb_time_t)n * d2->data.datetime;
66 else
67 return -1;
68 res->type = d1->type;
69 return 0;
70 } /* data_lin */
72 /* Multiply d1 with d2. */
73 static int
74 data_mul(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
75 {
76 if (d1->type == SDB_TYPE_INTEGER) {
77 if (d2->type == SDB_TYPE_INTEGER)
78 res->data.integer = d1->data.integer * d2->data.integer;
79 else if (d2->type == SDB_TYPE_DATETIME) {
80 res->data.datetime = (sdb_time_t)d1->data.integer
81 * d2->data.datetime;
82 res->type = SDB_TYPE_DATETIME;
83 return 0;
84 }
85 else
86 return -1;
87 }
88 else if (d1->type == SDB_TYPE_DECIMAL) {
89 if (d2->type == SDB_TYPE_DECIMAL)
90 res->data.decimal = d1->data.decimal * d2->data.decimal;
91 else if (d2->type == SDB_TYPE_DATETIME) {
92 res->data.datetime = (sdb_time_t)(d1->data.decimal
93 * (double)d2->data.datetime);
94 res->type = SDB_TYPE_DATETIME;
95 return 0;
96 }
97 else
98 return -1;
99 }
100 else if (d1->type == SDB_TYPE_DATETIME) {
101 if (d2->type == SDB_TYPE_DATETIME)
102 res->data.datetime = d1->data.datetime
103 * d2->data.datetime;
104 else if (d2->type == SDB_TYPE_INTEGER)
105 res->data.datetime = d1->data.datetime
106 * (sdb_time_t)d2->data.integer;
107 else if (d2->type == SDB_TYPE_DECIMAL)
108 res->data.datetime = (sdb_time_t)((double)d1->data.datetime
109 * d2->data.decimal);
110 else
111 return -1;
112 }
113 else
114 return -1;
116 res->type = d1->type;
117 return 0;
118 } /* data_mul */
120 /* Device d1 by d2 and return the result and the remainder. */
121 static int
122 data_div(const sdb_data_t *d1, const sdb_data_t *d2,
123 sdb_data_t *res, sdb_data_t *rem)
124 {
125 if (d1->type == SDB_TYPE_INTEGER) {
126 if (d2->type != SDB_TYPE_INTEGER)
127 return -1;
128 if (res)
129 res->data.integer = d1->data.integer / d2->data.integer;
130 if (rem)
131 rem->data.integer = d1->data.integer % d2->data.integer;
132 }
133 else if (d1->type == SDB_TYPE_DECIMAL) {
134 if (d2->type != SDB_TYPE_DECIMAL)
135 return -1;
136 if (res)
137 res->data.decimal = d1->data.decimal / d2->data.decimal;
138 if (rem)
139 rem->data.decimal = fmod(d1->data.decimal, d2->data.decimal);
140 }
141 else if (d1->type == SDB_TYPE_DATETIME) {
142 if (d2->type == SDB_TYPE_DECIMAL) {
143 if (res)
144 res->data.datetime = (sdb_time_t)((double)d1->data.datetime
145 / d2->data.decimal);
146 if (rem) {
147 double tmp = fmod((double)d1->data.datetime, d2->data.decimal);
148 rem->data.datetime = (sdb_time_t)tmp;
149 }
150 }
151 else {
152 sdb_time_t a, b;
153 if (d2->type == SDB_TYPE_DATETIME) {
154 a = d1->data.datetime;
155 b = d2->data.datetime;
156 }
157 else if (d2->type == SDB_TYPE_INTEGER) {
158 a = d1->data.datetime;
159 b = (sdb_time_t)d2->data.integer;
160 }
161 else
162 return -1;
163 if (res)
164 res->data.datetime = a / b;
165 if (rem)
166 rem->data.datetime = a % b;
167 }
168 }
169 else
170 return -1;
172 if (res)
173 res->type = d1->type;
174 if (rem)
175 rem->type = d1->type;
176 return 0;
177 } /* data_div */
179 /* Concatenate d1 and d2. */
180 static int
181 data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
182 {
183 unsigned char *new;
184 unsigned char *s1, *s2;
185 size_t len1, len2;
187 if (d1->type != d2->type)
188 return -1;
190 if (d1->type == SDB_TYPE_STRING) {
191 s1 = (unsigned char *)d1->data.string;
192 s2 = (unsigned char *)d2->data.string;
193 len1 = s1 ? strlen((char *)s1) : 0;
194 len2 = s2 ? strlen((char *)s2) : 0;
195 }
196 else if (d1->type == SDB_TYPE_BINARY) {
197 s1 = d1->data.binary.datum;
198 s2 = d2->data.binary.datum;
199 len1 = d1->data.binary.length;
200 len2 = d2->data.binary.length;
201 }
202 else
203 return -1;
205 assert(s1 && s2);
207 new = malloc(len1 + len2 + 1);
208 if (! new)
209 return -1;
211 if (len1)
212 memcpy(new, s1, len1);
213 if (len2)
214 memcpy(new + len1, s2, len2);
215 new[len1 + len2] = '\0';
217 res->type = d1->type;
218 if (res->type == SDB_TYPE_STRING) {
219 res->data.string = (char *)new;
220 }
221 else {
222 res->data.binary.datum = new;
223 res->data.binary.length = len1 + len2;
224 }
225 return 0;
226 } /* data_concat */
228 /*
229 * public API
230 */
232 const sdb_data_t SDB_DATA_NULL = SDB_DATA_INIT;
234 int
235 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
236 {
237 sdb_data_t tmp;
239 if ((! dst) || (! src))
240 return -1;
242 tmp = *src;
243 if (src->type == SDB_TYPE_STRING) {
244 if (src->data.string) {
245 tmp.data.string = strdup(src->data.string);
246 if (! tmp.data.string)
247 return -1;
248 }
249 }
250 else if (src->type == SDB_TYPE_BINARY) {
251 if (src->data.binary.datum) {
252 tmp.data.binary.datum = malloc(src->data.binary.length);
253 if (! tmp.data.binary.datum)
254 return -1;
255 memcpy(tmp.data.binary.datum, src->data.binary.datum,
256 src->data.binary.length);
257 }
258 }
259 else if (src->type == SDB_TYPE_REGEX) {
260 if (src->data.re.raw) {
261 tmp.data.re.raw = strdup(src->data.re.raw);
262 if (! tmp.data.re.raw)
263 return -1;
264 /* we need to recompile because the regex might point to
265 * dynamically allocated memory */
266 if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
267 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
268 free(tmp.data.re.raw);
269 return -1;
270 }
271 }
272 else
273 memset(&tmp.data.re.regex, 0, sizeof(tmp.data.re.regex));
274 }
276 sdb_data_free_datum(dst);
277 *dst = tmp;
278 return 0;
279 } /* sdb_data_copy */
281 void
282 sdb_data_free_datum(sdb_data_t *datum)
283 {
284 if (! datum)
285 return;
287 if (datum->type == SDB_TYPE_STRING) {
288 if (datum->data.string)
289 free(datum->data.string);
290 datum->data.string = NULL;
291 }
292 else if (datum->type == SDB_TYPE_BINARY) {
293 if (datum->data.binary.datum)
294 free(datum->data.binary.datum);
295 datum->data.binary.datum = NULL;
296 datum->data.binary.length = 0;
297 }
298 else if (datum->type == SDB_TYPE_REGEX) {
299 if (datum->data.re.raw) {
300 free(datum->data.re.raw);
301 regfree(&datum->data.re.regex);
302 }
303 datum->data.re.raw = NULL;
304 memset(&datum->data.re.regex, 0, sizeof(datum->data.re.regex));
305 }
306 } /* sdb_data_free_datum */
308 int
309 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
310 {
311 #define CMP_NULL(a, b) \
312 do { \
313 if (!(a) && !(b)) return 0; \
314 if (!(a)) return -1; \
315 if (!(b)) return 1; \
316 } while (0)
318 CMP_NULL(d1, d2);
320 if (d1->type != d2->type)
321 return SDB_CMP(d1->type, d2->type);
323 if (d1->type == SDB_TYPE_INTEGER)
324 return SDB_CMP(d1->data.integer, d2->data.integer);
325 else if (d1->type == SDB_TYPE_DECIMAL)
326 return SDB_CMP(d1->data.decimal, d2->data.decimal);
327 else if (d1->type == SDB_TYPE_STRING) {
328 CMP_NULL(d1->data.string, d2->data.string);
329 return strcasecmp(d1->data.string, d2->data.string);
330 }
331 else if (d1->type == SDB_TYPE_DATETIME)
332 return SDB_CMP(d1->data.datetime, d2->data.datetime);
333 else if (d1->type == SDB_TYPE_BINARY) {
334 int diff;
336 CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
338 /* on a common prefix, the shorter datum sorts less */
339 if (d1->data.binary.length < d2->data.binary.length) {
340 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
341 d1->data.binary.length);
342 diff = diff ? diff : -1;
343 }
344 else if (d1->data.binary.length > d2->data.binary.length) {
345 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
346 d2->data.binary.length);
347 diff = diff ? diff : 1;
348 }
349 else
350 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
351 d1->data.binary.length);
353 return diff;
354 }
355 else if (d1->type == SDB_TYPE_REGEX) {
356 CMP_NULL(d1->data.re.raw, d2->data.re.raw);
357 return strcmp(d1->data.re.raw, d2->data.re.raw);
358 }
359 return -1;
360 } /* sdb_data_cmp */
362 int
363 sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2)
364 {
365 char d1_str[sdb_data_strlen(d1) + 1];
366 char d2_str[sdb_data_strlen(d2) + 1];
368 if (sdb_data_isnull(d1))
369 d1 = NULL;
370 if (sdb_data_isnull(d2))
371 d2 = NULL;
373 CMP_NULL(d1, d2);
375 if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0)
376 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
377 if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0)
378 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
380 return strcasecmp(d1_str, d2_str);
381 #undef CMP_NULL
382 } /* sdb_data_strcmp */
384 _Bool
385 sdb_data_isnull(const sdb_data_t *datum)
386 {
387 if (! datum)
388 return 1;
389 if (datum->type == SDB_TYPE_NULL)
390 return 1;
391 if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string))
392 return 1;
393 if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
394 return 1;
395 if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw))
396 return 1;
397 return 0;
398 } /* sdb_data_isnull */
400 int
401 sdb_data_parse_op(const char *op)
402 {
403 if (! strcmp(op, "+"))
404 return SDB_DATA_ADD;
405 else if (! strcmp(op, "-"))
406 return SDB_DATA_SUB;
407 else if (! strcmp(op, "*"))
408 return SDB_DATA_MUL;
409 else if (! strcmp(op, "/"))
410 return SDB_DATA_DIV;
411 else if (! strcmp(op, "%"))
412 return SDB_DATA_MOD;
413 else if (! strcmp(op, "||"))
414 return SDB_DATA_CONCAT;
415 return -1;
416 } /* sdb_data_parse_op */
418 int
419 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
420 sdb_data_t *res)
421 {
422 if ((! d1) || (! d2) || (! res))
423 return -1;
424 if (sdb_data_isnull(d1) || sdb_data_isnull(d2)) {
425 *res = SDB_DATA_NULL;
426 return 0;
427 }
428 switch (op) {
429 case SDB_DATA_CONCAT:
430 return data_concat(d1, d2, res);
431 case SDB_DATA_ADD:
432 return data_lin(d1, 1, d2, res);
433 case SDB_DATA_SUB:
434 return data_lin(d1, -1, d2, res);
435 case SDB_DATA_MUL:
436 return data_mul(d1, d2, res);
437 case SDB_DATA_DIV:
438 return data_div(d1, d2, res, NULL);
439 case SDB_DATA_MOD:
440 return data_div(d1, d2, NULL, res);
441 }
442 return -1;
443 } /* sdb_data_expr_eval */
445 size_t
446 sdb_data_strlen(const sdb_data_t *datum)
447 {
448 if (! datum)
449 return 0;
451 if (datum->type == SDB_TYPE_INTEGER) {
452 /* log(64) */
453 return 20;
454 }
455 else if (datum->type == SDB_TYPE_DECIMAL) {
456 /* XXX: -d.dddddde+dd or -ddddd.dddddd */
457 return 42;
458 }
459 else if (datum->type == SDB_TYPE_STRING) {
460 if (! datum->data.string)
461 return 8; /* "<NULL>" */
462 /* in the worst case, each character needs to be escaped */
463 return 2 * strlen(datum->data.string) + 2;
464 }
465 else if (datum->type == SDB_TYPE_DATETIME) {
466 /* "YYYY-MM-DD HH:MM:SS +zzzz" */
467 return 27;
468 }
469 else if (datum->type == SDB_TYPE_BINARY) {
470 if (! datum->data.binary.datum)
471 return 8; /* "<NULL>" */
472 /* "\xNN" */
473 return 4 * datum->data.binary.length + 2;
474 }
475 else if (datum->type == SDB_TYPE_REGEX) {
476 if (! datum->data.re.raw)
477 return 8; /* "<NULL>" */
478 /* "/.../" */
479 return strlen(datum->data.re.raw) + 4;
480 }
481 return 0;
482 } /* sdb_data_strlen */
484 int
485 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
486 {
487 char tmp[sdb_data_strlen(datum) + 1];
488 char *data = NULL;
489 int ret = -1;
491 size_t i, pos;
493 if ((! datum) || (! buf))
494 return -1;
496 if (datum->type == SDB_TYPE_INTEGER) {
497 ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
498 }
499 else if (datum->type == SDB_TYPE_DECIMAL) {
500 ret = snprintf(buf, buflen, "%g", datum->data.decimal);
501 }
502 else if (datum->type == SDB_TYPE_STRING) {
503 if (! datum->data.string)
504 data = "<NULL>";
505 else {
506 pos = 0;
507 for (i = 0; i < strlen(datum->data.string); ++i) {
508 char byte = datum->data.string[i];
510 if ((byte == '\\') || (byte == '"')) {
511 tmp[pos] = '\\';
512 ++pos;
513 }
514 tmp[pos] = byte;
515 ++pos;
516 }
517 tmp[pos] = '\0';
518 data = tmp;
519 }
520 }
521 else if (datum->type == SDB_TYPE_DATETIME) {
522 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
523 datum->data.datetime))
524 return -1;
525 tmp[sizeof(tmp) - 1] = '\0';
526 data = tmp;
527 }
528 else if (datum->type == SDB_TYPE_BINARY) {
529 pos = 0;
530 for (i = 0; i < datum->data.binary.length; ++i) {
531 int byte = (int)datum->data.binary.datum[i];
532 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
533 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
535 tmp[pos] = '\\';
536 tmp[pos + 1] = 'x';
537 pos += 2;
539 if (byte > 0xf) {
540 tmp[pos] = hex[byte >> 4];
541 ++pos;
542 }
543 tmp[pos] = hex[byte & 0xf];
544 ++pos;
545 }
546 if (datum->data.binary.datum) {
547 tmp[pos] = '\0';
548 data = tmp;
549 }
550 else
551 data = "<NULL>";
552 }
553 else if (datum->type == SDB_TYPE_REGEX) {
554 if (! datum->data.re.raw)
555 data = "<NULL>";
556 else {
557 snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
558 data = tmp;
559 }
560 }
562 if (data) {
563 if (quoted == SDB_UNQUOTED)
564 ret = snprintf(buf, buflen, "%s", data);
565 else if (quoted == SDB_SINGLE_QUOTED)
566 ret = snprintf(buf, buflen, "'%s'", data);
567 else
568 ret = snprintf(buf, buflen, "\"%s\"", data);
569 }
570 buf[buflen - 1] = '\0';
571 return ret;
572 } /* sdb_data_format */
574 int
575 sdb_data_parse(char *str, int type, sdb_data_t *data)
576 {
577 sdb_data_t tmp;
579 char *endptr = NULL;
581 errno = 0;
582 if (type == SDB_TYPE_INTEGER) {
583 tmp.data.integer = strtoll(str, &endptr, 0);
584 }
585 else if (type == SDB_TYPE_DECIMAL) {
586 tmp.data.decimal = strtod(str, &endptr);
587 }
588 else if (type == SDB_TYPE_STRING) {
589 tmp.data.string = str;
590 }
591 else if (type == SDB_TYPE_DATETIME) {
592 double datetime = strtod(str, &endptr);
593 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
594 }
595 else if (type == SDB_TYPE_BINARY) {
596 /* we don't support any binary information containing 0-bytes */
597 tmp.data.binary.length = strlen(str);
598 tmp.data.binary.datum = (unsigned char *)str;
599 }
600 else if (type == SDB_TYPE_REGEX) {
601 tmp.data.re.raw = strdup(str);
602 if (! tmp.data.re.raw)
603 return -1;
604 if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
605 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
606 sdb_log(SDB_LOG_ERR, "core: Failed to compile regular "
607 "expression '%s'", tmp.data.re.raw);
608 free(tmp.data.re.raw);
609 return -1;
610 }
611 if (! data) {
612 tmp.type = SDB_TYPE_REGEX;
613 sdb_data_free_datum(&tmp);
614 }
615 }
616 else {
617 errno = EINVAL;
618 return -1;
619 }
621 if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
622 || (type == SDB_TYPE_DATETIME)) {
623 if (errno || (str == endptr)) {
624 char errbuf[1024];
625 sdb_log(SDB_LOG_ERR, "core: Failed to parse string "
626 "'%s' as numeric value (type %i): %s", str, type,
627 sdb_strerror(errno, errbuf, sizeof(errbuf)));
628 return -1;
629 }
630 else if (endptr && (*endptr != '\0'))
631 sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after "
632 "number while parsing numeric value (type %i): %s.",
633 type, endptr);
634 }
636 if (data) {
637 *data = tmp;
638 data->type = type;
639 }
640 return 0;
641 } /* sdb_data_parse */
643 size_t
644 sdb_data_sizeof(int type)
645 {
646 sdb_data_t v;
647 if (type == SDB_TYPE_INTEGER)
648 return sizeof(v.data.integer);
649 else if (type == SDB_TYPE_DECIMAL)
650 return sizeof(v.data.decimal);
651 else if (type == SDB_TYPE_STRING)
652 return sizeof(v.data.string);
653 else if (type == SDB_TYPE_DATETIME)
654 return sizeof(v.data.datetime);
655 else if (type == SDB_TYPE_BINARY)
656 return sizeof(v.data.binary);
657 else if (type == SDB_TYPE_REGEX)
658 return sizeof(v.data.re);
659 return 0;
660 } /* sdb_data_sizeof */
662 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */