-
Notifications
You must be signed in to change notification settings - Fork 121
/
HexDump.cs
113 lines (92 loc) · 4.02 KB
/
HexDump.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace SeBackupPrivilegePoC
{
internal class HexDump
{
public static void Dump(byte[] data, int nIndentCount)
{
IntPtr pBufferToRead = Marshal.AllocHGlobal(data.Length);
Marshal.Copy(data, 0, pBufferToRead, data.Length);
Dump(pBufferToRead, new IntPtr(-1), (uint)data.Length, nIndentCount);
Marshal.FreeHGlobal(pBufferToRead);
}
public static void Dump(byte[] data, uint nRange, int nIndentCount)
{
IntPtr pBufferToRead = Marshal.AllocHGlobal(data.Length);
Marshal.Copy(data, 0, pBufferToRead, data.Length);
Dump(pBufferToRead, new IntPtr(-1), nRange, nIndentCount);
Marshal.FreeHGlobal(pBufferToRead);
}
public static void Dump(byte[] data, IntPtr pBaseAddress, uint nRange, int nIndentCount)
{
IntPtr pBufferToRead = Marshal.AllocHGlobal(data.Length);
Marshal.Copy(data, 0, pBufferToRead, data.Length);
Dump(pBufferToRead, pBaseAddress, nRange, nIndentCount);
Marshal.FreeHGlobal(pBufferToRead);
}
public static void Dump(IntPtr pBufferToRead, uint nRange, int nIndentCount)
{
Dump(pBufferToRead, new IntPtr(-1), nRange, nIndentCount);
}
public static void Dump(IntPtr pBufferToRead, IntPtr pBaseAddress, uint nRange, int nIndentCount)
{
string addressFormat;
string headFormat;
string lineFormat;
var hexBuilder = new StringBuilder();
var charBuilder = new StringBuilder();
var outputBuilder = new StringBuilder();
if (pBaseAddress == new IntPtr(-1))
{
pBaseAddress = IntPtr.Zero;
addressFormat = "X8";
headFormat = string.Format("{{0,{0}}} {{1,-47}}\n\n", 8 + (nIndentCount * 4));
lineFormat = string.Format("{{0,{0}}} | {{1,-47}} | {{2}}\n", 8 + (nIndentCount * 4));
}
else
{
addressFormat = (IntPtr.Size == 8) ? "X16" : "X8";
headFormat = string.Format("{{0,{0}}} {{1,-47}}\n\n", (IntPtr.Size * 2) + (nIndentCount * 4));
lineFormat = string.Format("{{0,{0}}} | {{1,-47}} | {{2}}\n", (IntPtr.Size * 2) + (nIndentCount * 4));
}
if (nRange > 0)
{
outputBuilder.AppendFormat(headFormat, string.Empty, "00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
for (var idx = 0; idx < nRange; idx++)
{
var address = pBaseAddress.ToInt64() + (idx & (~0x0Fu));
var readByte = Marshal.ReadByte(pBufferToRead, idx);
hexBuilder.Append(readByte.ToString("X2"));
charBuilder.Append(IsPrintable((char)readByte) ? (char)readByte : '.');
if ((idx % 16 == 15) || (idx == (nRange - 1)))
{
outputBuilder.AppendFormat(
lineFormat,
address.ToString(addressFormat),
hexBuilder.ToString(),
charBuilder.ToString());
hexBuilder.Clear();
charBuilder.Clear();
}
else if ((idx % 16 == 7) && (idx != (nRange - 1)))
{
hexBuilder.Append("-");
charBuilder.Append(" ");
}
else
{
hexBuilder.Append(" ");
}
}
Console.WriteLine(outputBuilder.ToString());
outputBuilder.Clear();
}
}
private static bool IsPrintable(char code)
{
return (Char.IsLetterOrDigit(code) || Char.IsPunctuation(code) || Char.IsSymbol(code));
}
}
}