]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/EbcDxe/EbcDebugger/EdbSupportString.c
MdeModulePkg/EbcDebugger: Use AsciiCharToUpper and CharToUpper
[mirror_edk2.git] / MdeModulePkg / Universal / EbcDxe / EbcDebugger / EdbSupportString.c
1 /** @file
2
3 Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12
13 **/
14
15 #include "Edb.h"
16
17 /**
18
19 Convert hex string to uint.
20
21 @param Str - The string
22
23 **/
24 UINTN
25 EFIAPI
26 Xtoi (
27 CHAR16 *Str
28 )
29 {
30 UINTN RetVal;
31 CHAR16 TempChar;
32 UINTN MaxVal;
33
34 ASSERT (Str != NULL);
35
36 MaxVal = (UINTN) -1 >> 4;
37 //
38 // skip preceeding white space
39 //
40 while (*Str != '\0' && *Str == ' ') {
41 Str += 1;
42 }
43 //
44 // skip preceeding zeros
45 //
46 while (*Str != '\0' && *Str == '0') {
47 Str += 1;
48 }
49 //
50 // skip preceeding white space
51 //
52 if (*Str != '\0' && (*Str == 'x' || *Str == 'X')) {
53 Str += 1;
54 }
55 //
56 // convert hex digits
57 //
58 RetVal = 0;
59 TempChar = *(Str++);
60 while (TempChar != '\0') {
61 if (TempChar >= 'a' && TempChar <= 'f') {
62 TempChar -= 'a' - 'A';
63 }
64
65 if ((TempChar >= '0' && TempChar <= '9') || (TempChar >= 'A' && TempChar <= 'F')) {
66 if (RetVal > MaxVal) {
67 return (UINTN) -1;
68 }
69
70 RetVal = (RetVal << 4) | (TempChar - (TempChar >= 'A' ? 'A' - 10 : '0'));
71 } else {
72 break;
73 }
74
75 TempChar = *(Str++);
76 }
77
78 return RetVal;
79 }
80
81 /**
82
83 Convert hex string to uint.
84
85 @param Str - The string
86
87 **/
88 UINT64
89 EFIAPI
90 LXtoi (
91 CHAR16 *Str
92 )
93 {
94 UINT64 RetVal;
95 CHAR16 TempChar;
96 UINT64 MaxVal;
97
98 ASSERT (Str != NULL);
99
100 MaxVal = RShiftU64 ((UINT64) -1, 4);
101 //
102 // skip preceeding white space
103 //
104 while (*Str != '\0' && *Str == ' ') {
105 Str += 1;
106 }
107 //
108 // skip preceeding zeros
109 //
110 while (*Str != '\0' && *Str == '0') {
111 Str += 1;
112 }
113 //
114 // skip preceeding white space
115 //
116 if (*Str != '\0' && (*Str == 'x' || *Str == 'X')) {
117 Str += 1;
118 }
119 //
120 // convert hex digits
121 //
122 RetVal = 0;
123 TempChar = *(Str++);
124 while (TempChar != '\0') {
125 if (TempChar >= 'a' && TempChar <= 'f') {
126 TempChar -= 'a' - 'A';
127 }
128
129 if ((TempChar >= '0' && TempChar <= '9') || (TempChar >= 'A' && TempChar <= 'F')) {
130 if (RetVal > MaxVal) {
131 return (UINT64) -1;
132 }
133
134 RetVal = LShiftU64 (RetVal, 4);
135 RetVal = RetVal + (TempChar - (TempChar >= 'A' ? 'A' - 10 : '0'));
136 } else {
137 break;
138 }
139
140 TempChar = *(Str++);
141 }
142
143 return RetVal;
144 }
145
146 /**
147
148 Convert hex string to uint.
149
150 @param Str - The string
151
152 **/
153 UINTN
154 EFIAPI
155 Atoi (
156 CHAR16 *Str
157 )
158 {
159 UINTN RetVal;
160 CHAR16 TempChar;
161 UINTN MaxVal;
162 UINTN ResteVal;
163
164 ASSERT (Str != NULL);
165
166 MaxVal = (UINTN) -1 / 10;
167 ResteVal = (UINTN) -1 % 10;
168 //
169 // skip preceeding white space
170 //
171 while (*Str != '\0' && *Str == ' ') {
172 Str += 1;
173 }
174 //
175 // convert digits
176 //
177 RetVal = 0;
178 TempChar = *(Str++);
179 while (TempChar != '\0') {
180 if (TempChar >= '0' && TempChar <= '9') {
181 if (RetVal > MaxVal || (RetVal == MaxVal && TempChar - '0' > (INTN) ResteVal)) {
182 return (UINTN) -1;
183 }
184
185 RetVal = (RetVal * 10) + TempChar - '0';
186 } else {
187 break;
188 }
189
190 TempChar = *(Str++);
191 }
192
193 return RetVal;
194 }
195
196 /**
197
198 Convert hex string to uint.
199
200 @param Str - The string
201
202 **/
203 UINTN
204 EFIAPI
205 AsciiXtoi (
206 CHAR8 *Str
207 )
208 {
209 UINTN RetVal;
210 CHAR8 TempChar;
211 UINTN MaxVal;
212
213 ASSERT (Str != NULL);
214
215 MaxVal = (UINTN) -1 >> 4;
216 //
217 // skip preceeding white space
218 //
219 while (*Str != '\0' && *Str == ' ') {
220 Str += 1;
221 }
222 //
223 // skip preceeding zeros
224 //
225 while (*Str != '\0' && *Str == '0') {
226 Str += 1;
227 }
228 //
229 // skip preceeding white space
230 //
231 if (*Str != '\0' && (*Str == 'x' || *Str == 'X')) {
232 Str += 1;
233 }
234 //
235 // convert hex digits
236 //
237 RetVal = 0;
238 TempChar = *(Str++);
239 while (TempChar != '\0') {
240 if (TempChar >= 'a' && TempChar <= 'f') {
241 TempChar -= 'a' - 'A';
242 }
243
244 if ((TempChar >= '0' && TempChar <= '9') || (TempChar >= 'A' && TempChar <= 'F')) {
245 if (RetVal > MaxVal) {
246 return (UINTN) -1;
247 }
248
249 RetVal = (RetVal << 4) | (TempChar - (TempChar >= 'A' ? 'A' - 10 : '0'));
250 } else {
251 break;
252 }
253
254 TempChar = *(Str++);
255 }
256
257 return RetVal;
258 }
259
260 /**
261
262 Convert hex string to uint.
263
264 @param Str - The string
265
266 **/
267 UINTN
268 EFIAPI
269 AsciiAtoi (
270 CHAR8 *Str
271 )
272 {
273 UINTN RetVal;
274 CHAR8 TempChar;
275 UINTN MaxVal;
276 UINTN ResteVal;
277
278 ASSERT (Str != NULL);
279
280 MaxVal = (UINTN) -1 / 10;
281 ResteVal = (UINTN) -1 % 10;
282 //
283 // skip preceeding white space
284 //
285 while (*Str != '\0' && *Str == ' ') {
286 Str += 1;
287 }
288 //
289 // convert digits
290 //
291 RetVal = 0;
292 TempChar = *(Str++);
293 while (TempChar != '\0') {
294 if (TempChar >= '0' && TempChar <= '9') {
295 if (RetVal > MaxVal || (RetVal == MaxVal && TempChar - '0' > (INTN) ResteVal)) {
296 return (UINTN) -1;
297 }
298
299 RetVal = (RetVal * 10) + TempChar - '0';
300 } else {
301 break;
302 }
303
304 TempChar = *(Str++);
305 }
306
307 return RetVal;
308 }
309
310
311 /**
312 Compare the Unicode and Ascii string pointed by String to the string pointed by String2.
313
314 @param String - Unicode String to process
315
316 @param String2 - Ascii string to process
317
318 @return Return a positive integer if String is lexicall greater than String2; Zero if
319 the two strings are identical; and a negative interger if String is lexically
320 less than String2.
321
322 **/
323 INTN
324 EFIAPI
325 StrCmpUnicodeAndAscii (
326 IN CHAR16 *String,
327 IN CHAR8 *String2
328 )
329 {
330 while (*String != '\0') {
331 if (*String != (CHAR16)*String2) {
332 break;
333 }
334
335 String += 1;
336 String2 += 1;
337 }
338
339 return (*String - (CHAR16)*String2);
340 }
341
342 /**
343
344 Compare the Unicode string pointed by String to the string pointed by String2.
345
346 @param String - Unicode String to process
347 @param String2 - Unicode string to process
348
349 @return Return a positive integer if String is lexically greater than String2; Zero if
350 the two strings are identical; and a negative integer if String is lexically
351 less than String2.
352
353 **/
354 INTN
355 EFIAPI
356 StriCmp (
357 IN CHAR16 *String,
358 IN CHAR16 *String2
359 )
360 {
361 while ((*String != L'\0') &&
362 (CharToUpper (*String) == CharToUpper (*String2))) {
363 String++;
364 String2++;
365 }
366
367 return CharToUpper (*String) - CharToUpper (*String2);
368 }
369
370 /**
371
372 Compare the Unicode and Ascii string pointed by String to the string pointed by String2.
373
374 @param String - Unicode String to process
375 @param String2 - Ascii string to process
376
377 @return Return a positive integer if String is lexically greater than String2; Zero if
378 the two strings are identical; and a negative integer if String is lexically
379 less than String2.
380
381 **/
382 INTN
383 EFIAPI
384 StriCmpUnicodeAndAscii (
385 IN CHAR16 *String,
386 IN CHAR8 *String2
387 )
388 {
389 while ((*String != L'\0') &&
390 (CharToUpper (*String) == (CHAR16)AsciiCharToUpper (*String2))) {
391 String++;
392 String2++;
393 }
394
395 return CharToUpper (*String) - (CHAR16)AsciiCharToUpper (*String2);
396 }
397
398 /**
399
400 Verify if the string is end with the sub string.
401
402 @param Str - The string where to search the sub string
403 @param SubStr - The substring.
404
405 **/
406 BOOLEAN
407 EFIAPI
408 StrEndWith (
409 IN CHAR16 *Str,
410 IN CHAR16 *SubStr
411 )
412 {
413 CHAR16 *Temp;
414
415 if ((Str == NULL) || (SubStr == NULL) || (StrLen(Str) < StrLen(SubStr))) {
416 return FALSE;
417 }
418
419 Temp = Str + StrLen(Str) - StrLen(SubStr);
420
421 //
422 // Compare
423 //
424 if (StriCmp (Temp, SubStr) == 0) {
425 return TRUE;
426 } else {
427 return FALSE;
428 }
429 }
430
431 /**
432 Duplicate a string.
433
434 @param Src The string to be duplicated.
435
436 **/
437 CHAR16 *
438 EFIAPI
439 StrDuplicate (
440 IN CHAR16 *Src
441 )
442 {
443 CHAR16 *Dest;
444 UINTN Size;
445
446 Size = (StrLen(Src) + 1) * sizeof(CHAR16);
447 Dest = AllocateZeroPool (Size);
448 if (Dest != NULL) {
449 CopyMem (Dest, Src, Size);
450 }
451 return Dest;
452 }
453
454
455 CHAR16 *mLineBuffer = NULL;
456 CHAR16 *mFieldBuffer = NULL;
457
458 /**
459
460 Find the first substring.
461
462 @param String Point to the string where to find the substring.
463 @param CharSet Point to the string to be found.
464
465 **/
466 UINTN
467 EFIAPI
468 StrSpn (
469 IN CHAR16 *String,
470 IN CHAR16 *CharSet
471 )
472 {
473 UINTN Count;
474 CHAR16 *Str1;
475 CHAR16 *Str2;
476
477 Count = 0;
478
479 for (Str1 = String; *Str1 != L'\0'; Str1 ++) {
480 for (Str2 = CharSet; *Str2 != L'\0'; Str2 ++) {
481 if (*Str1 == *Str2) {
482 break;
483 }
484 }
485
486 if (*Str2 == L'\0') {
487 return Count;
488 }
489
490 Count ++;
491 }
492
493 return Count;
494 }
495
496 /**
497
498 Searches a string for the first occurrence of a character contained in a
499 specified buffer.
500
501 @param String Point to the string where to find the substring.
502 @param CharSet Point to the string to be found.
503
504 **/
505 CHAR16 *
506 EFIAPI
507 StrBrk (
508 IN CHAR16 *String,
509 IN CHAR16 *CharSet
510 )
511 {
512 CHAR16 *Str1;
513 CHAR16 *Str2;
514
515 for (Str1 = String; *Str1 != L'\0'; Str1 ++) {
516 for (Str2 = CharSet; *Str2 != L'\0'; Str2 ++) {
517 if (*Str1 == *Str2) {
518 return (CHAR16 *) Str1;
519 }
520 }
521 }
522
523 return NULL;
524 }
525
526 /**
527
528 Find the next token after one or more specified characters.
529
530 @param String Point to the string where to find the substring.
531 @param CharSet Point to the string to be found.
532
533 **/
534 CHAR16 *
535 EFIAPI
536 StrTokenLine (
537 IN CHAR16 *String OPTIONAL,
538 IN CHAR16 *CharSet
539 )
540 {
541 CHAR16 *Begin;
542 CHAR16 *End;
543
544 Begin = (String == NULL) ? mLineBuffer : String;
545 if (Begin == NULL) {
546 return NULL;
547 }
548
549 Begin += StrSpn (Begin, CharSet);
550 if (*Begin == L'\0') {
551 mLineBuffer = NULL;
552 return NULL;
553 }
554
555 End = StrBrk (Begin, CharSet);
556 if ((End != NULL) && (*End != L'\0')) {
557 *End = L'\0';
558 End ++;
559 }
560
561 mLineBuffer = End;
562 return Begin;
563 }
564
565 /**
566
567 Find the next token after one specificed characters.
568
569 @param String Point to the string where to find the substring.
570 @param CharSet Point to the string to be found.
571
572 **/
573 CHAR16 *
574 EFIAPI
575 StrTokenField (
576 IN CHAR16 *String OPTIONAL,
577 IN CHAR16 *CharSet
578 )
579 {
580 CHAR16 *Begin;
581 CHAR16 *End;
582
583
584 Begin = (String == NULL) ? mFieldBuffer : String;
585 if (Begin == NULL) {
586 return NULL;
587 }
588
589 if (*Begin == L'\0') {
590 mFieldBuffer = NULL;
591 return NULL;
592 }
593
594 End = StrBrk (Begin, CharSet);
595 if ((End != NULL) && (*End != L'\0')) {
596 *End = L'\0';
597 End ++;
598 }
599
600 mFieldBuffer = End;
601 return Begin;
602 }
603
604 /**
605
606 Find the next token after one or more specified characters.
607
608 @param String Point to the string where to find the substring.
609 @param CharSet Point to the string to be found.
610
611 **/
612 CHAR16 *
613 EFIAPI
614 StrGetNewTokenLine (
615 IN CHAR16 *String,
616 IN CHAR16 *CharSet
617 )
618 {
619 return StrTokenLine (String, CharSet);
620 }
621
622 /**
623
624 Find the next token after one or more specified characters.
625
626 @param CharSet Point to the string to be found.
627
628 **/
629 CHAR16 *
630 EFIAPI
631 StrGetNextTokenLine (
632 IN CHAR16 *CharSet
633 )
634 {
635 return StrTokenLine (NULL, CharSet);
636 }
637
638 /**
639
640 Find the next token after one specificed characters.
641
642 @param String Point to the string where to find the substring.
643 @param CharSet Point to the string to be found.
644
645 **/
646 CHAR16 *
647 EFIAPI
648 StrGetNewTokenField (
649 IN CHAR16 *String,
650 IN CHAR16 *CharSet
651 )
652 {
653 return StrTokenField (String, CharSet);
654 }
655
656 /**
657
658 Find the next token after one specificed characters.
659
660 @param CharSet Point to the string to be found.
661
662 **/
663 CHAR16 *
664 EFIAPI
665 StrGetNextTokenField (
666 IN CHAR16 *CharSet
667 )
668 {
669 return StrTokenField (NULL, CharSet);
670 }
671
672 /**
673
674 Patch a character to the end of a string.
675
676 @param Buffer The string to be patched.
677 @param Patch The patch character.
678
679 **/
680 VOID
681 EFIAPI
682 PatchForStrTokenAfter (
683 IN CHAR16 *Buffer,
684 IN CHAR16 Patch
685 )
686 {
687 CHAR16 *Str;
688
689 if (Buffer == NULL) {
690 return ;
691 }
692
693 Str = Buffer;
694 while (*Str != 0) {
695 Str ++;
696 }
697 *Str = Patch;
698
699 while (*(Str ++) != '\0') {
700 if (*Str == 0) {
701 *Str = Patch;
702 } else {
703 break;
704 }
705 }
706
707 return ;
708 }
709
710 /**
711 Patch a character at the beginning of a string.
712
713 @param Buffer The string to be patched.
714 @param Patch The patch character.
715
716 **/
717 VOID
718 EFIAPI
719 PatchForStrTokenBefore (
720 IN CHAR16 *Buffer,
721 IN CHAR16 Patch
722 )
723 {
724 CHAR16 *Str;
725
726 if (Buffer == NULL) {
727 return ;
728 }
729
730 Str = Buffer;
731 while (*(Str --) != '\0') {
732 if ((*Str == 0) || (*Str == Patch)) {
733 *Str = Patch;
734 } else {
735 break;
736 }
737 }
738
739 return ;
740 }
741
742 CHAR8 *mAsciiLineBuffer = NULL;
743 CHAR8 *mAsciiFieldBuffer = NULL;
744
745 /**
746
747 Find the first substring.
748
749 @param String Point to the string where to find the substring.
750 @param CharSet Point to the string to be found.
751
752 **/
753 UINTN
754 EFIAPI
755 AsciiStrSpn (
756 IN CHAR8 *String,
757 IN CHAR8 *CharSet
758 )
759 {
760 UINTN Count;
761 CHAR8 *Str1;
762 CHAR8 *Str2;
763
764 Count = 0;
765
766 for (Str1 = String; *Str1 != '\0'; Str1 ++) {
767 for (Str2 = CharSet; *Str2 != '\0'; Str2 ++) {
768 if (*Str1 == *Str2) {
769 break;
770 }
771 }
772
773 if (*Str2 == '\0') {
774 return Count;
775 }
776
777 Count ++;
778 }
779
780 return Count;
781 }
782
783 /**
784 Searches a string for the first occurrence of a character contained in a
785 specified buffer.
786
787 @param String Point to the string where to find the substring.
788 @param CharSet Point to the string to be found.
789
790 **/
791 CHAR8 *
792 EFIAPI
793 AsciiStrBrk (
794 IN CHAR8 *String,
795 IN CHAR8 *CharSet
796 )
797 {
798 CHAR8 *Str1;
799 CHAR8 *Str2;
800
801 for (Str1 = String; *Str1 != '\0'; Str1 ++) {
802 for (Str2 = CharSet; *Str2 != '\0'; Str2 ++) {
803 if (*Str1 == *Str2) {
804 return (CHAR8 *) Str1;
805 }
806 }
807 }
808
809 return NULL;
810 }
811
812 /**
813
814 Find the next token after one or more specified characters.
815
816 @param String Point to the string where to find the substring.
817 @param CharSet Point to the string to be found.
818
819 **/
820 CHAR8 *
821 EFIAPI
822 AsciiStrTokenLine (
823 IN CHAR8 *String OPTIONAL,
824 IN CHAR8 *CharSet
825 )
826 {
827 CHAR8 *Begin;
828 CHAR8 *End;
829
830 Begin = (String == NULL) ? mAsciiLineBuffer : String;
831 if (Begin == NULL) {
832 return NULL;
833 }
834
835 Begin += AsciiStrSpn (Begin, CharSet);
836 if (*Begin == '\0') {
837 mAsciiLineBuffer = NULL;
838 return NULL;
839 }
840
841 End = AsciiStrBrk (Begin, CharSet);
842 if ((End != NULL) && (*End != '\0')) {
843 *End = '\0';
844 End ++;
845 }
846
847 mAsciiLineBuffer = End;
848 return Begin;
849 }
850
851 /**
852
853 Find the next token after one specificed characters.
854
855 @param String Point to the string where to find the substring.
856 @param CharSet Point to the string to be found.
857
858 **/
859 CHAR8 *
860 EFIAPI
861 AsciiStrTokenField (
862 IN CHAR8 *String OPTIONAL,
863 IN CHAR8 *CharSet
864 )
865 {
866 CHAR8 *Begin;
867 CHAR8 *End;
868
869
870 Begin = (String == NULL) ? mAsciiFieldBuffer : String;
871 if (Begin == NULL) {
872 return NULL;
873 }
874
875 if (*Begin == '\0') {
876 mAsciiFieldBuffer = NULL;
877 return NULL;
878 }
879
880 End = AsciiStrBrk (Begin, CharSet);
881 if ((End != NULL) && (*End != '\0')) {
882 *End = '\0';
883 End ++;
884 }
885
886 mAsciiFieldBuffer = End;
887 return Begin;
888 }
889
890 /**
891
892 Find the next token after one or more specified characters.
893
894 @param String Point to the string where to find the substring.
895 @param CharSet Point to the string to be found.
896
897 **/
898 CHAR8 *
899 EFIAPI
900 AsciiStrGetNewTokenLine (
901 IN CHAR8 *String,
902 IN CHAR8 *CharSet
903 )
904 {
905 return AsciiStrTokenLine (String, CharSet);
906 }
907
908 /**
909
910 Find the next token after one or more specified characters.
911
912 @param CharSet Point to the string to be found.
913
914 **/
915 CHAR8 *
916 EFIAPI
917 AsciiStrGetNextTokenLine (
918 IN CHAR8 *CharSet
919 )
920 {
921 return AsciiStrTokenLine (NULL, CharSet);
922 }
923
924 /**
925
926 Find the next token after one specificed characters.
927
928 @param String Point to the string where to find the substring.
929 @param CharSet Point to the string to be found.
930
931 **/
932 CHAR8 *
933 EFIAPI
934 AsciiStrGetNewTokenField (
935 IN CHAR8 *String,
936 IN CHAR8 *CharSet
937 )
938 {
939 return AsciiStrTokenField (String, CharSet);
940 }
941
942 /**
943
944 Find the next token after one specificed characters.
945
946 @param CharSet Point to the string to be found.
947
948 **/
949 CHAR8 *
950 EFIAPI
951 AsciiStrGetNextTokenField (
952 IN CHAR8 *CharSet
953 )
954 {
955 return AsciiStrTokenField (NULL, CharSet);
956 }
957
958 /**
959
960 Patch a character to the end of a string.
961
962 @param Buffer The string to be patched.
963 @param Patch The patch character.
964
965 **/
966 VOID
967 EFIAPI
968 PatchForAsciiStrTokenAfter (
969 IN CHAR8 *Buffer,
970 IN CHAR8 Patch
971 )
972 {
973 CHAR8 *Str;
974
975 if (Buffer == NULL) {
976 return ;
977 }
978
979 Str = Buffer;
980 while (*Str != 0) {
981 Str ++;
982 }
983 *Str = Patch;
984
985 while (*(Str ++) != '\0') {
986 if (*Str == 0) {
987 *Str = Patch;
988 } else {
989 break;
990 }
991 }
992
993 return ;
994 }
995
996 /**
997 Patch a character at the beginning of a string.
998
999 @param Buffer The string to be patched.
1000 @param Patch The patch character.
1001
1002 **/
1003 VOID
1004 EFIAPI
1005 PatchForAsciiStrTokenBefore (
1006 IN CHAR8 *Buffer,
1007 IN CHAR8 Patch
1008 )
1009 {
1010 CHAR8 *Str;
1011
1012 if (Buffer == NULL) {
1013 return ;
1014 }
1015
1016 Str = Buffer;
1017 while (*(Str --) != '\0') {
1018 if ((*Str == 0) || (*Str == Patch)) {
1019 *Str = Patch;
1020 } else {
1021 break;
1022 }
1023 }
1024
1025 return ;
1026 }