|
| 1 | +/* |
| 2 | + * Copyright 2009-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 "bson-endian.h" |
| 18 | + |
| 19 | +#ifndef _CBSONMODULE_H |
| 20 | +#define _CBSONMODULE_H |
| 21 | + |
| 22 | +#if defined(WIN32) || defined(_MSC_VER) |
| 23 | +/* |
| 24 | + * This macro is basically an implementation of asprintf for win32 |
| 25 | + * We print to the provided buffer to get the string value as an int. |
| 26 | + * USE LL2STR. This is kept only to test LL2STR. |
| 27 | + */ |
| 28 | +#if defined(_MSC_VER) && (_MSC_VER >= 1400) |
| 29 | +#define INT2STRING(buffer, i) \ |
| 30 | + _snprintf_s((buffer), \ |
| 31 | + _scprintf("%lld", (i)) + 1, \ |
| 32 | + _scprintf("%lld", (i)) + 1, \ |
| 33 | + "%lld", \ |
| 34 | + (i)) |
| 35 | +#define STRCAT(dest, n, src) strcat_s((dest), (n), (src)) |
| 36 | +#else |
| 37 | +#define INT2STRING(buffer, i) \ |
| 38 | + _snprintf((buffer), \ |
| 39 | + _scprintf("%lld", (i)) + 1, \ |
| 40 | + "%lld", \ |
| 41 | + (i)) |
| 42 | +#define STRCAT(dest, n, src) strcat((dest), (src)) |
| 43 | +#endif |
| 44 | +#else |
| 45 | +#define INT2STRING(buffer, i) snprintf((buffer), sizeof((buffer)), "%lld", (i)) |
| 46 | +#define STRCAT(dest, n, src) strcat((dest), (src)) |
| 47 | +#endif |
| 48 | + |
| 49 | +/* Just enough space in char array to hold LLONG_MIN and null terminator */ |
| 50 | +#define BUF_SIZE 21 |
| 51 | +/* Converts integer to its string representation in decimal notation. */ |
| 52 | +extern int cbson_long_long_to_str(long long int num, char* str, size_t size); |
| 53 | +#define LL2STR(buffer, i) cbson_long_long_to_str((i), (buffer), sizeof(buffer)) |
| 54 | + |
| 55 | +typedef struct type_registry_t { |
| 56 | + PyObject* encoder_map; |
| 57 | + PyObject* decoder_map; |
| 58 | + PyObject* fallback_encoder; |
| 59 | + PyObject* registry_obj; |
| 60 | + unsigned char is_encoder_empty; |
| 61 | + unsigned char is_decoder_empty; |
| 62 | + unsigned char has_fallback_encoder; |
| 63 | +} type_registry_t; |
| 64 | + |
| 65 | +typedef struct codec_options_t { |
| 66 | + PyObject* document_class; |
| 67 | + unsigned char tz_aware; |
| 68 | + unsigned char uuid_rep; |
| 69 | + char* unicode_decode_error_handler; |
| 70 | + PyObject* tzinfo; |
| 71 | + type_registry_t type_registry; |
| 72 | + unsigned char datetime_conversion; |
| 73 | + PyObject* options_obj; |
| 74 | + unsigned char is_raw_bson; |
| 75 | +} codec_options_t; |
| 76 | + |
| 77 | +/* C API functions */ |
| 78 | +#define _cbson_buffer_write_bytes_INDEX 0 |
| 79 | +#define _cbson_buffer_write_bytes_RETURN int |
| 80 | +#define _cbson_buffer_write_bytes_PROTO (buffer_t buffer, const char* data, int size) |
| 81 | + |
| 82 | +#define _cbson_write_dict_INDEX 1 |
| 83 | +#define _cbson_write_dict_RETURN int |
| 84 | +#define _cbson_write_dict_PROTO (PyObject* self, buffer_t buffer, PyObject* dict, unsigned char check_keys, const codec_options_t* options, unsigned char top_level) |
| 85 | + |
| 86 | +#define _cbson_write_pair_INDEX 2 |
| 87 | +#define _cbson_write_pair_RETURN int |
| 88 | +#define _cbson_write_pair_PROTO (PyObject* self, buffer_t buffer, const char* name, int name_length, PyObject* value, unsigned char check_keys, const codec_options_t* options, unsigned char allow_id) |
| 89 | + |
| 90 | +#define _cbson_decode_and_write_pair_INDEX 3 |
| 91 | +#define _cbson_decode_and_write_pair_RETURN int |
| 92 | +#define _cbson_decode_and_write_pair_PROTO (PyObject* self, buffer_t buffer, PyObject* key, PyObject* value, unsigned char check_keys, const codec_options_t* options, unsigned char top_level) |
| 93 | + |
| 94 | +#define _cbson_convert_codec_options_INDEX 4 |
| 95 | +#define _cbson_convert_codec_options_RETURN int |
| 96 | +#define _cbson_convert_codec_options_PROTO (PyObject* self, PyObject* options_obj, codec_options_t* options) |
| 97 | + |
| 98 | +#define _cbson_destroy_codec_options_INDEX 5 |
| 99 | +#define _cbson_destroy_codec_options_RETURN void |
| 100 | +#define _cbson_destroy_codec_options_PROTO (codec_options_t* options) |
| 101 | + |
| 102 | +#define _cbson_buffer_write_double_INDEX 6 |
| 103 | +#define _cbson_buffer_write_double_RETURN int |
| 104 | +#define _cbson_buffer_write_double_PROTO (buffer_t buffer, double data) |
| 105 | + |
| 106 | +#define _cbson_buffer_write_int32_INDEX 7 |
| 107 | +#define _cbson_buffer_write_int32_RETURN int |
| 108 | +#define _cbson_buffer_write_int32_PROTO (buffer_t buffer, int32_t data) |
| 109 | + |
| 110 | +#define _cbson_buffer_write_int64_INDEX 8 |
| 111 | +#define _cbson_buffer_write_int64_RETURN int |
| 112 | +#define _cbson_buffer_write_int64_PROTO (buffer_t buffer, int64_t data) |
| 113 | + |
| 114 | +#define _cbson_buffer_write_int32_at_position_INDEX 9 |
| 115 | +#define _cbson_buffer_write_int32_at_position_RETURN void |
| 116 | +#define _cbson_buffer_write_int32_at_position_PROTO (buffer_t buffer, int position, int32_t data) |
| 117 | + |
| 118 | +#define _cbson_downcast_and_check_INDEX 10 |
| 119 | +#define _cbson_downcast_and_check_RETURN int |
| 120 | +#define _cbson_downcast_and_check_PROTO (Py_ssize_t size, uint8_t extra) |
| 121 | + |
| 122 | +/* Total number of C API pointers */ |
| 123 | +#define _cbson_API_POINTER_COUNT 11 |
| 124 | + |
| 125 | +#ifdef _CBSON_MODULE |
| 126 | +/* This section is used when compiling _cbsonmodule */ |
| 127 | + |
| 128 | +static _cbson_buffer_write_bytes_RETURN buffer_write_bytes _cbson_buffer_write_bytes_PROTO; |
| 129 | + |
| 130 | +static _cbson_write_dict_RETURN write_dict _cbson_write_dict_PROTO; |
| 131 | + |
| 132 | +static _cbson_write_pair_RETURN write_pair _cbson_write_pair_PROTO; |
| 133 | + |
| 134 | +static _cbson_decode_and_write_pair_RETURN decode_and_write_pair _cbson_decode_and_write_pair_PROTO; |
| 135 | + |
| 136 | +static _cbson_convert_codec_options_RETURN convert_codec_options _cbson_convert_codec_options_PROTO; |
| 137 | + |
| 138 | +static _cbson_destroy_codec_options_RETURN destroy_codec_options _cbson_destroy_codec_options_PROTO; |
| 139 | + |
| 140 | +static _cbson_buffer_write_double_RETURN buffer_write_double _cbson_buffer_write_double_PROTO; |
| 141 | + |
| 142 | +static _cbson_buffer_write_int32_RETURN buffer_write_int32 _cbson_buffer_write_int32_PROTO; |
| 143 | + |
| 144 | +static _cbson_buffer_write_int64_RETURN buffer_write_int64 _cbson_buffer_write_int64_PROTO; |
| 145 | + |
| 146 | +static _cbson_buffer_write_int32_at_position_RETURN buffer_write_int32_at_position _cbson_buffer_write_int32_at_position_PROTO; |
| 147 | + |
| 148 | +static _cbson_downcast_and_check_RETURN _downcast_and_check _cbson_downcast_and_check_PROTO; |
| 149 | + |
| 150 | +#else |
| 151 | +/* This section is used in modules that use _cbsonmodule's API */ |
| 152 | + |
| 153 | +static void **_cbson_API; |
| 154 | + |
| 155 | +#define buffer_write_bytes (*(_cbson_buffer_write_bytes_RETURN (*)_cbson_buffer_write_bytes_PROTO) _cbson_API[_cbson_buffer_write_bytes_INDEX]) |
| 156 | + |
| 157 | +#define write_dict (*(_cbson_write_dict_RETURN (*)_cbson_write_dict_PROTO) _cbson_API[_cbson_write_dict_INDEX]) |
| 158 | + |
| 159 | +#define write_pair (*(_cbson_write_pair_RETURN (*)_cbson_write_pair_PROTO) _cbson_API[_cbson_write_pair_INDEX]) |
| 160 | + |
| 161 | +#define decode_and_write_pair (*(_cbson_decode_and_write_pair_RETURN (*)_cbson_decode_and_write_pair_PROTO) _cbson_API[_cbson_decode_and_write_pair_INDEX]) |
| 162 | + |
| 163 | +#define convert_codec_options (*(_cbson_convert_codec_options_RETURN (*)_cbson_convert_codec_options_PROTO) _cbson_API[_cbson_convert_codec_options_INDEX]) |
| 164 | + |
| 165 | +#define destroy_codec_options (*(_cbson_destroy_codec_options_RETURN (*)_cbson_destroy_codec_options_PROTO) _cbson_API[_cbson_destroy_codec_options_INDEX]) |
| 166 | + |
| 167 | +#define buffer_write_double (*(_cbson_buffer_write_double_RETURN (*)_cbson_buffer_write_double_PROTO) _cbson_API[_cbson_buffer_write_double_INDEX]) |
| 168 | + |
| 169 | +#define buffer_write_int32 (*(_cbson_buffer_write_int32_RETURN (*)_cbson_buffer_write_int32_PROTO) _cbson_API[_cbson_buffer_write_int32_INDEX]) |
| 170 | + |
| 171 | +#define buffer_write_int64 (*(_cbson_buffer_write_int64_RETURN (*)_cbson_buffer_write_int64_PROTO) _cbson_API[_cbson_buffer_write_int64_INDEX]) |
| 172 | + |
| 173 | +#define buffer_write_int32_at_position (*(_cbson_buffer_write_int32_at_position_RETURN (*)_cbson_buffer_write_int32_at_position_PROTO) _cbson_API[_cbson_buffer_write_int32_at_position_INDEX]) |
| 174 | + |
| 175 | +#define _downcast_and_check (*(_cbson_downcast_and_check_RETURN (*)_cbson_downcast_and_check_PROTO) _cbson_API[_cbson_downcast_and_check_INDEX]) |
| 176 | + |
| 177 | +#define _cbson_IMPORT _cbson_API = (void **)PyCapsule_Import("_cbson._C_API", 0) |
| 178 | + |
| 179 | +#endif |
| 180 | + |
| 181 | +#endif // _CBSONMODULE_H |
0 commit comments