| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#ifndef LZMA_LZ_DECODER_H |
| 14 |
#define LZMA_LZ_DECODER_H |
| 15 |
|
| 16 |
#include "common.h" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#define LZ_DICT_REPEAT_MAX 288 |
| 37 |
|
| 38 |
|
| 39 |
typedef struct { |
| 40 |
|
| 41 |
uint8_t *buf; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
size_t pos; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
size_t full; |
| 51 |
|
| 52 |
|
| 53 |
size_t limit; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
size_t size; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
bool has_wrapped; |
| 64 |
|
| 65 |
|
| 66 |
bool need_reset; |
| 67 |
|
| 68 |
} lzma_dict; |
| 69 |
|
| 70 |
|
| 71 |
typedef struct { |
| 72 |
size_t dict_size; |
| 73 |
const uint8_t *preset_dict; |
| 74 |
size_t preset_dict_size; |
| 75 |
} lzma_lz_options; |
| 76 |
|
| 77 |
|
| 78 |
typedef struct { |
| 79 |
|
| 80 |
void *coder; |
| 81 |
|
| 82 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
void (*set_uncompressed)(void *coder, lzma_vli uncompressed_size, |
| 92 |
bool allow_eopm); |
| 93 |
|
| 94 |
|
| 95 |
void (*end)(void *coder, const lzma_allocator *allocator); |
| 96 |
|
| 97 |
} lzma_lz_decoder; |
| 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 |
|
| 110 |
extern lzma_ret lzma_lz_decoder_init(lzma_next_coder *next, |
| 111 |
const lzma_allocator *allocator, |
| 112 |
const lzma_filter_info *filters, |
| 113 |
lzma_ret (*lz_init)(lzma_lz_decoder *lz, |
| 114 |
const lzma_allocator *allocator, |
| 115 |
lzma_vli id, const void *options, |
| 116 |
lzma_lz_options *lz_options)); |
| 117 |
|
| 118 |
extern uint64_t lzma_lz_decoder_memusage(size_t dictionary_size); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
static inline uint8_t |
| 127 |
dict_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 |
|
| 135 |
|
| 136 |
static inline uint8_t |
| 137 |
dict_get0(const lzma_dict *const dict) |
| 138 |
{ |
| 139 |
return dict->buf[dict->pos - 1]; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
static inline bool |
| 145 |
dict_is_empty(const lzma_dict *const dict) |
| 146 |
{ |
| 147 |
return dict->full == 0; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
static inline bool |
| 153 |
dict_is_distance_valid(const lzma_dict *const dict, const size_t distance) |
| 154 |
{ |
| 155 |
return dict->full > distance; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
static inline bool |
| 161 |
dict_repeat(lzma_dict *dict, uint32_t distance, uint32_t *len) |
| 162 |
{ |
| 163 |
|
| 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 |
|
| 173 |
|
| 174 |
|
| 175 |
if (distance < left) { |
| 176 |
|
| 177 |
|
| 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 |
|
| 187 |
if (!dict->has_wrapped) |
| 188 |
dict->full = dict->pos - 2 * LZ_DICT_REPEAT_MAX; |
| 189 |
|
| 190 |
return *len != 0; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
static inline void |
| 195 |
dict_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 |
|
| 204 |
|
| 205 |
|
| 206 |
static inline bool |
| 207 |
dict_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 |
|
| 217 |
|
| 218 |
static inline void |
| 219 |
dict_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 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
if (in_size - *in_pos > *left) |
| 231 |
in_size = *in_pos + *left; |
| 232 |
|
| 233 |
*left -= lzma_bufcpy(in, in_pos, in_size, |
| 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 |
|
| 243 |
static inline void |
| 244 |
dict_reset(lzma_dict *dict) |
| 245 |
{ |
| 246 |
dict->need_reset = true; |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
#endif |