2007-12-08 23:42:33 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file common.h
|
|
|
|
/// \brief Definitions common to the whole liblzma library
|
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
2009-04-13 10:27:40 +02:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2007-12-08 23:42:33 +01:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef LZMA_COMMON_H
|
|
|
|
#define LZMA_COMMON_H
|
|
|
|
|
2008-04-25 12:58:56 +02:00
|
|
|
#include "../../common/sysdefs.h"
|
2008-12-30 23:30:49 +01:00
|
|
|
#include "../../common/mythread.h"
|
2008-06-18 17:02:10 +02:00
|
|
|
#include "../../common/integer.h"
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2009-02-07 10:11:50 +01:00
|
|
|
#if !defined(LZMA_API_STATIC) && defined(_WIN32)
|
2009-01-31 23:10:07 +01:00
|
|
|
# define LZMA_API_EXPORT __declspec(dllexport)
|
|
|
|
// Don't use ifdef or defined() below.
|
|
|
|
#elif HAVE_VISIBILITY
|
|
|
|
# define LZMA_API_EXPORT __attribute__((__visibility__("default")))
|
2007-12-08 23:42:33 +01:00
|
|
|
#else
|
2009-01-31 23:10:07 +01:00
|
|
|
# define LZMA_API_EXPORT
|
2007-12-08 23:42:33 +01:00
|
|
|
#endif
|
|
|
|
|
2009-02-02 19:14:03 +01:00
|
|
|
#define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL
|
2009-01-31 10:01:48 +01:00
|
|
|
|
|
|
|
#include "lzma.h"
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-06-18 17:02:10 +02:00
|
|
|
// These allow helping the compiler in some often-executed branches, whose
|
|
|
|
// result is almost always the same.
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
/// Size of temporary buffers needed in some filters
|
|
|
|
#define LZMA_BUFFER_SIZE 4096
|
|
|
|
|
|
|
|
|
2008-12-15 18:39:13 +01:00
|
|
|
/// Starting value for memory usage estimates. Instead of calculating size
|
|
|
|
/// of _every_ structure and taking into accont malloc() overhead etc. we
|
|
|
|
/// add a base size to all memory usage estimates. It's not very accurate
|
|
|
|
/// but should be easily good enough.
|
|
|
|
#define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15)
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Start of internal Filter ID space. These IDs must never be used
|
|
|
|
/// in Streams.
|
|
|
|
#define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62)
|
|
|
|
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
/// Internal helper filter used by Subblock decoder. It is mapped to an
|
|
|
|
/// otherwise invalid Filter ID, which is impossible to get from any input
|
|
|
|
/// file (even if malicious file).
|
2008-09-27 22:49:24 +02:00
|
|
|
#define LZMA_FILTER_SUBBLOCK_HELPER LZMA_VLI_C(0x7000000000000001)
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
/// Supported flags that can be passed to lzma_stream_decoder()
|
|
|
|
/// or lzma_auto_decoder().
|
|
|
|
#define LZMA_SUPPORTED_FLAGS \
|
2008-09-06 14:14:30 +02:00
|
|
|
( LZMA_TELL_NO_CHECK \
|
|
|
|
| LZMA_TELL_UNSUPPORTED_CHECK \
|
|
|
|
| LZMA_TELL_ANY_CHECK \
|
|
|
|
| LZMA_CONCATENATED )
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Type of encoder/decoder specific data; the actual structure is defined
|
|
|
|
/// differently in different coders.
|
2007-12-08 23:42:33 +01:00
|
|
|
typedef struct lzma_coder_s lzma_coder;
|
|
|
|
|
|
|
|
typedef struct lzma_next_coder_s lzma_next_coder;
|
|
|
|
|
|
|
|
typedef struct lzma_filter_info_s lzma_filter_info;
|
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Type of a function used to initialize a filter encoder or decoder
|
2007-12-08 23:42:33 +01:00
|
|
|
typedef lzma_ret (*lzma_init_function)(
|
|
|
|
lzma_next_coder *next, lzma_allocator *allocator,
|
|
|
|
const lzma_filter_info *filters);
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Type of a function to do some kind of coding work (filters, Stream,
|
|
|
|
/// Block encoders/decoders etc.). Some special coders use don't use both
|
|
|
|
/// input and output buffers, but for simplicity they still use this same
|
|
|
|
/// function prototype.
|
2007-12-08 23:42:33 +01:00
|
|
|
typedef lzma_ret (*lzma_code_function)(
|
|
|
|
lzma_coder *coder, 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);
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Type of a function to free the memory allocated for the coder
|
2007-12-08 23:42:33 +01:00
|
|
|
typedef void (*lzma_end_function)(
|
|
|
|
lzma_coder *coder, lzma_allocator *allocator);
|
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Raw coder validates and converts an array of lzma_filter structures to
|
|
|
|
/// an array of lzma_filter_info structures. This array is used with
|
|
|
|
/// lzma_next_filter_init to initialize the filter chain.
|
|
|
|
struct lzma_filter_info_s {
|
|
|
|
/// Pointer to function used to initialize the filter.
|
|
|
|
/// This is NULL to indicate end of array.
|
|
|
|
lzma_init_function init;
|
|
|
|
|
|
|
|
/// Pointer to filter's options structure
|
|
|
|
void *options;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-12-08 23:42:33 +01:00
|
|
|
/// Hold data and function pointers of the next filter in the chain.
|
|
|
|
struct lzma_next_coder_s {
|
|
|
|
/// Pointer to coder-specific data
|
|
|
|
lzma_coder *coder;
|
|
|
|
|
|
|
|
/// "Pointer" to init function. This is never called here.
|
|
|
|
/// We need only to detect if we are initializing a coder
|
2008-08-28 21:53:15 +02:00
|
|
|
/// that was allocated earlier. See lzma_next_coder_init and
|
|
|
|
/// lzma_next_strm_init macros in this file.
|
2007-12-08 23:42:33 +01:00
|
|
|
uintptr_t init;
|
|
|
|
|
|
|
|
/// Pointer to function to do the actual coding
|
|
|
|
lzma_code_function code;
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Pointer to function to free lzma_next_coder.coder. This can
|
|
|
|
/// be NULL; in that case, lzma_free is called to free
|
|
|
|
/// lzma_next_coder.coder.
|
2007-12-08 23:42:33 +01:00
|
|
|
lzma_end_function end;
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
/// Pointer to function to return the type of the integrity check.
|
|
|
|
/// Most coders won't support this.
|
2008-09-06 14:14:30 +02:00
|
|
|
lzma_check (*get_check)(const lzma_coder *coder);
|
2008-08-28 21:53:15 +02:00
|
|
|
|
2008-09-06 14:14:30 +02:00
|
|
|
/// Pointer to function to get and/or change the memory usage limit.
|
2009-01-22 11:53:33 +01:00
|
|
|
/// If new_memlimit == 0, the limit is not changed.
|
2008-12-15 18:39:13 +01:00
|
|
|
lzma_ret (*memconfig)(lzma_coder *coder, uint64_t *memusage,
|
|
|
|
uint64_t *old_memlimit, uint64_t new_memlimit);
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
/// Macro to initialize lzma_next_coder structure
|
2007-12-08 23:42:33 +01:00
|
|
|
#define LZMA_NEXT_CODER_INIT \
|
|
|
|
(lzma_next_coder){ \
|
|
|
|
.coder = NULL, \
|
2008-08-28 21:53:15 +02:00
|
|
|
.init = (uintptr_t)(NULL), \
|
2007-12-08 23:42:33 +01:00
|
|
|
.code = NULL, \
|
|
|
|
.end = NULL, \
|
2008-09-06 14:14:30 +02:00
|
|
|
.get_check = NULL, \
|
|
|
|
.memconfig = NULL, \
|
2007-12-08 23:42:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to
|
|
|
|
/// this is stored in lzma_stream.
|
2007-12-08 23:42:33 +01:00
|
|
|
struct lzma_internal_s {
|
2008-08-28 21:53:15 +02:00
|
|
|
/// The actual coder that should do something useful
|
2007-12-08 23:42:33 +01:00
|
|
|
lzma_next_coder next;
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Track the state of the coder. This is used to validate arguments
|
|
|
|
/// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH
|
|
|
|
/// is used on every call to lzma_code until next.code has returned
|
|
|
|
/// LZMA_STREAM_END.
|
2007-12-08 23:42:33 +01:00
|
|
|
enum {
|
|
|
|
ISEQ_RUN,
|
|
|
|
ISEQ_SYNC_FLUSH,
|
|
|
|
ISEQ_FULL_FLUSH,
|
|
|
|
ISEQ_FINISH,
|
|
|
|
ISEQ_END,
|
|
|
|
ISEQ_ERROR,
|
|
|
|
} sequence;
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// A copy of lzma_stream avail_in. This is used to verify that the
|
|
|
|
/// amount of input doesn't change once e.g. LZMA_FINISH has been
|
|
|
|
/// used.
|
2007-12-08 23:42:33 +01:00
|
|
|
size_t avail_in;
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Indicates which lzma_action values are allowed by next.code.
|
|
|
|
bool supported_actions[4];
|
2007-12-08 23:42:33 +01:00
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// If true, lzma_code will return LZMA_BUF_ERROR if no progress was
|
|
|
|
/// made (no input consumed and no output produced by next.code).
|
|
|
|
bool allow_buf_error;
|
2007-12-08 23:42:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Allocates memory
|
|
|
|
extern void *lzma_alloc(size_t size, lzma_allocator *allocator)
|
|
|
|
lzma_attribute((malloc));
|
|
|
|
|
|
|
|
/// Frees memory
|
|
|
|
extern void lzma_free(void *ptr, lzma_allocator *allocator);
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
|
|
|
|
/// Allocates strm->internal if it is NULL, and initializes *strm and
|
|
|
|
/// strm->internal. This function is only called via lzma_next_strm_init macro.
|
2007-12-08 23:42:33 +01:00
|
|
|
extern lzma_ret lzma_strm_init(lzma_stream *strm);
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Initializes the next filter in the chain, if any. This takes care of
|
|
|
|
/// freeing the memory of previously initialized filter if it is different
|
|
|
|
/// than the filter being initialized now. This way the actual filter
|
|
|
|
/// initialization functions don't need to use lzma_next_coder_init macro.
|
2007-12-08 23:42:33 +01:00
|
|
|
extern lzma_ret lzma_next_filter_init(lzma_next_coder *next,
|
|
|
|
lzma_allocator *allocator, const lzma_filter_info *filters);
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Frees the memory allocated for next->coder either using next->end or,
|
|
|
|
/// if next->end is NULL, using lzma_free.
|
|
|
|
extern void lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Copy as much data as possible from in[] to out[] and update *in_pos
|
|
|
|
/// and *out_pos accordingly. Returns the number of bytes copied.
|
|
|
|
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);
|
2007-12-08 23:42:33 +01:00
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// \brief Return if expression doesn't evaluate to LZMA_OK
|
2007-12-08 23:42:33 +01:00
|
|
|
///
|
2008-08-28 21:53:15 +02:00
|
|
|
/// There are several situations where we want to return immediatelly
|
|
|
|
/// with the value of expr if it isn't LZMA_OK. This macro shortens
|
|
|
|
/// the code a little.
|
|
|
|
#define return_if_error(expr) \
|
2007-12-08 23:42:33 +01:00
|
|
|
do { \
|
2008-08-28 21:53:15 +02:00
|
|
|
const lzma_ret ret_ = (expr); \
|
|
|
|
if (ret_ != LZMA_OK) \
|
|
|
|
return ret_; \
|
2007-12-08 23:42:33 +01:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// If next isn't already initialized, free the previous coder. Then mark
|
|
|
|
/// that next is _possibly_ initialized for the coder using this macro.
|
|
|
|
/// "Possibly" means that if e.g. allocation of next->coder fails, the
|
|
|
|
/// structure isn't actually initialized for this coder, but leaving
|
|
|
|
/// next->init to func is still OK.
|
|
|
|
#define lzma_next_coder_init(func, next, allocator) \
|
2007-12-08 23:42:33 +01:00
|
|
|
do { \
|
2009-04-28 22:08:32 +02:00
|
|
|
if ((uintptr_t)(func) != (next)->init) \
|
2008-08-28 21:53:15 +02:00
|
|
|
lzma_next_end(next, allocator); \
|
2009-04-28 22:08:32 +02:00
|
|
|
(next)->init = (uintptr_t)(func); \
|
2007-12-08 23:42:33 +01:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2008-08-28 21:53:15 +02:00
|
|
|
/// Initializes lzma_strm and calls func() to initialize strm->internal->next.
|
|
|
|
/// (The function being called will use lzma_next_coder_init()). If
|
|
|
|
/// initialization fails, memory that wasn't freed by func() is freed
|
|
|
|
/// along strm->internal.
|
|
|
|
#define lzma_next_strm_init(func, strm, ...) \
|
2007-12-08 23:42:33 +01:00
|
|
|
do { \
|
2008-08-28 21:53:15 +02:00
|
|
|
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); \
|
2007-12-08 23:42:33 +01:00
|
|
|
return ret_; \
|
2008-08-28 21:53:15 +02:00
|
|
|
} \
|
2007-12-08 23:42:33 +01:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#endif
|