]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cc.h
f4438451348a8b8ac615a82c524bde6186f8446a
[apple/xnu.git] / EXTERNAL_HEADERS / corecrypto / cc.h
1 /*
2 * cc.h
3 * corecrypto
4 *
5 * Created by Michael Brouwer on 12/16/10.
6 * Copyright 2010,2011 Apple Inc. All rights reserved.
7 *
8 */
9
10 #ifndef _CORECRYPTO_CC_H_
11 #define _CORECRYPTO_CC_H_
12
13 #include <corecrypto/cc_config.h>
14 #include <string.h>
15 #include <stdint.h>
16
17 #if CC_KERNEL
18 #include <kern/assert.h>
19 #else
20 #include <assert.h>
21 #include <stdio.h>
22 #endif
23
24 /* Declare a struct element with a guarenteed alignment of _alignment_.
25 The resulting struct can be used to create arrays that are aligned by
26 a certain amount. */
27 #define cc_aligned_struct(_alignment_) \
28 typedef struct { \
29 uint8_t b[_alignment_]; \
30 } __attribute__((aligned(_alignment_)))
31
32 /* number of array elements used in a cc_ctx_decl */
33 #define cc_ctx_n(_type_, _size_) ((_size_ + sizeof(_type_) - 1) / sizeof(_type_))
34
35 /* sizeof of a context declared with cc_ctx_decl */
36 #define cc_ctx_sizeof(_type_, _size_) sizeof(_type_[cc_ctx_n(_type_, _size_)])
37
38 #define cc_ctx_decl(_type_, _size_, _name_) \
39 _type_ _name_[cc_ctx_n(_type_, _size_)]
40
41 #if CC_HAS_BZERO
42 #define cc_zero(_size_,_data_) bzero((_data_), (_size_))
43 #else
44 /* Alternate version if you don't have bzero. */
45 #define cc_zero(_size_,_data_) memset((_data_),0 ,(_size_))
46 #endif
47
48 #if CC_KERNEL
49 #define cc_printf(x...) printf(x)
50 #else
51 #define cc_printf(x...) fprintf(stderr, x)
52 #endif
53
54 #define cc_assert(x) assert(x)
55
56 #define cc_copy(_size_, _dst_, _src_) memcpy(_dst_, _src_, _size_)
57
58 CC_INLINE CC_NONNULL2 CC_NONNULL3 CC_NONNULL4
59 void cc_xor(size_t size, void *r, const void *s, const void *t) {
60 uint8_t *_r=(uint8_t *)r;
61 const uint8_t *_s=(uint8_t *)s;
62 const uint8_t *_t=(uint8_t *)t;
63 while (size--) {
64 _r[size] = _s[size] ^ _t[size];
65 }
66 }
67
68 /* Exchange S and T of any type. NOTE: Both and S and T are evaluated
69 mutliple times and MUST NOT be expressions. */
70 #define CC_SWAP(S,T) do { \
71 __typeof__(S) _cc_swap_tmp = S; S = T; T = _cc_swap_tmp; \
72 } while(0)
73
74 /* Return the maximum value between S and T. */
75 #define CC_MAX(S, T) ({__typeof__(S) _cc_max_s = S; __typeof__(T) _cc_max_t = T; _cc_max_s > _cc_max_t ? _cc_max_s : _cc_max_t;})
76
77 /* Return the minimum value between S and T. */
78 #define CC_MIN(S, T) ({__typeof__(S) _cc_min_s = S; __typeof__(T) _cc_min_t = T; _cc_min_s <= _cc_min_t ? _cc_min_s : _cc_min_t;})
79
80 #endif /* _CORECRYPTO_CC_H_ */