]> git.proxmox.com Git - mirror_edk2.git/blob - RedfishPkg/PrivateInclude/Library/RedfishCrtLib.h
RedfishPkg/RedfishCrtLib: Redfish C runtime library
[mirror_edk2.git] / RedfishPkg / PrivateInclude / Library / RedfishCrtLib.h
1 /** @file
2 Redfish CRT wrapper functions.
3
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #ifndef REDFISH_CRT_LIB_H_
12 #define REDFISH_CRT_LIB_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 MAX_STRING_SIZE 0x10000000
20
21 // Minimum value for an object of type long long int.
22 #define LLONG_MIN MIN_INT64
23
24 // Maximum value for an object of type long long int.
25 #define LLONG_MAX MAX_INT64
26
27 // We dont support double on edk2
28 #define HUGE_VAL 0
29
30 #if defined(MDE_CPU_X64) || defined(MDE_CPU_AARCH64) || defined(MDE_CPU_RISCV64)
31 //
32 // With GCC we would normally use SIXTY_FOUR_BIT_LONG, but MSVC needs
33 // SIXTY_FOUR_BIT, because 'long' is 32-bit and only 'long long' is
34 // 64-bit. Since using 'long long' works fine on GCC too, just do that.
35 //
36 #define SIXTY_FOUR_BIT
37 #elif defined(MDE_CPU_IA32) || defined(MDE_CPU_ARM) || defined(MDE_CPU_EBC)
38 #define THIRTY_TWO_BIT
39 #endif
40
41 //
42 // Map all va_xxxx elements to VA_xxx defined in MdePkg/Include/Base.h
43 //
44 #if !defined(__CC_ARM) // if va_list is not already defined
45 #define va_list VA_LIST
46 #define va_arg VA_ARG
47 #define va_start VA_START
48 #define va_end VA_END
49 #else // __CC_ARM
50 #define va_start(Marker, Parameter) __va_start(Marker, Parameter)
51 #define va_arg(Marker, TYPE) __va_arg(Marker, TYPE)
52 #define va_end(Marker) ((void)0)
53 #endif
54
55 //
56 // Definitions for global constants used by CRT library routines
57 //
58 #define INT_MAX MAX_INT32 /* Maximum (signed) int value */
59 #define LONG_MAX 0X7FFFFFFFL /* max value for a long */
60 #define LONG_MIN (-LONG_MAX-1) /* min value for a long */
61 #define ULONG_MAX 0xFFFFFFFF /* Maximum unsigned long value */
62 #define CHAR_BIT 8 /* Number of bits in a char */
63
64 // Maximum value for an object of type unsigned long long int.
65 #define ULLONG_MAX 0xFFFFFFFFFFFFFFFFULL // 2^64 - 1
66 // Maximum value for an object of type unsigned char.
67 #define UCHAR_MAX 255 // 2^8 - 1
68
69 //
70 // Basic types mapping
71 //
72 typedef UINTN size_t;
73 typedef INTN ssize_t;
74 typedef INT32 time_t;
75 typedef UINT8 __uint8_t;
76 typedef UINT8 sa_family_t;
77 typedef UINT32 uid_t;
78 typedef UINT32 gid_t;
79 typedef INT32 int32_t;
80 typedef UINT32 uint32_t;
81 typedef UINT16 uint16_t;
82 typedef UINT8 uint8_t;
83 typedef enum {false, true} bool;
84
85 //
86 // File operations are not required for EFI building,
87 // so FILE is mapped to VOID * to pass build
88 //
89 typedef VOID *FILE;
90
91 /**
92 This is the Redfish version of CRT snprintf function, this function replaces "%s" to
93 "%a" before invoking AsciiSPrint(). That is becasue "%s" is unicode base on edk2
94 environment however "%s" is ascii code base on snprintf().
95 See definitions of AsciiSPrint() for the details.
96
97 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
98 ASCII string.
99 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
100 @param FormatString A Null-terminated ASCII format string.
101 @param ... Variable argument list whose contents are accessed based on the
102 format string specified by FormatString.
103
104 @return The number of ASCII characters in the produced output buffer not including the
105 Null-terminator. Zero means no string is produced or the error happens.
106
107 **/
108 UINTN
109 EFIAPI
110 RedfishAsciiSPrint (
111 OUT CHAR8 *StartOfBuffer,
112 IN UINTN BufferSize,
113 IN CONST CHAR8 *FormatString,
114 ...
115 );
116
117 /**
118 This is the Redfish version of CRT vsnprintf function, this function replaces "%s" to
119 "%a" before invoking AsciiVSPrint(). That is because "%s" is unicode base on edk2
120 environment however "%s" is ascii code base on vsnprintf().
121 See definitions of AsciiVSPrint() for the details.
122
123 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
124 ASCII string.
125 @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.
126 @param FormatString A Null-terminated ASCII format string.
127 @param Marker VA_LIST marker for the variable argument list.
128
129 @return The number of ASCII characters in the produced output buffer not including the
130 Null-terminator.
131
132 **/
133 UINTN
134 EFIAPI
135 RedfishAsciiVSPrint (
136 OUT CHAR8 *StartOfBuffer,
137 IN UINTN BufferSize,
138 IN CONST CHAR8 *FormatString,
139 IN VA_LIST Marker
140 );
141
142 //
143 // Global variables
144 //
145 extern int errno;
146 extern FILE *stderr;
147
148 //
149 // Function prototypes of CRT Library routines
150 //
151 void *malloc (size_t);
152 void *realloc (void *, size_t);
153 void *calloc (size_t Num, size_t Size);
154 void free (void *);
155 void *memset (void *, int, size_t);
156 int memcmp (const void *, const void *, size_t);
157 int isdigit (int);
158 int isspace (int);
159 int tolower (int);
160 int isupper (int);
161 int isxdigit (int);
162 int isalnum (int);
163 void *memcpy (void *, const void *, size_t);
164 void *memset (void *, int, size_t);
165 void *memchr (const void *, int, size_t);
166 int memcmp (const void *, const void *, size_t);
167 void *memmove (void *, const void *, size_t);
168 int strcmp (const char *, const char *);
169 int strncmp (const char *, const char *, size_t);
170 char *strcpy (char *, const char *);
171 size_t strlen (const char *);
172 char *strcat (char *, const char *);
173 char *strchr (const char *, int);
174 int strcasecmp (const char *, const char *);
175 int strncasecmp (const char *, const char *, size_t);
176 char *strncpy (char *, size_t, const char *, size_t);
177 int strncmp (const char *, const char *, size_t);
178 char *strrchr (const char *, int);
179 unsigned long strtoul (const char *, char **, int);
180 char * strstr (const char *s1 , const char *s2);
181 long strtol (const char *, char **, int);
182 char *strerror (int);
183 size_t strspn (const char *, const char *);
184 char * strdup (const char *str);
185 char * strpbrk (const char *s1, const char *s2);
186 unsigned long long strtoull(const char * nptr, char ** endptr, int base);
187 long long strtoll (const char * nptr, char ** endptr, int base);
188 long strtol (const char * nptr, char ** endptr, int base);
189 double strtod (const char * __restrict nptr, char ** __restrict endptr);
190 size_t strcspn (const char *, const char *);
191 int printf (const char *, ...);
192 int sscanf (const char *, const char *, ...);
193 FILE *fopen (const char *, const char *);
194 size_t fread (void *, size_t, size_t, FILE *);
195 size_t fwrite (const void *, size_t, size_t, FILE *);
196 int fclose (FILE *);
197 int fprintf (FILE *, const char *, ...);
198 int fgetc (FILE * _File);
199 uid_t getuid (void);
200 uid_t geteuid (void);
201 gid_t getgid (void);
202 gid_t getegid (void);
203 void qsort (void *, size_t, size_t, int (*)(const void *, const void *));
204 char *getenv (const char *);
205 #if defined(__GNUC__) && (__GNUC__ >= 2)
206 void abort (void) __attribute__((__noreturn__));
207 #else
208 void abort (void);
209 #endif
210 int toupper (int);
211 int Digit2Val (int);
212 time_t time (time_t *);
213
214 //
215 // Macros that directly map functions to BaseLib, BaseMemoryLib, and DebugLib functions
216 //
217 #define strcmp AsciiStrCmp
218 #define memcpy(dest,source,count) CopyMem(dest,source,(UINTN)(count))
219 #define memset(dest,ch,count) SetMem(dest,(UINTN)(count),(UINT8)(ch))
220 #define memchr(buf,ch,count) ScanMem8(buf,(UINTN)(count),(UINT8)ch)
221 #define memcmp(buf1,buf2,count) (int)(CompareMem(buf1,buf2,(UINTN)(count)))
222 #define memmove(dest,source,count) CopyMem(dest,source,(UINTN)(count))
223 #define strlen(str) (size_t)(AsciiStrnLenS(str,MAX_STRING_SIZE))
224 #define strcpy(strDest,strSource) AsciiStrCpyS(strDest,(strlen(strSource)+1),strSource)
225 #define strncpy(strDest,strSource,count) AsciiStrnCpyS(strDest,(UINTN)count,strSource,(UINTN)count)
226 #define strncpys(strDest, DestLen, strSource,count) AsciiStrnCpyS(strDest,DestLen,strSource,(UINTN)count)
227 #define strcat(strDest,strSource) AsciiStrCatS(strDest,(strlen(strSource)+strlen(strDest)+1),strSource)
228 #define strchr(str,ch) ScanMem8((VOID *)(str),AsciiStrSize(str),(UINT8)ch)
229 #define strcasecmp(str1,str2) (int)AsciiStriCmp(str1,str2)
230 #define strstr(s1,s2) AsciiStrStr(s1,s2)
231 #define snprintf(buf,len,...) RedfishAsciiSPrint(buf,len,__VA_ARGS__)
232 #define vsnprintf(buf,len,format,marker) RedfishAsciiVSPrint((buf),(len),(format),(marker))
233 #define assert(expression) ASSERT(expression)
234 #define offsetof(type,member) OFFSET_OF(type,member)
235
236 #define EOF (-1)
237
238 extern int errno;
239
240 #define ERANGE 34 /* 34 Result too large */
241
242 #endif