use '__builtin_expect' to improve EVP_EncryptUpdate performance for gcc/clang.
[openssl.git] / include / internal / common.h
1 /*
2  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #ifndef OSSL_INTERNAL_COMMON_H
11 # define OSSL_INTERNAL_COMMON_H
12 # pragma once
13
14 # include <stdlib.h>
15 # include <string.h>
16 # include "openssl/configuration.h"
17
18 # include "internal/e_os.h" /* ossl_inline in many files */
19 # include "internal/nelem.h"
20
21 #if defined(__GNUC__) || defined(__clang__)
22     #define likely(x) __builtin_expect(!!(x), 1)
23     #define unlikely(x) __builtin_expect(!!(x), 0)
24 #else
25     #define likely(x) x
26     #define unlikely(x) x
27 #endif
28
29 #ifdef NDEBUG
30 # define ossl_assert(x) ((x) != 0)
31 #else
32 __owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr,
33                                               const char *file, int line)
34 {
35     if (!expr)
36         OPENSSL_die(exprstr, file, line);
37
38     return expr;
39 }
40
41 # define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: "#x, \
42                                          __FILE__, __LINE__)
43
44 #endif
45
46 /* Check if |pre|, which must be a string literal, is a prefix of |str| */
47 #define HAS_PREFIX(str, pre) (strncmp(str, pre "", sizeof(pre) - 1) == 0)
48 /* As before, and if check succeeds, advance |str| past the prefix |pre| */
49 #define CHECK_AND_SKIP_PREFIX(str, pre) \
50     (HAS_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)
51 /* Check if the string literal |p| is a case-insensitive prefix of |s| */
52 #define HAS_CASE_PREFIX(s, p) (OPENSSL_strncasecmp(s, p "", sizeof(p) - 1) == 0)
53 /* As before, and if check succeeds, advance |str| past the prefix |pre| */
54 #define CHECK_AND_SKIP_CASE_PREFIX(str, pre) \
55     (HAS_CASE_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)
56 /* Check if the string literal |suffix| is a case-insensitive suffix of |str| */
57 #define HAS_CASE_SUFFIX(str, suffix) (strlen(str) < sizeof(suffix) - 1 ? 0 : \
58     OPENSSL_strcasecmp(str + strlen(str) - sizeof(suffix) + 1, suffix "") == 0)
59
60 /*
61  * Use this inside a union with the field that needs to be aligned to a
62  * reasonable boundary for the platform.  The most pessimistic alignment
63  * of the listed types will be used by the compiler.
64  */
65 # define OSSL_UNION_ALIGN       \
66     double align;               \
67     ossl_uintmax_t align_int;   \
68     void *align_ptr
69
70 # define OPENSSL_CONF             "openssl.cnf"
71
72 # ifndef OPENSSL_SYS_VMS
73 #  define X509_CERT_AREA          OPENSSLDIR
74 #  define X509_CERT_DIR           OPENSSLDIR "/certs"
75 #  define X509_CERT_FILE          OPENSSLDIR "/cert.pem"
76 #  define X509_PRIVATE_DIR        OPENSSLDIR "/private"
77 #  define CTLOG_FILE              OPENSSLDIR "/ct_log_list.cnf"
78 # else
79 #  define X509_CERT_AREA          "OSSL$DATAROOT:[000000]"
80 #  define X509_CERT_DIR           "OSSL$DATAROOT:[CERTS]"
81 #  define X509_CERT_FILE          "OSSL$DATAROOT:[000000]cert.pem"
82 #  define X509_PRIVATE_DIR        "OSSL$DATAROOT:[PRIVATE]"
83 #  define CTLOG_FILE              "OSSL$DATAROOT:[000000]ct_log_list.cnf"
84 # endif
85
86 # define X509_CERT_DIR_EVP        "SSL_CERT_DIR"
87 # define X509_CERT_FILE_EVP       "SSL_CERT_FILE"
88 # define CTLOG_FILE_EVP           "CTLOG_FILE"
89
90 /* size of string representations */
91 # define DECIMAL_SIZE(type)      ((sizeof(type)*8+2)/3+1)
92 # define HEX_SIZE(type)          (sizeof(type)*2)
93
94 # define c2l(c,l)        (l = ((unsigned long)(*((c)++)))     , \
95                          l|=(((unsigned long)(*((c)++)))<< 8), \
96                          l|=(((unsigned long)(*((c)++)))<<16), \
97                          l|=(((unsigned long)(*((c)++)))<<24))
98
99 /* NOTE - c is not incremented as per c2l */
100 # define c2ln(c,l1,l2,n) { \
101                         c+=n; \
102                         l1=l2=0; \
103                         switch (n) { \
104                         case 8: l2 =((unsigned long)(*(--(c))))<<24; \
105                         case 7: l2|=((unsigned long)(*(--(c))))<<16; \
106                         case 6: l2|=((unsigned long)(*(--(c))))<< 8; \
107                         case 5: l2|=((unsigned long)(*(--(c))));     \
108                         case 4: l1 =((unsigned long)(*(--(c))))<<24; \
109                         case 3: l1|=((unsigned long)(*(--(c))))<<16; \
110                         case 2: l1|=((unsigned long)(*(--(c))))<< 8; \
111                         case 1: l1|=((unsigned long)(*(--(c))));     \
112                                 } \
113                         }
114
115 # define l2c(l,c)        (*((c)++)=(unsigned char)(((l)    )&0xff), \
116                          *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
117                          *((c)++)=(unsigned char)(((l)>>16)&0xff), \
118                          *((c)++)=(unsigned char)(((l)>>24)&0xff))
119
120 # define n2l(c,l)        (l =((unsigned long)(*((c)++)))<<24, \
121                          l|=((unsigned long)(*((c)++)))<<16, \
122                          l|=((unsigned long)(*((c)++)))<< 8, \
123                          l|=((unsigned long)(*((c)++))))
124
125 # define n2l8(c,l)       (l =((uint64_t)(*((c)++)))<<56, \
126                          l|=((uint64_t)(*((c)++)))<<48, \
127                          l|=((uint64_t)(*((c)++)))<<40, \
128                          l|=((uint64_t)(*((c)++)))<<32, \
129                          l|=((uint64_t)(*((c)++)))<<24, \
130                          l|=((uint64_t)(*((c)++)))<<16, \
131                          l|=((uint64_t)(*((c)++)))<< 8, \
132                          l|=((uint64_t)(*((c)++))))
133
134 # define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
135                          *((c)++)=(unsigned char)(((l)>>16)&0xff), \
136                          *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
137                          *((c)++)=(unsigned char)(((l)    )&0xff))
138
139 # define l2n8(l,c)       (*((c)++)=(unsigned char)(((l)>>56)&0xff), \
140                          *((c)++)=(unsigned char)(((l)>>48)&0xff), \
141                          *((c)++)=(unsigned char)(((l)>>40)&0xff), \
142                          *((c)++)=(unsigned char)(((l)>>32)&0xff), \
143                          *((c)++)=(unsigned char)(((l)>>24)&0xff), \
144                          *((c)++)=(unsigned char)(((l)>>16)&0xff), \
145                          *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
146                          *((c)++)=(unsigned char)(((l)    )&0xff))
147
148 /* NOTE - c is not incremented as per l2c */
149 # define l2cn(l1,l2,c,n) { \
150                         c+=n; \
151                         switch (n) { \
152                         case 8: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
153                         case 7: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
154                         case 6: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
155                         case 5: *(--(c))=(unsigned char)(((l2)    )&0xff); \
156                         case 4: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
157                         case 3: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
158                         case 2: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
159                         case 1: *(--(c))=(unsigned char)(((l1)    )&0xff); \
160                                 } \
161                         }
162
163 # define n2s(c,s)        ((s=(((unsigned int)((c)[0]))<< 8)| \
164                              (((unsigned int)((c)[1]))    )),(c)+=2)
165 # define s2n(s,c)        (((c)[0]=(unsigned char)(((s)>> 8)&0xff), \
166                            (c)[1]=(unsigned char)(((s)    )&0xff)),(c)+=2)
167
168 # define n2l3(c,l)       ((l =(((unsigned long)((c)[0]))<<16)| \
169                               (((unsigned long)((c)[1]))<< 8)| \
170                               (((unsigned long)((c)[2]))    )),(c)+=3)
171
172 # define l2n3(l,c)       (((c)[0]=(unsigned char)(((l)>>16)&0xff), \
173                            (c)[1]=(unsigned char)(((l)>> 8)&0xff), \
174                            (c)[2]=(unsigned char)(((l)    )&0xff)),(c)+=3)
175
176 static ossl_inline int ossl_ends_with_dirsep(const char *path)
177 {
178     if (*path != '\0')
179         path += strlen(path) - 1;
180 # if defined __VMS
181     if (*path == ']' || *path == '>' || *path == ':')
182         return 1;
183 # elif defined _WIN32
184     if (*path == '\\')
185         return 1;
186 # endif
187     return *path == '/';
188 }
189
190 static ossl_inline int ossl_is_absolute_path(const char *path)
191 {
192 # if defined __VMS
193     if (strchr(path, ':') != NULL
194         || ((path[0] == '[' || path[0] == '<')
195             && path[1] != '.' && path[1] != '-'
196             && path[1] != ']' && path[1] != '>'))
197         return 1;
198 # elif defined _WIN32
199     if (path[0] == '\\'
200         || (path[0] != '\0' && path[1] == ':'))
201         return 1;
202 # endif
203     return path[0] == '/';
204 }
205
206 #endif