@@ -511,15 +511,15 @@ ZEND_FUNCTION(zstd_uncompress)
511511
512512ZEND_FUNCTION (zstd_compress_dict )
513513{
514+ size_t result ;
515+ smart_string out = { 0 };
514516 zend_long level = DEFAULT_COMPRESS_LEVEL ;
515-
516- zend_string * output ;
517- char * input , * dict ;
518- size_t input_len , dict_len ;
517+ zend_string * input , * dict ;
518+ php_zstd_context ctx ;
519519
520520 ZEND_PARSE_PARAMETERS_START (2 , 3 )
521- Z_PARAM_STRING (input , input_len )
522- Z_PARAM_STRING (dict , dict_len )
521+ Z_PARAM_STR (input )
522+ Z_PARAM_STR (dict )
523523 Z_PARAM_OPTIONAL
524524 Z_PARAM_LONG (level )
525525 ZEND_PARSE_PARAMETERS_END_EX (RETURN_FALSE );
@@ -528,40 +528,33 @@ ZEND_FUNCTION(zstd_compress_dict)
528528 RETURN_FALSE ;
529529 }
530530
531- ZSTD_CCtx * const cctx = ZSTD_createCCtx ();
532- if (cctx == NULL ) {
533- ZSTD_WARNING ("failed to create compress context" );
534- RETURN_FALSE ;
535- }
536- ZSTD_CDict * const cdict = ZSTD_createCDict (dict ,
537- dict_len ,
538- (int )level );
539- if (!cdict ) {
540- ZSTD_freeCStream (cctx );
541- ZSTD_WARNING ("failed to load dictionary" );
531+ php_zstd_context_init (& ctx );
532+ if (php_zstd_context_create_compress (& ctx , level , dict ) != SUCCESS ) {
533+ php_zstd_context_free (& ctx );
542534 RETURN_FALSE ;
543535 }
544536
545- size_t const cBuffSize = ZSTD_compressBound (input_len );
546- output = zend_string_alloc (cBuffSize , 0 );
537+ ctx .input .src = ZSTR_VAL (input );
538+ ctx .input .size = ZSTR_LEN (input );
539+ ctx .input .pos = 0 ;
547540
548- size_t const cSize = ZSTD_compress_usingCDict (cctx , ZSTR_VAL (output ), cBuffSize ,
549- input ,
550- input_len ,
551- cdict );
552- if (ZSTD_IS_ERROR (cSize )) {
553- ZSTD_freeCStream (cctx );
554- ZSTD_freeCDict (cdict );
555- zend_string_efree (output );
556- ZSTD_WARNING ("%s" , ZSTD_getErrorName (cSize ));
557- RETURN_FALSE ;
558- }
541+ do {
542+ ctx .output .pos = 0 ;
543+ result = ZSTD_compressStream2 (ctx .cctx , & ctx .output ,
544+ & ctx .input , ZSTD_e_end );
545+ if (ZSTD_isError (result )) {
546+ ZSTD_WARNING ("%s" , ZSTD_getErrorName (result ));
547+ smart_string_free (& out );
548+ php_zstd_context_free (& ctx );
549+ RETURN_FALSE ;
550+ }
551+ smart_string_appendl (& out , ctx .output .dst , ctx .output .pos );
552+ } while (result > 0 );
559553
560- output = zstd_string_output_truncate ( output , cSize );
561- RETVAL_NEW_STR ( output );
554+ RETVAL_STRINGL ( out . c , out . len );
555+ smart_string_free ( & out );
562556
563- ZSTD_freeCCtx (cctx );
564- ZSTD_freeCDict (cdict );
557+ php_zstd_context_free (& ctx );
565558}
566559
567560ZEND_FUNCTION (zstd_uncompress_dict )
0 commit comments