]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/String.c
Make MDE package pass intel IPF compiler with /W4 /WX switched on.
[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 (UINT8) ((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 CHAR8 UpperFirstString;
679 CHAR8 UpperSecondString;
680
681 //
682 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
683 //
684 ASSERT (AsciiStrSize (FirstString));
685 ASSERT (AsciiStrSize (SecondString));
686
687 UpperFirstString = AsciiToUpper (*FirstString);
688 UpperSecondString = AsciiToUpper (*SecondString);
689 while ((*FirstString != '\0') && (UpperFirstString == UpperSecondString)) {
690 FirstString++;
691 SecondString++;
692 UpperFirstString = AsciiToUpper (*FirstString);
693 UpperSecondString = AsciiToUpper (*SecondString);
694 }
695
696 return UpperFirstString - UpperSecondString;
697 }
698
699 /**
700 Compares two Null-terminated ASCII strings with maximum lengths, and returns
701 the difference between the first mismatched ASCII characters.
702
703 This function compares the Null-terminated ASCII string FirstString to the
704 Null-terminated ASCII string SecondString. At most, Length ASCII characters
705 will be compared. If Length is 0, then 0 is returned. If FirstString is
706 identical to SecondString, then 0 is returned. Otherwise, the value returned
707 is the first mismatched ASCII character in SecondString subtracted from the
708 first mismatched ASCII character in FirstString.
709
710 If FirstString is NULL, then ASSERT().
711 If SecondString is NULL, then ASSERT().
712 If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
713 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
714 then ASSERT().
715 If PcdMaximumAsciiStringLength is not zero and SecondString contains more than
716 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
717 then ASSERT().
718
719 @param FirstString Pointer to a Null-terminated ASCII string.
720 @param SecondString Pointer to a Null-terminated ASCII string.
721
722 @retval 0 FirstString is identical to SecondString.
723 @retval !=0 FirstString is not identical to SecondString.
724
725 **/
726 INTN
727 EFIAPI
728 AsciiStrnCmp (
729 IN CONST CHAR8 *FirstString,
730 IN CONST CHAR8 *SecondString,
731 IN UINTN Length
732 )
733 {
734 if (Length == 0) {
735 return 0;
736 }
737
738 //
739 // ASSERT both strings are less long than PcdMaximumAsciiStringLength
740 //
741 ASSERT (AsciiStrSize (FirstString));
742 ASSERT (AsciiStrSize (SecondString));
743
744 while ((*FirstString != '\0') &&
745 (*FirstString == *SecondString) &&
746 (Length > 1)) {
747 FirstString++;
748 SecondString++;
749 Length--;
750 }
751 return *FirstString - *SecondString;
752 }
753
754 /**
755 Concatenates one Null-terminated ASCII string to another Null-terminated
756 ASCII string, and returns the concatenated ASCII string.
757
758 This function concatenates two Null-terminated ASCII strings. The contents of
759 Null-terminated ASCII string Source are concatenated to the end of Null-
760 terminated ASCII string Destination. The Null-terminated concatenated ASCII
761 String is returned.
762
763 If Destination is NULL, then ASSERT().
764 If Source is NULL, then ASSERT().
765 If PcdMaximumAsciiStringLength is not zero and Destination contains more than
766 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
767 then ASSERT().
768 If PcdMaximumAsciiStringLength is not zero and Source contains more than
769 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
770 then ASSERT().
771 If PcdMaximumAsciiStringLength is not zero and concatenating Destination and
772 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
773 ASCII characters, then ASSERT().
774
775 @param Destination Pointer to a Null-terminated ASCII string.
776 @param Source Pointer to a Null-terminated ASCII string.
777
778 @return Destination
779
780 **/
781 CHAR8 *
782 EFIAPI
783 AsciiStrCat (
784 IN OUT CHAR8 *Destination,
785 IN CONST CHAR8 *Source
786 )
787 {
788 AsciiStrCpy (Destination + AsciiStrLen (Destination), Source);
789
790 //
791 // Size of the resulting string should never be zero.
792 // PcdMaximumUnicodeStringLength is tested inside StrLen().
793 //
794 ASSERT (AsciiStrSize (Destination) != 0);
795 return Destination;
796 }
797
798 /**
799 Concatenates one Null-terminated ASCII string with a maximum length to the
800 end of another Null-terminated ASCII string, and returns the concatenated
801 ASCII string.
802
803 This function concatenates two Null-terminated ASCII strings. The contents
804 of Null-terminated ASCII string Source are concatenated to the end of Null-
805 terminated ASCII string Destination, and Destination is returned. At most,
806 Length ASCII characters are concatenated from Source to the end of
807 Destination, and Destination is always Null-terminated. If Length is 0, then
808 Destination is returned unmodified. If Source and Destination overlap, then
809 the results are undefined.
810
811 If Destination is NULL, then ASSERT().
812 If Source is NULL, then ASSERT().
813 If Source and Destination overlap, then ASSERT().
814 If PcdMaximumAsciiStringLength is not zero, and Destination contains more than
815 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
816 then ASSERT().
817 If PcdMaximumAsciiStringLength is not zero, and Source contains more than
818 PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
819 then ASSERT().
820 If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and
821 Source results in a ASCII string with more than PcdMaximumAsciiStringLength
822 ASCII characters not including the Null-terminator, then ASSERT().
823
824 @param Destination Pointer to a Null-terminated ASCII string.
825 @param Source Pointer to a Null-terminated ASCII string.
826 @param Length Maximum number of ASCII characters to concatenate from
827 Source.
828
829 @return Destination
830
831 **/
832 CHAR8 *
833 EFIAPI
834 AsciiStrnCat (
835 IN OUT CHAR8 *Destination,
836 IN CONST CHAR8 *Source,
837 IN UINTN Length
838 )
839 {
840 AsciiStrnCpy (Destination + AsciiStrLen (Destination), Source, Length);
841
842 //
843 // Size of the resulting string should never be zero.
844 // PcdMaximumUnicodeStringLength is tested inside StrLen().
845 //
846 ASSERT (AsciiStrSize (Destination) != 0);
847 return Destination;
848 }
849
850 /**
851 Converts an 8-bit value to an 8-bit BCD value.
852
853 Converts the 8-bit value specified by Value to BCD. The BCD value is
854 returned.
855
856 If Value >= 100, then ASSERT().
857
858 @param Value The 8-bit value to convert to BCD. Range 0..99.
859
860 @return The BCD value
861
862 **/
863 UINT8
864 EFIAPI
865 DecimalToBcd8 (
866 IN UINT8 Value
867 )
868 {
869 ASSERT (Value < 100);
870 return (UINT8) (((Value / 10) << 4) | (Value % 10));
871 }
872
873 /**
874 Converts an 8-bit BCD value to an 8-bit value.
875
876 Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
877 value is returned.
878
879 If Value >= 0xA0, then ASSERT().
880 If (Value & 0x0F) >= 0x0A, then ASSERT().
881
882 @param Value The 8-bit BCD value to convert to an 8-bit value.
883
884 @return The 8-bit value is returned.
885
886 **/
887 UINT8
888 EFIAPI
889 BcdToDecimal8 (
890 IN UINT8 Value
891 )
892 {
893 ASSERT (Value < 0xa0);
894 ASSERT ((Value & 0xf) < 0xa);
895 return (UINT8) ((Value >> 4) * 10 + (Value & 0xf));
896 }