@@ -57,11 +57,70 @@ ZEND_GET_MODULE(bz2)
5757
5858struct php_bz2_stream_data_t {
5959 BZFILE * bz_file ;
60+ FILE * file ;
61+ bool stream_end ;
62+ bool read_failed ;
63+ bool report_read_errors ;
64+ bool read_error_reported ;
6065 zend_resource * stream_res ;
6166};
6267
6368/* {{{ BZip2 stream implementation */
6469
70+ static void php_bz2iop_report_read_error (php_stream * stream )
71+ {
72+ struct php_bz2_stream_data_t * self = stream -> abstract ;
73+
74+ self -> read_failed = true;
75+ stream -> eof = 1 ;
76+ if (self -> report_read_errors && !self -> read_error_reported ) {
77+ self -> read_error_reported = true;
78+ php_error_docref (NULL , E_WARNING , "Failed to read bzip2 stream" );
79+ }
80+ }
81+
82+ static bool php_bz2iop_next_stream (php_stream * stream )
83+ {
84+ struct php_bz2_stream_data_t * self = stream -> abstract ;
85+ char buffer [BZ_MAX_UNUSED ];
86+ void * unused ;
87+ int unused_len , error ;
88+
89+ BZ2_bzReadGetUnused (& error , self -> bz_file , & unused , & unused_len );
90+ if (error != BZ_OK ) {
91+ php_bz2iop_report_read_error (stream );
92+ return false;
93+ }
94+ memcpy (buffer , unused , unused_len );
95+ while (unused_len < 4 ) {
96+ int ch = fgetc (self -> file );
97+ if (ch == EOF ) {
98+ self -> read_failed = ferror (self -> file ) != 0 ;
99+ break ;
100+ }
101+ buffer [unused_len ++ ] = ch ;
102+ }
103+ if (self -> read_failed ) {
104+ php_bz2iop_report_read_error (stream );
105+ return false;
106+ }
107+ if (unused_len < 4 || memcmp (buffer , "BZh" , 3 ) != 0
108+ || buffer [3 ] < '1' || buffer [3 ] > '9' ) {
109+ stream -> eof = 1 ;
110+ return false;
111+ }
112+
113+ BZFILE * next = BZ2_bzReadOpen (& error , self -> file , 0 , strchr (stream -> mode , 's' ) != NULL , buffer , unused_len );
114+ if (error != BZ_OK ) {
115+ php_bz2iop_report_read_error (stream );
116+ return false;
117+ }
118+ BZ2_bzReadClose (& error , self -> bz_file );
119+ self -> bz_file = next ;
120+ self -> stream_end = false;
121+ return true;
122+ }
123+
65124static ssize_t php_bz2iop_read (php_stream * stream , char * buf , size_t count )
66125{
67126 struct php_bz2_stream_data_t * self = (struct php_bz2_stream_data_t * )stream -> abstract ;
@@ -72,17 +131,38 @@ static ssize_t php_bz2iop_read(php_stream *stream, char *buf, size_t count)
72131 size_t remain = count - ret ;
73132 int to_read = (int )(remain <= INT_MAX ? remain : INT_MAX );
74133
75- just_read = BZ2_bzread (self -> bz_file , buf , to_read );
134+ if (self -> read_failed ) {
135+ return ret ? (ssize_t ) ret : -1 ;
136+ }
137+ if (stream -> eof ) {
138+ return ret ? (ssize_t ) ret : (self -> read_failed ? -1 : 0 );
139+ }
140+ if (self -> file ) {
141+ int error ;
142+ if (self -> stream_end && !php_bz2iop_next_stream (stream )) {
143+ return ret ? (ssize_t ) ret : (self -> read_failed ? -1 : 0 );
144+ }
145+ just_read = BZ2_bzRead (& error , self -> bz_file , buf + ret , to_read );
146+ self -> stream_end = error == BZ_STREAM_END ;
147+ if (error != BZ_OK && error != BZ_STREAM_END ) {
148+ just_read = -1 ;
149+ } else if (just_read == 0 && self -> stream_end ) {
150+ continue ;
151+ }
152+ } else {
153+ just_read = BZ2_bzread (self -> bz_file , buf + ret , to_read );
154+ }
76155
77156 if (just_read < 1 ) {
78157 /* it is not safe to keep reading after an error, see #72613 */
79- stream -> eof = 1 ;
80158 if (just_read < 0 ) {
159+ php_bz2iop_report_read_error (stream );
81160 if (ret ) {
82161 return ret ;
83162 }
84163 return -1 ;
85164 }
165+ stream -> eof = 1 ;
86166 break ;
87167 }
88168
@@ -126,7 +206,13 @@ static int php_bz2iop_close(php_stream *stream, int close_handle)
126206 int ret = EOF ;
127207
128208 if (close_handle ) {
129- BZ2_bzclose (self -> bz_file );
209+ if (self -> file ) {
210+ int error ;
211+ BZ2_bzReadClose (& error , self -> bz_file );
212+ fclose (self -> file );
213+ } else {
214+ BZ2_bzclose (self -> bz_file );
215+ }
130216 }
131217
132218 /* The inner stream may have been closed by the user, only its resource is held */
@@ -164,14 +250,32 @@ const php_stream_ops php_stream_bz2io_ops = {
164250
165251/* {{{ Bzip2 stream openers */
166252
167- /* bzlib closes the descriptor it is given, so it gets a duplicate */
168- static BZFILE * php_bz2_bzdopen (php_socket_t fd , const char * mode )
253+ static BZFILE * php_bz2_readopen (FILE * file , const char * mode )
254+ {
255+ int error ;
256+ BZFILE * bz = BZ2_bzReadOpen (& error , file , 0 , strchr (mode , 's' ) != NULL , NULL , 0 );
257+ if (!bz ) {
258+ fclose (file );
259+ }
260+ return bz ;
261+ }
262+
263+ static BZFILE * php_bz2_bzdopen (php_socket_t fd , const char * mode , FILE * * file )
169264{
170265 int dup_fd = dup ((int ) fd );
171266 if (dup_fd == -1 ) {
172267 return NULL ;
173268 }
174269
270+ if (mode [0 ] == 'r' ) {
271+ * file = fdopen (dup_fd , "rb" );
272+ if (!* file ) {
273+ close (dup_fd );
274+ return NULL ;
275+ }
276+ return php_bz2_readopen (* file , mode );
277+ }
278+
175279 BZFILE * bz = BZ2_bzdopen (dup_fd , mode );
176280 if (!bz ) {
177281 close (dup_fd );
@@ -193,6 +297,11 @@ PHP_BZ2_API php_stream *_php_stream_bz2open_from_BZFILE(BZFILE *bz,
193297 GC_ADDREF (innerstream -> res );
194298 }
195299 self -> bz_file = bz ;
300+ self -> file = NULL ;
301+ self -> stream_end = false;
302+ self -> read_failed = false;
303+ self -> report_read_errors = false;
304+ self -> read_error_reported = false;
196305
197306 return php_stream_alloc_rel (& php_stream_bz2io_ops , self , 0 , mode );
198307}
@@ -207,6 +316,7 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
207316 php_stream * retstream = NULL , * stream = NULL ;
208317 char * path_copy = NULL ;
209318 BZFILE * bz_file = NULL ;
319+ FILE * file = NULL ;
210320
211321 if (strncasecmp ("compress.bzip2://" , path , 17 ) == 0 ) {
212322 path += 17 ;
@@ -229,7 +339,14 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
229339 }
230340
231341 /* try and open it directly first */
232- bz_file = BZ2_bzopen (path_copy , mode );
342+ if (mode [0 ] == 'r' ) {
343+ file = VCWD_FOPEN (path_copy , "rb" );
344+ if (file ) {
345+ bz_file = php_bz2_readopen (file , mode );
346+ }
347+ } else {
348+ bz_file = BZ2_bzopen (path_copy , mode );
349+ }
233350
234351 if (opened_path && bz_file ) {
235352 * opened_path = zend_string_init (path_copy , strlen (path_copy ), 0 );
@@ -246,7 +363,7 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
246363 if (stream ) {
247364 php_socket_t fd ;
248365 if (SUCCESS == php_stream_cast (stream , PHP_STREAM_AS_FD , (void * * ) & fd , REPORT_ERRORS )) {
249- bz_file = php_bz2_bzdopen (fd , mode );
366+ bz_file = php_bz2_bzdopen (fd , mode , & file );
250367 }
251368 }
252369
@@ -261,6 +378,9 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper,
261378 if (bz_file ) {
262379 retstream = _php_stream_bz2open_from_BZFILE (bz_file , mode , stream STREAMS_REL_CC );
263380 if (retstream ) {
381+ struct php_bz2_stream_data_t * self = retstream -> abstract ;
382+ self -> file = file ;
383+ self -> report_read_errors = wrapper != NULL ;
264384 return retstream ;
265385 }
266386
@@ -358,6 +478,7 @@ PHP_FUNCTION(bzopen)
358478
359479 BZFILE * bz ; /* The compressed file stream */
360480 php_stream * stream = NULL ;
481+ FILE * handle = NULL ;
361482
362483 if (zend_parse_parameters (ZEND_NUM_ARGS (), "zs" , & file , & mode , & mode_len ) == FAILURE ) {
363484 RETURN_THROWS ();
@@ -432,12 +553,13 @@ PHP_FUNCTION(bzopen)
432553 RETURN_FALSE ;
433554 }
434555
435- bz = php_bz2_bzdopen (fd , mode );
556+ bz = php_bz2_bzdopen (fd , mode , & handle );
436557 if (!bz ) {
437558 RETURN_FALSE ;
438559 }
439560
440561 stream = php_stream_bz2open_from_BZFILE (bz , mode , stream );
562+ ((struct php_bz2_stream_data_t * ) stream -> abstract )-> file = handle ;
441563 } else {
442564 zend_argument_type_error (1 , "must be of type string or file-resource, %s given" , zend_zval_value_name (file ));
443565 RETURN_THROWS ();
0 commit comments