]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/HiiDatabaseDxe/R8Lib.c
UEFI HII: Merge UEFI HII support changes from branch.
[mirror_edk2.git] / MdeModulePkg / Universal / HiiDatabaseDxe / R8Lib.c
CommitLineData
93e3992d 1/**@file\r
2 Copyright (c) 2007, Intel Corporation\r
3\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8\r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
12\r
13**/\r
14\r
15#include "HiiDatabase.h"\r
16\r
17\r
18CHAR16\r
19NibbleToHexChar (\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 Compare whether two names of languages are identical.\r
45\r
46 @param Language1 Name of language 1\r
47 @param Language2 Name of language 2\r
48\r
49 @retval TRUE same\r
50 @retval FALSE not same\r
51\r
52**/\r
53BOOLEAN\r
54R8_EfiLibCompareLanguage (\r
55 IN CHAR8 *Language1,\r
56 IN CHAR8 *Language2\r
57 )\r
58{\r
59 //\r
60 // Porting Guide:\r
61 // This library interface is simply obsolete.\r
62 // Include the source code to user code.\r
63 //\r
64 UINTN Index;\r
65\r
66 for (Index = 0; (Language1[Index] != 0) && (Language2[Index] != 0); Index++) {\r
67 if (Language1[Index] != Language2[Index]) {\r
68 return FALSE;\r
69 }\r
70 }\r
71\r
72 if (((Language1[Index] == 0) && (Language2[Index] == 0)) || \r
73 ((Language1[Index] == 0) && (Language2[Index] != ';')) ||\r
74 ((Language1[Index] == ';') && (Language2[Index] != 0)) ||\r
75 ((Language1[Index] == ';') && (Language2[Index] != ';'))) {\r
76 return TRUE;\r
77 }\r
78\r
79 return FALSE;\r
80}\r
81\r
82\r
83\r
84\r
85/**\r
86 Converts binary buffer to Unicode string.\r
87 At a minimum, any blob of data could be represented as a hex string.\r
88\r
89 @param Str Pointer to the string.\r
90 @param HexStringBufferLength Length in bytes of buffer to hold the hex string.\r
91 Includes tailing '\0' character. If routine return\r
92 with EFI_SUCCESS, containing length of hex string\r
93 buffer. If routine return with\r
94 EFI_BUFFER_TOO_SMALL, containg length of hex\r
95 string buffer desired.\r
96 @param Buf Buffer to be converted from.\r
97 @param Len Length in bytes of the buffer to be converted.\r
98\r
99 @retval EFI_SUCCESS Routine success.\r
100 @retval EFI_BUFFER_TOO_SMALL The hex string buffer is too small.\r
101\r
102**/\r
103EFI_STATUS\r
104R8_BufToHexString (\r
105 IN OUT CHAR16 *Str,\r
106 IN OUT UINTN *HexStringBufferLength,\r
107 IN UINT8 *Buf,\r
108 IN UINTN Len\r
109 )\r
110{\r
111 //\r
112 // Porting Guide:\r
113 // This library interface is simply obsolete.\r
114 // Include the source code to user code.\r
115 //\r
116 UINTN Idx;\r
117 UINT8 Byte;\r
118 UINTN StrLen;\r
119\r
120 //\r
121 // Make sure string is either passed or allocate enough.\r
122 // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer.\r
123 // Plus the Unicode termination character.\r
124 //\r
125 StrLen = Len * 2;\r
126 if (StrLen > ((*HexStringBufferLength) - 1)) {\r
127 *HexStringBufferLength = StrLen + 1;\r
128 return EFI_BUFFER_TOO_SMALL;\r
129 }\r
130\r
131 *HexStringBufferLength = StrLen + 1;\r
132 //\r
133 // Ends the string.\r
134 //\r
135 Str[StrLen] = L'\0';\r
136\r
137 for (Idx = 0; Idx < Len; Idx++) {\r
138\r
139 Byte = Buf[Idx];\r
140 Str[StrLen - 1 - Idx * 2] = NibbleToHexChar (Byte);\r
141 Str[StrLen - 2 - Idx * 2] = NibbleToHexChar ((UINT8)(Byte >> 4));\r
142 }\r
143\r
144 return EFI_SUCCESS;\r
145}\r
146\r
147\r
148\r
149\r
150/**\r
151 Converts Unicode string to binary buffer.\r
152 The conversion may be partial.\r
153 The first character in the string that is not hex digit stops the conversion.\r
154 At a minimum, any blob of data could be represented as a hex string.\r
155\r
156 @param Buf Pointer to buffer that receives the data.\r
157 @param Len Length in bytes of the buffer to hold converted\r
158 data. If routine return with EFI_SUCCESS,\r
159 containing length of converted data. If routine\r
160 return with EFI_BUFFER_TOO_SMALL, containg length\r
161 of buffer desired.\r
162 @param Str String to be converted from.\r
163 @param ConvertedStrLen Length of the Hex String consumed.\r
164\r
165 @retval EFI_SUCCESS Routine Success.\r
166 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold converted data.\r
167\r
168**/\r
169EFI_STATUS\r
170R8_HexStringToBuf (\r
171 IN OUT UINT8 *Buf,\r
172 IN OUT UINTN *Len,\r
173 IN CHAR16 *Str,\r
174 OUT UINTN *ConvertedStrLen OPTIONAL\r
175 )\r
176{\r
177 //\r
178 // Porting Guide:\r
179 // This library interface is simply obsolete.\r
180 // Include the source code to user code.\r
181 //\r
182\r
183 UINTN HexCnt;\r
184 UINTN Idx;\r
185 UINTN BufferLength;\r
186 UINT8 Digit;\r
187 UINT8 Byte;\r
188\r
189 //\r
190 // Find out how many hex characters the string has.\r
191 //\r
192 for (Idx = 0, HexCnt = 0; R8_IsHexDigit (&Digit, Str[Idx]); Idx++, HexCnt++);\r
193\r
194 if (HexCnt == 0) {\r
195 *Len = 0;\r
196 return EFI_SUCCESS;\r
197 }\r
198 //\r
199 // Two Unicode characters make up 1 buffer byte. Round up.\r
200 //\r
201 BufferLength = (HexCnt + 1) / 2;\r
202\r
203 //\r
204 // Test if buffer is passed enough.\r
205 //\r
206 if (BufferLength > (*Len)) {\r
207 *Len = BufferLength;\r
208 return EFI_BUFFER_TOO_SMALL;\r
209 }\r
210\r
211 *Len = BufferLength;\r
212\r
213 for (Idx = 0; Idx < HexCnt; Idx++) {\r
214\r
215 R8_IsHexDigit (&Digit, Str[HexCnt - 1 - Idx]);\r
216\r
217 //\r
218 // For odd charaters, write the lower nibble for each buffer byte,\r
219 // and for even characters, the upper nibble.\r
220 //\r
221 if ((Idx & 1) == 0) {\r
222 Byte = Digit;\r
223 } else {\r
224 Byte = Buf[Idx / 2];\r
225 Byte &= 0x0F;\r
226 Byte = (UINT8) (Byte | Digit << 4);\r
227 }\r
228\r
229 Buf[Idx / 2] = Byte;\r
230 }\r
231\r
232 if (ConvertedStrLen != NULL) {\r
233 *ConvertedStrLen = HexCnt;\r
234 }\r
235\r
236 return EFI_SUCCESS;\r
237}\r
238\r
239\r
240\r
241\r
242/**\r
243 Determines if a Unicode character is a hexadecimal digit.\r
244 The test is case insensitive.\r
245\r
246 @param Digit Pointer to byte that receives the value of the hex\r
247 character.\r
248 @param Char Unicode character to test.\r
249\r
250 @retval TRUE If the character is a hexadecimal digit.\r
251 @retval FALSE Otherwise.\r
252\r
253**/\r
254BOOLEAN\r
255R8_IsHexDigit (\r
256 OUT UINT8 *Digit,\r
257 IN CHAR16 Char\r
258 )\r
259{\r
260 //\r
261 // Porting Guide:\r
262 // This library interface is simply obsolete.\r
263 // Include the source code to user code.\r
264 //\r
265\r
266 if ((Char >= L'0') && (Char <= L'9')) {\r
267 *Digit = (UINT8) (Char - L'0');\r
268 return TRUE;\r
269 }\r
270\r
271 if ((Char >= L'A') && (Char <= L'F')) {\r
272 *Digit = (UINT8) (Char - L'A' + 0x0A);\r
273 return TRUE;\r
274 }\r
275\r
276 if ((Char >= L'a') && (Char <= L'f')) {\r
277 *Digit = (UINT8) (Char - L'a' + 0x0A);\r
278 return TRUE;\r
279 }\r
280\r
281 return FALSE;\r
282}\r
283\r
284\r