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