1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file lzma_encoder_private.h
4 /// \brief Private definitions for LZMA encoder
6 // Copyright (C) 1999-2006 Igor Pavlov
7 // Copyright (C) 2007 Lasse Collin
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 ///////////////////////////////////////////////////////////////////////////////
21 #ifndef LZMA_LZMA_ENCODER_PRIVATE_H
22 #define LZMA_LZMA_ENCODER_PRIVATE_H
24 #include "lzma_encoder.h"
25 #include "lzma_common.h"
26 #include "lz_encoder.h"
27 #include "range_encoder.h"
29 // We need space for about two encoding loops, because there is no check
30 // for available buffer space before end of payload marker gets written.
31 // 2*26 bytes should be enough for this... but Lasse isn't very sure about
32 // the exact value. 64 bytes certainly is enough. :-)
33 #if LZMA_LZ_TEMP_SIZE < 64
34 # error LZMA_LZ_TEMP_SIZE is too small.
38 #define move_pos(num) \
40 assert((int32_t)(num) >= 0); \
42 coder->additional_offset += num; \
43 coder->lz.skip(&coder->lz, num); \
51 probability low[POS_STATES_MAX][LEN_LOW_SYMBOLS];
52 probability mid[POS_STATES_MAX][LEN_MID_SYMBOLS];
53 probability high[LEN_HIGH_SYMBOLS];
55 uint32_t prices[POS_STATES_MAX][LEN_SYMBOLS];
57 uint32_t counters[POS_STATES_MAX];
59 } lzma_length_encoder;
63 lzma_lzma_state state;
72 uint32_t pos_prev; // pos_next;
81 // Next coder in the chain
84 // In window and match finder
88 lzma_range_encoder rc;
91 lzma_lzma_state state;
92 uint8_t previous_byte;
93 uint32_t rep_distances[REP_DISTANCES];
96 uint32_t match_distances[MATCH_MAX_LEN * 2 + 2 + 1];
97 uint32_t num_distance_pairs;
98 uint32_t additional_offset;
99 uint32_t now_pos; // Lowest 32 bits are enough here.
100 bool best_compression; ///< True when LZMA_MODE_BEST is used
104 lzma_literal_coder *literal_coder;
107 probability is_match[STATES][POS_STATES_MAX];
108 probability is_rep[STATES];
109 probability is_rep0[STATES];
110 probability is_rep1[STATES];
111 probability is_rep2[STATES];
112 probability is_rep0_long[STATES][POS_STATES_MAX];
113 probability pos_encoders[FULL_DISTANCES - END_POS_MODEL_INDEX];
116 probability pos_slot_encoder[LEN_TO_POS_STATES][1 << POS_SLOT_BITS];
117 probability pos_align_encoder[1 << ALIGN_BITS];
120 lzma_length_encoder match_len_encoder;
121 lzma_length_encoder rep_len_encoder;
124 lzma_optimal optimum[OPTS];
125 uint32_t optimum_end_index;
126 uint32_t optimum_current_index;
127 uint32_t longest_match_length;
128 bool longest_match_was_found;
131 uint32_t pos_slot_prices[LEN_TO_POS_STATES][DIST_TABLE_SIZE_MAX];
132 uint32_t distances_prices[LEN_TO_POS_STATES][FULL_DISTANCES];
133 uint32_t align_prices[ALIGN_TABLE_SIZE];
134 uint32_t align_price_count;
135 uint32_t dist_table_size;
136 uint32_t match_price_count;
138 // LZMA specific settings
139 uint32_t dictionary_size; ///< Size in bytes
141 uint32_t pos_state_bits;
142 uint32_t pos_mask; ///< (1 << pos_state_bits) - 1
146 extern void lzma_length_encoder_update_table(lzma_length_encoder *lencoder,
147 const uint32_t pos_state);
149 extern bool lzma_lzma_encode(lzma_coder *coder, uint8_t *restrict out,
150 size_t *restrict out_pos, size_t out_size);
152 extern void lzma_get_optimum(lzma_coder *restrict coder,
153 uint32_t *restrict back_res, uint32_t *restrict len_res);
155 extern void lzma_get_optimum_fast(lzma_coder *restrict coder,
156 uint32_t *restrict back_res, uint32_t *restrict len_res);
159 // NOTE: Don't add 'restrict'.
161 lzma_read_match_distances(lzma_coder *coder,
162 uint32_t *len_res, uint32_t *num_distance_pairs)
166 coder->lz.get_matches(&coder->lz, coder->match_distances);
168 *num_distance_pairs = coder->match_distances[0];
170 if (*num_distance_pairs > 0) {
171 *len_res = coder->match_distances[*num_distance_pairs - 1];
172 assert(*len_res <= MATCH_MAX_LEN);
174 if (*len_res == coder->fast_bytes) {
175 uint32_t offset = *len_res - 1;
176 const uint32_t distance = coder->match_distances[
177 *num_distance_pairs] + 1;
178 uint32_t limit = MATCH_MAX_LEN - *len_res;
180 assert(offset + limit < coder->lz.keep_size_after);
181 assert(coder->lz.read_pos <= coder->lz.write_pos);
183 // If we are close to end of the stream, we may need
184 // to limit the length of the match.
185 if (coder->lz.write_pos - coder->lz.read_pos
187 limit = coder->lz.write_pos
188 - (coder->lz.read_pos + offset);
190 offset += coder->lz.read_pos;
192 while (i < limit && coder->lz.buffer[offset + i]
194 offset + i - distance])
201 ++coder->additional_offset;