PurePerl.pm

filter: Statementstmt 60.5% 83 / 137 Branchbran 64.0% 41 / 64 Conditioncond 51.8% 14 / 27 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 41.6% 5 / 12 Podpod n/a totaltotal 57.6% 147 / 255 CC 43 SCAR 47.2
CC
43
Cov
61%
TIEHASH
140.5
STORE
2.0
DELETE
2.0
CRAP 112.2
↑ Summary dump_rpm.pl »
1 # Copyright (C) 2001,2002,2006 Troels Liebe Bentsen
2 # This library is free software; you can redistribute it and/or modify
3 # it under the same terms as Perl itself.
4  
5 package RPM::Header::PurePerl;
6 1 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION = q{1.0.2};
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use RPM::Header::PurePerl::Tagtable;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub TIEHASH # during tie()
13 1 ▶{
Subroutine TIEHASH: called CC=43 SCAR=49.5
14 1 my $RPM_HEADER_MAGIC = chr(0x8e).chr(0xad).chr(0xe8);
15 1 my $RPM_FILE_MAGIC = chr(0xed).chr(0xab).chr(0xee).chr(0xdb);
16 1 my $buff;
17
18 1 my ($class_name, $filename, $readtype) = @_;
19 1 my $self = bless { hash => {}, }, $class_name;
20
21 0 ▶ if (!defined($filename) or !open(RPMFILE, "<$filename")) { return undef; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename and open RPMFILE, "<$filename") 0 1 1
Condition33%1/3
defined $filename and open RPMFILE, "<$filename"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $filename and open RPMFILE, "<$filename"
defined $filename open RPMFILE, "<$filename"
Truth table33%1/3
A and B
A= defined $filenameB= open RPMFILE, "<$filename"
ABresult
0X0
100
111
22
23 1 binmode(RPMFILE);
24
25 # Read rpm lead
26 1 read(RPMFILE, $buff, 96);
27 ( $self->{'hash'}->{'LEAD_MAGIC'}, # unsigned char[4], í«îÛ == rpm
28 $self->{'hash'}->{'LEAD_MAJOR'}, # unsigned char, 3 == rpm version 3.x
29 $self->{'hash'}->{'LEAD_MINOR'}, # unsigned char, 0 == rpm version x.0
30 $self->{'hash'}->{'LEAD_TYPE'}, # short(int16), 0 == binary, 1 == source
31 $self->{'hash'}->{'LEAD_ARCHNUM'}, # short(int16), 1 == i386
32 $self->{'hash'}->{'LEAD_NAME'}, # char[66], rpm name
33 $self->{'hash'}->{'LEAD_OSNUM'}, # short(int16), 1 == Linux
34 $self->{'hash'}->{'LEAD_SIGNATURETYPE'}, # short(int16), 1280 == rpm 4.0
35 1 $self->{'hash'}->{'LEAD_RESERVED'} # char[16] future expansion
36 ) = unpack("a4CCssA66ssA16", $buff);
37 # DEBUG:
38 # foreach my $var (keys %{$self->{'hash'}}) { print "$self->{'hash'}->{$var}\n"; } exit;
39
40 0 ▶ if (!$self->{'hash'}->{'LEAD_MAGIC'} eq $RPM_FILE_MAGIC) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (!$self->{"hash"}{"LEAD_MAGIC"} eq $RPM_FILE_MAGIC) 0 1 1
41
42 # Quick read option.
43 0 ▶ if (defined($readtype) and ($readtype eq 'onlylead')) { return $self; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $readtype and $readtype eq "onlylead") 0 1 1
Condition33%1/3
defined $readtype and $readtype eq "onlylead"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $readtype and $readtype eq "onlylead"
defined $readtype $readtype eq "onlylead"
Truth table33%1/3
A and B
A= defined $readtypeB= $readtype eq "onlylead"
ABresult
0X0
100
111
44
45 1 ▶ for (my $header_num=1; $header_num < 3; $header_num++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_num < 3) 2 1 3
46 # DEBUG:
47 # print "hlead:".tell(RPMFILE)."\n";
48
49 # Read lead of the headers
50 2 read(RPMFILE, $buff, 16);
51
52 # DEBUG:
53 # print "hlead:".tell(RPMFILE)."\n";
54
55 2 my ($header_magic, $header_version, $header_reserved, $header_entries,
56 $header_size) = unpack("a3CNNN", $buff);
57
58 # DEBUG:
59 #print "$header_magic, $header_version, $header_reserved, $header_entries, $header_size\n"; next;
60 #read(RPMFILE, $buff, 2200, 0); print "header magic:".index($buff, $RPM_HEADER_MAGIC, 256)."\n"; exit;
61
62 2 ▶ if ($header_magic eq $RPM_HEADER_MAGIC) { # RPM_HEADER_MAGIC
Branch: 1 branch
BranchTrueFalseTotal
if ($header_magic eq $RPM_HEADER_MAGIC) 2 0 2
63 # Read the record structure.
64 2 my $record;
65 2 read(RPMFILE, $record, 16*$header_entries);
66
67 # Read the tag structure, pad to a multiplyer of 8 if it's the first header.
68 2 ▶ if ($header_num == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_num == 1) { } 1 1 2
69 # DEBUG:
70 #print "Offset 1: $header_size, ".tell(RPMFILE)."\n";
71 1 ▶ if (($header_size % 8) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_size % 8 == 0) { } 1 0 1
72 1 read(RPMFILE, $buff, $header_size);
73 }
74 else {
75 0 read(RPMFILE, $buff, $header_size+(8-($header_size % 8)));
76 }
77 }
78 else {
79 # DEBUG:
80 #print "Offset 2:".tell(RPMFILE)."\n";
81 1 read(RPMFILE, $buff, $header_size);
82 }
83
84 2 ▶ for (my $record_num=0; $record_num < $header_entries;
Branch: 1 branch
BranchTrueFalseTotal
if ($record_num < $header_entries) 51 2 53
85 $record_num++) { # RECORD LOOP
86 51 my ($tag, $type, $offset, $count) =
87 unpack("NNNN", substr($record, $record_num*16, 16));
88
89 51 my @value;
90
91 # 10x if signature header.
92 51 ▶ if ($header_num == 1) { $tag = $tag*10; }
Branch: 1 branch
BranchTrueFalseTotal
if ($header_num == 1) 4 47 51
93
94 # Unknown tag
95 51 ▶ if (!defined($hdr_tags{$tag})) {
Branch: 7 branches
BranchTrueFalseTotal
if (not defined $RPM::Header::PurePerl::hdr_tags{$tag}) { } 0 51 51
elsif ($type == 0) { } 0 51 51
elsif ($type == 1) { } 0 51 51
elsif ($type == 2) { } 0 51 51
elsif ($type == 3) { } 2 49 51
elsif ($type == 4) { } 14 35 49
elsif ($type == 5) { } 0 35 35
96 0 print "Unknown $tag, $type\n"; next;
97 }
98 # Null type
99 elsif ($type == 0) {
100 0 @value = ('');
101 }
102 # Char type
103 elsif ($type == 1) {
104 0 print "Char $count $hdr_tags{$tag}{'TAGNAME'}\n";
105 #for (my $i=0; $i < $count; $i++) {
106 #push(@value, substr($buff, $offset, $count));
107 # $header_info{$record}{'offset'} += $count;
108 #}
109 }
110 # int8
111 elsif ($type == 2) {
112 0 @value = unpack("C*", substr($buff, $offset, 1*$count));
113 0 $offset = 1*$count;
114 }
115 # int16
116 elsif ($type == 3) {
117 2 @value = unpack("n*", substr($buff, $offset, 2*$count));
118 2 $offset = 2*$count;
119 }
120 # int32
121 elsif ($type == 4) {
122 14 @value = unpack("N*", substr($buff, $offset, 4*$count));
123 14 $offset = 4*$count;
124 }
125 # int64
126 elsif ($type == 5) {
127 0 print "Int64(Not supported): ".
128 "$count $hdr_tags{$tag}{'TAGNAME'}\n";
129 #@value = unpack("N*", substr($buff, $offset, 4*$count));
130 #$offset = 4*$count;
131 }
132 # String, String array, I18N string array
133 51 ▶ if ($type == 6 or $type == 8 or $type == 9) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 6 or $type == 8 or $type == 9) { } 32 19 51
elsif ($type == 7) { } 3 16 19
Condition100%6/6
$type == 6 or $type == 8 or $type == 9
l!l&&r!l&&!r
29319
$type == 6 or $type == 8
l!l&&r!l&&!r
141522
MC/DC100%3/3
$type == 6 or $type == 8 or $type == 9
$type == 6 $type == 8 $type == 9
Truth table100%4/4
A or B or C
A= $type == 6B= $type == 8C= $type == 9
ABCresult
1XX1
01X1
0011
0000
134 32 ▶ for(my $i=0;$i<$count;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 34 32 66
135 34 my $length = index($buff, "\0", $offset)-$offset;
136 # unpack istedet for substr.
137 34 push(@value, substr($buff, $offset, $length));
138 34 $offset += $length+1;
139 }
140 }
141 # bin
142 elsif ($type == 7) {
143 #print "Bin $count $tags{$tag}{'TAGNAME'}\n";
144 3 $value[0] = substr($buff, $offset, $count);
145 }
146 # Find out if it's an array type or not.
147 51 ▶ if (defined($hdr_tags{$tag}{'TYPE'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $RPM::Header::PurePerl::hdr_tags{$tag}{'TYPE'} and $RPM::Header::PurePerl::hdr_tags{$tag}{'TYPE'} == 1) { } 36 15 51
Condition66%2/3
defined $RPM::Header::PurePerl::hdr_tags{$tag}{"TYPE"} and $RPM::Header::PurePerl::hdr_tags{$tag}{"TYPE"} == 1
!ll&&!rl&&r
15036
MC/DC50%1/2
defined $RPM::Header::PurePerl::hdr_tags{$tag}{"TYPE"} and $RPM::Header::PurePerl::hdr_tags{$tag}{"TYPE"} == 1
defined $RPM::Header::PurePerl::hdr_tags{$tag}{"TYPE"} $RPM::Header::PurePerl::hdr_tags{$tag}{"TYPE"} == 1
Truth table66%2/3
A and B
A= defined $RPM::Header::PurePerl::hdr_tags{$tag}{"TYPE"}B= $RPM::Header::PurePerl::hdr_tags{$tag}{"TYPE"} == 1
ABresult
0X0
100
111
148 and $hdr_tags{$tag}{'TYPE'} == 1) {
149 36 @{$self->{'hash'}->{$hdr_tags{$tag}{'TAGNAME'}}} = @value;
150 }
151 else {
152 15 $self->{'hash'}->{$hdr_tags{$tag}{'TAGNAME'}} = $value[0];
153 }
154 } # RECORD LOOP
155 } # HEADER LOOP
156 }
157
158 # Save package(cpio.gz) location.
159 1 $self->{'hash'}->{'PACKAGE_OFFSET'} = tell(RPMFILE);
160 1 close(RPMFILE);
161  
162 # Make old packages look like new ones.
163 1 ▶ if (defined($self->{'hash'}->{'FILENAMES'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"hash"}{"FILENAMES"}) 0 1 1
164 0 my $count = 0;
165 0 my %quick_dirnames;
166 0 foreach my $filename (@{$self->{'hash'}->{'FILENAMES'}}) {
167 0 my $file = ''; my $dir = '/';
168
169 0 ▶ if($filename =~ /(.*\/)(.*$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ m[(.*/)(.*$)]) { } 0 0 0
170 0 $file = $1; $dir = $2;
171 } else {
172 0 $file = $filename;
173 }
174
175 0 ▶ if (!defined($quick_dirnames{$dir})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $quick_dirnames{$dir}) 0 0 0
176 0 push(@{$self->{'hash'}->{'DIRNAMES'}}, $dir);
177 0 $quick_dirnames{$dir} = $count++;
178 }
179 0 push(@{$self->{'hash'}->{'BASENAMES'}}, $file);
180 0 push(@{$self->{'hash'}->{'DIRINDEXES'}}, $quick_dirnames{$dir});
181 }
182 0 delete($self->{'hash'}->{'FILENAMES'});
183 }
184  
185 # Wait I can beat it, a package sould also provide is's own name, sish (and only once).
186 1 my %quick_provides = map {$_ => 1} @{$self->{'hash'}->{'PROVIDENAME'}};
187 1 my %quick_provideflags = map {$_ => 1} @{$self->{'hash'}->{'PROVIDEFLAGS'}};
188 my %quick_provideversion
189 1 = map {$_ => 1} @{$self->{'hash'}->{'PROVIDEVERSION'}};
190
191 1 ▶ if (!defined($quick_provides{$self->{'hash'}->{'NAME'}}) and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $quick_provides{$self->{"hash"}{"NAME"}} or defined $quick_provideflags{8} or defined $quick_provideversion{$self->{"hash"}{"VERSION"}}) 0 1 1
Condition33%2/6
defined $quick_provides{$self->{"hash"}{"NAME"}} or defined $quick_provideflags{8} or defined $quick_provideversion{$self->{"hash"}{"VERSION"}}
l!l&&r!l&&!r
100
defined $quick_provides{$self->{"hash"}{"NAME"}} or defined $quick_provideflags{8}
l!l&&r!l&&!r
100
MC/DC0%0/3
defined $quick_provides{$self->{"hash"}{"NAME"}} or defined $quick_provideflags{8} or defined $quick_provideversion{$self->{"hash"}{"VERSION"}}
defined $quick_provides{$self->{"hash"}{"NAME"}} defined $quick_provideflags{8} defined $quick_provideversion{$self->{"hash"}{"VERSION"}}
Truth table25%1/4
A or B or C
A= defined $quick_provides{$self->{"hash"}{"NAME"}}B= defined $quick_provideflags{8}C= defined $quick_provideversion{$self->{"hash"}{"VERSION"}}
ABCresult
1XX1
01X1
0011
0000
192 !defined($quick_provideflags{8}) and
193 !defined($quick_provideversion{$self->{'hash'}->{'VERSION'}})) {
194 0 push(@{$self->{'hash'}->{'PROVIDENAME'}}, $self->{'hash'}->{'NAME'});
195 0 push(@{$self->{'hash'}->{'PROVIDEFLAGS'}}, 8);
196 0 push(@{$self->{'hash'}->{'PROVIDEVERSION'}},
197 0 $self->{'hash'}->{'VERSION'}.'-'.$self->{'hash'}->{'RELEASE'});
198 }
199
200 # FILEVERIFYFLAGS is signed
201 1 ▶ if ($self->{'hash'}->{'FILEVERIFYFLAGS'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hash"}{"FILEVERIFYFLAGS"}) 1 0 1
202 1 ▶ for(my $i=0;$i<int(@{$self->{'hash'}->{'FILEVERIFYFLAGS'}}); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < int @{$self->{"hash"}{"FILEVERIFYFLAGS"};}) 1 1 2
203 1 my $val = @{$self->{'hash'}->{'FILEVERIFYFLAGS'}}[$i];
204 1 ▶ if (int($val) == $val && $val >= 2147483648 &&
Branch: 1 branch
BranchTrueFalseTotal
if (int $val == $val and $val >= 2147483648 and $val <= 4294967295) 1 0 1
Condition33%2/6
int $val == $val and $val >= 2147483648 and $val <= 4294967295
!ll&&!rl&&r
001
int $val == $val and $val >= 2147483648
!ll&&!rl&&r
001
MC/DC0%0/3
int $val == $val and $val >= 2147483648 and $val <= 4294967295
int $val == $val $val >= 2147483648 $val <= 4294967295
Truth table25%1/4
A and B and C
A= int $val == $valB= $val >= 2147483648C= $val <= 4294967295
ABCresult
0XX0
10X0
1100
1111
205 $val <= 4294967295) {
206 1 @{$self->{'hash'}->{'FILEVERIFYFLAGS'}}[$i] -= 4294967296;
207 }
208 }
209 }
210
211 # Lets handel the SIGNATURE, this does not work, fix it please.
212 1 ▶ if (defined($self->{'hash'}->{'SIGNATURE_MD5'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"hash"}{"SIGNATURE_MD5"}) 0 1 1
213 $self->{'hash'}->{'SIGNATURE_MD5'} =
214 0 unpack("H*", $self->{'hash'}->{'SIGNATURE_MD5'});
215 }
216  
217 # Old stuff, so it can be a drop in replacement for RPM::HEADERS.
218 1 ▶ if (defined($self->{'hash'}->{'EPOCH'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"hash"}{"EPOCH"}) 0 1 1
219 0 $self->{'hash'}->{'SERIAL'} = $self->{'hash'}->{'EPOCH'};
220 }
221  
222 1 ▶ if (defined($self->{'hash'}->{'LICENSE'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"hash"}{"LICENSE"}) 1 0 1
223 1 $self->{'hash'}->{'COPYRIGHT'} = $self->{'hash'}->{'LICENSE'};
224 }
225
226 1 ▶ if (defined($self->{'hash'}->{'PROVIDENAME'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"hash"}{"PROVIDENAME"}) 1 0 1
227 1 $self->{'hash'}->{'PROVIDES'} = $self->{'hash'}->{'PROVIDENAME'};
228 }
229
230 1 ▶ if (defined($self->{'hash'}->{'OBSOLETENAME'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"hash"}{"OBSOLETENAME"}) 0 1 1
231 0 $self->{'hash'}->{'OBSOLETES'} = $self->{'hash'}->{'OBSOLETENAME'};
232 }
233
234 1 return $self;
235 }
236  
237 sub FETCH # during $a = $ht{something};
238 2 ▶{
Subroutine FETCH: called CC=1 SCAR=0.0
239 2 my ($self, $key) = @_;
240 2 return $self->{hash}->{$key};
241 }
242  
243 sub STORE # during $ht{something} = $a;
244 0 ▶{
Subroutine STORE: not called CC=1 SCAR=6.9
245 0 my ($self, $key, $val) = @_;
246 0 $self->{hash}->{$key} = $val;
247 }
248  
249 sub DELETE # during delete $ht{something}
250 0 ▶{
Subroutine DELETE: not called CC=1 SCAR=6.9
251 0 my ($self, $key) = @_;
252 0 delete $self->{hash}->{$key};
253 }
254  
255 sub CLEAR # during %h = ();
256 0 ▶{
Subroutine CLEAR: not called CC=1 SCAR=6.9
257 0 my ($self) = @_;
258 0 $self->{hash} = {};
259 0 ();
260 }
261  
262 sub EXISTS # during if (exists $h{something}) { ... }
263 0 ▶{
Subroutine EXISTS: not called CC=1 SCAR=6.9
264 0 my ($self, $key) = @_;
265 0 return exists $self->{hash}->{$key};
266 }
267  
268 sub FIRSTKEY # at the beginning of foreach (keys %h) { ... }
269 0 ▶{
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
270 0 my ($self) = @_;
271 0 each %{$self->{hash}};
272 }
273  
274 sub NEXTKEY # during foreach()
275 0 ▶{
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
276 0 my ($self) = @_;
277 0 each %{$self->{hash}};
278 }
279  
280 sub DESTROY # well, when the hash gets destroyed
281 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
282 # do nothing here
283 }
284  
285 =head1 NAME
286  
287 RPM::Header::PurePerl - a perl only implementation of a RPM header reader.
288  
289 =head1 VERSION
290  
291 Version 1.0.2
292  
293 =head1 SYNOPSIS
294  
295 use RPM::Header::PurePerl;
296 tie my %rpm, "RPM::Header::PurePerl", "rpm-4.0-1-i586.rpm"
297 or die "Problem, could not open rpm";
298 print $rpm{'NAME'};
299  
300 =head1 DESCRIPTION
301  
302 RPM::Header::PurePerl is a clone of RPM::Header written in only Perl, so it
303 provides a way to read a rpm package on systems where rpm is not installed.
304 RPM::Header::PurePerl can used as a drop in replacement for RPM::Header, if
305 needed also the other way round.
306  
307 =head1 NOTES
308  
309 The former name of this package was RPM::PerlOnly.
310  
311 =head1 AUTHOR
312  
313 Troels Liebe Bentsen <tlb@rapanden.dk>
314  
315 =head1 COPYRIGHT AND LICENCE
316  
317 Copyright (C) 2001,2002,2006 Troels Liebe Bentsen
318  
319 This library is free software; you can redistribute it and/or modify
320 it under the same terms as Perl itself.
321  
322 =cut
323  
324 1 1;
325 __END__
↑ Summary dump_rpm.pl »