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
|
/*
* Copyright (c) 2018 Vasile Vilvoiu <vasi.vilvoiu@gmail.com>
*
* libecbor is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include "ecbor.h"
#include "ecbor_internal.h"
uint16_t
ecbor_uint16_from_big_endian (uint16_t value)
{
#if __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
return value;
#elif __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
/* TODO: ARM optimization */
return ((value << 8) & 0xff00)
| ((value >> 8) & 0x00ff);
#else
#error "Endianness not supported!"
#endif
}
uint32_t
ecbor_uint32_from_big_endian (uint32_t value)
{
#if __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
return value;
#elif __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
/* TODO: ARM optimization */
return ((value << 8) & 0x00ff0000)
| ((value >> 8) & 0x0000ff00)
| ((value << 24) & 0xff000000)
| ((value >> 24) & 0x000000ff);
#else
#error "Endianness not supported!"
#endif
}
uint64_t
ecbor_uint64_from_big_endian (uint64_t value)
{
#if __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
return value;
#elif __BYTE_ORDER__ && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
/* TODO: ARM optimization */
return ((value << 8) & 0x000000ff00000000)
| ((value >> 8) & 0x00000000ff000000)
| ((value << 24) & 0x0000ff0000000000)
| ((value >> 24) & 0x0000000000ff0000)
| ((value << 40) & 0x00ff000000000000)
| ((value >> 40) & 0x000000000000ff00)
| ((value << 56) & 0xff00000000000000)
| ((value >> 56) & 0x00000000000000ff);
#else
#error "Endianness not supported!"
#endif
}
extern ecbor_major_type_t
ecbor_get_type (ecbor_item_t *item)
{
if (!item) {
return ECBOR_MT_UNDEFINED;
}
if (item->major_type < ECBOR_MT_UINT
|| item->major_type > ECBOR_MT_SPECIAL) {
return ECBOR_MT_UNDEFINED;
}
return item->major_type;
}
extern ecbor_error_t
ecbor_get_value (ecbor_item_t *item, void *value)
{
if (!item) {
return ECBOR_ERR_NULL_ITEM;
}
if (!value) {
return ECBOR_ERR_NULL_VALUE;
}
switch (item->major_type) {
case ECBOR_MT_NINT:
*((int64_t *)value) = item->value.integer;
break;
case ECBOR_MT_UINT:
*((uint64_t *)value) = item->value.uinteger;
break;
case ECBOR_MT_BSTR:
case ECBOR_MT_STR:
if (item->is_indefinite) {
return ECBOR_ERR_WONT_RETURN_INDEFINITE;
}
*((uint8_t **)value) = (uint8_t *) item->value.string.str;
break;
case ECBOR_MT_TAG:
*((uint64_t *)value) = item->value.tag.tag_value;
break;
default:
return ECBOR_ERR_INVALID_TYPE;
}
return ECBOR_OK;
}
extern ecbor_error_t
ecbor_get_length (ecbor_item_t *item, uint64_t *length)
{
if (!item) {
return ECBOR_ERR_NULL_ITEM;
}
if (!length) {
return ECBOR_ERR_NULL_VALUE;
}
switch (item->major_type) {
case ECBOR_MT_BSTR:
case ECBOR_MT_STR:
case ECBOR_MT_ARRAY:
*length = item->length;
break;
case ECBOR_MT_MAP:
*length = item->length / 2;
break;
default:
return ECBOR_ERR_INVALID_TYPE;
}
return ECBOR_OK;
}
extern ecbor_error_t
ecbor_get_array_item (ecbor_item_t *array, uint64_t index,
ecbor_item_t *item)
{
ecbor_decode_context_t context;
ecbor_error_t rc;
uint64_t i;
if (!array) {
return ECBOR_ERR_NULL_ARRAY;
}
if (array->major_type != ECBOR_MT_ARRAY) {
return ECBOR_ERR_INVALID_TYPE;
}
if (array->length <= index) {
return ECBOR_ERR_INDEX_OUT_OF_BOUNDS;
}
if (!item) {
return ECBOR_ERR_NULL_ITEM;
}
rc = ecbor_initialize_decode (&context, array->value.items, array->size);
if (rc != ECBOR_OK) {
return rc;
}
for (i = 0; i <= index; i ++) {
rc = ecbor_decode (&context, item);
if (rc != ECBOR_OK) {
if (rc == ECBOR_END_OF_BUFFER) {
return ECBOR_ERR_INVALID_END_OF_BUFFER;
} else {
return rc;
}
}
}
return ECBOR_OK;
}
extern ecbor_error_t
ecbor_get_map_item (ecbor_item_t *map, uint64_t index, ecbor_item_t *key,
ecbor_item_t *value)
{
ecbor_decode_context_t context;
ecbor_error_t rc;
uint64_t i;
if (!map) {
return ECBOR_ERR_NULL_MAP;
}
if (map->major_type != ECBOR_MT_MAP) {
return ECBOR_ERR_INVALID_TYPE;
}
if (map->length <= (index * 2)) {
return ECBOR_ERR_INDEX_OUT_OF_BOUNDS;
}
if (!key || !value) {
return ECBOR_ERR_NULL_ITEM;
}
rc = ecbor_initialize_decode (&context, map->value.items, map->size);
if (rc != ECBOR_OK) {
return rc;
}
for (i = 0; i <= index; i ++) {
rc = ecbor_decode (&context, key);
if (rc != ECBOR_OK) {
if (rc == ECBOR_END_OF_BUFFER) {
return ECBOR_ERR_INVALID_END_OF_BUFFER;
} else {
return rc;
}
}
rc = ecbor_decode (&context, value);
if (rc != ECBOR_OK) {
if (rc == ECBOR_END_OF_BUFFER) {
return ECBOR_ERR_INVALID_END_OF_BUFFER;
} else {
return rc;
}
}
}
return ECBOR_OK;
}
extern ecbor_error_t
ecbor_get_tag_item (ecbor_item_t *tag, ecbor_item_t *item)
{
ecbor_decode_context_t context;
ecbor_error_t rc;
if (!tag) {
return ECBOR_ERR_NULL_ITEM;
}
if (tag->major_type != ECBOR_MT_ARRAY) {
return ECBOR_ERR_INVALID_TYPE;
}
if (!item) {
return ECBOR_ERR_NULL_VALUE;
}
rc = ecbor_initialize_decode (&context, tag->value.items, tag->size);
if (rc != ECBOR_OK) {
return rc;
}
rc = ecbor_decode (&context, item);
if (rc != ECBOR_OK) {
if (rc == ECBOR_END_OF_BUFFER) {
return ECBOR_ERR_INVALID_END_OF_BUFFER;
} else {
return rc;
}
}
return ECBOR_OK;
}
extern ecbor_error_t
ecbor_get_root (ecbor_decode_context_t *context, ecbor_node_t **root)
{
if (!context) {
return ECBOR_ERR_NULL_CONTEXT;
}
if (!root) {
return ECBOR_ERR_NULL_NODE;
}
if (context->n_nodes <= 0) {
return ECBOR_ERR_EMPTY_NODE_BUFFER;
}
*root = &context->nodes[0];
return ECBOR_OK;
}
|