-
Notifications
You must be signed in to change notification settings - Fork 1
/
bson_parser.c
297 lines (276 loc) · 8.98 KB
/
bson_parser.c
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
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include "hashmap/hashmap.h"
#include "bson_parser.h"
#define INITIAL_BUFFER_SIZE 2
struct bson_container* create_bson_container(enum BSON_TYPE type)
{
struct bson_container* result = calloc(sizeof(struct bson_container), 1);
result->type = type;
return result;
}
void free_bson_object(BSON_Object object)
{
hashmap_iterate(object, free_map_iter, NULL);
hashmap_clear_keys(object);
hashmap_free(object);
}
int32_t free_map_iter(any_t item, any_t elem)
{
if (! elem) {
return MAP_OK;
}
BSON_Container container = (BSON_Container) elem;
switch(container->type) {
case BSON_STRING:
printf("freeing str\n");
free(container->data.value_str);
break;
case BSON_BINARY:
free(container->data.value_binary);
break;
case BSON_DOCUMENT:
case BSON_ARRAY:
hashmap_iterate(container->data.value_document, free_map_iter, NULL);
hashmap_free(container->data.value_document);
break;
default:
break;
}
free(container);
return MAP_OK;
}
int32_t parse_bson_byte(char* buf, uint32_t* bytes_read, uint32_t buf_size, uint8_t* result)
{
char* pbuf;
if (*bytes_read + 1 > buf_size) {
return -1;
}
pbuf = buf + *bytes_read;
*result = pbuf[0];
*bytes_read = *bytes_read + 1;
return 1;
}
int32_t parse_bson_boolean(char* buf, uint32_t* bytes_read, uint32_t buf_size, bool* result)
{
char* pbuf;
if (*bytes_read + 1 > buf_size) {
return -1;
}
pbuf = buf + *bytes_read;
if (pbuf[0] == 0) {
*result = false;
} else if (pbuf[0] == 1) {
*result = true;
} else {
return -1;
}
return 1;
}
int32_t parse_bson_int32(char* buf, uint32_t* bytes_read, uint32_t buf_size, int32_t* result)
{
char* pbuf;
if (*bytes_read + 4 > buf_size) {
return -1;
}
pbuf = buf + *bytes_read;
memcpy(result, pbuf, 4);
*bytes_read = *bytes_read + 4;
return 1;
}
int32_t parse_bson_int64(char* buf, uint32_t* bytes_read, uint32_t buf_size, int64_t* result)
{
char* pbuf;
if (*bytes_read + 8 > buf_size) {
return -1;
}
pbuf = buf + *bytes_read;
memcpy(result, pbuf, 8);
*bytes_read = *bytes_read + 8;
return 1;
}
/* Returns number of bytes read including the terminating null
*/
int32_t parse_bson_cstring(char* buf, uint32_t *bytes_read, uint32_t buf_size, char** result)
{
char* rbuf;
char* pbuf;
char c;
uint32_t cnt;
uint32_t cur_buf_size = INITIAL_BUFFER_SIZE;
pbuf = buf + *bytes_read;
rbuf = calloc(cur_buf_size, sizeof(char));
cnt = 0;
printf("reading str\n");
do {
c = pbuf[cnt];
if (cnt == cur_buf_size) {
void* tmp = NULL;
cur_buf_size = cur_buf_size * 2;
tmp = realloc(rbuf, cur_buf_size);
if (tmp == NULL) {
free(rbuf);
return -1;
}
rbuf = tmp;
}
rbuf[cnt] = c;
cnt++;
if (cnt > buf_size) {
if (rbuf) {
free(rbuf);
}
return -1;
}
} while(c != '\0');
*result = rbuf;
*bytes_read = *bytes_read + cnt;
return cnt;
}
int32_t parse_bson_binary(char* buf, uint32_t* bytes_read, uint32_t buf_size, char** result)
{
int32_t bin_length;
int32_t rval;
uint8_t bin_subtype;
char* rbuf;
char* pbuf = buf + *bytes_read;
rval = parse_bson_int32(buf, bytes_read, buf_size, &bin_length);
if (rval < 0) return -1;
if (*bytes_read + sizeof(bin_subtype) + bin_length > buf_size) {
return -1;
}
rval = parse_bson_byte(buf, bytes_read, buf_size, &bin_subtype);
if (rval < 0) return -1;
rbuf = malloc(bin_length);
memcpy(rbuf, pbuf, bin_length);
*result = rbuf;
*bytes_read = *bytes_read + bin_length;
return bin_length;
}
int32_t parse_bson_double(char* buf, uint32_t* bytes_read, uint32_t buf_size, double* result)
{
char* pbuf;
if (*bytes_read + 8 > buf_size) {
return -1;
}
pbuf = buf + *bytes_read;
memcpy(result, pbuf, 8);
*bytes_read = *bytes_read + 8;
return 1;
}
int32_t parse_bson_document(char* buf, uint32_t* bytes_read, uint32_t buf_size, BSON_Object result)
{
int32_t rval = 0;
int32_t doc_len = 0;
char* key;
uint8_t value_type;
int32_t initial_bytes;
int32_t value_len;
initial_bytes = *bytes_read;
rval = parse_bson_int32(buf, bytes_read, buf_size, &doc_len);
if (rval < 0) {
return -1;
}
while (( (*bytes_read - initial_bytes) < doc_len ) && /* guard check for valid encoding */
(*bytes_read < buf_size)) { /* guard check to prevent buffer over reads */
BSON_Container cont;
rval = parse_bson_byte(buf, bytes_read, buf_size, &value_type);
if (rval < 0) break;
if (value_type == 0) {
return 0;
}
rval = parse_bson_cstring(buf, bytes_read, buf_size, &key);
if (rval < 0) break;
switch(value_type) {
case BSON_DOUBLE:
cont = create_bson_container(BSON_DOUBLE);
rval = parse_bson_double(buf, bytes_read, buf_size, &cont->data.value_double);
if (rval < 0) break;
hashmap_put(result, key, cont);
break;
case BSON_STRING:
cont = create_bson_container(BSON_STRING);
rval = parse_bson_int32(buf, bytes_read, buf_size, &value_len);
if (rval < 0) break;
rval = parse_bson_cstring(buf, bytes_read, buf_size, &cont->data.value_str);
if (rval < 0) break;
cont->data_length = rval;
hashmap_put(result, key, cont);
break;
case BSON_ARRAY:
cont = create_bson_container(BSON_DOCUMENT);
cont->data.value_document = hashmap_new();
rval = parse_bson_document(buf, bytes_read, buf_size, cont->data.value_document);
if (rval < 0) break;
hashmap_put(result, key, cont);
break;
case BSON_DOCUMENT:
cont = create_bson_container(BSON_DOCUMENT);
cont->data.value_document = hashmap_new();
rval = parse_bson_document(buf, bytes_read, buf_size, cont->data.value_document);
if (rval < 0) break;
hashmap_put(result, key, cont);
break;
case BSON_BINARY:
cont = create_bson_container(BSON_BINARY);
rval = parse_bson_binary(buf, bytes_read, buf_size, &cont->data.value_binary);
if (rval < 0) break;
cont->data_length = rval;
hashmap_put(result, key, cont);
break;
case BSON_BOOLEAN:
cont = create_bson_container(BSON_BOOLEAN);
rval = parse_bson_boolean(buf, bytes_read, buf_size, &cont->data.value_boolean);
if (rval < 0) break;
hashmap_put(result, key, cont);
break;
case BSON_NULL:
cont = create_bson_container(BSON_NULL);
hashmap_put(result, key, cont);
break;
case BSON_INT32:
cont = create_bson_container(BSON_INT32);
rval = parse_bson_int32(buf, bytes_read, buf_size, &cont->data.value_int32);
if (rval < 0) break;
hashmap_put(result, key, cont);
break;
case BSON_UTCDATETIME:
cont = create_bson_container(BSON_UTCDATETIME);
rval = parse_bson_int64(buf,bytes_read, buf_size, &cont->data.value_int64);
if (rval < 0) break;
hashmap_put(result, key, cont);
break;
case BSON_TIMESTAMP:
cont = create_bson_container(BSON_INT64);
rval = parse_bson_int64(buf, bytes_read, buf_size, &cont->data.value_int64);
if (rval < 0) break;
hashmap_put(result, key, cont);
break;
case BSON_INT64:
cont = create_bson_container(BSON_INT64);
rval = parse_bson_int64(buf, bytes_read, buf_size, &cont->data.value_int64);
if (rval < 0) break;
hashmap_put(result, key, cont);
break;
case BSON_MIN_KEY:
cont = create_bson_container(BSON_MIN_KEY);
hashmap_put(result, key, cont);
break;
case BSON_MAX_KEY:
cont = create_bson_container(BSON_MAX_KEY);
hashmap_put(result, key, cont);
break;
default:
rval = -1; /* Parser error*/
break;
}
if (rval < 0) {
break;
}
}
return rval;
}