class-basebytereadstream.php
1 week ago
class-deflatereadstream.php
1 week ago
class-filereadstream.php
1 week ago
class-inflatereadstream.php
1 week ago
class-limitedbytereadstream.php
1 week ago
class-transformedreadstream.php
1 week ago
interface-bytereadstream.php
1 week ago
class-inflatereadstream.php
151 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\ByteStream\ReadStream; |
| 4 | |
| 5 | use Exception; |
| 6 | use WordPress\ByteStream\ByteStreamException; |
| 7 | |
| 8 | class InflateReadStream extends BaseByteReadStream { |
| 9 | |
| 10 | protected $inflate_context; |
| 11 | protected $inflate_encoding; |
| 12 | protected $upstream; |
| 13 | |
| 14 | /** |
| 15 | * The offset of the underlying reader at the time of the first read |
| 16 | * from the InflateReader. |
| 17 | * |
| 18 | * It's the only way to seek to an earlier offset when reading from |
| 19 | * a git-like object that has a plaintext header and gzipped body: |
| 20 | * |
| 21 | * blob <size>\x00 |
| 22 | * <gzip-compressed-data> |
| 23 | * |
| 24 | * If we just seeked to offset=0, we'd start inflating the plaintext, |
| 25 | * not the deflated body, and get an error. |
| 26 | * |
| 27 | * @var int|null |
| 28 | */ |
| 29 | protected $delegate_offset_0; |
| 30 | |
| 31 | /** |
| 32 | * Ensure properties are defined or inherited. |
| 33 | * |
| 34 | * @var bool |
| 35 | */ |
| 36 | protected $is_closed = false; |
| 37 | protected $buffer = ''; |
| 38 | protected $bytes_already_forgotten = 0; |
| 39 | protected $offset_in_current_buffer = 0; |
| 40 | |
| 41 | public function __construct( ByteReadStream $upstream, $encoding = ZLIB_ENCODING_DEFLATE ) { |
| 42 | $this->inflate_encoding = $encoding; |
| 43 | $this->inflate_init(); |
| 44 | $this->upstream = $upstream; |
| 45 | } |
| 46 | |
| 47 | protected function internal_pull( $n ): string { |
| 48 | if ( null === $this->delegate_offset_0 ) { |
| 49 | $this->delegate_offset_0 = $this->upstream->tell(); |
| 50 | } |
| 51 | |
| 52 | if ( ! $this->inflate_context ) { |
| 53 | return ''; |
| 54 | } |
| 55 | |
| 56 | if ( $this->upstream->reached_end_of_data() ) { |
| 57 | $bytes = inflate_add( $this->inflate_context, '', ZLIB_FINISH ); |
| 58 | $this->inflate_context = null; |
| 59 | |
| 60 | return $bytes; |
| 61 | } |
| 62 | |
| 63 | $n = max( 200, $n ); |
| 64 | $available = $this->upstream->pull( $n ); |
| 65 | |
| 66 | $inflated = inflate_add( $this->inflate_context, $this->upstream->consume( $available ) ); |
| 67 | if ( false === $inflated ) { |
| 68 | throw new ByteStreamException( esc_html( 'Inflate error: ' . $this->get_error_string() ) ); |
| 69 | } |
| 70 | |
| 71 | return $inflated; |
| 72 | } |
| 73 | |
| 74 | public function length(): ?int { |
| 75 | return null; |
| 76 | } |
| 77 | |
| 78 | protected function internal_close_reading(): void { |
| 79 | $this->inflate_context = null; |
| 80 | } |
| 81 | |
| 82 | protected function internal_reached_end_of_data(): bool { |
| 83 | return null === $this->inflate_context && $this->upstream->reached_end_of_data(); |
| 84 | } |
| 85 | |
| 86 | public function seek( $offset ): void { |
| 87 | if ( null !== $this->length() && $offset > $this->length() ) { |
| 88 | throw new ByteStreamException( 'Cannot seek past the available data. Call append_bytes() first.' ); |
| 89 | } |
| 90 | |
| 91 | if ( $offset < $this->tell() ) { |
| 92 | $this->upstream->seek( $this->delegate_offset_0 ?? 0 ); |
| 93 | $this->buffer = ''; |
| 94 | $this->bytes_already_forgotten = 0; |
| 95 | $this->offset_in_current_buffer = 0; |
| 96 | |
| 97 | $this->inflate_init(); |
| 98 | } |
| 99 | |
| 100 | while ( $this->tell() < $offset ) { |
| 101 | $remaining_bytes = $offset - $this->tell(); |
| 102 | $next_chunk_size = min( 50 * 1024, $remaining_bytes ); |
| 103 | $pulled = $this->pull( $next_chunk_size ); |
| 104 | // Keep skipping bytes until we've consumed enough. |
| 105 | $this->consume( min( $remaining_bytes, $pulled ) ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private function inflate_init() { |
| 110 | $this->inflate_context = inflate_init( $this->inflate_encoding ); |
| 111 | if ( ! $this->inflate_context ) { |
| 112 | throw new Exception( 'Failed to initialize inflate context' ); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | protected function get_error_string() { |
| 117 | $status = inflate_get_status( $this->inflate_context ); |
| 118 | switch ( $status ) { |
| 119 | case ZLIB_OK: |
| 120 | $error_string = 'ZLIB_OK'; |
| 121 | break; |
| 122 | case ZLIB_STREAM_END: |
| 123 | $error_string = 'ZLIB_STREAM_END'; |
| 124 | break; |
| 125 | case ZLIB_NEED_DICT: |
| 126 | $error_string = 'ZLIB_NEED_DICT'; |
| 127 | break; |
| 128 | case ZLIB_ERRNO: |
| 129 | $error_string = 'ZLIB_ERRNO'; |
| 130 | break; |
| 131 | case ZLIB_STREAM_ERROR: |
| 132 | $error_string = 'ZLIB_STREAM_ERROR'; |
| 133 | break; |
| 134 | case ZLIB_DATA_ERROR: |
| 135 | $error_string = 'ZLIB_DATA_ERROR'; |
| 136 | break; |
| 137 | case ZLIB_BUF_ERROR: |
| 138 | $error_string = 'ZLIB_BUF_ERROR'; |
| 139 | break; |
| 140 | case ZLIB_MEM_ERROR: |
| 141 | $error_string = 'ZLIB_MEM_ERROR'; |
| 142 | break; |
| 143 | default: |
| 144 | $error_string = 'Unknown error'; |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | return "Error $status: $error_string"; |
| 149 | } |
| 150 | } |
| 151 |