Code

data: Fixed copying of NULL data.
[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         }
261         sdb_data_free_datum(dst);
262         *dst = tmp;
263         return 0;
264 } /* sdb_data_copy */
266 void
267 sdb_data_free_datum(sdb_data_t *datum)
269         if (! datum)
270                 return;
272         switch (datum->type) {
273                 case SDB_TYPE_STRING:
274                         if (datum->data.string)
275                                 free(datum->data.string);
276                         datum->data.string = NULL;
277                         break;
278                 case SDB_TYPE_BINARY:
279                         if (datum->data.binary.datum)
280                                 free(datum->data.binary.datum);
281                         datum->data.binary.datum = NULL;
282                         datum->data.binary.length = 0;
283                         break;
284         }
285 } /* sdb_data_free_datum */
287 int
288 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
290 #define CMP_NULL(a, b) \
291         do { \
292                 if (!(a) && !(b)) return 0; \
293                 if (!(a)) return -1; \
294                 if (!(b)) return 1; \
295         } while (0)
297         CMP_NULL(d1, d2);
299         if (d1->type != d2->type)
300                 return SDB_CMP(d1->type, d2->type);
302         switch (d1->type) {
303                 case SDB_TYPE_INTEGER:
304                         return SDB_CMP(d1->data.integer, d2->data.integer);
305                 case SDB_TYPE_DECIMAL:
306                         return SDB_CMP(d1->data.decimal, d2->data.decimal);
307                 case SDB_TYPE_STRING:
308                         CMP_NULL(d1->data.string, d2->data.string);
309                         return strcasecmp(d1->data.string, d2->data.string);
310                 case SDB_TYPE_DATETIME:
311                         return SDB_CMP(d1->data.datetime, d2->data.datetime);
312                 case SDB_TYPE_BINARY:
313                 {
314                         int diff;
316                         CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
318                         /* on a common prefix, the shorter datum sorts less */
319                         if (d1->data.binary.length < d2->data.binary.length) {
320                                 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
321                                                 d1->data.binary.length);
322                                 diff = diff ? diff : -1;
323                         }
324                         else if (d1->data.binary.length > d2->data.binary.length) {
325                                 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
326                                                 d2->data.binary.length);
327                                 diff = diff ? diff : 1;
328                         }
329                         else
330                                 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
331                                                 d1->data.binary.length);
333                         return diff;
334                 }
335         }
336         return -1;
337 } /* sdb_data_cmp */
339 int
340 sdb_data_strcmp(const sdb_data_t *d1, const sdb_data_t *d2)
342         char d1_str[sdb_data_strlen(d1) + 1];
343         char d2_str[sdb_data_strlen(d2) + 1];
345         if (sdb_data_isnull(d1))
346                 d1 = NULL;
347         if (sdb_data_isnull(d2))
348                 d2 = NULL;
350         CMP_NULL(d1, d2);
352         if (sdb_data_format(d1, d1_str, sizeof(d1_str), SDB_UNQUOTED) < 0)
353                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
354         if (sdb_data_format(d2, d2_str, sizeof(d2_str), SDB_UNQUOTED) < 0)
355                 return SDB_CMP(sizeof(d1_str), sizeof(d2_str));
357         return strcasecmp(d1_str, d2_str);
358 #undef CMP_NULL
359 } /* sdb_data_strcmp */
361 _Bool
362 sdb_data_isnull(const sdb_data_t *datum)
364         if (! datum)
365                 return 1;
366         if ((datum->type == SDB_TYPE_STRING) && (! datum->data.string))
367                 return 1;
368         if ((datum->type == SDB_TYPE_BINARY) && (! datum->data.binary.datum))
369                 return 1;
370         return 0;
371 } /* sdb_data_isnull */
373 int
374 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
375                 sdb_data_t *res)
377         if ((! d1) || (! d2) || (! res))
378                 return -1;
379         switch (op) {
380                 case SDB_DATA_CONCAT:
381                         return data_concat(d1, d2, res);
382                 case SDB_DATA_ADD:
383                         return data_lin(d1, 1, d2, res);
384                 case SDB_DATA_SUB:
385                         return data_lin(d1, -1, d2, res);
386                 case SDB_DATA_MUL:
387                         return data_mul(d1, d2, res);
388                 case SDB_DATA_DIV:
389                         return data_div(d1, d2, res, NULL);
390                 case SDB_DATA_MOD:
391                         return data_div(d1, d2, NULL, res);
392         }
393         return -1;
394 } /* sdb_data_expr_eval */
396 size_t
397 sdb_data_strlen(const sdb_data_t *datum)
399         if (! datum)
400                 return 0;
402         switch (datum->type) {
403                 case SDB_TYPE_INTEGER:
404                         /* log(64) */
405                         return 20;
406                 case SDB_TYPE_DECIMAL:
407                         /* XXX: -d.dddddde+dd or -ddddd.dddddd */
408                         return 42;
409                 case SDB_TYPE_STRING:
410                         if (! datum->data.string)
411                                 return 8; /* "<NULL>" */
412                         /* in the worst case, each character needs to be escaped */
413                         return 2 * strlen(datum->data.string) + 2;
414                 case SDB_TYPE_DATETIME:
415                         /* "YYYY-MM-DD HH:MM:SS +zzzz" */
416                         return 27;
417                 case SDB_TYPE_BINARY:
418                         if (! datum->data.binary.datum)
419                                 return 8; /* "<NULL>" */
420                         /* "\xNN" */
421                         return 4 * datum->data.binary.length + 2;
422         }
423         return 0;
424 } /* sdb_data_strlen */
426 int
427 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
429         char tmp[sdb_data_strlen(datum) + 1];
430         char *data = NULL;
431         int ret = -1;
433         size_t i, pos;
435         if ((! datum) || (! buf))
436                 return -1;
438         switch (datum->type) {
439                 case SDB_TYPE_INTEGER:
440                         ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
441                         break;
442                 case SDB_TYPE_DECIMAL:
443                         ret = snprintf(buf, buflen, "%g", datum->data.decimal);
444                         break;
445                 case SDB_TYPE_STRING:
446                         if (! datum->data.string)
447                                 data = "<NULL>";
448                         else {
449                                 pos = 0;
450                                 for (i = 0; i < strlen(datum->data.string); ++i) {
451                                         char byte = datum->data.string[i];
453                                         if ((byte == '\\') || (byte == '"')) {
454                                                 tmp[pos] = '\\';
455                                                 ++pos;
456                                         }
457                                         tmp[pos] = byte;
458                                         ++pos;
459                                 }
460                                 tmp[pos] = '\0';
461                                 data = tmp;
462                         }
463                         break;
464                 case SDB_TYPE_DATETIME:
465                         if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
466                                                 datum->data.datetime))
467                                 return -1;
468                         tmp[sizeof(tmp) - 1] = '\0';
469                         data = tmp;
470                         break;
471                 case SDB_TYPE_BINARY:
472                         pos = 0;
473                         for (i = 0; i < datum->data.binary.length; ++i) {
474                                 int byte = (int)datum->data.binary.datum[i];
475                                 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
476                                         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
478                                 tmp[pos] = '\\';
479                                 tmp[pos + 1] = 'x';
480                                 pos += 2;
482                                 if (byte > 0xf) {
483                                         tmp[pos] = hex[byte >> 4];
484                                         ++pos;
485                                 }
486                                 tmp[pos] = hex[byte & 0xf];
487                                 ++pos;
488                         }
489                         if (datum->data.binary.datum) {
490                                 tmp[pos] = '\0';
491                                 data = tmp;
492                         }
493                         else
494                                 data = "<NULL>";
495                         break;
496         }
498         if (data) {
499                 if (quoted == SDB_UNQUOTED)
500                         ret = snprintf(buf, buflen, "%s", data);
501                 else if (quoted == SDB_SINGLE_QUOTED)
502                         ret = snprintf(buf, buflen, "'%s'", data);
503                 else
504                         ret = snprintf(buf, buflen, "\"%s\"", data);
505         }
506         buf[buflen - 1] = '\0';
507         return ret;
508 } /* sdb_data_format */
510 int
511 sdb_data_parse(char *str, int type, sdb_data_t *data)
513         sdb_data_t tmp;
515         char *endptr = NULL;
517         errno = 0;
518         switch (type) {
519                 case SDB_TYPE_INTEGER:
520                         tmp.data.integer = strtoll(str, &endptr, 0);
521                         break;
522                 case SDB_TYPE_DECIMAL:
523                         tmp.data.decimal = strtod(str, &endptr);
524                         break;
525                 case SDB_TYPE_STRING:
526                         tmp.data.string = str;
527                         break;
528                 case SDB_TYPE_DATETIME:
529                         {
530                                 double datetime = strtod(str, &endptr);
531                                 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
532                         }
533                         break;
534                 case SDB_TYPE_BINARY:
535                         /* we don't support any binary information containing 0-bytes */
536                         tmp.data.binary.length = strlen(str);
537                         tmp.data.binary.datum = (unsigned char *)str;
538                         break;
539                 default:
540                         errno = EINVAL;
541                         return -1;
542         }
544         if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
545                         || (type == SDB_TYPE_DATETIME)) {
546                 if (errno || (str == endptr)) {
547                         char errbuf[1024];
548                         sdb_log(SDB_LOG_ERR, "core: Failed to parse string "
549                                         "'%s' as numeric value (type %i): %s", str, type,
550                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
551                         return -1;
552                 }
553                 else if (endptr && (*endptr != '\0'))
554                         sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after "
555                                         "number while parsing numeric value (type %i): %s.",
556                                         type, endptr);
557         }
559         if (data) {
560                 *data = tmp;
561                 data->type = type;
562         }
563         return 0;
564 } /* sdb_data_parse */
566 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */