line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
#ifndef BSON_ENDIAN_H |
19
|
|
|
|
|
|
|
#define BSON_ENDIAN_H |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
#if !defined (BSON_INSIDE) && !defined (BSON_COMPILATION) |
23
|
|
|
|
|
|
|
# error "Only can be included directly." |
24
|
|
|
|
|
|
|
#endif |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
#if defined(__sun) |
28
|
|
|
|
|
|
|
# include |
29
|
|
|
|
|
|
|
#endif |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
#include "bson-config.h" |
32
|
|
|
|
|
|
|
#include "bson-macros.h" |
33
|
|
|
|
|
|
|
#include "bson-compat.h" |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
BSON_BEGIN_DECLS |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
#define BSON_BIG_ENDIAN 4321 |
40
|
|
|
|
|
|
|
#define BSON_LITTLE_ENDIAN 1234 |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
#if defined(__sun) |
44
|
|
|
|
|
|
|
# define BSON_UINT16_SWAP_LE_BE(v) BSWAP_16((uint16_t)v) |
45
|
|
|
|
|
|
|
# define BSON_UINT32_SWAP_LE_BE(v) BSWAP_32((uint32_t)v) |
46
|
|
|
|
|
|
|
# define BSON_UINT64_SWAP_LE_BE(v) BSWAP_64((uint64_t)v) |
47
|
|
|
|
|
|
|
#elif defined(__clang__) && defined(__clang_major__) && defined(__clang_minor__) && \ |
48
|
|
|
|
|
|
|
(__clang_major__ >= 3) && (__clang_minor__ >= 1) |
49
|
|
|
|
|
|
|
# if __has_builtin(__builtin_bswap16) |
50
|
|
|
|
|
|
|
# define BSON_UINT16_SWAP_LE_BE(v) __builtin_bswap16(v) |
51
|
|
|
|
|
|
|
# endif |
52
|
|
|
|
|
|
|
# if __has_builtin(__builtin_bswap32) |
53
|
|
|
|
|
|
|
# define BSON_UINT32_SWAP_LE_BE(v) __builtin_bswap32(v) |
54
|
|
|
|
|
|
|
# endif |
55
|
|
|
|
|
|
|
# if __has_builtin(__builtin_bswap64) |
56
|
|
|
|
|
|
|
# define BSON_UINT64_SWAP_LE_BE(v) __builtin_bswap64(v) |
57
|
|
|
|
|
|
|
# endif |
58
|
|
|
|
|
|
|
#elif defined(__GNUC__) && (__GNUC__ >= 4) |
59
|
|
|
|
|
|
|
# if __GNUC__ >= 4 && defined (__GNUC_MINOR__) && __GNUC_MINOR__ >= 3 |
60
|
|
|
|
|
|
|
# define BSON_UINT32_SWAP_LE_BE(v) __builtin_bswap32 ((uint32_t)v) |
61
|
|
|
|
|
|
|
# define BSON_UINT64_SWAP_LE_BE(v) __builtin_bswap64 ((uint64_t)v) |
62
|
|
|
|
|
|
|
# endif |
63
|
|
|
|
|
|
|
# if __GNUC__ >= 4 && defined (__GNUC_MINOR__) && __GNUC_MINOR__ >= 8 |
64
|
|
|
|
|
|
|
# define BSON_UINT16_SWAP_LE_BE(v) __builtin_bswap16 ((uint32_t)v) |
65
|
|
|
|
|
|
|
# endif |
66
|
|
|
|
|
|
|
#endif |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
#ifndef BSON_UINT16_SWAP_LE_BE |
70
|
|
|
|
|
|
|
# define BSON_UINT16_SWAP_LE_BE(v) __bson_uint16_swap_slow ((uint16_t)v) |
71
|
|
|
|
|
|
|
#endif |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
#ifndef BSON_UINT32_SWAP_LE_BE |
75
|
|
|
|
|
|
|
# define BSON_UINT32_SWAP_LE_BE(v) __bson_uint32_swap_slow ((uint32_t)v) |
76
|
|
|
|
|
|
|
#endif |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
#ifndef BSON_UINT64_SWAP_LE_BE |
80
|
|
|
|
|
|
|
# define BSON_UINT64_SWAP_LE_BE(v) __bson_uint64_swap_slow ((uint64_t)v) |
81
|
|
|
|
|
|
|
#endif |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
#if BSON_BYTE_ORDER == BSON_LITTLE_ENDIAN |
85
|
|
|
|
|
|
|
# define BSON_UINT16_FROM_LE(v) ((uint16_t)v) |
86
|
|
|
|
|
|
|
# define BSON_UINT16_TO_LE(v) ((uint16_t)v) |
87
|
|
|
|
|
|
|
# define BSON_UINT16_FROM_BE(v) BSON_UINT16_SWAP_LE_BE (v) |
88
|
|
|
|
|
|
|
# define BSON_UINT16_TO_BE(v) BSON_UINT16_SWAP_LE_BE (v) |
89
|
|
|
|
|
|
|
# define BSON_UINT32_FROM_LE(v) ((uint32_t)v) |
90
|
|
|
|
|
|
|
# define BSON_UINT32_TO_LE(v) ((uint32_t)v) |
91
|
|
|
|
|
|
|
# define BSON_UINT32_FROM_BE(v) BSON_UINT32_SWAP_LE_BE (v) |
92
|
|
|
|
|
|
|
# define BSON_UINT32_TO_BE(v) BSON_UINT32_SWAP_LE_BE (v) |
93
|
|
|
|
|
|
|
# define BSON_UINT64_FROM_LE(v) ((uint64_t)v) |
94
|
|
|
|
|
|
|
# define BSON_UINT64_TO_LE(v) ((uint64_t)v) |
95
|
|
|
|
|
|
|
# define BSON_UINT64_FROM_BE(v) BSON_UINT64_SWAP_LE_BE (v) |
96
|
|
|
|
|
|
|
# define BSON_UINT64_TO_BE(v) BSON_UINT64_SWAP_LE_BE (v) |
97
|
|
|
|
|
|
|
# define BSON_DOUBLE_FROM_LE(v) ((double)v) |
98
|
|
|
|
|
|
|
# define BSON_DOUBLE_TO_LE(v) ((double)v) |
99
|
|
|
|
|
|
|
#elif BSON_BYTE_ORDER == BSON_BIG_ENDIAN |
100
|
|
|
|
|
|
|
# define BSON_UINT16_FROM_LE(v) BSON_UINT16_SWAP_LE_BE (v) |
101
|
|
|
|
|
|
|
# define BSON_UINT16_TO_LE(v) BSON_UINT16_SWAP_LE_BE (v) |
102
|
|
|
|
|
|
|
# define BSON_UINT16_FROM_BE(v) ((uint16_t)v) |
103
|
|
|
|
|
|
|
# define BSON_UINT16_TO_BE(v) ((uint16_t)v) |
104
|
|
|
|
|
|
|
# define BSON_UINT32_FROM_LE(v) BSON_UINT32_SWAP_LE_BE (v) |
105
|
|
|
|
|
|
|
# define BSON_UINT32_TO_LE(v) BSON_UINT32_SWAP_LE_BE (v) |
106
|
|
|
|
|
|
|
# define BSON_UINT32_FROM_BE(v) ((uint32_t)v) |
107
|
|
|
|
|
|
|
# define BSON_UINT32_TO_BE(v) ((uint32_t)v) |
108
|
|
|
|
|
|
|
# define BSON_UINT64_FROM_LE(v) BSON_UINT64_SWAP_LE_BE (v) |
109
|
|
|
|
|
|
|
# define BSON_UINT64_TO_LE(v) BSON_UINT64_SWAP_LE_BE (v) |
110
|
|
|
|
|
|
|
# define BSON_UINT64_FROM_BE(v) ((uint64_t)v) |
111
|
|
|
|
|
|
|
# define BSON_UINT64_TO_BE(v) ((uint64_t)v) |
112
|
|
|
|
|
|
|
# define BSON_DOUBLE_FROM_LE(v) (__bson_double_swap_slow (v)) |
113
|
|
|
|
|
|
|
# define BSON_DOUBLE_TO_LE(v) (__bson_double_swap_slow (v)) |
114
|
|
|
|
|
|
|
#else |
115
|
|
|
|
|
|
|
# error "The endianness of target architecture is unknown." |
116
|
|
|
|
|
|
|
#endif |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
/* |
120
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
121
|
|
|
|
|
|
|
* |
122
|
|
|
|
|
|
|
* __bson_uint16_swap_slow -- |
123
|
|
|
|
|
|
|
* |
124
|
|
|
|
|
|
|
* Fallback endianness conversion for 16-bit integers. |
125
|
|
|
|
|
|
|
* |
126
|
|
|
|
|
|
|
* Returns: |
127
|
|
|
|
|
|
|
* The endian swapped version. |
128
|
|
|
|
|
|
|
* |
129
|
|
|
|
|
|
|
* Side effects: |
130
|
|
|
|
|
|
|
* None. |
131
|
|
|
|
|
|
|
* |
132
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
133
|
|
|
|
|
|
|
*/ |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
static BSON_INLINE uint16_t |
136
|
0
|
|
|
|
|
|
__bson_uint16_swap_slow (uint16_t v) /* IN */ |
137
|
|
|
|
|
|
|
{ |
138
|
0
|
|
|
|
|
|
return ((v & 0x00FF) << 8) | |
139
|
0
|
|
|
|
|
|
((v & 0xFF00) >> 8); |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
/* |
144
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
145
|
|
|
|
|
|
|
* |
146
|
|
|
|
|
|
|
* __bson_uint32_swap_slow -- |
147
|
|
|
|
|
|
|
* |
148
|
|
|
|
|
|
|
* Fallback endianness conversion for 32-bit integers. |
149
|
|
|
|
|
|
|
* |
150
|
|
|
|
|
|
|
* Returns: |
151
|
|
|
|
|
|
|
* The endian swapped version. |
152
|
|
|
|
|
|
|
* |
153
|
|
|
|
|
|
|
* Side effects: |
154
|
|
|
|
|
|
|
* None. |
155
|
|
|
|
|
|
|
* |
156
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
157
|
|
|
|
|
|
|
*/ |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
static BSON_INLINE uint32_t |
160
|
|
|
|
|
|
|
__bson_uint32_swap_slow (uint32_t v) /* IN */ |
161
|
|
|
|
|
|
|
{ |
162
|
|
|
|
|
|
|
return ((v & 0x000000FFU) << 24) | |
163
|
|
|
|
|
|
|
((v & 0x0000FF00U) << 8) | |
164
|
|
|
|
|
|
|
((v & 0x00FF0000U) >> 8) | |
165
|
|
|
|
|
|
|
((v & 0xFF000000U) >> 24); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
/* |
170
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
171
|
|
|
|
|
|
|
* |
172
|
|
|
|
|
|
|
* __bson_uint64_swap_slow -- |
173
|
|
|
|
|
|
|
* |
174
|
|
|
|
|
|
|
* Fallback endianness conversion for 64-bit integers. |
175
|
|
|
|
|
|
|
* |
176
|
|
|
|
|
|
|
* Returns: |
177
|
|
|
|
|
|
|
* The endian swapped version. |
178
|
|
|
|
|
|
|
* |
179
|
|
|
|
|
|
|
* Side effects: |
180
|
|
|
|
|
|
|
* None. |
181
|
|
|
|
|
|
|
* |
182
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
183
|
|
|
|
|
|
|
*/ |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
static BSON_INLINE uint64_t |
186
|
|
|
|
|
|
|
__bson_uint64_swap_slow (uint64_t v) /* IN */ |
187
|
|
|
|
|
|
|
{ |
188
|
|
|
|
|
|
|
return ((v & 0x00000000000000FFULL) << 56) | |
189
|
|
|
|
|
|
|
((v & 0x000000000000FF00ULL) << 40) | |
190
|
|
|
|
|
|
|
((v & 0x0000000000FF0000ULL) << 24) | |
191
|
|
|
|
|
|
|
((v & 0x00000000FF000000ULL) << 8) | |
192
|
|
|
|
|
|
|
((v & 0x000000FF00000000ULL) >> 8) | |
193
|
|
|
|
|
|
|
((v & 0x0000FF0000000000ULL) >> 24) | |
194
|
|
|
|
|
|
|
((v & 0x00FF000000000000ULL) >> 40) | |
195
|
|
|
|
|
|
|
((v & 0xFF00000000000000ULL) >> 56); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
/* |
200
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
201
|
|
|
|
|
|
|
* |
202
|
|
|
|
|
|
|
* __bson_double_swap_slow -- |
203
|
|
|
|
|
|
|
* |
204
|
|
|
|
|
|
|
* Fallback endianness conversion for double floating point. |
205
|
|
|
|
|
|
|
* |
206
|
|
|
|
|
|
|
* Returns: |
207
|
|
|
|
|
|
|
* The endian swapped version. |
208
|
|
|
|
|
|
|
* |
209
|
|
|
|
|
|
|
* Side effects: |
210
|
|
|
|
|
|
|
* None. |
211
|
|
|
|
|
|
|
* |
212
|
|
|
|
|
|
|
*-------------------------------------------------------------------------- |
213
|
|
|
|
|
|
|
*/ |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
BSON_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t)); |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
static BSON_INLINE double |
218
|
|
|
|
|
|
|
__bson_double_swap_slow (double v) /* IN */ |
219
|
|
|
|
|
|
|
{ |
220
|
|
|
|
|
|
|
uint64_t uv; |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
memcpy(&uv, &v, sizeof(v)); |
223
|
|
|
|
|
|
|
uv = BSON_UINT64_SWAP_LE_BE(uv); |
224
|
|
|
|
|
|
|
memcpy(&v, &uv, sizeof(v)); |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
return v; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
BSON_END_DECLS |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
#endif /* BSON_ENDIAN_H */ |