| 1 | /** @file\r |
| 2 | Print Library worker functions.\r |
| 3 | \r |
| 4 | Copyright (c) 2006, Intel Corporation<BR>\r |
| 5 | All rights reserved. This program and the accompanying materials\r |
| 6 | are licensed and made available under the terms and conditions of the BSD License\r |
| 7 | which accompanies this distribution. The full text of the license may be found at\r |
| 8 | http://opensource.org/licenses/bsd-license.php\r |
| 9 | \r |
| 10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
| 11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
| 12 | \r |
| 13 | Module Name: PrintLibInternal.c\r |
| 14 | \r |
| 15 | **/\r |
| 16 | \r |
| 17 | #include "PrintLibInternal.h"\r |
| 18 | \r |
| 19 | static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};\r |
| 20 | \r |
| 21 | \r |
| 22 | /**\r |
| 23 | Internal function that places the character into the Buffer.\r |
| 24 | \r |
| 25 | Internal function that places ASCII or Unicode character into the Buffer.\r |
| 26 | \r |
| 27 | @param Buffer Buffer to place the Unicode or ASCII string.\r |
| 28 | @param Length Count of character to be placed into Buffer.\r |
| 29 | @param Character Character to be placed into Buffer.\r |
| 30 | @param Increment Character increment in Buffer.\r |
| 31 | \r |
| 32 | @return Number of characters printed.\r |
| 33 | \r |
| 34 | **/\r |
| 35 | CHAR8 *\r |
| 36 | BasePrintLibFillBuffer (\r |
| 37 | CHAR8 *Buffer,\r |
| 38 | INTN Length,\r |
| 39 | UINTN Character,\r |
| 40 | INTN Increment\r |
| 41 | )\r |
| 42 | {\r |
| 43 | INTN Index;\r |
| 44 | \r |
| 45 | for (Index = 0; Index < Length; Index++) {\r |
| 46 | *Buffer = (CHAR8) Character;\r |
| 47 | *(Buffer + 1) = (CHAR8) (Character >> 8);\r |
| 48 | Buffer += Increment;\r |
| 49 | }\r |
| 50 | return Buffer;\r |
| 51 | }\r |
| 52 | \r |
| 53 | /**\r |
| 54 | Internal function that convert a decimal number to a string in Buffer.\r |
| 55 | \r |
| 56 | Print worker function that convert a decimal number to a string in Buffer.\r |
| 57 | \r |
| 58 | @param Buffer Location to place the Unicode or ASCII string of Value.\r |
| 59 | @param Value Value to convert to a Decimal or Hexidecimal string in Buffer.\r |
| 60 | @param Radix Radix of the value\r |
| 61 | \r |
| 62 | @return Number of characters printed.\r |
| 63 | \r |
| 64 | **/\r |
| 65 | UINTN\r |
| 66 | EFIAPI\r |
| 67 | BasePrintLibValueToString (\r |
| 68 | IN OUT CHAR8 *Buffer,\r |
| 69 | IN INT64 Value,\r |
| 70 | IN UINTN Radix\r |
| 71 | )\r |
| 72 | {\r |
| 73 | UINTN Digits;\r |
| 74 | UINT32 Remainder;\r |
| 75 | \r |
| 76 | //\r |
| 77 | // Loop to convert one digit at a time in reverse order\r |
| 78 | //\r |
| 79 | *(Buffer++) = 0;\r |
| 80 | Digits = 0;\r |
| 81 | do {\r |
| 82 | Value = (INT64)DivU64x32Remainder ((UINT64)Value, (UINT32)Radix, &Remainder);\r |
| 83 | *(Buffer++) = mHexStr[Remainder];\r |
| 84 | Digits++;\r |
| 85 | } while (Value != 0);\r |
| 86 | return Digits;\r |
| 87 | }\r |
| 88 | \r |
| 89 | /**\r |
| 90 | Internal function that converts a decimal value to a Null-terminated string.\r |
| 91 | \r |
| 92 | Converts the decimal number specified by Value to a Null-terminated \r |
| 93 | string specified by Buffer containing at most Width characters.\r |
| 94 | If Width is 0 then a width of MAXIMUM_VALUE_CHARACTERS is assumed.\r |
| 95 | The total number of characters placed in Buffer is returned.\r |
| 96 | If the conversion contains more than Width characters, then only the first\r |
| 97 | Width characters are returned, and the total number of characters \r |
| 98 | required to perform the conversion is returned.\r |
| 99 | Additional conversion parameters are specified in Flags. \r |
| 100 | The Flags bit LEFT_JUSTIFY is always ignored.\r |
| 101 | All conversions are left justified in Buffer.\r |
| 102 | If Width is 0, PREFIX_ZERO is ignored in Flags.\r |
| 103 | If COMMA_TYPE is set in Flags, then PREFIX_ZERO is ignored in Flags, and commas\r |
| 104 | are inserted every 3rd digit starting from the right.\r |
| 105 | If Value is < 0, then the fist character in Buffer is a '-'.\r |
| 106 | If PREFIX_ZERO is set in Flags and PREFIX_ZERO is not being ignored, \r |
| 107 | then Buffer is padded with '0' characters so the combination of the optional '-' \r |
| 108 | sign character, '0' characters, digit characters for Value, and the Null-terminator\r |
| 109 | add up to Width characters.\r |
| 110 | \r |
| 111 | If Buffer is NULL, then ASSERT().\r |
| 112 | If unsupported bits are set in Flags, then ASSERT().\r |
| 113 | If Width >= MAXIMUM_VALUE_CHARACTERS, then ASSERT()\r |
| 114 | \r |
| 115 | @param Buffer Pointer to the output buffer for the produced Null-terminated\r |
| 116 | string.\r |
| 117 | @param Flags The bitmask of flags that specify left justification, zero pad,\r |
| 118 | and commas.\r |
| 119 | @param Value The 64-bit signed value to convert to a string.\r |
| 120 | @param Width The maximum number of characters to place in Buffer.\r |
| 121 | @param Increment Character increment in Buffer.\r |
| 122 | \r |
| 123 | @return Total number of characters required to perform the conversion.\r |
| 124 | \r |
| 125 | **/\r |
| 126 | UINTN\r |
| 127 | BasePrintLibConvertValueToString (\r |
| 128 | IN OUT CHAR8 *Buffer,\r |
| 129 | IN UINTN Flags,\r |
| 130 | IN INT64 Value,\r |
| 131 | IN UINTN Width,\r |
| 132 | IN UINTN Increment\r |
| 133 | )\r |
| 134 | {\r |
| 135 | CHAR8 *OriginalBuffer;\r |
| 136 | CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];\r |
| 137 | UINTN Count;\r |
| 138 | UINTN Digits;\r |
| 139 | UINTN Index;\r |
| 140 | \r |
| 141 | OriginalBuffer = Buffer;\r |
| 142 | \r |
| 143 | if (Width == 0 || (Flags & COMMA_TYPE) != 0) {\r |
| 144 | Flags &= (~PREFIX_ZERO);\r |
| 145 | }\r |
| 146 | \r |
| 147 | if (Width == 0 || Width > (MAXIMUM_VALUE_CHARACTERS - 1)) {\r |
| 148 | Width = MAXIMUM_VALUE_CHARACTERS - 1;\r |
| 149 | }\r |
| 150 | \r |
| 151 | if (Value < 0) {\r |
| 152 | Value = -Value;\r |
| 153 | Buffer = BasePrintLibFillBuffer (Buffer, 1, '-', Increment);\r |
| 154 | }\r |
| 155 | \r |
| 156 | Count = BasePrintLibValueToString (ValueBuffer, Value, 10);\r |
| 157 | \r |
| 158 | if ((Flags & PREFIX_ZERO) != 0) {\r |
| 159 | Buffer = BasePrintLibFillBuffer (Buffer, Width - Count, '0', Increment);\r |
| 160 | }\r |
| 161 | \r |
| 162 | Digits = 3 - (Count % 3);\r |
| 163 | for (Index = 0; Index < Count; Index++) {\r |
| 164 | Buffer = BasePrintLibFillBuffer (Buffer, 1, ValueBuffer[Count - Index], Increment);\r |
| 165 | if ((Flags & COMMA_TYPE) != 0) {\r |
| 166 | Digits++;\r |
| 167 | if (Digits == 3) {\r |
| 168 | Digits = 0;\r |
| 169 | if ((Index + 1) < Count) {\r |
| 170 | Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', Increment);\r |
| 171 | }\r |
| 172 | }\r |
| 173 | }\r |
| 174 | }\r |
| 175 | \r |
| 176 | Buffer = BasePrintLibFillBuffer (Buffer, 1, 0, Increment);\r |
| 177 | \r |
| 178 | return ((Buffer - OriginalBuffer) / Increment);\r |
| 179 | }\r |