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