IMP/Cascade.pm

filter: Statementstmt 68.3% 293 / 429 Branchbran 49.3% 161 / 326 Conditioncond 64.6% 64 / 99 MC/DCmcdc 50.9% 28 / 55 Subroutinesub 82.7% 24 / 29 Podpod 100% 4 / 4 totaltotal 60.9% 574 / 942 CC 212 SCAR 54.2
CC
212
Cov
61%
__ANON__
707.9
get_interface
306.0
__ANON__
73.8
CRAP 225.4
↑ Summary IMP/ProtocolPinning.pm »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶no if $] >= 5.017011, warnings => 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 ############################################################################
6 # BEWARE! complex stuff!
7 # to aid with debugging problems:
8 # - switch on debug mode
9 # - to see whats going on in direction dir part p:
10 # grep for '[dir][p]'
11 # - to see whats transfering out of direction dir part p into next part/up:
12 # grep for '[dir][p>'
13 #
14 # basic design
15 # - we do not have lots of member variables, instead we put everything into
16 # new_analyzer as normal variables and declare various $sub = sub ... which
17 # use these variables. Thus the variables get bound once to the sub and we
18 # don't need to access it with $self->{field}... or so all the time
19 # - subs and data structures are described in new_analyzer, the most important
20 # are
21 # - $global_in - this is the sub data method
22 # - $part_in - called from global_in, itself and callbacks to put data
23 # into a specific part. Feeds the data in the associated analyzer
24 # - $imp_callback - callback for the analyzer of a specific part
25  
26 ############################################################################
27  
28 package Net::IMP::Cascade;
29 4 ▶use base 'Net::IMP::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 use fields (
31 4 'parts', # analyzer objects
32 # we do everything with closures inside new_analyzer here, so that the
33 # object has only fields for accessing some closures from subs
34 'dataf', # called from sub data
35 'closef', # called from DESTROY
36 4 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 4 ▶use Net::IMP; # constants
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use Hash::Util qw(lock_ref_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 4 ▶use Net::IMP::Debug;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 4 my %rtypes_implemented_myself = map { $_ => 1 } (
46 IMP_PASS,
47 IMP_PREPASS,
48 IMP_REPLACE,
49 IMP_REPLACE_LATER,
50 IMP_DENY,
51 IMP_DROP,
52 #IMP_TOSENDER, # not supported yet
53 IMP_LOG,
54 IMP_ACCTFIELD,
55 IMP_FATAL,
56 );
57  
58 sub get_interface {
59 0 ▶ my Net::IMP::Cascade $factory = shift;
Subroutine get_interface: not called CC=17 SCAR=57.2
Pod documented
60 0 my $parts = $factory->{factory_args}{parts};
61  
62 # collect interfaces by part
63 0 my @if4part;
64 0 for my $p ( @$parts ) {
65 0 my @if;
66 0 for my $if ( $p->get_interface(@_)) {
67 # $if should require only return types I support
68 push @if,$if
69 0 ▶ if ! grep { ! $rtypes_implemented_myself{$_} } @{ $if->[1] };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {not $rtypes_implemented_myself{$_};} @{$$if[1];} 0 0 0
70 }
71 0 ▶ @if or return; # nothing in common
Branch: 1 branch
BranchTrueFalseTotal
@if or return 0 0 0
72 0 push @if4part,\@if
73 }
74  
75 # find interfaces which are supported by all parts
76 0 my @common;
77 0 ▶ for( my $i=0;$i<@if4part;$i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @if4part) 0 0 0
78 0 for my $if_i ( @{ $if4part[$i] } ) {
79 0 my ($in_i,$out_i) = @$if_i;
80 # check if $if_i matches at least on interface description in
81 # all other parts, e.g. $if_i is same or included in $if_k
82 # - data type/proto: $in_k should be undef or same as $in_i
83 # - return types: $out_i should include $out_k
84 0 ▶ for( my $k=0;$k<@if4part;$k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < @if4part) 0 0 0
85 0 ▶ next if $i == $k; # same
Branch: 1 branch
BranchTrueFalseTotal
if $i == $k 0 0 0
86 0 for my $if_k ( @{ $if4part[$k] } ) {
87 0 my ($in_k,$out_k) = @$if_k;
88 # should be same data type or $in_k undef
89 0 ▶ next if $in_k and ( ! $in_i or $in_k != $in_i );
Branch: 1 branch
BranchTrueFalseTotal
if $in_k and !$in_i || $in_k != $in_i 0 0 0
Condition0%0/6
$in_k and !$in_i || $in_k != $in_i
!ll&&!rl&&r
000
not $in_i or $in_k != $in_i
l!l&&r!l&&!r
000
MC/DC0%0/3
$in_k and !$in_i || $in_k != $in_i
$in_k not $in_i $in_k != $in_i
Truth table0%0/4
A and not(B) or C
A= $in_kB= not $in_iC= $in_k != $in_i
ABCresult
0XX0
1000
11X1
1011
90 # $out_i should include all of $out_k
91 0 my %out_k = map { $_ => 1 } @$out_k;
92 0 delete @out_k{ @$out_i };
93 0 ▶ next if %out_k; # some in k are not in i
Branch: 1 branch
BranchTrueFalseTotal
if %out_k 0 0 0
94  
95 # junction if i and k
96 0 push @common,[ $in_k,$out_i ];
97 }
98 }
99 }
100 }
101  
102 # remove duplicates from match
103 0 my (@uniq,%m);
104 0 for( @common ) {
105 0 ▶ my $key = ( $_->[0] // '<undef>' )."\0".join("\0",sort @{$_->[1]});
Condition0%0/2
$_->[0] // "<undef>"
l!l
00
MC/DC0%0/1
$_->[0] // "<undef>"
$_->[0]
Truth table0%0/2
A
A= $_->[0]
Aresult
11
00
106 0 ▶ push @uniq,$_ if ! $m{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $m{$key}++ 0 0 0
107 }
108 0 return @uniq;
109 }
110  
111 sub set_interface {
112 0 ▶ my Net::IMP::Cascade $factory = shift;
Subroutine set_interface: not called CC=7 SCAR=40.3
Pod documented
113 0 my @if = @_;
114 0 my $parts = $factory->{factory_args}{parts};
115  
116 0 my @new_parts;
117 0 ▶ for(my $i=0;$i<@$parts;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$parts) 0 0 0
118 0 ▶ my $np = $parts->[$i]->set_interface(@if)
Branch: 1 branch
BranchTrueFalseTotal
my $np = $parts->[$i]->set_interface(@if) or return 0 0 0
119 or return; # cannot use interface
120 0 ▶ $np == $parts->[$i] and next; # no change of part
Branch: 1 branch
BranchTrueFalseTotal
$np == $parts->[$i] and next 0 0 0
121 0 $new_parts[$i] = $np; # got new factory for part
122 }
123  
124 0 ▶ return $factory if ! @new_parts; # interface supported by original factory
Branch: 1 branch
BranchTrueFalseTotal
unless @new_parts 0 0 0
125  
126 # some parts changed, create new factory for this cascade
127 0 ▶ for(my $i=0;$i<@$parts;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$parts) 0 0 0
128 0 ▶ $new_parts[$i] ||= $parts->[$i]; # copy parts which did not change
Condition0%0/3
$new_parts[$i] ||= $parts->[$i]
l!l&&r!l&&!r
000
MC/DC0%0/2
$new_parts[$i] ||= $parts->[$i]
$new_parts[$i] $parts->[$i]
Truth table0%0/3
A ||= B
A= $new_parts[$i]B= $parts->[$i]
ABresult
1X1
011
000
129 }
130  
131 0 return ref($factory)->new_factory( parts => \@new_parts );
132 }
133  
134 sub new_analyzer {
135 11 ▶ my ($factory,%args) = @_;
Subroutine new_analyzer: called CC=3 SCAR=12.2
Pod documented
136  
137 11 my $p = $factory->{factory_args}{parts};
138 11 my $self = $factory->SUPER::new_analyzer(%args);
139 11 my @imp = map { $_->new_analyzer(%args) } @$p;
140  
141 # $parts[$dir][$pi] is the part for direction $dir, analyzer $pi
142 # if part is optimized away due to IMP_PASS with IMP_MAXOFFSET
143 # $parts[$dir][$pi] contains instead an integer for adjustments
144 # from this part
145 11 my @parts;
146  
147 # pause/continue handling
148 # maintains pause status per part
149 my @pause;
150  
151 # to make sure we don't leak due to cross-references
152 11 weaken( my $wself = $self );
153  
154 my $new_buf = sub {
155 160 ▶ lock_ref_keys( my $buf = {
Subroutine __ANON__: called CC=2 SCAR=6.9
156 start => 0, # start of buf relativ to part
157 end => 0, # end of buf relativ to part
158 data => '', # data or undef for replace_later
159 dtype => 0, # data type
160 rtype => IMP_PASS, # IMP_PASS < IMP_PREPASS < IMP_REPLACE
161 gap => 0, # size of gap before buf?
162 gstart => 0, # start of buf relativ to cascade
163 gend => 0, # end of buf relativ to cascade
164 eof => 0 # flag if last buf in this direction
165 });
166 160 ▶ %$buf = ( %$buf, @_ ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 118 42 160
167 160 return $buf;
168 11 };
169  
170 my $new_part = sub {
171 42 ▶ lock_ref_keys( my $p = {
Subroutine __ANON__: called CC=1 SCAR=0.0
172 ibuf => [ &$new_buf ], # buffers, at least one
173 pass => 0, # can pass up to ..
174 prepass => 0, # can prepass up to ..
175 replace_later => 0, # will replace_later up to ..
176 adjust => 0, # total local adjustments from forwarded bufs
177 });
178 42 return $p;
179 11 };
180  
181 # initialize @parts
182 11 ▶ for( my $i=0;$i<@imp;$i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @imp) 21 11 32
183 21 $parts[0][$i] = $new_part->(); # client -> server, flow 0>1>2>..
184 21 $parts[1][$#imp-$i] = $new_part->(); # server -> client, flow 9>8>7>..
185 }
186  
187 my $dump_bufs = sub {
188 0 ▶ my $bufs = shift;
Subroutine __ANON__: not called CC=8 SCAR=42.8
189 0 my @out;
190 0 ▶ for my $i (@_ ? @_: 0..$#$bufs) {
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
191 0 my $buf = $bufs->[$i];
192 0 ▶ my $str = ! defined( $buf->{data} ) ? '<undef>' : do {
Branch: 1 branch
BranchTrueFalseTotal
!defined($buf->{'data'}) ? : 0 0 0
193 0 local $_ = $buf->{data};
194 0 ▶ $_ = substr($_,0,27).'...' if length($_)>30;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > 30 0 0 0
195 0 s{([\\\n\r\t[^:print:]])}{ sprintf("\\%03o",ord($1)) }esg;
196 0 $_
197 };
198 push @out, sprintf("#%02d %d..%d%s%s%s %s %s [%d,%d] '%s'",
199 $i,
200 $buf->{start},$buf->{end}, $buf->{eof} ? '$':'',
201 $buf->{gap} ? " +$buf->{gap}":"",
202 defined($buf->{data}) ? '':' RL',
203 $buf->{dtype},$buf->{rtype},
204 $buf->{gstart},$buf->{gend},
205 0 ▶ $str
Branch: 3 branches
BranchTrueFalseTotal
$buf->{'eof'} ? : 0 0 0
$buf->{'gap'} ? : 0 0 0
defined $buf->{'data'} ? : 0 0 0
206 );
207 }
208 0 return join("\n",@out);
209 11 };
210 my $dump_parts = sub {
211 0 ▶ my $dir = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
212 0 my $out = '';
213 0 ▶ for my $pi (@_ ? @_ : 0..$#imp) {
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
214 0 my $part = $parts[$dir][$pi];
215 0 ▶ if ( ! $part ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($part) 0 0 0
216 0 $out .= "part[$dir][$pi] - skip\n";
217 0 next;
218 }
219 $out .= sprintf("part[%d][%d] p|pp|rl=%d|%d|%d ibuf:\n",
220 0 $dir,$pi,$part->{pass},$part->{prepass},$part->{replace_later});
221 0 my $ib = $part->{ibuf};
222 0 $out .= $dump_bufs->( $part->{ibuf});
223 }
224 0 return $out;
225 11 };
226  
227 my $split_buf = sub {
228 58 ▶ my ($ibuf,$i,$fwd) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
229 58 my $buf = $ibuf->[$i];
230 58 ▶ die "no split for packet types" if $buf->{dtype}>0;
Branch: 1 branch
BranchTrueFalseTotal
if $buf->{"dtype"} > 0 0 58 58
231  
232 my $buf_before = $new_buf->(
233 %$buf,
234 eof => 0,
235 end => $buf->{start} + $fwd, # adjust end
236 defined($buf->{data})
237 58 ▶ ? ( data => substr($buf->{data},0,$fwd,'') ) # real data
Branch: 1 branch
BranchTrueFalseTotal
defined $buf->{'data'} ? : 58 0 58
238 : (), # replacement promise
239 );
240 # gap in buf_before
241 58 $buf->{gap} = 0;
242 58 $buf->{start} = $buf_before->{end};
243  
244 # if buf was not changed gend..gstart should reflect the
245 # original length of the data
246 58 ▶ if ( $buf->{rtype} != IMP_REPLACE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf->{'rtype'} != 4113) { } 54 4 58
247 54 $buf_before->{gend} = ( $buf->{gstart} += $fwd );
248 } else {
249 # split gstart..gend into full|0 per convention
250 4 $buf->{gstart} = $buf->{gend};
251 }
252  
253 # put buf_before before buf in ibuf
254 58 splice(@$ibuf,$i,0,$buf_before);
255 11 };
256  
257 11 my $fwd_collect; # collect bufs which can be forwarded
258 my $fwd_up; # collect what can be passed up
259 11 my $exec_fwd; # do the collected forwarding to next part or up
260  
261 11 my $global_in; # function where data gets fed into from outside (sub data)
262 11 my $part_in; # internal feed into each part
263  
264 11 my $imp_callback; # synchronization wrapper around callback for analyzers
265 11 my $_imp_callback; # real callback for the analyzers
266  
267 # pass passable bufs in part starting with ibuf[i]
268 # returns all bufs which can be passed and strips them from part.ibuf
269 $fwd_collect = sub {
270 148 ▶ my ($dir,$pi,$i,$r_passed) = @_;
Subroutine __ANON__: called CC=23 SCAR=31.8
271 148 my $part = $parts[$dir][$pi];
272 148 my $ibuf = $part->{ibuf};
273 148 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug(qq[fwd_collect[$dir][$pi]: p=$part->{"pass"} pp=$part->{"prepass"} ] . &$dump_bufs($ibuf)) 0 148 148
274 "fwd_collect[$dir][$pi]: p=$part->{pass} pp=$part->{prepass} "
275 .$dump_bufs->($ibuf));
276 148 my @fwd;
277 148 for my $pp (qw(pass prepass)) {
278 296 ▶ my $pass = $part->{$pp} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pass = $part->{$pp} or next 225 71 296
279 71 ▶ for( ;$i<@$ibuf;$i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$ibuf) 138 4 142
280 174 my $buf = $ibuf->[$i];
281 174 ▶ last if ! $buf->{dtype}; # dummy buf
Branch: 1 branch
BranchTrueFalseTotal
unless $buf->{"dtype"} 0 174 174
282 174 ▶ if ( $pass != IMP_MAXOFFSET and $buf->{start} >= $pass ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass != -1 and $buf->{"start"} >= $pass) 43 131 174
Condition100%3/3
$pass != -1 and $buf->{"start"} >= $pass
!ll&&!rl&&r
458643
MC/DC100%2/2
$pass != -1 and $buf->{"start"} >= $pass
$pass != -1 $buf->{"start"} >= $pass
Truth table100%3/3
A and B
A= $pass != -1B= $buf->{"start"} >= $pass
ABresult
0X0
100
111
283 43 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug(qq[fwd_collect[$dir][$pi]: reset $pp due to start[$i]($buf->{"start"})>=$pp($pass)]) 0 43 43
284 "fwd_collect[$dir][$pi]: reset $pp due to start[$i]($buf->{start})>=$pp($pass)");
285 43 $part->{$pp} = 0;
286 43 last;
287 }
288 die "cannot pass bufs with replace_later"
289 131 ▶ if ! defined $buf->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buf->{"data"} 0 131 131
290 131 ▶ if ( $pass == IMP_MAXOFFSET or $buf->{end} <= $pass ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass == -1 or $buf->{'end'} <= $pass) { } 95 36 131
Condition100%3/3
$pass == -1 or $buf->{"end"} <= $pass
l!l&&r!l&&!r
455036
MC/DC100%2/2
$pass == -1 or $buf->{"end"} <= $pass
$pass == -1 $buf->{"end"} <= $pass
Truth table100%3/3
A or B
A= $pass == -1B= $buf->{"end"} <= $pass
ABresult
1X1
011
000
291 # whole buffer can be passed
292 95 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug(qq[fwd_collect[$dir][$pi]: pass whole buffer[$i] $buf->{"start"}..$buf->{"end"}]) 0 95 95
293 "fwd_collect[$dir][$pi]: pass whole buffer[$i] $buf->{start}..$buf->{end}");
294 $buf->{rtype} = IMP_PREPASS if $pp eq 'prepass'
295 95 ▶ and $buf->{rtype} == IMP_PASS;
Branch: 1 branch
BranchTrueFalseTotal
if $pp eq "prepass" and $buf->{"rtype"} == "pass" 4 91 95
Condition100%3/3
$pp eq "prepass" and $buf->{"rtype"} == "pass"
!ll&&!rl&&r
8744
MC/DC100%2/2
$pp eq "prepass" and $buf->{"rtype"} == "pass"
$pp eq "prepass" $buf->{"rtype"} == "pass"
Truth table100%3/3
A and B
A= $pp eq "prepass"B= $buf->{"rtype"} == "pass"
ABresult
0X0
100
111
296 95 push @fwd,[ $pi,$dir,$buf ];
297  
298 # r_passed is set from part_in to track position if data
299 # are passed. In case of prepass we don't pass data but
300 # only put them into fwd
301 95 ▶ next if $r_passed && $pp eq 'prepass';
Branch: 1 branch
BranchTrueFalseTotal
if $r_passed and $pp eq "prepass" 4 91 95
Condition66%2/3
$r_passed and $pp eq "prepass"
!ll&&!rl&&r
9104
MC/DC50%1/2
$r_passed and $pp eq "prepass"
$r_passed $pp eq "prepass"
Truth table66%2/3
A and B
A= $r_passedB= $pp eq "prepass"
ABresult
0X0
100
111
302  
303 # track what got passed for part_in
304 91 ▶ $$r_passed = $buf->{end} if $r_passed;
Branch: 1 branch
BranchTrueFalseTotal
if $r_passed 0 91 91
305  
306 # remove passed data from ibuf, if ! r_passed also prepassed
307 # data (called from imp_callback)
308 91 shift(@$ibuf);
309 91 $i--;
310  
311 91 ▶ if ( ! @$ibuf ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$ibuf) 24 67 91
312 24 ▶ if ( $part->{pass} == IMP_MAXOFFSET || $buf->{eof} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{"pass"} == -1 or $buf->{"eof"}) 22 2 24
Condition66%2/3
$part->{"pass"} == -1 or $buf->{"eof"}
l!l&&r!l&&!r
2202
MC/DC50%1/2
$part->{"pass"} == -1 or $buf->{"eof"}
$part->{"pass"} == -1 $buf->{"eof"}
Truth table66%2/3
A or B
A= $part->{"pass"} == -1B= $buf->{"eof"}
ABresult
1X1
011
000
313 # part done, skip it in the future
314 22 push @fwd,[$pi,$dir,undef]; # buf = undef is special
315 }
316 # insert dummy
317 @$ibuf = $new_buf->(
318 start => $buf->{end},
319 end => $buf->{end},
320 gstart => $buf->{gend},
321 gend => $buf->{gend},
322 # keep type for streaming data
323 24 ▶ $buf->{dtype} < 0 ? ( dtype => $buf->{dtype} ):(),
Branch: 1 branch
BranchTrueFalseTotal
$buf->{'dtype'} < 0 ? : 24 0 24
324 );
325 24 last;
326 }
327  
328 } else {
329 # only part of buffer can be passed
330 # split buffer and re-enter loop, this will foreward the
331 # first part and keep the later part
332 36 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug(qq[fwd_collect[$dir][$pi]: need to split buffer[$i]: $buf->{"start"}..$pass..$buf->{"end"}]) 0 36 36
333 "fwd_collect[$dir][$pi]: need to split buffer[$i]: $buf->{start}..$pass..$buf->{end}");
334 36 $split_buf->($ibuf,$i,$pass - $buf->{start});
335 36 redo; # don't increase $i!
336 }
337 }
338 }
339 148 return @fwd;
340 11 };
341  
342 $fwd_up = sub {
343 71 ▶ my ($dir,$buf) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
344 71 ▶ if ( $buf->{gstart} == $buf->{gend} && ! $buf->{gap}
Branch: 1 branch
BranchTrueFalseTotal
if ($buf->{"gstart"} == $buf->{"gend"} and not $buf->{"gap"} and $buf->{"rtype"} ~~ ["pass", "prepass"]) 3 68 71
Condition83%5/6
$buf->{"gstart"} == $buf->{"gend"} and not $buf->{"gap"} and $buf->{"rtype"} ~~ ["pass", "prepass"]
!ll&&!rl&&r
6353
$buf->{"gstart"} == $buf->{"gend"} and not $buf->{"gap"}
!ll&&!rl&&r
6308
MC/DC66%2/3
$buf->{"gstart"} == $buf->{"gend"} and not $buf->{"gap"} and $buf->{"rtype"} ~~ ["pass", "prepass"]
$buf->{"gstart"} == $buf->{"gend"} not $buf->{"gap"} $buf->{"rtype"} ~~ ["pass", "prepass"]
Truth table75%3/4
A and not(B) and C
A= $buf->{"gstart"} == $buf->{"gend"}B= not $buf->{"gap"}C= $buf->{"rtype"} ~~ ["pass", "prepass"]
ABCresult
0XX0
10X0
1100
1111
345 && $buf->{rtype} ~~ [ IMP_PASS, IMP_PREPASS ]) {
346 # don't repeat last (pre)pass because of empty buffer
347 3 return;
348 }
349  
350 return [
351 $buf->{rtype},
352 $dir,
353 $buf->{gend},
354 68 ▶ ($buf->{rtype} == IMP_REPLACE) ? ( $buf->{data} ):()
Branch: 1 branch
BranchTrueFalseTotal
$buf->{'rtype'} == 4113 ? : 35 33 68
355 ];
356 11 };
357  
358 $exec_fwd = sub {
359 70 ▶ my @fwd = @_;
Subroutine __ANON__: called CC=38 SCAR=38.5
360 70 ▶ if (@fwd>1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fwd > 1) 50 20 70
361 $DEBUG && debug("trying to merge\n".join("\n", map {
362 50 ▶ ! defined $_->[0]
Branch: 3 branches
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("trying to merge\n" . join("\n", map({!defined($_->[0]) ? "<cb>" : "fwd[$_->[1]][$_->[0]] " . ($_->[2] ? &$dump_bufs([$_->[2]]) : "<pass infinite>");} @fwd))) 0 50 50
!defined($_->[0]) ? : 0 0 0
$_->[2] ? : 0 0 0
363 ? "<cb>"
364 : "fwd[$_->[1]][$_->[0]] " .
365 ( $_->[2] ? $dump_bufs->([$_->[2]]) : '<pass infinite>')
366 } @fwd));
367 # try to compress
368 50 my ($lpi,$ldir,$lbuf);
369 50 ▶ for( my $i=0;$i<@fwd;$i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @fwd) 146 50 196
370 146 ▶ if ( ! defined $fwd[$i][0] || ! defined $fwd[$i][2]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fwd[$i][0] and defined $fwd[$i][2]) 22 124 146
Condition66%2/3
defined $fwd[$i][0] and defined $fwd[$i][2]
!ll&&!rl&&r
022124
MC/DC50%1/2
defined $fwd[$i][0] and defined $fwd[$i][2]
defined $fwd[$i][0] defined $fwd[$i][2]
Truth table66%2/3
A and B
A= defined $fwd[$i][0]B= defined $fwd[$i][2]
ABresult
0X0
100
111
371 22 $lpi = undef;
372 22 next;
373 }
374 124 ▶ if ( ! defined $lpi
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $lpi or $lpi != $fwd[$i][0] or $ldir != $fwd[$i][1]) 0 74 74
Condition66%4/6
not defined $lpi or $lpi != $fwd[$i][0] or $ldir != $fwd[$i][1]
l!l&&r!l&&!r
50074
not defined $lpi or $lpi != $fwd[$i][0]
l!l&&r!l&&!r
50074
MC/DC33%1/3
not defined $lpi or $lpi != $fwd[$i][0] or $ldir != $fwd[$i][1]
not defined $lpi $lpi != $fwd[$i][0] $ldir != $fwd[$i][1]
Truth table50%2/4
not(A) or B or C
A= not defined $lpiB= $lpi != $fwd[$i][0]C= $ldir != $fwd[$i][1]
ABCresult
1XX1
01X1
0011
0000
375 or $lpi != $fwd[$i][0]
376 or $ldir != $fwd[$i][1] ) {
377 50 ($lpi,$ldir,$lbuf) = @{$fwd[$i]};
378 50 next;
379 }
380  
381 74 my $buf = $fwd[$i][2];
382  
383 74 ▶ if ( not $buf->{gap}
Branch: 1 branch
BranchTrueFalseTotal
if (not $buf->{'gap'} and $buf->{'dtype'} < 0 and $buf->{'start'} == $lbuf->{'end'} and $buf->{'rtype'} == $lbuf->{'rtype'} and $buf->{'dtype'} == $lbuf->{'dtype'}) { } 28 46 74
Condition66%8/12
not $buf->{"gap"} and $buf->{"dtype"} < 0 and $buf->{"start"} == $lbuf->{"end"} and $buf->{"rtype"} == $lbuf->{"rtype"} and $buf->{"dtype"} == $lbuf->{"dtype"}
!ll&&!rl&&r
46028
not $buf->{"gap"} and $buf->{"dtype"} < 0 and $buf->{"start"} == $lbuf->{"end"} and $buf->{"rtype"} == $lbuf->{"rtype"}
!ll&&!rl&&r
64028
not $buf->{"gap"} and $buf->{"dtype"} < 0 and $buf->{"start"} == $lbuf->{"end"}
!ll&&!rl&&r
0668
not $buf->{"gap"} and $buf->{"dtype"} < 0
!ll&&!rl&&r
0074
MC/DC40%2/5
not $buf->{"gap"} and $buf->{"dtype"} < 0 and $buf->{"start"} == $lbuf->{"end"} and $buf->{"rtype"} == $lbuf->{"rtype"} and $buf->{"dtype"} == $lbuf->{"dtype"}
not $buf->{"gap"} $buf->{"dtype"} < 0 $buf->{"start"} == $lbuf->{"end"} $buf->{"rtype"} == $lbuf->{"rtype"} $buf->{"dtype"} == $lbuf->{"dtype"}
Truth table50%3/6
not(A) and B and C and D and E
A= not $buf->{"gap"}B= $buf->{"dtype"} < 0C= $buf->{"start"} == $lbuf->{"end"}D= $buf->{"rtype"} == $lbuf->{"rtype"}E= $buf->{"dtype"} == $lbuf->{"dtype"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
384 and $buf->{dtype} < 0
385 and $buf->{start} == $lbuf->{end}
386 and $buf->{rtype} == $lbuf->{rtype}
387 and $buf->{dtype} == $lbuf->{dtype}
388 ) {
389 28 ▶ if ( $buf->{rtype} == IMP_REPLACE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf->{'rtype'} == 4113) { } 17 11 28
390 17 ▶ if ( $lbuf->{gend} == $buf->{gend} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lbuf->{'gend'} == $buf->{'gend'}) { } 2 15 17
elsif ($buf->{'data'} ne '' or $lbuf->{'data'} ne '') { } 11 4 15
391 # same global end, merge data
392 2 $lbuf->{data} .= $buf->{data};
393 ▶ } elsif ( $buf->{data} ne '' or $lbuf->{data} ne '' ) {
Condition100%3/3
$buf->{"data"} ne "" or $lbuf->{"data"} ne ""
l!l&&r!l&&!r
1104
MC/DC100%2/2
$buf->{"data"} ne "" or $lbuf->{"data"} ne ""
$buf->{"data"} ne "" $lbuf->{"data"} ne ""
Truth table100%3/3
A or B
A= $buf->{"data"} ne ""B= $lbuf->{"data"} ne ""
ABresult
1X1
011
000
394 # either one not empty, no merge
395 11 next;
396 }
397 } else {
398 # unchanged, append
399 11 $lbuf->{data} .= $buf->{data};
400 }
401 17 ▶ $DEBUG && debug("merge bufs ".$dump_bufs->([$lbuf,$buf]));
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug('merge bufs ' . &$dump_bufs([$lbuf, $buf])) 0 17 17
402 17 $lbuf->{gend} = $buf->{gend};
403 17 $lbuf->{end} = $buf->{end};
404 17 $lbuf->{eof} = $buf->{eof};
405 17 splice(@fwd,$i,1,());
406 17 $i--;
407 17 next;
408  
409 } else {
410 46 ($lpi,$ldir,$lbuf) = @{$fwd[$i]};
411 46 next;
412 }
413 }
414 }
415 70 ▶ while ( my $fwd = shift(@fwd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fwd = shift @fwd) 171 70 241
416 171 my $npi = my $pi = shift(@$fwd);
417 171 ▶ if ( ! defined $npi ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $npi) 0 171 171
418 # propagate prepared IMP callback
419 0 $wself->run_callback($fwd);
420 0 next;
421 }
422  
423 171 my ($dir,$buf) = @$fwd;
424  
425 171 ▶ if ( $buf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf) { } 127 44 171
426 127 my $np;
427 127 my $adjust = 0;
428 127 while (1) {
429 127 ▶ $npi += $dir?-1:+1;
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 13 114 127
430 127 ▶ last if $npi<0 or $npi>=@imp;
Branch: 1 branch
BranchTrueFalseTotal
if $npi < 0 or $npi >= @imp 69 58 127
Condition100%3/3
$npi < 0 or $npi >= @imp
l!l&&r!l&&!r
76258
MC/DC100%2/2
$npi < 0 or $npi >= @imp
$npi < 0 $npi >= @imp
Truth table100%3/3
A or B
A= $npi < 0B= $npi >= @imp
ABresult
1X1
011
000
431 58 ▶ last if ref( $np = $parts[$dir][$npi] );
Branch: 1 branch
BranchTrueFalseTotal
if ref($np = $parts[$dir][$npi]) 58 0 58
432 0 $adjust += $np;
433 0 ▶ $DEBUG && debug("skipping pi=$npi");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("skipping pi=$npi") 0 0 0
434 }
435  
436 127 ▶ if ( $buf->{eof} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf->{"eof"}) 22 105 127
437 # add pass infinite to fwd to propagate eof
438 22 push @fwd,[ $pi,$dir,undef ];
439 }
440 127 ▶ if ( $np ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($np) { } 58 69 127
441 # feed into next part
442 58 my $nib = $np->{ibuf};
443 # adjust start,end based on end of npi and gap
444 58 $buf->{start} = $nib->[-1]{end} + $buf->{gap} + $adjust;
445 58 $buf->{end} = $buf->{start} + length($buf->{data});
446 58 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("fwd_next[$dir][$pi>$npi] " . &$dump_bufs([$buf])) 0 58 58
447 "fwd_next[$dir][$pi>$npi] ".$dump_bufs->([$buf]));
448 58 $part_in->($npi,$dir,$buf);
449 } else {
450 # output from cascade
451 69 ▶ my $cb = $fwd_up->($dir,$buf) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $cb = &$fwd_up($dir, $buf) or next 1 68 69
452 68 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("fwd_up[$dir][$pi>>] " . &$dump_bufs([$buf])) 0 68 68
453 "fwd_up[$dir][$pi>>] ".$dump_bufs->([$buf]));
454 68 $wself->run_callback($cb);
455 }
456  
457 # special - part is done with IMP_PASS IMP_MAXOFFSET
458 } else {
459 # skip if we had a pass infinite already
460 44 ▶ next if ! ref $parts[$dir][$pi];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $parts[$dir][$pi] 20 24 44
461  
462 24 $parts[$dir][$pi] = $parts[$dir][$pi]->{adjust};
463 24 ▶ if ( grep { ref($_) } @{ $parts[$dir] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $_;} @{$parts[$dir];}) { } 12 12 24
464 # we have other unfinished parts, skip only this part
465 12 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("part[$dir][$pi>$npi] will be skipped in future, adjust=$parts[$dir][$pi]") 0 12 12
466 "part[$dir][$pi>$npi] will be skipped in future, adjust=$parts[$dir][$pi]");
467 } else {
468 # everything can be skipped
469 12 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("part[$dir][$pi>>] all parts will be skipped in future") 0 12 12
470 "part[$dir][$pi>>] all parts will be skipped in future");
471 # pass rest
472 12 $wself->run_callback([ IMP_PASS,$dir,IMP_MAXOFFSET ]);
473 }
474 }
475 }
476 11 };
477  
478 # the data function
479 # called from sub data on new data and from $process when data are finished
480 # in on part and should be transferred into the next part
481 # $pi - index into parts
482 # $dir - direction (e.g. target part is $parts[$dir][$pi])
483 # $buf - new buffer from $new_buf->() which might be merged with existing
484 $part_in = sub {
485 87 ▶ my ($pi,$dir,$buf) = @_;
Subroutine __ANON__: called CC=36 SCAR=43.0
486 87 ▶ $DEBUG && debug( "part_in[$dir][$pi]: ".$dump_bufs->([$buf]));
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("part_in[$dir][$pi]: " . &$dump_bufs([$buf])) 0 87 87
487  
488 87 my $part = $parts[$dir][$pi];
489 87 my $ibuf = $part->{ibuf};
490 87 my $lbuf = $ibuf->[-1];
491 87 my $lend = $lbuf->{end};
492  
493 # some sanity checks
494 87 if(1) {
495 die "data after eof [$dir][$pi] ".$dump_bufs->([$lbuf,$buf])
496 87 ▶ if $lbuf->{eof};
Branch: 1 branch
BranchTrueFalseTotal
if $lbuf->{"eof"} 0 87 87
497 87 ▶ if ( $buf->{start} != $lend ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($buf->{'start'} != $lend) { } 0 87 87
elsif ($buf->{'gap'}) { } 0 87 87
498 0 ▶ if ( $buf->{start} < $lend ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($buf->{'start'} < $lend) { } 0 0 0
elsif (not $buf->{'gap'}) { } 0 0 0
499 0 die "overlapping data off($buf->{start})<last.end($lend) in part[$dir][$pi]";
500 } elsif ( ! $buf->{gap} ) {
501 0 die "gap should be set because off($buf->{start})>last.end($lend) in part[$dir][$pi]"
502 }
503 } elsif ( $buf->{gap} ) {
504 0 die "gap specified even if off($buf->{start}) == last.end"
505 }
506 87 ▶ $part->{pass} == IMP_MAXOFFSET and die
Branch: 1 branch
BranchTrueFalseTotal
$part->{"pass"} == -1 and die "pass infinite should have been optimized by removing part[$dir][$pi]" 0 87 87
507 "pass infinite should have been optimized by removing part[$dir][$pi]";
508 }
509  
510 # add data to buf
511 87 ▶ if ( $lbuf->{data} eq '' and $lbuf->{rtype} == IMP_PASS ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lbuf->{'data'} eq '' and $lbuf->{'rtype'} == 4097) { } 26 61 87
elsif (not $buf->{'gap'} and $buf->{'data'} eq '' and $buf->{'rtype'} == $lbuf->{'rtype'} and $buf->{'dtype'} == $lbuf->{'dtype'} and $buf->{'dtype'} < 0 and not $buf->{'eof'}) { } 6 55 61
Condition100%3/3
$lbuf->{"data"} eq "" and $lbuf->{"rtype"} == "pass"
!ll&&!rl&&r
57426
MC/DC100%2/2
$lbuf->{"data"} eq "" and $lbuf->{"rtype"} == "pass"
$lbuf->{"data"} eq "" $lbuf->{"rtype"} == "pass"
Truth table100%3/3
A and B
A= $lbuf->{"data"} eq ""B= $lbuf->{"rtype"} == "pass"
ABresult
0X0
100
111
512 # empty dummy buffer
513 26 ▶ $DEBUG && debug("part_in[$dir][$pi]: replace dummy buffer");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("part_in[$dir][$pi]: replace dummy buffer") 0 26 26
514 26 ▶ @$ibuf == 1 or die "empty dummy buffer should only be at beginning";
Branch: 1 branch
BranchTrueFalseTotal
@$ibuf == 1 or die "empty dummy buffer should only be at beginning" 0 26 26
515 26 @$ibuf = $buf;
516  
517 ▶ } elsif ( ! $buf->{gap}
Condition80%12/15
not $buf->{"gap"} and $buf->{"data"} eq "" and $buf->{"rtype"} == $lbuf->{"rtype"} and $buf->{"dtype"} == $lbuf->{"dtype"} and $buf->{"dtype"} < 0 and not $buf->{"eof"}
!ll&&!rl&&r
44116
not $buf->{"gap"} and $buf->{"data"} eq "" and $buf->{"rtype"} == $lbuf->{"rtype"} and $buf->{"dtype"} == $lbuf->{"dtype"} and $buf->{"dtype"} < 0
!ll&&!rl&&r
44017
not $buf->{"gap"} and $buf->{"data"} eq "" and $buf->{"rtype"} == $lbuf->{"rtype"} and $buf->{"dtype"} == $lbuf->{"dtype"}
!ll&&!rl&&r
44017
not $buf->{"gap"} and $buf->{"data"} eq "" and $buf->{"rtype"} == $lbuf->{"rtype"}
!ll&&!rl&&r
42217
not $buf->{"gap"} and $buf->{"data"} eq ""
!ll&&!rl&&r
04219
MC/DC50%3/6
not $buf->{"gap"} and $buf->{"data"} eq "" and $buf->{"rtype"} == $lbuf->{"rtype"} and $buf->{"dtype"} == $lbuf->{"dtype"} and $buf->{"dtype"} < 0 and not $buf->{"eof"}
not $buf->{"gap"} $buf->{"data"} eq "" $buf->{"rtype"} == $lbuf->{"rtype"} $buf->{"dtype"} == $lbuf->{"dtype"} $buf->{"dtype"} < 0 not $buf->{"eof"}
Truth table57%4/7
not(A) and B and C and D and E and not(F)
A= not $buf->{"gap"}B= $buf->{"data"} eq ""C= $buf->{"rtype"} == $lbuf->{"rtype"}D= $buf->{"dtype"} == $lbuf->{"dtype"}E= $buf->{"dtype"} < 0F= not $buf->{"eof"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
518 and $buf->{data} eq ''
519 and $buf->{rtype} == $lbuf->{rtype}
520 and $buf->{dtype} == $lbuf->{dtype}
521 and $buf->{dtype} < 0
522 and ! $buf->{eof}
523 ) {
524 # just update eof,[g]end of lbuf
525 6 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug(qq[part_in[$dir][$pi]: set lbuf end=$buf->{"end"} gend=$buf->{"gend"}]) 0 6 6
526 "part_in[$dir][$pi]: set lbuf end=$buf->{end} gend=$buf->{gend}");
527 6 $lbuf->{end} = $buf->{end};
528 6 $lbuf->{gend} = $buf->{gend};
529 # nothing to do with these empty data
530 6 ▶ $DEBUG && debug("part_in[$dir][$pi] nothing to do on empty buffer");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("part_in[$dir][$pi] nothing to do on empty buffer") 0 6 6
531 6 return;
532  
533 } else {
534 # add new buf
535 55 ▶ $DEBUG && debug("part_in[$dir][$pi]: add new buffer");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("part_in[$dir][$pi]: add new buffer") 0 55 55
536 55 push @$ibuf,$buf;
537 }
538  
539 # determine what can be forwarded immediatly
540 81 my @fwd = $fwd_collect->($dir,$pi,$#$ibuf,\$lend);
541  
542 81 ▶ if ( $buf->{eof} ? $lend <= $buf->{end} : $lend < $buf->{end} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($buf->{'eof'} ? $lend <= $buf->{'end'} : $lend < $buf->{'end'}) { } 80 1 81
$buf->{'eof'} ? : 22 59 81
543 # send new data to the analyzer
544 80 my $rl = $part->{replace_later};
545 80 for(@$ibuf) {
546 173 ▶ next if $_->{start} < $lend;
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"start"} < $lend 89 84 173
547 die "last_end should be on buffer boundary"
548 84 ▶ if $_->{start} > $lend;
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"start"} > $lend 0 84 84
549 84 $lend = $_->{end};
550 $DEBUG && debug(
551 "analyzer[$dir][$pi] << %d bytes %s \@%d%s -> last_end=%d",
552 $_->{end} - $_->{start},
553 $_->{dtype},
554 84 ▶ $_->{start},$_->{gap} ? "(+$_->{gap})":'',
Branch: 2 branches
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("analyzer[$dir][$pi] << %d bytes %s \@%d%s -> last_end=%d", $_->{"end"} - $_->{"start"}, $_->{"dtype"}, $_->{"start"}, $_->{"gap"} ? qq[(+$_->{"gap"})] : "", $lend) 0 84 84
$_->{'gap'} ? : 0 0 0
555 $lend
556 );
557 $imp[$pi]->data($dir,
558 $_->{data},
559 $_->{gap} ? $_->{start}:0,
560 $_->{dtype}
561 84 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$_->{'gap'} ? : 0 84 84
562 $imp[$pi]->data($dir,'',0, $_->{dtype})
563 84 ▶ if $buf->{eof} && $_->{data} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $buf->{"eof"} and $_->{"data"} ne "" 8 76 84
Condition100%3/3
$buf->{"eof"} and $_->{"data"} ne ""
!ll&&!rl&&r
59178
MC/DC100%2/2
$buf->{"eof"} and $_->{"data"} ne ""
$buf->{"eof"} $_->{"data"} ne ""
Truth table100%3/3
A and B
A= $buf->{"eof"}B= $_->{"data"} ne ""
ABresult
0X0
100
111
564 84 ▶ $rl or next;
Branch: 1 branch
BranchTrueFalseTotal
$rl or next 84 0 84
565 0 ▶ if ( $rl == IMP_MAXOFFSET or $rl>= $lend ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rl == -1 or $rl >= $lend) { } 0 0 0
Condition0%0/3
$rl == -1 or $rl >= $lend
l!l&&r!l&&!r
000
MC/DC0%0/2
$rl == -1 or $rl >= $lend
$rl == -1 $rl >= $lend
Truth table0%0/3
A or B
A= $rl == -1B= $rl >= $lend
ABresult
1X1
011
000
566 0 $buf->{data} = undef;
567 } else {
568 0 $rl = $part->{replace_later} = 0; # reset
569 }
570 }
571 } else {
572 1 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug(qq[nothing to analyze[$dir][$pi]: last_end($lend) < end($buf->{"end"})]) 0 1 1
573 "nothing to analyze[$dir][$pi]: last_end($lend) < end($buf->{end})");
574 }
575  
576 # forward data which can be (pre)passed
577 81 ▶ $exec_fwd->(@fwd) if @fwd;
Branch: 1 branch
BranchTrueFalseTotal
if @fwd 4 77 81
578 11 };
579  
580 $_imp_callback = sub {
581 75 ▶ my $pi = shift;
Subroutine __ANON__: called CC=60 SCAR=65.6
582  
583 75 my @fwd;
584 75 for my $rv (@_) {
585 107 my $rtype = shift(@$rv);
586  
587 107 ▶ if ( $rtype ~~ [ IMP_FATAL, IMP_DENY, IMP_DROP, IMP_ACCTFIELD ]) {
Branch: 7 branches
BranchTrueFalseTotal
if ($rtype ~~ [4354, 4352, 4353, 4]) { } 0 107 107
elsif ($rtype == 1) { } 0 107 107
elsif ($rtype == 16) { } 0 107 107
elsif ($rtype == 17) { } 0 107 107
elsif ($rtype ~~ [4097, 4099]) { } 76 31 107
elsif ($rtype == 4113) { } 31 0 31
elsif ($rtype == 18) { } 0 0 0
588 0 ▶ $DEBUG && debug("callback[.][$pi] $rtype @$rv");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("callback[.][$pi] $rtype @$rv") 0 0 0
589 0 $wself->run_callback([ $rtype, @$rv ]);
590  
591 } elsif ( $rtype == IMP_LOG ) {
592 0 my ($dir,$offset,$len,$level,$msg,@extmsg) = @$rv;
593 0 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("callback[$dir][$pi] $rtype '${msg}' off=$offset len=$len lvl=$level") 0 0 0
594 "callback[$dir][$pi] $rtype '$msg' off=$offset len=$len lvl=$level");
595 # approximate offset to real position
596 0 my $newoff = 0;
597 0 my $part = $parts[$dir][$pi];
598 0 for ( @{$part->{ibuf}} ) {
599 0 ▶ if ( $_->{start} <= $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'start'} <= $offset) { } 0 0 0
600 $offset = ( $_->{rtype} == IMP_REPLACE )
601 ? $_->{gstart}
602 0 ▶ : $_->{gstart} - $_->{start} + $offset;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'rtype'} == 4113 ? : 0 0 0
603 } else {
604 last
605 0 }
606 }
607 0 $wself->run_callback([ IMP_LOG,$dir,$offset,$len,$level,$msg,@extmsg ]);
608  
609 } elsif ( $rtype == IMP_PAUSE ) {
610 0 my $dir = shift;
611 0 ▶ $DEBUG && debug("callback[$dir][$pi] $rtype");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("callback[$dir][$pi] $rtype") 0 0 0
612 0 ▶ next if $pause[$pi];
Branch: 1 branch
BranchTrueFalseTotal
if $pause[$pi] 0 0 0
613 0 $pause[$dir][$pi] = 1;
614 0 ▶ $wself->run_callback([ IMP_PAUSE ]) if grep { $_ } @pause > 1;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_;} @pause > 1 0 0 0
615  
616 } elsif ( $rtype == IMP_CONTINUE ) {
617 0 my $dir = shift;
618 0 ▶ $DEBUG && debug("callback[$dir][$pi] $rtype");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("callback[$dir][$pi] $rtype") 0 0 0
619 0 delete $pause[$dir][$pi];
620 $wself->run_callback([ IMP_CONTINUE ])
621 0 ▶ if not grep { $_ } @{$pause[$dir]};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_;} @{$pause[$dir];} 0 0 0
622  
623 } elsif ( $rtype ~~ [ IMP_PASS, IMP_PREPASS ] ) {
624 76 my ($dir,$offset) = @$rv;
625 76 ▶ $DEBUG && debug("callback[$dir][$pi] $rtype $offset");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("callback[$dir][$pi] $rtype $offset") 0 76 76
626 76 ▶ ref(my $part = $parts[$dir][$pi]) or next; # part skippable?
Branch: 1 branch
BranchTrueFalseTotal
ref(my $part = $parts[$dir][$pi]) or next 7 69 76
627 69 ▶ if ( $rtype == IMP_PASS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rtype == 4097) { } 65 4 69
628 65 ▶ next if $part->{pass} == IMP_MAXOFFSET; # no change
Branch: 1 branch
BranchTrueFalseTotal
if $part->{"pass"} == -1 0 65 65
629 65 ▶ if ( $offset == IMP_MAXOFFSET ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($offset == -1) { } 22 43 65
elsif ($offset > $part->{'pass'}) { } 43 0 43
630 22 $part->{pass} = IMP_MAXOFFSET;
631 22 $part->{prepass} = 0; # pass >= prepass
632 } elsif ( $offset > $part->{pass} ) {
633 43 $part->{pass} = $offset;
634 43 ▶ if ( $part->{prepass} != IMP_MAXOFFSET
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{"prepass"} != -1 and $part->{"prepass"} <= $offset) 43 0 43
Condition33%1/3
$part->{"prepass"} != -1 and $part->{"prepass"} <= $offset
!ll&&!rl&&r
0043
MC/DC0%0/2
$part->{"prepass"} != -1 and $part->{"prepass"} <= $offset
$part->{"prepass"} != -1 $part->{"prepass"} <= $offset
Truth table33%1/3
A and B
A= $part->{"prepass"} != -1B= $part->{"prepass"} <= $offset
ABresult
0X0
100
111
635 and $part->{prepass} <= $offset ) {
636 43 $part->{prepass} = 0; # pass >= prepass
637 }
638 } else {
639 0 next; # no change
640 }
641 } else { # IMP_PREPASS
642 4 ▶ next if $part->{prepass} == IMP_MAXOFFSET; # no change
Branch: 1 branch
BranchTrueFalseTotal
if $part->{"prepass"} == -1 2 2 4
643 2 ▶ if ( $offset == IMP_MAXOFFSET ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($offset == -1) { } 2 0 2
elsif ($offset > $part->{'prepass'}) { } 0 0 0
644 2 $part->{prepass} = IMP_MAXOFFSET;
645 } elsif ( $offset > $part->{prepass} ) {
646 0 $part->{prepass} = $offset;
647 } else {
648 0 next; # no change
649 }
650 }
651  
652 # pass/prepass got updated, so we might pass some more data
653 67 push @fwd, $fwd_collect->($dir,$pi,0);
654  
655 } elsif ( $rtype == IMP_REPLACE ) {
656 31 my ($dir,$offset,$newdata) = @$rv;
657 31 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("callback[$dir][$pi] $rtype $dir $offset len=%d", length $newdata) 0 31 31
658 "callback[$dir][$pi] $rtype $dir $offset len=%d",
659 length($newdata));
660 31 ▶ ref(my $part = $parts[$dir][$pi])
Branch: 1 branch
BranchTrueFalseTotal
ref(my $part = $parts[$dir][$pi]) or die "called replace for passed data" 0 31 31
661 or die "called replace for passed data";
662 31 my $ibuf = $part->{ibuf};
663  
664 # sanity checks
665 31 ▶ die "called replace although pass=IMP_MAXOFFSET" if ! $part;
Branch: 1 branch
BranchTrueFalseTotal
unless $part 0 31 31
666 31 ▶ die "no replace with IMP_MAXOFFSET" if $offset == IMP_MAXOFFSET;
Branch: 1 branch
BranchTrueFalseTotal
if $offset == -1 0 31 31
667 die "called replace for already passed data"
668 31 ▶ if $ibuf->[0]{start} > $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $ibuf->[0]{"start"} > $offset 0 31 31
669  
670 31 ▶ while (@$ibuf) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$ibuf) 49 0 49
671 71 my $buf = $ibuf->[0];
672 71 ▶ if ( $offset >= $buf->{end} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= $buf->{'end'}) { } 49 22 71
673 # replace complete buffer
674 49 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug(qq[replace complete buf $buf->{"start"}..$buf->{"end"}]) 0 49 49
675 "replace complete buf $buf->{start}..$buf->{end}");
676 49 ▶ if ( ! defined($buf->{data})
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $buf->{"data"} or $buf->{"data"} ne $newdata) 48 1 49
Condition66%2/3
not defined $buf->{"data"} or $buf->{"data"} ne $newdata
l!l&&r!l&&!r
0481
MC/DC50%1/2
not defined $buf->{"data"} or $buf->{"data"} ne $newdata
not defined $buf->{"data"} $buf->{"data"} ne $newdata
Truth table66%2/3
not(A) or B
A= not defined $buf->{"data"}B= $buf->{"data"} ne $newdata
ABresult
1X1
011
000
677 or $buf->{data} ne $newdata ) {
678 48 $buf->{rtype} = IMP_REPLACE;
679 48 $buf->{data} = $newdata;
680 $part->{adjust} +=
681 48 length($newdata) - $buf->{end} + $buf->{start};
682 48 $newdata = ''; # in the next buffers replace with ''
683 }
684 49 push @fwd,[ $pi,$dir,$buf ];
685 49 shift(@$ibuf);
686 49 ▶ if ( ! @$ibuf ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$ibuf) 5 44 49
687 # all bufs eaten
688 die "called replace for future data"
689 5 ▶ if $buf->{end}<$offset;
Branch: 1 branch
BranchTrueFalseTotal
if $buf->{"end"} < $offset 0 5 5
690 @$ibuf = $new_buf->( %$buf,
691 data => '',
692 start => $buf->{end},
693 end => $buf->{end},
694 gstart => $buf->{gend},
695 # packet types cannot get partial replacement
696 # at end
697 5 ▶ $buf->{dtype} > 0 ? ( dtype => 0 ):()
Branch: 1 branch
BranchTrueFalseTotal
$buf->{'dtype'} > 0 ? : 0 5 5
698 );
699 # remove eof from buf in @fwd because we added
700 # new one
701 5 $fwd[-1][2]{eof} = 0;
702 5 last;
703 }
704 44 ▶ last if $buf->{end} == $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $buf->{"end"} == $offset 26 18 44
705 } else {
706 # split buffer and replace first part
707 22 ▶ $DEBUG && debug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug(qq[replace - split buf $buf->{"start"}..$offset..$buf->{"end"}]) 0 22 22
708 "replace - split buf $buf->{start}..$offset..$buf->{end}");
709 22 $split_buf->($ibuf,0,$offset-$buf->{start});
710 22 redo;
711 }
712 }
713  
714 } elsif ( $rtype == IMP_REPLACE_LATER ) {
715 0 my ($dir,$offset) = @$rv;
716 0 ▶ $DEBUG && debug("callback[$dir][$pi] $rtype $offset");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("callback[$dir][$pi] $rtype $offset") 0 0 0
717 0 ▶ ref(my $part = $parts[$dir][$pi])
Branch: 1 branch
BranchTrueFalseTotal
ref(my $part = $parts[$dir][$pi]) or die "called replace for passed data" 0 0 0
718 or die "called replace for passed data";
719 0 my $ibuf = $part->{ibuf};
720 0 ▶ $_->{replace_later} == IMP_MAXOFFSET and next; # no change
Branch: 1 branch
BranchTrueFalseTotal
$_->{"replace_later"} == -1 and next 0 0 0
721  
722 # sanity checks
723 0 ▶ die "called replace_later although pass=IMP_MAX_OFFSET"
Branch: 1 branch
BranchTrueFalseTotal
unless $part 0 0 0
724 if ! $part;
725 die "called replace for already passed data" if
726 $offset != IMP_MAXOFFSET and
727 0 ▶ $ibuf->[0]{start} > $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $offset != -1 and $ibuf->[0]{"start"} > $offset 0 0 0
Condition0%0/3
$offset != -1 and $ibuf->[0]{"start"} > $offset
!ll&&!rl&&r
000
MC/DC0%0/2
$offset != -1 and $ibuf->[0]{"start"} > $offset
$offset != -1 $ibuf->[0]{"start"} > $offset
Truth table0%0/3
A and B
A= $offset != -1B= $ibuf->[0]{"start"} > $offset
ABresult
0X0
100
111
728  
729 0 ▶ if ( $offset == IMP_MAXOFFSET ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($offset == -1) { } 0 0 0
elsif ($offset <= $part->{'replace_later'}) { } 0 0 0
730 0 $_->{replace_later} = IMP_MAXOFFSET;
731 # change all to replace_later
732 0 $_->{data} = undef for(@$ibuf);
733 0 next;
734 } elsif ( $offset <= $part->{replace_later} ) {
735 # no change
736 } else {
737 0 $part->{replace_later} = $offset;
738 0 for(@$ibuf) {
739 0 ▶ defined($_->{data}) or next; # already replace_later
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{"data"} or next 0 0 0
740 0 ▶ my $len = length($_->{data}) or last; # dummy buffer
Branch: 1 branch
BranchTrueFalseTotal
my $len = length $_->{"data"} or last 0 0 0
741 0 ▶ if ( $_->{start} + $len <= $offset ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'start'} + $len <= $offset) { } 0 0 0
742 0 $_->{data} = undef;
743 } else {
744 0 $part->{replace_later} = 0;
745 0 last;
746 }
747 }
748 }
749 } else {
750 0 ▶ $DEBUG && debug("callback[.][$pi] $rtype @$rv");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("callback[.][$pi] $rtype @$rv") 0 0 0
751 0 die "don't know how to handle rtype $rtype";
752 }
753 }
754  
755 # pass to next part/output
756 75 ▶ $exec_fwd->(@fwd) if @fwd;
Branch: 1 branch
BranchTrueFalseTotal
if @fwd 66 9 75
757 11 };
758  
759 # While we are in $part_in function we will only spool callbacks and process
760 # them at the end. Otherwise $dataf might cause call of callback which then
761 # causes call of dataf etc - which makes debugging a nightmare.
762  
763 11 my $collect_callbacks;
764 $global_in = sub {
765 31 ▶ my ($dir,$data,$offset,$dtype) = @_;
Subroutine __ANON__: called CC=15 SCAR=29.6
766  
767 31 ▶ my %buf = (
Condition100%2/2
$dtype // "imp.data.stream"
l!l
2011
MC/DC100%1/1
$dtype // "imp.data.stream"
$dtype
Truth table100%2/2
A
A= $dtype
Aresult
11
00
768 data => $data,
769 dtype => $dtype // IMP_DATA_STREAM,
770 rtype => IMP_PASS,
771 eof => $data eq '',
772 );
773  
774 31 my $adjust = 0;
775 31 ▶ my $pi = $dir ? $#imp:0; # enter into first or last part
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 2 29 31
776 31 my $np;
777 31 while (1) {
778 34 ▶ ref( $np = $parts[$dir][$pi] ) and last;
Branch: 1 branch
BranchTrueFalseTotal
ref($np = $parts[$dir][$pi]) and last 29 5 34
779 5 $adjust += $np;
780 5 ▶ $pi += $dir?-1:1;
Branch: 1 branch
BranchTrueFalseTotal
$dir ? : 0 5 5
781 5 ▶ if ( $pi<0 or $pi>$#imp ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pi < 0 or $pi > $#imp) 2 3 5
Condition66%2/3
$pi < 0 or $pi > $#imp
l!l&&r!l&&!r
023
MC/DC50%1/2
$pi < 0 or $pi > $#imp
$pi < 0 $pi > $#imp
Truth table66%2/3
A or B
A= $pi < 0B= $pi > $#imp
ABresult
1X1
011
000
782 2 ▶ $DEBUG && debug("all skipped");
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMP::Cascade::DEBUG and debug("all skipped") 0 2 2
783 2 ▶ if ( my $cb = $fwd_up->($dir,$new_buf->(%buf))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = &$fwd_up($dir, &$new_buf(%buf))) 0 2 2
784 0 $self->run_callback($cb);
785 }
786 2 return;
787 }
788 }
789  
790 29 ▶ return if ! ref $np; # got IMP_PASS IMP_MAXOFFSET for all
Branch: 1 branch
BranchTrueFalseTotal
unless ref $np 0 29 29
791  
792 29 my $ibuf_end = $np->{ibuf}[-1]{gend};
793 29 ▶ if ( ! $offset ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $offset) { } 29 0 29
elsif ($offset < $ibuf_end) { } 0 0 0
elsif ($offset > $ibuf_end) { } 0 0 0
794 # no gap between data
795 29 $buf{gstart} = $ibuf_end;
796 } elsif ( $offset < $ibuf_end ) {
797 0 die "overlapping data";
798 } elsif ( $offset > $ibuf_end ) {
799 # gap between data
800 0 $buf{gstart} = $offset;
801 0 $buf{gap} = $offset - $ibuf_end;
802 } else {
803 # there was no need for giving offset
804 0 $buf{gstart} = $ibuf_end;
805 }
806 29 $buf{gend} = $buf{gstart} + length($data);
807 29 $buf{start} = $buf{gstart} + $adjust;
808 29 $buf{end} = $buf{gend} + $adjust;
809  
810 29 ▶ $collect_callbacks ||= [];
Condition50%1/2
$collect_callbacks ||= []
l!l
029
MC/DC0%0/1
$collect_callbacks ||= []
$collect_callbacks
Truth table50%1/2
A
A= $collect_callbacks
Aresult
11
00
811 29 $part_in->( $pi,$dir, $new_buf->(%buf));
812  
813 29 ▶ while ( my $cb = shift(@$collect_callbacks)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = shift @$collect_callbacks) 75 29 104
814 75 $_imp_callback->(@$cb);
815 }
816 $collect_callbacks = undef
817 11 };
818  
819 # wrapper which spools callbacks if within dataf
820 $imp_callback = sub {
821 75 ▶ if ( $collect_callbacks ) {
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if ($collect_callbacks) 75 0 75
822 # only spool and execute later
823 75 push @$collect_callbacks, [ @_ ];
824 75 return;
825 }
826 0 return $_imp_callback->(@_)
827 11 };
828  
829 # setup callbacks
830 11 $imp[$_]->set_callback( $imp_callback,$_ ) for (0..$#imp);
831  
832 # make some closures available within methods
833 11 $self->{dataf} = $global_in;
834 $self->{closef} = sub {
835 0 ▶ $global_in = $part_in = $imp_callback = $_imp_callback = undef;
Subroutine __ANON__: not called CC=1 SCAR=6.9
836 0 @parts = ();
837 11 };
838 11 return $self;
839 }
840  
841 sub data {
842 31 ▶ my $self = shift;
Subroutine data: called CC=1 SCAR=0.0
Pod documented
843 31 $self->{dataf}(@_);
844 }
845  
846 sub DESTROY {
847 11 ▶ my $closef = shift->{closef};
Subroutine DESTROY: called CC=2 SCAR=7.2
848 11 ▶ $closef->() if $closef;
Branch: 1 branch
BranchTrueFalseTotal
if $closef 0 11 11
849 }
850  
851  
852 4 1;
853  
854 __END__
↑ Summary IMP/ProtocolPinning.pm »