ProtocolBuffers/CodecIV64.pm

filter: Statementstmt 100% 69 / 69 Branchbran 100% 24 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 8 totaltotal 93.2% 110 / 118 CC 13 SCAR 8.4
CC
13
Cov
100%
decode_varint
4.0
encode_sint
4.0
decode_sfixed64
3.0
CRAP 2.3
« ProtocolBuffers/Codec.pm ↑ Summary ProtocolBuffers/Constants.pm »
1 ##
2 ## Warning, despite name of the file, subrotines from here belongs
3 ## to Google::ProtocolBuffers::Codec namespace
4 ##
5 package Google::ProtocolBuffers::Codec;
6 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7
8 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 14 ▶no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use warnings FATAL => 'substr';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Math::BigInt;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 14 ▶no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use constant MAX_UINT64 => 0xFFFF_FFFF_FFFF_FFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use constant MAX_SINT64 => 0x7FFF_FFFF_FFFF_FFFF;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use constant MIN_SINT64 =>-0x8000_0000_0000_0000;
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 ## Signature of all encode_* subs:
19 ## encode_*($buffer, $value);
20 ## Encoded value of $value will be appended to $buffer, which is a string
21 ## passed by reference. No meaningfull value is returned, in case of errors
22 ## an exception it thrown.
23 ##
24 ## Signature of all encode_* subs:
25 ## my $value = decode_*($buffer, $position);
26 ## $buffer is a string passed by reference, no copy is performed and it
27 ## is not modified. $position is a number variable passed by reference
28 ## (index in the string $buffer where to start decoding of a value), it
29 ## is incremented by decode_* subs. In case of errors an exception is
30 ## thrown.
31
32 712 ▶sub decode_varint {
Subroutine decode_varint: called CC=4 SCAR=13.9
Pod undocumented
33 712 my $v = 0;
34 712 my $shift = 0;
35 712 my $l = length($_[0]);
36 712 while (1) {
37 1241 ▶ die BROKEN_MESSAGE() if $_[1] >= $l;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] >= $l 18 1223 1241
38 1223 my $b = ord(substr($_[0], $_[1]++, 1));
39 1223 $v += (($b & 0x7F) << $shift);
40 1223 $shift += 7;
41 1223 ▶ last if ($b & 0x80)==0;
Branch: 1 branch
BranchTrueFalseTotal
if ($b & 128) == 0 692 531 1223
42 531 ▶ die if $shift > 63;
Branch: 1 branch
BranchTrueFalseTotal
if $shift > 63 2 529 531
43 }
44 692 return $v;
45 }
46
47 ##
48 ## Both signed and unsigned 32/64 ints are encoded by this sub.
49 ## Must it be more restrictive and don't allow negative values for uint types?
50 ## Moreover, should we check that the number is an integer and not a float,
51 ## for example? And truncate int32 types to 32 bits?
52 ##
53 108 ▶sub encode_int {
Subroutine encode_int: called CC=2 SCAR=6.9
Pod undocumented
54 108 ▶ if ($_[1]>=0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] >= 0) { } 93 15 108
55 93 encode_varint($_[0], $_[1]);
56 } else {
57 ## We need a positive 64 bit integer, which bit representation is
58 ## the same as of this negative value, static_cast<uint64>(int64).
59 ## unpack('Q', pack('q', $_[1])) is slightly slower than
60 ## 2^64 + $v === (2^64-1) + $v + 1, for $v<0
61 15 encode_varint($_[0], (MAX_UINT64+$_[1])+1);
62 }
63 }
64
65 115 ▶sub decode_int {
Subroutine decode_int: called CC=2 SCAR=6.9
Pod undocumented
66 115 my $v = decode_varint(@_);
67 107 ▶ if ($v>MAX_SINT64()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v > 9223372036854775807) { } 18 89 107
68 18 return ($v-MAX_UINT64())-1;
69 } else {
70 89 return $v;
71 }
72 }
73
74 ##
75 ## $_[1]<<1 is subject to overflow: a value that fit into
76 ## Perl's int (IV) may need unsigned int (UV) to fit,
77 ## and I don't know how to make Perl do that cast.
78 ##
79 30 ▶sub encode_sint {
Subroutine encode_sint: called CC=4 SCAR=13.9
Pod undocumented
80 30 ▶ if ($_[1]>=MAX_SINT64()) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_[1] >= 9223372036854775807) { } 2 28 30
elsif ($_[1] <= -9223372036854775808) { } 2 26 28
elsif ($_[1] >= 0) { } 20 6 26
81 2 encode_varint($_[0], Math::BigInt->new($_[1])<<1);
82 } elsif ($_[1]<=MIN_SINT64) {
83 2 encode_varint($_[0], ((-Math::BigInt->new($_[1]))<<1)-1);
84 } elsif ($_[1]>=0) {
85 20 encode_varint($_[0], $_[1]<<1);
86 } else {
87 6 encode_varint($_[0], ((-$_[1])<<1)-1);
88 }
89 }
90
91 15 ▶sub encode_fixed64 {
Subroutine encode_fixed64: called CC=1 SCAR=0.0
Pod undocumented
92 15 $_[0] .= pack('V', $_[1] & 0xFFFF_FFFF);
93 15 $_[0] .= pack('V', $_[1] >> 32);
94 }
95
96 18 ▶sub decode_fixed64 {
Subroutine decode_fixed64: called CC=2 SCAR=6.9
Pod undocumented
97 18 ▶ die BROKEN_MESSAGE() if $_[1]+8 > length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] + 8 > length $_[0] 1 17 18
98 17 my $a = unpack('V', substr($_[0], $_[1], 4));
99 17 my $b = unpack('V', substr($_[0], $_[1]+4, 4));
100 17 $_[1] += 8;
101 17 return $a | ($b<<32);
102 }
103
104 16 ▶sub encode_sfixed64 {
Subroutine encode_sfixed64: called CC=2 SCAR=6.9
Pod undocumented
105 16 ▶ my $v = ($_[1]<0) ? (MAX_UINT64()+$_[1])+1 : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
$_[1] < 0 ? : 4 12 16
106 16 $_[0] .= pack('V', $v & 0xFFFF_FFFF);
107 16 $_[0] .= pack('V', $v >> 32);
108 }
109
110 19 ▶sub decode_sfixed64 {
Subroutine decode_sfixed64: called CC=3 SCAR=11.0
Pod undocumented
111 19 ▶ die BROKEN_MESSAGE() if $_[1]+8 > length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] + 8 > length $_[0] 1 18 19
112 18 my $a = unpack('V', substr($_[0], $_[1], 4));
113 18 my $b = unpack('V', substr($_[0], $_[1]+4, 4));
114 18 $_[1] += 8;
115 18 $a |= $b<<32;
116 18 ▶ if ($a>MAX_SINT64()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a > 9223372036854775807) { } 4 14 18
117 4 return ($a-MAX_UINT64())-1;
118 } else {
119 14 return $a;
120 }
121 }
122
123
124
125 14 1;
« ProtocolBuffers/Codec.pm ↑ Summary ProtocolBuffers/Constants.pm »