line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Netstring; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1168
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
4
|
1
|
|
|
1
|
|
6
|
use vars qw($VERSION @ISA @EXPORT_OK); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
749
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require Exporter; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# Copyright (c) 2003-2006 James Raftery . 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
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
22
|
|
|
|
|
|
|
# Items to export into caller's namespace by request. |
23
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
24
|
|
|
|
|
|
|
netstring_encode netstring_decode netstring_verify netstring_read |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
$VERSION = '0.07'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub netstring_encode { |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# is argument a list reference? |
33
|
6
|
100
|
100
|
6
|
1
|
1121
|
@_ = @{$_[0]} if (scalar(@_)==1 and ref($_[0]) eq "ARRAY"); |
|
2
|
|
|
|
|
7
|
|
34
|
|
|
|
|
|
|
|
35
|
6
|
|
|
|
|
11
|
my @enc = map { length($_).":${_}," } @_; |
|
10
|
|
|
|
|
33
|
|
36
|
6
|
100
|
|
|
|
30
|
wantarray ? @enc : join("", @enc); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub netstring_decode { |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# is argument a list reference? |
42
|
5
|
50
|
66
|
5
|
1
|
784
|
@_ = @{$_[0]} if (scalar(@_)==1 and ref($_[0]) eq "ARRAY"); |
|
0
|
|
|
|
|
0
|
|
43
|
|
|
|
|
|
|
|
44
|
5
|
50
|
|
|
|
9
|
my @dec = map { /^(\d+):(.*),$/s and length($2)==$1 ? $2 : "" } @_; |
|
6
|
100
|
|
|
|
54
|
|
45
|
5
|
100
|
|
|
|
21
|
wantarray ? @dec : join("", @dec); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub netstring_verify { |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# is argument a list reference? |
51
|
17
|
50
|
66
|
17
|
1
|
1026
|
@_ = @{$_[0]} if (scalar(@_)== 1 and ref($_[0]) eq "ARRAY"); |
|
0
|
|
|
|
|
0
|
|
52
|
|
|
|
|
|
|
|
53
|
17
|
100
|
|
|
|
28
|
my @ver = map { /^(\d+):(.*),$/s and length($2)==$1 } @_; |
|
26
|
|
|
|
|
159
|
|
54
|
17
|
100
|
100
|
|
|
38
|
wantarray ? @ver : do { my $i=shift(@ver); foreach (@ver) {$i &&= $_}; $i }; |
|
15
|
|
|
|
|
21
|
|
|
15
|
|
|
|
|
26
|
|
|
5
|
|
|
|
|
16
|
|
|
15
|
|
|
|
|
38
|
|
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub netstring_read { |
58
|
0
|
0
|
|
0
|
1
|
|
my $sock = shift or return undef; |
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
|
0
|
|
|
|
|
defined($r = read($sock, $s, 1)) or return undef; |
67
|
|
|
|
|
|
|
|
68
|
0
|
0
|
|
|
|
|
return "" if !$r; |
69
|
0
|
0
|
|
|
|
|
last if $s eq ":"; |
70
|
0
|
0
|
|
|
|
|
return undef if $s !~ /^[0-9]$/; |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
$len = 10 * $len + $s; |
73
|
0
|
0
|
|
|
|
|
return undef if $len > 200000000; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
$ns = $len . ":"; |
77
|
0
|
|
|
|
|
|
$s = ""; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# read the string 'body' |
80
|
0
|
0
|
|
|
|
|
defined($r = read($sock, $s, $len)) or return undef; |
81
|
0
|
0
|
0
|
|
|
|
return "" if (!$r and $len != 0); # zero length is OK |
82
|
0
|
|
|
|
|
|
$ns .= $s; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
# read the trailing comma |
85
|
0
|
0
|
|
|
|
|
defined($r = read($sock, $s, 1)) or return undef; |
86
|
0
|
0
|
|
|
|
|
return "" if !$r; |
87
|
0
|
0
|
|
|
|
|
return undef if $s ne ","; |
88
|
0
|
|
|
|
|
|
$ns .= $s; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
return $ns; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |