remove unused macro in common.h
[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 #ifdef NDEBUG
22 # define ossl_assert(x) ((x) != 0)
23 #else
24 __owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr,
25                                               const char *file, int line)
26 {
27     if (!expr)
28         OPENSSL_die(exprstr, file, line);
29
30     return expr;
31 }
32
33 # define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: "#x, \
34                                          __FILE__, __LINE__)
35
36 #endif
37
38 /* Check if |pre|, which must be a string literal, is a prefix of |str| */
39 #define HAS_PREFIX(str, pre) (strncmp(str, pre "", sizeof(pre) - 1) == 0)
40 /* As before, and if check succeeds, advance |str| past the prefix |pre| */
41 #define CHECK_AND_SKIP_PREFIX(str, pre) \
42     (HAS_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)
43 /* Check if the string literal |p| is a case-insensitive prefix of |s| */
44 #define HAS_CASE_PREFIX(s, p) (OPENSSL_strncasecmp(s, p "", sizeof(p) - 1) == 0)
45 /* As before, and if check succeeds, advance |str| past the prefix |pre| */
46 #define CHECK_AND_SKIP_CASE_PREFIX(str, pre) \
47     (HAS_CASE_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)
48 /* Check if the string literal |suffix| is a case-insensitive suffix of |str| */
49 #define HAS_CASE_SUFFIX(str, suffix) (strlen(str) < sizeof(suffix) - 1 ? 0 : \
50     OPENSSL_strcasecmp(str + strlen(str) - sizeof(suffix) + 1, suffix "") == 0)
51
52 /*
53  * Use this inside a union with the field that needs to be aligned to a
54  * reasonable boundary for the platform.  The most pessimistic alignment
55  * of the listed types will be used by the compiler.
56  */
57 # define OSSL_UNION_ALIGN       \
58     double align;               \
59     ossl_uintmax_t align_int;   \
60     void *align_ptr
61
62 # define OPENSSL_CONF             "openssl.cnf"
63
64 # ifndef OPENSSL_SYS_VMS
65 #  define X509_CERT_AREA          OPENSSLDIR
66 #  define X509_CERT_DIR           OPENSSLDIR "/certs"
67 #  define X509_CERT_FILE          OPENSSLDIR "/cert.pem"
68 #  define X509_PRIVATE_DIR        OPENSSLDIR "/private"
69 #  define CTLOG_FILE              OPENSSLDIR "/ct_log_list.cnf"
70 # else
71 #  define X509_CERT_AREA          "OSSL$DATAROOT:[000000]"
72 #  define X509_CERT_DIR           "OSSL$DATAROOT:[CERTS]"
73 #  define X509_CERT_FILE          "OSSL$DATAROOT:[000000]cert.pem"
74 #  define X509_PRIVATE_DIR        "OSSL$DATAROOT:[PRIVATE]"
75 #  define CTLOG_FILE              "OSSL$DATAROOT:[000000]ct_log_list.cnf"
76 # endif
77
78 #ifndef OPENSSL_NO_WINSTORE
79 # define X509_CERT_URI            "org.openssl.winstore://"
80 #else
81 # define X509_CERT_URI            ""
82 #endif
83
84 # define X509_CERT_URI_EVP        "SSL_CERT_URI"
85 # define X509_CERT_PATH_EVP       "SSL_CERT_PATH"
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