From 16aaf59a56988f38c06fe62b34070c3d3cc75f07 Mon Sep 17 00:00:00 2001 From: "Young, Mark" Date: Mon, 25 May 2020 01:22:07 +0100 Subject: [PATCH] New -r option. Fixes #2 --- .vscode/launch.json | 5 +-- CHANGES.md | 8 ++++- HexPrintFile.csproj | 2 +- Program.cs | 76 +++++++++++++++++++++++++++++++-------------- 4 files changed, 64 insertions(+), 27 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index b933895..be82683 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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", @@ -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 diff --git a/CHANGES.md b/CHANGES.md index 952ecc7..2ddc505 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 @@ -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 \ No newline at end of file +[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 diff --git a/HexPrintFile.csproj b/HexPrintFile.csproj index 9dff646..cf9c6d1 100644 --- a/HexPrintFile.csproj +++ b/HexPrintFile.csproj @@ -7,7 +7,7 @@ false - 1.0.1 + 1.1.0 diff --git a/Program.cs b/Program.cs index c675d00..91879f7 100644 --- a/Program.cs +++ b/Program.cs @@ -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 @@ -98,6 +102,9 @@ private static int Main(string[] args) var opCountBytes = app.Option("-c|--count-bytes ", "Returns X bytes from start byte (if provided). Cannot be used with -e.", CommandOptionType.SingleValue); + var opChunkBytes = app.Option("-r|--read-chunk-size ", + "Sets the chunk size. Minimum 4, Maximum 64. Default is 16.", + CommandOptionType.SingleValue); var opUseExtendedChars = app.Option("-x|--extended", "Use extended ASCII characters in output.", CommandOptionType.NoValue); @@ -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()) @@ -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; @@ -214,22 +238,25 @@ private static int Main(string[] args) /// Full file size in bytes /// Use extended raw text output /// Should the counts be indexed from 1 or 0 + /// Chunk size to read data in /// TRUE on success 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); @@ -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(""); @@ -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 { @@ -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) @@ -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; @@ -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++; } } @@ -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; } /// /// Outputs the header for the HEX print /// + /// Chunk size /// Main byte range image /// Zero count byte range image - 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) { @@ -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 += "=";