If the user tries a higher level than supported by libzstd,
initialization will fail. Instead, let’s clamp the level to the highest
supported value.
Regarding negative levels: They are supported from libzstd 1.3.4, but
the query function ZSTD_minCLevel is only supported from 1.4.0 (from
1.3.6 with ZSTD_STATIC_LINKING_ONLY), so let’s not use it for
verification of the level. In libzstd 1.3.3 and older, negative levels
are silently converted to the zstd’s default level (3), so there’s no
major harm done if a user uses a negative level with older libzstd
versions.
state->stream = ZSTD_createCStream();
state->failed = false;
- size_t ret = ZSTD_initCStream(state->stream, level);
+ int8_t actual_level = MIN(level, ZSTD_maxCLevel());
+ if (actual_level != level) {
+ cc_log(
+ "Using compression level %d (max libzstd level) instead of %d",
+ actual_level,
+ level);
+ }
+
+ size_t ret = ZSTD_initCStream(state->stream, actual_level);
if (ZSTD_isError(ret)) {
ZSTD_freeCStream(state->stream);
free(state);