1 package Parse::Binary::Nested;
8 use Exporter qw(import);
10 our $VERSION = '1.10';
11 our @EXPORT_OK = qw(unpackf);
14 my ($class, $format) = @_;
16 or $format = [0 => $format];
17 ref $format eq 'ARRAY'
18 or croak "Invalid Parse::Binary::Nested format: should be an array ref";
19 bless [$format, $class->template($format)], $class;
23 my ($self, $format) = @_;
24 # total (flattened) unpack template from nested format definitions
26 @$format or return $template;
27 for (reverse 0 .. ($#$format - 1) >> 1) {
28 my $value = $format->[-($_ << 1) - 1];
29 if (ref $value eq 'ARRAY') {
30 my $count = $value->[0];
31 if ($count =~ /^\?/) {
36 $value = $self->template($value);
37 $value = $count =~ s/^([*\d]+)// ? "$count($value)$1"
38 : $count."X[$count]$count/($value)";
42 $value =~ s/=(?:\d+|.)//g; # hardcoded values
43 $value =~ s{^C/(a)(\d+)}{$1 . ($2 + 1)}e; # maximum length
51 my ($self, $format, $data, $pos) = @_;
52 # map flat results into a named and nested hash
54 $pos ||= \(my $_pos = 0);
55 for (my $i = 0; $i < $#$format; $i += 2) {
56 my ($field, $template) = @$format[$i, $i+1];
57 if (ref $template eq 'ARRAY') {
58 my ($count, @subformat) = @$template;
60 if ($count =~ /^\?(\d+)/) {
61 # character-terminated group
63 my $iterate = ref($self)->new(\@subformat);
64 push @{ $iterate->[0] }, -pos => '=.';
66 while ($subpos < length $data->[0]) {
67 last if substr($data->[0], $subpos, 1) eq $endmark;
68 my $iterdata = $iterate->convert($iterate->[0], [
69 unpack $iterate->[1], substr($data->[0], $subpos)
71 $subpos += delete $iterdata->{-pos};
72 push @{ $res{$field} }, $iterdata;
76 $self->template([ @$format[$i+2 .. $#$format] ]),
77 substr($data->[0], $subpos + 1)
78 ) if $subpos < length $data->[0];
82 $$pos++ if $count eq 'C';
83 my $max = $count =~ s/^(\d+)// ? $1 : 0;
84 $count = !$count ? $max
85 : $count eq '*' ? $res{levelcount}->{total} : shift @$data;
86 $res{$field}->[$_] = $self->convert(\@subformat, $data, $pos)
87 for 0 .. ($max || $count)-1;
88 splice @{ $res{$field} }, $count if $max > $count;
89 $res{$field} = $res{$field}->[0] if $max == 1;
93 for (split m{(?![0-9*/])(?<![/=])}, $template) {
94 my ($type, $count) = m{^(\D+)(\d+)?$} or die 'unsupported';
95 my $mult = $count // 1;
107 when (['s', 'S', 'n', 'v']) {
110 when (['a', 'A', 'Z', 'a*']) {
111 $$pos += length $data->[0];
114 $$pos += $count // 1 + length $data->[0];
116 when (['C/a', 'C/A']) {
117 $$pos += 1 + ($count // length $data->[0]);
118 $data->[0] = unpack 'C/a', $data->[0] if defined $count;
121 unshift @$data, $count;
124 unshift @$data, $$pos;
131 carp "Unrecognised template element '$type'";
134 if (defined $res{$field}) {
135 $res{$field} = [ $res{$field} ] unless ref $res{$field} eq 'ARRAY';
136 push @{ $res{$field} }, shift @$data;
139 $res{$field} = shift @$data;
148 my ($format, $input) = @_;
149 my $self = UNIVERSAL::isa($format, __PACKAGE__) ? $format
150 : __PACKAGE__->new($format);
151 my @data = unpack $self->[1], $input;
152 return $self->convert($self->[0], \@data);
159 Parse::Binary::Nested - Structured unpack
163 use Parse::Binary::Nested qw(unpackf);
164 my $data = unpackf([message => 'Z*'], "hi\0");
166 my $parser = Parser::Binary::Nested->new([
174 $data = $parser->unpackf("\1foo\0.rest");
175 print $data->{foos}->[0]->{message};
179 Converts a string into a hash of values, just like C<unpack>
180 except that it allows you to name and nest the resulting elements.
182 Format declarations are simalar to C<pack> templates,
183 with the following additions:
189 An array ref groups additional declarations,
190 with the first value specifying a repetition. If this count is variable,
191 the resulting value will be an array ref of hashes.
193 repeat => ['C', name => 'a*', value => 'S']
195 With a count of 1, it will return only a hash ref,
196 thereby simply grouping declarations:
199 test => [1, foo => 'C']
203 A template value of C<Ca$length> is recognised as a length-preceded string
204 with a constant (maximal) size, and will return only the string adjusted
206 This behaviour is very similar to C<(C/a@x$length)>, except that it never reads
207 more than the given number of bytes.
211 Hardcoded values can be inserted using C<=$number> values.
212 This can for example be useful to retain forwards-compatibility:
215 type => '=1', # nothing read
228 Mischa POSLAWSKY <perl@shiar.org>