--- /dev/null
+/** @file\r
+ Redfish CRT wrapper functions.\r
+\r
+ Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
+ (C) Copyright 2020 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
--- /dev/null
+/** @file\r
+ CRT wrapper functions for system call,the string operation functions\r
+ are remodeled after edk2-libc.\r
+\r
+ Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>\r
+\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+#include <Uefi.h>\r
+#include <Library/RedfishCrtLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/SortLib.h>\r
+#include <Library/UefiRuntimeServicesTableLib.h>\r
+\r
+int errno = 0;\r
+\r
+/**\r
+ Determine if a particular character is an alphanumeric character\r
+ @return Returns 1 if c is an alphanumeric character, otherwise returns 0.\r
+**/\r
+int isalnum (int c)\r
+{\r
+ //\r
+ // <alnum> ::= [0-9] | [a-z] | [A-Z]\r
+ //\r
+ return ((('0' <= (c)) && ((c) <= '9')) ||\r
+ (('a' <= (c)) && ((c) <= 'z')) ||\r
+ (('A' <= (c)) && ((c) <= 'Z')));\r
+}\r
+\r
+/**\r
+ Determine if a particular character is a digital character\r
+\r
+ @return Returns 1 if c is an digital character, otherwise returns 0.\r
+**/\r
+int isdchar (int c)\r
+{\r
+ //\r
+ // [0-9] | [e +-.]\r
+ //\r
+ return ((('0' <= (c)) && ((c) <= '9')) ||\r
+ (c == 'e') || (c == 'E') ||\r
+ (c == '+') || (c == '-') ||\r
+ (c == '.'));\r
+}\r
+\r
+/**\r
+ Determine if a particular character is a space character\r
+\r
+ @return Returns 1 if c is a space character\r
+**/\r
+int isspace (int c)\r
+{\r
+ //\r
+ // <space> ::= [ ]\r
+ //\r
+ return ((c) == ' ') || ((c) == '\t') || ((c) == '\r') || ((c) == '\n') || ((c) == '\v') || ((c) == '\f');\r
+}\r
+\r
+/**\r
+ Allocates memory blocks\r
+*/\r
+void *malloc (size_t size)\r
+{\r
+ return AllocatePool ((UINTN) size);\r
+}\r
+\r
+/**\r
+ De-allocates or frees a memory block\r
+*/\r
+void free (void *ptr)\r
+{\r
+ //\r
+ // In Standard C, free() handles a null pointer argument transparently. This\r
+ // is not true of FreePool() below, so protect it.\r
+ //\r
+ if (ptr != NULL) {\r
+ FreePool (ptr);\r
+ }\r
+}\r
+\r
+/**\r
+ NetBSD Compatibility Function strdup creates a duplicate copy of a string.\r
+\r
+ @return Returns the pointer to duplicated string.\r
+**/\r
+char * strdup(const char *str)\r
+{\r
+ size_t len;\r
+ char *copy;\r
+\r
+ len = strlen(str) + 1;\r
+ if ((copy = malloc(len)) == NULL)\r
+ return (NULL);\r
+ memcpy(copy, str, len);\r
+ return (copy);\r
+}\r
+\r
+/** The toupper function converts a lowercase letter to a corresponding\r
+ uppercase letter.\r
+\r
+ @param[in] c The character to be converted.\r
+\r
+ @return If the argument is a character for which islower is true and\r
+ there are one or more corresponding characters, as specified by\r
+ the current locale, for which isupper is true, the toupper\r
+ function returns one of the corresponding characters (always the\r
+ same one for any given locale); otherwise, the argument is\r
+ returned unchanged.\r
+**/\r
+int\r
+toupper(\r
+ IN int c\r
+ )\r
+{\r
+ if ( (c >= 'a') && (c <= 'z') ) {\r
+ c = c - ('a' - 'A');\r
+ }\r
+ return c;\r
+}\r
+\r
+/**\r
+ Digit to a value.\r
+\r
+ @return Returns the value of digit.\r
+**/\r
+int\r
+Digit2Val( int c)\r
+{\r
+ if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))) { /* If c is one of [A-Za-z]... */\r
+ c = toupper(c) - 7; // Adjust so 'A' is ('9' + 1)\r
+ }\r
+ return c - '0'; // Value returned is between 0 and 35, inclusive.\r
+}\r
+\r
+\r
+/** The strtoll function converts the initial portion of the string pointed to\r
+ by nptr to long long int representation.\r
+\r
+ See the description for strtol for more information.\r
+\r
+ @return The strtoll function returns the converted value, if any. If no\r
+ conversion could be performed, zero is returned. If the correct\r
+ value is outside the range of representable values, LLONG_MIN or\r
+ LLONG_MAX is returned (according to the sign of the value, if any),\r
+ and the value of the macro ERANGE is stored in errno.\r
+**/\r
+long long\r
+strtoll(const char * nptr, char ** endptr, int base)\r
+{\r
+ const char *pEnd;\r
+ long long Result = 0;\r
+ long long Previous;\r
+ int temp;\r
+ BOOLEAN Negative = FALSE;\r
+\r
+ pEnd = nptr;\r
+\r
+ if((base < 0) || (base == 1) || (base > 36)) {\r
+ if(endptr != NULL) {\r
+ *endptr = NULL;\r
+ }\r
+ return 0;\r
+ }\r
+ // Skip leading spaces.\r
+ while(isspace(*nptr)) ++nptr;\r
+\r
+ // Process Subject sequence: optional sign followed by digits.\r
+ if(*nptr == '+') {\r
+ Negative = FALSE;\r
+ ++nptr;\r
+ }\r
+ else if(*nptr == '-') {\r
+ Negative = TRUE;\r
+ ++nptr;\r
+ }\r
+\r
+ if(*nptr == '0') { /* Might be Octal or Hex */\r
+ if(toupper(nptr[1]) == 'X') { /* Looks like Hex */\r
+ if((base == 0) || (base == 16)) {\r
+ nptr += 2; /* Skip the "0X" */\r
+ base = 16; /* In case base was 0 */\r
+ }\r
+ }\r
+ else { /* Looks like Octal */\r
+ if((base == 0) || (base == 8)) {\r
+ ++nptr; /* Skip the leading "0" */\r
+ base = 8; /* In case base was 0 */\r
+ }\r
+ }\r
+ }\r
+ if(base == 0) { /* If still zero then must be decimal */\r
+ base = 10;\r
+ }\r
+ if(*nptr == '0') {\r
+ for( ; *nptr == '0'; ++nptr); /* Skip any remaining leading zeros */\r
+ pEnd = nptr;\r
+ }\r
+\r
+ while( isalnum(*nptr) && ((temp = Digit2Val(*nptr)) < base)) {\r
+ Previous = Result;\r
+ Result = MultS64x64 (Result, base) + (long long int)temp;\r
+ if( Result <= Previous) { // Detect Overflow\r
+ if(Negative) {\r
+ Result = LLONG_MIN;\r
+ }\r
+ else {\r
+ Result = LLONG_MAX;\r
+ }\r
+ Negative = FALSE;\r
+ errno = ERANGE;\r
+ break;\r
+ }\r
+ pEnd = ++nptr;\r
+ }\r
+ if(Negative) {\r
+ Result = -Result;\r
+ }\r
+\r
+ // Save pointer to final sequence\r
+ if(endptr != NULL) {\r
+ *endptr = (char *)pEnd;\r
+ }\r
+ return Result;\r
+}\r
+\r
+/** The strtol, strtoll, strtoul, and strtoull functions convert the initial\r
+ portion of the string pointed to by nptr to long int, long long int,\r
+ unsigned long int, and unsigned long long int representation, respectively.\r
+ First, they decompose the input string into three parts: an initial,\r
+ possibly empty, sequence of white-space characters (as specified by the\r
+ isspace function), a subject sequence resembling an integer represented in\r
+ some radix determined by the value of base, and a final string of one or\r
+ more unrecognized characters, including the terminating null character of\r
+ the input string. Then, they attempt to convert the subject sequence to an\r
+ integer, and return the result.\r
+\r
+ If the value of base is zero, the expected form of the subject sequence is\r
+ that of an integer constant, optionally preceded\r
+ by a plus or minus sign, but not including an integer suffix. If the value\r
+ of base is between 2 and 36 (inclusive), the expected form of the subject\r
+ sequence is a sequence of letters and digits representing an integer with\r
+ the radix specified by base, optionally preceded by a plus or minus sign,\r
+ but not including an integer suffix. The letters from a (or A) through z\r
+ (or Z) are ascribed the values 10 through 35; only letters and digits whose\r
+ ascribed values are less than that of base are permitted. If the value of\r
+ base is 16, the characters 0x or 0X may optionally precede the sequence of\r
+ letters and digits, following the sign if present.\r
+\r
+ The subject sequence is defined as the longest initial subsequence of the\r
+ input string, starting with the first non-white-space character, that is of\r
+ the expected form. The subject sequence contains no characters if the input\r
+ string is empty or consists entirely of white space, or if the first\r
+ non-white-space character is other than a sign or a permissible letter or digit.\r
+\r
+ If the subject sequence has the expected form and the value of base is\r
+ zero, the sequence of characters starting with the first digit is\r
+ interpreted as an integer constant. If the subject sequence has the\r
+ expected form and the value of base is between 2 and 36, it is used as the\r
+ base for conversion, ascribing to each letter its value as given above. If\r
+ the subject sequence begins with a minus sign, the value resulting from the\r
+ conversion is negated (in the return type). A pointer to the final string\r
+ is stored in the object pointed to by endptr, provided that endptr is\r
+ not a null pointer.\r
+\r
+ In other than the "C" locale, additional locale-specific subject sequence\r
+ forms may be accepted.\r
+\r
+ If the subject sequence is empty or does not have the expected form, no\r
+ conversion is performed; the value of nptr is stored in the object pointed\r
+ to by endptr, provided that endptr is not a null pointer.\r
+\r
+ @return The strtol, strtoll, strtoul, and strtoull functions return the\r
+ converted value, if any. If no conversion could be performed, zero\r
+ is returned. If the correct value is outside the range of\r
+ representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_MAX,\r
+ ULONG_MAX, or ULLONG_MAX is returned (according to the return type\r
+ and sign of the value, if any), and the value of the macro ERANGE\r
+ is stored in errno.\r
+**/\r
+long\r
+strtol(const char * nptr, char ** endptr, int base)\r
+{\r
+ const char *pEnd;\r
+ long Result = 0;\r
+ long Previous;\r
+ int temp;\r
+ BOOLEAN Negative = FALSE;\r
+\r
+ pEnd = nptr;\r
+\r
+ if((base < 0) || (base == 1) || (base > 36)) {\r
+ if(endptr != NULL) {\r
+ *endptr = NULL;\r
+ }\r
+ return 0;\r
+ }\r
+ // Skip leading spaces.\r
+ while(isspace(*nptr)) ++nptr;\r
+\r
+ // Process Subject sequence: optional sign followed by digits.\r
+ if(*nptr == '+') {\r
+ Negative = FALSE;\r
+ ++nptr;\r
+ }\r
+ else if(*nptr == '-') {\r
+ Negative = TRUE;\r
+ ++nptr;\r
+ }\r
+\r
+ if(*nptr == '0') { /* Might be Octal or Hex */\r
+ if(toupper(nptr[1]) == 'X') { /* Looks like Hex */\r
+ if((base == 0) || (base == 16)) {\r
+ nptr += 2; /* Skip the "0X" */\r
+ base = 16; /* In case base was 0 */\r
+ }\r
+ }\r
+ else { /* Looks like Octal */\r
+ if((base == 0) || (base == 8)) {\r
+ ++nptr; /* Skip the leading "0" */\r
+ base = 8; /* In case base was 0 */\r
+ }\r
+ }\r
+ }\r
+ if(base == 0) { /* If still zero then must be decimal */\r
+ base = 10;\r
+ }\r
+ if(*nptr == '0') {\r
+ for( ; *nptr == '0'; ++nptr); /* Skip any remaining leading zeros */\r
+ pEnd = nptr;\r
+ }\r
+\r
+ while( isalnum(*nptr) && ((temp = Digit2Val(*nptr)) < base)) {\r
+ Previous = Result;\r
+ Result = (Result * base) + (long int)temp;\r
+ if( Result <= Previous) { // Detect Overflow\r
+ if(Negative) {\r
+ Result = LONG_MIN;\r
+ }\r
+ else {\r
+ Result = LONG_MAX;\r
+ }\r
+ Negative = FALSE;\r
+ errno = ERANGE;\r
+ break;\r
+ }\r
+ pEnd = ++nptr;\r
+ }\r
+ if(Negative) {\r
+ Result = -Result;\r
+ }\r
+\r
+ // Save pointer to final sequence\r
+ if(endptr != NULL) {\r
+ *endptr = (char *)pEnd;\r
+ }\r
+ return Result;\r
+}\r
+\r
+/** The strtoull function converts the initial portion of the string pointed to\r
+ by nptr to unsigned long long int representation.\r
+\r
+ See the description for strtol for more information.\r
+\r
+ @return The strtoull function returns the converted value, if any. If no\r
+ conversion could be performed, zero is returned. If the correct\r
+ value is outside the range of representable values, ULLONG_MAX is\r
+ returned and the value of the macro ERANGE is stored in errno.\r
+**/\r
+unsigned long long\r
+strtoull(const char * nptr, char ** endptr, int base)\r
+{\r
+ const char *pEnd;\r
+ unsigned long long Result = 0;\r
+ unsigned long long Previous;\r
+ int temp;\r
+\r
+ pEnd = nptr;\r
+\r
+ if((base < 0) || (base == 1) || (base > 36)) {\r
+ if(endptr != NULL) {\r
+ *endptr = NULL;\r
+ }\r
+ return 0;\r
+ }\r
+ // Skip leading spaces.\r
+ while(isspace(*nptr)) ++nptr;\r
+\r
+ // Process Subject sequence: optional + sign followed by digits.\r
+ if(*nptr == '+') {\r
+ ++nptr;\r
+ }\r
+\r
+ if(*nptr == '0') { /* Might be Octal or Hex */\r
+ if(toupper(nptr[1]) == 'X') { /* Looks like Hex */\r
+ if((base == 0) || (base == 16)) {\r
+ nptr += 2; /* Skip the "0X" */\r
+ base = 16; /* In case base was 0 */\r
+ }\r
+ }\r
+ else { /* Looks like Octal */\r
+ if((base == 0) || (base == 8)) {\r
+ ++nptr; /* Skip the leading "0" */\r
+ base = 8; /* In case base was 0 */\r
+ }\r
+ }\r
+ }\r
+ if(base == 0) { /* If still zero then must be decimal */\r
+ base = 10;\r
+ }\r
+ if(*nptr == '0') {\r
+ for( ; *nptr == '0'; ++nptr); /* Skip any remaining leading zeros */\r
+ pEnd = nptr;\r
+ }\r
+\r
+ while( isalnum(*nptr) && ((temp = Digit2Val(*nptr)) < base)) {\r
+ Previous = Result;\r
+ Result = DivU64x32 (Result, base) + (unsigned long long)temp;\r
+ if( Result < Previous) { // If we overflowed\r
+ Result = ULLONG_MAX;\r
+ errno = ERANGE;\r
+ break;\r
+ }\r
+ pEnd = ++nptr;\r
+ }\r
+\r
+ // Save pointer to final sequence\r
+ if(endptr != NULL) {\r
+ *endptr = (char *)pEnd;\r
+ }\r
+ return Result;\r
+}\r
+\r
+/**\r
+ edk2 Jansson port does not support doubles, simply return 0.\r
+\r
+ These conversion functions convert the initial portion of the string\r
+ pointed to by nptr to double, float, and long double representation,\r
+ respectively.\r
+\r
+ The strtod(), strtof(), and strtold() functions return the converted\r
+ value, if any.\r
+\r
+ If endptr is not NULL, a pointer to the character after the last charac-\r
+ ter used in the conversion is stored in the location referenced by\r
+ endptr.\r
+\r
+ If no conversion is performed, zero is returned and the value of nptr is\r
+ stored in the location referenced by endptr.\r
+\r
+ If the correct value would cause overflow, plus or minus HUGE_VAL,\r
+ HUGE_VALF, or HUGE_VALL is returned (according to the sign and type of\r
+ the return value), and ERANGE is stored in errno. If the correct value\r
+ would cause underflow, zero is returned and ERANGE is stored in errno.\r
+\r
+ @return Return 0.\r
+**/\r
+double\r
+strtod (const char * __restrict nptr, char ** __restrict endptr) {\r
+\r
+ DEBUG((DEBUG_INFO, "We don't supprot double type on edk2 yet!"));\r
+ ASSERT(FALSE);\r
+ return (double)0;\r
+}\r
+\r
+/**\r
+ Allocate and zero-initialize array.\r
+**/\r
+void *\r
+calloc(size_t Num, size_t Size)\r
+{\r
+ void *RetVal;\r
+ size_t NumSize;\r
+\r
+ NumSize = Num * Size;\r
+ RetVal = NULL;\r
+ if (NumSize != 0) {\r
+ RetVal = malloc(NumSize);\r
+ if( RetVal != NULL) {\r
+ (VOID)ZeroMem( RetVal, NumSize);\r
+ }\r
+ }\r
+ DEBUG((DEBUG_POOL, "0x%p = calloc(%d, %d)\n", RetVal, Num, Size));\r
+\r
+ return RetVal;\r
+}\r
+\r
+//\r
+// The arrays give the cumulative number of days up to the first of the\r
+// month number used as the index (1 -> 12) for regular and leap years.\r
+// The value at index 13 is for the whole year.\r
+//\r
+UINTN CumulativeDays[2][14] = {\r
+ {\r
+ 0,\r
+ 0,\r
+ 31,\r
+ 31 + 28,\r
+ 31 + 28 + 31,\r
+ 31 + 28 + 31 + 30,\r
+ 31 + 28 + 31 + 30 + 31,\r
+ 31 + 28 + 31 + 30 + 31 + 30,\r
+ 31 + 28 + 31 + 30 + 31 + 30 + 31,\r
+ 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,\r
+ 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r
+ 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r
+ 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r
+ 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31\r
+ },\r
+ {\r
+ 0,\r
+ 0,\r
+ 31,\r
+ 31 + 29,\r
+ 31 + 29 + 31,\r
+ 31 + 29 + 31 + 30,\r
+ 31 + 29 + 31 + 30 + 31,\r
+ 31 + 29 + 31 + 30 + 31 + 30,\r
+ 31 + 29 + 31 + 30 + 31 + 30 + 31,\r
+ 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,\r
+ 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r
+ 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r
+ 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r
+ 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31\r
+ }\r
+};\r
+\r
+#define IsLeap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))\r
+#define SECSPERMIN (60)\r
+#define SECSPERHOUR (60 * 60)\r
+#define SECSPERDAY (24 * SECSPERHOUR)\r
+\r
+/**\r
+ Get the system time as seconds elapsed since midnight, January 1, 1970.\r
+**/\r
+time_t time (time_t *timer)\r
+{\r
+ EFI_TIME Time;\r
+ time_t CalTime;\r
+ UINTN Year;\r
+\r
+ //\r
+ // Get the current time and date information\r
+ //\r
+ gRT->GetTime (&Time, NULL);\r
+\r
+ //\r
+ // Years Handling\r
+ // UTime should now be set to 00:00:00 on Jan 1 of the current year.\r
+ //\r
+ for (Year = 1970, CalTime = 0; Year != Time.Year; Year++) {\r
+ CalTime = CalTime + (time_t)(CumulativeDays[IsLeap(Year)][13] * SECSPERDAY);\r
+ }\r
+\r
+ //\r
+ // Add in number of seconds for current Month, Day, Hour, Minute, Seconds, and TimeZone adjustment\r
+ //\r
+ CalTime = CalTime +\r
+ (time_t)((Time.TimeZone != EFI_UNSPECIFIED_TIMEZONE) ? (Time.TimeZone * 60) : 0) +\r
+ (time_t)(CumulativeDays[IsLeap(Time.Year)][Time.Month] * SECSPERDAY) +\r
+ (time_t)(((Time.Day > 0) ? Time.Day - 1 : 0) * SECSPERDAY) +\r
+ (time_t)(Time.Hour * SECSPERHOUR) +\r
+ (time_t)(Time.Minute * 60) +\r
+ (time_t)Time.Second;\r
+\r
+ if (timer != NULL) {\r
+ *timer = CalTime;\r
+ }\r
+\r
+ return CalTime;\r
+}\r
+\r
+/**\r
+ Performs a quick sort\r
+**/\r
+void qsort (void *base, size_t num, size_t width, int (*compare)(const void *, const void *))\r
+{\r
+\r
+ ASSERT (base != NULL);\r
+ ASSERT (compare != NULL);\r
+\r
+ PerformQuickSort (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare);\r
+ return;\r
+}\r
+\r
+/**\r
+ Get character from stream, we don't support file operastion on edk2 JSON library.\r
+\r
+ @return Returns the character currently pointed by the internal file position indicator of the specified stream\r
+\r
+**/\r
+int fgetc(FILE * _File){\r
+ return 0;\r
+}\r
+/**\r
+ This function check if this is the formating string specifier.\r
+\r
+ @param[in] FormatString A Null-terminated ASCII format string.\r
+ @param[in,out] CurrentPosition The starting position at the given string to check for\r
+ "[flags][width][.precision][length]s" string specifier.\r
+ @param[in] StrLength Maximum string length.\r
+\r
+ @return BOOLEAN TRUE means this is the formating string specifier. CurrentPosition is\r
+ returned at the position of "s".\r
+ FALSE means this is not the formating string specifier.. CurrentPosition is\r
+ returned at the position of failed character.\r
+\r
+**/\r
+BOOLEAN\r
+CheckFormatingString (\r
+ IN CONST CHAR8 *FormatString,\r
+ IN OUT UINTN *CurrentPosition,\r
+ IN UINTN StrLength\r
+ )\r
+{\r
+ CHAR8 FormatStringParamater;\r
+\r
+ while (*(FormatString + *CurrentPosition) != 's') {\r
+ //\r
+ // Loop until reach character 's' if the formating string is\r
+ // compliant with "[flags][width][.precision][length]" format for\r
+ // the string specifier.\r
+ //\r
+ FormatStringParamater = *(FormatString + *CurrentPosition);\r
+ if ((FormatStringParamater != '-') &&\r
+ (FormatStringParamater != '+') &&\r
+ (FormatStringParamater != '*') &&\r
+ (FormatStringParamater != '.') &&\r
+ !(((UINTN)FormatStringParamater >= (UINTN)'0') && ((UINTN)FormatStringParamater <= (UINTN)'9'))\r
+ ) {\r
+ return FALSE;\r
+ }\r
+ (*CurrentPosition)++;\r
+ if (*CurrentPosition >= StrLength) {\r
+ return FALSE;\r
+ }\r
+ };\r
+ return TRUE;\r
+}\r
+\r
+/**\r
+ This function clones *FormatString however replaces "%s" with "%a" in the\r
+ returned string.\r
+\r
+ @param[in] A Null-terminated ASCII format string.\r
+\r
+ @return The new format string. Caller has to free the memory of this string\r
+ using FreePool().\r
+\r
+**/\r
+CHAR8 *\r
+ReplaceUnicodeToAsciiStrFormat (\r
+ IN CONST CHAR8 *FormatString\r
+)\r
+{\r
+ UINTN FormatStrSize;\r
+ UINTN FormatStrIndex;\r
+ UINTN FormatStrSpecifier;\r
+ BOOLEAN PercentageMark;\r
+ CHAR8 *TempFormatBuffer;\r
+ BOOLEAN IsFormatString;\r
+\r
+ //\r
+ // Error checking.\r
+ //\r
+ if (FormatString == NULL) {\r
+ return NULL;\r
+ }\r
+ FormatStrSize = AsciiStrSize(FormatString);\r
+ if (FormatStrSize == 0) {\r
+ return NULL;\r
+ }\r
+ TempFormatBuffer = AllocatePool(FormatStrSize); // Allocate memory for the\r
+ // new string.\r
+ if (TempFormatBuffer== NULL) {\r
+ return NULL;\r
+ }\r
+ //\r
+ // Clone *FormatString but replace "%s" wih "%a".\r
+ // "%%" is not considered as the format tag.\r
+ //\r
+ PercentageMark = FALSE;\r
+ FormatStrIndex = 0;\r
+ while (FormatStrIndex < FormatStrSize) {\r
+ if (PercentageMark == TRUE) {\r
+ //\r
+ // Previous character is "%".\r
+ //\r
+ PercentageMark = FALSE;\r
+ if (*(FormatString + FormatStrIndex) != '%') { // Check if this is double "%".\r
+ FormatStrSpecifier = FormatStrIndex;\r
+ //\r
+ // Check if this is the formating string specifier.\r
+ //\r
+ IsFormatString = CheckFormatingString (FormatString, &FormatStrSpecifier, FormatStrSize);\r
+ if ((FormatStrSpecifier - FormatStrIndex) != 0) {\r
+ CopyMem((VOID *)(TempFormatBuffer + FormatStrIndex),\r
+ (VOID *)(FormatString + FormatStrIndex),\r
+ FormatStrSpecifier - FormatStrIndex\r
+ );\r
+ }\r
+ FormatStrIndex = FormatStrSpecifier;\r
+ if (IsFormatString == TRUE) {\r
+ //\r
+ // Replace 's' with 'a' which is printed in ASCII\r
+ // format on edk2 environment.\r
+ //\r
+ *(TempFormatBuffer + FormatStrSpecifier) = 'a';\r
+ FormatStrIndex ++;\r
+ }\r
+ continue;\r
+ }\r
+ goto ContinueCheck;\r
+ }\r
+ if (*(FormatString + FormatStrIndex) == '%') {\r
+ //\r
+ // This character is "%", set the flag.\r
+ //\r
+ PercentageMark = TRUE;\r
+ }\r
+ContinueCheck:\r
+ //\r
+ // Clone character to the new string and advance FormatStrIndex\r
+ // to process next character.\r
+ //\r
+ *(TempFormatBuffer + FormatStrIndex) = *(FormatString + FormatStrIndex);\r
+ FormatStrIndex++;\r
+ };\r
+ return TempFormatBuffer;\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
+ CHAR8 *TempFormatBuffer;\r
+ UINTN LenStrProduced;\r
+\r
+ //\r
+ // Looking for "%s" in the format string and replace it\r
+ // with "%a" for printing ASCII code characters on edk2\r
+ // environment.\r
+ //\r
+ TempFormatBuffer = ReplaceUnicodeToAsciiStrFormat (FormatString);\r
+ if (TempFormatBuffer == NULL) {\r
+ return 0;\r
+ }\r
+ LenStrProduced = AsciiVSPrint (StartOfBuffer, BufferSize, (CONST CHAR8 *)TempFormatBuffer, Marker);\r
+ FreePool (TempFormatBuffer);\r
+ return LenStrProduced;\r
+}\r
+\r
+/**\r
+ This is the Redfish version of CRT snprintf function, this function replaces "%s" to\r
+ "%a" before invoking AsciiSPrint(). That is because "%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.\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
+ VA_LIST Marker;\r
+ UINTN LenStrProduced;\r
+\r
+ VA_START(Marker, FormatString);\r
+ LenStrProduced = RedfishAsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);\r
+ return LenStrProduced;\r
+}\r
+\r