-static const struct wimlib_lzms_compressor_params lzms_default = {
- .hdr = {
- .size = sizeof(struct wimlib_lzms_compressor_params),
- },
- .min_match_length = 2,
- .max_match_length = UINT32_MAX,
- .nice_match_length = 32,
- .max_search_depth = 50,
- .optim_array_length = 1024,
-};
-
-static bool
-lzms_params_valid(const struct wimlib_compressor_params_header *);
-
-static const struct wimlib_lzms_compressor_params *
-lzms_get_params(const struct wimlib_compressor_params_header *_params)
-{
- const struct wimlib_lzms_compressor_params *params =
- (const struct wimlib_lzms_compressor_params*)_params;
-
- if (params == NULL)
- params = &lzms_default;
-
- LZMS_ASSERT(lzms_params_valid(¶ms->hdr));
-
- return params;
-}
-
-static int
-lzms_create_compressor(size_t max_block_size,
- const struct wimlib_compressor_params_header *_params,
- void **ctx_ret)
-{
- struct lzms_compressor *ctx;
- const struct wimlib_lzms_compressor_params *params = lzms_get_params(_params);
-
- if (max_block_size == 0 || max_block_size >= INT32_MAX) {
- LZMS_DEBUG("Invalid max_block_size (%u)", max_block_size);
- return WIMLIB_ERR_INVALID_PARAM;
- }
-
- ctx = CALLOC(1, sizeof(struct lzms_compressor));
- if (ctx == NULL)
- goto oom;
-
- ctx->window = MALLOC(max_block_size);
- if (ctx->window == NULL)
- goto oom;
-
- ctx->matches = MALLOC(min(params->max_match_length -
- params->min_match_length + 1,
- params->max_search_depth + 2) *
- sizeof(ctx->matches[0]));
- if (ctx->matches == NULL)
- goto oom;
-
- if (!lz_bt_init(&ctx->mf,
- max_block_size,
- params->min_match_length,
- params->max_match_length,
- params->nice_match_length,
- params->max_search_depth))
- goto oom;
-
- ctx->optimum = MALLOC((params->optim_array_length +
- min(params->nice_match_length,
- params->max_match_length)) *
- sizeof(ctx->optimum[0]));
- if (!ctx->optimum)
- goto oom;
-
- /* Initialize position and length slot data if not done already. */
- lzms_init_slots();
-
- /* Initialize range encoding cost table if not done already. */
- lzms_init_rc_costs();
-
- ctx->max_block_size = max_block_size;
- memcpy(&ctx->params, params, sizeof(*params));
-
- *ctx_ret = ctx;
- return 0;
-
-oom:
- lzms_free_compressor(ctx);
- return WIMLIB_ERR_NOMEM;
-}
-
-static u64
-lzms_get_needed_memory(size_t max_block_size,
- const struct wimlib_compressor_params_header *_params)
-{
- const struct wimlib_lzms_compressor_params *params = lzms_get_params(_params);
-
- u64 size = 0;
-
- size += max_block_size;
- size += sizeof(struct lzms_compressor);
- size += lz_bt_get_needed_memory(max_block_size);
- size += (params->optim_array_length +
- min(params->nice_match_length,
- params->max_match_length)) *
- sizeof(((struct lzms_compressor *)0)->optimum[0]);
- size += min(params->max_match_length - params->min_match_length + 1,
- params->max_search_depth + 2) *
- sizeof(((struct lzms_compressor*)0)->matches[0]);
- return size;
-}
-
-static bool
-lzms_params_valid(const struct wimlib_compressor_params_header *_params)
-{
- const struct wimlib_lzms_compressor_params *params =
- (const struct wimlib_lzms_compressor_params*)_params;
-
- if (params->hdr.size != sizeof(*params) ||
- params->max_match_length < params->min_match_length ||
- params->min_match_length < 2 ||
- params->optim_array_length == 0 ||
- min(params->max_match_length, params->nice_match_length) > 65536)
- return false;
-
- return true;
-}
-