| 1 | /* Copyright (c) (2012,2015,2016,2017,2019) Apple Inc. All rights reserved. |
| 2 | * |
| 3 | * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which |
| 4 | * is contained in the License.txt file distributed with corecrypto) and only to |
| 5 | * people who accept that license. IMPORTANT: Any license rights granted to you by |
| 6 | * Apple Inc. (if any) are limited to internal use within your organization only on |
| 7 | * devices and computers you own or control, for the sole purpose of verifying the |
| 8 | * security characteristics and correct functioning of the Apple Software. You may |
| 9 | * not, directly or indirectly, redistribute the Apple Software or any portions thereof. |
| 10 | * |
| 11 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 12 | * |
| 13 | * This file contains Original Code and/or Modifications of Original Code |
| 14 | * as defined in and that are subject to the Apple Public Source License |
| 15 | * Version 2.0 (the 'License'). You may not use this file except in |
| 16 | * compliance with the License. The rights granted to you under the License |
| 17 | * may not be used to create, or enable the creation or redistribution of, |
| 18 | * unlawful or unlicensed copies of an Apple operating system, or to |
| 19 | * circumvent, violate, or enable the circumvention or violation of, any |
| 20 | * terms of an Apple operating system software license agreement. |
| 21 | * |
| 22 | * Please obtain a copy of the License at |
| 23 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 24 | * |
| 25 | * The Original Code and all software distributed under the License are |
| 26 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER |
| 27 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 28 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 29 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
| 30 | * Please see the License for the specific language governing rights and |
| 31 | * limitations under the License. |
| 32 | * |
| 33 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 34 | */ |
| 35 | |
| 36 | #ifndef _CORECRYPTO_CC_MACROS_H_ |
| 37 | #define _CORECRYPTO_CC_MACROS_H_ |
| 38 | |
| 39 | #include <corecrypto/cc_config.h> |
| 40 | |
| 41 | #ifndef __CC_DEBUG_ASSERT_COMPONENT_NAME_STRING |
| 42 | #define __CC_DEBUG_ASSERT_COMPONENT_NAME_STRING "" |
| 43 | #endif |
| 44 | |
| 45 | #ifndef __CC_DEBUG_ASSERT_PRODUCTION_CODE |
| 46 | #define __CC_DEBUG_ASSERT_PRODUCTION_CODE !CORECRYPTO_DEBUG |
| 47 | #endif |
| 48 | |
| 49 | #if CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS |
| 50 | |
| 51 | #if !CC_KERNEL |
| 52 | #include <string.h> // for strstr |
| 53 | #endif // !CC_KERNEL |
| 54 | |
| 55 | CC_UNUSED static char * |
| 56 | cc_strstr(const char *file) |
| 57 | { |
| 58 | #if CC_KERNEL |
| 59 | (void) file; |
| 60 | #else |
| 61 | const char cc_char[] = "corecrypto" ; |
| 62 | char *p = strstr(file, cc_char); |
| 63 | if (p) { |
| 64 | return p + strlen(cc_char) + 1; |
| 65 | } |
| 66 | #endif |
| 67 | return NULL; |
| 68 | } |
| 69 | |
| 70 | #define __CC_DEBUG_REQUIRE_MESSAGE(name, assertion, label, message, file, line, value) \ |
| 71 | {char *___t = cc_strstr(file); cc_printf( "require: %s, %s%s:%d\n", assertion, (message!=0) ? message : "", ___t==NULL?file:___t, line);} |
| 72 | |
| 73 | #endif // CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS |
| 74 | |
| 75 | #ifndef cc_require |
| 76 | #if (__CC_DEBUG_ASSERT_PRODUCTION_CODE) || (!CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS) |
| 77 | #if defined(_WIN32) && defined (__clang__) |
| 78 | #define cc_require(assertion, exceptionLabel) \ |
| 79 | do { \ |
| 80 | if (!(assertion) ) { \ |
| 81 | goto exceptionLabel; \ |
| 82 | } \ |
| 83 | } while ( 0 ) |
| 84 | #else |
| 85 | #define cc_require(assertion, exceptionLabel) \ |
| 86 | do { \ |
| 87 | if ( __builtin_expect(!(assertion), 0) ) { \ |
| 88 | goto exceptionLabel; \ |
| 89 | } \ |
| 90 | } while ( 0 ) |
| 91 | #endif |
| 92 | #else |
| 93 | #define cc_require(assertion, exceptionLabel) \ |
| 94 | do { \ |
| 95 | if ( __builtin_expect(!(assertion), 0) ) { \ |
| 96 | __CC_DEBUG_REQUIRE_MESSAGE(__CC_DEBUG_ASSERT_COMPONENT_NAME_STRING, \ |
| 97 | #assertion, #exceptionLabel, 0, __FILE__, __LINE__, 0); \ |
| 98 | goto exceptionLabel; \ |
| 99 | } \ |
| 100 | } while ( 0 ) |
| 101 | #endif |
| 102 | #endif |
| 103 | |
| 104 | #ifndef cc_require_action |
| 105 | #if __CC_DEBUG_ASSERT_PRODUCTION_CODE || (!CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS) |
| 106 | #if defined(_WIN32) && defined(__clang__) |
| 107 | #define cc_require_action(assertion, exceptionLabel, action) \ |
| 108 | do \ |
| 109 | { \ |
| 110 | if (!(assertion)) \ |
| 111 | { \ |
| 112 | { \ |
| 113 | action; \ |
| 114 | } \ |
| 115 | goto exceptionLabel; \ |
| 116 | } \ |
| 117 | } while ( 0 ) |
| 118 | #else |
| 119 | #define cc_require_action(assertion, exceptionLabel, action) \ |
| 120 | do \ |
| 121 | { \ |
| 122 | if ( __builtin_expect(!(assertion), 0) ) \ |
| 123 | { \ |
| 124 | { \ |
| 125 | action; \ |
| 126 | } \ |
| 127 | goto exceptionLabel; \ |
| 128 | } \ |
| 129 | } while ( 0 ) |
| 130 | #endif |
| 131 | #else |
| 132 | #define cc_require_action(assertion, exceptionLabel, action) \ |
| 133 | do \ |
| 134 | { \ |
| 135 | if ( __builtin_expect(!(assertion), 0) ) \ |
| 136 | { \ |
| 137 | __CC_DEBUG_REQUIRE_MESSAGE( \ |
| 138 | __CC_DEBUG_ASSERT_COMPONENT_NAME_STRING, \ |
| 139 | #assertion, #exceptionLabel, 0, __FILE__, __LINE__, 0); \ |
| 140 | { \ |
| 141 | action; \ |
| 142 | } \ |
| 143 | goto exceptionLabel; \ |
| 144 | } \ |
| 145 | } while ( 0 ) |
| 146 | #endif |
| 147 | #endif |
| 148 | |
| 149 | #ifndef cc_require_or_return |
| 150 | #if (__CC_DEBUG_ASSERT_PRODUCTION_CODE) || (!CORECRYPTO_DEBUG_ENABLE_CC_REQUIRE_PRINTS) |
| 151 | #if defined(_WIN32) && defined (__clang__) |
| 152 | #define cc_require_or_return(assertion, value) \ |
| 153 | do { \ |
| 154 | if (!(assertion) ) { \ |
| 155 | return value; \ |
| 156 | } \ |
| 157 | } while ( 0 ) |
| 158 | #else |
| 159 | #define cc_require_or_return(assertion, value) \ |
| 160 | do { \ |
| 161 | if ( __builtin_expect(!(assertion), 0) ) { \ |
| 162 | return value; \ |
| 163 | } \ |
| 164 | } while ( 0 ) |
| 165 | #endif |
| 166 | #else |
| 167 | #define cc_require_or_return(assertion, value) \ |
| 168 | do { \ |
| 169 | if ( __builtin_expect(!(assertion), 0) ) { \ |
| 170 | __CC_DEBUG_REQUIRE_MESSAGE(__CC_DEBUG_ASSERT_COMPONENT_NAME_STRING, \ |
| 171 | #assertion, #exceptionLabel, 0, __FILE__, __LINE__, 0); \ |
| 172 | return value; \ |
| 173 | } \ |
| 174 | } while ( 0 ) |
| 175 | #endif |
| 176 | #endif |
| 177 | |
| 178 | #endif /* _CORECRYPTO_CC_MACROS_H_ */ |
| 179 | |