Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
address review
Browse files Browse the repository at this point in the history
blink1073 committed Sep 5, 2024
1 parent 5ff644f commit 72e2a1e
Showing 61 changed files with 2,166 additions and 4,365 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -130,3 +130,6 @@ ENV/

# .idea
.idea/**

# Local checkout of mongo-c-driver
mongo-c-driver/
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ Changelog

0.5.0
`````
Version 0.5.0 updates python-bsonjs's vendored copy of libbson to 1.27.6.
For a detailed breakdown of what changed in each version of libbson see its changelog:
https://github.com/mongodb/mongo-c-driver/blob/1.27.6/src/libbson/NEWS
http://mongoc.org/libbson/1.27.6/
62 changes: 20 additions & 42 deletions bsonjs/bson/bcon.c
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
#include <stdio.h>

#include "bcon.h"
#include "bson-config.h"
#include <bson/bson-config.h>

/* These stack manipulation macros are used to manage append recursion in
* bcon_append_ctx_va(). They take care of some awkward dereference rules (the
@@ -31,11 +31,9 @@

#define STACK_ELE(_delta, _name) (ctx->stack[(_delta) + ctx->n]._name)

#define STACK_BSON(_delta) \
(((_delta) + ctx->n) == 0 ? bson : &STACK_ELE (_delta, bson))
#define STACK_BSON(_delta) (((_delta) + ctx->n) == 0 ? bson : &STACK_ELE (_delta, bson))

#define STACK_ITER(_delta) \
(((_delta) + ctx->n) == 0 ? &root_iter : &STACK_ELE (_delta, iter))
#define STACK_ITER(_delta) (((_delta) + ctx->n) == 0 ? &root_iter : &STACK_ELE (_delta, iter))

#define STACK_BSON_PARENT STACK_BSON (-1)
#define STACK_BSON_CHILD STACK_BSON (0)
@@ -205,10 +203,7 @@ _noop (void)
/* appends val to the passed bson object. Meant to be a super simple dispatch
* table */
static void
_bcon_append_single (bson_t *bson,
bcon_type_t type,
const char *key,
bcon_append_t *val)
_bcon_append_single (bson_t *bson, bcon_type_t type, const char *key, bcon_append_t *val)
{
switch ((int) type) {
case BCON_TYPE_UTF8:
@@ -218,8 +213,7 @@ _bcon_append_single (bson_t *bson,
BSON_ASSERT (bson_append_double (bson, key, -1, val->DOUBLE));
break;
case BCON_TYPE_BIN: {
BSON_ASSERT (bson_append_binary (
bson, key, -1, val->BIN.subtype, val->BIN.binary, val->BIN.length));
BSON_ASSERT (bson_append_binary (bson, key, -1, val->BIN.subtype, val->BIN.binary, val->BIN.length));
break;
}
case BCON_TYPE_UNDEFINED:
@@ -238,13 +232,11 @@ _bcon_append_single (bson_t *bson,
BSON_ASSERT (bson_append_null (bson, key, -1));
break;
case BCON_TYPE_REGEX: {
BSON_ASSERT (
bson_append_regex (bson, key, -1, val->REGEX.regex, val->REGEX.flags));
BSON_ASSERT (bson_append_regex (bson, key, -1, val->REGEX.regex, val->REGEX.flags));
break;
}
case BCON_TYPE_DBPOINTER: {
BSON_ASSERT (bson_append_dbpointer (
bson, key, -1, val->DBPOINTER.collection, val->DBPOINTER.oid));
BSON_ASSERT (bson_append_dbpointer (bson, key, -1, val->DBPOINTER.collection, val->DBPOINTER.oid));
break;
}
case BCON_TYPE_CODE:
@@ -254,15 +246,13 @@ _bcon_append_single (bson_t *bson,
BSON_ASSERT (bson_append_symbol (bson, key, -1, val->SYMBOL, -1));
break;
case BCON_TYPE_CODEWSCOPE:
BSON_ASSERT (bson_append_code_with_scope (
bson, key, -1, val->CODEWSCOPE.js, val->CODEWSCOPE.scope));
BSON_ASSERT (bson_append_code_with_scope (bson, key, -1, val->CODEWSCOPE.js, val->CODEWSCOPE.scope));
break;
case BCON_TYPE_INT32:
BSON_ASSERT (bson_append_int32 (bson, key, -1, val->INT32));
break;
case BCON_TYPE_TIMESTAMP: {
BSON_ASSERT (bson_append_timestamp (
bson, key, -1, val->TIMESTAMP.timestamp, val->TIMESTAMP.increment));
BSON_ASSERT (bson_append_timestamp (bson, key, -1, val->TIMESTAMP.timestamp, val->TIMESTAMP.increment));
break;
}
case BCON_TYPE_INT64:
@@ -315,9 +305,7 @@ _bcon_append_single (bson_t *bson,
* procedural verification (if a parameter could have multiple types).
* */
static bool
_bcon_extract_single (const bson_iter_t *iter,
bcon_type_t type,
bcon_extract_t *val)
_bcon_extract_single (const bson_iter_t *iter, bcon_type_t type, bcon_extract_t *val)
{
switch ((int) type) {
case BCON_TYPE_UTF8:
@@ -330,8 +318,7 @@ _bcon_extract_single (const bson_iter_t *iter,
break;
case BCON_TYPE_BIN:
CHECK_TYPE (BSON_TYPE_BINARY);
bson_iter_binary (
iter, val->BIN.subtype, val->BIN.length, val->BIN.binary);
bson_iter_binary (iter, val->BIN.subtype, val->BIN.length, val->BIN.binary);
break;
case BCON_TYPE_UNDEFINED:
CHECK_TYPE (BSON_TYPE_UNDEFINED);
@@ -358,8 +345,7 @@ _bcon_extract_single (const bson_iter_t *iter,
break;
case BCON_TYPE_DBPOINTER:
CHECK_TYPE (BSON_TYPE_DBPOINTER);
bson_iter_dbpointer (
iter, NULL, val->DBPOINTER.collection, val->DBPOINTER.oid);
bson_iter_dbpointer (iter, NULL, val->DBPOINTER.collection, val->DBPOINTER.oid);
break;
case BCON_TYPE_CODE:
CHECK_TYPE (BSON_TYPE_CODE);
@@ -386,8 +372,7 @@ _bcon_extract_single (const bson_iter_t *iter,
break;
case BCON_TYPE_TIMESTAMP:
CHECK_TYPE (BSON_TYPE_TIMESTAMP);
bson_iter_timestamp (
iter, val->TIMESTAMP.timestamp, val->TIMESTAMP.increment);
bson_iter_timestamp (iter, val->TIMESTAMP.timestamp, val->TIMESTAMP.increment);
break;
case BCON_TYPE_INT64:
CHECK_TYPE (BSON_TYPE_INT64);
@@ -772,8 +757,7 @@ bcon_append_ctx_va (bson_t *bson, bcon_append_ctx_t *ctx, va_list *ap)
}

if (type == BCON_TYPE_DOC_END) {
STACK_POP_DOC (
bson_append_document_end (STACK_BSON_PARENT, STACK_BSON_CHILD));
STACK_POP_DOC (bson_append_document_end (STACK_BSON_PARENT, STACK_BSON_CHILD));
continue;
}

@@ -797,20 +781,16 @@ bcon_append_ctx_va (bson_t *bson, bcon_append_ctx_t *ctx, va_list *ap)

break;
case BCON_TYPE_DOC_START:
STACK_PUSH_DOC (bson_append_document_begin (
STACK_BSON_PARENT, key, -1, STACK_BSON_CHILD));
STACK_PUSH_DOC (bson_append_document_begin (STACK_BSON_PARENT, key, -1, STACK_BSON_CHILD));
break;
case BCON_TYPE_DOC_END:
STACK_POP_DOC (
bson_append_document_end (STACK_BSON_PARENT, STACK_BSON_CHILD));
STACK_POP_DOC (bson_append_document_end (STACK_BSON_PARENT, STACK_BSON_CHILD));
break;
case BCON_TYPE_ARRAY_START:
STACK_PUSH_ARRAY (bson_append_array_begin (
STACK_BSON_PARENT, key, -1, STACK_BSON_CHILD));
STACK_PUSH_ARRAY (bson_append_array_begin (STACK_BSON_PARENT, key, -1, STACK_BSON_CHILD));
break;
case BCON_TYPE_ARRAY_END:
STACK_POP_ARRAY (
bson_append_array_end (STACK_BSON_PARENT, STACK_BSON_CHILD));
STACK_POP_ARRAY (bson_append_array_end (STACK_BSON_PARENT, STACK_BSON_CHILD));
break;
default:
_bcon_append_single (STACK_BSON_CHILD, type, key, &u);
@@ -897,17 +877,15 @@ bcon_extract_ctx_va (bson_t *bson, bcon_extract_ctx_t *ctx, va_list *ap)
return false;
}

STACK_PUSH_DOC (
bson_iter_recurse (&current_iter, STACK_ITER_CHILD));
STACK_PUSH_DOC (bson_iter_recurse (&current_iter, STACK_ITER_CHILD));
break;
case BCON_TYPE_ARRAY_START:

if (bson_iter_type (&current_iter) != BSON_TYPE_ARRAY) {
return false;
}

STACK_PUSH_ARRAY (
bson_iter_recurse (&current_iter, STACK_ITER_CHILD));
STACK_PUSH_ARRAY (bson_iter_recurse (&current_iter, STACK_ITER_CHILD));
break;
default:

160 changes: 55 additions & 105 deletions bsonjs/bson/bcon.h
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
* @brief BCON (BSON C Object Notation) Declarations
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>

/* Copyright 2009-2013 MongoDB, Inc.
*
@@ -66,109 +66,69 @@ BCON_ENSURE_DECLARE (bson_type, bson_type_t)
BCON_ENSURE_DECLARE (bson_iter_ptr, bson_iter_t *)
BCON_ENSURE_DECLARE (const_bson_iter_ptr, const bson_iter_t *)

#define BCON_UTF8(_val) \
BCON_MAGIC, BCON_TYPE_UTF8, BCON_ENSURE (const_char_ptr, (_val))
#define BCON_DOUBLE(_val) \
BCON_MAGIC, BCON_TYPE_DOUBLE, BCON_ENSURE (double, (_val))
#define BCON_DOCUMENT(_val) \
BCON_MAGIC, BCON_TYPE_DOCUMENT, BCON_ENSURE (const_bson_ptr, (_val))
#define BCON_ARRAY(_val) \
BCON_MAGIC, BCON_TYPE_ARRAY, BCON_ENSURE (const_bson_ptr, (_val))
#define BCON_BIN(_subtype, _binary, _length) \
BCON_MAGIC, BCON_TYPE_BIN, BCON_ENSURE (subtype, (_subtype)), \
BCON_ENSURE (const_uint8_ptr, (_binary)), \
#define BCON_UTF8(_val) BCON_MAGIC, BCON_TYPE_UTF8, BCON_ENSURE (const_char_ptr, (_val))
#define BCON_DOUBLE(_val) BCON_MAGIC, BCON_TYPE_DOUBLE, BCON_ENSURE (double, (_val))
#define BCON_DOCUMENT(_val) BCON_MAGIC, BCON_TYPE_DOCUMENT, BCON_ENSURE (const_bson_ptr, (_val))
#define BCON_ARRAY(_val) BCON_MAGIC, BCON_TYPE_ARRAY, BCON_ENSURE (const_bson_ptr, (_val))
#define BCON_BIN(_subtype, _binary, _length) \
BCON_MAGIC, BCON_TYPE_BIN, BCON_ENSURE (subtype, (_subtype)), BCON_ENSURE (const_uint8_ptr, (_binary)), \
BCON_ENSURE (uint32, (_length))
#define BCON_UNDEFINED BCON_MAGIC, BCON_TYPE_UNDEFINED
#define BCON_OID(_val) \
BCON_MAGIC, BCON_TYPE_OID, BCON_ENSURE (const_oid_ptr, (_val))
#define BCON_OID(_val) BCON_MAGIC, BCON_TYPE_OID, BCON_ENSURE (const_oid_ptr, (_val))
#define BCON_BOOL(_val) BCON_MAGIC, BCON_TYPE_BOOL, BCON_ENSURE (bool, (_val))
#define BCON_DATE_TIME(_val) \
BCON_MAGIC, BCON_TYPE_DATE_TIME, BCON_ENSURE (int64, (_val))
#define BCON_DATE_TIME(_val) BCON_MAGIC, BCON_TYPE_DATE_TIME, BCON_ENSURE (int64, (_val))
#define BCON_NULL BCON_MAGIC, BCON_TYPE_NULL
#define BCON_REGEX(_regex, _flags) \
BCON_MAGIC, BCON_TYPE_REGEX, BCON_ENSURE (const_char_ptr, (_regex)), \
BCON_ENSURE (const_char_ptr, (_flags))
#define BCON_DBPOINTER(_collection, _oid) \
BCON_MAGIC, BCON_TYPE_DBPOINTER, \
BCON_ENSURE (const_char_ptr, (_collection)), \
BCON_ENSURE (const_oid_ptr, (_oid))
#define BCON_CODE(_val) \
BCON_MAGIC, BCON_TYPE_CODE, BCON_ENSURE (const_char_ptr, (_val))
#define BCON_SYMBOL(_val) \
BCON_MAGIC, BCON_TYPE_SYMBOL, BCON_ENSURE (const_char_ptr, (_val))
#define BCON_CODEWSCOPE(_js, _scope) \
BCON_MAGIC, BCON_TYPE_CODEWSCOPE, BCON_ENSURE (const_char_ptr, (_js)), \
BCON_ENSURE (const_bson_ptr, (_scope))
#define BCON_INT32(_val) \
BCON_MAGIC, BCON_TYPE_INT32, BCON_ENSURE (int32, (_val))
#define BCON_TIMESTAMP(_timestamp, _increment) \
BCON_MAGIC, BCON_TYPE_TIMESTAMP, BCON_ENSURE (int32, (_timestamp)), \
BCON_ENSURE (int32, (_increment))
#define BCON_INT64(_val) \
BCON_MAGIC, BCON_TYPE_INT64, BCON_ENSURE (int64, (_val))
#define BCON_DECIMAL128(_val) \
BCON_MAGIC, BCON_TYPE_DECIMAL128, BCON_ENSURE (const_decimal128_ptr, (_val))
#define BCON_REGEX(_regex, _flags) \
BCON_MAGIC, BCON_TYPE_REGEX, BCON_ENSURE (const_char_ptr, (_regex)), BCON_ENSURE (const_char_ptr, (_flags))
#define BCON_DBPOINTER(_collection, _oid) \
BCON_MAGIC, BCON_TYPE_DBPOINTER, BCON_ENSURE (const_char_ptr, (_collection)), BCON_ENSURE (const_oid_ptr, (_oid))
#define BCON_CODE(_val) BCON_MAGIC, BCON_TYPE_CODE, BCON_ENSURE (const_char_ptr, (_val))
#define BCON_SYMBOL(_val) BCON_MAGIC, BCON_TYPE_SYMBOL, BCON_ENSURE (const_char_ptr, (_val))
#define BCON_CODEWSCOPE(_js, _scope) \
BCON_MAGIC, BCON_TYPE_CODEWSCOPE, BCON_ENSURE (const_char_ptr, (_js)), BCON_ENSURE (const_bson_ptr, (_scope))
#define BCON_INT32(_val) BCON_MAGIC, BCON_TYPE_INT32, BCON_ENSURE (int32, (_val))
#define BCON_TIMESTAMP(_timestamp, _increment) \
BCON_MAGIC, BCON_TYPE_TIMESTAMP, BCON_ENSURE (int32, (_timestamp)), BCON_ENSURE (int32, (_increment))
#define BCON_INT64(_val) BCON_MAGIC, BCON_TYPE_INT64, BCON_ENSURE (int64, (_val))
#define BCON_DECIMAL128(_val) BCON_MAGIC, BCON_TYPE_DECIMAL128, BCON_ENSURE (const_decimal128_ptr, (_val))
#define BCON_MAXKEY BCON_MAGIC, BCON_TYPE_MAXKEY
#define BCON_MINKEY BCON_MAGIC, BCON_TYPE_MINKEY
#define BCON(_val) \
BCON_MAGIC, BCON_TYPE_BCON, BCON_ENSURE (const_bson_ptr, (_val))
#define BCON_ITER(_val) \
BCON_MAGIC, BCON_TYPE_ITER, BCON_ENSURE (const_bson_iter_ptr, (_val))

#define BCONE_UTF8(_val) \
BCONE_MAGIC, BCON_TYPE_UTF8, BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_val))
#define BCONE_DOUBLE(_val) \
BCONE_MAGIC, BCON_TYPE_DOUBLE, BCON_ENSURE_STORAGE (double_ptr, (_val))
#define BCONE_DOCUMENT(_val) \
BCONE_MAGIC, BCON_TYPE_DOCUMENT, BCON_ENSURE_STORAGE (bson_ptr, (_val))
#define BCONE_ARRAY(_val) \
BCONE_MAGIC, BCON_TYPE_ARRAY, BCON_ENSURE_STORAGE (bson_ptr, (_val))
#define BCON(_val) BCON_MAGIC, BCON_TYPE_BCON, BCON_ENSURE (const_bson_ptr, (_val))
#define BCON_ITER(_val) BCON_MAGIC, BCON_TYPE_ITER, BCON_ENSURE (const_bson_iter_ptr, (_val))

#define BCONE_UTF8(_val) BCONE_MAGIC, BCON_TYPE_UTF8, BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_val))
#define BCONE_DOUBLE(_val) BCONE_MAGIC, BCON_TYPE_DOUBLE, BCON_ENSURE_STORAGE (double_ptr, (_val))
#define BCONE_DOCUMENT(_val) BCONE_MAGIC, BCON_TYPE_DOCUMENT, BCON_ENSURE_STORAGE (bson_ptr, (_val))
#define BCONE_ARRAY(_val) BCONE_MAGIC, BCON_TYPE_ARRAY, BCON_ENSURE_STORAGE (bson_ptr, (_val))
#define BCONE_BIN(subtype, binary, length) \
BCONE_MAGIC, BCON_TYPE_BIN, BCON_ENSURE_STORAGE (subtype_ptr, (subtype)), \
BCON_ENSURE_STORAGE (const_uint8_ptr_ptr, (binary)), \
BCON_ENSURE_STORAGE (uint32_ptr, (length))
BCON_ENSURE_STORAGE (const_uint8_ptr_ptr, (binary)), BCON_ENSURE_STORAGE (uint32_ptr, (length))
#define BCONE_UNDEFINED BCONE_MAGIC, BCON_TYPE_UNDEFINED
#define BCONE_OID(_val) \
BCONE_MAGIC, BCON_TYPE_OID, BCON_ENSURE_STORAGE (const_oid_ptr_ptr, (_val))
#define BCONE_BOOL(_val) \
BCONE_MAGIC, BCON_TYPE_BOOL, BCON_ENSURE_STORAGE (bool_ptr, (_val))
#define BCONE_DATE_TIME(_val) \
BCONE_MAGIC, BCON_TYPE_DATE_TIME, BCON_ENSURE_STORAGE (int64_ptr, (_val))
#define BCONE_OID(_val) BCONE_MAGIC, BCON_TYPE_OID, BCON_ENSURE_STORAGE (const_oid_ptr_ptr, (_val))
#define BCONE_BOOL(_val) BCONE_MAGIC, BCON_TYPE_BOOL, BCON_ENSURE_STORAGE (bool_ptr, (_val))
#define BCONE_DATE_TIME(_val) BCONE_MAGIC, BCON_TYPE_DATE_TIME, BCON_ENSURE_STORAGE (int64_ptr, (_val))
#define BCONE_NULL BCONE_MAGIC, BCON_TYPE_NULL
#define BCONE_REGEX(_regex, _flags) \
BCONE_MAGIC, BCON_TYPE_REGEX, \
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_regex)), \
#define BCONE_REGEX(_regex, _flags) \
BCONE_MAGIC, BCON_TYPE_REGEX, BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_regex)), \
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_flags))
#define BCONE_DBPOINTER(_collection, _oid) \
BCONE_MAGIC, BCON_TYPE_DBPOINTER, \
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_collection)), \
#define BCONE_DBPOINTER(_collection, _oid) \
BCONE_MAGIC, BCON_TYPE_DBPOINTER, BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_collection)), \
BCON_ENSURE_STORAGE (const_oid_ptr_ptr, (_oid))
#define BCONE_CODE(_val) \
BCONE_MAGIC, BCON_TYPE_CODE, BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_val))
#define BCONE_SYMBOL(_val) \
BCONE_MAGIC, BCON_TYPE_SYMBOL, \
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_val))
#define BCONE_CODEWSCOPE(_js, _scope) \
BCONE_MAGIC, BCON_TYPE_CODEWSCOPE, \
BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_js)), \
#define BCONE_CODE(_val) BCONE_MAGIC, BCON_TYPE_CODE, BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_val))
#define BCONE_SYMBOL(_val) BCONE_MAGIC, BCON_TYPE_SYMBOL, BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_val))
#define BCONE_CODEWSCOPE(_js, _scope) \
BCONE_MAGIC, BCON_TYPE_CODEWSCOPE, BCON_ENSURE_STORAGE (const_char_ptr_ptr, (_js)), \
BCON_ENSURE_STORAGE (bson_ptr, (_scope))
#define BCONE_INT32(_val) \
BCONE_MAGIC, BCON_TYPE_INT32, BCON_ENSURE_STORAGE (int32_ptr, (_val))
#define BCONE_TIMESTAMP(_timestamp, _increment) \
BCONE_MAGIC, BCON_TYPE_TIMESTAMP, \
BCON_ENSURE_STORAGE (int32_ptr, (_timestamp)), \
#define BCONE_INT32(_val) BCONE_MAGIC, BCON_TYPE_INT32, BCON_ENSURE_STORAGE (int32_ptr, (_val))
#define BCONE_TIMESTAMP(_timestamp, _increment) \
BCONE_MAGIC, BCON_TYPE_TIMESTAMP, BCON_ENSURE_STORAGE (int32_ptr, (_timestamp)), \
BCON_ENSURE_STORAGE (int32_ptr, (_increment))
#define BCONE_INT64(_val) \
BCONE_MAGIC, BCON_TYPE_INT64, BCON_ENSURE_STORAGE (int64_ptr, (_val))
#define BCONE_DECIMAL128(_val) \
BCONE_MAGIC, BCON_TYPE_DECIMAL128, \
BCON_ENSURE_STORAGE (const_decimal128_ptr, (_val))
#define BCONE_INT64(_val) BCONE_MAGIC, BCON_TYPE_INT64, BCON_ENSURE_STORAGE (int64_ptr, (_val))
#define BCONE_DECIMAL128(_val) BCONE_MAGIC, BCON_TYPE_DECIMAL128, BCON_ENSURE_STORAGE (const_decimal128_ptr, (_val))
#define BCONE_MAXKEY BCONE_MAGIC, BCON_TYPE_MAXKEY
#define BCONE_MINKEY BCONE_MAGIC, BCON_TYPE_MINKEY
#define BCONE_SKIP(_val) \
BCONE_MAGIC, BCON_TYPE_SKIP, BCON_ENSURE (bson_type, (_val))
#define BCONE_ITER(_val) \
BCONE_MAGIC, BCON_TYPE_ITER, BCON_ENSURE_STORAGE (bson_iter_ptr, (_val))
#define BCONE_SKIP(_val) BCONE_MAGIC, BCON_TYPE_SKIP, BCON_ENSURE (bson_type, (_val))
#define BCONE_ITER(_val) BCONE_MAGIC, BCON_TYPE_ITER, BCON_ENSURE_STORAGE (bson_iter_ptr, (_val))

#define BCON_MAGIC bson_bcon_magic ()
#define BCONE_MAGIC bson_bcone_magic ()
@@ -232,9 +192,7 @@ typedef struct _bcon_extract_ctx_t {
BSON_EXPORT (void)
bcon_append (bson_t *bson, ...) BSON_GNUC_NULL_TERMINATED;
BSON_EXPORT (void)
bcon_append_ctx (bson_t *bson,
bcon_append_ctx_t *ctx,
...) BSON_GNUC_NULL_TERMINATED;
bcon_append_ctx (bson_t *bson, bcon_append_ctx_t *ctx, ...) BSON_GNUC_NULL_TERMINATED;
BSON_EXPORT (void)
bcon_append_ctx_va (bson_t *bson, bcon_append_ctx_t *ctx, va_list *va);
BSON_EXPORT (void)
@@ -244,9 +202,7 @@ BSON_EXPORT (void)
bcon_extract_ctx_init (bcon_extract_ctx_t *ctx);

BSON_EXPORT (void)
bcon_extract_ctx (bson_t *bson,
bcon_extract_ctx_t *ctx,
...) BSON_GNUC_NULL_TERMINATED;
bcon_extract_ctx (bson_t *bson, bcon_extract_ctx_t *ctx, ...) BSON_GNUC_NULL_TERMINATED;

BSON_EXPORT (bool)
bcon_extract_ctx_va (bson_t *bson, bcon_extract_ctx_t *ctx, va_list *ap);
@@ -255,9 +211,7 @@ BSON_EXPORT (bool)
bcon_extract (bson_t *bson, ...) BSON_GNUC_NULL_TERMINATED;

BSON_EXPORT (bool)
bcon_extract_va (bson_t *bson,
bcon_extract_ctx_t *ctx,
...) BSON_GNUC_NULL_TERMINATED;
bcon_extract_va (bson_t *bson, bcon_extract_ctx_t *ctx, ...) BSON_GNUC_NULL_TERMINATED;

BSON_EXPORT (bson_t *)
bcon_new (void *unused, ...) BSON_GNUC_NULL_TERMINATED;
@@ -270,16 +224,12 @@ bcon_new (void *unused, ...) BSON_GNUC_NULL_TERMINATED;
* integer type then you need to add an explicit cast."
* Case in point: GCC on Solaris (at least)
*/
#define BCON_APPEND(_bson, ...) \
bcon_append ((_bson), __VA_ARGS__, (void *) NULL)
#define BCON_APPEND_CTX(_bson, _ctx, ...) \
bcon_append_ctx ((_bson), (_ctx), __VA_ARGS__, (void *) NULL)
#define BCON_APPEND(_bson, ...) bcon_append ((_bson), __VA_ARGS__, (void *) NULL)
#define BCON_APPEND_CTX(_bson, _ctx, ...) bcon_append_ctx ((_bson), (_ctx), __VA_ARGS__, (void *) NULL)

#define BCON_EXTRACT(_bson, ...) \
bcon_extract ((_bson), __VA_ARGS__, (void *) NULL)
#define BCON_EXTRACT(_bson, ...) bcon_extract ((_bson), __VA_ARGS__, (void *) NULL)

#define BCON_EXTRACT_CTX(_bson, _ctx, ...) \
bcon_extract ((_bson), (_ctx), __VA_ARGS__, (void *) NULL)
#define BCON_EXTRACT_CTX(_bson, _ctx, ...) bcon_extract ((_bson), (_ctx), __VA_ARGS__, (void *) NULL)

#define BCON_NEW(...) bcon_new (NULL, __VA_ARGS__, (void *) NULL)

53 changes: 16 additions & 37 deletions bsonjs/bson/bson-atomic.c
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
*/


#include "bson-atomic.h"
#include <bson/bson-atomic.h>

#ifdef BSON_OS_UNIX
/* For sched_yield() */
@@ -25,7 +25,7 @@
int32_t
bson_atomic_int_add (volatile int32_t *p, int32_t n)
{
return n + bson_atomic_int32_fetch_add (p, n, bson_memory_order_seq_cst);
return n + bson_atomic_int32_fetch_add ((DECL_ATOMIC_INTEGRAL_INT32 *) p, n, bson_memory_order_seq_cst);
}

int64_t
@@ -57,38 +57,32 @@ static void
_lock_emul_atomic (void)
{
int i;
if (bson_atomic_int8_compare_exchange_weak (
&gEmulAtomicLock, 0, 1, bson_memory_order_acquire) == 0) {
if (bson_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, bson_memory_order_acquire) == 0) {
/* Successfully took the spinlock */
return;
}
/* Failed. Try taking ten more times, then begin sleeping. */
for (i = 0; i < 10; ++i) {
if (bson_atomic_int8_compare_exchange_weak (
&gEmulAtomicLock, 0, 1, bson_memory_order_acquire) == 0) {
if (bson_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, bson_memory_order_acquire) == 0) {
/* Succeeded in taking the lock */
return;
}
}
/* Still don't have the lock. Spin and yield */
while (bson_atomic_int8_compare_exchange_weak (
&gEmulAtomicLock, 0, 1, bson_memory_order_acquire) != 0) {
while (bson_atomic_int8_compare_exchange_weak (&gEmulAtomicLock, 0, 1, bson_memory_order_acquire) != 0) {
bson_thrd_yield ();
}
}

static void
_unlock_emul_atomic (void)
{
int64_t rv = bson_atomic_int8_exchange (
&gEmulAtomicLock, 0, bson_memory_order_release);
int64_t rv = bson_atomic_int8_exchange (&gEmulAtomicLock, 0, bson_memory_order_release);
BSON_ASSERT (rv == 1 && "Released atomic lock while not holding it");
}

int64_t
_bson_emul_atomic_int64_fetch_add (volatile int64_t *p,
int64_t n,
enum bson_memory_order _unused)
_bson_emul_atomic_int64_fetch_add (volatile int64_t *p, int64_t n, enum bson_memory_order _unused)
{
int64_t ret;

@@ -102,9 +96,7 @@ _bson_emul_atomic_int64_fetch_add (volatile int64_t *p,
}

int64_t
_bson_emul_atomic_int64_exchange (volatile int64_t *p,
int64_t n,
enum bson_memory_order _unused)
_bson_emul_atomic_int64_exchange (volatile int64_t *p, int64_t n, enum bson_memory_order _unused)
{
int64_t ret;

@@ -143,15 +135,12 @@ _bson_emul_atomic_int64_compare_exchange_weak (volatile int64_t *p,
enum bson_memory_order order)
{
/* We're emulating. We can't do a weak version. */
return _bson_emul_atomic_int64_compare_exchange_strong (
p, expect_value, new_value, order);
return _bson_emul_atomic_int64_compare_exchange_strong (p, expect_value, new_value, order);
}


int32_t
_bson_emul_atomic_int32_fetch_add (volatile int32_t *p,
int32_t n,
enum bson_memory_order _unused)
_bson_emul_atomic_int32_fetch_add (volatile int32_t *p, int32_t n, enum bson_memory_order _unused)
{
int32_t ret;

@@ -165,9 +154,7 @@ _bson_emul_atomic_int32_fetch_add (volatile int32_t *p,
}

int32_t
_bson_emul_atomic_int32_exchange (volatile int32_t *p,
int32_t n,
enum bson_memory_order _unused)
_bson_emul_atomic_int32_exchange (volatile int32_t *p, int32_t n, enum bson_memory_order _unused)
{
int32_t ret;

@@ -206,15 +193,12 @@ _bson_emul_atomic_int32_compare_exchange_weak (volatile int32_t *p,
enum bson_memory_order order)
{
/* We're emulating. We can't do a weak version. */
return _bson_emul_atomic_int32_compare_exchange_strong (
p, expect_value, new_value, order);
return _bson_emul_atomic_int32_compare_exchange_strong (p, expect_value, new_value, order);
}


int
_bson_emul_atomic_int_fetch_add (volatile int *p,
int n,
enum bson_memory_order _unused)
_bson_emul_atomic_int_fetch_add (volatile int *p, int n, enum bson_memory_order _unused)
{
int ret;

@@ -228,9 +212,7 @@ _bson_emul_atomic_int_fetch_add (volatile int *p,
}

int
_bson_emul_atomic_int_exchange (volatile int *p,
int n,
enum bson_memory_order _unused)
_bson_emul_atomic_int_exchange (volatile int *p, int n, enum bson_memory_order _unused)
{
int ret;

@@ -269,14 +251,11 @@ _bson_emul_atomic_int_compare_exchange_weak (volatile int *p,
enum bson_memory_order order)
{
/* We're emulating. We can't do a weak version. */
return _bson_emul_atomic_int_compare_exchange_strong (
p, expect_value, new_value, order);
return _bson_emul_atomic_int_compare_exchange_strong (p, expect_value, new_value, order);
}

void *
_bson_emul_atomic_ptr_exchange (void *volatile *p,
void *n,
enum bson_memory_order _unused)
_bson_emul_atomic_ptr_exchange (void *volatile *p, void *n, enum bson_memory_order _unused)
{
void *ret;

624 changes: 230 additions & 394 deletions bsonjs/bson/bson-atomic.h

Large diffs are not rendered by default.

29 changes: 3 additions & 26 deletions bsonjs/bson/bson-clock.c
Original file line number Diff line number Diff line change
@@ -14,26 +14,16 @@
* limitations under the License.
*/


#ifdef __APPLE__
#include <mach/clock.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <sys/time.h>
#endif


#include "bson-config.h"
#include "bson-compat.h"
#include <bson/bson-config.h>
#include <bson/bson-compat.h>


#if defined(BSON_HAVE_CLOCK_GETTIME)
#include <time.h>
#include <sys/time.h>
#endif

#include "bson-clock.h"

#include <bson/bson-clock.h>

/*
*--------------------------------------------------------------------------
@@ -125,19 +115,6 @@ bson_get_monotonic_time (void)
* int64_t to avoid truncation. */
clock_gettime (CLOCK_MONOTONIC, &ts);
return (((int64_t) ts.tv_sec * 1000000) + (ts.tv_nsec / 1000));
#elif defined(__APPLE__)
static mach_timebase_info_data_t info = {0};
static double ratio = 0.0;

if (!info.denom) {
/* the value from mach_absolute_time () * info.numer / info.denom
* is in nano seconds. So we have to divid by 1000.0 to get micro
* seconds*/
mach_timebase_info (&info);
ratio = (double) info.numer / (double) info.denom / 1000.0;
}

return mach_absolute_time () * ratio;
#elif defined(_WIN32)
/* Despite it's name, this is in milliseconds! */
int64_t ticks = GetTickCount64 ();
8 changes: 4 additions & 4 deletions bsonjs/bson/bson-clock.h
Original file line number Diff line number Diff line change
@@ -14,16 +14,16 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>


#ifndef BSON_CLOCK_H
#define BSON_CLOCK_H


#include "bson-compat.h"
#include "bson-macros.h"
#include "bson-types.h"
#include <bson/bson-compat.h>
#include <bson/bson-macros.h>
#include <bson/bson-types.h>


BSON_BEGIN_DECLS
116 changes: 46 additions & 70 deletions bsonjs/bson/bson-cmp.h
Original file line number Diff line number Diff line change
@@ -14,15 +14,15 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>


#ifndef BSON_CMP_H
#define BSON_CMP_H


#include "bson-compat.h" /* ssize_t */
#include "bson-macros.h" /* BSON_CONCAT */
#include <bson/bson-compat.h> /* ssize_t */
#include <bson/bson-macros.h> /* BSON_CONCAT */

#include <limits.h>
#include <stdbool.h>
@@ -54,54 +54,39 @@ BSON_BEGIN_DECLS
*/


#define BSON_CMP_SET(op, ss, uu, su, us) \
static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _ss) (int64_t t, \
int64_t u) \
{ \
return (ss); \
} \
\
static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _uu) (uint64_t t, \
uint64_t u) \
{ \
return (uu); \
} \
\
static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _su) (int64_t t, \
uint64_t u) \
{ \
return (su); \
} \
\
static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _us) (uint64_t t, \
int64_t u) \
{ \
return (us); \
#define BSON_CMP_SET(op, ss, uu, su, us) \
static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _ss) (int64_t t, int64_t u) \
{ \
return (ss); \
} \
\
static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _uu) (uint64_t t, uint64_t u) \
{ \
return (uu); \
} \
\
static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _su) (int64_t t, uint64_t u) \
{ \
return (su); \
} \
\
static BSON_INLINE bool BSON_CONCAT3 (bson_cmp_, op, _us) (uint64_t t, int64_t u) \
{ \
return (us); \
}

BSON_CMP_SET (equal,
t == u,
t == u,
t < 0 ? false : (uint64_t) (t) == u,
u < 0 ? false : t == (uint64_t) (u))
BSON_CMP_SET (equal, t == u, t == u, t < 0 ? false : (uint64_t) (t) == u, u < 0 ? false : t == (uint64_t) (u))

BSON_CMP_SET (not_equal,
!bson_cmp_equal_ss (t, u),
!bson_cmp_equal_uu (t, u),
!bson_cmp_equal_su (t, u),
!bson_cmp_equal_us (t, u))

BSON_CMP_SET (less,
t < u,
t < u,
t < 0 ? true : (uint64_t) (t) < u,
u < 0 ? false : t < (uint64_t) (u))
BSON_CMP_SET (less, t < u, t < u, t < 0 ? true : (uint64_t) (t) < u, u < 0 ? false : t < (uint64_t) (u))

BSON_CMP_SET (greater,
bson_cmp_less_ss (u, t),
bson_cmp_less_uu (u, t),
bson_cmp_less_us (u, t),
bson_cmp_less_su (u, t))
BSON_CMP_SET (
greater, bson_cmp_less_ss (u, t), bson_cmp_less_uu (u, t), bson_cmp_less_us (u, t), bson_cmp_less_su (u, t))

BSON_CMP_SET (less_equal,
!bson_cmp_greater_ss (t, u),
@@ -120,35 +105,28 @@ BSON_CMP_SET (greater_equal,

/* Return true if the given value is within the range of the corresponding
* signed type. The suffix must match the signedness of the given value. */
#define BSON_IN_RANGE_SET_SIGNED(Type, min, max) \
static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _signed) ( \
int64_t value) \
{ \
return bson_cmp_greater_equal_ss (value, min) && \
bson_cmp_less_equal_ss (value, max); \
} \
\
static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _unsigned) ( \
uint64_t value) \
{ \
return bson_cmp_greater_equal_us (value, min) && \
bson_cmp_less_equal_us (value, max); \
#define BSON_IN_RANGE_SET_SIGNED(Type, min, max) \
static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _signed) (int64_t value) \
{ \
return bson_cmp_greater_equal_ss (value, min) && bson_cmp_less_equal_ss (value, max); \
} \
\
static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _unsigned) (uint64_t value) \
{ \
return bson_cmp_greater_equal_us (value, min) && bson_cmp_less_equal_us (value, max); \
}

/* Return true if the given value is within the range of the corresponding
* unsigned type. The suffix must match the signedness of the given value. */
#define BSON_IN_RANGE_SET_UNSIGNED(Type, max) \
static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _signed) ( \
int64_t value) \
{ \
return bson_cmp_greater_equal_su (value, 0u) && \
bson_cmp_less_equal_su (value, max); \
} \
\
static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _unsigned) ( \
uint64_t value) \
{ \
return bson_cmp_less_equal_uu (value, max); \
#define BSON_IN_RANGE_SET_UNSIGNED(Type, max) \
static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _signed) (int64_t value) \
{ \
return bson_cmp_greater_equal_su (value, 0u) && bson_cmp_less_equal_su (value, max); \
} \
\
static BSON_INLINE bool BSON_CONCAT3 (bson_in_range, _##Type, _unsigned) (uint64_t value) \
{ \
return bson_cmp_less_equal_uu (value, max); \
}

BSON_IN_RANGE_SET_SIGNED (signed_char, SCHAR_MIN, SCHAR_MAX)
@@ -182,13 +160,11 @@ BSON_IN_RANGE_SET_UNSIGNED (size_t, SIZE_MAX)

/* Return true if the value with *signed* type is in the representable range of
* Type and false otherwise. */
#define bson_in_range_signed(Type, value) \
BSON_CONCAT3 (bson_in_range, _##Type, _signed) (value)
#define bson_in_range_signed(Type, value) BSON_CONCAT3 (bson_in_range, _##Type, _signed) (value)

/* Return true if the value with *unsigned* type is in the representable range
* of Type and false otherwise. */
#define bson_in_range_unsigned(Type, value) \
BSON_CONCAT3 (bson_in_range, _##Type, _unsigned) (value)
#define bson_in_range_unsigned(Type, value) BSON_CONCAT3 (bson_in_range, _##Type, _unsigned) (value)


BSON_END_DECLS
146 changes: 8 additions & 138 deletions bsonjs/bson/bson-compat.h
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>


#ifndef BSON_COMPAT_H
@@ -31,8 +31,8 @@
#endif
#endif

#include "bson-config.h"
#include "bson-macros.h"
#include <bson/bson-config.h>
#include <bson/bson-macros.h>


#ifdef BSON_OS_WIN32
@@ -64,7 +64,7 @@
#endif


#include "bson-macros.h"
#include <bson/bson-macros.h>


#include <errno.h>
@@ -133,8 +133,7 @@ typedef SSIZE_T ssize_t;
/* Derive the maximum representable value of signed integer type T using the
* formula 2^(N - 1) - 1 where N is the number of bits in type T. This assumes
* T is represented using two's complement. */
#define BSON_NUMERIC_LIMITS_MAX_SIGNED(T) \
((T) ((((size_t) 0x01u) << (sizeof (T) * (size_t) CHAR_BIT - 1u)) - 1u))
#define BSON_NUMERIC_LIMITS_MAX_SIGNED(T) ((T) ((((size_t) 0x01u) << (sizeof (T) * (size_t) CHAR_BIT - 1u)) - 1u))

/* Derive the minimum representable value of signed integer type T as one less
* than the negation of its maximum representable value. This assumes T is
@@ -145,134 +144,6 @@ typedef SSIZE_T ssize_t;
* all its bits to 1. */
#define BSON_NUMERIC_LIMITS_MAX_UNSIGNED(T) ((T) (~((T) 0)))

/* Define numeric limit constants if not already available for C90
* compatibility. These can be removed once C99 is declared the minimum
* supported C standard. */
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L

#ifndef SCHAR_MAX
#define SCHAR_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (signed char)
#endif

#ifndef SHRT_MAX
#define SHRT_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (short)
#endif

#ifndef INT_MAX
#define INT_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (int)
#endif

#ifndef LONG_MAX
#define LONG_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (long)
#endif

#ifndef LLONG_MAX
#define LLONG_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (long long)
#endif

#ifndef UCHAR_MAX
#define UCHAR_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (unsigned char)
#endif

#ifndef USHRT_MAX
#define USHRT_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (unsigned short)
#endif

#ifndef UINT_MAX
#define UINT_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (unsigned int)
#endif

#ifndef ULONG_MAX
#define ULONG_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (unsigned long)
#endif

#ifndef ULLONG_MAX
#define ULLONG_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (unsigned long long)
#endif

#ifndef INT8_MAX
#define INT8_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (int8_t)
#endif

#ifndef INT16_MAX
#define INT16_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (int16_t)
#endif

#ifndef INT32_MAX
#define INT32_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (int32_t)
#endif

#ifndef INT64_MAX
#define INT64_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (int64_t)
#endif

#ifndef UINT8_MAX
#define UINT8_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (uint8_t)
#endif

#ifndef UINT16_MAX
#define UINT16_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (uint16_t)
#endif

#ifndef UINT32_MAX
#define UINT32_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (uint32_t)
#endif

#ifndef UINT64_MAX
#define UINT64_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (uint64_t)
#endif

#ifndef SIZE_MAX
#define SIZE_MAX BSON_NUMERIC_LIMITS_MAX_UNSIGNED (size_t)
#endif

#ifndef PTRDIFF_MAX
#define PTRDIFF_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (ptrdiff_t)
#endif

#ifndef SCHAR_MIN
#define SCHAR_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (signed char, SCHAR_MAX)
#endif

#ifndef SHRT_MIN
#define SHRT_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (short, SHRT_MAX)
#endif

#ifndef INT_MIN
#define INT_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (int, INT_MAX)
#endif

#ifndef LONG_MIN
#define LONG_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (long, LONG_MAX)
#endif

#ifndef LLONG_MIN
#define LLONG_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (long long, LLONG_MAX)
#endif

#ifndef INT8_MIN
#define INT8_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (int8_t, INT8_MAX)
#endif

#ifndef INT16_MIN
#define INT16_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (int16_t, INT16_MAX)
#endif

#ifndef INT32_MIN
#define INT32_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (int32_t, INT32_MAX)
#endif

#ifndef INT64_MIN
#define INT64_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (int64_t, INT64_MAX)
#endif

#ifndef PTRDIFF_MIN
#define PTRDIFF_MIN BSON_NUMERIC_LIMITS_MIN_SIGNED (ptrdiff_t, PTRDIFF_MAX)
#endif

#endif /* !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L */


#ifndef SSIZE_MAX
#define SSIZE_MAX BSON_NUMERIC_LIMITS_MAX_SIGNED (ssize_t)
#endif
@@ -301,11 +172,10 @@ typedef signed char bool;
#if defined(__GNUC__)
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
#define bson_sync_synchronize() __sync_synchronize ()
#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || \
defined(__i686__) || defined(__x86_64__)
#define bson_sync_synchronize() asm volatile("mfence" ::: "memory")
#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(__x86_64__)
#define bson_sync_synchronize() asm volatile ("mfence" ::: "memory")
#else
#define bson_sync_synchronize() asm volatile("sync" ::: "memory")
#define bson_sync_synchronize() asm volatile ("sync" ::: "memory")
#endif
#elif defined(_MSC_VER)
#define bson_sync_synchronize() MemoryBarrier ()
4 changes: 2 additions & 2 deletions bsonjs/bson/bson-context-private.h
Original file line number Diff line number Diff line change
@@ -14,14 +14,14 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>


#ifndef BSON_CONTEXT_PRIVATE_H
#define BSON_CONTEXT_PRIVATE_H


#include "bson-context.h"
#include <bson/bson-context.h>
#include "common-thread-private.h"


40 changes: 14 additions & 26 deletions bsonjs/bson/bson-context.c
Original file line number Diff line number Diff line change
@@ -14,19 +14,19 @@
* limitations under the License.
*/

#include "bson-compat.h"
#include <bson/bson-compat.h>

#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "bson-atomic.h"
#include "bson-clock.h"
#include "bson-context.h"
#include "bson-context-private.h"
#include "bson-memory.h"
#include <bson/bson-atomic.h>
#include <bson/bson-clock.h>
#include <bson/bson-context.h>
#include <bson/bson-context-private.h>
#include <bson/bson-memory.h>
#include "common-thread-private.h"


@@ -62,20 +62,17 @@ _bson_context_set_oid_seq32 (bson_context_t *context, /* IN */
bson_oid_t *oid) /* OUT */
{
uint32_t seq = (uint32_t) bson_atomic_int32_fetch_add (
(int32_t *) &context->seq32, 1, bson_memory_order_seq_cst);
(DECL_ATOMIC_INTEGRAL_INT32 *) &context->seq32, 1, bson_memory_order_seq_cst);
seq = BSON_UINT32_TO_BE (seq);
memcpy (&oid->bytes[BSON_OID_SEQ32_OFFSET],
((uint8_t *) &seq) + 1,
BSON_OID_SEQ32_SIZE);
memcpy (&oid->bytes[BSON_OID_SEQ32_OFFSET], ((uint8_t *) &seq) + 1, BSON_OID_SEQ32_SIZE);
}


void
_bson_context_set_oid_seq64 (bson_context_t *context, /* IN */
bson_oid_t *oid) /* OUT */
{
uint64_t seq = (uint64_t) bson_atomic_int64_fetch_add (
(int64_t *) &context->seq64, 1, bson_memory_order_seq_cst);
uint64_t seq = (uint64_t) bson_atomic_int64_fetch_add ((int64_t *) &context->seq64, 1, bson_memory_order_seq_cst);

seq = BSON_UINT64_TO_BE (seq);
memcpy (&oid->bytes[BSON_OID_SEQ64_OFFSET], &seq, BSON_OID_SEQ64_SIZE);
@@ -96,9 +93,7 @@ _bson_context_get_hostname (char out[HOST_NAME_MAX])
{
if (gethostname (out, HOST_NAME_MAX) != 0) {
if (errno == ENAMETOOLONG) {
fprintf (stderr,
"hostname exceeds %d characters, truncating.",
HOST_NAME_MAX);
fprintf (stderr, "hostname exceeds %d characters, truncating.", HOST_NAME_MAX);
} else {
fprintf (stderr, "unable to get hostname: %d", errno);
}
@@ -158,10 +153,7 @@ _sip_round (uint64_t *v0, uint64_t *v1, uint64_t *v2, uint64_t *v3)
}

void
_siphash (const void *in,
const size_t inlen,
const uint64_t key[2],
uint64_t digest[2])
_siphash (const void *in, const size_t inlen, const uint64_t key[2], uint64_t digest[2])
{
const unsigned char *ni = (const unsigned char *) in;
const unsigned char *kk = (const unsigned char *) key;
@@ -286,8 +278,7 @@ _bson_context_init_random (bson_context_t *context, bool init_seq)
bson_gettimeofday (&rand_params.time);
rand_params.pid = _bson_getpid ();
_bson_context_get_hostname (rand_params.hostname);
rand_params.rand_call_counter = bson_atomic_int64_fetch_add (
&s_rand_call_counter, 1, bson_memory_order_seq_cst);
rand_params.rand_call_counter = bson_atomic_int64_fetch_add (&s_rand_call_counter, 1, bson_memory_order_seq_cst);

/* Generate a SipHash key. We do not care about secrecy or determinism, only
* uniqueness. */
@@ -332,14 +323,11 @@ _bson_context_set_oid_rand (bson_context_t *context, bson_oid_t *oid)
* after a call to fork() */
uint64_t now_pid = _bson_getpid ();
if (now_pid != context->pid) {
_bson_context_init_random (
context, false /* Do not update the sequence counters */);
_bson_context_init_random (context, false /* Do not update the sequence counters */);
}
}
/* Copy the stored randomness into the OID */
memcpy (oid->bytes + BSON_OID_RANDOMESS_OFFSET,
&context->randomness,
BSON_OID_RANDOMNESS_SIZE);
memcpy (oid->bytes + BSON_OID_RANDOMESS_OFFSET, &context->randomness, BSON_OID_RANDOMNESS_SIZE);
}


6 changes: 3 additions & 3 deletions bsonjs/bson/bson-context.h
Original file line number Diff line number Diff line change
@@ -14,15 +14,15 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>


#ifndef BSON_CONTEXT_H
#define BSON_CONTEXT_H


#include "bson-macros.h"
#include "bson-types.h"
#include <bson/bson-macros.h>
#include <bson/bson-types.h>


BSON_BEGIN_DECLS
120 changes: 57 additions & 63 deletions bsonjs/bson/bson-decimal128.c
Original file line number Diff line number Diff line change
@@ -19,10 +19,11 @@
#include <string.h>
#include <ctype.h>

#include "bson-decimal128.h"
#include "bson-types.h"
#include "bson-macros.h"
#include "bson-string.h"
#include <bson/bson-cmp.h>
#include <bson/bson-decimal128.h>
#include <bson/bson-types.h>
#include <bson/bson-macros.h>
#include <bson/bson-string.h>


#define BSON_DECIMAL128_EXPONENT_MAX 6111
@@ -31,15 +32,17 @@
#define BSON_DECIMAL128_MAX_DIGITS 34

#define BSON_DECIMAL128_SET_NAN(dec) \
do { \
if (1) { \
(dec).high = 0x7c00000000000000ull; \
(dec).low = 0; \
} while (0);
} else \
(void) 0
#define BSON_DECIMAL128_SET_INF(dec, isneg) \
do { \
if (1) { \
(dec).high = 0x7800000000000000ull + 0x8000000000000000ull * (isneg); \
(dec).low = 0; \
} while (0);
} else \
(void) 0

/**
* _bson_uint128_t:
@@ -82,16 +85,15 @@ _bson_uint128_divide1B (_bson_uint128_t value, /* IN */
uint64_t _rem = 0;
int i = 0;

if (!value.parts[0] && !value.parts[1] && !value.parts[2] &&
!value.parts[3]) {
if (!value.parts[0] && !value.parts[1] && !value.parts[2] && !value.parts[3]) {
*quotient = value;
*rem = 0;
return;
}


for (i = 0; i <= 3; i++) {
_rem <<= 32; /* Adjust remainder to match value of next dividend */
_rem <<= 32; /* Adjust remainder to match value of next dividend */
_rem += value.parts[i]; /* Add the divided to _rem */
value.parts[i] = (uint32_t) (_rem / DIVISOR);
_rem %= DIVISOR; /* Store the remainder */
@@ -138,34 +140,33 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */

/* Note: bits in this routine are referred to starting at 0, */
/* from the sign bit, towards the coefficient. */
uint32_t high; /* bits 0 - 31 */
uint32_t midh; /* bits 32 - 63 */
uint32_t midl; /* bits 64 - 95 */
uint32_t low; /* bits 96 - 127 */
uint32_t combination; /* bits 1 - 5 */
uint32_t biased_exponent; /* decoded biased exponent (14 bits) */
uint32_t significand_digits = 0; /* the number of significand digits */
uint32_t significand[36] = {0}; /* the base-10 digits in the significand */
uint32_t high; /* bits 0 - 31 */
uint32_t midh; /* bits 32 - 63 */
uint32_t midl; /* bits 64 - 95 */
uint32_t low; /* bits 96 - 127 */
uint32_t combination; /* bits 1 - 5 */
uint32_t biased_exponent; /* decoded biased exponent (14 bits) */
uint32_t significand_digits = 0; /* the number of significand digits */
uint32_t significand[36] = {0}; /* the base-10 digits in the significand */
uint32_t *significand_read = significand; /* read pointer into significand */
int32_t exponent; /* unbiased exponent */
int32_t scientific_exponent; /* the exponent if scientific notation is
* used */
bool is_zero = false; /* true if the number is zero */
int32_t scientific_exponent; /* the exponent if scientific notation is
* used */
bool is_zero = false; /* true if the number is zero */

uint8_t significand_msb; /* the most signifcant significand bits (50-46) */
_bson_uint128_t
significand128; /* temporary storage for significand decoding */
size_t i; /* indexing variables */
int j, k;
uint8_t significand_msb; /* the most signifcant significand bits (50-46) */
_bson_uint128_t significand128; /* temporary storage for significand decoding */

memset (significand_str, 0, sizeof (significand_str));

if ((int64_t) dec->high < 0) { /* negative */
*(str_out++) = '-';
}

low = (uint32_t) dec->low, midl = (uint32_t) (dec->low >> 32),
midh = (uint32_t) dec->high, high = (uint32_t) (dec->high >> 32);
low = (uint32_t) dec->low;
midl = (uint32_t) (dec->low >> 32);
midh = (uint32_t) dec->high;
high = (uint32_t) (dec->high >> 32);

/* Decode combination field and exponent */
combination = (high >> 26) & COMBINATION_MASK;
@@ -200,8 +201,8 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
significand128.parts[2] = midl;
significand128.parts[3] = low;

if (significand128.parts[0] == 0 && significand128.parts[1] == 0 &&
significand128.parts[2] == 0 && significand128.parts[3] == 0) {
if (significand128.parts[0] == 0 && significand128.parts[1] == 0 && significand128.parts[2] == 0 &&
significand128.parts[3] == 0) {
is_zero = true;
} else if (significand128.parts[0] >= (1 << 17)) {
/* The significand is non-canonical or zero.
@@ -212,18 +213,17 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
*/
is_zero = true;
} else {
for (k = 3; k >= 0; k--) {
for (int k = 3; k >= 0; k--) {
uint32_t least_digits = 0;
_bson_uint128_divide1B (
significand128, &significand128, &least_digits);
_bson_uint128_divide1B (significand128, &significand128, &least_digits);

/* We now have the 9 least significant digits (in base 2). */
/* Convert and output to string. */
if (!least_digits) {
continue;
}

for (j = 8; j >= 0; j--) {
for (int j = 8; j >= 0; j--) {
significand[k * 9 + j] = least_digits % 10;
least_digits /= 10;
}
@@ -264,7 +264,7 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
*(str_out++) = '.';
}

for (i = 0; i < significand_digits && (str_out - str) < 36; i++) {
for (uint32_t i = 0; i < significand_digits && (str_out - str) < 36; i++) {
*(str_out++) = *(significand_read++) + '0';
}
/* Exponent */
@@ -273,17 +273,15 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
} else {
/* Regular format with no decimal place */
if (exponent >= 0) {
for (i = 0; i < significand_digits && (str_out - str) < 36; i++) {
for (uint32_t i = 0; i < significand_digits && (str_out - str) < 36; i++) {
*(str_out++) = *(significand_read++) + '0';
}
*str_out = '\0';
} else {
int32_t radix_position = significand_digits + exponent;

if (radix_position > 0) { /* non-zero digits before radix */
for (i = 0;
i < radix_position && (str_out - str) < BSON_DECIMAL128_STRING;
i++) {
for (int32_t i = 0; i < radix_position && (str_out - str) < BSON_DECIMAL128_STRING; i++) {
*(str_out++) = *(significand_read++) + '0';
}
} else { /* leading zero before radix point */
@@ -295,9 +293,8 @@ bson_decimal128_to_string (const bson_decimal128_t *dec, /* IN */
*(str_out++) = '0';
}

for (i = 0;
(i < significand_digits - BSON_MAX (radix_position - 1, 0)) &&
(str_out - str) < BSON_DECIMAL128_STRING;
for (uint32_t i = 0; bson_cmp_greater_us (significand_digits - i, BSON_MAX (radix_position - 1, 0)) &&
(str_out - str) < BSON_DECIMAL128_STRING;
i++) {
*(str_out++) = *(significand_read++) + '0';
}
@@ -331,8 +328,7 @@ _mul_64x64 (uint64_t left, /* IN */
uint64_t right, /* IN */
_bson_uint128_6464_t *product) /* OUT */
{
uint64_t left_high, left_low, right_high, right_low, product_high,
product_mid, product_mid2, product_low;
uint64_t left_high, left_low, right_high, right_low, product_high, product_mid, product_mid2, product_low;
_bson_uint128_6464_t rt = {0};

if (!left && !right) {
@@ -489,9 +485,9 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */
size_t significant_digits = 0; /* Total number of significant digits
* (no leading or trailing zero) */
size_t ndigits_read = 0; /* Total number of significand digits read */
size_t ndigits = 0; /* Total number of digits (no leading zeros) */
size_t radix_position = 0; /* The number of the digits after radix */
size_t first_nonzero = 0; /* The index of the first non-zero in *str* */
size_t ndigits = 0; /* Total number of digits (no leading zeros) */
size_t radix_position = 0; /* The number of the digits after radix */
size_t first_nonzero = 0; /* The index of the first non-zero in *str* */

uint16_t digits[BSON_DECIMAL128_MAX_DIGITS] = {0};
uint16_t ndigits_stored = 0; /* The number of digits in digits */
@@ -515,8 +511,7 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */

/* Check for Infinity or NaN */
if (!isdigit (*str_read) && *str_read != '.') {
if (_dec128_istreq (str_read, "inf") ||
_dec128_istreq (str_read, "infinity")) {
if (_dec128_istreq (str_read, "inf") || _dec128_istreq (str_read, "infinity")) {
BSON_DECIMAL128_SET_INF (*dec, is_negative);
return true;
} else if (_dec128_istreq (str_read, "nan")) {
@@ -529,8 +524,7 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */
}

/* Read digits */
while (((isdigit (*str_read) || *str_read == '.')) &&
(len == -1 || str_read < string + len)) {
while (((isdigit (*str_read) || *str_read == '.')) && (len == -1 || str_read < string + len)) {
if (*str_read == '.') {
if (saw_radix) {
BSON_DECIMAL128_SET_NAN (*dec);
@@ -579,14 +573,16 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */
#else
#define SSCANF sscanf
#endif
int read_exponent = SSCANF (++str_read, "%d%n", &exponent, &nread);
int64_t temp_exponent = 0;
int read_exponent = SSCANF (++str_read, "%" SCNd64 "%n", &temp_exponent, &nread);
str_read += nread;

if (!read_exponent || nread == 0) {
if (!read_exponent || nread == 0 || !bson_in_range_int32_t_signed (temp_exponent)) {
BSON_DECIMAL128_SET_NAN (*dec);
return false;
}

exponent = (int32_t) temp_exponent;
#undef SSCANF
}

@@ -610,8 +606,7 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */
last_digit = ndigits_stored - 1;
significant_digits = ndigits;
/* Mark trailing zeros as non-significant */
while (string[first_nonzero + significant_digits - 1 + includes_sign +
saw_radix] == '0') {
while (string[first_nonzero + significant_digits - 1 + includes_sign + saw_radix] == '0') {
significant_digits--;
}
}
@@ -623,10 +618,12 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */
/* to represent user input */

/* Overflow prevention */
if (exponent <= radix_position && radix_position - exponent > (1 << 14)) {
if (bson_cmp_less_equal_su (exponent, radix_position) &&
bson_cmp_greater_us (radix_position, exponent + (1 << 14))) {
exponent = BSON_DECIMAL128_EXPONENT_MIN;
} else {
exponent -= radix_position;
BSON_ASSERT (bson_in_range_unsigned (int32_t, radix_position));
exponent -= (int32_t) radix_position;
}

/* Attempt to normalize the exponent */
@@ -664,8 +661,7 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */
}

if (ndigits_stored < ndigits) {
if (string[ndigits - 1 + includes_sign + saw_radix] - '0' != 0 &&
significant_digits != 0) {
if (string[ndigits - 1 + includes_sign + saw_radix] - '0' != 0 && significant_digits != 0) {
BSON_DECIMAL128_SET_NAN (*dec);
return false;
}
@@ -697,9 +693,7 @@ bson_decimal128_from_string_w_len (const char *string, /* IN */

/* There are non-zero digits after last_digit that need rounding. */
/* We round to nearest, ties to even */
round_digit =
string[first_nonzero + last_digit + includes_sign + saw_radix + 1] -
'0';
round_digit = string[first_nonzero + last_digit + includes_sign + saw_radix + 1] - '0';

if (round_digit != 0) {
/* Inexact (non-zero) rounding is not allowed */
12 changes: 5 additions & 7 deletions bsonjs/bson/bson-decimal128.h
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>


#ifndef BSON_DECIMAL128_H
@@ -23,9 +23,9 @@

#include <string.h>

#include "bson-macros.h"
#include "bson-config.h"
#include "bson-types.h"
#include <bson/bson-macros.h>
#include <bson/bson-config.h>
#include <bson/bson-types.h>


/**
@@ -54,9 +54,7 @@ BSON_EXPORT (bool)
bson_decimal128_from_string (const char *string, bson_decimal128_t *dec);

BSON_EXPORT (bool)
bson_decimal128_from_string_w_len (const char *string,
int len,
bson_decimal128_t *dec);
bson_decimal128_from_string_w_len (const char *string, int len, bson_decimal128_t *dec);

BSON_END_DECLS

1,382 changes: 0 additions & 1,382 deletions bsonjs/bson/bson-dsl.h

This file was deleted.

26 changes: 10 additions & 16 deletions bsonjs/bson/bson-endian.h
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>


#ifndef BSON_ENDIAN_H
@@ -25,9 +25,9 @@
#include <sys/byteorder.h>
#endif

#include "bson-config.h"
#include "bson-macros.h"
#include "bson-compat.h"
#include <bson/bson-config.h>
#include <bson/bson-macros.h>
#include <bson/bson-compat.h>


BSON_BEGIN_DECLS
@@ -41,8 +41,7 @@ BSON_BEGIN_DECLS
#define BSON_UINT16_SWAP_LE_BE(v) BSWAP_16 ((uint16_t) v)
#define BSON_UINT32_SWAP_LE_BE(v) BSWAP_32 ((uint32_t) v)
#define BSON_UINT64_SWAP_LE_BE(v) BSWAP_64 ((uint64_t) v)
#elif defined(__clang__) && defined(__clang_major__) && \
defined(__clang_minor__) && (__clang_major__ >= 3) && \
#elif defined(__clang__) && defined(__clang_major__) && defined(__clang_minor__) && (__clang_major__ >= 3) && \
(__clang_minor__ >= 1)
#if __has_builtin(__builtin_bswap16)
#define BSON_UINT16_SWAP_LE_BE(v) __builtin_bswap16 (v)
@@ -156,8 +155,7 @@ __bson_uint16_swap_slow (uint16_t v) /* IN */
static BSON_INLINE uint32_t
__bson_uint32_swap_slow (uint32_t v) /* IN */
{
return ((v & 0x000000FFU) << 24) | ((v & 0x0000FF00U) << 8) |
((v & 0x00FF0000U) >> 8) | ((v & 0xFF000000U) >> 24);
return ((v & 0x000000FFU) << 24) | ((v & 0x0000FF00U) << 8) | ((v & 0x00FF0000U) >> 8) | ((v & 0xFF000000U) >> 24);
}


@@ -180,14 +178,10 @@ __bson_uint32_swap_slow (uint32_t v) /* IN */
static BSON_INLINE uint64_t
__bson_uint64_swap_slow (uint64_t v) /* IN */
{
return ((v & 0x00000000000000FFULL) << 56) |
((v & 0x000000000000FF00ULL) << 40) |
((v & 0x0000000000FF0000ULL) << 24) |
((v & 0x00000000FF000000ULL) << 8) |
((v & 0x000000FF00000000ULL) >> 8) |
((v & 0x0000FF0000000000ULL) >> 24) |
((v & 0x00FF000000000000ULL) >> 40) |
((v & 0xFF00000000000000ULL) >> 56);
return ((v & 0x00000000000000FFULL) << 56) | ((v & 0x000000000000FF00ULL) << 40) |
((v & 0x0000000000FF0000ULL) << 24) | ((v & 0x00000000FF000000ULL) << 8) |
((v & 0x000000FF00000000ULL) >> 8) | ((v & 0x0000FF0000000000ULL) >> 24) |
((v & 0x00FF000000000000ULL) >> 40) | ((v & 0xFF00000000000000ULL) >> 56);
}


83 changes: 71 additions & 12 deletions bsonjs/bson/bson-error.c
Original file line number Diff line number Diff line change
@@ -18,12 +18,17 @@
#include <stdio.h>
#include <stdarg.h>

#include "bson-compat.h"
#include "bson-config.h"
#include "bson-error.h"
#include "bson-memory.h"
#include "bson-string.h"
#include "bson-types.h"
#include <bson/bson-compat.h>
#include <bson/bson-config.h>
#include <bson/bson-error.h>
#include <bson/bson-memory.h>
#include <bson/bson-string.h>
#include <bson/bson-types.h>

// See `bson_strerror_r()` definition below.
#if !defined(_WIN32) && !defined(__APPLE__)
#include <locale.h> // uselocale()
#endif


/*
@@ -98,21 +103,75 @@ bson_set_error (bson_error_t *error, /* OUT */
*/

char *
bson_strerror_r (int err_code, /* IN */
char *buf, /* IN */
size_t buflen) /* IN */
bson_strerror_r (int err_code, /* IN */
char *buf BSON_MAYBE_UNUSED, /* IN */
size_t buflen BSON_MAYBE_UNUSED) /* IN */
{
static const char *unknown_msg = "Unknown error";
char *ret = NULL;

#if defined(_WIN32)
// Windows does not provide `strerror_l` or `strerror_r`, but it does
// unconditionally provide `strerror_s`.
if (strerror_s (buf, buflen, err_code) != 0) {
ret = buf;
}
#else /* XSI strerror_r */
if (strerror_r (err_code, buf, buflen) == 0) {
ret = buf;
#elif defined(_AIX)
// AIX does not provide strerror_l, and its strerror_r isn't glibc's.
// But it does provide a glibc compatible one called __linux_strerror_r
ret = __linux_strerror_r (err_code, buf, buflen);
#elif defined(__APPLE__)
// Apple does not provide `strerror_l`, but it does unconditionally provide
// the XSI-compliant `strerror_r`, but only when compiling with Apple Clang.
// GNU extensions may still be a problem if we are being compiled with GCC on
// Apple. Avoid the compatibility headaches with GNU extensions and the musl
// library by assuming the implementation will not cause UB when reading the
// error message string even when `strerror_r` fails, as encouraged (but not
// required) by the POSIX spec (see:
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strerror.html#tag_16_574_08).
(void) strerror_r (err_code, buf, buflen);
#elif defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 700
// The behavior (of `strerror_l`) is undefined if the locale argument to
// `strerror_l()` is the special locale object LC_GLOBAL_LOCALE or is not a
// valid locale object handle.
locale_t locale = uselocale ((locale_t) 0);
// No need to test for error (it can only be [EINVAL]).
if (locale == LC_GLOBAL_LOCALE) {
// Only use our own locale if a thread-local locale was not already set.
// This is just to satisfy `strerror_l`. We do NOT want to unconditionally
// set a thread-local locale.
locale = newlocale (LC_MESSAGES_MASK, "C", (locale_t) 0);
}
BSON_ASSERT (locale != LC_GLOBAL_LOCALE);

// Avoid `strerror_r` compatibility headaches with GNU extensions and the
// musl library by using `strerror_l` instead. Furthermore, `strerror_r` is
// scheduled to be marked as obsolete in favor of `strerror_l` in the
// upcoming POSIX Issue 8 (see:
// https://www.austingroupbugs.net/view.php?id=655).
//
// POSIX Spec: since strerror_l() is required to return a string for some
// errors, an application wishing to check for all error situations should
// set errno to 0, then call strerror_l(), then check errno.
if (locale != (locale_t) 0) {
errno = 0;
ret = strerror_l (err_code, locale);

if (errno != 0) {
ret = NULL;
}

freelocale (locale);
} else {
// Could not obtain a valid `locale_t` object to satisfy `strerror_l`.
// Fallback to `bson_strncpy` below.
}
#elif defined(_GNU_SOURCE)
// Unlikely, but continue supporting use of GNU extension in cases where the
// C Driver is being built without _XOPEN_SOURCE=700.
ret = strerror_r (err_code, buf, buflen);
#else
#error "Unable to find a supported strerror_r candidate"
#endif

if (!ret) {
14 changes: 5 additions & 9 deletions bsonjs/bson/bson-error.h
Original file line number Diff line number Diff line change
@@ -14,16 +14,16 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>


#ifndef BSON_ERROR_H
#define BSON_ERROR_H


#include "bson-compat.h"
#include "bson-macros.h"
#include "bson-types.h"
#include <bson/bson-compat.h>
#include <bson/bson-macros.h>
#include <bson/bson-types.h>


BSON_BEGIN_DECLS
@@ -35,11 +35,7 @@ BSON_BEGIN_DECLS


BSON_EXPORT (void)
bson_set_error (bson_error_t *error,
uint32_t domain,
uint32_t code,
const char *format,
...) BSON_GNUC_PRINTF (4, 5);
bson_set_error (bson_error_t *error, uint32_t domain, uint32_t code, const char *format, ...) BSON_GNUC_PRINTF (4, 5);
BSON_EXPORT (char *)
bson_strerror_r (int err_code, char *buf, size_t buflen);

13 changes: 5 additions & 8 deletions bsonjs/bson/bson-iso8601-private.h
Original file line number Diff line number Diff line change
@@ -14,25 +14,22 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>


#ifndef BSON_ISO8601_PRIVATE_H
#define BSON_ISO8601_PRIVATE_H


#include "bson-compat.h"
#include "bson-macros.h"
#include "bson-string.h"
#include <bson/bson-compat.h>
#include <bson/bson-macros.h>
#include <bson/bson-string.h>


BSON_BEGIN_DECLS

bool
_bson_iso8601_date_parse (const char *str,
int32_t len,
int64_t *out,
bson_error_t *error);
_bson_iso8601_date_parse (const char *str, int32_t len, int64_t *out, bson_error_t *error);

/**
* _bson_iso8601_date_format:
56 changes: 20 additions & 36 deletions bsonjs/bson/bson-iso8601.c
Original file line number Diff line number Diff line change
@@ -15,20 +15,16 @@
*/


#include "bson-compat.h"
#include "bson-macros.h"
#include "bson-error.h"
#include "bson-iso8601-private.h"
#include "bson-json.h"
#include "bson-timegm-private.h"
#include <bson/bson-compat.h>
#include <bson/bson-macros.h>
#include <bson/bson-error.h>
#include <bson/bson-iso8601-private.h>
#include <bson/bson-json.h>
#include <bson/bson-timegm-private.h>


static bool
get_tok (const char *terminals,
const char **ptr,
int32_t *remaining,
const char **out,
int32_t *out_len)
get_tok (const char *terminals, const char **ptr, int32_t *remaining, const char **out, int32_t *out_len)
{
const char *terminal;
bool found_terminal = false;
@@ -41,8 +37,7 @@ get_tok (const char *terminals,
*out = *ptr;
*out_len = -1;

for (; *remaining && !found_terminal;
(*ptr)++, (*remaining)--, (*out_len)++) {
for (; *remaining && !found_terminal; (*ptr)++, (*remaining)--, (*out_len)++) {
for (terminal = terminals; *terminal; terminal++) {
if (**ptr == *terminal) {
found_terminal = true;
@@ -73,12 +68,7 @@ digits_only (const char *str, int32_t len)
}

static bool
parse_num (const char *str,
int32_t len,
int32_t digits,
int32_t min,
int32_t max,
int32_t *out)
parse_num (const char *str, int32_t len, int32_t digits, int32_t min, int32_t max, int32_t *out)
{
int i;
int magnitude = 1;
@@ -102,10 +92,7 @@ parse_num (const char *str,
}

bool
_bson_iso8601_date_parse (const char *str,
int32_t len,
int64_t *out,
bson_error_t *error)
_bson_iso8601_date_parse (const char *str, int32_t len, int64_t *out, bson_error_t *error)
{
const char *ptr;
int32_t remaining = len;
@@ -139,25 +126,21 @@ _bson_iso8601_date_parse (const char *str,

struct bson_tm posix_date = {0};

#define DATE_PARSE_ERR(msg) \
bson_set_error (error, \
BSON_ERROR_JSON, \
BSON_JSON_ERROR_READ_INVALID_PARAM, \
"Could not parse \"%s\" as date: " msg, \
str); \
#define DATE_PARSE_ERR(msg) \
bson_set_error ( \
error, BSON_ERROR_JSON, BSON_JSON_ERROR_READ_INVALID_PARAM, "Could not parse \"%s\" as date: " msg, str); \
return false

#define DEFAULT_DATE_PARSE_ERR \
DATE_PARSE_ERR ("use ISO8601 format yyyy-mm-ddThh:mm plus timezone, either" \
" \"Z\" or like \"+0500\"")
" \"Z\" or like \"+0500\" or like \"+05:00\"")

ptr = str;

/* we have to match at least yyyy-mm-ddThh:mm */
if (!(get_tok ("-", &ptr, &remaining, &year_ptr, &year_len) &&
get_tok ("-", &ptr, &remaining, &month_ptr, &month_len) &&
get_tok ("T", &ptr, &remaining, &day_ptr, &day_len) &&
get_tok (":", &ptr, &remaining, &hour_ptr, &hour_len) &&
get_tok ("T", &ptr, &remaining, &day_ptr, &day_len) && get_tok (":", &ptr, &remaining, &hour_ptr, &hour_len) &&
get_tok (":+-Z", &ptr, &remaining, &min_ptr, &min_len))) {
DEFAULT_DATE_PARSE_ERR;
}
@@ -231,22 +214,23 @@ _bson_iso8601_date_parse (const char *str,
int32_t tz_hour;
int32_t tz_min;

if (tz_len != 5 || !digits_only (tz_ptr + 1, 4)) {
if ((tz_len != 5 || !digits_only (tz_ptr + 1, 4)) &&
(tz_len != 6 || !digits_only (tz_ptr + 1, 2) || tz_ptr[3] != ':' || !digits_only (tz_ptr + 4, 2))) {
DATE_PARSE_ERR ("could not parse timezone");
}

if (!parse_num (tz_ptr + 1, 2, -1, -23, 23, &tz_hour)) {
DATE_PARSE_ERR ("timezone hour must be at most 23");
}

if (!parse_num (tz_ptr + 3, 2, -1, 0, 59, &tz_min)) {
int32_t tz_min_offset = tz_ptr[3] == ':' ? 1 : 0;
if (!parse_num (tz_ptr + 3 + tz_min_offset, 2, -1, 0, 59, &tz_min)) {
DATE_PARSE_ERR ("timezone minute must be at most 59");
}

/* we inflect the meaning of a 'positive' timezone. Those are hours
* we have to subtract, and vice versa */
tz_adjustment =
(tz_ptr[0] == '-' ? 1 : -1) * ((tz_min * 60) + (tz_hour * 60 * 60));
tz_adjustment = (tz_ptr[0] == '-' ? 1 : -1) * ((tz_min * 60) + (tz_hour * 60 * 60));

if (!(tz_adjustment > -86400 && tz_adjustment < 86400)) {
DATE_PARSE_ERR ("timezone offset must be less than 24 hours");
105 changes: 48 additions & 57 deletions bsonjs/bson/bson-iter.c
Original file line number Diff line number Diff line change
@@ -15,10 +15,10 @@
*/


#include "bson-iter.h"
#include "bson-config.h"
#include "bson-decimal128.h"
#include "bson-types.h"
#include <bson/bson-iter.h>
#include <bson/bson-config.h>
#include <bson/bson-decimal128.h>
#include <bson/bson-types.h>

#define ITER_TYPE(i) ((bson_type_t) * ((i)->raw + (i)->type))

@@ -110,8 +110,13 @@ bson_iter_init_from_data (bson_iter_t *iter, /* OUT */
return false;
}

if (BSON_UNLIKELY (!bson_in_range_unsigned (uint32_t, length))) {
memset (iter, 0, sizeof *iter);
return false;
}

iter->raw = (uint8_t *) data;
iter->len = length;
iter->len = (uint32_t) length;
iter->off = 0;
iter->type = 0;
iter->key = 0;
@@ -234,8 +239,7 @@ bson_iter_init_find_w_len (bson_iter_t *iter, /* INOUT */
BSON_ASSERT (bson);
BSON_ASSERT (key);

return bson_iter_init (iter, bson) &&
bson_iter_find_w_len (iter, key, keylen);
return bson_iter_init (iter, bson) && bson_iter_find_w_len (iter, key, keylen);
}


@@ -1525,8 +1529,7 @@ bson_iter_dbpointer (const bson_iter_t *iter, /* IN */

if (ITER_TYPE (iter) == BSON_TYPE_DBPOINTER) {
if (collection_len) {
memcpy (
collection_len, (iter->raw + iter->d1), sizeof (*collection_len));
memcpy (collection_len, (iter->raw + iter->d1), sizeof (*collection_len));
*collection_len = BSON_UINT32_FROM_LE (*collection_len);

if ((*collection_len) > 0) {
@@ -1941,8 +1944,11 @@ bson_iter_visit_all (bson_iter_t *iter, /* INOUT */

bson_iter_document (iter, &doclen, &docbuf);

if (bson_init_static (&b, docbuf, doclen) &&
VISIT_DOCUMENT (iter, key, &b, data)) {
if (!bson_init_static (&b, docbuf, doclen)) {
iter->err_off = iter->off;
break;
}
if (VISIT_DOCUMENT (iter, key, &b, data)) {
return true;
}
} break;
@@ -1953,8 +1959,11 @@ bson_iter_visit_all (bson_iter_t *iter, /* INOUT */

bson_iter_array (iter, &doclen, &docbuf);

if (bson_init_static (&b, docbuf, doclen) &&
VISIT_ARRAY (iter, key, &b, data)) {
if (!bson_init_static (&b, docbuf, doclen)) {
iter->err_off = iter->off;
break;
}
if (VISIT_ARRAY (iter, key, &b, data)) {
return true;
}
} break;
@@ -2030,8 +2039,7 @@ bson_iter_visit_all (bson_iter_t *iter, /* INOUT */
return true;
}

if (VISIT_DBPOINTER (
iter, key, collection_len, collection, oid, data)) {
if (VISIT_DBPOINTER (iter, key, collection_len, collection, oid, data)) {
return true;
}
} break;
@@ -2079,8 +2087,11 @@ bson_iter_visit_all (bson_iter_t *iter, /* INOUT */
return true;
}

if (bson_init_static (&b, docbuf, doclen) &&
VISIT_CODEWSCOPE (iter, key, length, code, &b, data)) {
if (!bson_init_static (&b, docbuf, doclen)) {
iter->err_off = iter->off;
break;
}
if (VISIT_CODEWSCOPE (iter, key, length, code, &b, data)) {
return true;
}
} break;
@@ -2140,8 +2151,7 @@ bson_iter_visit_all (bson_iter_t *iter, /* INOUT */
}

if (iter->err_off) {
if (unsupported && visitor->visit_unsupported_type &&
bson_utf8_validate (key, strlen (key), false)) {
if (unsupported && visitor->visit_unsupported_type && bson_utf8_validate (key, strlen (key), false)) {
visitor->visit_unsupported_type (iter, key, bson_type, data);
return false;
}
@@ -2190,16 +2200,13 @@ bson_iter_overwrite_oid (bson_iter_t *iter, const bson_oid_t *value)
BSON_ASSERT (iter);

if (ITER_TYPE (iter) == BSON_TYPE_OID) {
memcpy (
(void *) (iter->raw + iter->d1), value->bytes, sizeof (value->bytes));
memcpy ((void *) (iter->raw + iter->d1), value->bytes, sizeof (value->bytes));
}
}


void
bson_iter_overwrite_timestamp (bson_iter_t *iter,
uint32_t timestamp,
uint32_t increment)
bson_iter_overwrite_timestamp (bson_iter_t *iter, uint32_t timestamp, uint32_t increment)
{
uint64_t value;
BSON_ASSERT (iter);
@@ -2387,18 +2394,13 @@ bson_iter_value (bson_iter_t *iter) /* IN */
value->value.v_double = bson_iter_double (iter);
break;
case BSON_TYPE_UTF8:
value->value.v_utf8.str =
(char *) bson_iter_utf8 (iter, &value->value.v_utf8.len);
value->value.v_utf8.str = (char *) bson_iter_utf8 (iter, &value->value.v_utf8.len);
break;
case BSON_TYPE_DOCUMENT:
bson_iter_document (iter,
&value->value.v_doc.data_len,
(const uint8_t **) &value->value.v_doc.data);
bson_iter_document (iter, &value->value.v_doc.data_len, (const uint8_t **) &value->value.v_doc.data);
break;
case BSON_TYPE_ARRAY:
bson_iter_array (iter,
&value->value.v_doc.data_len,
(const uint8_t **) &value->value.v_doc.data);
bson_iter_array (iter, &value->value.v_doc.data_len, (const uint8_t **) &value->value.v_doc.data);
break;
case BSON_TYPE_BINARY:
bson_iter_binary (iter,
@@ -2416,41 +2418,34 @@ bson_iter_value (bson_iter_t *iter) /* IN */
value->value.v_datetime = bson_iter_date_time (iter);
break;
case BSON_TYPE_REGEX:
value->value.v_regex.regex = (char *) bson_iter_regex (
iter, (const char **) &value->value.v_regex.options);
value->value.v_regex.regex = (char *) bson_iter_regex (iter, (const char **) &value->value.v_regex.options);
break;
case BSON_TYPE_DBPOINTER: {
const bson_oid_t *oid;

bson_iter_dbpointer (iter,
&value->value.v_dbpointer.collection_len,
(const char **) &value->value.v_dbpointer.collection,
&oid);
bson_iter_dbpointer (
iter, &value->value.v_dbpointer.collection_len, (const char **) &value->value.v_dbpointer.collection, &oid);
bson_oid_copy (oid, &value->value.v_dbpointer.oid);
break;
}
case BSON_TYPE_CODE:
value->value.v_code.code =
(char *) bson_iter_code (iter, &value->value.v_code.code_len);
value->value.v_code.code = (char *) bson_iter_code (iter, &value->value.v_code.code_len);
break;
case BSON_TYPE_SYMBOL:
value->value.v_symbol.symbol =
(char *) bson_iter_symbol (iter, &value->value.v_symbol.len);
value->value.v_symbol.symbol = (char *) bson_iter_symbol (iter, &value->value.v_symbol.len);
break;
case BSON_TYPE_CODEWSCOPE:
value->value.v_codewscope.code = (char *) bson_iter_codewscope (
iter,
&value->value.v_codewscope.code_len,
&value->value.v_codewscope.scope_len,
(const uint8_t **) &value->value.v_codewscope.scope_data);
value->value.v_codewscope.code =
(char *) bson_iter_codewscope (iter,
&value->value.v_codewscope.code_len,
&value->value.v_codewscope.scope_len,
(const uint8_t **) &value->value.v_codewscope.scope_data);
break;
case BSON_TYPE_INT32:
value->value.v_int32 = bson_iter_int32 (iter);
break;
case BSON_TYPE_TIMESTAMP:
bson_iter_timestamp (iter,
&value->value.v_timestamp.timestamp,
&value->value.v_timestamp.increment);
bson_iter_timestamp (iter, &value->value.v_timestamp.timestamp, &value->value.v_timestamp.increment);
break;
case BSON_TYPE_INT64:
value->value.v_int64 = bson_iter_int64 (iter);
@@ -2486,11 +2481,8 @@ bson_iter_key_len (const bson_iter_t *iter)
}

bool
bson_iter_init_from_data_at_offset (bson_iter_t *iter,
const uint8_t *data,
size_t length,
uint32_t offset,
uint32_t keylen)
bson_iter_init_from_data_at_offset (
bson_iter_t *iter, const uint8_t *data, size_t length, uint32_t offset, uint32_t keylen)
{
const char *key;
uint32_t bson_type;
@@ -2512,8 +2504,7 @@ bson_iter_init_from_data_at_offset (bson_iter_t *iter,
iter->next_off = offset;
iter->err_off = 0;

if (!_bson_iter_next_internal (
iter, keylen, &key, &bson_type, &unsupported)) {
if (!_bson_iter_next_internal (iter, keylen, &key, &bson_type, &unsupported)) {
memset (iter, 0, sizeof *iter);
return false;
}
146 changes: 58 additions & 88 deletions bsonjs/bson/bson-iter.h
Original file line number Diff line number Diff line change
@@ -14,84 +14,69 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>


#ifndef BSON_ITER_H
#define BSON_ITER_H


#include "bson.h"
#include "bson-endian.h"
#include "bson-macros.h"
#include "bson-types.h"
#include <bson/bson-endian.h>
#include <bson/bson-macros.h>
#include <bson/bson-types.h>


BSON_BEGIN_DECLS


#define BSON_ITER_HOLDS_DOUBLE(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_DOUBLE)
#define BSON_ITER_HOLDS_DOUBLE(iter) (bson_iter_type ((iter)) == BSON_TYPE_DOUBLE)

#define BSON_ITER_HOLDS_UTF8(iter) (bson_iter_type ((iter)) == BSON_TYPE_UTF8)

#define BSON_ITER_HOLDS_DOCUMENT(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_DOCUMENT)
#define BSON_ITER_HOLDS_DOCUMENT(iter) (bson_iter_type ((iter)) == BSON_TYPE_DOCUMENT)

#define BSON_ITER_HOLDS_ARRAY(iter) (bson_iter_type ((iter)) == BSON_TYPE_ARRAY)

#define BSON_ITER_HOLDS_BINARY(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_BINARY)
#define BSON_ITER_HOLDS_BINARY(iter) (bson_iter_type ((iter)) == BSON_TYPE_BINARY)

#define BSON_ITER_HOLDS_UNDEFINED(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_UNDEFINED)
#define BSON_ITER_HOLDS_UNDEFINED(iter) (bson_iter_type ((iter)) == BSON_TYPE_UNDEFINED)

#define BSON_ITER_HOLDS_OID(iter) (bson_iter_type ((iter)) == BSON_TYPE_OID)

#define BSON_ITER_HOLDS_BOOL(iter) (bson_iter_type ((iter)) == BSON_TYPE_BOOL)

#define BSON_ITER_HOLDS_DATE_TIME(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_DATE_TIME)
#define BSON_ITER_HOLDS_DATE_TIME(iter) (bson_iter_type ((iter)) == BSON_TYPE_DATE_TIME)

#define BSON_ITER_HOLDS_NULL(iter) (bson_iter_type ((iter)) == BSON_TYPE_NULL)

#define BSON_ITER_HOLDS_REGEX(iter) (bson_iter_type ((iter)) == BSON_TYPE_REGEX)

#define BSON_ITER_HOLDS_DBPOINTER(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_DBPOINTER)
#define BSON_ITER_HOLDS_DBPOINTER(iter) (bson_iter_type ((iter)) == BSON_TYPE_DBPOINTER)

#define BSON_ITER_HOLDS_CODE(iter) (bson_iter_type ((iter)) == BSON_TYPE_CODE)

#define BSON_ITER_HOLDS_SYMBOL(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_SYMBOL)
#define BSON_ITER_HOLDS_SYMBOL(iter) (bson_iter_type ((iter)) == BSON_TYPE_SYMBOL)

#define BSON_ITER_HOLDS_CODEWSCOPE(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_CODEWSCOPE)
#define BSON_ITER_HOLDS_CODEWSCOPE(iter) (bson_iter_type ((iter)) == BSON_TYPE_CODEWSCOPE)

#define BSON_ITER_HOLDS_INT32(iter) (bson_iter_type ((iter)) == BSON_TYPE_INT32)

#define BSON_ITER_HOLDS_TIMESTAMP(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_TIMESTAMP)
#define BSON_ITER_HOLDS_TIMESTAMP(iter) (bson_iter_type ((iter)) == BSON_TYPE_TIMESTAMP)

#define BSON_ITER_HOLDS_INT64(iter) (bson_iter_type ((iter)) == BSON_TYPE_INT64)

#define BSON_ITER_HOLDS_DECIMAL128(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_DECIMAL128)
#define BSON_ITER_HOLDS_DECIMAL128(iter) (bson_iter_type ((iter)) == BSON_TYPE_DECIMAL128)

#define BSON_ITER_HOLDS_MAXKEY(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_MAXKEY)
#define BSON_ITER_HOLDS_MAXKEY(iter) (bson_iter_type ((iter)) == BSON_TYPE_MAXKEY)

#define BSON_ITER_HOLDS_MINKEY(iter) \
(bson_iter_type ((iter)) == BSON_TYPE_MINKEY)
#define BSON_ITER_HOLDS_MINKEY(iter) (bson_iter_type ((iter)) == BSON_TYPE_MINKEY)

#define BSON_ITER_HOLDS_INT(iter) \
(BSON_ITER_HOLDS_INT32 (iter) || BSON_ITER_HOLDS_INT64 (iter))
#define BSON_ITER_HOLDS_INT(iter) (BSON_ITER_HOLDS_INT32 (iter) || BSON_ITER_HOLDS_INT64 (iter))

#define BSON_ITER_HOLDS_NUMBER(iter) \
(BSON_ITER_HOLDS_INT (iter) || BSON_ITER_HOLDS_DOUBLE (iter))
#define BSON_ITER_HOLDS_NUMBER(iter) (BSON_ITER_HOLDS_INT (iter) || BSON_ITER_HOLDS_DOUBLE (iter))

#define BSON_ITER_IS_KEY(iter, key) \
(0 == strcmp ((key), bson_iter_key ((iter))))
#define BSON_ITER_IS_KEY(iter, key) (0 == strcmp ((key), bson_iter_key ((iter))))


BSON_EXPORT (const bson_value_t *)
@@ -109,25 +94,24 @@ bson_iter_value (bson_iter_t *iter);
static BSON_INLINE uint32_t
bson_iter_utf8_len_unsafe (const bson_iter_t *iter)
{
int32_t val;
uint32_t raw;
memcpy (&raw, iter->raw + iter->d1, sizeof (raw));

memcpy (&val, iter->raw + iter->d1, sizeof (val));
val = BSON_UINT32_FROM_LE (val);
return BSON_MAX (0, val - 1);
const uint32_t native = BSON_UINT32_FROM_LE (raw);

int32_t len;
memcpy (&len, &native, sizeof (len));

return len <= 0 ? 0u : (uint32_t) (len - 1);
}


BSON_EXPORT (void)
bson_iter_array (const bson_iter_t *iter,
uint32_t *array_len,
const uint8_t **array);
bson_iter_array (const bson_iter_t *iter, uint32_t *array_len, const uint8_t **array);


BSON_EXPORT (void)
bson_iter_binary (const bson_iter_t *iter,
bson_subtype_t *subtype,
uint32_t *binary_len,
const uint8_t **binary);
bson_iter_binary (const bson_iter_t *iter, bson_subtype_t *subtype, uint32_t *binary_len, const uint8_t **binary);


BSON_EXPORT (const char *)
@@ -152,10 +136,7 @@ bson_iter_code_unsafe (const bson_iter_t *iter, uint32_t *length)


BSON_EXPORT (const char *)
bson_iter_codewscope (const bson_iter_t *iter,
uint32_t *length,
uint32_t *scope_len,
const uint8_t **scope);
bson_iter_codewscope (const bson_iter_t *iter, uint32_t *length, uint32_t *scope_len, const uint8_t **scope);


BSON_EXPORT (void)
@@ -166,9 +147,7 @@ bson_iter_dbpointer (const bson_iter_t *iter,


BSON_EXPORT (void)
bson_iter_document (const bson_iter_t *iter,
uint32_t *document_len,
const uint8_t **document);
bson_iter_document (const bson_iter_t *iter, uint32_t *document_len, const uint8_t **document);


BSON_EXPORT (double)
@@ -199,33 +178,23 @@ BSON_EXPORT (bool)
bson_iter_init (bson_iter_t *iter, const bson_t *bson);

BSON_EXPORT (bool)
bson_iter_init_from_data (bson_iter_t *iter,
const uint8_t *data,
size_t length);
bson_iter_init_from_data (bson_iter_t *iter, const uint8_t *data, size_t length);


BSON_EXPORT (bool)
bson_iter_init_find (bson_iter_t *iter, const bson_t *bson, const char *key);


BSON_EXPORT (bool)
bson_iter_init_find_w_len (bson_iter_t *iter,
const bson_t *bson,
const char *key,
int keylen);
bson_iter_init_find_w_len (bson_iter_t *iter, const bson_t *bson, const char *key, int keylen);


BSON_EXPORT (bool)
bson_iter_init_find_case (bson_iter_t *iter,
const bson_t *bson,
const char *key);
bson_iter_init_find_case (bson_iter_t *iter, const bson_t *bson, const char *key);

BSON_EXPORT (bool)
bson_iter_init_from_data_at_offset (bson_iter_t *iter,
const uint8_t *data,
size_t length,
uint32_t offset,
uint32_t keylen);
bson_iter_init_from_data_at_offset (
bson_iter_t *iter, const uint8_t *data, size_t length, uint32_t offset, uint32_t keylen);

BSON_EXPORT (int32_t)
bson_iter_int32 (const bson_iter_t *iter);
@@ -242,10 +211,14 @@ bson_iter_int32 (const bson_iter_t *iter);
static BSON_INLINE int32_t
bson_iter_int32_unsafe (const bson_iter_t *iter)
{
int32_t val;
uint32_t raw;
memcpy (&raw, iter->raw + iter->d1, sizeof (raw));

memcpy (&val, iter->raw + iter->d1, sizeof (val));
return BSON_UINT32_FROM_LE (val);
const uint32_t native = BSON_UINT32_FROM_LE (raw);

int32_t res;
memcpy (&res, &native, sizeof (res));
return res;
}


@@ -268,10 +241,14 @@ bson_iter_as_int64 (const bson_iter_t *iter);
static BSON_INLINE int64_t
bson_iter_int64_unsafe (const bson_iter_t *iter)
{
int64_t val;
uint64_t raw;
memcpy (&raw, iter->raw + iter->d1, sizeof (raw));

memcpy (&val, iter->raw + iter->d1, sizeof (val));
return BSON_UINT64_FROM_LE (val);
const uint64_t native = BSON_UINT64_FROM_LE (raw);

int64_t res;
memcpy (&res, &native, sizeof (res));
return res;
}


@@ -288,9 +265,7 @@ bson_iter_find_case (bson_iter_t *iter, const char *key);


BSON_EXPORT (bool)
bson_iter_find_descendant (bson_iter_t *iter,
const char *dotkey,
bson_iter_t *descendant);
bson_iter_find_descendant (bson_iter_t *iter, const char *dotkey, bson_iter_t *descendant);


BSON_EXPORT (bool)
@@ -407,7 +382,7 @@ bson_iter_time_t (const bson_iter_t *iter);
static BSON_INLINE time_t
bson_iter_time_t_unsafe (const bson_iter_t *iter)
{
return (time_t) (bson_iter_int64_unsafe (iter) / 1000UL);
return (time_t) (bson_iter_int64_unsafe (iter) / 1000);
}


@@ -428,17 +403,16 @@ bson_iter_timeval_unsafe (const bson_iter_t *iter, struct timeval *tv)
int64_t value = bson_iter_int64_unsafe (iter);
#ifdef BSON_OS_WIN32
tv->tv_sec = (long) (value / 1000);
tv->tv_usec = (long) (value % 1000) * 1000;
#else
tv->tv_sec = (suseconds_t) (value / 1000);
tv->tv_sec = (time_t) (value / 1000);
tv->tv_usec = (suseconds_t) (value % 1000) * 1000;
#endif
tv->tv_usec = (value % 1000) * 1000;
}


BSON_EXPORT (void)
bson_iter_timestamp (const bson_iter_t *iter,
uint32_t *timestamp,
uint32_t *increment);
bson_iter_timestamp (const bson_iter_t *iter, uint32_t *timestamp, uint32_t *increment);


BSON_EXPORT (bool)
@@ -523,19 +497,15 @@ bson_iter_overwrite_oid (bson_iter_t *iter, const bson_oid_t *value);


BSON_EXPORT (void)
bson_iter_overwrite_timestamp (bson_iter_t *iter,
uint32_t timestamp,
uint32_t increment);
bson_iter_overwrite_timestamp (bson_iter_t *iter, uint32_t timestamp, uint32_t increment);


BSON_EXPORT (void)
bson_iter_overwrite_date_time (bson_iter_t *iter, int64_t value);


BSON_EXPORT (bool)
bson_iter_visit_all (bson_iter_t *iter,
const bson_visitor_t *visitor,
void *data);
bson_iter_visit_all (bson_iter_t *iter, const bson_visitor_t *visitor, void *data);

BSON_EXPORT (uint32_t)
bson_iter_offset (bson_iter_t *iter);
3 changes: 2 additions & 1 deletion bsonjs/bson/bson-json-private.h
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>

#ifndef BSON_JSON_PRIVATE_H
#define BSON_JSON_PRIVATE_H
@@ -23,6 +23,7 @@
struct _bson_json_opts_t {
bson_json_mode_t mode;
int32_t max_len;
bool is_outermost_array;
};


608 changes: 201 additions & 407 deletions bsonjs/bson/bson-json.c

Large diffs are not rendered by default.

24 changes: 8 additions & 16 deletions bsonjs/bson/bson-json.h
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
* limitations under the License.
*/

#include "bson-prelude.h"
#include <bson/bson-prelude.h>


#ifndef BSON_JSON_H
@@ -61,36 +61,28 @@ BSON_EXPORT (bson_json_opts_t *)
bson_json_opts_new (bson_json_mode_t mode, int32_t max_len);
BSON_EXPORT (void)
bson_json_opts_destroy (bson_json_opts_t *opts);
BSON_EXPORT (void)
bson_json_opts_set_outermost_array (bson_json_opts_t *opts, bool is_outermost_array);


typedef ssize_t (*bson_json_reader_cb) (void *handle,
uint8_t *buf,
size_t count);
typedef ssize_t (*bson_json_reader_cb) (void *handle, uint8_t *buf, size_t count);
typedef void (*bson_json_destroy_cb) (void *handle);


BSON_EXPORT (bson_json_reader_t *)
bson_json_reader_new (void *data,
bson_json_reader_cb cb,
bson_json_destroy_cb dcb,
bool allow_multiple,
size_t buf_size);
bson_json_reader_new (
void *data, bson_json_reader_cb cb, bson_json_destroy_cb dcb, bool allow_multiple, size_t buf_size);
BSON_EXPORT (bson_json_reader_t *)
bson_json_reader_new_from_fd (int fd, bool close_on_destroy);
BSON_EXPORT (bson_json_reader_t *)
bson_json_reader_new_from_file (const char *filename, bson_error_t *error);
BSON_EXPORT (void)
bson_json_reader_destroy (bson_json_reader_t *reader);
BSON_EXPORT (int)
bson_json_reader_read (bson_json_reader_t *reader,
bson_t *bson,
bson_error_t *error);
bson_json_reader_read (bson_json_reader_t *reader, bson_t *bson, bson_error_t *error);
BSON_EXPORT (bson_json_reader_t *)
bson_json_data_reader_new (bool allow_multiple, size_t size);
BSON_EXPORT (void)
bson_json_data_reader_ingest (bson_json_reader_t *reader,
const uint8_t *data,
size_t len);
bson_json_data_reader_ingest (bson_json_reader_t *reader, const uint8_t *data, size_t len);


BSON_END_DECLS
Loading

0 comments on commit 72e2a1e

Please sign in to comment.