-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathheob.c
9006 lines (8049 loc) · 249 KB
/
heob.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright Hannes Domani 2014 - 2024.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// includes {{{
#include "heob-internal.h"
#ifndef NO_DWARFSTACK
#include <dwarfstack.h>
#else
#include <stdint.h>
#define DWST_BASE_ADDR 0
#define DWST_NO_DBG_SYM -1
#endif
#ifndef NO_DBGENG
#include <dbgeng.h>
#endif
#include <shobjidl.h>
// }}}
// output variant declarations {{{
typedef enum
{
ATT_NORMAL,
ATT_OK,
ATT_SECTION,
ATT_INFO,
ATT_WARN,
ATT_BASE,
ATT_COUNT,
}
textColorAtt;
struct textColor;
typedef void func_WriteText( struct textColor*,const char*,size_t );
typedef void func_WriteTextW( struct textColor*,const wchar_t*,size_t );
typedef void func_TextColor( struct textColor*,textColorAtt );
typedef struct textColor
{
func_WriteText *fWriteText;
func_WriteText *fWriteSubText;
func_WriteTextW *fWriteSubTextW;
func_TextColor *fTextColor;
int canWriteWideChar;
HANDLE out;
union {
int colors[ATT_COUNT];
const char *styles[ATT_COUNT];
};
textColorAtt color;
}
textColor;
// }}}
// CRT replacements {{{
static inline char num2hex( unsigned int bits )
{
bits &= 0xf;
return( bits>=10 ? bits - 10 + 'A' : bits + '0' );
}
char *num2hexstr( char *str,UINT64 arg,int count )
{
#ifndef _WIN64
uint32_t a32[2];
RtlMoveMemory( a32,&arg,sizeof(arg) );
int b;
for( b=count-1; b>=0; b-- )
{
uint32_t v = a32[b>=8];
str++[0] = num2hex( v>>((b%8)*4) );
}
return( str );
#else
int b;
for( b=count-1; b>=0; b-- )
str++[0] = num2hex( (unsigned)(arg>>(b*4)) );
return( str );
#endif
}
wchar_t *num2hexstrW( wchar_t *str,UINT64 arg,int count )
{
char s[16];
char *e = num2hexstr( s,arg,count );
int l = (int)( e - s );
int i;
for( i=0; i<l; i++ )
str++[i] = s[i];
return( str );
}
static char *num2commastr( char *start,uintptr_t arg,int minus,int comma )
{
int c = 0;
if( comma<0 ) comma = 0;
while( arg || c<=comma )
{
(--start)[0] = arg%10 + '0';
arg /= 10;
if( ++c==comma )
(--start)[0] = '.';
}
if( minus )
(--start)[0] = '-';
return( start );
}
static NOINLINE char *num2str( char *start,uintptr_t arg,int minus )
{
return( num2commastr(start,arg,minus,0) );
}
NOINLINE wchar_t *num2strW( wchar_t *start,uintptr_t arg,int minus )
{
if( !arg )
(--start)[0] = '0';
while( arg )
{
(--start)[0] = arg%10 + '0';
arg /= 10;
}
if( minus )
(--start)[0] = '-';
return( start );
}
static NOINLINE void mprintf( textColor *tc,const char *format,... )
{
if( !tc || !tc->out ) return;
va_list vl;
va_start( vl,format );
const char *ptr = format;
while( ptr[0] )
{
// argument {{{
if( ptr[0]=='%' && ptr[1] )
{
// % = argument
if( ptr>format )
tc->fWriteText( tc,format,ptr-format );
switch( ptr[1] )
{
// strings {{{
case 's': // const char*
{
const char *arg = va_arg( vl,const char* );
if( arg && arg[0] )
tc->fWriteSubText( tc,arg,lstrlen(arg) );
}
break;
case 'S': // const wchar_t*
{
const wchar_t *arg = va_arg( vl,const wchar_t* );
if( arg && arg[0] )
tc->fWriteSubTextW( tc,arg,lstrlenW(arg) );
}
break;
// }}}
// decimal numbers {{{
case 'd': // int
case 'D': // intptr_t
case 'u': // unsigned int
case 'U': // uintptr_t
{
uintptr_t arg = 0;
int minus = 0;
switch( ptr[1] )
{
case 'd':
case 'D':
{
intptr_t argi;
if( ptr[1]=='D' )
argi = va_arg( vl,intptr_t );
else
argi = va_arg( vl,int );
if( argi<0 )
{
arg = -argi;
minus = 1;
}
else
arg = argi;
}
break;
case 'u':
arg = va_arg( vl,unsigned int );
break;
case 'U':
arg = va_arg( vl,uintptr_t );
break;
}
char str[32];
char *end = str + sizeof(str);
char *start = num2str( end,arg,minus );
tc->fWriteText( tc,start,end-start );
}
break;
// }}}
// hexadecimal numbers {{{
case 'w': // unsigned short (word)
case 'x': // unsigned int
case 'X': // uintptr_t
case 'p': // void*
{
uintptr_t arg;
int bytes;
if( ptr[1]=='p' )
{
arg = (uintptr_t)va_arg( vl,void* );
bytes = sizeof(void*);
}
else if( ptr[1]=='X' )
{
arg = va_arg( vl,uintptr_t );
bytes = sizeof(uintptr_t);
}
else if( ptr[1]=='w' )
{
arg = (unsigned short)va_arg( vl,unsigned int );
bytes = sizeof(unsigned short);
}
else
{
arg = va_arg( vl,unsigned int );
bytes = sizeof(unsigned int);
}
char str[20];
char *end = str;
end++[0] = '0';
end++[0] = 'x';
end = num2hexstr( end,arg,bytes*2 );
tc->fWriteText( tc,str,end-str );
}
break;
// }}}
// indention {{{
case 'i': // indent
case 'E': // new entry
case 'I': // indent level end
{
int indent = va_arg( vl,int );
int i;
wchar_t firstSpace = ' ';
wchar_t bar[2] = { ' ','|' };
wchar_t specialBars[2][2] = { {' ',' '},{' ',' '} };
int specialCount = 0;
if( tc->canWriteWideChar )
{
const wchar_t barTopBottom = 0x2502;
const wchar_t barRightBottom = 0x250c;
const wchar_t barTopRightBottom = 0x251c;
const wchar_t barTopLeft = 0x2518;
const wchar_t barLeftRight = 0x2500;
const wchar_t barRightDoubleTopBottom = 0x255f;
bar[1] = barTopBottom;
if( ptr[1]=='E' )
{
specialBars[0][1] = barRightBottom;
specialCount = 1;
}
else if( ptr[1]=='I' )
{
if( indent==1 )
firstSpace = barRightDoubleTopBottom;
specialBars[0][1] = barTopRightBottom;
specialBars[1][0] = barLeftRight;
specialBars[1][1] = barTopLeft;
specialCount = 2;
}
}
tc->fWriteSubTextW( tc,&firstSpace,1 );
for( i=0; i<indent; i++ )
{
if( tc->fTextColor )
tc->fTextColor( tc,i%ATT_BASE );
if( i>=indent-specialCount )
RtlMoveMemory( bar,specialBars[i-(indent-specialCount)],4 );
tc->fWriteSubTextW( tc,bar,2 );
}
if( tc->fTextColor )
tc->fTextColor( tc,ATT_NORMAL );
tc->fWriteText( tc," ",1 );
}
break;
// }}}
// time {{{
case 'm': // milliseconds
{
DWORD ticks = va_arg( vl,DWORD );
unsigned milli = ticks%1000;
ticks /= 1000;
unsigned sec = ticks%60;
ticks /= 60;
unsigned min = ticks%60;
ticks /= 60;
unsigned hour = ticks%24;
ticks /= 24;
unsigned day = ticks;
char timestr[15] = {
day /10 +'0',day %10+'0',':',
hour /10 +'0',hour %10+'0',':',
min /10 +'0',min %10+'0',':',
sec /10 +'0',sec %10+'0','.',
milli/100+'0',(milli/10)%10+'0',milli%10+'0',
};
tc->fWriteText( tc,timestr,15 );
}
break;
case 't': // seconds
{
DWORD ticks = va_arg( vl,DWORD );
unsigned sec = ticks%60;
ticks /= 60;
unsigned min = ticks%60;
ticks /= 60;
unsigned hour = ticks%24;
ticks /= 24;
unsigned day = ticks;
if( day )
{
char str[32];
char *end = str + sizeof(str);
(--end)[0] = ':';
char *start = num2str( end,day,0 );
tc->fWriteText( tc,start,end+1-start );
}
char timestr[8] = {
hour/10+'0',hour%10+'0',':',
min /10+'0',min %10+'0',':',
sec /10+'0',sec %10+'0',
};
tc->fWriteText( tc,timestr,8 );
}
break;
case 'T': // FILETIME*
{
FILETIME *ft = va_arg( vl,FILETIME* );
SYSTEMTIME st,stl;
if( !FileTimeToSystemTime(ft,&st) ||
!SystemTimeToTzSpecificLocalTime(NULL,&st,&stl) )
RtlZeroMemory( &stl,sizeof(stl) );
int y = stl.wYear;
int m = stl.wMonth;
int d = stl.wDay;
int h = stl.wHour;
int mi = stl.wMinute;
int s = stl.wSecond;
char timestr[19] = {
y/1000+'0',(y/100)%10+'0',(y/10)%10+'0',y%10+'0','.',
m /10+'0',m %10+'0','.',
d /10+'0',d %10+'0',' ',
h /10+'0',h %10+'0',':',
mi /10+'0',mi %10+'0',':',
s /10+'0',s %10+'0',
};
tc->fWriteText( tc,timestr,19 );
}
break;
// }}}
// last-error code {{{
case 'e': // last-error code
{
DWORD e = va_arg( vl,DWORD );
wchar_t *s = NULL;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,e,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
(LPWSTR)&s,0,NULL );
if( s && s[0] )
{
int l = lstrlenW( s );
while( l && (s[l-1]=='\r' || s[l-1]=='\n') ) l--;
if( l )
tc->fWriteSubTextW( tc,s,l );
}
LocalFree( s );
}
break;
// }}}
// percent (%) character {{{
case '%':
format = ptr + 1;
ptr += 2;
continue;
// }}}
// bytes {{{
case 'B': // size_t
{
size_t arg = va_arg( vl,size_t );
if( arg<1024 )
{
char str[32];
char *end = str + sizeof(str) - 8;
char *start = num2str( end,arg,0 );
end++[0] = ' ';
end++[0] = 'B';
tc->fWriteText( tc,start,end-start );
break;
}
int unit = 0;
while( arg>=1048576 )
{
arg >>= 10;
unit++;
}
arg *= 1000;
arg >>= 10;
const char *units = "KMGTPE";
char str[32];
char *end = str + sizeof(str) - 8;
char *start = num2commastr( end,arg,0,3 );
if( end-start>5 ) end = start + 5;
if( end[-1]=='.' ) end--;
end++[0] = ' ';
end++[0] = units[unit];
end++[0] = 'i';
end++[0] = 'B';
tc->fWriteText( tc,start,end-start );
break;
}
// }}}
}
ptr += 2;
format = ptr;
continue;
}
// }}}
// color {{{
else if( ptr[0]=='$' && ptr[1] )
{
// $ = color
// $N = normal
// $O = ok
// $S = section
// $I = info
// $W = warn
// $B = base
if( ptr>format )
tc->fWriteText( tc,format,ptr-format );
if( tc->fTextColor )
{
textColorAtt color = ATT_NORMAL;
switch( ptr[1] )
{
case 'O': color = ATT_OK; break;
case 'S': color = ATT_SECTION; break;
case 'I': color = ATT_INFO; break;
case 'W': color = ATT_WARN; break;
case 'B': color = ATT_BASE; break;
}
tc->fTextColor( tc,color );
}
ptr += 2;
format = ptr;
continue;
}
// }}}
// newline {{{
else if( ptr[0]=='\n' && tc->fTextColor && tc->color!=ATT_NORMAL )
{
if( ptr>format )
tc->fWriteText( tc,format,ptr-format );
tc->fTextColor( tc,ATT_NORMAL );
format = ptr;
}
// }}}
ptr++;
}
if( ptr>format )
tc->fWriteText( tc,format,ptr-format );
va_end( vl );
}
#define printf(...) mprintf(tc,__VA_ARGS__)
static FILETIME secondsToFiletime( DWORD seconds )
{
ULONGLONG ll = UInt32x32To64( seconds,10000000 ) + 116444736000000000ULL;
FILETIME ft;
ft.dwLowDateTime = (DWORD)ll;
ft.dwHighDateTime = (DWORD)( ll>>32 );
return( ft );
}
#ifndef NO_DBGENG
static NOINLINE char *mstrchr( const char *s,char c )
{
for( ; *s; s++ )
if( *s==c ) return( (char*)s );
return( NULL );
}
#define strchr mstrchr
#endif
static NOINLINE wchar_t *mstrchrW( const wchar_t *s,wchar_t c )
{
for( ; *s; s++ )
if( *s==c ) return( (wchar_t*)s );
return( NULL );
}
#define strchrW mstrchrW
NOINLINE wchar_t *mstrrchrW( const wchar_t *s,wchar_t c )
{
wchar_t *ret = NULL;
for( ; *s; s++ )
if( *s==c ) ret = (wchar_t*)s;
return( ret );
}
#define strrchrW mstrrchrW
static NOINLINE uint64_t wtou64( const wchar_t *s )
{
uint64_t ret = 0;
if( s[0]=='0' && s[1]=='x' )
{
s += 2;
for( ; ; s++ )
{
wchar_t c = *s;
int add;
if( c>='0' && c<='9' )
add = c - '0';
else if( c>='a' && c<='f' )
add = c - 'a' + 10;
else if( c>='A' && c<='F' )
add = c - 'A' + 10;
else break;
ret = ( ret<<4 ) + add;
}
return( ret );
}
for( ; *s>='0' && *s<='9'; s++ )
ret = ret*10 + ( *s - '0' );
return( ret );
}
static inline uintptr_t wtop( const wchar_t *s )
{
return( (uintptr_t)wtou64(s) );
}
static inline int mwtoi( const wchar_t *s )
{
return( (int)wtop(s) );
}
#define wtoi mwtoi
static int mmemcmp( const void *p1,const void *p2,size_t s )
{
const unsigned char *b1 = p1;
const unsigned char *b2 = p2;
size_t i;
for( i=0; i<s; i++ )
if( b1[i]!=b2[i] ) return( (int)b2[i]-(int)b1[i] );
return( 0 );
}
#define memcmp mmemcmp
static int ptrcmp( const uintptr_t *p1,const uintptr_t *p2,size_t s )
{
size_t i;
for( i=0; i<s; i++ )
if( p1[i]!=p2[i] ) return( p2[i]>p1[i] ? 1 : -1 );
return( 0 );
}
static const void *mmemchr( const void *p,int ch,size_t s )
{
const unsigned char *b = p;
const unsigned char *eob = b + s;
unsigned char c = ch;
for( ; b<eob; b++ )
if( *b==c ) return( b );
return( NULL );
}
#define memchr mmemchr
static NOINLINE wchar_t *mstrstrW( const wchar_t *s,const wchar_t *f )
{
int ls = lstrlenW( s );
int lf = lstrlenW( f );
if( lf>ls ) return( NULL );
if( !lf ) return( (wchar_t*)s );
int ld = ls - lf + 1;
int i;
for( i=0; i<ld; i++ )
if( !mmemcmp(s+i,f,2*lf) ) return( (wchar_t*)s+i );
return( NULL );
}
#define strstrW mstrstrW
static NOINLINE wchar_t *strreplace(
const wchar_t *str,const wchar_t *from,const wchar_t *to,HANDLE heap )
{
const wchar_t *pos = strstrW( str,from );
if( !pos ) return( NULL );
int strLen = lstrlenW( str );
int fromLen = lstrlenW( from );
int toLen = lstrlenW( to );
wchar_t *replace = HeapAlloc( heap,0,2*(strLen-fromLen+toLen+1) );
if( !replace ) return( NULL );
int replacePos = 0;
if( pos>str )
{
RtlMoveMemory( replace,str,2*(pos-str) );
replacePos += (int)( pos - str );
}
if( toLen )
{
RtlMoveMemory( replace+replacePos,to,2*toLen );
replacePos += toLen;
}
if( str+strLen>pos+fromLen )
{
int endLen = (int)( (str+strLen) - (pos+fromLen) );
RtlMoveMemory( replace+replacePos,pos+fromLen,2*endLen );
replacePos += endLen;
}
replace[replacePos] = 0;
return( replace );
}
static NOINLINE wchar_t *strreplacenum(
const wchar_t *str,const wchar_t *from,uintptr_t to,HANDLE heap )
{
wchar_t numStr[32];
wchar_t *numEnd = numStr + sizeof(numStr)/2;
(--numEnd)[0] = 0;
wchar_t *numStart = num2strW( numEnd,to,0 );
return( strreplace(str,from,numStart,heap) );
}
int strstart( const char *str,const char *start )
{
int l1 = lstrlen( str );
int l2 = lstrlen( start );
if( l1<l2 ) return( 0 );
return( CompareString(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,
str,l2,start,l2)==2 );
}
static int strstartW( const wchar_t *str,const wchar_t *start )
{
int l1 = lstrlenW( str );
int l2 = lstrlenW( start );
if( l1<l2 ) return( 0 );
return( CompareStringW(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,
str,l2,start,l2)==2 );
}
// }}}
// output variants {{{
static void WriteText( textColor *tc,const char *t,size_t l )
{
DWORD written;
WriteFile( tc->out,t,(DWORD)l,&written,NULL );
}
static int UTF16toUTF32( const uint16_t *u16,size_t l,uint32_t *c32 )
{
uint16_t c16_1 = u16[0];
if( c16_1<0xd800 ) // 11010AAA AAAAAAAA
*c32 = c16_1; // -> 11010AAA AAAAAAAA
else if( c16_1<0xdc00 ) // 110110AA AAAAAAAA
{
if( l<2 ) return( 0 );
uint16_t c16_2 = u16[1];
if( (c16_2&0xdc00)!=0xdc00 ) // 110111BB BBBBBBBB
return( 0 );
*c32 =
( (((uint32_t)c16_1&0x3ff)<<10) | // -> 0000AAAA AAAAAABB BBBBBBBB
((uint32_t)c16_2&0x3ff) ) +
0x10000;
return( 2 );
}
else if( c16_1<0xe000 ) // 110XXXXX XXXXXXXX
return( 0 );
else // AAAAAAAA AAAAAAAA
*c32 = c16_1; // -> 00000000 AAAAAAAA AAAAAAAA
return( 1 );
}
static void WriteTextW( textColor *tc,const wchar_t *t,size_t l )
{
const uint16_t *u16 = t;
uint32_t c32;
DWORD written;
size_t i;
for( i=0; i<l; i++ )
{
int chars = UTF16toUTF32( u16+i,l-i,&c32 );
if( !chars ) continue;
uint8_t c8 = c32;
if( c32>=0x80 )
c8 = '?';
WriteFile( tc->out,&c8,1,&written,NULL );
if( chars>1 ) i++;
}
}
static void WriteTextConsoleW( textColor *tc,const wchar_t *t,size_t l )
{
DWORD written;
WriteConsoleW( tc->out,t,(DWORD)l,&written,NULL );
}
static void TextColorConsole( textColor *tc,textColorAtt color )
{
if( tc->color==color ) return;
SetConsoleTextAttribute( tc->out,tc->colors[color] );
tc->color = color;
}
static void TextColorTerminal( textColor *tc,textColorAtt color )
{
if( tc->color==color ) return;
int c = tc->colors[color];
char text[] = { 27,'[',(c/10000)%10+'0',(c/1000)%10+'0',(c/100)%10+'0',';',
(c/10)%10+'0',c%10+'0','m' };
DWORD written;
WriteFile( tc->out,text,sizeof(text),&written,NULL );
tc->color = color;
}
static void WriteTextHtml( textColor *tc,const char *ts,size_t l )
{
const unsigned char *t = (const unsigned char*)ts;
const unsigned char *end = t + l;
const unsigned char *next;
char lt[] = "<";
char gt[] = ">";
char amp[] = "&";
char quot[] = """;
DWORD written;
for( next=t; next<end; next++ )
{
unsigned char c = next[0];
if( c<0x09 || (c>=0x0b && c<=0x0c) || (c>=0x0e && c<=0x1f) || c>=0x7f )
{
if( next>t )
WriteFile( tc->out,t,(DWORD)(next-t),&written,NULL );
char hex[] = "�";
num2hexstr( hex+3,c,2 );
WriteFile( tc->out,hex,sizeof(hex)-1,&written,NULL );
t = next + 1;
continue;
}
if( c!='<' && c!='>' && c!='&' && c!='"' ) continue;
if( next>t )
WriteFile( tc->out,t,(DWORD)(next-t),&written,NULL );
if( c=='<' )
WriteFile( tc->out,lt,sizeof(lt)-1,&written,NULL );
else if( c=='>' )
WriteFile( tc->out,gt,sizeof(gt)-1,&written,NULL );
else if( c=='&' )
WriteFile( tc->out,amp,sizeof(amp)-1,&written,NULL );
else
WriteFile( tc->out,quot,sizeof(quot)-1,&written,NULL );
t = next + 1;
}
if( next>t )
WriteFile( tc->out,t,(DWORD)(next-t),&written,NULL );
}
static void WriteTextHtmlW( textColor *tc,const wchar_t *ts,size_t l )
{
const uint16_t *u16 = ts;
uint32_t c32;
char lt[] = "<";
char gt[] = ">";
char amp[] = "&";
char quot[] = """;
DWORD written;
size_t i;
for( i=0; i<l; i++ )
{
int chars = UTF16toUTF32( u16+i,l-i,&c32 );
if( !chars ) continue;
if( c32<0x09 || (c32>=0x0b && c32<=0x0c) ||
(c32>=0x0e && c32<=0x1f) || c32>=0x7f )
{
char hex[] = "�";
int bytes = c32>=0x10000 ? 3 : ( c32>=0x100 ? 2 : 1 );
char *end = num2hexstr( hex+3,c32,bytes*2 );
end++[0] = ';';
WriteFile( tc->out,hex,(DWORD)(end-hex),&written,NULL );
}
else if( c32=='<' )
WriteFile( tc->out,lt,sizeof(lt)-1,&written,NULL );
else if( c32=='>' )
WriteFile( tc->out,gt,sizeof(gt)-1,&written,NULL );
else if( c32=='&' )
WriteFile( tc->out,amp,sizeof(amp)-1,&written,NULL );
else if( c32=='"' )
WriteFile( tc->out,quot,sizeof(quot)-1,&written,NULL );
else
{
uint8_t c8 = c32;
WriteFile( tc->out,&c8,1,&written,NULL );
}
if( chars>1 ) i++;
}
}
static void TextColorHtml( textColor *tc,textColorAtt color )
{
if( tc->color==color ) return;
DWORD written;
if( tc->color )
{
const char *spanEnd = "</span>";
WriteFile( tc->out,spanEnd,lstrlen(spanEnd),&written,NULL );
}
if( color )
{
const char *span1 = "<span class=\"";
const char *style = tc->styles[color];
const char *span2 = "\">";
WriteFile( tc->out,span1,lstrlen(span1),&written,NULL );
WriteFile( tc->out,style,lstrlen(style),&written,NULL );
WriteFile( tc->out,span2,lstrlen(span2),&written,NULL );
}
tc->color = color;
}
static void setTextColorTerminal( textColor *tc )
{
tc->fTextColor = &TextColorTerminal;
tc->colors[ATT_NORMAL] = 4939;
tc->colors[ATT_OK] = 4932;
tc->colors[ATT_SECTION] = 4936;
tc->colors[ATT_INFO] = 4935;
tc->colors[ATT_WARN] = 4931;
tc->colors[ATT_BASE] = 10030;
TextColorTerminal( tc,ATT_NORMAL );
}
static void writeHeobInfo( textColor *tc,const char *t )
{
printf(
"<!-- %s of memory leak / profiling data.\n"
" Generated by heob %s.\n"
" https://github.com/ssbssa/heob -->\n\n",
t,HEOB_VER );
}
static void checkOutputVariant( textColor *tc,HANDLE out,
const wchar_t *exeName )
{
// default
tc->fWriteText = &WriteText;
tc->fWriteSubText = &WriteText;
tc->fWriteSubTextW = &WriteTextW;
tc->fTextColor = NULL;
tc->canWriteWideChar = 0;
tc->out = out;
tc->color = ATT_NORMAL;
if( !out ) return;
HMODULE ntdll = GetModuleHandle( "ntdll.dll" );
if( !ntdll ) return;
DWORD flags;
if( GetConsoleMode(tc->out,&flags) )
{
if( GetProcAddress(ntdll,"wine_get_version") )
{
// wine terminal
setTextColorTerminal( tc );
return;
}
// windows console {{{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo( tc->out,&csbi );
int bg = csbi.wAttributes&0xf0;
tc->fWriteSubTextW = &WriteTextConsoleW;
tc->fTextColor = &TextColorConsole;
tc->canWriteWideChar = 1;
tc->colors[ATT_NORMAL] = csbi.wAttributes&0xff;
tc->colors[ATT_OK] = bg|FOREGROUND_GREEN|FOREGROUND_INTENSITY;
tc->colors[ATT_SECTION] =
bg|FOREGROUND_BLUE|FOREGROUND_GREEN|FOREGROUND_INTENSITY;
tc->colors[ATT_INFO] =
bg|FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY;
tc->colors[ATT_WARN] = bg|FOREGROUND_RED|FOREGROUND_INTENSITY;
tc->colors[ATT_BASE] = ( bg^BACKGROUND_INTENSITY )|( bg>>4 );
int i;
bg = bg | ( bg>>4 );
for( i=0; i<ATT_COUNT; i++ )
if( tc->colors[i]==bg ) tc->colors[i] ^= 0x08;
// }}}
return;
}
func_NtQueryObject *fNtQueryObject =
(func_NtQueryObject*)GetProcAddress( ntdll,"NtQueryObject" );
if( fNtQueryObject )
{
HANDLE heap = GetProcessHeap();
OBJECT_NAME_INFORMATION *oni =
HeapAlloc( heap,0,sizeof(OBJECT_NAME_INFORMATION) );
ULONG len;
if( !fNtQueryObject(tc->out,ObjectNameInformation,
oni,sizeof(OBJECT_NAME_INFORMATION),&len) )
{
wchar_t namedPipe[] = L"\\Device\\NamedPipe\\";
size_t l1 = sizeof(namedPipe)/2 - 1;
wchar_t toMaster[] = L"-to-master";
size_t l2 = sizeof(toMaster)/2 - 1;
wchar_t toMaster2[] = L"-to-master-nat";
size_t l3 = sizeof(toMaster2)/2 - 1;
wchar_t html[] = L".html";
size_t hl = sizeof(html)/2 - 1;
wchar_t deviceNull[] = L"\\Device\\Null";
size_t dnl = sizeof(deviceNull)/2 - 1;
if( (size_t)oni->Name.Length/2>l1+l2 &&
!memcmp(oni->Name.Buffer,namedPipe,l1*2) &&
(!memcmp(oni->Name.Buffer+(oni->Name.Length/2-l2),toMaster,l2*2) ||
!memcmp(oni->Name.Buffer+(oni->Name.Length/2-l3),toMaster2,l3*2)) )
{
// terminal emulator
setTextColorTerminal( tc );
}
else if( oni->Name.Length/2==dnl &&
!memcmp(oni->Name.Buffer,deviceNull,dnl*2) )
{
// null device
tc->out = NULL;
}
else if( GetFileType(tc->out)==FILE_TYPE_DISK &&
(size_t)oni->Name.Length/2>hl &&
!memcmp(oni->Name.Buffer+(oni->Name.Length/2-hl),html,hl*2) )
{
// html file {{{
tc->fWriteSubText = &WriteTextHtml;
tc->fWriteSubTextW = &WriteTextHtmlW;
tc->fTextColor = &TextColorHtml;
tc->canWriteWideChar = 1;
tc->styles[ATT_NORMAL] = NULL;
tc->styles[ATT_OK] = "ok";
tc->styles[ATT_SECTION] = "section";
tc->styles[ATT_INFO] = "info";
tc->styles[ATT_WARN] = "warn";
tc->styles[ATT_BASE] = "base";