/* This a compressor for the LZMS compression format. More details about this
* format can be found in lzms-decompress.c.
*
- * This is currently an unsophisticated implementation that is fast but does not
- * attain the best compression ratios allowed by the format.
+ * NOTE: this compressor currently does not code any delta matches.
*/
#ifdef HAVE_CONFIG_H
#include "wimlib/compress_common.h"
#include "wimlib/endianness.h"
#include "wimlib/error.h"
-#include "wimlib/lz_hash.h"
#include "wimlib/lz_sarray.h"
#include "wimlib/lzms.h"
#include "wimlib/util.h"
#include <limits.h>
#include <pthread.h>
-#define LZMS_OPTIM_ARRAY_SIZE 1024
-
struct lzms_compressor;
struct lzms_adaptive_state {
struct lzms_lz_lru_queues lru;
/* Size of the data in @buffer. */
u32 window_size;
-#if 0
- /* Temporary array used by lz_analyze_block(); must be at least as long
- * as the window. */
- u32 *prev_tab;
-#endif
-
/* Suffix array match-finder. */
struct lz_sarray lz_sarray;
lzms_end_encode_item(ctx, length);
}
-#if 0
-static void
-lzms_record_literal(u8 literal, void *_ctx)
-{
- struct lzms_compressor *ctx = _ctx;
-
- lzms_encode_literal(ctx, literal);
-}
-
-static void
-lzms_record_match(unsigned length, unsigned offset, void *_ctx)
-{
- struct lzms_compressor *ctx = _ctx;
-
- lzms_encode_lz_match(ctx, length, offset);
-}
-
-static void
-lzms_fast_encode(struct lzms_compressor *ctx)
-{
- static const struct lz_params lzms_lz_params = {
- .min_match = 3,
- .max_match = UINT_MAX,
- .max_offset = UINT_MAX,
- .nice_match = 64,
- .good_match = 32,
- .max_chain_len = 64,
- .max_lazy_match = 258,
- .too_far = 4096,
- };
-
- lz_analyze_block(ctx->window,
- ctx->window_size,
- lzms_record_match,
- lzms_record_literal,
- ctx,
- &lzms_lz_params,
- ctx->prev_tab);
-
-}
-#endif
-
/* Fast heuristic cost evaluation to use in the inner loop of the match-finder.
* Unlike lzms_get_lz_match_cost(), which does a true cost evaluation, this
* simply prioritize matches based on their offset. */
* - The costs of literals and matches are estimated using the range encoder
* states and the semi-adaptive Huffman codes. Except for range encoding
* states, costs are assumed to be constant throughout a single run of the
- * parsing algorithm, which can parse up to LZMS_OPTIM_ARRAY_SIZE bytes of
- * data. This introduces a source of inaccuracy because the probabilities and
- * Huffman codes can change over this part of the data.
+ * parsing algorithm, which can parse up to @optim_array_length (from the
+ * `struct wimlib_lzms_compressor_params') bytes of data. This introduces a
+ * source of inaccuracy because the probabilities and Huffman codes can change
+ * over this part of the data.
*/
static void
-lzms_normal_encode(struct lzms_compressor *ctx)
+lzms_encode(struct lzms_compressor *ctx)
{
struct raw_match match;
/* Copy the uncompressed data into the @ctx->window buffer. */
memcpy(ctx->window, udata, ulen);
- memset(&ctx->window[ulen], 0, 8);
ctx->cur_window_pos = 0;
ctx->window_size = ulen;
/* Compute and encode a literal/match sequence that decompresses to the
* preprocessed data. */
-#if 1
- lzms_normal_encode(ctx);
-#else
- lzms_fast_encode(ctx);
-#endif
+ lzms_encode(ctx);
/* Get and return the compressed data size. */
compressed_size = lzms_finalize(ctx, compressed_data,
if (ctx) {
FREE(ctx->window);
-#if 0
- FREE(ctx->prev_tab);
-#endif
FREE(ctx->matches);
lz_sarray_destroy(&ctx->lz_sarray);
lz_match_chooser_destroy(&ctx->mc);
}
}
-static const struct wimlib_lzms_compressor_params default_params = {
- .hdr = sizeof(struct wimlib_lzms_compressor_params),
+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,
.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;
+ 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;
}
- if (_params)
- params = (const struct wimlib_lzms_compressor_params*)_params;
- else
- params = &default_params;
-
- if (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) {
- LZMS_DEBUG("Invalid compression parameter!");
- return WIMLIB_ERR_INVALID_PARAM;
- }
-
ctx = CALLOC(1, sizeof(struct lzms_compressor));
if (ctx == NULL)
goto oom;
- ctx->window = MALLOC(max_block_size + 8);
+ ctx->window = MALLOC(max_block_size);
if (ctx->window == NULL)
goto oom;
-#if 0
- ctx->prev_tab = MALLOC(max_block_size * sizeof(ctx->prev_tab[0]));
- if (ctx->prev_tab == NULL)
- goto oom;
-#endif
-
ctx->matches = MALLOC(min(params->max_match_length -
params->min_match_length + 1,
params->max_matches_per_pos) *
if (!lz_sarray_init(&ctx->lz_sarray, max_block_size,
params->min_match_length,
- params->max_match_length,
+ min(params->max_match_length, LZ_SARRAY_LEN_MAX),
params->max_search_depth,
params->max_matches_per_pos))
goto oom;
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_sarray_get_needed_memory(max_block_size);
+ size += lz_match_chooser_get_needed_memory(params->optim_array_length,
+ params->nice_match_length,
+ params->max_match_length);
+ size += min(params->max_match_length -
+ params->min_match_length + 1,
+ params->max_matches_per_pos) *
+ 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;
+}
+
const struct compressor_ops lzms_compressor_ops = {
+ .params_valid = lzms_params_valid,
+ .get_needed_memory = lzms_get_needed_memory,
.create_compressor = lzms_create_compressor,
.compress = lzms_compress,
.free_compressor = lzms_free_compressor,