Path: blob/main/crypto/openssl/include/internal/common.h
103335 views
/*1* Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#ifndef OSSL_INTERNAL_COMMON_H10#define OSSL_INTERNAL_COMMON_H11#pragma once1213#include <stdlib.h>14#include <string.h>15#include "openssl/configuration.h"1617#include "internal/e_os.h" /* ossl_inline in many files */18#include "internal/nelem.h"1920#if defined(__GNUC__) || defined(__clang__)21#define ossl_likely(x) __builtin_expect(!!(x), 1)22#define ossl_unlikely(x) __builtin_expect(!!(x), 0)23#else24#define ossl_likely(x) (x)25#define ossl_unlikely(x) (x)26#endif2728#if defined(__GNUC__) || defined(__clang__)29#define ALIGN32 __attribute((aligned(32)))30#define ALIGN64 __attribute((aligned(64)))31#elif defined(_MSC_VER)32#define ALIGN32 __declspec(align(32))33#define ALIGN64 __declspec(align(64))34#else35#define ALIGN3236#define ALIGN6437#endif3839#ifdef NDEBUG40#define ossl_assert(x) ossl_likely((x) != 0)41#else42__owur static ossl_inline int ossl_assert_int(int expr, const char *exprstr,43const char *file, int line)44{45if (!expr)46OPENSSL_die(exprstr, file, line);4748return expr;49}5051#define ossl_assert(x) ossl_assert_int((x) != 0, "Assertion failed: " #x, \52__FILE__, __LINE__)5354#endif5556/* Check if |pre|, which must be a string literal, is a prefix of |str| */57#define HAS_PREFIX(str, pre) (strncmp(str, pre "", sizeof(pre) - 1) == 0)58/* As before, and if check succeeds, advance |str| past the prefix |pre| */59#define CHECK_AND_SKIP_PREFIX(str, pre) \60(HAS_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)61/* Check if the string literal |p| is a case-insensitive prefix of |s| */62#define HAS_CASE_PREFIX(s, p) (OPENSSL_strncasecmp(s, p "", sizeof(p) - 1) == 0)63/* As before, and if check succeeds, advance |str| past the prefix |pre| */64#define CHECK_AND_SKIP_CASE_PREFIX(str, pre) \65(HAS_CASE_PREFIX(str, pre) ? ((str) += sizeof(pre) - 1, 1) : 0)66/* Check if the string literal |suffix| is a case-insensitive suffix of |str| */67#define HAS_CASE_SUFFIX(str, suffix) (strlen(str) < sizeof(suffix) - 1 ? 0 : OPENSSL_strcasecmp(str + strlen(str) - sizeof(suffix) + 1, suffix "") == 0)6869/*70* Use this inside a union with the field that needs to be aligned to a71* reasonable boundary for the platform. The most pessimistic alignment72* of the listed types will be used by the compiler.73*/74#define OSSL_UNION_ALIGN \75double align; \76ossl_uintmax_t align_int; \77void *align_ptr7879#define OPENSSL_CONF "openssl.cnf"8081#ifndef OPENSSL_SYS_VMS82#define X509_CERT_AREA OPENSSLDIR83#define X509_CERT_DIR OPENSSLDIR "/certs"84#define X509_CERT_FILE OPENSSLDIR "/cert.pem"85#define X509_PRIVATE_DIR OPENSSLDIR "/private"86#define CTLOG_FILE OPENSSLDIR "/ct_log_list.cnf"87#else88#define X509_CERT_AREA "OSSL$DATAROOT:[000000]"89#define X509_CERT_DIR "OSSL$DATAROOT:[CERTS]"90#define X509_CERT_FILE "OSSL$DATAROOT:[000000]cert.pem"91#define X509_PRIVATE_DIR "OSSL$DATAROOT:[PRIVATE]"92#define CTLOG_FILE "OSSL$DATAROOT:[000000]ct_log_list.cnf"93#endif9495#define X509_CERT_DIR_EVP "SSL_CERT_DIR"96#define X509_CERT_FILE_EVP "SSL_CERT_FILE"97#define CTLOG_FILE_EVP "CTLOG_FILE"9899/* size of string representations */100#define DECIMAL_SIZE(type) ((sizeof(type) * 8 + 2) / 3 + 1)101#define HEX_SIZE(type) (sizeof(type) * 2)102103#define c2l(c, l) (l = ((unsigned long)(*((c)++))), \104l |= (((unsigned long)(*((c)++))) << 8), \105l |= (((unsigned long)(*((c)++))) << 16), \106l |= (((unsigned long)(*((c)++))) << 24))107108/* NOTE - c is not incremented as per c2l */109#define c2ln(c, l1, l2, n) \110{ \111c += n; \112l1 = l2 = 0; \113switch (n) { \114case 8: \115l2 = ((unsigned long)(*(--(c)))) << 24; \116case 7: \117l2 |= ((unsigned long)(*(--(c)))) << 16; \118case 6: \119l2 |= ((unsigned long)(*(--(c)))) << 8; \120case 5: \121l2 |= ((unsigned long)(*(--(c)))); \122case 4: \123l1 = ((unsigned long)(*(--(c)))) << 24; \124case 3: \125l1 |= ((unsigned long)(*(--(c)))) << 16; \126case 2: \127l1 |= ((unsigned long)(*(--(c)))) << 8; \128case 1: \129l1 |= ((unsigned long)(*(--(c)))); \130} \131}132133#define l2c(l, c) (*((c)++) = (unsigned char)(((l)) & 0xff), \134*((c)++) = (unsigned char)(((l) >> 8) & 0xff), \135*((c)++) = (unsigned char)(((l) >> 16) & 0xff), \136*((c)++) = (unsigned char)(((l) >> 24) & 0xff))137138#define n2l(c, l) (l = ((unsigned long)(*((c)++))) << 24, \139l |= ((unsigned long)(*((c)++))) << 16, \140l |= ((unsigned long)(*((c)++))) << 8, \141l |= ((unsigned long)(*((c)++))))142143#define n2l8(c, l) (l = ((uint64_t)(*((c)++))) << 56, \144l |= ((uint64_t)(*((c)++))) << 48, \145l |= ((uint64_t)(*((c)++))) << 40, \146l |= ((uint64_t)(*((c)++))) << 32, \147l |= ((uint64_t)(*((c)++))) << 24, \148l |= ((uint64_t)(*((c)++))) << 16, \149l |= ((uint64_t)(*((c)++))) << 8, \150l |= ((uint64_t)(*((c)++))))151152#define l2n(l, c) (*((c)++) = (unsigned char)(((l) >> 24) & 0xff), \153*((c)++) = (unsigned char)(((l) >> 16) & 0xff), \154*((c)++) = (unsigned char)(((l) >> 8) & 0xff), \155*((c)++) = (unsigned char)(((l)) & 0xff))156157#define l2n8(l, c) (*((c)++) = (unsigned char)(((l) >> 56) & 0xff), \158*((c)++) = (unsigned char)(((l) >> 48) & 0xff), \159*((c)++) = (unsigned char)(((l) >> 40) & 0xff), \160*((c)++) = (unsigned char)(((l) >> 32) & 0xff), \161*((c)++) = (unsigned char)(((l) >> 24) & 0xff), \162*((c)++) = (unsigned char)(((l) >> 16) & 0xff), \163*((c)++) = (unsigned char)(((l) >> 8) & 0xff), \164*((c)++) = (unsigned char)(((l)) & 0xff))165166/* NOTE - c is not incremented as per l2c */167#define l2cn(l1, l2, c, n) \168{ \169c += n; \170switch (n) { \171case 8: \172*(--(c)) = (unsigned char)(((l2) >> 24) & 0xff); \173case 7: \174*(--(c)) = (unsigned char)(((l2) >> 16) & 0xff); \175case 6: \176*(--(c)) = (unsigned char)(((l2) >> 8) & 0xff); \177case 5: \178*(--(c)) = (unsigned char)(((l2)) & 0xff); \179case 4: \180*(--(c)) = (unsigned char)(((l1) >> 24) & 0xff); \181case 3: \182*(--(c)) = (unsigned char)(((l1) >> 16) & 0xff); \183case 2: \184*(--(c)) = (unsigned char)(((l1) >> 8) & 0xff); \185case 1: \186*(--(c)) = (unsigned char)(((l1)) & 0xff); \187} \188}189190#define n2s(c, s) ((s = (((unsigned int)((c)[0])) << 8) | (((unsigned int)((c)[1])))), (c) += 2)191#define s2n(s, c) (((c)[0] = (unsigned char)(((s) >> 8) & 0xff), \192(c)[1] = (unsigned char)(((s)) & 0xff)), \193(c) += 2)194195#define n2l3(c, l) ((l = (((unsigned long)((c)[0])) << 16) | (((unsigned long)((c)[1])) << 8) | (((unsigned long)((c)[2])))), (c) += 3)196197#define l2n3(l, c) (((c)[0] = (unsigned char)(((l) >> 16) & 0xff), \198(c)[1] = (unsigned char)(((l) >> 8) & 0xff), \199(c)[2] = (unsigned char)(((l)) & 0xff)), \200(c) += 3)201202static ossl_inline int ossl_ends_with_dirsep(const char *path)203{204if (*path != '\0')205path += strlen(path) - 1;206#if defined __VMS207if (*path == ']' || *path == '>' || *path == ':')208return 1;209#elif defined _WIN32210if (*path == '\\')211return 1;212#endif213return *path == '/';214}215216static ossl_inline char ossl_determine_dirsep(const char *path)217{218if (ossl_ends_with_dirsep(path))219return '\0';220221#if defined(_WIN32)222return '\\';223#elif defined(__VMS)224return ':';225#else226return '/';227#endif228}229230static ossl_inline int ossl_is_absolute_path(const char *path)231{232#if defined __VMS233if (strchr(path, ':') != NULL234|| ((path[0] == '[' || path[0] == '<')235&& path[1] != '.' && path[1] != '-'236&& path[1] != ']' && path[1] != '>'))237return 1;238#elif defined _WIN32239if (path[0] == '\\'240|| (path[0] != '\0' && path[1] == ':'))241return 1;242#endif243return path[0] == '/';244}245246const char *ossl_get_openssldir(void);247const char *ossl_get_enginesdir(void);248const char *ossl_get_modulesdir(void);249const char *ossl_get_wininstallcontext(void);250251#endif252253254