Rizin
unix-like reverse engineering framework and cli tools
lzma2_encoder.c File Reference

LZMA2 encoder. More...

#include "lz_encoder.h"
#include "lzma_encoder.h"
#include "fastpos.h"
#include "lzma2_encoder.h"

Go to the source code of this file.

Classes

struct  lzma_lzma2_coder
 

Functions

static void lzma2_header_lzma (lzma_lzma2_coder *coder)
 
static void lzma2_header_uncompressed (lzma_lzma2_coder *coder)
 
static lzma_ret lzma2_encode (void *coder_ptr, lzma_mf *restrict mf, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size)
 
static void lzma2_encoder_end (void *coder_ptr, const lzma_allocator *allocator)
 
static lzma_ret lzma2_encoder_options_update (void *coder_ptr, const lzma_filter *filter)
 
static lzma_ret lzma2_encoder_init (lzma_lz_encoder *lz, const lzma_allocator *allocator, const void *options, lzma_lz_options *lz_options)
 
lzma_ret lzma_lzma2_encoder_init (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
 
uint64_t lzma_lzma2_encoder_memusage (const void *options)
 
lzma_ret lzma_lzma2_props_encode (const void *options, uint8_t *out)
 
uint64_t lzma_lzma2_block_size (const void *options)
 

Detailed Description

LZMA2 encoder.

Definition in file lzma2_encoder.c.

Function Documentation

◆ lzma2_encode()

static lzma_ret lzma2_encode ( void *  coder_ptr,
lzma_mf *restrict  mf,
uint8_t *restrict  out,
size_t *restrict  out_pos,
size_t  out_size 
)
static

Definition at line 136 of file lzma2_encoder.c.

139 {
140  lzma_lzma2_coder *restrict coder = coder_ptr;
141 
142  while (*out_pos < out_size)
143  switch (coder->sequence) {
144  case SEQ_INIT:
145  // If there's no input left and we are flushing or finishing,
146  // don't start a new chunk.
147  if (mf_unencoded(mf) == 0) {
148  // Write end of payload marker if finishing.
149  if (mf->action == LZMA_FINISH)
150  out[(*out_pos)++] = 0;
151 
152  return mf->action == LZMA_RUN
154  }
155 
156  if (coder->need_state_reset)
158  coder->lzma, &coder->opt_cur));
159 
160  coder->uncompressed_size = 0;
161  coder->compressed_size = 0;
162  coder->sequence = SEQ_LZMA_ENCODE;
163 
164  // Fall through
165 
166  case SEQ_LZMA_ENCODE: {
167  // Calculate how much more uncompressed data this chunk
168  // could accept.
169  const uint32_t left = LZMA2_UNCOMPRESSED_MAX
170  - coder->uncompressed_size;
171  uint32_t limit;
172 
173  if (left < mf->match_len_max) {
174  // Must flush immediately since the next LZMA symbol
175  // could make the uncompressed size of the chunk too
176  // big.
177  limit = 0;
178  } else {
179  // Calculate maximum read_limit that is OK from point
180  // of view of LZMA2 chunk size.
181  limit = mf->read_pos - mf->read_ahead
182  + left - mf->match_len_max;
183  }
184 
185  // Save the start position so that we can update
186  // coder->uncompressed_size.
187  const uint32_t read_start = mf->read_pos - mf->read_ahead;
188 
189  // Call the LZMA encoder until the chunk is finished.
190  const lzma_ret ret = lzma_lzma_encode(coder->lzma, mf,
191  coder->buf + LZMA2_HEADER_MAX,
192  &coder->compressed_size,
194 
195  coder->uncompressed_size += mf->read_pos - mf->read_ahead
196  - read_start;
197 
198  assert(coder->compressed_size <= LZMA2_CHUNK_MAX);
199  assert(coder->uncompressed_size <= LZMA2_UNCOMPRESSED_MAX);
200 
201  if (ret != LZMA_STREAM_END)
202  return LZMA_OK;
203 
204  // See if the chunk compressed. If it didn't, we encode it
205  // as uncompressed chunk. This saves a few bytes of space
206  // and makes decoding faster.
207  if (coder->compressed_size >= coder->uncompressed_size) {
208  coder->uncompressed_size += mf->read_ahead;
209  assert(coder->uncompressed_size
211  mf->read_ahead = 0;
213  coder->need_state_reset = true;
214  coder->sequence = SEQ_UNCOMPRESSED_HEADER;
215  break;
216  }
217 
218  // The chunk did compress at least by one byte, so we store
219  // the chunk as LZMA.
220  lzma2_header_lzma(coder);
221 
222  coder->sequence = SEQ_LZMA_COPY;
223  }
224 
225  // Fall through
226 
227  case SEQ_LZMA_COPY:
228  // Copy the compressed chunk along its headers to the
229  // output buffer.
230  lzma_bufcpy(coder->buf, &coder->buf_pos,
231  coder->compressed_size,
232  out, out_pos, out_size);
233  if (coder->buf_pos != coder->compressed_size)
234  return LZMA_OK;
235 
236  coder->sequence = SEQ_INIT;
237  break;
238 
239  case SEQ_UNCOMPRESSED_HEADER:
240  // Copy the three-byte header to indicate uncompressed chunk.
241  lzma_bufcpy(coder->buf, &coder->buf_pos,
243  out, out_pos, out_size);
244  if (coder->buf_pos != LZMA2_HEADER_UNCOMPRESSED)
245  return LZMA_OK;
246 
247  coder->sequence = SEQ_UNCOMPRESSED_COPY;
248 
249  // Fall through
250 
251  case SEQ_UNCOMPRESSED_COPY:
252  // Copy the uncompressed data as is from the dictionary
253  // to the output buffer.
254  mf_read(mf, out, out_pos, out_size, &coder->uncompressed_size);
255  if (coder->uncompressed_size != 0)
256  return LZMA_OK;
257 
258  coder->sequence = SEQ_INIT;
259  break;
260  }
261 
262  return LZMA_OK;
263 }
const lzma_allocator const uint8_t size_t uint8_t size_t * out_pos
Definition: block.h:528
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define restrict
static void mf_read(lzma_mf *mf, uint8_t *out, size_t *out_pos, size_t out_size, size_t *left)
Definition: lz_encoder.h:279
static uint32_t mf_unencoded(const lzma_mf *mf)
Definition: lz_encoder.h:238
static void lzma2_header_lzma(lzma_lzma2_coder *coder)
Definition: lzma2_encoder.c:55
static void lzma2_header_uncompressed(lzma_lzma2_coder *coder)
#define LZMA2_HEADER_UNCOMPRESSED
Size of a header for uncompressed chunk.
Definition: lzma2_encoder.h:30
#define LZMA2_CHUNK_MAX
Maximum number of bytes of actual data per chunk (no headers)
Definition: lzma2_encoder.h:21
#define LZMA2_HEADER_MAX
Maximum size of LZMA2 headers.
Definition: lzma2_encoder.h:27
#define LZMA2_UNCOMPRESSED_MAX
Maximum uncompressed size of LZMA chunk (no headers)
Definition: lzma2_encoder.h:24
lzma_ret lzma_lzma_encode(lzma_lzma1_encoder *restrict coder, lzma_mf *restrict mf, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size, uint32_t limit)
Definition: lzma_encoder.c:312
lzma_ret lzma_lzma_encoder_reset(lzma_lzma1_encoder *coder, const lzma_options_lzma *options)
Definition: lzma_encoder.c:476
assert(limit<=UINT32_MAX/2)
static uint32_t const uint8_t uint32_t uint32_t limit
Definition: memcmplen.h:45
unsigned int uint32_t
Definition: sftypes.h:29
uint32_t read_pos
Definition: lz_encoder.h:63
lzma_action action
Definition: lz_encoder.h:119
uint32_t match_len_max
Definition: lz_encoder.h:114
uint32_t read_ahead
Definition: lz_encoder.h:67
#define return_if_error(expr)
Return if expression doesn't evaluate to LZMA_OK.
Definition: common.h:278
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
@ LZMA_FINISH
Finish the coding operation.
Definition: base.h:328
@ LZMA_RUN
Continue coding.
Definition: base.h:251
size_t lzma_bufcpy(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)
Definition: common.c:94

References assert(), limit, LZMA2_CHUNK_MAX, lzma2_header_lzma(), LZMA2_HEADER_MAX, lzma2_header_uncompressed(), LZMA2_HEADER_UNCOMPRESSED, LZMA2_UNCOMPRESSED_MAX, lzma_bufcpy(), LZMA_FINISH, lzma_lzma_encode(), lzma_lzma_encoder_reset(), LZMA_OK, LZMA_RUN, LZMA_STREAM_END, mf_read(), mf_unencoded(), out, out_pos, restrict, and return_if_error.

Referenced by lzma2_encoder_init().

◆ lzma2_encoder_end()

static void lzma2_encoder_end ( void *  coder_ptr,
const lzma_allocator allocator 
)
static

Definition at line 267 of file lzma2_encoder.c.

268 {
269  lzma_lzma2_coder *coder = coder_ptr;
270  lzma_free(coder->lzma, allocator);
271  lzma_free(coder, allocator);
272  return;
273 }
const lzma_allocator * allocator
Definition: block.h:377
lzma_lz_decoder lzma
LZMA decoder.
Definition: lzma2_decoder.c:35
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.
Definition: common.c:78

References allocator, lzma_lzma2_coder::lzma, and lzma_free().

Referenced by lzma2_encoder_init().

◆ lzma2_encoder_init()

static lzma_ret lzma2_encoder_init ( lzma_lz_encoder lz,
const lzma_allocator allocator,
const void *  options,
lzma_lz_options lz_options 
)
static

Definition at line 312 of file lzma2_encoder.c.

314 {
315  if (options == NULL)
316  return LZMA_PROG_ERROR;
317 
318  lzma_lzma2_coder *coder = lz->coder;
319  if (coder == NULL) {
320  coder = lzma_alloc(sizeof(lzma_lzma2_coder), allocator);
321  if (coder == NULL)
322  return LZMA_MEM_ERROR;
323 
324  lz->coder = coder;
325  lz->code = &lzma2_encode;
326  lz->end = &lzma2_encoder_end;
328 
329  coder->lzma = NULL;
330  }
331 
332  coder->opt_cur = *(const lzma_options_lzma *)(options);
333 
334  coder->sequence = SEQ_INIT;
335  coder->need_properties = true;
336  coder->need_state_reset = false;
337  coder->need_dictionary_reset
338  = coder->opt_cur.preset_dict == NULL
339  || coder->opt_cur.preset_dict_size == 0;
340 
341  // Initialize LZMA encoder
343  &coder->opt_cur, lz_options));
344 
345  // Make sure that we will always have enough history available in
346  // case we need to use uncompressed chunks. They are used when the
347  // compressed size of a chunk is not smaller than the uncompressed
348  // size, so we need to have at least LZMA2_COMPRESSED_MAX bytes
349  // history available.
350  if (lz_options->before_size + lz_options->dict_size < LZMA2_CHUNK_MAX)
351  lz_options->before_size
352  = LZMA2_CHUNK_MAX - lz_options->dict_size;
353 
354  return LZMA_OK;
355 }
#define NULL
Definition: cris-opc.c:27
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
static lzma_ret lzma2_encoder_options_update(void *coder_ptr, const lzma_filter *filter)
static void lzma2_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
static lzma_ret lzma2_encode(void *coder_ptr, lzma_mf *restrict mf, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size)
lzma_ret lzma_lzma_encoder_create(void **coder_ptr, const lzma_allocator *allocator, const lzma_options_lzma *options, lzma_lz_options *lz_options)
Definition: lzma_encoder.c:549
lzma_ret(* code)(void *coder, lzma_mf *restrict mf, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size)
Function to encode from *dict to out[].
Definition: lz_encoder.h:197
void(* end)(void *coder, const lzma_allocator *allocator)
Free allocated resources.
Definition: lz_encoder.h:202
lzma_ret(* options_update)(void *coder, const lzma_filter *filter)
Update the options in the middle of the encoding.
Definition: lz_encoder.h:205
void * coder
Data specific to the LZ-based encoder.
Definition: lz_encoder.h:194
size_t dict_size
Size of the history buffer.
Definition: lz_decoder.h:48
size_t before_size
Definition: lz_encoder.h:132
lzma_options_lzma opt_cur
LZMA options currently in use.
Definition: lzma2_encoder.c:33
bool need_dictionary_reset
Definition: lzma2_decoder.c:50
Options specific to the LZMA1 and LZMA2 filters.
Definition: lzma12.h:185
const uint8_t * preset_dict
Pointer to an initial dictionary.
Definition: lzma12.h:240
uint32_t preset_dict_size
Size of the preset dictionary.
Definition: lzma12.h:254
void * lzma_alloc(size_t size, const lzma_allocator *allocator) lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
Allocates memory.
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218
@ LZMA_MEM_ERROR
Cannot allocate memory.
Definition: base.h:128

