Path: blob/main/crypto/openssl/include/internal/common.h
34879 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 : \68OPENSSL_strcasecmp(str + strlen(str) - sizeof(suffix) + 1, suffix "") == 0)6970/*71* Use this inside a union with the field that needs to be aligned to a72* reasonable boundary for the platform. The most pessimistic alignment73* of the listed types will be used by the compiler.74*/75# define OSSL_UNION_ALIGN \76double align; \77ossl_uintmax_t align_int; \78void *align_ptr7980# define OPENSSL_CONF "openssl.cnf"8182# ifndef OPENSSL_SYS_VMS83# define X509_CERT_AREA OPENSSLDIR84# define X509_CERT_DIR OPENSSLDIR "/certs"85# define X509_CERT_FILE OPENSSLDIR "/cert.pem"86# define X509_PRIVATE_DIR OPENSSLDIR "/private"87# define CTLOG_FILE OPENSSLDIR "/ct_log_list.cnf"88# else89# define X509_CERT_AREA "OSSL$DATAROOT:[000000]"90# define X509_CERT_DIR "OSSL$DATAROOT:[CERTS]"91# define X509_CERT_FILE "OSSL$DATAROOT:[000000]cert.pem"92# define X509_PRIVATE_DIR "OSSL$DATAROOT:[PRIVATE]"93# define CTLOG_FILE "OSSL$DATAROOT:[000000]ct_log_list.cnf"94# endif9596# define X509_CERT_DIR_EVP "SSL_CERT_DIR"97# define X509_CERT_FILE_EVP "SSL_CERT_FILE"98# define CTLOG_FILE_EVP "CTLOG_FILE"99100/* size of string representations */101# define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1)102# define HEX_SIZE(type) (sizeof(type)*2)103104# define c2l(c,l) (l = ((unsigned long)(*((c)++))) , \105l|=(((unsigned long)(*((c)++)))<< 8), \106l|=(((unsigned long)(*((c)++)))<<16), \107l|=(((unsigned long)(*((c)++)))<<24))108109/* NOTE - c is not incremented as per c2l */110# define c2ln(c,l1,l2,n) { \111c+=n; \112l1=l2=0; \113switch (n) { \114case 8: l2 =((unsigned long)(*(--(c))))<<24; \115case 7: l2|=((unsigned long)(*(--(c))))<<16; \116case 6: l2|=((unsigned long)(*(--(c))))<< 8; \117case 5: l2|=((unsigned long)(*(--(c)))); \118case 4: l1 =((unsigned long)(*(--(c))))<<24; \119case 3: l1|=((unsigned long)(*(--(c))))<<16; \120case 2: l1|=((unsigned long)(*(--(c))))<< 8; \121case 1: l1|=((unsigned long)(*(--(c)))); \122} \123}124125# define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \126*((c)++)=(unsigned char)(((l)>> 8)&0xff), \127*((c)++)=(unsigned char)(((l)>>16)&0xff), \128*((c)++)=(unsigned char)(((l)>>24)&0xff))129130# define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24, \131l|=((unsigned long)(*((c)++)))<<16, \132l|=((unsigned long)(*((c)++)))<< 8, \133l|=((unsigned long)(*((c)++))))134135# define n2l8(c,l) (l =((uint64_t)(*((c)++)))<<56, \136l|=((uint64_t)(*((c)++)))<<48, \137l|=((uint64_t)(*((c)++)))<<40, \138l|=((uint64_t)(*((c)++)))<<32, \139l|=((uint64_t)(*((c)++)))<<24, \140l|=((uint64_t)(*((c)++)))<<16, \141l|=((uint64_t)(*((c)++)))<< 8, \142l|=((uint64_t)(*((c)++))))143144# define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \145*((c)++)=(unsigned char)(((l)>>16)&0xff), \146*((c)++)=(unsigned char)(((l)>> 8)&0xff), \147*((c)++)=(unsigned char)(((l) )&0xff))148149# define l2n8(l,c) (*((c)++)=(unsigned char)(((l)>>56)&0xff), \150*((c)++)=(unsigned char)(((l)>>48)&0xff), \151*((c)++)=(unsigned char)(((l)>>40)&0xff), \152*((c)++)=(unsigned char)(((l)>>32)&0xff), \153*((c)++)=(unsigned char)(((l)>>24)&0xff), \154*((c)++)=(unsigned char)(((l)>>16)&0xff), \155*((c)++)=(unsigned char)(((l)>> 8)&0xff), \156*((c)++)=(unsigned char)(((l) )&0xff))157158/* NOTE - c is not incremented as per l2c */159# define l2cn(l1,l2,c,n) { \160c+=n; \161switch (n) { \162case 8: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \163case 7: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \164case 6: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \165case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \166case 4: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \167case 3: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \168case 2: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \169case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \170} \171}172173# define n2s(c,s) ((s=(((unsigned int)((c)[0]))<< 8)| \174(((unsigned int)((c)[1])) )),(c)+=2)175# define s2n(s,c) (((c)[0]=(unsigned char)(((s)>> 8)&0xff), \176(c)[1]=(unsigned char)(((s) )&0xff)),(c)+=2)177178# define n2l3(c,l) ((l =(((unsigned long)((c)[0]))<<16)| \179(((unsigned long)((c)[1]))<< 8)| \180(((unsigned long)((c)[2])) )),(c)+=3)181182# define l2n3(l,c) (((c)[0]=(unsigned char)(((l)>>16)&0xff), \183(c)[1]=(unsigned char)(((l)>> 8)&0xff), \184(c)[2]=(unsigned char)(((l) )&0xff)),(c)+=3)185186static ossl_inline int ossl_ends_with_dirsep(const char *path)187{188if (*path != '\0')189path += strlen(path) - 1;190# if defined __VMS191if (*path == ']' || *path == '>' || *path == ':')192return 1;193# elif defined _WIN32194if (*path == '\\')195return 1;196# endif197return *path == '/';198}199200static ossl_inline char ossl_determine_dirsep(const char *path)201{202if (ossl_ends_with_dirsep(path))203return '\0';204205# if defined(_WIN32)206return '\\';207# elif defined(__VMS)208return ':';209# else210return '/';211# endif212}213214static ossl_inline int ossl_is_absolute_path(const char *path)215{216# if defined __VMS217if (strchr(path, ':') != NULL218|| ((path[0] == '[' || path[0] == '<')219&& path[1] != '.' && path[1] != '-'220&& path[1] != ']' && path[1] != '>'))221return 1;222# elif defined _WIN32223if (path[0] == '\\'224|| (path[0] != '\0' && path[1] == ':'))225return 1;226# endif227return path[0] == '/';228}229230const char *ossl_get_openssldir(void);231const char *ossl_get_enginesdir(void);232const char *ossl_get_modulesdir(void);233const char *ossl_get_wininstallcontext(void);234235#endif236237238