1207753Smm/////////////////////////////////////////////////////////////////////////////// 2207753Smm// 3207753Smm/// \file common.h 4207753Smm/// \brief Definitions common to the whole liblzma library 5207753Smm// 6207753Smm// Author: Lasse Collin 7207753Smm// 8207753Smm// This file has been put into the public domain. 9207753Smm// You can do whatever you want with this file. 10207753Smm// 11207753Smm/////////////////////////////////////////////////////////////////////////////// 12207753Smm 13207753Smm#ifndef LZMA_COMMON_H 14207753Smm#define LZMA_COMMON_H 15207753Smm 16207753Smm#include "sysdefs.h" 17207753Smm#include "mythread.h" 18207753Smm#include "tuklib_integer.h" 19207753Smm 20207753Smm#if defined(_WIN32) || defined(__CYGWIN__) 21207753Smm# ifdef DLL_EXPORT 22207753Smm# define LZMA_API_EXPORT __declspec(dllexport) 23207753Smm# else 24207753Smm# define LZMA_API_EXPORT 25207753Smm# endif 26207753Smm// Don't use ifdef or defined() below. 27207753Smm#elif HAVE_VISIBILITY 28207753Smm# define LZMA_API_EXPORT __attribute__((__visibility__("default"))) 29207753Smm#else 30207753Smm# define LZMA_API_EXPORT 31207753Smm#endif 32207753Smm 33207753Smm#define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL 34207753Smm 35207753Smm#include "lzma.h" 36207753Smm 37207753Smm// These allow helping the compiler in some often-executed branches, whose 38207753Smm// result is almost always the same. 39207753Smm#ifdef __GNUC__ 40207753Smm# define likely(expr) __builtin_expect(expr, true) 41207753Smm# define unlikely(expr) __builtin_expect(expr, false) 42207753Smm#else 43207753Smm# define likely(expr) (expr) 44207753Smm# define unlikely(expr) (expr) 45207753Smm#endif 46207753Smm 47207753Smm 48207753Smm/// Size of temporary buffers needed in some filters 49207753Smm#define LZMA_BUFFER_SIZE 4096 50207753Smm 51207753Smm 52207753Smm/// Starting value for memory usage estimates. Instead of calculating size 53207753Smm/// of _every_ structure and taking into account malloc() overhead etc., we 54207753Smm/// add a base size to all memory usage estimates. It's not very accurate 55207753Smm/// but should be easily good enough. 56207753Smm#define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15) 57207753Smm 58207753Smm/// Start of internal Filter ID space. These IDs must never be used 59207753Smm/// in Streams. 60207753Smm#define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62) 61207753Smm 62207753Smm 63207753Smm/// Supported flags that can be passed to lzma_stream_decoder() 64207753Smm/// or lzma_auto_decoder(). 65207753Smm#define LZMA_SUPPORTED_FLAGS \ 66207753Smm ( LZMA_TELL_NO_CHECK \ 67207753Smm | LZMA_TELL_UNSUPPORTED_CHECK \ 68207753Smm | LZMA_TELL_ANY_CHECK \ 69207753Smm | LZMA_CONCATENATED ) 70207753Smm 71207753Smm 72207753Smm/// Type of encoder/decoder specific data; the actual structure is defined 73207753Smm/// differently in different coders. 74207753Smmtypedef struct lzma_coder_s lzma_coder; 75207753Smm 76207753Smmtypedef struct lzma_next_coder_s lzma_next_coder; 77207753Smm 78207753Smmtypedef struct lzma_filter_info_s lzma_filter_info; 79207753Smm 80207753Smm 81207753Smm/// Type of a function used to initialize a filter encoder or decoder 82207753Smmtypedef lzma_ret (*lzma_init_function)( 83207753Smm lzma_next_coder *next, lzma_allocator *allocator, 84207753Smm const lzma_filter_info *filters); 85207753Smm 86207753Smm/// Type of a function to do some kind of coding work (filters, Stream, 87207753Smm/// Block encoders/decoders etc.). Some special coders use don't use both 88207753Smm/// input and output buffers, but for simplicity they still use this same 89207753Smm/// function prototype. 90207753Smmtypedef lzma_ret (*lzma_code_function)( 91207753Smm lzma_coder *coder, lzma_allocator *allocator, 92207753Smm const uint8_t *restrict in, size_t *restrict in_pos, 93207753Smm size_t in_size, uint8_t *restrict out, 94207753Smm size_t *restrict out_pos, size_t out_size, 95207753Smm lzma_action action); 96207753Smm 97207753Smm/// Type of a function to free the memory allocated for the coder 98207753Smmtypedef void (*lzma_end_function)( 99207753Smm lzma_coder *coder, lzma_allocator *allocator); 100207753Smm 101207753Smm 102207753Smm/// Raw coder validates and converts an array of lzma_filter structures to 103207753Smm/// an array of lzma_filter_info structures. This array is used with 104207753Smm/// lzma_next_filter_init to initialize the filter chain. 105207753Smmstruct lzma_filter_info_s { 106207753Smm /// Filter ID. This is used only by the encoder 107207753Smm /// with lzma_filters_update(). 108207753Smm lzma_vli id; 109207753Smm 110207753Smm /// Pointer to function used to initialize the filter. 111207753Smm /// This is NULL to indicate end of array. 112207753Smm lzma_init_function init; 113207753Smm 114207753Smm /// Pointer to filter's options structure 115207753Smm void *options; 116207753Smm}; 117207753Smm 118207753Smm 119207753Smm/// Hold data and function pointers of the next filter in the chain. 120207753Smmstruct lzma_next_coder_s { 121207753Smm /// Pointer to coder-specific data 122207753Smm lzma_coder *coder; 123207753Smm 124207753Smm /// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't 125207753Smm /// point to a filter coder. 126207753Smm lzma_vli id; 127207753Smm 128207753Smm /// "Pointer" to init function. This is never called here. 129207753Smm /// We need only to detect if we are initializing a coder 130207753Smm /// that was allocated earlier. See lzma_next_coder_init and 131207753Smm /// lzma_next_strm_init macros in this file. 132207753Smm uintptr_t init; 133207753Smm 134207753Smm /// Pointer to function to do the actual coding 135207753Smm lzma_code_function code; 136207753Smm 137207753Smm /// Pointer to function to free lzma_next_coder.coder. This can 138207753Smm /// be NULL; in that case, lzma_free is called to free 139207753Smm /// lzma_next_coder.coder. 140207753Smm lzma_end_function end; 141207753Smm 142207753Smm /// Pointer to function to return the type of the integrity check. 143207753Smm /// Most coders won't support this. 144207753Smm lzma_check (*get_check)(const lzma_coder *coder); 145207753Smm 146207753Smm /// Pointer to function to get and/or change the memory usage limit. 147207753Smm /// If new_memlimit == 0, the limit is not changed. 148207753Smm lzma_ret (*memconfig)(lzma_coder *coder, uint64_t *memusage, 149207753Smm uint64_t *old_memlimit, uint64_t new_memlimit); 150207753Smm 151207753Smm /// Update the filter-specific options or the whole filter chain 152207753Smm /// in the encoder. 153207753Smm lzma_ret (*update)(lzma_coder *coder, lzma_allocator *allocator, 154207753Smm const lzma_filter *filters, 155207753Smm const lzma_filter *reversed_filters); 156207753Smm}; 157207753Smm 158207753Smm 159207753Smm/// Macro to initialize lzma_next_coder structure 160207753Smm#define LZMA_NEXT_CODER_INIT \ 161207753Smm (lzma_next_coder){ \ 162207753Smm .coder = NULL, \ 163207753Smm .init = (uintptr_t)(NULL), \ 164207753Smm .id = LZMA_VLI_UNKNOWN, \ 165207753Smm .code = NULL, \ 166207753Smm .end = NULL, \ 167207753Smm .get_check = NULL, \ 168207753Smm .memconfig = NULL, \ 169207753Smm .update = NULL, \ 170207753Smm } 171207753Smm 172207753Smm 173207753Smm/// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to 174207753Smm/// this is stored in lzma_stream. 175207753Smmstruct lzma_internal_s { 176207753Smm /// The actual coder that should do something useful 177207753Smm lzma_next_coder next; 178207753Smm 179207753Smm /// Track the state of the coder. This is used to validate arguments 180207753Smm /// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH 181207753Smm /// is used on every call to lzma_code until next.code has returned 182207753Smm /// LZMA_STREAM_END. 183207753Smm enum { 184207753Smm ISEQ_RUN, 185207753Smm ISEQ_SYNC_FLUSH, 186207753Smm ISEQ_FULL_FLUSH, 187207753Smm ISEQ_FINISH, 188207753Smm ISEQ_END, 189207753Smm ISEQ_ERROR, 190207753Smm } sequence; 191207753Smm 192207753Smm /// A copy of lzma_stream avail_in. This is used to verify that the 193207753Smm /// amount of input doesn't change once e.g. LZMA_FINISH has been 194207753Smm /// used. 195207753Smm size_t avail_in; 196207753Smm 197207753Smm /// Indicates which lzma_action values are allowed by next.code. 198207753Smm bool supported_actions[4]; 199207753Smm 200207753Smm /// If true, lzma_code will return LZMA_BUF_ERROR if no progress was 201207753Smm /// made (no input consumed and no output produced by next.code). 202207753Smm bool allow_buf_error; 203207753Smm}; 204207753Smm 205207753Smm 206207753Smm/// Allocates memory 207207753Smmextern void *lzma_alloc(size_t size, lzma_allocator *allocator) 208223935Smm lzma_attribute((__malloc__)) lzma_attr_alloc_size(1); 209207753Smm 210207753Smm/// Frees memory 211207753Smmextern void lzma_free(void *ptr, lzma_allocator *allocator); 212207753Smm 213207753Smm 214207753Smm/// Allocates strm->internal if it is NULL, and initializes *strm and 215207753Smm/// strm->internal. This function is only called via lzma_next_strm_init macro. 216207753Smmextern lzma_ret lzma_strm_init(lzma_stream *strm); 217207753Smm 218207753Smm/// Initializes the next filter in the chain, if any. This takes care of 219207753Smm/// freeing the memory of previously initialized filter if it is different 220207753Smm/// than the filter being initialized now. This way the actual filter 221207753Smm/// initialization functions don't need to use lzma_next_coder_init macro. 222207753Smmextern lzma_ret lzma_next_filter_init(lzma_next_coder *next, 223207753Smm lzma_allocator *allocator, const lzma_filter_info *filters); 224207753Smm 225207753Smm/// Update the next filter in the chain, if any. This checks that 226207753Smm/// the application is not trying to change the Filter IDs. 227207753Smmextern lzma_ret lzma_next_filter_update( 228207753Smm lzma_next_coder *next, lzma_allocator *allocator, 229207753Smm const lzma_filter *reversed_filters); 230207753Smm 231207753Smm/// Frees the memory allocated for next->coder either using next->end or, 232207753Smm/// if next->end is NULL, using lzma_free. 233207753Smmextern void lzma_next_end(lzma_next_coder *next, lzma_allocator *allocator); 234207753Smm 235207753Smm 236207753Smm/// Copy as much data as possible from in[] to out[] and update *in_pos 237207753Smm/// and *out_pos accordingly. Returns the number of bytes copied. 238207753Smmextern size_t lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos, 239207753Smm size_t in_size, uint8_t *restrict out, 240207753Smm size_t *restrict out_pos, size_t out_size); 241207753Smm 242207753Smm 243207753Smm/// \brief Return if expression doesn't evaluate to LZMA_OK 244207753Smm/// 245207753Smm/// There are several situations where we want to return immediately 246207753Smm/// with the value of expr if it isn't LZMA_OK. This macro shortens 247207753Smm/// the code a little. 248207753Smm#define return_if_error(expr) \ 249207753Smmdo { \ 250207753Smm const lzma_ret ret_ = (expr); \ 251207753Smm if (ret_ != LZMA_OK) \ 252207753Smm return ret_; \ 253207753Smm} while (0) 254207753Smm 255207753Smm 256207753Smm/// If next isn't already initialized, free the previous coder. Then mark 257207753Smm/// that next is _possibly_ initialized for the coder using this macro. 258207753Smm/// "Possibly" means that if e.g. allocation of next->coder fails, the 259207753Smm/// structure isn't actually initialized for this coder, but leaving 260207753Smm/// next->init to func is still OK. 261207753Smm#define lzma_next_coder_init(func, next, allocator) \ 262207753Smmdo { \ 263207753Smm if ((uintptr_t)(func) != (next)->init) \ 264207753Smm lzma_next_end(next, allocator); \ 265207753Smm (next)->init = (uintptr_t)(func); \ 266207753Smm} while (0) 267207753Smm 268207753Smm 269207753Smm/// Initializes lzma_strm and calls func() to initialize strm->internal->next. 270207753Smm/// (The function being called will use lzma_next_coder_init()). If 271207753Smm/// initialization fails, memory that wasn't freed by func() is freed 272207753Smm/// along strm->internal. 273207753Smm#define lzma_next_strm_init(func, strm, ...) \ 274207753Smmdo { \ 275207753Smm return_if_error(lzma_strm_init(strm)); \ 276207753Smm const lzma_ret ret_ = func(&(strm)->internal->next, \ 277207753Smm (strm)->allocator, __VA_ARGS__); \ 278207753Smm if (ret_ != LZMA_OK) { \ 279207753Smm lzma_end(strm); \ 280207753Smm return ret_; \ 281207753Smm } \ 282207753Smm} while (0) 283207753Smm 284207753Smm#endif 285