common.h 9.0 KB

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