Path: blob/master/Utilities/cmliblzma/liblzma/lzma/lzma2_encoder.c
3156 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file lzma2_encoder.c5/// \brief LZMA2 encoder6///7// Authors: Igor Pavlov8// Lasse Collin9//10///////////////////////////////////////////////////////////////////////////////1112#include "lz_encoder.h"13#include "lzma_encoder.h"14#include "fastpos.h"15#include "lzma2_encoder.h"161718typedef struct {19enum {20SEQ_INIT,21SEQ_LZMA_ENCODE,22SEQ_LZMA_COPY,23SEQ_UNCOMPRESSED_HEADER,24SEQ_UNCOMPRESSED_COPY,25} sequence;2627/// LZMA encoder28void *lzma;2930/// LZMA options currently in use.31lzma_options_lzma opt_cur;3233bool need_properties;34bool need_state_reset;35bool need_dictionary_reset;3637/// Uncompressed size of a chunk38size_t uncompressed_size;3940/// Compressed size of a chunk (excluding headers); this is also used41/// to indicate the end of buf[] in SEQ_LZMA_COPY.42size_t compressed_size;4344/// Read position in buf[]45size_t buf_pos;4647/// Buffer to hold the chunk header and LZMA compressed data48uint8_t buf[LZMA2_HEADER_MAX + LZMA2_CHUNK_MAX];49} lzma_lzma2_coder;505152static void53lzma2_header_lzma(lzma_lzma2_coder *coder)54{55assert(coder->uncompressed_size > 0);56assert(coder->uncompressed_size <= LZMA2_UNCOMPRESSED_MAX);57assert(coder->compressed_size > 0);58assert(coder->compressed_size <= LZMA2_CHUNK_MAX);5960size_t pos;6162if (coder->need_properties) {63pos = 0;6465if (coder->need_dictionary_reset)66coder->buf[pos] = 0x80 + (3 << 5);67else68coder->buf[pos] = 0x80 + (2 << 5);69} else {70pos = 1;7172if (coder->need_state_reset)73coder->buf[pos] = 0x80 + (1 << 5);74else75coder->buf[pos] = 0x80;76}7778// Set the start position for copying.79coder->buf_pos = pos;8081// Uncompressed size82size_t size = coder->uncompressed_size - 1;83coder->buf[pos++] += size >> 16;84coder->buf[pos++] = (size >> 8) & 0xFF;85coder->buf[pos++] = size & 0xFF;8687// Compressed size88size = coder->compressed_size - 1;89coder->buf[pos++] = size >> 8;90coder->buf[pos++] = size & 0xFF;9192// Properties, if needed93if (coder->need_properties)94lzma_lzma_lclppb_encode(&coder->opt_cur, coder->buf + pos);9596coder->need_properties = false;97coder->need_state_reset = false;98coder->need_dictionary_reset = false;99100// The copying code uses coder->compressed_size to indicate the end101// of coder->buf[], so we need add the maximum size of the header here.102coder->compressed_size += LZMA2_HEADER_MAX;103104return;105}106107108static void109lzma2_header_uncompressed(lzma_lzma2_coder *coder)110{111assert(coder->uncompressed_size > 0);112assert(coder->uncompressed_size <= LZMA2_CHUNK_MAX);113114// If this is the first chunk, we need to include dictionary115// reset indicator.116if (coder->need_dictionary_reset)117coder->buf[0] = 1;118else119coder->buf[0] = 2;120121coder->need_dictionary_reset = false;122123// "Compressed" size124coder->buf[1] = (coder->uncompressed_size - 1) >> 8;125coder->buf[2] = (coder->uncompressed_size - 1) & 0xFF;126127// Set the start position for copying.128coder->buf_pos = 0;129return;130}131132133static lzma_ret134lzma2_encode(void *coder_ptr, lzma_mf *restrict mf,135uint8_t *restrict out, size_t *restrict out_pos,136size_t out_size)137{138lzma_lzma2_coder *restrict coder = coder_ptr;139140while (*out_pos < out_size)141switch (coder->sequence) {142case SEQ_INIT:143// If there's no input left and we are flushing or finishing,144// don't start a new chunk.145if (mf_unencoded(mf) == 0) {146// Write end of payload marker if finishing.147if (mf->action == LZMA_FINISH)148out[(*out_pos)++] = 0;149150return mf->action == LZMA_RUN151? LZMA_OK : LZMA_STREAM_END;152}153154if (coder->need_state_reset)155return_if_error(lzma_lzma_encoder_reset(156coder->lzma, &coder->opt_cur));157158coder->uncompressed_size = 0;159coder->compressed_size = 0;160coder->sequence = SEQ_LZMA_ENCODE;161162// Fall through163164case SEQ_LZMA_ENCODE: {165// Calculate how much more uncompressed data this chunk166// could accept.167const uint32_t left = LZMA2_UNCOMPRESSED_MAX168- coder->uncompressed_size;169uint32_t limit;170171if (left < mf->match_len_max) {172// Must flush immediately since the next LZMA symbol173// could make the uncompressed size of the chunk too174// big.175limit = 0;176} else {177// Calculate maximum read_limit that is OK from point178// of view of LZMA2 chunk size.179limit = mf->read_pos - mf->read_ahead180+ left - mf->match_len_max;181}182183// Save the start position so that we can update184// coder->uncompressed_size.185const uint32_t read_start = mf->read_pos - mf->read_ahead;186187// Call the LZMA encoder until the chunk is finished.188const lzma_ret ret = lzma_lzma_encode(coder->lzma, mf,189coder->buf + LZMA2_HEADER_MAX,190&coder->compressed_size,191LZMA2_CHUNK_MAX, limit);192193coder->uncompressed_size += mf->read_pos - mf->read_ahead194- read_start;195196assert(coder->compressed_size <= LZMA2_CHUNK_MAX);197assert(coder->uncompressed_size <= LZMA2_UNCOMPRESSED_MAX);198199if (ret != LZMA_STREAM_END)200return LZMA_OK;201202// See if the chunk compressed. If it didn't, we encode it203// as uncompressed chunk. This saves a few bytes of space204// and makes decoding faster.205if (coder->compressed_size >= coder->uncompressed_size) {206coder->uncompressed_size += mf->read_ahead;207assert(coder->uncompressed_size208<= LZMA2_UNCOMPRESSED_MAX);209mf->read_ahead = 0;210lzma2_header_uncompressed(coder);211coder->need_state_reset = true;212coder->sequence = SEQ_UNCOMPRESSED_HEADER;213break;214}215216// The chunk did compress at least by one byte, so we store217// the chunk as LZMA.218lzma2_header_lzma(coder);219220coder->sequence = SEQ_LZMA_COPY;221}222223// Fall through224225case SEQ_LZMA_COPY:226// Copy the compressed chunk along its headers to the227// output buffer.228lzma_bufcpy(coder->buf, &coder->buf_pos,229coder->compressed_size,230out, out_pos, out_size);231if (coder->buf_pos != coder->compressed_size)232return LZMA_OK;233234coder->sequence = SEQ_INIT;235break;236237case SEQ_UNCOMPRESSED_HEADER:238// Copy the three-byte header to indicate uncompressed chunk.239lzma_bufcpy(coder->buf, &coder->buf_pos,240LZMA2_HEADER_UNCOMPRESSED,241out, out_pos, out_size);242if (coder->buf_pos != LZMA2_HEADER_UNCOMPRESSED)243return LZMA_OK;244245coder->sequence = SEQ_UNCOMPRESSED_COPY;246247// Fall through248249case SEQ_UNCOMPRESSED_COPY:250// Copy the uncompressed data as is from the dictionary251// to the output buffer.252mf_read(mf, out, out_pos, out_size, &coder->uncompressed_size);253if (coder->uncompressed_size != 0)254return LZMA_OK;255256coder->sequence = SEQ_INIT;257break;258}259260return LZMA_OK;261}262263264static void265lzma2_encoder_end(void *coder_ptr, const lzma_allocator *allocator)266{267lzma_lzma2_coder *coder = coder_ptr;268lzma_free(coder->lzma, allocator);269lzma_free(coder, allocator);270return;271}272273274static lzma_ret275lzma2_encoder_options_update(void *coder_ptr, const lzma_filter *filter)276{277lzma_lzma2_coder *coder = coder_ptr;278279// New options can be set only when there is no incomplete chunk.280// This is the case at the beginning of the raw stream and right281// after LZMA_SYNC_FLUSH.282if (filter->options == NULL || coder->sequence != SEQ_INIT)283return LZMA_PROG_ERROR;284285// Look if there are new options. At least for now,286// only lc/lp/pb can be changed.287const lzma_options_lzma *opt = filter->options;288if (coder->opt_cur.lc != opt->lc || coder->opt_cur.lp != opt->lp289|| coder->opt_cur.pb != opt->pb) {290// Validate the options.291if (opt->lc > LZMA_LCLP_MAX || opt->lp > LZMA_LCLP_MAX292|| opt->lc + opt->lp > LZMA_LCLP_MAX293|| opt->pb > LZMA_PB_MAX)294return LZMA_OPTIONS_ERROR;295296// The new options will be used when the encoder starts297// a new LZMA2 chunk.298coder->opt_cur.lc = opt->lc;299coder->opt_cur.lp = opt->lp;300coder->opt_cur.pb = opt->pb;301coder->need_properties = true;302coder->need_state_reset = true;303}304305return LZMA_OK;306}307308309static lzma_ret310lzma2_encoder_init(lzma_lz_encoder *lz, const lzma_allocator *allocator,311lzma_vli id lzma_attribute((__unused__)), const void *options,312lzma_lz_options *lz_options)313{314if (options == NULL)315return LZMA_PROG_ERROR;316317lzma_lzma2_coder *coder = lz->coder;318if (coder == NULL) {319coder = lzma_alloc(sizeof(lzma_lzma2_coder), allocator);320if (coder == NULL)321return LZMA_MEM_ERROR;322323lz->coder = coder;324lz->code = &lzma2_encode;325lz->end = &lzma2_encoder_end;326lz->options_update = &lzma2_encoder_options_update;327328coder->lzma = NULL;329}330331coder->opt_cur = *(const lzma_options_lzma *)(options);332333coder->sequence = SEQ_INIT;334coder->need_properties = true;335coder->need_state_reset = false;336coder->need_dictionary_reset337= coder->opt_cur.preset_dict == NULL338|| coder->opt_cur.preset_dict_size == 0;339340// Initialize LZMA encoder341return_if_error(lzma_lzma_encoder_create(&coder->lzma, allocator,342LZMA_FILTER_LZMA2, &coder->opt_cur, lz_options));343344// Make sure that we will always have enough history available in345// case we need to use uncompressed chunks. They are used when the346// compressed size of a chunk is not smaller than the uncompressed347// size, so we need to have at least LZMA2_COMPRESSED_MAX bytes348// history available.349if (lz_options->before_size + lz_options->dict_size < LZMA2_CHUNK_MAX)350lz_options->before_size351= LZMA2_CHUNK_MAX - lz_options->dict_size;352353return LZMA_OK;354}355356357extern lzma_ret358lzma_lzma2_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,359const lzma_filter_info *filters)360{361return lzma_lz_encoder_init(362next, allocator, filters, &lzma2_encoder_init);363}364365366extern uint64_t367lzma_lzma2_encoder_memusage(const void *options)368{369const uint64_t lzma_mem = lzma_lzma_encoder_memusage(options);370if (lzma_mem == UINT64_MAX)371return UINT64_MAX;372373return sizeof(lzma_lzma2_coder) + lzma_mem;374}375376377extern lzma_ret378lzma_lzma2_props_encode(const void *options, uint8_t *out)379{380if (options == NULL)381return LZMA_PROG_ERROR;382383const lzma_options_lzma *const opt = options;384uint32_t d = my_max(opt->dict_size, LZMA_DICT_SIZE_MIN);385386// Round up to the next 2^n - 1 or 2^n + 2^(n - 1) - 1 depending387// on which one is the next:388--d;389d |= d >> 2;390d |= d >> 3;391d |= d >> 4;392d |= d >> 8;393d |= d >> 16;394395// Get the highest two bits using the proper encoding:396if (d == UINT32_MAX)397out[0] = 40;398else399out[0] = get_dist_slot(d + 1) - 24;400401return LZMA_OK;402}403404405extern uint64_t406lzma_lzma2_block_size(const void *options)407{408const lzma_options_lzma *const opt = options;409410if (!IS_ENC_DICT_SIZE_VALID(opt->dict_size))411return UINT64_MAX;412413// Use at least 1 MiB to keep compression ratio better.414return my_max((uint64_t)(opt->dict_size) * 3, UINT64_C(1) << 20);415}416417418