]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/String.c
Synchronize function comment in MdePkg\Library\BaseLib.h with the instance of this...
[mirror_edk2.git] / MdePkg / Library / BaseLib / String.c
CommitLineData
e1f414b6 1/** @file\r
2 Unicode and ASCII string primatives.\r
3\r
24dcb5e5 4 Copyright (c) 2006 - 2008, Intel Corporation<BR>\r
e1f414b6 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
e1f414b6 13**/\r
14\r
e1f414b6 15#include "BaseLibInternals.h"\r
16\r
24dcb5e5 17#define QUOTIENT_MAX_UINTN_DIVIDED_BY_10 ((UINTN) -1 / 10)\r
18#define REMAINDER_MAX_UINTN_DIVIDED_BY_10 ((UINTN) -1 % 10)\r
19\r
20#define QUOTIENT_MAX_UINTN_DIVIDED_BY_16 ((UINTN) -1 / 16)\r
21#define REMAINDER_MAX_UINTN_DIVIDED_BY_16 ((UINTN) -1 % 16)\r
22\r
23#define QUOTIENT_MAX_UINT64_DIVIDED_BY_10 ((UINT64) -1 / 10)\r
24#define REMAINDER_MAX_UINT64_DIVIDED_BY_10 ((UINT64) -1 % 10)\r
25\r
26#define QUOTIENT_MAX_UINT64_DIVIDED_BY_16 ((UINT64) -1 / 16)\r
27#define REMAINDER_MAX_UINT64_DIVIDED_BY_16 ((UINT64) -1 % 16)\r
28\r
e1f414b6 29/**\r
30 Copies one Null-terminated Unicode string to another Null-terminated Unicode\r
31 string and returns the new Unicode string.\r
32\r
33 This function copies the contents of the Unicode string Source to the Unicode\r
34 string Destination, and returns Destination. If Source and Destination\r
35 overlap, then the results are undefined.\r
36\r
37 If Destination is NULL, then ASSERT().\r
38 If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
39 If Source is NULL, then ASSERT().\r
40 If Source is not aligned on a 16-bit boundary, then ASSERT().\r
41 If Source and Destination overlap, then ASSERT().\r
42 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
9aa049d9 43 PcdMaximumUnicodeStringLength Unicode characters not including the\r
e1f414b6 44 Null-terminator, then ASSERT().\r
45\r
46 @param Destination Pointer to a Null-terminated Unicode string.\r
47 @param Source Pointer to a Null-terminated Unicode string.\r
48\r
9aa049d9 49 @return Destination.\r
e1f414b6 50\r
51**/\r
52CHAR16 *\r
53EFIAPI\r
54StrCpy (\r
55 OUT CHAR16 *Destination,\r
56 IN CONST CHAR16 *Source\r
57 )\r
58{\r
59 CHAR16 *ReturnValue;\r
60\r
61 //\r
62 // Destination cannot be NULL\r
63 //\r
64 ASSERT (Destination != NULL);\r
24dcb5e5 65 ASSERT (((UINTN) Destination & BIT0) == 0);\r
e1f414b6 66\r
67 //\r
68 // Destination and source cannot overlap\r
69 //\r
70 ASSERT ((UINTN)(Destination - Source) > StrLen (Source));\r
71 ASSERT ((UINTN)(Source - Destination) > StrLen (Source));\r
72\r
73 ReturnValue = Destination;\r
42eedea9 74 while (*Source != 0) {\r
e1f414b6 75 *(Destination++) = *(Source++);\r
76 }\r
77 *Destination = 0;\r
78 return ReturnValue;\r
79}\r
80\r
81/**\r
9aa049d9 82 Copies up to a specified length from one Null-terminated Unicode string to \r
83 another Null-terminated Unicode string and returns the new Unicode string.\r
e1f414b6 84\r
85 This function copies the contents of the Unicode string Source to the Unicode\r
86 string Destination, and returns Destination. At most, Length Unicode\r
87 characters are copied from Source to Destination. If Length is 0, then\r
88 Destination is returned unmodified. If Length is greater that the number of\r
89 Unicode characters in Source, then Destination is padded with Null Unicode\r
90 characters. If Source and Destination overlap, then the results are\r
91 undefined.\r
92\r
93 If Length > 0 and Destination is NULL, then ASSERT().\r
94 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().\r
95 If Length > 0 and Source is NULL, then ASSERT().\r
96 If Length > 0 and Source is not aligned on a 16-bit bounadry, then ASSERT().\r
97 If Source and Destination overlap, then ASSERT().\r
98 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
9aa049d9 99 PcdMaximumUnicodeStringLength Unicode characters not including the\r
e1f414b6 100 Null-terminator, then ASSERT().\r
101\r
102 @param Destination Pointer to a Null-terminated Unicode string.\r
103 @param Source Pointer to a Null-terminated Unicode string.\r
104 @param Length Maximum number of Unicode characters to copy.\r
105\r
9aa049d9 106 @return Destination.\r
e1f414b6 107\r
108**/\r
109CHAR16 *\r
110EFIAPI\r
111StrnCpy (\r
112 OUT CHAR16 *Destination,\r
113 IN CONST CHAR16 *Source,\r
114 IN UINTN Length\r
115 )\r
116{\r
117 CHAR16 *ReturnValue;\r
118\r
119 if (Length == 0) {\r
120 return Destination;\r
121 }\r
122\r
123 //\r
124 // Destination cannot be NULL if Length is not zero\r
125 //\r
126 ASSERT (Destination != NULL);\r
24dcb5e5 127 ASSERT (((UINTN) Destination & BIT0) == 0);\r
e1f414b6 128\r
129 //\r
130 // Destination and source cannot overlap\r
e1f414b6 131 //\r
132 ASSERT ((UINTN)(Destination - Source) > StrLen (Source));\r
133 ASSERT ((UINTN)(Source - Destination) >= Length);\r
134\r
135 ReturnValue = Destination;\r
136\r
137 while ((*Source != L'\0') && (Length > 0)) {\r
138 *(Destination++) = *(Source++);\r
139 Length--;\r
140 }\r
141\r
142 ZeroMem (Destination, Length * sizeof (*Destination));\r
143 return ReturnValue;\r
144}\r
145\r
146/**\r
147 Returns the length of a Null-terminated Unicode string.\r
148\r
149 This function returns the number of Unicode characters in the Null-terminated\r
150 Unicode string specified by String.\r
151\r
152 If String is NULL, then ASSERT().\r
153 If String is not aligned on a 16-bit boundary, then ASSERT().\r
154 If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
9aa049d9 155 PcdMaximumUnicodeStringLength Unicode characters not including the\r
e1f414b6 156 Null-terminator, then ASSERT().\r
157\r
158 @param String Pointer to a Null-terminated Unicode string.\r
159\r
160 @return The length of String.\r
161\r
162**/\r
163UINTN\r
164EFIAPI\r
165StrLen (\r
166 IN CONST CHAR16 *String\r
167 )\r
168{\r
169 UINTN Length;\r
170\r
171 ASSERT (String != NULL);\r
24dcb5e5 172 ASSERT (((UINTN) String & BIT0) == 0);\r
e1f414b6 173\r
174 for (Length = 0; *String != L'\0'; String++, Length++) {\r
175 //\r
176 // If PcdMaximumUnicodeStringLength is not zero,\r
177 // length should not more than PcdMaximumUnicodeStringLength\r
178 //\r
179 if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r
180 ASSERT (Length < PcdGet32 (PcdMaximumUnicodeStringLength));\r
181 }\r
182 }\r
183 return Length;\r
184}\r
185\r
186/**\r
187 Returns the size of a Null-terminated Unicode string in bytes, including the\r
188 Null terminator.\r
189\r
9aa049d9 190 This function returns the size, in bytes, of the Null-terminated Unicode string \r
191 specified by String.\r
e1f414b6 192\r
193 If String is NULL, then ASSERT().\r
194 If String is not aligned on a 16-bit boundary, then ASSERT().\r
195 If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
9aa049d9 196 PcdMaximumUnicodeStringLength Unicode characters not including the\r
e1f414b6 197 Null-terminator, then ASSERT().\r
198\r
199 @param String Pointer to a Null-terminated Unicode string.\r
200\r
9aa049d9 201 @return The size of String.\r
e1f414b6 202\r
203**/\r
204UINTN\r
205EFIAPI\r
206StrSize (\r
207 IN CONST CHAR16 *String\r
208 )\r
209{\r
210 return (StrLen (String) + 1) * sizeof (*String);\r
211}\r
212\r
213/**\r
214 Compares two Null-terminated Unicode strings, and returns the difference\r
215 between the first mismatched Unicode characters.\r
216\r
217 This function compares the Null-terminated Unicode string FirstString to the\r
218 Null-terminated Unicode string SecondString. If FirstString is identical to\r
219 SecondString, then 0 is returned. Otherwise, the value returned is the first\r
220 mismatched Unicode character in SecondString subtracted from the first\r
221 mismatched Unicode character in FirstString.\r
222\r
223 If FirstString is NULL, then ASSERT().\r
224 If FirstString is not aligned on a 16-bit boundary, then ASSERT().\r
225 If SecondString is NULL, then ASSERT().\r
226 If SecondString is not aligned on a 16-bit boundary, then ASSERT().\r
227 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r
9aa049d9 228 than PcdMaximumUnicodeStringLength Unicode characters not including the\r
e1f414b6 229 Null-terminator, then ASSERT().\r
230 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r
9aa049d9 231 than PcdMaximumUnicodeStringLength Unicode characters not including the\r
e1f414b6 232 Null-terminator, then ASSERT().\r
233\r
234 @param FirstString Pointer to a Null-terminated Unicode string.\r
235 @param SecondString Pointer to a Null-terminated Unicode string.\r
236\r
1106ffe1 237 @retval 0 FirstString is identical to SecondString.\r
9aa049d9 238 @return others FirstString is not identical to SecondString.\r
e1f414b6 239\r
240**/\r
241INTN\r
242EFIAPI\r
243StrCmp (\r
244 IN CONST CHAR16 *FirstString,\r
245 IN CONST CHAR16 *SecondString\r
246 )\r
247{\r
248 //\r
249 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength\r
250 //\r
251 ASSERT (StrSize (FirstString) != 0);\r
252 ASSERT (StrSize (SecondString) != 0);\r
253\r
254 while ((*FirstString != L'\0') && (*FirstString == *SecondString)) {\r
255 FirstString++;\r
256 SecondString++;\r
257 }\r
258 return *FirstString - *SecondString;\r
259}\r
260\r
261/**\r
9aa049d9 262 Compares up to a specified length the contents of two Null-terminated Unicode strings,\r
263 and returns the difference between the first mismatched Unicode characters.\r
e1f414b6 264\r
265 This function compares the Null-terminated Unicode string FirstString to the\r
266 Null-terminated Unicode string SecondString. At most, Length Unicode\r
267 characters will be compared. If Length is 0, then 0 is returned. If\r
268 FirstString is identical to SecondString, then 0 is returned. Otherwise, the\r
269 value returned is the first mismatched Unicode character in SecondString\r
270 subtracted from the first mismatched Unicode character in FirstString.\r
271\r
272 If Length > 0 and FirstString is NULL, then ASSERT().\r
273 If Length > 0 and FirstString is not aligned on a 16-bit bounadary, then ASSERT().\r
274 If Length > 0 and SecondString is NULL, then ASSERT().\r
275 If Length > 0 and SecondString is not aligned on a 16-bit bounadary, then ASSERT().\r
276 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r
277 than PcdMaximumUnicodeStringLength Unicode characters not including the\r
278 Null-terminator, then ASSERT().\r
279 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r
280 than PcdMaximumUnicodeStringLength Unicode characters not including the\r
281 Null-terminator, then ASSERT().\r
282\r
283 @param FirstString Pointer to a Null-terminated Unicode string.\r
284 @param SecondString Pointer to a Null-terminated Unicode string.\r
285 @param Length Maximum number of Unicode characters to compare.\r
286\r
1106ffe1 287 @retval 0 FirstString is identical to SecondString.\r
9aa049d9 288 @return others FirstString is not identical to SecondString.\r
e1f414b6 289\r
290**/\r
291INTN\r
292EFIAPI\r
293StrnCmp (\r
294 IN CONST CHAR16 *FirstString,\r
295 IN CONST CHAR16 *SecondString,\r
296 IN UINTN Length\r
297 )\r
298{\r
2bfb6009 299 if (Length == 0) {\r
e1f414b6 300 return 0;\r
301 }\r
302\r
303 //\r
304 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.\r
305 // Length tests are performed inside StrLen().\r
306 //\r
307 ASSERT (StrSize (FirstString) != 0);\r
308 ASSERT (StrSize (SecondString) != 0);\r
309\r
310 while ((*FirstString != L'\0') &&\r
311 (*FirstString == *SecondString) &&\r
312 (Length > 1)) {\r
313 FirstString++;\r
314 SecondString++;\r
315 Length--;\r
316 }\r
317\r
318 return *FirstString - *SecondString;\r
319}\r
320\r
321/**\r
322 Concatenates one Null-terminated Unicode string to another Null-terminated\r
323 Unicode string, and returns the concatenated Unicode string.\r
324\r
325 This function concatenates two Null-terminated Unicode strings. The contents\r
326 of Null-terminated Unicode string Source are concatenated to the end of\r
327 Null-terminated Unicode string Destination. The Null-terminated concatenated\r
328 Unicode String is returned. If Source and Destination overlap, then the\r
329 results are undefined.\r
330\r
331 If Destination is NULL, then ASSERT().\r
9aa049d9 332 If Destination is not aligned on a 16-bit bounadary, then ASSERT().\r
e1f414b6 333 If Source is NULL, then ASSERT().\r
9aa049d9 334 If Source is not aligned on a 16-bit bounadary, then ASSERT().\r
e1f414b6 335 If Source and Destination overlap, then ASSERT().\r
336 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
337 than PcdMaximumUnicodeStringLength Unicode characters not including the\r
338 Null-terminator, then ASSERT().\r
339 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
340 PcdMaximumUnicodeStringLength Unicode characters not including the\r
341 Null-terminator, then ASSERT().\r
342 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r
343 and Source results in a Unicode string with more than\r
344 PcdMaximumUnicodeStringLength Unicode characters not including the\r
345 Null-terminator, then ASSERT().\r
346\r
347 @param Destination Pointer to a Null-terminated Unicode string.\r
348 @param Source Pointer to a Null-terminated Unicode string.\r
349\r
9aa049d9 350 @return Destination.\r
e1f414b6 351\r
352**/\r
353CHAR16 *\r
354EFIAPI\r
355StrCat (\r
356 IN OUT CHAR16 *Destination,\r
357 IN CONST CHAR16 *Source\r
358 )\r
359{\r
360 StrCpy (Destination + StrLen (Destination), Source);\r
361\r
362 //\r
363 // Size of the resulting string should never be zero.\r
364 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
365 //\r
366 ASSERT (StrSize (Destination) != 0);\r
367 return Destination;\r
368}\r
369\r
370/**\r
9aa049d9 371 Concatenates up to a specified length one Null-terminated Unicode to the end \r
372 of another Null-terminated Unicode string, and returns the concatenated \r
e1f414b6 373 Unicode string.\r
374\r
375 This function concatenates two Null-terminated Unicode strings. The contents\r
376 of Null-terminated Unicode string Source are concatenated to the end of\r
377 Null-terminated Unicode string Destination, and Destination is returned. At\r
378 most, Length Unicode characters are concatenated from Source to the end of\r
379 Destination, and Destination is always Null-terminated. If Length is 0, then\r
380 Destination is returned unmodified. If Source and Destination overlap, then\r
381 the results are undefined.\r
382\r
383 If Destination is NULL, then ASSERT().\r
384 If Length > 0 and Destination is not aligned on a 16-bit boundary, then ASSERT().\r
385 If Length > 0 and Source is NULL, then ASSERT().\r
386 If Length > 0 and Source is not aligned on a 16-bit boundary, then ASSERT().\r
387 If Source and Destination overlap, then ASSERT().\r
388 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r
389 than PcdMaximumUnicodeStringLength Unicode characters not including the\r
390 Null-terminator, then ASSERT().\r
391 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
392 PcdMaximumUnicodeStringLength Unicode characters not including the\r
393 Null-terminator, then ASSERT().\r
394 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r
395 and Source results in a Unicode string with more than\r
396 PcdMaximumUnicodeStringLength Unicode characters not including the\r
397 Null-terminator, then ASSERT().\r
398\r
399 @param Destination Pointer to a Null-terminated Unicode string.\r
400 @param Source Pointer to a Null-terminated Unicode string.\r
401 @param Length Maximum number of Unicode characters to concatenate from\r
402 Source.\r
403\r
9aa049d9 404 @return Destination.\r
e1f414b6 405\r
406**/\r
407CHAR16 *\r
408EFIAPI\r
409StrnCat (\r
410 IN OUT CHAR16 *Destination,\r
411 IN CONST CHAR16 *Source,\r
412 IN UINTN Length\r
413 )\r
414{\r
415 StrnCpy (Destination + StrLen (Destination), Source, Length);\r
416\r
417 //\r
418 // Size of the resulting string should never be zero.\r
419 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
420 //\r
421 ASSERT (StrSize (Destination) != 0);\r
422 return Destination;\r
423}\r
424\r
425/**\r
9aa049d9 426 Returns the first occurrence of a Null-terminated Unicode sub-string\r
e1f414b6 427 in a Null-terminated Unicode string.\r
428\r
9aa049d9 429 This function scans the contents of the Null-terminated Unicode string\r
430 specified by String and returns the first occurrence of SearchString.\r
431 If SearchString is not found in String, then NULL is returned. If\r
432 the length of SearchString is zero, then String is\r
e1f414b6 433 returned.\r
9aa049d9 434\r
e1f414b6 435 If String is NULL, then ASSERT().\r
436 If String is not aligned on a 16-bit boundary, then ASSERT().\r
437 If SearchString is NULL, then ASSERT().\r
438 If SearchString is not aligned on a 16-bit boundary, then ASSERT().\r
439\r
9aa049d9 440 If PcdMaximumUnicodeStringLength is not zero, and SearchString\r
441 or String contains more than PcdMaximumUnicodeStringLength Unicode\r
e1f414b6 442 characters not including the Null-terminator, then ASSERT().\r
443\r
9aa049d9 444 @param String Pointer to a Null-terminated Unicode string.\r
445 @param SearchString Pointer to a Null-terminated Unicode string to search for.\r
e1f414b6 446\r
9aa049d9 447 @retval NULL If the SearchString does not appear in String.\r
448 @return others If there is a match.\r
e1f414b6 449\r
450**/\r
451CHAR16 *\r
452EFIAPI\r
453StrStr (\r
4df26661 454 IN CONST CHAR16 *String,\r
455 IN CONST CHAR16 *SearchString\r
e1f414b6 456 )\r
457{\r
458 CONST CHAR16 *FirstMatch;\r
459 CONST CHAR16 *SearchStringTmp;\r
460\r
e1f414b6 461 //\r
4df26661 462 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.\r
463 // Length tests are performed inside StrLen().\r
e1f414b6 464 //\r
4df26661 465 ASSERT (StrSize (String) != 0);\r
466 ASSERT (StrSize (SearchString) != 0);\r
e1f414b6 467\r
468 while (*String != '\0') {\r
469 SearchStringTmp = SearchString;\r
470 FirstMatch = String;\r
471 \r
472 while ((*String == *SearchStringTmp) \r
473 && (*SearchStringTmp != '\0') \r
474 && (*String != '\0')) {\r
475 String++;\r
476 SearchStringTmp++;\r
477 } \r
478 \r
955c32f2 479 if (*SearchStringTmp == '\0') {\r
e1f414b6 480 return (CHAR16 *) FirstMatch;\r
481 }\r
482\r
483 if (SearchStringTmp == SearchString) {\r
484 //\r
485 // If no character from SearchString match,\r
486 // move the pointer to the String under search\r
487 // by one character.\r
488 //\r
489 String++;\r
490 }\r
491 }\r
492\r
493 return NULL;\r
494}\r
495\r
496/**\r
497 Check if a Unicode character is a decimal character.\r
498\r
499 This internal function checks if a Unicode character is a \r
500 decimal character. The valid decimal character is from\r
501 L'0' to L'9'.\r
502\r
e1f414b6 503 @param Char The character to check against.\r
504\r
505 @retval TRUE If the Char is a decmial character.\r
24dcb5e5 506 @retval FALSE If the Char is not a decmial character.\r
e1f414b6 507\r
508**/\r
e1f414b6 509BOOLEAN\r
42eedea9 510EFIAPI\r
e1f414b6 511InternalIsDecimalDigitCharacter (\r
512 IN CHAR16 Char\r
513 )\r
514{\r
515 return (BOOLEAN) (Char >= L'0' && Char <= L'9');\r
516}\r
517\r
518/**\r
519 Convert a Unicode character to upper case only if \r
520 it maps to a valid small-case ASCII character.\r
521\r
522 This internal function only deal with Unicode character\r
24dcb5e5 523 which maps to a valid small-case ASCII character, i.e.\r
e1f414b6 524 L'a' to L'z'. For other Unicode character, the input character\r
525 is returned directly.\r
526\r
e1f414b6 527 @param Char The character to convert.\r
528\r
529 @retval LowerCharacter If the Char is with range L'a' to L'z'.\r
530 @retval Unchanged Otherwise.\r
531\r
532**/\r
e1f414b6 533CHAR16\r
42eedea9 534EFIAPI\r
e1f414b6 535InternalCharToUpper (\r
536 IN CHAR16 Char\r
537 )\r
538{\r
539 if (Char >= L'a' && Char <= L'z') {\r
540 return (CHAR16) (Char - (L'a' - L'A'));\r
541 }\r
542\r
543 return Char;\r
544}\r
545\r
546/**\r
547 Convert a Unicode character to numerical value.\r
548\r
549 This internal function only deal with Unicode character\r
550 which maps to a valid hexadecimal ASII character, i.e.\r
551 L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other \r
552 Unicode character, the value returned does not make sense.\r
553\r
554 @param Char The character to convert.\r
555\r
24dcb5e5 556 @return The numerical value converted.\r
e1f414b6 557\r
558**/\r
e1f414b6 559UINTN\r
42eedea9 560EFIAPI\r
e1f414b6 561InternalHexCharToUintn (\r
562 IN CHAR16 Char\r
563 )\r
564{\r
565 if (InternalIsDecimalDigitCharacter (Char)) {\r
566 return Char - L'0';\r
567 }\r
568\r
569 return (UINTN) (10 + InternalCharToUpper (Char) - L'A');\r
570}\r
571\r
572/**\r
573 Check if a Unicode character is a hexadecimal character.\r
574\r
575 This internal function checks if a Unicode character is a \r
576 decimal character. The valid hexadecimal character is \r
577 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
578\r
579\r
580 @param Char The character to check against.\r
581\r
582 @retval TRUE If the Char is a hexadecmial character.\r
24dcb5e5 583 @retval FALSE If the Char is not a hexadecmial character.\r
e1f414b6 584\r
585**/\r
e1f414b6 586BOOLEAN\r
42eedea9 587EFIAPI\r
e1f414b6 588InternalIsHexaDecimalDigitCharacter (\r
589 IN CHAR16 Char\r
590 )\r
591{\r
592\r
593 return (BOOLEAN) (InternalIsDecimalDigitCharacter (Char) ||\r
594 (Char >= L'A' && Char <= L'F') ||\r
595 (Char >= L'a' && Char <= L'f'));\r
596}\r
597\r
598/**\r
9aa049d9 599 Convert a Null-terminated Unicode decimal string to a value of\r
e1f414b6 600 type UINTN.\r
601\r
9aa049d9 602 This function returns a value of type UINTN by interpreting the contents\r
603 of the Unicode string specified by String as a decimal number. The format\r
e1f414b6 604 of the input Unicode string String is:\r
9aa049d9 605\r
606 [spaces] [decimal digits].\r
607\r
608 The valid decimal digit character is in the range [0-9]. The\r
609 function will ignore the pad space, which includes spaces or\r
610 tab characters, before [decimal digits]. The running zero in the\r
611 beginning of [decimal digits] will be ignored. Then, the function\r
612 stops at the first character that is a not a valid decimal character\r
613 or a Null-terminator, whichever one comes first.\r
614\r
e1f414b6 615 If String is NULL, then ASSERT().\r
9aa049d9 616 If String is not aligned in a 16-bit boundary, then ASSERT().\r
e1f414b6 617 If String has only pad spaces, then 0 is returned.\r
9aa049d9 618 If String has no pad spaces or valid decimal digits,\r
e1f414b6 619 then 0 is returned.\r
9aa049d9 620 If the number represented by String overflows according\r
e1f414b6 621 to the range defined by UINTN, then ASSERT().\r
9aa049d9 622\r
623 If PcdMaximumUnicodeStringLength is not zero, and String contains\r
624 more than PcdMaximumUnicodeStringLength Unicode characters not including\r
e1f414b6 625 the Null-terminator, then ASSERT().\r
626\r
9aa049d9 627 @param String Pointer to a Null-terminated Unicode string.\r
e1f414b6 628\r
9aa049d9 629 @retval Value translated from String.\r
e1f414b6 630\r
631**/\r
632UINTN\r
633EFIAPI\r
634StrDecimalToUintn (\r
4df26661 635 IN CONST CHAR16 *String\r
e1f414b6 636 )\r
637{\r
638 UINTN Result;\r
639 \r
4df26661 640 //\r
641 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
642 // Length tests are performed inside StrLen().\r
643 //\r
644 ASSERT (StrSize (String) != 0);\r
e1f414b6 645\r
646 //\r
647 // Ignore the pad spaces (space or tab)\r
648 //\r
955c32f2 649 while ((*String == L' ') || (*String == L'\t')) {\r
e1f414b6 650 String++;\r
651 }\r
652\r
653 //\r
654 // Ignore leading Zeros after the spaces\r
655 //\r
955c32f2 656 while (*String == L'0') {\r
e1f414b6 657 String++;\r
658 }\r
659\r
660 Result = 0;\r
661\r
662 while (InternalIsDecimalDigitCharacter (*String)) {\r
663 //\r
664 // If the number represented by String overflows according \r
665 // to the range defined by UINTN, then ASSERT().\r
666 //\r
24dcb5e5 667 ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_10) ||\r
955c32f2 668 ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_10) &&\r
24dcb5e5 669 (*String - L'0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10)\r
e1f414b6 670 );\r
671\r
672 Result = Result * 10 + (*String - L'0');\r
673 String++;\r
674 }\r
675 \r
676 return Result;\r
677}\r
678\r
679\r
680/**\r
9aa049d9 681 Convert a Null-terminated Unicode decimal string to a value of\r
e1f414b6 682 type UINT64.\r
683\r
9aa049d9 684 This function returns a value of type UINT64 by interpreting the contents\r
685 of the Unicode string specified by String as a decimal number. The format\r
e1f414b6 686 of the input Unicode string String is:\r
9aa049d9 687\r
688 [spaces] [decimal digits].\r
689\r
690 The valid decimal digit character is in the range [0-9]. The\r
691 function will ignore the pad space, which includes spaces or\r
692 tab characters, before [decimal digits]. The running zero in the\r
693 beginning of [decimal digits] will be ignored. Then, the function\r
694 stops at the first character that is a not a valid decimal character\r
695 or a Null-terminator, whichever one comes first.\r
696\r
e1f414b6 697 If String is NULL, then ASSERT().\r
9aa049d9 698 If String is not aligned in a 16-bit boundary, then ASSERT().\r
e1f414b6 699 If String has only pad spaces, then 0 is returned.\r
9aa049d9 700 If String has no pad spaces or valid decimal digits,\r
e1f414b6 701 then 0 is returned.\r
9aa049d9 702 If the number represented by String overflows according\r
e1f414b6 703 to the range defined by UINT64, then ASSERT().\r
9aa049d9 704\r
705 If PcdMaximumUnicodeStringLength is not zero, and String contains\r
706 more than PcdMaximumUnicodeStringLength Unicode characters not including\r
e1f414b6 707 the Null-terminator, then ASSERT().\r
708\r
9aa049d9 709 @param String Pointer to a Null-terminated Unicode string.\r
e1f414b6 710\r
9aa049d9 711 @retval Value translated from String.\r
e1f414b6 712\r
713**/\r
714UINT64\r
715EFIAPI\r
716StrDecimalToUint64 (\r
4df26661 717 IN CONST CHAR16 *String\r
e1f414b6 718 )\r
719{\r
720 UINT64 Result;\r
721 \r
4df26661 722 //\r
723 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
724 // Length tests are performed inside StrLen().\r
725 //\r
726 ASSERT (StrSize (String) != 0);\r
e1f414b6 727\r
728 //\r
729 // Ignore the pad spaces (space or tab)\r
730 //\r
955c32f2 731 while ((*String == L' ') || (*String == L'\t')) {\r
e1f414b6 732 String++;\r
733 }\r
734\r
735 //\r
736 // Ignore leading Zeros after the spaces\r
737 //\r
955c32f2 738 while (*String == L'0') {\r
e1f414b6 739 String++;\r
740 }\r
741\r
742 Result = 0;\r
743\r
744 while (InternalIsDecimalDigitCharacter (*String)) {\r
745 //\r
746 // If the number represented by String overflows according \r
747 // to the range defined by UINTN, then ASSERT().\r
748 //\r
24dcb5e5 749 ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_10) || \r
955c32f2 750 ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_10) && \r
24dcb5e5 751 (*String - L'0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10)\r
e1f414b6 752 );\r
753\r
754 Result = MultU64x32 (Result, 10) + (*String - L'0');\r
755 String++;\r
756 }\r
757 \r
758 return Result;\r
759}\r
760\r
761/**\r
762 Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.\r
763\r
9aa049d9 764 This function returns a value of type UINTN by interpreting the contents\r
765 of the Unicode string specified by String as a hexadecimal number.\r
e1f414b6 766 The format of the input Unicode string String is:\r
9aa049d9 767\r
768 [spaces][zeros][x][hexadecimal digits].\r
769\r
770 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
771 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
772 If "x" appears in the input string, it must be prefixed with at least one 0.\r
773 The function will ignore the pad space, which includes spaces or tab characters,\r
774 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
775 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
776 first valid hexadecimal digit. Then, the function stops at the first character that is\r
e1f414b6 777 a not a valid hexadecimal character or NULL, whichever one comes first.\r
778\r
779 If String is NULL, then ASSERT().\r
780 If String is not aligned in a 16-bit boundary, then ASSERT().\r
781 If String has only pad spaces, then zero is returned.\r
9aa049d9 782 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
e1f414b6 783 then zero is returned.\r
9aa049d9 784 If the number represented by String overflows according to the range defined by\r
e1f414b6 785 UINTN, then ASSERT().\r
786\r
9aa049d9 787 If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
788 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,\r
e1f414b6 789 then ASSERT().\r
790\r
9aa049d9 791 @param String Pointer to a Null-terminated Unicode string.\r
e1f414b6 792\r
9aa049d9 793 @retval Value translated from String.\r
e1f414b6 794\r
795**/\r
796UINTN\r
797EFIAPI\r
798StrHexToUintn (\r
4df26661 799 IN CONST CHAR16 *String\r
e1f414b6 800 )\r
801{\r
802 UINTN Result;\r
803\r
4df26661 804 //\r
805 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
806 // Length tests are performed inside StrLen().\r
807 //\r
808 ASSERT (StrSize (String) != 0);\r
e1f414b6 809 \r
810 //\r
811 // Ignore the pad spaces (space or tab) \r
812 //\r
955c32f2 813 while ((*String == L' ') || (*String == L'\t')) {\r
e1f414b6 814 String++;\r
815 }\r
816\r
817 //\r
818 // Ignore leading Zeros after the spaces\r
819 //\r
955c32f2 820 while (*String == L'0') {\r
e1f414b6 821 String++;\r
822 }\r
823\r
824 if (InternalCharToUpper (*String) == L'X') {\r
955c32f2 825 ASSERT (*(String - 1) == L'0');\r
826 if (*(String - 1) != L'0') {\r
e1f414b6 827 return 0;\r
828 }\r
829 //\r
830 // Skip the 'X'\r
831 //\r
832 String++;\r
833 }\r
834\r
835 Result = 0;\r
836 \r
837 while (InternalIsHexaDecimalDigitCharacter (*String)) {\r
838 //\r
839 // If the Hex Number represented by String overflows according \r
840 // to the range defined by UINTN, then ASSERT().\r
841 //\r
24dcb5e5 842 ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_16) ||\r
955c32f2 843 ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_16) && \r
24dcb5e5 844 (InternalHexCharToUintn (*String) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16))\r
e1f414b6 845 );\r
846\r
847 Result = (Result << 4) + InternalHexCharToUintn (*String);\r
848 String++;\r
849 }\r
850\r
851 return Result;\r
852}\r
853\r
854\r
855/**\r
856 Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.\r
857\r
9aa049d9 858 This function returns a value of type UINT64 by interpreting the contents\r
859 of the Unicode string specified by String as a hexadecimal number.\r
860 The format of the input Unicode string String is\r
861\r
862 [spaces][zeros][x][hexadecimal digits].\r
863\r
864 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
865 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.\r
866 If "x" appears in the input string, it must be prefixed with at least one 0.\r
867 The function will ignore the pad space, which includes spaces or tab characters,\r
868 before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or\r
869 [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the\r
870 first valid hexadecimal digit. Then, the function stops at the first character that is\r
e1f414b6 871 a not a valid hexadecimal character or NULL, whichever one comes first.\r
872\r
873 If String is NULL, then ASSERT().\r
874 If String is not aligned in a 16-bit boundary, then ASSERT().\r
875 If String has only pad spaces, then zero is returned.\r
9aa049d9 876 If String has no leading pad spaces, leading zeros or valid hexadecimal digits,\r
e1f414b6 877 then zero is returned.\r
9aa049d9 878 If the number represented by String overflows according to the range defined by\r
e1f414b6 879 UINT64, then ASSERT().\r
880\r
9aa049d9 881 If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r
882 PcdMaximumUnicodeStringLength Unicode characters not including the Null-terminator,\r
e1f414b6 883 then ASSERT().\r
884\r
9aa049d9 885 @param String Pointer to a Null-terminated Unicode string.\r
e1f414b6 886\r
9aa049d9 887 @retval Value translated from String.\r
e1f414b6 888\r
9aa049d9 889 **/\r
e1f414b6 890UINT64\r
891EFIAPI\r
892StrHexToUint64 (\r
4df26661 893 IN CONST CHAR16 *String\r
e1f414b6 894 )\r
895{\r
896 UINT64 Result;\r
897\r
4df26661 898 //\r
899 // ASSERT String is less long than PcdMaximumUnicodeStringLength.\r
900 // Length tests are performed inside StrLen().\r
901 //\r
902 ASSERT (StrSize (String) != 0);\r
e1f414b6 903 \r
904 //\r
905 // Ignore the pad spaces (space or tab) \r
906 //\r
955c32f2 907 while ((*String == L' ') || (*String == L'\t')) {\r
e1f414b6 908 String++;\r
909 }\r
910\r
911 //\r
912 // Ignore leading Zeros after the spaces\r
913 //\r
955c32f2 914 while (*String == L'0') {\r
e1f414b6 915 String++;\r
916 }\r
917\r
918 if (InternalCharToUpper (*String) == L'X') {\r
955c32f2 919 ASSERT (*(String - 1) == L'0');\r
920 if (*(String - 1) != L'0') {\r
e1f414b6 921 return 0;\r
922 }\r
923 //\r
924 // Skip the 'X'\r
925 //\r
926 String++;\r
927 }\r
928\r
929 Result = 0;\r
930 \r
931 while (InternalIsHexaDecimalDigitCharacter (*String)) {\r
932 //\r
933 // If the Hex Number represented by String overflows according \r
934 // to the range defined by UINTN, then ASSERT().\r
935 //\r
24dcb5e5 936 ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_16)|| \r
955c32f2 937 ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_16) && \r
24dcb5e5 938 (InternalHexCharToUintn (*String) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16))\r
e1f414b6 939 );\r
940\r
941 Result = LShiftU64 (Result, 4);\r
942 Result = Result + InternalHexCharToUintn (*String);\r
943 String++;\r
944 }\r
945\r
946 return Result;\r
947}\r
948\r
949/**\r
950 Check if a ASCII character is a decimal character.\r
951\r
952 This internal function checks if a Unicode character is a \r
953 decimal character. The valid decimal character is from\r
954 '0' to '9'.\r
955\r
956 @param Char The character to check against.\r
957\r
958 @retval TRUE If the Char is a decmial character.\r
24dcb5e5 959 @retval FALSE If the Char is not a decmial character.\r
e1f414b6 960\r
961**/\r
e1f414b6 962BOOLEAN\r
42eedea9 963EFIAPI\r
e1f414b6 964InternalAsciiIsDecimalDigitCharacter (\r
965 IN CHAR8 Char\r
966 )\r
967{\r
968 return (BOOLEAN) (Char >= '0' && Char <= '9');\r
969}\r
970\r
971/**\r
972 Check if a ASCII character is a hexadecimal character.\r
973\r
974 This internal function checks if a ASCII character is a \r
975 decimal character. The valid hexadecimal character is \r
976 L'0' to L'9', L'a' to L'f', or L'A' to L'F'.\r
977\r
978\r
979 @param Char The character to check against.\r
980\r
981 @retval TRUE If the Char is a hexadecmial character.\r
24dcb5e5 982 @retval FALSE If the Char is not a hexadecmial character.\r
e1f414b6 983\r
984**/\r
e1f414b6 985BOOLEAN\r
42eedea9 986EFIAPI\r
e1f414b6 987InternalAsciiIsHexaDecimalDigitCharacter (\r
988 IN CHAR8 Char\r
989 )\r
990{\r
991\r
992 return (BOOLEAN) (InternalAsciiIsDecimalDigitCharacter (Char) ||\r
993 (Char >= 'A' && Char <= 'F') ||\r
994 (Char >= 'a' && Char <= 'f'));\r
995}\r
996\r
997/**\r
9aa049d9 998 Convert a Null-terminated Unicode string to a Null-terminated\r
e1f414b6 999 ASCII string and returns the ASCII string.\r
9aa049d9 1000\r
1001 This function converts the content of the Unicode string Source\r
1002 to the ASCII string Destination by copying the lower 8 bits of\r
1003 each Unicode character. It returns Destination.\r
1004\r
1005 If any Unicode characters in Source contain non-zero value in\r
1006 the upper 8 bits, then ASSERT().\r
e1f414b6 1007\r
1008 If Destination is NULL, then ASSERT().\r
1009 If Source is NULL, then ASSERT().\r
1010 If Source is not aligned on a 16-bit boundary, then ASSERT().\r
1011 If Source and Destination overlap, then ASSERT().\r
1012\r
9aa049d9 1013 If PcdMaximumUnicodeStringLength is not zero, and Source contains\r
1014 more than PcdMaximumUnicodeStringLength Unicode characters not including\r
e1f414b6 1015 the Null-terminator, then ASSERT().\r
9aa049d9 1016\r
1017 If PcdMaximumAsciiStringLength is not zero, and Source contains more\r
1018 than PcdMaximumAsciiStringLength Unicode characters not including the\r
e1f414b6 1019 Null-terminator, then ASSERT().\r
1020\r
1021 @param Source Pointer to a Null-terminated Unicode string.\r
1022 @param Destination Pointer to a Null-terminated ASCII string.\r
1023\r
9aa049d9 1024 @return Destination.\r
e1f414b6 1025\r
1026**/\r
1027CHAR8 *\r
1028EFIAPI\r
1029UnicodeStrToAsciiStr (\r
4df26661 1030 IN CONST CHAR16 *Source,\r
1031 OUT CHAR8 *Destination\r
e1f414b6 1032 )\r
1033{\r
4df26661 1034 CHAR8 *ReturnValue;\r
1035\r
e1f414b6 1036 ASSERT (Destination != NULL);\r
4df26661 1037\r
1038 //\r
1039 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.\r
1040 // Length tests are performed inside StrLen().\r
1041 //\r
1042 ASSERT (StrSize (Source) != 0);\r
e1f414b6 1043\r
1044 //\r
1045 // Source and Destination should not overlap\r
1046 //\r
1047 ASSERT ((UINTN) ((CHAR16 *) Destination - Source) > StrLen (Source));\r
1048 ASSERT ((UINTN) ((CHAR8 *) Source - Destination) > StrLen (Source));\r
1049\r
e1f414b6 1050\r
4df26661 1051 ReturnValue = Destination;\r
e1f414b6 1052 while (*Source != '\0') {\r
1053 //\r
1054 // If any Unicode characters in Source contain \r
1055 // non-zero value in the upper 8 bits, then ASSERT().\r
1056 //\r
1057 ASSERT (*Source < 0x100);\r
1058 *(Destination++) = (CHAR8) *(Source++);\r
1059 }\r
1060\r
1061 *Destination = '\0';\r
4df26661 1062\r
1063 //\r
1064 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.\r
1065 // Length tests are performed inside AsciiStrLen().\r
1066 //\r
1067 ASSERT (AsciiStrSize (ReturnValue) != 0);\r
1068\r
1069 return ReturnValue;\r
e1f414b6 1070}\r
1071\r
1072\r
1073/**\r
1074 Copies one Null-terminated ASCII string to another Null-terminated ASCII\r
1075 string and returns the new ASCII string.\r
1076\r
1077 This function copies the contents of the ASCII string Source to the ASCII\r
1078 string Destination, and returns Destination. If Source and Destination\r
1079 overlap, then the results are undefined.\r
1080\r
1081 If Destination is NULL, then ASSERT().\r
1082 If Source is NULL, then ASSERT().\r
1083 If Source and Destination overlap, then ASSERT().\r
1084 If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
1085 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1086 then ASSERT().\r
1087\r
1088 @param Destination Pointer to a Null-terminated ASCII string.\r
1089 @param Source Pointer to a Null-terminated ASCII string.\r
1090\r
9aa049d9 1091 @return Destination\r
e1f414b6 1092\r
1093**/\r
1094CHAR8 *\r
1095EFIAPI\r
1096AsciiStrCpy (\r
1097 OUT CHAR8 *Destination,\r
1098 IN CONST CHAR8 *Source\r
1099 )\r
1100{\r
1101 CHAR8 *ReturnValue;\r
1102\r
1103 //\r
1104 // Destination cannot be NULL\r
1105 //\r
1106 ASSERT (Destination != NULL);\r
1107\r
1108 //\r
1109 // Destination and source cannot overlap\r
1110 //\r
1111 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r
1112 ASSERT ((UINTN)(Source - Destination) > AsciiStrLen (Source));\r
1113\r
1114 ReturnValue = Destination;\r
42eedea9 1115 while (*Source != 0) {\r
e1f414b6 1116 *(Destination++) = *(Source++);\r
1117 }\r
1118 *Destination = 0;\r
1119 return ReturnValue;\r
1120}\r
1121\r
1122/**\r
9aa049d9 1123 Copies up to a specified length one Null-terminated ASCII string to another \r
1124 Null-terminated ASCII string and returns the new ASCII string.\r
e1f414b6 1125\r
1126 This function copies the contents of the ASCII string Source to the ASCII\r
1127 string Destination, and returns Destination. At most, Length ASCII characters\r
1128 are copied from Source to Destination. If Length is 0, then Destination is\r
1129 returned unmodified. If Length is greater that the number of ASCII characters\r
1130 in Source, then Destination is padded with Null ASCII characters. If Source\r
1131 and Destination overlap, then the results are undefined.\r
1132\r
1133 If Destination is NULL, then ASSERT().\r
1134 If Source is NULL, then ASSERT().\r
1135 If Source and Destination overlap, then ASSERT().\r
1136 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
1137 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1138 then ASSERT().\r
1139\r
1140 @param Destination Pointer to a Null-terminated ASCII string.\r
1141 @param Source Pointer to a Null-terminated ASCII string.\r
1142 @param Length Maximum number of ASCII characters to copy.\r
1143\r
9aa049d9 1144 @return Destination\r
e1f414b6 1145\r
1146**/\r
1147CHAR8 *\r
1148EFIAPI\r
1149AsciiStrnCpy (\r
1150 OUT CHAR8 *Destination,\r
1151 IN CONST CHAR8 *Source,\r
1152 IN UINTN Length\r
1153 )\r
1154{\r
1155 CHAR8 *ReturnValue;\r
1156\r
2bfb6009 1157 if (Length == 0) {\r
e1f414b6 1158 return Destination;\r
1159 }\r
1160\r
1161 //\r
1162 // Destination cannot be NULL\r
1163 //\r
1164 ASSERT (Destination != NULL);\r
1165\r
1166 //\r
1167 // Destination and source cannot overlap\r
1168 //\r
1169 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r
1170 ASSERT ((UINTN)(Source - Destination) >= Length);\r
1171\r
1172 ReturnValue = Destination;\r
1173\r
42eedea9 1174 while (*Source != 0 && Length > 0) {\r
e1f414b6 1175 *(Destination++) = *(Source++);\r
1176 Length--;\r
1177 }\r
1178\r
1179 ZeroMem (Destination, Length * sizeof (*Destination));\r
1180 return ReturnValue;\r
1181}\r
1182\r
1183/**\r
1184 Returns the length of a Null-terminated ASCII string.\r
1185\r
1186 This function returns the number of ASCII characters in the Null-terminated\r
1187 ASCII string specified by String.\r
1188\r
9aa049d9 1189 If Length > 0 and Destination is NULL, then ASSERT().\r
1190 If Length > 0 and Source is NULL, then ASSERT().\r
e1f414b6 1191 If PcdMaximumAsciiStringLength is not zero and String contains more than\r
1192 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1193 then ASSERT().\r
1194\r
1195 @param String Pointer to a Null-terminated ASCII string.\r
1196\r
1197 @return The length of String.\r
1198\r
1199**/\r
1200UINTN\r
1201EFIAPI\r
1202AsciiStrLen (\r
1203 IN CONST CHAR8 *String\r
1204 )\r
1205{\r
1206 UINTN Length;\r
1207\r
1208 ASSERT (String != NULL);\r
1209\r
1210 for (Length = 0; *String != '\0'; String++, Length++) {\r
1211 //\r
1212 // If PcdMaximumUnicodeStringLength is not zero,\r
1213 // length should not more than PcdMaximumUnicodeStringLength\r
1214 //\r
1215 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r
1216 ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength));\r
1217 }\r
1218 }\r
1219 return Length;\r
1220}\r
1221\r
1222/**\r
1223 Returns the size of a Null-terminated ASCII string in bytes, including the\r
1224 Null terminator.\r
1225\r
1226 This function returns the size, in bytes, of the Null-terminated ASCII string\r
1227 specified by String.\r
1228\r
1229 If String is NULL, then ASSERT().\r
1230 If PcdMaximumAsciiStringLength is not zero and String contains more than\r
1231 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1232 then ASSERT().\r
1233\r
1234 @param String Pointer to a Null-terminated ASCII string.\r
1235\r
1236 @return The size of String.\r
1237\r
1238**/\r
1239UINTN\r
1240EFIAPI\r
1241AsciiStrSize (\r
1242 IN CONST CHAR8 *String\r
1243 )\r
1244{\r
1245 return (AsciiStrLen (String) + 1) * sizeof (*String);\r
1246}\r
1247\r
1248/**\r
1249 Compares two Null-terminated ASCII strings, and returns the difference\r
1250 between the first mismatched ASCII characters.\r
1251\r
1252 This function compares the Null-terminated ASCII string FirstString to the\r
1253 Null-terminated ASCII string SecondString. If FirstString is identical to\r
1254 SecondString, then 0 is returned. Otherwise, the value returned is the first\r
1255 mismatched ASCII character in SecondString subtracted from the first\r
1256 mismatched ASCII character in FirstString.\r
1257\r
1258 If FirstString is NULL, then ASSERT().\r
1259 If SecondString is NULL, then ASSERT().\r
1260 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
1261 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1262 then ASSERT().\r
1263 If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
1264 than PcdMaximumAsciiStringLength ASCII characters not including the\r
1265 Null-terminator, then ASSERT().\r
1266\r
1267 @param FirstString Pointer to a Null-terminated ASCII string.\r
1268 @param SecondString Pointer to a Null-terminated ASCII string.\r
1269\r
9aa049d9 1270 @retval ==0 FirstString is identical to SecondString.\r
1271 @retval !=0 FirstString is not identical to SecondString.\r
e1f414b6 1272\r
1273**/\r
1274INTN\r
1275EFIAPI\r
1276AsciiStrCmp (\r
1277 IN CONST CHAR8 *FirstString,\r
1278 IN CONST CHAR8 *SecondString\r
1279 )\r
1280{\r
1281 //\r
1282 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1283 //\r
1284 ASSERT (AsciiStrSize (FirstString));\r
1285 ASSERT (AsciiStrSize (SecondString));\r
1286\r
1287 while ((*FirstString != '\0') && (*FirstString == *SecondString)) {\r
1288 FirstString++;\r
1289 SecondString++;\r
1290 }\r
1291\r
1292 return *FirstString - *SecondString;\r
1293}\r
1294\r
1295/**\r
24dcb5e5 1296 Converts a lowercase Ascii character to upper one.\r
e1f414b6 1297\r
1298 If Chr is lowercase Ascii character, then converts it to upper one.\r
1299\r
1300 If Value >= 0xA0, then ASSERT().\r
1301 If (Value & 0x0F) >= 0x0A, then ASSERT().\r
1302\r
42eedea9 1303 @param Chr one Ascii character\r
e1f414b6 1304\r
1305 @return The uppercase value of Ascii character \r
1306\r
1307**/\r
e1f414b6 1308CHAR8\r
42eedea9 1309EFIAPI\r
e1f414b6 1310AsciiToUpper (\r
1311 IN CHAR8 Chr\r
1312 )\r
1313{\r
1314 return (UINT8) ((Chr >= 'a' && Chr <= 'z') ? Chr - ('a' - 'A') : Chr);\r
1315}\r
1316\r
1317/**\r
1318 Convert a ASCII character to numerical value.\r
1319\r
1320 This internal function only deal with Unicode character\r
1321 which maps to a valid hexadecimal ASII character, i.e.\r
1322 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other \r
1323 ASCII character, the value returned does not make sense.\r
1324\r
1325 @param Char The character to convert.\r
1326\r
24dcb5e5 1327 @return The numerical value converted.\r
e1f414b6 1328\r
1329**/\r
e1f414b6 1330UINTN\r
42eedea9 1331EFIAPI\r
e1f414b6 1332InternalAsciiHexCharToUintn (\r
1333 IN CHAR8 Char\r
1334 )\r
1335{\r
1336 if (InternalIsDecimalDigitCharacter (Char)) {\r
1337 return Char - '0';\r
1338 }\r
1339\r
1340 return (UINTN) (10 + AsciiToUpper (Char) - 'A');\r
1341}\r
1342\r
1343\r
1344/**\r
1345 Performs a case insensitive comparison of two Null-terminated ASCII strings,\r
1346 and returns the difference between the first mismatched ASCII characters.\r
1347\r
1348 This function performs a case insensitive comparison of the Null-terminated\r
1349 ASCII string FirstString to the Null-terminated ASCII string SecondString. If\r
1350 FirstString is identical to SecondString, then 0 is returned. Otherwise, the\r
1351 value returned is the first mismatched lower case ASCII character in\r
1352 SecondString subtracted from the first mismatched lower case ASCII character\r
1353 in FirstString.\r
1354\r
1355 If FirstString is NULL, then ASSERT().\r
1356 If SecondString is NULL, then ASSERT().\r
1357 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
1358 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1359 then ASSERT().\r
1360 If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r
1361 than PcdMaximumAsciiStringLength ASCII characters not including the\r
1362 Null-terminator, then ASSERT().\r
1363\r
1364 @param FirstString Pointer to a Null-terminated ASCII string.\r
1365 @param SecondString Pointer to a Null-terminated ASCII string.\r
1366\r
9aa049d9 1367 @retval ==0 FirstString is identical to SecondString using case insensitive\r
1106ffe1 1368 comparisons.\r
9aa049d9 1369 @retval !=0 FirstString is not identical to SecondString using case\r
1370 insensitive comparisons.\r
e1f414b6 1371\r
1372**/\r
1373INTN\r
1374EFIAPI\r
1375AsciiStriCmp (\r
1376 IN CONST CHAR8 *FirstString,\r
1377 IN CONST CHAR8 *SecondString\r
1378 )\r
1379{\r
1380 CHAR8 UpperFirstString;\r
1381 CHAR8 UpperSecondString;\r
1382\r
1383 //\r
1384 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1385 //\r
1386 ASSERT (AsciiStrSize (FirstString));\r
1387 ASSERT (AsciiStrSize (SecondString));\r
1388\r
1389 UpperFirstString = AsciiToUpper (*FirstString);\r
1390 UpperSecondString = AsciiToUpper (*SecondString);\r
1391 while ((*FirstString != '\0') && (UpperFirstString == UpperSecondString)) {\r
1392 FirstString++;\r
1393 SecondString++;\r
1394 UpperFirstString = AsciiToUpper (*FirstString);\r
1395 UpperSecondString = AsciiToUpper (*SecondString);\r
1396 }\r
1397\r
1398 return UpperFirstString - UpperSecondString;\r
1399}\r
1400\r
1401/**\r
1402 Compares two Null-terminated ASCII strings with maximum lengths, and returns\r
1403 the difference between the first mismatched ASCII characters.\r
1404\r
1405 This function compares the Null-terminated ASCII string FirstString to the\r
1406 Null-terminated ASCII string SecondString. At most, Length ASCII characters\r
1407 will be compared. If Length is 0, then 0 is returned. If FirstString is\r
1408 identical to SecondString, then 0 is returned. Otherwise, the value returned\r
1409 is the first mismatched ASCII character in SecondString subtracted from the\r
1410 first mismatched ASCII character in FirstString.\r
1411\r
9aa049d9 1412 If Length > 0 and FirstString is NULL, then ASSERT().\r
1413 If Length > 0 and SecondString is NULL, then ASSERT().\r
e1f414b6 1414 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r
1415 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1416 then ASSERT().\r
1417 If PcdMaximumAsciiStringLength is not zero and SecondString contains more than\r
1418 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1419 then ASSERT().\r
1420\r
1421 @param FirstString Pointer to a Null-terminated ASCII string.\r
1422 @param SecondString Pointer to a Null-terminated ASCII string.\r
9aa049d9 1423 @param Length Maximum number of ASCII characters for compare.\r
1424\r
1425 @retval ==0 FirstString is identical to SecondString.\r
1426 @retval !=0 FirstString is not identical to SecondString.\r
e1f414b6 1427\r
1428**/\r
1429INTN\r
1430EFIAPI\r
1431AsciiStrnCmp (\r
1432 IN CONST CHAR8 *FirstString,\r
1433 IN CONST CHAR8 *SecondString,\r
1434 IN UINTN Length\r
1435 )\r
1436{\r
2bfb6009 1437 if (Length == 0) {\r
e1f414b6 1438 return 0;\r
1439 }\r
1440\r
1441 //\r
1442 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
1443 //\r
1444 ASSERT (AsciiStrSize (FirstString));\r
1445 ASSERT (AsciiStrSize (SecondString));\r
1446\r
1447 while ((*FirstString != '\0') &&\r
1448 (*FirstString == *SecondString) &&\r
1449 (Length > 1)) {\r
1450 FirstString++;\r
1451 SecondString++;\r
1452 Length--;\r
1453 }\r
1454 return *FirstString - *SecondString;\r
1455}\r
1456\r
1457/**\r
1458 Concatenates one Null-terminated ASCII string to another Null-terminated\r
1459 ASCII string, and returns the concatenated ASCII string.\r
1460\r
1461 This function concatenates two Null-terminated ASCII strings. The contents of\r
1462 Null-terminated ASCII string Source are concatenated to the end of Null-\r
1463 terminated ASCII string Destination. The Null-terminated concatenated ASCII\r
1464 String is returned.\r
1465\r
1466 If Destination is NULL, then ASSERT().\r
1467 If Source is NULL, then ASSERT().\r
1468 If PcdMaximumAsciiStringLength is not zero and Destination contains more than\r
1469 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1470 then ASSERT().\r
1471 If PcdMaximumAsciiStringLength is not zero and Source contains more than\r
1472 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1473 then ASSERT().\r
1474 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and\r
1475 Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
1476 ASCII characters, then ASSERT().\r
1477\r
1478 @param Destination Pointer to a Null-terminated ASCII string.\r
1479 @param Source Pointer to a Null-terminated ASCII string.\r
1480\r
9aa049d9 1481 @return Destination\r
e1f414b6 1482\r
1483**/\r
1484CHAR8 *\r
1485EFIAPI\r
1486AsciiStrCat (\r
1487 IN OUT CHAR8 *Destination,\r
1488 IN CONST CHAR8 *Source\r
1489 )\r
1490{\r
1491 AsciiStrCpy (Destination + AsciiStrLen (Destination), Source);\r
1492\r
1493 //\r
1494 // Size of the resulting string should never be zero.\r
1495 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
1496 //\r
1497 ASSERT (AsciiStrSize (Destination) != 0);\r
1498 return Destination;\r
1499}\r
1500\r
1501/**\r
9aa049d9 1502 Concatenates up to a specified length one Null-terminated ASCII string to \r
1503 the end of another Null-terminated ASCII string, and returns the \r
1504 concatenated ASCII string.\r
e1f414b6 1505\r
1506 This function concatenates two Null-terminated ASCII strings. The contents\r
1507 of Null-terminated ASCII string Source are concatenated to the end of Null-\r
1508 terminated ASCII string Destination, and Destination is returned. At most,\r
1509 Length ASCII characters are concatenated from Source to the end of\r
1510 Destination, and Destination is always Null-terminated. If Length is 0, then\r
1511 Destination is returned unmodified. If Source and Destination overlap, then\r
1512 the results are undefined.\r
1513\r
9aa049d9 1514 If Length > 0 and Destination is NULL, then ASSERT().\r
1515 If Length > 0 and Source is NULL, then ASSERT().\r
e1f414b6 1516 If Source and Destination overlap, then ASSERT().\r
1517 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than\r
1518 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1519 then ASSERT().\r
1520 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
1521 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
1522 then ASSERT().\r
1523 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and\r
1524 Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r
1525 ASCII characters not including the Null-terminator, then ASSERT().\r
1526\r
1527 @param Destination Pointer to a Null-terminated ASCII string.\r
1528 @param Source Pointer to a Null-terminated ASCII string.\r
1529 @param Length Maximum number of ASCII characters to concatenate from\r
1530 Source.\r
1531\r
9aa049d9 1532 @return Destination\r
e1f414b6 1533\r
1534**/\r
1535CHAR8 *\r
1536EFIAPI\r
1537AsciiStrnCat (\r
1538 IN OUT CHAR8 *Destination,\r
1539 IN CONST CHAR8 *Source,\r
1540 IN UINTN Length\r
1541 )\r
1542{\r
1543 AsciiStrnCpy (Destination + AsciiStrLen (Destination), Source, Length);\r
1544\r
1545 //\r
1546 // Size of the resulting string should never be zero.\r
1547 // PcdMaximumUnicodeStringLength is tested inside StrLen().\r
1548 //\r
1549 ASSERT (AsciiStrSize (Destination) != 0);\r
1550 return Destination;\r
1551}\r
1552\r
1553/**\r
9aa049d9 1554 Returns the first occurrence of a Null-terminated ASCII sub-string\r
e1f414b6 1555 in a Null-terminated ASCII string.\r
1556\r
9aa049d9 1557 This function scans the contents of the ASCII string specified by String\r
1558 and returns the first occurrence of SearchString. If SearchString is not\r
1559 found in String, then NULL is returned. If the length of SearchString is zero,\r
e1f414b6 1560 then String is returned.\r
9aa049d9 1561\r
e1f414b6 1562 If String is NULL, then ASSERT().\r
1563 If SearchString is NULL, then ASSERT().\r
1564\r
9aa049d9 1565 If PcdMaximumAsciiStringLength is not zero, and SearchString or\r
1566 String contains more than PcdMaximumAsciiStringLength Unicode characters\r
e1f414b6 1567 not including the Null-terminator, then ASSERT().\r
1568\r
4df26661 1569 @param String Pointer to a Null-terminated ASCII string.\r
1570 @param SearchString Pointer to a Null-terminated ASCII string to search for.\r
e1f414b6 1571\r
1572 @retval NULL If the SearchString does not appear in String.\r
9aa049d9 1573 @retval others If there is a match return the first occurrence of SearchingString.\r
1574 If the length of SearchString is zero,return String.\r
e1f414b6 1575\r
1576**/\r
1577CHAR8 *\r
1578EFIAPI\r
1579AsciiStrStr (\r
4df26661 1580 IN CONST CHAR8 *String,\r
e1f414b6 1581 IN CONST CHAR8 *SearchString\r
1582 )\r
1583{\r
1584 CONST CHAR8 *FirstMatch;\r
1585 CONST CHAR8 *SearchStringTmp;\r
1586\r
e1f414b6 1587 //\r
4df26661 1588 // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r
e1f414b6 1589 //\r
4df26661 1590 ASSERT (AsciiStrSize (String) != 0);\r
1591 ASSERT (AsciiStrSize (SearchString) != 0);\r
e1f414b6 1592\r
1593 while (*String != '\0') {\r
1594 SearchStringTmp = SearchString;\r
1595 FirstMatch = String;\r
1596 \r
1597 while ((*String == *SearchStringTmp) \r
1598 && (*SearchStringTmp != '\0') \r
1599 && (*String != '\0')) {\r
1600 String++;\r
1601 SearchStringTmp++;\r
1602 } \r
1603 \r
1604 if (*SearchStringTmp == '\0') {\r
1605 return (CHAR8 *) FirstMatch;\r
1606 }\r
1607\r
1608 if (SearchStringTmp == SearchString) {\r
1609 //\r
1610 // If no character from SearchString match,\r
1611 // move the pointer to the String under search\r
1612 // by one character.\r
1613 //\r
1614 String++;\r
1615 }\r
1616\r
1617 }\r
1618\r
1619 return NULL;\r
1620}\r
1621\r
1622/**\r
9aa049d9 1623 Convert a Null-terminated ASCII decimal string to a value of type\r
e1f414b6 1624 UINTN.\r
1625\r
9aa049d9 1626 This function returns a value of type UINTN by interpreting the contents\r
1627 of the ASCII string String as a decimal number. The format of the input\r
e1f414b6 1628 ASCII string String is:\r
9aa049d9 1629\r
e1f414b6 1630 [spaces] [decimal digits].\r
9aa049d9 1631\r
1632 The valid decimal digit character is in the range [0-9]. The function will\r
1633 ignore the pad space, which includes spaces or tab characters, before the digits.\r
1634 The running zero in the beginning of [decimal digits] will be ignored. Then, the\r
1635 function stops at the first character that is a not a valid decimal character or\r
e1f414b6 1636 Null-terminator, whichever on comes first.\r
9aa049d9 1637\r
e1f414b6 1638 If String has only pad spaces, then 0 is returned.\r
1639 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
9aa049d9 1640 If the number represented by String overflows according to the range defined by\r
e1f414b6 1641 UINTN, then ASSERT().\r
1642 If String is NULL, then ASSERT().\r
9aa049d9 1643 If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
1644 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 1645 then ASSERT().\r
1646\r
9aa049d9 1647 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1648\r
9aa049d9 1649 @retval Value translated from String.\r
e1f414b6 1650\r
1651**/\r
1652UINTN\r
1653EFIAPI\r
1654AsciiStrDecimalToUintn (\r
4df26661 1655 IN CONST CHAR8 *String\r
e1f414b6 1656 )\r
1657{\r
1658 UINTN Result;\r
1659 \r
4df26661 1660 //\r
1661 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1662 //\r
1663 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1664\r
1665 //\r
1666 // Ignore the pad spaces (space or tab)\r
1667 //\r
955c32f2 1668 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1669 String++;\r
1670 }\r
1671\r
1672 //\r
1673 // Ignore leading Zeros after the spaces\r
1674 //\r
955c32f2 1675 while (*String == '0') {\r
e1f414b6 1676 String++;\r
1677 }\r
1678\r
1679 Result = 0;\r
1680\r
1681 while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
1682 //\r
1683 // If the number represented by String overflows according \r
1684 // to the range defined by UINTN, then ASSERT().\r
1685 //\r
24dcb5e5 1686 ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_10) ||\r
955c32f2 1687 ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_10) && \r
24dcb5e5 1688 (*String - '0') <= REMAINDER_MAX_UINTN_DIVIDED_BY_10)\r
e1f414b6 1689 );\r
1690\r
1691 Result = Result * 10 + (*String - '0');\r
1692 String++;\r
1693 }\r
1694 \r
1695 return Result;\r
1696}\r
1697\r
1698\r
1699/**\r
9aa049d9 1700 Convert a Null-terminated ASCII decimal string to a value of type\r
e1f414b6 1701 UINT64.\r
1702\r
9aa049d9 1703 This function returns a value of type UINT64 by interpreting the contents\r
1704 of the ASCII string String as a decimal number. The format of the input\r
e1f414b6 1705 ASCII string String is:\r
9aa049d9 1706\r
e1f414b6 1707 [spaces] [decimal digits].\r
9aa049d9 1708\r
1709 The valid decimal digit character is in the range [0-9]. The function will\r
1710 ignore the pad space, which includes spaces or tab characters, before the digits.\r
1711 The running zero in the beginning of [decimal digits] will be ignored. Then, the\r
1712 function stops at the first character that is a not a valid decimal character or\r
e1f414b6 1713 Null-terminator, whichever on comes first.\r
9aa049d9 1714\r
e1f414b6 1715 If String has only pad spaces, then 0 is returned.\r
1716 If String has no pad spaces or valid decimal digits, then 0 is returned.\r
9aa049d9 1717 If the number represented by String overflows according to the range defined by\r
e1f414b6 1718 UINT64, then ASSERT().\r
1719 If String is NULL, then ASSERT().\r
9aa049d9 1720 If PcdMaximumAsciiStringLength is not zero, and String contains more than\r
1721 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 1722 then ASSERT().\r
1723\r
9aa049d9 1724 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1725\r
9aa049d9 1726 @retval Value translated from String.\r
e1f414b6 1727\r
1728**/\r
1729UINT64\r
1730EFIAPI\r
1731AsciiStrDecimalToUint64 (\r
4df26661 1732 IN CONST CHAR8 *String\r
e1f414b6 1733 )\r
1734{\r
1735 UINT64 Result;\r
1736 \r
4df26661 1737 //\r
1738 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1739 //\r
1740 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1741\r
1742 //\r
1743 // Ignore the pad spaces (space or tab)\r
1744 //\r
955c32f2 1745 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1746 String++;\r
1747 }\r
1748\r
1749 //\r
1750 // Ignore leading Zeros after the spaces\r
1751 //\r
955c32f2 1752 while (*String == '0') {\r
e1f414b6 1753 String++;\r
1754 }\r
1755\r
1756 Result = 0;\r
1757\r
1758 while (InternalAsciiIsDecimalDigitCharacter (*String)) {\r
1759 //\r
1760 // If the number represented by String overflows according \r
1761 // to the range defined by UINTN, then ASSERT().\r
1762 //\r
24dcb5e5 1763 ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_10) || \r
955c32f2 1764 ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_10) && \r
24dcb5e5 1765 (*String - '0') <= REMAINDER_MAX_UINT64_DIVIDED_BY_10)\r
e1f414b6 1766 );\r
1767\r
1768 Result = MultU64x32 (Result, 10) + (*String - '0');\r
1769 String++;\r
1770 }\r
1771 \r
1772 return Result;\r
1773}\r
1774\r
1775/**\r
1776 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.\r
1777\r
9aa049d9 1778 This function returns a value of type UINTN by interpreting the contents of\r
1779 the ASCII string String as a hexadecimal number. The format of the input ASCII\r
e1f414b6 1780 string String is:\r
9aa049d9 1781\r
e1f414b6 1782 [spaces][zeros][x][hexadecimal digits].\r
9aa049d9 1783\r
1784 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
1785 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"\r
1786 appears in the input string, it must be prefixed with at least one 0. The function\r
1787 will ignore the pad space, which includes spaces or tab characters, before [zeros],\r
1788 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]\r
1789 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal\r
1790 digit. Then, the function stops at the first character that is a not a valid\r
e1f414b6 1791 hexadecimal character or Null-terminator, whichever on comes first.\r
9aa049d9 1792\r
e1f414b6 1793 If String has only pad spaces, then 0 is returned.\r
1794 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1795 0 is returned.\r
1796\r
9aa049d9 1797 If the number represented by String overflows according to the range defined by UINTN,\r
e1f414b6 1798 then ASSERT().\r
1799 If String is NULL, then ASSERT().\r
9aa049d9 1800 If PcdMaximumAsciiStringLength is not zero,\r
1801 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
e1f414b6 1802 the Null-terminator, then ASSERT().\r
1803\r
9aa049d9 1804 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1805\r
9aa049d9 1806 @retval Value translated from String.\r
e1f414b6 1807\r
1808**/\r
1809UINTN\r
1810EFIAPI\r
1811AsciiStrHexToUintn (\r
4df26661 1812 IN CONST CHAR8 *String\r
e1f414b6 1813 )\r
1814{\r
1815 UINTN Result;\r
1816\r
4df26661 1817 //\r
1818 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1819 //\r
1820 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1821 \r
1822 //\r
1823 // Ignore the pad spaces (space or tab) \r
1824 //\r
955c32f2 1825 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1826 String++;\r
1827 }\r
1828\r
1829 //\r
1830 // Ignore leading Zeros after the spaces\r
1831 //\r
955c32f2 1832 while (*String == '0') {\r
e1f414b6 1833 String++;\r
1834 }\r
1835\r
1836 if (AsciiToUpper (*String) == 'X') {\r
955c32f2 1837 ASSERT (*(String - 1) == '0');\r
1838 if (*(String - 1) != '0') {\r
e1f414b6 1839 return 0;\r
1840 }\r
1841 //\r
1842 // Skip the 'X'\r
1843 //\r
1844 String++;\r
1845 }\r
1846\r
1847 Result = 0;\r
1848 \r
1849 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
1850 //\r
1851 // If the Hex Number represented by String overflows according \r
1852 // to the range defined by UINTN, then ASSERT().\r
1853 //\r
24dcb5e5 1854 ASSERT ((Result < QUOTIENT_MAX_UINTN_DIVIDED_BY_16) ||\r
955c32f2 1855 ((Result == QUOTIENT_MAX_UINTN_DIVIDED_BY_16) && \r
24dcb5e5 1856 (InternalAsciiHexCharToUintn (*String) <= REMAINDER_MAX_UINTN_DIVIDED_BY_16))\r
e1f414b6 1857 );\r
1858\r
1859 Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);\r
1860 String++;\r
1861 }\r
1862\r
1863 return Result;\r
1864}\r
1865\r
1866\r
1867/**\r
1868 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.\r
1869\r
9aa049d9 1870 This function returns a value of type UINT64 by interpreting the contents of\r
1871 the ASCII string String as a hexadecimal number. The format of the input ASCII\r
e1f414b6 1872 string String is:\r
9aa049d9 1873\r
e1f414b6 1874 [spaces][zeros][x][hexadecimal digits].\r
9aa049d9 1875\r
1876 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].\r
1877 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"\r
1878 appears in the input string, it must be prefixed with at least one 0. The function\r
1879 will ignore the pad space, which includes spaces or tab characters, before [zeros],\r
1880 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]\r
1881 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal\r
1882 digit. Then, the function stops at the first character that is a not a valid\r
e1f414b6 1883 hexadecimal character or Null-terminator, whichever on comes first.\r
9aa049d9 1884\r
e1f414b6 1885 If String has only pad spaces, then 0 is returned.\r
1886 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then\r
1887 0 is returned.\r
1888\r
9aa049d9 1889 If the number represented by String overflows according to the range defined by UINT64,\r
e1f414b6 1890 then ASSERT().\r
1891 If String is NULL, then ASSERT().\r
9aa049d9 1892 If PcdMaximumAsciiStringLength is not zero,\r
1893 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including\r
e1f414b6 1894 the Null-terminator, then ASSERT().\r
1895\r
9aa049d9 1896 @param String Pointer to a Null-terminated ASCII string.\r
e1f414b6 1897\r
9aa049d9 1898 @retval Value translated from String.\r
e1f414b6 1899\r
1900**/\r
1901UINT64\r
1902EFIAPI\r
1903AsciiStrHexToUint64 (\r
4df26661 1904 IN CONST CHAR8 *String\r
e1f414b6 1905 )\r
1906{\r
1907 UINT64 Result;\r
1908\r
4df26661 1909 //\r
1910 // ASSERT Strings is less long than PcdMaximumAsciiStringLength\r
1911 //\r
1912 ASSERT (AsciiStrSize (String) != 0);\r
e1f414b6 1913 \r
1914 //\r
1915 // Ignore the pad spaces (space or tab) and leading Zeros\r
1916 //\r
1917 //\r
1918 // Ignore the pad spaces (space or tab) \r
1919 //\r
955c32f2 1920 while ((*String == ' ') || (*String == '\t' )) {\r
e1f414b6 1921 String++;\r
1922 }\r
1923\r
1924 //\r
1925 // Ignore leading Zeros after the spaces\r
1926 //\r
955c32f2 1927 while (*String == '0') {\r
e1f414b6 1928 String++;\r
1929 }\r
1930\r
1931 if (AsciiToUpper (*String) == 'X') {\r
955c32f2 1932 ASSERT (*(String - 1) == '0');\r
1933 if (*(String - 1) != '0') {\r
e1f414b6 1934 return 0;\r
1935 }\r
1936 //\r
1937 // Skip the 'X'\r
1938 //\r
1939 String++;\r
1940 }\r
1941\r
1942 Result = 0;\r
1943 \r
1944 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {\r
1945 //\r
1946 // If the Hex Number represented by String overflows according \r
1947 // to the range defined by UINTN, then ASSERT().\r
1948 //\r
24dcb5e5 1949 ASSERT ((Result < QUOTIENT_MAX_UINT64_DIVIDED_BY_16) ||\r
955c32f2 1950 ((Result == QUOTIENT_MAX_UINT64_DIVIDED_BY_16) && \r
24dcb5e5 1951 (InternalAsciiHexCharToUintn (*String) <= REMAINDER_MAX_UINT64_DIVIDED_BY_16))\r
e1f414b6 1952 );\r
1953\r
1954 Result = LShiftU64 (Result, 4);\r
1955 Result = Result + InternalAsciiHexCharToUintn (*String);\r
1956 String++;\r
1957 }\r
1958\r
1959 return Result;\r
1960}\r
1961\r
1962\r
1963/**\r
9aa049d9 1964 Convert one Null-terminated ASCII string to a Null-terminated\r
e1f414b6 1965 Unicode string and returns the Unicode string.\r
1966\r
9aa049d9 1967 This function converts the contents of the ASCII string Source to the Unicode\r
1968 string Destination, and returns Destination. The function terminates the\r
1969 Unicode string Destination by appending a Null-terminator character at the end.\r
1970 The caller is responsible to make sure Destination points to a buffer with size\r
e1f414b6 1971 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.\r
9aa049d9 1972\r
e1f414b6 1973 If Destination is NULL, then ASSERT().\r
1974 If Destination is not aligned on a 16-bit boundary, then ASSERT().\r
1975 If Source is NULL, then ASSERT().\r
1976 If Source and Destination overlap, then ASSERT().\r
9aa049d9 1977 If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r
1978 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,\r
e1f414b6 1979 then ASSERT().\r
9aa049d9 1980 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r
1981 PcdMaximumUnicodeStringLength ASCII characters not including the\r
e1f414b6 1982 Null-terminator, then ASSERT().\r
1983\r
1984 @param Source Pointer to a Null-terminated ASCII string.\r
1985 @param Destination Pointer to a Null-terminated Unicode string.\r
1986\r
9aa049d9 1987 @return Destination.\r
e1f414b6 1988\r
1989**/\r
1990CHAR16 *\r
1991EFIAPI\r
1992AsciiStrToUnicodeStr (\r
4df26661 1993 IN CONST CHAR8 *Source,\r
1994 OUT CHAR16 *Destination\r
e1f414b6 1995 )\r
1996{\r
4df26661 1997 CHAR16 *ReturnValue;\r
1998\r
e1f414b6 1999 ASSERT (Destination != NULL);\r
e1f414b6 2000\r
2001 //\r
4df26661 2002 // ASSERT Source is less long than PcdMaximumAsciiStringLength\r
e1f414b6 2003 //\r
4df26661 2004 ASSERT (AsciiStrSize (Source) != 0);\r
e1f414b6 2005\r
2006 //\r
4df26661 2007 // Source and Destination should not overlap\r
e1f414b6 2008 //\r
4df26661 2009 ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));\r
2010 ASSERT ((UINTN) (Source - (CHAR8 *) Destination) > (AsciiStrLen (Source) * sizeof (CHAR16)));\r
e1f414b6 2011\r
4df26661 2012 \r
2013 ReturnValue = Destination;\r
e1f414b6 2014 while (*Source != '\0') {\r
2015 *(Destination++) = (CHAR16) *(Source++);\r
2016 }\r
2017 //\r
2018 // End the Destination with a NULL.\r
2019 //\r
2020 *Destination = '\0';\r
2021\r
4df26661 2022 //\r
2023 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength\r
2024 //\r
2025 ASSERT (StrSize (ReturnValue) != 0);\r
2026\r
2027 return ReturnValue;\r
e1f414b6 2028}\r
2029\r
2030/**\r
2031 Converts an 8-bit value to an 8-bit BCD value.\r
2032\r
2033 Converts the 8-bit value specified by Value to BCD. The BCD value is\r
2034 returned.\r
2035\r
2036 If Value >= 100, then ASSERT().\r
2037\r
2038 @param Value The 8-bit value to convert to BCD. Range 0..99.\r
2039\r
9aa049d9 2040 @return The BCD value.\r
e1f414b6 2041\r
2042**/\r
2043UINT8\r
2044EFIAPI\r
2045DecimalToBcd8 (\r
2046 IN UINT8 Value\r
2047 )\r
2048{\r
2049 ASSERT (Value < 100);\r
2050 return (UINT8) (((Value / 10) << 4) | (Value % 10));\r
2051}\r
2052\r
2053/**\r
2054 Converts an 8-bit BCD value to an 8-bit value.\r
2055\r
2056 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit\r
2057 value is returned.\r
2058\r
2059 If Value >= 0xA0, then ASSERT().\r
2060 If (Value & 0x0F) >= 0x0A, then ASSERT().\r
2061\r
2062 @param Value The 8-bit BCD value to convert to an 8-bit value.\r
2063\r
9aa049d9 2064 @return The 8-bit value is returned.\r
e1f414b6 2065\r
2066**/\r
2067UINT8\r
2068EFIAPI\r
2069BcdToDecimal8 (\r
2070 IN UINT8 Value\r
2071 )\r
2072{\r
2073 ASSERT (Value < 0xa0);\r
2074 ASSERT ((Value & 0xf) < 0xa);\r
2075 return (UINT8) ((Value >> 4) * 10 + (Value & 0xf));\r
2076}\r
2077\r
2078\r
d9e5c1ff 2079/**\r
2080 Convert a nibble in the low 4 bits of a byte to a Unicode hexadecimal character.\r
2081\r
2082 This function converts a nibble in the low 4 bits of a byte to a Unicode hexadecimal \r
2083 character For example, the nibble 0x01 and 0x0A will converted to L'1' and L'A' \r
2084 respectively.\r
2085\r
2086 The upper nibble in the input byte will be masked off.\r
2087\r
2088 @param Nibble The nibble which is in the low 4 bits of the input byte.\r
2089\r
9aa049d9 2090 @retval CHAR16 The Unicode hexadecimal character.\r
2091\r
d9e5c1ff 2092**/\r
2093CHAR16\r
1b37333d 2094EFIAPI\r
d9e5c1ff 2095NibbleToHexChar (\r
2096 IN UINT8 Nibble\r
2097 )\r
2098{\r
2099 Nibble &= 0x0F;\r
2100 if (Nibble <= 0x9) {\r
2101 return (CHAR16)(Nibble + L'0');\r
2102 }\r
2103\r
2104 return (CHAR16)(Nibble - 0xA + L'A');\r
2105}\r
2106\r
2107/** \r
2108 Convert binary buffer to a Unicode String in a specified sequence. \r
2109\r
1106ffe1 2110 This function converts bytes in the memory block pointed by Buffer to a Unicode String Str. \r
d9e5c1ff 2111 Each byte will be represented by two Unicode characters. For example, byte 0xA1 will \r
2112 be converted into two Unicode character L'A' and L'1'. In the output String, the Unicode Character \r
2113 for the Most Significant Nibble will be put before the Unicode Character for the Least Significant\r
2114 Nibble. The output string for the buffer containing a single byte 0xA1 will be L"A1". \r
2115 For a buffer with multiple bytes, the Unicode character produced by the first byte will be put into the \r
2116 the last character in the output string. The one next to first byte will be put into the\r
2117 character before the last character. This rules applies to the rest of the bytes. The Unicode\r
2118 character by the last byte will be put into the first character in the output string. For example,\r
9aa049d9 2119 the input buffer for a 64-bits unsigned integer 0x12345678abcdef1234 will be converted to\r
d9e5c1ff 2120 a Unicode string equal to L"12345678abcdef1234".\r
2121\r
9aa049d9 2122 @param String On input, String is pointed to the buffer allocated for the convertion.\r
2123 @param StringLen The Length of String buffer to hold the output String. The length must include the tailing '\0' character.\r
2124 The StringLen required to convert a N bytes Buffer will be a least equal to or greater \r
2125 than 2*N + 1.\r
2126 @param Buffer The pointer to a input buffer.\r
2127 @param BufferSizeInBytes Length in bytes of the input buffer.\r
2128\r
2129\r
2130 @retval EFI_SUCCESS The convertion is successful. All bytes in Buffer has been convert to the corresponding\r
2131 Unicode character and placed into the right place in String.\r
2132 @retval EFI_BUFFER_TOO_SMALL StringSizeInBytes is smaller than 2 * N + 1the number of bytes required to\r
2133 complete the convertion. \r
d9e5c1ff 2134**/\r
2135RETURN_STATUS\r
2136EFIAPI\r
2137BufToHexString (\r
2138 IN OUT CHAR16 *String,\r
2139 IN OUT UINTN *StringLen,\r
2140 IN CONST UINT8 *Buffer,\r
2141 IN UINTN BufferSizeInBytes\r
2142 )\r
2143{\r
2144 UINTN Idx;\r
2145 UINT8 Byte;\r
2146 UINTN StrLen;\r
2147\r
2148 //\r
2149 // Make sure string is either passed or allocate enough.\r
2150 // It takes 2 Unicode characters (4 bytes) to represent 1 byte of the binary buffer.\r
2151 // Plus the Unicode termination character.\r
2152 //\r
2153 StrLen = BufferSizeInBytes * 2;\r
2154 if (StrLen > ((*StringLen) - 1)) {\r
2155 *StringLen = StrLen + 1;\r
2156 return RETURN_BUFFER_TOO_SMALL;\r
2157 }\r
2158\r
2159 *StringLen = StrLen + 1;\r
2160 //\r
2161 // Ends the string.\r
2162 //\r
2163 String[StrLen] = L'\0'; \r
2164\r
2165 for (Idx = 0; Idx < BufferSizeInBytes; Idx++) {\r
2166\r
2167 Byte = Buffer[Idx];\r
2168 String[StrLen - 1 - Idx * 2] = NibbleToHexChar (Byte);\r
2169 String[StrLen - 2 - Idx * 2] = NibbleToHexChar ((UINT8)(Byte >> 4));\r
2170 }\r
2171\r
2172 return RETURN_SUCCESS;\r
2173}\r
2174\r
2175\r
2176/**\r
2177 Convert a Unicode string consisting of hexadecimal characters to a output byte buffer.\r
2178\r
2179 This function converts a Unicode string consisting of characters in the range of Hexadecimal\r
2180 character (L'0' to L'9', L'A' to L'F' and L'a' to L'f') to a output byte buffer. The function will stop\r
2181 at the first non-hexadecimal character or the NULL character. The convertion process can be\r
2182 simply viewed as the reverse operations defined by BufToHexString. Two Unicode characters will be \r
2183 converted into one byte. The first Unicode character represents the Most Significant Nibble and the\r
2184 second Unicode character represents the Least Significant Nibble in the output byte. \r
2185 The first pair of Unicode characters represents the last byte in the output buffer. The second pair of Unicode \r
2186 characters represent the the byte preceding the last byte. This rule applies to the rest pairs of bytes. \r
2187 The last pair represent the first byte in the output buffer. \r
2188\r
2189 For example, a Unciode String L"12345678" will be converted into a buffer wil the following bytes \r
2190 (first byte is the byte in the lowest memory address): "0x78, 0x56, 0x34, 0x12".\r
2191\r
2192 If String has N valid hexadecimal characters for conversion, the caller must make sure Buffer is at least \r
2193 N/2 (if N is even) or (N+1)/2 (if N if odd) bytes. \r
2194\r
2195 @param Buffer The output buffer allocated by the caller.\r
2196 @param BufferSizeInBytes On input, the size in bytes of Buffer. On output, it is updated to \r
2197 contain the size of the Buffer which is actually used for the converstion.\r
2198 For Unicode string with 2*N hexadecimal characters (not including the \r
2199 tailing NULL character), N bytes of Buffer will be used for the output.\r
2200 @param String The input hexadecimal string.\r
2201 @param ConvertedStrLen The number of hexadecimal characters used to produce content in output\r
2202 buffer Buffer.\r
2203\r
2204 @retval RETURN_BUFFER_TOO_SMALL The input BufferSizeInBytes is too small to hold the output. BufferSizeInBytes\r
2205 will be updated to the size required for the converstion.\r
2206 @retval RETURN_SUCCESS The convertion is successful or the first Unicode character from String\r
2207 is hexadecimal. If ConvertedStrLen is not NULL, it is updated\r
2208 to the number of hexadecimal character used for the converstion.\r
2209**/\r
2210RETURN_STATUS\r
2211EFIAPI\r
2212HexStringToBuf (\r
2213 OUT UINT8 *Buffer, \r
2214 IN OUT UINTN *BufferSizeInBytes,\r
2215 IN CONST CHAR16 *String,\r
2216 OUT UINTN *ConvertedStrLen OPTIONAL\r
2217 )\r
2218{\r
2219 UINTN HexCnt;\r
2220 UINTN Idx;\r
2221 UINTN BufferLength;\r
2222 UINT8 Digit;\r
2223 UINT8 Byte;\r
2224\r
2225 //\r
2226 // Find out how many hex characters the string has.\r
2227 //\r
2228 for (Idx = 0, HexCnt = 0; IsHexDigit (&Digit, String[Idx]); Idx++, HexCnt++);\r
2229\r
2230 if (HexCnt == 0) {\r
2231 *ConvertedStrLen = 0;\r
2232 return RETURN_SUCCESS;\r
2233 }\r
2234 //\r
2235 // Two Unicode characters make up 1 buffer byte. Round up.\r
2236 //\r
2237 BufferLength = (HexCnt + 1) / 2; \r
2238\r
2239 //\r
2240 // Test if buffer is passed enough.\r
2241 //\r
2242 if (BufferLength > (*BufferSizeInBytes)) {\r
2243 *BufferSizeInBytes = BufferLength;\r
2244 return RETURN_BUFFER_TOO_SMALL;\r
2245 }\r
2246\r
2247 *BufferSizeInBytes = BufferLength;\r
2248\r
2249 for (Idx = 0; Idx < HexCnt; Idx++) {\r
2250\r
2251 IsHexDigit (&Digit, String[HexCnt - 1 - Idx]);\r
2252\r
2253 //\r
2254 // For odd charaters, write the lower nibble for each buffer byte,\r
2255 // and for even characters, the upper nibble.\r
2256 //\r
2257 if ((Idx & 1) == 0) {\r
2258 Byte = Digit;\r
2259 } else {\r
2260 Byte = Buffer[Idx / 2];\r
2261 Byte &= 0x0F;\r
2262 Byte = (UINT8) (Byte | Digit << 4);\r
2263 }\r
2264\r
2265 Buffer[Idx / 2] = Byte;\r
2266 }\r
2267\r
2268 if (ConvertedStrLen != NULL) {\r
2269 *ConvertedStrLen = HexCnt;\r
2270 }\r
2271\r
2272 return RETURN_SUCCESS;\r
2273}\r
2274\r
2275\r
2276/**\r
2277 Test if a Unicode character is a hexadecimal digit. If true, the input\r
2278 Unicode character is converted to a byte. \r
2279\r
2280 This function tests if a Unicode character is a hexadecimal digit. If true, the input\r
2281 Unicode character is converted to a byte. For example, Unicode character\r
2282 L'A' will be converted to 0x0A. \r
2283\r
9aa049d9 2284 If Digit is NULL, then ASSERT.\r
2285\r
1106ffe1 2286 @param Digit The output hexadecimal digit.\r
9aa049d9 2287\r
1106ffe1 2288 @param Char The input Unicode character.\r
d9e5c1ff 2289\r
2290 @retval TRUE Char is in the range of Hexadecimal number. Digit is updated\r
2291 to the byte value of the number.\r
2292 @retval FALSE Char is not in the range of Hexadecimal number. Digit is keep\r
2293 intact.\r
9aa049d9 2294\r
d9e5c1ff 2295**/\r
2296BOOLEAN\r
1b37333d 2297EFIAPI\r
d9e5c1ff 2298IsHexDigit (\r
2299 OUT UINT8 *Digit,\r
2300 IN CHAR16 Char\r
2301 )\r
2302{\r
2303 ASSERT (Digit != NULL);\r
2304 \r
2305 if ((Char >= L'0') && (Char <= L'9')) {\r
2306 *Digit = (UINT8) (Char - L'0');\r
2307 return TRUE;\r
2308 }\r
2309\r
2310 if ((Char >= L'A') && (Char <= L'F')) {\r
2311 *Digit = (UINT8) (Char - L'A' + 0x0A);\r
2312 return TRUE;\r
2313 }\r
2314\r
2315 if ((Char >= L'a') && (Char <= L'f')) {\r
2316 *Digit = (UINT8) (Char - L'a' + 0x0A);\r
2317 return TRUE;\r
2318 }\r
2319\r
2320 return FALSE;\r
2321}\r
2322\r
2323\r