ProtocolBuffers/Codec.pm

filter: Statementstmt 90.6% 280 / 309 Branchbran 75.8% 94 / 124 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 7 / 7 Subroutinesub 90.4% 38 / 42 Podpod 0.0% 0 / 21 totaltotal 83.6% 431 / 515 CC 72 SCAR 22.7
CC
72
Cov
87%
encode
23.2
_decode_partial
18.5
_skip_unknown_field
8.5
CRAP 9.7
« ProtocolBuffers/CodeGen.pm ↑ Summary ProtocolBuffers/CodecIV64.pm »
1 package Google::ProtocolBuffers::Codec;
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 ## FATAL substr warnings ("substring outside of string") was intended
5 ## to report about incomplete messages.
6 ## However, substr("abc", 3, 1) returns chr(0) without warning.
7 ## Thats why the code below has to check length of string and
8 ## substring index manually
9 14 ▶use warnings FATAL => 'substr';
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 14 ▶use Config qw/%Config/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Google::ProtocolBuffers::Constants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 14 ▶use constant BROKEN_MESSAGE => "Mesage is incomplete or invalid";
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use constant MAX_UINT32 => 0xffff_ffff;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use constant MAX_SINT32 => 0x7fff_ffff;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 14 ▶use constant MIN_SINT32 =>-0x8000_0000;
Subroutine BEGIN: called CC=1 SCAR=0.0
19
20 14 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=15.1
21 ## Protocol Buffer standard requires support of 64-bit integers.
22 ## If platform doen't support them internally, they will be emulated
23 ## by Math::BigInt number.
24 ## Libraries below contains identically named funtions that are either
25 ## use native 64-bit ints or Math::BigInts
26 14 my $ivsize = $Config{ivsize};
27 14 ▶ if ($ivsize>=8) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ivsize >= 8) { } 14 0 14
elsif ($ivsize == 4) { } 0 0 0
28 14 require 'Google/ProtocolBuffers/CodecIV64.pm';
29 } elsif ($ivsize==4) {
30 0 require 'Google/ProtocolBuffers/CodecIV32.pm';
31 } else {
32 0 die "Unsupported size of internal Perl IntegerValue: '$ivsize' bytes.";
33 }
34 }
35
36 14 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=14.2
37 ## Floats and doubles are packed in their native format,
38 ## which is different on big-endian and litte-endian platforms
39 ## Maybe create and load one of two files, like CodecIV* above?
40 14 my $bo = $Config{byteorder};
41 14 ▶ if ($bo =~ '^1234') {
Branch: 2 branches
BranchTrueFalseTotal
if ($bo =~ /^1234/) { } 14 0 14
elsif ($bo =~ /4321$/) { } 0 0 0
42 ## little-endian platform
43 14 *encode_float = \&encode_float_le;
44 14 *decode_float = \&decode_float_le;
45 14 *encode_double = \&encode_double_le;
46 14 *decode_double = \&decode_double_le;
47 } elsif ($bo =~ '4321$') {
48 ## big-endian
49 0 *encode_float = \&encode_float_be;
50 0 *decode_float = \&decode_float_be;
51 0 *encode_double = \&encode_double_be;
52 0 *decode_double = \&decode_double_be;
53 }
54 }
55
56 14 my @primitive_type_encoders;
57 14 $primitive_type_encoders[TYPE_DOUBLE] = \&encode_double;
58 14 $primitive_type_encoders[TYPE_FLOAT] = \&encode_float;
59 14 $primitive_type_encoders[TYPE_INT64] = \&encode_int;
60 14 $primitive_type_encoders[TYPE_UINT64] = \&encode_uint;
61 14 $primitive_type_encoders[TYPE_INT32] = \&encode_int;
62 14 $primitive_type_encoders[TYPE_FIXED64] = \&encode_fixed64;
63 14 $primitive_type_encoders[TYPE_FIXED32] = \&encode_fixed32;
64 14 $primitive_type_encoders[TYPE_BOOL] = \&encode_bool;
65 14 $primitive_type_encoders[TYPE_STRING] = \&encode_string;
66 14 $primitive_type_encoders[TYPE_BYTES] = \&encode_string;
67 14 $primitive_type_encoders[TYPE_UINT32] = \&encode_uint;
68 14 $primitive_type_encoders[TYPE_ENUM] = \&encode_int;
69 14 $primitive_type_encoders[TYPE_SFIXED64] = \&encode_sfixed64;
70 14 $primitive_type_encoders[TYPE_SFIXED32] = \&encode_sfixed32;
71 14 $primitive_type_encoders[TYPE_SINT32] = \&encode_sint;
72 14 $primitive_type_encoders[TYPE_SINT64] = \&encode_sint;
73
74 14 my @primitive_type_decoders;
75 14 $primitive_type_decoders[TYPE_DOUBLE] = \&decode_double;
76 14 $primitive_type_decoders[TYPE_FLOAT] = \&decode_float;
77 14 $primitive_type_decoders[TYPE_INT64] = \&decode_int;
78 14 $primitive_type_decoders[TYPE_UINT64] = \&decode_uint;
79 14 $primitive_type_decoders[TYPE_INT32] = \&decode_int;
80 14 $primitive_type_decoders[TYPE_FIXED64] = \&decode_fixed64;
81 14 $primitive_type_decoders[TYPE_FIXED32] = \&decode_fixed32;
82 14 $primitive_type_decoders[TYPE_BOOL] = \&decode_bool;
83 14 $primitive_type_decoders[TYPE_STRING] = \&decode_string;
84 14 $primitive_type_decoders[TYPE_BYTES] = \&decode_string;
85 14 $primitive_type_decoders[TYPE_UINT32] = \&decode_uint;
86 14 $primitive_type_decoders[TYPE_ENUM] = \&decode_int;
87 14 $primitive_type_decoders[TYPE_SFIXED64] = \&decode_sfixed64;
88 14 $primitive_type_decoders[TYPE_SFIXED32] = \&decode_sfixed32;
89 14 $primitive_type_decoders[TYPE_SINT32] = \&decode_sint;
90 14 $primitive_type_decoders[TYPE_SINT64] = \&decode_sint;
91
92 14 my @wire_types;
93 14 $wire_types[TYPE_DOUBLE] = WIRETYPE_FIXED64;
94 14 $wire_types[TYPE_FLOAT] = WIRETYPE_FIXED32;
95 14 $wire_types[TYPE_INT64] = WIRETYPE_VARINT;
96 14 $wire_types[TYPE_UINT64] = WIRETYPE_VARINT;
97 14 $wire_types[TYPE_INT32] = WIRETYPE_VARINT;
98 14 $wire_types[TYPE_FIXED64] = WIRETYPE_FIXED64;
99 14 $wire_types[TYPE_FIXED32] = WIRETYPE_FIXED32;
100 14 $wire_types[TYPE_BOOL] = WIRETYPE_VARINT;
101 14 $wire_types[TYPE_STRING] = WIRETYPE_LENGTH_DELIMITED;
102 ## these types were removed deliberatly from the list,
103 ## since they must be serialized by their own classes
104 ##$wire_types[TYPE_GROUP]
105 ##$wire_types[TYPE_MESSAGE]
106 14 $wire_types[TYPE_BYTES] = WIRETYPE_LENGTH_DELIMITED;
107 14 $wire_types[TYPE_UINT32] = WIRETYPE_VARINT;
108 ## we create a special class for each enum, but these classes
109 ## are just namespaces for constants. User can create a message
110 ## field with type=TYPE_ENUM and integer value.
111 14 $wire_types[TYPE_ENUM] = WIRETYPE_VARINT;
112 14 $wire_types[TYPE_SFIXED32] = WIRETYPE_FIXED32;
113 14 $wire_types[TYPE_SFIXED64] = WIRETYPE_FIXED64;
114 14 $wire_types[TYPE_SINT32] = WIRETYPE_VARINT;
115 14 $wire_types[TYPE_SINT64] = WIRETYPE_VARINT;
116
117
118 ##
119 ## Class or instance method.
120 ## Must not be called directly, only as a method of derived class.
121 ##
122 ## Input: data structure (hash-ref)
123 ## Output: in-memory string with serialized data
124 ##
125 ## Example:
126 ## my $str = My::Message->encode({a => 1});
127 ## or
128 ## my $message = bless {a => 1}, 'My::Message';
129 ## my $str = $message->encode;
130 ##
131 sub encode
132 218 ▶{
Subroutine encode: called CC=23 SCAR=31.4
Pod undocumented
133 218 my $self = shift;
134 218 ▶ my $data = (ref $self) ? $self : shift();
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 1 217 218
135
136 ##unless (ref $data eq 'HASH') {
137 ## my $class = ref $self || $self;
138 ## die "Hashref was expected for $self->encode; found '$data' instead";
139 ##}
140
141 218 my $buf = '';
142 218 foreach my $field (@{ $self->_pb_fields_list }) {
143 12666 my ($cardinality, $type, $name, $field_number, $default) = @$field;
144 ## Check mising values and their cardinality (i.e. label): required, optional or repeated.
145 ## For required fields, put a default value into stream, if exists, and raise an error otherwise.
146 12666 my $value = $data->{$name};
147 12666 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 12406 260 12666
148 12406 ▶ if ($cardinality==LABEL_REQUIRED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cardinality == 2) { } 11 12395 12406
149 11 ▶ if (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default) { } 11 0 11
150 11 $value = $default;
151 } else {
152 0 die "Required field '$name' is missing in $self";
153 }
154 } else {
155 12395 next;
156 }
157 }
158
159 271 ▶ if (ref $value && ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and ref $value eq "ARRAY") 47 224 271
Condition100%3/3
ref $value and ref $value eq "ARRAY"
!ll&&!rl&&r
1943047
MC/DC100%2/2
ref $value and ref $value eq "ARRAY"
ref $value ref $value eq "ARRAY"
Truth table100%3/3
A and B
A= ref $valueB= ref $value eq "ARRAY"
ABresult
0X0
100
111
160 47 ▶ if ($cardinality!=LABEL_REPEATED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cardinality != 3) 0 47 47
161 ## Oops, several values were given for a non-repeated field.
162 ## We'll take the last one - the specification states that
163 ## if several (non-repeaded) fields are in a stream,
164 ## the last one must be taken
165 0 $value = $value->[-1];
166 }
167 }
168 271 ▶ my $is_repeated = ref $value && ref $value eq 'ARRAY';
Condition100%3/3
ref $value && ref $value eq 'ARRAY'
!ll&&!rl&&r
1943047
MC/DC100%2/2
ref $value && ref $value eq 'ARRAY'
ref $value ref $value eq 'ARRAY'
Truth table100%3/3
A && B
A= ref $valueB= ref $value eq 'ARRAY'
ABresult
0X0
100
111
169
170 271 $field_number <<= 3;
171
172 14 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
173 271 my $encoder = $primitive_type_encoders[$type];
174 14 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
175
176 271 ▶ if ($encoder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoder) { } 235 36 271
177 ##
178 ## this field is one of the base types
179 ##
180 235 ▶ die $type unless exists $wire_types[$type];
Branch: 1 branch
BranchTrueFalseTotal
unless exists $wire_types[$type] 0 235 235
181 235 ▶ if (!$is_repeated) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_repeated) { } 197 38 235
182 197 encode_varint($buf, $field_number | $wire_types[$type]);
183 197 $encoder->($buf, $value);
184 } else {
185 38 my $key;
186 38 encode_varint($key, $field_number | $wire_types[$type]);
187 38 foreach my $v (@$value) {
188 72 $buf .= $key;
189 72 $encoder->($buf, $v);
190 }
191 }
192 } else {
193 ##
194 ## This field is one of complex types: another message, group or enum
195 ##
196 36 my $kind = $type->_pb_complex_type_kind;
197 36 ▶ if ($kind==MESSAGE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($kind == 1) { } 17 19 36
elsif ($kind == 3) { } 12 7 19
elsif ($kind == 2) { } 7 0 7
198 17 ▶ if (!$is_repeated) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_repeated) { } 11 6 17
199 11 encode_varint($buf, $field_number | WIRETYPE_LENGTH_DELIMITED);
200 11 my $message = $type->encode($value);
201 11 encode_varint($buf, length($message));
202 11 $buf .= $message;
203 } else {
204 6 my $key;
205 6 encode_varint($key, $field_number | WIRETYPE_LENGTH_DELIMITED);
206 6 foreach my $v (@$value) {
207 11 $buf .= $key;
208 11 my $message = $type->encode($v);
209 11 encode_varint($buf, length($message));
210 11 $buf .= $message;
211 }
212 }
213 }
214 elsif ($kind==ENUM) {
215 12 ▶ if (!$is_repeated) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_repeated) { } 11 1 12
216 11 encode_varint($buf, $field_number | WIRETYPE_VARINT);
217 11 encode_int($buf, $value);
218 } else {
219 1 my $key;
220 1 encode_varint($key, $field_number | WIRETYPE_VARINT);
221 1 foreach my $v (@$value) {
222 2 $buf .= $key;
223 2 encode_int($buf, $v);
224 }
225 }
226 }
227 elsif ($kind==GROUP) {
228 7 ▶ if (!$is_repeated) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_repeated) { } 5 2 7
229 5 encode_varint($buf, $field_number | WIRETYPE_START_GROUP);
230 5 $buf .= encode($type, $value);
231 5 encode_varint($buf, $field_number | WIRETYPE_END_GROUP);
232 } else {
233 2 my ($start,$end);
234 2 encode_varint($start, $field_number | WIRETYPE_START_GROUP);
235 2 encode_varint($end, $field_number | WIRETYPE_END_GROUP);
236 2 foreach my $v (@$value) {
237 3 $buf .= $start;
238 3 $buf .= encode($type, $v);
239 3 $buf .= $end;
240 }
241 }
242 } else {
243 0 die "Unkown type: $type ($kind)";
244 }
245 }
246 }
247 218 return $buf;
248 }
249
250 ##
251 ## Class method.
252 ## Must not be called directly, only as a method of derived class
253 ##
254 ## Input: string of serialized data
255 ## Output: data structure (hashref)
256 ## If serialized data contains errors, an exception will be thrown.
257 ##
258 ## Example:
259 ## my $data = My::Message->decode($str);
260 ## ## $data is now a hashref like this: {a => 1}
261 ##
262 243 ▶sub decode {
Subroutine decode: called CC=2 SCAR=7.4
Pod undocumented
263 243 my $class = shift;
264
265 ## position must be a modifiable variable (it's passed by reference
266 ## to all decode subroutines, that call each other recursively)
267 ## It's slightly quicker then passing it as an object attribute
268 ## ($self->{pos}) to each method, but readability is poor.
269 243 my $pos = 0;
270 243 ▶ if (Encode::is_utf8($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($_[0])) 0 243 243
271 ## oops, wide-character string, where did you get it from?
272 ## Should we silently encode it to utf-8 and then process
273 ## the resulted byte-string?
274 0 die "Input data string is a wide-character string";
275 }
276 243 return _decode_partial($class, $_[0], $pos, length($_[0]));
277 }
278
279 ##
280 ## Internal method, decodes both Messages and Groups
281 ## Input:
282 ## data string,
283 ## start_position (passed by reference, this must be a variable),
284 ## length of message
285 ## Output:
286 ## for Messages: data structure
287 ## for Groups: (data structure, field number of ending group tag)
288 ##
289 282 ▶sub _decode_partial {
Subroutine _decode_partial: called CC=18 SCAR=29.2
290 282 my $class = shift;
291
292 282 my $length = $_[2];
293 282 my $end_position = $_[1]+$length;
294
295 282 my $data = bless {}, $class;
296 282 my $fields = $class->_pb_fields_by_number;
297
298 PAIR:
299 282 ▶ while ($_[1] < $end_position) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] < $end_position) 431 242 673
300 431 my $v = decode_varint($_[0], $_[1]);
301 430 my ($field_number, $wire_type) = ($v>>3, $v&7);
302
303 430 ▶ if ($wire_type==WIRETYPE_END_GROUP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wire_type == 4) 10 420 430
304 10 ▶ if ($class->_pb_complex_type_kind==GROUP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_pb_complex_type_kind == 2) { } 10 0 10
305 10 return ($data, $field_number);
306 } else {
307 0 die "Unexpected end of group in message";
308 }
309 }
310
311 420 ▶ if (my $field = $fields->{$field_number}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $field = $fields->{$field_number}) { } 398 22 420
312 398 my ($cardinality, $type, $name, $field_number_, $default) = @$field;
313 398 ▶ die unless $field_number_== $field_number;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_number_ == $field_number 0 398 398
314 398 my $value;
315
316 14 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
317 398 my $decoder = $primitive_type_decoders[$type];
318 14 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
319
320 398 ▶ if ($decoder) {
Branch: 1 branch
BranchTrueFalseTotal
if ($decoder) { } 345 53 398
321 345 ▶ if ($wire_type==WIRETYPE_LENGTH_DELIMITED && $type!=TYPE_STRING && $type!=TYPE_BYTES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wire_type == 2 and $type != 9 and $type != 12) { } 1 344 345
Condition100%6/6
$wire_type == 2 and $type != 9 and $type != 12
!ll&&!rl&&r
328161
$wire_type == 2 and $type != 9
!ll&&!rl&&r
2953317
MC/DC100%3/3
$wire_type == 2 and $type != 9 and $type != 12
$wire_type == 2 $type != 9 $type != 12
Truth table100%4/4
A and B and C
A= $wire_type == 2B= $type != 9C= $type != 12
ABCresult
0XX0
10X0
1100
1111
322 ##
323 ## Packed Repeated Fields:
324 ## <length of the field>; sequence of encoded <primitive values>
325 ##
326 ## order is important - $_[1] changed by decode_varint()
327 1 my $l = decode_varint($_[0], $_[1]); ## length of the packed field
328 1 my $e = $_[1] + $l; ## last position of the field
329
330 1 my @values;
331 1 ▶ while ($_[1]<$e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] < $e) 3 1 4
332 3 push @values, $decoder->($_[0], $_[1]);
333 }
334 1 ▶ if ($cardinality==LABEL_REPEATED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cardinality == 3) { } 1 0 1
335 1 push @{$data->{$name}}, @values;
336 } else {
337 0 $data->{$name} = $values[-1];
338 }
339 1 next PAIR;
340
341 } else {
342 ## regular primitive value, string or byte array
343 344 $value = $decoder->($_[0], $_[1]);
344 }
345 } else {
346 53 my $kind = $type->_pb_complex_type_kind;
347 53 ▶ if ($kind==MESSAGE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($kind == 1) { } 28 25 53
elsif ($kind == 3) { } 14 11 25
elsif ($kind == 2) { } 11 0 11
348 28 my $message_length = decode_varint($_[0], $_[1]);
349 28 $value = _decode_partial($type, $_[0], $_[1], $message_length);
350 } elsif ($kind==ENUM) {
351 14 $value = decode_int($_[0], $_[1]);
352 } elsif ($kind==GROUP) {
353 11 my $end_field_number;
354 11 ($value, $end_field_number) = _decode_partial($type, $_[0], $_[1], $end_position-$_[1]);
355 10 ▶ die unless $field_number == $end_field_number;
Branch: 1 branch
BranchTrueFalseTotal
unless $field_number == $end_field_number 0 10 10
356 } else {
357 0 die "Unkown type: $type ($kind)";
358 }
359 }
360 368 ▶ if ($cardinality==LABEL_REPEATED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cardinality == 3) { } 90 278 368
361 90 push @{$data->{$name}}, $value;
362 } else {
363 278 $data->{$name} = $value;
364 }
365 }
366 else {
367 22 _skip_unknown_field($_[0], $_[1], $field_number, $wire_type);
368 }
369 }
370
371 242 ▶ if ($class->_pb_complex_type_kind==GROUP) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->_pb_complex_type_kind == 2) { } 0 242 242
372 0 die "End of group token was not found";
373 } else {
374 242 return $data;
375 }
376 }
377
378 ##
379 ## Subroutines for skipping unknown fields
380 ##
381 ## _skip_unknown_field($buffer, $position, $field_number, $wire_type)
382 ## $buffer is immutable
383 ## $position will be advanced
384 ## $field_number is for groups only, and for checks that closing group
385 ## field_number equals to the (given) opening field_number
386 ## $wire_type is to know lenght of field to be skipped
387 ## Returns none
388 ##
389 24 ▶sub _skip_unknown_field {
Subroutine _skip_unknown_field: called CC=8 SCAR=21.4
390 24 my ($field_number, $wire_type) = ($_[2], $_[3]);
391
392 24 ▶ if ($wire_type==WIRETYPE_VARINT) {
Branch: 6 branches
BranchTrueFalseTotal
if ($wire_type == 0) { } 14 10 24
elsif ($wire_type == 1) { } 3 7 10
elsif ($wire_type == 2) { } 3 4 7
elsif ($wire_type == 3) { } 1 3 4
elsif ($wire_type == 4) { } 0 3 3
elsif ($wire_type == 5) { } 3 0 3
393 14 _skip_varint($_[0], $_[1]);
394 } elsif ($wire_type==WIRETYPE_FIXED64) {
395 3 $_[1] += 8;
396 } elsif ($wire_type==WIRETYPE_LENGTH_DELIMITED) {
397 3 my $len = decode_varint($_[0], $_[1]);
398 3 $_[1] += $len;
399 } elsif ($wire_type==WIRETYPE_START_GROUP) {
400 1 my $closing_field_number = _skip_until_end_of_group($_[0], $_[1]);
401 1 ▶ die unless $closing_field_number==$field_number;
Branch: 1 branch
BranchTrueFalseTotal
unless $closing_field_number == $field_number 0 1 1
402 } elsif ($wire_type==WIRETYPE_END_GROUP) {
403 0 die "Unexpected end of group";
404 } elsif ($wire_type==WIRETYPE_FIXED32) {
405 3 $_[1] += 4;
406 } else {
407 0 die "Unknown wire type $wire_type";
408 }
409 }
410
411 ##
412 ## _skip_until_end_of_group($buffer, $position);
413 ## Returns field_number of closing group tag
414 ##
415 1 ▶sub _skip_until_end_of_group {
Subroutine _skip_until_end_of_group: called CC=2 SCAR=6.9
416 1 while (1) {
417 3 my $v = decode_varint($_[0], $_[1]);
418 3 my ($field_number, $wire_type) = ($v>>3, $v&7);
419 3 ▶ return $field_number if $wire_type==WIRETYPE_END_GROUP;
Branch: 1 branch
BranchTrueFalseTotal
if $wire_type == 4 1 2 3
420 2 _skip_unknown_field($_[0], $_[1], $field_number, $wire_type);
421 }
422 }
423
424 ##
425 ## _skip_varint($buffer, $position)
426 ## Returns none
427 14 ▶sub _skip_varint {
Subroutine _skip_varint: called CC=4 SCAR=14.0
428 14 my $c = 0;
429 14 my $l = length($_[0]);
430 14 while (1) {
431 16 ▶ die BROKEN_MESSAGE() if $_[1] >= $l; ## if $_[1]+1 > $l
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] >= $l 0 16 16
432 16 ▶ last if (ord(substr($_[0], $_[1]++, 1)) & 0x80) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ord substr($_[0], $_[1]++, 1) & 128) == 0 14 2 16
433 2 ▶ die "Varint is too long" if ++$c>=9;
Branch: 1 branch
BranchTrueFalseTotal
if ++$c >= 9 0 2 2
434 }
435 }
436
437 ##
438 ## Implementations of primitive types serialization/deserialization are
439 ## below. Some of subroutines are defined in IV32/IV64 modules.
440 ##
441 ## Signature of all encode_* subs:
442 ## encode_*($buffer, $value);
443 ## Encoded value of $value will be appended to $buffer, which is a string
444 ## passed by reference. No meaningfull value is returned, in case of errors
445 ## an exception it thrown.
446 ##
447 ## Signature of all encode_* subs:
448 ## my $value = decode_*($buffer, $position);
449 ## $buffer is a string passed by reference, no copy is performed and it
450 ## is not modified. $position is a number variable passed by reference
451 ## (index in the string $buffer where to start decoding of a value), it
452 ## is incremented by decode_* subs. In case of errors an exception is
453 ## thrown.
454 ##
455 ## Sorry for poor readability, these subroutines were optimized for speed.
456 ## Most probably, they (and this module entirely) should be written in XS
457 ##
458
459 ##
460 ## type: varint
461 ##
462 ## Our implementation of varint knows about positive numbers only.
463 ## It's caller's responsibility to convert negative values into
464 ## 64-bit positives
465 ##
466 485 ▶sub encode_varint {
Subroutine encode_varint: called CC=4 SCAR=14.0
Pod undocumented
467 485 my $v = $_[1];
468 485 ▶ die "Varint is negative" if $v < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $v < 0 0 485 485
469 485 my $c = 0;
470 485 ▶ while ($v > 0x7F) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v > 127) 377 485 862
471 377 $_[0] .= chr( ($v&0x7F) | 0x80 );
472 377 $v >>= 7;
473 377 ▶ die "Number is too long" if ++$c >= 10;
Branch: 1 branch
BranchTrueFalseTotal
if ++$c >= 10 0 377 377
474 }
475 485 $_[0] .= chr( ($v&0x7F) );
476 }
477 ## sub decode_varint - word-size sensitive
478
479 ##
480 ## type: unsigend int (32/64)
481 ##
482 ## sub encode_uint - word-size sensitive
483 14 *encode_uint = \&encode_int;
484
485 ## decode_varint always returns positive value
486 32 ▶sub decode_uint {
Subroutine decode_uint: called CC=1 SCAR=0.0
Pod undocumented
487 32 return decode_varint(@_);
488 }
489
490 ##
491 ## type: signed int (32/64)
492 ##
493 ## Signed zigzag-encode integers
494 ## Acutally, zigzag encoded value is just ($v>0) ? $v*2 : (-$v)*2-1;
495 ##
496
497 38 ▶sub decode_sint {
Subroutine decode_sint: called CC=2 SCAR=6.9
Pod undocumented
498 38 my $v = decode_varint(@_);
499 34 ▶ if ($v & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v & 1) { } 8 26 34
500 ## warning: -(($v+1)>>1) may cause overflow
501 8 return -(1 + (($v-1)>>1))
502 } else {
503 26 return $v>>1;
504 }
505 }
506
507 ##
508 ## type: boolean
509 ##
510 9 ▶sub encode_bool {
Subroutine encode_bool: called CC=2 SCAR=6.9
Pod undocumented
511 9 ▶ if ($_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) { } 5 4 9
512 5 encode_varint($_[0], 1);
513 } else {
514 4 encode_varint($_[0], 0);
515 }
516 }
517
518 12 ▶sub decode_bool {
Subroutine decode_bool: called CC=2 SCAR=6.9
Pod undocumented
519 12 ▶ return (decode_varint(@_)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
decode_varint(@_) ? : 6 5 11
520 }
521
522 ##
523 ## type: unsigned fixed 64-bit int
524 ##
525 ##sub encode_fixed64 - word-size sensitive
526 ##sub decode_fixed64 - word-size sensitive
527
528 ##
529 ## type: signed fixed 64-bit int
530 ##
531 ##sub encode_sfixed64 - word-size sensitive
532 ##sub decode_sfixed64 - word-size sensitive
533
534 ##
535 ## type: double
536 ##
537 ## little-endian versions
538 16 ▶sub encode_double_le {
Subroutine encode_double_le: called CC=1 SCAR=0.0
Pod undocumented
539 16 $_[0] .= pack('d', $_[1]);
540 }
541 19 ▶sub decode_double_le {
Subroutine decode_double_le: called CC=2 SCAR=6.9
Pod undocumented
542 19 ▶ die BROKEN_MESSAGE() if $_[1]+8 > length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] + 8 > length $_[0] 1 18 19
543 18 my $v = unpack('d', substr($_[0], $_[1], 8));
544 18 $_[1] += 8;
545 18 return $v;
546 }
547
548 ## big-endian versions
549 0 ▶sub encode_double_be {
Subroutine encode_double_be: not called CC=1 SCAR=6.9
Pod undocumented
550 0 $_[0] .= reverse pack('d', $_[1]);
551 }
552 0 ▶sub decode_double_be {
Subroutine decode_double_be: not called CC=2 SCAR=17.9
Pod undocumented
553 0 ▶ die BROKEN_MESSAGE() if $_[1]+8 > length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] + 8 > length $_[0] 0 0 0
554 0 my $v = unpack('d', reverse substr($_[0], $_[1], 8));
555 0 $_[1] += 8;
556 0 return $v;
557 }
558
559 ##
560 ## type: string and bytes
561 ##
562 38 ▶sub encode_string {
Subroutine encode_string: called CC=3 SCAR=12.3
Pod undocumented
563 14 ▶ use Carp; Carp::cluck("Undefined string") unless defined $_[1];
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 0 38 38
564 38 ▶ if (Encode::is_utf8($_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($_[1])) { } 0 38 38
565 ## Ops, the string has wide-characters.
566 ## Well, encode them to utf-8 bytes.
567 0 my $v = Encode::encode_utf8($_[1]);
568 0 encode_varint($_[0], length($v));
569 0 $_[0] .= $v;
570 } else {
571 38 encode_varint($_[0], length($_[1]));
572 38 $_[0] .= $_[1];
573 }
574 }
575
576 49 ▶sub decode_string {
Subroutine decode_string: called CC=2 SCAR=6.9
Pod undocumented
577 49 my $length = decode_varint(@_);
578 47 ▶ die BROKEN_MESSAGE() if $_[1]+$length > length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] + $length > length $_[0] 2 45 47
579 45 my $str = substr($_[0], $_[1], $length);
580 45 $_[1] += $length;
581 45 return $str;
582 }
583
584 ##
585 ## type: unsigned 32-bit
586 ##
587 12 ▶sub encode_fixed32 {
Subroutine encode_fixed32: called CC=1 SCAR=0.0
Pod undocumented
588 12 $_[0] .= pack('V', $_[1]);
589 }
590 15 ▶sub decode_fixed32 {
Subroutine decode_fixed32: called CC=2 SCAR=6.9
Pod undocumented
591 15 ▶ die BROKEN_MESSAGE() if $_[1]+4 > length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] + 4 > length $_[0] 1 14 15
592 14 my $v = unpack('V', substr($_[0], $_[1], 4));
593 14 $_[1] += 4;
594 14 return $v;
595 }
596
597 ##
598 ## type: signed 32-bit
599 ##
600 13 ▶sub encode_sfixed32 {
Subroutine encode_sfixed32: called CC=1 SCAR=0.0
Pod undocumented
601 13 $_[0] .= pack('V', $_[1]);
602 }
603 16 ▶sub decode_sfixed32 {
Subroutine decode_sfixed32: called CC=3 SCAR=11.0
Pod undocumented
604 16 ▶ die BROKEN_MESSAGE() if $_[1]+4 > length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] + 4 > length $_[0] 1 15 16
605 15 my $v = unpack('V', substr($_[0], $_[1], 4));
606 15 $_[1] += 4;
607 15 ▶ return ($v>MAX_SINT32()) ? ($v-MAX_UINT32())-1 : $v;
Branch: 1 branch
BranchTrueFalseTotal
$v > 2147483647 ? : 3 12 15
608 }
609
610 ##
611 ## type: float
612 ##
613 25 ▶sub encode_float_le {
Subroutine encode_float_le: called CC=1 SCAR=0.0
Pod undocumented
614 25 $_[0] .= pack('f', $_[1]);
615 }
616 28 ▶sub decode_float_le {
Subroutine decode_float_le: called CC=2 SCAR=6.9
Pod undocumented
617 28 ▶ die BROKEN_MESSAGE() if $_[1]+4 > length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] + 4 > length $_[0] 1 27 28
618 27 my $v = unpack('f', substr($_[0], $_[1], 4));
619 27 $_[1] += 4;
620 27 return $v;
621 }
622
623 0 ▶sub encode_float_be {
Subroutine encode_float_be: not called CC=1 SCAR=6.9
Pod undocumented
624 0 $_[0] .= reverse pack('f', $_[1]);
625 }
626 0 ▶sub decode_float_be {
Subroutine decode_float_be: not called CC=2 SCAR=17.9
Pod undocumented
627 0 ▶ die BROKEN_MESSAGE() if $_[1]+4 > length($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] + 4 > length $_[0] 0 0 0
628 0 my $v = unpack('f', reverse substr($_[0], $_[1], 4));
629 0 $_[1] += 4;
630 0 return $v;
631 }
632
633
« ProtocolBuffers/CodeGen.pm ↑ Summary ProtocolBuffers/CodecIV64.pm »