#ifndef LZMA_COMMON_H
#define LZMA_COMMON_H
#include "sysdefs.h"
#include "mythread.h"
#include "tuklib_integer.h"
#if defined(_WIN32) || defined(__CYGWIN__)
# ifdef DLL_EXPORT
# define LZMA_API_EXPORT __declspec(dllexport)
# else
# define LZMA_API_EXPORT
# endif
# define lzma_attr_visibility_hidden
#elif HAVE_VISIBILITY
# define LZMA_API_EXPORT __attribute__((__visibility__("default")))
# define lzma_attr_visibility_hidden \
__attribute__((__visibility__("hidden")))
#else
# define LZMA_API_EXPORT
# define lzma_attr_visibility_hidden
#endif
#define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL
#include "lzma.h"
#ifdef __has_attribute
# define lzma_has_attribute(attr) __has_attribute(attr)
#else
# define lzma_has_attribute(attr) 0
#endif
#if defined(HAVE_SYMBOL_VERSIONS_LINUX) \
&& (HAVE_SYMBOL_VERSIONS_LINUX == 2 && !defined(PIC))
# undef HAVE_SYMBOL_VERSIONS_LINUX
#endif
#ifdef HAVE_SYMBOL_VERSIONS_LINUX
# if lzma_has_attribute(__symver__)
# define LZMA_SYMVER_API(extnamever, type, intname) \
extern __attribute__((__symver__(extnamever))) \
LZMA_API(type) intname
# else
# define LZMA_SYMVER_API(extnamever, type, intname) \
__asm__(".symver " #intname "," extnamever); \
extern LZMA_API(type) intname
# endif
#endif
#if defined(_MSC_VER)
# define lzma_always_inline __forceinline
#elif defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER) \
|| lzma_has_attribute(__always_inline__)
# define lzma_always_inline inline __attribute__((__always_inline__))
#else
# define lzma_always_inline inline
#endif
#ifdef __GNUC__
# define likely(expr) __builtin_expect(expr, true)
# define unlikely(expr) __builtin_expect(expr, false)
#else
# define likely(expr) (expr)
# define unlikely(expr) (expr)
#endif
#define LZMA_BUFFER_SIZE 4096
#define LZMA_THREADS_MAX 16384
#define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15)
#define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62)
#define LZMA_SUPPORTED_FLAGS \
( LZMA_TELL_NO_CHECK \
| LZMA_TELL_UNSUPPORTED_CHECK \
| LZMA_TELL_ANY_CHECK \
| LZMA_IGNORE_CHECK \
| LZMA_CONCATENATED \
| LZMA_FAIL_FAST )
#define LZMA_ACTION_MAX ((unsigned int)(LZMA_FULL_BARRIER))
#define LZMA_TIMED_OUT LZMA_RET_INTERNAL1
#define LZMA_INDEX_DETECTED LZMA_RET_INTERNAL2
typedef struct lzma_next_coder_s lzma_next_coder;
typedef struct lzma_filter_info_s lzma_filter_info;
typedef lzma_ret (*lzma_init_function)(
lzma_next_coder *next, const lzma_allocator *allocator,
const lzma_filter_info *filters);
typedef lzma_ret (*lzma_code_function)(
void *coder, const lzma_allocator *allocator,
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,
lzma_action action);
typedef void (*lzma_end_function)(
void *coder, const lzma_allocator *allocator);
struct lzma_filter_info_s {
lzma_vli id;
lzma_init_function init;
void *options;
};
struct lzma_next_coder_s {
void *coder;
lzma_vli id;
uintptr_t init;
lzma_code_function code;
lzma_end_function end;
void (*get_progress)(void *coder,
uint64_t *progress_in, uint64_t *progress_out);
lzma_check (*get_check)(const void *coder);
lzma_ret (*memconfig)(void *coder, uint64_t *memusage,
uint64_t *old_memlimit, uint64_t new_memlimit);
lzma_ret (*update)(void *coder, const lzma_allocator *allocator,
const lzma_filter *filters,
const lzma_filter *reversed_filters);
lzma_ret (*set_out_limit)(void *coder, uint64_t *uncomp_size,
uint64_t out_limit);
};
#define LZMA_NEXT_CODER_INIT \
(lzma_next_coder){ \
.coder = NULL, \
.init = (uintptr_t)(NULL), \
.id = LZMA_VLI_UNKNOWN, \
.code = NULL, \
.end = NULL, \
.get_progress = NULL, \
.get_check = NULL, \
.memconfig = NULL, \
.update = NULL, \
.set_out_limit = NULL, \
}
struct lzma_internal_s {
lzma_next_coder next;
enum {
ISEQ_RUN,
ISEQ_SYNC_FLUSH,
ISEQ_FULL_FLUSH,
ISEQ_FINISH,
ISEQ_FULL_BARRIER,
ISEQ_END,
ISEQ_ERROR,
} sequence;
size_t avail_in;
bool supported_actions[LZMA_ACTION_MAX + 1];
bool allow_buf_error;
};
lzma_attr_alloc_size(1)
extern void *lzma_alloc(size_t size, const lzma_allocator *allocator);
lzma_attr_alloc_size(1)
extern void *lzma_alloc_zero(size_t size, const lzma_allocator *allocator);
extern void lzma_free(void *ptr, const lzma_allocator *allocator);
extern lzma_ret lzma_strm_init(lzma_stream *strm);
extern lzma_ret lzma_next_filter_init(lzma_next_coder *next,
const lzma_allocator *allocator,
const lzma_filter_info *filters);
extern lzma_ret lzma_next_filter_update(
lzma_next_coder *next, const lzma_allocator *allocator,
const lzma_filter *reversed_filters);
extern void lzma_next_end(lzma_next_coder *next,
const lzma_allocator *allocator);
extern 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);
#define return_if_error(expr) \
do { \
const lzma_ret ret_ = (expr); \
if (ret_ != LZMA_OK) \
return ret_; \
} while (0)
#define lzma_next_coder_init(func, next, allocator) \
do { \
if ((uintptr_t)(func) != (next)->init) \
lzma_next_end(next, allocator); \
(next)->init = (uintptr_t)(func); \
} while (0)
#define lzma_next_strm_init(func, strm, ...) \
do { \
return_if_error(lzma_strm_init(strm)); \
const lzma_ret ret_ = func(&(strm)->internal->next, \
(strm)->allocator, __VA_ARGS__); \
if (ret_ != LZMA_OK) { \
lzma_end(strm); \
return ret_; \
} \
} while (0)
#endif