@@ -366,27 +366,38 @@ PHP_FUNCTION(bzopen)
366366 php_stream_from_zval (stream , file );
367367 stream_mode_len = strlen (stream -> mode );
368368
369- if (stream_mode_len != 1 && !(stream_mode_len == 2 && memchr (stream -> mode , 'b' , 2 ))) {
370- php_error_docref (NULL , E_WARNING , "Cannot use stream opened in mode '%s'" , stream -> mode );
371- RETURN_FALSE ;
372- } else if (stream_mode_len == 1 && stream -> mode [0 ] != 'r' && stream -> mode [0 ] != 'w' && stream -> mode [0 ] != 'a' && stream -> mode [0 ] != 'x' ) {
369+ char primary_stream_mode ;
370+ if (stream_mode_len == 1 ) {
371+ primary_stream_mode = stream -> mode [0 ];
372+ } else if (stream_mode_len == 2 ) {
373+ char secondary_stream_mode = 0 ;
374+ if (stream -> mode [0 ] != 'b' ) {
375+ primary_stream_mode = stream -> mode [0 ];
376+ secondary_stream_mode = stream -> mode [1 ];
377+ } else {
378+ primary_stream_mode = stream -> mode [1 ];
379+ secondary_stream_mode = stream -> mode [0 ];
380+ }
381+ if (secondary_stream_mode != 'b' ) {
382+ goto unsupported_mode ;
383+ }
384+ } else {
385+ unsupported_mode :
373386 php_error_docref (NULL , E_WARNING , "Cannot use stream opened in mode '%s'" , stream -> mode );
374387 RETURN_FALSE ;
375388 }
376389
377390 switch (mode [0 ]) {
378391 case 'r' :
379392 /* only "r" and "rb" are supported */
380- if (stream -> mode [ 0 ] != mode [ 0 ] && !( stream_mode_len == 2 && stream -> mode [ 1 ] != mode [ 0 ]) ) {
393+ if (primary_stream_mode != 'r' ) {
381394 php_error_docref (NULL , E_WARNING , "Cannot read from a stream opened in write only mode" );
382395 RETURN_FALSE ;
383396 }
384397 break ;
385398 case 'w' :
386399 /* support only "w"(b), "a"(b), "x"(b) */
387- if (stream -> mode [0 ] != mode [0 ] && !(stream_mode_len == 2 && stream -> mode [1 ] != mode [0 ])
388- && stream -> mode [0 ] != 'a' && !(stream_mode_len == 2 && stream -> mode [1 ] != 'a' )
389- && stream -> mode [0 ] != 'x' && !(stream_mode_len == 2 && stream -> mode [1 ] != 'x' )) {
400+ if (!strchr ("wax" , primary_stream_mode )) {
390401 php_error_docref (NULL , E_WARNING , "cannot write to a stream opened in read only mode" );
391402 RETURN_FALSE ;
392403 }
0 commit comments