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