| 1 | package Text::Netstring; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # Copyright (c) 2003-2006 James Raftery <james@now.ie>. All rights reserved. | ||||||||||||||||||||||||||||
| 10 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 11 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 12 | # Please submit bug reports, patches and comments to the author. | ||||||||||||||||||||||||||||
| 13 | # Latest information at http://romana.now.ie/ | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # $Id: Netstring.pm,v 1.13 2006/11/20 18:28:49 james Exp $ | ||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||
| 17 | # See the Text::Netstring man page that was installed with this module for | ||||||||||||||||||||||||||||
| 18 | # information on how to use the module. | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 22 | # Items to export into caller's namespace by request. | ||||||||||||||||||||||||||||
| 23 | 1 | @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 24 | netstring_encode netstring_decode netstring_verify netstring_read | ||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 6 | ▶ | sub netstring_encode { | ||||||||||||||||||||||||||
|
Subroutine
netstring_encode: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # is argument a list reference? | ||||||||||||||||||||||||||||
| 33 | 6 | ▶ | @_ = @{$_[0]} if (scalar(@_)==1 and ref($_[0]) eq "ARRAY"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
MC/DC100%2/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1
ref $_[0] eq "ARRAY"
Truth table100%3/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 6 | my @enc = map { length($_).":${_}," } @_; | |||||||||||||||||||||||||||
| 36 | 6 | ▶ | wantarray ? @enc : join("", @enc); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 5 | ▶ | sub netstring_decode { | ||||||||||||||||||||||||||
|
Subroutine
netstring_decode: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # is argument a list reference? | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | @_ = @{$_[0]} if (scalar(@_)==1 and ref($_[0]) eq "ARRAY"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1
ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 5 | ▶ | my @dec = map { /^(\d+):(.*),$/s and length($2)==$1 ? $2 : "" } @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
/^(\d+):(.*),$/s && (length $2 == $1 ? $2 : '')
MC/DC100%2/2
/^(\d+):(.*),$/s && (length $2 == $1 ? $2 : '')
/^(\d+):(.*),$/s
(length $2 == $1 ? $2 : '')
Truth table100%3/3
A && B
A= /^(\d+):(.*),$/sB= (length $2 == $1 ? $2 : '')
| |||||||||||||||||||||||||||||
| 45 | 5 | ▶ | wantarray ? @dec : join("", @dec); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 17 | ▶ | sub netstring_verify { | ||||||||||||||||||||||||||
|
Subroutine
netstring_verify: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # is argument a list reference? | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | @_ = @{$_[0]} if (scalar(@_)== 1 and ref($_[0]) eq "ARRAY"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1
ref $_[0] eq "ARRAY"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 17 | ▶ | my @ver = map { /^(\d+):(.*),$/s and length($2)==$1 } @_; | ||||||||||||||||||||||||||
|
Condition100%3/3
/^(\d+):(.*),$/s && length $2 == $1
MC/DC100%2/2
/^(\d+):(.*),$/s && length $2 == $1
/^(\d+):(.*),$/s
length $2 == $1
Truth table100%3/3
A && B
A= /^(\d+):(.*),$/sB= length $2 == $1
| |||||||||||||||||||||||||||||
| 54 | 17 | ▶ | wantarray ? @ver : do { my $i=shift(@ver); foreach (@ver) {$i &&= $_}; $i }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i &&= $_
MC/DC100%2/2
$i &&= $_
$i
$_
Truth table100%3/3
A &&= B
A= $iB= $_
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub netstring_read { | ||||||||||||||||||||||||||
|
Subroutine
netstring_read: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $sock = shift or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | my($r, $ns); | |||||||||||||||||||||||||||
| 61 | 0 | my $s = ""; | |||||||||||||||||||||||||||
| 62 | 0 | my $len = 0; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # read the length | ||||||||||||||||||||||||||||
| 65 | 0 | for (;;) { | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | defined($r = read($sock, $s, 1)) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | return "" if !$r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | last if $s eq ":"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | return undef if $s !~ /^[0-9]$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | $len = 10 * $len + $s; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | return undef if $len > 200000000; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | $ns = $len . ":"; | |||||||||||||||||||||||||||
| 77 | 0 | $s = ""; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # read the string 'body' | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | defined($r = read($sock, $s, $len)) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | return "" if (!$r and $len != 0); # zero length is OK | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $r and $len != 0
MC/DC0%0/2
not $r and $len != 0
not $r
$len != 0
Truth table0%0/3
not(A) and B
A= not $rB= $len != 0
| |||||||||||||||||||||||||||||
| 82 | 0 | $ns .= $s; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # read the trailing comma | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | defined($r = read($sock, $s, 1)) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | return "" if !$r; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | return undef if $s ne ","; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $ns .= $s; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | return $ns; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | 1; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | __END__ | ||||||||||||||||||||||||||||