bson 4.5.0 → 4.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/README.md +7 -5
- data/ext/bson/{native-endian.h → bson-endian.h} +5 -99
- data/ext/bson/bson-native.h +112 -0
- data/ext/bson/bytebuf.c +133 -0
- data/ext/bson/endian.c +116 -0
- data/ext/bson/init.c +289 -0
- data/ext/bson/libbson-utf8.c +230 -0
- data/ext/bson/read.c +294 -0
- data/ext/bson/util.c +55 -0
- data/ext/bson/write.c +637 -0
- data/lib/bson/document.rb +43 -1
- data/lib/bson/hash.rb +11 -2
- data/lib/bson/int32.rb +19 -13
- data/lib/bson/int64.rb +19 -13
- data/lib/bson/version.rb +1 -1
- data/spec/bson/byte_buffer_read_spec.rb +141 -0
- data/spec/bson/byte_buffer_spec.rb +14 -451
- data/spec/bson/byte_buffer_write_spec.rb +758 -0
- data/spec/bson/corpus_spec.rb +8 -5
- data/spec/bson/document_spec.rb +29 -29
- data/spec/bson/hash_spec.rb +65 -0
- data/spec/bson/int32_spec.rb +21 -3
- data/spec/bson/int64_spec.rb +22 -3
- data/spec/bson/string_spec.rb +18 -0
- data/spec/support/corpus-tests/array.json +8 -2
- data/spec/support/shared_examples.rb +2 -4
- data/spec/support/utils.rb +10 -0
- metadata +74 -55
- metadata.gz.sig +0 -0
- data/ext/bson/bson_native.c +0 -1344
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e49b6acc5ef1fcd05a0a246bd4c7c03682840f952f265de4b98cbc82137cb94
|
4
|
+
data.tar.gz: 2002ec01d574018844a22afa695b08e876ba7e5dfbeeaa8fd6c12632c5f357e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6ba14998b5902fd6f69c555dd78775c9818731102cbd4fb16bf9ee94697c81a223ddc250eec6720693d89cc7eb19163d011c69158e419d062fc0bd184c98f90
|
7
|
+
data.tar.gz: 2c0418f4761c196bac99e787fe7216aa4c5905fadccbadc1206ba6d86902ed28c52609ffd60ef21e0b54388ec1a7fd44e147a42b4217e4e605aa26735f0be968
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/README.md
CHANGED
@@ -6,18 +6,19 @@ An implementation of the BSON specification in Ruby.
|
|
6
6
|
Compatibility
|
7
7
|
-------------
|
8
8
|
|
9
|
-
BSON is tested against MRI (
|
9
|
+
BSON is tested against MRI (2.3+) and JRuby (9.2+).
|
10
10
|
|
11
11
|
Documentation
|
12
12
|
-------------
|
13
13
|
|
14
|
-
Current documentation can be found
|
14
|
+
Current documentation can be found
|
15
|
+
[here](http://docs.mongodb.org/ecosystem/tutorial/ruby-bson-tutorial/#ruby-bson-tutorial).
|
15
16
|
|
16
17
|
API Documentation
|
17
18
|
-----------------
|
18
19
|
|
19
|
-
The [API Documentation](
|
20
|
-
located at
|
20
|
+
The [API Documentation](https://api.mongodb.com/bson-ruby/current/) is
|
21
|
+
located at api.mongodb.com.
|
21
22
|
|
22
23
|
BSON Specification
|
23
24
|
------------------
|
@@ -27,7 +28,8 @@ The [BSON specification](http://bsonspec.org) is at bsonspec.org.
|
|
27
28
|
Versioning
|
28
29
|
----------
|
29
30
|
|
30
|
-
As of 2.0.0, this project adheres to the
|
31
|
+
As of 2.0.0, this project adheres to the
|
32
|
+
[Semantic Versioning Specification](http://semver.org/).
|
31
33
|
|
32
34
|
License
|
33
35
|
-------
|
@@ -1,5 +1,5 @@
|
|
1
1
|
/*
|
2
|
-
* Copyright 2015-2019 MongoDB, Inc.
|
2
|
+
* Copyright (C) 2015-2019 MongoDB, Inc.
|
3
3
|
*
|
4
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
5
|
* you may not use this file except in compliance with the License.
|
@@ -106,101 +106,7 @@
|
|
106
106
|
# error "The endianness of target architecture is unknown."
|
107
107
|
#endif
|
108
108
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
*
|
114
|
-
* Fallback endianness conversion for 16-bit integers.
|
115
|
-
*
|
116
|
-
* Returns:
|
117
|
-
* The endian swapped version.
|
118
|
-
*
|
119
|
-
* Side effects:
|
120
|
-
* None.
|
121
|
-
*
|
122
|
-
*--------------------------------------------------------------------------
|
123
|
-
*/
|
124
|
-
static uint16_t __bson_uint16_swap_slow(uint16_t v)
|
125
|
-
{
|
126
|
-
return ((v & 0x00FF) << 8) |
|
127
|
-
((v & 0xFF00) >> 8);
|
128
|
-
}
|
129
|
-
|
130
|
-
/*
|
131
|
-
*--------------------------------------------------------------------------
|
132
|
-
*
|
133
|
-
* __bson_uint32_swap_slow --
|
134
|
-
*
|
135
|
-
* Fallback endianness conversion for 32-bit integers.
|
136
|
-
*
|
137
|
-
* Returns:
|
138
|
-
* The endian swapped version.
|
139
|
-
*
|
140
|
-
* Side effects:
|
141
|
-
* None.
|
142
|
-
*
|
143
|
-
*--------------------------------------------------------------------------
|
144
|
-
*/
|
145
|
-
static uint32_t __bson_uint32_swap_slow(uint32_t v)
|
146
|
-
{
|
147
|
-
return ((v & 0x000000FFU) << 24) |
|
148
|
-
((v & 0x0000FF00U) << 8) |
|
149
|
-
((v & 0x00FF0000U) >> 8) |
|
150
|
-
((v & 0xFF000000U) >> 24);
|
151
|
-
}
|
152
|
-
|
153
|
-
|
154
|
-
/*
|
155
|
-
*--------------------------------------------------------------------------
|
156
|
-
*
|
157
|
-
* __bson_uint64_swap_slow --
|
158
|
-
*
|
159
|
-
* Fallback endianness conversion for 64-bit integers.
|
160
|
-
*
|
161
|
-
* Returns:
|
162
|
-
* The endian swapped version.
|
163
|
-
*
|
164
|
-
* Side effects:
|
165
|
-
* None.
|
166
|
-
*
|
167
|
-
*--------------------------------------------------------------------------
|
168
|
-
*/
|
169
|
-
static uint64_t __bson_uint64_swap_slow(uint64_t v)
|
170
|
-
{
|
171
|
-
return ((v & 0x00000000000000FFULL) << 56) |
|
172
|
-
((v & 0x000000000000FF00ULL) << 40) |
|
173
|
-
((v & 0x0000000000FF0000ULL) << 24) |
|
174
|
-
((v & 0x00000000FF000000ULL) << 8) |
|
175
|
-
((v & 0x000000FF00000000ULL) >> 8) |
|
176
|
-
((v & 0x0000FF0000000000ULL) >> 24) |
|
177
|
-
((v & 0x00FF000000000000ULL) >> 40) |
|
178
|
-
((v & 0xFF00000000000000ULL) >> 56);
|
179
|
-
}
|
180
|
-
|
181
|
-
/*
|
182
|
-
*--------------------------------------------------------------------------
|
183
|
-
*
|
184
|
-
* __bson_double_swap_slow --
|
185
|
-
*
|
186
|
-
* Fallback endianness conversion for double floating point.
|
187
|
-
*
|
188
|
-
* Returns:
|
189
|
-
* The endian swapped version.
|
190
|
-
*
|
191
|
-
* Side effects:
|
192
|
-
* None.
|
193
|
-
*
|
194
|
-
*--------------------------------------------------------------------------
|
195
|
-
*/
|
196
|
-
static double __bson_double_swap_slow(double v)
|
197
|
-
{
|
198
|
-
uint64_t uv;
|
199
|
-
|
200
|
-
memcpy(&uv, &v, sizeof(v));
|
201
|
-
uv = BSON_UINT64_SWAP_LE_BE(uv);
|
202
|
-
memcpy(&v, &uv, sizeof(v));
|
203
|
-
|
204
|
-
return v;
|
205
|
-
}
|
206
|
-
|
109
|
+
uint16_t __bson_uint16_swap_slow(uint16_t v);
|
110
|
+
uint32_t __bson_uint32_swap_slow(uint32_t v);
|
111
|
+
uint64_t __bson_uint64_swap_slow(uint64_t v);
|
112
|
+
double __bson_double_swap_slow(double v);
|
@@ -0,0 +1,112 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2009-2019 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 <ruby.h>
|
18
|
+
#include <stdbool.h>
|
19
|
+
#include <unistd.h>
|
20
|
+
#include <time.h>
|
21
|
+
#include "bson-endian.h"
|
22
|
+
|
23
|
+
void
|
24
|
+
rb_bson_utf8_validate (const char *utf8, /* IN */
|
25
|
+
size_t utf8_len, /* IN */
|
26
|
+
bool allow_null, /* IN */
|
27
|
+
const char *data_type); /* IN */
|
28
|
+
|
29
|
+
#define BSON_BYTE_BUFFER_SIZE 1024
|
30
|
+
|
31
|
+
#ifndef HOST_NAME_HASH_MAX
|
32
|
+
#define HOST_NAME_HASH_MAX 256
|
33
|
+
#endif
|
34
|
+
|
35
|
+
#define BSON_TYPE_DOUBLE 1
|
36
|
+
#define BSON_TYPE_STRING 2
|
37
|
+
#define BSON_TYPE_DOCUMENT 3
|
38
|
+
#define BSON_TYPE_ARRAY 4
|
39
|
+
#define BSON_TYPE_BOOLEAN 8
|
40
|
+
#define BSON_TYPE_INT32 16
|
41
|
+
#define BSON_TYPE_INT64 18
|
42
|
+
|
43
|
+
typedef struct {
|
44
|
+
size_t size;
|
45
|
+
size_t write_position;
|
46
|
+
size_t read_position;
|
47
|
+
char buffer[BSON_BYTE_BUFFER_SIZE];
|
48
|
+
char *b_ptr;
|
49
|
+
} byte_buffer_t;
|
50
|
+
|
51
|
+
#define READ_PTR(byte_buffer_ptr) \
|
52
|
+
(byte_buffer_ptr->b_ptr + byte_buffer_ptr->read_position)
|
53
|
+
|
54
|
+
#define READ_SIZE(byte_buffer_ptr) \
|
55
|
+
(byte_buffer_ptr->write_position - byte_buffer_ptr->read_position)
|
56
|
+
|
57
|
+
#define WRITE_PTR(byte_buffer_ptr) \
|
58
|
+
(byte_buffer_ptr->b_ptr + byte_buffer_ptr->write_position)
|
59
|
+
|
60
|
+
#define ENSURE_BSON_WRITE(buffer_ptr, length) \
|
61
|
+
{ if (buffer_ptr->write_position + length > buffer_ptr->size) rb_bson_expand_buffer(buffer_ptr, length); }
|
62
|
+
|
63
|
+
#define ENSURE_BSON_READ(buffer_ptr, length) \
|
64
|
+
{ if (buffer_ptr->read_position + length > buffer_ptr->write_position) \
|
65
|
+
rb_raise(rb_eRangeError, "Attempted to read %zu bytes, but only %zu bytes remain", (size_t)length, READ_SIZE(buffer_ptr)); }
|
66
|
+
|
67
|
+
VALUE rb_bson_byte_buffer_allocate(VALUE klass);
|
68
|
+
VALUE rb_bson_byte_buffer_initialize(int argc, VALUE *argv, VALUE self);
|
69
|
+
VALUE rb_bson_byte_buffer_length(VALUE self);
|
70
|
+
VALUE rb_bson_byte_buffer_get_byte(VALUE self);
|
71
|
+
VALUE rb_bson_byte_buffer_get_bytes(VALUE self, VALUE i);
|
72
|
+
VALUE rb_bson_byte_buffer_get_cstring(VALUE self);
|
73
|
+
VALUE rb_bson_byte_buffer_get_decimal128_bytes(VALUE self);
|
74
|
+
VALUE rb_bson_byte_buffer_get_double(VALUE self);
|
75
|
+
VALUE rb_bson_byte_buffer_get_int32(VALUE self);
|
76
|
+
VALUE rb_bson_byte_buffer_get_int64(VALUE self);
|
77
|
+
VALUE rb_bson_byte_buffer_get_string(VALUE self);
|
78
|
+
VALUE rb_bson_byte_buffer_get_hash(VALUE self);
|
79
|
+
VALUE rb_bson_byte_buffer_get_array(VALUE self);
|
80
|
+
VALUE rb_bson_byte_buffer_put_byte(VALUE self, VALUE byte);
|
81
|
+
VALUE rb_bson_byte_buffer_put_bytes(VALUE self, VALUE bytes);
|
82
|
+
VALUE rb_bson_byte_buffer_put_cstring(VALUE self, VALUE string);
|
83
|
+
VALUE rb_bson_byte_buffer_put_decimal128(VALUE self, VALUE low, VALUE high);
|
84
|
+
VALUE rb_bson_byte_buffer_put_double(VALUE self, VALUE f);
|
85
|
+
VALUE rb_bson_byte_buffer_put_int32(VALUE self, VALUE i);
|
86
|
+
VALUE rb_bson_byte_buffer_put_int64(VALUE self, VALUE i);
|
87
|
+
VALUE rb_bson_byte_buffer_put_string(VALUE self, VALUE string);
|
88
|
+
VALUE rb_bson_byte_buffer_put_symbol(VALUE self, VALUE symbol);
|
89
|
+
VALUE rb_bson_byte_buffer_put_hash(VALUE self, VALUE hash, VALUE validating_keys);
|
90
|
+
VALUE rb_bson_byte_buffer_put_array(VALUE self, VALUE array, VALUE validating_keys);
|
91
|
+
VALUE rb_bson_byte_buffer_read_position(VALUE self);
|
92
|
+
VALUE rb_bson_byte_buffer_replace_int32(VALUE self, VALUE index, VALUE i);
|
93
|
+
VALUE rb_bson_byte_buffer_rewind(VALUE self);
|
94
|
+
VALUE rb_bson_byte_buffer_write_position(VALUE self);
|
95
|
+
VALUE rb_bson_byte_buffer_to_s(VALUE self);
|
96
|
+
VALUE rb_bson_object_id_generator_next(int argc, VALUE* args, VALUE self);
|
97
|
+
|
98
|
+
size_t rb_bson_byte_buffer_memsize(const void *ptr);
|
99
|
+
void rb_bson_byte_buffer_free(void *ptr);
|
100
|
+
void rb_bson_expand_buffer(byte_buffer_t* buffer_ptr, size_t length);
|
101
|
+
void rb_bson_generate_machine_id(VALUE rb_md5_class, char *rb_bson_machine_id);
|
102
|
+
|
103
|
+
/**
|
104
|
+
* The counter for incrementing object ids.
|
105
|
+
*/
|
106
|
+
extern uint32_t rb_bson_object_id_counter;
|
107
|
+
|
108
|
+
extern VALUE rb_bson_registry;
|
109
|
+
|
110
|
+
extern VALUE rb_bson_illegal_key;
|
111
|
+
|
112
|
+
extern const rb_data_type_t rb_byte_buffer_data_type;
|
data/ext/bson/bytebuf.c
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2009-2019 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-native.h"
|
18
|
+
|
19
|
+
/**
|
20
|
+
* Allocates a bson byte buffer that wraps a byte_buffer_t.
|
21
|
+
*/
|
22
|
+
VALUE rb_bson_byte_buffer_allocate(VALUE klass)
|
23
|
+
{
|
24
|
+
byte_buffer_t *b;
|
25
|
+
VALUE obj = TypedData_Make_Struct(klass, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
26
|
+
b->b_ptr = b->buffer;
|
27
|
+
b->size = BSON_BYTE_BUFFER_SIZE;
|
28
|
+
return obj;
|
29
|
+
}
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Initialize a byte buffer.
|
33
|
+
*/
|
34
|
+
VALUE rb_bson_byte_buffer_initialize(int argc, VALUE *argv, VALUE self)
|
35
|
+
{
|
36
|
+
VALUE bytes;
|
37
|
+
rb_scan_args(argc, argv, "01", &bytes);
|
38
|
+
|
39
|
+
if (!NIL_P(bytes)) {
|
40
|
+
rb_bson_byte_buffer_put_bytes(self, bytes);
|
41
|
+
}
|
42
|
+
|
43
|
+
return self;
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Expand the byte buffer linearly.
|
48
|
+
*/
|
49
|
+
void rb_bson_expand_buffer(byte_buffer_t* buffer_ptr, size_t length)
|
50
|
+
{
|
51
|
+
const size_t required_size = buffer_ptr->write_position - buffer_ptr->read_position + length;
|
52
|
+
if (required_size <= buffer_ptr->size) {
|
53
|
+
memmove(buffer_ptr->b_ptr, READ_PTR(buffer_ptr), READ_SIZE(buffer_ptr));
|
54
|
+
buffer_ptr->write_position -= buffer_ptr->read_position;
|
55
|
+
buffer_ptr->read_position = 0;
|
56
|
+
} else {
|
57
|
+
char *new_b_ptr;
|
58
|
+
const size_t new_size = required_size * 2;
|
59
|
+
new_b_ptr = ALLOC_N(char, new_size);
|
60
|
+
memcpy(new_b_ptr, READ_PTR(buffer_ptr), READ_SIZE(buffer_ptr));
|
61
|
+
if (buffer_ptr->b_ptr != buffer_ptr->buffer) {
|
62
|
+
xfree(buffer_ptr->b_ptr);
|
63
|
+
}
|
64
|
+
buffer_ptr->b_ptr = new_b_ptr;
|
65
|
+
buffer_ptr->size = new_size;
|
66
|
+
buffer_ptr->write_position -= buffer_ptr->read_position;
|
67
|
+
buffer_ptr->read_position = 0;
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
/**
|
72
|
+
* Free the memory for the byte buffer.
|
73
|
+
*/
|
74
|
+
void rb_bson_byte_buffer_free(void *ptr)
|
75
|
+
{
|
76
|
+
byte_buffer_t *b = ptr;
|
77
|
+
if (b->b_ptr != b->buffer) {
|
78
|
+
xfree(b->b_ptr);
|
79
|
+
}
|
80
|
+
xfree(b);
|
81
|
+
}
|
82
|
+
|
83
|
+
/**
|
84
|
+
* Get the length of the buffer.
|
85
|
+
*/
|
86
|
+
VALUE rb_bson_byte_buffer_length(VALUE self)
|
87
|
+
{
|
88
|
+
byte_buffer_t *b;
|
89
|
+
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
90
|
+
return UINT2NUM(READ_SIZE(b));
|
91
|
+
}
|
92
|
+
|
93
|
+
/* The docstring is in init.c. */
|
94
|
+
VALUE rb_bson_byte_buffer_read_position(VALUE self)
|
95
|
+
{
|
96
|
+
byte_buffer_t *b;
|
97
|
+
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
98
|
+
return INT2NUM(b->read_position);
|
99
|
+
}
|
100
|
+
|
101
|
+
/* The docstring is in init.c. */
|
102
|
+
VALUE rb_bson_byte_buffer_rewind(VALUE self)
|
103
|
+
{
|
104
|
+
byte_buffer_t *b;
|
105
|
+
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
106
|
+
b->read_position = 0;
|
107
|
+
|
108
|
+
return self;
|
109
|
+
}
|
110
|
+
|
111
|
+
/* The docstring is in init.c. */
|
112
|
+
VALUE rb_bson_byte_buffer_write_position(VALUE self)
|
113
|
+
{
|
114
|
+
byte_buffer_t *b;
|
115
|
+
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
116
|
+
return INT2NUM(b->write_position);
|
117
|
+
}
|
118
|
+
|
119
|
+
/* The docstring is in init.c. */
|
120
|
+
VALUE rb_bson_byte_buffer_to_s(VALUE self)
|
121
|
+
{
|
122
|
+
byte_buffer_t *b;
|
123
|
+
TypedData_Get_Struct(self, byte_buffer_t, &rb_byte_buffer_data_type, b);
|
124
|
+
return rb_str_new(READ_PTR(b), READ_SIZE(b));
|
125
|
+
}
|
126
|
+
|
127
|
+
/**
|
128
|
+
* Get the size of the byte_buffer_t in memory.
|
129
|
+
*/
|
130
|
+
size_t rb_bson_byte_buffer_memsize(const void *ptr)
|
131
|
+
{
|
132
|
+
return ptr ? sizeof(byte_buffer_t) : 0;
|
133
|
+
}
|
data/ext/bson/endian.c
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2015-2019 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 <string.h>
|
18
|
+
#include "bson-endian.h"
|
19
|
+
|
20
|
+
/*
|
21
|
+
*--------------------------------------------------------------------------
|
22
|
+
*
|
23
|
+
* __bson_uint16_swap_slow --
|
24
|
+
*
|
25
|
+
* Fallback endianness conversion for 16-bit integers.
|
26
|
+
*
|
27
|
+
* Returns:
|
28
|
+
* The endian swapped version.
|
29
|
+
*
|
30
|
+
* Side effects:
|
31
|
+
* None.
|
32
|
+
*
|
33
|
+
*--------------------------------------------------------------------------
|
34
|
+
*/
|
35
|
+
uint16_t __bson_uint16_swap_slow(uint16_t v)
|
36
|
+
{
|
37
|
+
return ((v & 0x00FF) << 8) |
|
38
|
+
((v & 0xFF00) >> 8);
|
39
|
+
}
|
40
|
+
|
41
|
+
/*
|
42
|
+
*--------------------------------------------------------------------------
|
43
|
+
*
|
44
|
+
* __bson_uint32_swap_slow --
|
45
|
+
*
|
46
|
+
* Fallback endianness conversion for 32-bit integers.
|
47
|
+
*
|
48
|
+
* Returns:
|
49
|
+
* The endian swapped version.
|
50
|
+
*
|
51
|
+
* Side effects:
|
52
|
+
* None.
|
53
|
+
*
|
54
|
+
*--------------------------------------------------------------------------
|
55
|
+
*/
|
56
|
+
uint32_t __bson_uint32_swap_slow(uint32_t v)
|
57
|
+
{
|
58
|
+
return ((v & 0x000000FFU) << 24) |
|
59
|
+
((v & 0x0000FF00U) << 8) |
|
60
|
+
((v & 0x00FF0000U) >> 8) |
|
61
|
+
((v & 0xFF000000U) >> 24);
|
62
|
+
}
|
63
|
+
|
64
|
+
|
65
|
+
/*
|
66
|
+
*--------------------------------------------------------------------------
|
67
|
+
*
|
68
|
+
* __bson_uint64_swap_slow --
|
69
|
+
*
|
70
|
+
* Fallback endianness conversion for 64-bit integers.
|
71
|
+
*
|
72
|
+
* Returns:
|
73
|
+
* The endian swapped version.
|
74
|
+
*
|
75
|
+
* Side effects:
|
76
|
+
* None.
|
77
|
+
*
|
78
|
+
*--------------------------------------------------------------------------
|
79
|
+
*/
|
80
|
+
uint64_t __bson_uint64_swap_slow(uint64_t v)
|
81
|
+
{
|
82
|
+
return ((v & 0x00000000000000FFULL) << 56) |
|
83
|
+
((v & 0x000000000000FF00ULL) << 40) |
|
84
|
+
((v & 0x0000000000FF0000ULL) << 24) |
|
85
|
+
((v & 0x00000000FF000000ULL) << 8) |
|
86
|
+
((v & 0x000000FF00000000ULL) >> 8) |
|
87
|
+
((v & 0x0000FF0000000000ULL) >> 24) |
|
88
|
+
((v & 0x00FF000000000000ULL) >> 40) |
|
89
|
+
((v & 0xFF00000000000000ULL) >> 56);
|
90
|
+
}
|
91
|
+
|
92
|
+
/*
|
93
|
+
*--------------------------------------------------------------------------
|
94
|
+
*
|
95
|
+
* __bson_double_swap_slow --
|
96
|
+
*
|
97
|
+
* Fallback endianness conversion for double floating point.
|
98
|
+
*
|
99
|
+
* Returns:
|
100
|
+
* The endian swapped version.
|
101
|
+
*
|
102
|
+
* Side effects:
|
103
|
+
* None.
|
104
|
+
*
|
105
|
+
*--------------------------------------------------------------------------
|
106
|
+
*/
|
107
|
+
double __bson_double_swap_slow(double v)
|
108
|
+
{
|
109
|
+
uint64_t uv;
|
110
|
+
|
111
|
+
memcpy(&uv, &v, sizeof(v));
|
112
|
+
uv = BSON_UINT64_SWAP_LE_BE(uv);
|
113
|
+
memcpy(&v, &uv, sizeof(v));
|
114
|
+
|
115
|
+
return v;
|
116
|
+
}
|