ubjson-read.c
11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Copyright (C) 2014 René Kijewski <rene.kijewski@fu-berlin.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @ingroup sys
* @{
* @file
* @brief Universal Binary JSON deserializer
* @author René Kijewski <rene.kijewski@fu-berlin.de>
* @}
*/
#include "ubjson-internal.h"
#include "ubjson.h"
#include "byteorder.h"
#define READ_MARKER() \
do { \
result = _ubjson_read_marker(cookie, &marker); \
if (result != UBJSON_OKAY) { \
return result; \
} \
} while (0)
static ubjson_read_callback_result_t _ubjson_read_marker(ubjson_cookie_t *restrict cookie,
char *marker)
{
while (1) {
ssize_t bytes_read;
*marker = cookie->marker;
if (!*marker) {
bytes_read = cookie->rw.read(cookie, marker, 1);
}
else {
cookie->marker = 0;
bytes_read = 1;
}
if (bytes_read == 1) {
return UBJSON_OKAY;
}
else if (bytes_read != 0) {
return UBJSON_PREMATURELY_ENDED;
}
}
}
ssize_t ubjson_get_string(ubjson_cookie_t *restrict cookie, ssize_t length, void *dest_)
{
ssize_t total = 0;
char *dest = dest_;
while (total < length) {
ssize_t read = cookie->rw.read(cookie, dest, length - total);
if (read < 0) {
return read;
}
total += read;
dest += read;
}
return total;
}
ssize_t ubjson_get_i32(ubjson_cookie_t *restrict cookie, ssize_t content, int32_t *dest)
{
static const int8_t LENGHTS[] = { 1, 1, 2, 4 };
union {
int8_t i8;
uint8_t u8;
network_uint16_t i16;
network_uint32_t i32;
} value;
int type = content;
ssize_t result = ubjson_get_string(cookie, LENGHTS[type], &value);
if (result > 0) {
switch (type) {
case UBJSON_INT32_INT8:
*dest = value.i8;
break;
case UBJSON_INT32_UINT8:
*dest = value.u8;
break;
case UBJSON_INT32_INT16:
*dest = (int16_t) byteorder_ntohs(value.i16);
break;
case UBJSON_INT32_INT32:
*dest = (int32_t) byteorder_ntohl(value.i32);
break;
default:
return -1;
}
}
return result;
}
ssize_t ubjson_get_i64(ubjson_cookie_t *restrict cookie, ssize_t content, int64_t *dest)
{
(void) content;
network_uint64_t buf;
ssize_t result = ubjson_get_string(cookie, 8, &buf);
*dest = byteorder_ntohll(buf);
return result;
}
static ubjson_read_callback_result_t _ubjson_read_length(ubjson_cookie_t *restrict cookie,
ssize_t *len)
{
ubjson_type_t type;
ssize_t content = 0;
ubjson_read_callback_result_t result = ubjson_peek_value(cookie, &type, &content);
if (result != UBJSON_OKAY) {
return result;
}
int64_t len64 = -1;
ssize_t read;
if (type == UBJSON_TYPE_INT32) {
int32_t len32;
read = ubjson_get_i32(cookie, content, &len32);
if (read > 0) {
len64 = len32;
}
}
else if (type == UBJSON_TYPE_INT64) {
read = ubjson_get_i64(cookie, content, &len64);
}
else {
return UBJSON_INVALID_DATA;
}
if (read <= 0) {
return UBJSON_PREMATURELY_ENDED;
}
else if ((ssize_t) len64 < 0) {
return UBJSON_SIZE_ERROR;
}
else if (len64 < 0) {
return UBJSON_INVALID_DATA;
}
*len = len64;
return UBJSON_OKAY;
}
static ubjson_read_callback_result_t _ubjson_get_call(ubjson_cookie_t *restrict cookie,
char marker,
ubjson_type_t *type, ssize_t *content)
{
*content = -1;
switch (marker) {
case UBJSON_MARKER_NULL:
*type = UBJSON_TYPE_NULL;
break;
case UBJSON_MARKER_NOOP:
*type = UBJSON_TYPE_NOOP;
break;
case UBJSON_MARKER_TRUE:
case UBJSON_MARKER_FALSE:
*type = UBJSON_TYPE_BOOL,
*content = (marker == UBJSON_MARKER_TRUE);
break;
case UBJSON_MARKER_INT8:
*content = UBJSON_INT32_INT8;
*type = UBJSON_TYPE_INT32;
break;
case UBJSON_MARKER_UINT8:
*content = UBJSON_INT32_UINT8;
*type = UBJSON_TYPE_INT32;
break;
case UBJSON_MARKER_INT16:
*content = UBJSON_INT32_INT16;
*type = UBJSON_TYPE_INT32;
break;
case UBJSON_MARKER_INT32:
*content = UBJSON_INT32_INT32;
*type = UBJSON_TYPE_INT32;
break;
case UBJSON_MARKER_INT64:
*type = UBJSON_TYPE_INT64;
break;
case UBJSON_MARKER_CHAR:
*type = UBJSON_TYPE_STRING;
break;
case UBJSON_MARKER_FLOAT32:
*type = UBJSON_TYPE_FLOAT;
break;
case UBJSON_MARKER_FLOAT64:
*type = UBJSON_TYPE_DOUBLE;
break;
case UBJSON_MARKER_STRING: {
ubjson_read_callback_result_t result = _ubjson_read_length(cookie, content);
if (result != UBJSON_OKAY) {
return result;
}
*type = UBJSON_TYPE_STRING;
break;
}
case UBJSON_MARKER_ARRAY_START:
*type = UBJSON_ENTER_ARRAY;
break;
case UBJSON_MARKER_OBJECT_START:
*type = UBJSON_ENTER_OBJECT;
break;
/* NOTE: High-precision numbers are not implemented, yet. Implement if needed.
*
* case UBJSON_MARKER_HP_NUMBER:
* ...
* break;
*/
case UBJSON_MARKER_ARRAY_END:
case UBJSON_MARKER_OBJECT_END:
case UBJSON_MARKER_COUNT:
case UBJSON_MARKER_TYPE:
default:
return UBJSON_INVALID_DATA;
}
return UBJSON_OKAY;
}
typedef bool (*_ubjson_read_struct_continue)(ubjson_cookie_t *restrict cookie, char marker,
ubjson_read_callback_result_t *result,
ssize_t count, ssize_t index,
ubjson_type_t *type1, ssize_t *content1);
static ubjson_read_callback_result_t _ubjson_read_struct(ubjson_cookie_t *restrict cookie,
_ubjson_read_struct_continue get_continue)
{
ubjson_read_callback_result_t result;
ssize_t count = -1;
char marker, type_marker = 0;
READ_MARKER();
if (marker == UBJSON_MARKER_TYPE) {
READ_MARKER();
if (marker == 0) {
return UBJSON_INVALID_DATA;
}
type_marker = marker;
READ_MARKER();
}
if (marker == UBJSON_MARKER_COUNT) {
result = _ubjson_read_length(cookie, &count);
if (result != UBJSON_OKAY) {
return result;
}
READ_MARKER();
}
cookie->marker = marker;
if ((type_marker != 0) && (count < 0)) {
/* If a type is specified, a count must be specified as well.
* Otherwise a ']' could either be data (e.g. the character ']'),
* or be meant to close the array.
*/
return UBJSON_INVALID_DATA;
}
for (ssize_t index = 0; (count < 0) || (index < count); ++index) {
ubjson_type_t type1;
ssize_t content1;
READ_MARKER();
if (!get_continue(cookie, marker, &result, count, index, &type1, &content1)
|| (result != UBJSON_OKAY)) {
break;
}
result = cookie->callback.read(cookie, type1, content1,
UBJSON_ABSENT, (unsigned char) type_marker);
if (result != UBJSON_OKAY) {
break;
}
}
return result;
}
static bool _ubjson_read_array_continue(ubjson_cookie_t *restrict cookie, char marker,
ubjson_read_callback_result_t *result,
ssize_t count, ssize_t index,
ubjson_type_t *type1, ssize_t *content1)
{
if (marker == UBJSON_MARKER_ARRAY_END) {
if (count >= 0) {
*result = UBJSON_INVALID_DATA;
}
return false;
}
cookie->marker = marker;
*type1 = UBJSON_INDEX;
*content1 = index;
return true;
}
static bool _ubjson_read_object_continue(ubjson_cookie_t *restrict cookie, char marker,
ubjson_read_callback_result_t *result,
ssize_t count, ssize_t index,
ubjson_type_t *type1, ssize_t *content1)
{
(void) index;
if (marker == UBJSON_MARKER_OBJECT_END) {
if (count >= 0) {
*result = UBJSON_INVALID_DATA;
}
return false;
}
cookie->marker = marker;
*type1 = UBJSON_KEY;
*result = _ubjson_read_length(cookie, content1);
return true;
}
ubjson_read_callback_result_t ubjson_read_array(ubjson_cookie_t *restrict cookie)
{
return _ubjson_read_struct(cookie, _ubjson_read_array_continue);
}
ubjson_read_callback_result_t ubjson_read_object(ubjson_cookie_t *restrict cookie)
{
return _ubjson_read_struct(cookie, _ubjson_read_object_continue);
}
ubjson_read_callback_result_t ubjson_read_next(ubjson_cookie_t *restrict cookie)
{
char marker;
ubjson_read_callback_result_t result;
READ_MARKER();
ubjson_type_t type;
ssize_t content;
result = _ubjson_get_call(cookie, marker, &type, &content);
if (result != UBJSON_OKAY) {
return result;
}
return cookie->callback.read(cookie, type, content, UBJSON_ABSENT, 0);
}
ubjson_read_callback_result_t ubjson_peek_value(ubjson_cookie_t *restrict cookie,
ubjson_type_t *type, ssize_t *content)
{
char marker = (char) *content;
if (marker == 0) {
ubjson_read_callback_result_t result;
READ_MARKER();
}
return _ubjson_get_call(cookie, marker, type, content);
}