]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/String.c
Refine code.
[mirror_edk2.git] / MdePkg / Library / BaseLib / String.c
1 /** @file
2 Unicode and ASCII string primatives.
3
4 Copyright (c) 2006 - 2010, 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 If any Unicode characters in Source contain non-zero value in
999 the upper 8 bits, then ASSERT().
1000
1001 If Destination is NULL, then ASSERT().
1002 If Source is NULL, then ASSERT().
1003 If Source is not aligned on a 16-bit boundary, then ASSERT().
1004 If Source and Destination overlap, then ASSERT().
1005
1006 If PcdMaximumUnicodeStringLength is not zero, and Source contains
1007 more than PcdMaximumUnicodeStringLength Unicode characters, not including
1008 the Null-terminator, then ASSERT().
1009
1010 If PcdMaximumAsciiStringLength is not zero, and Source contains more
1011 than PcdMaximumAsciiStringLength Unicode characters, not including the
1012 Null-terminator, then ASSERT().
1013
1014 @param Source A pointer to a Null-terminated Unicode string.
1015 @param Destination A pointer to a Null-terminated ASCII string.
1016
1017 @return Destination.
1018
1019 **/
1020 CHAR8 *
1021 EFIAPI
1022 UnicodeStrToAsciiStr (
1023 IN CONST CHAR16 *Source,
1024 OUT CHAR8 *Destination
1025 )
1026 {
1027 CHAR8 *ReturnValue;
1028
1029 ASSERT (Destination != NULL);
1030
1031 //
1032 // ASSERT if Source is long than PcdMaximumUnicodeStringLength.
1033 // Length tests are performed inside StrLen().
1034 //
1035 ASSERT (StrSize (Source) != 0);
1036
1037 //
1038 // Source and Destination should not overlap
1039 //
1040 ASSERT ((UINTN) ((CHAR16 *) Destination - Source) > StrLen (Source));
1041 ASSERT ((UINTN) ((CHAR8 *) Source - Destination) > StrLen (Source));
1042
1043
1044 ReturnValue = Destination;
1045 while (*Source != '\0') {
1046 //
1047 // If any Unicode characters in Source contain
1048 // non-zero value in the upper 8 bits, then ASSERT().
1049 //
1050 ASSERT (*Source < 0x100);
1051 *(Destination++) = (CHAR8) *(Source++);
1052 }
1053
1054 *Destination = '\0';
1055
1056 //
1057 // ASSERT Original Destination is less long than PcdMaximumAsciiStringLength.
1058 // Length tests are performed inside AsciiStrLen().
1059 //
1060 ASSERT (AsciiStrSize (ReturnValue) != 0);
1061
1062 return ReturnValue;
1063 }
1064
1065
1066 /**
1067 Copies one Null-terminated ASCII string to another Null-terminated ASCII
1068 string and returns the new ASCII string.
1069
1070 This function copies the contents of the ASCII string Source to the ASCII
1071 string Destination, and returns Destination. If Source and Destination
1072 overlap, then the results are undefined.
1073
1074 If Destination is NULL, then ASSERT().
1075 If Source is NULL, then ASSERT().
1076 If Source and Destination overlap, then ASSERT().
1077 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1078 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1079 then ASSERT().
1080
1081 @param Destination A pointer to a Null-terminated ASCII string.
1082 @param Source A pointer to a Null-terminated ASCII string.
1083
1084 @return Destination
1085
1086 **/
1087 CHAR8 *
1088 EFIAPI
1089 AsciiStrCpy (
1090 OUT CHAR8 *Destination,
1091 IN CONST CHAR8 *Source
1092 )
1093 {
1094 CHAR8 *ReturnValue;
1095
1096 //
1097 // Destination cannot be NULL
1098 //
1099 ASSERT (Destination != NULL);
1100
1101 //
1102 // Destination and source cannot overlap
1103 //
1104 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));
1105 ASSERT ((UINTN)(Source - Destination) > AsciiStrLen (Source));
1106
1107 ReturnValue = Destination;
1108 while (*Source != 0) {
1109 *(Destination++) = *(Source++);
1110 }
1111 *Destination = 0;
1112 return ReturnValue;
1113 }
1114
1115 /**
1116 Copies up to a specified length one Null-terminated ASCII string to another
1117 Null-terminated ASCII string and returns the new ASCII string.
1118
1119 This function copies the contents of the ASCII string Source to the ASCII
1120 string Destination, and returns Destination. At most, Length ASCII characters
1121 are copied from Source to Destination. If Length is 0, then Destination is
1122 returned unmodified. If Length is greater that the number of ASCII characters
1123 in Source, then Destination is padded with Null ASCII characters. If Source
1124 and Destination overlap, then the results are undefined.
1125
1126 If Destination is NULL, then ASSERT().
1127 If Source is NULL, then ASSERT().
1128 If Source and Destination overlap, then ASSERT().
1129 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1130 PcdMaximumAsciiStringLength, then ASSERT().
1131 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1132 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1133 then ASSERT().
1134
1135 @param Destination A pointer to a Null-terminated ASCII string.
1136 @param Source A pointer to a Null-terminated ASCII string.
1137 @param Length The maximum number of ASCII characters to copy.
1138
1139 @return Destination
1140
1141 **/
1142 CHAR8 *
1143 EFIAPI
1144 AsciiStrnCpy (
1145 OUT CHAR8 *Destination,
1146 IN CONST CHAR8 *Source,
1147 IN UINTN Length
1148 )
1149 {
1150 CHAR8 *ReturnValue;
1151
1152 if (Length == 0) {
1153 return Destination;
1154 }
1155
1156 //
1157 // Destination cannot be NULL
1158 //
1159 ASSERT (Destination != NULL);
1160
1161 //
1162 // Destination and source cannot overlap
1163 //
1164 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));
1165 ASSERT ((UINTN)(Source - Destination) >= Length);
1166
1167 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {
1168 ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));
1169 }
1170
1171 ReturnValue = Destination;
1172
1173 while (*Source != 0 && Length > 0) {
1174 *(Destination++) = *(Source++);
1175 Length--;
1176 }
1177
1178 ZeroMem (Destination, Length * sizeof (*Destination));
1179 return ReturnValue;
1180 }
1181
1182 /**
1183 Returns the length of a Null-terminated ASCII string.
1184
1185 This function returns the number of ASCII characters in the Null-terminated
1186 ASCII string specified by String.
1187
1188 If Length > 0 and Destination is NULL, then ASSERT().
1189 If Length > 0 and Source is NULL, then ASSERT().
1190 If PcdMaximumAsciiStringLength is not zero and String contains more than
1191 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1192 then ASSERT().
1193
1194 @param String A pointer to a Null-terminated ASCII string.
1195
1196 @return The length of String.
1197
1198 **/
1199 UINTN
1200 EFIAPI
1201 AsciiStrLen (
1202 IN CONST CHAR8 *String
1203 )
1204 {
1205 UINTN Length;
1206
1207 ASSERT (String != NULL);
1208
1209 for (Length = 0; *String != '\0'; String++, Length++) {
1210 //
1211 // If PcdMaximumUnicodeStringLength is not zero,
1212 // length should not more than PcdMaximumUnicodeStringLength
1213 //
1214 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {
1215 ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength));
1216 }
1217 }
1218 return Length;
1219 }
1220
1221 /**
1222 Returns the size of a Null-terminated ASCII string in bytes, including the
1223 Null terminator.
1224
1225 This function returns the size, in bytes, of the Null-terminated ASCII string
1226 specified by String.
1227
1228 If String is NULL, then ASSERT().
1229 If PcdMaximumAsciiStringLength is not zero and String contains more than
1230 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1231 then ASSERT().
1232
1233 @param String A pointer to a Null-terminated ASCII string.
1234
1235 @return The size of String.
1236
1237 **/
1238 UINTN
1239 EFIAPI
1240 AsciiStrSize (
1241 IN CONST CHAR8 *String
1242 )
1243 {
1244 return (AsciiStrLen (String) + 1) * sizeof (*String);
1245 }
1246
1247 /**
1248 Compares two Null-terminated ASCII strings, and returns the difference
1249 between the first mismatched ASCII characters.
1250
1251 This function compares the Null-terminated ASCII string FirstString to the
1252 Null-terminated ASCII string SecondString. If FirstString is identical to
1253 SecondString, then 0 is returned. Otherwise, the value returned is the first
1254 mismatched ASCII character in SecondString subtracted from the first
1255 mismatched ASCII character in FirstString.
1256
1257 If FirstString is NULL, then ASSERT().
1258 If SecondString is NULL, then ASSERT().
1259 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1260 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1261 then ASSERT().
1262 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1263 than PcdMaximumAsciiStringLength ASCII characters, not including the
1264 Null-terminator, then ASSERT().
1265
1266 @param FirstString A pointer to a Null-terminated ASCII string.
1267 @param SecondString A pointer to a Null-terminated ASCII string.
1268
1269 @retval ==0 FirstString is identical to SecondString.
1270 @retval !=0 FirstString is not identical to SecondString.
1271
1272 **/
1273 INTN
1274 EFIAPI
1275 AsciiStrCmp (
1276 IN CONST CHAR8 *FirstString,
1277 IN CONST CHAR8 *SecondString
1278 )
1279 {
1280 //
1281 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1282 //
1283 ASSERT (AsciiStrSize (FirstString));
1284 ASSERT (AsciiStrSize (SecondString));
1285
1286 while ((*FirstString != '\0') && (*FirstString == *SecondString)) {
1287 FirstString++;
1288 SecondString++;
1289 }
1290
1291 return *FirstString - *SecondString;
1292 }
1293
1294 /**
1295 Converts a lowercase Ascii character to upper one.
1296
1297 If Chr is lowercase Ascii character, then converts it to upper one.
1298
1299 If Value >= 0xA0, then ASSERT().
1300 If (Value & 0x0F) >= 0x0A, then ASSERT().
1301
1302 @param Chr one Ascii character
1303
1304 @return The uppercase value of Ascii character
1305
1306 **/
1307 CHAR8
1308 EFIAPI
1309 InternalBaseLibAsciiToUpper (
1310 IN CHAR8 Chr
1311 )
1312 {
1313 return (UINT8) ((Chr >= 'a' && Chr <= 'z') ? Chr - ('a' - 'A') : Chr);
1314 }
1315
1316 /**
1317 Convert a ASCII character to numerical value.
1318
1319 This internal function only deal with Unicode character
1320 which maps to a valid hexadecimal ASII character, i.e.
1321 '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
1322 ASCII character, the value returned does not make sense.
1323
1324 @param Char The character to convert.
1325
1326 @return The numerical value converted.
1327
1328 **/
1329 UINTN
1330 EFIAPI
1331 InternalAsciiHexCharToUintn (
1332 IN CHAR8 Char
1333 )
1334 {
1335 if (InternalIsDecimalDigitCharacter (Char)) {
1336 return Char - '0';
1337 }
1338
1339 return (UINTN) (10 + InternalBaseLibAsciiToUpper (Char) - 'A');
1340 }
1341
1342
1343 /**
1344 Performs a case insensitive comparison of two Null-terminated ASCII strings,
1345 and returns the difference between the first mismatched ASCII characters.
1346
1347 This function performs a case insensitive comparison of the Null-terminated
1348 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
1349 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
1350 value returned is the first mismatched lower case ASCII character in
1351 SecondString subtracted from the first mismatched lower case ASCII character
1352 in FirstString.
1353
1354 If FirstString is NULL, then ASSERT().
1355 If SecondString is NULL, then ASSERT().
1356 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
1357 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1358 then ASSERT().
1359 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
1360 than PcdMaximumAsciiStringLength ASCII characters, not including the
1361 Null-terminator, then ASSERT().
1362
1363 @param FirstString A pointer to a Null-terminated ASCII string.
1364 @param SecondString A pointer to a Null-terminated ASCII string.
1365
1366 @retval ==0 FirstString is identical to SecondString using case insensitive
1367 comparisons.
1368 @retval !=0 FirstString is not identical to SecondString using case
1369 insensitive comparisons.
1370
1371 **/
1372 INTN
1373 EFIAPI
1374 AsciiStriCmp (
1375 IN CONST CHAR8 *FirstString,
1376 IN CONST CHAR8 *SecondString
1377 )
1378 {
1379 CHAR8 UpperFirstString;
1380 CHAR8 UpperSecondString;
1381
1382 //
1383 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1384 //
1385 ASSERT (AsciiStrSize (FirstString));
1386 ASSERT (AsciiStrSize (SecondString));
1387
1388 UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);
1389 UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);
1390 while ((*FirstString != '\0') && (UpperFirstString == UpperSecondString)) {
1391 FirstString++;
1392 SecondString++;
1393 UpperFirstString = InternalBaseLibAsciiToUpper (*FirstString);
1394 UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);
1395 }
1396
1397 return UpperFirstString - UpperSecondString;
1398 }
1399
1400 /**
1401 Compares two Null-terminated ASCII strings with maximum lengths, and returns
1402 the difference between the first mismatched ASCII characters.
1403
1404 This function compares the Null-terminated ASCII string FirstString to the
1405 Null-terminated ASCII string SecondString. At most, Length ASCII characters
1406 will be compared. If Length is 0, then 0 is returned. If FirstString is
1407 identical to SecondString, then 0 is returned. Otherwise, the value returned
1408 is the first mismatched ASCII character in SecondString subtracted from the
1409 first mismatched ASCII character in FirstString.
1410
1411 If Length > 0 and FirstString is NULL, then ASSERT().
1412 If Length > 0 and SecondString is NULL, then ASSERT().
1413 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1414 PcdMaximumAsciiStringLength, then ASSERT().
1415 If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than
1416 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1417 then ASSERT().
1418 If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than
1419 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1420 then ASSERT().
1421
1422 @param FirstString A pointer to a Null-terminated ASCII string.
1423 @param SecondString A pointer to a Null-terminated ASCII string.
1424 @param Length The maximum number of ASCII characters for compare.
1425
1426 @retval ==0 FirstString is identical to SecondString.
1427 @retval !=0 FirstString is not identical to SecondString.
1428
1429 **/
1430 INTN
1431 EFIAPI
1432 AsciiStrnCmp (
1433 IN CONST CHAR8 *FirstString,
1434 IN CONST CHAR8 *SecondString,
1435 IN UINTN Length
1436 )
1437 {
1438 if (Length == 0) {
1439 return 0;
1440 }
1441
1442 //
1443 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1444 //
1445 ASSERT (AsciiStrSize (FirstString));
1446 ASSERT (AsciiStrSize (SecondString));
1447
1448 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {
1449 ASSERT (Length <= PcdGet32 (PcdMaximumAsciiStringLength));
1450 }
1451
1452 while ((*FirstString != '\0') &&
1453 (*FirstString == *SecondString) &&
1454 (Length > 1)) {
1455 FirstString++;
1456 SecondString++;
1457 Length--;
1458 }
1459 return *FirstString - *SecondString;
1460 }
1461
1462 /**
1463 Concatenates one Null-terminated ASCII string to another Null-terminated
1464 ASCII string, and returns the concatenated ASCII string.
1465
1466 This function concatenates two Null-terminated ASCII strings. The contents of
1467 Null-terminated ASCII string Source are concatenated to the end of Null-
1468 terminated ASCII string Destination. The Null-terminated concatenated ASCII
1469 String is returned.
1470
1471 If Destination is NULL, then ASSERT().
1472 If Source is NULL, then ASSERT().
1473 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
1474 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1475 then ASSERT().
1476 If PcdMaximumAsciiStringLength is not zero and Source contains more than
1477 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1478 then ASSERT().
1479 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
1480 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1481 ASCII characters, then ASSERT().
1482
1483 @param Destination A pointer to a Null-terminated ASCII string.
1484 @param Source A pointer to a Null-terminated ASCII string.
1485
1486 @return Destination
1487
1488 **/
1489 CHAR8 *
1490 EFIAPI
1491 AsciiStrCat (
1492 IN OUT CHAR8 *Destination,
1493 IN CONST CHAR8 *Source
1494 )
1495 {
1496 AsciiStrCpy (Destination + AsciiStrLen (Destination), Source);
1497
1498 //
1499 // Size of the resulting string should never be zero.
1500 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1501 //
1502 ASSERT (AsciiStrSize (Destination) != 0);
1503 return Destination;
1504 }
1505
1506 /**
1507 Concatenates up to a specified length one Null-terminated ASCII string to
1508 the end of another Null-terminated ASCII string, and returns the
1509 concatenated ASCII string.
1510
1511 This function concatenates two Null-terminated ASCII strings. The contents
1512 of Null-terminated ASCII string Source are concatenated to the end of Null-
1513 terminated ASCII string Destination, and Destination is returned. At most,
1514 Length ASCII characters are concatenated from Source to the end of
1515 Destination, and Destination is always Null-terminated. If Length is 0, then
1516 Destination is returned unmodified. If Source and Destination overlap, then
1517 the results are undefined.
1518
1519 If Length > 0 and Destination is NULL, then ASSERT().
1520 If Length > 0 and Source is NULL, then ASSERT().
1521 If Source and Destination overlap, then ASSERT().
1522 If PcdMaximumAsciiStringLength is not zero, and Length is greater than
1523 PcdMaximumAsciiStringLength, then ASSERT().
1524 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
1525 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1526 then ASSERT().
1527 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1528 PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
1529 then ASSERT().
1530 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
1531 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
1532 ASCII characters, not including the Null-terminator, then ASSERT().
1533
1534 @param Destination A pointer to a Null-terminated ASCII string.
1535 @param Source A pointer to a Null-terminated ASCII string.
1536 @param Length The maximum number of ASCII characters to concatenate from
1537 Source.
1538
1539 @return Destination
1540
1541 **/
1542 CHAR8 *
1543 EFIAPI
1544 AsciiStrnCat (
1545 IN OUT CHAR8 *Destination,
1546 IN CONST CHAR8 *Source,
1547 IN UINTN Length
1548 )
1549 {
1550 UINTN DestinationLen;
1551
1552 DestinationLen = AsciiStrLen (Destination);
1553 AsciiStrnCpy (Destination + DestinationLen, Source, Length);
1554 Destination[DestinationLen + Length] = '\0';
1555
1556 //
1557 // Size of the resulting string should never be zero.
1558 // PcdMaximumUnicodeStringLength is tested inside StrLen().
1559 //
1560 ASSERT (AsciiStrSize (Destination) != 0);
1561 return Destination;
1562 }
1563
1564 /**
1565 Returns the first occurrence of a Null-terminated ASCII sub-string
1566 in a Null-terminated ASCII string.
1567
1568 This function scans the contents of the ASCII string specified by String
1569 and returns the first occurrence of SearchString. If SearchString is not
1570 found in String, then NULL is returned. If the length of SearchString is zero,
1571 then String is returned.
1572
1573 If String is NULL, then ASSERT().
1574 If SearchString is NULL, then ASSERT().
1575
1576 If PcdMaximumAsciiStringLength is not zero, and SearchString or
1577 String contains more than PcdMaximumAsciiStringLength Unicode characters
1578 not including the Null-terminator, then ASSERT().
1579
1580 @param String A pointer to a Null-terminated ASCII string.
1581 @param SearchString A pointer to a Null-terminated ASCII string to search for.
1582
1583 @retval NULL If the SearchString does not appear in String.
1584 @retval others If there is a match return the first occurrence of SearchingString.
1585 If the length of SearchString is zero,return String.
1586
1587 **/
1588 CHAR8 *
1589 EFIAPI
1590 AsciiStrStr (
1591 IN CONST CHAR8 *String,
1592 IN CONST CHAR8 *SearchString
1593 )
1594 {
1595 CONST CHAR8 *FirstMatch;
1596 CONST CHAR8 *SearchStringTmp;
1597
1598 //
1599 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
1600 //
1601 ASSERT (AsciiStrSize (String) != 0);
1602 ASSERT (AsciiStrSize (SearchString) != 0);
1603
1604 if (*SearchString == '\0') {
1605 return (CHAR8 *) String;
1606 }
1607
1608 while (*String != '\0') {
1609 SearchStringTmp = SearchString;
1610 FirstMatch = String;
1611
1612 while ((*String == *SearchStringTmp)
1613 && (*String != '\0')) {
1614 String++;
1615 SearchStringTmp++;
1616 }
1617
1618 if (*SearchStringTmp == '\0') {
1619 return (CHAR8 *) FirstMatch;
1620 }
1621
1622 if (*String == '\0') {
1623 return NULL;
1624 }
1625
1626 String = FirstMatch + 1;
1627 }
1628
1629 return NULL;
1630 }
1631
1632 /**
1633 Convert a Null-terminated ASCII decimal string to a value of type
1634 UINTN.
1635
1636 This function returns a value of type UINTN by interpreting the contents
1637 of the ASCII string String as a decimal number. The format of the input
1638 ASCII string String is:
1639
1640 [spaces] [decimal digits].
1641
1642 The valid decimal digit character is in the range [0-9]. The function will
1643 ignore the pad space, which includes spaces or tab characters, before the digits.
1644 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1645 function stops at the first character that is a not a valid decimal character or
1646 Null-terminator, whichever on comes first.
1647
1648 If String has only pad spaces, then 0 is returned.
1649 If String has no pad spaces or valid decimal digits, then 0 is returned.
1650 If the number represented by String overflows according to the range defined by
1651 UINTN, then ASSERT().
1652 If String is NULL, then ASSERT().
1653 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1654 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1655 then ASSERT().
1656
1657 @param String A pointer to a Null-terminated ASCII string.
1658
1659 @retval Value translated from String.
1660
1661 **/
1662 UINTN
1663 EFIAPI
1664 AsciiStrDecimalToUintn (
1665 IN CONST CHAR8 *String
1666 )
1667 {
1668 UINTN Result;
1669
1670 //
1671 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1672 //
1673 ASSERT (AsciiStrSize (String) != 0);
1674
1675 //
1676 // Ignore the pad spaces (space or tab)
1677 //
1678 while ((*String == ' ') || (*String == '\t' )) {
1679 String++;
1680 }
1681
1682 //
1683 // Ignore leading Zeros after the spaces
1684 //
1685 while (*String == '0') {
1686 String++;
1687 }
1688
1689 Result = 0;
1690
1691 while (InternalAsciiIsDecimalDigitCharacter (*String)) {
1692 //
1693 // If the number represented by String overflows according
1694 // to the range defined by UINTN, then ASSERT().
1695 //
1696 ASSERT (Result <= ((((UINTN) ~0) - (*String - L'0')) / 10));
1697
1698 Result = Result * 10 + (*String - '0');
1699 String++;
1700 }
1701
1702 return Result;
1703 }
1704
1705
1706 /**
1707 Convert a Null-terminated ASCII decimal string to a value of type
1708 UINT64.
1709
1710 This function returns a value of type UINT64 by interpreting the contents
1711 of the ASCII string String as a decimal number. The format of the input
1712 ASCII string String is:
1713
1714 [spaces] [decimal digits].
1715
1716 The valid decimal digit character is in the range [0-9]. The function will
1717 ignore the pad space, which includes spaces or tab characters, before the digits.
1718 The running zero in the beginning of [decimal digits] will be ignored. Then, the
1719 function stops at the first character that is a not a valid decimal character or
1720 Null-terminator, whichever on comes first.
1721
1722 If String has only pad spaces, then 0 is returned.
1723 If String has no pad spaces or valid decimal digits, then 0 is returned.
1724 If the number represented by String overflows according to the range defined by
1725 UINT64, then ASSERT().
1726 If String is NULL, then ASSERT().
1727 If PcdMaximumAsciiStringLength is not zero, and String contains more than
1728 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1729 then ASSERT().
1730
1731 @param String A pointer to a Null-terminated ASCII string.
1732
1733 @retval Value translated from String.
1734
1735 **/
1736 UINT64
1737 EFIAPI
1738 AsciiStrDecimalToUint64 (
1739 IN CONST CHAR8 *String
1740 )
1741 {
1742 UINT64 Result;
1743
1744 //
1745 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1746 //
1747 ASSERT (AsciiStrSize (String) != 0);
1748
1749 //
1750 // Ignore the pad spaces (space or tab)
1751 //
1752 while ((*String == ' ') || (*String == '\t' )) {
1753 String++;
1754 }
1755
1756 //
1757 // Ignore leading Zeros after the spaces
1758 //
1759 while (*String == '0') {
1760 String++;
1761 }
1762
1763 Result = 0;
1764
1765 while (InternalAsciiIsDecimalDigitCharacter (*String)) {
1766 //
1767 // If the number represented by String overflows according
1768 // to the range defined by UINTN, then ASSERT().
1769 //
1770 ASSERT (Result <= DivU64x32 (((UINT64) ~0) - (*String - L'0') , 10));
1771
1772 Result = MultU64x32 (Result, 10) + (*String - '0');
1773 String++;
1774 }
1775
1776 return Result;
1777 }
1778
1779 /**
1780 Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
1781
1782 This function returns a value of type UINTN by interpreting the contents of
1783 the ASCII string String as a hexadecimal number. The format of the input ASCII
1784 string String is:
1785
1786 [spaces][zeros][x][hexadecimal digits].
1787
1788 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1789 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1790 appears in the input string, it must be prefixed with at least one 0. The function
1791 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1792 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1793 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1794 digit. Then, the function stops at the first character that is a not a valid
1795 hexadecimal character or Null-terminator, whichever on comes first.
1796
1797 If String has only pad spaces, then 0 is returned.
1798 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1799 0 is returned.
1800
1801 If the number represented by String overflows according to the range defined by UINTN,
1802 then ASSERT().
1803 If String is NULL, then ASSERT().
1804 If PcdMaximumAsciiStringLength is not zero,
1805 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1806 the Null-terminator, then ASSERT().
1807
1808 @param String A pointer to a Null-terminated ASCII string.
1809
1810 @retval Value translated from String.
1811
1812 **/
1813 UINTN
1814 EFIAPI
1815 AsciiStrHexToUintn (
1816 IN CONST CHAR8 *String
1817 )
1818 {
1819 UINTN Result;
1820
1821 //
1822 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1823 //
1824 ASSERT (AsciiStrSize (String) != 0);
1825
1826 //
1827 // Ignore the pad spaces (space or tab)
1828 //
1829 while ((*String == ' ') || (*String == '\t' )) {
1830 String++;
1831 }
1832
1833 //
1834 // Ignore leading Zeros after the spaces
1835 //
1836 while (*String == '0') {
1837 String++;
1838 }
1839
1840 if (InternalBaseLibAsciiToUpper (*String) == 'X') {
1841 ASSERT (*(String - 1) == '0');
1842 if (*(String - 1) != '0') {
1843 return 0;
1844 }
1845 //
1846 // Skip the 'X'
1847 //
1848 String++;
1849 }
1850
1851 Result = 0;
1852
1853 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {
1854 //
1855 // If the Hex Number represented by String overflows according
1856 // to the range defined by UINTN, then ASSERT().
1857 //
1858 ASSERT (Result <= ((((UINTN) ~0) - InternalHexCharToUintn (*String)) >> 4));
1859
1860 Result = (Result << 4) + InternalAsciiHexCharToUintn (*String);
1861 String++;
1862 }
1863
1864 return Result;
1865 }
1866
1867
1868 /**
1869 Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.
1870
1871 This function returns a value of type UINT64 by interpreting the contents of
1872 the ASCII string String as a hexadecimal number. The format of the input ASCII
1873 string String is:
1874
1875 [spaces][zeros][x][hexadecimal digits].
1876
1877 The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1878 The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1879 appears in the input string, it must be prefixed with at least one 0. The function
1880 will ignore the pad space, which includes spaces or tab characters, before [zeros],
1881 [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1882 will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1883 digit. Then, the function stops at the first character that is a not a valid
1884 hexadecimal character or Null-terminator, whichever on comes first.
1885
1886 If String has only pad spaces, then 0 is returned.
1887 If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1888 0 is returned.
1889
1890 If the number represented by String overflows according to the range defined by UINT64,
1891 then ASSERT().
1892 If String is NULL, then ASSERT().
1893 If PcdMaximumAsciiStringLength is not zero,
1894 and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1895 the Null-terminator, then ASSERT().
1896
1897 @param String A pointer to a Null-terminated ASCII string.
1898
1899 @retval Value translated from String.
1900
1901 **/
1902 UINT64
1903 EFIAPI
1904 AsciiStrHexToUint64 (
1905 IN CONST CHAR8 *String
1906 )
1907 {
1908 UINT64 Result;
1909
1910 //
1911 // ASSERT Strings is less long than PcdMaximumAsciiStringLength
1912 //
1913 ASSERT (AsciiStrSize (String) != 0);
1914
1915 //
1916 // Ignore the pad spaces (space or tab) and leading Zeros
1917 //
1918 //
1919 // Ignore the pad spaces (space or tab)
1920 //
1921 while ((*String == ' ') || (*String == '\t' )) {
1922 String++;
1923 }
1924
1925 //
1926 // Ignore leading Zeros after the spaces
1927 //
1928 while (*String == '0') {
1929 String++;
1930 }
1931
1932 if (InternalBaseLibAsciiToUpper (*String) == 'X') {
1933 ASSERT (*(String - 1) == '0');
1934 if (*(String - 1) != '0') {
1935 return 0;
1936 }
1937 //
1938 // Skip the 'X'
1939 //
1940 String++;
1941 }
1942
1943 Result = 0;
1944
1945 while (InternalAsciiIsHexaDecimalDigitCharacter (*String)) {
1946 //
1947 // If the Hex Number represented by String overflows according
1948 // to the range defined by UINTN, then ASSERT().
1949 //
1950 ASSERT (Result <= RShiftU64 (((UINT64) ~0) - InternalHexCharToUintn (*String) , 4));
1951
1952 Result = LShiftU64 (Result, 4);
1953 Result = Result + InternalAsciiHexCharToUintn (*String);
1954 String++;
1955 }
1956
1957 return Result;
1958 }
1959
1960
1961 /**
1962 Convert one Null-terminated ASCII string to a Null-terminated
1963 Unicode string and returns the Unicode string.
1964
1965 This function converts the contents of the ASCII string Source to the Unicode
1966 string Destination, and returns Destination. The function terminates the
1967 Unicode string Destination by appending a Null-terminator character at the end.
1968 The caller is responsible to make sure Destination points to a buffer with size
1969 equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.
1970
1971 If Destination is NULL, then ASSERT().
1972 If Destination is not aligned on a 16-bit boundary, then ASSERT().
1973 If Source is NULL, then ASSERT().
1974 If Source and Destination overlap, then ASSERT().
1975 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
1976 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1977 then ASSERT().
1978 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
1979 PcdMaximumUnicodeStringLength ASCII characters not including the
1980 Null-terminator, then ASSERT().
1981
1982 @param Source A pointer to a Null-terminated ASCII string.
1983 @param Destination A pointer to a Null-terminated Unicode string.
1984
1985 @return Destination.
1986
1987 **/
1988 CHAR16 *
1989 EFIAPI
1990 AsciiStrToUnicodeStr (
1991 IN CONST CHAR8 *Source,
1992 OUT CHAR16 *Destination
1993 )
1994 {
1995 CHAR16 *ReturnValue;
1996
1997 ASSERT (Destination != NULL);
1998
1999 //
2000 // ASSERT Source is less long than PcdMaximumAsciiStringLength
2001 //
2002 ASSERT (AsciiStrSize (Source) != 0);
2003
2004 //
2005 // Source and Destination should not overlap
2006 //
2007 ASSERT ((UINTN) ((CHAR8 *) Destination - Source) > AsciiStrLen (Source));
2008 ASSERT ((UINTN) (Source - (CHAR8 *) Destination) > (AsciiStrLen (Source) * sizeof (CHAR16)));
2009
2010
2011 ReturnValue = Destination;
2012 while (*Source != '\0') {
2013 *(Destination++) = (CHAR16) *(Source++);
2014 }
2015 //
2016 // End the Destination with a NULL.
2017 //
2018 *Destination = '\0';
2019
2020 //
2021 // ASSERT Original Destination is less long than PcdMaximumUnicodeStringLength
2022 //
2023 ASSERT (StrSize (ReturnValue) != 0);
2024
2025 return ReturnValue;
2026 }
2027
2028 /**
2029 Converts an 8-bit value to an 8-bit BCD value.
2030
2031 Converts the 8-bit value specified by Value to BCD. The BCD value is
2032 returned.
2033
2034 If Value >= 100, then ASSERT().
2035
2036 @param Value The 8-bit value to convert to BCD. Range 0..99.
2037
2038 @return The BCD value.
2039
2040 **/
2041 UINT8
2042 EFIAPI
2043 DecimalToBcd8 (
2044 IN UINT8 Value
2045 )
2046 {
2047 ASSERT (Value < 100);
2048 return (UINT8) (((Value / 10) << 4) | (Value % 10));
2049 }
2050
2051 /**
2052 Converts an 8-bit BCD value to an 8-bit value.
2053
2054 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
2055 value is returned.
2056
2057 If Value >= 0xA0, then ASSERT().
2058 If (Value & 0x0F) >= 0x0A, then ASSERT().
2059
2060 @param Value The 8-bit BCD value to convert to an 8-bit value.
2061
2062 @return The 8-bit value is returned.
2063
2064 **/
2065 UINT8
2066 EFIAPI
2067 BcdToDecimal8 (
2068 IN UINT8 Value
2069 )
2070 {
2071 ASSERT (Value < 0xa0);
2072 ASSERT ((Value & 0xf) < 0xa);
2073 return (UINT8) ((Value >> 4) * 10 + (Value & 0xf));
2074 }