Skip to content

Commit c4fff56

Browse files
committed
feat: minimum system libzstd library version to 1.4.0
1 parent 4cac95a commit c4fff56

2 files changed

Lines changed: 5 additions & 130 deletions

File tree

config.m4

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ PHP_ARG_WITH(libzstd, whether to use system zstd library,
2929

3030
if test "$PHP_ZSTD" != "no"; then
3131

32+
LIBZSTD_MIN_VERSION=1.4.0
33+
3234
if test "$PHP_LIBZSTD" != "no"; then
3335
AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
3436

3537
AC_MSG_CHECKING(for libzstd)
3638
if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists libzstd; then
37-
if $PKG_CONFIG libzstd --atleast-version 1; then
39+
if $PKG_CONFIG libzstd --atleast-version $LIBZSTD_MIN_VERSION; then
3840
LIBZSTD_CFLAGS=`$PKG_CONFIG libzstd --cflags`
3941
LIBZSTD_LIBDIR=`$PKG_CONFIG libzstd --libs`
4042
LIBZSTD_VERSON=`$PKG_CONFIG libzstd --modversion`
4143
AC_MSG_RESULT(from pkgconfig: version $LIBZSTD_VERSON)
4244
else
43-
AC_MSG_ERROR(system libzstd is too old)
45+
AC_MSG_ERROR(system libzstd mus be upgraded to version >= $LIBZSTD_MIN_VERSION)
4446
fi
4547
else
4648
AC_MSG_ERROR(pkg-config not found)

zstd.c

Lines changed: 1 addition & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -86,19 +86,12 @@ static size_t zstd_check_compress_level(zend_long level)
8686
{
8787
uint16_t maxLevel = (uint16_t) ZSTD_maxCLevel();
8888

89-
#if ZSTD_VERSION_NUMBER >= 10304
9089
if (level > maxLevel) {
9190
ZSTD_WARNING("compression level (" ZEND_LONG_FMT ")"
9291
" must be within 1..%d or smaller then 0", level, maxLevel);
9392
return 0;
9493
}
95-
#else
96-
if (level > maxLevel || level < 0) {
97-
ZSTD_WARNING("compression level (%ld)"
98-
" must be within 1..%d", level, maxLevel);
99-
return 0;
100-
}
101-
#endif
94+
10295
return 1;
10396
}
10497

@@ -423,53 +416,6 @@ static int php_zstd_decomp_close(php_stream *stream, int close_handle)
423416
return EOF;
424417
}
425418

426-
#if ZSTD_VERSION_NUMBER < 10400
427-
static int php_zstd_comp_flush_or_end(php_zstd_stream_data *self, int end)
428-
{
429-
size_t res;
430-
int ret = 0;
431-
432-
/* Compress remaining data */
433-
if (self->input.size) {
434-
self->input.pos = 0;
435-
do {
436-
self->output.size = self->sizeout;
437-
self->output.pos = 0;
438-
res = ZSTD_compressStream(self->cctx, &self->output, &self->input);
439-
if (ZSTD_IS_ERROR(res)) {
440-
php_error_docref(NULL, E_WARNING,
441-
"libzstd error %s\n", ZSTD_getErrorName(res));
442-
ret = EOF;
443-
}
444-
php_stream_write(self->stream, self->bufout, self->output.pos);
445-
} while (self->input.pos != self->input.size);
446-
}
447-
448-
/* Flush / End */
449-
do {
450-
self->output.size = self->sizeout;
451-
self->output.pos = 0;
452-
453-
if (end) {
454-
res = ZSTD_endStream(self->cctx, &self->output);
455-
} else {
456-
res = ZSTD_flushStream(self->cctx, &self->output);
457-
}
458-
if (ZSTD_IS_ERROR(res)) {
459-
php_error_docref(NULL, E_WARNING,
460-
"libzstd error %s\n", ZSTD_getErrorName(res));
461-
ret = EOF;
462-
}
463-
php_stream_write(self->stream, self->bufout, self->output.pos);
464-
} while (res > 0);
465-
466-
self->input.pos = 0;
467-
self->input.size = 0;
468-
469-
return ret;
470-
}
471-
472-
#else
473419
static int php_zstd_comp_flush_or_end(php_zstd_stream_data *self, int end)
474420
{
475421
size_t res;
@@ -492,7 +438,6 @@ static int php_zstd_comp_flush_or_end(php_zstd_stream_data *self, int end)
492438

493439
return ret;
494440
}
495-
#endif
496441

497442

498443
static int php_zstd_comp_flush(php_stream *stream)
@@ -521,12 +466,7 @@ static int php_zstd_comp_close(php_stream *stream, int close_handle)
521466
}
522467

523468
ZSTD_freeCCtx(self->cctx);
524-
#if ZSTD_VERSION_NUMBER >= 10400
525469
efree(self->output.dst);
526-
#else
527-
efree(self->bufin);
528-
efree(self->bufout);
529-
#endif
530470
efree(self);
531471
stream->abstract = NULL;
532472

