#ifndef LZMA_LZ_DECODER_H
#define LZMA_LZ_DECODER_H
#include "common.h"
#ifdef HAVE_IMMINTRIN_H
# include <immintrin.h>
#endif
#ifndef LZMA_LZ_DECODER_CONFIG
# if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
&& defined(HAVE_IMMINTRIN_H) \
&& (defined(__SSE2__) || defined(_M_X64) \
|| (defined(_M_IX86_FP) && _M_IX86_FP >= 2))
# define LZMA_LZ_DECODER_CONFIG 2
# else
# define LZMA_LZ_DECODER_CONFIG 1
# endif
#endif
#if LZMA_LZ_DECODER_CONFIG >= 2
# define LZ_DICT_EXTRA 32
#else
# define LZ_DICT_EXTRA 0
#endif
#define LZ_DICT_REPEAT_MAX 288
#define LZ_DICT_INIT_POS (2 * LZ_DICT_REPEAT_MAX)
typedef struct {
uint8_t *buf;
size_t pos;
size_t full;
size_t limit;
size_t size;
bool has_wrapped;
bool need_reset;
} lzma_dict;
typedef struct {
size_t dict_size;
const uint8_t *preset_dict;
size_t preset_dict_size;
} lzma_lz_options;
typedef struct {
void *coder;
lzma_ret (*code)(void *coder,
lzma_dict *restrict dict, const uint8_t *restrict in,
size_t *restrict in_pos, size_t in_size);
void (*reset)(void *coder, const void *options);
void (*set_uncompressed)(void *coder, lzma_vli uncompressed_size,
bool allow_eopm);
void (*end)(void *coder, const lzma_allocator *allocator);
} lzma_lz_decoder;
#define LZMA_LZ_DECODER_INIT \
(lzma_lz_decoder){ \
.coder = NULL, \
.code = NULL, \
.reset = NULL, \
.set_uncompressed = NULL, \
.end = NULL, \
}
extern 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));
extern uint64_t lzma_lz_decoder_memusage(size_t dictionary_size);
static inline uint8_t
dict_get(const lzma_dict *const dict, const uint32_t distance)
{
return dict->buf[dict->pos - distance - 1
+ (distance < dict->pos
? 0 : dict->size - LZ_DICT_REPEAT_MAX)];
}
static inline uint8_t
dict_get0(const lzma_dict *const dict)
{
return dict->buf[dict->pos - 1];
}
static inline bool
dict_is_empty(const lzma_dict *const dict)
{
return dict->full == 0;
}
static inline bool
dict_is_distance_valid(const lzma_dict *const dict, const size_t distance)
{
return dict->full > distance;
}
static inline bool
dict_repeat(lzma_dict *restrict dict,
uint32_t distance, uint32_t *restrict len)
{
const size_t dict_avail = dict->limit - dict->pos;
uint32_t left = my_min(dict_avail, *len);
*len -= left;
size_t back = dict->pos - distance - 1;
if (distance >= dict->pos)
back += dict->size - LZ_DICT_REPEAT_MAX;
#if LZMA_LZ_DECODER_CONFIG == 0
while (left-- > 0) {
dict->buf[dict->pos++] = dict->buf[back++];
}
#else
if (distance < left) {
do {
dict->buf[dict->pos++] = dict->buf[back++];
} while (--left > 0);
} else {
# if LZMA_LZ_DECODER_CONFIG == 1
memcpy(dict->buf + dict->pos, dict->buf + back, left);
dict->pos += left;
# elif LZMA_LZ_DECODER_CONFIG == 2
size_t pos = dict->pos;
dict->pos += left;
do {
const __m128i x0 = _mm_loadu_si128(
(__m128i *)(dict->buf + back));
const __m128i x1 = _mm_loadu_si128(
(__m128i *)(dict->buf + back + 16));
back += 32;
_mm_storeu_si128(
(__m128i *)(dict->buf + pos), x0);
_mm_storeu_si128(
(__m128i *)(dict->buf + pos + 16), x1);
pos += 32;
} while (pos < dict->pos);
# else
# error "Invalid LZMA_LZ_DECODER_CONFIG value"
# endif
}
#endif
if (!dict->has_wrapped)
dict->full = dict->pos - LZ_DICT_INIT_POS;
return *len != 0;
}
static inline void
dict_put(lzma_dict *restrict dict, uint8_t byte)
{
dict->buf[dict->pos++] = byte;
if (!dict->has_wrapped)
dict->full = dict->pos - LZ_DICT_INIT_POS;
}
static inline bool
dict_put_safe(lzma_dict *restrict dict, uint8_t byte)
{
if (unlikely(dict->pos == dict->limit))
return true;
dict_put(dict, byte);
return false;
}
static inline void
dict_write(lzma_dict *restrict dict, const uint8_t *restrict in,
size_t *restrict in_pos, size_t in_size,
size_t *restrict left)
{
if (in_size - *in_pos > *left)
in_size = *in_pos + *left;
*left -= lzma_bufcpy(in, in_pos, in_size,
dict->buf, &dict->pos, dict->limit);
if (!dict->has_wrapped)
dict->full = dict->pos - LZ_DICT_INIT_POS;
return;
}
static inline void
dict_reset(lzma_dict *dict)
{
dict->need_reset = true;
return;
}
#endif