File: mini_bson.c

package info (click to toggle)
php-mongo 1.5.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,040 kB
  • ctags: 2,802
  • sloc: ansic: 17,632; xml: 2,195; php: 1,630; pascal: 330; makefile: 52; sh: 39
file content (534 lines) | stat: -rw-r--r-- 13,876 bytes parent folder | download
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/**
 *  Copyright 2009-2014 MongoDB, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "str.h"
#include "mini_bson.h"
#include "types.h"
#include "bson_helpers.h"
#include "connections.h"

#define MONGO_QUERY_FLAG_EMPTY    0x00
#define MONGO_QUERY_FLAG_SLAVE_OK 0x04

/* Creates a simple query header.
 *
 * The ns argument selects which namespace to use. If it's not set, we use
 * "admin.$cmd". */
static mcon_str *create_simple_header(mongo_connection *con, char *ns)
{
	struct mcon_str *str;

	mcon_str_ptr_init(str);

	mcon_serialize_int(str, 0); /* We need to fill this with the length */

	mcon_serialize_int(str, mongo_connection_get_reqid(con));
	mcon_serialize_int(str, 0); /* Response to */
	mcon_serialize_int(str, 2004); /* OP_QUERY */

	mcon_serialize_int(str, MONGO_QUERY_FLAG_SLAVE_OK); /* Flags */
	mcon_str_addl(str, ns ? ns : "admin.$cmd", ns ? strlen(ns) + 1 : 11, 0);
	mcon_serialize_int(str, 0); /* Number to skip */
	mcon_serialize_int(str, -1); /* Number to return, has to be -1 for admin commands */

	return str;
}

void bson_add_int32(mcon_str *str, char *fieldname, int32_t v)
{
	mcon_str_addl(str, "\x10", 1, 0);
	mcon_str_addl(str, fieldname, strlen(fieldname) + 1, 0);
	mcon_serialize_int32(str, v);
}

void bson_add_long(mcon_str *str, char *fieldname, int64_t v)
{
	mcon_str_addl(str, "\x12", 1, 0);
	mcon_str_addl(str, fieldname, strlen(fieldname) + 1, 0);
	mcon_serialize_int64(str, v);
}

void bson_add_stringl(mcon_str *str, char *fieldname, char *string, int len)
{
	mcon_str_addl(str, "\x02", 1, 0);
	mcon_str_addl(str, fieldname, strlen(fieldname)+1, 0);
	mcon_serialize_int(str, len);
	mcon_str_add(str, string, 0);
	mcon_str_addl(str, "", 1, 0); /* Trailing 0x00 */
}

void bson_add_string(mcon_str *str, char *fieldname, char *string)
{
	bson_add_stringl(str, fieldname, string, strlen(string) + 1);
}

mcon_str *bson_create_ping_packet(mongo_connection *con)
{
	struct mcon_str *str = create_simple_header(con, NULL);
	int    hdr;

	hdr = str->l;
	mcon_serialize_int(str, 0); /* We need to fill this with the length */
	bson_add_long(str, "ping", 1);
	mcon_str_addl(str, "", 1, 0); /* Trailing 0x00 */

	/* Set length */
	((int*) (&(str->d[hdr])))[0] = str->l - hdr;

	((int*) str->d)[0] = str->l;
	return str;
}

mcon_str *bson_create_ismaster_packet(mongo_connection *con)
{
	struct mcon_str *str = create_simple_header(con, NULL);
	int    hdr;

	hdr = str->l;
	mcon_serialize_int(str, 0); /* We need to fill this with the length */
	bson_add_long(str, "isMaster", 1);
	mcon_str_addl(str, "", 1, 0); /* Trailing 0x00 */

	/* Set length */
	((int*) (&(str->d[hdr])))[0] = str->l - hdr;

	((int*) str->d)[0] = str->l;
	return str;
}

mcon_str *bson_create_buildinfo_packet(mongo_connection *con)
{
	struct mcon_str *str = create_simple_header(con, NULL);
	int    hdr;

	hdr = str->l;
	mcon_serialize_int(str, 0); /* We need to fill this with the length */
	bson_add_long(str, "buildInfo", 1);
	mcon_str_addl(str, "", 1, 0); /* Trailing 0x00 */

	/* Set length */
	((int*) (&(str->d[hdr])))[0] = str->l - hdr;

	((int*) str->d)[0] = str->l;
	return str;
}

