Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"name": "Debug Run | Program.cs -s 100 -c 150",
"name": "Debug Run | Program.cs -s 100 -c 150 -r 8",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
Expand All @@ -14,7 +14,8 @@
"args": [
"${workspaceFolder}/Program.cs",
"-s 100",
"-c 150"
"-c 150",
"-r 8"
],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
8 changes: 7 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.1.0] - 2020-05-25

### Added
- New option to change the read block size (minimum 4, maximum 64, default 16)

## [1.0.1] - 2020-05-24

### Added
Expand All @@ -21,4 +26,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Initial release

[unreleased]: https://github.com/tip2tail/HexPrintFile/compare/v1.0.1...HEAD
[1.0.1]: https://github.com/tip2tail/HexPrintFile/compare/v1.0.0...v1.0.1
[1.0.1]: https://github.com/tip2tail/HexPrintFile/compare/v1.0.0...v1.0.1
[1.1.0]: https://github.com/tip2tail/HexPrintFile/compare/v1.0.1...v1.1.0
2 changes: 1 addition & 1 deletion HexPrintFile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<!-- Deterministic needs to be false for the build date functionality to work -->
<Deterministic>false</Deterministic>
<AssemblyVersion>1.0.1</AssemblyVersion>
<AssemblyVersion>1.1.0</AssemblyVersion>

</PropertyGroup>

Expand Down
76 changes: 53 additions & 23 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ private enum ReturnCode
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,
}

// Read in 16 byte chunks
Expand Down Expand Up @@ -98,6 +102,9 @@ private static int Main(string[] args)
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);
Expand Down Expand Up @@ -127,6 +134,23 @@ private static int Main(string[] args)
// 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())
Expand Down Expand Up @@ -190,7 +214,7 @@ private static int Main(string[] args)
var useExtended = opUseExtendedChars.HasValue();

// We have validated our inputs, pass for processing
if (ExecuteHexPrint(fileName.Value, startAt, endAt, fileSize, useExtended, indexFromOne)) return 0;
if (ExecuteHexPrint(fileName.Value, startAt, endAt, fileSize, useExtended, indexFromOne, chunkSize)) return 0;

Console.WriteLine("ERROR: Cannot display data.");
return (int)ReturnCode.GENERAL_ERROR;
Expand All @@ -214,22 +238,25 @@ private static int Main(string[] args)
/// <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)
bool indexFromOne,
uint chunkSize)
{
var block = new byte[ChunkSize];
int chunkSizeInt = Convert.ToInt32(chunkSize);
var block = new byte[chunkSizeInt];
var toRead = endAt - startAt;

// We read in chunks (set above at 16, however may be changed in future)
// 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 15 bytes extra - so be it!
var actualBytesRead = Math.Min(((toRead - 1) | (ChunkSize - 1)) + 1, fileSize);
var chunksToRead = actualBytesRead / ChunkSize;
// 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);
Expand All @@ -243,7 +270,7 @@ private static bool ExecuteHexPrint(string fileName,
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: " + ChunkSize + " bytes");
Console.WriteLine("Chunk size: " + chunkSizeInt + " bytes");
Console.WriteLine("Actual total read: " + actualBytesRead + " bytes");
Console.WriteLine("Chunks: " + chunksToRead);
Console.WriteLine("");
Expand All @@ -252,7 +279,7 @@ private static bool ExecuteHexPrint(string fileName,
: "Note: Strings are all ZERO INDEXED");
Console.WriteLine("");

OutputHeader(ref mainImage, zeroImage);
OutputHeader(chunkSizeInt, ref mainImage, zeroImage);

try
{
Expand All @@ -263,13 +290,13 @@ private static bool ExecuteHexPrint(string fileName,
}
stream.Seek(startAt, SeekOrigin.Begin);

var byteEnd = (startAt + ChunkSize) - 1;
var byteEnd = (startAt + chunkSizeInt) - 1;
var fromZeroStart = 0;
var fromZeroEnd = ChunkSize - 1;
var fromZeroEnd = chunkSizeInt - 1;
var chunksRead = 0;

int bytesRead;
while ((bytesRead = stream.Read(block, 0, ChunkSize)) > 0)
while ((bytesRead = stream.Read(block, 0, chunkSizeInt)) > 0)
{

if (chunksRead >= chunksToRead)
Expand All @@ -279,14 +306,14 @@ private static bool ExecuteHexPrint(string fileName,

var hexString = BitConverter.ToString(block).Replace("-", " ");
var rawString = AsciiOctets2String(block, useExtended);
if (bytesRead != ChunkSize)
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((ChunkSize * 3) - 1);
hexString = hexString.PadRight((chunkSizeInt * 3) - 1);
rawString = rawString.Substring(0, bytesRead);
rawString = rawString.PadRight(ChunkSize);
rawString = rawString.PadRight(chunkSizeInt);
}

string outputString;
Expand All @@ -303,10 +330,10 @@ private static bool ExecuteHexPrint(string fileName,

Console.WriteLine(outputString);

startAt += ChunkSize;
byteEnd += ChunkSize;
fromZeroStart += ChunkSize;
fromZeroEnd += ChunkSize;
startAt += chunkSizeInt;
byteEnd += chunkSizeInt;
fromZeroStart += chunkSizeInt;
fromZeroEnd += chunkSizeInt;
chunksRead++;
}
}
Expand All @@ -315,17 +342,17 @@ private static bool ExecuteHexPrint(string fileName,
Console.WriteLine("EXCEPTION: " + exception.Message);
return false;
}

OutputHeader(ref mainImage, zeroImage);
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(ref string mainImage, string zeroImage)
private static void OutputHeader(int chunkSize, ref string mainImage, string zeroImage)
{
// Main heading minimum = 14
if (mainImage.Length < 5) {
Expand All @@ -343,7 +370,10 @@ private static void OutputHeader(ref string mainImage, string zeroImage)
zeroHeading += " ";
}

var dataLine = $"* HEX DATA * | RAW DATA | {mainHeading} | {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 += "=";
Expand Down