DKIM/MessageParser.pm

filter: Statementstmt 89.7% 79 / 88 Branchbran 91.1% 31 / 34 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 57.1% 8 / 14 Podpod 0.0% 0 / 8 totaltotal 82.5% 123 / 149 CC 19 SCAR 22.0
CC
19
Cov
90%
PRINT
16.0
CLOSE
4.0
new_handle
2.0
CRAP 9.0
« DKIM/Canonicalization/Base.pm ↑ Summary DKIM/KeyValueList.pm »
1 package Mail::DKIM::MessageParser;
2 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 our $VERSION = '1.20261105'; # VERSION
5 # ABSTRACT: Signs/verifies Internet mail with DKIM/DomainKey signatures
6  
7 # Copyright 2005 Messiah College. All rights reserved.
8 # Jason Long <jlong@messiah.edu>
9  
10 # Copyright (c) 2004 Anthony D. Urso. All rights reserved.
11 # This program is free software; you can redistribute it and/or
12 # modify it under the same terms as Perl itself.
13  
14 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1621 ▶sub new_object {
Subroutine new_object: called CC=1 SCAR=0.0
Pod undocumented
17 1621 my $class = shift;
18 1621 return $class->TIEHANDLE(@_);
19 }
20  
21 0 ▶sub new_handle {
Subroutine new_handle: not called CC=1 SCAR=6.9
Pod undocumented
22 0 my $class = shift;
23 0 local *TMP;
24 0 tie *TMP, $class, @_;
25 0 return *TMP;
26 }
27  
28 1621 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=1 SCAR=0.0
29 1621 my $class = shift;
30 1621 my %args = @_;
31 1621 my $self = bless \%args, $class;
32 1621 $self->init;
33 1619 return $self;
34 }
35  
36 1621 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
37 1621 my $self = shift;
38  
39 1621 my $buf = '';
40 1621 $self->{buf_ref} = \$buf;
41 1621 $self->{in_header} = 1;
42 1621 $self->{header_scan} = 0;
43 }
44  
45 30466 ▶sub PRINT {
Subroutine PRINT: called CC=16 SCAR=27.7
46 30466 my $self = shift;
47 30466 my $buf_ref = $self->{buf_ref};
48 30466 ▶ $$buf_ref .= @_ == 1 ? $_[0] : join( '', @_ ) if @_;
Branch: 2 branches
BranchTrueFalseTotal
if @_ 30466 0 30466
@_ == 1 ? : 30466 0 30466
49  
50 30466 ▶ if ( $self->{in_header} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_header"}) 30441 25 30466
51  
52 # Walk the buffer with an offset, remove the parsed fields once,
53 # and resume the search for the end of an incomplete field where
54 # the last call stopped: removing each field from the front of
55 # the buffer, or rescanning an incomplete field on every call,
56 # is quadratic.
57 30441 my $off = 0;
58 30441 my $scan = $self->{header_scan};
59 30441 ▶ while ( $off < length($$buf_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < length $$buf_ref) 436393 1 436394
60 436393 ▶ if ( substr( $$buf_ref, $off, 2 ) eq "\015\012" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$buf_ref, $off, 2) eq "\r\n") 338 436055 436393
61 338 substr( $$buf_ref, 0, $off + 2 ) = '';
62 338 $off = $scan = 0;
63 338 $self->finish_header();
64 338 $self->{in_header} = 0;
65 338 last;
66 }
67  
68 # the field ends at the first CRLF not followed by SP or HTAB
69 436055 my $end;
70 436055 ▶ my $i = $scan > $off ? $scan : $off;
Branch: 1 branch
BranchTrueFalseTotal
$scan > $off ? : 30088 405967 436055
71 436055 while (1) {
72 470199 my $j = index( $$buf_ref, "\015\012", $i );
73 470199 ▶ if ( $j < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < 0) 79 470120 470199
74 79 $i = length($$buf_ref) - 1; # a final CR may start a CRLF
75 79 last;
76 }
77 470120 ▶ if ( $j + 2 >= length($$buf_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j + 2 >= length $$buf_ref) 30023 440097 470120
78 30023 $i = $j; # the next character isn't here yet
79 30023 last;
80 }
81 440097 my $c = substr( $$buf_ref, $j + 2, 1 );
82 440097 ▶ if ( $c ne ' ' && $c ne "\t" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne " " and $c ne "\t") 405953 34144 440097
Condition100%3/3
$c ne " " and $c ne "\t"
!ll&&!rl&&r
34004140405953
MC/DC100%2/2
$c ne " " and $c ne "\t"
$c ne " " $c ne "\t"
Truth table100%3/3
A and B
A= $c ne " "B= $c ne "\t"
ABresult
0X0
100
111
83 405953 $end = $j + 2;
84 405953 last;
85 }
86 34144 $i = $j + 2;
87 }
88 436055 ▶ if ( !defined $end ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $end) 30102 405953 436055
89 30102 $scan = $i;
90 30102 last;
91 }
92 405953 $self->add_header( substr( $$buf_ref, $off, $end - $off ) );
93 405953 $off = $end;
94 405953 $scan = 0;
95 }
96 30441 ▶ substr( $$buf_ref, 0, $off ) = '' if $off;
Branch: 1 branch
BranchTrueFalseTotal
if $off 16 30425 30441
97 30441 ▶ $self->{header_scan} = $scan > $off ? $scan - $off : 0;
Branch: 1 branch
BranchTrueFalseTotal
$scan > $off ? : 30093 348 30441
98 }
99  
100 30466 ▶ if ( !$self->{in_header} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_header"}) 363 30103 30466
101 363 my $j = rindex( $$buf_ref, "\015\012" );
102 363 ▶ if ( $j >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j >= 0) 341 22 363
103  
104 # avoid copying a large buffer: the unterminated
105 # last line is typically short compared to the rest
106  
107 341 my $carry = substr( $$buf_ref, $j + 2 );
108 341 substr( $$buf_ref, $j + 2 ) = ''; # shrink to last CRLF
109 341 $self->add_body($$buf_ref); # must end on CRLF
110 341 $$buf_ref = $carry; # restore unterminated last line
111 }
112 }
113 30466 return 1;
114 }
115  
116 344 ▶sub CLOSE {
Subroutine CLOSE: called CC=4 SCAR=13.9
117 344 my $self = shift;
118 344 my $buf_ref = $self->{buf_ref};
119  
120 344 ▶ if ( $self->{in_header} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'in_header'}) { } 6 338 344
121 6 ▶ if ( $$buf_ref ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buf_ref ne "") 5 1 6
122  
123 # A line of header text ending CRLF would not have been
124 # processed yet since before we couldn't tell if it was
125 # the complete header. Now that we're in CLOSE, we can
126 # finish the header...
127 5 $$buf_ref =~ s/\015\012\z//s;
128 5 $self->add_header("$$buf_ref\015\012");
129 }
130 6 $self->finish_header;
131 6 $self->{in_header} = 0;
132 }
133 else {
134 338 ▶ if ( $$buf_ref ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buf_ref ne "") 0 338 338
135 0 $self->add_body($$buf_ref);
136 }
137 }
138 344 $$buf_ref = '';
139 344 $self->finish_body;
140 344 return 1;
141 }
142  
143 0 ▶sub add_header {
Subroutine add_header: not called CC=1 SCAR=6.9
Pod undocumented
144 0 die 'add_header not implemented';
145 }
146  
147 0 ▶sub finish_header {
Subroutine finish_header: not called CC=1 SCAR=6.9
Pod undocumented
148 0 die 'finish_header not implemented';
149 }
150  
151 0 ▶sub add_body {
Subroutine add_body: not called CC=1 SCAR=6.9
Pod undocumented
152 0 die 'add_body not implemented';
153 }
154  
155 0 ▶sub finish_body {
Subroutine finish_body: not called CC=1 SCAR=0.0
Pod undocumented
156  
157 # do nothing by default
158 }
159  
160 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod undocumented
161 0 carp 'reset not implemented';
162 }
163  
164 19 1;
165  
166 __END__
« DKIM/Canonicalization/Base.pm ↑ Summary DKIM/KeyValueList.pm »