├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── LICENSE ├── README.rst ├── THIRD-PARTY-NOTICES ├── benchmark.py ├── bsonnumpy ├── __init__.py ├── bson │ ├── bcon.c │ ├── bcon.h │ ├── bson-atomic.c │ ├── bson-atomic.h │ ├── bson-clock.c │ ├── bson-clock.h │ ├── bson-compat.h │ ├── bson-config.h │ ├── bson-context-private.h │ ├── bson-context.c │ ├── bson-context.h │ ├── bson-decimal128.c │ ├── bson-decimal128.h │ ├── bson-endian.h │ ├── bson-error.c │ ├── bson-error.h │ ├── bson-iso8601-private.h │ ├── bson-iso8601.c │ ├── bson-iter.c │ ├── bson-iter.h │ ├── bson-json.c │ ├── bson-json.h │ ├── bson-keys.c │ ├── bson-keys.h │ ├── bson-macros.h │ ├── bson-md5.c │ ├── bson-md5.h │ ├── bson-memory.c │ ├── bson-memory.h │ ├── bson-oid.c │ ├── bson-oid.h │ ├── bson-prelude.h │ ├── bson-private.h │ ├── bson-reader.c │ ├── bson-reader.h │ ├── bson-string.c │ ├── bson-string.h │ ├── bson-timegm-private.h │ ├── bson-timegm.c │ ├── bson-types.h │ ├── bson-utf8.c │ ├── bson-utf8.h │ ├── bson-value.c │ ├── bson-value.h │ ├── bson-version-functions.c │ ├── bson-version-functions.h │ ├── bson-version.h │ ├── bson-writer.c │ ├── bson-writer.h │ ├── bson.c │ └── bson.h ├── bsonnumpy.c ├── bsonnumpy.h ├── bsonnumpy_field_order.c ├── bsonnumpy_field_order.h ├── bsonnumpy_hashtable.c ├── bsonnumpy_hashtable.h ├── bsonnumpy_util.c ├── common │ ├── common-b64-private.h │ ├── common-b64.c │ ├── common-config.h │ ├── common-macros-private.h │ ├── common-md5-private.h │ ├── common-md5.c │ ├── common-prelude.h │ ├── common-thread-private.h │ └── common-thread.c ├── jsonsl │ ├── LICENSE │ ├── jsonsl.c │ └── jsonsl.h └── version.py ├── cmake └── FindNumpy.cmake ├── doc ├── .gitignore ├── Makefile ├── _static │ ├── .git-keep │ └── warnDeprecated.js ├── conf.py └── index.rst ├── setup.py ├── test ├── __init__.py ├── test_error_cases.py ├── test_flexible_dtype.py ├── test_ndarray_to_sequence.py ├── test_nested.py ├── test_pandas.py ├── test_scalars.py ├── test_sequence_to_ndarray.py └── test_type_errors.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | .tox/ 2 | *.egg-info/ 3 | *.so 4 | build/ 5 | *.pyc 6 | *.eggs 7 | *.idea 8 | bsonenv 9 | cmake-build-debug/ 10 | /doc/_build 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | 3 | # cache artifacts for faster builds 4 | cache: pip 5 | 6 | # build matrix 7 | python: 8 | - "3.5" 9 | - "3.6" 10 | - "3.7" 11 | 12 | # build matrix 13 | env: 14 | - MONGODB=2.6.12 15 | - MONGODB=3.0.15 16 | - MONGODB=3.2.22 17 | - MONGODB=3.4.23 18 | - MONGODB=3.6.14 19 | - MONGODB=4.0.12 20 | 21 | # safelist 22 | branches: 23 | only: 24 | - master 25 | 26 | before_install: 27 | - sudo apt-get install gcc 28 | 29 | install: 30 | - wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-${MONGODB}.tgz -O /tmp/mongodb.tgz 31 | - tar -xvf /tmp/mongodb.tgz 32 | - mkdir /tmp/data 33 | - ${PWD}/mongodb-linux-x86_64-${MONGODB}/bin/mongod --dbpath /tmp/data --bind_ip 127.0.0.1 &> /dev/null & 34 | 35 | before_script: 36 | - pip install -U numpy pandas pymongo 37 | 38 | script: 39 | - echo $TRAVIS_PYTHON_VERSION 40 | - python setup.py test 41 | - if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then pip install -U sphinx && python setup.py doc --test; fi 42 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.3) 2 | 3 | include(FindPythonInterp) 4 | message(STATUS "Found Python ${PYTHON_VERSION_STRING} ${PYTHON_EXECUTABLE}") 5 | include(FindPythonLibs) 6 | include(cmake/FindNumpy.cmake) 7 | include_directories( 8 | BEFORE 9 | ${PYTHON_INCLUDE_DIRS} 10 | ${NUMPY_INCLUDE_DIR} 11 | bson-numpy/bson 12 | bson-numpy/jsonsl) 13 | 14 | file(GLOB bsonnumpy_SRC "bson-numpy/*.c" "bson-numpy/bson/*.c" "bson-numpy/jsonsl/*.c") 15 | add_library(bsonnumpy SHARED ${bsonnumpy_SRC}) 16 | target_include_directories(bsonnumpy PRIVATE bson-numpy) 17 | target_link_libraries(bsonnumpy ${PYTHON_LIBRARIES} ${BSON_LIBRARIES}) 18 | target_compile_definitions(bsonnumpy PRIVATE BSON_COMPILATION) 19 | 20 | if (APPLE) 21 | set_property(TARGET bsonnumpy PROPERTY PREFIX "") 22 | set_property(TARGET bsonnumpy PROPERTY SUFFIX "") 23 | set_property(TARGET bsonnumpy PROPERTY OUTPUT_NAME "bsonnumpy.so") 24 | endif() 25 | 26 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ========== 2 | BSON-NumPy 3 | ========== 4 | 5 | MongoDB, Inc. has stopped development of BSON-NumPy and this project has been superseded by 6 | `PyMongoArrow `_. In addition 7 | to NumPy arrays, **PyMongoArrow** also supports direct conversion of MongoDB query results to 8 | Pandas DataFrames and Apache Arrow Tables, amongst other features. See the project 9 | `documentation `_ to get started with **PyMongoArrow**. 10 | 11 | :Info: See `the mongo site `_ for more information. See `GitHub `_ for the latest source. 12 | :Author: Anna Herlihy 13 | :Maintainer: The MongoDB Python Team 14 | :Documentation: https://bson-numpy.readthedocs.io/en/latest/ 15 | 16 | About 17 | ===== 18 | 19 | A Python extension written in C that uses `libbson 20 | `_ to convert between `NumPy `_ 21 | arrays and `BSON `_, the native data format of MongoDB. 22 | 23 | This project is a **prototype** and not ready for production use. 24 | 25 | Bugs / Feature Requests 26 | ======================= 27 | 28 | Think you’ve found a bug? Want to see a new feature in BSON-NumPy? `Please open a 29 | issue in github. `_ 30 | 31 | How To Ask For Help 32 | ------------------- 33 | 34 | Please include all of the following information when opening an issue: 35 | 36 | - Detailed steps to reproduce the problem, including full traceback, if possible. 37 | - The exact python version used, with patch level:: 38 | 39 | $ python -c "import sys; print(sys.version)" 40 | 41 | - The exact version of bson-numpy used:: 42 | 43 | $ python -c "import bsonnumpy; print(bsonnumpy.__version__)" 44 | 45 | - The operating system and version (e.g. Windows 7, OSX 10.8, ...) 46 | 47 | Installation 48 | ============ 49 | 50 | Please see the `instructions on readthedocs. 51 | `_ 52 | 53 | Dependencies 54 | ============ 55 | 56 | BSON-NumPy supports CPython 3.5+. BSON-NumPy requires NumPy 1.17.0+, and works 57 | with PyMongo 3.6+. 58 | 59 | Examples 60 | ======== 61 | 62 | Please see the `examples on readthedocs. 63 | `_ 64 | 65 | Documentation 66 | ============= 67 | 68 | Please see the `documentation on readthedocs. 69 | `_ 70 | 71 | Testing 72 | ======= 73 | 74 | The easiest way to run the tests is to run **python setup.py test** in 75 | the root of the distribution. 76 | 77 | .. _sphinx: http://sphinx.pocoo.org/ 78 | -------------------------------------------------------------------------------- /THIRD-PARTY-NOTICES: -------------------------------------------------------------------------------- 1 | BSON-NumPy uses third-party libraries or other resources that may 2 | be distributed under licenses different than the BSON-NumPy software. 3 | 4 | In the event that we accidentally failed to list a required notice, 5 | please bring it to our attention through any of the ways detailed here: 6 | 7 | mongodb-dev@googlegroups.com 8 | 9 | The attached notices are provided for information only. 10 | 11 | For any licenses that require disclosure of source, sources are available at 12 | https://github.com/mongodb/bson-numpy. 13 | 14 | 15 | 1) License Notice for jsonsl 16 | ---------------------------- 17 | 18 | Copyright (c) 2012-2015 M. Nunberg, mnunberg@haskalah.org 19 | 20 | Permission is hereby granted, free of charge, to any person obtaining 21 | a copy of this software and associated documentation files (the 22 | "Software"), to deal in the Software without restriction, including 23 | without limitation the rights to use, copy, modify, merge, publish, 24 | distribute, sublicense, and/or sell copies of the Software, and to 25 | permit persons to whom the Software is furnished to do so, subject to 26 | the following conditions: 27 | 28 | The above copyright notice and this permission notice shall be 29 | included in all copies or substantial portions of the Software. 30 | 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 32 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 33 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 34 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 35 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 36 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 37 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 38 | -------------------------------------------------------------------------------- /benchmark.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import collections 3 | import math 4 | import string 5 | import sys 6 | import timeit 7 | from functools import partial 8 | 9 | import pymongo 10 | import numpy as np 11 | from bson import BSON, CodecOptions, Int64, ObjectId 12 | from bson.raw_bson import RawBSONDocument 13 | 14 | try: 15 | import bsonnumpy 16 | except (ImportError, OSError) as exc: 17 | print(exc) 18 | bsonnumpy = None 19 | 20 | try: 21 | import monary 22 | except (ImportError, OSError) as exc: 23 | monary = None 24 | 25 | assert pymongo.has_c() 26 | 27 | # Use large document in tests? If SMALL, no, if LARGE, then yes. 28 | SMALL = False 29 | LARGE = True 30 | db = None 31 | raw_bson = None 32 | large_doc_keys = None 33 | collection_names = {LARGE: "large", SMALL: "small"} 34 | dtypes = {} 35 | raw_bsons = {} 36 | 37 | 38 | def _setup(): 39 | global db 40 | global raw_bson 41 | global large_doc_keys 42 | 43 | db = pymongo.MongoClient().bsonnumpy_test 44 | small = db[collection_names[SMALL]] 45 | small.drop() 46 | 47 | print("%d small docs, %d bytes each with 3 keys" % ( 48 | N_SMALL_DOCS, 49 | len(BSON.encode({'_id': ObjectId(), 'x': 1, 'y': math.pi})))) 50 | 51 | small.insert_many([ 52 | collections.OrderedDict([('x', 1), ('y', math.pi)]) 53 | for _ in range(N_SMALL_DOCS)]) 54 | 55 | dtypes[SMALL] = np.dtype([('x', np.int64), ('y', np.float64)]) 56 | 57 | large = db[collection_names[LARGE]] 58 | large.drop() 59 | # 2600 keys: 'a', 'aa', 'aaa', .., 'zz..z' 60 | large_doc_keys = [c * i for c in string.ascii_lowercase 61 | for i in range(1, 101)] 62 | large_doc = collections.OrderedDict([(k, math.pi) for k in large_doc_keys]) 63 | print("%d large docs, %dk each with %d keys" % ( 64 | N_LARGE_DOCS, len(BSON.encode(large_doc)) // 1024, len(large_doc_keys))) 65 | 66 | large.insert_many([large_doc.copy() for _ in range(N_LARGE_DOCS)]) 67 | 68 | dtypes[LARGE] = np.dtype([(k, np.float64) for k in large_doc_keys]) 69 | 70 | # Ignore for now that the first batch defaults to 101 documents. 71 | raw_bson_docs_small = [{'x': 1, 'y': math.pi} for _ in range(N_SMALL_DOCS)] 72 | raw_bson_small = BSON.encode({'ok': 1, 73 | 'cursor': { 74 | 'id': Int64(1234), 75 | 'ns': 'db.collection', 76 | 'firstBatch': raw_bson_docs_small}}) 77 | 78 | raw_bson_docs_large = [large_doc.copy() for _ in range(N_LARGE_DOCS)] 79 | raw_bson_large = BSON.encode({'ok': 1, 80 | 'cursor': { 81 | 'id': Int64(1234), 82 | 'ns': 'db.collection', 83 | 'firstBatch': raw_bson_docs_large}}) 84 | 85 | raw_bsons[SMALL] = raw_bson_small 86 | raw_bsons[LARGE] = raw_bson_large 87 | 88 | 89 | def _teardown(): 90 | db.collection.drop() 91 | 92 | 93 | bench_fns = collections.OrderedDict() 94 | 95 | 96 | def bench(name): 97 | def assign_name(fn): 98 | bench_fns[name] = fn 99 | return fn 100 | 101 | return assign_name 102 | 103 | 104 | @bench('conventional-to-ndarray') 105 | def conventional_func(use_large): 106 | collection = db[collection_names[use_large]] 107 | cursor = collection.find() 108 | dtype = dtypes[use_large] 109 | 110 | if use_large: 111 | np.array([tuple(doc[k] for k in large_doc_keys) for doc in cursor], 112 | dtype=dtype) 113 | else: 114 | np.array([(doc['x'], doc['y']) for doc in cursor], dtype=dtype) 115 | 116 | 117 | @bench('raw-bson-to-ndarray') 118 | def bson_numpy_func(use_large): 119 | raw_coll = db.get_collection( 120 | collection_names[use_large], 121 | codec_options=CodecOptions(document_class=RawBSONDocument)) 122 | 123 | cursor = raw_coll.find() 124 | dtype = dtypes[use_large] 125 | bsonnumpy.sequence_to_ndarray( 126 | (doc.raw for doc in cursor), dtype, raw_coll.count()) 127 | 128 | 129 | @bench('raw-batches-to-ndarray') 130 | def raw_bson_func(use_large): 131 | c = db[collection_names[use_large]] 132 | if not hasattr(c, 'find_raw_batches'): 133 | print("Wrong PyMongo: no 'find_raw_batches' feature") 134 | return 135 | 136 | dtype = dtypes[use_large] 137 | bsonnumpy.sequence_to_ndarray(c.find_raw_batches(), dtype, c.count()) 138 | 139 | 140 | @bench('monary') 141 | def monary_func(use_large): 142 | # Monary doesn't allow > 1024 keys, and it's too slow to benchmark anyway. 143 | if use_large: 144 | return 145 | 146 | m = monary.Monary() 147 | dtype = dtypes[use_large] 148 | m.query(db.name, collection_names[use_large], {}, dtype.names, 149 | ["float64"] * len(dtype.names)) 150 | 151 | 152 | @bench('parse-dtype') 153 | def raw_bson_func(use_large): 154 | dtype = dtypes[use_large] 155 | bsonnumpy.sequence_to_ndarray([], dtype, 0) 156 | 157 | 158 | @bench('decoded-cmd-reply') 159 | def bson_func(use_large): 160 | for _ in BSON(raw_bsons[use_large]).decode()['cursor']['firstBatch']: 161 | pass 162 | 163 | 164 | @bench('raw-cmd-reply') 165 | def raw_bson_func(use_large): 166 | options = CodecOptions(document_class=RawBSONDocument) 167 | for _ in BSON(raw_bsons[use_large]).decode(options)['cursor']['firstBatch']: 168 | pass 169 | 170 | 171 | parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, 172 | epilog=""" 173 | Available benchmark functions: 174 | %s 175 | """ % ("\n ".join(bench_fns.keys()),)) 176 | parser.add_argument('--large', action='store_true', 177 | help='only test with large documents') 178 | parser.add_argument('--small', action='store_true', 179 | help='only test with small documents') 180 | parser.add_argument('--test', action='store_true', 181 | help='quick test of benchmark.py') 182 | parser.add_argument('funcs', nargs='*', default=bench_fns.keys()) 183 | options = parser.parse_args() 184 | 185 | if options.test: 186 | N_LARGE_DOCS = 2 187 | N_SMALL_DOCS = 2 188 | N_TRIALS = 1 189 | else: 190 | N_LARGE_DOCS = 1000 191 | N_SMALL_DOCS = 100000 192 | N_TRIALS = 5 193 | 194 | # Run tests with both small and large documents. 195 | sizes = [SMALL, LARGE] 196 | if options.large and not options.small: 197 | sizes.remove(SMALL) 198 | if options.small and not options.large: 199 | sizes.remove(LARGE) 200 | 201 | for name in options.funcs: 202 | if name not in bench_fns: 203 | sys.stderr.write("Unknown function \"%s\"\n" % name) 204 | sys.stderr.write("Available functions:\n%s\n" % ("\n".join(bench_fns))) 205 | sys.exit(1) 206 | 207 | _setup() 208 | 209 | print() 210 | print("%25s: %7s %7s" % ("BENCH", "SMALL", "LARGE")) 211 | 212 | for name, fn in bench_fns.items(): 213 | if name in options.funcs: 214 | sys.stdout.write("%25s: " % name) 215 | sys.stdout.flush() 216 | 217 | # Test with small and large documents. 218 | for size in (SMALL, LARGE): 219 | if size not in sizes: 220 | sys.stdout.write("%7s" % "-") 221 | else: 222 | timer = timeit.Timer(partial(fn, size)) 223 | duration = min(timer.repeat(3, N_TRIALS)) / float(N_TRIALS) 224 | sys.stdout.write("%7.2f " % duration) 225 | sys.stdout.flush() 226 | 227 | sys.stdout.write("\n") 228 | 229 | _teardown() 230 | -------------------------------------------------------------------------------- /bsonnumpy/__init__.py: -------------------------------------------------------------------------------- 1 | from .version import __version__ 2 | from ._cbsonnumpy import * 3 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-atomic.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #include "bson-atomic.h" 19 | 20 | 21 | /* 22 | * We should only ever hit these on non-Windows systems, for which we require 23 | * pthread support. Therefore, we will avoid making a threading portability 24 | * for threads here and just use pthreads directly. 25 | */ 26 | 27 | 28 | #ifdef __BSON_NEED_BARRIER 29 | #include 30 | static pthread_mutex_t gBarrier = PTHREAD_MUTEX_INITIALIZER; 31 | void 32 | bson_memory_barrier (void) 33 | { 34 | pthread_mutex_lock (&gBarrier); 35 | pthread_mutex_unlock (&gBarrier); 36 | } 37 | #endif 38 | 39 | 40 | #ifdef __BSON_NEED_ATOMIC_32 41 | #include 42 | static pthread_mutex_t gSync32 = PTHREAD_MUTEX_INITIALIZER; 43 | int32_t 44 | bson_atomic_int_add (volatile int32_t *p, int32_t n) 45 | { 46 | int ret; 47 | 48 | pthread_mutex_lock (&gSync32); 49 | *p += n; 50 | ret = *p; 51 | pthread_mutex_unlock (&gSync32); 52 | 53 | return ret; 54 | } 55 | #endif 56 | 57 | 58 | #ifdef __BSON_NEED_ATOMIC_64 59 | #include 60 | static pthread_mutex_t gSync64 = PTHREAD_MUTEX_INITIALIZER; 61 | int64_t 62 | bson_atomic_int64_add (volatile int64_t *p, int64_t n) 63 | { 64 | int64_t ret; 65 | 66 | pthread_mutex_lock (&gSync64); 67 | *p += n; 68 | ret = *p; 69 | pthread_mutex_unlock (&gSync64); 70 | 71 | return ret; 72 | } 73 | #endif 74 | 75 | 76 | /* 77 | * The logic in the header is such that __BSON_NEED_ATOMIC_WINDOWS should only 78 | * be defined if neither __BSON_NEED_ATOMIC_32 nor __BSON_NEED_ATOMIC_64 are. 79 | */ 80 | 81 | 82 | #ifdef __BSON_NEED_ATOMIC_WINDOWS 83 | int32_t 84 | bson_atomic_int_add (volatile int32_t *p, int32_t n) 85 | { 86 | return InterlockedExchangeAdd (p, n) + n; 87 | } 88 | 89 | 90 | int64_t 91 | bson_atomic_int64_add (volatile int64_t *p, int64_t n) 92 | { 93 | return InterlockedExchangeAdd (p, n) + n; 94 | } 95 | #endif 96 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-atomic.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-2014 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_ATOMIC_H 21 | #define BSON_ATOMIC_H 22 | 23 | 24 | #include "bson-config.h" 25 | #include "bson-compat.h" 26 | #include "bson-macros.h" 27 | 28 | 29 | BSON_BEGIN_DECLS 30 | 31 | 32 | #if defined(__sun) && defined(__SVR4) 33 | /* Solaris */ 34 | #include 35 | #define bson_atomic_int_add(p, v) \ 36 | atomic_add_32_nv ((volatile uint32_t *) p, (v)) 37 | #define bson_atomic_int64_add(p, v) \ 38 | atomic_add_64_nv ((volatile uint64_t *) p, (v)) 39 | #elif defined(_WIN32) 40 | /* MSVC/MinGW */ 41 | #define __BSON_NEED_ATOMIC_WINDOWS 42 | #else 43 | #ifdef BSON_HAVE_ATOMIC_32_ADD_AND_FETCH 44 | #define bson_atomic_int_add(p, v) __sync_add_and_fetch ((p), (v)) 45 | #else 46 | #define __BSON_NEED_ATOMIC_32 47 | #endif 48 | #ifdef BSON_HAVE_ATOMIC_64_ADD_AND_FETCH 49 | #if BSON_GNUC_IS_VERSION(4, 1) 50 | /* 51 | * GCC 4.1 on i386 can generate buggy 64-bit atomic increment. 52 | * So we will work around with a fallback. 53 | * 54 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=40693 55 | */ 56 | #define __BSON_NEED_ATOMIC_64 57 | #else 58 | #define bson_atomic_int64_add(p, v) \ 59 | __sync_add_and_fetch ((volatile int64_t *) (p), (int64_t) (v)) 60 | #endif 61 | #else 62 | #define __BSON_NEED_ATOMIC_64 63 | #endif 64 | #endif 65 | 66 | #ifdef __BSON_NEED_ATOMIC_32 67 | BSON_EXPORT (int32_t) 68 | bson_atomic_int_add (volatile int32_t *p, int32_t n); 69 | #endif 70 | #ifdef __BSON_NEED_ATOMIC_64 71 | BSON_EXPORT (int64_t) 72 | bson_atomic_int64_add (volatile int64_t *p, int64_t n); 73 | #endif 74 | /* 75 | * The logic above is such that __BSON_NEED_ATOMIC_WINDOWS should only be 76 | * defined if neither __BSON_NEED_ATOMIC_32 nor __BSON_NEED_ATOMIC_64 are. 77 | */ 78 | #ifdef __BSON_NEED_ATOMIC_WINDOWS 79 | BSON_EXPORT (int32_t) 80 | bson_atomic_int_add (volatile int32_t *p, int32_t n); 81 | BSON_EXPORT (int64_t) 82 | bson_atomic_int64_add (volatile int64_t *p, int64_t n); 83 | #endif 84 | 85 | 86 | #if defined(_WIN32) 87 | #define bson_memory_barrier() MemoryBarrier () 88 | #elif defined(__GNUC__) 89 | #if BSON_GNUC_CHECK_VERSION(4, 1) 90 | #define bson_memory_barrier() __sync_synchronize () 91 | #else 92 | #warning "GCC Pre-4.1 discovered, using inline assembly for memory barrier." 93 | #define bson_memory_barrier() __asm__ volatile("" ::: "memory") 94 | #endif 95 | #elif defined(__SUNPRO_C) 96 | #include 97 | #define bson_memory_barrier() __machine_rw_barrier () 98 | #elif defined(__xlC__) 99 | #define bson_memory_barrier() __sync () 100 | #else 101 | #define __BSON_NEED_BARRIER 1 102 | #warning "Unknown compiler, using lock for compiler barrier." 103 | BSON_EXPORT (void) 104 | bson_memory_barrier (void); 105 | #endif 106 | 107 | 108 | BSON_END_DECLS 109 | 110 | 111 | #endif /* BSON_ATOMIC_H */ 112 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-clock.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #ifdef __APPLE__ 19 | #include 20 | #include 21 | #include 22 | #include 23 | #endif 24 | 25 | 26 | #include "bson-config.h" 27 | #include "bson-compat.h" 28 | 29 | 30 | #if defined(BSON_HAVE_CLOCK_GETTIME) 31 | #include 32 | #include 33 | #endif 34 | 35 | #include "bson-clock.h" 36 | 37 | 38 | /* 39 | *-------------------------------------------------------------------------- 40 | * 41 | * bson_gettimeofday -- 42 | * 43 | * A wrapper around gettimeofday() with fallback support for Windows. 44 | * 45 | * Returns: 46 | * 0 if successful. 47 | * 48 | * Side effects: 49 | * @tv is set. 50 | * 51 | *-------------------------------------------------------------------------- 52 | */ 53 | 54 | int 55 | bson_gettimeofday (struct timeval *tv) /* OUT */ 56 | { 57 | #if defined(_WIN32) 58 | #if defined(_MSC_VER) 59 | #define DELTA_EPOCH_IN_MICROSEC 11644473600000000Ui64 60 | #else 61 | #define DELTA_EPOCH_IN_MICROSEC 11644473600000000ULL 62 | #endif 63 | FILETIME ft; 64 | uint64_t tmp = 0; 65 | 66 | /* 67 | * The const value is shamelessly stolen from 68 | * http://www.boost.org/doc/libs/1_55_0/boost/chrono/detail/inlined/win/chrono.hpp 69 | * 70 | * File times are the number of 100 nanosecond intervals elapsed since 71 | * 12:00 am Jan 1, 1601 UTC. I haven't check the math particularly hard 72 | * 73 | * ... good luck 74 | */ 75 | 76 | if (tv) { 77 | GetSystemTimeAsFileTime (&ft); 78 | 79 | /* pull out of the filetime into a 64 bit uint */ 80 | tmp |= ft.dwHighDateTime; 81 | tmp <<= 32; 82 | tmp |= ft.dwLowDateTime; 83 | 84 | /* convert from 100's of nanosecs to microsecs */ 85 | tmp /= 10; 86 | 87 | /* adjust to unix epoch */ 88 | tmp -= DELTA_EPOCH_IN_MICROSEC; 89 | 90 | tv->tv_sec = (long) (tmp / 1000000UL); 91 | tv->tv_usec = (long) (tmp % 1000000UL); 92 | } 93 | 94 | return 0; 95 | #else 96 | return gettimeofday (tv, NULL); 97 | #endif 98 | } 99 | 100 | 101 | /* 102 | *-------------------------------------------------------------------------- 103 | * 104 | * bson_get_monotonic_time -- 105 | * 106 | * Returns the monotonic system time, if available. A best effort is 107 | * made to use the monotonic clock. However, some systems may not 108 | * support such a feature. 109 | * 110 | * Returns: 111 | * The monotonic clock in microseconds. 112 | * 113 | * Side effects: 114 | * None. 115 | * 116 | *-------------------------------------------------------------------------- 117 | */ 118 | 119 | int64_t 120 | bson_get_monotonic_time (void) 121 | { 122 | #if defined(BSON_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) 123 | struct timespec ts; 124 | /* ts.tv_sec may be a four-byte integer on 32 bit machines, so cast to 125 | * int64_t to avoid truncation. */ 126 | clock_gettime (CLOCK_MONOTONIC, &ts); 127 | return (((int64_t) ts.tv_sec * 1000000) + (ts.tv_nsec / 1000)); 128 | #elif defined(__APPLE__) 129 | static mach_timebase_info_data_t info = {0}; 130 | static double ratio = 0.0; 131 | 132 | if (!info.denom) { 133 | /* the value from mach_absolute_time () * info.numer / info.denom 134 | * is in nano seconds. So we have to divid by 1000.0 to get micro 135 | * seconds*/ 136 | mach_timebase_info (&info); 137 | ratio = (double) info.numer / (double) info.denom / 1000.0; 138 | } 139 | 140 | return mach_absolute_time () * ratio; 141 | #elif defined(_WIN32) 142 | /* Despite it's name, this is in milliseconds! */ 143 | int64_t ticks = GetTickCount64 (); 144 | return (ticks * 1000); 145 | #elif defined(__hpux__) 146 | int64_t nanosec = gethrtime (); 147 | return (nanosec / 1000UL); 148 | #else 149 | #pragma message "Monotonic clock is not yet supported on your platform." 150 | struct timeval tv; 151 | 152 | bson_gettimeofday (&tv); 153 | return ((int64_t) tv.tv_sec * 1000000) + tv.tv_usec; 154 | #endif 155 | } 156 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-clock.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_CLOCK_H 21 | #define BSON_CLOCK_H 22 | 23 | 24 | #include "bson-compat.h" 25 | #include "bson-macros.h" 26 | #include "bson-types.h" 27 | 28 | 29 | BSON_BEGIN_DECLS 30 | 31 | 32 | BSON_EXPORT (int64_t) 33 | bson_get_monotonic_time (void); 34 | BSON_EXPORT (int) 35 | bson_gettimeofday (struct timeval *tv); 36 | 37 | 38 | BSON_END_DECLS 39 | 40 | 41 | #endif /* BSON_CLOCK_H */ 42 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-compat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_COMPAT_H 21 | #define BSON_COMPAT_H 22 | 23 | 24 | #if defined(__MINGW32__) 25 | #if defined(__USE_MINGW_ANSI_STDIO) 26 | #if __USE_MINGW_ANSI_STDIO < 1 27 | #error "__USE_MINGW_ANSI_STDIO > 0 is required for correct PRI* macros" 28 | #endif 29 | #else 30 | #define __USE_MINGW_ANSI_STDIO 1 31 | #endif 32 | #endif 33 | 34 | #include "bson-config.h" 35 | #include "bson-macros.h" 36 | 37 | 38 | #ifdef BSON_OS_WIN32 39 | #if defined(_WIN32_WINNT) && (_WIN32_WINNT < 0x0600) 40 | #undef _WIN32_WINNT 41 | #endif 42 | #ifndef _WIN32_WINNT 43 | #define _WIN32_WINNT 0x0600 44 | #endif 45 | #ifndef NOMINMAX 46 | #define NOMINMAX 47 | #endif 48 | #include 49 | #ifndef WIN32_LEAN_AND_MEAN 50 | #define WIN32_LEAN_AND_MEAN 51 | #include 52 | #undef WIN32_LEAN_AND_MEAN 53 | #else 54 | #include 55 | #endif 56 | #include 57 | #include 58 | #endif 59 | 60 | 61 | #ifdef BSON_OS_UNIX 62 | #include 63 | #include 64 | #endif 65 | 66 | 67 | #include "bson-macros.h" 68 | 69 | 70 | #include 71 | #include 72 | #include 73 | #include 74 | #include 75 | #include 76 | #include 77 | #include 78 | #include 79 | #include 80 | #include 81 | 82 | 83 | BSON_BEGIN_DECLS 84 | 85 | #if !defined(_MSC_VER) || (_MSC_VER >= 1800) 86 | #include 87 | #endif 88 | #ifdef _MSC_VER 89 | #ifndef __cplusplus 90 | /* benign redefinition of type */ 91 | #pragma warning(disable : 4142) 92 | #ifndef _SSIZE_T_DEFINED 93 | #define _SSIZE_T_DEFINED 94 | typedef SSIZE_T ssize_t; 95 | #endif 96 | #ifndef _SIZE_T_DEFINED 97 | #define _SIZE_T_DEFINED 98 | typedef SIZE_T size_t; 99 | #endif 100 | #pragma warning(default : 4142) 101 | #else 102 | /* 103 | * MSVC++ does not include ssize_t, just size_t. 104 | * So we need to synthesize that as well. 105 | */ 106 | #pragma warning(disable : 4142) 107 | #ifndef _SSIZE_T_DEFINED 108 | #define _SSIZE_T_DEFINED 109 | typedef SSIZE_T ssize_t; 110 | #endif 111 | #pragma warning(default : 4142) 112 | #endif 113 | #ifndef PRIi32 114 | #define PRIi32 "d" 115 | #endif 116 | #ifndef PRId32 117 | #define PRId32 "d" 118 | #endif 119 | #ifndef PRIu32 120 | #define PRIu32 "u" 121 | #endif 122 | #ifndef PRIi64 123 | #define PRIi64 "I64i" 124 | #endif 125 | #ifndef PRId64 126 | #define PRId64 "I64i" 127 | #endif 128 | #ifndef PRIu64 129 | #define PRIu64 "I64u" 130 | #endif 131 | #endif 132 | 133 | #if defined(__MINGW32__) && !defined(INIT_ONCE_STATIC_INIT) 134 | #define INIT_ONCE_STATIC_INIT RTL_RUN_ONCE_INIT 135 | typedef RTL_RUN_ONCE INIT_ONCE; 136 | #endif 137 | 138 | #ifdef BSON_HAVE_STDBOOL_H 139 | #include 140 | #elif !defined(__bool_true_false_are_defined) 141 | #ifndef __cplusplus 142 | typedef signed char bool; 143 | #define false 0 144 | #define true 1 145 | #endif 146 | #define __bool_true_false_are_defined 1 147 | #endif 148 | 149 | 150 | #if defined(__GNUC__) 151 | #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) 152 | #define bson_sync_synchronize() __sync_synchronize () 153 | #elif defined(__i386__) || defined(__i486__) || defined(__i586__) || \ 154 | defined(__i686__) || defined(__x86_64__) 155 | #define bson_sync_synchronize() asm volatile("mfence" ::: "memory") 156 | #else 157 | #define bson_sync_synchronize() asm volatile("sync" ::: "memory") 158 | #endif 159 | #elif defined(_MSC_VER) 160 | #define bson_sync_synchronize() MemoryBarrier () 161 | #endif 162 | 163 | 164 | #if !defined(va_copy) && defined(__va_copy) 165 | #define va_copy(dst, src) __va_copy (dst, src) 166 | #endif 167 | 168 | 169 | #if !defined(va_copy) 170 | #define va_copy(dst, src) ((dst) = (src)) 171 | #endif 172 | 173 | 174 | BSON_END_DECLS 175 | 176 | 177 | #endif /* BSON_COMPAT_H */ 178 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #if !defined(BSON_INSIDE) && !defined(BSON_COMPILATION) 18 | #error "Only can be included directly." 19 | #endif 20 | 21 | #ifndef BSON_CONFIG_H 22 | #define BSON_CONFIG_H 23 | 24 | #define PY_SSIZE_T_CLEAN /* Make "s#" use Py_ssize_t rather than int. */ 25 | 26 | /* 27 | * Rely on CPython to make libbson portable 28 | */ 29 | #include 30 | 31 | /* 32 | * Define to 1234 for Little Endian, 4321 for Big Endian. 33 | */ 34 | #ifdef WORDS_BIGENDIAN 35 | # define BSON_BYTE_ORDER 4321 36 | #else 37 | # define BSON_BYTE_ORDER 1234 38 | #endif 39 | 40 | 41 | /* 42 | * Define to 1 if you have stdbool.h 43 | */ 44 | #define BSON_HAVE_STDBOOL_H 0 45 | #if BSON_HAVE_STDBOOL_H != 1 46 | # undef BSON_HAVE_STDBOOL_H 47 | #endif 48 | 49 | 50 | /* 51 | * Define to 1 for POSIX-like systems, 2 for Windows. 52 | */ 53 | #ifdef MS_WINDOWS 54 | # define BSON_OS 2 55 | #else 56 | # define BSON_OS 1 57 | #endif 58 | 59 | 60 | /* 61 | * Define to 1 if we have access to GCC 32-bit atomic builtins. 62 | * While this requires GCC 4.1+ in most cases, it is also architecture 63 | * dependent. For example, some PPC or ARM systems may not have it even 64 | * if it is a recent GCC version. 65 | */ 66 | #define BSON_HAVE_ATOMIC_32_ADD_AND_FETCH 0 67 | #if BSON_HAVE_ATOMIC_32_ADD_AND_FETCH != 1 68 | # undef BSON_HAVE_ATOMIC_32_ADD_AND_FETCH 69 | #endif 70 | 71 | /* 72 | * Similarly, define to 1 if we have access to GCC 64-bit atomic builtins. 73 | */ 74 | #define BSON_HAVE_ATOMIC_64_ADD_AND_FETCH 0 75 | #if BSON_HAVE_ATOMIC_64_ADD_AND_FETCH != 1 76 | # undef BSON_HAVE_ATOMIC_64_ADD_AND_FETCH 77 | #endif 78 | 79 | 80 | /* 81 | * Define to 1 if you have clock_gettime() available. 82 | */ 83 | #ifdef HAVE_CLOCK_GETTIME 84 | # define BSON_HAVE_CLOCK_GETTIME 1 85 | #endif 86 | 87 | #if BSON_HAVE_CLOCK_GETTIME != 1 88 | # undef BSON_HAVE_CLOCK_GETTIME 89 | #endif 90 | 91 | 92 | /* 93 | * Define to 1 if you have strings.h available on your platform. 94 | */ 95 | #define BSON_HAVE_STRINGS_H 0 96 | #if BSON_HAVE_STRINGS_H != 1 97 | # undef BSON_HAVE_STRINGS_H 98 | #endif 99 | 100 | 101 | /* 102 | * Define to 1 if you have strnlen available on your platform. 103 | */ 104 | #define BSON_HAVE_STRNLEN 0 105 | #if BSON_HAVE_STRNLEN != 1 106 | # undef BSON_HAVE_STRNLEN 107 | #endif 108 | 109 | 110 | /* 111 | * Define to 1 if you have snprintf available on your platform. 112 | */ 113 | #ifdef MS_WINDOWS 114 | # define BSON_HAVE_SNPRINTF 0 115 | #else 116 | # define BSON_HAVE_SNPRINTF 1 117 | #endif 118 | 119 | #if BSON_HAVE_SNPRINTF != 1 120 | # undef BSON_HAVE_SNPRINTF 121 | #endif 122 | 123 | 124 | /* 125 | * Define to 1 if you have gmtime_r available on your platform. 126 | */ 127 | #ifdef MS_WINDOWS 128 | # define BSON_HAVE_GMTIME_R 0 129 | #else 130 | # define BSON_HAVE_GMTIME_R 1 131 | #endif 132 | 133 | #if BSON_HAVE_GMTIME_R != 1 134 | # undef BSON_HAVE_GMTIME_R 135 | #endif 136 | 137 | 138 | /* 139 | * Define to 1 if you have reallocf available on your platform. 140 | */ 141 | #define BSON_HAVE_REALLOCF 0 142 | #if BSON_HAVE_REALLOCF != 1 143 | # undef BSON_HAVE_REALLOCF 144 | #endif 145 | 146 | 147 | /* 148 | * Define to 1 if you have struct timespec available on your platform. 149 | */ 150 | #ifdef HAVE_CLOCK_GETTIME 151 | # define BSON_HAVE_TIMESPEC 1 152 | #endif 153 | 154 | #if BSON_HAVE_TIMESPEC != 1 155 | # undef BSON_HAVE_TIMESPEC 156 | #endif 157 | 158 | 159 | /* 160 | * Define to 1 if you want extra aligned types in libbson 161 | */ 162 | #define BSON_EXTRA_ALIGN 1 163 | #if BSON_EXTRA_ALIGN != 1 164 | # undef BSON_EXTRA_ALIGN 165 | #endif 166 | 167 | 168 | /* 169 | * Define to 1 if you have SYS_gettid syscall 170 | */ 171 | #define BSON_HAVE_SYSCALL_TID 0 172 | #if BSON_HAVE_SYSCALL_TID != 1 173 | # undef BSON_HAVE_SYSCALL_TID 174 | #endif 175 | 176 | 177 | #ifdef MS_WINDOWS 178 | # define BSON_HAVE_RAND_R 0 179 | #else 180 | # define BSON_HAVE_RAND_R 1 181 | #endif 182 | #if BSON_HAVE_RAND_R != 1 183 | # undef BSON_HAVE_RAND_R 184 | #endif 185 | 186 | 187 | #define BSON_HAVE_STRLCPY 0 188 | #if BSON_HAVE_STRLCPY != 1 189 | # undef BSON_HAVE_STRLCPY 190 | #endif 191 | 192 | 193 | #endif /* BSON_CONFIG_H */ 194 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-context-private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_CONTEXT_PRIVATE_H 21 | #define BSON_CONTEXT_PRIVATE_H 22 | 23 | 24 | #include "bson-context.h" 25 | #include "common-thread-private.h" 26 | 27 | 28 | BSON_BEGIN_DECLS 29 | 30 | 31 | struct _bson_context_t { 32 | /* flags are defined in bson_context_flags_t */ 33 | int flags; 34 | int32_t seq32; 35 | int64_t seq64; 36 | uint8_t rand[5]; 37 | uint16_t pid; 38 | 39 | void (*oid_set_seq32) (bson_context_t *context, bson_oid_t *oid); 40 | void (*oid_set_seq64) (bson_context_t *context, bson_oid_t *oid); 41 | 42 | /* this function pointer allows us to mock gethostname for testing. */ 43 | void (*gethostname) (char *out); 44 | }; 45 | 46 | void 47 | _bson_context_set_oid_rand (bson_context_t *context, bson_oid_t *oid); 48 | 49 | 50 | BSON_END_DECLS 51 | 52 | 53 | #endif /* BSON_CONTEXT_PRIVATE_H */ 54 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-context.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_CONTEXT_H 21 | #define BSON_CONTEXT_H 22 | 23 | 24 | #include "bson-macros.h" 25 | #include "bson-types.h" 26 | 27 | 28 | BSON_BEGIN_DECLS 29 | 30 | 31 | BSON_EXPORT (bson_context_t *) 32 | bson_context_new (bson_context_flags_t flags); 33 | BSON_EXPORT (void) 34 | bson_context_destroy (bson_context_t *context); 35 | BSON_EXPORT (bson_context_t *) 36 | bson_context_get_default (void); 37 | 38 | 39 | BSON_END_DECLS 40 | 41 | 42 | #endif /* BSON_CONTEXT_H */ 43 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-decimal128.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_DECIMAL128_H 21 | #define BSON_DECIMAL128_H 22 | 23 | 24 | #include 25 | 26 | #include "bson-macros.h" 27 | #include "bson-config.h" 28 | #include "bson-types.h" 29 | 30 | 31 | /** 32 | * BSON_DECIMAL128_STRING: 33 | * 34 | * The length of a decimal128 string (with null terminator). 35 | * 36 | * 1 for the sign 37 | * 35 for digits and radix 38 | * 2 for exponent indicator and sign 39 | * 4 for exponent digits 40 | */ 41 | #define BSON_DECIMAL128_STRING 43 42 | #define BSON_DECIMAL128_INF "Infinity" 43 | #define BSON_DECIMAL128_NAN "NaN" 44 | 45 | 46 | BSON_BEGIN_DECLS 47 | 48 | BSON_EXPORT (void) 49 | bson_decimal128_to_string (const bson_decimal128_t *dec, char *str); 50 | 51 | 52 | /* Note: @string must be ASCII characters only! */ 53 | BSON_EXPORT (bool) 54 | bson_decimal128_from_string (const char *string, bson_decimal128_t *dec); 55 | 56 | BSON_EXPORT (bool) 57 | bson_decimal128_from_string_w_len (const char *string, 58 | int len, 59 | bson_decimal128_t *dec); 60 | 61 | BSON_END_DECLS 62 | 63 | 64 | #endif /* BSON_DECIMAL128_H */ 65 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-endian.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_ENDIAN_H 21 | #define BSON_ENDIAN_H 22 | 23 | 24 | #if defined(__sun) 25 | #include 26 | #endif 27 | 28 | #include "bson-config.h" 29 | #include "bson-macros.h" 30 | #include "bson-compat.h" 31 | 32 | 33 | BSON_BEGIN_DECLS 34 | 35 | 36 | #define BSON_BIG_ENDIAN 4321 37 | #define BSON_LITTLE_ENDIAN 1234 38 | 39 | 40 | #if defined(__sun) 41 | #define BSON_UINT16_SWAP_LE_BE(v) BSWAP_16 ((uint16_t) v) 42 | #define BSON_UINT32_SWAP_LE_BE(v) BSWAP_32 ((uint32_t) v) 43 | #define BSON_UINT64_SWAP_LE_BE(v) BSWAP_64 ((uint64_t) v) 44 | #elif defined(__clang__) && defined(__clang_major__) && \ 45 | defined(__clang_minor__) && (__clang_major__ >= 3) && \ 46 | (__clang_minor__ >= 1) 47 | #if __has_builtin(__builtin_bswap16) 48 | #define BSON_UINT16_SWAP_LE_BE(v) __builtin_bswap16 (v) 49 | #endif 50 | #if __has_builtin(__builtin_bswap32) 51 | #define BSON_UINT32_SWAP_LE_BE(v) __builtin_bswap32 (v) 52 | #endif 53 | #if __has_builtin(__builtin_bswap64) 54 | #define BSON_UINT64_SWAP_LE_BE(v) __builtin_bswap64 (v) 55 | #endif 56 | #elif defined(__GNUC__) && (__GNUC__ >= 4) 57 | #if __GNUC__ > 4 || (defined(__GNUC_MINOR__) && __GNUC_MINOR__ >= 3) 58 | #define BSON_UINT32_SWAP_LE_BE(v) __builtin_bswap32 ((uint32_t) v) 59 | #define BSON_UINT64_SWAP_LE_BE(v) __builtin_bswap64 ((uint64_t) v) 60 | #endif 61 | #if __GNUC__ > 4 || (defined(__GNUC_MINOR__) && __GNUC_MINOR__ >= 8) 62 | #define BSON_UINT16_SWAP_LE_BE(v) __builtin_bswap16 ((uint32_t) v) 63 | #endif 64 | #endif 65 | 66 | 67 | #ifndef BSON_UINT16_SWAP_LE_BE 68 | #define BSON_UINT16_SWAP_LE_BE(v) __bson_uint16_swap_slow ((uint16_t) v) 69 | #endif 70 | 71 | 72 | #ifndef BSON_UINT32_SWAP_LE_BE 73 | #define BSON_UINT32_SWAP_LE_BE(v) __bson_uint32_swap_slow ((uint32_t) v) 74 | #endif 75 | 76 | 77 | #ifndef BSON_UINT64_SWAP_LE_BE 78 | #define BSON_UINT64_SWAP_LE_BE(v) __bson_uint64_swap_slow ((uint64_t) v) 79 | #endif 80 | 81 | 82 | #if BSON_BYTE_ORDER == BSON_LITTLE_ENDIAN 83 | #define BSON_UINT16_FROM_LE(v) ((uint16_t) v) 84 | #define BSON_UINT16_TO_LE(v) ((uint16_t) v) 85 | #define BSON_UINT16_FROM_BE(v) BSON_UINT16_SWAP_LE_BE (v) 86 | #define BSON_UINT16_TO_BE(v) BSON_UINT16_SWAP_LE_BE (v) 87 | #define BSON_UINT32_FROM_LE(v) ((uint32_t) v) 88 | #define BSON_UINT32_TO_LE(v) ((uint32_t) v) 89 | #define BSON_UINT32_FROM_BE(v) BSON_UINT32_SWAP_LE_BE (v) 90 | #define BSON_UINT32_TO_BE(v) BSON_UINT32_SWAP_LE_BE (v) 91 | #define BSON_UINT64_FROM_LE(v) ((uint64_t) v) 92 | #define BSON_UINT64_TO_LE(v) ((uint64_t) v) 93 | #define BSON_UINT64_FROM_BE(v) BSON_UINT64_SWAP_LE_BE (v) 94 | #define BSON_UINT64_TO_BE(v) BSON_UINT64_SWAP_LE_BE (v) 95 | #define BSON_DOUBLE_FROM_LE(v) ((double) v) 96 | #define BSON_DOUBLE_TO_LE(v) ((double) v) 97 | #elif BSON_BYTE_ORDER == BSON_BIG_ENDIAN 98 | #define BSON_UINT16_FROM_LE(v) BSON_UINT16_SWAP_LE_BE (v) 99 | #define BSON_UINT16_TO_LE(v) BSON_UINT16_SWAP_LE_BE (v) 100 | #define BSON_UINT16_FROM_BE(v) ((uint16_t) v) 101 | #define BSON_UINT16_TO_BE(v) ((uint16_t) v) 102 | #define BSON_UINT32_FROM_LE(v) BSON_UINT32_SWAP_LE_BE (v) 103 | #define BSON_UINT32_TO_LE(v) BSON_UINT32_SWAP_LE_BE (v) 104 | #define BSON_UINT32_FROM_BE(v) ((uint32_t) v) 105 | #define BSON_UINT32_TO_BE(v) ((uint32_t) v) 106 | #define BSON_UINT64_FROM_LE(v) BSON_UINT64_SWAP_LE_BE (v) 107 | #define BSON_UINT64_TO_LE(v) BSON_UINT64_SWAP_LE_BE (v) 108 | #define BSON_UINT64_FROM_BE(v) ((uint64_t) v) 109 | #define BSON_UINT64_TO_BE(v) ((uint64_t) v) 110 | #define BSON_DOUBLE_FROM_LE(v) (__bson_double_swap_slow (v)) 111 | #define BSON_DOUBLE_TO_LE(v) (__bson_double_swap_slow (v)) 112 | #else 113 | #error "The endianness of target architecture is unknown." 114 | #endif 115 | 116 | 117 | /* 118 | *-------------------------------------------------------------------------- 119 | * 120 | * __bson_uint16_swap_slow -- 121 | * 122 | * Fallback endianness conversion for 16-bit integers. 123 | * 124 | * Returns: 125 | * The endian swapped version. 126 | * 127 | * Side effects: 128 | * None. 129 | * 130 | *-------------------------------------------------------------------------- 131 | */ 132 | 133 | static BSON_INLINE uint16_t 134 | __bson_uint16_swap_slow (uint16_t v) /* IN */ 135 | { 136 | return ((v & 0x00FF) << 8) | ((v & 0xFF00) >> 8); 137 | } 138 | 139 | 140 | /* 141 | *-------------------------------------------------------------------------- 142 | * 143 | * __bson_uint32_swap_slow -- 144 | * 145 | * Fallback endianness conversion for 32-bit integers. 146 | * 147 | * Returns: 148 | * The endian swapped version. 149 | * 150 | * Side effects: 151 | * None. 152 | * 153 | *-------------------------------------------------------------------------- 154 | */ 155 | 156 | static BSON_INLINE uint32_t 157 | __bson_uint32_swap_slow (uint32_t v) /* IN */ 158 | { 159 | return ((v & 0x000000FFU) << 24) | ((v & 0x0000FF00U) << 8) | 160 | ((v & 0x00FF0000U) >> 8) | ((v & 0xFF000000U) >> 24); 161 | } 162 | 163 | 164 | /* 165 | *-------------------------------------------------------------------------- 166 | * 167 | * __bson_uint64_swap_slow -- 168 | * 169 | * Fallback endianness conversion for 64-bit integers. 170 | * 171 | * Returns: 172 | * The endian swapped version. 173 | * 174 | * Side effects: 175 | * None. 176 | * 177 | *-------------------------------------------------------------------------- 178 | */ 179 | 180 | static BSON_INLINE uint64_t 181 | __bson_uint64_swap_slow (uint64_t v) /* IN */ 182 | { 183 | return ((v & 0x00000000000000FFULL) << 56) | 184 | ((v & 0x000000000000FF00ULL) << 40) | 185 | ((v & 0x0000000000FF0000ULL) << 24) | 186 | ((v & 0x00000000FF000000ULL) << 8) | 187 | ((v & 0x000000FF00000000ULL) >> 8) | 188 | ((v & 0x0000FF0000000000ULL) >> 24) | 189 | ((v & 0x00FF000000000000ULL) >> 40) | 190 | ((v & 0xFF00000000000000ULL) >> 56); 191 | } 192 | 193 | 194 | /* 195 | *-------------------------------------------------------------------------- 196 | * 197 | * __bson_double_swap_slow -- 198 | * 199 | * Fallback endianness conversion for double floating point. 200 | * 201 | * Returns: 202 | * The endian swapped version. 203 | * 204 | * Side effects: 205 | * None. 206 | * 207 | *-------------------------------------------------------------------------- 208 | */ 209 | 210 | BSON_STATIC_ASSERT2 (sizeof_uint64_t, sizeof (double) == sizeof (uint64_t)); 211 | 212 | static BSON_INLINE double 213 | __bson_double_swap_slow (double v) /* IN */ 214 | { 215 | uint64_t uv; 216 | 217 | memcpy (&uv, &v, sizeof (v)); 218 | uv = BSON_UINT64_SWAP_LE_BE (uv); 219 | memcpy (&v, &uv, sizeof (v)); 220 | 221 | return v; 222 | } 223 | 224 | BSON_END_DECLS 225 | 226 | 227 | #endif /* BSON_ENDIAN_H */ 228 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-error.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #include 19 | #include 20 | 21 | #include "bson-compat.h" 22 | #include "bson-config.h" 23 | #include "bson-error.h" 24 | #include "bson-memory.h" 25 | #include "bson-string.h" 26 | #include "bson-types.h" 27 | 28 | 29 | /* 30 | *-------------------------------------------------------------------------- 31 | * 32 | * bson_set_error -- 33 | * 34 | * Initializes @error using the parameters specified. 35 | * 36 | * @domain is an application specific error domain which should 37 | * describe which module initiated the error. Think of this as the 38 | * exception type. 39 | * 40 | * @code is the @domain specific error code. 41 | * 42 | * @format is used to generate the format string. It uses vsnprintf() 43 | * internally so the format should match what you would use there. 44 | * 45 | * Parameters: 46 | * @error: A #bson_error_t. 47 | * @domain: The error domain. 48 | * @code: The error code. 49 | * @format: A printf style format string. 50 | * 51 | * Returns: 52 | * None. 53 | * 54 | * Side effects: 55 | * @error is initialized. 56 | * 57 | *-------------------------------------------------------------------------- 58 | */ 59 | 60 | void 61 | bson_set_error (bson_error_t *error, /* OUT */ 62 | uint32_t domain, /* IN */ 63 | uint32_t code, /* IN */ 64 | const char *format, /* IN */ 65 | ...) /* IN */ 66 | { 67 | va_list args; 68 | 69 | if (error) { 70 | error->domain = domain; 71 | error->code = code; 72 | 73 | va_start (args, format); 74 | bson_vsnprintf (error->message, sizeof error->message, format, args); 75 | va_end (args); 76 | 77 | error->message[sizeof error->message - 1] = '\0'; 78 | } 79 | } 80 | 81 | 82 | /* 83 | *-------------------------------------------------------------------------- 84 | * 85 | * bson_strerror_r -- 86 | * 87 | * This is a reentrant safe macro for strerror. 88 | * 89 | * The resulting string may be stored in @buf. 90 | * 91 | * Returns: 92 | * A pointer to a static string or @buf. 93 | * 94 | * Side effects: 95 | * None. 96 | * 97 | *-------------------------------------------------------------------------- 98 | */ 99 | 100 | char * 101 | bson_strerror_r (int err_code, /* IN */ 102 | char *buf, /* IN */ 103 | size_t buflen) /* IN */ 104 | { 105 | static const char *unknown_msg = "Unknown error"; 106 | char *ret = NULL; 107 | 108 | #if defined(_WIN32) 109 | if (strerror_s (buf, buflen, err_code) != 0) { 110 | ret = buf; 111 | } 112 | #elif defined(__GNUC__) && defined(_GNU_SOURCE) 113 | ret = strerror_r (err_code, buf, buflen); 114 | #else /* XSI strerror_r */ 115 | if (strerror_r (err_code, buf, buflen) == 0) { 116 | ret = buf; 117 | } 118 | #endif 119 | 120 | if (!ret) { 121 | bson_strncpy (buf, unknown_msg, buflen); 122 | ret = buf; 123 | } 124 | 125 | return ret; 126 | } 127 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-error.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_ERROR_H 21 | #define BSON_ERROR_H 22 | 23 | 24 | #include "bson-compat.h" 25 | #include "bson-macros.h" 26 | #include "bson-types.h" 27 | 28 | 29 | BSON_BEGIN_DECLS 30 | 31 | 32 | #define BSON_ERROR_JSON 1 33 | #define BSON_ERROR_READER 2 34 | #define BSON_ERROR_INVALID 3 35 | 36 | 37 | BSON_EXPORT (void) 38 | bson_set_error (bson_error_t *error, 39 | uint32_t domain, 40 | uint32_t code, 41 | const char *format, 42 | ...) BSON_GNUC_PRINTF (4, 5); 43 | BSON_EXPORT (char *) 44 | bson_strerror_r (int err_code, char *buf, size_t buflen); 45 | 46 | 47 | BSON_END_DECLS 48 | 49 | 50 | #endif /* BSON_ERROR_H */ 51 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-iso8601-private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_ISO8601_PRIVATE_H 21 | #define BSON_ISO8601_PRIVATE_H 22 | 23 | 24 | #include "bson-compat.h" 25 | #include "bson-macros.h" 26 | #include "bson-string.h" 27 | 28 | 29 | BSON_BEGIN_DECLS 30 | 31 | bool 32 | _bson_iso8601_date_parse (const char *str, 33 | int32_t len, 34 | int64_t *out, 35 | bson_error_t *error); 36 | 37 | /** 38 | * _bson_iso8601_date_format: 39 | * @msecs_since_epoch: A positive number of milliseconds since Jan 1, 1970. 40 | * @str: The string to append the ISO8601-formatted to. 41 | * 42 | * Appends a date formatted like "2012-12-24T12:15:30.500Z" to @str. 43 | */ 44 | void 45 | _bson_iso8601_date_format (int64_t msecs_since_epoch, bson_string_t *str); 46 | 47 | BSON_END_DECLS 48 | 49 | 50 | #endif /* BSON_ISO8601_PRIVATE_H */ 51 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-iso8601.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #include "bson-compat.h" 19 | #include "bson-macros.h" 20 | #include "bson-error.h" 21 | #include "bson-iso8601-private.h" 22 | #include "bson-json.h" 23 | #include "bson-timegm-private.h" 24 | 25 | 26 | static bool 27 | get_tok (const char *terminals, 28 | const char **ptr, 29 | int32_t *remaining, 30 | const char **out, 31 | int32_t *out_len) 32 | { 33 | const char *terminal; 34 | bool found_terminal = false; 35 | 36 | if (!*remaining) { 37 | *out = ""; 38 | *out_len = 0; 39 | } 40 | 41 | *out = *ptr; 42 | *out_len = -1; 43 | 44 | for (; *remaining && !found_terminal; 45 | (*ptr)++, (*remaining)--, (*out_len)++) { 46 | for (terminal = terminals; *terminal; terminal++) { 47 | if (**ptr == *terminal) { 48 | found_terminal = true; 49 | break; 50 | } 51 | } 52 | } 53 | 54 | if (!found_terminal) { 55 | (*out_len)++; 56 | } 57 | 58 | return found_terminal; 59 | } 60 | 61 | static bool 62 | digits_only (const char *str, int32_t len) 63 | { 64 | int i; 65 | 66 | for (i = 0; i < len; i++) { 67 | if (!isdigit (str[i])) { 68 | return false; 69 | } 70 | } 71 | 72 | return true; 73 | } 74 | 75 | static bool 76 | parse_num (const char *str, 77 | int32_t len, 78 | int32_t digits, 79 | int32_t min, 80 | int32_t max, 81 | int32_t *out) 82 | { 83 | int i; 84 | int magnitude = 1; 85 | int32_t value = 0; 86 | 87 | if ((digits >= 0 && len != digits) || !digits_only (str, len)) { 88 | return false; 89 | } 90 | 91 | for (i = 1; i <= len; i++, magnitude *= 10) { 92 | value += (str[len - i] - '0') * magnitude; 93 | } 94 | 95 | if (value < min || value > max) { 96 | return false; 97 | } 98 | 99 | *out = value; 100 | 101 | return true; 102 | } 103 | 104 | bool 105 | _bson_iso8601_date_parse (const char *str, 106 | int32_t len, 107 | int64_t *out, 108 | bson_error_t *error) 109 | { 110 | const char *ptr; 111 | int32_t remaining = len; 112 | 113 | const char *year_ptr = NULL; 114 | const char *month_ptr = NULL; 115 | const char *day_ptr = NULL; 116 | const char *hour_ptr = NULL; 117 | const char *min_ptr = NULL; 118 | const char *sec_ptr = NULL; 119 | const char *millis_ptr = NULL; 120 | const char *tz_ptr = NULL; 121 | 122 | int32_t year_len = 0; 123 | int32_t month_len = 0; 124 | int32_t day_len = 0; 125 | int32_t hour_len = 0; 126 | int32_t min_len = 0; 127 | int32_t sec_len = 0; 128 | int32_t millis_len = 0; 129 | int32_t tz_len = 0; 130 | 131 | int32_t year; 132 | int32_t month; 133 | int32_t day; 134 | int32_t hour; 135 | int32_t min; 136 | int32_t sec = 0; 137 | int64_t millis = 0; 138 | int32_t tz_adjustment = 0; 139 | 140 | struct bson_tm posix_date = {0}; 141 | 142 | #define DATE_PARSE_ERR(msg) \ 143 | bson_set_error (error, \ 144 | BSON_ERROR_JSON, \ 145 | BSON_JSON_ERROR_READ_INVALID_PARAM, \ 146 | "Could not parse \"%s\" as date: " msg, \ 147 | str); \ 148 | return false 149 | 150 | #define DEFAULT_DATE_PARSE_ERR \ 151 | DATE_PARSE_ERR ("use ISO8601 format yyyy-mm-ddThh:mm plus timezone, either" \ 152 | " \"Z\" or like \"+0500\"") 153 | 154 | ptr = str; 155 | 156 | /* we have to match at least yyyy-mm-ddThh:mm */ 157 | if (!(get_tok ("-", &ptr, &remaining, &year_ptr, &year_len) && 158 | get_tok ("-", &ptr, &remaining, &month_ptr, &month_len) && 159 | get_tok ("T", &ptr, &remaining, &day_ptr, &day_len) && 160 | get_tok (":", &ptr, &remaining, &hour_ptr, &hour_len) && 161 | get_tok (":+-Z", &ptr, &remaining, &min_ptr, &min_len))) { 162 | DEFAULT_DATE_PARSE_ERR; 163 | } 164 | 165 | /* if the minute has a ':' at the end look for seconds */ 166 | if (min_ptr[min_len] == ':') { 167 | if (remaining < 2) { 168 | DATE_PARSE_ERR ("reached end of date while looking for seconds"); 169 | } 170 | 171 | get_tok (".+-Z", &ptr, &remaining, &sec_ptr, &sec_len); 172 | 173 | if (!sec_len) { 174 | DATE_PARSE_ERR ("minute ends in \":\" seconds is required"); 175 | } 176 | } 177 | 178 | /* if we had a second and it is followed by a '.' look for milliseconds */ 179 | if (sec_len && sec_ptr[sec_len] == '.') { 180 | if (remaining < 2) { 181 | DATE_PARSE_ERR ("reached end of date while looking for milliseconds"); 182 | } 183 | 184 | get_tok ("+-Z", &ptr, &remaining, &millis_ptr, &millis_len); 185 | 186 | if (!millis_len) { 187 | DATE_PARSE_ERR ("seconds ends in \".\", milliseconds is required"); 188 | } 189 | } 190 | 191 | /* backtrack by 1 to put ptr on the timezone */ 192 | ptr--; 193 | remaining++; 194 | 195 | get_tok ("", &ptr, &remaining, &tz_ptr, &tz_len); 196 | 197 | if (!parse_num (year_ptr, year_len, 4, -9999, 9999, &year)) { 198 | DATE_PARSE_ERR ("year must be an integer"); 199 | } 200 | 201 | /* values are as in struct tm */ 202 | year -= 1900; 203 | 204 | if (!parse_num (month_ptr, month_len, 2, 1, 12, &month)) { 205 | DATE_PARSE_ERR ("month must be an integer"); 206 | } 207 | 208 | /* values are as in struct tm */ 209 | month -= 1; 210 | 211 | if (!parse_num (day_ptr, day_len, 2, 1, 31, &day)) { 212 | DATE_PARSE_ERR ("day must be an integer"); 213 | } 214 | 215 | if (!parse_num (hour_ptr, hour_len, 2, 0, 23, &hour)) { 216 | DATE_PARSE_ERR ("hour must be an integer"); 217 | } 218 | 219 | if (!parse_num (min_ptr, min_len, 2, 0, 59, &min)) { 220 | DATE_PARSE_ERR ("minute must be an integer"); 221 | } 222 | 223 | if (sec_len && !parse_num (sec_ptr, sec_len, 2, 0, 60, &sec)) { 224 | DATE_PARSE_ERR ("seconds must be an integer"); 225 | } 226 | 227 | if (tz_len > 0) { 228 | if (tz_ptr[0] == 'Z' && tz_len == 1) { 229 | /* valid */ 230 | } else if (tz_ptr[0] == '+' || tz_ptr[0] == '-') { 231 | int32_t tz_hour; 232 | int32_t tz_min; 233 | 234 | if (tz_len != 5 || !digits_only (tz_ptr + 1, 4)) { 235 | DATE_PARSE_ERR ("could not parse timezone"); 236 | } 237 | 238 | if (!parse_num (tz_ptr + 1, 2, -1, -23, 23, &tz_hour)) { 239 | DATE_PARSE_ERR ("timezone hour must be at most 23"); 240 | } 241 | 242 | if (!parse_num (tz_ptr + 3, 2, -1, 0, 59, &tz_min)) { 243 | DATE_PARSE_ERR ("timezone minute must be at most 59"); 244 | } 245 | 246 | /* we inflect the meaning of a 'positive' timezone. Those are hours 247 | * we have to subtract, and vice versa */ 248 | tz_adjustment = 249 | (tz_ptr[0] == '-' ? 1 : -1) * ((tz_min * 60) + (tz_hour * 60 * 60)); 250 | 251 | if (!(tz_adjustment > -86400 && tz_adjustment < 86400)) { 252 | DATE_PARSE_ERR ("timezone offset must be less than 24 hours"); 253 | } 254 | } else { 255 | DATE_PARSE_ERR ("timezone is required"); 256 | } 257 | } 258 | 259 | if (millis_len > 0) { 260 | int i; 261 | int magnitude; 262 | millis = 0; 263 | 264 | if (millis_len > 3 || !digits_only (millis_ptr, millis_len)) { 265 | DATE_PARSE_ERR ("milliseconds must be an integer"); 266 | } 267 | 268 | for (i = 1, magnitude = 1; i <= millis_len; i++, magnitude *= 10) { 269 | millis += (millis_ptr[millis_len - i] - '0') * magnitude; 270 | } 271 | 272 | if (millis_len == 1) { 273 | millis *= 100; 274 | } else if (millis_len == 2) { 275 | millis *= 10; 276 | } 277 | 278 | if (millis < 0 || millis > 1000) { 279 | DATE_PARSE_ERR ("milliseconds must be at least 0 and less than 1000"); 280 | } 281 | } 282 | 283 | posix_date.tm_sec = sec; 284 | posix_date.tm_min = min; 285 | posix_date.tm_hour = hour; 286 | posix_date.tm_mday = day; 287 | posix_date.tm_mon = month; 288 | posix_date.tm_year = year; 289 | posix_date.tm_wday = 0; 290 | posix_date.tm_yday = 0; 291 | 292 | millis = 1000 * _bson_timegm (&posix_date) + millis; 293 | millis += tz_adjustment * 1000; 294 | *out = millis; 295 | 296 | return true; 297 | } 298 | 299 | 300 | void 301 | _bson_iso8601_date_format (int64_t msec_since_epoch, bson_string_t *str) 302 | { 303 | time_t t; 304 | int64_t msecs_part; 305 | char buf[64]; 306 | 307 | msecs_part = msec_since_epoch % 1000; 308 | t = (time_t) (msec_since_epoch / 1000); 309 | 310 | #ifdef BSON_HAVE_GMTIME_R 311 | { 312 | struct tm posix_date; 313 | gmtime_r (&t, &posix_date); 314 | strftime (buf, sizeof buf, "%Y-%m-%dT%H:%M:%S", &posix_date); 315 | } 316 | #elif defined(_MSC_VER) 317 | { 318 | /* Windows gmtime_s is thread-safe */ 319 | struct tm time_buf; 320 | gmtime_s (&time_buf, &t); 321 | strftime (buf, sizeof buf, "%Y-%m-%dT%H:%M:%S", &time_buf); 322 | } 323 | #else 324 | strftime (buf, sizeof buf, "%Y-%m-%dT%H:%M:%S", gmtime (&t)); 325 | #endif 326 | 327 | if (msecs_part) { 328 | bson_string_append_printf (str, "%s.%03" PRId64 "Z", buf, msecs_part); 329 | } else { 330 | bson_string_append (str, buf); 331 | bson_string_append_c (str, 'Z'); 332 | } 333 | } 334 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-json.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_JSON_H 21 | #define BSON_JSON_H 22 | 23 | 24 | #include "bson.h" 25 | 26 | 27 | BSON_BEGIN_DECLS 28 | 29 | 30 | typedef struct _bson_json_reader_t bson_json_reader_t; 31 | 32 | 33 | typedef enum { 34 | BSON_JSON_ERROR_READ_CORRUPT_JS = 1, 35 | BSON_JSON_ERROR_READ_INVALID_PARAM, 36 | BSON_JSON_ERROR_READ_CB_FAILURE, 37 | } bson_json_error_code_t; 38 | 39 | 40 | typedef ssize_t (*bson_json_reader_cb) (void *handle, 41 | uint8_t *buf, 42 | size_t count); 43 | typedef void (*bson_json_destroy_cb) (void *handle); 44 | 45 | 46 | BSON_EXPORT (bson_json_reader_t *) 47 | bson_json_reader_new (void *data, 48 | bson_json_reader_cb cb, 49 | bson_json_destroy_cb dcb, 50 | bool allow_multiple, 51 | size_t buf_size); 52 | BSON_EXPORT (bson_json_reader_t *) 53 | bson_json_reader_new_from_fd (int fd, bool close_on_destroy); 54 | BSON_EXPORT (bson_json_reader_t *) 55 | bson_json_reader_new_from_file (const char *filename, bson_error_t *error); 56 | BSON_EXPORT (void) 57 | bson_json_reader_destroy (bson_json_reader_t *reader); 58 | BSON_EXPORT (int) 59 | bson_json_reader_read (bson_json_reader_t *reader, 60 | bson_t *bson, 61 | bson_error_t *error); 62 | BSON_EXPORT (bson_json_reader_t *) 63 | bson_json_data_reader_new (bool allow_multiple, size_t size); 64 | BSON_EXPORT (void) 65 | bson_json_data_reader_ingest (bson_json_reader_t *reader, 66 | const uint8_t *data, 67 | size_t len); 68 | 69 | 70 | BSON_END_DECLS 71 | 72 | 73 | #endif /* BSON_JSON_H */ 74 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-keys.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #include 19 | 20 | #include "bson-keys.h" 21 | #include "bson-string.h" 22 | 23 | 24 | static const char *gUint32Strs[] = { 25 | "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", 26 | "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", 27 | "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", 28 | "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", 29 | "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", 30 | "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", 31 | "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", 32 | "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", 33 | "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", 34 | "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", 35 | "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", 36 | "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", 37 | "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", 38 | "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", 39 | "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", 40 | "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", 41 | "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", 42 | "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", 43 | "198", "199", "200", "201", "202", "203", "204", "205", "206", "207", "208", 44 | "209", "210", "211", "212", "213", "214", "215", "216", "217", "218", "219", 45 | "220", "221", "222", "223", "224", "225", "226", "227", "228", "229", "230", 46 | "231", "232", "233", "234", "235", "236", "237", "238", "239", "240", "241", 47 | "242", "243", "244", "245", "246", "247", "248", "249", "250", "251", "252", 48 | "253", "254", "255", "256", "257", "258", "259", "260", "261", "262", "263", 49 | "264", "265", "266", "267", "268", "269", "270", "271", "272", "273", "274", 50 | "275", "276", "277", "278", "279", "280", "281", "282", "283", "284", "285", 51 | "286", "287", "288", "289", "290", "291", "292", "293", "294", "295", "296", 52 | "297", "298", "299", "300", "301", "302", "303", "304", "305", "306", "307", 53 | "308", "309", "310", "311", "312", "313", "314", "315", "316", "317", "318", 54 | "319", "320", "321", "322", "323", "324", "325", "326", "327", "328", "329", 55 | "330", "331", "332", "333", "334", "335", "336", "337", "338", "339", "340", 56 | "341", "342", "343", "344", "345", "346", "347", "348", "349", "350", "351", 57 | "352", "353", "354", "355", "356", "357", "358", "359", "360", "361", "362", 58 | "363", "364", "365", "366", "367", "368", "369", "370", "371", "372", "373", 59 | "374", "375", "376", "377", "378", "379", "380", "381", "382", "383", "384", 60 | "385", "386", "387", "388", "389", "390", "391", "392", "393", "394", "395", 61 | "396", "397", "398", "399", "400", "401", "402", "403", "404", "405", "406", 62 | "407", "408", "409", "410", "411", "412", "413", "414", "415", "416", "417", 63 | "418", "419", "420", "421", "422", "423", "424", "425", "426", "427", "428", 64 | "429", "430", "431", "432", "433", "434", "435", "436", "437", "438", "439", 65 | "440", "441", "442", "443", "444", "445", "446", "447", "448", "449", "450", 66 | "451", "452", "453", "454", "455", "456", "457", "458", "459", "460", "461", 67 | "462", "463", "464", "465", "466", "467", "468", "469", "470", "471", "472", 68 | "473", "474", "475", "476", "477", "478", "479", "480", "481", "482", "483", 69 | "484", "485", "486", "487", "488", "489", "490", "491", "492", "493", "494", 70 | "495", "496", "497", "498", "499", "500", "501", "502", "503", "504", "505", 71 | "506", "507", "508", "509", "510", "511", "512", "513", "514", "515", "516", 72 | "517", "518", "519", "520", "521", "522", "523", "524", "525", "526", "527", 73 | "528", "529", "530", "531", "532", "533", "534", "535", "536", "537", "538", 74 | "539", "540", "541", "542", "543", "544", "545", "546", "547", "548", "549", 75 | "550", "551", "552", "553", "554", "555", "556", "557", "558", "559", "560", 76 | "561", "562", "563", "564", "565", "566", "567", "568", "569", "570", "571", 77 | "572", "573", "574", "575", "576", "577", "578", "579", "580", "581", "582", 78 | "583", "584", "585", "586", "587", "588", "589", "590", "591", "592", "593", 79 | "594", "595", "596", "597", "598", "599", "600", "601", "602", "603", "604", 80 | "605", "606", "607", "608", "609", "610", "611", "612", "613", "614", "615", 81 | "616", "617", "618", "619", "620", "621", "622", "623", "624", "625", "626", 82 | "627", "628", "629", "630", "631", "632", "633", "634", "635", "636", "637", 83 | "638", "639", "640", "641", "642", "643", "644", "645", "646", "647", "648", 84 | "649", "650", "651", "652", "653", "654", "655", "656", "657", "658", "659", 85 | "660", "661", "662", "663", "664", "665", "666", "667", "668", "669", "670", 86 | "671", "672", "673", "674", "675", "676", "677", "678", "679", "680", "681", 87 | "682", "683", "684", "685", "686", "687", "688", "689", "690", "691", "692", 88 | "693", "694", "695", "696", "697", "698", "699", "700", "701", "702", "703", 89 | "704", "705", "706", "707", "708", "709", "710", "711", "712", "713", "714", 90 | "715", "716", "717", "718", "719", "720", "721", "722", "723", "724", "725", 91 | "726", "727", "728", "729", "730", "731", "732", "733", "734", "735", "736", 92 | "737", "738", "739", "740", "741", "742", "743", "744", "745", "746", "747", 93 | "748", "749", "750", "751", "752", "753", "754", "755", "756", "757", "758", 94 | "759", "760", "761", "762", "763", "764", "765", "766", "767", "768", "769", 95 | "770", "771", "772", "773", "774", "775", "776", "777", "778", "779", "780", 96 | "781", "782", "783", "784", "785", "786", "787", "788", "789", "790", "791", 97 | "792", "793", "794", "795", "796", "797", "798", "799", "800", "801", "802", 98 | "803", "804", "805", "806", "807", "808", "809", "810", "811", "812", "813", 99 | "814", "815", "816", "817", "818", "819", "820", "821", "822", "823", "824", 100 | "825", "826", "827", "828", "829", "830", "831", "832", "833", "834", "835", 101 | "836", "837", "838", "839", "840", "841", "842", "843", "844", "845", "846", 102 | "847", "848", "849", "850", "851", "852", "853", "854", "855", "856", "857", 103 | "858", "859", "860", "861", "862", "863", "864", "865", "866", "867", "868", 104 | "869", "870", "871", "872", "873", "874", "875", "876", "877", "878", "879", 105 | "880", "881", "882", "883", "884", "885", "886", "887", "888", "889", "890", 106 | "891", "892", "893", "894", "895", "896", "897", "898", "899", "900", "901", 107 | "902", "903", "904", "905", "906", "907", "908", "909", "910", "911", "912", 108 | "913", "914", "915", "916", "917", "918", "919", "920", "921", "922", "923", 109 | "924", "925", "926", "927", "928", "929", "930", "931", "932", "933", "934", 110 | "935", "936", "937", "938", "939", "940", "941", "942", "943", "944", "945", 111 | "946", "947", "948", "949", "950", "951", "952", "953", "954", "955", "956", 112 | "957", "958", "959", "960", "961", "962", "963", "964", "965", "966", "967", 113 | "968", "969", "970", "971", "972", "973", "974", "975", "976", "977", "978", 114 | "979", "980", "981", "982", "983", "984", "985", "986", "987", "988", "989", 115 | "990", "991", "992", "993", "994", "995", "996", "997", "998", "999"}; 116 | 117 | 118 | /* 119 | *-------------------------------------------------------------------------- 120 | * 121 | * bson_uint32_to_string -- 122 | * 123 | * Converts @value to a string. 124 | * 125 | * If @value is from 0 to 1000, it will use a constant string in the 126 | * data section of the library. 127 | * 128 | * If not, a string will be formatted using @str and snprintf(). This 129 | * is much slower, of course and therefore we try to optimize it out. 130 | * 131 | * @strptr will always be set. It will either point to @str or a 132 | * constant string. You will want to use this as your key. 133 | * 134 | * Parameters: 135 | * @value: A #uint32_t to convert to string. 136 | * @strptr: (out): A pointer to the resulting string. 137 | * @str: (out): Storage for a string made with snprintf. 138 | * @size: Size of @str. 139 | * 140 | * Returns: 141 | * The number of bytes in the resulting string. 142 | * 143 | * Side effects: 144 | * None. 145 | * 146 | *-------------------------------------------------------------------------- 147 | */ 148 | 149 | size_t 150 | bson_uint32_to_string (uint32_t value, /* IN */ 151 | const char **strptr, /* OUT */ 152 | char *str, /* OUT */ 153 | size_t size) /* IN */ 154 | { 155 | if (value < 1000) { 156 | *strptr = gUint32Strs[value]; 157 | 158 | if (value < 10) { 159 | return 1; 160 | } else if (value < 100) { 161 | return 2; 162 | } else { 163 | return 3; 164 | } 165 | } 166 | 167 | *strptr = str; 168 | 169 | return bson_snprintf (str, size, "%u", value); 170 | } 171 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-keys.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_KEYS_H 21 | #define BSON_KEYS_H 22 | 23 | 24 | #include "bson-macros.h" 25 | #include "bson-types.h" 26 | 27 | 28 | BSON_BEGIN_DECLS 29 | 30 | 31 | BSON_EXPORT (size_t) 32 | bson_uint32_to_string (uint32_t value, 33 | const char **strptr, 34 | char *str, 35 | size_t size); 36 | 37 | 38 | BSON_END_DECLS 39 | 40 | 41 | #endif /* BSON_KEYS_H */ 42 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-macros.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_MACROS_H 21 | #define BSON_MACROS_H 22 | 23 | 24 | #include 25 | 26 | #ifdef __cplusplus 27 | #include 28 | #endif 29 | 30 | #include "bson-config.h" 31 | 32 | 33 | #if BSON_OS == 1 34 | #define BSON_OS_UNIX 35 | #elif BSON_OS == 2 36 | #define BSON_OS_WIN32 37 | #else 38 | #error "Unknown operating system." 39 | #endif 40 | 41 | 42 | #ifdef __cplusplus 43 | #define BSON_BEGIN_DECLS extern "C" { 44 | #define BSON_END_DECLS } 45 | #else 46 | #define BSON_BEGIN_DECLS 47 | #define BSON_END_DECLS 48 | #endif 49 | 50 | 51 | #if defined(__GNUC__) 52 | #define BSON_GNUC_CHECK_VERSION(major, minor) \ 53 | ((__GNUC__ > (major)) || \ 54 | ((__GNUC__ == (major)) && (__GNUC_MINOR__ >= (minor)))) 55 | #else 56 | #define BSON_GNUC_CHECK_VERSION(major, minor) 0 57 | #endif 58 | 59 | 60 | #if defined(__GNUC__) 61 | #define BSON_GNUC_IS_VERSION(major, minor) \ 62 | ((__GNUC__ == (major)) && (__GNUC_MINOR__ == (minor))) 63 | #else 64 | #define BSON_GNUC_IS_VERSION(major, minor) 0 65 | #endif 66 | 67 | 68 | /* Decorate public functions: 69 | * - if BSON_STATIC, we're compiling a program that uses libbson as a static 70 | * library, don't decorate functions 71 | * - else if BSON_COMPILATION, we're compiling a static or shared libbson, mark 72 | * public functions for export from the shared lib (which has no effect on 73 | * the static lib) 74 | * - else, we're compiling a program that uses libbson as a shared library, 75 | * mark public functions as DLL imports for Microsoft Visual C 76 | */ 77 | 78 | #ifdef _MSC_VER 79 | /* 80 | * Microsoft Visual C 81 | */ 82 | #ifdef BSON_STATIC 83 | #define BSON_API 84 | #elif defined(BSON_COMPILATION) 85 | #define BSON_API __declspec(dllexport) 86 | #else 87 | #define BSON_API __declspec(dllimport) 88 | #endif 89 | #define BSON_CALL __cdecl 90 | 91 | #elif defined(__GNUC__) 92 | /* 93 | * GCC 94 | */ 95 | #ifdef BSON_STATIC 96 | #define BSON_API 97 | #elif defined(BSON_COMPILATION) 98 | #define BSON_API __attribute__ ((visibility ("default"))) 99 | #else 100 | #define BSON_API 101 | #endif 102 | #define BSON_CALL 103 | 104 | #else 105 | /* 106 | * Other compilers 107 | */ 108 | #define BSON_API 109 | #define BSON_CALL 110 | 111 | #endif 112 | 113 | #define BSON_EXPORT(type) BSON_API type BSON_CALL 114 | 115 | 116 | #ifdef MIN 117 | #define BSON_MIN MIN 118 | #elif defined(__cplusplus) 119 | #define BSON_MIN(a, b) ((std::min) (a, b)) 120 | #elif defined(_MSC_VER) 121 | #define BSON_MIN(a, b) ((a) < (b) ? (a) : (b)) 122 | #else 123 | #define BSON_MIN(a, b) (((a) < (b)) ? (a) : (b)) 124 | #endif 125 | 126 | 127 | #ifdef MAX 128 | #define BSON_MAX MAX 129 | #elif defined(__cplusplus) 130 | #define BSON_MAX(a, b) ((std::max) (a, b)) 131 | #elif defined(_MSC_VER) 132 | #define BSON_MAX(a, b) ((a) > (b) ? (a) : (b)) 133 | #else 134 | #define BSON_MAX(a, b) (((a) > (b)) ? (a) : (b)) 135 | #endif 136 | 137 | 138 | #ifdef ABS 139 | #define BSON_ABS ABS 140 | #else 141 | #define BSON_ABS(a) (((a) < 0) ? ((a) * -1) : (a)) 142 | #endif 143 | 144 | #ifdef _MSC_VER 145 | #ifdef _WIN64 146 | #define BSON_ALIGN_OF_PTR 8 147 | #else 148 | #define BSON_ALIGN_OF_PTR 4 149 | #endif 150 | #else 151 | #define BSON_ALIGN_OF_PTR (sizeof (void *)) 152 | #endif 153 | 154 | #ifdef BSON_EXTRA_ALIGN 155 | #if defined(_MSC_VER) 156 | #define BSON_ALIGNED_BEGIN(_N) __declspec(align (_N)) 157 | #define BSON_ALIGNED_END(_N) 158 | #else 159 | #define BSON_ALIGNED_BEGIN(_N) 160 | #define BSON_ALIGNED_END(_N) __attribute__ ((aligned (_N))) 161 | #endif 162 | #else 163 | #if defined(_MSC_VER) 164 | #define BSON_ALIGNED_BEGIN(_N) __declspec(align (BSON_ALIGN_OF_PTR)) 165 | #define BSON_ALIGNED_END(_N) 166 | #else 167 | #define BSON_ALIGNED_BEGIN(_N) 168 | #define BSON_ALIGNED_END(_N) \ 169 | __attribute__ ( \ 170 | (aligned ((_N) > BSON_ALIGN_OF_PTR ? BSON_ALIGN_OF_PTR : (_N)))) 171 | #endif 172 | #endif 173 | 174 | 175 | #define bson_str_empty(s) (!s[0]) 176 | #define bson_str_empty0(s) (!s || !s[0]) 177 | 178 | 179 | #if defined(_MSC_VER) 180 | #define BSON_FUNC __FUNCTION__ 181 | #elif defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L 182 | #define BSON_FUNC __FUNCTION__ 183 | #else 184 | #define BSON_FUNC __func__ 185 | #endif 186 | 187 | #define BSON_ASSERT(test) \ 188 | do { \ 189 | if (!(BSON_LIKELY (test))) { \ 190 | fprintf (stderr, \ 191 | "%s:%d %s(): precondition failed: %s\n", \ 192 | __FILE__, \ 193 | __LINE__, \ 194 | BSON_FUNC, \ 195 | #test); \ 196 | abort (); \ 197 | } \ 198 | } while (0) 199 | 200 | /* Used for asserting parameters to provide a more precise error message */ 201 | #define BSON_ASSERT_PARAM(param) \ 202 | do { \ 203 | if ((BSON_UNLIKELY (param == NULL))) { \ 204 | fprintf (stderr, \ 205 | "The parameter: %s, in function %s, cannot be NULL\n", \ 206 | #param, \ 207 | BSON_FUNC); \ 208 | abort (); \ 209 | } \ 210 | } while (0) 211 | 212 | /* obsolete macros, preserved for compatibility */ 213 | #define BSON_STATIC_ASSERT(s) BSON_STATIC_ASSERT_ (s, __LINE__) 214 | #define BSON_STATIC_ASSERT_JOIN(a, b) BSON_STATIC_ASSERT_JOIN2 (a, b) 215 | #define BSON_STATIC_ASSERT_JOIN2(a, b) a##b 216 | #define BSON_STATIC_ASSERT_(s, l) \ 217 | typedef char BSON_STATIC_ASSERT_JOIN (static_assert_test_, \ 218 | __LINE__)[(s) ? 1 : -1] 219 | 220 | /* modern macros */ 221 | #define BSON_STATIC_ASSERT2(_name, _s) \ 222 | BSON_STATIC_ASSERT2_ (_s, __LINE__, _name) 223 | #define BSON_STATIC_ASSERT_JOIN3(_a, _b, _name) \ 224 | BSON_STATIC_ASSERT_JOIN4 (_a, _b, _name) 225 | #define BSON_STATIC_ASSERT_JOIN4(_a, _b, _name) _a##_b##_name 226 | #define BSON_STATIC_ASSERT2_(_s, _l, _name) \ 227 | typedef char BSON_STATIC_ASSERT_JOIN3 ( \ 228 | static_assert_test_, __LINE__, _name)[(_s) ? 1 : -1] 229 | 230 | 231 | #if defined(__GNUC__) 232 | #define BSON_GNUC_PURE __attribute__ ((pure)) 233 | #define BSON_GNUC_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result)) 234 | #else 235 | #define BSON_GNUC_PURE 236 | #define BSON_GNUC_WARN_UNUSED_RESULT 237 | #endif 238 | 239 | 240 | #if BSON_GNUC_CHECK_VERSION(4, 0) && !defined(_WIN32) 241 | #define BSON_GNUC_NULL_TERMINATED __attribute__ ((sentinel)) 242 | #define BSON_GNUC_INTERNAL __attribute__ ((visibility ("hidden"))) 243 | #else 244 | #define BSON_GNUC_NULL_TERMINATED 245 | #define BSON_GNUC_INTERNAL 246 | #endif 247 | 248 | 249 | #if defined(__GNUC__) 250 | #define BSON_LIKELY(x) __builtin_expect (!!(x), 1) 251 | #define BSON_UNLIKELY(x) __builtin_expect (!!(x), 0) 252 | #else 253 | #define BSON_LIKELY(v) v 254 | #define BSON_UNLIKELY(v) v 255 | #endif 256 | 257 | 258 | #if defined(__clang__) 259 | #define BSON_GNUC_PRINTF(f, v) __attribute__ ((format (printf, f, v))) 260 | #elif BSON_GNUC_CHECK_VERSION(4, 4) 261 | #define BSON_GNUC_PRINTF(f, v) __attribute__ ((format (gnu_printf, f, v))) 262 | #else 263 | #define BSON_GNUC_PRINTF(f, v) 264 | #endif 265 | 266 | 267 | #if defined(__LP64__) || defined(_LP64) 268 | #define BSON_WORD_SIZE 64 269 | #else 270 | #define BSON_WORD_SIZE 32 271 | #endif 272 | 273 | 274 | #if defined(_MSC_VER) 275 | #define BSON_INLINE __inline 276 | #else 277 | #define BSON_INLINE __inline__ 278 | #endif 279 | 280 | 281 | #ifdef _MSC_VER 282 | #define BSON_ENSURE_ARRAY_PARAM_SIZE(_n) 283 | #define BSON_TYPEOF decltype 284 | #else 285 | #define BSON_ENSURE_ARRAY_PARAM_SIZE(_n) static(_n) 286 | #define BSON_TYPEOF typeof 287 | #endif 288 | 289 | 290 | #if BSON_GNUC_CHECK_VERSION(3, 1) 291 | #define BSON_GNUC_DEPRECATED __attribute__ ((__deprecated__)) 292 | #else 293 | #define BSON_GNUC_DEPRECATED 294 | #endif 295 | 296 | 297 | #if BSON_GNUC_CHECK_VERSION(4, 5) 298 | #define BSON_GNUC_DEPRECATED_FOR(f) \ 299 | __attribute__ ((deprecated ("Use " #f " instead"))) 300 | #else 301 | #define BSON_GNUC_DEPRECATED_FOR(f) BSON_GNUC_DEPRECATED 302 | #endif 303 | 304 | 305 | #endif /* BSON_MACROS_H */ 306 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-md5.c: -------------------------------------------------------------------------------- 1 | #include "bson-compat.h" 2 | 3 | #include "bson-md5.h" 4 | #include "common-md5-private.h" 5 | 6 | 7 | void 8 | bson_md5_init (bson_md5_t *pms) 9 | { 10 | COMMON_PREFIX (_bson_md5_init (pms)); 11 | } 12 | 13 | 14 | void 15 | bson_md5_append (bson_md5_t *pms, const uint8_t *data, uint32_t nbytes) 16 | { 17 | COMMON_PREFIX (_bson_md5_append (pms, data, nbytes)); 18 | } 19 | 20 | void 21 | bson_md5_finish (bson_md5_t *pms, uint8_t digest[16]) 22 | { 23 | COMMON_PREFIX (_bson_md5_finish (pms, digest)); 24 | } 25 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-md5.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 1999, 2002 Aladdin Enterprises. All rights reserved. 3 | 4 | This software is provided 'as-is', without any express or implied 5 | warranty. In no event will the authors be held liable for any damages 6 | arising from the use of this software. 7 | 8 | Permission is granted to anyone to use this software for any purpose, 9 | including commercial applications, and to alter it and redistribute it 10 | freely, subject to the following restrictions: 11 | 12 | 1. The origin of this software must not be misrepresented; you must not 13 | claim that you wrote the original software. If you use this software 14 | in a product, an acknowledgement in the product documentation would be 15 | appreciated but is not required. 16 | 2. Altered source versions must be plainly marked as such, and must not be 17 | misrepresented as being the original software. 18 | 3. This notice may not be removed or altered from any source distribution. 19 | 20 | L. Peter Deutsch 21 | ghost@aladdin.com 22 | 23 | */ 24 | /* $Id: md5.h,v 1.4 2002/04/13 19:20:28 lpd Exp $ */ 25 | /* 26 | Independent implementation of MD5 (RFC 1321). 27 | 28 | This code implements the MD5 Algorithm defined in RFC 1321, whose 29 | text is available at 30 | http://www.ietf.org/rfc/rfc1321.txt 31 | The code is derived from the text of the RFC, including the test suite 32 | (section A.5) but excluding the rest of Appendix A. It does not include 33 | any code or documentation that is identified in the RFC as being 34 | copyrighted. 35 | 36 | The original and principal author of md5.h is L. Peter Deutsch 37 | . Other authors are noted in the change history 38 | that follows (in reverse chronological order): 39 | 40 | 2002-04-13 lpd Removed support for non-ANSI compilers; removed 41 | references to Ghostscript; clarified derivation from RFC 1321; 42 | now handles byte order either statically or dynamically. 43 | 1999-11-04 lpd Edited comments slightly for automatic TOC extraction. 44 | 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5); 45 | added conditionalization for C++ compilation from Martin 46 | Purschke . 47 | 1999-05-03 lpd Original version. 48 | */ 49 | 50 | 51 | /* 52 | * The following MD5 implementation has been modified to use types as 53 | * specified in libbson. 54 | */ 55 | 56 | #include "bson-prelude.h" 57 | 58 | 59 | #ifndef BSON_MD5_H 60 | #define BSON_MD5_H 61 | 62 | 63 | #include "bson-endian.h" 64 | 65 | 66 | BSON_BEGIN_DECLS 67 | 68 | 69 | typedef struct { 70 | uint32_t count[2]; /* message length in bits, lsw first */ 71 | uint32_t abcd[4]; /* digest buffer */ 72 | uint8_t buf[64]; /* accumulate block */ 73 | } bson_md5_t; 74 | 75 | 76 | BSON_EXPORT (void) 77 | bson_md5_init (bson_md5_t *pms) BSON_GNUC_DEPRECATED; 78 | BSON_EXPORT (void) 79 | bson_md5_append (bson_md5_t *pms, 80 | const uint8_t *data, 81 | uint32_t nbytes) BSON_GNUC_DEPRECATED; 82 | BSON_EXPORT (void) 83 | bson_md5_finish (bson_md5_t *pms, uint8_t digest[16]) BSON_GNUC_DEPRECATED; 84 | 85 | 86 | BSON_END_DECLS 87 | 88 | 89 | #endif /* BSON_MD5_H */ 90 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-memory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "bson-atomic.h" 24 | #include "bson-config.h" 25 | #include "bson-memory.h" 26 | 27 | 28 | static bson_mem_vtable_t gMemVtable = { 29 | malloc, 30 | calloc, 31 | #ifdef BSON_HAVE_REALLOCF 32 | reallocf, 33 | #else 34 | realloc, 35 | #endif 36 | free, 37 | }; 38 | 39 | 40 | /* 41 | *-------------------------------------------------------------------------- 42 | * 43 | * bson_malloc -- 44 | * 45 | * Allocates @num_bytes of memory and returns a pointer to it. If 46 | * malloc failed to allocate the memory, abort() is called. 47 | * 48 | * Libbson does not try to handle OOM conditions as it is beyond the 49 | * scope of this library to handle so appropriately. 50 | * 51 | * Parameters: 52 | * @num_bytes: The number of bytes to allocate. 53 | * 54 | * Returns: 55 | * A pointer if successful; otherwise abort() is called and this 56 | * function will never return. 57 | * 58 | * Side effects: 59 | * None. 60 | * 61 | *-------------------------------------------------------------------------- 62 | */ 63 | 64 | void * 65 | bson_malloc (size_t num_bytes) /* IN */ 66 | { 67 | void *mem = NULL; 68 | 69 | if (BSON_LIKELY (num_bytes)) { 70 | if (BSON_UNLIKELY (!(mem = gMemVtable.malloc (num_bytes)))) { 71 | fprintf (stderr, "Failure to allocate memory in bson_malloc(). errno: %d.\n", errno); 72 | abort (); 73 | } 74 | } 75 | 76 | return mem; 77 | } 78 | 79 | 80 | /* 81 | *-------------------------------------------------------------------------- 82 | * 83 | * bson_malloc0 -- 84 | * 85 | * Like bson_malloc() except the memory is zeroed first. This is 86 | * similar to calloc() except that abort() is called in case of 87 | * failure to allocate memory. 88 | * 89 | * Parameters: 90 | * @num_bytes: The number of bytes to allocate. 91 | * 92 | * Returns: 93 | * A pointer if successful; otherwise abort() is called and this 94 | * function will never return. 95 | * 96 | * Side effects: 97 | * None. 98 | * 99 | *-------------------------------------------------------------------------- 100 | */ 101 | 102 | void * 103 | bson_malloc0 (size_t num_bytes) /* IN */ 104 | { 105 | void *mem = NULL; 106 | 107 | if (BSON_LIKELY (num_bytes)) { 108 | if (BSON_UNLIKELY (!(mem = gMemVtable.calloc (1, num_bytes)))) { 109 | fprintf (stderr, "Failure to allocate memory in bson_malloc0(). errno: %d.\n", errno); 110 | abort (); 111 | } 112 | } 113 | 114 | return mem; 115 | } 116 | 117 | 118 | /* 119 | *-------------------------------------------------------------------------- 120 | * 121 | * bson_realloc -- 122 | * 123 | * This function behaves similar to realloc() except that if there is 124 | * a failure abort() is called. 125 | * 126 | * Parameters: 127 | * @mem: The memory to realloc, or NULL. 128 | * @num_bytes: The size of the new allocation or 0 to free. 129 | * 130 | * Returns: 131 | * The new allocation if successful; otherwise abort() is called and 132 | * this function never returns. 133 | * 134 | * Side effects: 135 | * None. 136 | * 137 | *-------------------------------------------------------------------------- 138 | */ 139 | 140 | void * 141 | bson_realloc (void *mem, /* IN */ 142 | size_t num_bytes) /* IN */ 143 | { 144 | /* 145 | * Not all platforms are guaranteed to free() the memory if a call to 146 | * realloc() with a size of zero occurs. Windows, Linux, and FreeBSD do, 147 | * however, OS X does not. 148 | */ 149 | if (BSON_UNLIKELY (num_bytes == 0)) { 150 | gMemVtable.free (mem); 151 | return NULL; 152 | } 153 | 154 | mem = gMemVtable.realloc (mem, num_bytes); 155 | 156 | if (BSON_UNLIKELY (!mem)) { 157 | fprintf (stderr, "Failure to re-allocate memory in bson_realloc(). errno: %d.\n", errno); 158 | abort (); 159 | } 160 | 161 | return mem; 162 | } 163 | 164 | 165 | /* 166 | *-------------------------------------------------------------------------- 167 | * 168 | * bson_realloc_ctx -- 169 | * 170 | * This wraps bson_realloc and provides a compatible api for similar 171 | * functions with a context 172 | * 173 | * Parameters: 174 | * @mem: The memory to realloc, or NULL. 175 | * @num_bytes: The size of the new allocation or 0 to free. 176 | * @ctx: Ignored 177 | * 178 | * Returns: 179 | * The new allocation if successful; otherwise abort() is called and 180 | * this function never returns. 181 | * 182 | * Side effects: 183 | * None. 184 | * 185 | *-------------------------------------------------------------------------- 186 | */ 187 | 188 | 189 | void * 190 | bson_realloc_ctx (void *mem, /* IN */ 191 | size_t num_bytes, /* IN */ 192 | void *ctx) /* IN */ 193 | { 194 | return bson_realloc (mem, num_bytes); 195 | } 196 | 197 | 198 | /* 199 | *-------------------------------------------------------------------------- 200 | * 201 | * bson_free -- 202 | * 203 | * Frees @mem using the underlying allocator. 204 | * 205 | * Currently, this only calls free() directly, but that is subject to 206 | * change. 207 | * 208 | * Parameters: 209 | * @mem: An allocation to free. 210 | * 211 | * Returns: 212 | * None. 213 | * 214 | * Side effects: 215 | * None. 216 | * 217 | *-------------------------------------------------------------------------- 218 | */ 219 | 220 | void 221 | bson_free (void *mem) /* IN */ 222 | { 223 | gMemVtable.free (mem); 224 | } 225 | 226 | 227 | /* 228 | *-------------------------------------------------------------------------- 229 | * 230 | * bson_zero_free -- 231 | * 232 | * Frees @mem using the underlying allocator. @size bytes of @mem will 233 | * be zeroed before freeing the memory. This is useful in scenarios 234 | * where @mem contains passwords or other sensitive information. 235 | * 236 | * Parameters: 237 | * @mem: An allocation to free. 238 | * @size: The number of bytes in @mem. 239 | * 240 | * Returns: 241 | * None. 242 | * 243 | * Side effects: 244 | * None. 245 | * 246 | *-------------------------------------------------------------------------- 247 | */ 248 | 249 | void 250 | bson_zero_free (void *mem, /* IN */ 251 | size_t size) /* IN */ 252 | { 253 | if (BSON_LIKELY (mem)) { 254 | memset (mem, 0, size); 255 | gMemVtable.free (mem); 256 | } 257 | } 258 | 259 | 260 | /* 261 | *-------------------------------------------------------------------------- 262 | * 263 | * bson_mem_set_vtable -- 264 | * 265 | * This function will change our allocation vtable. 266 | * 267 | * It is imperative that this is called at the beginning of the 268 | * process before any memory has been allocated by the default 269 | * allocator. 270 | * 271 | * Returns: 272 | * None. 273 | * 274 | * Side effects: 275 | * None. 276 | * 277 | *-------------------------------------------------------------------------- 278 | */ 279 | 280 | void 281 | bson_mem_set_vtable (const bson_mem_vtable_t *vtable) 282 | { 283 | BSON_ASSERT (vtable); 284 | 285 | if (!vtable->malloc || !vtable->calloc || !vtable->realloc || 286 | !vtable->free) { 287 | fprintf (stderr, 288 | "Failure to install BSON vtable, " 289 | "missing functions.\n"); 290 | return; 291 | } 292 | 293 | gMemVtable = *vtable; 294 | } 295 | 296 | void 297 | bson_mem_restore_vtable (void) 298 | { 299 | bson_mem_vtable_t vtable = { 300 | malloc, 301 | calloc, 302 | #ifdef BSON_HAVE_REALLOCF 303 | reallocf, 304 | #else 305 | realloc, 306 | #endif 307 | free, 308 | }; 309 | 310 | bson_mem_set_vtable (&vtable); 311 | } 312 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_MEMORY_H 21 | #define BSON_MEMORY_H 22 | 23 | 24 | #include "bson-macros.h" 25 | #include "bson-types.h" 26 | 27 | 28 | BSON_BEGIN_DECLS 29 | 30 | 31 | typedef void *(*bson_realloc_func) (void *mem, size_t num_bytes, void *ctx); 32 | 33 | 34 | typedef struct _bson_mem_vtable_t { 35 | void *(*malloc) (size_t num_bytes); 36 | void *(*calloc) (size_t n_members, size_t num_bytes); 37 | void *(*realloc) (void *mem, size_t num_bytes); 38 | void (*free) (void *mem); 39 | void *padding[4]; 40 | } bson_mem_vtable_t; 41 | 42 | 43 | BSON_EXPORT (void) 44 | bson_mem_set_vtable (const bson_mem_vtable_t *vtable); 45 | BSON_EXPORT (void) 46 | bson_mem_restore_vtable (void); 47 | BSON_EXPORT (void *) 48 | bson_malloc (size_t num_bytes); 49 | BSON_EXPORT (void *) 50 | bson_malloc0 (size_t num_bytes); 51 | BSON_EXPORT (void *) 52 | bson_realloc (void *mem, size_t num_bytes); 53 | BSON_EXPORT (void *) 54 | bson_realloc_ctx (void *mem, size_t num_bytes, void *ctx); 55 | BSON_EXPORT (void) 56 | bson_free (void *mem); 57 | BSON_EXPORT (void) 58 | bson_zero_free (void *mem, size_t size); 59 | 60 | 61 | BSON_END_DECLS 62 | 63 | 64 | #endif /* BSON_MEMORY_H */ 65 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-oid.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_OID_H 21 | #define BSON_OID_H 22 | 23 | 24 | #include 25 | 26 | #include "bson-context.h" 27 | #include "bson-macros.h" 28 | #include "bson-types.h" 29 | #include "bson-endian.h" 30 | 31 | 32 | BSON_BEGIN_DECLS 33 | 34 | 35 | BSON_EXPORT (int) 36 | bson_oid_compare (const bson_oid_t *oid1, const bson_oid_t *oid2); 37 | BSON_EXPORT (void) 38 | bson_oid_copy (const bson_oid_t *src, bson_oid_t *dst); 39 | BSON_EXPORT (bool) 40 | bson_oid_equal (const bson_oid_t *oid1, const bson_oid_t *oid2); 41 | BSON_EXPORT (bool) 42 | bson_oid_is_valid (const char *str, size_t length); 43 | BSON_EXPORT (time_t) 44 | bson_oid_get_time_t (const bson_oid_t *oid); 45 | BSON_EXPORT (uint32_t) 46 | bson_oid_hash (const bson_oid_t *oid); 47 | BSON_EXPORT (void) 48 | bson_oid_init (bson_oid_t *oid, bson_context_t *context); 49 | BSON_EXPORT (void) 50 | bson_oid_init_from_data (bson_oid_t *oid, const uint8_t *data); 51 | BSON_EXPORT (void) 52 | bson_oid_init_from_string (bson_oid_t *oid, const char *str); 53 | BSON_EXPORT (void) 54 | bson_oid_init_sequence (bson_oid_t *oid, 55 | bson_context_t *context) BSON_GNUC_DEPRECATED; 56 | BSON_EXPORT (void) 57 | bson_oid_to_string (const bson_oid_t *oid, char str[25]); 58 | 59 | 60 | /** 61 | * bson_oid_compare_unsafe: 62 | * @oid1: A bson_oid_t. 63 | * @oid2: A bson_oid_t. 64 | * 65 | * Performs a qsort() style comparison between @oid1 and @oid2. 66 | * 67 | * This function is meant to be as fast as possible and therefore performs 68 | * no argument validation. That is the callers responsibility. 69 | * 70 | * Returns: An integer < 0 if @oid1 is less than @oid2. Zero if they are equal. 71 | * An integer > 0 if @oid1 is greater than @oid2. 72 | */ 73 | static BSON_INLINE int 74 | bson_oid_compare_unsafe (const bson_oid_t *oid1, const bson_oid_t *oid2) 75 | { 76 | return memcmp (oid1, oid2, sizeof *oid1); 77 | } 78 | 79 | 80 | /** 81 | * bson_oid_equal_unsafe: 82 | * @oid1: A bson_oid_t. 83 | * @oid2: A bson_oid_t. 84 | * 85 | * Checks the equality of @oid1 and @oid2. 86 | * 87 | * This function is meant to be as fast as possible and therefore performs 88 | * no checks for argument validity. That is the callers responsibility. 89 | * 90 | * Returns: true if @oid1 and @oid2 are equal; otherwise false. 91 | */ 92 | static BSON_INLINE bool 93 | bson_oid_equal_unsafe (const bson_oid_t *oid1, const bson_oid_t *oid2) 94 | { 95 | return !memcmp (oid1, oid2, sizeof *oid1); 96 | } 97 | 98 | /** 99 | * bson_oid_hash_unsafe: 100 | * @oid: A bson_oid_t. 101 | * 102 | * This function performs a DJB style hash upon the bytes contained in @oid. 103 | * The result is a hash key suitable for use in a hashtable. 104 | * 105 | * This function is meant to be as fast as possible and therefore performs no 106 | * validation of arguments. The caller is responsible to ensure they are 107 | * passing valid arguments. 108 | * 109 | * Returns: A uint32_t containing a hash code. 110 | */ 111 | static BSON_INLINE uint32_t 112 | bson_oid_hash_unsafe (const bson_oid_t *oid) 113 | { 114 | uint32_t hash = 5381; 115 | uint32_t i; 116 | 117 | for (i = 0; i < sizeof oid->bytes; i++) { 118 | hash = ((hash << 5) + hash) + oid->bytes[i]; 119 | } 120 | 121 | return hash; 122 | } 123 | 124 | 125 | /** 126 | * bson_oid_copy_unsafe: 127 | * @src: A bson_oid_t to copy from. 128 | * @dst: A bson_oid_t to copy into. 129 | * 130 | * Copies the contents of @src into @dst. This function is meant to be as 131 | * fast as possible and therefore performs no argument checking. It is the 132 | * callers responsibility to ensure they are passing valid data into the 133 | * function. 134 | */ 135 | static BSON_INLINE void 136 | bson_oid_copy_unsafe (const bson_oid_t *src, bson_oid_t *dst) 137 | { 138 | memcpy (dst, src, sizeof *src); 139 | } 140 | 141 | 142 | /** 143 | * bson_oid_parse_hex_char: 144 | * @hex: A character to parse to its integer value. 145 | * 146 | * This function contains a jump table to return the integer value for a 147 | * character containing a hexadecimal value (0-9, a-f, A-F). If the character 148 | * is not a hexadecimal character then zero is returned. 149 | * 150 | * Returns: An integer between 0 and 15. 151 | */ 152 | static BSON_INLINE uint8_t 153 | bson_oid_parse_hex_char (char hex) 154 | { 155 | switch (hex) { 156 | case '0': 157 | return 0; 158 | case '1': 159 | return 1; 160 | case '2': 161 | return 2; 162 | case '3': 163 | return 3; 164 | case '4': 165 | return 4; 166 | case '5': 167 | return 5; 168 | case '6': 169 | return 6; 170 | case '7': 171 | return 7; 172 | case '8': 173 | return 8; 174 | case '9': 175 | return 9; 176 | case 'a': 177 | case 'A': 178 | return 0xa; 179 | case 'b': 180 | case 'B': 181 | return 0xb; 182 | case 'c': 183 | case 'C': 184 | return 0xc; 185 | case 'd': 186 | case 'D': 187 | return 0xd; 188 | case 'e': 189 | case 'E': 190 | return 0xe; 191 | case 'f': 192 | case 'F': 193 | return 0xf; 194 | default: 195 | return 0; 196 | } 197 | } 198 | 199 | 200 | /** 201 | * bson_oid_init_from_string_unsafe: 202 | * @oid: A bson_oid_t to store the result. 203 | * @str: A 24-character hexadecimal encoded string. 204 | * 205 | * Parses a string containing 24 hexadecimal encoded bytes into a bson_oid_t. 206 | * This function is meant to be as fast as possible and inlined into your 207 | * code. For that purpose, the function does not perform any sort of bounds 208 | * checking and it is the callers responsibility to ensure they are passing 209 | * valid input to the function. 210 | */ 211 | static BSON_INLINE void 212 | bson_oid_init_from_string_unsafe (bson_oid_t *oid, const char *str) 213 | { 214 | int i; 215 | 216 | for (i = 0; i < 12; i++) { 217 | oid->bytes[i] = ((bson_oid_parse_hex_char (str[2 * i]) << 4) | 218 | (bson_oid_parse_hex_char (str[2 * i + 1]))); 219 | } 220 | } 221 | 222 | 223 | /** 224 | * bson_oid_get_time_t_unsafe: 225 | * @oid: A bson_oid_t. 226 | * 227 | * Fetches the time @oid was generated. 228 | * 229 | * Returns: A time_t containing the UNIX timestamp of generation. 230 | */ 231 | static BSON_INLINE time_t 232 | bson_oid_get_time_t_unsafe (const bson_oid_t *oid) 233 | { 234 | uint32_t t; 235 | 236 | memcpy (&t, oid, sizeof (t)); 237 | return BSON_UINT32_FROM_BE (t); 238 | } 239 | 240 | 241 | BSON_END_DECLS 242 | 243 | 244 | #endif /* BSON_OID_H */ 245 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-prelude.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #if !defined(BSON_INSIDE) && !defined(BSON_COMPILATION) 18 | #error "Only can be included directly." 19 | #endif 20 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_PRIVATE_H 21 | #define BSON_PRIVATE_H 22 | 23 | 24 | #include "bson-macros.h" 25 | #include "bson-memory.h" 26 | #include "bson-types.h" 27 | 28 | 29 | #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) 30 | #define BEGIN_IGNORE_DEPRECATIONS \ 31 | _Pragma ("GCC diagnostic push") \ 32 | _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"") 33 | #define END_IGNORE_DEPRECATIONS _Pragma ("GCC diagnostic pop") 34 | #elif defined(__clang__) 35 | #define BEGIN_IGNORE_DEPRECATIONS \ 36 | _Pragma ("clang diagnostic push") \ 37 | _Pragma ("clang diagnostic ignored \"-Wdeprecated-declarations\"") 38 | #define END_IGNORE_DEPRECATIONS _Pragma ("clang diagnostic pop") 39 | #else 40 | #define BEGIN_IGNORE_DEPRECATIONS 41 | #define END_IGNORE_DEPRECATIONS 42 | #endif 43 | 44 | 45 | BSON_BEGIN_DECLS 46 | 47 | 48 | typedef enum { 49 | BSON_FLAG_NONE = 0, 50 | BSON_FLAG_INLINE = (1 << 0), 51 | BSON_FLAG_STATIC = (1 << 1), 52 | BSON_FLAG_RDONLY = (1 << 2), 53 | BSON_FLAG_CHILD = (1 << 3), 54 | BSON_FLAG_IN_CHILD = (1 << 4), 55 | BSON_FLAG_NO_FREE = (1 << 5), 56 | } bson_flags_t; 57 | 58 | 59 | #ifdef BSON_MEMCHECK 60 | #define BSON_INLINE_DATA_SIZE (120 - sizeof (char *)) 61 | #else 62 | #define BSON_INLINE_DATA_SIZE 120 63 | #endif 64 | 65 | 66 | BSON_ALIGNED_BEGIN (128) 67 | typedef struct { 68 | bson_flags_t flags; 69 | uint32_t len; 70 | #ifdef BSON_MEMCHECK 71 | char *canary; 72 | #endif 73 | uint8_t data[BSON_INLINE_DATA_SIZE]; 74 | } bson_impl_inline_t BSON_ALIGNED_END (128); 75 | 76 | 77 | BSON_STATIC_ASSERT2 (impl_inline_t, sizeof (bson_impl_inline_t) == 128); 78 | 79 | 80 | BSON_ALIGNED_BEGIN (128) 81 | typedef struct { 82 | bson_flags_t flags; /* flags describing the bson_t */ 83 | /* len is part of the public bson_t declaration. It is not 84 | * exposed through an accessor function. Plus, it's redundant since 85 | * BSON self describes the length in the first four bytes of the 86 | * buffer. */ 87 | uint32_t len; /* length of bson document in bytes */ 88 | bson_t *parent; /* parent bson if a child */ 89 | uint32_t depth; /* Subdocument depth. */ 90 | uint8_t **buf; /* pointer to buffer pointer */ 91 | size_t *buflen; /* pointer to buffer length */ 92 | size_t offset; /* our offset inside *buf */ 93 | uint8_t *alloc; /* buffer that we own. */ 94 | size_t alloclen; /* length of buffer that we own. */ 95 | bson_realloc_func realloc; /* our realloc implementation */ 96 | void *realloc_func_ctx; /* context for our realloc func */ 97 | } bson_impl_alloc_t BSON_ALIGNED_END (128); 98 | 99 | 100 | BSON_STATIC_ASSERT2 (impl_alloc_t, sizeof (bson_impl_alloc_t) <= 128); 101 | 102 | 103 | #define BSON_REGEX_OPTIONS_SORTED "ilmsux" 104 | 105 | BSON_END_DECLS 106 | 107 | 108 | #endif /* BSON_PRIVATE_H */ 109 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-reader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_READER_H 21 | #define BSON_READER_H 22 | 23 | 24 | #include "bson-compat.h" 25 | #include "bson-oid.h" 26 | #include "bson-types.h" 27 | 28 | 29 | BSON_BEGIN_DECLS 30 | 31 | 32 | #define BSON_ERROR_READER_BADFD 1 33 | 34 | 35 | /* 36 | *-------------------------------------------------------------------------- 37 | * 38 | * bson_reader_read_func_t -- 39 | * 40 | * This function is a callback used by bson_reader_t to read the 41 | * next chunk of data from the underlying opaque file descriptor. 42 | * 43 | * This function is meant to operate similar to the read() function 44 | * as part of libc on UNIX-like systems. 45 | * 46 | * Parameters: 47 | * @handle: The handle to read from. 48 | * @buf: The buffer to read into. 49 | * @count: The number of bytes to read. 50 | * 51 | * Returns: 52 | * 0 for end of stream. 53 | * -1 for read failure. 54 | * Greater than zero for number of bytes read into @buf. 55 | * 56 | * Side effects: 57 | * None. 58 | * 59 | *-------------------------------------------------------------------------- 60 | */ 61 | 62 | typedef ssize_t (*bson_reader_read_func_t) (void *handle, /* IN */ 63 | void *buf, /* IN */ 64 | size_t count); /* IN */ 65 | 66 | 67 | /* 68 | *-------------------------------------------------------------------------- 69 | * 70 | * bson_reader_destroy_func_t -- 71 | * 72 | * Destroy callback to release any resources associated with the 73 | * opaque handle. 74 | * 75 | * Parameters: 76 | * @handle: the handle provided to bson_reader_new_from_handle(). 77 | * 78 | * Returns: 79 | * None. 80 | * 81 | * Side effects: 82 | * None. 83 | * 84 | *-------------------------------------------------------------------------- 85 | */ 86 | 87 | typedef void (*bson_reader_destroy_func_t) (void *handle); /* IN */ 88 | 89 | 90 | BSON_EXPORT (bson_reader_t *) 91 | bson_reader_new_from_handle (void *handle, 92 | bson_reader_read_func_t rf, 93 | bson_reader_destroy_func_t df); 94 | BSON_EXPORT (bson_reader_t *) 95 | bson_reader_new_from_fd (int fd, bool close_on_destroy); 96 | BSON_EXPORT (bson_reader_t *) 97 | bson_reader_new_from_file (const char *path, bson_error_t *error); 98 | BSON_EXPORT (bson_reader_t *) 99 | bson_reader_new_from_data (const uint8_t *data, size_t length); 100 | BSON_EXPORT (void) 101 | bson_reader_destroy (bson_reader_t *reader); 102 | BSON_EXPORT (void) 103 | bson_reader_set_read_func (bson_reader_t *reader, bson_reader_read_func_t func); 104 | BSON_EXPORT (void) 105 | bson_reader_set_destroy_func (bson_reader_t *reader, 106 | bson_reader_destroy_func_t func); 107 | BSON_EXPORT (const bson_t *) 108 | bson_reader_read (bson_reader_t *reader, bool *reached_eof); 109 | BSON_EXPORT (off_t) 110 | bson_reader_tell (bson_reader_t *reader); 111 | BSON_EXPORT (void) 112 | bson_reader_reset (bson_reader_t *reader); 113 | 114 | BSON_END_DECLS 115 | 116 | 117 | #endif /* BSON_READER_H */ 118 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-string.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_STRING_H 21 | #define BSON_STRING_H 22 | 23 | 24 | #include 25 | 26 | #include "bson-macros.h" 27 | #include "bson-types.h" 28 | 29 | 30 | BSON_BEGIN_DECLS 31 | 32 | 33 | typedef struct { 34 | char *str; 35 | uint32_t len; 36 | uint32_t alloc; 37 | } bson_string_t; 38 | 39 | 40 | BSON_EXPORT (bson_string_t *) 41 | bson_string_new (const char *str); 42 | BSON_EXPORT (char *) 43 | bson_string_free (bson_string_t *string, bool free_segment); 44 | BSON_EXPORT (void) 45 | bson_string_append (bson_string_t *string, const char *str); 46 | BSON_EXPORT (void) 47 | bson_string_append_c (bson_string_t *string, char str); 48 | BSON_EXPORT (void) 49 | bson_string_append_unichar (bson_string_t *string, bson_unichar_t unichar); 50 | BSON_EXPORT (void) 51 | bson_string_append_printf (bson_string_t *string, const char *format, ...) 52 | BSON_GNUC_PRINTF (2, 3); 53 | BSON_EXPORT (void) 54 | bson_string_truncate (bson_string_t *string, uint32_t len); 55 | BSON_EXPORT (char *) 56 | bson_strdup (const char *str); 57 | BSON_EXPORT (char *) 58 | bson_strdup_printf (const char *format, ...) BSON_GNUC_PRINTF (1, 2); 59 | BSON_EXPORT (char *) 60 | bson_strdupv_printf (const char *format, va_list args) BSON_GNUC_PRINTF (1, 0); 61 | BSON_EXPORT (char *) 62 | bson_strndup (const char *str, size_t n_bytes); 63 | BSON_EXPORT (void) 64 | bson_strncpy (char *dst, const char *src, size_t size); 65 | BSON_EXPORT (int) 66 | bson_vsnprintf (char *str, size_t size, const char *format, va_list ap) 67 | BSON_GNUC_PRINTF (3, 0); 68 | BSON_EXPORT (int) 69 | bson_snprintf (char *str, size_t size, const char *format, ...) 70 | BSON_GNUC_PRINTF (3, 4); 71 | BSON_EXPORT (void) 72 | bson_strfreev (char **strv); 73 | BSON_EXPORT (size_t) 74 | bson_strnlen (const char *s, size_t maxlen); 75 | BSON_EXPORT (int64_t) 76 | bson_ascii_strtoll (const char *str, char **endptr, int base); 77 | BSON_EXPORT (int) 78 | bson_strcasecmp (const char *s1, const char *s2); 79 | BSON_EXPORT (bool) 80 | bson_isspace (int c); 81 | 82 | 83 | BSON_END_DECLS 84 | 85 | 86 | #endif /* BSON_STRING_H */ 87 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-timegm-private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_TIMEGM_PRIVATE_H 21 | #define BSON_TIMEGM_PRIVATE_H 22 | 23 | 24 | #include "bson-compat.h" 25 | #include "bson-macros.h" 26 | 27 | 28 | BSON_BEGIN_DECLS 29 | 30 | /* avoid system-dependent struct tm definitions */ 31 | struct bson_tm { 32 | int64_t tm_sec; /* seconds after the minute [0-60] */ 33 | int64_t tm_min; /* minutes after the hour [0-59] */ 34 | int64_t tm_hour; /* hours since midnight [0-23] */ 35 | int64_t tm_mday; /* day of the month [1-31] */ 36 | int64_t tm_mon; /* months since January [0-11] */ 37 | int64_t tm_year; /* years since 1900 */ 38 | int64_t tm_wday; /* days since Sunday [0-6] */ 39 | int64_t tm_yday; /* days since January 1 [0-365] */ 40 | int64_t tm_isdst; /* Daylight Savings Time flag */ 41 | int64_t tm_gmtoff; /* offset from CUT in seconds */ 42 | char *tm_zone; /* timezone abbreviation */ 43 | }; 44 | 45 | int64_t 46 | _bson_timegm (struct bson_tm *const tmp); 47 | 48 | BSON_END_DECLS 49 | 50 | 51 | #endif /* BSON_TIMEGM_PRIVATE_H */ 52 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-utf8.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_UTF8_H 21 | #define BSON_UTF8_H 22 | 23 | 24 | #include "bson-macros.h" 25 | #include "bson-types.h" 26 | 27 | 28 | BSON_BEGIN_DECLS 29 | 30 | 31 | BSON_EXPORT (bool) 32 | bson_utf8_validate (const char *utf8, size_t utf8_len, bool allow_null); 33 | BSON_EXPORT (char *) 34 | bson_utf8_escape_for_json (const char *utf8, ssize_t utf8_len); 35 | BSON_EXPORT (bson_unichar_t) 36 | bson_utf8_get_char (const char *utf8); 37 | BSON_EXPORT (const char *) 38 | bson_utf8_next_char (const char *utf8); 39 | BSON_EXPORT (void) 40 | bson_utf8_from_unichar (bson_unichar_t unichar, char utf8[6], uint32_t *len); 41 | 42 | 43 | BSON_END_DECLS 44 | 45 | 46 | #endif /* BSON_UTF8_H */ 47 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-value.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #include "bson-memory.h" 19 | #include "bson-string.h" 20 | #include "bson-value.h" 21 | #include "bson-oid.h" 22 | 23 | 24 | void 25 | bson_value_copy (const bson_value_t *src, /* IN */ 26 | bson_value_t *dst) /* OUT */ 27 | { 28 | BSON_ASSERT (src); 29 | BSON_ASSERT (dst); 30 | 31 | dst->value_type = src->value_type; 32 | 33 | switch (src->value_type) { 34 | case BSON_TYPE_DOUBLE: 35 | dst->value.v_double = src->value.v_double; 36 | break; 37 | case BSON_TYPE_UTF8: 38 | dst->value.v_utf8.len = src->value.v_utf8.len; 39 | dst->value.v_utf8.str = bson_malloc (src->value.v_utf8.len + 1); 40 | memcpy ( 41 | dst->value.v_utf8.str, src->value.v_utf8.str, dst->value.v_utf8.len); 42 | dst->value.v_utf8.str[dst->value.v_utf8.len] = '\0'; 43 | break; 44 | case BSON_TYPE_DOCUMENT: 45 | case BSON_TYPE_ARRAY: 46 | dst->value.v_doc.data_len = src->value.v_doc.data_len; 47 | dst->value.v_doc.data = bson_malloc (src->value.v_doc.data_len); 48 | memcpy (dst->value.v_doc.data, 49 | src->value.v_doc.data, 50 | dst->value.v_doc.data_len); 51 | break; 52 | case BSON_TYPE_BINARY: 53 | dst->value.v_binary.subtype = src->value.v_binary.subtype; 54 | dst->value.v_binary.data_len = src->value.v_binary.data_len; 55 | dst->value.v_binary.data = bson_malloc (src->value.v_binary.data_len); 56 | if (dst->value.v_binary.data_len) { 57 | memcpy (dst->value.v_binary.data, src->value.v_binary.data, 58 | dst->value.v_binary.data_len); 59 | } 60 | break; 61 | case BSON_TYPE_OID: 62 | bson_oid_copy (&src->value.v_oid, &dst->value.v_oid); 63 | break; 64 | case BSON_TYPE_BOOL: 65 | dst->value.v_bool = src->value.v_bool; 66 | break; 67 | case BSON_TYPE_DATE_TIME: 68 | dst->value.v_datetime = src->value.v_datetime; 69 | break; 70 | case BSON_TYPE_REGEX: 71 | dst->value.v_regex.regex = bson_strdup (src->value.v_regex.regex); 72 | dst->value.v_regex.options = bson_strdup (src->value.v_regex.options); 73 | break; 74 | case BSON_TYPE_DBPOINTER: 75 | dst->value.v_dbpointer.collection_len = 76 | src->value.v_dbpointer.collection_len; 77 | dst->value.v_dbpointer.collection = 78 | bson_malloc (src->value.v_dbpointer.collection_len + 1); 79 | memcpy (dst->value.v_dbpointer.collection, 80 | src->value.v_dbpointer.collection, 81 | dst->value.v_dbpointer.collection_len); 82 | dst->value.v_dbpointer.collection[dst->value.v_dbpointer.collection_len] = 83 | '\0'; 84 | bson_oid_copy (&src->value.v_dbpointer.oid, &dst->value.v_dbpointer.oid); 85 | break; 86 | case BSON_TYPE_CODE: 87 | dst->value.v_code.code_len = src->value.v_code.code_len; 88 | dst->value.v_code.code = bson_malloc (src->value.v_code.code_len + 1); 89 | memcpy (dst->value.v_code.code, 90 | src->value.v_code.code, 91 | dst->value.v_code.code_len); 92 | dst->value.v_code.code[dst->value.v_code.code_len] = '\0'; 93 | break; 94 | case BSON_TYPE_SYMBOL: 95 | dst->value.v_symbol.len = src->value.v_symbol.len; 96 | dst->value.v_symbol.symbol = bson_malloc (src->value.v_symbol.len + 1); 97 | memcpy (dst->value.v_symbol.symbol, 98 | src->value.v_symbol.symbol, 99 | dst->value.v_symbol.len); 100 | dst->value.v_symbol.symbol[dst->value.v_symbol.len] = '\0'; 101 | break; 102 | case BSON_TYPE_CODEWSCOPE: 103 | dst->value.v_codewscope.code_len = src->value.v_codewscope.code_len; 104 | dst->value.v_codewscope.code = 105 | bson_malloc (src->value.v_codewscope.code_len + 1); 106 | memcpy (dst->value.v_codewscope.code, 107 | src->value.v_codewscope.code, 108 | dst->value.v_codewscope.code_len); 109 | dst->value.v_codewscope.code[dst->value.v_codewscope.code_len] = '\0'; 110 | dst->value.v_codewscope.scope_len = src->value.v_codewscope.scope_len; 111 | dst->value.v_codewscope.scope_data = 112 | bson_malloc (src->value.v_codewscope.scope_len); 113 | memcpy (dst->value.v_codewscope.scope_data, 114 | src->value.v_codewscope.scope_data, 115 | dst->value.v_codewscope.scope_len); 116 | break; 117 | case BSON_TYPE_INT32: 118 | dst->value.v_int32 = src->value.v_int32; 119 | break; 120 | case BSON_TYPE_TIMESTAMP: 121 | dst->value.v_timestamp.timestamp = src->value.v_timestamp.timestamp; 122 | dst->value.v_timestamp.increment = src->value.v_timestamp.increment; 123 | break; 124 | case BSON_TYPE_INT64: 125 | dst->value.v_int64 = src->value.v_int64; 126 | break; 127 | case BSON_TYPE_DECIMAL128: 128 | dst->value.v_decimal128 = src->value.v_decimal128; 129 | break; 130 | case BSON_TYPE_UNDEFINED: 131 | case BSON_TYPE_NULL: 132 | case BSON_TYPE_MAXKEY: 133 | case BSON_TYPE_MINKEY: 134 | break; 135 | case BSON_TYPE_EOD: 136 | default: 137 | BSON_ASSERT (false); 138 | return; 139 | } 140 | } 141 | 142 | 143 | void 144 | bson_value_destroy (bson_value_t *value) /* IN */ 145 | { 146 | if (!value) { 147 | return; 148 | } 149 | 150 | switch (value->value_type) { 151 | case BSON_TYPE_UTF8: 152 | bson_free (value->value.v_utf8.str); 153 | break; 154 | case BSON_TYPE_DOCUMENT: 155 | case BSON_TYPE_ARRAY: 156 | bson_free (value->value.v_doc.data); 157 | break; 158 | case BSON_TYPE_BINARY: 159 | bson_free (value->value.v_binary.data); 160 | break; 161 | case BSON_TYPE_REGEX: 162 | bson_free (value->value.v_regex.regex); 163 | bson_free (value->value.v_regex.options); 164 | break; 165 | case BSON_TYPE_DBPOINTER: 166 | bson_free (value->value.v_dbpointer.collection); 167 | break; 168 | case BSON_TYPE_CODE: 169 | bson_free (value->value.v_code.code); 170 | break; 171 | case BSON_TYPE_SYMBOL: 172 | bson_free (value->value.v_symbol.symbol); 173 | break; 174 | case BSON_TYPE_CODEWSCOPE: 175 | bson_free (value->value.v_codewscope.code); 176 | bson_free (value->value.v_codewscope.scope_data); 177 | break; 178 | case BSON_TYPE_DOUBLE: 179 | case BSON_TYPE_UNDEFINED: 180 | case BSON_TYPE_OID: 181 | case BSON_TYPE_BOOL: 182 | case BSON_TYPE_DATE_TIME: 183 | case BSON_TYPE_NULL: 184 | case BSON_TYPE_INT32: 185 | case BSON_TYPE_TIMESTAMP: 186 | case BSON_TYPE_INT64: 187 | case BSON_TYPE_DECIMAL128: 188 | case BSON_TYPE_MAXKEY: 189 | case BSON_TYPE_MINKEY: 190 | case BSON_TYPE_EOD: 191 | default: 192 | break; 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-value.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_VALUE_H 21 | #define BSON_VALUE_H 22 | 23 | 24 | #include "bson-macros.h" 25 | #include "bson-types.h" 26 | 27 | 28 | BSON_BEGIN_DECLS 29 | 30 | 31 | BSON_EXPORT (void) 32 | bson_value_copy (const bson_value_t *src, bson_value_t *dst); 33 | BSON_EXPORT (void) 34 | bson_value_destroy (bson_value_t *value); 35 | 36 | 37 | BSON_END_DECLS 38 | 39 | 40 | #endif /* BSON_VALUE_H */ 41 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-version-functions.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #include "bson-version.h" 19 | #include "bson-version-functions.h" 20 | 21 | 22 | /** 23 | * bson_get_major_version: 24 | * 25 | * Helper function to return the runtime major version of the library. 26 | */ 27 | int 28 | bson_get_major_version (void) 29 | { 30 | return BSON_MAJOR_VERSION; 31 | } 32 | 33 | 34 | /** 35 | * bson_get_minor_version: 36 | * 37 | * Helper function to return the runtime minor version of the library. 38 | */ 39 | int 40 | bson_get_minor_version (void) 41 | { 42 | return BSON_MINOR_VERSION; 43 | } 44 | 45 | /** 46 | * bson_get_micro_version: 47 | * 48 | * Helper function to return the runtime micro version of the library. 49 | */ 50 | int 51 | bson_get_micro_version (void) 52 | { 53 | return BSON_MICRO_VERSION; 54 | } 55 | 56 | /** 57 | * bson_get_version: 58 | * 59 | * Helper function to return the runtime string version of the library. 60 | */ 61 | const char * 62 | bson_get_version (void) 63 | { 64 | return BSON_VERSION_S; 65 | } 66 | 67 | /** 68 | * bson_check_version: 69 | * 70 | * True if libmongoc's version is greater than or equal to the required 71 | * version. 72 | */ 73 | bool 74 | bson_check_version (int required_major, int required_minor, int required_micro) 75 | { 76 | return BSON_CHECK_VERSION (required_major, required_minor, required_micro); 77 | } 78 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-version-functions.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #include "bson-prelude.h" 19 | 20 | 21 | #ifndef BSON_VERSION_FUNCTIONS_H 22 | #define BSON_VERSION_FUNCTIONS_H 23 | 24 | #include "bson-types.h" 25 | 26 | BSON_BEGIN_DECLS 27 | 28 | BSON_EXPORT (int) 29 | bson_get_major_version (void); 30 | BSON_EXPORT (int) 31 | bson_get_minor_version (void); 32 | BSON_EXPORT (int) 33 | bson_get_micro_version (void); 34 | BSON_EXPORT (const char *) 35 | bson_get_version (void); 36 | BSON_EXPORT (bool) 37 | bson_check_version (int required_major, int required_minor, int required_micro); 38 | 39 | BSON_END_DECLS 40 | 41 | #endif /* BSON_VERSION_FUNCTIONS_H */ 42 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-version.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #if !defined (BSON_INSIDE) && !defined (BSON_COMPILATION) 19 | #error "Only can be included directly." 20 | #endif 21 | 22 | 23 | #ifndef BSON_VERSION_H 24 | #define BSON_VERSION_H 25 | 26 | 27 | /** 28 | * BSON_MAJOR_VERSION: 29 | * 30 | * BSON major version component (e.g. 1 if %BSON_VERSION is 1.2.3) 31 | */ 32 | #define BSON_MAJOR_VERSION (1) 33 | 34 | 35 | /** 36 | * BSON_MINOR_VERSION: 37 | * 38 | * BSON minor version component (e.g. 2 if %BSON_VERSION is 1.2.3) 39 | */ 40 | #define BSON_MINOR_VERSION (17) 41 | 42 | 43 | /** 44 | * BSON_MICRO_VERSION: 45 | * 46 | * BSON micro version component (e.g. 3 if %BSON_VERSION is 1.2.3) 47 | */ 48 | #define BSON_MICRO_VERSION (0) 49 | 50 | 51 | /** 52 | * BSON_PRERELEASE_VERSION: 53 | * 54 | * BSON prerelease version component (e.g. pre if %BSON_VERSION is 1.2.3-pre) 55 | */ 56 | #define BSON_PRERELEASE_VERSION () 57 | 58 | /** 59 | * BSON_VERSION: 60 | * 61 | * BSON version. 62 | */ 63 | #define BSON_VERSION (1.17.0) 64 | 65 | 66 | /** 67 | * BSON_VERSION_S: 68 | * 69 | * BSON version, encoded as a string, useful for printing and 70 | * concatenation. 71 | */ 72 | #define BSON_VERSION_S "1.17.0" 73 | 74 | 75 | /** 76 | * BSON_VERSION_HEX: 77 | * 78 | * BSON version, encoded as an hexadecimal number, useful for 79 | * integer comparisons. 80 | */ 81 | #define BSON_VERSION_HEX (BSON_MAJOR_VERSION << 24 | \ 82 | BSON_MINOR_VERSION << 16 | \ 83 | BSON_MICRO_VERSION << 8) 84 | 85 | 86 | /** 87 | * BSON_CHECK_VERSION: 88 | * @major: required major version 89 | * @minor: required minor version 90 | * @micro: required micro version 91 | * 92 | * Compile-time version checking. Evaluates to %TRUE if the version 93 | * of BSON is greater than the required one. 94 | */ 95 | #define BSON_CHECK_VERSION(major,minor,micro) \ 96 | (BSON_MAJOR_VERSION > (major) || \ 97 | (BSON_MAJOR_VERSION == (major) && BSON_MINOR_VERSION > (minor)) || \ 98 | (BSON_MAJOR_VERSION == (major) && BSON_MINOR_VERSION == (minor) && \ 99 | BSON_MICRO_VERSION >= (micro))) 100 | 101 | #endif /* BSON_VERSION_H */ 102 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-writer.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | 18 | #include "bson-private.h" 19 | #include "bson-writer.h" 20 | 21 | 22 | struct _bson_writer_t { 23 | bool ready; 24 | uint8_t **buf; 25 | size_t *buflen; 26 | size_t offset; 27 | bson_realloc_func realloc_func; 28 | void *realloc_func_ctx; 29 | bson_t b; 30 | }; 31 | 32 | 33 | /* 34 | *-------------------------------------------------------------------------- 35 | * 36 | * bson_writer_new -- 37 | * 38 | * Creates a new instance of bson_writer_t using the buffer, length, 39 | * offset, and realloc() function supplied. 40 | * 41 | * The caller is expected to clean up the structure when finished 42 | * using bson_writer_destroy(). 43 | * 44 | * Parameters: 45 | * @buf: (inout): A pointer to a target buffer. 46 | * @buflen: (inout): A pointer to the buffer length. 47 | * @offset: The offset in the target buffer to start from. 48 | * @realloc_func: A realloc() style function or NULL. 49 | * 50 | * Returns: 51 | * A newly allocated bson_writer_t that should be freed with 52 | * bson_writer_destroy(). 53 | * 54 | * Side effects: 55 | * None. 56 | * 57 | *-------------------------------------------------------------------------- 58 | */ 59 | 60 | bson_writer_t * 61 | bson_writer_new (uint8_t **buf, /* IN */ 62 | size_t *buflen, /* IN */ 63 | size_t offset, /* IN */ 64 | bson_realloc_func realloc_func, /* IN */ 65 | void *realloc_func_ctx) /* IN */ 66 | { 67 | bson_writer_t *writer; 68 | 69 | writer = bson_malloc0 (sizeof *writer); 70 | writer->buf = buf; 71 | writer->buflen = buflen; 72 | writer->offset = offset; 73 | writer->realloc_func = realloc_func; 74 | writer->realloc_func_ctx = realloc_func_ctx; 75 | writer->ready = true; 76 | 77 | return writer; 78 | } 79 | 80 | 81 | /* 82 | *-------------------------------------------------------------------------- 83 | * 84 | * bson_writer_destroy -- 85 | * 86 | * Cleanup after @writer and release any allocated memory. Note that 87 | * the buffer supplied to bson_writer_new() is NOT freed from this 88 | * method. The caller is responsible for that. 89 | * 90 | * Returns: 91 | * None. 92 | * 93 | * Side effects: 94 | * None. 95 | * 96 | *-------------------------------------------------------------------------- 97 | */ 98 | 99 | void 100 | bson_writer_destroy (bson_writer_t *writer) /* IN */ 101 | { 102 | bson_free (writer); 103 | } 104 | 105 | 106 | /* 107 | *-------------------------------------------------------------------------- 108 | * 109 | * bson_writer_get_length -- 110 | * 111 | * Fetches the current length of the content written by the buffer 112 | * (including the initial offset). This includes a partly written 113 | * document currently being written. 114 | * 115 | * This is useful if you want to check to see if you've passed a given 116 | * memory boundary that cannot be sent in a packet. See 117 | * bson_writer_rollback() to abort the current document being written. 118 | * 119 | * Returns: 120 | * The number of bytes written plus initial offset. 121 | * 122 | * Side effects: 123 | * None. 124 | * 125 | *-------------------------------------------------------------------------- 126 | */ 127 | 128 | size_t 129 | bson_writer_get_length (bson_writer_t *writer) /* IN */ 130 | { 131 | return writer->offset + writer->b.len; 132 | } 133 | 134 | 135 | /* 136 | *-------------------------------------------------------------------------- 137 | * 138 | * bson_writer_begin -- 139 | * 140 | * Begins writing a new document. The caller may use the bson 141 | * structure to write out a new BSON document. When completed, the 142 | * caller must call either bson_writer_end() or 143 | * bson_writer_rollback(). 144 | * 145 | * Parameters: 146 | * @writer: A bson_writer_t. 147 | * @bson: (out): A location for a bson_t*. 148 | * 149 | * Returns: 150 | * true if the underlying realloc was successful; otherwise false. 151 | * 152 | * Side effects: 153 | * @bson is initialized if true is returned. 154 | * 155 | *-------------------------------------------------------------------------- 156 | */ 157 | 158 | bool 159 | bson_writer_begin (bson_writer_t *writer, /* IN */ 160 | bson_t **bson) /* OUT */ 161 | { 162 | bson_impl_alloc_t *b; 163 | bool grown = false; 164 | 165 | BSON_ASSERT (writer); 166 | BSON_ASSERT (writer->ready); 167 | BSON_ASSERT (bson); 168 | 169 | writer->ready = false; 170 | 171 | memset (&writer->b, 0, sizeof (bson_t)); 172 | 173 | b = (bson_impl_alloc_t *) &writer->b; 174 | b->flags = BSON_FLAG_STATIC | BSON_FLAG_NO_FREE; 175 | b->len = 5; 176 | b->parent = NULL; 177 | b->buf = writer->buf; 178 | b->buflen = writer->buflen; 179 | b->offset = writer->offset; 180 | b->alloc = NULL; 181 | b->alloclen = 0; 182 | b->realloc = writer->realloc_func; 183 | b->realloc_func_ctx = writer->realloc_func_ctx; 184 | 185 | while ((writer->offset + writer->b.len) > *writer->buflen) { 186 | if (!writer->realloc_func) { 187 | memset (&writer->b, 0, sizeof (bson_t)); 188 | writer->ready = true; 189 | return false; 190 | } 191 | grown = true; 192 | 193 | if (!*writer->buflen) { 194 | *writer->buflen = 64; 195 | } else { 196 | (*writer->buflen) *= 2; 197 | } 198 | } 199 | 200 | if (grown) { 201 | *writer->buf = writer->realloc_func ( 202 | *writer->buf, *writer->buflen, writer->realloc_func_ctx); 203 | } 204 | 205 | memset ((*writer->buf) + writer->offset + 1, 0, 5); 206 | (*writer->buf)[writer->offset] = 5; 207 | 208 | *bson = &writer->b; 209 | 210 | return true; 211 | } 212 | 213 | 214 | /* 215 | *-------------------------------------------------------------------------- 216 | * 217 | * bson_writer_end -- 218 | * 219 | * Complete writing of a bson_writer_t to the buffer supplied. 220 | * 221 | * Returns: 222 | * None. 223 | * 224 | * Side effects: 225 | * None. 226 | * 227 | *-------------------------------------------------------------------------- 228 | */ 229 | 230 | void 231 | bson_writer_end (bson_writer_t *writer) /* IN */ 232 | { 233 | BSON_ASSERT (writer); 234 | BSON_ASSERT (!writer->ready); 235 | 236 | writer->offset += writer->b.len; 237 | memset (&writer->b, 0, sizeof (bson_t)); 238 | writer->ready = true; 239 | } 240 | 241 | 242 | /* 243 | *-------------------------------------------------------------------------- 244 | * 245 | * bson_writer_rollback -- 246 | * 247 | * Abort the appending of the current bson_t to the memory region 248 | * managed by @writer. This is useful if you detected that you went 249 | * past a particular memory limit. For example, MongoDB has 48MB 250 | * message limits. 251 | * 252 | * Returns: 253 | * None. 254 | * 255 | * Side effects: 256 | * None. 257 | * 258 | *-------------------------------------------------------------------------- 259 | */ 260 | 261 | void 262 | bson_writer_rollback (bson_writer_t *writer) /* IN */ 263 | { 264 | BSON_ASSERT (writer); 265 | 266 | if (writer->b.len) { 267 | memset (&writer->b, 0, sizeof (bson_t)); 268 | } 269 | 270 | writer->ready = true; 271 | } 272 | -------------------------------------------------------------------------------- /bsonnumpy/bson/bson-writer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013 MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bson-prelude.h" 18 | 19 | 20 | #ifndef BSON_WRITER_H 21 | #define BSON_WRITER_H 22 | 23 | 24 | #include "bson.h" 25 | 26 | 27 | BSON_BEGIN_DECLS 28 | 29 | 30 | /** 31 | * bson_writer_t: 32 | * 33 | * The bson_writer_t structure is a helper for writing a series of BSON 34 | * documents to a single malloc() buffer. You can provide a realloc() style 35 | * function to grow the buffer as you go. 36 | * 37 | * This is useful if you want to build a series of BSON documents right into 38 | * the target buffer for an outgoing packet. The offset parameter allows you to 39 | * start at an offset of the target buffer. 40 | */ 41 | typedef struct _bson_writer_t bson_writer_t; 42 | 43 | 44 | BSON_EXPORT (bson_writer_t *) 45 | bson_writer_new (uint8_t **buf, 46 | size_t *buflen, 47 | size_t offset, 48 | bson_realloc_func realloc_func, 49 | void *realloc_func_ctx); 50 | BSON_EXPORT (void) 51 | bson_writer_destroy (bson_writer_t *writer); 52 | BSON_EXPORT (size_t) 53 | bson_writer_get_length (bson_writer_t *writer); 54 | BSON_EXPORT (bool) 55 | bson_writer_begin (bson_writer_t *writer, bson_t **bson); 56 | BSON_EXPORT (void) 57 | bson_writer_end (bson_writer_t *writer); 58 | BSON_EXPORT (void) 59 | bson_writer_rollback (bson_writer_t *writer); 60 | 61 | 62 | BSON_END_DECLS 63 | 64 | 65 | #endif /* BSON_WRITER_H */ 66 | -------------------------------------------------------------------------------- /bsonnumpy/bsonnumpy.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef BSONNUMPY_H 18 | #define BSONNUMPY_H 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION 26 | #include 27 | 28 | #include "bson/bson.h" 29 | 30 | typedef struct 31 | { 32 | const char *s; 33 | size_t len; 34 | } bsnp_string_t; 35 | 36 | 37 | ssize_t 38 | bsnp_next_power_of_two(ssize_t v); 39 | 40 | void 41 | bsnp_string_init(bsnp_string_t *string, const char *s); 42 | 43 | 44 | #endif //BSONNUMPY_H 45 | -------------------------------------------------------------------------------- /bsonnumpy/bsonnumpy_field_order.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bsonnumpy_field_order.h" 18 | 19 | 20 | void 21 | field_order_init(field_order_t *order, size_t size) 22 | { 23 | order->maxsize = (size_t) bsnp_next_power_of_two(BSON_MIN(size, 8)); 24 | order->elems = bson_malloc(order->maxsize * sizeof(field_order_elem_t)); 25 | order->n_elems = 0; 26 | } 27 | 28 | 29 | void 30 | field_order_destroy(field_order_t *order) 31 | { 32 | bson_free(order->elems); 33 | } 34 | -------------------------------------------------------------------------------- /bsonnumpy/bsonnumpy_field_order.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef BSONNUMPY_FIELDorder_H 18 | #define BSONNUMPY_FIELDorder_H 19 | 20 | #include "bsonnumpy.h" 21 | 22 | 23 | static const ssize_t NO_INDEX = -1; 24 | 25 | 26 | typedef struct 27 | { 28 | bsnp_string_t key; 29 | ssize_t dtype_index; 30 | } field_order_elem_t; 31 | 32 | 33 | static const field_order_elem_t final = {{"", 0}, -1}; 34 | 35 | 36 | typedef struct 37 | { 38 | size_t maxsize; 39 | field_order_elem_t *elems; 40 | size_t n_elems; 41 | } field_order_t; 42 | 43 | 44 | void 45 | field_order_init(field_order_t *order, size_t size); 46 | 47 | 48 | static inline void 49 | field_order_set(field_order_t *order, size_t bson_index, const char *key, 50 | ssize_t dtype_index) 51 | { 52 | if (bson_index >= order->maxsize) { 53 | order->maxsize *= 2; 54 | order->elems = bson_realloc( 55 | order->elems, order->maxsize * sizeof(field_order_elem_t)); 56 | } 57 | 58 | bsnp_string_init(&order->elems[bson_index].key, key); 59 | order->elems[bson_index].dtype_index = dtype_index; 60 | order->n_elems = BSON_MAX(bson_index + 1, order->n_elems); 61 | } 62 | 63 | 64 | static inline bool 65 | field_order_match(field_order_t *order, size_t bson_index, bson_iter_t *it, 66 | const field_order_elem_t **out) 67 | { 68 | const field_order_elem_t *elem; 69 | const char *next_key; 70 | 71 | if (bson_index > order->n_elems) { 72 | return false; 73 | } 74 | 75 | if (bson_index == order->n_elems) { 76 | /* reached the end */ 77 | *out = &final; 78 | return true; 79 | } 80 | 81 | elem = &order->elems[bson_index]; 82 | 83 | if ((it->next_off + elem->key.len + 1) > it->len) { 84 | return false; 85 | } 86 | 87 | next_key = (const char *) (it->raw + it->next_off + 1); 88 | if (0 != memcmp(next_key, elem->key.s, elem->key.len)) { 89 | return false; 90 | } 91 | 92 | *out = elem; 93 | return true; 94 | } 95 | 96 | 97 | void 98 | field_order_destroy(field_order_t *order); 99 | 100 | 101 | #endif //BSONNUMPY_FIELDorder_H 102 | -------------------------------------------------------------------------------- /bsonnumpy/bsonnumpy_hashtable.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bsonnumpy_hashtable.h" 18 | 19 | 20 | /* how much larger the table is than the number of entries */ 21 | const ssize_t TABLE_MULTIPLE = 4; 22 | 23 | 24 | Py_ssize_t 25 | string_hash(const char *string, size_t len) 26 | { 27 | size_t hash = 5381; 28 | const unsigned char *p = (const unsigned char *) string; 29 | 30 | for (; len >= 8; len -= 8) { 31 | hash = ((hash << 5) + hash) + *p++; 32 | hash = ((hash << 5) + hash) + *p++; 33 | hash = ((hash << 5) + hash) + *p++; 34 | hash = ((hash << 5) + hash) + *p++; 35 | hash = ((hash << 5) + hash) + *p++; 36 | hash = ((hash << 5) + hash) + *p++; 37 | hash = ((hash << 5) + hash) + *p++; 38 | hash = ((hash << 5) + hash) + *p++; 39 | } 40 | 41 | switch (len) { 42 | case 7: hash = ((hash << 5) + hash) + *p++; 43 | case 6: hash = ((hash << 5) + hash) + *p++; 44 | case 5: hash = ((hash << 5) + hash) + *p++; 45 | case 4: hash = ((hash << 5) + hash) + *p++; 46 | case 3: hash = ((hash << 5) + hash) + *p++; 47 | case 2: hash = ((hash << 5) + hash) + *p++; 48 | case 1: hash = ((hash << 5) + hash) + *p++; 49 | break; 50 | case 0: 51 | default: 52 | break; 53 | } 54 | 55 | return hash; 56 | } 57 | 58 | 59 | void 60 | table_init(hash_table_t *table, ssize_t n_entries) 61 | { 62 | ssize_t i; 63 | 64 | table->size = bsnp_next_power_of_two(n_entries * TABLE_MULTIPLE); 65 | table->entries = bson_malloc0(table->size * sizeof(hash_table_entry_t)); 66 | 67 | for (i = 0; i < table->size; i++) { 68 | table->entries[i].value = EMPTY; 69 | } 70 | } 71 | 72 | 73 | /* simple insertion w/ robin hood hashing. keys are always unique. no resize. */ 74 | void 75 | table_insert(hash_table_t *table, const char *key, ssize_t value) 76 | { 77 | ssize_t mask = table->size - 1; 78 | ssize_t dist_key = 0; 79 | Py_ssize_t hash; 80 | ssize_t i; 81 | 82 | hash_table_entry_t entry; 83 | 84 | bsnp_string_init(&entry.key, key); 85 | entry.value = value; 86 | 87 | hash = string_hash(key, entry.key.len); 88 | 89 | /* table size is power of 2, hash & (size-1) is faster than hash % size */ 90 | i = entry.ideal_pos = hash & mask; 91 | 92 | while (true) { 93 | hash_table_entry_t *inplace; 94 | ssize_t dist_inplace; 95 | 96 | inplace = &table->entries[i]; 97 | if (inplace->value == EMPTY) { 98 | memcpy(inplace, &entry, sizeof(hash_table_entry_t)); 99 | table->used++; 100 | return; 101 | } 102 | 103 | /* this spot is taken. if this entry is closer to its ideal spot than 104 | * the input is, swap them and find a new place for this entry. */ 105 | dist_inplace = (i - inplace->ideal_pos) & mask; 106 | if (dist_inplace < dist_key) { 107 | hash_table_entry_t tmp; 108 | 109 | /* swap with input, start searching for place for swapped entry */ 110 | memcpy(&tmp, inplace, sizeof(hash_table_entry_t)); 111 | memcpy(inplace, &entry, sizeof(hash_table_entry_t)); 112 | memcpy(&entry, &tmp, sizeof(hash_table_entry_t)); 113 | 114 | dist_key = dist_inplace; 115 | } 116 | 117 | dist_key++; 118 | i++; 119 | i &= mask; 120 | } 121 | } 122 | 123 | 124 | ssize_t 125 | table_lookup(hash_table_t *table, const char *key) 126 | { 127 | ssize_t mask = table->size - 1; 128 | Py_ssize_t hash; 129 | ssize_t i; 130 | ssize_t dist_key = 0; 131 | 132 | hash = string_hash(key, strlen(key)); 133 | i = hash & mask; 134 | 135 | while (true) { 136 | hash_table_entry_t *entry = &table->entries[i]; 137 | 138 | if (entry->value == EMPTY || !strcmp(entry->key.s, key)) { 139 | return entry->value; 140 | } 141 | 142 | /* we haven't yet found the key in the table, and this entry is farther 143 | * from its ideal spot than key would be if it were here, so we know 144 | * the key is absent */ 145 | if (dist_key > ((i - entry->ideal_pos) & mask)) { 146 | return EMPTY; 147 | } 148 | 149 | dist_key++; 150 | i++; 151 | i &= mask; 152 | } 153 | } 154 | 155 | 156 | void 157 | table_destroy(hash_table_t *table) 158 | { 159 | bson_free(table->entries); 160 | } 161 | -------------------------------------------------------------------------------- /bsonnumpy/bsonnumpy_hashtable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #ifndef BSONNUMPY_HASHTABLE_H 18 | #define BSONNUMPY_HASHTABLE_H 19 | 20 | #include "bsonnumpy.h" 21 | 22 | 23 | typedef struct 24 | { 25 | bsnp_string_t key; 26 | ssize_t ideal_pos; 27 | ssize_t value; 28 | } hash_table_entry_t; 29 | 30 | typedef struct 31 | { 32 | hash_table_entry_t *entries; 33 | ssize_t size; 34 | ssize_t used; 35 | } hash_table_t; 36 | 37 | static const ssize_t EMPTY = -1; 38 | 39 | void 40 | table_init(hash_table_t *table, ssize_t n_entries); 41 | 42 | void 43 | table_insert(hash_table_t *table, const char *key, ssize_t value); 44 | 45 | ssize_t 46 | table_lookup(hash_table_t *table, const char *key); 47 | 48 | void 49 | table_destroy(hash_table_t *table); 50 | 51 | #endif //BSONNUMPY_HASHTABLE_H 52 | -------------------------------------------------------------------------------- /bsonnumpy/bsonnumpy_util.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "bsonnumpy.h" 18 | 19 | 20 | void 21 | bsnp_string_init(bsnp_string_t *string, const char *s) 22 | { 23 | string->s = s; 24 | string->len = strlen(s); 25 | } 26 | 27 | 28 | ssize_t 29 | bsnp_next_power_of_two(ssize_t v) 30 | { 31 | v--; 32 | v |= v >> 1; 33 | v |= v >> 2; 34 | v |= v >> 4; 35 | v |= v >> 8; 36 | v |= v >> 16; 37 | #if BSON_WORD_SIZE == 64 38 | v |= v >> 32; 39 | #endif 40 | v++; 41 | 42 | return v; 43 | } 44 | -------------------------------------------------------------------------------- /bsonnumpy/common/common-b64-private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018-present MongoDB Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "common-prelude.h" 18 | 19 | #ifndef COMMON_B64_PRIVATE_H 20 | #define COMMON_B64_PRIVATE_H 21 | 22 | #include 23 | 24 | /* When encoding from "network" (raw data) to "presentation" (base64 encoded). 25 | * Includes the trailing null byte. */ 26 | size_t COMMON_PREFIX (bson_b64_ntop_calculate_target_size) (size_t raw_size); 27 | 28 | /* When encoding from "presentation" (base64 encoded) to "network" (raw data). 29 | * This may be an overestimate if the base64 data includes spaces. For a more 30 | * accurate size, call bson_b64_pton (src, NULL, 0), which will read the src 31 | * data and return an exact size. */ 32 | size_t COMMON_PREFIX (bson_b64_pton_calculate_target_size) ( 33 | size_t base64_encoded_size); 34 | 35 | /* Returns the number of bytes written (excluding NULL byte) to target on 36 | * success or -1 on error. Adds a trailing NULL byte. 37 | * Encodes from "network" (raw data) to "presentation" (base64 encoded), 38 | * hence the obscure name "ntop". 39 | */ 40 | int COMMON_PREFIX (bson_b64_ntop) (uint8_t const *src, 41 | size_t srclength, 42 | char *target, 43 | size_t targsize); 44 | 45 | /* If target is not NULL, the number of bytes written to target on success or -1 46 | * on error. 47 | * If target is NULL, returns the exact number of bytes that would be 48 | * written to target on decoding. 49 | * Encodes from "presentation" (base64 encoded) to "network" (raw data), 50 | * hence the obscure name "pton". 51 | */ 52 | int COMMON_PREFIX (bson_b64_pton) (char const *src, 53 | uint8_t *target, 54 | size_t targsize); 55 | 56 | #endif /* COMMON_B64_PRIVATE_H */ 57 | -------------------------------------------------------------------------------- /bsonnumpy/common/common-config.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMON_CONFIG_H 2 | #define COMMON_CONFIG_H 3 | 4 | #define MONGOC_ENABLE_DEBUG_ASSERTIONS 0 5 | 6 | #if MONGOC_ENABLE_DEBUG_ASSERTIONS != 1 7 | # undef MONGOC_ENABLE_DEBUG_ASSERTIONS 8 | #endif 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /bsonnumpy/common/common-macros-private.h: -------------------------------------------------------------------------------- 1 | 2 | #include "common-prelude.h" 3 | 4 | #ifndef MONGO_C_DRIVER_COMMON_MACROS_H 5 | #define MONGO_C_DRIVER_COMMON_MACROS_H 6 | 7 | /* Test only assert. Is a noop unless -DENABLE_DEBUG_ASSERTIONS=ON is set 8 | * during configuration */ 9 | #if defined(MONGOC_ENABLE_DEBUG_ASSERTIONS) && defined(BSON_OS_UNIX) 10 | #define MONGOC_DEBUG_ASSERT(statement) BSON_ASSERT (statement) 11 | #else 12 | #define MONGOC_DEBUG_ASSERT(statement) ((void) 0) 13 | #endif 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /bsonnumpy/common/common-md5-private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "common-prelude.h" 18 | 19 | #ifndef COMMON_MD5_PRIVATE_H 20 | #define COMMON_MD5_PRIVATE_H 21 | 22 | #include "bson/bson.h" 23 | 24 | BSON_BEGIN_DECLS 25 | 26 | void COMMON_PREFIX (_bson_md5_init) (bson_md5_t *pms); 27 | void COMMON_PREFIX (_bson_md5_append) (bson_md5_t *pms, 28 | const uint8_t *data, 29 | uint32_t nbytes); 30 | void COMMON_PREFIX (_bson_md5_finish) (bson_md5_t *pms, uint8_t digest[16]); 31 | 32 | BSON_END_DECLS 33 | 34 | #endif /* COMMON_MD5_PRIVATE_H */ 35 | -------------------------------------------------------------------------------- /bsonnumpy/common/common-prelude.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2018-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #if !defined(MONGOC_INSIDE) && !defined(MONGOC_COMPILATION) && \ 18 | !defined(BSON_COMPILATION) && !defined(BSON_INSIDE) 19 | #error "Only or can be included directly." 20 | #endif 21 | 22 | #ifndef COMMON_PREFIX_ 23 | #define COMMON_PREFIX_ 24 | #endif 25 | #define JOINER(x, y) x##_##y 26 | #define NAME_EVALUATOR(x, y) JOINER (x, y) 27 | #define COMMON_PREFIX(name) NAME_EVALUATOR (COMMON_PREFIX_, name) 28 | -------------------------------------------------------------------------------- /bsonnumpy/common/common-thread-private.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2013-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "common-prelude.h" 18 | 19 | #ifndef COMMON_THREAD_PRIVATE_H 20 | #define COMMON_THREAD_PRIVATE_H 21 | 22 | #define BSON_INSIDE 23 | #include "bson/bson-compat.h" 24 | #include "bson/bson-config.h" 25 | #include "bson/bson-macros.h" 26 | #undef BSON_INSIDE 27 | 28 | BSON_BEGIN_DECLS 29 | 30 | #if defined(BSON_OS_UNIX) 31 | #include 32 | #define BSON_ONCE_FUN(n) void n (void) 33 | #define BSON_ONCE_RETURN return 34 | #define BSON_ONCE_INIT PTHREAD_ONCE_INIT 35 | #define bson_mutex_destroy pthread_mutex_destroy 36 | #define bson_mutex_init(_n) pthread_mutex_init ((_n), NULL) 37 | #define bson_mutex_lock pthread_mutex_lock 38 | #define bson_mutex_t pthread_mutex_t 39 | #define bson_mutex_unlock pthread_mutex_unlock 40 | #define bson_once pthread_once 41 | #define bson_once_t pthread_once_t 42 | #define bson_thread_t pthread_t 43 | #define BSON_THREAD_FUN(_function_name, _arg_name) \ 44 | void *(_function_name) (void *(_arg_name)) 45 | #define BSON_THREAD_FUN_TYPE(_function_name) void *(*(_function_name)) (void *) 46 | #define BSON_THREAD_RETURN return NULL 47 | #else 48 | #include 49 | #define BSON_ONCE_FUN(n) \ 50 | BOOL CALLBACK n (PINIT_ONCE _ignored_a, PVOID _ignored_b, PVOID *_ignored_c) 51 | #define BSON_ONCE_INIT INIT_ONCE_STATIC_INIT 52 | #define BSON_ONCE_RETURN return true 53 | #define bson_mutex_destroy DeleteCriticalSection 54 | #define bson_mutex_init InitializeCriticalSection 55 | #define bson_mutex_lock EnterCriticalSection 56 | #define bson_mutex_t CRITICAL_SECTION 57 | #define bson_mutex_unlock LeaveCriticalSection 58 | #define bson_once(o, c) InitOnceExecuteOnce (o, c, NULL, NULL) 59 | #define bson_once_t INIT_ONCE 60 | #define bson_thread_t HANDLE 61 | #define BSON_THREAD_FUN(_function_name, _arg_name) \ 62 | unsigned(__stdcall _function_name) (void *(_arg_name)) 63 | #define BSON_THREAD_FUN_TYPE(_function_name) \ 64 | unsigned(__stdcall * _function_name) (void *) 65 | #define BSON_THREAD_RETURN return 66 | #endif 67 | 68 | /* Functions that require definitions get the common prefix (_mongoc for 69 | * libmongoc or _bson for libbson) to avoid duplicate symbols when linking both 70 | * libbson and libmongoc statically. */ 71 | int COMMON_PREFIX (thread_join) (bson_thread_t thread); 72 | int COMMON_PREFIX (thread_create) (bson_thread_t *thread, 73 | BSON_THREAD_FUN_TYPE (func), 74 | void *arg); 75 | 76 | BSON_END_DECLS 77 | 78 | #endif /* COMMON_THREAD_PRIVATE_H */ 79 | -------------------------------------------------------------------------------- /bsonnumpy/common/common-thread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2020-present MongoDB, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | #include "common-thread-private.h" 18 | 19 | #if defined(BSON_OS_UNIX) 20 | int COMMON_PREFIX (thread_create) (bson_thread_t *thread, 21 | BSON_THREAD_FUN_TYPE (func), 22 | void *arg) 23 | { 24 | return pthread_create (thread, NULL, func, arg); 25 | } 26 | int COMMON_PREFIX (thread_join) (bson_thread_t thread) 27 | { 28 | return pthread_join (thread, NULL); 29 | } 30 | #else 31 | int COMMON_PREFIX (thread_create) (bson_thread_t *thread, 32 | BSON_THREAD_FUN_TYPE (func), 33 | void *arg) 34 | { 35 | *thread = (HANDLE) _beginthreadex (NULL, 0, func, arg, 0, NULL); 36 | if (0 == *thread) { 37 | return 1; 38 | } 39 | return 0; 40 | } 41 | int COMMON_PREFIX (thread_join) (bson_thread_t thread) 42 | { 43 | int ret; 44 | 45 | /* zero indicates success for WaitForSingleObject. */ 46 | ret = WaitForSingleObject (thread, INFINITE); 47 | if (WAIT_OBJECT_0 != ret) { 48 | return ret; 49 | } 50 | /* zero indicates failure for CloseHandle. */ 51 | ret = CloseHandle (thread); 52 | if (0 == ret) { 53 | return 1; 54 | } 55 | return 0; 56 | } 57 | #endif 58 | -------------------------------------------------------------------------------- /bsonnumpy/jsonsl/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2015 M. Nunberg, mnunberg@haskalah.org 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /bsonnumpy/version.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019-present MongoDB, Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | __version__ = '0.2.0.dev0' 16 | -------------------------------------------------------------------------------- /cmake/FindNumpy.cmake: -------------------------------------------------------------------------------- 1 | # Inspired by a CMake file by Anton Deguet 2 | # Run "include(FindPythonInterp)" first. 3 | 4 | # Read-Only variables: 5 | # NUMPY_FOUND 6 | # NUMPY_INCLUDE_DIR 7 | 8 | # PYTHON_EXECUTABLE is set by FindPythonInterp. 9 | if (PYTHON_EXECUTABLE) 10 | file( 11 | WRITE ${CMAKE_CURRENT_BINARY_DIR}/determineNumpyPath.py " 12 | try: 13 | import numpy 14 | print numpy.get_include() 15 | except Exception as exc: 16 | print(exc)") 17 | 18 | exec_program( 19 | "${PYTHON_EXECUTABLE}" 20 | ARGS "\"${CMAKE_CURRENT_BINARY_DIR}/determineNumpyPath.py\"" 21 | OUTPUT_VARIABLE NUMPY_PATH) 22 | endif (PYTHON_EXECUTABLE) 23 | 24 | find_path( 25 | NUMPY_INCLUDE_DIR numpy/arrayobject.h 26 | "${NUMPY_PATH}" 27 | "${PYTHON_INCLUDE_PATH}" 28 | DOC 29 | "Directory where the arrayobject.h header file can be found. \ 30 | This file is part of the numpy package") 31 | 32 | if (NUMPY_INCLUDE_DIR) 33 | message(STATUS "Found NumPy includes: ${NUMPY_INCLUDE_DIR}") 34 | set(NUMPY_FOUND 1 CACHE INTERNAL "NumPy found") 35 | endif () 36 | -------------------------------------------------------------------------------- /doc/.gitignore: -------------------------------------------------------------------------------- 1 | .doctrees 2 | _sources 3 | html 4 | -------------------------------------------------------------------------------- /doc/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | SPHINXPROJ = BSON-NumPy 8 | SOURCEDIR = . 9 | BUILDDIR = _build 10 | 11 | # Put it first so that "make" without argument is like "make help". 12 | help: 13 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 14 | 15 | .PHONY: help Makefile 16 | 17 | # Catch-all target: route all unknown targets to Sphinx using the new 18 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 19 | %: Makefile 20 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) -------------------------------------------------------------------------------- /doc/_static/.git-keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mongodb/bson-numpy/323c2ef8083048f21ab81f25b08bc82f4b85ceb9/doc/_static/.git-keep -------------------------------------------------------------------------------- /doc/_static/warnDeprecated.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function warnProjectDeprecated() { 4 | var warning = document.createElement('div'); 5 | warning.setAttribute('class', 'admonition danger'); 6 | warning.innerHTML = "

