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 52278433Srpaulo/// Maximum number of worker threads within one multithreaded component. 53278433Srpaulo/// The limit exists solely to make it simpler to prevent integer overflows 54278433Srpaulo/// when allocating structures etc. This should be big enough for now... 55278433Srpaulo/// the code won't scale anywhere close to this number anyway. 56278433Srpaulo#define LZMA_THREADS_MAX 16384 57278433Srpaulo 58278433Srpaulo 59207753Smm/// Starting value for memory usage estimates. Instead of calculating size 60207753Smm/// of _every_ structure and taking into account malloc() overhead etc., we 61207753Smm/// add a base size to all memory usage estimates. It's not very accurate 62207753Smm/// but should be easily good enough. 63207753Smm#define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15) 64207753Smm 65207753Smm/// Start of internal Filter ID space. These IDs must never be used 66207753Smm/// in Streams. 67207753Smm#define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62) 68207753Smm 69207753Smm 70207753Smm/// Supported flags that can be passed to lzma_stream_decoder() 71207753Smm/// or lzma_auto_decoder(). 72207753Smm#define LZMA_SUPPORTED_FLAGS \ 73207753Smm ( LZMA_TELL_NO_CHECK \ 74207753Smm | LZMA_TELL_UNSUPPORTED_CHECK \ 75207753Smm | LZMA_TELL_ANY_CHECK \ 76278433Srpaulo | LZMA_IGNORE_CHECK \ 77207753Smm | LZMA_CONCATENATED ) 78207753Smm 79207753Smm 80278433Srpaulo/// Largest valid lzma_action value as unsigned integer. 81278433Srpaulo#define LZMA_ACTION_MAX ((unsigned int)(LZMA_FULL_BARRIER)) 82278433Srpaulo 83278433Srpaulo 84278433Srpaulo/// Special return value (lzma_ret) to indicate that a timeout was reached 85278433Srpaulo/// and lzma_code() must not return LZMA_BUF_ERROR. This is converted to 86278433Srpaulo/// LZMA_OK in lzma_code(). This is not in the lzma_ret enumeration because 87278433Srpaulo/// there's no need to have it in the public API. 88278433Srpaulo#define LZMA_TIMED_OUT 32 89278433Srpaulo 90278433Srpaulo 91207753Smm/// Type of encoder/decoder specific data; the actual structure is defined 92207753Smm/// differently in different coders. 93207753Smmtypedef struct lzma_coder_s lzma_coder; 94207753Smm 95207753Smmtypedef struct lzma_next_coder_s lzma_next_coder; 96207753Smm 97207753Smmtypedef struct lzma_filter_info_s lzma_filter_info; 98207753Smm 99207753Smm 100207753Smm/// Type of a function used to initialize a filter encoder or decoder 101207753Smmtypedef lzma_ret (*lzma_init_function)( 102278433Srpaulo lzma_next_coder *next, const lzma_allocator *allocator, 103207753Smm const lzma_filter_info *filters); 104207753Smm 105207753Smm/// Type of a function to do some kind of coding work (filters, Stream, 106207753Smm/// Block encoders/decoders etc.). Some special coders use don't use both 107207753Smm/// input and output buffers, but for simplicity they still use this same 108207753Smm/// function prototype. 109207753Smmtypedef lzma_ret (*lzma_code_function)( 110278433Srpaulo lzma_coder *coder, const lzma_allocator *allocator, 111207753Smm const uint8_t *restrict in, size_t *restrict in_pos, 112207753Smm size_t in_size, uint8_t *restrict out, 113207753Smm size_t *restrict out_pos, size_t out_size, 114207753Smm lzma_action action); 115207753Smm 116207753Smm/// Type of a function to free the memory allocated for the coder 117207753Smmtypedef void (*lzma_end_function)( 118278433Srpaulo lzma_coder *coder, const lzma_allocator *allocator); 119207753Smm 120207753Smm 121207753Smm/// Raw coder validates and converts an array of lzma_filter structures to 122207753Smm/// an array of lzma_filter_info structures. This array is used with 123207753Smm/// lzma_next_filter_init to initialize the filter chain. 124207753Smmstruct lzma_filter_info_s { 125207753Smm /// Filter ID. This is used only by the encoder 126207753Smm /// with lzma_filters_update(). 127207753Smm lzma_vli id; 128207753Smm 129207753Smm /// Pointer to function used to initialize the filter. 130207753Smm /// This is NULL to indicate end of array. 131207753Smm lzma_init_function init; 132207753Smm 133207753Smm /// Pointer to filter's options structure 134207753Smm void *options; 135207753Smm}; 136207753Smm 137207753Smm 138207753Smm/// Hold data and function pointers of the next filter in the chain. 139207753Smmstruct lzma_next_coder_s { 140207753Smm /// Pointer to coder-specific data 141207753Smm lzma_coder *coder; 142207753Smm 143207753Smm /// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't 144207753Smm /// point to a filter coder. 145207753Smm lzma_vli id; 146207753Smm 147207753Smm /// "Pointer" to init function. This is never called here. 148207753Smm /// We need only to detect if we are initializing a coder 149207753Smm /// that was allocated earlier. See lzma_next_coder_init and 150207753Smm /// lzma_next_strm_init macros in this file. 151207753Smm uintptr_t init; 152207753Smm 153207753Smm /// Pointer to function to do the actual coding 154207753Smm lzma_code_function code; 155207753Smm 156207753Smm /// Pointer to function to free lzma_next_coder.coder. This can 157207753Smm /// be NULL; in that case, lzma_free is called to free 158207753Smm /// lzma_next_coder.coder. 159207753Smm lzma_end_function end; 160207753Smm 161278433Srpaulo /// Pointer to a function to get progress information. If this is NULL, 162278433Srpaulo /// lzma_stream.total_in and .total_out are used instead. 163278433Srpaulo void (*get_progress)(lzma_coder *coder, 164278433Srpaulo uint64_t *progress_in, uint64_t *progress_out); 165278433Srpaulo 166207753Smm /// Pointer to function to return the type of the integrity check. 167207753Smm /// Most coders won't support this. 168207753Smm lzma_check (*get_check)(const lzma_coder *coder); 169207753Smm 170207753Smm /// Pointer to function to get and/or change the memory usage limit. 171207753Smm /// If new_memlimit == 0, the limit is not changed. 172207753Smm lzma_ret (*memconfig)(lzma_coder *coder, uint64_t *memusage, 173207753Smm uint64_t *old_memlimit, uint64_t new_memlimit); 174207753Smm 175207753Smm /// Update the filter-specific options or the whole filter chain 176207753Smm /// in the encoder. 177278433Srpaulo lzma_ret (*update)(lzma_coder *coder, const lzma_allocator *allocator, 178207753Smm const lzma_filter *filters, 179207753Smm const lzma_filter *reversed_filters); 180207753Smm}; 181207753Smm 182207753Smm 183207753Smm/// Macro to initialize lzma_next_coder structure 184207753Smm#define LZMA_NEXT_CODER_INIT \ 185207753Smm (lzma_next_coder){ \ 186207753Smm .coder = NULL, \ 187207753Smm .init = (uintptr_t)(NULL), \ 188207753Smm .id = LZMA_VLI_UNKNOWN, \ 189207753Smm .code = NULL, \ 190207753Smm .end = NULL, \ 191278433Srpaulo .get_progress = NULL, \ 192207753Smm .get_check = NULL, \ 193207753Smm .memconfig = NULL, \ 194207753Smm .update = NULL, \ 195207753Smm } 196207753Smm 197207753Smm 198207753Smm/// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to 199207753Smm/// this is stored in lzma_stream. 200207753Smmstruct lzma_internal_s { 201207753Smm /// The actual coder that should do something useful 202207753Smm lzma_next_coder next; 203207753Smm 204207753Smm /// Track the state of the coder. This is used to validate arguments 205207753Smm /// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH 206207753Smm /// is used on every call to lzma_code until next.code has returned 207207753Smm /// LZMA_STREAM_END. 208207753Smm enum { 209207753Smm ISEQ_RUN, 210207753Smm ISEQ_SYNC_FLUSH, 211207753Smm ISEQ_FULL_FLUSH, 212207753Smm ISEQ_FINISH, 213278433Srpaulo ISEQ_FULL_BARRIER, 214207753Smm ISEQ_END, 215207753Smm ISEQ_ERROR, 216207753Smm } sequence; 217207753Smm 218207753Smm /// A copy of lzma_stream avail_in. This is used to verify that the 219207753Smm /// amount of input doesn't change once e.g. LZMA_FINISH has been 220207753Smm /// used. 221207753Smm size_t avail_in; 222207753Smm 223207753Smm /// Indicates which lzma_action values are allowed by next.code. 224278433Srpaulo bool supported_actions[LZMA_ACTION_MAX + 1]; 225207753Smm 226207753Smm /// If true, lzma_code will return LZMA_BUF_ERROR if no progress was 227207753Smm /// made (no input consumed and no output produced by next.code). 228207753Smm bool allow_buf_error; 229207753Smm}; 230207753Smm 231207753Smm 232207753Smm/// Allocates memory 233278433Srpauloextern void *lzma_alloc(size_t size, const lzma_allocator *allocator) 234223935Smm lzma_attribute((__malloc__)) lzma_attr_alloc_size(1); 235207753Smm 236278433Srpaulo/// Allocates memory and zeroes it (like calloc()). This can be faster 237278433Srpaulo/// than lzma_alloc() + memzero() while being backward compatible with 238278433Srpaulo/// custom allocators. 239278433Srpauloextern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1) 240278433Srpaulo lzma_alloc_zero(size_t size, const lzma_allocator *allocator); 241278433Srpaulo 242207753Smm/// Frees memory 243278433Srpauloextern void lzma_free(void *ptr, const lzma_allocator *allocator); 244207753Smm 245207753Smm 246207753Smm/// Allocates strm->internal if it is NULL, and initializes *strm and 247207753Smm/// strm->internal. This function is only called via lzma_next_strm_init macro. 248207753Smmextern lzma_ret lzma_strm_init(lzma_stream *strm); 249207753Smm 250207753Smm/// Initializes the next filter in the chain, if any. This takes care of 251207753Smm/// freeing the memory of previously initialized filter if it is different 252207753Smm/// than the filter being initialized now. This way the actual filter 253207753Smm/// initialization functions don't need to use lzma_next_coder_init macro. 254207753Smmextern lzma_ret lzma_next_filter_init(lzma_next_coder *next, 255278433Srpaulo const lzma_allocator *allocator, 256278433Srpaulo const lzma_filter_info *filters); 257207753Smm 258207753Smm/// Update the next filter in the chain, if any. This checks that 259207753Smm/// the application is not trying to change the Filter IDs. 260207753Smmextern lzma_ret lzma_next_filter_update( 261278433Srpaulo lzma_next_coder *next, const lzma_allocator *allocator, 262207753Smm const lzma_filter *reversed_filters); 263207753Smm 264207753Smm/// Frees the memory allocated for next->coder either using next->end or, 265207753Smm/// if next->end is NULL, using lzma_free. 266278433Srpauloextern void lzma_next_end(lzma_next_coder *next, 267278433Srpaulo const lzma_allocator *allocator); 268207753Smm 269207753Smm 270207753Smm/// Copy as much data as possible from in[] to out[] and update *in_pos 271207753Smm/// and *out_pos accordingly. Returns the number of bytes copied. 272207753Smmextern size_t lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos, 273207753Smm size_t in_size, uint8_t *restrict out, 274207753Smm size_t *restrict out_pos, size_t out_size); 275207753Smm 276207753Smm 277207753Smm/// \brief Return if expression doesn't evaluate to LZMA_OK 278207753Smm/// 279207753Smm/// There are several situations where we want to return immediately 280207753Smm/// with the value of expr if it isn't LZMA_OK. This macro shortens 281207753Smm/// the code a little. 282207753Smm#define return_if_error(expr) \ 283207753Smmdo { \ 284207753Smm const lzma_ret ret_ = (expr); \ 285207753Smm if (ret_ != LZMA_OK) \ 286207753Smm return ret_; \ 287207753Smm} while (0) 288207753Smm 289207753Smm 290207753Smm/// If next isn't already initialized, free the previous coder. Then mark 291207753Smm/// that next is _possibly_ initialized for the coder using this macro. 292207753Smm/// "Possibly" means that if e.g. allocation of next->coder fails, the 293207753Smm/// structure isn't actually initialized for this coder, but leaving 294207753Smm/// next->init to func is still OK. 295207753Smm#define lzma_next_coder_init(func, next, allocator) \ 296207753Smmdo { \ 297207753Smm if ((uintptr_t)(func) != (next)->init) \ 298207753Smm lzma_next_end(next, allocator); \ 299207753Smm (next)->init = (uintptr_t)(func); \ 300207753Smm} while (0) 301207753Smm 302207753Smm 303207753Smm/// Initializes lzma_strm and calls func() to initialize strm->internal->next. 304207753Smm/// (The function being called will use lzma_next_coder_init()). If 305207753Smm/// initialization fails, memory that wasn't freed by func() is freed 306207753Smm/// along strm->internal. 307207753Smm#define lzma_next_strm_init(func, strm, ...) \ 308207753Smmdo { \ 309207753Smm return_if_error(lzma_strm_init(strm)); \ 310207753Smm const lzma_ret ret_ = func(&(strm)->internal->next, \ 311207753Smm (strm)->allocator, __VA_ARGS__); \ 312207753Smm if (ret_ != LZMA_OK) { \ 313207753Smm lzma_end(strm); \ 314207753Smm return ret_; \ 315207753Smm } \ 316207753Smm} while (0) 317207753Smm 318207753Smm#endif 319