lib/Mail/DKIM2/BodyPlan.pm

filter: Statementstmt 52.7% 201 / 381 Branchbran 43.8% 79 / 180 Conditioncond 39.6% 23 / 58 MC/DCmcdc 27.7% 10 / 36 Subroutinesub 62.5% 20 / 32 Podpod 100% 12 / 12 totaltotal 49.3% 345 / 699 CC 137 SCAR 35.4
CC
137
Cov
49%
_block
83.9
_block
83.9
finish
38.5
CRAP 34.3
« lib/Mail/DKIM2/MessageInstance.pm ↑ Summary lib/Mail/DKIM2/RecipeJSON.pm »
1 package Mail::DKIM2::BodyPlan;
2 182 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 182 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 182 our $VERSION = '0.33';
6  
7 182 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 182 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 182 ▶use Mail::DKIM2::Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 182 ▶use Mail::DKIM2::MessageInstance;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # Every Message-Instance's body hash in one forward pass over the current
13 # body, without rebuilding any previous body.
14 #
15 # Each instance's body is described as a list of segments, each a run of
16 # lines of the current body ([0, from, to], line numbers) or of Recipe
17 # literals ([1, from, to], indexes into one literal table). The top
18 # instance is the whole current body; each instance below is its parent's
19 # list with the parent's body Recipe applied to it -- a copy range picks
20 # lines out of the parent's segments, a literal adds a literal segment.
21 # Copy ranges ascend (-09 §5.2), so every list is in body order, and the
22 # body is read once, front to back, each line going to every instance whose
23 # list holds it. Instances with the same list (no body Recipe: an unchanged
24 # body) share one set of digests.
25  
26 # The top instance's body: every line, however many there turn out to be.
27 182 ▶use constant ALL_LINES => 2**52;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Lines per regex jump when finding a line's offset in a block: under perl's
30 # 32766 limit on a counted quantifier.
31 182 ▶use constant JUMP => 30000;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # new(@mi): the Message-Instance objects, highest m= first, each the parent
34 # of the next.
35 1346 ▶sub new {
Subroutine new: called CC=11 SCAR=34.1
Pod documented
36 0 ▶ my ($class, @mi) = @_;
Subroutine new: not called CC=11 SCAR=34.1
Pod documented
37 0 my $self = bless {
38 instances => [],
39 by_m => {},
40 groups => [],
41 literals => [],
42 line => 1, # number of the next body line
43 carry => '', # a line not yet ended, from the last chunk
44 }, $class;
45  
46 0 my %group_for; # segment list (as a string) => group
47 0 my $impl = Mail::DKIM2::MessageInstance::hash_algs();
48 0 my $segs = [[0, 1, ALL_LINES]];
49 0 for my $mi (@mi) {
50 0 ▶ my $hashes = $mi->get_tag('hashes') || {};
Condition25%1/4
$mi->get_tag('hashes') || {}
l!l
24460
$mi->get_tag('hashes') || {}
l!l
00
MC/DC0%0/1
$mi->get_tag('hashes') || {}
$mi->get_tag('hashes')
MC/DC0%0/1
$mi->get_tag('hashes') || {}
$mi->get_tag('hashes')
Truth table50%1/2
A
A= $mi->get_tag('hashes')
Aresult
11
00
Truth table0%0/2
A
A= $mi->get_tag('hashes')
Aresult
11
00
51 my $inst = {
52 m => 0 + $mi->get_tag('m'), # m=01 is 1
53 rb => $mi->get_tag('rb'),
54 null => $mi->unrecoverable,
55 0 algs => [ sort grep { $impl->{$_} } keys %$hashes ],
56 };
57 0 ▶ if ($segs) {
Branch: 2 branches
BranchTrueFalseTotal
if ($segs) 2392 54 2446
if ($segs) 0 0 0
58 0 my $key = join ';', map { join ',', @$_ } @$segs;
59 0 ▶ my $g = $group_for{$key} //= do {
Condition33%2/6
$group_for{$key} //= do { push @{$$self{"groups"};}, _new_group($segs); $self->{"groups"}[-1] }
l!l&&r!l&&!r
15822340
$group_for{$key} //= do { push @{$$self{"groups"};}, _new_group($segs); $self->{"groups"}[-1] }
l!l&&r!l&&!r
000
MC/DC0%0/2
$group_for{$key} //= do { push @{$$self{"groups"};}, _new_group($segs); $self->{"groups"}[-1] }
$group_for{$key} do { push @{$$self{"groups"};}, _new_group($segs); $self->{"groups"}[-1] }
MC/DC0%0/2
$group_for{$key} //= do { push @{$$self{"groups"};}, _new_group($segs); $self->{"groups"}[-1] }
$group_for{$key} do { push @{$$self{"groups"};}, _new_group($segs); $self->{"groups"}[-1] }
Truth table66%2/3
A //= B
A= $group_for{$key}B= do { push @{$$self{"groups"};}, _new_group($segs); $self->{"groups"}[-1] }
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $group_for{$key}B= do { push @{$$self{"groups"};}, _new_group($segs); $self->{"groups"}[-1] }
ABresult
1X1
011
000
60 0 push @{$self->{groups}}, _new_group($segs);
61 0 $self->{groups}[-1];
62 };
63 0 ▶ $g->{digest}{$_} //= Mail::DKIM2::Digest->new($_) for @{$inst->{algs}};
Condition33%2/6
$g->{"digest"}{$_} //= "Mail::DKIM2::Digest"->new($_)
l!l&&r!l&&!r
18233430
$g->{"digest"}{$_} //= "Mail::DKIM2::Digest"->new($_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$g->{"digest"}{$_} //= "Mail::DKIM2::Digest"->new($_)
$g->{"digest"}{$_} "Mail::DKIM2::Digest"->new($_)
MC/DC0%0/2
$g->{"digest"}{$_} //= "Mail::DKIM2::Digest"->new($_)
$g->{"digest"}{$_} "Mail::DKIM2::Digest"->new($_)
Truth table66%2/3
A //= B
A= $g->{"digest"}{$_}B= "Mail::DKIM2::Digest"->new($_)
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $g->{"digest"}{$_}B= "Mail::DKIM2::Digest"->new($_)
ABresult
1X1
011
000
64 0 $inst->{group} = $g;
65 }
66 0 push @{$self->{instances}}, $inst;
67 0 $self->{by_m}{$inst->{m}} = $inst;
68  
69 # The next instance down: the same body, none, or this Recipe's.
70 0 ▶ if (!$segs || $inst->{null}) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $segs or $inst->{'null'}) { } 110 2336 2446
elsif (defined $inst->{'rb'}) { } 999 1337 2336
if (not $segs or $inst->{'null'}) { } 0 0 0
elsif (defined $inst->{'rb'}) { } 0 0 0
Condition50%3/6
not $segs or $inst->{"null"}
l!l&&r!l&&!r
54562336
not $segs or $inst->{"null"}
l!l&&r!l&&!r
000
MC/DC100%2/2
not $segs or $inst->{"null"}
not $segs $inst->{"null"}
MC/DC0%0/2
not $segs or $inst->{"null"}
not $segs $inst->{"null"}
Truth table100%3/3
not(A) or B
A= not $segsB= $inst->{"null"}
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not $segsB= $inst->{"null"}
ABresult
1X1
011
000
71 0 $segs = undef;
72 } elsif (defined $inst->{rb}) {
73 0 my $ok = eval { Mail::DKIM2::MessageInstance::_check_recipe('body', $inst->{rb}); 1 };
74 0 ▶ $segs = $ok ? compose($segs, $inst->{rb}, $self->{literals}) : undef;
Branch: 2 branches
BranchTrueFalseTotal
$ok ? : 999 0 999
$ok ? : 0 0 0
75 }
76 }
77 0 return $self;
78 }
79  
80 2234 ▶sub _new_group {
Subroutine _new_group: called CC=1 SCAR=1.2
81 0 ▶ my ($segs) = @_;
Subroutine _new_group: not called CC=1 SCAR=1.2
82 return {
83 0 segs => $segs,
84 next => 0, # index of the next segment to emit
85 digest => {},
86 pending => 0, # blank lines not yet hashed: they may be trailing
87 lines => 0, # lines in the body so far, blank or not
88 };
89 }
90  
91 # compose($segs, $recipe, \@literals): the segment list for the lines (or
92 # fields) $recipe rebuilds from those $segs lists, its literals appended to
93 # @literals. The copy ranges are known to ascend; one past the end of $segs
94 # is cut short here, for the caller to report against the real length.
95 # Mail::DKIM2::HeaderPlan uses it for each field name's values.
96 2610 ▶sub compose {
Subroutine compose: called CC=7 SCAR=26.2
Pod documented
97 0 ▶ my ($segs, $recipe, $literals) = @_;
Subroutine compose: not called CC=7 SCAR=26.2
Pod documented
98 0 my @out;
99 0 my ($i, $before) = (0, 0); # $segs->[$i] starts at line $before + 1
100 0 for my $step (@$recipe) {
101 0 ▶ unless (ref $step) {
Branch: 2 branches
BranchTrueFalseTotal
unless (ref $step) 3249 1827 5076
unless (ref $step) 0 0 0
102 0 push @$literals, $step;
103 0 _append(\@out, 1, $#$literals, $#$literals);
104 0 next;
105 }
106 0 my ($from, $to) = @$step;
107 0 ▶ while ($from <= $to && $i < @$segs) {
Branch: 2 branches
BranchTrueFalseTotal
if ($from <= $to and $i < @$segs) 2629 1827 4456
if ($from <= $to and $i < @$segs) 0 0 0
Condition33%2/6
$from <= $to and $i < @$segs
!ll&&!rl&&r
182702629
$from <= $to and $i < @$segs
!ll&&!rl&&r
000
MC/DC50%1/2
$from <= $to and $i < @$segs
$from <= $to $i < @$segs
MC/DC0%0/2
$from <= $to and $i < @$segs
$from <= $to $i < @$segs
Truth table66%2/3
A and B
A= $from <= $toB= $i < @$segs
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $from <= $toB= $i < @$segs
ABresult
0X0
100
111
108 0 my ($type, $s, $e) = @{$segs->[$i]};
109 0 my $len = $e - $s + 1;
110 0 ▶ if ($from > $before + $len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($from > $before + $len) 454 2175 2629
if ($from > $before + $len) 0 0 0
111 0 $before += $len;
112 0 $i++;
113 0 next;
114 }
115 0 ▶ my $last = $to < $before + $len ? $to : $before + $len;
Branch: 2 branches
BranchTrueFalseTotal
$to < $before + $len ? : 1380 795 2175
$to < $before + $len ? : 0 0 0
116 0 _append(\@out, $type, $s + $from - $before - 1, $s + $last - $before - 1);
117 0 $from = $last + 1;
118 }
119 }
120 0 return \@out;
121 }
122  
123 5424 ▶sub _append {
Subroutine _append: called CC=4 SCAR=17.9
124 0 ▶ my ($out, $type, $s, $e) = @_;
Subroutine _append: not called CC=4 SCAR=17.9
125 0 my $prev = $out->[-1];
126 0 ▶ if ($prev && $prev->[0] == $type && $prev->[2] + 1 == $s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($prev and $prev->[0] == $type and $prev->[2] + 1 == $s) { } 1557 3867 5424
if ($prev and $prev->[0] == $type and $prev->[2] + 1 == $s) { } 0 0 0
Condition50%6/12
$prev and $prev->[0] == $type and $prev->[2] + 1 == $s
!ll&&!rl&&r
37061611557
$prev and $prev->[0] == $type
!ll&&!rl&&r
162020861718
$prev and $prev->[0] == $type and $prev->[2] + 1 == $s
!ll&&!rl&&r
000
$prev and $prev->[0] == $type
!ll&&!rl&&r
000
MC/DC100%3/3
$prev and $prev->[0] == $type and $prev->[2] + 1 == $s
$prev $prev->[0] == $type $prev->[2] + 1 == $s
MC/DC0%0/3
$prev and $prev->[0] == $type and $prev->[2] + 1 == $s
$prev $prev->[0] == $type $prev->[2] + 1 == $s
Truth table100%4/4
A and B and C
A= $prevB= $prev->[0] == $typeC= $prev->[2] + 1 == $s
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $prevB= $prev->[0] == $typeC= $prev->[2] + 1 == $s
ABCresult
0XX0
10X0
1100
1111
127 0 $prev->[2] = $e;
128 } else {
129 0 push @$out, [$type, $s, $e];
130 }
131 }
132  
133 # add($bytes): the next piece of the current body, cut anywhere.
134 1658 ▶sub add {
Subroutine add: called CC=7 SCAR=27.9
Pod documented
135 0 ▶ my $self = shift;
Subroutine add: not called CC=7 SCAR=27.9
Pod documented
136 0 ▶ croak "BodyPlan: add after finish" if $self->{done};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"done"} 0 1658 1658
if $self->{"done"} 0 0 0
137 0 ▶ return unless defined $_[0] && length $_[0];
Branch: 2 branches
BranchTrueFalseTotal
unless defined $_[0] and length $_[0] 0 1658 1658
unless defined $_[0] and length $_[0] 0 0 0
Condition16%1/6
defined $_[0] and length $_[0]
!ll&&!rl&&r
001658
defined $_[0] and length $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0] and length $_[0]
defined $_[0] length $_[0]
MC/DC0%0/2
defined $_[0] and length $_[0]
defined $_[0] length $_[0]
Truth table33%1/3
A and B
A= defined $_[0]B= length $_[0]
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $_[0]B= length $_[0]
ABresult
0X0
100
111
138 0 my $nl = rindex($_[0], "\n");
139 0 ▶ if ($nl < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nl < 0) 44 1614 1658
if ($nl < 0) 0 0 0
140 0 $self->{carry} .= $_[0];
141 0 return;
142 }
143 # The whole lines, read where they are when nothing is carried over and
144 # the piece ends at a line end (a whole body does): copied otherwise.
145 0 ▶ if (!length $self->{carry} && $nl == length($_[0]) - 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (not length $self->{"carry"} and $nl == length($_[0]) - 1) 1311 303 1614
if (not length $self->{"carry"} and $nl == length($_[0]) - 1) 0 0 0
Condition50%3/6
not length $self->{"carry"} and $nl == length($_[0]) - 1
!ll&&!rl&&r
234691311
not length $self->{"carry"} and $nl == length($_[0]) - 1
!ll&&!rl&&r
000
MC/DC100%2/2
not length $self->{"carry"} and $nl == length($_[0]) - 1
not length $self->{"carry"} $nl == length($_[0]) - 1
MC/DC0%0/2
not length $self->{"carry"} and $nl == length($_[0]) - 1
not length $self->{"carry"} $nl == length($_[0]) - 1
Truth table100%3/3
not(A) and B
A= not length $self->{"carry"}B= $nl == length($_[0]) - 1
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not length $self->{"carry"}B= $nl == length($_[0]) - 1
ABresult
0X0
100
111
146 0 $self->_block(\$_[0]);
147 0 return;
148 }
149 0 my $block = $self->{carry} . substr($_[0], 0, $nl + 1);
150 0 $self->{carry} = substr($_[0], $nl + 1);
151 0 $self->_block(\$block);
152 }
153  
154 # A run of whole lines, each ending in a LF. Hashed as CRLF lines: a bare LF
155 # ends a line like a CRLF does, as in the Recipes' line numbering.
156 # The block may be the caller's own string: it is never changed in place.
157 1615 ▶sub _block {
Subroutine _block: called CC=20 SCAR=44.3
158 0 ▶ my ($self, $bref) = @_;
Subroutine _block: not called CC=20 SCAR=44.3
159 0 ▶ if ($$bref =~ /(?<!\r)\n/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$bref =~ /(?<!\r)\n/) 0 1615 1615
if ($$bref =~ /(?<!\r)\n/) 0 0 0
160 0 (my $copy = $$bref) =~ s/\r\n/\n/g;
161 0 $copy =~ s/\n/\r\n/g;
162 0 $bref = \$copy;
163 }
164 0 my $first = $self->{line};
165 0 my $last = $first + ($$bref =~ tr/\n//) - 1;
166 0 $self->{line} = $last + 1;
167  
168 # Each group's part of this block: spans of lines and literal segments.
169 0 my (@work, %need);
170 0 for my $g (@{$self->{groups}}) {
171 0 my $segs = $g->{segs};
172 0 my @ops;
173 0 ▶ while ($g->{next} < @$segs) {
Branch: 2 branches
BranchTrueFalseTotal
if ($g->{"next"} < @$segs) 4795 1007 5802
if ($g->{"next"} < @$segs) 0 0 0
174 0 my ($type, $s, $e) = @{$segs->[$g->{next}]};
175 0 ▶ if ($type) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type) 1467 3328 4795
if ($type) 0 0 0
176 0 push @ops, [1, $s, $e];
177 0 $g->{next}++;
178 0 next;
179 }
180 0 ▶ last if $s > $last;
Branch: 2 branches
BranchTrueFalseTotal
if $s > $last 23 3305 3328
if $s > $last 0 0 0
181 0 ▶ my $from = $s > $first ? $s : $first;
Branch: 2 branches
BranchTrueFalseTotal
$s > $first ? : 1201 2104 3305
$s > $first ? : 0 0 0
182 0 ▶ my $to = $e < $last ? $e : $last;
Branch: 2 branches
BranchTrueFalseTotal
$e < $last ? : 1739 1566 3305
$e < $last ? : 0 0 0
183 0 ▶ if ($from <= $to) {
Branch: 2 branches
BranchTrueFalseTotal
if ($from <= $to) 3305 0 3305
if ($from <= $to) 0 0 0
184 0 push @ops, [0, $from, $to];
185 0 @need{$from, $to + 1} = ();
186 }
187 0 ▶ last if $e > $last;
Branch: 2 branches
BranchTrueFalseTotal
if $e > $last 1548 1757 3305
if $e > $last 0 0 0
188 0 $g->{next}++;
189 }
190 0 ▶ push @work, [$g, \@ops] if @ops;
Branch: 2 branches
BranchTrueFalseTotal
if @ops 2549 29 2578
if @ops 0 0 0
191 }
192 0 ▶ return unless @work;
Branch: 2 branches
BranchTrueFalseTotal
unless @work 100 1515 1615
unless @work 0 0 0
193  
194 # Byte offset of each line wanted, in one walk down the block.
195 0 my %off;
196 0 my ($line, $pos) = ($first, 0);
197 0 for my $want (sort { $a <=> $b } keys %need) {
198 0 ▶ for (my $k = $want - $line; $k > 0; ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k > 0) 3959 5474 9433
if ($k > 0) 0 0 0
199 0 ▶ my $j = $k > JUMP ? JUMP : $k;
Branch: 2 branches
BranchTrueFalseTotal
$k > 30000 ? : 0 3959 3959
$k > 30000 ? : 0 0 0
200 0 pos($$bref) = $pos;
201 0 ▶ $$bref =~ /\G(?:[^\n]*\n){$j}/g
Branch: 2 branches
BranchTrueFalseTotal
$$bref =~ /\G(?:[^\n]*\n){$j}/g or croak("BodyPlan: line $want not in block") 3959 0 3959
$$bref =~ /\G(?:[^\n]*\n){$j}/g or croak("BodyPlan: line $want not in block") 0 0 0
202 or croak "BodyPlan: line $want not in block";
203 0 $pos = pos($$bref);
204 0 $k -= $j;
205 }
206 0 $line = $want;
207 0 $off{$want} = $pos;
208 }
209  
210 0 for my $w (@work) {
211 0 my ($g, $ops) = @$w;
212 0 for my $op (@$ops) {
213 0 my ($type, $s, $e) = @$op;
214 0 ▶ if ($type) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type) { } 1467 3305 4772
if ($type) { } 0 0 0
215 0 _literal($g, $self->{literals}[$_]) for $s .. $e;
216 } else {
217 0 _span($g, $bref, $off{$s}, $off{$e + 1}, $e - $s + 1);
218 }
219 }
220 }
221 }
222  
223 # The $n whole CRLF lines of $$bref from $from to $to into $g, in one piece.
224 # Blank lines are held back until a non-blank line follows them (trailing
225 # blank lines are not hashed, -09 §6.1): those at the end of the run are
226 # found by looking back from its end, and counted, not hashed.
227 3305 ▶sub _span {
Subroutine _span: called CC=5 SCAR=20.9
228 0 ▶ my ($g, $bref, $from, $to, $n) = @_;
Subroutine _span: not called CC=5 SCAR=20.9
229 0 $g->{lines} += $n;
230 0 ▶ if ($to - $from == 2 * $n) {
Branch: 2 branches
BranchTrueFalseTotal
if ($to - $from == 2 * $n) 504 2801 3305
if ($to - $from == 2 * $n) 0 0 0
231 0 $g->{pending} += $n;
232 0 return;
233 }
234 0 my ($end, $blank) = ($to, 0);
235 0 ▶ while ($end - $from >= 4 && substr($$bref, $end - 4, 4) eq "\r\n\r\n") {
Branch: 2 branches
BranchTrueFalseTotal
if ($end - $from >= 4 and substr($$bref, $end - 4, 4) eq "\r\n\r\n") 1101 2801 3902
if ($end - $from >= 4 and substr($$bref, $end - 4, 4) eq "\r\n\r\n") 0 0 0
Condition50%3/6
$end - $from >= 4 and substr($$bref, $end - 4, 4) eq "\r\n\r\n"
!ll&&!rl&&r
71820831101
$end - $from >= 4 and substr($$bref, $end - 4, 4) eq "\r\n\r\n"
!ll&&!rl&&r
000
MC/DC100%2/2
$end - $from >= 4 and substr($$bref, $end - 4, 4) eq "\r\n\r\n"
$end - $from >= 4 substr($$bref, $end - 4, 4) eq "\r\n\r\n"
MC/DC0%0/2
$end - $from >= 4 and substr($$bref, $end - 4, 4) eq "\r\n\r\n"
$end - $from >= 4 substr($$bref, $end - 4, 4) eq "\r\n\r\n"
Truth table100%3/3
A and B
A= $end - $from >= 4B= substr($$bref, $end - 4, 4) eq "\r\n\r\n"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $end - $from >= 4B= substr($$bref, $end - 4, 4) eq "\r\n\r\n"
ABresult
0X0
100
111
236 0 $end -= 2;
237 0 $blank++;
238 }
239 0 ▶ _emit($g, "\r\n" x $g->{pending}) if $g->{pending};
Branch: 2 branches
BranchTrueFalseTotal
if $g->{"pending"} 536 2265 2801
if $g->{"pending"} 0 0 0
240 0 _emit($g, substr($$bref, $from, $end - $from));
241 0 $g->{pending} = $blank;
242 }
243  
244 3324 ▶sub _literal {
Subroutine _literal: called CC=2 SCAR=9.2
245 0 ▶ my ($g, $text) = @_;
Subroutine _literal: not called CC=2 SCAR=9.2
246 0 $g->{lines}++;
247 0 ▶ if ($text eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($text eq "") 747 2577 3324
if ($text eq "") 0 0 0
248 0 $g->{pending}++;
249 0 return;
250 }
251 0 _emit($g, ("\r\n" x $g->{pending}) . "$text\r\n");
252 0 $g->{pending} = 0;
253 }
254  
255 5921 ▶sub _emit {
Subroutine _emit: called CC=2 SCAR=9.2
256 0 ▶ my ($g) = @_;
Subroutine _emit: not called CC=2 SCAR=9.2
257 0 $_->add($_[1]) for values %{$g->{digest}};
258 0 $g->{emitted} = 1;
259 }
260  
261 # finish(): the body has all been added. Hashes the literals after the last
262 # body line, and checks each body Recipe's copy ranges against the length of
263 # the body it applies to.
264 1184 ▶sub finish {
Subroutine finish: called CC=14 SCAR=36.5
Pod documented
265 0 ▶ my ($self) = @_;
Subroutine finish: not called CC=14 SCAR=36.5
Pod documented
266 0 ▶ return if $self->{done};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"done"} 7 1177 1184
if $self->{"done"} 0 0 0
267 0 ▶ if (length $self->{carry}) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $self->{"carry"}) 1 1176 1177
if (length $self->{"carry"}) 0 0 0
268 0 my $last = delete($self->{carry}) . "\r\n";
269 0 $self->_block(\$last);
270 }
271 0 for my $g (@{$self->{groups}}) {
272 0 my $segs = $g->{segs};
273 0 ▶ for (; $g->{next} < @$segs; $g->{next}++) {
Branch: 2 branches
BranchTrueFalseTotal
if ($g->{"next"} < @$segs) 1177 2120 3297
if ($g->{"next"} < @$segs) 0 0 0
274 0 my ($type, $s, $e) = @{$segs->[$g->{next}]};
275 0 ▶ next unless $type;
Branch: 2 branches
BranchTrueFalseTotal
unless $type 1176 1 1177
unless $type 0 0 0
276 0 _literal($g, $self->{literals}[$_]) for $s .. $e;
277 }
278 0 ▶ _emit($g, "\r\n") unless $g->{emitted};
Branch: 2 branches
BranchTrueFalseTotal
unless $g->{"emitted"} 7 2113 2120
unless $g->{"emitted"} 0 0 0
279 $g->{hash}{$_} = encode_base64($g->{digest}{$_}->digest, '')
280 0 for keys %{$g->{digest}};
281 }
282  
283 # The undo walk's verdicts, in its words: the first bad body Recipe
284 # ends the walk, and no instance below it has a body.
285 0 my $broken;
286 0 for my $inst (@{$self->{instances}}) {
287 0 ▶ delete $inst->{group} if $broken;
Branch: 2 branches
BranchTrueFalseTotal
if $broken 4 2279 2283
if $broken 0 0 0
288 0 ▶ next unless defined $inst->{rb};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $inst->{"rb"} 1323 960 2283
unless defined $inst->{"rb"} 0 0 0
289 0 ▶ my $lines = $inst->{group} ? $inst->{group}{lines} : undef;
Branch: 2 branches
BranchTrueFalseTotal
$inst->{'group'} ? : 959 1 960
$inst->{'group'} ? : 0 0 0
290 0 eval { Mail::DKIM2::MessageInstance::_check_recipe('body', $inst->{rb}, $lines); 1 }
291 0 ▶ or ($inst->{error} = $@, $broken = 1);
Branch: 2 branches
BranchTrueFalseTotal
eval { do { Mail::DKIM2::MessageInstance::_check_recipe("body", $inst->{"rb"}, $lines); 1 } } or $inst->{"error"} = $@, $broken = 1 956 4 960
eval { do { Mail::DKIM2::MessageInstance::_check_recipe("body", $inst->{"rb"}, $lines); 1 } } or $inst->{"error"} = $@, $broken = 1 0 0 0
292 }
293 0 $self->{done} = 1;
294 0 return;
295 }
296  
297 # body_hash($m, $alg): the base64 body hash of instance m= by $alg, or undef
298 # if that instance's body cannot be rebuilt or it has no $alg hash-set.
299 3289 ▶sub body_hash {
Subroutine body_hash: called CC=4 SCAR=19.9
Pod documented
300 0 ▶ my ($self, $m, $alg) = @_;
Subroutine body_hash: not called CC=4 SCAR=19.9
Pod documented
301 0 ▶ croak "BodyPlan: body_hash before finish" unless $self->{done};
Branch: 2 branches
BranchTrueFalseTotal
unless $self->{"done"} 0 3289 3289
unless $self->{"done"} 0 0 0
302 0 ▶ my $inst = $self->{by_m}{0 + $m} or return undef;
Branch: 2 branches
BranchTrueFalseTotal
my $inst = $self->{"by_m"}{0 + $m} or return undef 3289 0 3289
my $inst = $self->{"by_m"}{0 + $m} or return undef 0 0 0
303 0 ▶ my $g = $inst->{group} or return undef;
Branch: 2 branches
BranchTrueFalseTotal
my $g = $inst->{"group"} or return undef 3283 6 3289
my $g = $inst->{"group"} or return undef 0 0 0
304 0 return $g->{hash}{lc $alg};
305 }
306  
307 # recipe_error($m): why instance m='s body Recipe does not undo, or undef.
308 1451 ▶sub recipe_error {
Subroutine recipe_error: called CC=3 SCAR=16.5
Pod documented
309 0 ▶ my ($self, $m) = @_;
Subroutine recipe_error: not called CC=3 SCAR=16.5
Pod documented
310 0 ▶ croak "BodyPlan: recipe_error before finish" unless $self->{done};
Branch: 2 branches
BranchTrueFalseTotal
unless $self->{"done"} 0 1451 1451
unless $self->{"done"} 0 0 0
311 0 ▶ my $inst = $self->{by_m}{0 + $m} or return undef;
Branch: 2 branches
BranchTrueFalseTotal
my $inst = $self->{"by_m"}{0 + $m} or return undef 1451 0 1451
my $inst = $self->{"by_m"}{0 + $m} or return undef 0 0 0
312 0 return $inst->{error};
313 }
314  
315 182 1;
316  
317 __END__
« lib/Mail/DKIM2/MessageInstance.pm ↑ Summary lib/Mail/DKIM2/RecipeJSON.pm »