Parse::Binary::Nested: track parsed bytes
[wormy.git] / Parse / Binary / Nested.pm
index 872ccaac1e772d0b97384c1b93b2307cd8f0f715..371a65b47dbf76ff9d9893a560c5e8a1e8f1fdf9 100644 (file)
@@ -1,11 +1,12 @@
 package Parse::Binary::Nested;
 
+use 5.010;
 use strict;
 use warnings;
 
 use Carp;
 
-our $VERSION = '1.01';
+our $VERSION = '1.02';
 
 sub new {
        my ($class, $format) = @_;
@@ -26,39 +27,82 @@ sub template {
                                : $count."X[$count]$count/($value)";
                }
                else {
-                       $value =~ s/=\d*//g;  # hardcoded values
-                       $value =~ s/^C(a)(\d+)/$1 . ($2 + 1)/e;  # length prefix
+                       $value =~ s/=(?:\d+|.)//g;  # hardcoded values
+                       $value =~ s{^C/(a)(\d+)}{$1 . ($2 + 1)}e;  # maximum length
                }
                $value;
        } reverse 0 .. ($#$format - 1) >> 1;
 }
 
 sub convert {
-       my ($self, $format, $data) = @_;
+       my ($self, $format, $data, $pos) = @_;
        # map flat results into a named and nested hash
        my %res;
+       $pos ||= \(my $_pos);
        while (my ($field, $template) = splice @$format, 0, 2) {
                if (ref $template eq 'ARRAY') {
                        my ($count, @subformat) = @$template;
+                       $$pos++ if $count eq 'C';
                        my $max = $count =~ s/^(\d+)// ? $1 : 0;
                        $count = !$count ? $max
                                : $count eq '*' ? $res{levelcount}->{total} : shift @$data;
-                       $res{$field}->[$_] = $self->convert([@subformat], $data) for 0 .. ($max || $count)-1;
+                       $res{$field}->[$_] = $self->convert([@subformat], $data, $pos)
+                               for 0 .. ($max || $count)-1;
                        splice @{ $res{$field} }, $count if $max > $count;
                        $res{$field} = $res{$field}->[0] if $max == 1;
                        next;
                }
-               elsif ($template =~ /^Ca/) {
-                       $data->[0] = unpack 'C/a', $data->[0];
-               }
-               elsif ($template =~ /^(?:[xX]\d*)*$/) {
-                       next;  # no values
-               }
-               elsif ($template =~ /=(\d+)?/) {
-                       $res{$field} = $1;
-                       next;
+               else {
+                       for (split m{(?![0-9*/])(?<![/=])}, $template) {
+                               my ($type, $count) = m{^(\D+)(\d+)?$} or die 'unsupported';
+                               my $mult = $count // 1;
+                               given ($type) {
+                                       when (['c', 'C']) {
+                                               $$pos += $mult;
+                                       }
+                                       when ('x') {
+                                               $$pos += $mult;
+                                               next;
+                                       }
+                                       when (['b', 'B']) {
+                                               $$pos++;
+                                       }
+                                       when (['s', 'S', 'n', 'v']) {
+                                               $$pos += $mult * 2;
+                                       }
+                                       when (['a', 'A', 'Z', 'a*']) {
+                                               $$pos += length $data->[0];
+                                       }
+                                       when ('Z*') {
+                                               $$pos += $count // 1 + length $data->[0];
+                                       }
+                                       when (['C/a', 'C/A']) {
+                                               $$pos += 1 + ($count // length $data->[0]);
+                                               $data->[0] = unpack 'C/a', $data->[0] if defined $count;
+                                       }
+                                       when ('=') {
+                                               unshift @$data, $count;
+                                       }
+                                       when ('=.') {
+                                               unshift @$data, $$pos;
+                                       }
+                                       when ('X') {
+                                               $$pos -= $mult;
+                                               next;
+                                       }
+                                       default {
+                                               carp "Unrecognised template element '$type'";
+                                       }
+                               }
+                               if (defined $res{$field}) {
+                                       $res{$field} = [ $res{$field} ] unless ref $res{$field} eq 'ARRAY';
+                                       push @{ $res{$field} }, shift @$data;
+                               }
+                               else {
+                                       $res{$field} = shift @$data;
+                               }
+                       }
                }
-               $res{$field} = shift @$data;
        }
        return \%res;
 }