From 05f5d050684ac2329afde6e5034db20aca55843f Mon Sep 17 00:00:00 2001 From: ndossche <7771979+ndossche@users.noreply.github.com> Date: Tue, 26 May 2026 17:16:00 +0200 Subject: [PATCH] Switch to smart_str API This is the same as https://github.com/kjdev/php-ext-brotli/pull/83, but for zstd. --- zstd.c | 74 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/zstd.c b/zstd.c index b12e303..136eb79 100644 --- a/zstd.c +++ b/zstd.c @@ -33,15 +33,10 @@ #include #include #include -#if PHP_VERSION_ID < 70200 -#include -#else -#include "Zend/zend_smart_string.h" -#endif +#include #if defined(HAVE_APCU_SUPPORT) #include #include -#include #endif #include #include @@ -65,6 +60,19 @@ #define zend_string_efree(string) zend_string_free(string) #endif +#if PHP_VERSION_ID < 70200 +static zend_always_inline zend_string *smart_str_extract(smart_str *str) { + if (str->s) { + smart_str_0(str); + zend_string *res = str->s; + str->s = NULL; + return res; + } else { + return ZSTR_EMPTY_ALLOC(); + } +} +#endif + #define ZSTD_WARNING(...) \ php_error_docref(NULL, E_WARNING, __VA_ARGS__) @@ -448,7 +456,7 @@ static size_t zstd_check_compress_level(zend_long level) ZEND_FUNCTION(zstd_compress) { size_t result; - smart_string out = { 0 }; + smart_str out = { 0 }; zend_long level = ZSTD_CLEVEL_DEFAULT; zend_string *input, *dict = NULL; php_zstd_context ctx; @@ -480,15 +488,14 @@ ZEND_FUNCTION(zstd_compress) &ctx.input, ZSTD_e_end); if (ZSTD_isError(result)) { ZSTD_WARNING("%s", ZSTD_getErrorName(result)); - smart_string_free(&out); + smart_str_free(&out); php_zstd_context_free(&ctx); RETURN_FALSE; } - smart_string_appendl(&out, ctx.output.dst, ctx.output.pos); + smart_str_appendl(&out, ctx.output.dst, ctx.output.pos); } while (result > 0); - RETVAL_STRINGL(out.c, out.len); - smart_string_free(&out); + RETVAL_STR(smart_str_extract(&out)); php_zstd_context_free(&ctx); } @@ -497,7 +504,7 @@ ZEND_FUNCTION(zstd_uncompress) { size_t chunk, result; uint64_t size; - smart_string out = { 0 }; + smart_str out = { 0 }; zend_string *input, *dict = NULL; php_zstd_context ctx; @@ -540,21 +547,20 @@ ZEND_FUNCTION(zstd_uncompress) ctx.output.pos = 0; result = ZSTD_decompressStream(ctx.dctx, &ctx.output, &ctx.input); if (ZSTD_IS_ERROR(result)) { - smart_string_free(&out); + smart_str_free(&out); php_zstd_context_free(&ctx); ZSTD_WARNING("%s", ZSTD_getErrorName(result)); RETURN_FALSE; } - smart_string_appendl(&out, ctx.output.dst, ctx.output.pos); + smart_str_appendl(&out, ctx.output.dst, ctx.output.pos); if (result == 0) { break; } } - RETVAL_STRINGL(out.c, out.len); - smart_string_free(&out); + RETVAL_STR(smart_str_extract(&out)); php_zstd_context_free(&ctx); } @@ -562,7 +568,7 @@ ZEND_FUNCTION(zstd_uncompress) ZEND_FUNCTION(zstd_compress_dict) { size_t result; - smart_string out = { 0 }; + smart_str out = { 0 }; zend_long level = ZSTD_CLEVEL_DEFAULT; zend_string *input, *dict; php_zstd_context ctx; @@ -596,15 +602,14 @@ ZEND_FUNCTION(zstd_compress_dict) &ctx.input, ZSTD_e_end); if (ZSTD_isError(result)) { ZSTD_WARNING("%s", ZSTD_getErrorName(result)); - smart_string_free(&out); + smart_str_free(&out); php_zstd_context_free(&ctx); RETURN_FALSE; } - smart_string_appendl(&out, ctx.output.dst, ctx.output.pos); + smart_str_appendl(&out, ctx.output.dst, ctx.output.pos); } while (result > 0); - RETVAL_STRINGL(out.c, out.len); - smart_string_free(&out); + RETVAL_STR(smart_str_extract(&out)); php_zstd_context_free(&ctx); } @@ -613,7 +618,7 @@ ZEND_FUNCTION(zstd_uncompress_dict) { size_t chunk, result; unsigned long long size; - smart_string out = { 0 }; + smart_str out = { 0 }; zend_string *input, *dict; php_zstd_context ctx; @@ -659,21 +664,20 @@ ZEND_FUNCTION(zstd_uncompress_dict) ctx.output.pos = 0; result = ZSTD_decompressStream(ctx.dctx, &ctx.output, &ctx.input); if (ZSTD_IS_ERROR(result)) { - smart_string_free(&out); + smart_str_free(&out); php_zstd_context_free(&ctx); ZSTD_WARNING("%s", ZSTD_getErrorName(result)); RETURN_FALSE; } - smart_string_appendl(&out, ctx.output.dst, ctx.output.pos); + smart_str_appendl(&out, ctx.output.dst, ctx.output.pos); if (result == 0) { break; } } - RETVAL_STRINGL(out.c, out.len); - smart_string_free(&out); + RETVAL_STR(smart_str_extract(&out)); php_zstd_context_free(&ctx); } @@ -706,7 +710,7 @@ ZEND_FUNCTION(zstd_compress_add) php_zstd_context *ctx; zend_string *input; zend_bool end = 0; - smart_string out = {0}; + smart_str out = {0}; #if PHP_VERSION_ID >= 80000 zend_object *obj; #else @@ -743,14 +747,13 @@ ZEND_FUNCTION(zstd_compress_add) &in, end ? ZSTD_e_end : ZSTD_e_flush); if (ZSTD_isError(res)) { ZSTD_WARNING("%s", ZSTD_getErrorName(res)); - smart_string_free(&out); + smart_str_free(&out); RETURN_FALSE; } - smart_string_appendl(&out, ctx->output.dst, ctx->output.pos); + smart_str_appendl(&out, ctx->output.dst, ctx->output.pos); } while (res > 0); - RETVAL_STRINGL(out.c, out.len); - smart_string_free(&out); + RETVAL_STR(smart_str_extract(&out)); } ZEND_FUNCTION(zstd_uncompress_init) @@ -775,7 +778,7 @@ ZEND_FUNCTION(zstd_uncompress_add) zend_object *context; php_zstd_context *ctx; zend_string *input; - smart_string out = {0}; + smart_str out = {0}; #if PHP_VERSION_ID >= 80000 zend_object *obj; #else @@ -815,15 +818,14 @@ ZEND_FUNCTION(zstd_uncompress_add) res = ZSTD_decompressStream(ctx->dctx, &ctx->output, &in); if (ZSTD_isError(res)) { ZSTD_WARNING("%s", ZSTD_getErrorName(res)); - smart_string_free(&out); + smart_str_free(&out); RETURN_FALSE; } - smart_string_appendl(&out, ctx->output.dst, ctx->output.pos); + smart_str_appendl(&out, ctx->output.dst, ctx->output.pos); } - RETVAL_STRINGL(out.c, out.len); - smart_string_free(&out); + RETVAL_STR(smart_str_extract(&out)); } typedef struct _php_zstd_stream_data {