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)
122 {
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)
180 {
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)
235 {
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 tmp.data.string = strdup(src->data.string);
245 if (! tmp.data.string)
246 return -1;
247 break;
248 case SDB_TYPE_BINARY:
249 tmp.data.binary.datum = malloc(src->data.binary.length);
250 if (! tmp.data.binary.datum)
251 return -1;
252 memcpy(tmp.data.binary.datum, src->data.binary.datum,
253 src->data.binary.length);
254 break;
255 }
257 sdb_data_free_datum(dst);
258 *dst = tmp;
259 return 0;
260 } /* sdb_data_copy */
262 void
263 sdb_data_free_datum(sdb_data_t *datum)
264 {
265 if (! datum)
266 return;
268 switch (datum->type) {
269 case SDB_TYPE_STRING:
270 if (datum->data.string)
271 free(datum->data.string);
272 datum->data.string = NULL;
273 break;
274 case SDB_TYPE_BINARY:
275 if (datum->data.binary.datum)
276 free(datum->data.binary.datum);
277 datum->data.binary.datum = NULL;
278 datum->data.binary.length = 0;
279 break;
280 }
281 } /* sdb_data_free_datum */
283 int
284 sdb_data_cmp(const sdb_data_t *d1, const sdb_data_t *d2)
285 {
286 #define CMP_NULL(a, b) \
287 do { \
288 if (!(a) && !(b)) return 0; \
289 if (!(a)) return -1; \
290 if (!(b)) return 1; \
291 } while (0)
293 CMP_NULL(d1, d2);
295 if (d1->type != d2->type)
296 return SDB_CMP(d1->type, d2->type);
298 switch (d1->type) {
299 case SDB_TYPE_INTEGER:
300 return SDB_CMP(d1->data.integer, d2->data.integer);
301 case SDB_TYPE_DECIMAL:
302 return SDB_CMP(d1->data.decimal, d2->data.decimal);
303 case SDB_TYPE_STRING:
304 CMP_NULL(d1->data.string, d2->data.string);
305 return strcasecmp(d1->data.string, d2->data.string);
306 case SDB_TYPE_DATETIME:
307 return SDB_CMP(d1->data.datetime, d2->data.datetime);
308 case SDB_TYPE_BINARY:
309 {
310 int diff;
312 CMP_NULL(d1->data.binary.datum, d2->data.binary.datum);
314 /* on a common prefix, the shorter datum sorts less */
315 if (d1->data.binary.length < d2->data.binary.length) {
316 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
317 d1->data.binary.length);
318 diff = diff ? diff : -1;
319 }
320 else if (d1->data.binary.length > d2->data.binary.length) {
321 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
322 d2->data.binary.length);
323 diff = diff ? diff : 1;
324 }
325 else
326 diff = memcmp(d1->data.binary.datum, d2->data.binary.datum,
327 d1->data.binary.length);
329 return diff;
330 }
331 default:
332 return -1;
333 }
334 #undef CMP_NULL
335 } /* sdb_data_cmp */
337 int
338 sdb_data_expr_eval(int op, const sdb_data_t *d1, const sdb_data_t *d2,
339 sdb_data_t *res)
340 {
341 if ((! d1) || (! d2) || (! res))
342 return -1;
343 switch (op) {
344 case SDB_DATA_CONCAT:
345 return data_concat(d1, d2, res);
346 case SDB_DATA_ADD:
347 return data_lin(d1, 1, d2, res);
348 case SDB_DATA_SUB:
349 return data_lin(d1, -1, d2, res);
350 case SDB_DATA_MUL:
351 return data_mul(d1, d2, res);
352 case SDB_DATA_DIV:
353 return data_div(d1, d2, res, NULL);
354 case SDB_DATA_MOD:
355 return data_div(d1, d2, NULL, res);
356 }
357 return -1;
358 } /* sdb_data_expr_eval */
360 size_t
361 sdb_data_strlen(const sdb_data_t *datum)
362 {
363 if (! datum)
364 return 0;
366 switch (datum->type) {
367 case SDB_TYPE_INTEGER:
368 /* log(64) */
369 return 20;
370 case SDB_TYPE_DECIMAL:
371 /* XXX: -0xN.NNNNNNp+NNN */
372 return 42;
373 case SDB_TYPE_STRING:
374 if (! datum->data.string)
375 return 8; /* "<NULL>" */
376 /* in the worst case, each character needs to be escaped */
377 return 2 * strlen(datum->data.string) + 2;
378 case SDB_TYPE_DATETIME:
379 /* "YYYY-MM-DD HH:MM:SS +zzzz" */
380 return 27;
381 case SDB_TYPE_BINARY:
382 if (! datum->data.binary.datum)
383 return 8; /* "<NULL>" */
384 /* "\xNN" */
385 return 4 * datum->data.binary.length + 2;
386 }
387 return 0;
388 } /* sdb_data_strlen */
390 int
391 sdb_data_format(const sdb_data_t *datum, char *buf, size_t buflen, int quoted)
392 {
393 char tmp[sdb_data_strlen(datum) + 1];
394 char *data = NULL;
395 int ret = -1;
397 size_t i, pos;
399 if ((! datum) || (! buf))
400 return -1;
402 switch (datum->type) {
403 case SDB_TYPE_INTEGER:
404 ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
405 break;
406 case SDB_TYPE_DECIMAL:
407 ret = snprintf(buf, buflen, "%a", datum->data.decimal);
408 break;
409 case SDB_TYPE_STRING:
410 if (! datum->data.string)
411 data = "<NULL>";
412 else {
413 pos = 0;
414 for (i = 0; i < strlen(datum->data.string); ++i) {
415 char byte = datum->data.string[i];
417 if ((byte == '\\') || (byte == '"')) {
418 tmp[pos] = '\\';
419 ++pos;
420 }
421 tmp[pos] = byte;
422 ++pos;
423 }
424 tmp[pos] = '\0';
425 data = tmp;
426 }
427 break;
428 case SDB_TYPE_DATETIME:
429 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
430 datum->data.datetime))
431 return -1;
432 tmp[sizeof(tmp) - 1] = '\0';
433 data = tmp;
434 break;
435 case SDB_TYPE_BINARY:
436 pos = 0;
437 for (i = 0; i < datum->data.binary.length; ++i) {
438 int byte = (int)datum->data.binary.datum[i];
439 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
440 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
442 tmp[pos] = '\\';
443 tmp[pos + 1] = 'x';
444 pos += 2;
446 if (byte > 0xf) {
447 tmp[pos] = hex[byte >> 4];
448 ++pos;
449 }
450 tmp[pos] = hex[byte & 0xf];
451 ++pos;
452 }
453 if (datum->data.binary.datum) {
454 tmp[pos] = '\0';
455 data = tmp;
456 }
457 else
458 data = "<NULL>";
459 break;
460 }
462 if (data) {
463 if (quoted == SDB_UNQUOTED)
464 ret = snprintf(buf, buflen, "%s", data);
465 else if (quoted == SDB_SINGLE_QUOTED)
466 ret = snprintf(buf, buflen, "'%s'", data);
467 else
468 ret = snprintf(buf, buflen, "\"%s\"", data);
469 }
470 buf[buflen - 1] = '\0';
471 return ret;
472 } /* sdb_data_format */
474 int
475 sdb_data_parse(char *str, int type, sdb_data_t *data)
476 {
477 sdb_data_t tmp;
479 char *endptr = NULL;
481 errno = 0;
482 switch (type) {
483 case SDB_TYPE_INTEGER:
484 tmp.data.integer = strtoll(str, &endptr, 0);
485 break;
486 case SDB_TYPE_DECIMAL:
487 tmp.data.decimal = strtod(str, &endptr);
488 break;
489 case SDB_TYPE_STRING:
490 tmp.data.string = str;
491 break;
492 case SDB_TYPE_DATETIME:
493 {
494 double datetime = strtod(str, &endptr);
495 tmp.data.datetime = DOUBLE_TO_SDB_TIME(datetime);
496 }
497 break;
498 case SDB_TYPE_BINARY:
499 /* we don't support any binary information containing 0-bytes */
500 tmp.data.binary.length = strlen(str);
501 tmp.data.binary.datum = (unsigned char *)str;
502 break;
503 default:
504 errno = EINVAL;
505 return -1;
506 }
508 if ((type == SDB_TYPE_INTEGER) || (type == SDB_TYPE_DECIMAL)
509 || (type == SDB_TYPE_DATETIME)) {
510 if (errno || (str == endptr)) {
511 char errbuf[1024];
512 sdb_log(SDB_LOG_ERR, "core: Failed to parse string "
513 "'%s' as numeric value (type %i): %s", str, type,
514 sdb_strerror(errno, errbuf, sizeof(errbuf)));
515 return -1;
516 }
517 else if (endptr && (*endptr != '\0'))
518 sdb_log(SDB_LOG_WARNING, "core: Ignoring garbage after "
519 "number while parsing numeric value (type %i): %s.",
520 type, endptr);
521 }
523 if (data) {
524 *data = tmp;
525 data->type = type;
526 }
527 return 0;
528 } /* sdb_data_parse */
530 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */