]>
Commit | Line | Data |
---|---|---|
93e3992d | 1 | /**@file |
2 | Copyright (c) 2007, Intel Corporation | |
3 | ||
4 | All rights reserved. This program and the accompanying materials | |
5 | are licensed and made available under the terms and conditions of the BSD License | |
6 | which accompanies this distribution. The full text of the license may be found at | |
7 | http://opensource.org/licenses/bsd-license.php | |
8 | ||
9 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, | |
10 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. | |
11 | ||
12 | ||
13 | **/ | |
14 | ||
15 | #include "Setup.h" | |
16 | ||
17 | CHAR16 | |
18 | NibbleToHexChar ( | |
19 | IN UINT8 Nibble | |
20 | ) | |
21 | /*++ | |
22 | ||
23 | Routine Description: | |
24 | Converts the low nibble of a byte to hex unicode character. | |
25 | ||
26 | Arguments: | |
27 | Nibble - lower nibble of a byte. | |
28 | ||
29 | Returns: | |
30 | Hex unicode character. | |
31 | ||
32 | --*/ | |
33 | { | |
34 | Nibble &= 0x0F; | |
35 | if (Nibble <= 0x9) { | |
36 | return (CHAR16)(Nibble + L'0'); | |
37 | } | |
38 | ||
39 | return (CHAR16)(Nibble - 0xA + L'A'); | |
40 | } | |
41 | ||
42 | ||
43 | ||
44 | /** | |
45 | Converts binary buffer to Unicode string. | |
46 | At a minimum, any blob of data could be represented as a hex string. | |
47 | ||
48 | @param Str Pointer to the string. | |
49 | @param HexStringBufferLength Length in bytes of buffer to hold the hex string. | |
50 | Includes tailing '\0' character. If routine return | |
51 | with EFI_SUCCESS, containing length of hex string | |
52 | buffer. If routine return with | |
53 | EFI_BUFFER_TOO_SMALL, containg length of hex | |
54 | string buffer desired. | |
55 | @param Buf Buffer to be converted from. | |
56 | @param Len Length in bytes of the buffer to be converted. | |
57 | ||
58 | @retval EFI_SUCCESS Routine success. | |
59 | @retval EFI_BUFFER_TOO_SMALL The hex string buffer is too small. | |
60 | ||
61 | **/ | |
62 | EFI_STATUS | |
63 | R8_BufToHexString ( | |
64 | IN OUT CHAR16 *Str, | |
65 | IN OUT UINTN *HexStringBufferLength, | |
66 | IN UINT8 *Buf, | |
67 | IN UINTN Len | |
68 | ) | |
69 | { | |
70 | // | |
71 | // Porting Guide: | |
72 | // This library interface is simply obsolete. | |
73 | // Include the source code to user code. | |
74 | // | |
75 | UINTN Idx; | |
76 | UINT8 Byte; | |
77 | UINTN StrLen; | |
78 | ||
79 | // | |
80 | // Make sure string is either passed or allocate enough. | |
81 | // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer. | |
82 | // Plus the Unicode termination character. | |
83 | // | |
84 | StrLen = Len * 2; | |
85 | if (StrLen > ((*HexStringBufferLength) - 1)) { | |
86 | *HexStringBufferLength = StrLen + 1; | |
87 | return EFI_BUFFER_TOO_SMALL; | |
88 | } | |
89 | ||
90 | *HexStringBufferLength = StrLen + 1; | |
91 | // | |
92 | // Ends the string. | |
93 | // | |
94 | Str[StrLen] = L'\0'; | |
95 | ||
96 | for (Idx = 0; Idx < Len; Idx++) { | |
97 | ||
98 | Byte = Buf[Idx]; | |
99 | Str[StrLen - 1 - Idx * 2] = NibbleToHexChar (Byte); | |
100 | Str[StrLen - 2 - Idx * 2] = NibbleToHexChar ((UINT8)(Byte >> 4)); | |
101 | } | |
102 | ||
103 | return EFI_SUCCESS; | |
104 | } | |
105 | ||
106 | ||
107 | ||
108 | ||
109 | /** | |
110 | Converts Unicode string to binary buffer. | |
111 | The conversion may be partial. | |
112 | The first character in the string that is not hex digit stops the conversion. | |
113 | At a minimum, any blob of data could be represented as a hex string. | |
114 | ||
115 | @param Buf Pointer to buffer that receives the data. | |
116 | @param Len Length in bytes of the buffer to hold converted | |
117 | data. If routine return with EFI_SUCCESS, | |
118 | containing length of converted data. If routine | |
119 | return with EFI_BUFFER_TOO_SMALL, containg length | |
120 | of buffer desired. | |
121 | @param Str String to be converted from. | |
122 | @param ConvertedStrLen Length of the Hex String consumed. | |
123 | ||
124 | @retval EFI_SUCCESS Routine Success. | |
125 | @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold converted data. | |
126 | ||
127 | **/ | |
128 | EFI_STATUS | |
129 | R8_HexStringToBuf ( | |
130 | IN OUT UINT8 *Buf, | |
131 | IN OUT UINTN *Len, | |
132 | IN CHAR16 *Str, | |
133 | OUT UINTN *ConvertedStrLen OPTIONAL | |
134 | ) | |
135 | { | |
136 | // | |
137 | // Porting Guide: | |
138 | // This library interface is simply obsolete. | |
139 | // Include the source code to user code. | |
140 | // | |
141 | ||
142 | UINTN HexCnt; | |
143 | UINTN Idx; | |
144 | UINTN BufferLength; | |
145 | UINT8 Digit; | |
146 | UINT8 Byte; | |
147 | ||
148 | // | |
149 | // Find out how many hex characters the string has. | |
150 | // | |
151 | for (Idx = 0, HexCnt = 0; R8_IsHexDigit (&Digit, Str[Idx]); Idx++, HexCnt++); | |
152 | ||
153 | if (HexCnt == 0) { | |
154 | *Len = 0; | |
155 | return EFI_SUCCESS; | |
156 | } | |
157 | // | |
158 | // Two Unicode characters make up 1 buffer byte. Round up. | |
159 | // | |
160 | BufferLength = (HexCnt + 1) / 2; | |
161 | ||
162 | // | |
163 | // Test if buffer is passed enough. | |
164 | // | |
165 | if (BufferLength > (*Len)) { | |
166 | *Len = BufferLength; | |
167 | return EFI_BUFFER_TOO_SMALL; | |
168 | } | |
169 | ||
170 | *Len = BufferLength; | |
171 | ||
172 | for (Idx = 0; Idx < HexCnt; Idx++) { | |
173 | ||
174 | R8_IsHexDigit (&Digit, Str[HexCnt - 1 - Idx]); | |
175 | ||
176 | // | |
177 | // For odd charaters, write the lower nibble for each buffer byte, | |
178 | // and for even characters, the upper nibble. | |
179 | // | |
180 | if ((Idx & 1) == 0) { | |
181 | Byte = Digit; | |
182 | } else { | |
183 | Byte = Buf[Idx / 2]; | |
184 | Byte &= 0x0F; | |
185 | Byte = (UINT8) (Byte | Digit << 4); | |
186 | } | |
187 | ||
188 | Buf[Idx / 2] = Byte; | |
189 | } | |
190 | ||
191 | if (ConvertedStrLen != NULL) { | |
192 | *ConvertedStrLen = HexCnt; | |
193 | } | |
194 | ||
195 | return EFI_SUCCESS; | |
196 | } | |
197 | ||
198 | ||
199 | ||
200 | ||
201 | /** | |
202 | Determines if a Unicode character is a hexadecimal digit. | |
203 | The test is case insensitive. | |
204 | ||
205 | @param Digit Pointer to byte that receives the value of the hex | |
206 | character. | |
207 | @param Char Unicode character to test. | |
208 | ||
209 | @retval TRUE If the character is a hexadecimal digit. | |
210 | @retval FALSE Otherwise. | |
211 | ||
212 | **/ | |
213 | BOOLEAN | |
214 | R8_IsHexDigit ( | |
215 | OUT UINT8 *Digit, | |
216 | IN CHAR16 Char | |
217 | ) | |
218 | { | |
219 | // | |
220 | // Porting Guide: | |
221 | // This library interface is simply obsolete. | |
222 | // Include the source code to user code. | |
223 | // | |
224 | ||
225 | if ((Char >= L'0') && (Char <= L'9')) { | |
226 | *Digit = (UINT8) (Char - L'0'); | |
227 | return TRUE; | |
228 | } | |
229 | ||
230 | if ((Char >= L'A') && (Char <= L'F')) { | |
231 | *Digit = (UINT8) (Char - L'A' + 0x0A); | |
232 | return TRUE; | |
233 | } | |
234 | ||
235 | if ((Char >= L'a') && (Char <= L'f')) { | |
236 | *Digit = (UINT8) (Char - L'a' + 0x0A); | |
237 | return TRUE; | |
238 | } | |
239 | ||
240 | return FALSE; | |
241 | } | |
242 | ||
243 |