]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/String.c
Optimized HighBitSetXX() functions
[mirror_edk2.git] / MdePkg / Library / BaseLib / String.c
1 /** @file
2 Unicode string primatives.
3
4 Copyright (c) 2006, 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 Module Name: String.c
14
15 **/
16
17 /**
18 Copies one Null-terminated Unicode string to another Null-terminated Unicode
19 string and returns the new Unicode string.
20
21 This function copies the contents of the Unicode string Source to the Unicode
22 string Destination, and returns Destination. If Source and Destination
23 overlap, then the results are undefined.
24
25 If Destination is NULL, then ASSERT().
26 If Source is NULL, then ASSERT().
27 If Source and Destination overlap, then ASSERT().
28 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
29 PcdMaximumUnicodeStringLength Unicode characters not including the
30 Null-terminator, then ASSERT().
31
32 @param Destination Pointer to a Null-terminated Unicode string.
33 @param Source Pointer to a Null-terminated Unicode string.
34
35 @return Destiantion
36
37 **/
38 CHAR16 *
39 EFIAPI
40 StrCpy (
41 OUT CHAR16 *Destination,
42 IN CONST CHAR16 *Source
43 )
44 {
45 CHAR16 *ReturnValue;
46
47 //
48 // Destination cannot be NULL
49 //
50 ASSERT (Destination != NULL);
51
52 //
53 // Destination and source cannot overlap
54 //
55 ASSERT ((UINTN)(Destination - Source) > StrLen (Source));
56 ASSERT ((UINTN)(Source - Destination) > StrLen (Source));
57
58 ReturnValue = Destination;
59 while (*Source) {
60 *(Destination++) = *(Source++);
61 }
62 *Destination = 0;
63 return ReturnValue;
64 }
65
66 /**
67 Copies one Null-terminated Unicode string with a maximum length to another
68 Null-terminated Unicode string with a maximum length and returns the new
69 Unicode string.
70
71 This function copies the contents of the Unicode string Source to the Unicode
72 string Destination, and returns Destination. At most, Length Unicode
73 characters are copied from Source to Destination. If Length is 0, then
74 Destination is returned unmodified. If Length is greater that the number of
75 Unicode characters in Source, then Destination is padded with Null Unicode
76 characters. If Source and Destination overlap, then the results are
77 undefined.
78
79 If Destination is NULL, then ASSERT().
80 If Source is NULL, then ASSERT().
81 If Source and Destination overlap, then ASSERT().
82 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
83 PcdMaximumUnicodeStringLength Unicode characters not including the
84 Null-terminator, then ASSERT().
85
86 @param Destination Pointer to a Null-terminated Unicode string.
87 @param Source Pointer to a Null-terminated Unicode string.
88 @param Length Maximum number of Unicode characters to copy.
89
90 @return Destination
91
92 **/
93 CHAR16 *
94 EFIAPI
95 StrnCpy (
96 OUT CHAR16 *Destination,
97 IN CONST CHAR16 *Source,
98 IN UINTN Length
99 )
100 {
101 CHAR16 *ReturnValue;
102
103 if (Length == 0) {
104 return Destination;
105 }
106
107 //
108 // Destination cannot be NULL if Length is not zero
109 //
110 ASSERT (Destination != NULL);
111
112 //
113 // Destination and source cannot overlap
114 // Q: Does Source have to be NULL-terminated?
115 //
116 ASSERT ((UINTN)(Destination - Source) > StrLen (Source));
117 ASSERT ((UINTN)(Source - Destination) >= Length);
118
119 ReturnValue = Destination;
120
121 while ((*Source != L'\0') && (Length > 0)) {
122 *(Destination++) = *(Source++);
123 Length--;
124 }
125
126 ZeroMem (Destination, Length * sizeof (*Destination));
127 return ReturnValue;
128 }
129
130 /**
131 Returns the length of a Null-terminated Unicode string.
132
133 This function returns the number of Unicode characters in the Null-terminated
134 Unicode string specified by String.
135
136 If String is NULL, then ASSERT().
137 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
138 PcdMaximumUnicodeStringLength Unicode characters not including the
139 Null-terminator, then ASSERT().
140
141 @param String Pointer to a Null-terminated Unicode string.
142
143 @return The length of String.
144
145 **/
146 UINTN
147 EFIAPI
148 StrLen (
149 IN CONST CHAR16 *String
150 )
151 {
152 UINTN Length;
153
154 ASSERT (String != NULL);
155
156 for (Length = 0; *String != L'\0'; String++, Length++) {
157 //
158 // If PcdMaximumUnicodeStringLength is not zero,
159 // length should not more than PcdMaximumUnicodeStringLength
160 //
161 if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {
162 ASSERT (Length < PcdGet32 (PcdMaximumUnicodeStringLength));
163 }
164 }
165 return Length;
166 }
167
168 /**
169 Returns the size of a Null-terminated Unicode string in bytes, including the
170 Null terminator.
171
172 This function returns the size, in bytes, of the Null-terminated Unicode
173 string specified by String.
174
175 If String is NULL, then ASSERT().
176 If PcdMaximumUnicodeStringLength is not zero, and String contains more than
177 PcdMaximumUnicodeStringLength Unicode characters not including the
178 Null-terminator, then ASSERT().
179
180 @param String Pointer to a Null-terminated Unicode string.
181
182 @return The size of String.
183
184 **/
185 UINTN
186 EFIAPI
187 StrSize (
188 IN CONST CHAR16 *String
189 )
190 {
191 return (StrLen (String) + 1) * sizeof (*String);
192 }
193
194 /**
195 Compares two Null-terminated Unicode strings, and returns the difference
196 between the first mismatched Unicode characters.
197
198 This function compares the Null-terminated Unicode string FirstString to the
199 Null-terminated Unicode string SecondString. If FirstString is identical to
200 SecondString, then 0 is returned. Otherwise, the value returned is the first
201 mismatched Unicode character in SecondString subtracted from the first
202 mismatched Unicode character in FirstString.
203
204 If FirstString is NULL, then ASSERT().
205 If SecondString is NULL, then ASSERT().
206 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
207 than PcdMaximumUnicodeStringLength Unicode characters not including the
208 Null-terminator, then ASSERT().
209 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
210 than PcdMaximumUnicodeStringLength Unicode characters not including the
211 Null-terminator, then ASSERT().
212
213 @param FirstString Pointer to a Null-terminated Unicode string.
214 @param SecondString Pointer to a Null-terminated Unicode string.
215
216 @retval 0 FirstString is identical to SecondString.
217 @retval !=0 FirstString is not identical to SecondString.
218
219 **/
220 INTN
221 EFIAPI
222 StrCmp (
223 IN CONST CHAR16 *FirstString,
224 IN CONST CHAR16 *SecondString
225 )
226 {
227 //
228 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength
229 //
230 ASSERT (StrSize (FirstString) != 0);
231 ASSERT (StrSize (SecondString) != 0);
232
233 while ((*FirstString != L'\0') && (*FirstString == *SecondString)) {
234 FirstString++;
235 SecondString++;
236 }
237 return *FirstString - *SecondString;
238 }
239
240 /**
241 Compares two Null-terminated Unicode strings with maximum lengths, and
242 returns the difference between the first mismatched Unicode characters.
243
244 This function compares the Null-terminated Unicode string FirstString to the
245 Null-terminated Unicode string SecondString. At most, Length Unicode
246 characters will be compared. If Length is 0, then 0 is returned. If
247 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
248 value returned is the first mismatched Unicode character in SecondString
249 subtracted from the first mismatched Unicode character in FirstString.
250
251 If FirstString is NULL, then ASSERT().
252 If SecondString is NULL, then ASSERT().
253 If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
254 than PcdMaximumUnicodeStringLength Unicode characters not including the
255 Null-terminator, then ASSERT().
256 If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
257 than PcdMaximumUnicodeStringLength Unicode characters not including the
258 Null-terminator, then ASSERT().
259
260 @param FirstString Pointer to a Null-terminated Unicode string.
261 @param SecondString Pointer to a Null-terminated Unicode string.
262 @param Length Maximum number of Unicode characters to compare.
263
264 @retval 0 FirstString is identical to SecondString.
265 @retval !=0 FirstString is not identical to SecondString.
266
267 **/
268 INTN
269 EFIAPI
270 StrnCmp (
271 IN CONST CHAR16 *FirstString,
272 IN CONST CHAR16 *SecondString,
273 IN UINTN Length
274 )
275 {
276 if (Length == 0) {
277 return 0;
278 }
279
280 //
281 // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
282 // Length tests are performed inside StrLen().
283 //
284 ASSERT (StrSize (FirstString) != 0);
285 ASSERT (StrSize (SecondString) != 0);
286
287 while ((*FirstString != L'\0') &&
288 (*FirstString == *SecondString) &&
289 (Length > 1)) {
290 FirstString++;
291 SecondString++;
292 Length--;
293 }
294
295 return *FirstString - *SecondString;
296 }
297
298 /**
299 Concatenates one Null-terminated Unicode string to another Null-terminated
300 Unicode string, and returns the concatenated Unicode string.
301
302 This function concatenates two Null-terminated Unicode strings. The contents
303 of Null-terminated Unicode string Source are concatenated to the end of
304 Null-terminated Unicode string Destination. The Null-terminated concatenated
305 Unicode String is returned. If Source and Destination overlap, then the
306 results are undefined.
307
308 If Destination is NULL, then ASSERT().
309 If Source is NULL, then ASSERT().
310 If Source and Destination overlap, then ASSERT().
311 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
312 than PcdMaximumUnicodeStringLength Unicode characters not including the
313 Null-terminator, then ASSERT().
314 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
315 PcdMaximumUnicodeStringLength Unicode characters not including the
316 Null-terminator, then ASSERT().
317 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
318 and Source results in a Unicode string with more than
319 PcdMaximumUnicodeStringLength Unicode characters not including the
320 Null-terminator, then ASSERT().
321
322 @param Destination Pointer to a Null-terminated Unicode string.
323 @param Source Pointer to a Null-terminated Unicode string.
324
325 @return Destination
326
327 **/
328 CHAR16 *
329 EFIAPI
330 StrCat (
331 IN OUT CHAR16 *Destination,
332 IN CONST CHAR16 *Source
333 )
334 {
335 StrCpy (Destination + StrLen (Destination), Source);
336
337 //
338 // Size of the resulting string should never be zero.
339 // PcdMaximumUnicodeStringLength is tested inside StrLen().
340 //
341 ASSERT (StrSize (Destination) != 0);
342 return Destination;
343 }
344
345 /**
346 Concatenates one Null-terminated Unicode string with a maximum length to the
347 end of another Null-terminated Unicode string, and returns the concatenated
348 Unicode string.
349
350 This function concatenates two Null-terminated Unicode strings. The contents
351 of Null-terminated Unicode string Source are concatenated to the end of
352 Null-terminated Unicode string Destination, and Destination is returned. At
353 most, Length Unicode characters are concatenated from Source to the end of
354 Destination, and Destination is always Null-terminated. If Length is 0, then
355 Destination is returned unmodified. If Source and Destination overlap, then
356 the results are undefined.
357
358 If Destination is NULL, then ASSERT().
359 If Source is NULL, then ASSERT().
360 If Source and Destination overlap, then ASSERT().
361 If PcdMaximumUnicodeStringLength is not zero, and Destination contains more
362 than PcdMaximumUnicodeStringLength Unicode characters not including the
363 Null-terminator, then ASSERT().
364 If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
365 PcdMaximumUnicodeStringLength Unicode characters not including the
366 Null-terminator, then ASSERT().
367 If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination
368 and Source results in a Unicode string with more than
369 PcdMaximumUnicodeStringLength Unicode characters not including the
370 Null-terminator, then ASSERT().
371
372 @param Destination Pointer to a Null-terminated Unicode string.
373 @param Source Pointer to a Null-terminated Unicode string.
374 @param Length Maximum number of Unicode characters to concatenate from
375 Source.
376
377 @return Destination
378
379 **/
380 CHAR16 *
381 EFIAPI
382 StrnCat (
383 IN OUT CHAR16 *Destination,
384 IN CONST CHAR16 *Source,
385 IN UINTN Length
386 )
387 {
388 StrnCpy (Destination + StrLen (Destination), Source, Length);
389
390 //
391 // Size of the resulting string should never be zero.
392 // PcdMaximumUnicodeStringLength is tested inside StrLen().
393 //
394 ASSERT (StrSize (Destination) != 0);
395 return Destination;
396 }
397
398 /**
399 Copies one Null-terminated ASCII string to another Null-terminated ASCII
400 string and returns the new ASCII string.
401
402 This function copies the contents of the ASCII string Source to the ASCII
403 string Destination, and returns Destination. If Source and Destination
404 overlap, then the results are undefined.
405
406 If Destination is NULL, then ASSERT().
407 If Source is NULL, then ASSERT().
408 If Source and Destination overlap, then ASSERT().
409 If PcdMaximumAsciiStringLength is not zero and Source contains more than
410 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
411 then ASSERT().
412
413 @param Destination Pointer to a Null-terminated ASCII string.
414 @param Source Pointer to a Null-terminated ASCII string.
415
416 @return Destination
417
418 **/
419 CHAR8 *
420 EFIAPI
421 AsciiStrCpy (
422 OUT CHAR8 *Destination,
423 IN CONST CHAR8 *Source
424 )
425 {
426 CHAR8 *ReturnValue;
427
428 //
429 // Destination cannot be NULL
430 //
431 ASSERT (Destination != NULL);
432
433 //
434 // Destination and source cannot overlap
435 //
436 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));
437 ASSERT ((UINTN)(Source - Destination) > AsciiStrLen (Source));
438
439 ReturnValue = Destination;
440 while (*Source) {
441 *(Destination++) = *(Source++);
442 }
443 *Destination = 0;
444 return ReturnValue;
445 }
446
447 /**
448 Copies one Null-terminated ASCII string with a maximum length to another
449 Null-terminated ASCII string with a maximum length and returns the new ASCII
450 string.
451
452 This function copies the contents of the ASCII string Source to the ASCII
453 string Destination, and returns Destination. At most, Length ASCII characters
454 are copied from Source to Destination. If Length is 0, then Destination is
455 returned unmodified. If Length is greater that the number of ASCII characters
456 in Source, then Destination is padded with Null ASCII characters. If Source
457 and Destination overlap, then the results are undefined.
458
459 If Destination is NULL, then ASSERT().
460 If Source is NULL, then ASSERT().
461 If Source and Destination overlap, then ASSERT().
462 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
463 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
464 then ASSERT().
465
466 @param Destination Pointer to a Null-terminated ASCII string.
467 @param Source Pointer to a Null-terminated ASCII string.
468 @param Length Maximum number of ASCII characters to copy.
469
470 @return Destination
471
472 **/
473 CHAR8 *
474 EFIAPI
475 AsciiStrnCpy (
476 OUT CHAR8 *Destination,
477 IN CONST CHAR8 *Source,
478 IN UINTN Length
479 )
480 {
481 CHAR8 *ReturnValue;
482
483 if (Length == 0) {
484 return Destination;
485 }
486
487 //
488 // Destination cannot be NULL
489 //
490 ASSERT (Destination != NULL);
491
492 //
493 // Destination and source cannot overlap
494 //
495 ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));
496 ASSERT ((UINTN)(Source - Destination) >= Length);
497
498 ReturnValue = Destination;
499
500 while (*Source && Length > 0) {
501 *(Destination++) = *(Source++);
502 Length--;
503 }
504
505 ZeroMem (Destination, Length * sizeof (*Destination));
506 return ReturnValue;
507 }
508
509 /**
510 Returns the length of a Null-terminated ASCII string.
511
512 This function returns the number of ASCII characters in the Null-terminated
513 ASCII string specified by String.
514
515 If String is NULL, then ASSERT().
516 If PcdMaximumAsciiStringLength is not zero and String contains more than
517 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
518 then ASSERT().
519
520 @param String Pointer to a Null-terminated ASCII string.
521
522 @return The length of String.
523
524 **/
525 UINTN
526 EFIAPI
527 AsciiStrLen (
528 IN CONST CHAR8 *String
529 )
530 {
531 UINTN Length;
532
533 ASSERT (String != NULL);
534
535 for (Length = 0; *String != '\0'; String++, Length++) {
536 //
537 // If PcdMaximumUnicodeStringLength is not zero,
538 // length should not more than PcdMaximumUnicodeStringLength
539 //
540 if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {
541 ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength));
542 }
543 }
544 return Length;
545 }
546
547 /**
548 Returns the size of a Null-terminated ASCII string in bytes, including the
549 Null terminator.
550
551 This function returns the size, in bytes, of the Null-terminated ASCII string
552 specified by String.
553
554 If String is NULL, then ASSERT().
555 If PcdMaximumAsciiStringLength is not zero and String contains more than
556 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
557 then ASSERT().
558
559 @param String Pointer to a Null-terminated ASCII string.
560
561 @return The size of String.
562
563 **/
564 UINTN
565 EFIAPI
566 AsciiStrSize (
567 IN CONST CHAR8 *String
568 )
569 {
570 return (AsciiStrLen (String) + 1) * sizeof (*String);
571 }
572
573 /**
574 Compares two Null-terminated ASCII strings, and returns the difference
575 between the first mismatched ASCII characters.
576
577 This function compares the Null-terminated ASCII string FirstString to the
578 Null-terminated ASCII string SecondString. If FirstString is identical to
579 SecondString, then 0 is returned. Otherwise, the value returned is the first
580 mismatched ASCII character in SecondString subtracted from the first
581 mismatched ASCII character in FirstString.
582
583 If FirstString is NULL, then ASSERT().
584 If SecondString is NULL, then ASSERT().
585 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
586 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
587 then ASSERT().
588 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
589 than PcdMaximumAsciiStringLength ASCII characters not including the
590 Null-terminator, then ASSERT().
591
592 @param FirstString Pointer to a Null-terminated ASCII string.
593 @param SecondString Pointer to a Null-terminated ASCII string.
594
595 @retval 0 FirstString is identical to SecondString.
596 @retval !=0 FirstString is not identical to SecondString.
597
598 **/
599 INTN
600 EFIAPI
601 AsciiStrCmp (
602 IN CONST CHAR8 *FirstString,
603 IN CONST CHAR8 *SecondString
604 )
605 {
606 //
607 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
608 //
609 ASSERT (AsciiStrSize (FirstString));
610 ASSERT (AsciiStrSize (SecondString));
611
612 while ((*FirstString != '\0') && (*FirstString == *SecondString)) {
613 FirstString++;
614 SecondString++;
615 }
616
617 return *FirstString - *SecondString;
618 }
619
620 /**
621 Converts a lowercase Ascii character to upper one
622
623 If Chr is lowercase Ascii character, then converts it to upper one.
624
625 If Value >= 0xA0, then ASSERT().
626 If (Value & 0x0F) >= 0x0A, then ASSERT().
627
628 @param chr one Ascii character
629
630 @return The uppercase value of Ascii character
631
632 **/
633 STATIC
634 CHAR8
635 AsciiToUpper (
636 IN CHAR8 Chr
637 )
638 {
639 return (Chr >= 'a' && Chr <= 'z') ? Chr - ('a' - 'A') : Chr;
640 }
641
642 /**
643 Performs a case insensitive comparison of two Null-terminated ASCII strings,
644 and returns the difference between the first mismatched ASCII characters.
645
646 This function performs a case insensitive comparison of the Null-terminated
647 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
648 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
649 value returned is the first mismatched lower case ASCII character in
650 SecondString subtracted from the first mismatched lower case ASCII character
651 in FirstString.
652
653 If FirstString is NULL, then ASSERT().
654 If SecondString is NULL, then ASSERT().
655 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
656 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
657 then ASSERT().
658 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
659 than PcdMaximumAsciiStringLength ASCII characters not including the
660 Null-terminator, then ASSERT().
661
662 @param FirstString Pointer to a Null-terminated ASCII string.
663 @param SecondString Pointer to a Null-terminated ASCII string.
664
665 @retval 0 FirstString is identical to SecondString using case insensitive
666 comparisons.
667 @retval !=0 FirstString is not identical to SecondString using case
668 insensitive comparisons.
669
670 **/
671 INTN
672 EFIAPI
673 AsciiStriCmp (
674 IN CONST CHAR8 *FirstString,
675 IN CONST CHAR8 *SecondString
676 )
677 {
678 //
679 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
680 //
681 ASSERT (AsciiStrSize (FirstString));
682 ASSERT (AsciiStrSize (SecondString));
683
684 while ((*FirstString != '\0') &&
685 (AsciiToUpper (*FirstString) == AsciiToUpper (*SecondString))) {
686 FirstString++;
687 SecondString++;
688 }
689
690 return AsciiToUpper (*FirstString) - AsciiToUpper (*SecondString);
691 }
692
693 /**
694 Compares two Null-terminated ASCII strings with maximum lengths, and returns
695 the difference between the first mismatched ASCII characters.
696
697 This function compares the Null-terminated ASCII string FirstString to the
698 Null-terminated ASCII string SecondString. At most, Length ASCII characters
699 will be compared. If Length is 0, then 0 is returned. If FirstString is
700 identical to SecondString, then 0 is returned. Otherwise, the value returned
701 is the first mismatched ASCII character in SecondString subtracted from the
702 first mismatched ASCII character in FirstString.
703
704 If FirstString is NULL, then ASSERT().
705 If SecondString is NULL, then ASSERT().
706 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
707 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
708 then ASSERT().
709 If PcdMaximumAsciiStringLength is not zero and SecondString contains more than
710 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
711 then ASSERT().
712
713 @param FirstString Pointer to a Null-terminated ASCII string.
714 @param SecondString Pointer to a Null-terminated ASCII string.
715
716 @retval 0 FirstString is identical to SecondString.
717 @retval !=0 FirstString is not identical to SecondString.
718
719 **/
720 INTN
721 EFIAPI
722 AsciiStrnCmp (
723 IN CONST CHAR8 *FirstString,
724 IN CONST CHAR8 *SecondString,
725 IN UINTN Length
726 )
727 {
728 if (Length == 0) {
729 return 0;
730 }
731
732 //
733 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
734 //
735 ASSERT (AsciiStrSize (FirstString));
736 ASSERT (AsciiStrSize (SecondString));
737
738 while ((*FirstString != '\0') &&
739 (*FirstString == *SecondString) &&
740 (Length > 1)) {
741 FirstString++;
742 SecondString++;
743 Length--;
744 }
745 return *FirstString - *SecondString;
746 }
747
748 /**
749 Concatenates one Null-terminated ASCII string to another Null-terminated
750 ASCII string, and returns the concatenated ASCII string.
751
752 This function concatenates two Null-terminated ASCII strings. The contents of
753 Null-terminated ASCII string Source are concatenated to the end of Null-
754 terminated ASCII string Destination. The Null-terminated concatenated ASCII
755 String is returned.
756
757 If Destination is NULL, then ASSERT().
758 If Source is NULL, then ASSERT().
759 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
760 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
761 then ASSERT().
762 If PcdMaximumAsciiStringLength is not zero and Source contains more than
763 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
764 then ASSERT().
765 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
766 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
767 ASCII characters, then ASSERT().
768
769 @param Destination Pointer to a Null-terminated ASCII string.
770 @param Source Pointer to a Null-terminated ASCII string.
771
772 @return Destination
773
774 **/
775 CHAR8 *
776 EFIAPI
777 AsciiStrCat (
778 IN OUT CHAR8 *Destination,
779 IN CONST CHAR8 *Source
780 )
781 {
782 AsciiStrCpy (Destination + AsciiStrLen (Destination), Source);
783
784 //
785 // Size of the resulting string should never be zero.
786 // PcdMaximumUnicodeStringLength is tested inside StrLen().
787 //
788 ASSERT (AsciiStrSize (Destination) != 0);
789 return Destination;
790 }
791
792 /**
793 Concatenates one Null-terminated ASCII string with a maximum length to the
794 end of another Null-terminated ASCII string, and returns the concatenated
795 ASCII string.
796
797 This function concatenates two Null-terminated ASCII strings. The contents
798 of Null-terminated ASCII string Source are concatenated to the end of Null-
799 terminated ASCII string Destination, and Destination is returned. At most,
800 Length ASCII characters are concatenated from Source to the end of
801 Destination, and Destination is always Null-terminated. If Length is 0, then
802 Destination is returned unmodified. If Source and Destination overlap, then
803 the results are undefined.
804
805 If Destination is NULL, then ASSERT().
806 If Source is NULL, then ASSERT().
807 If Source and Destination overlap, then ASSERT().
808 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
809 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
810 then ASSERT().
811 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
812 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
813 then ASSERT().
814 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
815 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
816 ASCII characters not including the Null-terminator, then ASSERT().
817
818 @param Destination Pointer to a Null-terminated ASCII string.
819 @param Source Pointer to a Null-terminated ASCII string.
820 @param Length Maximum number of ASCII characters to concatenate from
821 Source.
822
823 @return Destination
824
825 **/
826 CHAR8 *
827 EFIAPI
828 AsciiStrnCat (
829 IN OUT CHAR8 *Destination,
830 IN CONST CHAR8 *Source,
831 IN UINTN Length
832 )
833 {
834 AsciiStrnCpy (Destination + AsciiStrLen (Destination), Source, Length);
835
836 //
837 // Size of the resulting string should never be zero.
838 // PcdMaximumUnicodeStringLength is tested inside StrLen().
839 //
840 ASSERT (AsciiStrSize (Destination) != 0);
841 return Destination;
842 }
843
844 /**
845 Converts an 8-bit value to an 8-bit BCD value.
846
847 Converts the 8-bit value specified by Value to BCD. The BCD value is
848 returned.
849
850 If Value >= 100, then ASSERT().
851
852 @param Value The 8-bit value to convert to BCD. Range 0..99.
853
854 @return The BCD value
855
856 **/
857 UINT8
858 EFIAPI
859 DecimalToBcd8 (
860 IN UINT8 Value
861 )
862 {
863 ASSERT (Value < 100);
864 return ((Value / 10) << 4) | (Value % 10);
865 }
866
867 /**
868 Converts an 8-bit BCD value to an 8-bit value.
869
870 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
871 value is returned.
872
873 If Value >= 0xA0, then ASSERT().
874 If (Value & 0x0F) >= 0x0A, then ASSERT().
875
876 @param Value The 8-bit BCD value to convert to an 8-bit value.
877
878 @return The 8-bit value is returned.
879
880 **/
881 UINT8
882 EFIAPI
883 BcdToDecimal8 (
884 IN UINT8 Value
885 )
886 {
887 ASSERT (Value < 0xa0);
888 ASSERT ((Value & 0xf) < 0xa);
889 return (Value >> 4) * 10 + (Value & 0xf);
890 }