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