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