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