File: HexDump.pm

package info (click to toggle)
libtest-hexdifferences-perl 1.001-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 148 kB
  • sloc: perl: 862; makefile: 7
file content (452 lines) | stat: -rw-r--r-- 10,990 bytes parent folder | download
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
package Test::HexDifferences::HexDump;  ## no critic (TidyCode)

use strict;
use warnings;

our $VERSION = '0.008';

use Hash::Util qw(lock_keys);
use Sub::Exporter -setup => {
    exports => [
        qw(hex_dump),
    ],
    groups  => {
        default => [ qw(hex_dump) ],
    },
};

my $default_format = "%a : %4C : %d\n";

sub hex_dump {
    my ($data, $attr_ref) = @_;

    defined $data
        or return $data;
    ref $data
        and return $data;
    $attr_ref
        = ref $attr_ref eq 'HASH'
        ? $attr_ref
        : {};
    my $data_pool = {
        # global
        data               => $data,
        format             => $attr_ref->{format}  || "$default_format%*x",
        address            => $attr_ref->{address} || 0,
        output             => q{},
        # to format a block
        format_block       => undef,
        data_length        => undef,
        is_multibyte_error => undef,
    };
    lock_keys %{$data_pool};
    BLOCK:
    while ( length $data_pool->{data} ) {
        _next_format($data_pool);
        _format_items($data_pool);
    }

    return $data_pool->{output};
}

sub _next_format {
    my $data_pool = shift;

    my $is_match = $data_pool->{format} =~ s{
        \A
          ( .*? [^%] )               # format of the block
          % ( 0* [1-9] \d* | [*] ) x # repetition factor
    } {
        my $new_count = $2 eq q{*} ? q{*} : $2 - 1;
        $data_pool->{format_block} = $1;
        $new_count
        ? "$1\%${new_count}x"
        : q{};
    }xmse;
    if ( $data_pool->{is_multibyte_error} || ! $is_match ) {
        $data_pool->{format}             = "$default_format%*x";
        $data_pool->{format_block}       = $default_format;
        $data_pool->{is_multibyte_error} = 0;
        return;
    }

    return;
}

sub _format_items {
    my $data_pool = shift;

    $data_pool->{data_length} = 0;
    RUN: {
        # % written as %%
        $data_pool->{format_block} =~ s{
            \A % ( % )
        } {
            do {
                $data_pool->{output} .= $1;
                q{};
            }
        }xmse and redo RUN;
        # \n written as %\n will be ignored
        $data_pool->{format_block} =~ s{
            \A % [\n]
        }{}xms and redo RUN;
        # address
        _format_address($data_pool)
            and redo RUN;
        # words
        _format_word($data_pool)
            and redo RUN;
        # display ASCII
        _format_ascii($data_pool)
            and redo RUN;
        # display any other char
        $data_pool->{format_block} =~ s{
            \A (.)
        } {
            do {
                $data_pool->{output} .= $1;
                q{};
            }
        }xmse and redo RUN;
        if ( $data_pool->{data_length} ) {
            # clear already displayed data
            substr $data_pool->{data}, 0, $data_pool->{data_length}, q{};
            $data_pool->{data_length} = 0;
        }
    }

    return;
}

sub _format_address {
    my $data_pool = shift;

    return $data_pool->{format_block} =~ s{
        \A % ( 0* [48]? ) a
    } {
        do {
            my $length = $1 || 4;
            $data_pool->{output}
                .= sprintf "%0${length}X", $data_pool->{address};
            q{};
        }
    }xmse;
}

my $big_endian    = q{>};
my $little_endian = q{<};
my $machine_endian
    = ( pack 'S', 1 ) eq ( pack 'n', 1 )
    ? $big_endian # network order
    : $little_endian;
my %format_of = (
    'C'  => { # unsigned char
        bytes  => 1,
        endian => $big_endian,
    },
    'S'  => { # unsigned 16-bit, endian depends on machine
        bytes  => 2,
        endian => $machine_endian,
    },
    'S<' => { # unsigned 16-bit, little-endian
        bytes  => 2,
        endian => $little_endian,
    },
    'S>' => { # unsigned 16-bit, big-endian
        bytes  => 2,
        endian => $big_endian,
    },
    'v'  => { # unsigned 16-bit, little-endian
        bytes  => 2,
        endian => $little_endian,
    },
    'n'  => { # unsigned 16-bit, big-endian
        bytes  => 2,
        endian => $big_endian,
    },
    'L'  => { # unsigned 32-bit, endian depends on machine
        bytes  => 4,
        endian => $machine_endian,
    },
    'L<' => { # unsigned 32-bit, little-endian
        bytes  => 4,
        endian => $little_endian,
    },
    'L>' => { # unsigned 32-bit, big-endian
        bytes  => 4,
        endian => $big_endian,
    },
    'V'  => { # unsigned 32-bit, little-endian
        bytes  => 4,
        endian => $little_endian,
    },
    'N'  => { # unsigned 32-bit, big-endian
        bytes  => 4,
        endian => $big_endian,
    },
    'Q'  => { # unsigned 64-bit, endian depends on machine
        bytes  => 8,
        endian => $machine_endian,
    },
    'Q<' => { # unsigned 64-bit, little-endian
        bytes  => 8,
        endian => $little_endian,
    },
    'Q>' => { # unsigned 64-bit, big-endian
        bytes  => 8,
        endian => $big_endian,
    },
);

sub _format_word {
    my $data_pool = shift;

    return $data_pool->{format_block} =~ s{
        \A
        % ( 0* [1-9] \d* )?
        ( [LSQ] [<>] | [CVNvnLSQ] )
    } {
        do {
            my ($byte_length, $endian)
                = @{ $format_of{$2} }{ qw(bytes endian) };
            $data_pool->{output} .= join q{ }, map {
                (
                    length $data_pool->{data}
                    >= $data_pool->{data_length} + $byte_length
                )
                ? do {
                    my @unpacked
                        = unpack
                            q{C} x $byte_length,
                            substr
                                $data_pool->{data},
                                $data_pool->{data_length},
                                $byte_length;
                    if ( $endian eq q{<} ) {
                        @unpacked = reverse @unpacked;
                    }
                    my $hex = sprintf
                        '%02X' x $byte_length,
                        @unpacked;
                    $data_pool->{data_length} += $byte_length;
                    $data_pool->{address}     += $byte_length;
                    $hex;
                }
                : do {
                    if ( $byte_length > 1 ) {
                        $data_pool->{is_multibyte_error}++;
                    }
                    q{ } x 2 x $byte_length;
                };
            } 1 .. ( $1 || 1 );
            q{};
        }
    }xmse;
}

sub _format_ascii {
    my $data_pool = shift;

    return $data_pool->{format_block} =~ s{
        \A %d
    } {
        do {
            my $data = substr $data_pool->{data}, 0, $data_pool->{data_length};
            $data =~ s{
                ( ['"\\] )
                | ( [!-~] )
                | .
            } {
                defined $1   ? q{.}
                : defined $2 ? $2
                :              q{.}
            }xmsge;
            $data_pool->{output} .= $data;
            q{};
        }
    }xmse;
}

# $Id$

1;

__END__

=head1 NAME

Test::HexDifferences::HexDump - Format binary to hexadecimal strings

=head1 VERSION

0.008

=head1 SYNOPSIS

    use Test::HexDifferences::HexDump;

    $string = hex_dump(
        $binary,
    );

    $string = hex_dump(
        $binary,
        {
            address => $start_address,
            format  => "%a : %4C : %d\n",
        }
    );

=head2 Format elements

Every format element in the format string is starting with % like sprintf.

If the given format is shorter defined as needed for the data length
the remaining data are displayed in default format.
If the given format is longer defined as the data length
the output will filled with space and it stops before next repetition.

=head3 Data format

It is not very clever to use little-endian formats for tests.
There is a fallback to bytes if multibyte formats can not displayed.

 %C  - unsigned char
 %S  - unsigned 16-bit, endian depends on machine
 %S< - unsigned 16-bit, little-endian
 %S> - unsigned 16-bit, big-endian
 %v  - unsigned 16-bit, little-endian
 %n  - unsigned 16-bit, big-endian
 %L  - unsigned 32-bit, endian depends on machine
 %L< - unsigned 32-bit, little-endian
 %L> - unsigned 32-bit, big-endian
 %V  - unsigned 32-bit, little-endian
 %N  - unsigned 32-bit, big-endian
 %Q  - unsigned 64-bit, endian depends on machine
 %Q< - unsigned 64-bit, little-endian
 %Q> - unsigned 64-bit, big-endian

"pack" and "unpack" before Perl v5.10
do not allow "<" and ">" to mark the byte order.
This is allowed here for all Perl versions.

"pack" and "unpack" on a 32 bit machine
do not allow the "Q" formats.
This is allowed here for all machines.

=head3 Address format

 %a  - 16 bit address
 %4a - 16 bit address
 %8a - 32 bit address

=head3 ASCII format

It can not display all chars.
First it must be a printable ASCII char.
It can not be anything of space, q{.}, q{'}, q{"} or q{\}.
Otherwise q{.} will be printed.

 %d - display ASCII

=head3 Repetition

 %*x - repetition endless
 %1x - repetition 1 time
 %2x - repetition 2 times
 ...

=head3 Special formats

 %\n - ignore \n

=head2 Default format

The default format is:

 "%a : %4C : %d\n"

or fully written as

 "%a : %4C : %d\n%*x"

=head2 Complex formats

The %...x allows to write mixed formats e.g.

 Format:
  %a : %N %4C : %d\n%1x%
  %a : %n %2C : %d\n%*x
 Input:
    \0x01\0x23\0x45\0x67\0x89\0xAB\0xCD\0xEF
    \0x01\0x23\0x45\0x67
    \0x89\0xAB\0xCD\0xEF
 Output:
    0000 : 01234567 89 AB CD EF : .#-Eg...
    0008 : 0123 45 67 : .#-E
    000C : 89AB CD EF : g...

=head1 EXAMPLE

Inside of this Distribution is a directory named example.
Run this *.t files.

=head1 DESCRIPTION

This is a formatter for binary data.

=head1 SUBROUTINES/METHODS

=head2 subroutine hex_dump

    $string = hex_dump(
        $binary,
        {
            address => $display_start_address,
            format  => $format_string,
        }
    );

=head1 DIAGNOSTICS

nothing

=head1 CONFIGURATION AND ENVIRONMENT

nothing

=head1 DEPENDENCIES

L<Hash::Util|Hash::Util>

L<Sub::Exporter|Sub::Exporter>

=head1 INCOMPATIBILITIES

none

=head1 BUGS AND LIMITATIONS

none

=head1 SEE ALSO

L<Test::HexDifferences|Test::HexDifferences>

L<Data::Hexdumper|Data::HexDumper> inspired by

=head1 AUTHOR

Steffen Winkler

=head1 LICENSE AND COPYRIGHT

Copyright (c) 2012 - 2014,
Steffen Winkler
C<< <steffenw at cpan.org> >>.
All rights reserved.

This module is free software;
you can redistribute it and/or modify it
under the same terms as Perl itself.