+/** @file\r
+ Redfish CRT wrapper functions.\r
+\r
+ Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
+ (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>\r
+\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#ifndef REDFISH_CRT_LIB_H_\r
+#define REDFISH_CRT_LIB_H_\r
+\r
+#include <Library/BaseLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/PrintLib.h>\r
+\r
+#define MAX_STRING_SIZE 0x10000000\r
+\r
+// Minimum value for an object of type long long int.\r
+#define LLONG_MIN MIN_INT64\r
+\r
+// Maximum value for an object of type long long int.\r
+#define LLONG_MAX MAX_INT64\r
+\r
+// We dont support double on edk2\r
+#define HUGE_VAL 0\r
+\r
+#if defined(MDE_CPU_X64) || defined(MDE_CPU_AARCH64) || defined(MDE_CPU_RISCV64)\r
+//\r
+// With GCC we would normally use SIXTY_FOUR_BIT_LONG, but MSVC needs\r
+// SIXTY_FOUR_BIT, because 'long' is 32-bit and only 'long long' is\r
+// 64-bit. Since using 'long long' works fine on GCC too, just do that.\r
+//\r
+#define SIXTY_FOUR_BIT\r
+#elif defined(MDE_CPU_IA32) || defined(MDE_CPU_ARM) || defined(MDE_CPU_EBC)\r
+#define THIRTY_TWO_BIT\r
+#endif\r
+\r
+//\r
+// Map all va_xxxx elements to VA_xxx defined in MdePkg/Include/Base.h\r
+//\r
+#if !defined(__CC_ARM) // if va_list is not already defined\r
+#define va_list VA_LIST\r
+#define va_arg VA_ARG\r
+#define va_start VA_START\r
+#define va_end VA_END\r
+#else // __CC_ARM\r
+#define va_start(Marker, Parameter) __va_start(Marker, Parameter)\r
+#define va_arg(Marker, TYPE) __va_arg(Marker, TYPE)\r
+#define va_end(Marker) ((void)0)\r
+#endif\r
+\r
+//\r
+// Definitions for global constants used by CRT library routines\r
+//\r
+#define INT_MAX MAX_INT32 /* Maximum (signed) int value */\r
+#define LONG_MAX 0X7FFFFFFFL /* max value for a long */\r
+#define LONG_MIN (-LONG_MAX-1) /* min value for a long */\r
+#define ULONG_MAX 0xFFFFFFFF /* Maximum unsigned long value */\r
+#define CHAR_BIT 8 /* Number of bits in a char */\r
+\r
+// Maximum value for an object of type unsigned long long int.\r
+#define ULLONG_MAX 0xFFFFFFFFFFFFFFFFULL // 2^64 - 1\r
+// Maximum value for an object of type unsigned char.\r
+#define UCHAR_MAX 255 // 2^8 - 1\r
+\r
+//\r
+// Basic types mapping\r
+//\r
+typedef UINTN size_t;\r
+typedef INTN ssize_t;\r
+typedef INT32 time_t;\r
+typedef UINT8 __uint8_t;\r
+typedef UINT8 sa_family_t;\r
+typedef UINT32 uid_t;\r
+typedef UINT32 gid_t;\r
+typedef INT32 int32_t;\r
+typedef UINT32 uint32_t;\r
+typedef UINT16 uint16_t;\r
+typedef UINT8 uint8_t;\r
+typedef enum {false, true} bool;\r
+\r
+//\r
+// File operations are not required for EFI building,\r
+// so FILE is mapped to VOID * to pass build\r
+//\r
+typedef VOID *FILE;\r
+\r
+/**\r
+ This is the Redfish version of CRT snprintf function, this function replaces "%s" to\r
+ "%a" before invoking AsciiSPrint(). That is becasue "%s" is unicode base on edk2\r
+ environment however "%s" is ascii code base on snprintf().\r
+ See definitions of AsciiSPrint() for the details.\r
+\r
+ @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
+ ASCII string.\r
+ @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
+ @param FormatString A Null-terminated ASCII format string.\r
+ @param ... Variable argument list whose contents are accessed based on the\r
+ format string specified by FormatString.\r
+\r
+ @return The number of ASCII characters in the produced output buffer not including the\r
+ Null-terminator. Zero means no string is produced or the error happens.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+RedfishAsciiSPrint (\r
+ OUT CHAR8 *StartOfBuffer,\r
+ IN UINTN BufferSize,\r
+ IN CONST CHAR8 *FormatString,\r
+ ...\r
+ );\r
+\r
+/**\r
+ This is the Redfish version of CRT vsnprintf function, this function replaces "%s" to\r
+ "%a" before invoking AsciiVSPrint(). That is because "%s" is unicode base on edk2\r
+ environment however "%s" is ascii code base on vsnprintf().\r
+ See definitions of AsciiVSPrint() for the details.\r
+\r
+ @param StartOfBuffer A pointer to the output buffer for the produced Null-terminated\r
+ ASCII string.\r
+ @param BufferSize The size, in bytes, of the output buffer specified by StartOfBuffer.\r
+ @param FormatString A Null-terminated ASCII format string.\r
+ @param Marker VA_LIST marker for the variable argument list.\r
+\r
+ @return The number of ASCII characters in the produced output buffer not including the\r
+ Null-terminator.\r
+\r
+**/\r
+UINTN\r
+EFIAPI\r
+RedfishAsciiVSPrint (\r
+ OUT CHAR8 *StartOfBuffer,\r
+ IN UINTN BufferSize,\r
+ IN CONST CHAR8 *FormatString,\r
+ IN VA_LIST Marker\r
+ );\r
+\r
+//\r
+// Global variables\r
+//\r
+extern int errno;\r
+extern FILE *stderr;\r
+\r
+//\r
+// Function prototypes of CRT Library routines\r
+//\r
+void *malloc (size_t);\r
+void *realloc (void *, size_t);\r
+void *calloc (size_t Num, size_t Size);\r
+void free (void *);\r
+void *memset (void *, int, size_t);\r
+int memcmp (const void *, const void *, size_t);\r
+int isdigit (int);\r
+int isspace (int);\r
+int tolower (int);\r
+int isupper (int);\r
+int isxdigit (int);\r
+int isalnum (int);\r
+void *memcpy (void *, const void *, size_t);\r
+void *memset (void *, int, size_t);\r
+void *memchr (const void *, int, size_t);\r
+int memcmp (const void *, const void *, size_t);\r
+void *memmove (void *, const void *, size_t);\r
+int strcmp (const char *, const char *);\r
+int strncmp (const char *, const char *, size_t);\r
+char *strcpy (char *, const char *);\r
+size_t strlen (const char *);\r
+char *strcat (char *, const char *);\r
+char *strchr (const char *, int);\r
+int strcasecmp (const char *, const char *);\r
+int strncasecmp (const char *, const char *, size_t);\r
+char *strncpy (char *, size_t, const char *, size_t);\r
+int strncmp (const char *, const char *, size_t);\r
+char *strrchr (const char *, int);\r
+unsigned long strtoul (const char *, char **, int);\r
+char * strstr (const char *s1 , const char *s2);\r
+long strtol (const char *, char **, int);\r
+char *strerror (int);\r
+size_t strspn (const char *, const char *);\r
+char * strdup (const char *str);\r
+char * strpbrk (const char *s1, const char *s2);\r
+unsigned long long strtoull(const char * nptr, char ** endptr, int base);\r
+long long strtoll (const char * nptr, char ** endptr, int base);\r
+long strtol (const char * nptr, char ** endptr, int base);\r
+double strtod (const char * __restrict nptr, char ** __restrict endptr);\r
+size_t strcspn (const char *, const char *);\r
+int printf (const char *, ...);\r
+int sscanf (const char *, const char *, ...);\r
+FILE *fopen (const char *, const char *);\r
+size_t fread (void *, size_t, size_t, FILE *);\r
+size_t fwrite (const void *, size_t, size_t, FILE *);\r
+int fclose (FILE *);\r
+int fprintf (FILE *, const char *, ...);\r
+int fgetc (FILE * _File);\r
+uid_t getuid (void);\r
+uid_t geteuid (void);\r
+gid_t getgid (void);\r
+gid_t getegid (void);\r
+void qsort (void *, size_t, size_t, int (*)(const void *, const void *));\r
+char *getenv (const char *);\r
+#if defined(__GNUC__) && (__GNUC__ >= 2)\r
+void abort (void) __attribute__((__noreturn__));\r
+#else\r
+void abort (void);\r
+#endif\r
+int toupper (int);\r
+int Digit2Val (int);\r
+time_t time (time_t *);\r
+\r
+//\r
+// Macros that directly map functions to BaseLib, BaseMemoryLib, and DebugLib functions\r
+//\r
+#define strcmp AsciiStrCmp\r
+#define memcpy(dest,source,count) CopyMem(dest,source,(UINTN)(count))\r
+#define memset(dest,ch,count) SetMem(dest,(UINTN)(count),(UINT8)(ch))\r
+#define memchr(buf,ch,count) ScanMem8(buf,(UINTN)(count),(UINT8)ch)\r
+#define memcmp(buf1,buf2,count) (int)(CompareMem(buf1,buf2,(UINTN)(count)))\r
+#define memmove(dest,source,count) CopyMem(dest,source,(UINTN)(count))\r
+#define strlen(str) (size_t)(AsciiStrnLenS(str,MAX_STRING_SIZE))\r
+#define strcpy(strDest,strSource) AsciiStrCpyS(strDest,(strlen(strSource)+1),strSource)\r
+#define strncpy(strDest,strSource,count) AsciiStrnCpyS(strDest,(UINTN)count,strSource,(UINTN)count)\r
+#define strncpys(strDest, DestLen, strSource,count) AsciiStrnCpyS(strDest,DestLen,strSource,(UINTN)count)\r
+#define strcat(strDest,strSource) AsciiStrCatS(strDest,(strlen(strSource)+strlen(strDest)+1),strSource)\r
+#define strchr(str,ch) ScanMem8((VOID *)(str),AsciiStrSize(str),(UINT8)ch)\r
+#define strcasecmp(str1,str2) (int)AsciiStriCmp(str1,str2)\r
+#define strstr(s1,s2) AsciiStrStr(s1,s2)\r
+#define snprintf(buf,len,...) RedfishAsciiSPrint(buf,len,__VA_ARGS__)\r
+#define vsnprintf(buf,len,format,marker) RedfishAsciiVSPrint((buf),(len),(format),(marker))\r
+#define assert(expression) ASSERT(expression)\r
+#define offsetof(type,member) OFFSET_OF(type,member)\r
+\r
+#define EOF (-1)\r
+\r
+extern int errno;\r
+\r
+#define ERANGE 34 /* 34 Result too large */\r
+\r
+#endif\r