Path: blob/master/Utilities/cmliblzma/liblzma/common/alone_encoder.c
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file alone_encoder.c5/// \brief Encoder for LZMA_Alone files6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#include "common.h"12#include "lzma_encoder.h"131415#define ALONE_HEADER_SIZE (1 + 4 + 8)161718typedef struct {19lzma_next_coder next;2021enum {22SEQ_HEADER,23SEQ_CODE,24} sequence;2526size_t header_pos;27uint8_t header[ALONE_HEADER_SIZE];28} lzma_alone_coder;293031static lzma_ret32alone_encode(void *coder_ptr, const lzma_allocator *allocator,33const uint8_t *restrict in, size_t *restrict in_pos,34size_t in_size, uint8_t *restrict out,35size_t *restrict out_pos, size_t out_size,36lzma_action action)37{38lzma_alone_coder *coder = coder_ptr;3940while (*out_pos < out_size)41switch (coder->sequence) {42case SEQ_HEADER:43lzma_bufcpy(coder->header, &coder->header_pos,44ALONE_HEADER_SIZE,45out, out_pos, out_size);46if (coder->header_pos < ALONE_HEADER_SIZE)47return LZMA_OK;4849coder->sequence = SEQ_CODE;50break;5152case SEQ_CODE:53return coder->next.code(coder->next.coder,54allocator, in, in_pos, in_size,55out, out_pos, out_size, action);5657default:58assert(0);59return LZMA_PROG_ERROR;60}6162return LZMA_OK;63}646566static void67alone_encoder_end(void *coder_ptr, const lzma_allocator *allocator)68{69lzma_alone_coder *coder = coder_ptr;70lzma_next_end(&coder->next, allocator);71lzma_free(coder, allocator);72return;73}747576static lzma_ret77alone_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,78const lzma_options_lzma *options)79{80lzma_next_coder_init(&alone_encoder_init, next, allocator);8182lzma_alone_coder *coder = next->coder;8384if (coder == NULL) {85coder = lzma_alloc(sizeof(lzma_alone_coder), allocator);86if (coder == NULL)87return LZMA_MEM_ERROR;8889next->coder = coder;90next->code = &alone_encode;91next->end = &alone_encoder_end;92coder->next = LZMA_NEXT_CODER_INIT;93}9495// Basic initializations96coder->sequence = SEQ_HEADER;97coder->header_pos = 0;9899// Encode the header:100// - Properties (1 byte)101if (lzma_lzma_lclppb_encode(options, coder->header))102return LZMA_OPTIONS_ERROR;103104// - Dictionary size (4 bytes)105if (options->dict_size < LZMA_DICT_SIZE_MIN)106return LZMA_OPTIONS_ERROR;107108// Round up to the next 2^n or 2^n + 2^(n - 1) depending on which109// one is the next unless it is UINT32_MAX. While the header would110// allow any 32-bit integer, we do this to keep the decoder of liblzma111// accepting the resulting files.112uint32_t d = options->dict_size - 1;113d |= d >> 2;114d |= d >> 3;115d |= d >> 4;116d |= d >> 8;117d |= d >> 16;118if (d != UINT32_MAX)119++d;120121write32le(coder->header + 1, d);122123// - Uncompressed size (always unknown and using EOPM)124memset(coder->header + 1 + 4, 0xFF, 8);125126// Initialize the LZMA encoder.127const lzma_filter_info filters[2] = {128{129.id = LZMA_FILTER_LZMA1,130.init = &lzma_lzma_encoder_init,131.options = (void *)(options),132}, {133.init = NULL,134}135};136137return lzma_next_filter_init(&coder->next, allocator, filters);138}139140141extern LZMA_API(lzma_ret)142lzma_alone_encoder(lzma_stream *strm, const lzma_options_lzma *options)143{144lzma_next_strm_init(alone_encoder_init, strm, options);145146strm->internal->supported_actions[LZMA_RUN] = true;147strm->internal->supported_actions[LZMA_FINISH] = true;148149return LZMA_OK;150}151152153