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