1 /////////////////////////////////////////////////////////////////////////////// 2 // 3 /// \file common.h 4 /// \brief Definitions common to the whole liblzma library 5 // 6 // Author: Lasse Collin 7 // 8 // This file has been put into the public domain. 9 // You can do whatever you want with this file. 10 // 11 /////////////////////////////////////////////////////////////////////////////// 12 13 #ifndef LZMA_COMMON_H 14 #define LZMA_COMMON_H 15 16 #include "sysdefs.h" 17 #include "mythread.h" 18 #include "tuklib_integer.h" 19 20 #if defined(_WIN32) || defined(__CYGWIN__) 21 # ifdef DLL_EXPORT 22 # define LZMA_API_EXPORT __declspec(dllexport) 23 # else 24 # define LZMA_API_EXPORT 25 # endif 26 // Don't use ifdef or defined() below. 27 #elif HAVE_VISIBILITY 28 # define LZMA_API_EXPORT __attribute__((__visibility__("default"))) 29 #else 30 # define LZMA_API_EXPORT 31 #endif 32 33 #define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL 34 35 #include "lzma.h" 36 37 // These allow helping the compiler in some often-executed branches, whose 38 // result is almost always the same. 39 #ifdef __GNUC__ 40 # define likely(expr) __builtin_expect(expr, true) 41 # define unlikely(expr) __builtin_expect(expr, false) 42 #else 43 # define likely(expr) (expr) 44 # define unlikely(expr) (expr) 45 #endif 46 47 48 /// Size of temporary buffers needed in some filters 49 #define LZMA_BUFFER_SIZE 4096 50 51 52 /// Maximum number of worker threads within one multithreaded component. 53 /// The limit exists solely to make it simpler to prevent integer overflows 54 /// when allocating structures etc. This should be big enough for now... 55 /// the code won't scale anywhere close to this number anyway. 56 #define LZMA_THREADS_MAX 16384 57 58 59 /// Starting value for memory usage estimates. Instead of calculating size 60 /// of _every_ structure and taking into account malloc() overhead etc., we 61 /// add a base size to all memory usage estimates. It's not very accurate 62 /// but should be easily good enough. 63 #define LZMA_MEMUSAGE_BASE (UINT64_C(1) << 15) 64 65 /// Start of internal Filter ID space. These IDs must never be used 66 /// in Streams. 67 #define LZMA_FILTER_RESERVED_START (LZMA_VLI_C(1) << 62) 68 69 70 /// Supported flags that can be passed to lzma_stream_decoder() 71 /// or lzma_auto_decoder(). 72 #define LZMA_SUPPORTED_FLAGS \ 73 ( LZMA_TELL_NO_CHECK \ 74 | LZMA_TELL_UNSUPPORTED_CHECK \ 75 | LZMA_TELL_ANY_CHECK \ 76 | LZMA_IGNORE_CHECK \ 77 | LZMA_CONCATENATED ) 78 79 80 /// Largest valid lzma_action value as unsigned integer. 81 #define LZMA_ACTION_MAX ((unsigned int)(LZMA_FULL_BARRIER)) 82 83 84 /// Special return value (lzma_ret) to indicate that a timeout was reached 85 /// and lzma_code() must not return LZMA_BUF_ERROR. This is converted to 86 /// LZMA_OK in lzma_code(). This is not in the lzma_ret enumeration because 87 /// there's no need to have it in the public API. 88 #define LZMA_TIMED_OUT 32 89 90 91 /// Type of encoder/decoder specific data; the actual structure is defined 92 /// differently in different coders. 93 typedef struct lzma_coder_s lzma_coder; 94 95 typedef struct lzma_next_coder_s lzma_next_coder; 96 97 typedef struct lzma_filter_info_s lzma_filter_info; 98 99 100 /// Type of a function used to initialize a filter encoder or decoder 101 typedef lzma_ret (*lzma_init_function)( 102 lzma_next_coder *next, const lzma_allocator *allocator, 103 const lzma_filter_info *filters); 104 105 /// Type of a function to do some kind of coding work (filters, Stream, 106 /// Block encoders/decoders etc.). Some special coders use don't use both 107 /// input and output buffers, but for simplicity they still use this same 108 /// function prototype. 109 typedef lzma_ret (*lzma_code_function)( 110 lzma_coder *coder, const lzma_allocator *allocator, 111 const uint8_t *restrict in, size_t *restrict in_pos, 112 size_t in_size, uint8_t *restrict out, 113 size_t *restrict out_pos, size_t out_size, 114 lzma_action action); 115 116 /// Type of a function to free the memory allocated for the coder 117 typedef void (*lzma_end_function)( 118 lzma_coder *coder, const lzma_allocator *allocator); 119 120 121 /// Raw coder validates and converts an array of lzma_filter structures to 122 /// an array of lzma_filter_info structures. This array is used with 123 /// lzma_next_filter_init to initialize the filter chain. 124 struct lzma_filter_info_s { 125 /// Filter ID. This is used only by the encoder 126 /// with lzma_filters_update(). 127 lzma_vli id; 128 129 /// Pointer to function used to initialize the filter. 130 /// This is NULL to indicate end of array. 131 lzma_init_function init; 132 133 /// Pointer to filter's options structure 134 void *options; 135 }; 136 137 138 /// Hold data and function pointers of the next filter in the chain. 139 struct lzma_next_coder_s { 140 /// Pointer to coder-specific data 141 lzma_coder *coder; 142 143 /// Filter ID. This is LZMA_VLI_UNKNOWN when this structure doesn't 144 /// point to a filter coder. 145 lzma_vli id; 146 147 /// "Pointer" to init function. This is never called here. 148 /// We need only to detect if we are initializing a coder 149 /// that was allocated earlier. See lzma_next_coder_init and 150 /// lzma_next_strm_init macros in this file. 151 uintptr_t init; 152 153 /// Pointer to function to do the actual coding 154 lzma_code_function code; 155 156 /// Pointer to function to free lzma_next_coder.coder. This can 157 /// be NULL; in that case, lzma_free is called to free 158 /// lzma_next_coder.coder. 159 lzma_end_function end; 160 161 /// Pointer to a function to get progress information. If this is NULL, 162 /// lzma_stream.total_in and .total_out are used instead. 163 void (*get_progress)(lzma_coder *coder, 164 uint64_t *progress_in, uint64_t *progress_out); 165 166 /// Pointer to function to return the type of the integrity check. 167 /// Most coders won't support this. 168 lzma_check (*get_check)(const lzma_coder *coder); 169 170 /// Pointer to function to get and/or change the memory usage limit. 171 /// If new_memlimit == 0, the limit is not changed. 172 lzma_ret (*memconfig)(lzma_coder *coder, uint64_t *memusage, 173 uint64_t *old_memlimit, uint64_t new_memlimit); 174 175 /// Update the filter-specific options or the whole filter chain 176 /// in the encoder. 177 lzma_ret (*update)(lzma_coder *coder, const lzma_allocator *allocator, 178 const lzma_filter *filters, 179 const lzma_filter *reversed_filters); 180 }; 181 182 183 /// Macro to initialize lzma_next_coder structure 184 #define LZMA_NEXT_CODER_INIT \ 185 (lzma_next_coder){ \ 186 .coder = NULL, \ 187 .init = (uintptr_t)(NULL), \ 188 .id = LZMA_VLI_UNKNOWN, \ 189 .code = NULL, \ 190 .end = NULL, \ 191 .get_progress = NULL, \ 192 .get_check = NULL, \ 193 .memconfig = NULL, \ 194 .update = NULL, \ 195 } 196 197 198 /// Internal data for lzma_strm_init, lzma_code, and lzma_end. A pointer to 199 /// this is stored in lzma_stream. 200 struct lzma_internal_s { 201 /// The actual coder that should do something useful 202 lzma_next_coder next; 203 204 /// Track the state of the coder. This is used to validate arguments 205 /// so that the actual coders can rely on e.g. that LZMA_SYNC_FLUSH 206 /// is used on every call to lzma_code until next.code has returned 207 /// LZMA_STREAM_END. 208 enum { 209 ISEQ_RUN, 210 ISEQ_SYNC_FLUSH, 211 ISEQ_FULL_FLUSH, 212 ISEQ_FINISH, 213 ISEQ_FULL_BARRIER, 214 ISEQ_END, 215 ISEQ_ERROR, 216 } sequence; 217 218 /// A copy of lzma_stream avail_in. This is used to verify that the 219 /// amount of input doesn't change once e.g. LZMA_FINISH has been 220 /// used. 221 size_t avail_in; 222 223 /// Indicates which lzma_action values are allowed by next.code. 224 bool supported_actions[LZMA_ACTION_MAX + 1]; 225 226 /// If true, lzma_code will return LZMA_BUF_ERROR if no progress was 227 /// made (no input consumed and no output produced by next.code). 228 bool allow_buf_error; 229 }; 230 231 232 /// Allocates memory 233 extern void *lzma_alloc(size_t size, const lzma_allocator *allocator) 234 lzma_attribute((__malloc__)) lzma_attr_alloc_size(1); 235 236 /// Allocates memory and zeroes it (like calloc()). This can be faster 237 /// than lzma_alloc() + memzero() while being backward compatible with 238 /// custom allocators. 239 extern void * lzma_attribute((__malloc__)) lzma_attr_alloc_size(1) 240 lzma_alloc_zero(size_t size, const lzma_allocator *allocator); 241 242 /// Frees memory 243 extern void lzma_free(void *ptr, const lzma_allocator *allocator); 244 245 246 /// Allocates strm->internal if it is NULL, and initializes *strm and 247 /// strm->internal. This function is only called via lzma_next_strm_init macro. 248 extern lzma_ret lzma_strm_init(lzma_stream *strm); 249 250 /// Initializes the next filter in the chain, if any. This takes care of 251 /// freeing the memory of previously initialized filter if it is different 252 /// than the filter being initialized now. This way the actual filter 253 /// initialization functions don't need to use lzma_next_coder_init macro. 254 extern lzma_ret lzma_next_filter_init(lzma_next_coder *next, 255 const lzma_allocator *allocator, 256 const lzma_filter_info *filters); 257 258 /// Update the next filter in the chain, if any. This checks that 259 /// the application is not trying to change the Filter IDs. 260 extern lzma_ret lzma_next_filter_update( 261 lzma_next_coder *next, const lzma_allocator *allocator, 262 const lzma_filter *reversed_filters); 263 264 /// Frees the memory allocated for next->coder either using next->end or, 265 /// if next->end is NULL, using lzma_free. 266 extern void lzma_next_end(lzma_next_coder *next, 267 const lzma_allocator *allocator); 268 269 270 /// Copy as much data as possible from in[] to out[] and update *in_pos 271 /// and *out_pos accordingly. Returns the number of bytes copied. 272 extern size_t lzma_bufcpy(const uint8_t *restrict in, size_t *restrict in_pos, 273 size_t in_size, uint8_t *restrict out, 274 size_t *restrict out_pos, size_t out_size); 275 276 277 /// \brief Return if expression doesn't evaluate to LZMA_OK 278 /// 279 /// There are several situations where we want to return immediately 280 /// with the value of expr if it isn't LZMA_OK. This macro shortens 281 /// the code a little. 282 #define return_if_error(expr) \ 283 do { \ 284 const lzma_ret ret_ = (expr); \ 285 if (ret_ != LZMA_OK) \ 286 return ret_; \ 287 } while (0) 288 289 290 /// If next isn't already initialized, free the previous coder. Then mark 291 /// that next is _possibly_ initialized for the coder using this macro. 292 /// "Possibly" means that if e.g. allocation of next->coder fails, the 293 /// structure isn't actually initialized for this coder, but leaving 294 /// next->init to func is still OK. 295 #define lzma_next_coder_init(func, next, allocator) \ 296 do { \ 297 if ((uintptr_t)(func) != (next)->init) \ 298 lzma_next_end(next, allocator); \ 299 (next)->init = (uintptr_t)(func); \ 300 } while (0) 301 302 303 /// Initializes lzma_strm and calls func() to initialize strm->internal->next. 304 /// (The function being called will use lzma_next_coder_init()). If 305 /// initialization fails, memory that wasn't freed by func() is freed 306 /// along strm->internal. 307 #define lzma_next_strm_init(func, strm, ...) \ 308 do { \ 309 return_if_error(lzma_strm_init(strm)); \ 310 const lzma_ret ret_ = func(&(strm)->internal->next, \ 311 (strm)->allocator, __VA_ARGS__); \ 312 if (ret_ != LZMA_OK) { \ 313 lzma_end(strm); \ 314 return ret_; \ 315 } \ 316 } while (0) 317 318 #endif 319