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