Code

data: Added sdb_data_sizeof().
[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  * 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)
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)
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)
237         sdb_data_t tmp;
239         if ((! dst) || (! src))
240                 return -1;
242         tmp = *src;
243         switch (src->type) {
244                 case SDB_TYPE_STRING:
245                         if (src->data.string) {
246                                 tmp.data.string = strdup(src->data.string);
247                                 if (! tmp.data.string)
248                                         return -1;
249                         }
250                         break;
251                 case SDB_TYPE_BINARY:
252                         if (src->data.binary.datum) {
253                                 tmp.data.binary.datum = malloc(src->data.binary.length);
254                                 if (! tmp.data.binary.datum)
255                                         return -1;
256                                 memcpy(tmp.data.binary.datum, src->data.binary.datum,
257                                                 src->data.binary.length);
258                         }
259                         break;
260                 case SDB_TYPE_REGEX:
261                         if (src->data.re.raw) {
262                                 tmp.data.re.raw = strdup(src->data.re.raw);
263                                 if (! tmp.data.re.raw)
264                                         return -1;
265                                 /* we need to recompile because the regex might point to
266                                  * dynamically allocated memory */
267                                 if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
268                                                         REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
269                                         free(tmp.data.re.raw);
270                                         return -1;
271                                 }
272                         }
273                         else
274                                 memset(&tmp.data.re.regex, 0, sizeof(tmp.data.re.regex));
275                         break;
276         }
278         sdb_data_free_datum(dst);
279         *dst = tmp;
280         return 0;
281 } /* sdb_data_copy */
283 void
284 sdb_data_free_datum(sdb_data_t *datum)
286         if (! datum)
287                 return;
289         switch (datum->type) {
290                 case SDB_TYPE_STRING:
291                         if (datum->data.string)
292                                 free(datum->data.string);
293                         datum->data.string = NULL;
294                         break;
295                 case SDB_TYPE_BINARY:
296                         if (datum->data.binary.datum)
297                                 free(datum->data.binary.datum);
298                         datum->data.binary.datum = NULL;
299                         datum->data.binary.length = 0;
300                         break;
301                 case SDB_TYPE_REGEX:
302                         if (datum->data.re.raw) {
303                                 free(datum->data.re.raw);
304                                 regfree(&datum->data.re.regex);
305                         }
306                         datum->data.re.raw = NULL;
307                         memset(&datum->data.re.regex, 0, sizeof(datum->data.re.regex));
308                         break;
309         }
310 } /* sdb_data_free_datum */
312 int
313 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
315 #define CMP_NULL(a, b) \
316         do { \
317                 if (!(a) && !(b)) return 0; \
318                 if (!(a)) return -1; \
319                 if (!(b)) return 1; \
320         } while (0)
322         CMP_NULL(d1, d2);
324         if (d1->type != d2->type)
325                 return SDB_CMP(d1->type, d2->type);
327         switch (d1->type) {
328                 case SDB_TYPE_INTEGER:
329                         return SDB_CMP(d1->data.integer, d2->data.integer);
330                 case SDB_TYPE_DECIMAL:
331                         return SDB_CMP(d1->data.decimal, d2->data.decimal);
332                 case SDB_TYPE_STRING:
333                         CMP_NULL(d1->data.string, d2->data.string);
334                         return strcasecmp(d1->data.string, d2->data.string);
335                 case SDB_TYPE_DATETIME:
336                         return SDB_CMP(d1->data.datetime, d2->data.datetime);
337                 case SDB_TYPE_BINARY:
338                 {
339                         int diff;
341                         CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
343                         /* on a common prefix, the shorter datum sorts less */
344                         if (d1->data.binary.length < d2->data.binary.length) {
345                                 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
346                                                 d1->data.binary.length);
347                                 diff = diff ? diff : -1;
348                         }
349                         else if (d1->data.binary.length > d2->data.binary.length) {
350                                 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
351                                                 d2->data.binary.length);
352                                 diff = diff ? diff : 1;
353                         }
354                         else
355                                 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
356                                                 d1->data.binary.length);
358                         return diff;
359                 }
360                 case SDB_TYPE_REGEX:
361                         CMP_NULL(d1->data.re.raw, d2->data.re.raw);
362                         return strcmp(d1->data.re.raw, d2->data.re.raw);
363         }
364         return -1;
365 } /* sdb_data_cmp */
367 int
368 sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2)
370         char d1_str[sdb_data_strlen(d1) + 1];
371         char d2_str[sdb_data_strlen(d2) + 1];
373         if (sdb_data_isnull(d1))
374                 d1 = NULL;
375         if (sdb_data_isnull(d2))
376                 d2 = NULL;
378         CMP_NULL(d1, d2);
380         if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0)
381                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
382         if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0)
383                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
385         return strcasecmp(d1_str, d2_str);
386 #undef CMP_NULL
387 } /* sdb_data_strcmp */
389 _Bool
390 sdb_data_isnull(const sdb_data_t *datum)
392         if (! datum)
393                 return 1;
394         if (datum->type == SDB_TYPE_NULL)
395                 return 1;
396         if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string))
397                 return 1;
398         if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
399                 return 1;
400         if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw))
401                 return 1;
402         return 0;
403 } /* sdb_data_isnull */
405 int
406 sdb_data_parse_op(const char *op)
408         if (! strcmp(op, "+"))
409                 return SDB_DATA_ADD;
410         else if (! strcmp(op, "-"))
411                 return SDB_DATA_SUB;
412         else if (! strcmp(op, "*"))
413                 return SDB_DATA_MUL;
414         else if (! strcmp(op, "/"))
415                 return SDB_DATA_DIV;
416         else if (! strcmp(op, "%"))
417                 return SDB_DATA_MOD;
418         else if (! strcmp(op, "||"))
419                 return SDB_DATA_CONCAT;
420         return -1;
421 } /* sdb_data_parse_op */
423 int
424 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
425                 sdb_data_t *res)
427         if ((! d1) || (! d2) || (! res))
428                 return -1;
429         if (sdb_data_isnull(d1) || sdb_data_isnull(d2)) {
430                 *res = SDB_DATA_NULL;
431                 return 0;
432         }
433         switch (op) {
434                 case SDB_DATA_CONCAT:
435                         return data_concat(d1, d2, res);
436                 case SDB_DATA_ADD:
437                         return data_lin(d1, 1, d2, res);
438                 case SDB_DATA_SUB:
439                         return data_lin(d1, -1, d2, res);
440                 case SDB_DATA_MUL:
441                         return data_mul(d1, d2, res);
442                 case SDB_DATA_DIV:
443                         return data_div(d1, d2, res, NULL);
444                 case SDB_DATA_MOD:
445                         return data_div(d1, d2, NULL, res);
446         }
447         return -1;
448 } /* sdb_data_expr_eval */
450 size_t
451 sdb_data_strlen(const sdb_data_t *datum)
453         if (! datum)
454                 return 0;
456         switch (datum->type) {
457                 case SDB_TYPE_INTEGER:
458                         /* log(64) */
459                         return 20;
460                 case SDB_TYPE_DECIMAL:
461                         /* XXX: -d.dddddde+dd or -ddddd.dddddd */
462                         return 42;
463                 case SDB_TYPE_STRING:
464                         if (! datum->data.string)
465                                 return 8; /* "<NULL>" */
466                         /* in the worst case, each character needs to be escaped */
467                         return 2 * strlen(datum->data.string) + 2;
468                 case SDB_TYPE_DATETIME:
469                         /* "YYYY-MM-DD HH:MM:SS +zzzz" */
470                         return 27;
471                 case SDB_TYPE_BINARY:
472                         if (! datum->data.binary.datum)
473                                 return 8; /* "<NULL>" */
474                         /* "\xNN" */
475                         return 4 * datum->data.binary.length + 2;
476                 case SDB_TYPE_REGEX:
477                         if (! datum->data.re.raw)
478                                 return 8; /* "<NULL>" */
479                         /* "/.../" */
480                         return strlen(datum->data.re.raw) + 4;
481         }
482         return 0;
483 } /* sdb_data_strlen */
485 int
486 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
488         char tmp[sdb_data_strlen(datum) + 1];
489         char *data = NULL;
490         int ret = -1;
492         size_t i, pos;
494         if ((! datum) || (! buf))
495                 return -1;
497         switch (datum->type) {
498                 case SDB_TYPE_INTEGER:
499                         ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
500                         break;
501                 case SDB_TYPE_DECIMAL:
502                         ret = snprintf(buf, buflen, "%g", datum->data.decimal);
503                         break;
504                 case SDB_TYPE_STRING:
505                         if (! datum->data.string)
506                                 data = "<NULL>";
507                         else {
508                                 pos = 0;
509                                 for (i = 0; i < strlen(datum->data.string); ++i) {
510                                         char byte = datum->data.string[i];
512                                         if ((byte == '\\') || (byte == '"')) {
513                                                 tmp[pos] = '\\';
514                                                 ++pos;
515                                         }
516                                         tmp[pos] = byte;
517                                         ++pos;
518                                 }
519                                 tmp[pos] = '\0';
520                                 data = tmp;
521                         }
522                         break;
523                 case SDB_TYPE_DATETIME:
524                         if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
525                                                 datum->data.datetime))
526                                 return -1;
527                         tmp[sizeof(tmp) - 1] = '\0';
528                         data = tmp;
529                         break;
530                 case SDB_TYPE_BINARY:
531                         pos = 0;
532                         for (i = 0; i < datum->data.binary.length; ++i) {
533                                 int byte = (int)datum->data.binary.datum[i];
534                                 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
535                                         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
537                                 tmp[pos] = '\\';
538                                 tmp[pos + 1] = 'x';
539                                 pos += 2;
541                                 if (byte > 0xf) {
542                                         tmp[pos] = hex[byte >> 4];
543                                         ++pos;
544                                 }
545                                 tmp[pos] = hex[byte & 0xf];
546                                 ++pos;
547                         }
548                         if (datum->data.binary.datum) {
549                                 tmp[pos] = '\0';
550                                 data = tmp;
551                         }
552                         else
553                                 data = "<NULL>";
554                         break;
555                 case SDB_TYPE_REGEX:
556                         if (! datum->data.re.raw)
557                                 data = "<NULL>";
558                         else {
559                                 snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
560                                 data = tmp;
561                         }
562                         break;
563         }
565         if (data) {
566                 if (quoted == SDB_UNQUOTED)
567                         ret = snprintf(buf, buflen, "%s", data);
568                 else if (quoted == SDB_SINGLE_QUOTED)
569                         ret = snprintf(buf, buflen, "'%s'", data);
570                 else
571                         ret = snprintf(buf, buflen, "\"%s\"", data);
572         }
573         buf[buflen - 1] = '\0';
574         return ret;
575 } /* sdb_data_format */
577 int
578 sdb_data_parse(char *str, int type, sdb_data_t *data)
580         sdb_data_t tmp;
582         char *endptr = NULL;
584         errno = 0;
585         switch (type) {
586                 case SDB_TYPE_INTEGER:
587                         tmp.data.integer = strtoll(str, &endptr, 0);
588                         break;
589                 case SDB_TYPE_DECIMAL:
590                         tmp.data.decimal = strtod(str, &endptr);
591                         break;
592                 case SDB_TYPE_STRING:
593                         tmp.data.string = str;
594                         break;
595                 case SDB_TYPE_DATETIME:
596                         {
597                                 double datetime = strtod(str, &endptr);
598                                 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
599                         }
600                         break;
601                 case SDB_TYPE_BINARY:
602                         /* we don't support any binary information containing 0-bytes */
603                         tmp.data.binary.length = strlen(str);
604                         tmp.data.binary.datum = (unsigned char *)str;
605                         break;
606                 case SDB_TYPE_REGEX:
607                         tmp.data.re.raw = strdup(str);
608                         if (! tmp.data.re.raw)
609                                 return -1;
610                         if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
611                                                 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
612                                 sdb_log(SDB_LOG_ERR, "core: Failed to compile regular "
613                                                 "expression '%s'", tmp.data.re.raw);
614                                 free(tmp.data.re.raw);
615                                 return -1;
616                         }
617                         if (! data) {
618                                 tmp.type = SDB_TYPE_REGEX;
619                                 sdb_data_free_datum(&tmp);
620                         }
621                         break;
622                 default:
623                         errno = EINVAL;
624                         return -1;
625         }
627         if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
628                         || (type == SDB_TYPE_DATETIME)) {
629                 if (errno || (str == endptr)) {
630                         char errbuf[1024];
631                         sdb_log(SDB_LOG_ERR, "core: Failed to parse string "
632                                         "'%s' as numeric value (type %i): %s", str, type,
633                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
634                         return -1;
635                 }
636                 else if (endptr && (*endptr != '\0'))
637                         sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after "
638                                         "number while parsing numeric value (type %i): %s.",
639                                         type, endptr);
640         }
642         if (data) {
643                 *data = tmp;
644                 data->type = type;
645         }
646         return 0;
647 } /* sdb_data_parse */
649 size_t
650 sdb_data_sizeof(int type)
652         sdb_data_t v;
653         if (type == SDB_TYPE_INTEGER)
654                 return sizeof(v.data.integer);
655         else if (type == SDB_TYPE_DECIMAL)
656                 return sizeof(v.data.decimal);
657         else if (type == SDB_TYPE_STRING)
658                 return sizeof(v.data.string);
659         else if (type == SDB_TYPE_DATETIME)
660                 return sizeof(v.data.datetime);
661         else if (type == SDB_TYPE_BINARY)
662                 return sizeof(v.data.binary);
663         else if (type == SDB_TYPE_REGEX)
664                 return sizeof(v.data.re);
665         return 0;
666 } /* sdb_data_sizeof */
668 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */