Reader/Decoder.pm

filter: Statementstmt 100% 191 / 191 Branchbran 96.0% 48 / 50 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 32 / 32 Podpod 0.0% 0 / 1 totaltotal 98.9% 271 / 274 CC 29 SCAR 14.8
CC
29
Cov
99%
decode
13.0
_decode_uint
4.0
_size_from_ctrl_byte
4.0
CRAP 4.4
« Reader/PP.pm ↑ Summary Reader/Role/Sysreader.pm »
1 package MaxMind::DB::Reader::Decoder;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 our $VERSION = '1.000014';
9  
10 21 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use Data::IEEE754 qw( unpack_double_be unpack_float_be );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use Math::BigInt qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 ▶use MaxMind::DB::Common 0.040001 qw( %TypeNumToName );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use MaxMind::DB::Reader::Data::Container;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 ▶use MaxMind::DB::Reader::Data::EndMarker;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 21 ▶use MaxMind::DB::Types qw( Int );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 21 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 21 ▶use MooX::StrictConstructor;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 21 with 'MaxMind::DB::Role::Debugs', 'MaxMind::DB::Reader::Role::Sysreader';
23  
24 21 ▶use constant DEBUG => $ENV{MAXMIND_DB_DECODER_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # This is a constant so that outside of testing any references to it can be
27 # optimised away by the compiler.
28 21 ▶use constant POINTER_TEST_HACK => $ENV{MAXMIND_DB_POINTER_TEST_HACK};
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 21 binmode STDERR, ':utf8'
31 if DEBUG;
32  
33 21 has _pointer_base => (
34 is => 'ro',
35 isa => Int,
36 init_arg => 'pointer_base',
37 default => 0,
38 );
39  
40 1240 ▶sub decode {
Subroutine decode: called CC=13 SCAR=25.7
Pod undocumented
41 1240 my $self = shift;
42 1240 my $offset = shift;
43  
44 1240 ▶ confess 'You must provide an offset to decode from when calling ->decode'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 1 1239 1240
45 unless defined $offset;
46  
47 1239 ▶ confess
Branch: 1 branch
BranchTrueFalseTotal
if $offset >= $self->_data_source_size 1 1238 1239
48 q{The MaxMind DB file's data section contains bad data (unknown data type or corrupt data)}
49 if $offset >= $self->_data_source_size;
50  
51 1238 if (DEBUG) {
52 $self->_debug_newline;
53 $self->_debug_string( 'Offset', $offset );
54 }
55  
56 1238 my $ctrl_byte;
57 1238 $self->_read( \$ctrl_byte, $offset, 1 );
58 1238 $offset++;
59  
60 1238 $self->_debug_binary( 'Control byte', $ctrl_byte )
61 if DEBUG;
62  
63 1238 $ctrl_byte = unpack( C => $ctrl_byte );
64  
65 # The type is encoded in the first 3 bits of the byte.
66 1238 my $type = $TypeNumToName{ $ctrl_byte >> 5 };
67  
68 1238 $self->_debug_string( 'Type', $type )
69 if DEBUG;
70  
71 # Pointers are a special case, we don't read the next $size bytes, we use
72 # the size to determine the length of the pointer and then follow it.
73 1238 ▶ if ( $type eq 'pointer' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "pointer") 38 1200 1238
74 38 my ( $pointer, $new_offset )
75 = $self->_decode_pointer( $ctrl_byte, $offset );
76  
77 38 return $pointer if POINTER_TEST_HACK;
78  
79 28 my $value = $self->decode($pointer);
80 return wantarray
81 27 ▶ ? ( $value, $new_offset )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 25 2 27
82 : $value;
83 }
84  
85 1200 ▶ if ( $type eq 'extended' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "extended") 108 1092 1200
86 108 my $next_byte;
87 108 $self->_read( \$next_byte, $offset, 1 );
88  
89 108 $self->_debug_binary( 'Next byte', $next_byte )
90 if DEBUG;
91  
92 108 my $type_num = unpack( C => $next_byte ) + 7;
93 108 ▶ confess
Branch: 1 branch
BranchTrueFalseTotal
if $type_num < 8 0 108 108
94 "Something went horribly wrong in the decoder. An extended type resolved to a type number < 8 ($type_num)"
95 if $type_num < 8;
96  
97 108 $type = $TypeNumToName{$type_num};
98 108 $offset++;
99 }
100  
101 1200 ( my $size, $offset )
102 = $self->_size_from_ctrl_byte( $ctrl_byte, $offset );
103  
104 1200 $self->_debug_string( 'Size', $size )
105 if DEBUG;
106  
107 # The map and array types are special cases, since we don't read the next
108 # $size bytes. For all other types, we do.
109 1200 ▶ return $self->_decode_map( $size, $offset )
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "map" 221 979 1200
110 if $type eq 'map';
111  
112 979 ▶ return $self->_decode_array( $size, $offset )
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "array" 24 955 979
113 if $type eq 'array';
114  
115 955 ▶ return $self->_decode_boolean( $size, $offset )
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "boolean" 2 953 955
116 if $type eq 'boolean';
117  
118 953 my $buffer;
119 953 ▶ $self->_read( \$buffer, $offset, $size )
Branch: 1 branch
BranchTrueFalseTotal
if $size 924 29 953
120 if $size;
121  
122 953 $self->_debug_binary( 'Buffer', $buffer )
123 if DEBUG;
124  
125 953 ▶ my $method = '_decode_' . ( $type =~ /^uint/ ? 'uint' : $type );
Branch: 1 branch
BranchTrueFalseTotal
$type =~ /^uint/ ? : 165 788 953
126 return wantarray
127 953 ▶ ? ( $self->$method( $buffer, $size ), $offset + $size )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 814 139 953
128 : $self->$method( $buffer, $size );
129 }
130  
131 21 my %pointer_value_offset = (
132 1 => 0,
133 2 => 2**11,
134 3 => 2**19 + 2**11,
135 4 => 0,
136 );
137  
138 38 ▶sub _decode_pointer {
Subroutine _decode_pointer: called CC=2 SCAR=6.9
139 38 my $self = shift;
140 38 my $ctrl_byte = shift;
141 38 my $offset = shift;
142  
143 38 my $pointer_size = ( ( $ctrl_byte >> 3 ) & 0b00000011 ) + 1;
144  
145 38 $self->_debug_string( 'Pointer size', $pointer_size )
146 if DEBUG;
147  
148 38 my $buffer;
149 38 $self->_read( \$buffer, $offset, $pointer_size );
150  
151 38 $self->_debug_binary( 'Buffer', $buffer )
152 if DEBUG;
153  
154 38 ▶ my $packed
Branch: 1 branch
BranchTrueFalseTotal
$pointer_size == 4 ? : 1 37 38
155 = $pointer_size == 4
156 ? $buffer
157 : ( pack( C => $ctrl_byte & 0b00000111 ) ) . $buffer;
158  
159 38 $packed = $self->_zero_pad_left( $packed, 4 );
160  
161 38 $self->_debug_binary( 'Packed pointer', $packed )
162 if DEBUG;
163  
164 38 my $pointer = unpack( 'N' => $packed ) + $self->_pointer_base;
165 38 $pointer += $pointer_value_offset{$pointer_size};
166  
167 38 $self->_debug_string( 'Pointer to', $pointer )
168 if DEBUG;
169  
170 38 return ( $pointer, $offset + $pointer_size );
171 }
172  
173 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
174 728 ▶sub _decode_utf8_string {
Subroutine _decode_utf8_string: called CC=2 SCAR=6.9
175 728 my $self = shift;
176 728 my $buffer = shift;
177 728 my $size = shift;
178  
179 728 ▶ return q{} if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 1 727 728
180  
181 ## no critic (Subroutines::ProhibitCallsToUnexportedSubs)
182 727 return Encode::decode( 'utf-8', $buffer, Encode::FB_CROAK );
183 }
184  
185 17 ▶sub _decode_double {
Subroutine _decode_double: called CC=1 SCAR=0.0
186 17 my $self = shift;
187 17 my $buffer = shift;
188 17 my $size = shift;
189  
190 17 $self->_verify_size( 8, $size );
191 16 return unpack_double_be($buffer);
192 }
193  
194 18 ▶sub _decode_float {
Subroutine _decode_float: called CC=1 SCAR=0.0
195 18 my $self = shift;
196 18 my $buffer = shift;
197 18 my $size = shift;
198  
199 18 $self->_verify_size( 4, $size );
200 18 return unpack_float_be($buffer);
201 }
202  
203 11 ▶sub _decode_bytes {
Subroutine _decode_bytes: called CC=2 SCAR=6.9
204 11 my $self = shift;
205 11 my $buffer = shift;
206 11 my $size = shift;
207  
208 11 ▶ return q{} if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 1 10 11
209  
210 10 return $buffer;
211 }
212  
213 221 ▶sub _decode_map {
Subroutine _decode_map: called CC=3 SCAR=11.0
214 221 my $self = shift;
215 221 my $size = shift;
216 221 my $offset = shift;
217  
218 221 $self->_debug_string( 'Map size', $size )
219 if DEBUG;
220  
221 221 my %map;
222 221 for ( 1 .. $size ) {
223 421 ( my $key, $offset ) = $self->decode($offset);
224 421 ( my $val, $offset ) = $self->decode($offset);
225  
226 419 if (DEBUG) {
227 $self->_debug_string( "Key $_", $key );
228 $self->_debug_string( "Value $_", $val );
229 }
230  
231 419 $map{$key} = $val;
232 }
233  
234 219 $self->_debug_structure( 'Decoded map', \%map )
235 if DEBUG;
236  
237 219 ▶ return wantarray ? ( \%map, $offset ) : \%map;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 24 195 219
238 }
239  
240 12 ▶sub _decode_int32 {
Subroutine _decode_int32: called CC=2 SCAR=6.9
241 12 my $self = shift;
242 12 my $buffer = shift;
243 12 my $size = shift;
244  
245 12 ▶ return 0 if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 1 11 12
246  
247 11 return unpack( 'N!' => $self->_zero_pad_left( $buffer, 4 ) );
248 }
249  
250 {
251 21 my $max_int_bytes = log( ~0 ) / ( 8 * log(2) );
252  
253 165 ▶ sub _decode_uint {
Subroutine _decode_uint: called CC=4 SCAR=13.9
254 165 my $self = shift;
255 165 my $buffer = shift;
256 165 my $size = shift;
257  
258 165 if (DEBUG) {
259 $self->_debug_string( 'UINT size', $size );
260 $self->_debug_binary( 'Buffer', $buffer );
261 }
262  
263 165 ▶ my $int = $size <= $max_int_bytes ? 0 : Math::BigInt->bzero;
Branch: 1 branch
BranchTrueFalseTotal
$size <= $max_int_bytes ? : 157 8 165
264 165 ▶ return $int if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 24 141 165
265  
266 141 my @unpacked = unpack( 'C*', $buffer );
267 141 for my $piece (@unpacked) {
268 382 $int = ( $int << 8 ) | $piece;
269 }
270  
271 141 return $int;
272 }
273 }
274  
275 21 ▶sub _decode_array {
Subroutine _decode_array: called CC=3 SCAR=11.0
276 24 my $self = shift;
277 24 my $size = shift;
278 24 my $offset = shift;
279  
280 24 $self->_debug_string( 'Array size', $size )
281 if DEBUG;
282  
283 24 my @array;
284 24 for ( 1 .. $size ) {
285 43 ( my $val, $offset ) = $self->decode($offset);
286  
287 43 if (DEBUG) {
288 $self->_debug_string( "Value $_", $val );
289 }
290  
291 43 push @array, $val;
292 }
293  
294 24 $self->_debug_structure( 'Decoded array', \@array )
295 if DEBUG;
296  
297 24 ▶ return wantarray ? ( \@array, $offset ) : \@array;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 21 3 24
298 }
299  
300 1 ▶sub _decode_container {
Subroutine _decode_container: called CC=1 SCAR=0.0
301 1 return MaxMind::DB::Reader::Data::Container->new;
302 }
303  
304 1 ▶sub _decode_end_marker {
Subroutine _decode_end_marker: called CC=1 SCAR=0.0
305 1 return MaxMind::DB::Reader::Data::EndMarker->new;
306 }
307  
308 2 ▶sub _decode_boolean {
Subroutine _decode_boolean: called CC=2 SCAR=7.0
309 2 my $self = shift;
310 2 my $size = shift;
311 2 my $offset = shift;
312  
313 2 ▶ return wantarray ? ( $size, $offset ) : $size;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
314 }
315 ## use critic
316  
317 35 ▶sub _verify_size {
Subroutine _verify_size: called CC=2 SCAR=6.9
318 35 my $self = shift;
319 35 my $expected = shift;
320 35 my $actual = shift;
321  
322 35 ▶ confess
Branch: 1 branch
BranchTrueFalseTotal
unless $expected == $actual 1 34 35
323 q{The MaxMind DB file's data section contains bad data (unknown data type or corrupt data)}
324 unless $expected == $actual;
325 }
326  
327 1200 ▶sub _size_from_ctrl_byte {
Subroutine _size_from_ctrl_byte: called CC=4 SCAR=13.9
328 1200 my $self = shift;
329 1200 my $ctrl_byte = shift;
330 1200 my $offset = shift;
331  
332 1200 my $size = $ctrl_byte & 0b00011111;
333 1200 ▶ return ( $size, $offset )
Branch: 1 branch
BranchTrueFalseTotal
if $size < 29 1186 14 1200
334 if $size < 29;
335  
336 14 my $bytes_to_read = $size - 28;
337  
338 14 my $buffer;
339 14 $self->_read( \$buffer, $offset, $bytes_to_read );
340  
341 14 ▶ if ( $size == 29 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size == 29) { } 8 6 14
elsif ($size == 30) { } 4 2 6
342 8 $size = 29 + unpack( 'C', $buffer );
343 }
344 elsif ( $size == 30 ) {
345 4 $size = 285 + unpack( 'n', $buffer );
346 }
347 else {
348 2 $size = 65821 + unpack( 'N', $self->_zero_pad_left( $buffer, 4 ) );
349 }
350  
351 14 return ( $size, $offset + $bytes_to_read );
352 }
353  
354 54 ▶sub _zero_pad_left {
Subroutine _zero_pad_left: called CC=1 SCAR=0.0
355 54 my $self = shift;
356 54 my $content = shift;
357 54 my $desired_length = shift;
358  
359 54 return ( "\x00" x ( $desired_length - length($content) ) ) . $content;
360 }
361  
362 21 1;
« Reader/PP.pm ↑ Summary Reader/Role/Sysreader.pm »