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