Code

data: Let CONCAT return NULL if any of its operands is NULL.
[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;
208                 if (len1)
209                         memcpy(new, s1, len1);
210                 if (len2)
211                         memcpy(new + len1, s2, len2);
212                 new[len1 + len2] = '\0';
213         }
214         else {
215                 len1 = len2 = 0;
216                 new = NULL;
217         }
219         res->type = d1->type;
220         if (res->type == SDB_TYPE_STRING) {
221                 res->data.string = (char *)new;
222         }
223         else {
224                 res->data.binary.datum = new;
225                 res->data.binary.length = len1 + len2;
226         }
227         return 0;
228 } /* data_concat */
230 /*
231  * public API
232  */
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_STRING) && (! datum->data.string))
395                 return 1;
396         if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
397                 return 1;
398         if ((datum->type == SDB_TYPE_REGEX) && (! datum->data.re.raw))
399                 return 1;
400         return 0;
401 } /* sdb_data_isnull */
403 int
404 sdb_data_parse_op(const char *op)
406         if (! strcmp(op, "+"))
407                 return SDB_DATA_ADD;
408         else if (! strcmp(op, "-"))
409                 return SDB_DATA_SUB;
410         else if (! strcmp(op, "*"))
411                 return SDB_DATA_MUL;
412         else if (! strcmp(op, "/"))
413                 return SDB_DATA_DIV;
414         else if (! strcmp(op, "%"))
415                 return SDB_DATA_MOD;
416         else if (! strcmp(op, "||"))
417                 return SDB_DATA_CONCAT;
418         return -1;
419 } /* sdb_data_parse_op */
421 int
422 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
423                 sdb_data_t *res)
425         if ((! d1) || (! d2) || (! res))
426                 return -1;
427         switch (op) {
428                 case SDB_DATA_CONCAT:
429                         return data_concat(d1, d2, res);
430                 case SDB_DATA_ADD:
431                         return data_lin(d1, 1, d2, res);
432                 case SDB_DATA_SUB:
433                         return data_lin(d1, -1, d2, res);
434                 case SDB_DATA_MUL:
435                         return data_mul(d1, d2, res);
436                 case SDB_DATA_DIV:
437                         return data_div(d1, d2, res, NULL);
438                 case SDB_DATA_MOD:
439                         return data_div(d1, d2, NULL, res);
440         }
441         return -1;
442 } /* sdb_data_expr_eval */
444 size_t
445 sdb_data_strlen(const sdb_data_t *datum)
447         if (! datum)
448                 return 0;
450         switch (datum->type) {
451                 case SDB_TYPE_INTEGER:
452                         /* log(64) */
453                         return 20;
454                 case SDB_TYPE_DECIMAL:
455                         /* XXX: -d.dddddde+dd or -ddddd.dddddd */
456                         return 42;
457                 case SDB_TYPE_STRING:
458                         if (! datum->data.string)
459                                 return 8; /* "<NULL>" */
460                         /* in the worst case, each character needs to be escaped */
461                         return 2 * strlen(datum->data.string) + 2;
462                 case SDB_TYPE_DATETIME:
463                         /* "YYYY-MM-DD HH:MM:SS +zzzz" */
464                         return 27;
465                 case SDB_TYPE_BINARY:
466                         if (! datum->data.binary.datum)
467                                 return 8; /* "<NULL>" */
468                         /* "\xNN" */
469                         return 4 * datum->data.binary.length + 2;
470                 case SDB_TYPE_REGEX:
471                         if (! datum->data.re.raw)
472                                 return 8; /* "<NULL>" */
473                         /* "/.../" */
474                         return strlen(datum->data.re.raw) + 4;
475         }
476         return 0;
477 } /* sdb_data_strlen */
479 int
480 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
482         char tmp[sdb_data_strlen(datum) + 1];
483         char *data = NULL;
484         int ret = -1;
486         size_t i, pos;
488         if ((! datum) || (! buf))
489                 return -1;
491         switch (datum->type) {
492                 case SDB_TYPE_INTEGER:
493                         ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
494                         break;
495                 case SDB_TYPE_DECIMAL:
496                         ret = snprintf(buf, buflen, "%g", datum->data.decimal);
497                         break;
498                 case SDB_TYPE_STRING:
499                         if (! datum->data.string)
500                                 data = "<NULL>";
501                         else {
502                                 pos = 0;
503                                 for (i = 0; i < strlen(datum->data.string); ++i) {
504                                         char byte = datum->data.string[i];
506                                         if ((byte == '\\') || (byte == '"')) {
507                                                 tmp[pos] = '\\';
508                                                 ++pos;
509                                         }
510                                         tmp[pos] = byte;
511                                         ++pos;
512                                 }
513                                 tmp[pos] = '\0';
514                                 data = tmp;
515                         }
516                         break;
517                 case SDB_TYPE_DATETIME:
518                         if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
519                                                 datum->data.datetime))
520                                 return -1;
521                         tmp[sizeof(tmp) - 1] = '\0';
522                         data = tmp;
523                         break;
524                 case SDB_TYPE_BINARY:
525                         pos = 0;
526                         for (i = 0; i < datum->data.binary.length; ++i) {
527                                 int byte = (int)datum->data.binary.datum[i];
528                                 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
529                                         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
531                                 tmp[pos] = '\\';
532                                 tmp[pos + 1] = 'x';
533                                 pos += 2;
535                                 if (byte > 0xf) {
536                                         tmp[pos] = hex[byte >> 4];
537                                         ++pos;
538                                 }
539                                 tmp[pos] = hex[byte & 0xf];
540                                 ++pos;
541                         }
542                         if (datum->data.binary.datum) {
543                                 tmp[pos] = '\0';
544                                 data = tmp;
545                         }
546                         else
547                                 data = "<NULL>";
548                         break;
549                 case SDB_TYPE_REGEX:
550                         if (! datum->data.re.raw)
551                                 data = "<NULL>";
552                         else {
553                                 snprintf(tmp, sizeof(tmp), "/%s/", datum->data.re.raw);
554                                 data = tmp;
555                         }
556                         break;
557         }
559         if (data) {
560                 if (quoted == SDB_UNQUOTED)
561                         ret = snprintf(buf, buflen, "%s", data);
562                 else if (quoted == SDB_SINGLE_QUOTED)
563                         ret = snprintf(buf, buflen, "'%s'", data);
564                 else
565                         ret = snprintf(buf, buflen, "\"%s\"", data);
566         }
567         buf[buflen - 1] = '\0';
568         return ret;
569 } /* sdb_data_format */
571 int
572 sdb_data_parse(char *str, int type, sdb_data_t *data)
574         sdb_data_t tmp;
576         char *endptr = NULL;
578         errno = 0;
579         switch (type) {
580                 case SDB_TYPE_INTEGER:
581                         tmp.data.integer = strtoll(str, &endptr, 0);
582                         break;
583                 case SDB_TYPE_DECIMAL:
584                         tmp.data.decimal = strtod(str, &endptr);
585                         break;
586                 case SDB_TYPE_STRING:
587                         tmp.data.string = str;
588                         break;
589                 case SDB_TYPE_DATETIME:
590                         {
591                                 double datetime = strtod(str, &endptr);
592                                 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
593                         }
594                         break;
595                 case 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                         break;
600                 case 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                         break;
616                 default:
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 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */