@@ -35,6 +35,10 @@ private enum ReturnCode
3535 COMMAND_EXCEPTION = 8 ,
3636 [ Description ( "Start byte is less than one and index from one mode enabled" ) ]
3737 START_LESS_ONE = 9 ,
38+ [ Description ( "Read block size cannot be less than 4 bytes" ) ]
39+ CHUNK_LESS_THAN_MINIMUM = 10 ,
40+ [ Description ( "Read block size cannot be more than 64 bytes" ) ]
41+ CHUNK_MORE_THAN_MAXIMUM = 11 ,
3842 }
3943
4044 // Read in 16 byte chunks
@@ -98,6 +102,9 @@ private static int Main(string[] args)
98102 var opCountBytes = app . Option < long > ( "-c|--count-bytes <COUNT>" ,
99103 "Returns X bytes from start byte (if provided). Cannot be used with -e." ,
100104 CommandOptionType . SingleValue ) ;
105+ var opChunkBytes = app . Option < uint > ( "-r|--read-chunk-size <BYTES>" ,
106+ "Sets the chunk size. Minimum 4, Maximum 64. Default is 16." ,
107+ CommandOptionType . SingleValue ) ;
101108 var opUseExtendedChars = app . Option < bool > ( "-x|--extended" ,
102109 "Use extended ASCII characters in output." ,
103110 CommandOptionType . NoValue ) ;
@@ -127,6 +134,23 @@ private static int Main(string[] args)
127134 // Index from one?
128135 var indexFromOne = opIndexFromOne . HasValue ( ) ;
129136
137+ // Set the chunk size
138+ uint chunkSize = 16 ;
139+ if ( opChunkBytes . HasValue ( ) )
140+ {
141+ if ( opChunkBytes . ParsedValue < 4 )
142+ {
143+ Console . WriteLine ( "ERROR: Read block size cannot be less than 4 bytes." ) ;
144+ return ( int ) ReturnCode . CHUNK_LESS_THAN_MINIMUM ;
145+ }
146+ if ( opChunkBytes . ParsedValue > 64 )
147+ {
148+ Console . WriteLine ( "ERROR: Read block size cannot be greater than 64 bytes." ) ;
149+ return ( int ) ReturnCode . CHUNK_MORE_THAN_MAXIMUM ;
150+ }
151+ chunkSize = opChunkBytes . ParsedValue ;
152+ }
153+
130154 // Validate if we have a start and end
131155 long startAt = 0 ;
132156 if ( opStartByte . HasValue ( ) )
@@ -190,7 +214,7 @@ private static int Main(string[] args)
190214 var useExtended = opUseExtendedChars . HasValue ( ) ;
191215
192216 // We have validated our inputs, pass for processing
193- if ( ExecuteHexPrint ( fileName . Value , startAt , endAt , fileSize , useExtended , indexFromOne ) ) return 0 ;
217+ if ( ExecuteHexPrint ( fileName . Value , startAt , endAt , fileSize , useExtended , indexFromOne , chunkSize ) ) return 0 ;
194218
195219 Console . WriteLine ( "ERROR: Cannot display data." ) ;
196220 return ( int ) ReturnCode . GENERAL_ERROR ;
@@ -214,22 +238,25 @@ private static int Main(string[] args)
214238 /// <param name="fileSize">Full file size in bytes</param>
215239 /// <param name="useExtended">Use extended raw text output</param>
216240 /// <param name="indexFromOne">Should the counts be indexed from 1 or 0</param>
241+ /// <param name="chunkSize">Chunk size to read data in</param>
217242 /// <returns>TRUE on success</returns>
218243 private static bool ExecuteHexPrint ( string fileName ,
219244 long startAt ,
220245 long endAt ,
221246 long fileSize ,
222247 bool useExtended ,
223- bool indexFromOne )
248+ bool indexFromOne ,
249+ uint chunkSize )
224250 {
225- var block = new byte [ ChunkSize ] ;
251+ int chunkSizeInt = Convert . ToInt32 ( chunkSize ) ;
252+ var block = new byte [ chunkSizeInt ] ;
226253 var toRead = endAt - startAt ;
227254
228- // We read in chunks (set above at 16, however may be changed in future )
255+ // We read in chunks (default 16 )
229256 // So we need to work out how many chunks will give us our data
230- // The result is that we may read up to 15 bytes extra - so be it!
231- var actualBytesRead = Math . Min ( ( ( toRead - 1 ) | ( ChunkSize - 1 ) ) + 1 , fileSize ) ;
232- var chunksToRead = actualBytesRead / ChunkSize ;
257+ // The result is that we may read up to (n-1) bytes extra - so be it!
258+ var actualBytesRead = Math . Min ( ( ( toRead - 1 ) | ( chunkSize - 1 ) ) + 1 , fileSize ) ;
259+ var chunksToRead = actualBytesRead / chunkSizeInt ;
233260
234261 var mainImage = StringLengthImage ( endAt ) ;
235262 var zeroImage = StringLengthImage ( actualBytesRead ) ;
@@ -243,7 +270,7 @@ private static bool ExecuteHexPrint(string fileName,
243270 Console . WriteLine ( "Start At Byte: " + ( indexFromOne ? ( startAt + 1 ) . ToString ( mainImage ) : startAt . ToString ( mainImage ) ) ) ;
244271 Console . WriteLine ( "End At Byte: " + ( indexFromOne ? ( endAt + 1 ) . ToString ( mainImage ) : endAt . ToString ( mainImage ) ) ) ;
245272 Console . WriteLine ( "Bytes to read: " + toRead . ToString ( ) ) ;
246- Console . WriteLine ( "Chunk size: " + ChunkSize + " bytes" ) ;
273+ Console . WriteLine ( "Chunk size: " + chunkSizeInt + " bytes" ) ;
247274 Console . WriteLine ( "Actual total read: " + actualBytesRead + " bytes" ) ;
248275 Console . WriteLine ( "Chunks: " + chunksToRead ) ;
249276 Console . WriteLine ( "" ) ;
@@ -252,7 +279,7 @@ private static bool ExecuteHexPrint(string fileName,
252279 : "Note: Strings are all ZERO INDEXED" ) ;
253280 Console . WriteLine ( "" ) ;
254281
255- OutputHeader ( ref mainImage , zeroImage ) ;
282+ OutputHeader ( chunkSizeInt , ref mainImage , zeroImage ) ;
256283
257284 try
258285 {
@@ -263,13 +290,13 @@ private static bool ExecuteHexPrint(string fileName,
263290 }
264291 stream . Seek ( startAt , SeekOrigin . Begin ) ;
265292
266- var byteEnd = ( startAt + ChunkSize ) - 1 ;
293+ var byteEnd = ( startAt + chunkSizeInt ) - 1 ;
267294 var fromZeroStart = 0 ;
268- var fromZeroEnd = ChunkSize - 1 ;
295+ var fromZeroEnd = chunkSizeInt - 1 ;
269296 var chunksRead = 0 ;
270297
271298 int bytesRead ;
272- while ( ( bytesRead = stream . Read ( block , 0 , ChunkSize ) ) > 0 )
299+ while ( ( bytesRead = stream . Read ( block , 0 , chunkSizeInt ) ) > 0 )
273300 {
274301
275302 if ( chunksRead >= chunksToRead )
@@ -279,14 +306,14 @@ private static bool ExecuteHexPrint(string fileName,
279306
280307 var hexString = BitConverter . ToString ( block ) . Replace ( "-" , " " ) ;
281308 var rawString = AsciiOctets2String ( block , useExtended ) ;
282- if ( bytesRead != ChunkSize )
309+ if ( bytesRead != chunkSizeInt )
283310 {
284311 // To tidy this up remove the 00 bytes that we did not read
285312 var endIndex = ( bytesRead * 3 ) - 1 ;
286313 hexString = hexString . Substring ( 0 , endIndex ) ;
287- hexString = hexString . PadRight ( ( ChunkSize * 3 ) - 1 ) ;
314+ hexString = hexString . PadRight ( ( chunkSizeInt * 3 ) - 1 ) ;
288315 rawString = rawString . Substring ( 0 , bytesRead ) ;
289- rawString = rawString . PadRight ( ChunkSize ) ;
316+ rawString = rawString . PadRight ( chunkSizeInt ) ;
290317 }
291318
292319 string outputString ;
@@ -303,10 +330,10 @@ private static bool ExecuteHexPrint(string fileName,
303330
304331 Console . WriteLine ( outputString ) ;
305332
306- startAt += ChunkSize ;
307- byteEnd += ChunkSize ;
308- fromZeroStart += ChunkSize ;
309- fromZeroEnd += ChunkSize ;
333+ startAt += chunkSizeInt ;
334+ byteEnd += chunkSizeInt ;
335+ fromZeroStart += chunkSizeInt ;
336+ fromZeroEnd += chunkSizeInt ;
310337 chunksRead ++ ;
311338 }
312339 }
@@ -315,17 +342,17 @@ private static bool ExecuteHexPrint(string fileName,
315342 Console . WriteLine ( "EXCEPTION: " + exception . Message ) ;
316343 return false ;
317344 }
318-
319- OutputHeader ( ref mainImage , zeroImage ) ;
345+ OutputHeader ( chunkSizeInt , ref mainImage , zeroImage ) ;
320346 return true ;
321347 }
322348
323349 /// <summary>
324350 /// Outputs the header for the HEX print
325351 /// </summary>
352+ /// <param name="chunkSize">Chunk size</param>
326353 /// <param name="mainImage" ref="true">Main byte range image</param>
327354 /// <param name="zeroImage">Zero count byte range image</param>
328- private static void OutputHeader ( ref string mainImage , string zeroImage )
355+ private static void OutputHeader ( int chunkSize , ref string mainImage , string zeroImage )
329356 {
330357 // Main heading minimum = 14
331358 if ( mainImage . Length < 5 ) {
@@ -343,7 +370,10 @@ private static void OutputHeader(ref string mainImage, string zeroImage)
343370 zeroHeading += " " ;
344371 }
345372
346- var dataLine = $ "* HEX DATA * | RAW DATA | { mainHeading } | { zeroHeading } ";
373+ var dataHeading = " HEX DATA " . PadRight ( ( chunkSize * 3 ) + 1 ) ;
374+ var rawHeading = " RAW DATA " . PadRight ( chunkSize + 2 ) ;
375+
376+ var dataLine = $ "*{ dataHeading } * |{ rawHeading } | { mainHeading } | { zeroHeading } ";
347377 var equalsLine = string . Empty ;
348378 while ( equalsLine . Length < dataLine . Length ) {
349379 equalsLine += "=" ;
0 commit comments