References allocator, lzma_lz_options::before_size, lzma_lz_encoder::code, lzma_lz_encoder::coder, lzma_lz_options::dict_size, lzma_lz_encoder::end, lzma_lzma2_coder::lzma, LZMA2_CHUNK_MAX, lzma2_encode(), lzma2_encoder_end(), lzma2_encoder_options_update(), lzma_alloc(), lzma_lzma_encoder_create(), LZMA_MEM_ERROR, LZMA_OK, LZMA_PROG_ERROR, lzma_lzma2_coder::need_dictionary_reset, lzma_lzma2_coder::need_properties, lzma_lzma2_coder::need_state_reset, NULL, lzma_lzma2_coder::opt_cur, options, lzma_lz_encoder::options_update, lzma_options_lzma::preset_dict, lzma_options_lzma::preset_dict_size, and return_if_error.

Referenced by lzma_lzma2_encoder_init().

◆ lzma2_encoder_options_update()

static lzma_ret lzma2_encoder_options_update ( void *  coder_ptr,
const lzma_filter filter 
)
static

Definition at line 277 of file lzma2_encoder.c.

278 {
279  lzma_lzma2_coder *coder = coder_ptr;
280 
281  // New options can be set only when there is no incomplete chunk.
282  // This is the case at the beginning of the raw stream and right
283  // after LZMA_SYNC_FLUSH.
284  if (filter->options == NULL || coder->sequence != SEQ_INIT)
285  return LZMA_PROG_ERROR;
286 
287  // Look if there are new options. At least for now,
288  // only lc/lp/pb can be changed.
289  const lzma_options_lzma *opt = filter->options;
290  if (coder->opt_cur.lc != opt->lc || coder->opt_cur.lp != opt->lp
291  || coder->opt_cur.pb != opt->pb) {
292  // Validate the options.
293  if (opt->lc > LZMA_LCLP_MAX || opt->lp > LZMA_LCLP_MAX
294  || opt->lc + opt->lp > LZMA_LCLP_MAX
295  || opt->pb > LZMA_PB_MAX)
296  return LZMA_OPTIONS_ERROR;
297 
298  // The new options will be used when the encoder starts
299  // a new LZMA2 chunk.
300  coder->opt_cur.lc = opt->lc;
301  coder->opt_cur.lp = opt->lp;
302  coder->opt_cur.pb = opt->pb;
303  coder->need_properties = true;
304  coder->need_state_reset = true;
305  }
306 
307  return LZMA_OK;
308 }
#define LZMA_PB_MAX
Definition: lzma12.h:318
#define LZMA_LCLP_MAX
Definition: lzma12.h:283
uint32_t lp
Number of literal position bits.
Definition: lzma12.h:293
uint32_t lc
Number of literal context bits.
Definition: lzma12.h:281
uint32_t pb
Number of position bits.
Definition: lzma12.h:316
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160

References lzma_options_lzma::lc, lzma_options_lzma::lp, LZMA_LCLP_MAX, LZMA_OK, LZMA_OPTIONS_ERROR, LZMA_PB_MAX, LZMA_PROG_ERROR, lzma_lzma2_coder::need_properties, lzma_lzma2_coder::need_state_reset, NULL, lzma_lzma2_coder::opt_cur, and lzma_options_lzma::pb.

Referenced by lzma2_encoder_init().

◆ lzma2_header_lzma()

static void lzma2_header_lzma ( lzma_lzma2_coder coder)
static

Definition at line 55 of file lzma2_encoder.c.

56 {
57  assert(coder->uncompressed_size > 0);
59  assert(coder->compressed_size > 0);
61 
62  size_t pos;
63 
64  if (coder->need_properties) {
65  pos = 0;
66 
67  if (coder->need_dictionary_reset)
68  coder->buf[pos] = 0x80 + (3 << 5);
69  else
70  coder->buf[pos] = 0x80 + (2 << 5);
71  } else {
72  pos = 1;
73 
74  if (coder->need_state_reset)
75  coder->buf[pos] = 0x80 + (1 << 5);
76  else
77  coder->buf[pos] = 0x80;
78  }
79 
80  // Set the start position for copying.
81  coder->buf_pos = pos;
82 
83  // Uncompressed size
84  size_t size = coder->uncompressed_size - 1;
85  coder->buf[pos++] += size >> 16;
86  coder->buf[pos++] = (size >> 8) & 0xFF;
87  coder->buf[pos++] = size & 0xFF;
88 
89  // Compressed size
90  size = coder->compressed_size - 1;
91  coder->buf[pos++] = size >> 8;
92  coder->buf[pos++] = size & 0xFF;
93 
94  // Properties, if needed
95  if (coder->need_properties)
96  lzma_lzma_lclppb_encode(&coder->opt_cur, coder->buf + pos);
97 
98  coder->need_properties = false;
99  coder->need_state_reset = false;
100  coder->need_dictionary_reset = false;
101 
102  // The copying code uses coder->compressed_size to indicate the end
103  // of coder->buf[], so we need add the maximum size of the header here.
105 
106  return;
107 }
voidpf void uLong size
Definition: ioapi.h:138
bool lzma_lzma_lclppb_encode(const lzma_options_lzma *options, uint8_t *byte)
Encodes lc/lp/pb into one byte. Returns false on success and true on error.
Definition: lzma_encoder.c:645
size_t uncompressed_size
Uncompressed size of LZMA chunk.
Definition: lzma2_decoder.c:38
size_t buf_pos
Read position in buf[].
Definition: lzma2_encoder.c:47
uint8_t buf[LZMA2_HEADER_MAX+LZMA2_CHUNK_MAX]
Buffer to hold the chunk header and LZMA compressed data.
Definition: lzma2_encoder.c:50
size_t compressed_size
Definition: lzma2_decoder.c:42
int pos
Definition: main.c:11

References assert(), lzma_lzma2_coder::buf, lzma_lzma2_coder::buf_pos, lzma_lzma2_coder::compressed_size, LZMA2_CHUNK_MAX, LZMA2_HEADER_MAX, LZMA2_UNCOMPRESSED_MAX, lzma_lzma_lclppb_encode(), lzma_lzma2_coder::need_dictionary_reset, lzma_lzma2_coder::need_properties, lzma_lzma2_coder::need_state_reset, lzma_lzma2_coder::opt_cur, pos, and lzma_lzma2_coder::uncompressed_size.

Referenced by lzma2_encode().

◆ lzma2_header_uncompressed()

static void lzma2_header_uncompressed ( lzma_lzma2_coder coder)
static

Definition at line 111 of file lzma2_encoder.c.

112 {
113  assert(coder->uncompressed_size > 0);
115 
116  // If this is the first chunk, we need to include dictionary
117  // reset indicator.
118  if (coder->need_dictionary_reset)
119  coder->buf[0] = 1;
120  else
121  coder->buf[0] = 2;
122 
123  coder->need_dictionary_reset = false;
124 
125  // "Compressed" size
126  coder->buf[1] = (coder->uncompressed_size - 1) >> 8;
127  coder->buf[2] = (coder->uncompressed_size - 1) & 0xFF;
128 
129  // Set the start position for copying.
130  coder->buf_pos = 0;
131  return;
132 }

References assert(), lzma_lzma2_coder::buf, lzma_lzma2_coder::buf_pos, LZMA2_CHUNK_MAX, lzma_lzma2_coder::need_dictionary_reset, and lzma_lzma2_coder::uncompressed_size.

Referenced by lzma2_encode().

◆ lzma_lzma2_block_size()

uint64_t lzma_lzma2_block_size ( const void *  options)

Definition at line 404 of file lzma2_encoder.c.

405 {
406  const lzma_options_lzma *const opt = options;
407 
408  // Use at least 1 MiB to keep compression ratio better.
409  return my_max((uint64_t)(opt->dict_size) * 3, UINT64_C(1) << 20);
410 }
unsigned long uint64_t
Definition: sftypes.h:28
#define UINT64_C(val)
uint32_t dict_size
Dictionary size in bytes.
Definition: lzma12.h:217
#define my_max(x, y)
Definition: sysdefs.h:186

References lzma_options_lzma::dict_size, my_max, options, and UINT64_C.

◆ lzma_lzma2_encoder_init()

lzma_ret lzma_lzma2_encoder_init ( lzma_next_coder next,
const lzma_allocator allocator,
const lzma_filter_info filters 
)

Definition at line 359 of file lzma2_encoder.c.

361 {
362  return lzma_lz_encoder_init(
364 }
const lzma_filter * filters
Definition: container.h:315
lzma_ret lzma_lz_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters, lzma_ret(*lz_init)(lzma_lz_encoder *lz, const lzma_allocator *allocator, const void *options, lzma_lz_options *lz_options))
Definition: lz_encoder.c:525
static lzma_ret lzma2_encoder_init(lzma_lz_encoder *lz, const lzma_allocator *allocator, const void *options, lzma_lz_options *lz_options)

References allocator, filters, lzma2_encoder_init(), and lzma_lz_encoder_init().

◆ lzma_lzma2_encoder_memusage()

uint64_t lzma_lzma2_encoder_memusage ( const void *  options)

Definition at line 368 of file lzma2_encoder.c.

369 {
370  const uint64_t lzma_mem = lzma_lzma_encoder_memusage(options);
371  if (lzma_mem == UINT64_MAX)
372  return UINT64_MAX;
373 
374  return sizeof(lzma_lzma2_coder) + lzma_mem;
375 }
uint64_t lzma_lzma_encoder_memusage(const void *options)
Definition: lzma_encoder.c:628
#define UINT64_MAX

References lzma_lzma_encoder_memusage(), options, and UINT64_MAX.

◆ lzma_lzma2_props_encode()

lzma_ret lzma_lzma2_props_encode ( const void *  options,
uint8_t out 
)

Definition at line 379 of file lzma2_encoder.c.

380 {
381  const lzma_options_lzma *const opt = options;
383 
384  // Round up to the next 2^n - 1 or 2^n + 2^(n - 1) - 1 depending
385  // on which one is the next:
386  --d;
387  d |= d >> 2;
388  d |= d >> 3;
389  d |= d >> 4;
390  d |= d >> 8;
391  d |= d >> 16;
392 
393  // Get the highest two bits using the proper encoding:
394  if (d == UINT32_MAX)
395  out[0] = 40;
396  else
397  out[0] = get_dist_slot(d + 1) - 24;
398 
399  return LZMA_OK;
400 }
static uint32_t get_dist_slot(uint32_t dist)
Definition: fastpos.h:109
#define LZMA_DICT_SIZE_MIN
Definition: lzma12.h:218
#define d(i)
Definition: sha256.c:44
#define UINT32_MAX

References d, lzma_options_lzma::dict_size, get_dist_slot(), LZMA_DICT_SIZE_MIN, LZMA_OK, my_max, options, out, and UINT32_MAX.