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