@@ -748,10 +748,7 @@ ZEND_FUNCTION(zstd_uncompress_add)
748748typedef struct _php_zstd_stream_data {
749749 char * bufin , * bufout ;
750750 size_t sizein , sizeout ;
751- ZSTD_CCtx * cctx ;
752- ZSTD_DCtx * dctx ;
753- ZSTD_inBuffer input ;
754- ZSTD_outBuffer output ;
751+ php_zstd_context ctx ;
755752 php_stream * stream ;
756753} php_zstd_stream_data ;
757754
@@ -776,9 +773,9 @@ static int php_zstd_decomp_close(php_stream *stream, int close_handle)
776773 }
777774 }
778775
779- ZSTD_freeDCtx (self -> dctx );
776+ php_zstd_context_free (& self -> ctx );
777+
780778 efree (self -> bufin );
781- efree (self -> bufout );
782779 efree (self );
783780 stream -> abstract = NULL ;
784781
@@ -794,14 +791,15 @@ static int php_zstd_comp_flush_or_end(php_zstd_stream_data *self, int end)
794791
795792 /* Flush / End */
796793 do {
797- self -> output .pos = 0 ;
798- res = ZSTD_compressStream2 (self -> cctx , & self -> output , & in ,
794+ self -> ctx . output .pos = 0 ;
795+ res = ZSTD_compressStream2 (self -> ctx . cctx , & self -> ctx . output , & in ,
799796 end ? ZSTD_e_end : ZSTD_e_flush );
800797 if (ZSTD_isError (res )) {
801798 ZSTD_WARNING ("zstd: %s" , ZSTD_getErrorName (res ));
802799 ret = EOF ;
803800 }
804- php_stream_write (self -> stream , self -> output .dst , self -> output .pos );
801+ php_stream_write (self -> stream ,
802+ self -> ctx .output .dst , self -> ctx .output .pos );
805803 } while (res > 0 );
806804
807805 return ret ;
@@ -833,8 +831,8 @@ static int php_zstd_comp_close(php_stream *stream, int close_handle)
833831 }
834832 }
835833
836- ZSTD_freeCCtx ( self -> cctx );
837- efree ( self -> output . dst );
834+ php_zstd_context_free ( & self -> ctx );
835+
838836 efree (self );
839837 stream -> abstract = NULL ;
840838
@@ -855,44 +853,44 @@ static ssize_t php_zstd_decomp_read(php_stream *stream, char *buf, size_t count)
855853 STREAM_DATA_FROM_STREAM ();
856854
857855 while (count > 0 ) {
858- x = self -> output .size - self -> output .pos ;
856+ x = self -> ctx . output .size - self -> ctx . output .pos ;
859857 /* enough available */
860858 if (x >= count ) {
861- memcpy (buf , self -> bufout + self -> output .pos , count );
862- self -> output .pos += count ;
859+ memcpy (buf , self -> bufout + self -> ctx . output .pos , count );
860+ self -> ctx . output .pos += count ;
863861 ret += count ;
864862 return ret ;
865863 }
866864 /* take remaining from out */
867865 if (x ) {
868- memcpy (buf , self -> bufout + self -> output .pos , x );
869- self -> output .pos += x ;
866+ memcpy (buf , self -> bufout + self -> ctx . output .pos , x );
867+ self -> ctx . output .pos += x ;
870868 ret += x ;
871869 buf += x ;
872870 count -= x ;
873871 }
874872 /* decompress */
875- if (self -> input .pos < self -> input .size ) {
873+ if (self -> ctx . input .pos < self -> ctx . input .size ) {
876874 /* for zstd */
877- self -> output .pos = 0 ;
878- self -> output .size = self -> sizeout ;
879- res = ZSTD_decompressStream (self -> dctx ,
880- & self -> output , & self -> input );
875+ self -> ctx . output .pos = 0 ;
876+ self -> ctx . output .size = self -> sizeout ;
877+ res = ZSTD_decompressStream (self -> ctx . dctx ,
878+ & self -> ctx . output , & self -> ctx . input );
881879 if (ZSTD_IS_ERROR (res )) {
882880 ZSTD_WARNING ("zstd: %s" , ZSTD_getErrorName (res ));
883881#if PHP_VERSION_ID >= 70400
884882 return -1 ;
885883#endif
886884 }
887885 /* for us */
888- self -> output .size = self -> output .pos ;
889- self -> output .pos = 0 ;
886+ self -> ctx . output .size = self -> ctx . output .pos ;
887+ self -> ctx . output .pos = 0 ;
890888 } else {
891889 /* read */
892- self -> input .pos = 0 ;
893- self -> input .size = php_stream_read (self -> stream ,
894- self -> bufin , self -> sizein );
895- if (!self -> input .size ) {
890+ self -> ctx . input .pos = 0 ;
891+ self -> ctx . input .size = php_stream_read (self -> stream ,
892+ self -> bufin , self -> sizein );
893+ if (!self -> ctx . input .size ) {
896894 /* EOF */
897895 count = 0 ;
898896 }
@@ -915,16 +913,17 @@ php_zstd_comp_write(php_stream *stream, const char *buf, size_t count)
915913 ZSTD_inBuffer in = { buf , count , 0 };
916914
917915 do {
918- self -> output .pos = 0 ;
919- res = ZSTD_compressStream2 (self -> cctx , & self -> output ,
916+ self -> ctx . output .pos = 0 ;
917+ res = ZSTD_compressStream2 (self -> ctx . cctx , & self -> ctx . output ,
920918 & in , ZSTD_e_continue );
921919 if (ZSTD_isError (res )) {
922920 ZSTD_WARNING ("zstd: %s" , ZSTD_getErrorName (res ));
923921#if PHP_VERSION_ID >= 70400
924922 return -1 ;
925923#endif
926924 }
927- php_stream_write (self -> stream , self -> output .dst , self -> output .pos );
925+ php_stream_write (self -> stream ,
926+ self -> ctx .output .dst , self -> ctx .output .pos );
928927
929928 } while (res > 0 );
930929
@@ -971,8 +970,7 @@ php_stream_zstd_opener(
971970 php_zstd_stream_data * self ;
972971 int level = ZSTD_CLEVEL_DEFAULT ;
973972 int compress ;
974- ZSTD_CDict * cdict = NULL ;
975- ZSTD_DDict * ddict = NULL ;
973+ zend_string * dict = NULL ;
976974
977975 if (strncasecmp (STREAM_NAME , path , sizeof (STREAM_NAME )- 1 ) == 0 ) {
978976 path += sizeof (STREAM_NAME )- 1 ;
@@ -997,21 +995,14 @@ php_stream_zstd_opener(
997995
998996 if (context ) {
999997 zval * tmpzval ;
1000- zend_string * data ;
1001998
1002999 tmpzval = php_stream_context_get_option (context , "zstd" , "level" );
10031000 if (NULL != tmpzval ) {
10041001 level = zval_get_long (tmpzval );
10051002 }
10061003 tmpzval = php_stream_context_get_option (context , "zstd" , "dict" );
10071004 if (NULL != tmpzval ) {
1008- data = zval_get_string (tmpzval );
1009- if (compress ) {
1010- cdict = ZSTD_createCDict (ZSTR_VAL (data ), ZSTR_LEN (data ), level );
1011- } else {
1012- ddict = ZSTD_createDDict (ZSTR_VAL (data ), ZSTR_LEN (data ));
1013- }
1014- zend_string_release (data );
1005+ dict = zval_get_string (tmpzval );
10151006 }
10161007 }
10171008
@@ -1026,48 +1017,54 @@ php_stream_zstd_opener(
10261017 options | REPORT_ERRORS , NULL );
10271018 if (!self -> stream ) {
10281019 efree (self );
1020+ if (dict ) {
1021+ zend_string_release (dict );
1022+ }
10291023 return NULL ;
10301024 }
10311025
1026+ php_zstd_context_init (& self -> ctx );
1027+
10321028 /* File */
10331029 if (compress ) {
1034- self -> dctx = NULL ;
1035- self -> cctx = ZSTD_createCCtx ();
1036- if (!self -> cctx ) {
1030+ if (php_zstd_context_create_compress (& self -> ctx ,
1031+ level , dict ) != SUCCESS ) {
10371032 ZSTD_WARNING ("zstd: compression context failed" );
10381033 php_stream_close (self -> stream );
10391034 efree (self );
1035+ if (dict ) {
1036+ zend_string_release (dict );
1037+ }
10401038 return NULL ;
10411039 }
1042- ZSTD_CCtx_reset (self -> cctx , ZSTD_reset_session_only );
1043- ZSTD_CCtx_refCDict (self -> cctx , cdict );
1044- ZSTD_CCtx_setParameter (self -> cctx , ZSTD_c_compressionLevel , level );
10451040
1046- self -> output . size = ZSTD_CStreamOutSize ();
1047- self -> output . dst = emalloc ( self -> output . size );
1048- self -> output . pos = 0 ;
1041+ if ( dict ) {
1042+ zend_string_release ( dict );
1043+ }
10491044
10501045 return php_stream_alloc (& php_stream_zstd_write_ops , self , NULL , mode );
10511046
10521047 } else {
1053- self -> dctx = ZSTD_createDCtx ();
1054- if (!self -> dctx ) {
1048+ if (php_zstd_context_create_decompress (& self -> ctx , dict ) != SUCCESS ) {
10551049 ZSTD_WARNING ("zstd: compression context failed" );
10561050 php_stream_close (self -> stream );
10571051 efree (self );
1052+ if (dict ) {
1053+ zend_string_release (dict );
1054+ }
10581055 return NULL ;
10591056 }
1060- self -> cctx = NULL ;
10611057 self -> bufin = emalloc (self -> sizein = ZSTD_DStreamInSize ());
1062- self -> bufout = emalloc (self -> sizeout = ZSTD_DStreamOutSize ());
1063- ZSTD_DCtx_reset (self -> dctx , ZSTD_reset_session_only );
1064- ZSTD_DCtx_refDDict (self -> dctx , ddict );
1065- self -> input .src = self -> bufin ;
1066- self -> input .pos = 0 ;
1067- self -> input .size = 0 ;
1068- self -> output .dst = self -> bufout ;
1069- self -> output .pos = 0 ;
1070- self -> output .size = 0 ;
1058+ self -> bufout = self -> ctx .output .dst ;
1059+ self -> sizeout = self -> ctx .output .size ;
1060+ self -> ctx .input .src = self -> bufin ;
1061+ self -> ctx .input .pos = 0 ;
1062+ self -> ctx .input .size = 0 ;
1063+ self -> ctx .output .size = 0 ;
1064+
1065+ if (dict ) {
1066+ zend_string_release (dict );
1067+ }
10711068
10721069 return php_stream_alloc (& php_stream_zstd_read_ops , self , NULL , mode );
10731070 }
@@ -1246,21 +1243,12 @@ static zend_result php_zstd_output_handler_context_start(php_zstd_context *ctx)
12461243 level = ZSTD_CLEVEL_DEFAULT ;
12471244 }
12481245
1249- ctx -> cctx = ZSTD_createCCtx ();
1250- if (!ctx -> cctx ) {
1246+ zend_string * dict = php_zstd_output_handler_load_dict (ctx );
1247+
1248+ if (php_zstd_context_create_compress (ctx , level , dict ) != SUCCESS ) {
12511249 return FAILURE ;
12521250 }
12531251
1254- php_zstd_output_handler_load_dict (ctx );
1255-
1256- ZSTD_CCtx_reset (ctx -> cctx , ZSTD_reset_session_only );
1257- ZSTD_CCtx_refCDict (ctx -> cctx , ctx -> cdict );
1258- ZSTD_CCtx_setParameter (ctx -> cctx , ZSTD_c_compressionLevel , level );
1259-
1260- ctx -> output .size = ZSTD_CStreamOutSize ();
1261- ctx -> output .dst = emalloc (ctx -> output .size );
1262- ctx -> output .pos = 0 ;
1263-
12641252 return SUCCESS ;
12651253}
12661254
0 commit comments