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