handle = $handle; $this->closed = false; $this->file_size = (int) $stat['size']; try { $this->open_metadata( $path ); } catch ( \Throwable $e ) { $this->close(); throw $e; } } /** * The database's metadata block. * * @throws \BadMethodCallException When the reader is closed. */ public function metadata(): Metadata { $this->assert_open(); return clone $this->metadata; } /** * The record for an address: an array decoded from the database, or null * when the database has no record for it. Private and reserved ranges are * generally absent. * * @param string $ip An IPv4 or IPv6 address. * @return mixed * @throws \InvalidArgumentException When $ip is not an address, or is IPv6 in an IPv4-only database. * @throws InvalidDatabaseException When the tree or the record is malformed, or a resource limit refuses the decode. * @throws \BadMethodCallException When the reader is closed. */ public function get( string $ip ) { $this->assert_open(); $node = $this->find_node( $ip ); if ( $node === $this->metadata->node_count ) { return null; } // A record above node_count points into the data section. The values // node_count + 1 to + 15 fall inside the separator and are never valid. $excess = $node - $this->metadata->node_count; if ( $excess < self::DATA_SEPARATOR_BYTES ) { throw new InvalidDatabaseException( "The MaxMind DB file's search tree is corrupt (record $node)." ); } $offset = $excess + $this->metadata->search_tree_size; if ( $offset >= $this->file_size ) { throw new InvalidDatabaseException( "The MaxMind DB file's search tree is corrupt (record $node points past the end of the file)." ); } [ $value ] = $this->data->decode( $offset ); return $value; } /** * Release the file. Safe to call more than once. */ public function close(): void { if ( ! $this->closed ) { fclose( $this->handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose $this->closed = true; } } public function __destruct() { $this->close(); } /** * Find the metadata block, decode it, and check the tree and data bounds. * * @throws InvalidDatabaseException */ private function open_metadata( string $path ): void { $tail_length = min( $this->file_size, self::METADATA_MAX_BYTES + strlen( self::METADATA_MARKER ) ); $tail_start = $this->file_size - $tail_length; $tail = Bytes::read( $this->handle, $this->file_size, $tail_start, $tail_length ); $marker_at = strrpos( $tail, self::METADATA_MARKER ); if ( false === $marker_at ) { throw new InvalidDatabaseException( "Error opening database file ($path). Is this a valid MaxMind DB file?" ); } $marker_offset = $tail_start + $marker_at; $metadata_at = $marker_offset + strlen( self::METADATA_MARKER ); [ $map ] = ( new Decoder( $this->handle, $metadata_at, $this->file_size ) )->decode( $metadata_at ); if ( ! is_array( $map ) ) { throw new InvalidDatabaseException( 'The MaxMind DB metadata must be a map.' ); } $this->metadata = Metadata::from_map( $map ); // The search tree, then 16 zero bytes, then the data section, all before the metadata marker. $data_start = $this->metadata->search_tree_size + self::DATA_SEPARATOR_BYTES; if ( $data_start > $marker_offset ) { throw new InvalidDatabaseException( 'The MaxMind DB search tree does not fit before its metadata.' ); } $this->data = new Decoder( $this->handle, $data_start, $this->file_size ); $this->ipv4_start = 4 === $this->metadata->ip_version ? 0 : $this->walk_to_ipv4_prefix(); } /** * In an IPv6 tree, the node reached by walking the 96 zero bits of ::/96. * * @throws InvalidDatabaseException */ private function walk_to_ipv4_prefix(): int { $node = 0; for ( $i = 0; $i < 96 && $node < $this->metadata->node_count; ++$i ) { $node = $this->read_node( $node, 0 ); } return $node; } /** * The search-tree node number an address lands on: node_count when the * address has no record, a larger number for a record in the data section. * * @throws \InvalidArgumentException * @throws InvalidDatabaseException */ private function find_node( string $ip ): int { $packed = inet_pton( $ip ); if ( false === $packed ) { throw new \InvalidArgumentException( "The value \"$ip\" is not a valid IP address." ); } $bits = strlen( $packed ) * 8; $node = 0; if ( 6 === $this->metadata->ip_version && 32 === $bits ) { $node = $this->ipv4_start; } elseif ( 4 === $this->metadata->ip_version && 128 === $bits ) { throw new \InvalidArgumentException( "Error looking up $ip. You attempted to look up an IPv6 address in an IPv4-only database." ); } $node_count = $this->metadata->node_count; for ( $i = 0; $i < $bits && $node < $node_count; ++$i ) { $bit = ( ord( $packed[ $i >> 3 ] ) >> ( 7 - ( $i & 7 ) ) ) & 1; $node = $this->read_node( $node, $bit ); } if ( $node < $node_count ) { throw new InvalidDatabaseException( 'The MaxMind DB search tree is corrupt: the maximum search depth was reached without a leaf.' ); } return $node; } /** * One record of a node: the left (bit 0) or right (bit 1) child, in the * tree's record size. * * @throws InvalidDatabaseException */ private function read_node( int $node, int $bit ): int { $base = $node * $this->metadata->node_byte_size; switch ( $this->metadata->record_size ) { case 24: return Bytes::unsigned( $this->bytes( $base + 3 * $bit, 3 ) ); case 28: // Seven bytes hold two 28-bit records. The left record takes the high nibble // of the middle byte, the right record the low nibble. $bytes = $this->bytes( $base + 3 * $bit, 4 ); $nibble = 0 === $bit ? ( ord( $bytes[3] ) >> 4 ) : ( ord( $bytes[0] ) & 0x0F ); $rest = 0 === $bit ? substr( $bytes, 0, 3 ) : substr( $bytes, 1, 3 ); return ( $nibble << 24 ) | Bytes::unsigned( $rest ); default: return Bytes::unsigned( $this->bytes( $base + 4 * $bit, 4 ) ); } } /** * @throws InvalidDatabaseException */ private function bytes( int $offset, int $length ): string { return Bytes::read( $this->handle, $this->file_size, $offset, $length ); } /** * @throws \BadMethodCallException */ private function assert_open(): void { if ( $this->closed ) { throw new \BadMethodCallException( 'Attempt to read from a closed MaxMind DB.' ); } } }