]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/ExtendedIfrSupportLib/R8Lib.c
c7e82a28f9495d48848abf322b9477703e2b69e1
[mirror_edk2.git] / MdeModulePkg / Library / ExtendedIfrSupportLib / 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 "LibraryInternal.h"
16
17
18 CHAR16
19 InternalNibbleToHexChar (
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 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] = InternalNibbleToHexChar (Byte);
100 Str[StrLen - 2 - Idx * 2] = InternalNibbleToHexChar ((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 Determines if a Unicode character is a hexadecimal digit.
201 The test is case insensitive.
202
203 @param Digit Pointer to byte that receives the value of the hex
204 character.
205 @param Char Unicode character to test.
206
207 @retval TRUE If the character is a hexadecimal digit.
208 @retval FALSE Otherwise.
209
210 **/
211 BOOLEAN
212 R8_IsHexDigit (
213 OUT UINT8 *Digit,
214 IN CHAR16 Char
215 )
216 {
217 //
218 // Porting Guide:
219 // This library interface is simply obsolete.
220 // Include the source code to user code.
221 //
222
223 if ((Char >= L'0') && (Char <= L'9')) {
224 *Digit = (UINT8) (Char - L'0');
225 return TRUE;
226 }
227
228 if ((Char >= L'A') && (Char <= L'F')) {
229 *Digit = (UINT8) (Char - L'A' + 0x0A);
230 return TRUE;
231 }
232
233 if ((Char >= L'a') && (Char <= L'f')) {
234 *Digit = (UINT8) (Char - L'a' + 0x0A);
235 return TRUE;
236 }
237
238 return FALSE;
239 }
240
241