File Coverage

blib/lib/TLV/Parser.pm
Criterion Covered Total %
statement 45 45 100.0
branch 7 10 70.0
condition 2 4 50.0
subroutine 5 5 100.0
pod 2 2 100.0
total 61 66 92.4


line stmt bran cond sub pod time code
1             package TLV::Parser;
2 1     1   339592 use strict;
  1         2  
  1         43  
3 1     1   5 use warnings;
  1         2  
  1         112  
4              
5             our ( $VERSION );
6              
7             BEGIN {
8 1     1   486 $VERSION = '1.01';
9             }
10              
11             sub new {
12 1     1 1 1142 my $class = shift;
13 1         4 my $href = shift;
14 1 50       2 die "No tags defined!" unless scalar @{$href->{tag_aref}};
  1         5  
15 1         3 my $self = {};
16 1         2 my ($tag_href, $tag_len_href);
17 1         3 foreach ( @{$href->{tag_aref}} ) {
  1         3  
18 1         3 $tag_href->{$_} = undef;
19 1         3 my $len = length $_;
20 1 50       6 $tag_len_href->{$len} = undef unless exists $tag_len_href->{$len};
21             }
22 1         3 $self->{tag} = $tag_href;
23 1         2 $self->{tag_len} = [ keys %{$tag_len_href} ];
  1         6  
24 1   50     24 $self->{l_len} = $href->{l_len} || 2;
25            
26 1         6 bless $self, $class;
27             }
28              
29             sub parse {
30 2     2 1 1236 my $self = shift;
31 2   50     8 my $tlv_string = shift || die "no string to parse?";
32 2         8 my $l_len = $self->{l_len};
33 2         4 my $result;
34              
35 2         5 while ( length $tlv_string > 0 ) {
36 3         4 my $found;
37 3         6 foreach my $t_len ( @{$self->{tag_len}} ) {
  3         7  
38 3         7 my $tmp = substr($tlv_string, 0, $t_len);
39              
40 3 100       9 if ( exists $self->{tag}->{$tmp} ) {
41 2         6 my $v_len = hex (substr $tlv_string, $t_len, $l_len);
42 2         6 my $v = substr $tlv_string, ($t_len + $l_len), 2 * $v_len;
43 2         6 $result->{$tmp} = $v;
44 2         3 $found = 1;
45              
46 2         4 my $offset = $t_len + $l_len + 2 * $v_len;
47 2         5 $tlv_string = substr $tlv_string, $offset;
48 2 50       7 last if $found;
49             }
50             }
51 3 100       11 unless ( $found ) {
52 1         7 $self->{remain} = $tlv_string;
53 1         3 $self->{error} = "parsing incomplete";
54 1         3 last;
55             }
56             }
57 2         8 $self->{result} = $result;
58             }
59             1;
60            
61             __END__