Decoder/BinHex.pm

filter: Statementstmt 91.4% 75 / 82 Branchbran 55.0% 11 / 20 Conditioncond 30.0% 3 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 72.7% 8 / 11 Podpod 40.0% 2 / 5 totaltotal 74.4% 99 / 133 CC 15 SCAR 18.7
CC
15
Cov
79%
decode_it
11.5
last_preamble
6.0
last_filename
6.0
CRAP 6.5
« WordDecoder.pm ↑ Summary Decoder.pm »
1 package MIME::Decoder::BinHex;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5  
6 =head1 NAME
7  
8 MIME::Decoder::BinHex - decode a "binhex" stream
9  
10  
11 =head1 SYNOPSIS
12  
13 A generic decoder object; see L<MIME::Decoder> for usage.
14  
15 Also supports a preamble() method to recover text before
16 the binhexed portion of the stream.
17  
18  
19 =head1 DESCRIPTION
20  
21 A MIME::Decoder subclass for a nonstandard encoding whereby
22 data are binhex-encoded. Common non-standard MIME encodings for this:
23  
24 x-uu
25 x-uuencode
26  
27 =head1 SEE ALSO
28  
29 L<MIME::Decoder>
30  
31 =head1 AUTHOR
32  
33 Julian Field (F<mailscanner@ecs.soton.ac.uk>).
34  
35 All rights reserved. This program is free software; you can redistribute
36 it and/or modify it under the same terms as Perl itself.
37  
38 =cut
39  
40  
41 1 require 5.002;
42 1 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use MIME::Decoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 1 ▶use MIME::Tools qw(whine);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use Convert::BinHex;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 1 @ISA = qw(MIME::Decoder);
48  
49 # The package version, both in 1.23 style *and* usable by MakeMaker:
50 1 $VERSION = "5.522";
51  
52  
53 #------------------------------
54 #
55 # decode_it IN, OUT
56 #
57 1 ▶sub decode_it {
Subroutine decode_it: called CC=10 SCAR=24.4
Pod documented
58 1 my ($self, $in, $out) = @_;
59 1 my ($mode, $file);
60 1 my (@preamble, @data);
61 1 my $H2B = Convert::BinHex->hex2bin;
62 1 my $line;
63  
64 1 $self->{MDU_Preamble} = \@preamble;
65 1 $self->{MDU_Mode} = '600';
66 1 $self->{MDU_File} = undef;
67  
68 ### Find beginning...
69 1 local $_;
70 1 ▶ while (defined($_ = $in->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $in->getline)) 1 0 1
71 1 ▶ if (/^\(This file must be converted/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\(This file must be converted/) 1 0 1
72 1 $_ = $in->getline;
73 1 ▶ last if /^:/;
Branch: 1 branch
BranchTrueFalseTotal
if /^:/ 1 0 1
74 }
75 0 push @preamble, $_;
76 }
77 1 ▶ die("binhex decoding: fell off end of file\n") if !defined($_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 1 1
78  
79 ### Decode:
80 1 my $data;
81 1 $data = $H2B->next($_); # or whine("Next error is $@ $!\n");
82 1 my $len = unpack("C", $data);
83 1 ▶ while ($len > length($data)+21 && defined($line = $in->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > length($data) + 21 and defined($line = $in->getline)) 0 1 1
Condition33%1/3
$len > length($data) + 21 and defined($line = $in->getline)
!ll&&!rl&&r
100
MC/DC0%0/2
$len > length($data) + 21 and defined($line = $in->getline)
$len > length($data) + 21 defined($line = $in->getline)
Truth table33%1/3
A and B
A= $len > length($data) + 21B= defined($line = $in->getline)
ABresult
0X0
100
111
84 0 $data .= $H2B->next($line);
85 }
86 1 ▶ if (length($data) >= 22+$len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $data >= 22 + $len) { } 1 0 1
87 1 $data = substr($data, 22+$len);
88 } else {
89 0 $data = '';
90 }
91  
92 1 $out->print($data);
93 1 ▶ while (defined($_ = $in->getline)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $in->getline)) 63 0 63
94 63 $line = $_;
95 63 $data = $H2B->next($line);
96 63 $out->print($data);
97 63 ▶ last if $line =~ /:$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /:$/ 1 62 63
98 }
99 1 1;
100 }
101  
102 #------------------------------
103 #
104 # encode_it IN, OUT
105 #
106 1 ▶sub encode_it {
Subroutine encode_it: called CC=4 SCAR=13.9
Pod documented
107 1 my ($self, $in, $out) = @_;
108 1 my $line;
109 1 my $buf = '';
110 1 ▶ my $fname = (($self->head &&
Condition40%2/5
$self->head && $self->head->mime_attr('content-disposition.filename') || ''
l!l
01
$self->head and $self->head->mime_attr("content-disposition.filename")
!ll&&!rl&&r
100
MC/DC0%0/2
$self->head && $self->head->mime_attr('content-disposition.filename') || ''
$self->head $self->head->mime_attr("content-disposition.filename")
Truth table33%1/3
A and B
A= $self->headB= $self->head->mime_attr("content-disposition.filename")
ABresult
111
0X0
100
111 $self->head->mime_attr('content-disposition.filename')) ||
112 '');
113 1 my $B2H = Convert::BinHex->bin2hex;
114 1 $out->print("(This file must be converted with BinHex 4.0)\n");
115  
116 # Sigh... get length of file
117 1 $in->seek(0, 2);
118 1 my $datalen = $in->tell();
119 1 $in->seek(0, 0);
120  
121 # Build header in core:
122 1 my @hdrs;
123 1 my $flen = length($fname);
124 1 push @hdrs, pack("C", $flen);
125 1 push @hdrs, pack("a$flen", $fname);
126 1 push @hdrs, pack('C', 4);
127 1 push @hdrs, pack('a4', '????');
128 1 push @hdrs, pack('a4', '????');
129 1 push @hdrs, pack('n', 0);
130 1 push @hdrs, pack('N', $datalen);
131 1 push @hdrs, pack('N', 0); # Resource length
132 1 my $hdr = join '', @hdrs;
133  
134 # Compute the header CRC:
135 1 my $crc = Convert::BinHex::binhex_crc("\000\000",
136 Convert::BinHex::binhex_crc($hdr, 0));
137  
138 # Output the header (plus its CRC):
139 1 $out->print($B2H->next($hdr . pack('n', $crc)));
140  
141 1 ▶ while ($in->read($buf, 1000)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in->read($buf, 1000)) 4 1 5
142 4 $out->print($B2H->next($buf));
143 }
144 1 $out->print($B2H->done);
145 1 1;
146 }
147  
148 #------------------------------
149 #
150 # last_preamble
151 #
152 # Return the last preamble as ref to array of lines.
153 # Gets reset by decode_it().
154 #
155 0 ▶sub last_preamble {
Subroutine last_preamble: not called CC=2 SCAR=17.9
Pod undocumented
156 0 my $self = shift;
157 0 ▶ return $self->{MDU_Preamble} || [];
Condition0%0/2
$self->{'MDU_Preamble'} || []
l!l
00
MC/DC0%0/1
$self->{'MDU_Preamble'} || []
$self->{'MDU_Preamble'}
Truth table0%0/2
A
A= $self->{'MDU_Preamble'}
Aresult
11
00
158 }
159  
160 #------------------------------
161 #
162 # last_mode
163 #
164 # Return the last mode.
165 # Gets reset to undef by decode_it().
166 #
167 0 ▶sub last_mode {
Subroutine last_mode: not called CC=1 SCAR=6.9
Pod undocumented
168 0 shift->{MDU_Mode};
169 }
170  
171 #------------------------------
172 #
173 # last_filename
174 #
175 # Return the last filename.
176 # Gets reset by decode_it().
177 #
178 0 ▶sub last_filename {
Subroutine last_filename: not called CC=2 SCAR=17.9
Pod undocumented
179 0 ▶ shift->{MDU_File} || undef; #[];
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"MDU_File"} or undef 0 0 0
180 }
181  
182 #------------------------------
183 1 1;
« WordDecoder.pm ↑ Summary Decoder.pm »