| 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
| |||||||||||||||
| 45 | unless defined $offset; | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 1239 | ▶ | confess | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 82 | : $value; | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 1200 | ▶ | if ( $type eq 'extended' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 110 | if $type eq 'map'; | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 979 | ▶ | return $self->_decode_array( $size, $offset ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | if $type eq 'array'; | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 955 | ▶ | return $self->_decode_boolean( $size, $offset ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | if $type eq 'boolean'; | ||||||||||||||
| 117 | |||||||||||||||
| 118 | 953 | my $buffer; | |||||||||||||
| 119 | 953 | ▶ | $self->_read( \$buffer, $offset, $size ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 126 | return wantarray | ||||||||||||||
| 127 | 953 | ▶ | ? ( $self->$method( $buffer, $size ), $offset + $size ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 264 | 165 | ▶ | return $int if $size == 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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; | |||||||||||||