1 /** @file
2   Root include file of C runtime library to support building the third-party
3   cryptographic library.
4 
5 Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.<BR>
6 Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8 
9 **/
10 
11 #ifndef __CRT_LIB_SUPPORT_H__
12 #define __CRT_LIB_SUPPORT_H__
13 
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/PrintLib.h>
18 
19 #define OPENSSLDIR ""
20 #define ENGINESDIR ""
21 
22 #define MAX_STRING_SIZE  0x1000
23 
24 //
25 // We already have "no-ui" in out Configure invocation.
26 // but the code still fails to compile.
27 // Ref:  https://github.com/openssl/openssl/issues/8904
28 //
29 // This is defined in CRT library(stdio.h).
30 //
31 #ifndef BUFSIZ
32 #define BUFSIZ  8192
33 #endif
34 
35 //
36 // OpenSSL relies on explicit configuration for word size in crypto/bn,
37 // but we want it to be automatically inferred from the target. So we
38 // bypass what's in <openssl/opensslconf.h> for OPENSSL_SYS_UEFI, and
39 // define our own here.
40 //
41 #ifdef CONFIG_HEADER_BN_H
42 #error CONFIG_HEADER_BN_H already defined
43 #endif
44 
45 #define CONFIG_HEADER_BN_H
46 
47 #if defined(MDE_CPU_X64) || defined(MDE_CPU_AARCH64) || defined(MDE_CPU_IA64) || defined(MDE_CPU_RISCV64)
48 //
49 // With GCC we would normally use SIXTY_FOUR_BIT_LONG, but MSVC needs
50 // SIXTY_FOUR_BIT, because 'long' is 32-bit and only 'long long' is
51 // 64-bit. Since using 'long long' works fine on GCC too, just do that.
52 //
53 #define SIXTY_FOUR_BIT
54 #elif defined(MDE_CPU_IA32) || defined(MDE_CPU_ARM) || defined(MDE_CPU_EBC)
55 #define THIRTY_TWO_BIT
56 #else
57 #error Unknown target architecture
58 #endif
59 
60 //
61 // Map all va_xxxx elements to VA_xxx defined in MdePkg/Include/Base.h
62 //
63 #if !defined(__CC_ARM) // if va_list is not already defined
64 #define va_list   VA_LIST
65 #define va_arg    VA_ARG
66 #define va_start  VA_START
67 #define va_end    VA_END
68 #else // __CC_ARM
69 #define va_start(Marker, Parameter)   __va_start(Marker, Parameter)
70 #define va_arg(Marker, TYPE)          __va_arg(Marker, TYPE)
71 #define va_end(Marker)                ((void)0)
72 #endif
73 
74 //
75 // Definitions for global constants used by CRT library routines
76 //
77 #define EINVAL       22               /* Invalid argument */
78 #define EAFNOSUPPORT 47               /* Address family not supported by protocol family */
79 #define INT_MAX      0x7FFFFFFF       /* Maximum (signed) int value */
80 #define LONG_MAX     0X7FFFFFFFL      /* max value for a long */
81 #define LONG_MIN     (-LONG_MAX-1)    /* min value for a long */
82 #define ULONG_MAX    0xFFFFFFFF       /* Maximum unsigned long value */
83 #define CHAR_BIT     8                /* Number of bits in a char */
84 
85 //
86 // Address families.
87 //
88 #define AF_INET   2     /* internetwork: UDP, TCP, etc. */
89 #define AF_INET6  24    /* IP version 6 */
90 
91 //
92 // Define constants based on RFC0883, RFC1034, RFC 1035
93 //
94 #define NS_INT16SZ    2   /*%< #/bytes of data in a u_int16_t */
95 #define NS_INADDRSZ   4   /*%< IPv4 T_A */
96 #define NS_IN6ADDRSZ  16  /*%< IPv6 T_AAAA */
97 
98 //
99 // Basic types mapping
100 //
101 typedef UINTN          size_t;
102 typedef UINTN          u_int;
103 typedef INTN           ssize_t;
104 typedef INT32          time_t;
105 typedef UINT8          __uint8_t;
106 typedef UINT8          sa_family_t;
107 typedef UINT8          u_char;
108 typedef UINT32         uid_t;
109 typedef UINT32         gid_t;
110 
111 //
112 // File operations are not required for EFI building,
113 // so FILE is mapped to VOID * to pass build
114 //
115 typedef VOID  *FILE;
116 
117 //
118 // Structures Definitions
119 //
120 struct tm {
121   int   tm_sec;     /* seconds after the minute [0-60] */
122   int   tm_min;     /* minutes after the hour [0-59] */
123   int   tm_hour;    /* hours since midnight [0-23] */
124   int   tm_mday;    /* day of the month [1-31] */
125   int   tm_mon;     /* months since January [0-11] */
126   int   tm_year;    /* years since 1900 */
127   int   tm_wday;    /* days since Sunday [0-6] */
128   int   tm_yday;    /* days since January 1 [0-365] */
129   int   tm_isdst;   /* Daylight Savings Time flag */
130   long  tm_gmtoff;  /* offset from CUT in seconds */
131   char  *tm_zone;   /* timezone abbreviation */
132 };
133 
134 struct timeval {
135   long tv_sec;      /* time value, in seconds */
136   long tv_usec;     /* time value, in microseconds */
137 };
138 
139 struct sockaddr {
140   __uint8_t    sa_len;       /* total length */
141   sa_family_t  sa_family;    /* address family */
142   char         sa_data[14];  /* actually longer; address value */
143 };
144 
145 //
146 // Global variables
147 //
148 extern int  errno;
149 extern FILE *stderr;
150 
151 //
152 // Function prototypes of CRT Library routines
153 //
154 void           *malloc     (size_t);
155 void           *realloc    (void *, size_t);
156 void           free        (void *);
157 void           *memset     (void *, int, size_t);
158 int            memcmp      (const void *, const void *, size_t);
159 int            isdigit     (int);
160 int            isspace     (int);
161 int            isxdigit    (int);
162 int            isalnum     (int);
163 int            isupper     (int);
164 int            tolower     (int);
165 int            strcmp      (const char *, const char *);
166 int            strncasecmp (const char *, const char *, size_t);
167 char           *strchr     (const char *, int);
168 char           *strrchr    (const char *, int);
169 unsigned long  strtoul     (const char *, char **, int);
170 long           strtol      (const char *, char **, int);
171 char           *strerror   (int);
172 size_t         strspn      (const char *, const char *);
173 size_t         strcspn     (const char *, const char *);
174 int            printf      (const char *, ...);
175 int            sscanf      (const char *, const char *, ...);
176 FILE           *fopen      (const char *, const char *);
177 size_t         fread       (void *, size_t, size_t, FILE *);
178 size_t         fwrite      (const void *, size_t, size_t, FILE *);
179 int            fclose      (FILE *);
180 int            fprintf     (FILE *, const char *, ...);
181 time_t         time        (time_t *);
182 struct tm      *gmtime     (const time_t *);
183 uid_t          getuid      (void);
184 uid_t          geteuid     (void);
185 gid_t          getgid      (void);
186 gid_t          getegid     (void);
187 int            issetugid   (void);
188 void           qsort       (void *, size_t, size_t, int (*)(const void *, const void *));
189 char           *getenv     (const char *);
190 char           *secure_getenv (const char *);
191 #if defined(__GNUC__) && (__GNUC__ >= 2)
192 void           abort       (void) __attribute__((__noreturn__));
193 #else
194 void           abort       (void);
195 #endif
196 int            inet_pton   (int, const char *, void *);
197 
198 //
199 // Macros that directly map functions to BaseLib, BaseMemoryLib, and DebugLib functions
200 //
201 #define memcpy(dest,source,count)         CopyMem(dest,source,(UINTN)(count))
202 #define memset(dest,ch,count)             SetMem(dest,(UINTN)(count),(UINT8)(ch))
203 #define memchr(buf,ch,count)              ScanMem8(buf,(UINTN)(count),(UINT8)ch)
204 #define memcmp(buf1,buf2,count)           (int)(CompareMem(buf1,buf2,(UINTN)(count)))
205 #define memmove(dest,source,count)        CopyMem(dest,source,(UINTN)(count))
206 #define strlen(str)                       (size_t)(AsciiStrnLenS(str,MAX_STRING_SIZE))
207 #define strcpy(strDest,strSource)         AsciiStrCpyS(strDest,MAX_STRING_SIZE,strSource)
208 #define strncpy(strDest,strSource,count)  AsciiStrnCpyS(strDest,MAX_STRING_SIZE,strSource,(UINTN)count)
209 #define strcat(strDest,strSource)         AsciiStrCatS(strDest,MAX_STRING_SIZE,strSource)
210 #define strncmp(string1,string2,count)    (int)(AsciiStrnCmp(string1,string2,(UINTN)(count)))
211 #define strcasecmp(str1,str2)             (int)AsciiStriCmp(str1,str2)
212 #define sprintf(buf,...)                  AsciiSPrint(buf,MAX_STRING_SIZE,__VA_ARGS__)
213 #define localtime(timer)                  NULL
214 #define assert(expression)
215 #define offsetof(type,member)             OFFSET_OF(type,member)
216 #define atoi(nptr)                        AsciiStrDecimalToUintn(nptr)
217 #define gettimeofday(tvp,tz)              do { (tvp)->tv_sec = time(NULL); (tvp)->tv_usec = 0; } while (0)
218 
219 #endif
220