File Coverage

blib/lib/Disassemble/X86/MemRegion.pm
Criterion Covered Total %
statement 34 57 59.6
branch 5 18 27.7
condition 5 25 20.0
subroutine 9 15 60.0
pod 11 11 100.0
total 64 126 50.7


line stmt bran cond sub pod time code
1             package Disassemble::X86::MemRegion;
2              
3 1     1   19 use 5.006;
  1         2  
  1         31  
4 1     1   4 use strict;
  1         2  
  1         34  
5 1     1   6 use warnings;
  1         1  
  1         29  
6 1     1   4 use integer;
  1         2  
  1         5  
7              
8             sub new {
9 2     2 1 7 my ($class, %args) = @_;
10 2         6 my $self = bless { } => $class;
11 2         4 my $mem = $args{mem};
12 2 50       7 defined $mem or $mem = "";
13 2         10 $self->{mem} = $mem;
14 2         6 $self->{len} = length($mem);
15 2   50     12 $self->{start} = $args{start} || 0;
16 2         6 $self->{end} = $self->{start} + $self->{len};
17 2         9 return $self;
18             } # new
19              
20 0     0 1 0 sub mem { $_[0]->{mem} }
21 2     2 1 15 sub start { $_[0]->{start} }
22 0     0 1 0 sub end { $_[0]->{end} }
23              
24             sub contains {
25 0     0 1 0 my ($self, $addr) = @_;
26 0   0     0 return $addr >= $self->{start} && $addr < $self->{end};
27             } # contains
28              
29             sub get_byte {
30 1333     1333 1 2539 my ($self, $pos) = @_;
31 1333         2249 $pos -= $self->{start};
32 1333 100 66     8329 return undef if $pos < 0 || $pos >= $self->{len};
33 1329         6025 return ord substr($self->{mem}, $pos, 1);
34             } # get_byte
35              
36             sub get_word {
37 64     64 1 92 my ($self, $pos) = @_;
38 64         99 $pos -= $self->{start};
39 64 50 33     536 return undef if $pos < 0 || $pos+2 > $self->{len};
40 64         443 return unpack "v", substr($self->{mem}, $pos, 2);
41             } # get_word
42              
43             sub get_long {
44 56     56 1 273 my ($self, $pos) = @_;
45 56         125 $pos -= $self->{start};
46 56 50 33     385 return undef if $pos < 0 || $pos+4 > $self->{len};
47 56         373 return unpack "V", substr($self->{mem}, $pos, 4);
48             } # get_long
49              
50             sub get_string {
51 0     0 1   my ($self, $pos, $len) = @_;
52 0           $pos -= $self->{start};
53 0 0 0       return undef if $pos < 0 || $pos > $self->{len};
54 0   0       $len ||= "*";
55 0           return unpack "x${pos}Z$len", $self->{mem};
56             } # get_string
57              
58             sub get_string_lenbyte {
59 0     0 1   my ($self, $pos) = @_;
60 0           $pos -= $self->{start};
61 0           my $mem_len = $self->{len};
62 0 0 0       return undef if $pos < 0 || $pos >= $mem_len;
63 0           my $str_len = ord substr($self->{mem}, $pos, 1);
64 0 0         return undef if $pos+$str_len >= $mem_len;
65 0           return substr($self->{mem}, $pos+1, $str_len);
66             } # get_string_lenbyte
67              
68             sub get_string_lenword {
69 0     0 1   my ($self, $pos) = @_;
70 0           $pos -= $self->{start};
71 0           my $mem_len = $self->{len};
72 0 0 0       return undef if $pos < 0 || $pos+2 > $mem_len;
73 0           my $str_len = unpack "v", substr($self->{mem}, $pos, 2);
74 0 0         return undef if $pos+$str_len+2 > $mem_len;
75 0           return substr($self->{mem}, $pos+2, $str_len);
76             } # get_string_lenword
77              
78             1 # end MemRegion.pm
79             __END__