1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
///////////////////////////////////////////////////////////////////////////////
//
/// \file lzma_encoder.c
/// \brief LZMA encoder
///
// Authors: Igor Pavlov
// Lasse Collin
//
// This file has been put into the public domain.
// You can do whatever you want with this file.
//
///////////////////////////////////////////////////////////////////////////////
#include "lzma2_encoder.h"
#include "lzma_encoder_private.h"
#include "fastpos.h"
/////////////
// Literal //
/////////////
static inline void
literal_matched(lzma_range_encoder *rc, probability *subcoder,
uint32_t match_byte, uint32_t symbol)
{
uint32_t offset = 0x100;
symbol += UINT32_C(1) << 8;
do {
uint32_t match_bit;
uint32_t subcoder_index;
uint32_t bit;
match_byte <<= 1;
match_bit = match_byte & offset;
subcoder_index = offset + match_bit + (symbol >> 8);
bit = (symbol >> 7) & 1;
rc_bit(rc, &subcoder[subcoder_index], bit);
symbol <<= 1;
offset &= ~(match_byte ^ symbol);
} while (symbol < (UINT32_C(1) << 16));
}
static inline void
literal(lzma_coder *coder, lzma_mf *mf, uint32_t position)
{
// Locate the literal byte to be encoded and the subcoder.
const uint8_t cur_byte = mf->buffer[
mf->read_pos - mf->read_ahead];
probability *subcoder = literal_subcoder(coder->literal,
coder->literal_context_bits, coder->literal_pos_mask,
position, mf->buffer[mf->read_pos - mf->read_ahead - 1]);
if (is_literal_state(coder->state)) {
// Previous LZMA-symbol was a literal. Encode a normal
// literal without a match byte.
rc_bittree(&coder->rc, subcoder, 8, cur_byte);
} else {
// Previous LZMA-symbol was a match. Use the last byte of
// the match as a "match byte". That is, compare the bits
// of the current literal and the match byte.
const uint8_t match_byte = mf->buffer[
mf->read_pos - coder->reps[0] - 1
- mf->read_ahead];
literal_matched(&coder->rc, subcoder, match_byte, cur_byte);
}
update_literal(coder->state);
}
//////////////////
// Match length //
//////////////////
static void
length_update_prices(lzma_length_encoder *lc, const uint32_t pos_state)
{
uint32_t a0, a1, b0, b1;
uint32_t *prices;
uint32_t i;
const uint32_t table_size = lc->table_size;
lc->counters[pos_state] = table_size;
a0 = rc_bit_0_price(lc->choice);
a1 = rc_bit_1_price(lc->choice);
b0 = a1 + rc_bit_0_price(lc->choice2);
b1 = a1 + rc_bit_1_price(lc->choice2);
prices = lc->prices[pos_state];
for (i = 0; i < table_size && i < LEN_LOW_SYMBOLS; ++i)
prices[i] = a0 + rc_bittree_price(lc->low[pos_state],
LEN_LOW_BITS, i);
for (; i < table_size && i < LEN_LOW_SYMBOLS + LEN_MID_SYMBOLS; ++i)
prices[i] = b0 + rc_bittree_price(lc->mid[pos_state],
LEN_MID_BITS, i - LEN_LOW_SYMBOLS);
for (; i < table_size; ++i)
prices[i] = b1 + rc_bittree_price(lc->high, LEN_HIGH_BITS,
i - LEN_LOW_SYMBOLS - LEN_MID_SYMBOLS);
return;
}
static inline void
length(lzma_range_encoder *rc, lzma_length_encoder *lc,
const uint32_t pos_state, uint32_t len, const bool fast_mode)
{
assert(len <= MATCH_LEN_MAX);
len -= MATCH_LEN_MIN;
if (len < LEN_LOW_SYMBOLS) {
rc_bit(rc, &lc->choice, 0);
rc_bittree(rc, lc->low[pos_state], LEN_LOW_BITS, len);
} else {
rc_bit(rc, &lc->choice, 1);
len -= LEN_LOW_SYMBOLS;
if (len < LEN_MID_SYMBOLS) {
rc_bit(rc, &lc->choice2, 0);
rc_bittree(rc, lc->mid[pos_state], LEN_MID_BITS, len);
} else {
rc_bit(rc, &lc->choice2, 1);
len -= LEN_MID_SYMBOLS;
rc_bittree(rc, lc->high, LEN_HIGH_BITS, len);
}
}
// Only getoptimum uses the prices so don't update the table when
// in fast mode.
if (!fast_mode)
if (--lc->counters[pos_state] == 0)
length_update_prices(lc, pos_state);
}
///////////
// Match //
///////////
static inline void
match(lzma_coder *coder, const uint32_t pos_state,
const uint32_t distance, const uint32_t len)
{
uint32_t pos_slot;
uint32_t len_to_pos_state;
update_match(coder->state);
length(&coder->rc, &coder->match_len_encoder, pos_state, len,
coder->fast_mode);
pos_slot = get_pos_slot(distance);
len_to_pos_state = get_len_to_pos_state(len);
rc_bittree(&coder->rc, coder->pos_slot[len_to_pos_state],
POS_SLOT_BITS, pos_slot);
if (pos_slot >= START_POS_MODEL_INDEX) {
const uint32_t footer_bits = (pos_slot >> 1) - 1;
const uint32_t base = (2 | (pos_slot & 1)) << footer_bits;
const uint32_t pos_reduced = distance - base;
if (pos_slot < END_POS_MODEL_INDEX) {
// Careful here: base - pos_slot - 1 can be -1, but
// rc_bittree_reverse starts at probs[1], not probs[0].
rc_bittree_reverse(&coder->rc,
coder->pos_special + base - pos_slot - 1,
footer_bits, pos_reduced);
} else {
rc_direct(&coder->rc, pos_reduced >> ALIGN_BITS,
footer_bits - ALIGN_BITS);
rc_bittree_reverse(
&coder->rc, coder->pos_align,
ALIGN_BITS, pos_reduced & ALIGN_MASK);
++coder->align_price_count;
}
}
coder->reps[3] = coder->reps[2];
coder->reps[2] = coder->reps[1];
coder->reps[1] = coder->reps[0];
coder->reps[0] = distance;
++coder->match_price_count;
}
////////////////////
// Repeated match //
////////////////////
static inline void
rep_match(lzma_coder *coder, const uint32_t pos_state,
const uint32_t rep, const uint32_t len)
{
if (rep == 0) {
rc_bit(&coder->rc, &coder->is_rep0[coder->state], 0);
rc_bit(&coder->rc,
&coder->is_rep0_long[coder->state][pos_state],
len != 1);
} else {
const uint32_t distance = coder->reps[rep];
rc_bit(&coder->rc, &coder->is_rep0[coder->state], 1);
if (rep == 1) {
rc_bit(&coder->rc, &coder->is_rep1[coder->state], 0);
} else {
rc_bit(&coder->rc, &coder->is_rep1[coder->state], 1);
rc_bit(&coder->rc, &coder->is_rep2[coder->state],
rep - 2);
if (rep == 3)
coder->reps[3] = coder->reps[2];
coder->reps[2] = coder->reps[1];
}
coder->reps[1] = coder->reps[0];
coder->reps[0] = distance;
}
if (len == 1) {
update_short_rep(coder->state);
} else {
length(&coder->rc, &coder->rep_len_encoder, pos_state, len,
coder->fast_mode);
update_long_rep(coder->state);
}
}
//////////
// Main //
//////////
static void
encode_symbol(lzma_coder *coder, lzma_mf *mf,
uint32_t back, uint32_t len, uint32_t position)
{
const uint32_t pos_state = position & coder->pos_mask;
if (back == UINT32_MAX) {
// Literal i.e. eight-bit byte
assert(len == 1);
rc_bit(&coder->rc,
&coder->is_match[coder->state][pos_state], 0);
literal(coder, mf, position);
} else {
// Some type of match
rc_bit(&coder->rc,
&coder->is_match[coder->state][pos_state], 1);
if (back < REP_DISTANCES) {
// It's a repeated match i.e. the same distance
// has been used earlier.
rc_bit(&coder->rc, &coder->is_rep[coder->state], 1);
rep_match(coder, pos_state, back, len);
} else {
// Normal match
rc_bit(&coder->rc, &coder->is_rep[coder->state], 0);
match(coder, pos_state, back - REP_DISTANCES, len);
}
}
assert(mf->read_ahead >= len);
mf->read_ahead -= len;
}
static bool
encode_init(lzma_coder *coder, lzma_mf *mf)
{
assert(mf_position(mf) == 0);
if (mf->read_pos == mf->read_limit) {
if (mf->action == LZMA_RUN)
return false; // We cannot do anything.
// We are finishing (we cannot get here when flushing).
assert(mf->write_pos == mf->read_pos);
assert(mf->action == LZMA_FINISH);
} else {
// Do the actual initialization. The first LZMA symbol must
// always be a literal.
mf_skip(mf, 1);
mf->read_ahead = 0;
rc_bit(&coder->rc, &coder->is_match[0][0], 0);
rc_bittree(&coder->rc, coder->literal[0], 8, mf->buffer[0]);
}
// Initialization is done (except if empty file).
coder->is_initialized = true;
return true;
}
static void
encode_eopm(lzma_coder *coder, uint32_t position)
{
const uint32_t pos_state = position & coder->pos_mask;
rc_bit(&coder->rc, &coder->is_match[coder->state][pos_state], 1);
rc_bit(&coder->rc, &coder->is_rep[coder->state], 0);
match(coder, pos_state, UINT32_MAX, MATCH_LEN_MIN);
}
/// Number of bytes that a single encoding loop in lzma_lzma_encode() can
/// consume from the dictionary. This limit comes from lzma_lzma_optimum()
/// and may need to be updated if that function is significantly modified.
#define LOOP_INPUT_MAX (OPTS + 1)
extern lzma_ret
lzma_lzma_encode(lzma_coder *restrict coder, lzma_mf *restrict mf,
uint8_t *restrict out, size_t *restrict out_pos,
size_t out_size, uint32_t limit)
{
uint32_t position;
// Initialize the stream if no data has been encoded yet.
if (!coder->is_initialized && !encode_init(coder, mf))
return LZMA_OK;
// Get the lowest bits of the uncompressed offset from the LZ layer.
position = mf_position(mf);
while (true) {
uint32_t len;
uint32_t back;
// Encode pending bits, if any. Calling this before encoding
// the next symbol is needed only with plain LZMA, since
// LZMA2 always provides big enough buffer to flush
// everything out from the range encoder. For the same reason,
// rc_encode() never returns true when this function is used
// as part of LZMA2 encoder.
if (rc_encode(&coder->rc, out, out_pos, out_size)) {
assert(limit == UINT32_MAX);
return LZMA_OK;
}
// With LZMA2 we need to take care that compressed size of
// a chunk doesn't get too big.
// FIXME? Check if this could be improved.
if (limit != UINT32_MAX
&& (mf->read_pos - mf->read_ahead >= limit
|| *out_pos + rc_pending(&coder->rc)
>= LZMA2_CHUNK_MAX
- LOOP_INPUT_MAX))
break;
// Check that there is some input to process.
if (mf->read_pos >= mf->read_limit) {
if (mf->action == LZMA_RUN)
return LZMA_OK;
if (mf->read_ahead == 0)
break;
}
// Get optimal match (repeat position and length).
// Value ranges for pos:
// - [0, REP_DISTANCES): repeated match
// - [REP_DISTANCES, UINT32_MAX):
// match at (pos - REP_DISTANCES)
// - UINT32_MAX: not a match but a literal
// Value ranges for len:
// - [MATCH_LEN_MIN, MATCH_LEN_MAX]
if (coder->fast_mode)
lzma_lzma_optimum_fast(coder, mf, &back, &len);
else
lzma_lzma_optimum_normal(
coder, mf, &back, &len, position);
encode_symbol(coder, mf, back, len, position);
position += len;
}
if (!coder->is_flushed) {
coder->is_flushed = true;
// We don't support encoding plain LZMA streams without EOPM,
// and LZMA2 doesn't use EOPM at LZMA level.
if (limit == UINT32_MAX)
encode_eopm(coder, position);
// Flush the remaining bytes from the range encoder.
rc_flush(&coder->rc);
// Copy the remaining bytes to the output buffer. If there
// isn't enough output space, we will copy out the remaining
// bytes on the next call to this function by using
// the rc_encode() call in the encoding loop above.
if (rc_encode(&coder->rc, out, out_pos, out_size)) {
assert(limit == UINT32_MAX);
return LZMA_OK;
}
}
// Make it ready for the next LZMA2 chunk.
coder->is_flushed = false;
return LZMA_STREAM_END;
}
static lzma_ret
lzma_encode(lzma_coder *restrict coder, lzma_mf *restrict mf,
uint8_t *restrict out, size_t *restrict out_pos,
size_t out_size)
{
// Plain LZMA has no support for sync-flushing.
if (unlikely(mf->action == LZMA_SYNC_FLUSH))
return LZMA_OPTIONS_ERROR;
return lzma_lzma_encode(coder, mf, out, out_pos, out_size, UINT32_MAX);
}
////////////////////
// Initialization //
////////////////////
static bool
is_options_valid(const lzma_options_lzma *options)
{
// Validate some of the options. LZ encoder validates nice_len too
// but we need a valid value here earlier.
return is_lclppb_valid(options)
&& options->nice_len >= MATCH_LEN_MIN
&& options->nice_len <= MATCH_LEN_MAX
&& (options->mode == LZMA_MODE_FAST
|| options->mode == LZMA_MODE_NORMAL);
}
static void
set_lz_options(lzma_lz_options *lz_options, const lzma_options_lzma *options)
{
// LZ encoder initialization does the validation for these so we
// don't need to validate here.
lz_options->before_size = OPTS;
lz_options->dict_size = options->dict_size;
lz_options->after_size = LOOP_INPUT_MAX;
lz_options->match_len_max = MATCH_LEN_MAX;
lz_options->nice_len = options->nice_len;
lz_options->match_finder = options->mf;
lz_options->depth = options->depth;
lz_options->preset_dict = options->preset_dict;
lz_options->preset_dict_size = options->preset_dict_size;
return;
}
static void
length_encoder_reset(lzma_length_encoder *lencoder,
const uint32_t num_pos_states, const bool fast_mode)
{
size_t pos_state;
bit_reset(lencoder->choice);
bit_reset(lencoder->choice2);
for (pos_state = 0; pos_state < num_pos_states; ++pos_state) {
bittree_reset(lencoder->low[pos_state], LEN_LOW_BITS);
bittree_reset(lencoder->mid[pos_state], LEN_MID_BITS);
}
bittree_reset(lencoder->high, LEN_HIGH_BITS);
if (!fast_mode)
for (pos_state = 0; pos_state < num_pos_states;
++pos_state)
length_update_prices(lencoder, pos_state);
return;
}
extern lzma_ret
lzma_lzma_encoder_reset(lzma_coder *coder, const lzma_options_lzma *options)
{
size_t i, j;
if (!is_options_valid(options))
return LZMA_OPTIONS_ERROR;
coder->pos_mask = (1U << options->pb) - 1;
coder->literal_context_bits = options->lc;
coder->literal_pos_mask = (1U << options->lp) - 1;
// Range coder
rc_reset(&coder->rc);
// State
coder->state = STATE_LIT_LIT;
for (i = 0; i < REP_DISTANCES; ++i)
coder->reps[i] = 0;
literal_init(coder->literal, options->lc, options->lp);
// Bit encoders
for (i = 0; i < STATES; ++i) {
for (j = 0; j <= coder->pos_mask; ++j) {
bit_reset(coder->is_match[i][j]);
bit_reset(coder->is_rep0_long[i][j]);
}
bit_reset(coder->is_rep[i]);
bit_reset(coder->is_rep0[i]);
bit_reset(coder->is_rep1[i]);
bit_reset(coder->is_rep2[i]);
}
for (i = 0; i < FULL_DISTANCES - END_POS_MODEL_INDEX; ++i)
bit_reset(coder->pos_special[i]);
// Bit tree encoders
for (i = 0; i < LEN_TO_POS_STATES; ++i)
bittree_reset(coder->pos_slot[i], POS_SLOT_BITS);
bittree_reset(coder->pos_align, ALIGN_BITS);
// Length encoders
length_encoder_reset(&coder->match_len_encoder,
1U << options->pb, coder->fast_mode);
length_encoder_reset(&coder->rep_len_encoder,
1U << options->pb, coder->fast_mode);
// Price counts are incremented every time appropriate probabilities
// are changed. price counts are set to zero when the price tables
// are updated, which is done when the appropriate price counts have
// big enough value, and lzma_mf.read_ahead == 0 which happens at
// least every OPTS (a few thousand) possible price count increments.
//
// By resetting price counts to UINT32_MAX / 2, we make sure that the
// price tables will be initialized before they will be used (since
// the value is definitely big enough), and that it is OK to increment
// price counts without risk of integer overflow (since UINT32_MAX / 2
// is small enough). The current code doesn't increment price counts
// before initializing price tables, but it maybe done in future if
// we add support for saving the state between LZMA2 chunks.
coder->match_price_count = UINT32_MAX / 2;
coder->align_price_count = UINT32_MAX / 2;
coder->opts_end_index = 0;
coder->opts_current_index = 0;
return LZMA_OK;
}
extern lzma_ret
lzma_lzma_encoder_create(lzma_coder **coder_ptr, lzma_allocator *allocator,
const lzma_options_lzma *options, lzma_lz_options *lz_options)
{
lzma_coder *coder;
uint32_t log_size = 0;
// Allocate lzma_coder if it wasn't already allocated.
if (*coder_ptr == NULL) {
*coder_ptr = lzma_alloc(sizeof(lzma_coder), allocator);
if (*coder_ptr == NULL)
return LZMA_MEM_ERROR;
}
coder = *coder_ptr;
// Set compression mode. We haven't validates the options yet,
// but it's OK here, since nothing bad happens with invalid
// options in the code below, and they will get rejected by
// lzma_lzma_encoder_reset() call at the end of this function.
switch (options->mode) {
case LZMA_MODE_FAST:
coder->fast_mode = true;
break;
case LZMA_MODE_NORMAL: {
coder->fast_mode = false;
// Set dist_table_size.
// Round the dictionary size up to next 2^n.
while ((UINT32_C(1) << log_size) < options->dict_size)
++log_size;
coder->dist_table_size = log_size * 2;
// Length encoders' price table size
coder->match_len_encoder.table_size
= options->nice_len + 1 - MATCH_LEN_MIN;
coder->rep_len_encoder.table_size
= options->nice_len + 1 - MATCH_LEN_MIN;
break;
}
default:
return LZMA_OPTIONS_ERROR;
}
// We don't need to write the first byte as literal if there is
// a non-empty preset dictionary. encode_init() wouldn't even work
// if there is a non-empty preset dictionary, because encode_init()
// assumes that position is zero and previous byte is also zero.
coder->is_initialized = options->preset_dict != NULL
&& options->preset_dict_size > 0;
coder->is_flushed = false;
set_lz_options(lz_options, options);
return lzma_lzma_encoder_reset(coder, options);
}
static lzma_ret
lzma_encoder_init(lzma_lz_encoder *lz, lzma_allocator *allocator,
const void *options, lzma_lz_options *lz_options)
{
lz->code = &lzma_encode;
return lzma_lzma_encoder_create(
&lz->coder, allocator, options, lz_options);
}
extern lzma_ret
lzma_lzma_encoder_init(lzma_next_coder *next, lzma_allocator *allocator,
const lzma_filter_info *filters)
{
return lzma_lz_encoder_init(
next, allocator, filters, &lzma_encoder_init);
}
extern uint64_t
lzma_lzma_encoder_memusage(const void *options)
{
lzma_lz_options lz_options;
uint64_t lz_memusage;
if (!is_options_valid(options))
return UINT64_MAX;
set_lz_options(&lz_options, options);
lz_memusage = lzma_lz_encoder_memusage(&lz_options);
if (lz_memusage == UINT64_MAX)
return UINT64_MAX;
return (uint64_t)(sizeof(lzma_coder)) + lz_memusage;
}
extern bool
lzma_lzma_lclppb_encode(const lzma_options_lzma *options, uint8_t *byte)
{
if (!is_lclppb_valid(options))
return true;
*byte = (options->pb * 5 + options->lp) * 9 + options->lc;
assert(*byte <= (4 * 5 + 4) * 9 + 8);
return false;
}
#ifdef HAVE_ENCODER_LZMA1
extern lzma_ret
lzma_lzma_props_encode(const void *options, uint8_t *out)
{
const lzma_options_lzma *const opt = options;
if (lzma_lzma_lclppb_encode(opt, out))
return LZMA_PROG_ERROR;
unaligned_write32le(out + 1, opt->dict_size);
return LZMA_OK;
}
#endif
extern LZMA_API(lzma_bool)
lzma_mode_is_supported(lzma_mode mode)
{
return mode == LZMA_MODE_FAST || mode == LZMA_MODE_NORMAL;
}
|