Skip to content

Commit 979aa4a

Browse files
committed
refactor: zstd_compress_dict()
1 parent cc7a177 commit 979aa4a

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

zstd.c

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,11 @@ ZEND_FUNCTION(zstd_uncompress)
289289
ZEND_FUNCTION(zstd_compress_dict)
290290
{
291291
zend_long level = DEFAULT_COMPRESS_LEVEL;
292-
293292
zend_string *output;
294293
char *input, *dict;
295-
size_t input_len, dict_len;
294+
size_t input_len, dict_len, size, result;
295+
ZSTD_CCtx *cctx;
296+
ZSTD_CDict *cdict;
296297

297298
ZEND_PARSE_PARAMETERS_START(2, 3)
298299
Z_PARAM_STRING(input, input_len)
@@ -305,40 +306,37 @@ ZEND_FUNCTION(zstd_compress_dict)
305306
RETURN_FALSE;
306307
}
307308

308-
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
309+
cctx = ZSTD_createCCtx();
309310
if (cctx == NULL) {
310311
ZSTD_WARNING("ZSTD_createCCtx() error");
311312
RETURN_FALSE;
312313
}
313-
ZSTD_CDict* const cdict = ZSTD_createCDict(dict,
314-
dict_len,
315-
(int)level);
314+
315+
cdict = ZSTD_createCDict(dict, dict_len, (int) level);
316316
if (!cdict) {
317-
ZSTD_freeCStream(cctx);
317+
ZSTD_freeCCtx(cctx);
318318
ZSTD_WARNING("ZSTD_createCDict() error");
319319
RETURN_FALSE;
320320
}
321321

322-
size_t const cBuffSize = ZSTD_compressBound(input_len);
323-
output = zend_string_alloc(cBuffSize, 0);
322+
size = ZSTD_compressBound(input_len);
323+
output = zend_string_alloc(size, 0);
324324

325-
size_t const cSize = ZSTD_compress_usingCDict(cctx, ZSTR_VAL(output), cBuffSize,
326-
input,
327-
input_len,
328-
cdict);
329-
if (ZSTD_IS_ERROR(cSize)) {
330-
ZSTD_freeCStream(cctx);
325+
result = ZSTD_compress_usingCDict(cctx, ZSTR_VAL(output), size,
326+
input, input_len, cdict);
327+
if (ZSTD_IS_ERROR(result)) {
328+
ZSTD_freeCCtx(cctx);
331329
ZSTD_freeCDict(cdict);
332330
zend_string_efree(output);
333-
ZSTD_WARNING("%s", ZSTD_getErrorName(cSize));
331+
ZSTD_WARNING("%s", ZSTD_getErrorName(result));
334332
RETURN_FALSE;
335333
}
336334

337-
output = zstd_string_output_truncate(output, cSize);
338-
RETVAL_NEW_STR(output);
339-
340335
ZSTD_freeCCtx(cctx);
341336
ZSTD_freeCDict(cdict);
337+
338+
output = zstd_string_output_truncate(output, result);
339+
RETVAL_NEW_STR(output);
342340
}
343341

344342
ZEND_FUNCTION(zstd_uncompress_dict)

0 commit comments

Comments
 (0)