Move more general parts of internal/cryptlib.h to new internal/common.h
[openssl.git] / include / internal / common.h
1 /*
2  * Copyright 1995-2021 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 "../../e_os.h" /* To get strncasecmp() on Windows */
17
18 # include "internal/nelem.h"
19
20 #ifdef NDEBUG
21 # define ossl_assert(x) ((x) != 0)
22 #else
23 __owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr,
24                                               const char *file, int line)
25 {
26     if (!expr)
27         OPENSSL_die(exprstr, file, line);
28
29     return expr;
30 }
31
32 # define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: "#x, \
33                                          __FILE__, __LINE__)
34
35 #endif
36
37 /* Check if |pre|, which must be a string literal, is a prefix of |str| */
38 #define HAS_PREFIX(str, pre) (strncmp(str, pre "", sizeof(pre) - 1) == 0)
39 /* As before, and if check succeeds, advance |str| past the prefix |pre| */
40 #define CHECK_AND_SKIP_PREFIX(str, pre) \
41     (HAS_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)
42 /* Check if the string literal |p| is a case-insensitive prefix of |s| */
43 #define HAS_CASE_PREFIX(s, p) (strncasecmp(s, p "", sizeof(p) - 1) == 0)
44 /* As before, and if check succeeds, advance |str| past the prefix |pre| */
45 #define CHECK_AND_SKIP_CASE_PREFIX(str, pre) \
46     (HAS_CASE_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)
47 /* Check if the string literal |suffix| is a case-insensitive suffix of |str| */
48 #define HAS_CASE_SUFFIX(str, suffix) (strlen(str) < sizeof(suffix) - 1 ? 0 : \
49     strcasecmp(str + strlen(str) - sizeof(suffix) + 1, suffix "") == 0)
50
51 /*
52  * Use this inside a union with the field that needs to be aligned to a
53  * reasonable boundary for the platform.  The most pessimistic alignment
54  * of the listed types will be used by the compiler.
55  */
56 # define OSSL_UNION_ALIGN       \
57     double align;               \
58     ossl_uintmax_t align_int;   \
59     void *align_ptr
60
61 # define OPENSSL_CONF             "openssl.cnf"
62
63 # ifndef OPENSSL_SYS_VMS
64 #  define X509_CERT_AREA          OPENSSLDIR
65 #  define X509_CERT_DIR           OPENSSLDIR "/certs"
66 #  define X509_CERT_FILE          OPENSSLDIR "/cert.pem"
67 #  define X509_PRIVATE_DIR        OPENSSLDIR "/private"
68 #  define CTLOG_FILE              OPENSSLDIR "/ct_log_list.cnf"
69 # else
70 #  define X509_CERT_AREA          "OSSL$DATAROOT:[000000]"
71 #  define X509_CERT_DIR           "OSSL$DATAROOT:[CERTS]"
72 #  define X509_CERT_FILE          "OSSL$DATAROOT:[000000]cert.pem"
73 #  define X509_PRIVATE_DIR        "OSSL$DATAROOT:[PRIVATE]"
74 #  define CTLOG_FILE              "OSSL$DATAROOT:[000000]ct_log_list.cnf"
75 # endif
76
77 # define X509_CERT_DIR_EVP        "SSL_CERT_DIR"
78 # define X509_CERT_FILE_EVP       "SSL_CERT_FILE"
79 # define CTLOG_FILE_EVP           "CTLOG_FILE"
80
81 /* size of string representations */
82 # define DECIMAL_SIZE(type)      ((sizeof(type)*8+2)/3+1)
83 # define HEX_SIZE(type)          (sizeof(type)*2)
84
85 static ossl_inline int ossl_ends_with_dirsep(const char *path)
86 {
87     if (*path != '\0')
88         path += strlen(path) - 1;
89 # if defined __VMS
90     if (*path == ']' || *path == '>' || *path == ':')
91         return 1;
92 # elif defined _WIN32
93     if (*path == '\\')
94         return 1;
95 # endif
96     return *path == '/';
97 }
98
99 static ossl_inline int ossl_is_absolute_path(const char *path)
100 {
101 # if defined __VMS
102     if (strchr(path, ':') != NULL
103         || ((path[0] == '[' || path[0] == '<')
104             && path[1] != '.' && path[1] != '-'
105             && path[1] != ']' && path[1] != '>'))
106         return 1;
107 # elif defined _WIN32
108     if (path[0] == '\\'
109         || (path[0] != '\0' && path[1] == ':'))
110         return 1;
111 # endif
112     return path[0] == '/';
113 }
114
115 #endif