@@ -797,6 +797,7 @@ typedef struct _php_brotli_stream_data {
797797 php_brotli_context ctx ;
798798 BrotliDecoderResult result ;
799799 php_stream * stream ;
800+ uint8_t * input_buf ;
800801} php_brotli_stream_data ;
801802
802803#define STREAM_DATA_FROM_STREAM () \
@@ -822,6 +823,10 @@ static int php_brotli_decompress_close(php_stream *stream,
822823
823824 php_brotli_context_close (& self -> ctx );
824825
826+ if (self -> input_buf ) {
827+ efree (self -> input_buf );
828+ }
829+
825830 efree (self );
826831
827832 stream -> abstract = NULL ;
@@ -845,48 +850,33 @@ static ssize_t php_brotli_decompress_read(php_stream *stream,
845850 STREAM_DATA_FROM_STREAM ();
846851
847852 /* input */
848- uint8_t * input = (uint8_t * )emalloc (PHP_BROTLI_BUFFER_SIZE );
849- if (!input ) {
850- #if PHP_VERSION_ID < 70400
851- return 0 ;
852- #else
853- return -1 ;
854- #endif
853+ if (!self -> input_buf ) {
854+ self -> input_buf = emalloc (PHP_BROTLI_BUFFER_SIZE );
855855 }
856856
857857 if (self -> result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT ) {
858858 if (php_stream_eof (self -> stream )) {
859859 /* corrupt input */
860- efree (input );
861860#if PHP_VERSION_ID < 70400
862861 return 0 ;
863862#else
864863 return -1 ;
865864#endif
866865 }
867- self -> ctx .available_in = php_stream_read (self -> stream , input ,
866+ self -> ctx .available_in = php_stream_read (self -> stream , self -> input_buf ,
868867 PHP_BROTLI_BUFFER_SIZE );
869868 if (!self -> ctx .available_in ) {
870- efree (input );
871869#if PHP_VERSION_ID < 70400
872870 return 0 ;
873871#else
874872 return -1 ;
875873#endif
876874 }
877- self -> ctx .next_in = input ;
875+ self -> ctx .next_in = self -> input_buf ;
878876 }
879877
880878 /* output */
881879 uint8_t * output = (uint8_t * )emalloc (count );
882- if (!output ) {
883- efree (input );
884- #if PHP_VERSION_ID < 70400
885- return 0 ;
886- #else
887- return -1 ;
888- #endif
889- }
890880 self -> ctx .available_out = count ;
891881 self -> ctx .next_out = output ;
892882
@@ -912,15 +902,15 @@ static ssize_t php_brotli_decompress_read(php_stream *stream,
912902 break ;
913903 }
914904 self -> ctx .available_in = php_stream_read (self -> stream ,
915- input , count );
916- self -> ctx .next_in = input ;
905+ self -> input_buf ,
906+ PHP_BROTLI_BUFFER_SIZE );
907+ self -> ctx .next_in = self -> input_buf ;
917908 } else {
918909 /* decoder error */
919910 break ;
920911 }
921912 }
922913
923- efree (input );
924914 efree (output );
925915
926916 return ret ;
0 commit comments