mcon_str *bson_create_rs_status_packet(mongo_connection *con)
{
	struct mcon_str *str = create_simple_header(con, NULL);
	int    hdr;

	hdr = str->l;
	mcon_serialize_int(str, 0); /* We need to fill this with the length */
	bson_add_long(str, "replSetGetStatus", 1);
	mcon_str_addl(str, "", 1, 0); /* Trailing 0x00 */

	/* Set length */
	((int*) (&(str->d[hdr])))[0] = str->l - hdr;

	((int*) str->d)[0] = str->l;
	return str;
}

mcon_str *bson_create_getnonce_packet(mongo_connection *con)
{
	struct mcon_str *str = create_simple_header(con, NULL);
	int    hdr;

	hdr = str->l;
	mcon_serialize_int(str, 0); /* We need to fill this with the length */
	bson_add_long(str, "getnonce", 1);
	mcon_str_addl(str, "", 1, 0); /* Trailing 0x00 */

	/* Set length */
	((int*) (&(str->d[hdr])))[0] = str->l - hdr;

	((int*) str->d)[0] = str->l;
	return str;
}

mcon_str *bson_create_authenticate_packet(mongo_connection *con, char *mechanism, char *database, char *username, char *nonce, char *key)
{
	struct mcon_str *str;
	char  *ns;
	int    hdr, length;

	/* We use the selected database to construct the namespace */
	length = strlen(database) + 5 + 1;
	ns = malloc(length);
	snprintf(ns, length, "%s.$cmd", database);
	str = create_simple_header(con, ns);
	free(ns);

	hdr = str->l;
	mcon_serialize_int(str, 0); /* We need to fill this with the length */
	bson_add_long(str, "authenticate", 1);
	bson_add_string(str, "user", username);

	/* MONGODB_X509 doesn't use nonce or key */
	if (nonce) {
		bson_add_string(str, "nonce", nonce);
	}
	if (key) {
		bson_add_string(str, "key", key);
	}
	if (mechanism) {
		bson_add_string(str, "mechanism", mechanism);
	}
	mcon_str_addl(str, "", 1, 0); /* Trailing 0x00 */

	/* Set length */
	((int*) (&(str->d[hdr])))[0] = str->l - hdr;

	((int*) str->d)[0] = str->l;
	return str;
}

/* { saslStart: 1, mechanism: String, payload: BinaryOrString, autoAuthorize : 1 } */
mcon_str *bson_create_saslstart_packet(mongo_connection *con, char *database, char *mechanism, char *payload, int payload_len)
{
	struct mcon_str *str;
	char  *ns;
	int    hdr, length;

	/* We use the $external database to construct the namespace */
	length = strlen(database) + 5 + 1;
	ns = malloc(length);
	snprintf(ns, length, "%s.$cmd", database);
	str = create_simple_header(con, ns);
	free(ns);

	hdr = str->l;
	mcon_serialize_int(str, 0); /* We need to fill this with the length */
	bson_add_long(str, "saslStart", 1);
	if (mechanism) {
		bson_add_string(str, "mechanism", mechanism);
		bson_add_stringl(str, "payload", (char *)payload, payload_len);
		bson_add_long(str, "autoAuthorize", 1);
	} else {
		bson_add_string(str, "mechanism", "What-Do-You-Support?");
		bson_add_string(str, "payload", "");
		bson_add_long(str, "autoAuthorize", 1);
	}
	mcon_str_addl(str, "", 1, 0); /* Trailing 0x00 */

	/* Set length */
	((int*) (&(str->d[hdr])))[0] = str->l - hdr;

	((int*) str->d)[0] = str->l;
	return str;
}

mcon_str *bson_create_saslcontinue_packet(mongo_connection *con, int32_t conversation_id, char *payload, int payload_len)
{
	struct mcon_str *str;
	char  *ns;
	int    hdr, length;

	/* We use the $external database to construct the namespace */
	length = 9 + 5 + 1;
	ns = malloc(length);
	snprintf(ns, length, "$external.$cmd");
	str = create_simple_header(con, ns);
	free(ns);

	hdr = str->l;
	mcon_serialize_int(str, 0); /* We need to fill this with the length */
	bson_add_long(str, "saslContinue", 1);

	bson_add_int32(str, "conversationId", conversation_id);
	bson_add_stringl(str, "payload", (char *)payload, payload_len);
	mcon_str_addl(str, "", 1, 0); /* Trailing 0x00 */

	/* Set length */
	((int*) (&(str->d[hdr])))[0] = str->l - hdr;

	((int*) str->d)[0] = str->l;
	return str;
}


