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