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