-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_bson.c
512 lines (400 loc) · 11.6 KB
/
js_bson.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include "js.h"
#ifdef _WIN32
#define fread_unlocked _fread_nolock
#define fwrite_unlocked _fwrite_nolock
#define fgetc_unlocked _fgetc_nolock
#endif
#ifdef __APPLE__
#define fread_unlocked fread
#define fwrite_unlocked fwrite
#define fgetc_unlocked getc_unlocked
#endif
Status bson_read (FILE *file, int len, int *amt, value_t *result) {
value_t namestr[1024];
value_t v, val[1024];
int doclen[1024];
char name[4096];
int code, size;
int depth = 0;
int ch;
val[0] = newObject(vt_object);
doclen[0] = len;
while (doclen[depth]-- > 0) {
code = fgetc_unlocked(file);
(*amt)++;
if (code < 0 )
return ERROR_endoffile;
if (code == 0) { // end of document
if (doclen[depth])
return ERROR_endoffile;
if (!depth--) {
*result = val[0];
return OK;
}
incrRefCnt(val[depth+1]);
if (val[depth].type == vt_object)
replaceValue(lookup(val[depth], namestr[depth], true, 0), val[depth+1]);
else
vec_push(val[depth].aval->valuePtr, val[depth+1]);
continue;
}
size = 0;
while (((*amt)++, ch = fgetc_unlocked(file)) > 0 ) {
if (doclen[depth]-- > 0) {
if (size < sizeof(name) )
name[size++] = ch;
else
return ERROR_bsonformat;
} else
return ERROR_bsonformat;
}
doclen[depth]--;
namestr[depth] = newString(name, size);
switch (code) {
case 0x1: {
v.bits = vt_dbl;
if (fread_unlocked (&v.dbl, sizeof(double), 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= sizeof(double);
(*amt) += sizeof(double);
break;
}
case 0x2: {
uint32_t strLen;
string_t *str;
if (fread_unlocked (&strLen, sizeof(uint32_t), 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= sizeof(uint32_t);
(*amt) += sizeof(uint32_t);
v = newString(NULL, strLen - 1);
str = v.addr;
if (fread_unlocked (str->val, strLen, 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= strLen;
(*amt) += strLen;
break;
}
case 0x3: {
uint32_t objLen;
if (fread_unlocked (&objLen, sizeof(uint32_t), 1, file) < 1)
return ERROR_endoffile;
(*amt) += sizeof(uint32_t);
doclen[depth] -= objLen;
doclen[++depth] = objLen - sizeof(uint32_t);
val[depth] = newObject(vt_object);
continue;
}
case 0x4: {
uint32_t arrayLen;
if (fread_unlocked (&arrayLen, sizeof(uint32_t), 1, file) < 1)
return ERROR_endoffile;
(*amt) += sizeof(uint32_t);
doclen[depth] -= arrayLen;
doclen[++depth] = arrayLen - sizeof(uint32_t);
val[depth] = newArray(array_value, 0);
continue;
}
case 0x5: {
uint32_t miscLen;
string_t *str;
uint8_t type;
if (fread_unlocked (&miscLen, sizeof(uint32_t), 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= sizeof(uint32_t);
(*amt) += sizeof(uint32_t);
if (fread_unlocked (&type, sizeof(uint8_t), 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= 1;
(*amt) += 1;
v = newString(NULL, miscLen);
str = v.addr;
if (fread_unlocked (str->val, miscLen, 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= miscLen;
(*amt) += miscLen;
break;
}
case 0x7: {
string_t *str;
v = newString(NULL, 12);
str = v.addr;
if (fread_unlocked (str->val, 12, 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= 12;
(*amt) += 12;
break;
}
case 0x8: {
uint32_t num = 0;
v.bits = vt_bool;
if (fread_unlocked (&num, 1, 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= 1;
(*amt) += 1;
v.nval = num;
break;
}
case 0x9: {
uint64_t num;
if (fread_unlocked (&num, sizeof(uint64_t), 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= sizeof(uint64_t);
(*amt) += sizeof(uint64_t);
v.bits = vt_date;
v.nval = num;
break;
}
case 0x10: {
uint32_t num;
if (fread_unlocked (&num, sizeof(uint32_t), 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= sizeof(uint32_t);
(*amt) += sizeof(uint32_t);
v.bits = vt_int;
v.nval = num;
break;
}
case 0x11: {
uint64_t num;
if (fread_unlocked (&num, sizeof(uint64_t), 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= sizeof(uint64_t);
(*amt) += sizeof(uint64_t);
v.bits = vt_int;
v.nval = num;
break;
}
case 0x12: {
uint64_t num;
if (fread_unlocked (&num, sizeof(uint64_t), 1, file) < 1)
return ERROR_endoffile;
doclen[depth] -= sizeof(uint64_t);
(*amt) += sizeof(uint64_t);
v.bits = vt_int;
v.nval = num;
break;
}
}
incrRefCnt(v);
if (val[depth].type == vt_object)
replaceValue(lookup(val[depth], namestr[depth], true, 0), v);
else
vec_push(val[depth].aval->valuePtr, v);
}
return ERROR_endoffile;
}
typedef struct Builder {
struct Builder *next;
uint32_t length;
char body[500];
} build_t;
void build_append(uint32_t *size, build_t **rawDoc, build_t **doclast, void *str, uint32_t len) {
if (!*doclast) {
(*doclast) = js_alloc(sizeof(build_t), false);
(*rawDoc) = *doclast;
(*doclast)->next = NULL;
(*doclast)->length = 0;
}
if (500 - (*doclast)->length < len) {
(*doclast) = js_alloc(sizeof(build_t), false);
(*doclast)->next = NULL;
(*doclast)->length = 0;
}
memcpy ((*doclast)->body + (*doclast)->length, str, len);
(*doclast)->length += len;
*size += len;
}
void build_move(uint8_t type, build_t **rawDoc, build_t **doclast, uint32_t *doclen, value_t name) {
build_t *curr = rawDoc[1];
uint8_t zero = 0;
uint32_t len;
build_append(doclen, rawDoc, doclast, &type, 1);
if (name.type == vt_string) {
string_t *str = js_dbaddr(name, NULL);
build_append(doclen, rawDoc, doclast, str->val, str->len);
}
build_append(doclen, rawDoc, doclast, &zero, 1);
len = doclen[1] + 4 + 1;
build_append(doclen, rawDoc, doclast, &len, sizeof(uint32_t));
// splice the document chain
if (curr) {
if (doclast[0])
doclast[0]->next = curr;
doclast[0] = doclast[1];
doclen[0] += doclen[1];
}
build_append(doclen, rawDoc, doclast, &zero, 1);
}
// respond to a query request with an array of documents/values
// TODO: convert docs to value_t so database docs can be sent
Status bson_response (FILE *file, uint32_t request, uint32_t response, uint32_t flags, uint64_t cursorId, uint32_t opcode, uint32_t start, array_t *docs) {
uint32_t count = vec_cnt(docs->valuePtr);
uint32_t *length = NULL, size = 0;
build_t **result = NULL;
build_t *rawDoc[1024];
build_t *doclast[1024];
uint32_t doclen[1024];
value_t name[1024];
value_t obj[1024];
uint8_t doctype;
uint32_t idx[1024];
uint32_t i, depth;
char zero[1];
zero[0] = 0;
for (i = 0; i < count; i++) {
obj[0] = docs->valuePtr[i];
name[0].bits = vt_null;
rawDoc[0] = NULL;
doclast[0] = NULL;
doclen[0] = 0;
idx[0] = 0;
depth = 1;
rawDoc[depth] = NULL;
doclast[depth] = 0;
doclen[depth] = 0;
idx[depth] = 0;
while (depth > 0) {
value_t *item = &obj[depth - 1];
// find next value in parent object or array
if (item->type == vt_array) {
dbarray_t *dbaval = js_dbaddr(*item, NULL);
value_t *values = item->marshaled ? dbaval->valueArray : item->aval->valuePtr;
uint32_t max = item->marshaled ? dbaval->cnt : vec_cnt(values);
if (idx[depth] < max) {
obj[depth] = values[idx[depth]++];
name[depth].bits = vt_null;
} else {
if (--depth) {
build_move (0x04, rawDoc + depth, doclast + depth, doclen + depth, name[depth]);
}
continue;
}
} else if (item->type == vt_object) {
dbobject_t *dboval = js_dbaddr(*item, NULL);
pair_t *pairs = item->marshaled ? dboval->pairs : item->oval->pairsPtr;
uint32_t cnt = item->marshaled ? dboval->cnt : vec_cnt(pairs);
if (idx[depth] < cnt) {
name[depth] = pairs[idx[depth]].name;
obj[depth] = pairs[idx[depth]++].value;
} else {
if (--depth) {
build_move (0x03, rawDoc + depth, doclast + depth, doclen + depth, name[depth]);
}
continue;
}
} else {
depth--;
continue;
}
switch (obj[depth].type) {
case vt_string: {
string_t *str = js_dbaddr(obj[depth], NULL);
uint32_t len = str->len + 1;
doctype = 0x02;
build_append(doclen + depth, rawDoc + depth, doclast + depth, &doctype, 1);
if (name[depth].type == vt_string) {
string_t *namestr = js_dbaddr(name[depth], NULL);
build_append(doclen + depth, rawDoc + depth, doclast + depth, namestr->val, namestr->len);
}
build_append(doclen + depth, rawDoc + depth, doclast + depth, zero, 1);
build_append(doclen + depth, rawDoc + depth, doclast + depth, &len, sizeof(uint32_t));
build_append(doclen + depth, rawDoc + depth, doclast + depth, str->val, str->len);
build_append(doclen + depth, rawDoc + depth, doclast + depth, zero, 1);
break;
}
case vt_int: {
int len = sizeof(uint64_t);
doctype = 0x12;
build_append(doclen + depth, rawDoc + depth, doclast + depth, &doctype, 1);
if (depth && name[depth].type == vt_string) {
string_t *str = js_dbaddr(name[depth], NULL);
build_append(doclen + depth, rawDoc + depth, doclast + depth, str->val, str->len);
}
build_append(doclen + depth, rawDoc + depth, doclast + depth, zero, 1);
build_append(doclen + depth, rawDoc + depth, doclast + depth, &obj[depth].nval, len);
break;
}
case vt_dbl: {
doctype = 0x01;
build_append(doclen + depth, rawDoc + depth, doclast + depth, &doctype, 1);
if (depth && name[depth].type == vt_string) {
string_t *str = js_dbaddr(name[depth], NULL);
build_append(doclen + depth, rawDoc + depth, doclast + depth, str->val, str->len);
}
build_append(doclen + depth, rawDoc + depth, doclast + depth, zero, 1);
build_append(doclen + depth, rawDoc + depth, doclast + depth, &obj[depth].dbl, sizeof(double));
break;
}
case vt_array:
case vt_object: {
rawDoc[++depth] = NULL;
name[depth].bits = vt_null;
doclast[depth] = 0;
doclen[depth] = 0;
idx[depth] = 0;
break;
}
default:;
}
}
// allow for zero terminator and size of length
vec_push (result, rawDoc[1]);
vec_push (length, doclen[1] + 1 + 4);
size += doclen[1] + 1 + 4;
}
// emit response
size += 4 * sizeof(uint32_t);
if (opcode == 1)
size += 3 * sizeof(uint32_t) + sizeof(uint64_t);
else
size += 1 + sizeof(uint32_t);
if (fwrite_unlocked (&size, sizeof(uint32_t), 1, file) < 1)
return ERROR_tcperror;
if (fwrite_unlocked (&request, sizeof(uint32_t), 1, file) < 1)
return ERROR_tcperror;
if (fwrite_unlocked (&response, sizeof(uint32_t), 1, file) < 1)
return ERROR_tcperror;
if (fwrite_unlocked (&opcode, sizeof(uint32_t), 1, file) < 1)
return ERROR_tcperror;
if (opcode == 1) {
if (fwrite_unlocked (&flags, sizeof(uint32_t), 1, file) < 1)
return ERROR_tcperror;
if (fwrite_unlocked (&cursorId, sizeof(uint64_t), 1, file) < 1)
return ERROR_tcperror;
if (fwrite_unlocked (&start, sizeof(uint32_t), 1, file) < 1)
return ERROR_tcperror;
if (fwrite_unlocked (&count, sizeof(uint32_t), 1, file) < 1)
return ERROR_tcperror;
}
// write the documents
for (i = 0; i < count; i++) {
uint32_t amt = length[i];
build_t *next, *resp;
int wire = 0;
if (fwrite_unlocked (&amt, sizeof(uint32_t), 1, file) < 1)
return ERROR_tcperror;
wire += sizeof(uint32_t);
if ((resp = result[i])) do {
if (fwrite_unlocked (resp->body, resp->length, 1, file) < 1)
return ERROR_tcperror;
else
next = resp->next;
wire += resp->length;
js_free(resp);
} while ((resp = next));
if (fwrite_unlocked (zero, 1, 1, file) < 1)
return ERROR_tcperror;
wire += 1;
}
vec_free(result);
vec_free(length);
if (opcode == 2011) {
size = sizeof(uint32_t) + 1;
if (fwrite_unlocked (&size, sizeof(uint32_t), 1, file) < 1)
return ERROR_tcperror;
if (fwrite_unlocked (zero, 1, 1, file) < 1)
return ERROR_tcperror;
}
fflush(file);
return OK;
}