2530ff0412d6a789a696ab6638ff105a0b7b2c5f
[openssl.git] / include / internal / common.h
1 /*
2  * Copyright 1995-2023 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 ossl_likely(x)     __builtin_expect(!!(x), 1)
23 #  define ossl_unlikely(x)   __builtin_expect(!!(x), 0)
24 # else
25 #  define ossl_likely(x)     x
26 #  define ossl_unlikely(x)   x
27 # endif
28
29 # if defined(__GNUC__) || defined(__clang__)
30 #  define ALIGN32       __attribute((aligned(32)))
31 #  define ALIGN64       __attribute((aligned(64)))
32 # elif defined(_MSC_VER)
33 #  define ALIGN32       __declspec(align(32))
34 #  define ALIGN64       __declspec(align(64))
35 # else
36 #  define ALIGN32
37 #  define ALIGN64
38 # endif
39
40 # ifdef NDEBUG
41 #  define ossl_assert(x) ossl_likely((x) != 0)
42 # else
43 __owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr,
44                                               const char *file, int line)
45 {
46     if (!expr)
47         OPENSSL_die(exprstr, file, line);
48
49     return expr;
50 }
51
52 #  define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: "#x, \
53                                          __FILE__, __LINE__)
54
55 # endif
56
57 /* Check if |pre|, which must be a string literal, is a prefix of |str| */
58 #define HAS_PREFIX(str, pre) (strncmp(str, pre "", sizeof(pre) - 1) == 0)
59 /* As before, and if check succeeds, advance |str| past the prefix |pre| */
60 #define CHECK_AND_SKIP_PREFIX(str, pre) \
61     (HAS_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)
62 /* Check if the string literal |p| is a case-insensitive prefix of |s| */
63 #define HAS_CASE_PREFIX(s, p) (OPENSSL_strncasecmp(s, p "", sizeof(p) - 1) == 0)
64 /* As before, and if check succeeds, advance |str| past the prefix |pre| */
65 #define CHECK_AND_SKIP_CASE_PREFIX(str, pre) \
66     (HAS_CASE_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)
67 /* Check if the string literal |suffix| is a case-insensitive suffix of |str| */
68 #define HAS_CASE_SUFFIX(str, suffix) (strlen(str) < sizeof(suffix) - 1 ? 0 : \
69     OPENSSL_strcasecmp(str + strlen(str) - sizeof(suffix) + 1, suffix "") == 0)
70
71 /*
72  * Use this inside a union with the field that needs to be aligned to a
73  * reasonable boundary for the platform.  The most pessimistic alignment
74  * of the listed types will be used by the compiler.
75  */
76 # define OSSL_UNION_ALIGN       \
77     double align;               \
78     ossl_uintmax_t align_int;   \
79     void *align_ptr
80
81 # define OPENSSL_CONF             "openssl.cnf"
82
83 # ifndef OPENSSL_SYS_VMS
84 #  define X509_CERT_AREA          OPENSSLDIR
85 #  define X509_CERT_DIR           OPENSSLDIR "/certs"
86 #  define X509_CERT_FILE          OPENSSLDIR "/cert.pem"
87 #  define X509_PRIVATE_DIR        OPENSSLDIR "/private"
88 #  define CTLOG_FILE              OPENSSLDIR "/ct_log_list.cnf"
89 # else
90 #  define X509_CERT_AREA          "OSSL$DATAROOT:[000000]"
91 #  define X509_CERT_DIR           "OSSL$DATAROOT:[CERTS]"
92 #  define X509_CERT_FILE          "OSSL$DATAROOT:[000000]cert.pem"
93 #  define X509_PRIVATE_DIR        "OSSL$DATAROOT:[PRIVATE]"
94 #  define CTLOG_FILE              "OSSL$DATAROOT:[000000]ct_log_list.cnf"
95 # endif
96
97 # define X509_CERT_DIR_EVP        "SSL_CERT_DIR"
98 # define X509_CERT_FILE_EVP       "SSL_CERT_FILE"
99 # define CTLOG_FILE_EVP           "CTLOG_FILE"
100
101 /* size of string representations */
102 # define DECIMAL_SIZE(type)      ((sizeof(type)*8+2)/3+1)
103 # define HEX_SIZE(type)          (sizeof(type)*2)
104
105 # define c2l(c,l)        (l = ((unsigned long)(*((c)++)))     , \
106                          l|=(((unsigned long)(*((c)++)))<< 8), \
107                          l|=(((unsigned long)(*((c)++)))<<16), \
108                          l|=(((unsigned long)(*((c)++)))<<24))
109
110 /* NOTE - c is not incremented as per c2l */
111 # define c2ln(c,l1,l2,n) { \
112                         c+=n; \
113                         l1=l2=0; \
114                         switch (n) { \
115                         case 8: l2 =((unsigned long)(*(--(c))))<<24; \
116                         case 7: l2|=((unsigned long)(*(--(c))))<<16; \
117                         case 6: l2|=((unsigned long)(*(--(c))))<< 8; \
118                         case 5: l2|=((unsigned long)(*(--(c))));     \
119                         case 4: l1 =((unsigned long)(*(--(c))))<<24; \
120                         case 3: l1|=((unsigned long)(*(--(c))))<<16; \
121                         case 2: l1|=((unsigned long)(*(--(c))))<< 8; \
122                         case 1: l1|=((unsigned long)(*(--(c))));     \
123                                 } \
124                         }
125
126 # define l2c(l,c)        (*((c)++)=(unsigned char)(((l)    )&0xff), \
127                          *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
128                          *((c)++)=(unsigned char)(((l)>>16)&0xff), \
129                          *((c)++)=(unsigned char)(((l)>>24)&0xff))
130
131 # define n2l(c,l)        (l =((unsigned long)(*((c)++)))<<24, \
132                          l|=((unsigned long)(*((c)++)))<<16, \
133                          l|=((unsigned long)(*((c)++)))<< 8, \
134                          l|=((unsigned long)(*((c)++))))
135
136 # define n2l8(c,l)       (l =((uint64_t)(*((c)++)))<<56, \
137                          l|=((uint64_t)(*((c)++)))<<48, \
138                          l|=((uint64_t)(*((c)++)))<<40, \
139                          l|=((uint64_t)(*((c)++)))<<32, \
140                          l|=((uint64_t)(*((c)++)))<<24, \
141                          l|=((uint64_t)(*((c)++)))<<16, \
142                          l|=((uint64_t)(*((c)++)))<< 8, \
143                          l|=((uint64_t)(*((c)++))))
144
145 # define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
146                          *((c)++)=(unsigned char)(((l)>>16)&0xff), \
147                          *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
148                          *((c)++)=(unsigned char)(((l)    )&0xff))
149
150 # define l2n8(l,c)       (*((c)++)=(unsigned char)(((l)>>56)&0xff), \
151                          *((c)++)=(unsigned char)(((l)>>48)&0xff), \
152                          *((c)++)=(unsigned char)(((l)>>40)&0xff), \
153                          *((c)++)=(unsigned char)(((l)>>32)&0xff), \
154                          *((c)++)=(unsigned char)(((l)>>24)&0xff), \
155                          *((c)++)=(unsigned char)(((l)>>16)&0xff), \
156                          *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
157                          *((c)++)=(unsigned char)(((l)    )&0xff))
158
159 /* NOTE - c is not incremented as per l2c */
160 # define l2cn(l1,l2,c,n) { \
161                         c+=n; \
162                         switch (n) { \
163                         case 8: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
164                         case 7: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
165                         case 6: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
166                         case 5: *(--(c))=(unsigned char)(((l2)    )&0xff); \
167                         case 4: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
168                         case 3: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
169                         case 2: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
170                         case 1: *(--(c))=(unsigned char)(((l1)    )&0xff); \
171                                 } \
172                         }
173
174 # define n2s(c,s)        ((s=(((unsigned int)((c)[0]))<< 8)| \
175                              (((unsigned int)((c)[1]))    )),(c)+=2)
176 # define s2n(s,c)        (((c)[0]=(unsigned char)(((s)>> 8)&0xff), \
177                            (c)[1]=(unsigned char)(((s)    )&0xff)),(c)+=2)
178
179 # define n2l3(c,l)       ((l =(((unsigned long)((c)[0]))<<16)| \
180                               (((unsigned long)((c)[1]))<< 8)| \
181                               (((unsigned long)((c)[2]))    )),(c)+=3)
182
183 # define l2n3(l,c)       (((c)[0]=(unsigned char)(((l)>>16)&0xff), \
184                            (c)[1]=(unsigned char)(((l)>> 8)&0xff), \
185                            (c)[2]=(unsigned char)(((l)    )&0xff)),(c)+=3)
186
187 static ossl_inline int ossl_ends_with_dirsep(const char *path)
188 {
189     if (*path != '\0')
190         path += strlen(path) - 1;
191 # if defined __VMS
192     if (*path == ']' || *path == '>' || *path == ':')
193         return 1;
194 # elif defined _WIN32
195     if (*path == '\\')
196         return 1;
197 # endif
198     return *path == '/';
199 }
200
201 static ossl_inline char ossl_determine_dirsep(const char *path)
202 {
203     if (ossl_ends_with_dirsep(path))
204         return '\0';
205
206 # if defined(_WIN32)
207     return '\\';
208 # elif defined(__VMS)
209     return ':';
210 # else
211     return '/';
212 # endif
213 }
214
215 static ossl_inline int ossl_is_absolute_path(const char *path)
216 {
217 # if defined __VMS
218     if (strchr(path, ':') != NULL
219         || ((path[0] == '[' || path[0] == '<')
220             && path[1] != '.' && path[1] != '-'
221             && path[1] != ']' && path[1] != '>'))
222         return 1;
223 # elif defined _WIN32
224     if (path[0] == '\\'
225         || (path[0] != '\0' && path[1] == ':'))
226         return 1;
227 # endif
228     return path[0] == '/';
229 }
230
231 #endif