Code

data: Let sdb_data_parse() support inline casts from string to other types.
[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 <errno.h>
39 #include <inttypes.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
45 #include <math.h>
47 /*
48  * private helper functions
49  */
51 /* Calculate the linear function 'd1 + n * d2'. */
52 static int
53 data_lin(const sdb_data_t *d1, int n, const sdb_data_t *d2, sdb_data_t *res)
54 {
55         if (d1->type != d2->type)
56                 return -1;
58         if (d1->type == SDB_TYPE_INTEGER)
59                 res->data.integer = d1->data.integer + (int64_t)n * d2->data.integer;
60         else if (d1->type == SDB_TYPE_DECIMAL)
61                 res->data.decimal = d1->data.decimal + (double)n * d2->data.decimal;
62         else if (d1->type ==  SDB_TYPE_DATETIME)
63                 res->data.datetime = d1->data.datetime + (sdb_time_t)n * d2->data.datetime;
64         else
65                 return -1;
66         res->type = d1->type;
67         return 0;
68 } /* data_lin */
70 /* Multiply d1 with d2. */
71 static int
72 data_mul(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
73 {
74         if (d1->type == SDB_TYPE_INTEGER) {
75                 if (d2->type == SDB_TYPE_INTEGER)
76                         res->data.integer = d1->data.integer * d2->data.integer;
77                 else if (d2->type == SDB_TYPE_DATETIME) {
78                         res->data.datetime = (sdb_time_t)d1->data.integer
79                                 * d2->data.datetime;
80                         res->type = SDB_TYPE_DATETIME;
81                         return 0;
82                 }
83                 else
84                         return -1;
85         }
86         else if (d1->type == SDB_TYPE_DECIMAL) {
87                 if (d2->type == SDB_TYPE_DECIMAL)
88                         res->data.decimal = d1->data.decimal * d2->data.decimal;
89                 else if (d2->type == SDB_TYPE_DATETIME) {
90                         res->data.datetime = (sdb_time_t)(d1->data.decimal
91                                         * (double)d2->data.datetime);
92                         res->type = SDB_TYPE_DATETIME;
93                         return 0;
94                 }
95                 else
96                         return -1;
97         }
98         else if (d1->type == SDB_TYPE_DATETIME) {
99                 if (d2->type == SDB_TYPE_DATETIME)
100                         res->data.datetime = d1->data.datetime
101                                 * d2->data.datetime;
102                 else if (d2->type == SDB_TYPE_INTEGER)
103                         res->data.datetime = d1->data.datetime
104                                 * (sdb_time_t)d2->data.integer;
105                 else if (d2->type == SDB_TYPE_DECIMAL)
106                         res->data.datetime = (sdb_time_t)((double)d1->data.datetime
107                                         * d2->data.decimal);
108                 else
109                         return -1;
110         }
111         else
112                 return -1;
114         res->type = d1->type;
115         return 0;
116 } /* data_mul */
118 /* Device d1 by d2 and return the result and the remainder. */
119 static int
120 data_div(const sdb_data_t *d1, const sdb_data_t *d2,
121                 sdb_data_t *res, sdb_data_t *rem)
123         if (d1->type == SDB_TYPE_INTEGER) {
124                 if (d2->type != SDB_TYPE_INTEGER)
125                         return -1;
126                 if (res)
127                         res->data.integer = d1->data.integer / d2->data.integer;
128                 if (rem)
129                         rem->data.integer = d1->data.integer % d2->data.integer;
130         }
131         else if (d1->type == SDB_TYPE_DECIMAL) {
132                 if (d2->type != SDB_TYPE_DECIMAL)
133                         return -1;
134                 if (res)
135                         res->data.decimal = d1->data.decimal / d2->data.decimal;
136                 if (rem)
137                         rem->data.decimal = fmod(d1->data.decimal, d2->data.decimal);
138         }
139         else if (d1->type == SDB_TYPE_DATETIME) {
140                 if (d2->type == SDB_TYPE_DECIMAL) {
141                         if (res)
142                                 res->data.datetime = (sdb_time_t)((double)d1->data.datetime
143                                                 / d2->data.decimal);
144                         if (rem) {
145                                 double tmp = fmod((double)d1->data.datetime, d2->data.decimal);
146                                 rem->data.datetime = (sdb_time_t)tmp;
147                         }
148                 }
149                 else {
150                         sdb_time_t a, b;
151                         if (d2->type == SDB_TYPE_DATETIME) {
152                                 a = d1->data.datetime;
153                                 b = d2->data.datetime;
154                         }
155                         else if (d2->type == SDB_TYPE_INTEGER) {
156                                 a = d1->data.datetime;
157                                 b = (sdb_time_t)d2->data.integer;
158                         }
159                         else
160                                 return -1;
161                         if (res)
162                                 res->data.datetime = a / b;
163                         if (rem)
164                                 rem->data.datetime = a % b;
165                 }
166         }
167         else
168                 return -1;
170         if (res)
171                 res->type = d1->type;
172         if (rem)
173                 rem->type = d1->type;
174         return 0;
175 } /* data_div */
177 /* Concatenate d1 and d2. */
178 static int
179 data_concat(const sdb_data_t *d1, const sdb_data_t *d2, sdb_data_t *res)
181         unsigned char *new;
182         unsigned char *s1, *s2;
183         size_t len1, len2;
185         if (d1->type != d2->type)
186                 return -1;
188         if (d1->type == SDB_TYPE_STRING) {
189                 s1 = (unsigned char *)d1->data.string;
190                 s2 = (unsigned char *)d2->data.string;
191                 len1 = s1 ? strlen((char *)s1) : 0;
192                 len2 = s2 ? strlen((char *)s2) : 0;
193         }
194         else if (d1->type == SDB_TYPE_BINARY) {
195                 s1 = d1->data.binary.datum;
196                 s2 = d2->data.binary.datum;
197                 len1 = d1->data.binary.length;
198                 len2 = d2->data.binary.length;
199         }
200         else
201                 return -1;
203         if (s1 || s2) {
204                 new = malloc(len1 + len2 + 1);
205                 if (! new)
206                         return -1;
207         }
208         else
209                 new = NULL;
211         if (len1)
212                 memcpy(new, s1, len1);
213         if (len2)
214                 memcpy(new + len1, s2, len2);
215         if (new)
216                 new[len1 + len2] = '\0';
218         res->type = d1->type;
219         if (res->type == SDB_TYPE_STRING) {
220                 res->data.string = (char *)new;
221         }
222         else {
223                 res->data.binary.datum = new;
224                 res->data.binary.length = len1 + len2;
225         }
226         return 0;
227 } /* data_concat */
229 /*
230  * public API
231  */
233 int
234 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
236         sdb_data_t tmp;
238         if ((! dst) || (! src))
239                 return -1;
241         tmp = *src;
242         switch (src->type) {
243                 case 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                         break;
250                 case 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                         break;
259                 case 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                         break;
275         }
277         sdb_data_free_datum(dst);
278         *dst = tmp;
279         return 0;
280 } /* sdb_data_copy */
282 void
283 sdb_data_free_datum(sdb_data_t *datum)
285         if (! datum)
286                 return;
288         switch (datum->type) {
289                 case SDB_TYPE_STRING:
290                         if (datum->data.string)
291                                 free(datum->data.string);
292                         datum->data.string = NULL;
293                         break;
294                 case SDB_TYPE_BINARY:
295                         if (datum->data.binary.datum)
296                                 free(datum->data.binary.datum);
297                         datum->data.binary.datum = NULL;
298                         datum->data.binary.length = 0;
299                         break;
300                 case SDB_TYPE_REGEX:
301                         if (datum->data.re.raw) {
302                                 free(datum->data.re.raw);
303                                 regfree(&datum->data.re.regex);
304                         }
305                         datum->data.re.raw = NULL;
306                         memset(&datum->data.re.regex, 0, sizeof(datum->data.re.regex));
307                         break;
308         }
309 } /* sdb_data_free_datum */
311 int
312 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
314 #define CMP_NULL(a, b) \
315         do { \
316                 if (!(a) && !(b)) return 0; \
317                 if (!(a)) return -1; \
318                 if (!(b)) return 1; \
319         } while (0)
321         CMP_NULL(d1, d2);
323         if (d1->type != d2->type)
324                 return SDB_CMP(d1->type, d2->type);
326         switch (d1->type) {
327                 case SDB_TYPE_INTEGER:
328                         return SDB_CMP(d1->data.integer, d2->data.integer);
329                 case SDB_TYPE_DECIMAL:
330                         return SDB_CMP(d1->data.decimal, d2->data.decimal);
331                 case SDB_TYPE_STRING:
332                         CMP_NULL(d1->data.string, d2->data.string);
333                         return strcasecmp(d1->data.string, d2->data.string);
334                 case SDB_TYPE_DATETIME:
335                         return SDB_CMP(d1->data.datetime, d2->data.datetime);
336                 case SDB_TYPE_BINARY:
337                 {
338                         int diff;
340                         CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
342                         /* on a common prefix, the shorter datum sorts less */
343                         if (d1->data.binary.length < d2->data.binary.length) {
344                                 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
345                                                 d1->data.binary.length);
346                                 diff = diff ? diff : -1;
347                         }
348                         else if (d1->data.binary.length > d2->data.binary.length) {
349                                 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
350                                                 d2->data.binary.length);
351                                 diff = diff ? diff : 1;
352                         }
353                         else
354                                 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
355                                                 d1->data.binary.length);
357                         return diff;
358                 }
359                 case SDB_TYPE_REGEX:
360                         CMP_NULL(d1->data.re.raw, d2->data.re.raw);
361                         return strcmp(d1->data.re.raw, d2->data.re.raw);
362         }
363         return -1;
364 } /* sdb_data_cmp */
366 int
367 sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2)
369         char d1_str[sdb_data_strlen(d1) + 1];
370         char d2_str[sdb_data_strlen(d2) + 1];
372         if (sdb_data_isnull(d1))
373                 d1 = NULL;
374         if (sdb_data_isnull(d2))
375                 d2 = NULL;
377         CMP_NULL(d1, d2);
379         if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0)
380                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
381         if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0)
382                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
384         return strcasecmp(d1_str, d2_str);
385 #undef CMP_NULL
386 } /* sdb_data_strcmp */
388 _Bool
389 sdb_data_isnull(const sdb_data_t *datum)
391         if (! datum)
392                 return 1;
393         if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string))
394                 return 1;
395         if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
396                 return 1;
397         if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw))
398                 return 1;
399         return 0;
400 } /* sdb_data_isnull */
402 int
403 sdb_data_parse_op(const char *op)
405         if (! strcmp(op, "+"))
406                 return SDB_DATA_ADD;
407         else if (! strcmp(op, "-"))
408                 return SDB_DATA_SUB;
409         else if (! strcmp(op, "*"))
410                 return SDB_DATA_MUL;
411         else if (! strcmp(op, "/"))
412                 return SDB_DATA_DIV;
413         else if (! strcmp(op, "%"))
414                 return SDB_DATA_MOD;
415         else if (! strcmp(op, "||"))
416                 return SDB_DATA_CONCAT;
417         return -1;
418 } /* sdb_data_parse_op */
420 int
421 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
422                 sdb_data_t *res)
424         if ((! d1) || (! d2) || (! res))
425                 return -1;
426         switch (op) {
427                 case SDB_DATA_CONCAT:
428                         return data_concat(d1, d2, res);
429                 case SDB_DATA_ADD:
430                         return data_lin(d1, 1, d2, res);
431                 case SDB_DATA_SUB:
432                         return data_lin(d1, -1, d2, res);
433                 case SDB_DATA_MUL:
434                         return data_mul(d1, d2, res);
435                 case SDB_DATA_DIV:
436                         return data_div(d1, d2, res, NULL);
437                 case SDB_DATA_MOD:
438                         return data_div(d1, d2, NULL, res);
439         }
440         return -1;
441 } /* sdb_data_expr_eval */
443 size_t
444 sdb_data_strlen(const sdb_data_t *datum)
446         if (! datum)
447                 return 0;
449         switch (datum->type) {
450                 case SDB_TYPE_INTEGER:
451                         /* log(64) */
452                         return 20;
453                 case SDB_TYPE_DECIMAL:
454                         /* XXX: -d.dddddde+dd or -ddddd.dddddd */
455                         return 42;
456                 case SDB_TYPE_STRING:
457                         if (! datum->data.string)
458                                 return 8; /* "<NULL>" */
459                         /* in the worst case, each character needs to be escaped */
460                         return 2 * strlen(datum->data.string) + 2;
461                 case SDB_TYPE_DATETIME:
462                         /* "YYYY-MM-DD HH:MM:SS +zzzz" */
463                         return 27;
464                 case SDB_TYPE_BINARY:
465                         if (! datum->data.binary.datum)
466                                 return 8; /* "<NULL>" */
467                         /* "\xNN" */
468                         return 4 * datum->data.binary.length + 2;
469                 case SDB_TYPE_REGEX:
470                         if (! datum->data.re.raw)
471                                 return 8; /* "<NULL>" */
472                         /* "/.../" */
473                         return strlen(datum->data.re.raw) + 4;
474         }
475         return 0;
476 } /* sdb_data_strlen */
478 int
479 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
481         char tmp[sdb_data_strlen(datum) + 1];
482         char *data = NULL;
483         int ret = -1;
485         size_t i, pos;
487         if ((! datum) || (! buf))
488                 return -1;
490         switch (datum->type) {
491                 case SDB_TYPE_INTEGER:
492                         ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
493                         break;
494                 case SDB_TYPE_DECIMAL:
495                         ret = snprintf(buf, buflen, "%g", datum->data.decimal);
496                         break;
497                 case SDB_TYPE_STRING:
498                         if (! datum->data.string)
499                                 data = "<NULL>";
500                         else {
501                                 pos = 0;
502                                 for (i = 0; i < strlen(datum->data.string); ++i) {
503                                         char byte = datum->data.string[i];
505                                         if ((byte == '\\') || (byte == '"')) {
506                                                 tmp[pos] = '\\';
507                                                 ++pos;
508                                         }
509                                         tmp[pos] = byte;
510                                         ++pos;
511                                 }
512                                 tmp[pos] = '\0';
513                                 data = tmp;
514                         }
515                         break;
516                 case SDB_TYPE_DATETIME:
517                         if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
518                                                 datum->data.datetime))
519                                 return -1;
520                         tmp[sizeof(tmp) - 1] = '\0';
521                         data = tmp;
522                         break;
523                 case SDB_TYPE_BINARY:
524                         pos = 0;
525                         for (i = 0; i < datum->data.binary.length; ++i) {
526                                 int byte = (int)datum->data.binary.datum[i];
527                                 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
528                                         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
530                                 tmp[pos] = '\\';
531                                 tmp[pos + 1] = 'x';
532                                 pos += 2;
534                                 if (byte > 0xf) {
535                                         tmp[pos] = hex[byte >> 4];
536                                         ++pos;
537                                 }
538                                 tmp[pos] = hex[byte & 0xf];
539                                 ++pos;
540                         }
541                         if (datum->data.binary.datum) {
542                                 tmp[pos] = '\0';
543                                 data = tmp;
544                         }
545                         else
546                                 data = "<NULL>";
547                         break;
548                 case SDB_TYPE_REGEX:
549                         if (! datum->data.re.raw)
550                                 data = "<NULL>";
551                         else {
552                                 snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
553                                 data = tmp;
554                         }
555                         break;
556         }
558         if (data) {
559                 if (quoted == SDB_UNQUOTED)
560                         ret = snprintf(buf, buflen, "%s", data);
561                 else if (quoted == SDB_SINGLE_QUOTED)
562                         ret = snprintf(buf, buflen, "'%s'", data);
563                 else
564                         ret = snprintf(buf, buflen, "\"%s\"", data);
565         }
566         buf[buflen - 1] = '\0';
567         return ret;
568 } /* sdb_data_format */
570 int
571 sdb_data_parse(char *str, int type, sdb_data_t *data)
573         sdb_data_t tmp;
575         char *endptr = NULL;
577         errno = 0;
578         switch (type) {
579                 case SDB_TYPE_INTEGER:
580                         tmp.data.integer = strtoll(str, &endptr, 0);
581                         break;
582                 case SDB_TYPE_DECIMAL:
583                         tmp.data.decimal = strtod(str, &endptr);
584                         break;
585                 case SDB_TYPE_STRING:
586                         tmp.data.string = str;
587                         break;
588                 case SDB_TYPE_DATETIME:
589                         {
590                                 double datetime = strtod(str, &endptr);
591                                 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
592                         }
593                         break;
594                 case SDB_TYPE_BINARY:
595                         /* we don't support any binary information containing 0-bytes */
596                         tmp.data.binary.length = strlen(str);
597                         tmp.data.binary.datum = (unsigned char *)str;
598                         break;
599                 case SDB_TYPE_REGEX:
600                         tmp.data.re.raw = strdup(str);
601                         if (! tmp.data.re.raw)
602                                 return -1;
603                         if (regcomp(&tmp.data.re.regex, tmp.data.re.raw,
604                                                 REG_EXTENDED | REG_ICASE | REG_NOSUB)) {
605                                 free(tmp.data.re.raw);
606                                 sdb_log(SDB_LOG_ERR, "core: Failed to compile regular "
607                                                 "expression '%s'", tmp.data.re.raw);
608                                 return -1;
609                         }
610                         if (! data) {
611                                 tmp.type = SDB_TYPE_REGEX;
612                                 sdb_data_free_datum(&tmp);
613                         }
614                         break;
615                 default:
616                         errno = EINVAL;
617                         return -1;
618         }
620         if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
621                         || (type == SDB_TYPE_DATETIME)) {
622                 if (errno || (str == endptr)) {
623                         char errbuf[1024];
624                         sdb_log(SDB_LOG_ERR, "core: Failed to parse string "
625                                         "'%s' as numeric value (type %i): %s", str, type,
626                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
627                         return -1;
628                 }
629                 else if (endptr && (*endptr != '\0'))
630                         sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after "
631                                         "number while parsing numeric value (type %i): %s.",
632                                         type, endptr);
633         }
635         if (data) {
636                 *data = tmp;
637                 data->type = type;
638         }
639         return 0;
640 } /* sdb_data_parse */
642 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */