| 1 | package Parse::Binary::FixedFormat; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use bytes; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use integer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 0 | my ($class, $layout) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 10 | 0 | my $self; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 0 | ▶ | if (UNIVERSAL::isa($layout, 'HASH')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | require Parse::Binary::FixedFormat::Variants; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | $self = Parse::Binary::FixedFormat::Variants->new($layout); | |||||||||||||||||||||||||||||||||||||||||
| 14 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | $self = { Names=>[], Count=>[], Format=>"" }; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | ▶ | $self->parse_fields($layout) if $layout; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub parse_fields { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_fields: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | my ($self,$fmt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | foreach my $fld (@$fmt) { | |||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | my ($name, $format, $count, $group) = split /\s*:\s*/,$fld; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | push @{$self->{Names}}, $name; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | push @{$self->{Count}}, $count; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | push @{$self->{Group}}, $group; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | if (defined $count) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | push @{$self->{Format}||=[]}, "($format)$count"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"Format"} ||= []
MC/DC0%0/1
$self->{"Format"} ||= []
$self->{"Format"}
Truth table0%0/2
A
A= $self->{"Format"}
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | push @{$self->{Format}||=[]}, $format; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"Format"} ||= []
MC/DC0%0/1
$self->{"Format"} ||= []
$self->{"Format"}
Truth table0%0/2
A
A= $self->{"Format"}
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | my %_format_cache; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub _format { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_format: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my ($self, $lazy) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $self->{_format} ||= do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"_format"} ||= do {
my $format = join("", @{$self->{"Format"};});
$_format_cache{$format} ||= do {
$format =~ s/\((.*?)\)\*$/a*/ if $lazy;
$format =~ s/\((.*?)\)(?:(\d+)|(\*))/$1 x ($3 ? 1 : $2);/eg if $] < "5.008";
$format
}
}
MC/DC0%0/2
$self->{"_format"} ||= do {
my $format = join("", @{$self->{"Format"};});
$_format_cache{$format} ||= do {
$format =~ s/\((.*?)\)\*$/a*/ if $lazy;
$format =~ s/\((.*?)\)(?:(\d+)|(\*))/$1 x ($3 ? 1 : $2);/eg if $] < "5.008";
$format
}
}
$self->{"_format"}
do {
my $format = join("", @{$self->{"Format"};});
$_format_cache{$format} ||= do {
$format =~ s/\((.*?)\)\*$/a*/ if $lazy;
$format =~ s/\((.*?)\)(?:(\d+)|(\*))/$1 x ($3 ? 1 : $2);/eg if $] < "5.008";
$format
}
}
Truth table0%0/3
A ||= B
A= $self->{"_format"}B= do {
my $format = join("", @{$self->{"Format"};});
$_format_cache{$format} ||= do {
$format =~ s/\((.*?)\)\*$/a*/ if $lazy;
$format =~ s/\((.*?)\)(?:(\d+)|(\*))/$1 x ($3 ? 1 : $2);/eg if $] < "5.008";
$format
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $format = join('', @{$self->{Format}}); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | $_format_cache{$format} ||= do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$_format_cache{$format} ||= do {
$format =~ s/\((.*?)\)\*$/a*/ if $lazy;
$format =~ s/\((.*?)\)(?:(\d+)|(\*))/$1 x ($3 ? 1 : $2);/eg if $] < "5.008";
$format
}
MC/DC0%0/2
$_format_cache{$format} ||= do {
$format =~ s/\((.*?)\)\*$/a*/ if $lazy;
$format =~ s/\((.*?)\)(?:(\d+)|(\*))/$1 x ($3 ? 1 : $2);/eg if $] < "5.008";
$format
}
$_format_cache{$format}
do {
$format =~ s/\((.*?)\)\*$/a*/ if $lazy;
$format =~ s/\((.*?)\)(?:(\d+)|(\*))/$1 x ($3 ? 1 : $2);/eg if $] < "5.008";
$format
}
Truth table0%0/3
A ||= B
A= $_format_cache{$format}B= do {
$format =~ s/\((.*?)\)\*$/a*/ if $lazy;
$format =~ s/\((.*?)\)(?:(\d+)|(\*))/$1 x ($3 ? 1 : $2);/eg if $] < "5.008";
$format
}
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $format =~ s/\((.*?)\)\*$/a*/ if $lazy; # tail iteration | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $format =~ s/\((.*?)\)(?:(\d+)|(\*))/$1 x ($3 ? 1 : $2)/eg if ($] < 5.008); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $format; | |||||||||||||||||||||||||||||||||||||||||
| 47 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | my %_parent_format; | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub unformat { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
unformat: not called CC=19 SCAR=59.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | my @flds = shift; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $lazy = shift; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $parent = shift; | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my $format = $self->_format($lazy); | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | @flds = unpack($format, $flds[0]) unless $format eq 'a*'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $rec = {}; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | foreach my $i (0 .. $#{$self->{Names}}) { | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $name = $self->{Names}[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if (defined(my $count = $self->{Count}[$i])) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | next unless $count; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | my $group = $self->{Group}[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ($count eq '*') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $count = @flds; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $group ||= 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$group ||= 1
MC/DC0%0/1
$group ||= 1
$group
Truth table0%0/2
A
A= $group
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ($group) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $pad = 0; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | $pad = length($1) if $self->{Format}[$i] =~ /(X+)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ($lazy and $i == $#{$self->{Names}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$lazy and $i == $#{$$self{"Names"};}
MC/DC0%0/2
$lazy and $i == $#{$$self{"Names"};}
$lazy
$i == $#{$$self{"Names"};}
Truth table0%0/3
A and B
A= $lazyB= $i == $#{$$self{"Names"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $format = $self->{Format}[$i] or die "No format found"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $format =~ s/^\((.*?)\)\*$/$1/ or die "Not a count=* field"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $record = ($rec->{$name} ||= []); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$rec->{$name} ||= []
MC/DC0%0/1
$rec->{$name} ||= []
$rec->{$name}
Truth table0%0/2
A
A= $rec->{$name}
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | push @$record, $self->lazy_unformat( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@flds and length $flds[0]
MC/DC0%0/2
@flds and length $flds[0]
@flds
length $flds[0]
Truth table0%0/3
A and B
A= @fldsB= length $flds[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | $parent, $record, $pad, $format, \($flds[0]) | ||||||||||||||||||||||||||||||||||||||||||
| 84 | ) if @flds and length($flds[0]); | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my $count_idx = 0; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | while (my @content = splice(@flds, 0, $group)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | substr($content[-1], -$pad, $pad, '') if $pad; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | push @{$rec->{$name}}, \@content; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $count_idx += $group; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | last if $count_idx >= $count; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | @{$rec->{$name}} = splice @flds, 0, $count; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | $rec->{$name} = shift @flds; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | return $rec; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | sub lazy_unformat { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lazy_unformat: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my ($self, $parent, $record, $pad, $format, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | # for each request of a member data, we: | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | my $valid_sub = ($parent->can('valid_unformat') ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | return sub { { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | # grab one chunk of data | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | my @content = unpack($format, $$data); | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $length = length(pack($format, @content)); | |||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | # eliminate it from the source string | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | my $chunk = substr($$data, 0, $length, ''); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $done = (length($$data) <= $pad); | |||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ($valid_sub and !$done and !$_[0]->valid_unformat(\@content, \$chunk, $done)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$valid_sub and not $done and not $_[0]->valid_unformat(\@content, \$chunk, $done)
$valid_sub and not $done
MC/DC0%0/3
$valid_sub and not $done and not $_[0]->valid_unformat(\@content, \$chunk, $done)
$valid_sub
not $done
not $_[0]->valid_unformat(\@content, \$chunk, $done)
Truth table0%0/4
A and not(B) and not(C)
A= $valid_subB= not $doneC= not $_[0]->valid_unformat(\@content, \$chunk, $done)
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | # weed out invalid data immediately | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | redo; | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | # remove extra padding | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | substr($content[-1], -$pad, $pad, '') if $pad; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | # and prepend (or replace if there are no more data) with it | ||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | splice(@{$_[1]}, -1, $done, \@content); | |||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | return \@content; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | } }; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub format { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format: not called CC=6 SCAR=34.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | my ($self,$rec) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my @flds; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | foreach my $name (@{$self->{Names}}) { | |||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($self->{Count}[$i]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | push @flds,map {ref($_) ? @$_ : $_} @{$rec->{$name}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if (ref($rec->{$name}) eq "ARRAY") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if (@{$rec->{$name}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | push @flds,$rec->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | push @flds,$rec->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | return pack($self->_format, @flds); | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub blank { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
blank: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | my $rec = $self->unformat(pack($self->_format, | |||||||||||||||||||||||||||||||||||||||||
| 160 | unpack($self->_format, | ||||||||||||||||||||||||||||||||||||||||||
| 161 | ''))); | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | return $rec; | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | Parse::Binary::FixedFormat - Convert between fixed-length fields and hashes | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | use Parse::Binary::FixedFormat; | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | my $tarhdr = | ||||||||||||||||||||||||||||||||||||||||||
| 176 | new Parse::Binary::FixedFormat [ qw(name:a100 mode:a8 uid:a8 gid:a8 size:a12 | ||||||||||||||||||||||||||||||||||||||||||
| 177 | mtime:a12 chksum:a8 typeflag:a1 linkname:a100 | ||||||||||||||||||||||||||||||||||||||||||
| 178 | magic:a6 version:a2 uname:a32 gname:a32 | ||||||||||||||||||||||||||||||||||||||||||
| 179 | devmajor:a8 devminor:a8 prefix:a155) ]; | ||||||||||||||||||||||||||||||||||||||||||
| 180 | my $buf; | ||||||||||||||||||||||||||||||||||||||||||
| 181 | read TARFILE, $buf, 512; | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | # create a hash from the buffer read from the file | ||||||||||||||||||||||||||||||||||||||||||
| 184 | my $hdr = $tarhdr->unformat($buf); # $hdr gets a hash ref | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | # create a flat record from a hash reference | ||||||||||||||||||||||||||||||||||||||||||
| 187 | my $buf = $tarhdr->format($hdr); # $hdr is a hash ref | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | # create a hash for a new record | ||||||||||||||||||||||||||||||||||||||||||
| 190 | my $newrec = $tarhdr->blank(); | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | B<Parse::Binary::FixedFormat> can be used to convert between a buffer with | ||||||||||||||||||||||||||||||||||||||||||
| 195 | fixed-length field definitions and a hash with named entries for each | ||||||||||||||||||||||||||||||||||||||||||
| 196 | field. The perl C<pack> and C<unpack> functions are used to perform | ||||||||||||||||||||||||||||||||||||||||||
| 197 | the conversions. B<Parse::Binary::FixedFormat> builds the format string by | ||||||||||||||||||||||||||||||||||||||||||
| 198 | concatenating the field descriptions and converts between the lists | ||||||||||||||||||||||||||||||||||||||||||
| 199 | used by C<pack> and C<unpack> and a hash that can be reference by | ||||||||||||||||||||||||||||||||||||||||||
| 200 | field name. | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | B<Parse::Binary::FixedFormat> provides the following methods. | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | To create a converter, invoke the B<new> method with a reference to a | ||||||||||||||||||||||||||||||||||||||||||
| 209 | list of field specifications. | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | my $cvt = | ||||||||||||||||||||||||||||||||||||||||||
| 212 | new Parse::Binary::FixedFormat [ 'field-name:descriptor:count', ... ]; | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | Field specifications contain the following information. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | =item field-name | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | This is the name of the field and will be used as the hash index. | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | =item descriptor | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | This describes the content and size of the field. All of the | ||||||||||||||||||||||||||||||||||||||||||
| 225 | descriptors get strung together and passed to B<pack> and B<unpack> as | ||||||||||||||||||||||||||||||||||||||||||
| 226 | part of the template argument. See B<perldoc -f pack> for information | ||||||||||||||||||||||||||||||||||||||||||
| 227 | on what can be specified here. | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | Don't use repeat counts in the descriptor except for string types | ||||||||||||||||||||||||||||||||||||||||||
| 230 | ("a", "A", "h, "H", and "Z"). If you want to get an array out of the | ||||||||||||||||||||||||||||||||||||||||||
| 231 | buffer, use the C<count> argument. | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | =item count | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | This specifies a repeat count for the field. If specified as a | ||||||||||||||||||||||||||||||||||||||||||
| 236 | non-zero value, this field's entry in the resultant hash will be an | ||||||||||||||||||||||||||||||||||||||||||
| 237 | array reference instead of a scalar. | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | =head2 unformat | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | To convert a buffer of data into a hash, pass the buffer to the | ||||||||||||||||||||||||||||||||||||||||||
| 244 | B<unformat> method. | ||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||
| 246 | $hashref = $cvt->unformat($buf); | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | Parse::Binary::FixedFormat applies the constructed format to the buffer with | ||||||||||||||||||||||||||||||||||||||||||
| 249 | C<unpack> and maps the returned list of elements to hash entries. | ||||||||||||||||||||||||||||||||||||||||||
| 250 | Fields can now be accessed by name though the hash: | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | print $hashref->{field-name}; | ||||||||||||||||||||||||||||||||||||||||||
| 253 | print $hashref->{array-field}[3]; | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | =head2 format | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | To convert the hash back into a fixed-format buffer, pass the hash | ||||||||||||||||||||||||||||||||||||||||||
| 258 | reference to the B<format> method. | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | $buf = $cvt->format($hashref); | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | =head2 blank | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | To get a hash that can be used to create a new record, call the | ||||||||||||||||||||||||||||||||||||||||||
| 266 | B<blank> method. | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | $newrec = $cvt->blank(); | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | Each Parse::Binary::FixedFormat instance contains the following attributes. | ||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||
| 274 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | =item Names | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | Names contains a list of the field names for this variant. | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | =item Count | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | Count contains a list of occurrence counts. This is used to indicate | ||||||||||||||||||||||||||||||||||||||||||
| 283 | which fields contain arrays. | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | =item Format | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | Format contains the template string for the Perl B<pack> and B<unpack> | ||||||||||||||||||||||||||||||||||||||||||
| 288 | functions. | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | Audrey Tang E<lt>cpan@audreyt.orgE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | Based on Data::FixedFormat, written by Thomas Pfau <pfau@nbpfaus.net> | ||||||||||||||||||||||||||||||||||||||||||
| 297 | http://nbpfaus.net/~pfau/. | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | Copyright 2004-2009 by Audrey Tang E<lt>cpan@audreyt.orgE<gt>. | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | Copyright (C) 2000,2002 Thomas Pfau. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | This module is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 306 | under the terms of the GNU General Public License as published by the | ||||||||||||||||||||||||||||||||||||||||||
| 307 | Free Software Foundation; either version 2 of the License, or (at your | ||||||||||||||||||||||||||||||||||||||||||
| 308 | option) any later version. | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | This library is distributed in the hope that it will be useful, but | ||||||||||||||||||||||||||||||||||||||||||
| 311 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||||||||||||||||
| 312 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||||||||||||||||||||||||||||||||||||||
| 313 | Library General Public License for more details. | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||||||||||||||||
| 316 | along with this program; if not, write to the Free Software Foundation, | ||||||||||||||||||||||||||||||||||||||||||
| 317 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | =cut | ||||||||||||||||||||||||||||||||||||||||||