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