| 1 | package Net::SFTP::Foreign::Buffer; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | our $VERSION = '1.68_05'; | |||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | ▶ | use constant HAS_QUADS => do { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 3 | local $@; | |||||||||
| 13 | 3 | local $SIG{__DIE__}; | |||||||||
| 14 | 3 | ▶ | no warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 3 | eval q{ | |||||||||
| 16 | pack(Q => 0x1122334455667788) eq "\x11\x22\x33\x44\x55\x66\x77\x88" | ||||||||||
| 17 | } | ||||||||||
| 18 | 3 | }; | |||||||||
| 19 | |||||||||||
| 20 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 21 | 0 | my $class = shift; | |||||||||
| 22 | 0 | my $data = ''; | |||||||||
| 23 | 0 | ▶ | @_ and put(\$data, @_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | bless \$data, $class; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 0 | ▶ | sub make { bless \$_[1], $_[0] } | ||||||||
|
Subroutine
make: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 28 | |||||||||||
| 29 | 0 | ▶ | sub bytes { ${$_[0]} } | ||||||||
|
Subroutine
bytes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 30 | |||||||||||
| 31 | 0 | ▶ | sub get_int8 { | ||||||||
|
Subroutine
get_int8: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 32 | 0 | ▶ | length ${$_[0]} >=1 or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 0 | unpack(C => substr(${$_[0]}, 0, 1, '')); | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | sub get_int16 { | ||||||||
|
Subroutine
get_int16: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 37 | 0 | ▶ | length ${$_[0]} >=2 or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | unpack(n => substr(${$_[0]}, 0, 2, '')); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | sub get_int32 { | ||||||||
|
Subroutine
get_int32: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 42 | 0 | ▶ | length ${$_[0]} >=4 or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | unpack(N => substr(${$_[0]}, 0, 4, '')); | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 0 | ▶ | sub get_int32_untaint { | ||||||||
|
Subroutine
get_int32_untaint: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 47 | 0 | my ($v) = substr(${$_[0]}, 0, 4, '') =~ /(.*)/s; | |||||||||
| 48 | 0 | get_int32(\$v); | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub get_int64_quads { | ||||||||
|
Subroutine
get_int64_quads: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 52 | 0 | ▶ | length ${$_[0]} >= 8 or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 0 | unpack Q => substr(${$_[0]}, 0, 8, '') | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 0 | ▶ | sub get_int64_no_quads { | ||||||||
|
Subroutine
get_int64_no_quads: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 57 | 0 | ▶ | length ${$_[0]} >= 8 or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 0 | my ($big, $small) = unpack(NN => substr(${$_[0]}, 0, 8, '')); | |||||||||
| 59 | 0 | ▶ | if ($big) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | # too big for an integer, try to handle it as a float: | ||||||||||
| 61 | 0 | my $high = $big * 4294967296; | |||||||||
| 62 | 0 | my $result = $high + $small; | |||||||||
| 63 | 0 | ▶ | unless ($result - $high == $small) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | # too big event for a float, use a BigInt; | ||||||||||
| 65 | 0 | require Math::BigInt; | |||||||||
| 66 | 0 | $result = Math::BigInt->new($big); | |||||||||
| 67 | 0 | $result <<= 32; | |||||||||
| 68 | 0 | $result += $small; | |||||||||
| 69 | } | ||||||||||
| 70 | 0 | return $result; | |||||||||
| 71 | } | ||||||||||
| 72 | 0 | return $small; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 3 | *get_int64 = (HAS_QUADS ? \&get_int64_quads : \&get_int64_no_quads); | |||||||||
| 76 | |||||||||||
| 77 | 0 | ▶ | sub get_int64_untaint { | ||||||||
|
Subroutine
get_int64_untaint: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 78 | 0 | my ($v) = substr(${$_[0]}, 0, 8, '') =~ /(.*)/s; | |||||||||
| 79 | 0 | get_int64(\$v); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 0 | ▶ | sub get_str { | ||||||||
|
Subroutine
get_str: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 83 | 0 | my $self = shift; | |||||||||
| 84 | 0 | ▶ | length $$self >=4 or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | my $len = unpack(N => substr($$self, 0, 4, '')); | |||||||||
| 86 | 0 | ▶ | length $$self >=$len or return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | substr($$self, 0, $len, ''); | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 0 | ▶ | sub get_str_list { | ||||||||
|
Subroutine
get_str_list: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 91 | 0 | my $self = shift; | |||||||||
| 92 | 0 | my @a; | |||||||||
| 93 | 0 | ▶ | if (my $n = $self->get_int32) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | for (1..$n) { | |||||||||
| 95 | 0 | my $str = $self->get_str; | |||||||||
| 96 | 0 | ▶ | last unless defined $str; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | push @a, $str; | |||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | 0 | return @a; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 0 | ▶ | sub get_attributes { Net::SFTP::Foreign::Attributes->new_from_buffer($_[0]) } | ||||||||
|
Subroutine
get_attributes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 104 | |||||||||||
| 105 | |||||||||||
| 106 | 0 | ▶ | sub skip_bytes { substr(${$_[0]}, 0, $_[1], '') } | ||||||||
|
Subroutine
skip_bytes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 107 | |||||||||||
| 108 | 0 | ▶ | sub skip_str { | ||||||||
|
Subroutine
skip_str: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 109 | 0 | my $self = shift; | |||||||||
| 110 | 0 | my $len = $self->get_int32; | |||||||||
| 111 | 0 | substr($$self, 0, $len, ''); | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | sub put_int8 { ${$_[0]} .= pack(C => $_[1]) } | ||||||||
|
Subroutine
put_int8: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 115 | |||||||||||
| 116 | 0 | ▶ | sub put_int32 { ${$_[0]} .= pack(N => $_[1]) } | ||||||||
|
Subroutine
put_int32: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 117 | |||||||||||
| 118 | 0 | ▶ | sub put_int64_quads { ${$_[0]} .= pack(Q => $_[1]) } | ||||||||
|
Subroutine
put_int64_quads: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 119 | |||||||||||
| 120 | 0 | ▶ | sub put_int64_no_quads { | ||||||||
|
Subroutine
put_int64_no_quads: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 121 | 0 | ▶ | if ($_[1] >= 4294967296) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 0 | my $high = int ( $_[1] / 4294967296); | |||||||||
| 123 | 0 | my $low = int ($_[1] - $high * 4294967296); | |||||||||
| 124 | 0 | ${$_[0]} .= pack(NN => $high, $low) | |||||||||
| 125 | } | ||||||||||
| 126 | else { | ||||||||||
| 127 | 0 | ${$_[0]} .= pack(NN => 0, $_[1]) | |||||||||
| 128 | } | ||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 3 | *put_int64 = (HAS_QUADS ? \&put_int64_quads : \&put_int64_no_quads); | |||||||||
| 132 | |||||||||||
| 133 | 0 | ▶ | sub put_str { | ||||||||
|
Subroutine
put_str: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 134 | 0 | ▶ | utf8::downgrade($_[1]) or croak "UTF8 data reached the SFTP buffer"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | ${$_[0]} .= pack(N => length($_[1])) . $_[1] | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | 0 | ▶ | sub put_char { ${$_[0]} .= $_[1] } | ||||||||
|
Subroutine
put_char: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 139 | |||||||||||
| 140 | 0 | ▶ | sub _attrs_as_buffer { | ||||||||
|
Subroutine
_attrs_as_buffer: not called CC=2 SCAR=17.9
| |||||||||||
| 141 | 0 | my $attrs = shift; | |||||||||
| 142 | 0 | my $ref = ref $attrs; | |||||||||
| 143 | 0 | ▶ | Net::SFTP::Foreign::Attributes->isa($ref) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | or croak("Object of class Net::SFTP::Foreign::Attributes " | ||||||||||
| 145 | . "expected, $ref found"); | ||||||||||
| 146 | 0 | $attrs->as_buffer; | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 0 | ▶ | sub put_attributes { ${$_[0]} .= ${_attrs_as_buffer $_[1]} } | ||||||||
|
Subroutine
put_attributes: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 150 | |||||||||||
| 151 | 3 | my %unpack = ( int8 => \&get_int8, | |||||||||
| 152 | int32 => \&get_int32, | ||||||||||
| 153 | int64 => \&get_int64, | ||||||||||
| 154 | str => \&get_str, | ||||||||||
| 155 | attr => \&get_attributtes ); | ||||||||||
| 156 | |||||||||||
| 157 | 0 | ▶ | sub get { | ||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 158 | 0 | my $buf = shift; | |||||||||
| 159 | 0 | map { $unpack{$_}->($buf) } @_; | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | 0 | ▶ | my %pack = ( int8 => sub { pack C => $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 163 | 0 | ▶ | int32 => sub { pack N => $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 164 | 0 | ▶ | int64 => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 165 | 0 | if (HAS_QUADS) { | |||||||||
| 166 | return pack(Q => $_[0]) | ||||||||||
| 167 | } | ||||||||||
| 168 | else { | ||||||||||
| 169 | 0 | ▶ | if ($_[0] >= 4294967296) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 0 | my $high = int ( $_[0] / 4294967296); | |||||||||
| 171 | 0 | my $low = int ($_[0] - $high * 4294967296); | |||||||||
| 172 | 0 | return pack(NN => $high, $low) | |||||||||
| 173 | } | ||||||||||
| 174 | else { | ||||||||||
| 175 | 0 | return pack(NN => 0, $_[0]) | |||||||||
| 176 | } | ||||||||||
| 177 | } | ||||||||||
| 178 | }, | ||||||||||
| 179 | 0 | ▶ | str => sub { pack(N => length($_[0])), $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 180 | 0 | ▶ | char => sub { $_[0] }, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 181 | 0 | ▶ | attr => sub { ${_attrs_as_buffer $_[0]} } ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||
| 182 | |||||||||||
| 183 | 0 | ▶ | sub put { | ||||||||
|
Subroutine
put: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 184 | 0 | my $buf =shift; | |||||||||
| 185 | 0 | ▶ | @_ & 1 and croak "bad number of arguments for put (@_)"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 186 | 0 | my @parts; | |||||||||
| 187 | 0 | ▶ | while (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 188 | 0 | my $type = shift; | |||||||||
| 189 | 0 | my $value = shift; | |||||||||
| 190 | 0 | ▶ | my $packer = $pack{$type} or Carp::confess("internal error: bad packing type '$type'"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 191 | 0 | push @parts, $packer->($value) | |||||||||
| 192 | } | ||||||||||
| 193 | 0 | $$buf.=join('', @parts); | |||||||||
| 194 | } | ||||||||||
| 195 | |||||||||||
| 196 | 3 | 1; | |||||||||
| 197 | __END__ | ||||||||||