File Coverage

blib/lib/Data/HexDump/Range.pm
Criterion Covered Total %
statement 38 40 95.0
branch n/a
condition n/a
subroutine 14 14 100.0
pod n/a
total 52 54 96.3


line stmt bran cond sub pod time code
1              
2             package Data::HexDump::Range ;
3              
4 1     1   55975 use strict;
  1         3  
  1         55  
5 1     1   8 use warnings ;
  1         1  
  1         30  
6 1     1   6 use Carp ;
  1         7  
  1         227  
7              
8             BEGIN
9             {
10              
11 1         10 use Sub::Exporter -setup =>
12             {
13             exports => [ qw() ],
14             groups =>
15             {
16             all => [ qw() ],
17             }
18 1     1   1200 };
  1         21694  
19            
20 1     1   359 use vars qw ($VERSION);
  1         2  
  1         44  
21 1     1   19 $VERSION = '0.13';
22             }
23              
24             #-------------------------------------------------------------------------------
25              
26 1     1   976 use English qw( -no_match_vars ) ;
  1         4590  
  1         7  
27              
28 1     1   1452 use Readonly ;
  1         2759  
  1         54  
29             Readonly my $EMPTY_STRING => q{} ;
30              
31 1     1   6 use Carp qw(carp croak confess) ;
  1         2  
  1         43  
32              
33 1     1   5 use List::Util qw(min max) ;
  1         2  
  1         143  
34 1     1   928 use List::MoreUtils qw(all) ;
  1         1212  
  1         79  
35 1     1   8 use Scalar::Util qw(looks_like_number) ;
  1         2  
  1         41  
36 1     1   1166 use Term::ANSIColor ;
  1         9374  
  1         90  
37 1     1   438 use Data::TreeDumper ;
  0            
  0            
38              
39             use Data::HexDump::Range::Object ;
40             use Data::HexDump::Range::Gather ;
41             use Data::HexDump::Range::Split ;
42             use Data::HexDump::Range::Format ;
43              
44             #-------------------------------------------------------------------------------
45              
46             =head1 NAME
47              
48             Data::HexDump::Range - Hexadecimal Range Dumper with color, bitfields and skip ranges
49              
50             =head1 SYNOPSIS
51              
52             my $hdr = Data::HexDump::Range->new() ;
53            
54             print $hdr->dump([['magic cookie', 12, 'red'],['image type', 2, 'green']], $data) ;
55             print $hdr->dump('magic cookie, 12, red :image type, 2, green', $data) ;
56            
57             $hdr->gather(['magic cookie', 12, 'red'], $data) ;
58             $hdr->gather(['image type', 2, 'green'], $other_data) ;
59            
60             print $hdr->dump_gathered() ;
61             $hdr->reset() ;
62              
63             =head1 DESCRIPTION
64              
65             Creates a dump from binary data and user defined I descriptions. The goal of this module is
66             to create an easy to understand dump of binary data.
67              
68             This achieved through:
69              
70             =over 2
71              
72             =item * Highlighted (colors) dump that is easier to understand than a monochrome blob of hex data
73              
74             =item * Multiple rendering modes with different output formats
75              
76             =item * Bitfield rendering
77              
78             =item * Skipping uninterresting data
79              
80             =item * The possibility to describe complex structures
81              
82             =back
83              
84             =head1 DOCUMENTATION
85              
86             The shortest perl dumper is C, courtesy of a golfing session
87             with Andrew Rodland aka I. I, I, I and other provided valuable insight particularely with the html output.
88              
89             B from libma L is nice tools that inspired me to write this module. This module offers many
90             more options but B may be a better alternative If you need very fast dump generation.
91              
92             B splits binary data according to user defined I and renderes them as a B or/and B data dump.
93             The data dump can be rendered in ANSI, ASCII or HTML.
94              
95             =head2 Rendered Columns
96              
97             You can choose which columns are rendered by setting options when creating a Data::HexDump::Range object.
98             The default rendering includes the following
99              
100             RANGE_NAME OFFSET CUMULATIVE_OFFSET HEX_DUMP ASCII_DUMP
101              
102             which corresponds to the object below:
103              
104             Data::HexDump::Range->new
105             (
106             FORMAT => 'ANSI',
107             COLOR => 'cycle',
108            
109             ORIENTATION => 'horizontal',
110            
111             DISPLAY_RANGE_NAME => 1 ,
112            
113             DISPLAY_OFFSET => 1 ,
114             OFFSET_FORMAT => 'hex',
115            
116             DISPLAY_HEX_DUMP => 1,
117             DISPLAY_ASCII_DUMP => 1,
118            
119             DATA_WIDTH => 16,
120             ) ;
121              
122             If you decided that you wanted the binary data to be showed in decimal instead for hexadecimal, you' set DISPLAY_HEX_DUMP => 0 and DISPLAY_DEC_DUMP => 1.
123             See L for all the possible options. Most option are also available from the command line utility I.
124              
125             =head2 Orientation
126              
127             The examples below show the output of the following command:
128              
129             $>hdr -r 'magic cookie,12:padding, 32:header,5:data, 20:extra data,#:header,5:data,40:footer,4' -col -o ver -display_ruler lib/Data/HexDump/Range.pm
130              
131             =head3 Vertical orientation
132              
133             In this orientation mode, each range displayed on a separate line.
134              
135             RANGE_NAME OFFSET CUMULATI HEX_DUMP ASCII_DUMP
136             0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789012345
137             magic cookie 00000000 00000000 0a 70 61 63 6b 61 67 65 20 44 61 74 .package Dat
138             padding 0000000c 00000000 61 3a 3a 48 a::H
139             padding 00000010 00000004 65 78 44 75 6d 70 3a 3a 52 61 6e 67 65 20 3b 0a exDump::Range ;.
140             padding 00000020 00000014 0a 75 73 65 20 73 74 72 69 63 74 3b .use strict;
141             header 0000002c 00000000 0a 75 73 65 .use
142             header 00000030 00000004 20
143             data 00000031 00000000 77 61 72 6e 69 6e 67 73 20 3b 0a 75 73 65 20 warnings ;.use
144             data 00000040 0000000f 43 61 72 70 20 Carp
145             "extra data"
146             header 00000045 00000000 3b 0a 0a 42 45 ;..BE
147             data 0000004a 00000000 47 49 4e 20 0a 7b GIN .{
148             data 00000050 00000006 0a 0a 75 73 65 20 53 75 62 3a 3a 45 78 70 6f 72 ..use Sub::Expor
149             data 00000060 00000016 74 65 72 20 2d 73 65 74 75 70 20 3d 3e 20 0a 09 ter -setup => ..
150             data 00000070 00000026 7b 0a {.
151             footer 00000072 00000000 09 65 78 70 .exp
152              
153             In colors:
154              
155             =begin html
156              
157            
 
158             RANGE_NAME OFFSET CUMULATI HEX_DUMP ASCII_DUMP
159             0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789012345
160             magic cookie 00000000 00000000 0a 70 61 63 6b 61 67 65 20 44 61 74 .package Dat
161             padding 0000000c 00000000 61 3a 3a 48 a::H
162             padding 00000010 00000004 65 78 44 75 6d 70 3a 3a 52 61 6e 67 65 20 3b 0a exDump::Range ;.
163             padding 00000020 00000014 0a 75 73 65 20 73 74 72 69 63 74 3b .use strict;
164             header 0000002c 00000000 0a 75 73 65 .use
165             header 00000030 00000004 20
166             data 00000031 00000000 77 61 72 6e 69 6e 67 73 20 3b 0a 75 73 65 20 warnings ;.use
167             data 00000040 0000000f 43 61 72 70 20 Carp
168             "extra data"
169             header 00000045 00000000 3b 0a 0a 42 45 ;..BE
170             data 0000004a 00000000 47 49 4e 20 0a 7b GIN .{
171             data 00000050 00000006 0a 0a 75 73 65 20 53 75 62 3a 3a 45 78 70 6f 72 ..use Sub::Expor
172             data 00000060 00000016 74 65 72 20 2d 73 65 74 75 70 20 3d 3e 20 0a 09 ter -setup => ..
173             data 00000070 00000026 7b 0a {.
174             footer 00000072 00000000 09 65 78 70 .exp
175            
176            
177              
178             =end html
179              
180             =head3 Horizontal orientation
181              
182             In this mode, the data are packed together in the dump
183              
184             OFFSET HEX_DUMP ASCII_DUMP RANGE_NAME
185             0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789012345
186             00000000 0a 70 61 63 6b 61 67 65 20 44 61 74 61 3a 3a 48 .package Data::H magic cookie, padding,
187             00000020 65 78 44 75 6d 70 3a 3a 52 61 6e 67 65 20 3b 0a exDump::Range ;. padding,
188             00000030 0a 75 73 65 20 73 74 72 69 63 74 3b 0a 75 73 65 .use strict;.use padding, header,
189             00000050 20 77 61 72 6e 69 6e 67 73 20 3b 0a 75 73 65 20 warnings ;.use header, data,
190             00000070 43 61 72 70 20 3b 0a 0a 42 45 47 49 4e 20 0a 7b Carp ;..BEGIN .{ data, "extra data", header, data,
191             000000a0 0a 0a 75 73 65 20 53 75 62 3a 3a 45 78 70 6f 72 ..use Sub::Expor data,
192             000000b0 74 65 72 20 2d 73 65 74 75 70 20 3d 3e 20 0a 09 ter -setup => .. data,
193             000000c0 7b 0a 09 65 78 70 {..exp data, footer,
194              
195             In colors:
196              
197             =begin html
198              
199            
 
200              
201             OFFSET HEX_DUMP ASCII_DUMP RANGE_NAME
202             0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789012345
203             00000000 0a 70 61 63 6b 61 67 65 20 44 61 74 61 3a 3a 48 .package Data::H magic cookie, padding,
204             00000020 65 78 44 75 6d 70 3a 3a 52 61 6e 67 65 20 3b 0a exDump::Range ;. padding,
205             00000030 0a 75 73 65 20 73 74 72 69 63 74 3b 0a 75 73 65 .use strict;.use padding, header,
206             00000050 20 77 61 72 6e 69 6e 67 73 20 3b 0a 75 73 65 20 warnings ;.use header, data,
207             00000070 43 61 72 70 20 3b 0a 0a 42 45 47 49 4e 20 0a 7b Carp ;..BEGIN .{ data, "extra data", header, data,
208             000000a0 0a 0a 75 73 65 20 53 75 62 3a 3a 45 78 70 6f 72 ..use Sub::Expor data,
209             000000b0 74 65 72 20 2d 73 65 74 75 70 20 3d 3e 20 0a 09 ter -setup => .. data,
210             000000c0 7b 0a 09 65 78 70 {..exp data, footer,
211              
212            
213              
214             =end html
215              
216             =head2 Range definition
217              
218             my $simple_range = ['magic cookie', 12, 'red'] ;
219            
220             Ranges are Array references containing two to four elements:
221              
222             =over 2
223              
224             =item * name - a string
225              
226             =item * size - an integer or a format - a string
227              
228             =item * color - a string or undef
229              
230             =item * user information - a very short string descibing the range or undef
231              
232             =back
233              
234             Any of the elements can be replaced by a subroutine reference. See L below.
235              
236             You can also pass the ranges as a string. The L command line range dumper that was installed by this module uses the string format.
237              
238             Example:
239              
240             $>hdr --col -display_ruler -o ver -r 'header,12:name,10:magic,2:offset,4:BITMAP,4,bright_yellow:ff,x2b2:fx,b32:f0,b16:field,x8b8:field2, b17:footer,20' my_binary
241              
242             =head3 Size field format
243              
244             The size field is used to defined if the range is a normal range, a comment, a bitfield or a skip range. The formats are a s follows:
245              
246             format range example
247            
248             normal range => integer header, 4, bright_blue
249             comment => # data section start, #
250             extra header => @ header, @, red
251             bitfield => [XInteger][xInteger]bInteger bitfield, X2x4b4 # X is byte offset, x is bit offset
252             skip range => xInteger boring, X256,, comment
253              
254             Note that the integer part can be a hexadecimal value starting with I<0x>
255              
256             =head3 Coloring
257              
258             Ranges and ranges names are displayed according to the color field in the range definition.
259              
260             The color definition is one of:
261              
262             =over 2
263              
264             =item * A user defined color name found in B (see L)
265              
266             =item * An ansi color definition - 'blue on_yellow'
267              
268             =item * undef - will be repaced by a white color or picked from a cyclic color list (see B in L).
269              
270             =back
271              
272             =head3 Linear ranges
273              
274             For simple data formats, your can put all the your range descriptions in a array:
275              
276             my $image_ranges =
277             [
278             ['magic cookie', 12, 'red'],
279             ['size', 10, 'yellow'],
280             ['data', 10, 'blue on_yellow'],
281             ['timestamp', 5, 'green'],
282             ] ;
283              
284             =head3 Structured Ranges
285              
286             my $data_range = # definition to re-use
287             [
288             ['data header', 5, 'blue on_yellow'],
289             ['data', 100, 'blue'],
290             ] ;
291            
292             my $structured_range =
293             [
294             [
295             ['magic cookie', 12, 'red'],
296             ['padding', 88, 'yellow'],
297             $data_range,
298             ],
299            
300             [
301             ['extra data', 12, undef],
302             [
303             $data_range,
304             ['footer', 4, 'yellow on_red'],
305             ]
306             ],
307             ]
308              
309             =head4 Comment ranges
310              
311             If the size of a range is the string '#', the whole range is considered a comment
312              
313             my $range_defintion_with_comments =
314             [
315             ['comment text', '#', 'optional color for meta range'],
316             ['magic cookie', 12, 'red'],
317             ['padding', 88, 'yellow'],
318            
319             [
320             ['another comment', '#'],
321             ['data header', 5, 'blue on_yellow'],
322             ['data', 100, 'blue'],
323             ],
324             ] ;
325              
326             =head4 Extra header
327              
328             If the size of a range is the string '@', and extra header is inserted in the output. This is useful when
329             you have very long output and want an extra header.
330              
331             =head3 Bitfields
332              
333             Bitfields can be up to 32 bits long and can overlap each other. Bitfields are applied on the previously defined range.
334              
335             In the example below, bitfields I are extracted form the data defined by the I range.
336              
337             .------------. .--------------.
338             .---. | data range | | data hexdump |
339             | b | '------------' '--------------'
340             | i | | |
341             | t | BITMAP <----' 00000000 00000000 0a 70 61 63 <--' .pac
342             | f | ^ .ff 02 .. 03 -- -- -- 00 ----------------------------00-- .bitfield: ---.
343             | i |---> .fx 00 .. 31 0a 70 61 63 00001010011100000110000101100011 .bitfield: .pac
344             | e | v .f0 00 .. 15 -- -- 61 63 ----------------0110000101100011 .bitfield: --ac
345             | l | ^ ^ ^ ^
346             | d | | | | |
347             | s | .----------------------.-------------------.----------------------. .---------------------.
348             '---' | start bit .. end bit | bitfields hexdump | bitfield binary dump | | bitfield ascci dump |
349             '----------------------'-------------------'----------------------' '---------------------'
350              
351             The the format definiton is: an optional "x (for offset) + offset" + "b (for bits) + number of bits". Eg: I second byte in MYDATA.
352              
353             An example output containing normal data and bifields dumps using the comand below.
354              
355             $>hdr -r 'header,12:BITMAP,4,bright_yellow:ff,x2b2:fx,b32:f0,b16:footer,16' -o ver file_name
356              
357             =begin html
358              
359            
 
360             header 00000000 00000000 44 61 74 61 3a 3a 48 65 78 44 75 6d Data::HexDum
361             BITMAP 0000000c 00000000 70 3a 3a 52 p::R
362             .ff 02 .. 03 -- -- -- 00 ----------------------------00-- .bitfield: ---.
363             .fx 00 .. 31 70 3a 3a 52 01110000001110100011101001010010 .bitfield: p::R
364             .f0 00 .. 15 -- -- 3a 52 ----------------0011101001010010 .bitfield: --:R
365             footer 00000010 00000000 61 6e 67 65 0a 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d ange.===========
366            
367            
368              
369             =end html
370              
371             By default bitfields are not displayed in horizontal mode.
372              
373             =head3 Skip ranges
374              
375             If the size format is 'X' + number, that number of bytes is skipped from the data. B
376             will display the skip range in the dump but not the data.
377              
378             In the command below, the range 'skip' removes 32 bytes from the display. '>>' is prepended to the range name.
379              
380             Command: I
381              
382             RANGE_NAME OFFSET CUMULATI HEX_DUMP ASCII_DUMP
383             0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789012345
384             magic cookie 00000000 00000000 44 61 74 61 3a Data:
385             other 00000005 00000000 3a 48 65 78 44 75 6d 70 3a 3a 52 :HexDump::R
386             other 00000010 0000000b 61 6e 67 65 0a 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d ange.===========
387             other 00000020 0000001b 3d 3d 3d 3d 3d 3d 3d 3d 3d 0a =========.
388             .bf 00 .. 07 -- -- -- 0a ------------------------00001010 .bitfield: ---.
389             >>skip 0000002a 00000049 00 00 00 20 bytes skipped
390             more 0000004a 00000000 20 63 6f 6c 6f 72 color
391             more 00000050 00000006 2c 20 62 69 74 66 69 65 6c 64 73 20 61 6e , bitfields an
392              
393             in color:
394              
395             =begin html
396              
397            
 
398             RANGE_NAME OFFSET CUMULATI HEX_DUMP ASCII_DUMP
399             0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789012345
400             magic cookie 00000000 00000000 44 61 74 61 3a Data:
401             other 00000005 00000000 3a 48 65 78 44 75 6d 70 3a 3a 52 :HexDump::R
402             other 00000010 0000000b 61 6e 67 65 0a 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d ange.===========
403             other 00000020 0000001b 3d 3d 3d 3d 3d 3d 3d 3d 3d 0a =========.
404             .bf 00 .. 07 -- -- -- 0a ------------------------00001010 .bitfield: ---.
405             >>skip 0000002a 00000049 00 00 00 20 bytes skipped
406             more 0000004a 00000000 20 63 6f 6c 6f 72 color
407             more 00000050 00000006 2c 20 62 69 74 66 69 65 6c 64 73 20 61 6e , bitfields an
408            
409            
410              
411             =end html
412              
413             =head3 Dynamic range definition
414              
415             The whole range can be replaced by a subroutine reference or elements of the range can be replaced by
416             a subroutine definition.
417              
418             my $dynamic_range =
419             [
420             [\&name, \&size, \&color, \&comment ],
421             [\&define_range] # returns a range definition
422             ] ;
423              
424             =head4 'name' sub ref
425              
426             sub cloth_size
427             {
428             my ($self, $data, $used_data, $size, $range) = @_ ;
429             my %types = (O => 'S', 1 => 'M', 2 => 'L',) ;
430             return 'size:' . ($types{$data} // '?') ;
431             }
432            
433             $hdr->dump([\&cloth_size, 1, 'yellow'], $data) ;
434              
435             =head4 'size' sub ref
436              
437             sub cloth_size
438             {
439             my ($self, $data, $used_data, $size, $range) = @_ ;
440             return unpack "a", $data ;
441             }
442            
443             $hdr->dump(['data', \&get_size, 'yellow'], $data) ;
444              
445             =head4 'color' sub ref
446              
447             my $flip_flop = 1 ;
448             my @colors = ('green', 'red') ;
449            
450             sub alternate_color {$flip_flop ^= 1 ; return $colors[$flip_flop] }
451            
452             $hdr->dump(['data', 100, \&alternate_color], $data) ;
453              
454             =head4 'range' sub ref
455              
456             sub define_range(['whole range', 5, 'on_yellow']}
457            
458             $hdr->dump([\&define_range], $data) ;
459              
460              
461             =head2 define_range($data, $offset)
462              
463             Returns a range description for the next range to dump
464              
465             I - See L
466              
467             =over 2
468              
469             =item * $self - A Data::HexDump::Range object
470              
471             =item * $data - Binary string - the data passed to the I method
472              
473             =item * $offset - Integer - current offset in $data
474              
475             =back
476              
477             I -
478              
479             =over 2
480              
481             =item * $range - An array reference containing a name, size and color and user_information
482              
483             OR
484              
485             =item * undef - Ignore this range
486              
487             =item * $comment - String - an optional comment that will be displayed if DUMP_RANGE_DESCRIPTION is set.
488              
489             =back
490              
491             =head4
492              
493             B this is, very, different from L below.
494            
495             =head3 User defined range generator
496              
497             A subroutine reference can be passed as a range definition. The subroutine will be called repetitively
498             till the data is exhausted or the subroutine returns I.
499              
500             sub my_parser
501             {
502             my ($data, $offset) = @_ ;
503            
504             my $first_byte = unpack ("x$offset C", $data) ;
505            
506             $offset < length($data)
507             ? $first_byte == ord(0)
508             ? ['from odd', 5, 'blue on_yellow']
509             : ['from even', 3, 'green']
510             : undef ;
511             }
512            
513             my $hdr = Data::HexDump::Range->new() ;
514             print $hdr->dump(\&my_parser, '01' x 50) ;
515              
516             =head2 my_parser($data, $offset)
517              
518             Returns a range description for the next range to dump
519              
520             I - See L
521              
522             =over 2
523              
524             =item * $self - A Data::HexDump::Range object
525              
526             =item * $data - Binary string - the data passed to the I method
527              
528             =item * $offset - Integer - current offset in $data
529              
530             =back
531              
532             I -
533              
534             =over 2
535              
536             =item * $range - An array reference containing a name, size and color
537              
538             OR
539              
540             =item * undef - Done parsing
541              
542             =back
543              
544             =cut
545              
546             =head1 EXAMPLES
547              
548             See L in the distribution.
549              
550             =head1 SUBROUTINES/METHODS
551              
552             =cut
553              
554             #-------------------------------------------------------------------------------
555              
556             sub new
557             {
558              
559             =head2 new(NAMED_ARGUMENTS)
560              
561             Create a Data::HexDump::Range object.
562              
563             my $hdr = Data::HexDump::Range->new() ; # use default setup
564            
565             my $hdr = Data::HexDump::Range->new
566             (
567             FORMAT => 'ANSI'|'ASCII'|'HTML',
568             COLOR => 'bw' | 'cycle',
569             OFFSET_FORMAT => 'hex' | 'dec',
570             DATA_WIDTH => 16 | 20 | ... ,
571             DISPLAY_RANGE_NAME => 1 ,
572             MAXIMUM_RANGE_NAME_SIZE => 16,
573             DISPLAY_COLUMN_NAMES => 0,
574             DISPLAY_RULER => 0,
575             DISPLAY_OFFSET => 1 ,
576             DISPLAY_CUMULATIVE_OFFSET => 1 ,
577             DISPLAY_ZERO_SIZE_RANGE_WARNING => 1,
578             DISPLAY_ZERO_SIZE_RANGE => 1,
579             DISPLAY_RANGE_SIZE => 1,
580             DISPLAY_ASCII_DUMP => 1 ,
581             DISPLAY_HEX_DUMP => 1,
582             DISPLAY_DEC_DUMP => 1,
583             COLOR_NAMES => {},
584             ORIENTATION => 'horizontal',
585             ) ;
586              
587             I - All arguments are optional. Default values are listed below.
588              
589             =over 2
590              
591             =item * NAME - String - Name of the Data::HexDump::Range object, set to 'Anonymous' by default
592              
593             =item * INTERACTION - Hash reference - Set of subs that are used to display information to the user
594              
595             Useful if you use Data::HexDump::Range in an application without terminal.
596              
597             =item * VERBOSE - Boolean - Display information about the creation of the object. Default is I
598              
599             =item * DUMP_ORIGINAL_RANGE_DESCRIPTION - Boolean - Diplays the un-processed range descritption.
600              
601             With B, this fields can be used to peek into what range descriptions the module get and how they are
602             transformed into the format that is internally used by the module. These are for debugging purpose and you should normally not need to used them.
603              
604             Original range description
605             |- 0 = 'Data'
606             |- 1 = '128'
607             |- 2 = undef
608             `- 3 = undef
609            
610             Original range description
611             |- 0 = CODE(0x1dc5230)
612             |- 1 = undef
613             |- 2 = undef
614             `- 3 = undef
615              
616             =item * DUMP_RANGE_DESCRIPTION - Boolean - Diplays the processed range descritption in plain text before the dump
617              
618             128->26:Data
619             |- COLOR = undef
620             |- DATA = '_blah_blah_blah_blah_blah[\n]'
621             |- IS_BITFIELD = '0'
622             |- IS_COMMENT = '0'
623             |- IS_SKIP = '0'
624             |- NAME = '128->26:Data'
625             |- OFFSET = '20'
626             |- unpack format = 'x20 a26'
627             `- USER_INFORMATION = undef
628              
629             =item * FORMAT - String - format of the dump string generated by Data::HexDump::Range.
630              
631             Default is B which allows for colors. Other formats are 'ASCII' and 'HTML'.
632              
633             =item * COLOR - String 'bw' or 'cycle'.
634              
635             Ranges for which no color has been defined, in 'ANSI' or 'HTML' format mode, will be rendered in
636             black and white or with a color picked from a cyclic color list. Default is 'bw'.
637              
638             =item * OFFSET_FORMAT - String - 'hex' or 'dec'
639              
640             If set to 'hex', the offset will be displayed in base 16. When set to 'dec' the offset is displayed
641             in base 10. Default is 'hex'.
642              
643             =item * DATA_WIDTH - Integer - Number of elements displayed per line. Default is 16.
644              
645             =item * DISPLAY_RANGE_NAME - Boolean - If set, range names are displayed in the dump.
646              
647             =item * MAXIMUM_RANGE_NAME_SIZE - Integer - maximum size of a range name (horizontal mode). Default size is 16.
648              
649             =item * DISPLAY_COLUMN_NAMES - Boolean - If set, the column names are displayed. Default I
650              
651             =item * DISPLAY_RULER - Boolean - if set, a ruler is displayed above the dump, Default is I
652              
653             =item * DISPLAY_OFFSET - Boolean - If set, the offset column is displayed. Default I
654              
655             =item * DISPLAY_CUMULATIVE_OFFSET - Boolean - If set, the cumulative offset column is displayed in 'vertical' rendering mode. Default is I
656              
657             =item * OFFSET_START - Integer - value added to the offset.
658              
659             =item * DISPLAY_ZERO_SIZE_RANGE - Boolean - if set, ranges that do not consume data are displayed. default is I
660              
661             =item * DISPLAY_ZERO_SIZE_RANGE_WARNING - Boolean - if set, a warning is emitted if ranges that do not consume data. Default is I
662              
663             =item * DISPLAY_COMMENT_RANGE - Boolean - if set, comment ranges are displayed. default is I
664              
665             =item * DISPLAY_RANGE_SIZE - Bolean - if set the range size is prepended to the name. Default I
666              
667             =item * DISPLAY_ASCII_DUMP - Boolean - If set, the ASCII representation of the binary data is displayed. Default is I
668              
669             =item * DISPLAY_HEX_DUMP - Boolean - If set, the hexadecimal dump column is displayed. Default is I
670              
671             =item * DISPLAY_DEC_DUMP - Boolean - If set, the decimall dump column is displayed. Default is I
672              
673             =item * DISPLAY_BITFIELD_SOURCE - Boolean - if set an extra column indicating the source of bitfields is displayed
674              
675             =item * MAXIMUM_BITFIELD_SOURCE_SIZE - Integer - maximum size of the bifield source column
676              
677             =item * DISPLAY_USER_INFORMATION - Boolean - if set an extra column displaying user supplied information is shown
678              
679             =item * MAXIMUM_USER_INFORMATION_SIZE - Integer - maximum size of theuser information column
680              
681             =item * DISPLAY_BITFIELDS - Boolean - if set the bitfields are displayed
682              
683             =item * BIT_ZERO_ON_LEFT - Boolean - if set the bit of index 0 is on left growing to the right. Default I
684              
685             =item * COLOR_NAMES - A hash reference
686              
687             {
688             ANSI =>
689             {
690             header => 'yellow on_blue',
691             data => 'yellow on_black',
692             },
693            
694             HTML =>
695             {
696             header => 'FFFF00 0000FF',
697             data => 'FFFF00 000000',
698             },
699             }
700              
701             =item * ORIENTATION - String - 'vertical' or 'horizontal' (the default).
702              
703             =back
704              
705             I - Nothing
706              
707             I - Dies if an unsupported option is passed.
708              
709             =cut
710              
711             my ($invocant, @setup_data) = @_ ;
712              
713             my $class = ref($invocant) || $invocant ;
714             confess 'Error: Invalid constructor call' unless defined $class ;
715              
716             my $object = {} ;
717              
718             my ($package, $file_name, $line) = caller() ;
719             bless $object, $class ;
720              
721             $object->Setup($package, $file_name, $line, @setup_data) ;
722              
723             return($object) ;
724             }
725              
726             #-------------------------------------------------------------------------------
727              
728             sub gather
729             {
730              
731             =head2 gather($range_description, $data, $offset, $size)
732              
733             Dump the data, up to $size, according to the description. The data dump is kept in the object so you can
734             merge multiple gathered dumps and get a single rendering.
735              
736             $hdr->gather($range_description, $data, $offset, $size)
737             $hdr->gather($range_description, $more_data)
738            
739             print $hdr->dump_gathered() ;
740              
741             I
742              
743             =over 2
744              
745             =item * $range_description - See L
746            
747             =item * $data - A string - binary data to dump
748              
749             =item * $offset - dump data from offset
750              
751             =over 2
752              
753             =item * undef - start from first byte
754              
755             =back
756              
757             =item * $size - amount of data to dump
758              
759             =over 2
760              
761             =item * undef - use range description
762              
763             =item * CONSUME_ALL_DATA - apply range descritption till all data is consumed
764              
765             =back
766              
767             =back
768              
769             I - An integer - the number of processed bytes
770              
771             I - See L<_gather>
772              
773             =cut
774              
775             my ($self, $range, $data, $offset, $size) = @_ ;
776              
777             my ($gathered_data, $used_data) = $self->_gather($self->{GATHERED}, $range, $data, $offset, $size) ;
778              
779             return $used_data ;
780             }
781              
782             #-------------------------------------------------------------------------------
783              
784             sub dump_gathered
785             {
786              
787             =head2 dump_gathered()
788              
789             Returns the dump string for the gathered data.
790              
791             $hdr->gather($range_description, $data, $size)
792             $hdr->gather($range_description, $data, $size)
793            
794             print $hdr->dump_gathered() ;
795              
796             I - None
797              
798             I - A string - the binary data formated according to the rnage descriptions
799              
800             I - None
801              
802             =cut
803              
804             my ($self) = @_ ;
805              
806             my $split_data = $self->split($self->{GATHERED}) ;
807              
808             $self->add_information($split_data) ;
809              
810             return $self->format($split_data) ;
811             }
812              
813             #-------------------------------------------------------------------------------
814              
815             sub dump ## no critic (Subroutines::ProhibitBuiltinHomonyms)
816             {
817              
818             =head2 dump($range_description, $data, $offset, $size)
819              
820             Dump the data, up to $size, according to the description
821              
822             I - See L
823              
824             I - A string - the formated dump
825              
826             I - dies if the range description is invalid
827              
828             =cut
829              
830             my ($self, $range_description, $data, $offset, $size) = @_ ;
831              
832             return unless defined wantarray ;
833              
834             local $self->{GATHERED} = [] ;
835              
836             my ($gathered_data, $used_data) = $self->_gather($self->{GATHERED}, $range_description, $data, $offset, $size) ;
837              
838             my $split_data = $self->split($gathered_data) ;
839              
840             $self->add_information($split_data) ;
841              
842             return $self->format($split_data) ;
843             }
844              
845             #-------------------------------------------------------------------------------
846              
847             sub get_dump_and_consumed_data_size
848             {
849              
850             =head2 get_dump_and_consumed_data_size($range_description, $data, $offset, $size)
851              
852             Dump the data, from $offset up to $size, according to the $range_description
853              
854             I - See L
855              
856             I -
857              
858             =over 2
859              
860             =item * A string - the formated dump
861              
862             =item * An integer - the number of bytes consumed by the range specification
863              
864             =back
865              
866             I - dies if the range description is invalid
867              
868             =cut
869              
870             my ($self,$data, $offset, $size) = @_ ;
871              
872             return unless defined wantarray ;
873              
874             my ($gathered_data, $used_data) = $self->_gather(undef, $data, $offset, $size) ;
875              
876             my $dump =$self->format($self->split($gathered_data)) ;
877              
878             return $dump, $used_data ;
879             }
880              
881             #-------------------------------------------------------------------------------
882              
883             sub reset ## no critic (Subroutines::ProhibitBuiltinHomonyms)
884             {
885              
886             =head2 reset()
887              
888             Clear the gathered dump
889              
890             I - None
891              
892             I - Nothing
893              
894             I - None
895              
896             =cut
897              
898             my ($self) = @_ ;
899              
900             $self->{GATHERED} = [] ;
901              
902             return ;
903             }
904              
905             #-------------------------------------------------------------------------------
906              
907             1 ;
908              
909             =head1 BUGS AND LIMITATIONS
910              
911             None so far.
912              
913             =head1 AUTHOR
914              
915             Nadim ibn hamouda el Khemir
916             CPAN ID: NKH
917             mailto: nadim@cpan.org
918              
919             =head1 COPYRIGHT AND LICENSE
920              
921             Copyright Nadim Khemir 2010.
922              
923             This program is free software; you can redistribute it and/or
924             modify it under the terms of either:
925              
926             =over 4
927              
928             =item * the GNU General Public License as published by the Free
929             Software Foundation; either version 1, or (at your option) any
930             later version, or
931              
932             =item * the Artistic License version 2.0.
933              
934             =back
935              
936             =head1 SUPPORT
937              
938             You can find documentation for this module with the perldoc command.
939              
940             perldoc Data::HexDump::Range
941              
942             You can also look for information at:
943              
944             =over 4
945              
946             =item * AnnoCPAN: Annotated CPAN documentation
947              
948             L
949              
950             =item * RT: CPAN's request tracker
951              
952             Please report any bugs or feature requests to L .
953              
954             We will be notified, and then you'll automatically be notified of progress on
955             your bug as we make changes.
956              
957             =item * Search CPAN
958              
959             L
960              
961             =back
962              
963             =head1 SEE ALSO
964              
965             L, L, L, L
966              
967             =cut