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 "core/data.h"
34 #include <inttypes.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 /*
41 * public API
42 */
44 int
45 sdb_data_copy(sdb_data_t *dst, const sdb_data_t *src)
46 {
47 sdb_data_t tmp;
49 if ((! dst) || (! src))
50 return -1;
52 tmp = *src;
53 switch (src->type) {
54 case SDB_TYPE_STRING:
55 tmp.data.string = strdup(src->data.string);
56 if (! tmp.data.string)
57 return -1;
58 break;
59 case SDB_TYPE_BINARY:
60 tmp.data.binary.datum = malloc(src->data.binary.length);
61 if (! tmp.data.binary.datum)
62 return -1;
63 memcpy(tmp.data.binary.datum, src->data.binary.datum,
64 src->data.binary.length);
65 break;
66 }
68 *dst = tmp;
69 return 0;
70 } /* sdb_data_copy */
72 void
73 sdb_data_free_datum(sdb_data_t *datum)
74 {
75 if (! datum)
76 return;
78 switch (datum->type) {
79 case SDB_TYPE_STRING:
80 if (datum->data.string)
81 free(datum->data.string);
82 datum->data.string = NULL;
83 break;
84 case SDB_TYPE_BINARY:
85 if (datum->data.binary.datum)
86 free(datum->data.binary.datum);
87 datum->data.binary.datum = NULL;
88 datum->data.binary.length = 0;
89 break;
90 }
91 } /* sdb_data_free_datum */
93 size_t
94 sdb_data_strlen(sdb_data_t *datum)
95 {
96 if (! datum)
97 return 0;
99 switch (datum->type) {
100 case SDB_TYPE_INTEGER:
101 /* log(64) */
102 return 20;
103 case SDB_TYPE_DECIMAL:
104 /* XXX: -0xN.NNNNNNp+NNN */
105 return 42;
106 case SDB_TYPE_STRING:
107 if (! datum->data.string)
108 return 6; /* "NULL" */
109 /* in the worst case, each character needs to be escaped */
110 return 2 * strlen(datum->data.string) + 2;
111 case SDB_TYPE_DATETIME:
112 /* "YYYY-MM-DD HH:MM:SS +zzzz" */
113 return 27;
114 case SDB_TYPE_BINARY:
115 /* "\xNN" */
116 return 4 * datum->data.binary.length + 2;
117 }
118 return 0;
119 } /* sdb_data_strlen */
121 int
122 sdb_data_format(sdb_data_t *datum, char *buf, size_t buflen, int quoted)
123 {
124 char tmp[sdb_data_strlen(datum) + 1];
125 char *data = NULL;
126 int ret = -1;
128 size_t i, pos;
130 if ((! datum) || (! buf))
131 return -1;
133 switch (datum->type) {
134 case SDB_TYPE_INTEGER:
135 ret = snprintf(buf, buflen, "%"PRIi64, datum->data.integer);
136 break;
137 case SDB_TYPE_DECIMAL:
138 ret = snprintf(buf, buflen, "%a", datum->data.decimal);
139 break;
140 case SDB_TYPE_STRING:
141 if (! datum->data.string)
142 data = "NULL";
143 else {
144 pos = 0;
145 for (i = 0; i < strlen(datum->data.string); ++i) {
146 char byte = datum->data.string[i];
148 if ((byte == '\\') || (byte == '"')) {
149 tmp[pos] = '\\';
150 ++pos;
151 }
152 tmp[pos] = byte;
153 ++pos;
154 }
155 tmp[pos] = '\0';
156 data = tmp;
157 }
158 break;
159 case SDB_TYPE_DATETIME:
160 if (! sdb_strftime(tmp, sizeof(tmp), "%F %T %z",
161 datum->data.datetime))
162 return -1;
163 tmp[sizeof(tmp) - 1] = '\0';
164 data = tmp;
165 break;
166 case SDB_TYPE_BINARY:
167 pos = 0;
168 for (i = 0; i < datum->data.binary.length; ++i) {
169 int byte = (int)datum->data.binary.datum[i];
170 char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
171 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
173 tmp[pos] = '\\';
174 tmp[pos + 1] = 'x';
175 pos += 2;
177 if (byte > 0xf) {
178 tmp[pos] = hex[byte >> 4];
179 ++pos;
180 }
181 tmp[pos] = hex[byte & 0xf];
182 ++pos;
183 }
184 tmp[pos] = '\0';
185 data = tmp;
186 break;
187 }
189 if (data) {
190 if (quoted == SDB_UNQUOTED)
191 ret = snprintf(buf, buflen, "%s", data);
192 else if (quoted == SDB_SINGLE_QUOTED)
193 ret = snprintf(buf, buflen, "'%s'", data);
194 else
195 ret = snprintf(buf, buflen, "\"%s\"", data);
196 }
197 buf[buflen - 1] = '\0';
198 return ret;
199 } /* sdb_data_format */
201 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */