1*3b35e7eeSXin LI // SPDX-License-Identifier: 0BSD
2*3b35e7eeSXin LI
373ed8e77SXin LI ///////////////////////////////////////////////////////////////////////////////
473ed8e77SXin LI //
573ed8e77SXin LI /// \file microlzma_encoder.c
673ed8e77SXin LI /// \brief Encode into MicroLZMA format
773ed8e77SXin LI //
873ed8e77SXin LI // Author: Lasse Collin
973ed8e77SXin LI //
1073ed8e77SXin LI ///////////////////////////////////////////////////////////////////////////////
1173ed8e77SXin LI
1273ed8e77SXin LI #include "lzma_encoder.h"
1373ed8e77SXin LI
1473ed8e77SXin LI
1573ed8e77SXin LI typedef struct {
1673ed8e77SXin LI /// LZMA1 encoder
1773ed8e77SXin LI lzma_next_coder lzma;
1873ed8e77SXin LI
1973ed8e77SXin LI /// LZMA properties byte (lc/lp/pb)
2073ed8e77SXin LI uint8_t props;
2173ed8e77SXin LI } lzma_microlzma_coder;
2273ed8e77SXin LI
2373ed8e77SXin LI
2473ed8e77SXin LI static lzma_ret
microlzma_encode(void * coder_ptr,const lzma_allocator * allocator,const uint8_t * restrict in,size_t * restrict in_pos,size_t in_size,uint8_t * restrict out,size_t * restrict out_pos,size_t out_size,lzma_action action)2573ed8e77SXin LI microlzma_encode(void *coder_ptr, const lzma_allocator *allocator,
2673ed8e77SXin LI const uint8_t *restrict in, size_t *restrict in_pos,
2773ed8e77SXin LI size_t in_size, uint8_t *restrict out,
2873ed8e77SXin LI size_t *restrict out_pos, size_t out_size, lzma_action action)
2973ed8e77SXin LI {
3073ed8e77SXin LI lzma_microlzma_coder *coder = coder_ptr;
3173ed8e77SXin LI
3273ed8e77SXin LI // Remember *out_pos so that we can overwrite the first byte with
3373ed8e77SXin LI // the LZMA properties byte.
3473ed8e77SXin LI const size_t out_start = *out_pos;
3573ed8e77SXin LI
3673ed8e77SXin LI // Remember *in_pos so that we can set it based on how many
3773ed8e77SXin LI // uncompressed bytes were actually encoded.
3873ed8e77SXin LI const size_t in_start = *in_pos;
3973ed8e77SXin LI
4073ed8e77SXin LI // Set the output size limit based on the available output space.
4173ed8e77SXin LI // We know that the encoder supports set_out_limit() so
4273ed8e77SXin LI // LZMA_OPTIONS_ERROR isn't possible. LZMA_BUF_ERROR is possible
4373ed8e77SXin LI // but lzma_code() has an assertion to not allow it to be returned
4473ed8e77SXin LI // from here and I don't want to change that for now, so
4573ed8e77SXin LI // LZMA_BUF_ERROR becomes LZMA_PROG_ERROR.
4673ed8e77SXin LI uint64_t uncomp_size;
4773ed8e77SXin LI if (coder->lzma.set_out_limit(coder->lzma.coder,
4873ed8e77SXin LI &uncomp_size, out_size - *out_pos) != LZMA_OK)
4973ed8e77SXin LI return LZMA_PROG_ERROR;
5073ed8e77SXin LI
5173ed8e77SXin LI // set_out_limit fails if this isn't true.
5273ed8e77SXin LI assert(out_size - *out_pos >= 6);
5373ed8e77SXin LI
5473ed8e77SXin LI // Encode as much as possible.
5573ed8e77SXin LI const lzma_ret ret = coder->lzma.code(coder->lzma.coder, allocator,
5673ed8e77SXin LI in, in_pos, in_size, out, out_pos, out_size, action);
5773ed8e77SXin LI
5873ed8e77SXin LI if (ret != LZMA_STREAM_END) {
5973ed8e77SXin LI if (ret == LZMA_OK) {
6073ed8e77SXin LI assert(0);
6173ed8e77SXin LI return LZMA_PROG_ERROR;
6273ed8e77SXin LI }
6373ed8e77SXin LI
6473ed8e77SXin LI return ret;
6573ed8e77SXin LI }
6673ed8e77SXin LI
6773ed8e77SXin LI // The first output byte is bitwise-negation of the properties byte.
6873ed8e77SXin LI // We know that there is space for this byte because set_out_limit
6973ed8e77SXin LI // and the actual encoding succeeded.
7073ed8e77SXin LI out[out_start] = (uint8_t)(~coder->props);
7173ed8e77SXin LI
7273ed8e77SXin LI // The LZMA encoder likely read more input than it was able to encode.
7373ed8e77SXin LI // Set *in_pos based on uncomp_size.
7473ed8e77SXin LI assert(uncomp_size <= in_size - in_start);
7573ed8e77SXin LI *in_pos = in_start + (size_t)(uncomp_size);
7673ed8e77SXin LI
7773ed8e77SXin LI return ret;
7873ed8e77SXin LI }
7973ed8e77SXin LI
8073ed8e77SXin LI
8173ed8e77SXin LI static void
microlzma_encoder_end(void * coder_ptr,const lzma_allocator * allocator)8273ed8e77SXin LI microlzma_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
8373ed8e77SXin LI {
8473ed8e77SXin LI lzma_microlzma_coder *coder = coder_ptr;
8573ed8e77SXin LI lzma_next_end(&coder->lzma, allocator);
8673ed8e77SXin LI lzma_free(coder, allocator);
8773ed8e77SXin LI return;
8873ed8e77SXin LI }
8973ed8e77SXin LI
9073ed8e77SXin LI
9173ed8e77SXin LI static lzma_ret
microlzma_encoder_init(lzma_next_coder * next,const lzma_allocator * allocator,const lzma_options_lzma * options)9273ed8e77SXin LI microlzma_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
9373ed8e77SXin LI const lzma_options_lzma *options)
9473ed8e77SXin LI {
9573ed8e77SXin LI lzma_next_coder_init(µlzma_encoder_init, next, allocator);
9673ed8e77SXin LI
9773ed8e77SXin LI lzma_microlzma_coder *coder = next->coder;
9873ed8e77SXin LI
9973ed8e77SXin LI if (coder == NULL) {
10073ed8e77SXin LI coder = lzma_alloc(sizeof(lzma_microlzma_coder), allocator);
10173ed8e77SXin LI if (coder == NULL)
10273ed8e77SXin LI return LZMA_MEM_ERROR;
10373ed8e77SXin LI
10473ed8e77SXin LI next->coder = coder;
10573ed8e77SXin LI next->code = µlzma_encode;
10673ed8e77SXin LI next->end = µlzma_encoder_end;
10773ed8e77SXin LI
10873ed8e77SXin LI coder->lzma = LZMA_NEXT_CODER_INIT;
10973ed8e77SXin LI }
11073ed8e77SXin LI
11173ed8e77SXin LI // Encode the properties byte. Bitwise-negation of it will be the
11273ed8e77SXin LI // first output byte.
113047153b4SXin LI if (lzma_lzma_lclppb_encode(options, &coder->props))
114047153b4SXin LI return LZMA_OPTIONS_ERROR;
11573ed8e77SXin LI
11673ed8e77SXin LI // Initialize the LZMA encoder.
11773ed8e77SXin LI const lzma_filter_info filters[2] = {
11873ed8e77SXin LI {
11973ed8e77SXin LI .id = LZMA_FILTER_LZMA1,
12073ed8e77SXin LI .init = &lzma_lzma_encoder_init,
12173ed8e77SXin LI .options = (void *)(options),
12273ed8e77SXin LI }, {
12373ed8e77SXin LI .init = NULL,
12473ed8e77SXin LI }
12573ed8e77SXin LI };
12673ed8e77SXin LI
12773ed8e77SXin LI return lzma_next_filter_init(&coder->lzma, allocator, filters);
12873ed8e77SXin LI }
12973ed8e77SXin LI
13073ed8e77SXin LI
13173ed8e77SXin LI extern LZMA_API(lzma_ret)
lzma_microlzma_encoder(lzma_stream * strm,const lzma_options_lzma * options)13273ed8e77SXin LI lzma_microlzma_encoder(lzma_stream *strm, const lzma_options_lzma *options)
13373ed8e77SXin LI {
13473ed8e77SXin LI lzma_next_strm_init(microlzma_encoder_init, strm, options);
13573ed8e77SXin LI
13673ed8e77SXin LI strm->internal->supported_actions[LZMA_FINISH] = true;
13773ed8e77SXin LI
13873ed8e77SXin LI return LZMA_OK;
13973ed8e77SXin LI
14073ed8e77SXin LI }
141