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