File Coverage

blib/lib/Data/ParseBinary/Data/Cap.pm
Criterion Covered Total %
statement 21 23 91.3
branch 2 4 50.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 30 34 88.2


line stmt bran cond sub pod time code
1             package Data::ParseBinary::Data::Cap;
2 1     1   698 use strict;
  1         2  
  1         37  
3 1     1   6 use warnings;
  1         2  
  1         27  
4 1     1   5 use Data::ParseBinary;
  1         1  
  1         408  
5 1     1   5 use Data::ParseBinary qw{OptionalGreedyRange};
  1         2  
  1         195  
6             #"""
7             #tcpdump capture file
8             #"""
9            
10            
11             my $packet = Struct("packet",
12             Data::ParseBinary::lib::DataCap::MicrosecAdapter->create(
13             Sequence("time",
14             ULInt32("time"),
15             ULInt32("usec"),
16             )
17             ),
18             ULInt32("length"),
19             Padding(4),
20             Field("data", sub { $_->ctx->{length} }),
21             );
22            
23             our $data_cap_parser = Struct("cap_file",
24             Padding(24),
25             OptionalGreedyRange($packet),
26             );
27            
28             require Exporter;
29             our @ISA = qw(Exporter);
30             our @EXPORT = qw($data_cap_parser);
31            
32             package Data::ParseBinary::lib::DataCap::MicrosecAdapter;
33             our @ISA;
34 1     1   228 BEGIN { @ISA = qw{Data::ParseBinary::Adapter}; }
35            
36             sub _decode {
37 21     21   39 my ($self, $value) = @_;
38 21         102 return sprintf("%d.%06d", @$value)
39             }
40            
41             sub _encode {
42 21     21   33 my ($self, $tvalue) = @_;
43 21 50       54 if ( index($tvalue, ".") >= 0 ) {
44 21         205 my ($sec, $usec) = $tvalue =~ /^(\d+)\.(\d*)$/;
45 21 50       56 if (length($usec) > 6) {
46 0         0 $usec = substr($usec, 0, 6);
47             } else {
48 21         47 $usec .= "0" x (6 - length($usec));
49             }
50 21         85 return [$sec, $usec];
51             } else {
52 0           return [$tvalue, 0];
53             }
54             }
55             #def _decode(self, obj, context):
56             # return datetime.fromtimestamp(obj[0] + (obj[1] / 1000000.0))
57             #def _encode(self, obj, context):
58             # offset = time.mktime(*obj.timetuple())
59             # sec = int(offset)
60             # usec = (offset - sec) * 1000000
61             # return (sec, usec)
62            
63            
64             1;
65            
66             __END__