handle = $handle; $this->size = $size; } /** * @throws \RuntimeException When the file is not an archive this version can read. */ public function assertMagic(): void { $this->seek( 0 ); $magic = (string) fread( $this->handle, strlen( Format::MAGIC ) ); if ( 'OMIG' !== substr( $magic, 0, 4 ) ) { throw new \RuntimeException( 'This file is not a MigrateX archive.' ); } if ( Format::MAGIC !== $magic ) { throw new \RuntimeException( 'This archive was made by an incompatible version of MigrateX.' ); } } public function firstEntryOffset(): int { return strlen( Format::MAGIC ); } /** * Moves to an absolute offset. fseek() reports failure (-1) instead of throwing, and does so * silently for offsets a 32-bit PHP cannot address, which would otherwise surface later as * garbage being parsed as a header. * * @throws \RuntimeException */ public function seek( int $offset ): void { if ( $offset < 0 || 0 !== fseek( $this->handle, $offset ) ) { throw new \RuntimeException( sprintf( 'Cannot seek to byte %d of the archive. If this server runs 32-bit PHP, archives over 2 GB cannot be restored on it.', (int) $offset ) ); } } /** * Confirms the file ends with the eof entry, so a cut-off or half-written file is reported * as such up front, before extraction reaches it and stops with a vaguer error. Trailing * zero padding after the marker is tolerated: nothing after the eof entry is ever read. * * @throws \RuntimeException */ public function assertEndMarker(): void { // Walk back over trailing zero padding (bounded) to the last real bytes. $end = $this->size; $tail = ''; while ( $end > 0 && $this->size - $end < 1048576 ) { $length = (int) min( $end, 4096 ); $this->seek( $end - $length ); $tail = rtrim( (string) fread( $this->handle, $length ), "\0" ); if ( '' !== $tail ) { break; } $end -= $length; } if ( 1 !== preg_match( '/\{"t":"eof"[^{}]*\}$/', $tail ) ) { throw new \RuntimeException( 'The archive has no end marker. The file is incomplete or was damaged.' ); } } /** * @return array{type:string,name:string,size:int,mtime:int,count:int,data:int,next:int} * @throws \RuntimeException On a truncated or malformed entry. */ public function header( int $offset ): array { if ( $offset < 0 || $offset + 4 > $this->size ) { throw new \RuntimeException( 'The archive ends unexpectedly. The upload may be incomplete.' ); } $this->seek( $offset ); $prefix = (string) fread( $this->handle, 4 ); if ( 4 !== strlen( $prefix ) ) { throw new \RuntimeException( 'Cannot read the archive.' ); } // Only the header JSON length is a 32-bit field (capped at MAX_HEADER). File sizes live in // the JSON as integers, so entries are not limited to 4 GB. $length = (int) unpack( 'N', $prefix )[1]; if ( 0 === $length ) { // Zero-filled data where an entry should start. It is not a clean end of the archive: // a real archive always finishes with an eof entry, so this means missing or damaged data. throw new \RuntimeException( sprintf( 'The archive contains empty (zero-filled) data at byte %d, where an entry should start. The file was not fully written or was damaged.', (int) $offset ) ); } if ( $length < 2 || $length > Format::MAX_HEADER || $offset + 4 + $length > $this->size ) { throw new \RuntimeException( sprintf( 'The archive contains a corrupt entry header at byte %d.', (int) $offset ) ); } $json = (string) fread( $this->handle, $length ); if ( strlen( $json ) !== $length ) { throw new \RuntimeException( 'Cannot read the archive.' ); } $meta = json_decode( $json, true ); if ( is_array( $meta ) && isset( $meta['s'] ) && is_float( $meta['s'] ) ) { // PHP turns integers beyond PHP_INT_MAX into floats: only possible on a 32-bit build. throw new \RuntimeException( 'A file in this archive is larger than this server\'s PHP can handle (32-bit PHP is limited to 2 GB).' ); } if ( ! is_array( $meta ) || ! isset( $meta['t'], $meta['n'], $meta['s'] ) || ! is_string( $meta['n'] ) || ! is_int( $meta['s'] ) || $meta['s'] < 0 ) { throw new \RuntimeException( sprintf( 'The archive contains a corrupt entry header at byte %d.', (int) $offset ) ); } $data = $offset + 4 + $length; $next = $data + $meta['s']; if ( $next > $this->size ) { throw new \RuntimeException( 'The archive is truncated. The upload may be incomplete.' ); } return array( 'type' => (string) $meta['t'], 'name' => $meta['n'], 'size' => $meta['s'], 'mtime' => isset( $meta['m'] ) ? (int) $meta['m'] : 0, 'count' => isset( $meta['c'] ) ? (int) $meta['c'] : 0, 'data' => $data, 'next' => $next, ); } }