-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
466 lines (426 loc) · 19.9 KB
/
Program.cs
File metadata and controls
466 lines (426 loc) · 19.9 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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using McMaster.Extensions.CommandLineUtils;
namespace HexPrintFile
{
internal static class Program
{
// Return Code
private enum ReturnCode
{
[Description("Success")]
OK = 0,
[Description("File not found")]
FILE_NOT_FOUND = 1,
[Description("Satrt byte greater than the length of the file")]
START_LARGER_THAN_LENGTH = 2,
[Description("End byte greater than or equal to the length of the file")]
END_BYTE_BEYOND_LENGTH = 3,
[Description("General error displaying the data")]
GENERAL_ERROR = 4,
[Description("Cannot use opptions -c and -e together")]
OPTION_C_AND_E = 5,
[Description("Start byte is less than zero")]
START_LESS_ZERO = 6,
[Description("End byte less than start byte")]
END_LESS_THAN_START = 7,
[Description("Command line parsing exception")]
COMMAND_EXCEPTION = 8,
[Description("Start byte is less than one and index from one mode enabled")]
START_LESS_ONE = 9,
[Description("Read block size cannot be less than 4 bytes")]
CHUNK_LESS_THAN_MINIMUM = 10,
[Description("Read block size cannot be more than 64 bytes")]
CHUNK_MORE_THAN_MAXIMUM = 11,
}
private static readonly string[] ControlChars =
{
"<NUL>" , "<SOH>" , "<STX>" , "<ETX>" ,
"<EOT>" , "<ENQ>" , "<ACK>" , "<BEL>" ,
"<BS>" , "<HT>" , "<LF>" , "<VT>" ,
"<FF>" , "<CR>" , "<SO>" , "<SI>" ,
"<DLE>" , "<DC1>" , "<DC2>" , "<DC3>" ,
"<DC4>" , "<NAK>" , "<SYN>" , "<ETB>" ,
"<CAN>" , "<EM>" , "<SUB>" , "<ESC>" ,
"<FS>" , "<GS>" , "<RS>" , "<US>" ,
} ;
/// <summary>
/// Main program access point
/// </summary>
/// <param name="args">Program arguments</param>
/// <returns>Error code, 0 on success</returns>
private static int Main(string[] args)
{
// Get Build Information
var buildDate = Assembly.GetExecutingAssembly().GetLinkerTimestampUtc().ToLocalTime();
var buildDateString = $"{buildDate.Year.ToString("0000")}-{buildDate.Month.ToString("00")}-{buildDate.Day.ToString("00")} {buildDate.Hour.ToString("00")}:{buildDate.Minute.ToString("00")}:{buildDate.Second.ToString("00")}";
var buildVersion = Assembly.GetEntryAssembly()?.GetName().Version;
var buildVersionString = $"{buildVersion.Major}.{buildVersion.Minor}.{buildVersion.Build}";
var app = new CommandLineApplication
{
Name = "HexPrintFile",
Description = "A tool to print the hex representation of a file, or portion of a file",
ExtendedHelpText = $@"
> ----------------------------------------------------
> Created by Mark Young
> github.com/tip2tail
> ----------------------------------------------------
> Return Codes
{GetReturnCodeDescriptions("> ")}
> ----------------------------------------------------
> Version: {buildVersionString}
> Buid Date: {buildDateString}
> ----------------------------------------------------
",
};
// Setup
app.HelpOption(inherited: true);
app.UnrecognizedArgumentHandling = UnrecognizedArgumentHandling.Throw;
// File is always required, the others are optional
var fileName = app.Argument("file", "File to hex print").IsRequired();
var opStartByte = app.Option<long>("-s|--start-byte <BYTE>",
"Byte to start reading from. 0 if excluded. Counted from index 0.",
CommandOptionType.SingleValue);
var opEndByte = app.Option<long>("-e|--end-byte <BYTE>",
"Byte to read to (inclusive). Reads to EOF if excluded. Counted from index 0. Cannot be used with -c.",
CommandOptionType.SingleValue);
var opCountBytes = app.Option<long>("-c|--count-bytes <COUNT>",
"Returns X bytes from start byte (if provided). Cannot be used with -e.",
CommandOptionType.SingleValue);
var opChunkBytes = app.Option<uint>("-r|--read-chunk-size <BYTES>",
"Sets the chunk size. Minimum 4, Maximum 64. Default is 16.",
CommandOptionType.SingleValue);
var opUseExtendedChars = app.Option<bool>("-x|--extended",
"Use extended ASCII characters in output.",
CommandOptionType.NoValue);
var opIndexFromOne = app.Option<bool>("-o|--index-from-one",
"Index the bytes from 1 rather than 0.",
CommandOptionType.NoValue);
// Execution logic
app.OnExecute(() =>
{
// Verify that the file is valid
if (!File.Exists(fileName.Value))
{
Console.WriteLine("ERROR: File not found.");
return (int)ReturnCode.FILE_NOT_FOUND; // File not found
}
if (opEndByte.HasValue() && opCountBytes.HasValue())
{
Console.WriteLine("ERROR: Cannot use -c and -e together.");
return (int)ReturnCode.OPTION_C_AND_E; // Cannot use -c and -e together
}
// Get the length in bytes
var fileSize = (new FileInfo(fileName.Value)).Length;
// Index from one?
var indexFromOne = opIndexFromOne.HasValue();
// Set the chunk size
uint chunkSize = 16;
if (opChunkBytes.HasValue())
{
if (opChunkBytes.ParsedValue < 4)
{
Console.WriteLine("ERROR: Read block size cannot be less than 4 bytes.");
return (int)ReturnCode.CHUNK_LESS_THAN_MINIMUM;
}
if (opChunkBytes.ParsedValue > 64)
{
Console.WriteLine("ERROR: Read block size cannot be greater than 64 bytes.");
return (int)ReturnCode.CHUNK_MORE_THAN_MAXIMUM;
}
chunkSize = opChunkBytes.ParsedValue;
}
// Validate if we have a start and end
long startAt = 0;
if (opStartByte.HasValue())
{
if (opStartByte.ParsedValue < 0)
{
Console.WriteLine("ERROR: Start byte cannot be less than zero.");
return (int)ReturnCode.START_LESS_ZERO;
}
if (indexFromOne && opStartByte.ParsedValue < 1)
{
Console.WriteLine("ERROR: Start byte cannot be less than one (when indexed from one).");
return (int)ReturnCode.START_LESS_ONE;
}
if (opStartByte.ParsedValue >= fileSize)
{
Console.WriteLine("ERROR: Start byte cannot be greater than the length of the file.");
return (int)ReturnCode.START_LARGER_THAN_LENGTH;
}
startAt = opStartByte.ParsedValue;
}
long endAt = fileSize - 1;
if (opCountBytes.HasValue())
{
if (indexFromOne) {
endAt = Math.Min(startAt + opCountBytes.ParsedValue, fileSize);
} else {
endAt = Math.Min(startAt + opCountBytes.ParsedValue, (fileSize - 1));
}
}
else if (opEndByte.HasValue())
{
if (indexFromOne)
{
if (opEndByte.ParsedValue > fileSize)
{
Console.WriteLine("ERROR: End byte cannot be greater than the length of the file.");
return (int)ReturnCode.END_BYTE_BEYOND_LENGTH;
}
}
else
{
if (opEndByte.ParsedValue >= fileSize)
{
Console.WriteLine("ERROR: End byte cannot be greater than the length of the file.");
return (int)ReturnCode.END_BYTE_BEYOND_LENGTH;
}
}
if (opEndByte.ParsedValue < startAt)
{
Console.WriteLine("ERROR: End byte must be greater than or equal to the start byte.");
return (int)ReturnCode.END_LESS_THAN_START;
}
endAt = opEndByte.ParsedValue;
}
// Extended output
var useExtended = opUseExtendedChars.HasValue();
// We have validated our inputs, pass for processing
if (ExecuteHexPrint(fileName.Value, startAt, endAt, fileSize, useExtended, indexFromOne, chunkSize)) return 0;
Console.WriteLine("ERROR: Cannot display data.");
return (int)ReturnCode.GENERAL_ERROR;
});
try {
return app.Execute(args);
}
catch (CommandParsingException ex) {
Console.WriteLine("EXCEPTION: Error when parsing the command line: " + ex.Message);
return (int)ReturnCode.COMMAND_EXCEPTION;
}
}
/// <summary>
/// Execute the HEX Print
/// </summary>
/// <param name="fileName">File to read</param>
/// <param name="startAt">Starting byte</param>
/// <param name="endAt">End byte</param>
/// <param name="fileSize">Full file size in bytes</param>
/// <param name="useExtended">Use extended raw text output</param>
/// <param name="indexFromOne">Should the counts be indexed from 1 or 0</param>
/// <param name="chunkSize">Chunk size to read data in</param>
/// <returns>TRUE on success</returns>
private static bool ExecuteHexPrint(string fileName,
long startAt,
long endAt,
long fileSize,
bool useExtended,
bool indexFromOne,
uint chunkSize)
{
int chunkSizeInt = Convert.ToInt32(chunkSize);
var block = new byte[chunkSizeInt];
var toRead = endAt - startAt;
// We read in chunks (default 16)
// So we need to work out how many chunks will give us our data
// The result is that we may read up to (n-1) bytes extra - so be it!
var actualBytesRead = Math.Min(((toRead - 1) | (chunkSize - 1)) + 1, fileSize);
var chunksToRead = actualBytesRead / chunkSizeInt;
var mainImage = StringLengthImage(endAt);
var zeroImage = StringLengthImage(actualBytesRead);
Console.WriteLine("HexPrintFile");
Console.WriteLine("============");
Console.WriteLine("");
Console.WriteLine("Opening file: " + fileName);
Console.WriteLine("File size: " + fileSize + " bytes");
Console.WriteLine("");
Console.WriteLine("Start At Byte: " + (indexFromOne ? (startAt+1).ToString(mainImage) : startAt.ToString(mainImage)));
Console.WriteLine("End At Byte: " + (indexFromOne ? (endAt+1).ToString(mainImage) : endAt.ToString(mainImage)));
Console.WriteLine("Bytes to read: " + toRead.ToString());
Console.WriteLine("Chunk size: " + chunkSizeInt + " bytes");
Console.WriteLine("Actual total read: " + actualBytesRead + " bytes");
Console.WriteLine("Chunks: " + chunksToRead);
Console.WriteLine("");
Console.WriteLine(indexFromOne
? "Note: Strings are all INDEXED FROM ONE"
: "Note: Strings are all ZERO INDEXED");
Console.WriteLine("");
OutputHeader(chunkSizeInt, ref mainImage, zeroImage);
try
{
// Open the file and seek the starting byte
var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
if (indexFromOne) {
startAt++;
}
stream.Seek(startAt, SeekOrigin.Begin);
var byteEnd = (startAt + chunkSizeInt) - 1;
var fromZeroStart = 0;
var fromZeroEnd = chunkSizeInt - 1;
var chunksRead = 0;
int bytesRead;
while ((bytesRead = stream.Read(block, 0, chunkSizeInt)) > 0)
{
if (chunksRead >= chunksToRead)
{
break;
}
var hexString = BitConverter.ToString(block).Replace("-", " ");
var rawString = AsciiOctets2String(block, useExtended);
if (bytesRead != chunkSizeInt)
{
// To tidy this up remove the 00 bytes that we did not read
var endIndex = (bytesRead * 3) - 1;
hexString = hexString.Substring(0, endIndex);
hexString = hexString.PadRight((chunkSizeInt * 3) - 1);
rawString = rawString.Substring(0, bytesRead);
rawString = rawString.PadRight(chunkSizeInt);
}
string outputString;
if (indexFromOne)
{
outputString =
$"* {hexString} * | {rawString} | {(startAt).ToString(mainImage)} -> {(byteEnd).ToString(mainImage)} | {(fromZeroStart+1).ToString(zeroImage)} -> {(fromZeroEnd+1).ToString(zeroImage)}";
}
else
{
outputString =
$"* {hexString} * | {rawString} | {startAt.ToString(mainImage)} -> {byteEnd.ToString(mainImage)} | {fromZeroStart.ToString(zeroImage)} -> {fromZeroEnd.ToString(zeroImage)}";
}
Console.WriteLine(outputString);
startAt += chunkSizeInt;
byteEnd += chunkSizeInt;
fromZeroStart += chunkSizeInt;
fromZeroEnd += chunkSizeInt;
chunksRead++;
}
}
catch (Exception exception)
{
Console.WriteLine("EXCEPTION: " + exception.Message);
return false;
}
OutputHeader(chunkSizeInt, ref mainImage, zeroImage);
return true;
}
/// <summary>
/// Outputs the header for the HEX print
/// </summary>
/// <param name="chunkSize">Chunk size</param>
/// <param name="mainImage" ref="true">Main byte range image</param>
/// <param name="zeroImage">Zero count byte range image</param>
private static void OutputHeader(int chunkSize, ref string mainImage, string zeroImage)
{
// Main heading minimum = 14
if (mainImage.Length < 5) {
mainImage = "00000";
}
var mainLen = ((2 * mainImage.Length) + 4) + 1;
var mainHeading = "ACTUAL BYTES";
while (mainHeading.Length < mainLen) {
mainHeading += " ";
}
var zeroLen = (2 * zeroImage.Length) + 4;
var zeroHeading = "READ COUNT";
while (zeroHeading.Length < zeroLen) {
zeroHeading += " ";
}
var dataHeading = " HEX DATA ".PadRight((chunkSize * 3) + 1);
var rawHeading = " RAW DATA ".PadRight(chunkSize + 2);
var dataLine = $"*{dataHeading}* |{rawHeading}| {mainHeading} | {zeroHeading} ";
var equalsLine = string.Empty;
while (equalsLine.Length < dataLine.Length) {
equalsLine += "=";
}
Console.WriteLine(equalsLine);
Console.WriteLine(dataLine);
Console.WriteLine(equalsLine);
}
/// <summary>
/// Converts a byte array to a string representation, converting any unprintable characters as required
/// </summary>
/// <param name="bytes">Array of bytes to convert</param>
/// <param name="useExtendedOutput">If true, will output a text representation of the unprintable character. Otherwise will display a box character.</param>
/// <returns>string output</returns>
private static string AsciiOctets2String(IReadOnlyCollection<byte> bytes, bool useExtendedOutput = false)
{
var sb = new StringBuilder(bytes.Count);
foreach (var c in bytes.Select(b => (char)b))
{
if (useExtendedOutput)
{
if (c < '\u0020')
{
sb.Append(ControlChars[c]);
}
else if (c == '\u007F')
{
sb.Append("<DEL>");
}
else if (c > '\u007F')
{
sb.AppendFormat(@"\u{0:X4}", (ushort) c);
}
else /* 0x20-0x7E */
{
sb.Append(c);
}
}
else
{
// Replace anything that is outside the range that is printable with a little box character
var s = c.ToString();
s = Regex.Replace(s, @"\p{C}+", "■");
sb.Append(s);
}
}
return sb.ToString() ;
}
/// <summary>
/// Returns a ToString image for the number of digits in the given value
/// </summary>
/// <param name="value">Array of bytes to convert</param>
/// <returns>string</returns>
private static string StringLengthImage(long value)
{
var image = string.Empty;
for (int i = 0; i < value.ToString().Length; i++) {
image += "0";
}
return image;
}
/// <summary>
/// Returns a string block showing all the values of the ReturnCode Enum
/// </summary>
/// <param name="lineStart">Characters to add at start of each line</param>
/// <returns>string</returns>
private static string GetReturnCodeDescriptions(string lineStart = null)
{
// Make sure this is not null
if (string.IsNullOrEmpty(lineStart))
{
lineStart = string.Empty;
}
var retVal = string.Empty;
foreach (ReturnCode retCode in (ReturnCode[])Enum.GetValues(typeof(ReturnCode)))
{
if (!retVal.Equals(string.Empty))
{
// New line
retVal += Environment.NewLine;
}
var value = (int)retCode;
var desc = retCode.GetDescription();
retVal += $"{lineStart}{value}: {desc}";
}
return retVal;
}
}
}