In AsciiStrncmp(), if length=0, should return 0
[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 STATIC
621 CHAR8
622 EFIAPI
623 AsciiToUpper (
624 IN CHAR8 Chr
625 )
626 {
627 return (Chr >= 'a' && Chr <= 'z') ? Chr - ('a' - 'A') : Chr;
628 }
629
630 /**
631 Performs a case insensitive comparison of two Null-terminated ASCII strings,
632 and returns the difference between the first mismatched ASCII characters.
633
634 This function performs a case insensitive comparison of the Null-terminated
635 ASCII string FirstString to the Null-terminated ASCII string SecondString. If
636 FirstString is identical to SecondString, then 0 is returned. Otherwise, the
637 value returned is the first mismatched lower case ASCII character in
638 SecondString subtracted from the first mismatched lower case ASCII character
639 in FirstString.
640
641 If FirstString is NULL, then ASSERT().
642 If SecondString is NULL, then ASSERT().
643 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
644 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
645 then ASSERT().
646 If PcdMaximumAsciiStringLength is not zero and SecondString contains more
647 than PcdMaximumAsciiStringLength ASCII characters not including the
648 Null-terminator, then ASSERT().
649
650 @param FirstString Pointer to a Null-terminated ASCII string.
651 @param SecondString Pointer to a Null-terminated ASCII string.
652
653 @retval 0 FirstString is identical to SecondString using case insensitive
654 comparisons.
655 @retval !=0 FirstString is not identical to SecondString using case
656 insensitive comparisons.
657
658 **/
659 INTN
660 EFIAPI
661 AsciiStriCmp (
662 IN CONST CHAR8 *FirstString,
663 IN CONST CHAR8 *SecondString
664 )
665 {
666 //
667 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
668 //
669 ASSERT (AsciiStrSize (FirstString));
670 ASSERT (AsciiStrSize (SecondString));
671
672 while ((*FirstString != '\0') &&
673 (AsciiToUpper (*FirstString) == AsciiToUpper (*SecondString))) {
674 FirstString++;
675 SecondString++;
676 }
677
678 return AsciiToUpper (*FirstString) - AsciiToUpper (*SecondString);
679 }
680
681 /**
682 Compares two Null-terminated ASCII strings with maximum lengths, and returns
683 the difference between the first mismatched ASCII characters.
684
685 This function compares the Null-terminated ASCII string FirstString to the
686 Null-terminated ASCII string SecondString. At most, Length ASCII characters
687 will be compared. If Length is 0, then 0 is returned. If FirstString is
688 identical to SecondString, then 0 is returned. Otherwise, the value returned
689 is the first mismatched ASCII character in SecondString subtracted from the
690 first mismatched ASCII character in FirstString.
691
692 If FirstString is NULL, then ASSERT().
693 If SecondString is NULL, then ASSERT().
694 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
695 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
696 then ASSERT().
697 If PcdMaximumAsciiStringLength is not zero and SecondString contains more than
698 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
699 then ASSERT().
700
701 @param FirstString Pointer to a Null-terminated ASCII string.
702 @param SecondString Pointer to a Null-terminated ASCII string.
703
704 @retval 0 FirstString is identical to SecondString.
705 @retval !=0 FirstString is not identical to SecondString.
706
707 **/
708 INTN
709 EFIAPI
710 AsciiStrnCmp (
711 IN CONST CHAR8 *FirstString,
712 IN CONST CHAR8 *SecondString,
713 IN UINTN Length
714 )
715 {
716 if (Length == 0) {
717 return 0;
718 }
719
720 //
721 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
722 //
723 ASSERT (AsciiStrSize (FirstString));
724 ASSERT (AsciiStrSize (SecondString));
725
726 while ((*FirstString != '\0') &&
727 (*FirstString == *SecondString) &&
728 (Length > 1)) {
729 FirstString++;
730 SecondString++;
731 Length--;
732 }
733 return *FirstString - *SecondString;
734 }
735
736 /**
737 Concatenates one Null-terminated ASCII string to another Null-terminated
738 ASCII string, and returns the concatenated ASCII string.
739
740 This function concatenates two Null-terminated ASCII strings. The contents of
741 Null-terminated ASCII string Source are concatenated to the end of Null-
742 terminated ASCII string Destination. The Null-terminated concatenated ASCII
743 String is returned.
744
745 If Destination is NULL, then ASSERT().
746 If Source is NULL, then ASSERT().
747 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
748 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
749 then ASSERT().
750 If PcdMaximumAsciiStringLength is not zero and Source contains more than
751 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
752 then ASSERT().
753 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
754 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
755 ASCII characters, then ASSERT().
756
757 @param Destination Pointer to a Null-terminated ASCII string.
758 @param Source Pointer to a Null-terminated ASCII string.
759
760 @return Destination
761
762 **/
763 CHAR8 *
764 EFIAPI
765 AsciiStrCat (
766 IN OUT CHAR8 *Destination,
767 IN CONST CHAR8 *Source
768 )
769 {
770 AsciiStrCpy (Destination + AsciiStrLen (Destination), Source);
771
772 //
773 // Size of the resulting string should never be zero.
774 // PcdMaximumUnicodeStringLength is tested inside StrLen().
775 //
776 ASSERT (AsciiStrSize (Destination) != 0);
777 return Destination;
778 }
779
780 /**
781 Concatenates one Null-terminated ASCII string with a maximum length to the
782 end of another Null-terminated ASCII string, and returns the concatenated
783 ASCII string.
784
785 This function concatenates two Null-terminated ASCII strings. The contents
786 of Null-terminated ASCII string Source are concatenated to the end of Null-
787 terminated ASCII string Destination, and Destination is returned. At most,
788 Length ASCII characters are concatenated from Source to the end of
789 Destination, and Destination is always Null-terminated. If Length is 0, then
790 Destination is returned unmodified. If Source and Destination overlap, then
791 the results are undefined.
792
793 If Destination is NULL, then ASSERT().
794 If Source is NULL, then ASSERT().
795 If Source and Destination overlap, then ASSERT().
796 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
797 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
798 then ASSERT().
799 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
800 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
801 then ASSERT().
802 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
803 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
804 ASCII characters not including the Null-terminator, then ASSERT().
805
806 @param Destination Pointer to a Null-terminated ASCII string.
807 @param Source Pointer to a Null-terminated ASCII string.
808 @param Length Maximum number of ASCII characters to concatenate from
809 Source.
810
811 @return Destination
812
813 **/
814 CHAR8 *
815 EFIAPI
816 AsciiStrnCat (
817 IN OUT CHAR8 *Destination,
818 IN CONST CHAR8 *Source,
819 IN UINTN Length
820 )
821 {
822 AsciiStrnCpy (Destination + AsciiStrLen (Destination), Source, Length);
823
824 //
825 // Size of the resulting string should never be zero.
826 // PcdMaximumUnicodeStringLength is tested inside StrLen().
827 //
828 ASSERT (AsciiStrSize (Destination) != 0);
829 return Destination;
830 }
831
832 /**
833 Converts an 8-bit value to an 8-bit BCD value.
834
835 Converts the 8-bit value specified by Value to BCD. The BCD value is
836 returned.
837
838 If Value >= 100, then ASSERT().
839
840 @param Value The 8-bit value to convert to BCD. Range 0..99.
841
842 @return The BCD value
843
844 **/
845 UINT8
846 EFIAPI
847 DecimalToBcd8 (
848 IN UINT8 Value
849 )
850 {
851 ASSERT (Value < 100);
852 return ((Value / 10) << 4) | (Value % 10);
853 }
854
855 /**
856 Converts an 8-bit BCD value to an 8-bit value.
857
858 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
859 value is returned.
860
861 If Value >= 0xA0, then ASSERT().
862 If (Value & 0x0F) >= 0x0A, then ASSERT().
863
864 @param Value The 8-bit BCD value to convert to an 8-bit value.
865
866 @return The 8-bit value is returned.
867
868 **/
869 UINT8
870 EFIAPI
871 BcdToDecimal8 (
872 IN UINT8 Value
873 )
874 {
875 ASSERT (Value < 0xa0);
876 ASSERT ((Value & 0xf) < 0xa);
877 return (Value >> 4) * 10 + (Value & 0xf);
878 }