/* Field reading functionality */
/* - helpers */
char *bson_skip_field_name(char *data)
{
	return strchr(data, '\0') + 1;
}

#define BSON_DOUBLE          0x01
#define BSON_STRING          0x02
#define BSON_DOCUMENT        0x03
#define BSON_ARRAY           0x04
#define BSON_BINARY          0x05
#define BSON_UNDEFINED       0x06
#define BSON_OBJECT_ID       0x07
#define BSON_BOOLEAN         0x08
#define BSON_DATETIME        0x09
#define BSON_NULL            0x0A
#define BSON_REGEXP          0x0B
#define BSON_DBPOINTER       0x0C
#define BSON_JAVASCRIPT      0x0D
#define BSON_SYMBOL          0x0E
#define BSON_JAVASCRIPT_WITH_SCOPE 0x0F
#define BSON_INT32           0x10
#define BSON_TIMESTAMP       0x11
#define BSON_INT64           0x12
#define BSON_MIN_KEY         0xFF
#define BSON_MAX_KEY         0x7F

char *bson_next(char *data)
{
	unsigned char type = data[0];
	int32_t       length;

	if (type == 0) {
		return NULL;
	}

	data = bson_skip_field_name(data + 1); /* Skip 1, because of the type in data[0] */
/*
element 	::= 	"\x01" e_name double 	Floating point
	| 	"\x02" e_name string 	UTF-8 string
	| 	"\x03" e_name document 	Embedded document
	| 	"\x04" e_name document 	Array
	| 	"\x05" e_name binary 	Binary data
	| 	"\x06" e_name 	Undefined — Deprecated
	| 	"\x07" e_name (byte*12) 	ObjectId
	| 	"\x08" e_name "\x00" 	Boolean "false"
	| 	"\x08" e_name "\x01" 	Boolean "true"
	| 	"\x09" e_name int64 	UTC datetime
	| 	"\x0A" e_name 	Null value
	| 	"\x0B" e_name cstring cstring 	Regular expression
	| 	"\x0C" e_name string (byte*12) 	DBPointer — Deprecated
	| 	"\x0D" e_name string 	JavaScript code
	| 	"\x0E" e_name string 	Symbol
	| 	"\x0F" e_name code_w_s 	JavaScript code w/ scope
	| 	"\x10" e_name int32 	32-bit Integer
	| 	"\x11" e_name int64 	Timestamp
	| 	"\x12" e_name int64 	64-bit integer
	| 	"\xFF" e_name 	Min key
	| 	"\x7F" e_name 	Max key
*/
	switch (type) {
		case BSON_DOUBLE:
			return data + sizeof(double);
		case BSON_STRING:
		case BSON_JAVASCRIPT:
		case BSON_SYMBOL:
			length = MONGO_32(*(int*)data);
			return data + sizeof(int32_t) + length;
		case BSON_DOCUMENT:
		case BSON_ARRAY:
			length = MONGO_32(*(int*)data);
			return data + length;
		case BSON_BINARY:
			length = MONGO_32(*(int*)data);
			return data + sizeof(int32_t) + 1 + length;
		case BSON_UNDEFINED:
		case BSON_NULL:
		case BSON_MIN_KEY:
		case BSON_MAX_KEY:
			return data;
		case BSON_OBJECT_ID:
			return data + 12;
		case BSON_BOOLEAN:
			return data + 1;
		case BSON_DATETIME:
		case BSON_TIMESTAMP:
		case BSON_INT64:
			return data + sizeof(int64_t);
		case BSON_REGEXP:
			return strchr(data, '\0') + 1;
		case BSON_DBPOINTER:
			length = MONGO_32(*(int*)data);
			return data + sizeof(int32_t) + length + 12;
		case BSON_JAVASCRIPT_WITH_SCOPE:
			exit(-3); /* TODO */
		case BSON_INT32:
			return data + sizeof(int32_t);
	}
	return NULL;
}

