handle = $handle; $this->pointer_base = $pointer_base; $this->file_size = $file_size; } /** * Decode the value at $offset under a fresh set of limits. * * @return array{0: mixed, 1: int} The value, and the offset just past it. * @throws InvalidDatabaseException When the value is malformed or exceeds a limit. */ public function decode( int $offset ): array { // The root value is the first occurrence, so it is charged here. $this->values_left = self::MAX_VALUES - 1; $this->payload_left = self::MAX_PAYLOAD_BYTES; return $this->value_at( $offset, 0, true ); } /** * @return array{0: mixed, 1: int} * @throws InvalidDatabaseException */ private function value_at( int $offset, int $depth, bool $may_point ): array { $control = ord( $this->read( $offset, 1 ) ); ++$offset; $type = $control >> 5; if ( self::TYPE_POINTER === $type ) { if ( ! $may_point ) { throw self::corrupt( 'a pointer points at another pointer' ); } [ $target, $offset ] = $this->pointer( $control, $offset ); if ( $depth >= self::MAX_DEPTH ) { throw self::limit( 'maximum depth' ); } [ $value ] = $this->value_at( $target, $depth + 1, false ); return array( $value, $offset ); } if ( self::TYPE_EXTENDED === $type ) { $type = ord( $this->read( $offset, 1 ) ) + 7; if ( $type < 8 ) { throw self::corrupt( 'an extended type is out of range' ); } ++$offset; } [ $size, $offset ] = $this->size( $control, $offset ); return $this->value_of_type( $type, $size, $offset, $depth ); } /** * @return array{0: mixed, 1: int} * @throws InvalidDatabaseException */ private function value_of_type( int $type, int $size, int $offset, int $depth ): array { switch ( $type ) { case self::TYPE_MAP: return $this->map( $size, $offset, $depth ); case self::TYPE_ARRAY: return $this->list( $size, $offset, $depth ); case self::TYPE_BOOLEAN: return array( 0 !== $size, $offset ); case self::TYPE_UTF8_STRING: case self::TYPE_BYTES: // A string is copied into a PHP string, so its bytes are charged here, every time. $this->charge_payload( $size ); return array( $this->read( $offset, $size ), $offset + $size ); } // The remaining types are fixed-width scalars. An oversized declared size is // refused before any bytes are read, so it cannot drive a large read. if ( $size > self::MAX_SCALAR_BYTES ) { throw self::corrupt( 'a scalar declares more than 16 bytes' ); } $bytes = $this->read( $offset, $size ); $next = $offset + $size; switch ( $type ) { case self::TYPE_DOUBLE: self::expect_width( $size, 8 ); $unpacked = unpack( 'E', $bytes ); // Big-endian IEEE 754 double. if ( false === $unpacked ) { throw self::corrupt( 'a double does not unpack' ); } return array( (float) $unpacked[1], $next ); case self::TYPE_FLOAT: self::expect_width( $size, 4 ); $unpacked = unpack( 'G', $bytes ); // Big-endian IEEE 754 single. if ( false === $unpacked ) { throw self::corrupt( 'a float does not unpack' ); } return array( (float) $unpacked[1], $next ); case self::TYPE_INT32: if ( $size > 4 ) { throw self::corrupt( 'an int32 is wider than 4 bytes' ); } $value = Bytes::unsigned( str_pad( $bytes, 4, "\x00", STR_PAD_LEFT ) ); return array( $value >= 0x80000000 ? $value - 0x100000000 : $value, $next ); case self::TYPE_UINT16: return self::uint( $bytes, 2, $next ); case self::TYPE_UINT32: return self::uint( $bytes, 4, $next ); case self::TYPE_UINT64: return self::uint( $bytes, 8, $next ); case self::TYPE_UINT128: return self::uint( $bytes, 16, $next ); } throw self::corrupt( 'an unknown or unexpected type (' . $type . ')' ); } /** * @return array{0: array, 1: int} * @throws InvalidDatabaseException */ private function map( int $size, int $offset, int $depth ): array { $this->enter( $size, $depth, 2 ); // A key and a value per entry. $map = array(); for ( $i = 0; $i < $size; ++$i ) { [ $key, $offset ] = $this->value_at( $offset, $depth + 1, true ); [ $value, $offset ] = $this->value_at( $offset, $depth + 1, true ); if ( ! is_string( $key ) ) { throw self::corrupt( 'a map key is not a string' ); } $map[ $key ] = $value; } return array( $map, $offset ); } /** * @return array{0: array, 1: int} * @throws InvalidDatabaseException */ private function list( int $size, int $offset, int $depth ): array { $this->enter( $size, $depth, 1 ); $list = array(); for ( $i = 0; $i < $size; ++$i ) { [ $value, $offset ] = $this->value_at( $offset, $depth + 1, true ); $list[] = $value; } return array( $list, $offset ); } /** * Charge a container's declared children against the value budget before * anything is allocated for them. * * @throws InvalidDatabaseException */ private function enter( int $size, int $depth, int $values_per_entry ): void { if ( $depth >= self::MAX_DEPTH ) { throw self::limit( 'maximum depth' ); } if ( $size > intdiv( $this->values_left, $values_per_entry ) ) { throw self::limit( 'maximum number of values' ); } $this->values_left -= $size * $values_per_entry; } /** * @throws InvalidDatabaseException */ private function charge_payload( int $size ): void { if ( $size > $this->payload_left ) { throw self::limit( 'maximum payload size' ); } $this->payload_left -= $size; } /** * A pointer's value, relative to $pointer_base, and the offset past it. * * @return array{0: int, 1: int} * @throws InvalidDatabaseException */ private function pointer( int $control, int $offset ): array { $width = ( $control >> 3 ) & 0x3; $low = $control & 0x7; switch ( $width ) { case 0: return array( ( ( $low << 8 ) | Bytes::unsigned( $this->read( $offset, 1 ) ) ) + $this->pointer_base, $offset + 1 ); case 1: return array( ( ( ( $low << 16 ) | Bytes::unsigned( $this->read( $offset, 2 ) ) ) + 2048 ) + $this->pointer_base, $offset + 2 ); case 2: return array( ( ( ( $low << 24 ) | Bytes::unsigned( $this->read( $offset, 3 ) ) ) + 526336 ) + $this->pointer_base, $offset + 3 ); } return array( Bytes::unsigned( $this->read( $offset, 4 ) ) + $this->pointer_base, $offset + 4 ); } /** * A value's size: the low five bits of its control byte, extended by one to * three bytes when they say so. Returns the size and the offset past it. * * @return array{0: int, 1: int} * @throws InvalidDatabaseException */ private function size( int $control, int $offset ): array { $size = $control & 0x1F; if ( $size < 29 ) { return array( $size, $offset ); } $extra = $size - 28; $bytes = $this->read( $offset, $extra ); if ( 29 === $size ) { return array( 29 + ord( $bytes ), $offset + 1 ); } if ( 30 === $size ) { return array( 285 + Bytes::unsigned( $bytes ), $offset + 2 ); } return array( 65821 + Bytes::unsigned( $bytes ), $offset + 3 ); } /** * @return array{0: int|string, 1: int} * @throws InvalidDatabaseException */ private static function uint( string $bytes, int $max_width, int $next ): array { if ( strlen( $bytes ) > $max_width ) { throw self::corrupt( 'an unsigned integer is wider than its type allows' ); } return array( Bytes::unsigned_value( $bytes ), $next ); } /** * @throws InvalidDatabaseException */ private static function expect_width( int $actual, int $expected ): void { if ( $actual !== $expected ) { throw self::corrupt( 'a floating-point value has the wrong width' ); } } /** * @throws InvalidDatabaseException */ private function read( int $offset, int $length ): string { return Bytes::read( $this->handle, $this->file_size, $offset, $length ); } private static function corrupt( string $reason ): InvalidDatabaseException { return new InvalidDatabaseException( "The MaxMind DB file's data section contains bad data ($reason)." ); } private static function limit( string $which ): InvalidDatabaseException { return new InvalidDatabaseException( "The MaxMind DB file exceeds the $which." ); } }