@@ -604,7 +544,6 @@ php_zstd_comp_write(php_stream *stream, const char *buf, size_t count)
604544
{
605545
STREAM_DATA_FROM_STREAM();
606546

607-
#if ZSTD_VERSION_NUMBER >= 10400
608547
size_t res;
609548
ZSTD_inBuffer in = { buf, count, 0 };
610549

@@ -624,50 +563,6 @@ php_zstd_comp_write(php_stream *stream, const char *buf, size_t count)
624563
} while (res > 0);
625564

626565
return count;
627-
628-
#else
629-
size_t ret = 0;
630-
size_t x, res;
631-
632-
while(count > 0) {
633-
/* enough room for full data */
634-
if (self->input.size + count < self->sizein) {
635-
memcpy(self->bufin + self->input.size, buf, count);
636-
self->input.size += count;
637-
ret += count;
638-
count = 0;
639-
break;
640-
}
641-
642-
/* fill input buffer */
643-
x = self->sizein - self->input.size;
644-
memcpy(self->bufin + self->input.size, buf, x);
645-
self->input.size += x;
646-
buf += x;
647-
count -= x;
648-
ret += x;
649-
650-
/* compress and write */
651-
self->input.pos = 0;
652-
do {
653-
self->output.size = self->sizeout;
654-
self->output.pos = 0;
655-
res = ZSTD_compressStream(self->cctx, &self->output, &self->input);
656-
if (ZSTD_IS_ERROR(res)) {
657-
php_error_docref(NULL, E_WARNING,
658-
"libzstd error %s\n", ZSTD_getErrorName(res));
659-
#if PHP_VERSION_ID >= 70400
660-
return -1;
661-
#endif
662-
}
663-
php_stream_write(self->stream, self->bufout, self->output.pos);
664-
} while (self->input.pos != self->input.size);
665-
666-
self->input.pos = 0;
667-
self->input.size = 0;
668-
}
669-
return ret;
670-
#endif
671566
}
672567

673568

@@ -710,10 +605,8 @@ php_stream_zstd_opener(
710605
php_zstd_stream_data *self;
711606
int level = ZSTD_CLEVEL_DEFAULT;
712607
int compress;
713-
#if ZSTD_VERSION_NUMBER >= 10400
714608
ZSTD_CDict *cdict = NULL;
715609
ZSTD_DDict *ddict = NULL;
716-
#endif
717610

718611
if (strncasecmp(STREAM_NAME, path, sizeof(STREAM_NAME)-1) == 0) {
719612
path += sizeof(STREAM_NAME)-1;
@@ -744,7 +637,6 @@ php_stream_zstd_opener(
744637
if (NULL != tmpzval) {
745638
level = zval_get_long(tmpzval);
746639
}
747-
#if ZSTD_VERSION_NUMBER >= 10400
748640
tmpzval = php_stream_context_get_option(context, "zstd", "dict");
749641
if (NULL != tmpzval) {
750642
data = zval_get_string(tmpzval);
@@ -755,7 +647,6 @@ php_stream_zstd_opener(
755647
}
756648
zend_string_release(data);
757649
}
758-
#endif
759650
}
760651

761652
if (level > ZSTD_maxCLevel()) {
@@ -784,7 +675,6 @@ php_stream_zstd_opener(
784675
efree(self);
785676
return NULL;
786677
}
787-
#if ZSTD_VERSION_NUMBER >= 10400
788678
ZSTD_CCtx_reset(self->cctx, ZSTD_reset_session_only);
789679
ZSTD_CCtx_refCDict(self->cctx, cdict);
790680
ZSTD_CCtx_setParameter(self->cctx, ZSTD_c_compressionLevel, level);
@@ -793,19 +683,6 @@ php_stream_zstd_opener(
793683
self->output.dst = emalloc(self->output.size);
794684
self->output.pos = 0;
795685

796-
#else
797-
ZSTD_initCStream(self->cctx, level);
798-
799-
self->bufin = emalloc(self->sizein = ZSTD_CStreamInSize());
800-
self->bufout = emalloc(self->sizeout = ZSTD_CStreamOutSize());
801-
self->input.src = self->bufin;
802-
self->input.pos = 0;
803-
self->input.size = 0;
804-
self->output.dst = self->bufout;
805-
self->output.pos = 0;
806-
self->output.size = 0;
807-
#endif
808-
809686
return php_stream_alloc(&php_stream_zstd_write_ops, self, NULL, mode);
810687

811688
} else {
@@ -820,12 +697,8 @@ php_stream_zstd_opener(
820697
self->cctx = NULL;
821698
self->bufin = emalloc(self->sizein = ZSTD_DStreamInSize());
822699
self->bufout = emalloc(self->sizeout = ZSTD_DStreamOutSize());
823-
#if ZSTD_VERSION_NUMBER >= 10400
824700
ZSTD_DCtx_reset(self->dctx, ZSTD_reset_session_only);
825701
ZSTD_DCtx_refDDict(self->dctx, ddict);
826-
#else
827-
ZSTD_initDStream(self->dctx);
828-
#endif
829702
self->input.src = self->bufin;
830703
self->input.pos = 0;
831704
self->input.size = 0;

0 commit comments

Comments
 (0)