]>
git.proxmox.com Git - mirror_edk2.git/blob - RedfishPkg/PrivateInclude/Library/RedfishCrtLib.h
2 Redfish CRT wrapper functions.
4 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #ifndef REDFISH_CRT_LIB_H_
12 #define REDFISH_CRT_LIB_H_
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16 #include <Library/DebugLib.h>
17 #include <Library/PrintLib.h>
19 #define MAX_STRING_SIZE 0x10000000
21 // Minimum value for an object of type long long int.
22 #define LLONG_MIN MIN_INT64
24 // Maximum value for an object of type long long int.
25 #define LLONG_MAX MAX_INT64
27 // We dont support double on edk2
30 #if defined(MDE_CPU_X64) || defined(MDE_CPU_AARCH64) || defined(MDE_CPU_RISCV64)
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.
36 #define SIXTY_FOUR_BIT
37 #elif defined(MDE_CPU_IA32) || defined(MDE_CPU_ARM) || defined(MDE_CPU_EBC)
38 #define THIRTY_TWO_BIT
42 // Map all va_xxxx elements to VA_xxx defined in MdePkg/Include/Base.h
44 #if !defined(__CC_ARM) // if va_list is not already defined
45 #define va_list VA_LIST
47 #define va_start VA_START
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)
56 // Definitions for global constants used by CRT library routines
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 */
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
70 // Basic types mapping
75 typedef UINT8 __uint8_t
;
76 typedef UINT8 sa_family_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;
86 // File operations are not required for EFI building,
87 // so FILE is mapped to VOID * to pass build
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.
97 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
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.
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.
111 OUT CHAR8
*StartOfBuffer
,
113 IN CONST CHAR8
*FormatString
,
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.
123 @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated
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.
129 @return The number of ASCII characters in the produced output buffer not including the
135 RedfishAsciiVSPrint (
136 OUT CHAR8
*StartOfBuffer
,
138 IN CONST CHAR8
*FormatString
,
149 // Function prototypes of CRT Library routines
151 void *malloc (size_t);
152 void *realloc (void *, size_t);
153 void *calloc (size_t Num
, size_t Size
);
155 void *memset (void *, int, size_t);
156 int memcmp (const void *, const void *, size_t);
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 *);
197 int fprintf (FILE *, const char *, ...);
198 int fgetc (FILE * _File
);
200 uid_t
geteuid (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__
));
212 time_t time (time_t *);
215 // Macros that directly map functions to BaseLib, BaseMemoryLib, and DebugLib functions
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)
240 #define ERANGE 34 /* 34 Result too large */