1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief LZ out window
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_LZ_DECODER_H
22 #define LZMA_LZ_DECODER_H
28 /// Pointer to the dictionary buffer. It can be an allocated buffer
29 /// internal to liblzma, or it can a be a buffer given by the
30 /// application when in single-call mode (not implemented yet).
33 /// Write position in dictionary. The next byte will be written to
37 /// Indicates how full the dictionary is. This is used by
38 /// dict_is_distance_valid() to detect corrupt files that would
39 /// read beyond the beginning of the dictionary.
45 /// Size of the dictionary
48 /// True when dictionary should be reset before decoding more data.
56 const uint8_t *preset_dict;
57 size_t preset_dict_size;
62 /// Data specific to the LZ-based decoder
65 /// Function to decode from in[] to *dict
66 lzma_ret (*code)(lzma_coder *restrict coder,
67 lzma_dict *restrict dict, const uint8_t *restrict in,
68 size_t *restrict in_pos, size_t in_size);
70 void (*reset)(lzma_coder *coder, const void *options);
72 /// Set the uncompressed size
73 void (*set_uncompressed)(lzma_coder *coder,
74 lzma_vli uncompressed_size);
76 /// Free allocated resources
77 void (*end)(lzma_coder *coder, lzma_allocator *allocator);
82 #define LZMA_LZ_DECODER_INIT \
87 .set_uncompressed = NULL, \
92 extern lzma_ret lzma_lz_decoder_init(lzma_next_coder *next,
93 lzma_allocator *allocator, const lzma_filter_info *filters,
94 lzma_ret (*lz_init)(lzma_lz_decoder *lz,
95 lzma_allocator *allocator, const void *options,
96 lzma_lz_options *lz_options));
98 extern uint64_t lzma_lz_decoder_memusage(size_t dictionary_size);
100 extern void lzma_lz_decoder_uncompressed(
101 lzma_coder *coder, lzma_vli uncompressed_size);
104 //////////////////////
105 // Inline functions //
106 //////////////////////
108 /// Get a byte from the history buffer.
109 static inline uint8_t
110 dict_get(const lzma_dict *const dict, const uint32_t distance)
112 return dict->buf[dict->pos - distance - 1
113 + (distance < dict->pos ? 0 : dict->size)];
117 /// Test if dictionary is empty.
119 dict_is_empty(const lzma_dict *const dict)
121 return dict->full == 0;
125 /// Validate the match distance
127 dict_is_distance_valid(const lzma_dict *const dict, const size_t distance)
129 return dict->full >= distance;
133 /// Repeat *len bytes at distance.
135 dict_repeat(lzma_dict *dict, uint32_t distance, uint32_t *len)
137 // Don't write past the end of the dictionary.
138 const size_t dict_avail = dict->limit - dict->pos;
139 uint32_t left = MIN(dict_avail, *len);
142 // Repeat a block of data from the history. Because memcpy() is faster
143 // than copying byte by byte in a loop, the copying process gets split
145 if (distance < left) {
146 // Source and target areas overlap, thus we can't use
147 // memcpy() nor even memmove() safely.
149 dict->buf[dict->pos] = dict_get(dict, distance);
151 } while (--left > 0);
153 } else if (distance < dict->pos) {
154 // The easiest and fastest case
155 memcpy(dict->buf + dict->pos,
156 dict->buf + dict->pos - distance - 1,
161 // The bigger the dictionary, the more rare this
162 // case occurs. We need to "wrap" the dict, thus
163 // we might need two memcpy() to copy all the data.
164 assert(dict->full == dict->size);
165 const uint32_t copy_pos
166 = dict->pos - distance - 1 + dict->size;
167 uint32_t copy_size = dict->size - copy_pos;
169 if (copy_size < left) {
170 memmove(dict->buf + dict->pos, dict->buf + copy_pos,
172 dict->pos += copy_size;
173 copy_size = left - copy_size;
174 memcpy(dict->buf + dict->pos, dict->buf, copy_size);
175 dict->pos += copy_size;
177 memmove(dict->buf + dict->pos, dict->buf + copy_pos,
183 // Update how full the dictionary is.
184 if (dict->full < dict->pos)
185 dict->full = dict->pos;
187 return unlikely(*len != 0);
191 /// Puts one byte into the dictionary. Returns true if the dictionary was
192 /// already full and the byte couldn't be added.
194 dict_put(lzma_dict *dict, uint8_t byte)
196 if (unlikely(dict->pos == dict->limit))
199 dict->buf[dict->pos++] = byte;
201 if (dict->pos > dict->full)
202 dict->full = dict->pos;
208 /// Copies arbitrary amount of data into the dictionary.
210 dict_write(lzma_dict *restrict dict, const uint8_t *restrict in,
211 size_t *restrict in_pos, size_t in_size,
212 size_t *restrict left)
214 // NOTE: If we are being given more data than the size of the
215 // dictionary, it could be possible to optimize the LZ decoder
216 // so that not everything needs to go through the dictionary.
217 // This shouldn't be very common thing in practice though, and
218 // the slowdown of one extra memcpy() isn't bad compared to how
219 // much time it would have taken if the data were compressed.
221 if (in_size - *in_pos > *left)
222 in_size = *in_pos + *left;
224 *left -= lzma_bufcpy(in, in_pos, in_size,
225 dict->buf, &dict->pos, dict->limit);
227 if (dict->pos > dict->full)
228 dict->full = dict->pos;
235 dict_reset(lzma_dict *dict)
237 dict->need_reset = true;