-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDataFormatter.cs
More file actions
214 lines (185 loc) · 7.66 KB
/
DataFormatter.cs
File metadata and controls
214 lines (185 loc) · 7.66 KB
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
/*
MBNCSUtil -- Managed Battle.net Authentication Library
Copyright (C) 2005-2008 by Robert Paveza
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1.) Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2.) Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3.) The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.
See LICENSE.TXT that should have accompanied this software for full terms and
conditions.
*/
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace MBNCSUtil
{
/// <summary>
/// Provides functions for printing bytes to various output devices. This class cannot
/// be inherited.
/// </summary>
/// <example>
/// <para>This example demonstrates how the formatter prints out binary data.</para>
/// <code language="c#">
/// DataFormatter.WriteToConsole(XSha1.CalculateHash(Encoding.Latin1.GetBytes("password")));
/// </code>
/// <para><b>Output:</b></para>
/// <code>
/// 0000 ec c8 0d 1d 76 e7 58 c0 b9 da 8c 25 ff 10 6a ff ìE..vçXA.U.%ÿ.jÿ
/// 0010 8e 24 29 16 .$).
/// </code>
/// </example>
public static class DataFormatter
{
/// <summary>
/// Formats a data into 16-byte rows followed by an ASCII representation.
/// </summary>
/// <param name="data">The data to format.</param>
/// <returns>A string representing the data.</returns>
/// <exception cref="ArgumentNullException">Thrown if <b>data</b> is <b>null</b>
/// (<b>Nothing</b> in Visual Basic).</exception>
public static string Format(byte[] data)
{
if (data == null)
throw new ArgumentNullException(Resources.param_data, Resources.dataNull);
StringBuilder sb = new StringBuilder();
sb.Append("0000 ");
if (data.Length == 0)
{
sb.Append("(empty)");
return sb.ToString();
}
StringBuilder lineAscii = new StringBuilder(16, 16);
for (int i = 0; i < data.Length; i++)
{
#region build the end-of-line ascii
char curData = (char)data[i];
if (char.IsLetterOrDigit(curData) || char.IsPunctuation(curData) ||
char.IsSymbol(curData) || curData == ' ')
{
lineAscii.Append(curData);
}
else
{
lineAscii.Append('.');
}
#endregion
sb.AppendFormat("{0:x2} ", data[i]);
if ((i + 1) % 8 == 0)
{
sb.Append(" ");
}
if (((i + 1) % 16 == 0) || ((i + 1) == data.Length))
{
if ((i + 1) == data.Length && ((i + 1) % 16) != 0)
{
int lenOfCurStr = ((i % 16) * 3);
if ((i % 16) > 8) lenOfCurStr++;
for (int j = 0; j < (47 - lenOfCurStr); j++)
sb.Append(' ');
}
sb.AppendFormat(" {0}", lineAscii.ToString());
lineAscii = new StringBuilder(16, 16);
sb.Append(Environment.NewLine);
if (data.Length > (i + 1))
{
sb.AppendFormat("{0:x4} ", i + 1);
}
}
}
return sb.ToString();
}
/// <summary>
/// Formats a data into 16-byte rows followed by an ASCII representation.
/// </summary>
/// <param name="data">The data to format.</param>
/// <param name="startIndex">The starting position of the data to format.</param>
/// <param name="length">The amount of data to format.</param>
/// <returns>A string representing the data.</returns>
/// <exception cref="ArgumentNullException">Thrown if <b>data</b> is <b>null</b>
/// (<b>Nothing</b> in Visual Basic).</exception>
public static string Format(byte[] data, int startIndex, int length)
{
if (data == null)
throw new ArgumentNullException(Resources.param_data, Resources.dataNull);
StringBuilder sb = new StringBuilder();
sb.Append("0000 ");
if (data.Length == 0)
{
sb.Append("(empty)");
return sb.ToString();
}
StringBuilder lineAscii = new StringBuilder(16, 16);
for (int i = startIndex; i < data.Length && i < (startIndex + length); i++)
{
#region build the end-of-line ascii
char curData = (char)data[i];
if (char.IsLetterOrDigit(curData) || char.IsPunctuation(curData) ||
char.IsSymbol(curData) || curData == ' ')
{
lineAscii.Append(curData);
}
else
{
lineAscii.Append('.');
}
#endregion
sb.AppendFormat("{0:x2} ", data[i]);
if ((i + 1) % 8 == 0)
{
sb.Append(" ");
}
if (((i + 1) % 16 == 0) || ((i + 1) == data.Length))
{
if ((i + 1) == data.Length && ((i + 1) % 16) != 0)
{
int lenOfCurStr = ((i % 16) * 3);
if ((i % 16) > 8) lenOfCurStr++;
for (int j = 0; j < (47 - lenOfCurStr); j++)
sb.Append(' ');
}
sb.AppendFormat(" {0}", lineAscii.ToString());
lineAscii = new StringBuilder(16, 16);
sb.Append(Environment.NewLine);
if (data.Length > (i + 1))
{
sb.AppendFormat("{0:x4} ", i + 1);
}
}
}
return sb.ToString();
}
/// <summary>
/// Writes a series of bytes to the console, printing them in 16-byte rows
/// followed by an ASCII representation.
/// </summary>
/// <param name="data">The data to print.</param>
public static void WriteToConsole(byte[] data)
{
Console.WriteLine(Format(data));
}
/// <summary>
/// Writes a series of bytes to trace listeners, printing them in 16-byte rows,
/// followed by an ASCII representation.
/// </summary>
/// <param name="data">The data to print.</param>
public static void WriteToTrace(byte[] data)
{
System.Diagnostics.Trace.WriteLine(Format(data));
}
/// <summary>
/// Writes a series of bytes to trace listeners, printing them in 16-byte rows,
/// followed by an ASCII representation.
/// </summary>
/// <param name="data">The data to print.</param>
/// <param name="category">A category name to classify the data.</param>
public static void WriteToTrace(byte[] data, string category)
{
System.Diagnostics.Trace.WriteLine(Format(data), category);
}
}
}