| 1 | package Linux::Event::Stream::Codec::Netstring; | ||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use v5.36; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | our $VERSION = '0.002'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | ▶ | sub new ($class, %opt) { | |||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 0 | my $max_frame = delete $opt{max_frame}; | |||||||||||||||||||||||||||
| 12 | 0 | ▶ | croak 'unknown options: ' . join(', ', sort keys %opt) if %opt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | return bless { | |||||||||||||||||||||||||||
| 15 | max_frame => $max_frame, | ||||||||||||||||||||||||||||
| 16 | decode => \&_decode, | ||||||||||||||||||||||||||||
| 17 | encode => \&_encode, | ||||||||||||||||||||||||||||
| 18 | }, $class; | ||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | ▶ | sub _decode ($self, $inref, $outref) { | |||||||||||||||||||||||||||
|
Subroutine
_decode: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 22 | 0 | while (1) { | |||||||||||||||||||||||||||
| 23 | # Find ':' separating length from payload. | ||||||||||||||||||||||||||||
| 24 | 0 | my $colon = index($$inref, ':'); | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | last if $colon < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | my $len_s = substr($$inref, 0, $colon); | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | return (0, 'invalid netstring length') if $len_s eq '' || $len_s =~ /\D/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$len_s eq "" or $len_s =~ /\D/u
MC/DC0%0/2
$len_s eq "" or $len_s =~ /\D/u
$len_s eq ""
$len_s =~ /\D/u
Truth table0%0/3
A or B
A= $len_s eq ""B= $len_s =~ /\D/u
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | my $len = 0 + $len_s; | |||||||||||||||||||||||||||
| 31 | 0 | my $max = $self->{max_frame}; | |||||||||||||||||||||||||||
| 32 | 0 | ▶ | if (defined($max) && $len > $max) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $max and $len > $max
MC/DC0%0/2
defined $max and $len > $max
defined $max
$len > $max
Truth table0%0/3
A and B
A= defined $maxB= $len > $max
| |||||||||||||||||||||||||||||
| 33 | 0 | return (0, "frame too large (>$max bytes)"); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | my $need = $colon + 1 + $len + 1; # <len>:<payload>, | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | last if length($$inref) < $need; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | return (0, 'invalid netstring terminator') if substr($$inref, $need - 1, 1) ne ','; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | my $payload = substr($$inref, $colon + 1, $len); | |||||||||||||||||||||||||||
| 42 | 0 | substr($$inref, 0, $need, ''); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | push @$outref, $payload; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | return 1; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | ▶ | sub _encode ($self, $msg) { | |||||||||||||||||||||||||||
|
Subroutine
_encode: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | $msg = '' if !defined $msg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | $msg = "$msg"; | |||||||||||||||||||||||||||
| 53 | 0 | return length($msg) . ':' . $msg . ','; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 7 | 1; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | __END__ | ||||||||||||||||||||||||||||