+/** @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