void *bson_get_current(char *data, char **field_name, int *type)
{
	*type = data[0];
	if (*type == 0) { /* We have reached the end */
		*field_name = NULL;
		return NULL;
	}
	*field_name = data + 1; /* Skip 1, because of the type in data[0] */
	data = bson_skip_field_name(data); /* Skip fieldname to get to data */
	return data;
}

void *bson_find_field(char *data, char *field_name, int type)
{
	void *return_data;
	char *ptr = data;
	char *read_field = NULL;
	int   read_type;

	return_data = bson_get_current(ptr, &read_field, &read_type);
	while (read_field && (strcmp(read_field, field_name) != 0 || read_type != type)) {
		ptr = bson_next(ptr);
		if (ptr == NULL) {
			read_field = NULL;
			break;
		}
		return_data = bson_get_current(ptr, &read_field, &read_type);
	}
	if (read_field && strcmp(read_field, field_name) == 0 && read_type == type) {
		return return_data;
	}
	return NULL;
}

/* - Public API */
int bson_find_field_as_array(char *buffer, char *field, char **data)
{
	char* tmp = bson_find_field(buffer, field, BSON_ARRAY);

	if (tmp) {
		*data = tmp + 4; /* int32 for length */
		return 1;
	}
	return 0;
}

int bson_find_field_as_document(char *buffer, char *field, char **data)
{
	char* tmp = bson_find_field(buffer, field, BSON_DOCUMENT);

	if (tmp) {
		*data = tmp + 4; /* int32 for length */
		return 1;
	}
	return 0;
}

int bson_find_field_as_double(char *buffer, char *field, double *data)
{
	char *tmp = (char*) bson_find_field(buffer, field, BSON_DOUBLE);

	if (tmp) {
		*data = ((double*)tmp)[0];
		return 1;
	}
	return 0;
}

int bson_find_field_as_bool(char *buffer, char *field, unsigned char *data)
{
	char *tmp = (char*) bson_find_field(buffer, field, BSON_BOOLEAN);

	if (tmp) {
		*data = tmp[0];
		return 1;
	}
	return 0;
}

int bson_find_field_as_int32(char *buffer, char *field, int32_t *data)
{
	char *tmp = (char*) bson_find_field(buffer, field, BSON_INT32);

	if (tmp) {
		*data = ((int32_t*)tmp)[0];
		return 1;
	}
	return 0;
}

int bson_find_field_as_int64(char *buffer, char *field, int64_t *data)
{
	char *tmp = (char*) bson_find_field(buffer, field, BSON_INT64);

	if (tmp) {
		*data = ((int64_t*)tmp)[0];
		return 1;
	}
	return 0;
}

int bson_find_field_as_stringl(char *buffer, char *field, char **data, int32_t *length, int duplicate)
{
	char* tmp = bson_find_field(buffer, field, BSON_STRING);

	if (tmp) {
		*length = ((int32_t*)tmp)[0];
		*data = duplicate ? strdup(tmp + 4) : tmp + 4; /* int32 for length */
		return 1;
	}
	return 0;
}

int bson_find_field_as_string(char *buffer, char *field, char **data)
{
	char* tmp = bson_find_field(buffer, field, BSON_STRING);

	if (tmp) {
		*data = tmp + 4; /* int32 for length */
		return 1;
	}
	return 0;
}

int bson_array_find_next_string(char **buffer, char **field, char **data)
{
	char *read_field;
	int   read_type;
	void *return_data;

	return_data = bson_get_current(*buffer, &read_field, &read_type);
	if (read_type == BSON_STRING) {
		*data = (char*) return_data + 4;
		if (field) {
			*field = strdup(read_field);
		}
	}
	*buffer = bson_next(*buffer);
	return *buffer == NULL ? 0 : 1;
}

int bson_array_find_next_int32(char **buffer, char **field, int32_t *data)
{
	char *read_field;
	int   read_type;
	void *return_data;

	return_data = bson_get_current(*buffer, &read_field, &read_type);
	if (read_type == BSON_INT32) {
		*data = ((int32_t*) return_data)[0];
		if (field) {
			*field = strdup(read_field);
		}
	}
	*buffer = bson_next(*buffer);
	return *buffer == NULL ? 0 : 1;
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: fdm=marker
 * vim: noet sw=4 ts=4
 */