Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
lz_decoder.h
Go to the documentation of this file.
1// SPDX-License-Identifier: 0BSD
2
4//
8// Authors: Igor Pavlov
9// Lasse Collin
10//
12
13#ifndef LZMA_LZ_DECODER_H
14#define LZMA_LZ_DECODER_H
15
16#include "common.h"
17
18
36#define LZ_DICT_REPEAT_MAX 288
37
38
39typedef struct {
41 uint8_t *buf;
42
45 size_t pos;
46
50 size_t full;
51
53 size_t limit;
54
59 size_t size;
60
64
66 bool need_reset;
67
68} lzma_dict;
69
70
71typedef struct {
72 size_t dict_size;
73 const uint8_t *preset_dict;
74 size_t preset_dict_size;
76
77
78typedef struct {
80 void *coder;
81
83 lzma_ret (*code)(void *coder,
84 lzma_dict *restrict dict, const uint8_t *restrict in,
85 size_t *restrict in_pos, size_t in_size);
86
87 void (*reset)(void *coder, const void *options);
88
91 void (*set_uncompressed)(void *coder, lzma_vli uncompressed_size,
92 bool allow_eopm);
93
95 void (*end)(void *coder, const lzma_allocator *allocator);
96
98
99
100#define LZMA_LZ_DECODER_INIT \
101 (lzma_lz_decoder){ \
102 .coder = NULL, \
103 .code = NULL, \
104 .reset = NULL, \
105 .set_uncompressed = NULL, \
106 .end = NULL, \
107 }
108
109
113 lzma_ret (*lz_init)(lzma_lz_decoder *lz,
115 lzma_vli id, const void *options,
116 lzma_lz_options *lz_options));
117
118extern uint64_t lzma_lz_decoder_memusage(size_t dictionary_size);
119
120
122// Inline functions //
124
126static inline uint8_t
127dict_get(const lzma_dict *const dict, const uint32_t distance)
128{
129 return dict->buf[dict->pos - distance - 1
130 + (distance < dict->pos
131 ? 0 : dict->size - LZ_DICT_REPEAT_MAX)];
132}
133
134
136static inline uint8_t
137dict_get0(const lzma_dict *const dict)
138{
139 return dict->buf[dict->pos - 1];
140}
141
142
144static inline bool
145dict_is_empty(const lzma_dict *const dict)
146{
147 return dict->full == 0;
148}
149
150
152static inline bool
153dict_is_distance_valid(const lzma_dict *const dict, const size_t distance)
154{
155 return dict->full > distance;
156}
157
158
160static inline bool
161dict_repeat(lzma_dict *dict, uint32_t distance, uint32_t *len)
162{
163 // Don't write past the end of the dictionary.
164 const size_t dict_avail = dict->limit - dict->pos;
165 uint32_t left = my_min(dict_avail, *len);
166 *len -= left;
167
168 size_t back = dict->pos - distance - 1;
169 if (distance >= dict->pos)
170 back += dict->size - LZ_DICT_REPEAT_MAX;
171
172 // Repeat a block of data from the history. Because memcpy() is faster
173 // than copying byte by byte in a loop, the copying process gets split
174 // into two cases.
175 if (distance < left) {
176 // Source and target areas overlap, thus we can't use
177 // memcpy() nor even memmove() safely.
178 do {
179 dict->buf[dict->pos++] = dict->buf[back++];
180 } while (--left > 0);
181 } else {
182 memcpy(dict->buf + dict->pos, dict->buf + back, left);
183 dict->pos += left;
184 }
185
186 // Update how full the dictionary is.
187 if (!dict->has_wrapped)
188 dict->full = dict->pos - 2 * LZ_DICT_REPEAT_MAX;
189
190 return *len != 0;
191}
192
193
194static inline void
195dict_put(lzma_dict *dict, uint8_t byte)
196{
197 dict->buf[dict->pos++] = byte;
198
199 if (!dict->has_wrapped)
200 dict->full = dict->pos - 2 * LZ_DICT_REPEAT_MAX;
201}
202
203
206static inline bool
207dict_put_safe(lzma_dict *dict, uint8_t byte)
208{
209 if (unlikely(dict->pos == dict->limit))
210 return true;
211
212 dict_put(dict, byte);
213 return false;
214}
215
216
218static inline void
219dict_write(lzma_dict *restrict dict, const uint8_t *restrict in,
220 size_t *restrict in_pos, size_t in_size,
221 size_t *restrict left)
222{
223 // NOTE: If we are being given more data than the size of the
224 // dictionary, it could be possible to optimize the LZ decoder
225 // so that not everything needs to go through the dictionary.
226 // This shouldn't be very common thing in practice though, and
227 // the slowdown of one extra memcpy() isn't bad compared to how
228 // much time it would have taken if the data were compressed.
229
230 if (in_size - *in_pos > *left)
231 in_size = *in_pos + *left;
232
234 dict->buf, &dict->pos, dict->limit);
235
236 if (!dict->has_wrapped)
237 dict->full = dict->pos - 2 * LZ_DICT_REPEAT_MAX;
238
239 return;
240}
241
242
243static inline void
244dict_reset(lzma_dict *dict)
245{
246 dict->need_reset = true;
247 return;
248}
249
250#endif
#define unlikely(expr)
Definition lz4.c:180
char buf[N_BUF]
Definition spewG.c:36
Definition inftrees.h:24
Custom functions for memory handling.
Definition base.h:372
Definition lz_decoder.h:20
bool has_wrapped
Definition lz_decoder.h:63
Definition common.h:177
Definition lz_decoder.h:54
Definition lz_decoder.h:47
Hold data and function pointers of the next filter in the chain.
Definition common.h:195
size_t size
Definition platform.h:559
size_t * left
Definition util.h:104
lzma_ret
Return values used by several functions in liblzma.
Definition base.h:57
const lzma_allocator const uint8_t size_t * in_pos
Definition block.h:579
const lzma_allocator const uint8_t size_t in_size
Definition block.h:527
const lzma_allocator const uint8_t * in
Definition block.h:527
const lzma_allocator * allocator
Definition block.h:377
const lzma_filter * filters
Definition container.h:315
uint64_t lzma_vli
Variable-length integer type.
Definition vli.h:63
#define my_min(x, y)
Definition sysdefs.h:185
uint64_t uint64_t lzma_bool uint32_t dict_size
Definition container.h:904
const lzma_options_lzma * options
Definition container.h:545
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
static uint32_t const uint8_t uint32_t len
Definition memcmplen.h:44
static uint32_t const uint8_t uint32_t uint32_t limit
Definition memcmplen.h:45
uint64_t lzma_lz_decoder_memusage(size_t dictionary_size)
Definition lz_decoder.c:301
lzma_ret lzma_lz_decoder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters, lzma_ret(*lz_init)(lzma_lz_decoder *lz, const lzma_allocator *allocator, lzma_vli id, const void *options, lzma_lz_options *lz_options))
Definition lz_decoder.c:212
uint64_t uncompressed_size
Definition list.c:209
#define LZ_DICT_REPEAT_MAX
Definition lz_decoder.h:36
const void * dict
Definition zbuff.h:76