webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
datatypes.h
Go to the documentation of this file.
1 /*
2  * datatypes.h
3  *
4  * data types for bit vectors and finite fields
5  *
6  * David A. McGrew
7  * Cisco Systems, Inc.
8  */
9 
10 /*
11  *
12  * Copyright (c) 2001-2006, Cisco Systems, Inc.
13  * All rights reserved.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  *
19  * Redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer.
21  *
22  * Redistributions in binary form must reproduce the above
23  * copyright notice, this list of conditions and the following
24  * disclaimer in the documentation and/or other materials provided
25  * with the distribution.
26  *
27  * Neither the name of the Cisco Systems, Inc. nor the names of its
28  * contributors may be used to endorse or promote products derived
29  * from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42  * OF THE POSSIBILITY OF SUCH DAMAGE.
43  *
44  */
45 
46 
47 #ifndef _DATATYPES_H
48 #define _DATATYPES_H
49 
50 #include "integers.h" /* definitions of uint32_t, et cetera */
51 #include "alloc.h"
52 
53 #include <stdarg.h>
54 
55 #include <stdio.h>
56 #include <string.h>
57 #include <time.h>
58 #ifdef HAVE_NETINET_IN_H
59 # include <netinet/in.h>
60 #elif defined HAVE_WINSOCK2_H
61 # include <winsock2.h>
62 #endif
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 
69 /* if DATATYPES_USE_MACROS is defined, then little functions are macros */
70 #define DATATYPES_USE_MACROS
71 
72 typedef union {
73  uint8_t v8[2];
75 } v16_t;
76 
77 typedef union {
78  uint8_t v8[4];
79  uint16_t v16[2];
81 } v32_t;
82 
83 typedef union {
84  uint8_t v8[8];
85  uint16_t v16[4];
86  uint32_t v32[2];
88 } v64_t;
89 
90 typedef union {
91  uint8_t v8[16];
92  uint16_t v16[8];
93  uint32_t v32[4];
94  uint64_t v64[2];
95 } v128_t;
96 
97 typedef union {
98  uint8_t v8[32];
99  uint16_t v16[16];
100  uint32_t v32[8];
101  uint64_t v64[4];
102 } v256_t;
103 
104 
105 /* some useful and simple math functions */
106 
107 #define pow_2(X) ( (unsigned int)1 << (X) ) /* 2^X */
108 
109 #define pow_minus_one(X) ( (X) ? -1 : 1 ) /* (-1)^X */
110 
111 
112 /*
113  * octet_get_weight(x) returns the hamming weight (number of bits equal to
114  * one) in the octet x
115  */
116 
117 int
119 
120 #define MAX_PRINT_STRING_LEN 1024
121 
122 char *
123 srtp_octet_string_hex_string(const void *str, int length);
124 
125 char *
127 
128 char *
130 
131 void
133 
134 void
135 v128_left_shift(v128_t *x, int shift_index);
136 
137 void
138 v128_right_shift(v128_t *x, int shift_index);
139 
140 /*
141  * the following macros define the data manipulation functions
142  *
143  * If DATATYPES_USE_MACROS is defined, then these macros are used
144  * directly (and function call overhead is avoided). Otherwise,
145  * the macros are used through the functions defined in datatypes.c
146  * (and the compiler provides better warnings).
147  */
148 
149 #define _v128_set_to_zero(x) \
150 ( \
151  (x)->v32[0] = 0, \
152  (x)->v32[1] = 0, \
153  (x)->v32[2] = 0, \
154  (x)->v32[3] = 0 \
155 )
156 
157 #define _v128_copy(x, y) \
158 ( \
159  (x)->v32[0] = (y)->v32[0], \
160  (x)->v32[1] = (y)->v32[1], \
161  (x)->v32[2] = (y)->v32[2], \
162  (x)->v32[3] = (y)->v32[3] \
163 )
164 
165 #define _v128_xor(z, x, y) \
166 ( \
167  (z)->v32[0] = (x)->v32[0] ^ (y)->v32[0], \
168  (z)->v32[1] = (x)->v32[1] ^ (y)->v32[1], \
169  (z)->v32[2] = (x)->v32[2] ^ (y)->v32[2], \
170  (z)->v32[3] = (x)->v32[3] ^ (y)->v32[3] \
171 )
172 
173 #define _v128_and(z, x, y) \
174 ( \
175  (z)->v32[0] = (x)->v32[0] & (y)->v32[0], \
176  (z)->v32[1] = (x)->v32[1] & (y)->v32[1], \
177  (z)->v32[2] = (x)->v32[2] & (y)->v32[2], \
178  (z)->v32[3] = (x)->v32[3] & (y)->v32[3] \
179 )
180 
181 #define _v128_or(z, x, y) \
182 ( \
183  (z)->v32[0] = (x)->v32[0] | (y)->v32[0], \
184  (z)->v32[1] = (x)->v32[1] | (y)->v32[1], \
185  (z)->v32[2] = (x)->v32[2] | (y)->v32[2], \
186  (z)->v32[3] = (x)->v32[3] | (y)->v32[3] \
187 )
188 
189 #define _v128_complement(x) \
190 ( \
191  (x)->v32[0] = ~(x)->v32[0], \
192  (x)->v32[1] = ~(x)->v32[1], \
193  (x)->v32[2] = ~(x)->v32[2], \
194  (x)->v32[3] = ~(x)->v32[3] \
195 )
196 
197 /* ok for NO_64BIT_MATH if it can compare uint64_t's (even as structures) */
198 #define _v128_is_eq(x, y) \
199  (((x)->v64[0] == (y)->v64[0]) && ((x)->v64[1] == (y)->v64[1]))
200 
201 
202 #ifdef NO_64BIT_MATH
203 #define _v128_xor_eq(z, x) \
204 ( \
205  (z)->v32[0] ^= (x)->v32[0], \
206  (z)->v32[1] ^= (x)->v32[1], \
207  (z)->v32[2] ^= (x)->v32[2], \
208  (z)->v32[3] ^= (x)->v32[3] \
209 )
210 #else
211 #define _v128_xor_eq(z, x) \
212 ( \
213  (z)->v64[0] ^= (x)->v64[0], \
214  (z)->v64[1] ^= (x)->v64[1] \
215 )
216 #endif
217 
218 /* NOTE! This assumes an odd ordering! */
219 /* This will not be compatible directly with math on some processors */
220 /* bit 0 is first 32-bit word, low order bit. in little-endian, that's
221  the first byte of the first 32-bit word. In big-endian, that's
222  the 3rd byte of the first 32-bit word */
223 /* The get/set bit code is used by the replay code ONLY, and it doesn't
224  really care which bit is which. AES does care which bit is which, but
225  doesn't use the 128-bit get/set or 128-bit shifts */
226 
227 #define _v128_get_bit(x, bit) \
228 ( \
229  ((((x)->v32[(bit) >> 5]) >> ((bit) & 31)) & 1) \
230 )
231 
232 #define _v128_set_bit(x, bit) \
233 ( \
234  (((x)->v32[(bit) >> 5]) |= ((uint32_t)1 << ((bit) & 31))) \
235 )
236 
237 #define _v128_clear_bit(x, bit) \
238 ( \
239  (((x)->v32[(bit) >> 5]) &= ~((uint32_t)1 << ((bit) & 31))) \
240 )
241 
242 #define _v128_set_bit_to(x, bit, value) \
243 ( \
244  (value) ? _v128_set_bit(x, bit) : \
245  _v128_clear_bit(x, bit) \
246 )
247 
248 
249 #if 0
250 /* nothing uses this */
251 #ifdef WORDS_BIGENDIAN
252 
253 #define _v128_add(z, x, y) { \
254  uint64_t tmp; \
255  \
256  tmp = x->v32[3] + y->v32[3]; \
257  z->v32[3] = (uint32_t) tmp; \
258  \
259  tmp = x->v32[2] + y->v32[2] + (tmp >> 32); \
260  z->v32[2] = (uint32_t) tmp; \
261  \
262  tmp = x->v32[1] + y->v32[1] + (tmp >> 32); \
263  z->v32[1] = (uint32_t) tmp; \
264  \
265  tmp = x->v32[0] + y->v32[0] + (tmp >> 32); \
266  z->v32[0] = (uint32_t) tmp; \
267 }
268 
269 #else /* assume little endian architecture */
270 
271 #define _v128_add(z, x, y) { \
272  uint64_t tmp; \
273  \
274  tmp = htonl(x->v32[3]) + htonl(y->v32[3]); \
275  z->v32[3] = ntohl((uint32_t) tmp); \
276  \
277  tmp = htonl(x->v32[2]) + htonl(y->v32[2]) \
278  + htonl(tmp >> 32); \
279  z->v32[2] = ntohl((uint32_t) tmp); \
280  \
281  tmp = htonl(x->v32[1]) + htonl(y->v32[1]) \
282  + htonl(tmp >> 32); \
283  z->v32[1] = ntohl((uint32_t) tmp); \
284  \
285  tmp = htonl(x->v32[0]) + htonl(y->v32[0]) \
286  + htonl(tmp >> 32); \
287  z->v32[0] = ntohl((uint32_t) tmp); \
288 }
289 #endif /* WORDS_BIGENDIAN */
290 #endif /* 0 */
291 
292 
293 #ifdef DATATYPES_USE_MACROS /* little functions are really macros */
294 
295 #define v128_set_to_zero(z) _v128_set_to_zero(z)
296 #define v128_copy(z, x) _v128_copy(z, x)
297 #define v128_xor(z, x, y) _v128_xor(z, x, y)
298 #define v128_and(z, x, y) _v128_and(z, x, y)
299 #define v128_or(z, x, y) _v128_or(z, x, y)
300 #define v128_complement(x) _v128_complement(x)
301 #define v128_is_eq(x, y) _v128_is_eq(x, y)
302 #define v128_xor_eq(x, y) _v128_xor_eq(x, y)
303 #define v128_get_bit(x, i) _v128_get_bit(x, i)
304 #define v128_set_bit(x, i) _v128_set_bit(x, i)
305 #define v128_clear_bit(x, i) _v128_clear_bit(x, i)
306 #define v128_set_bit_to(x, i, y) _v128_set_bit_to(x, i, y)
307 
308 #else
309 
310 void
312 
313 int
314 v128_is_eq(const v128_t *x, const v128_t *y);
315 
316 void
317 v128_copy(v128_t *x, const v128_t *y);
318 
319 void
321 
322 void
324 
325 void
326 v128_or(v128_t *z, v128_t *x, v128_t *y);
327 
328 void
330 
331 int
332 v128_get_bit(const v128_t *x, int i);
333 
334 void
335 v128_set_bit(v128_t *x, int i) ;
336 
337 void
338 v128_clear_bit(v128_t *x, int i);
339 
340 void
341 v128_set_bit_to(v128_t *x, int i, int y);
342 
343 #endif /* DATATYPES_USE_MACROS */
344 
345 /*
346  * octet_string_is_eq(a, b, len) returns 1 if the length len strings a
347  * and b are not equal. It returns 0 otherwise. The running time of the
348  * comparison depends only on len, making this safe to use for (e.g.)
349  * verifying authentication tags.
350  */
351 
352 int
354 
355 void
357 
358 
359 #if defined(HAVE_CONFIG_H)
360 
361 /*
362  * Convert big endian integers to CPU byte order.
363  */
364 #ifdef WORDS_BIGENDIAN
365 /* Nothing to do. */
366 # define be32_to_cpu(x) (x)
367 # define be64_to_cpu(x) (x)
368 #elif defined(HAVE_BYTESWAP_H)
369 /* We have (hopefully) optimized versions in byteswap.h */
370 # include <byteswap.h>
371 # define be32_to_cpu(x) bswap_32((x))
372 # define be64_to_cpu(x) bswap_64((x))
373 #else
374 
375 #if defined(__GNUC__) && defined(HAVE_X86)
376 /* Fall back. */
377 static inline uint32_t be32_to_cpu(uint32_t v) {
378  /* optimized for x86. */
379  asm("bswap %0" : "=r" (v) : "0" (v));
380  return v;
381 }
382 # else /* HAVE_X86 */
383 # ifdef HAVE_NETINET_IN_H
384 # include <netinet/in.h>
385 # elif defined HAVE_WINSOCK2_H
386 # include <winsock2.h>
387 # endif
388 # define be32_to_cpu(x) ntohl((x))
389 # endif /* HAVE_X86 */
390 
391 static inline uint64_t be64_to_cpu(uint64_t v) {
392 # ifdef NO_64BIT_MATH
393  /* use the make64 functions to do 64-bit math */
394  v = make64(htonl(low32(v)),htonl(high32(v)));
395 # else
396  /* use the native 64-bit math */
397  v= (uint64_t)((be32_to_cpu((uint32_t)(v >> 32))) | (((uint64_t)be32_to_cpu((uint32_t)v)) << 32));
398 # endif
399  return v;
400 }
401 
402 #endif
403 
404 #endif /* WORDS_BIGENDIAN */
405 
406 /*
407  * functions manipulating bitvector_t
408  *
409  * A bitvector_t consists of an array of words and an integer
410  * representing the number of significant bits stored in the array.
411  * The bits are packed as follows: the least significant bit is that
412  * of word[0], while the most significant bit is the nth most
413  * significant bit of word[m], where length = bits_per_word * m + n.
414  *
415  */
416 
417 #define bits_per_word 32
418 #define bytes_per_word 4
419 
420 typedef struct {
423 } bitvector_t;
424 
425 
426 #define _bitvector_get_bit(v, bit_index) \
427 ( \
428  ((((v)->word[((bit_index) >> 5)]) >> ((bit_index) & 31)) & 1) \
429 )
430 
431 
432 #define _bitvector_set_bit(v, bit_index) \
433 ( \
434  (((v)->word[((bit_index) >> 5)] |= ((uint32_t)1 << ((bit_index) & 31)))) \
435 )
436 
437 #define _bitvector_clear_bit(v, bit_index) \
438 ( \
439  (((v)->word[((bit_index) >> 5)] &= ~((uint32_t)1 << ((bit_index) & 31)))) \
440 )
441 
442 #define _bitvector_get_length(v) \
443 ( \
444  ((v)->length) \
445 )
446 
447 #ifdef DATATYPES_USE_MACROS /* little functions are really macros */
448 
449 #define bitvector_get_bit(v, bit_index) _bitvector_get_bit(v, bit_index)
450 #define bitvector_set_bit(v, bit_index) _bitvector_set_bit(v, bit_index)
451 #define bitvector_clear_bit(v, bit_index) _bitvector_clear_bit(v, bit_index)
452 #define bitvector_get_length(v) _bitvector_get_length(v)
453 
454 #else
455 
456 int
457 bitvector_get_bit(const bitvector_t *v, int bit_index);
458 
459 void
460 bitvector_set_bit(bitvector_t *v, int bit_index);
461 
462 void
463 bitvector_clear_bit(bitvector_t *v, int bit_index);
464 
465 unsigned long
467 
468 #endif
469 
470 int
471 bitvector_alloc(bitvector_t *v, unsigned long length);
472 
473 void
475 
476 void
478 
479 void
481 
482 char *
484 
485 #ifdef __cplusplus
486 }
487 #endif
488 
489 #endif /* _DATATYPES_H */
int bitvector_alloc(bitvector_t *v, unsigned long length)
Definition: datatypes.c:351
unsigned long long uint64_t
Definition: ptypes.h:120
uint16_t value
Definition: datatypes.h:74
#define bitvector_get_bit(v, bit_index)
Definition: datatypes.h:449
unsigned int uint32_t
Definition: ptypes.h:105
#define v128_is_eq(x, y)
Definition: datatypes.h:301
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: gl2ext.h:134
#define v128_copy(z, x)
Definition: datatypes.h:296
uint32_t length
Definition: datatypes.h:421
void v128_right_shift(v128_t *x, int shift_index)
Definition: datatypes.c:261
uint32_t * word
Definition: datatypes.h:422
Definition: datatypes.h:72
#define bitvector_clear_bit(v, bit_index)
Definition: datatypes.h:451
uint64_t value
Definition: datatypes.h:87
#define v128_complement(x)
Definition: datatypes.h:300
char * srtp_octet_string_hex_string(const void *str, int length)
Definition: datatypes.c:111
#define v128_or(z, x, y)
Definition: datatypes.h:299
Definition: datatypes.h:90
std::integral_constant< std::uint64_t, V > uint64_t
Definition: Brigand.h:445
#define bitvector_get_length(v)
Definition: datatypes.h:452
OPENSSL_EXPORT const ASN1_OBJECT int const unsigned char int len
Definition: x509.h:1053
EGLSurface EGLint x
Definition: eglext.h:950
#define v128_set_to_zero(z)
Definition: datatypes.h:295
GLuint index
Definition: gl2.h:383
Definition: datatypes.h:97
unsigned char uint8_t
Definition: ptypes.h:89
unsigned short uint16_t
Definition: ptypes.h:97
#define v128_set_bit(x, i)
Definition: datatypes.h:304
Definition: datatypes.h:83
Definition: datatypes.h:77
#define v128_xor(z, x, y)
Definition: datatypes.h:297
void v128_left_shift(v128_t *x, int shift_index)
Definition: datatypes.c:299
EGLSurface EGLint EGLint y
Definition: eglext.h:950
GLboolean GLboolean GLboolean GLboolean a
Definition: gl2ext.h:306
char * v128_bit_string(v128_t *x)
Definition: datatypes.c:144
const GLfloat * v
Definition: gl2.h:514
#define v128_get_bit(x, i)
Definition: datatypes.h:303
char * v128_hex_string(v128_t *x)
Definition: datatypes.c:131
void v128_copy_octet_string(v128_t *x, const uint8_t s[16])
Definition: datatypes.c:163
str
Definition: make-dist.py:305
for i
Definition: complexityMeasures.m:24
struct A s
uint32_t value
Definition: datatypes.h:80
GLfloat GLfloat GLfloat z
Definition: gl2.h:517
int octet_string_is_eq(uint8_t *a, uint8_t *b, int len)
Definition: datatypes.c:444
#define v128_clear_bit(x, i)
Definition: datatypes.h:305
#define v128_set_bit_to(x, i, y)
Definition: datatypes.h:306
GLboolean GLboolean GLboolean b
Definition: gl2ext.h:306
void bitvector_left_shift(bitvector_t *x, int index)
Definition: datatypes.c:416
int octet_get_weight(uint8_t octet)
Definition: datatypes.c:89
char * bitvector_bit_string(bitvector_t *x, char *buf, int len)
Definition: datatypes.c:395
#define v128_and(z, x, y)
Definition: datatypes.h:298
void bitvector_dealloc(bitvector_t *v)
Definition: datatypes.c:380
GLuint GLsizei GLsizei * length
Definition: gl2.h:435
#define bitvector_set_bit(v, bit_index)
Definition: datatypes.h:450
void bitvector_set_to_zero(bitvector_t *x)
Definition: datatypes.c:388
Definition: datatypes.h:420
void octet_string_set_to_zero(uint8_t *s, int len)
Definition: datatypes.c:461