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