Attention

" + 7 | "

" + 8 | "BSON-NumPy has been superseded by PyMongoArrow " + 9 | "and is no longer actively developed. In addition to NumPy arrays, PyMongoArrow " + 10 | "also supports direct conversion of MongoDB query results to Pandas DataFrames and Apache Arrow Tables. " + 11 | "Users are encouraged to migrate their BSON-NumPy workloads to PyMongoArrow for continued support." + 12 | "

"; 13 | 14 | var parent = document.querySelector('div.body') 15 | || document.querySelector('div.document') 16 | || document.body; 17 | parent.insertBefore(warning, parent.firstChild); 18 | } 19 | 20 | document.addEventListener('DOMContentLoaded', warnProjectDeprecated); 21 | -------------------------------------------------------------------------------- /doc/conf.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 3 | # BSON-NumPy documentation build configuration file, created by 4 | # sphinx-quickstart on Mon Jan 16 09:23:29 2017. 5 | # 6 | # This file is execfile()d with the current directory set to its 7 | # containing dir. 8 | # 9 | # Note that not all possible configuration values are present in this 10 | # autogenerated file. 11 | # 12 | # All configuration values have a default; values that are commented out 13 | # serve to show the default. 14 | 15 | # If extensions (or modules to document with autodoc) are in another directory, 16 | # add these directories to sys.path here. If the directory is relative to the 17 | # documentation root, use os.path.abspath to make it absolute, like shown here. 18 | # 19 | # import os 20 | # import sys 21 | # sys.path.insert(0, os.path.abspath('.')) 22 | 23 | 24 | # -- General configuration ------------------------------------------------ 25 | 26 | # If your documentation needs a minimal Sphinx version, state it here. 27 | # 28 | # needs_sphinx = '1.0' 29 | 30 | # Add any Sphinx extension module names here, as strings. They can be 31 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 32 | # ones. 33 | extensions = [ 34 | 'sphinx.ext.doctest', 35 | 'sphinx.ext.intersphinx'] 36 | 37 | # Add any paths that contain templates here, relative to this directory. 38 | templates_path = ['_templates'] 39 | 40 | # The suffix(es) of source filenames. 41 | # You can specify multiple suffix as a list of string: 42 | # 43 | # source_suffix = ['.rst', '.md'] 44 | source_suffix = '.rst' 45 | 46 | # The master toctree document. 47 | master_doc = 'index' 48 | 49 | # General information about the project. 50 | project = u'BSON-NumPy' 51 | copyright = u'MongoDB, Inc. 2016-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc' 52 | author = u'Anna Herlihy and A. Jesse Jiryu Davis' 53 | 54 | # The version info for the project you're documenting, acts as replacement for 55 | # |version| and |release|, also used in various other places throughout the 56 | # built documents. 57 | 58 | from bsonnumpy import __version__ as v 59 | # The short X.Y version. 60 | version = v.rsplit('.', v.count('.') - 1)[0] 61 | # The full version, including alpha/beta/rc tags. 62 | release = v 63 | 64 | # The language for content autogenerated by Sphinx. Refer to documentation 65 | # for a list of supported languages. 66 | # 67 | # This is also used if you do content translation via gettext catalogs. 68 | # Usually you set "language" from the command line for these cases. 69 | language = None 70 | 71 | # List of patterns, relative to source directory, that match files and 72 | # directories to ignore when looking for source files. 73 | # This patterns also effect to html_static_path and html_extra_path 74 | exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] 75 | 76 | # The name of the Pygments (syntax highlighting) style to use. 77 | pygments_style = 'sphinx' 78 | 79 | # If true, `todo` and `todoList` produce output, else they produce nothing. 80 | todo_include_todos = False 81 | 82 | # Add NORMALIZE_WHITESPACE flag. 83 | import doctest 84 | 85 | doctest_default_flags = (doctest.NORMALIZE_WHITESPACE | 86 | doctest.ELLIPSIS | 87 | doctest.IGNORE_EXCEPTION_DETAIL | 88 | doctest.DONT_ACCEPT_TRUE_FOR_1) 89 | 90 | # -- Options for HTML output ---------------------------------------------- 91 | 92 | # The theme to use for HTML and HTML Help pages. See the documentation for 93 | # a list of builtin themes. 94 | # 95 | html_theme = 'alabaster' 96 | 97 | # Theme options are theme-specific and customize the look and feel of a theme 98 | # further. For a list of options available for each theme, see the 99 | # documentation. 100 | # 101 | # html_theme_options = {} 102 | 103 | # Add any paths that contain custom static files (such as style sheets) here, 104 | # relative to this directory. They are copied after the builtin static files, 105 | # so a file named "default.css" will overwrite the builtin "default.css". 106 | html_static_path = ['_static'] 107 | 108 | html_js_files = [ 109 | 'warnDeprecated.js', 110 | ] 111 | 112 | # -- Options for HTMLHelp output ------------------------------------------ 113 | 114 | # Output file base name for HTML help builder. 115 | htmlhelp_basename = 'BSON-NumPydoc' 116 | 117 | 118 | # -- Options for LaTeX output --------------------------------------------- 119 | 120 | latex_elements = { 121 | # The paper size ('letterpaper' or 'a4paper'). 122 | # 123 | # 'papersize': 'letterpaper', 124 | 125 | # The font size ('10pt', '11pt' or '12pt'). 126 | # 127 | # 'pointsize': '10pt', 128 | 129 | # Additional stuff for the LaTeX preamble. 130 | # 131 | # 'preamble': '', 132 | 133 | # Latex figure (float) alignment 134 | # 135 | # 'figure_align': 'htbp', 136 | } 137 | 138 | # Grouping the document tree into LaTeX files. List of tuples 139 | # (source start file, target name, title, 140 | # author, documentclass [howto, manual, or own class]). 141 | latex_documents = [ 142 | (master_doc, 'BSON-NumPy.tex', u'BSON-NumPy Documentation', 143 | u'Anna Herlihy and A. Jesse Jiryu Davis', 'manual'), 144 | ] 145 | 146 | 147 | # -- Options for manual page output --------------------------------------- 148 | 149 | # One entry per manual page. List of tuples 150 | # (source start file, name, description, authors, manual section). 151 | man_pages = [ 152 | (master_doc, 'bson-numpy', u'BSON-NumPy Documentation', 153 | [author], 1) 154 | ] 155 | 156 | 157 | # -- Options for Texinfo output ------------------------------------------- 158 | 159 | # Grouping the document tree into Texinfo files. List of tuples 160 | # (source start file, target name, title, author, 161 | # dir menu entry, description, category) 162 | texinfo_documents = [ 163 | (master_doc, 'BSON-NumPy', u'BSON-NumPy Documentation', 164 | author, 'BSON-NumPy', 'One line description of project.', 165 | 'Miscellaneous'), 166 | ] 167 | 168 | 169 | intersphinx_mapping = {'python': ('https://docs.python.org/3', None), 170 | 'numpy': ('https://docs.scipy.org/doc/numpy/', None)} 171 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import glob 2 | import os 3 | import subprocess 4 | import sys 5 | 6 | # Suppress warnings during shutdown, http://bugs.python.org/issue15881 7 | try: 8 | import multiprocessing 9 | except ImportError: 10 | pass 11 | 12 | from setuptools import setup, Extension 13 | from setuptools.command.build_ext import build_ext as _build_ext 14 | 15 | 16 | # Single source the version. 17 | version_file = os.path.realpath(os.path.join( 18 | os.path.dirname(__file__), 'bsonnumpy', 'version.py')) 19 | version = {} 20 | with open(version_file) as fp: 21 | exec(fp.read(), version) 22 | 23 | 24 | try: 25 | from sphinx.setup_command import BuildDoc 26 | from sphinx.cmd import build as sphinxbuild 27 | HAVE_SPHINX = True 28 | except Exception: 29 | HAVE_SPHINX = False 30 | 31 | 32 | # Hack that ensures NumPy is installed prior to the build commencing. 33 | # See http://stackoverflow.com/questions/19919905 for details. 34 | class build_ext(_build_ext): 35 | def finalize_options(self): 36 | _build_ext.finalize_options(self) 37 | try: 38 | # Prevent numpy from thinking it is still in its setup process: 39 | __builtins__.__NUMPY_SETUP__ = False 40 | except Exception as exc: 41 | print("Warning: %s" % exc) 42 | import numpy 43 | self.include_dirs.append(numpy.get_include()) 44 | 45 | 46 | CMDCLASS = {'build_ext': build_ext} 47 | 48 | 49 | # Enables building docs and running doctests from setup.py 50 | if HAVE_SPHINX: 51 | class build_sphinx(BuildDoc): 52 | 53 | description = "generate or test documentation" 54 | 55 | user_options = [("test", "t", 56 | "run doctests instead of generating documentation")] 57 | 58 | boolean_options = ["test"] 59 | 60 | def initialize_options(self): 61 | self.test = False 62 | super().initialize_options() 63 | 64 | def run(self): 65 | # Run in-place build before Sphinx doc build. 66 | ret = subprocess.call( 67 | [sys.executable, sys.argv[0], 'build_ext', '-i']) 68 | if ret != 0: 69 | raise RuntimeError("Building BSON-Numpy failed!") 70 | 71 | if not HAVE_SPHINX: 72 | raise RuntimeError("You must install Sphinx to build or test " 73 | "the documentation.") 74 | 75 | if self.test: 76 | path = os.path.join( 77 | os.path.abspath('.'), "doc", "_build", "doctest") 78 | mode = "doctest" 79 | else: 80 | path = os.path.join( 81 | os.path.abspath('.'), "doc", "_build", version) 82 | mode = "html" 83 | 84 | try: 85 | os.makedirs(path) 86 | except: 87 | pass 88 | 89 | sphinx_args = ["-E", "-b", mode, "doc", path] 90 | status = sphinxbuild.main(sphinx_args) 91 | 92 | if status: 93 | raise RuntimeError("Documentation step '%s' failed" % (mode,)) 94 | 95 | msg = "\nDocumentation step '{}' performed, results here:\n {}\n" 96 | sys.stdout.write(msg.format(mode, path)) 97 | 98 | CMDCLASS["doc"] = build_sphinx 99 | 100 | 101 | def setup_package(): 102 | with open('README.rst') as f: 103 | readme_content = f.read() 104 | 105 | build_requires = ["numpy>=1.17.0"] 106 | tests_require = build_requires + ["pymongo>=3.9.0,<4"] 107 | install_requires = build_requires + ["pymongo>=3.6.0,<4"] 108 | 109 | libraries = [] 110 | if sys.platform == "win32": 111 | libraries.append("ws2_32") 112 | elif sys.platform != "darwin": 113 | # librt may be needed for clock_gettime() 114 | libraries.append("rt") 115 | 116 | setup( 117 | name='BSON-NumPy', 118 | version=version['__version__'], 119 | description='Module for converting directly from BSON to NumPy ' 120 | 'ndarrays', 121 | long_description=readme_content, 122 | author='Anna Herlihy', 123 | author_email='mongodb-user@googlegroups.com', 124 | url='https://github.com/mongodb/bson-numpy', 125 | keywords=["mongo", "mongodb", "pymongo", "numpy", "bson"], 126 | license="Apache License, Version 2.0", 127 | python_requires=">=3.5", 128 | classifiers=[ 129 | "Development Status :: 7 - Inactive", 130 | "Intended Audience :: Developers", 131 | "License :: OSI Approved :: Apache Software License", 132 | "Operating System :: MacOS :: MacOS X", 133 | "Operating System :: POSIX", 134 | "Programming Language :: Python :: 3", 135 | "Programming Language :: Python :: 3.5", 136 | "Programming Language :: Python :: 3.6", 137 | "Programming Language :: Python :: 3.7", 138 | "Programming Language :: Python :: Implementation :: CPython", 139 | "Topic :: Database"], 140 | setup_requires=build_requires, 141 | ext_modules=[ 142 | Extension( 143 | 'bsonnumpy._cbsonnumpy', 144 | sources=(glob.glob("bsonnumpy/*.c") + 145 | glob.glob("bsonnumpy/*/*.c")), 146 | include_dirs=["bsonnumpy", "bsonnumpy/bson", 147 | "bsonnumpy/common"], 148 | define_macros=[("BSON_COMPILATION", 1)], 149 | libraries=libraries)], 150 | install_requires=install_requires, 151 | test_suite="test", 152 | tests_require=tests_require, 153 | cmdclass=CMDCLASS, 154 | packages=["bsonnumpy"]) 155 | 156 | 157 | if __name__ == '__main__': 158 | setup_package() 159 | -------------------------------------------------------------------------------- /test/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | from functools import wraps 4 | 5 | import bson 6 | import bsonnumpy 7 | import numpy as np 8 | import pymongo 9 | 10 | if sys.version_info[:2] == (2, 6): 11 | import unittest2 as unittest 12 | from unittest2 import SkipTest 13 | else: 14 | import unittest 15 | from unittest import SkipTest 16 | 17 | PY3 = sys.version_info[0] >= 3 18 | 19 | host = bson.py3compat._unicode(os.environ.get("DB_IP", 'localhost')) 20 | port = int(os.environ.get("DB_PORT", 27017)) 21 | pair = '%s:%d' % (host, port) 22 | 23 | 24 | class ClientContext(object): 25 | """ 26 | ClientContext from PyMongo test suite. May eventually need more _require 27 | functions, but for now only care if we have a server connection. 28 | """ 29 | 30 | def __init__(self): 31 | try: 32 | client = pymongo.MongoClient(host, port, 33 | serverSelectionTimeoutMS=100) 34 | client.admin.command('ismaster') # Can we connect? 35 | 36 | except pymongo.errors.ConnectionFailure: 37 | self.connected = False 38 | self.client = None 39 | else: 40 | self.connected = True 41 | self.client = pymongo.MongoClient(host, port, connect=False) 42 | 43 | def _require(self, condition, msg, func=None): 44 | def make_wrapper(f): 45 | @wraps(f) 46 | def wrap(*args, **kwargs): 47 | # Always raise SkipTest if we can't connect to MongoDB 48 | if not self.connected: 49 | raise SkipTest("Cannot connect to MongoDB on %s" % pair) 50 | if condition: 51 | return f(*args, **kwargs) 52 | raise SkipTest(msg) 53 | 54 | return wrap 55 | 56 | if func is None: 57 | def decorate(f): 58 | return make_wrapper(f) 59 | 60 | return decorate 61 | return make_wrapper(func) 62 | 63 | def require_connected(self, func): 64 | return self._require(self.connected, 65 | "Cannot connect to MongoDB on %s" % pair, 66 | func=func) 67 | 68 | 69 | client_context = ClientContext() 70 | 71 | 72 | class TestToNdarray(unittest.TestCase): 73 | if hasattr(unittest.TestCase, 'assertRaisesRegex'): 74 | assertRaisesPattern = unittest.TestCase.assertRaisesRegex 75 | else: 76 | assertRaisesPattern = unittest.TestCase.assertRaisesRegexp 77 | 78 | @classmethod 79 | def setUpClass(cls): 80 | cls.client = client_context.client 81 | 82 | def compare_seq_to_ndarray_result(self, np_type, document): 83 | data = bson._dict_to_bson(document, False, bson.DEFAULT_CODEC_OPTIONS) 84 | dtype = np.dtype(np_type) 85 | result = bsonnumpy.sequence_to_ndarray([data], dtype, 1) 86 | self.assertEqual(result.dtype, dtype) 87 | for key in document: 88 | self.assertEqual(result[0][key], document[key], 89 | "Comparison failed for type %s: %s != %s" % ( 90 | dtype, result[0][key], document[key])) 91 | 92 | def compare_elements(self, expected, actual, dtype): 93 | if isinstance(expected, dict): 94 | for key, value in expected.items(): 95 | self.compare_elements(value, actual[key], 96 | dtype=dtype.fields[key][0]) 97 | 98 | elif isinstance(expected, list): 99 | self.assertEqual(len(actual), len(expected)) 100 | 101 | # If an array's shape is (3,2), its subarrays' shapes are (2,). 102 | subdtype, shape = dtype.subdtype 103 | self.assertGreaterEqual(len(shape), 1) 104 | subarray_dtype = np.dtype((subdtype, shape[1:])) 105 | for i in range(len(expected)): 106 | self.compare_elements(expected[i], actual[i], subarray_dtype) 107 | 108 | elif dtype.kind == 'V': 109 | self.assertEqual(bytes(expected.ljust(dtype.itemsize, b'\0')), 110 | bytes(actual)) 111 | 112 | elif dtype.kind == 'S': 113 | # NumPy only stores bytes, not str. 114 | self.assertEqual(expected, actual.decode('utf-8')) 115 | 116 | else: 117 | self.assertEqual(expected, actual) 118 | 119 | # TODO: deal with both name and title in dtype 120 | def compare_results(self, dtype, expected, actual): 121 | self.assertEqual(dtype, actual.dtype) 122 | self.assertEqual(expected.count(), len(actual)) 123 | for act in actual: 124 | exp = next(expected) 125 | for name in dtype.names: 126 | self.compare_elements(exp[name], act[name], 127 | dtype=dtype.fields[name][0]) 128 | 129 | def get_cursor_sequence(self, docs): 130 | self.client.bsonnumpy_test.coll.delete_many({}) 131 | self.client.bsonnumpy_test.coll.insert_many(docs) 132 | return self.client.bsonnumpy_test.coll 133 | 134 | def make_mixed_collection_test(self, docs, dtype): 135 | coll = self.get_cursor_sequence(docs) 136 | 137 | ndarray = bsonnumpy.sequence_to_ndarray( 138 | coll.find_raw_batches(), dtype, coll.count()) 139 | self.compare_results(np.dtype(dtype), 140 | self.client.bsonnumpy_test.coll.find(), 141 | ndarray) 142 | 143 | 144 | def millis(delta): 145 | if hasattr(delta, 'total_seconds'): 146 | return delta.total_seconds() * 1000 147 | 148 | # Python 2.6. 149 | return ((delta.days * 86400 + delta.seconds) * 1000 + 150 | delta.microseconds / 1000.0) 151 | -------------------------------------------------------------------------------- /test/test_flexible_dtype.py: -------------------------------------------------------------------------------- 1 | import random 2 | import string 3 | 4 | import bson 5 | import numpy as np 6 | 7 | from test import unittest 8 | 9 | 10 | class TestFlexibleTypes(unittest.TestCase): 11 | def test_flexible_type(self): 12 | num_dicts = 10 13 | lists = [(random.choice(string.ascii_lowercase) * 10, 14 | [random.randint(0, 100), 15 | random.randint(0, 100)]) for _ in range(num_dicts)] 16 | sons = bson.SON([ 17 | (str(i), 18 | bson.SON([ 19 | ("name", lists[i][0]), 20 | ("grades", lists[i][1])])) for i in range(num_dicts) 21 | ]) 22 | 23 | dtype = np.dtype([('name', np.str, 18), ('grades', np.int32, (2,))]) 24 | ndarray = np.array(lists, dtype=dtype) 25 | 26 | self.assertEqual(ndarray.dtype, dtype) 27 | self.assertEqual(num_dicts, len(ndarray)) 28 | for i in range(num_dicts): 29 | for desc in dtype.descr: 30 | name = desc[0] 31 | if len(desc) > 2: 32 | self.assertTrue( 33 | (sons[str(i)][name] == ndarray[i][name]).all()) 34 | else: 35 | self.assertEqual(sons[str(i)][name], ndarray[i][name]) 36 | 37 | if __name__ == '__main__': 38 | unittest.main() 39 | -------------------------------------------------------------------------------- /test/test_nested.py: -------------------------------------------------------------------------------- 1 | import bson 2 | import bsonnumpy 3 | import numpy as np 4 | 5 | from test import client_context, TestToNdarray, unittest 6 | 7 | 8 | class TestNested(TestToNdarray): 9 | dtype = np.dtype([('x', np.int32), ('y', np.int32)]) 10 | bson_docs = [bson._dict_to_bson( 11 | doc, False, bson.DEFAULT_CODEC_OPTIONS) for doc in [ 12 | bson.SON([("x", i), ("y", -i)]) for i in range(10)]] 13 | ndarray = np.array([(i, -i) for i in range(10)], dtype=dtype) 14 | if hasattr(unittest.TestCase, 'assertRaisesRegex'): 15 | assertRaisesPattern = unittest.TestCase.assertRaisesRegex 16 | else: 17 | assertRaisesPattern = unittest.TestCase.assertRaisesRegexp 18 | 19 | def test_array_scalar_load00(self): 20 | # Test arrays with documents as elements 21 | 22 | son_docs = [ 23 | bson.SON([('x', [i, i, i, i])]) for i in range(2, 4)] 24 | raw_docs = [bson._dict_to_bson( 25 | doc, False, bson.DEFAULT_CODEC_OPTIONS) for doc in son_docs] 26 | dtype = np.dtype([('x', '4int32')]) 27 | 28 | ndarray = np.array([([i, i, i, i],) for i in range(2, 4)], dtype) 29 | 30 | # Correct dtype 31 | res = bsonnumpy.sequence_to_ndarray(raw_docs, dtype, 2) 32 | 33 | self.assertTrue(np.array_equal(ndarray, res)) 34 | 35 | def test_array_scalar_load0(self): 36 | # Test arrays with documents as elements 37 | 38 | son_docs = [ 39 | bson.SON([('x', [[i, i] for _ in range(3)])]) for i in range(2, 4)] 40 | raw_docs = [bson._dict_to_bson( 41 | doc, False, bson.DEFAULT_CODEC_OPTIONS) for doc in son_docs] 42 | dtype = np.dtype([('x', '(3,2)int32')]) 43 | 44 | ndarray = np.array( 45 | [([[i, i] for _ in range(3)],) for i in range(2, 4)], dtype) 46 | 47 | # Correct dtype 48 | res = bsonnumpy.sequence_to_ndarray(raw_docs, dtype, 2) 49 | 50 | self.assertTrue(np.array_equal(ndarray, res)) 51 | 52 | def test_array_scalar_load1(self): 53 | # Test arrays with documents as elements 54 | 55 | son_docs = [ 56 | bson.SON( 57 | [('x', [ 58 | bson.SON([('a', i), ('b', i)]), 59 | bson.SON([('a', -i), ('b', -i)]) 60 | ])]) for i in range(2, 4)] 61 | raw_docs = [bson._dict_to_bson( 62 | doc, False, bson.DEFAULT_CODEC_OPTIONS) for doc in son_docs] 63 | sub_dtype = np.dtype(([('a', 'int32'), ('b', 'int32')], 2)) 64 | dtype = np.dtype([('x', sub_dtype)]) 65 | 66 | # Correct dtype 67 | with self.assertRaisesPattern(bsonnumpy.error, 68 | r'unsupported BSON type: Sub-document'): 69 | bsonnumpy.sequence_to_ndarray(raw_docs, dtype, 2) 70 | 71 | def test_array_scalar_load2(self): 72 | # Test sub arrays with documents as elements 73 | son_docs = [ 74 | bson.SON( 75 | [('x', [ 76 | [ 77 | bson.SON([('a', i), ('b', i)]), 78 | bson.SON([('a', -i), ('b', -i)]) 79 | ], 80 | [ 81 | bson.SON([('c', i), ('d', i)]), 82 | bson.SON([('c', -i), ('d', -i)]) 83 | ], 84 | 85 | ])]) for i in range(2, 4)] 86 | raw_docs = [bson._dict_to_bson( 87 | doc, False, bson.DEFAULT_CODEC_OPTIONS) for doc in son_docs] 88 | sub_sub_dtype = np.dtype(([('a', 'int32'), ('b', 'int32')], 2)) 89 | sub_dtype = np.dtype((sub_sub_dtype, 2)) 90 | dtype = np.dtype([('x', sub_dtype)]) 91 | 92 | ndarray = np.array( 93 | [[([(i, i), (-i, -i)],), 94 | ([(i, i), (-i, -i)],)] for i in range(2, 4)], dtype) 95 | 96 | # Correct dtype 97 | with self.assertRaisesPattern(bsonnumpy.error, 98 | r'unsupported BSON type: unknown'): 99 | bsonnumpy.sequence_to_ndarray(raw_docs, dtype, 2) 100 | 101 | def test_array_scalar_load3(self): 102 | # Test sub arrays with documents that have arrays 103 | son_docs = [ 104 | bson.SON( 105 | [('x', [ 106 | bson.SON([('a', [i, i, i, i]), 107 | ('b', [i, i, i, i])]), 108 | bson.SON([('a', [-i, -i, -i, -i]), 109 | ('b', [-i, -i, -i, -i])]) 110 | ])]) for i in range(10)] 111 | 112 | raw_docs = [bson._dict_to_bson( 113 | doc, False, bson.DEFAULT_CODEC_OPTIONS) for doc in son_docs] 114 | sub_dtype = np.dtype(([('a', '4int32'), ('b', '4int32')], 2)) 115 | dtype = np.dtype([('x', sub_dtype)]) 116 | 117 | # Correct dtype 118 | with self.assertRaisesPattern(bsonnumpy.error, 119 | r'unsupported BSON type: Sub-document'): 120 | bsonnumpy.sequence_to_ndarray(raw_docs, dtype, 4) 121 | 122 | def test_array_scalar_load4(self): 123 | # Test documents with multiple levels of sub documents 124 | son_docs = [ 125 | bson.SON( 126 | [('x', [ 127 | [ 128 | bson.SON([('a', i), ('b', i)]), 129 | bson.SON([('a', -i), ('b', -i)]) 130 | ], 131 | [ 132 | bson.SON([('c', i), ('d', i)]), 133 | bson.SON([('c', -i), ('d', -i)]) 134 | ], 135 | 136 | ])]) for i in range(10)] 137 | raw_docs = [bson._dict_to_bson( 138 | doc, False, bson.DEFAULT_CODEC_OPTIONS) for doc in son_docs] 139 | sub_sub_sub_dtype = np.dtype([('q', 'int32')]) 140 | sub_sub_dtype = np.dtype( 141 | ([('a', sub_sub_sub_dtype), ('b', sub_sub_sub_dtype)], 2)) 142 | sub_dtype = np.dtype((sub_sub_dtype, 2)) 143 | dtype = np.dtype([('x', sub_dtype)]) 144 | 145 | # Correct dtype 146 | with self.assertRaisesPattern(bsonnumpy.error, 147 | r'unsupported BSON type: unknown'): 148 | bsonnumpy.sequence_to_ndarray(raw_docs, dtype, 4) 149 | -------------------------------------------------------------------------------- /test/test_pandas.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | try: 4 | import pandas as pd 5 | from pandas.testing import assert_index_equal 6 | except ImportError: 7 | pd = None 8 | 9 | import numpy as np 10 | 11 | import bsonnumpy 12 | from test import client_context, unittest 13 | 14 | 15 | def to_dataframe(seq, dtype, n): 16 | data = bsonnumpy.sequence_to_ndarray(seq, dtype, n) 17 | if '_id' in dtype.fields: 18 | return pd.DataFrame(data, index=data['_id']) 19 | else: 20 | return pd.DataFrame(data) 21 | 22 | 23 | @unittest.skipUnless(pd, "requires pandas") 24 | class TestSequence2Pandas(unittest.TestCase): 25 | def dataframe_test(self, docs, dtype): 26 | db = client_context.client.bsonnumpy_test 27 | coll = db.coll 28 | coll.delete_many({}) 29 | coll.insert_many(docs) 30 | return to_dataframe(coll.find_raw_batches().sort('_id'), dtype, coll.count()) 31 | 32 | @client_context.require_connected 33 | def test_one_value(self): 34 | docs = [{"_id": i} for i in range(10, 20)] 35 | df = self.dataframe_test(docs, np.dtype([('_id', np.int32)])) 36 | self.assertEqual(df.shape, (10, 1)) 37 | self.assertEqual(df['_id'].name, '_id') 38 | self.assertEqual(df['_id'].dtype, np.int32) 39 | np.testing.assert_array_equal(df['_id'].values, np.arange(10, 20)) 40 | 41 | @client_context.require_connected 42 | def test_multi_values(self): 43 | now = datetime.datetime.now() 44 | docs = [{'d': 2.0, 45 | 'int32': 1, 46 | 'int64': 2 ** 40, 47 | 'b': False, 48 | 'dt': now}] 49 | 50 | df = self.dataframe_test(docs, 51 | np.dtype([ 52 | ('_id', 'S12'), 53 | ('d', np.double), 54 | ('int32', '