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