-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cs
94 lines (85 loc) · 4.29 KB
/
Logger.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Narrative;
class Logger {
public static void diff_bytes ( Byte[] data1, Byte[] data2, UInt64 start, UInt64 end, UInt64 line_length = 32 ) {
for ( UInt64 i = start ; i < end ; i += line_length ) {
Boolean line_diff = false;
List<Action> act_1_hex = new List<Action>();
List<Action> act_1_char = new List<Action>();
List<Action> act_2_hex = new List<Action>();
List<Action> act_2_char = new List<Action>();
for ( UInt64 j = 0 ; j < line_length && i + j < end ; ++j ) {
Byte c1 = data1[i + j];
Byte c2 = data2[i + j];
Boolean diff = c1 != c2;
if ( c1 != c2 )
diff = true;
act_1_hex.Add(() => {Console.ForegroundColor = diff ? ConsoleColor.Red : ConsoleColor.White;});
act_1_hex.Add(() => {Console.Write(c1.ToString("X2").Replace("0", "-"));});
act_1_char.Add(() => {Console.ForegroundColor = diff ? ConsoleColor.Red : ConsoleColor.White;});
act_1_char.Add(() => {Console.Write(c1 >= 32 && c1 <= 126 ? (Char) c1 : '.');});
act_2_hex.Add(() => {Console.ForegroundColor = diff ? ConsoleColor.Red : ConsoleColor.White;});
act_2_hex.Add(() => {Console.Write(c2.ToString("X2").Replace("0", "-"));});
act_2_char.Add(() => {Console.ForegroundColor = diff ? ConsoleColor.Red : ConsoleColor.White;});
act_2_char.Add(() => {Console.Write(c2 >= 32 && c2 <= 126 ? (Char) c2 : '.');});
if ( diff )
line_diff = true;
}
if ( line_diff ) {
Console.ForegroundColor = ConsoleColor.White;
Console.Write($"{i:X6}: ");
act_1_hex.ForEach(cmd => cmd());
Console.ForegroundColor = ConsoleColor.White;
Console.Write($" | ");
act_2_hex.ForEach(cmd => cmd());
Console.ForegroundColor = ConsoleColor.White;
Console.Write($" || ");
act_1_char.ForEach(cmd => cmd());
Console.ForegroundColor = ConsoleColor.White;
Console.Write($" | ");
act_2_char.ForEach(cmd => cmd());
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
}
}
}
public static void hex_dump ( Byte[] bytes, AddressRange<UInt32> range, UInt32 line_length = 32, Func<UInt32, ConsoleColor> color_func = null ) {
hex_dump(bytes, range.start, range.end, line_length, color_func);
}
public static void hex_dump ( Byte[] bytes, AddressRange<UInt64> range, UInt32 line_length = 32, Func<UInt32, ConsoleColor> color_func = null ) {
hex_dump(bytes, (UInt32) range.start, (UInt32) range.end, line_length, color_func);
}
public static void hex_dump ( Byte[] bytes, UInt32 start = 0, UInt32 end = 0, UInt32 line_length = 32, Func<UInt32, ConsoleColor> color_func = null ) {
if (end == 0)
end = (UInt32)bytes.Length;
if (color_func == null)
color_func = (_) => ConsoleColor.White;
for ( UInt32 i = start; i < end; i += line_length ) {
Console.Write($"{i:X6} | {i-start:X6} : ");
for ( UInt32 j = 0; j < line_length; j++ ) {
Console.ForegroundColor = color_func(i + j);
Console.Write(
i + j < end ?
$"{bytes[i + j]:X2} ".Replace("0", "-") :
" "
);
}
Console.ForegroundColor = ConsoleColor.White;
Console.Write(" ");
for ( UInt32 j = 0; j < line_length && i + j < end; ++j ) {
Console.ForegroundColor = color_func(i + j);
Console.Write(
bytes[i + j] >= 32 && bytes[i + j] <= 126 ?
$"{(char)bytes[i + j]}" :
"."
);
}
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine();
}
Console.ForegroundColor = ConsoleColor.White;
}
}