File Coverage

bson/bson-oid.h
Criterion Covered Total %
statement 0 39 0.0
branch 0 4 0.0
condition n/a
subroutine n/a
pod n/a
total 0 43 0.0


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_OID_H
19             #define BSON_OID_H
20              
21              
22             #if !defined (BSON_INSIDE) && !defined (BSON_COMPILATION)
23             # error "Only can be included directly."
24             #endif
25              
26              
27             #include
28              
29             #include "bson-context.h"
30             #include "bson-macros.h"
31             #include "bson-types.h"
32             #include "bson-endian.h"
33              
34              
35             BSON_BEGIN_DECLS
36              
37              
38             int bson_oid_compare (const bson_oid_t *oid1,
39             const bson_oid_t *oid2);
40             void bson_oid_copy (const bson_oid_t *src,
41             bson_oid_t *dst);
42             bool bson_oid_equal (const bson_oid_t *oid1,
43             const bson_oid_t *oid2);
44             bool bson_oid_is_valid (const char *str,
45             size_t length);
46             time_t bson_oid_get_time_t (const bson_oid_t *oid);
47             uint32_t bson_oid_hash (const bson_oid_t *oid);
48             void bson_oid_init (bson_oid_t *oid,
49             bson_context_t *context);
50             void bson_oid_init_from_data (bson_oid_t *oid,
51             const uint8_t *data);
52             void bson_oid_init_from_string (bson_oid_t *oid,
53             const char *str);
54             void bson_oid_init_sequence (bson_oid_t *oid,
55             bson_context_t *context);
56             void bson_oid_to_string (const bson_oid_t *oid,
57             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 0           bson_oid_compare_unsafe (const bson_oid_t *oid1,
75             const bson_oid_t *oid2)
76             {
77 0           return memcmp (oid1, oid2, sizeof *oid1);
78             }
79              
80              
81             /**
82             * bson_oid_equal_unsafe:
83             * @oid1: A bson_oid_t.
84             * @oid2: A bson_oid_t.
85             *
86             * Checks the equality of @oid1 and @oid2.
87             *
88             * This function is meant to be as fast as possible and therefore performs
89             * no checks for argument validity. That is the callers responsibility.
90             *
91             * Returns: true if @oid1 and @oid2 are equal; otherwise false.
92             */
93             static BSON_INLINE bool
94 0           bson_oid_equal_unsafe (const bson_oid_t *oid1,
95             const bson_oid_t *oid2)
96             {
97 0           return !memcmp (oid1, oid2, sizeof *oid1);
98             }
99              
100             /**
101             * bson_oid_hash_unsafe:
102             * @oid: A bson_oid_t.
103             *
104             * This function performs a DJB style hash upon the bytes contained in @oid.
105             * The result is a hash key suitable for use in a hashtable.
106             *
107             * This function is meant to be as fast as possible and therefore performs no
108             * validation of arguments. The caller is responsible to ensure they are
109             * passing valid arguments.
110             *
111             * Returns: A uint32_t containing a hash code.
112             */
113             static BSON_INLINE uint32_t
114 0           bson_oid_hash_unsafe (const bson_oid_t *oid)
115             {
116 0           uint32_t hash = 5381;
117             uint32_t i;
118              
119 0 0         for (i = 0; i < sizeof oid->bytes; i++) {
120 0           hash = ((hash << 5) + hash) + oid->bytes[i];
121             }
122              
123 0           return hash;
124             }
125              
126              
127             /**
128             * bson_oid_copy_unsafe:
129             * @src: A bson_oid_t to copy from.
130             * @dst: A bson_oid_t to copy into.
131             *
132             * Copies the contents of @src into @dst. This function is meant to be as
133             * fast as possible and therefore performs no argument checking. It is the
134             * callers responsibility to ensure they are passing valid data into the
135             * function.
136             */
137             static BSON_INLINE void
138 0           bson_oid_copy_unsafe (const bson_oid_t *src,
139             bson_oid_t *dst)
140             {
141 0           memcpy (dst, src, sizeof *src);
142 0           }
143              
144              
145             /**
146             * bson_oid_parse_hex_char:
147             * @hex: A character to parse to its integer value.
148             *
149             * This function contains a jump table to return the integer value for a
150             * character containing a hexidecimal value (0-9, a-f, A-F). If the character
151             * is not a hexidecimal character then zero is returned.
152             *
153             * Returns: An integer between 0 and 15.
154             */
155             static BSON_INLINE uint8_t
156 0           bson_oid_parse_hex_char (char hex)
157             {
158 0           switch (hex) {
159             case '0':
160 0           return 0;
161             case '1':
162 0           return 1;
163             case '2':
164 0           return 2;
165             case '3':
166 0           return 3;
167             case '4':
168 0           return 4;
169             case '5':
170 0           return 5;
171             case '6':
172 0           return 6;
173             case '7':
174 0           return 7;
175             case '8':
176 0           return 8;
177             case '9':
178 0           return 9;
179             case 'a':
180             case 'A':
181 0           return 0xa;
182             case 'b':
183             case 'B':
184 0           return 0xb;
185             case 'c':
186             case 'C':
187 0           return 0xc;
188             case 'd':
189             case 'D':
190 0           return 0xd;
191             case 'e':
192             case 'E':
193 0           return 0xe;
194             case 'f':
195             case 'F':
196 0           return 0xf;
197             default:
198 0           return 0;
199             }
200             }
201              
202              
203             /**
204             * bson_oid_init_from_string_unsafe:
205             * @oid: A bson_oid_t to store the result.
206             * @str: A 24-character hexidecimal encoded string.
207             *
208             * Parses a string containing 24 hexidecimal encoded bytes into a bson_oid_t.
209             * This function is meant to be as fast as possible and inlined into your
210             * code. For that purpose, the function does not perform any sort of bounds
211             * checking and it is the callers responsibility to ensure they are passing
212             * valid input to the function.
213             */
214             static BSON_INLINE void
215 0           bson_oid_init_from_string_unsafe (bson_oid_t *oid,
216             const char *str)
217             {
218             int i;
219              
220 0 0         for (i = 0; i < 12; i++) {
221 0           oid->bytes[i] = ((bson_oid_parse_hex_char (str[2 * i]) << 4) |
222 0           (bson_oid_parse_hex_char (str[2 * i + 1])));
223             }
224 0           }
225              
226              
227             /**
228             * bson_oid_get_time_t_unsafe:
229             * @oid: A bson_oid_t.
230             *
231             * Fetches the time @oid was generated.
232             *
233             * Returns: A time_t containing the UNIX timestamp of generation.
234             */
235             static BSON_INLINE time_t
236 0           bson_oid_get_time_t_unsafe (const bson_oid_t *oid)
237             {
238             uint32_t t;
239              
240 0           memcpy (&t, oid, sizeof (t));
241 0           return BSON_UINT32_FROM_BE (t);
242             }
243              
244              
245             BSON_END_DECLS
246              
247              
248             #endif /* BSON_OID_H */