Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 1 | /* _lzma - Low-level Python interface to liblzma. |
| 2 | |
| 3 | Initial implementation by Per Øyvind Karlsen. |
| 4 | Rewritten by Nadeem Vawda. |
| 5 | |
| 6 | */ |
| 7 | |
| 8 | #define PY_SSIZE_T_CLEAN |
| 9 | |
| 10 | #include "Python.h" |
| 11 | #include "structmember.h" |
| 12 | #ifdef WITH_THREAD |
| 13 | #include "pythread.h" |
| 14 | #endif |
| 15 | |
| 16 | #include <stdarg.h> |
| 17 | #include <string.h> |
| 18 | |
| 19 | #include <lzma.h> |
| 20 | |
| 21 | |
| 22 | #ifndef PY_LONG_LONG |
| 23 | #error "This module requires PY_LONG_LONG to be defined" |
| 24 | #endif |
| 25 | |
| 26 | |
| 27 | #ifdef WITH_THREAD |
| 28 | #define ACQUIRE_LOCK(obj) do { \ |
| 29 | if (!PyThread_acquire_lock((obj)->lock, 0)) { \ |
| 30 | Py_BEGIN_ALLOW_THREADS \ |
| 31 | PyThread_acquire_lock((obj)->lock, 1); \ |
| 32 | Py_END_ALLOW_THREADS \ |
| 33 | } } while (0) |
| 34 | #define RELEASE_LOCK(obj) PyThread_release_lock((obj)->lock) |
| 35 | #else |
| 36 | #define ACQUIRE_LOCK(obj) |
| 37 | #define RELEASE_LOCK(obj) |
| 38 | #endif |
| 39 | |
| 40 | |
| 41 | /* Container formats: */ |
| 42 | enum { |
| 43 | FORMAT_AUTO, |
| 44 | FORMAT_XZ, |
| 45 | FORMAT_ALONE, |
| 46 | FORMAT_RAW, |
| 47 | }; |
| 48 | |
| 49 | #define LZMA_CHECK_UNKNOWN (LZMA_CHECK_ID_MAX + 1) |
| 50 | |
| 51 | |
| 52 | typedef struct { |
| 53 | PyObject_HEAD |
| 54 | lzma_allocator alloc; |
| 55 | lzma_stream lzs; |
| 56 | int flushed; |
| 57 | #ifdef WITH_THREAD |
| 58 | PyThread_type_lock lock; |
| 59 | #endif |
| 60 | } Compressor; |
| 61 | |
| 62 | typedef struct { |
| 63 | PyObject_HEAD |
| 64 | lzma_allocator alloc; |
| 65 | lzma_stream lzs; |
| 66 | int check; |
| 67 | char eof; |
| 68 | PyObject *unused_data; |
| 69 | #ifdef WITH_THREAD |
| 70 | PyThread_type_lock lock; |
| 71 | #endif |
| 72 | } Decompressor; |
| 73 | |
| 74 | /* LZMAError class object. */ |
| 75 | static PyObject *Error; |
| 76 | |
| 77 | /* An empty tuple, used by the filter specifier parsing code. */ |
| 78 | static PyObject *empty_tuple; |
| 79 | |
| 80 | |
| 81 | /* Helper functions. */ |
| 82 | |
| 83 | static int |
| 84 | catch_lzma_error(lzma_ret lzret) |
| 85 | { |
| 86 | switch (lzret) { |
| 87 | case LZMA_OK: |
| 88 | case LZMA_GET_CHECK: |
| 89 | case LZMA_NO_CHECK: |
| 90 | case LZMA_STREAM_END: |
| 91 | return 0; |
| 92 | case LZMA_UNSUPPORTED_CHECK: |
| 93 | PyErr_SetString(Error, "Unsupported integrity check"); |
| 94 | return 1; |
| 95 | case LZMA_MEM_ERROR: |
| 96 | PyErr_NoMemory(); |
| 97 | return 1; |
| 98 | case LZMA_MEMLIMIT_ERROR: |
| 99 | PyErr_SetString(Error, "Memory usage limit exceeded"); |
| 100 | return 1; |
| 101 | case LZMA_FORMAT_ERROR: |
| 102 | PyErr_SetString(Error, "Input format not supported by decoder"); |
| 103 | return 1; |
| 104 | case LZMA_OPTIONS_ERROR: |
| 105 | PyErr_SetString(Error, "Invalid or unsupported options"); |
| 106 | return 1; |
| 107 | case LZMA_DATA_ERROR: |
| 108 | PyErr_SetString(Error, "Corrupt input data"); |
| 109 | return 1; |
| 110 | case LZMA_BUF_ERROR: |
| 111 | PyErr_SetString(Error, "Insufficient buffer space"); |
| 112 | return 1; |
| 113 | case LZMA_PROG_ERROR: |
| 114 | PyErr_SetString(Error, "Internal error"); |
| 115 | return 1; |
| 116 | default: |
| 117 | PyErr_Format(Error, "Unrecognized error from liblzma: %d", lzret); |
| 118 | return 1; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static void* |
| 123 | PyLzma_Malloc(void *opaque, size_t items, size_t size) |
| 124 | { |
| 125 | if (items > (size_t)PY_SSIZE_T_MAX / size) |
| 126 | return NULL; |
| 127 | /* PyMem_Malloc() cannot be used: |
| 128 | the GIL is not held when lzma_code() is called */ |
| 129 | return PyMem_RawMalloc(items * size); |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | PyLzma_Free(void *opaque, void *ptr) |
| 134 | { |
| 135 | PyMem_RawFree(ptr); |
| 136 | } |
| 137 | |
| 138 | #if BUFSIZ < 8192 |
| 139 | #define INITIAL_BUFFER_SIZE 8192 |
| 140 | #else |
| 141 | #define INITIAL_BUFFER_SIZE BUFSIZ |
| 142 | #endif |
| 143 | |
| 144 | static int |
| 145 | grow_buffer(PyObject **buf) |
| 146 | { |
| 147 | size_t size = PyBytes_GET_SIZE(*buf); |
| 148 | return _PyBytes_Resize(buf, size + (size >> 3) + 6); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /* Some custom type conversions for PyArg_ParseTupleAndKeywords(), |
| 153 | since the predefined conversion specifiers do not suit our needs: |
| 154 | |
| 155 | uint32_t - the "I" (unsigned int) specifier is the right size, but |
| 156 | silently ignores overflows on conversion. |
| 157 | |
| 158 | lzma_vli - the "K" (unsigned PY_LONG_LONG) specifier is the right |
| 159 | size, but like "I" it silently ignores overflows on conversion. |
| 160 | |
| 161 | lzma_mode and lzma_match_finder - these are enumeration types, and |
| 162 | so the size of each is implementation-defined. Worse, different |
| 163 | enum types can be of different sizes within the same program, so |
| 164 | to be strictly correct, we need to define two separate converters. |
| 165 | */ |
| 166 | |
| 167 | #define INT_TYPE_CONVERTER_FUNC(TYPE, FUNCNAME) \ |
| 168 | static int \ |
| 169 | FUNCNAME(PyObject *obj, void *ptr) \ |
| 170 | { \ |
| 171 | unsigned PY_LONG_LONG val; \ |
| 172 | \ |
| 173 | val = PyLong_AsUnsignedLongLong(obj); \ |
| 174 | if (PyErr_Occurred()) \ |
| 175 | return 0; \ |
| 176 | if ((unsigned PY_LONG_LONG)(TYPE)val != val) { \ |
| 177 | PyErr_SetString(PyExc_OverflowError, \ |
| 178 | "Value too large for " #TYPE " type"); \ |
| 179 | return 0; \ |
| 180 | } \ |
| 181 | *(TYPE *)ptr = (TYPE)val; \ |
| 182 | return 1; \ |
| 183 | } |
| 184 | |
| 185 | INT_TYPE_CONVERTER_FUNC(uint32_t, uint32_converter) |
| 186 | INT_TYPE_CONVERTER_FUNC(lzma_vli, lzma_vli_converter) |
| 187 | INT_TYPE_CONVERTER_FUNC(lzma_mode, lzma_mode_converter) |
| 188 | INT_TYPE_CONVERTER_FUNC(lzma_match_finder, lzma_mf_converter) |
| 189 | |
| 190 | #undef INT_TYPE_CONVERTER_FUNC |
| 191 | |
| 192 | |
| 193 | /* Filter specifier parsing. |
| 194 | |
| 195 | This code handles converting filter specifiers (Python dicts) into |
| 196 | the C lzma_filter structs expected by liblzma. */ |
| 197 | |
| 198 | static void * |
| 199 | parse_filter_spec_lzma(PyObject *spec) |
| 200 | { |
| 201 | static char *optnames[] = {"id", "preset", "dict_size", "lc", "lp", |
| 202 | "pb", "mode", "nice_len", "mf", "depth", NULL}; |
| 203 | PyObject *id; |
| 204 | PyObject *preset_obj; |
| 205 | uint32_t preset = LZMA_PRESET_DEFAULT; |
| 206 | lzma_options_lzma *options; |
| 207 | |
| 208 | /* First, fill in default values for all the options using a preset. |
| 209 | Then, override the defaults with any values given by the caller. */ |
| 210 | |
| 211 | preset_obj = PyMapping_GetItemString(spec, "preset"); |
| 212 | if (preset_obj == NULL) { |
| 213 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 214 | PyErr_Clear(); |
| 215 | else |
| 216 | return NULL; |
| 217 | } else { |
| 218 | int ok = uint32_converter(preset_obj, &preset); |
| 219 | Py_DECREF(preset_obj); |
| 220 | if (!ok) |
| 221 | return NULL; |
| 222 | } |
| 223 | |
| 224 | options = (lzma_options_lzma *)PyMem_Malloc(sizeof *options); |
| 225 | if (options == NULL) |
| 226 | return PyErr_NoMemory(); |
| 227 | memset(options, 0, sizeof *options); |
| 228 | |
| 229 | if (lzma_lzma_preset(options, preset)) { |
| 230 | PyMem_Free(options); |
| 231 | PyErr_Format(Error, "Invalid compression preset: %d", preset); |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | if (!PyArg_ParseTupleAndKeywords(empty_tuple, spec, |
| 236 | "|OOO&O&O&O&O&O&O&O&", optnames, |
| 237 | &id, &preset_obj, |
| 238 | uint32_converter, &options->dict_size, |
| 239 | uint32_converter, &options->lc, |
| 240 | uint32_converter, &options->lp, |
| 241 | uint32_converter, &options->pb, |
| 242 | lzma_mode_converter, &options->mode, |
| 243 | uint32_converter, &options->nice_len, |
| 244 | lzma_mf_converter, &options->mf, |
| 245 | uint32_converter, &options->depth)) { |
| 246 | PyErr_SetString(PyExc_ValueError, |
| 247 | "Invalid filter specifier for LZMA filter"); |
| 248 | PyMem_Free(options); |
| 249 | options = NULL; |
| 250 | } |
| 251 | return options; |
| 252 | } |
| 253 | |
| 254 | static void * |
| 255 | parse_filter_spec_delta(PyObject *spec) |
| 256 | { |
| 257 | static char *optnames[] = {"id", "dist", NULL}; |
| 258 | PyObject *id; |
| 259 | uint32_t dist = 1; |
| 260 | lzma_options_delta *options; |
| 261 | |
| 262 | if (!PyArg_ParseTupleAndKeywords(empty_tuple, spec, "|OO&", optnames, |
| 263 | &id, uint32_converter, &dist)) { |
| 264 | PyErr_SetString(PyExc_ValueError, |
| 265 | "Invalid filter specifier for delta filter"); |
| 266 | return NULL; |
| 267 | } |
| 268 | |
| 269 | options = (lzma_options_delta *)PyMem_Malloc(sizeof *options); |
| 270 | if (options == NULL) |
| 271 | return PyErr_NoMemory(); |
| 272 | memset(options, 0, sizeof *options); |
| 273 | options->type = LZMA_DELTA_TYPE_BYTE; |
| 274 | options->dist = dist; |
| 275 | return options; |
| 276 | } |
| 277 | |
| 278 | static void * |
| 279 | parse_filter_spec_bcj(PyObject *spec) |
| 280 | { |
| 281 | static char *optnames[] = {"id", "start_offset", NULL}; |
| 282 | PyObject *id; |
| 283 | uint32_t start_offset = 0; |
| 284 | lzma_options_bcj *options; |
| 285 | |
| 286 | if (!PyArg_ParseTupleAndKeywords(empty_tuple, spec, "|OO&", optnames, |
| 287 | &id, uint32_converter, &start_offset)) { |
| 288 | PyErr_SetString(PyExc_ValueError, |
| 289 | "Invalid filter specifier for BCJ filter"); |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | options = (lzma_options_bcj *)PyMem_Malloc(sizeof *options); |
| 294 | if (options == NULL) |
| 295 | return PyErr_NoMemory(); |
| 296 | memset(options, 0, sizeof *options); |
| 297 | options->start_offset = start_offset; |
| 298 | return options; |
| 299 | } |
| 300 | |
| 301 | static int |
| 302 | lzma_filter_converter(PyObject *spec, void *ptr) |
| 303 | { |
| 304 | lzma_filter *f = (lzma_filter *)ptr; |
| 305 | PyObject *id_obj; |
| 306 | |
| 307 | if (!PyMapping_Check(spec)) { |
| 308 | PyErr_SetString(PyExc_TypeError, |
| 309 | "Filter specifier must be a dict or dict-like object"); |
| 310 | return 0; |
| 311 | } |
| 312 | id_obj = PyMapping_GetItemString(spec, "id"); |
| 313 | if (id_obj == NULL) { |
| 314 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 315 | PyErr_SetString(PyExc_ValueError, |
| 316 | "Filter specifier must have an \"id\" entry"); |
| 317 | return 0; |
| 318 | } |
| 319 | f->id = PyLong_AsUnsignedLongLong(id_obj); |
| 320 | Py_DECREF(id_obj); |
| 321 | if (PyErr_Occurred()) |
| 322 | return 0; |
| 323 | |
| 324 | switch (f->id) { |
| 325 | case LZMA_FILTER_LZMA1: |
| 326 | case LZMA_FILTER_LZMA2: |
| 327 | f->options = parse_filter_spec_lzma(spec); |
| 328 | return f->options != NULL; |
| 329 | case LZMA_FILTER_DELTA: |
| 330 | f->options = parse_filter_spec_delta(spec); |
| 331 | return f->options != NULL; |
| 332 | case LZMA_FILTER_X86: |
| 333 | case LZMA_FILTER_POWERPC: |
| 334 | case LZMA_FILTER_IA64: |
| 335 | case LZMA_FILTER_ARM: |
| 336 | case LZMA_FILTER_ARMTHUMB: |
| 337 | case LZMA_FILTER_SPARC: |
| 338 | f->options = parse_filter_spec_bcj(spec); |
| 339 | return f->options != NULL; |
| 340 | default: |
| 341 | PyErr_Format(PyExc_ValueError, "Invalid filter ID: %llu", f->id); |
| 342 | return 0; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | static void |
| 347 | free_filter_chain(lzma_filter filters[]) |
| 348 | { |
| 349 | int i; |
| 350 | |
| 351 | for (i = 0; filters[i].id != LZMA_VLI_UNKNOWN; i++) |
| 352 | PyMem_Free(filters[i].options); |
| 353 | } |
| 354 | |
| 355 | static int |
| 356 | parse_filter_chain_spec(lzma_filter filters[], PyObject *filterspecs) |
| 357 | { |
| 358 | Py_ssize_t i, num_filters; |
| 359 | |
| 360 | num_filters = PySequence_Length(filterspecs); |
| 361 | if (num_filters == -1) |
| 362 | return -1; |
| 363 | if (num_filters > LZMA_FILTERS_MAX) { |
| 364 | PyErr_Format(PyExc_ValueError, |
| 365 | "Too many filters - liblzma supports a maximum of %d", |
| 366 | LZMA_FILTERS_MAX); |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | for (i = 0; i < num_filters; i++) { |
| 371 | int ok = 1; |
| 372 | PyObject *spec = PySequence_GetItem(filterspecs, i); |
| 373 | if (spec == NULL || !lzma_filter_converter(spec, &filters[i])) |
| 374 | ok = 0; |
| 375 | Py_XDECREF(spec); |
| 376 | if (!ok) { |
| 377 | filters[i].id = LZMA_VLI_UNKNOWN; |
| 378 | free_filter_chain(filters); |
| 379 | return -1; |
| 380 | } |
| 381 | } |
| 382 | filters[num_filters].id = LZMA_VLI_UNKNOWN; |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | /* Filter specifier construction. |
| 388 | |
| 389 | This code handles converting C lzma_filter structs into |
| 390 | Python-level filter specifiers (represented as dicts). */ |
| 391 | |
| 392 | static int |
| 393 | spec_add_field(PyObject *spec, _Py_Identifier *key, unsigned PY_LONG_LONG value) |
| 394 | { |
| 395 | int status; |
| 396 | PyObject *value_object; |
| 397 | |
| 398 | value_object = PyLong_FromUnsignedLongLong(value); |
| 399 | if (value_object == NULL) |
| 400 | return -1; |
| 401 | |
| 402 | status = _PyDict_SetItemId(spec, key, value_object); |
| 403 | Py_DECREF(value_object); |
| 404 | return status; |
| 405 | } |
| 406 | |
| 407 | static PyObject * |
| 408 | build_filter_spec(const lzma_filter *f) |
| 409 | { |
| 410 | PyObject *spec; |
| 411 | |
| 412 | spec = PyDict_New(); |
| 413 | if (spec == NULL) |
| 414 | return NULL; |
| 415 | |
| 416 | #define ADD_FIELD(SOURCE, FIELD) \ |
| 417 | do { \ |
| 418 | _Py_IDENTIFIER(FIELD); \ |
| 419 | if (spec_add_field(spec, &PyId_##FIELD, SOURCE->FIELD) == -1) \ |
| 420 | goto error;\ |
| 421 | } while (0) |
| 422 | |
| 423 | ADD_FIELD(f, id); |
| 424 | |
| 425 | switch (f->id) { |
| 426 | /* For LZMA1 filters, lzma_properties_{encode,decode}() only look at the |
| 427 | lc, lp, pb, and dict_size fields. For LZMA2 filters, only the |
| 428 | dict_size field is used. */ |
| 429 | case LZMA_FILTER_LZMA1: { |
| 430 | lzma_options_lzma *options = f->options; |
| 431 | ADD_FIELD(options, lc); |
| 432 | ADD_FIELD(options, lp); |
| 433 | ADD_FIELD(options, pb); |
| 434 | ADD_FIELD(options, dict_size); |
| 435 | break; |
| 436 | } |
| 437 | case LZMA_FILTER_LZMA2: { |
| 438 | lzma_options_lzma *options = f->options; |
| 439 | ADD_FIELD(options, dict_size); |
| 440 | break; |
| 441 | } |
| 442 | case LZMA_FILTER_DELTA: { |
| 443 | lzma_options_delta *options = f->options; |
| 444 | ADD_FIELD(options, dist); |
| 445 | break; |
| 446 | } |
| 447 | case LZMA_FILTER_X86: |
| 448 | case LZMA_FILTER_POWERPC: |
| 449 | case LZMA_FILTER_IA64: |
| 450 | case LZMA_FILTER_ARM: |
| 451 | case LZMA_FILTER_ARMTHUMB: |
| 452 | case LZMA_FILTER_SPARC: { |
| 453 | lzma_options_bcj *options = f->options; |
| 454 | ADD_FIELD(options, start_offset); |
| 455 | break; |
| 456 | } |
| 457 | default: |
| 458 | PyErr_Format(PyExc_ValueError, "Invalid filter ID: %llu", f->id); |
| 459 | goto error; |
| 460 | } |
| 461 | |
| 462 | #undef ADD_FIELD |
| 463 | |
| 464 | return spec; |
| 465 | |
| 466 | error: |
| 467 | Py_DECREF(spec); |
| 468 | return NULL; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | /*[clinic input] |
| 473 | output preset file |
| 474 | module _lzma |
| 475 | class _lzma.LZMACompressor "Compressor *" "&Compressor_type" |
| 476 | class _lzma.LZMADecompressor "Decompressor *" "&Decompressor_type" |
| 477 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 478 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=f17afc786525d6c2]*/ |
Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 479 | |
| 480 | #include "clinic/_lzmamodule.c.h" |
| 481 | |
| 482 | /*[python input] |
| 483 | |
| 484 | class lzma_vli_converter(CConverter): |
| 485 | type = 'lzma_vli' |
| 486 | converter = 'lzma_vli_converter' |
| 487 | |
| 488 | class lzma_filter_converter(CConverter): |
| 489 | type = 'lzma_filter' |
| 490 | converter = 'lzma_filter_converter' |
| 491 | c_default = c_ignored_default = "{LZMA_VLI_UNKNOWN, NULL}" |
| 492 | |
| 493 | def cleanup(self): |
| 494 | name = ensure_legal_c_identifier(self.name) |
| 495 | return ('if (%(name)s.id != LZMA_VLI_UNKNOWN)\n' |
| 496 | ' PyMem_Free(%(name)s.options);\n') % {'name': name} |
| 497 | |
| 498 | [python start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 499 | /*[python end generated code: output=da39a3ee5e6b4b0d input=74fe7631ce377a94]*/ |
Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 500 | |
| 501 | |
| 502 | /* LZMACompressor class. */ |
| 503 | |
| 504 | static PyObject * |
| 505 | compress(Compressor *c, uint8_t *data, size_t len, lzma_action action) |
| 506 | { |
| 507 | size_t data_size = 0; |
| 508 | PyObject *result; |
| 509 | |
| 510 | result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE); |
| 511 | if (result == NULL) |
| 512 | return NULL; |
| 513 | c->lzs.next_in = data; |
| 514 | c->lzs.avail_in = len; |
| 515 | c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result); |
| 516 | c->lzs.avail_out = PyBytes_GET_SIZE(result); |
| 517 | for (;;) { |
| 518 | lzma_ret lzret; |
| 519 | |
| 520 | Py_BEGIN_ALLOW_THREADS |
| 521 | lzret = lzma_code(&c->lzs, action); |
| 522 | data_size = (char *)c->lzs.next_out - PyBytes_AS_STRING(result); |
| 523 | Py_END_ALLOW_THREADS |
| 524 | if (catch_lzma_error(lzret)) |
| 525 | goto error; |
| 526 | if ((action == LZMA_RUN && c->lzs.avail_in == 0) || |
| 527 | (action == LZMA_FINISH && lzret == LZMA_STREAM_END)) { |
| 528 | break; |
| 529 | } else if (c->lzs.avail_out == 0) { |
| 530 | if (grow_buffer(&result) == -1) |
| 531 | goto error; |
| 532 | c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size; |
| 533 | c->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size; |
| 534 | } |
| 535 | } |
| 536 | if (data_size != PyBytes_GET_SIZE(result)) |
| 537 | if (_PyBytes_Resize(&result, data_size) == -1) |
| 538 | goto error; |
| 539 | return result; |
| 540 | |
| 541 | error: |
| 542 | Py_XDECREF(result); |
| 543 | return NULL; |
| 544 | } |
| 545 | |
| 546 | /*[clinic input] |
| 547 | _lzma.LZMACompressor.compress |
| 548 | |
| 549 | self: self(type="Compressor *") |
| 550 | data: Py_buffer |
| 551 | / |
| 552 | |
| 553 | Provide data to the compressor object. |
| 554 | |
| 555 | Returns a chunk of compressed data if possible, or b'' otherwise. |
| 556 | |
| 557 | When you have finished providing data to the compressor, call the |
| 558 | flush() method to finish the compression process. |
| 559 | [clinic start generated code]*/ |
| 560 | |
| 561 | static PyObject * |
| 562 | _lzma_LZMACompressor_compress_impl(Compressor *self, Py_buffer *data) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 563 | /*[clinic end generated code: output=31f615136963e00f input=8b60cb13e0ce6420]*/ |
Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 564 | { |
| 565 | PyObject *result = NULL; |
| 566 | |
| 567 | ACQUIRE_LOCK(self); |
| 568 | if (self->flushed) |
| 569 | PyErr_SetString(PyExc_ValueError, "Compressor has been flushed"); |
| 570 | else |
| 571 | result = compress(self, data->buf, data->len, LZMA_RUN); |
| 572 | RELEASE_LOCK(self); |
| 573 | return result; |
| 574 | } |
| 575 | |
| 576 | /*[clinic input] |
| 577 | _lzma.LZMACompressor.flush |
| 578 | |
| 579 | self: self(type="Compressor *") |
| 580 | |
| 581 | Finish the compression process. |
| 582 | |
| 583 | Returns the compressed data left in internal buffers. |
| 584 | |
| 585 | The compressor object may not be used after this method is called. |
| 586 | [clinic start generated code]*/ |
| 587 | |
| 588 | static PyObject * |
| 589 | _lzma_LZMACompressor_flush_impl(Compressor *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 590 | /*[clinic end generated code: output=fec21f3e22504f50 input=3060fb26f9b4042c]*/ |
Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 591 | { |
| 592 | PyObject *result = NULL; |
| 593 | |
| 594 | ACQUIRE_LOCK(self); |
| 595 | if (self->flushed) { |
| 596 | PyErr_SetString(PyExc_ValueError, "Repeated call to flush()"); |
| 597 | } else { |
| 598 | self->flushed = 1; |
| 599 | result = compress(self, NULL, 0, LZMA_FINISH); |
| 600 | } |
| 601 | RELEASE_LOCK(self); |
| 602 | return result; |
| 603 | } |
| 604 | |
| 605 | static PyObject * |
| 606 | Compressor_getstate(Compressor *self, PyObject *noargs) |
| 607 | { |
| 608 | PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object", |
| 609 | Py_TYPE(self)->tp_name); |
| 610 | return NULL; |
| 611 | } |
| 612 | |
| 613 | static int |
| 614 | Compressor_init_xz(lzma_stream *lzs, int check, uint32_t preset, |
| 615 | PyObject *filterspecs) |
| 616 | { |
| 617 | lzma_ret lzret; |
| 618 | |
| 619 | if (filterspecs == Py_None) { |
| 620 | lzret = lzma_easy_encoder(lzs, preset, check); |
| 621 | } else { |
| 622 | lzma_filter filters[LZMA_FILTERS_MAX + 1]; |
| 623 | |
| 624 | if (parse_filter_chain_spec(filters, filterspecs) == -1) |
| 625 | return -1; |
| 626 | lzret = lzma_stream_encoder(lzs, filters, check); |
| 627 | free_filter_chain(filters); |
| 628 | } |
| 629 | if (catch_lzma_error(lzret)) |
| 630 | return -1; |
| 631 | else |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static int |
| 636 | Compressor_init_alone(lzma_stream *lzs, uint32_t preset, PyObject *filterspecs) |
| 637 | { |
| 638 | lzma_ret lzret; |
| 639 | |
| 640 | if (filterspecs == Py_None) { |
| 641 | lzma_options_lzma options; |
| 642 | |
| 643 | if (lzma_lzma_preset(&options, preset)) { |
| 644 | PyErr_Format(Error, "Invalid compression preset: %d", preset); |
| 645 | return -1; |
| 646 | } |
| 647 | lzret = lzma_alone_encoder(lzs, &options); |
| 648 | } else { |
| 649 | lzma_filter filters[LZMA_FILTERS_MAX + 1]; |
| 650 | |
| 651 | if (parse_filter_chain_spec(filters, filterspecs) == -1) |
| 652 | return -1; |
| 653 | if (filters[0].id == LZMA_FILTER_LZMA1 && |
| 654 | filters[1].id == LZMA_VLI_UNKNOWN) { |
| 655 | lzret = lzma_alone_encoder(lzs, filters[0].options); |
| 656 | } else { |
| 657 | PyErr_SetString(PyExc_ValueError, |
| 658 | "Invalid filter chain for FORMAT_ALONE - " |
| 659 | "must be a single LZMA1 filter"); |
| 660 | lzret = LZMA_PROG_ERROR; |
| 661 | } |
| 662 | free_filter_chain(filters); |
| 663 | } |
| 664 | if (PyErr_Occurred() || catch_lzma_error(lzret)) |
| 665 | return -1; |
| 666 | else |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static int |
| 671 | Compressor_init_raw(lzma_stream *lzs, PyObject *filterspecs) |
| 672 | { |
| 673 | lzma_filter filters[LZMA_FILTERS_MAX + 1]; |
| 674 | lzma_ret lzret; |
| 675 | |
| 676 | if (filterspecs == Py_None) { |
| 677 | PyErr_SetString(PyExc_ValueError, |
| 678 | "Must specify filters for FORMAT_RAW"); |
| 679 | return -1; |
| 680 | } |
| 681 | if (parse_filter_chain_spec(filters, filterspecs) == -1) |
| 682 | return -1; |
| 683 | lzret = lzma_raw_encoder(lzs, filters); |
| 684 | free_filter_chain(filters); |
| 685 | if (catch_lzma_error(lzret)) |
| 686 | return -1; |
| 687 | else |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | /*[-clinic input] |
| 692 | _lzma.LZMACompressor.__init__ |
| 693 | |
| 694 | self: self(type="Compressor *") |
| 695 | format: int(c_default="FORMAT_XZ") = FORMAT_XZ |
| 696 | The container format to use for the output. This can |
| 697 | be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW. |
| 698 | |
| 699 | check: int(c_default="-1") = unspecified |
| 700 | The integrity check to use. For FORMAT_XZ, the default |
| 701 | is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not suport integrity |
| 702 | checks; for these formats, check must be omitted, or be CHECK_NONE. |
| 703 | |
| 704 | preset: object = None |
| 705 | If provided should be an integer in the range 0-9, optionally |
| 706 | OR-ed with the constant PRESET_EXTREME. |
| 707 | |
| 708 | filters: object = None |
| 709 | If provided should be a sequence of dicts. Each dict should |
| 710 | have an entry for "id" indicating the ID of the filter, plus |
| 711 | additional entries for options to the filter. |
| 712 | |
| 713 | Create a compressor object for compressing data incrementally. |
| 714 | |
| 715 | The settings used by the compressor can be specified either as a |
| 716 | preset compression level (with the 'preset' argument), or in detail |
| 717 | as a custom filter chain (with the 'filters' argument). For FORMAT_XZ |
| 718 | and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset |
| 719 | level. For FORMAT_RAW, the caller must always specify a filter chain; |
| 720 | the raw compressor does not support preset compression levels. |
| 721 | |
| 722 | For one-shot compression, use the compress() function instead. |
| 723 | [-clinic start generated code]*/ |
| 724 | static int |
| 725 | Compressor_init(Compressor *self, PyObject *args, PyObject *kwargs) |
| 726 | { |
| 727 | static char *arg_names[] = {"format", "check", "preset", "filters", NULL}; |
| 728 | int format = FORMAT_XZ; |
| 729 | int check = -1; |
| 730 | uint32_t preset = LZMA_PRESET_DEFAULT; |
| 731 | PyObject *preset_obj = Py_None; |
| 732 | PyObject *filterspecs = Py_None; |
| 733 | |
| 734 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 735 | "|iiOO:LZMACompressor", arg_names, |
| 736 | &format, &check, &preset_obj, |
| 737 | &filterspecs)) |
| 738 | return -1; |
| 739 | |
| 740 | if (format != FORMAT_XZ && check != -1 && check != LZMA_CHECK_NONE) { |
| 741 | PyErr_SetString(PyExc_ValueError, |
| 742 | "Integrity checks are only supported by FORMAT_XZ"); |
| 743 | return -1; |
| 744 | } |
| 745 | |
| 746 | if (preset_obj != Py_None && filterspecs != Py_None) { |
| 747 | PyErr_SetString(PyExc_ValueError, |
| 748 | "Cannot specify both preset and filter chain"); |
| 749 | return -1; |
| 750 | } |
| 751 | |
| 752 | if (preset_obj != Py_None) |
| 753 | if (!uint32_converter(preset_obj, &preset)) |
| 754 | return -1; |
| 755 | |
| 756 | self->alloc.opaque = NULL; |
| 757 | self->alloc.alloc = PyLzma_Malloc; |
| 758 | self->alloc.free = PyLzma_Free; |
| 759 | self->lzs.allocator = &self->alloc; |
| 760 | |
| 761 | #ifdef WITH_THREAD |
| 762 | self->lock = PyThread_allocate_lock(); |
| 763 | if (self->lock == NULL) { |
| 764 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock"); |
| 765 | return -1; |
| 766 | } |
| 767 | #endif |
| 768 | |
| 769 | self->flushed = 0; |
| 770 | switch (format) { |
| 771 | case FORMAT_XZ: |
| 772 | if (check == -1) |
| 773 | check = LZMA_CHECK_CRC64; |
| 774 | if (Compressor_init_xz(&self->lzs, check, preset, filterspecs) != 0) |
| 775 | break; |
| 776 | return 0; |
| 777 | |
| 778 | case FORMAT_ALONE: |
| 779 | if (Compressor_init_alone(&self->lzs, preset, filterspecs) != 0) |
| 780 | break; |
| 781 | return 0; |
| 782 | |
| 783 | case FORMAT_RAW: |
| 784 | if (Compressor_init_raw(&self->lzs, filterspecs) != 0) |
| 785 | break; |
| 786 | return 0; |
| 787 | |
| 788 | default: |
| 789 | PyErr_Format(PyExc_ValueError, |
| 790 | "Invalid container format: %d", format); |
| 791 | break; |
| 792 | } |
| 793 | |
| 794 | #ifdef WITH_THREAD |
| 795 | PyThread_free_lock(self->lock); |
| 796 | self->lock = NULL; |
| 797 | #endif |
| 798 | return -1; |
| 799 | } |
| 800 | |
| 801 | static void |
| 802 | Compressor_dealloc(Compressor *self) |
| 803 | { |
| 804 | lzma_end(&self->lzs); |
| 805 | #ifdef WITH_THREAD |
| 806 | if (self->lock != NULL) |
| 807 | PyThread_free_lock(self->lock); |
| 808 | #endif |
| 809 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 810 | } |
| 811 | |
| 812 | static PyMethodDef Compressor_methods[] = { |
| 813 | _LZMA_LZMACOMPRESSOR_COMPRESS_METHODDEF |
| 814 | _LZMA_LZMACOMPRESSOR_FLUSH_METHODDEF |
| 815 | {"__getstate__", (PyCFunction)Compressor_getstate, METH_NOARGS}, |
| 816 | {NULL} |
| 817 | }; |
| 818 | |
| 819 | PyDoc_STRVAR(Compressor_doc, |
| 820 | "LZMACompressor(format=FORMAT_XZ, check=-1, preset=None, filters=None)\n" |
| 821 | "\n" |
| 822 | "Create a compressor object for compressing data incrementally.\n" |
| 823 | "\n" |
| 824 | "format specifies the container format to use for the output. This can\n" |
| 825 | "be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW.\n" |
| 826 | "\n" |
| 827 | "check specifies the integrity check to use. For FORMAT_XZ, the default\n" |
| 828 | "is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not suport integrity\n" |
| 829 | "checks; for these formats, check must be omitted, or be CHECK_NONE.\n" |
| 830 | "\n" |
| 831 | "The settings used by the compressor can be specified either as a\n" |
| 832 | "preset compression level (with the 'preset' argument), or in detail\n" |
| 833 | "as a custom filter chain (with the 'filters' argument). For FORMAT_XZ\n" |
| 834 | "and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset\n" |
| 835 | "level. For FORMAT_RAW, the caller must always specify a filter chain;\n" |
| 836 | "the raw compressor does not support preset compression levels.\n" |
| 837 | "\n" |
| 838 | "preset (if provided) should be an integer in the range 0-9, optionally\n" |
| 839 | "OR-ed with the constant PRESET_EXTREME.\n" |
| 840 | "\n" |
| 841 | "filters (if provided) should be a sequence of dicts. Each dict should\n" |
| 842 | "have an entry for \"id\" indicating the ID of the filter, plus\n" |
| 843 | "additional entries for options to the filter.\n" |
| 844 | "\n" |
| 845 | "For one-shot compression, use the compress() function instead.\n"); |
| 846 | |
| 847 | static PyTypeObject Compressor_type = { |
| 848 | PyVarObject_HEAD_INIT(NULL, 0) |
| 849 | "_lzma.LZMACompressor", /* tp_name */ |
| 850 | sizeof(Compressor), /* tp_basicsize */ |
| 851 | 0, /* tp_itemsize */ |
| 852 | (destructor)Compressor_dealloc, /* tp_dealloc */ |
| 853 | 0, /* tp_print */ |
| 854 | 0, /* tp_getattr */ |
| 855 | 0, /* tp_setattr */ |
| 856 | 0, /* tp_reserved */ |
| 857 | 0, /* tp_repr */ |
| 858 | 0, /* tp_as_number */ |
| 859 | 0, /* tp_as_sequence */ |
| 860 | 0, /* tp_as_mapping */ |
| 861 | 0, /* tp_hash */ |
| 862 | 0, /* tp_call */ |
| 863 | 0, /* tp_str */ |
| 864 | 0, /* tp_getattro */ |
| 865 | 0, /* tp_setattro */ |
| 866 | 0, /* tp_as_buffer */ |
| 867 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 868 | Compressor_doc, /* tp_doc */ |
| 869 | 0, /* tp_traverse */ |
| 870 | 0, /* tp_clear */ |
| 871 | 0, /* tp_richcompare */ |
| 872 | 0, /* tp_weaklistoffset */ |
| 873 | 0, /* tp_iter */ |
| 874 | 0, /* tp_iternext */ |
| 875 | Compressor_methods, /* tp_methods */ |
| 876 | 0, /* tp_members */ |
| 877 | 0, /* tp_getset */ |
| 878 | 0, /* tp_base */ |
| 879 | 0, /* tp_dict */ |
| 880 | 0, /* tp_descr_get */ |
| 881 | 0, /* tp_descr_set */ |
| 882 | 0, /* tp_dictoffset */ |
| 883 | (initproc)Compressor_init, /* tp_init */ |
| 884 | 0, /* tp_alloc */ |
| 885 | PyType_GenericNew, /* tp_new */ |
| 886 | }; |
| 887 | |
| 888 | |
| 889 | /* LZMADecompressor class. */ |
| 890 | |
| 891 | static PyObject * |
| 892 | decompress(Decompressor *d, uint8_t *data, size_t len) |
| 893 | { |
| 894 | size_t data_size = 0; |
| 895 | PyObject *result; |
| 896 | |
| 897 | result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE); |
| 898 | if (result == NULL) |
| 899 | return NULL; |
| 900 | d->lzs.next_in = data; |
| 901 | d->lzs.avail_in = len; |
| 902 | d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result); |
| 903 | d->lzs.avail_out = PyBytes_GET_SIZE(result); |
| 904 | for (;;) { |
| 905 | lzma_ret lzret; |
| 906 | |
| 907 | Py_BEGIN_ALLOW_THREADS |
| 908 | lzret = lzma_code(&d->lzs, LZMA_RUN); |
| 909 | data_size = (char *)d->lzs.next_out - PyBytes_AS_STRING(result); |
| 910 | Py_END_ALLOW_THREADS |
| 911 | if (catch_lzma_error(lzret)) |
| 912 | goto error; |
| 913 | if (lzret == LZMA_GET_CHECK || lzret == LZMA_NO_CHECK) |
| 914 | d->check = lzma_get_check(&d->lzs); |
| 915 | if (lzret == LZMA_STREAM_END) { |
| 916 | d->eof = 1; |
| 917 | if (d->lzs.avail_in > 0) { |
| 918 | Py_CLEAR(d->unused_data); |
| 919 | d->unused_data = PyBytes_FromStringAndSize( |
| 920 | (char *)d->lzs.next_in, d->lzs.avail_in); |
| 921 | if (d->unused_data == NULL) |
| 922 | goto error; |
| 923 | } |
| 924 | break; |
| 925 | } else if (d->lzs.avail_in == 0) { |
| 926 | break; |
| 927 | } else if (d->lzs.avail_out == 0) { |
| 928 | if (grow_buffer(&result) == -1) |
| 929 | goto error; |
| 930 | d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size; |
| 931 | d->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size; |
| 932 | } |
| 933 | } |
| 934 | if (data_size != PyBytes_GET_SIZE(result)) |
| 935 | if (_PyBytes_Resize(&result, data_size) == -1) |
| 936 | goto error; |
| 937 | return result; |
| 938 | |
| 939 | error: |
| 940 | Py_XDECREF(result); |
| 941 | return NULL; |
| 942 | } |
| 943 | |
| 944 | /*[clinic input] |
| 945 | _lzma.LZMADecompressor.decompress |
| 946 | |
| 947 | self: self(type="Decompressor *") |
| 948 | data: Py_buffer |
| 949 | / |
| 950 | |
| 951 | Provide data to the decompressor object. |
| 952 | |
| 953 | Returns a chunk of decompressed data if possible, or b'' otherwise. |
| 954 | |
| 955 | Attempting to decompress data after the end of stream is reached |
| 956 | raises an EOFError. Any data found after the end of the stream |
| 957 | is ignored and saved in the unused_data attribute. |
| 958 | [clinic start generated code]*/ |
| 959 | |
| 960 | static PyObject * |
| 961 | _lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 962 | /*[clinic end generated code: output=d86e78da7ff0ff21 input=50c4768b821bf0ef]*/ |
Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 963 | { |
| 964 | PyObject *result = NULL; |
| 965 | |
| 966 | ACQUIRE_LOCK(self); |
| 967 | if (self->eof) |
| 968 | PyErr_SetString(PyExc_EOFError, "Already at end of stream"); |
| 969 | else |
| 970 | result = decompress(self, data->buf, data->len); |
| 971 | RELEASE_LOCK(self); |
| 972 | return result; |
| 973 | } |
| 974 | |
| 975 | static PyObject * |
| 976 | Decompressor_getstate(Decompressor *self, PyObject *noargs) |
| 977 | { |
| 978 | PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object", |
| 979 | Py_TYPE(self)->tp_name); |
| 980 | return NULL; |
| 981 | } |
| 982 | |
| 983 | static int |
| 984 | Decompressor_init_raw(lzma_stream *lzs, PyObject *filterspecs) |
| 985 | { |
| 986 | lzma_filter filters[LZMA_FILTERS_MAX + 1]; |
| 987 | lzma_ret lzret; |
| 988 | |
| 989 | if (parse_filter_chain_spec(filters, filterspecs) == -1) |
| 990 | return -1; |
| 991 | lzret = lzma_raw_decoder(lzs, filters); |
| 992 | free_filter_chain(filters); |
| 993 | if (catch_lzma_error(lzret)) |
| 994 | return -1; |
| 995 | else |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | /*[clinic input] |
| 1000 | _lzma.LZMADecompressor.__init__ |
| 1001 | |
| 1002 | self: self(type="Decompressor *") |
| 1003 | format: int(c_default="FORMAT_AUTO") = FORMAT_AUTO |
| 1004 | Specifies the container format of the input stream. If this is |
| 1005 | FORMAT_AUTO (the default), the decompressor will automatically detect |
| 1006 | whether the input is FORMAT_XZ or FORMAT_ALONE. Streams created with |
| 1007 | FORMAT_RAW cannot be autodetected. |
| 1008 | |
| 1009 | memlimit: object = None |
| 1010 | Limit the amount of memory used by the decompressor. This will cause |
| 1011 | decompression to fail if the input cannot be decompressed within the |
| 1012 | given limit. |
| 1013 | |
| 1014 | filters: object = None |
| 1015 | A custom filter chain. This argument is required for FORMAT_RAW, and |
| 1016 | not accepted with any other format. When provided, this should be a |
| 1017 | sequence of dicts, each indicating the ID and options for a single |
| 1018 | filter. |
| 1019 | |
| 1020 | Create a decompressor object for decompressing data incrementally. |
| 1021 | |
| 1022 | For one-shot decompression, use the decompress() function instead. |
| 1023 | [clinic start generated code]*/ |
| 1024 | |
| 1025 | static int |
| 1026 | _lzma_LZMADecompressor___init___impl(Decompressor *self, int format, PyObject *memlimit, PyObject *filters) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1027 | /*[clinic end generated code: output=9b119f6f2cc2d7a8 input=458ca6132ef29801]*/ |
Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 1028 | { |
| 1029 | const uint32_t decoder_flags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK; |
| 1030 | uint64_t memlimit_ = UINT64_MAX; |
| 1031 | lzma_ret lzret; |
| 1032 | |
| 1033 | if (memlimit != Py_None) { |
| 1034 | if (format == FORMAT_RAW) { |
| 1035 | PyErr_SetString(PyExc_ValueError, |
| 1036 | "Cannot specify memory limit with FORMAT_RAW"); |
| 1037 | return -1; |
| 1038 | } |
| 1039 | memlimit_ = PyLong_AsUnsignedLongLong(memlimit); |
| 1040 | if (PyErr_Occurred()) |
| 1041 | return -1; |
| 1042 | } |
| 1043 | |
| 1044 | if (format == FORMAT_RAW && filters == Py_None) { |
| 1045 | PyErr_SetString(PyExc_ValueError, |
| 1046 | "Must specify filters for FORMAT_RAW"); |
| 1047 | return -1; |
| 1048 | } else if (format != FORMAT_RAW && filters != Py_None) { |
| 1049 | PyErr_SetString(PyExc_ValueError, |
| 1050 | "Cannot specify filters except with FORMAT_RAW"); |
| 1051 | return -1; |
| 1052 | } |
| 1053 | |
| 1054 | self->alloc.opaque = NULL; |
| 1055 | self->alloc.alloc = PyLzma_Malloc; |
| 1056 | self->alloc.free = PyLzma_Free; |
| 1057 | self->lzs.allocator = &self->alloc; |
| 1058 | |
| 1059 | #ifdef WITH_THREAD |
| 1060 | self->lock = PyThread_allocate_lock(); |
| 1061 | if (self->lock == NULL) { |
| 1062 | PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock"); |
| 1063 | return -1; |
| 1064 | } |
| 1065 | #endif |
| 1066 | |
| 1067 | self->check = LZMA_CHECK_UNKNOWN; |
| 1068 | self->unused_data = PyBytes_FromStringAndSize(NULL, 0); |
| 1069 | if (self->unused_data == NULL) |
| 1070 | goto error; |
| 1071 | |
| 1072 | switch (format) { |
| 1073 | case FORMAT_AUTO: |
| 1074 | lzret = lzma_auto_decoder(&self->lzs, memlimit_, decoder_flags); |
| 1075 | if (catch_lzma_error(lzret)) |
| 1076 | break; |
| 1077 | return 0; |
| 1078 | |
| 1079 | case FORMAT_XZ: |
| 1080 | lzret = lzma_stream_decoder(&self->lzs, memlimit_, decoder_flags); |
| 1081 | if (catch_lzma_error(lzret)) |
| 1082 | break; |
| 1083 | return 0; |
| 1084 | |
| 1085 | case FORMAT_ALONE: |
| 1086 | self->check = LZMA_CHECK_NONE; |
| 1087 | lzret = lzma_alone_decoder(&self->lzs, memlimit_); |
| 1088 | if (catch_lzma_error(lzret)) |
| 1089 | break; |
| 1090 | return 0; |
| 1091 | |
| 1092 | case FORMAT_RAW: |
| 1093 | self->check = LZMA_CHECK_NONE; |
| 1094 | if (Decompressor_init_raw(&self->lzs, filters) == -1) |
| 1095 | break; |
| 1096 | return 0; |
| 1097 | |
| 1098 | default: |
| 1099 | PyErr_Format(PyExc_ValueError, |
| 1100 | "Invalid container format: %d", format); |
| 1101 | break; |
| 1102 | } |
| 1103 | |
| 1104 | error: |
| 1105 | Py_CLEAR(self->unused_data); |
| 1106 | #ifdef WITH_THREAD |
| 1107 | PyThread_free_lock(self->lock); |
| 1108 | self->lock = NULL; |
| 1109 | #endif |
| 1110 | return -1; |
| 1111 | } |
| 1112 | |
| 1113 | static void |
| 1114 | Decompressor_dealloc(Decompressor *self) |
| 1115 | { |
| 1116 | lzma_end(&self->lzs); |
| 1117 | Py_CLEAR(self->unused_data); |
| 1118 | #ifdef WITH_THREAD |
| 1119 | if (self->lock != NULL) |
| 1120 | PyThread_free_lock(self->lock); |
| 1121 | #endif |
| 1122 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 1123 | } |
| 1124 | |
| 1125 | static PyMethodDef Decompressor_methods[] = { |
| 1126 | _LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF |
| 1127 | {"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS}, |
| 1128 | {NULL} |
| 1129 | }; |
| 1130 | |
| 1131 | PyDoc_STRVAR(Decompressor_check_doc, |
| 1132 | "ID of the integrity check used by the input stream."); |
| 1133 | |
| 1134 | PyDoc_STRVAR(Decompressor_eof_doc, |
| 1135 | "True if the end-of-stream marker has been reached."); |
| 1136 | |
| 1137 | PyDoc_STRVAR(Decompressor_unused_data_doc, |
| 1138 | "Data found after the end of the compressed stream."); |
| 1139 | |
| 1140 | static PyMemberDef Decompressor_members[] = { |
| 1141 | {"check", T_INT, offsetof(Decompressor, check), READONLY, |
| 1142 | Decompressor_check_doc}, |
| 1143 | {"eof", T_BOOL, offsetof(Decompressor, eof), READONLY, |
| 1144 | Decompressor_eof_doc}, |
| 1145 | {"unused_data", T_OBJECT_EX, offsetof(Decompressor, unused_data), READONLY, |
| 1146 | Decompressor_unused_data_doc}, |
| 1147 | {NULL} |
| 1148 | }; |
| 1149 | |
| 1150 | static PyTypeObject Decompressor_type = { |
| 1151 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1152 | "_lzma.LZMADecompressor", /* tp_name */ |
| 1153 | sizeof(Decompressor), /* tp_basicsize */ |
| 1154 | 0, /* tp_itemsize */ |
| 1155 | (destructor)Decompressor_dealloc, /* tp_dealloc */ |
| 1156 | 0, /* tp_print */ |
| 1157 | 0, /* tp_getattr */ |
| 1158 | 0, /* tp_setattr */ |
| 1159 | 0, /* tp_reserved */ |
| 1160 | 0, /* tp_repr */ |
| 1161 | 0, /* tp_as_number */ |
| 1162 | 0, /* tp_as_sequence */ |
| 1163 | 0, /* tp_as_mapping */ |
| 1164 | 0, /* tp_hash */ |
| 1165 | 0, /* tp_call */ |
| 1166 | 0, /* tp_str */ |
| 1167 | 0, /* tp_getattro */ |
| 1168 | 0, /* tp_setattro */ |
| 1169 | 0, /* tp_as_buffer */ |
| 1170 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1171 | _lzma_LZMADecompressor___init____doc__, /* tp_doc */ |
| 1172 | 0, /* tp_traverse */ |
| 1173 | 0, /* tp_clear */ |
| 1174 | 0, /* tp_richcompare */ |
| 1175 | 0, /* tp_weaklistoffset */ |
| 1176 | 0, /* tp_iter */ |
| 1177 | 0, /* tp_iternext */ |
| 1178 | Decompressor_methods, /* tp_methods */ |
| 1179 | Decompressor_members, /* tp_members */ |
| 1180 | 0, /* tp_getset */ |
| 1181 | 0, /* tp_base */ |
| 1182 | 0, /* tp_dict */ |
| 1183 | 0, /* tp_descr_get */ |
| 1184 | 0, /* tp_descr_set */ |
| 1185 | 0, /* tp_dictoffset */ |
| 1186 | _lzma_LZMADecompressor___init__, /* tp_init */ |
| 1187 | 0, /* tp_alloc */ |
| 1188 | PyType_GenericNew, /* tp_new */ |
| 1189 | }; |
| 1190 | |
| 1191 | |
| 1192 | /* Module-level functions. */ |
| 1193 | |
| 1194 | /*[clinic input] |
| 1195 | _lzma.is_check_supported |
| 1196 | check_id: int |
| 1197 | / |
| 1198 | |
| 1199 | Test whether the given integrity check is supported. |
| 1200 | |
| 1201 | Always returns True for CHECK_NONE and CHECK_CRC32. |
| 1202 | [clinic start generated code]*/ |
| 1203 | |
| 1204 | static PyObject * |
| 1205 | _lzma_is_check_supported_impl(PyModuleDef *module, int check_id) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1206 | /*[clinic end generated code: output=bb828e90e00ad96e input=5518297b97b2318f]*/ |
Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 1207 | { |
| 1208 | return PyBool_FromLong(lzma_check_is_supported(check_id)); |
| 1209 | } |
| 1210 | |
| 1211 | |
| 1212 | /*[clinic input] |
| 1213 | _lzma._encode_filter_properties |
| 1214 | filter: lzma_filter(c_default="{LZMA_VLI_UNKNOWN, NULL}") |
| 1215 | / |
| 1216 | |
| 1217 | Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict). |
| 1218 | |
| 1219 | The result does not include the filter ID itself, only the options. |
| 1220 | [clinic start generated code]*/ |
| 1221 | |
| 1222 | static PyObject * |
| 1223 | _lzma__encode_filter_properties_impl(PyModuleDef *module, lzma_filter filter) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1224 | /*[clinic end generated code: output=b5fe690acd6b61d1 input=d4c64f1b557c77d4]*/ |
Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 1225 | { |
| 1226 | lzma_ret lzret; |
| 1227 | uint32_t encoded_size; |
| 1228 | PyObject *result = NULL; |
| 1229 | |
| 1230 | lzret = lzma_properties_size(&encoded_size, &filter); |
| 1231 | if (catch_lzma_error(lzret)) |
| 1232 | goto error; |
| 1233 | |
| 1234 | result = PyBytes_FromStringAndSize(NULL, encoded_size); |
| 1235 | if (result == NULL) |
| 1236 | goto error; |
| 1237 | |
| 1238 | lzret = lzma_properties_encode( |
| 1239 | &filter, (uint8_t *)PyBytes_AS_STRING(result)); |
| 1240 | if (catch_lzma_error(lzret)) |
| 1241 | goto error; |
| 1242 | |
| 1243 | return result; |
| 1244 | |
| 1245 | error: |
| 1246 | Py_XDECREF(result); |
| 1247 | return NULL; |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | /*[clinic input] |
| 1252 | _lzma._decode_filter_properties |
| 1253 | filter_id: lzma_vli |
| 1254 | encoded_props: Py_buffer |
| 1255 | / |
| 1256 | |
| 1257 | Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict). |
| 1258 | |
| 1259 | The result does not include the filter ID itself, only the options. |
| 1260 | [clinic start generated code]*/ |
| 1261 | |
| 1262 | static PyObject * |
| 1263 | _lzma__decode_filter_properties_impl(PyModuleDef *module, lzma_vli filter_id, Py_buffer *encoded_props) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 1264 | /*[clinic end generated code: output=235f7f5345d48744 input=246410800782160c]*/ |
Larry Hastings | f256c22 | 2014-01-25 21:30:37 -0800 | [diff] [blame] | 1265 | { |
| 1266 | lzma_filter filter; |
| 1267 | lzma_ret lzret; |
| 1268 | PyObject *result = NULL; |
| 1269 | filter.id = filter_id; |
| 1270 | |
| 1271 | lzret = lzma_properties_decode( |
| 1272 | &filter, NULL, encoded_props->buf, encoded_props->len); |
| 1273 | if (catch_lzma_error(lzret)) |
| 1274 | return NULL; |
| 1275 | |
| 1276 | result = build_filter_spec(&filter); |
| 1277 | |
| 1278 | /* We use vanilla free() here instead of PyMem_Free() - filter.options was |
| 1279 | allocated by lzma_properties_decode() using the default allocator. */ |
| 1280 | free(filter.options); |
| 1281 | return result; |
| 1282 | } |
| 1283 | |
| 1284 | |
| 1285 | /* Module initialization. */ |
| 1286 | |
| 1287 | static PyMethodDef module_methods[] = { |
| 1288 | _LZMA_IS_CHECK_SUPPORTED_METHODDEF |
| 1289 | _LZMA__ENCODE_FILTER_PROPERTIES_METHODDEF |
| 1290 | _LZMA__DECODE_FILTER_PROPERTIES_METHODDEF |
| 1291 | {NULL} |
| 1292 | }; |
| 1293 | |
| 1294 | static PyModuleDef _lzmamodule = { |
| 1295 | PyModuleDef_HEAD_INIT, |
| 1296 | "_lzma", |
| 1297 | NULL, |
| 1298 | -1, |
| 1299 | module_methods, |
| 1300 | NULL, |
| 1301 | NULL, |
| 1302 | NULL, |
| 1303 | NULL, |
| 1304 | }; |
| 1305 | |
| 1306 | /* Some of our constants are more than 32 bits wide, so PyModule_AddIntConstant |
| 1307 | would not work correctly on platforms with 32-bit longs. */ |
| 1308 | static int |
| 1309 | module_add_int_constant(PyObject *m, const char *name, PY_LONG_LONG value) |
| 1310 | { |
| 1311 | PyObject *o = PyLong_FromLongLong(value); |
| 1312 | if (o == NULL) |
| 1313 | return -1; |
| 1314 | if (PyModule_AddObject(m, name, o) == 0) |
| 1315 | return 0; |
| 1316 | Py_DECREF(o); |
| 1317 | return -1; |
| 1318 | } |
| 1319 | |
| 1320 | #define ADD_INT_PREFIX_MACRO(m, macro) \ |
| 1321 | module_add_int_constant(m, #macro, LZMA_ ## macro) |
| 1322 | |
| 1323 | PyMODINIT_FUNC |
| 1324 | PyInit__lzma(void) |
| 1325 | { |
| 1326 | PyObject *m; |
| 1327 | |
| 1328 | empty_tuple = PyTuple_New(0); |
| 1329 | if (empty_tuple == NULL) |
| 1330 | return NULL; |
| 1331 | |
| 1332 | m = PyModule_Create(&_lzmamodule); |
| 1333 | if (m == NULL) |
| 1334 | return NULL; |
| 1335 | |
| 1336 | if (PyModule_AddIntMacro(m, FORMAT_AUTO) == -1 || |
| 1337 | PyModule_AddIntMacro(m, FORMAT_XZ) == -1 || |
| 1338 | PyModule_AddIntMacro(m, FORMAT_ALONE) == -1 || |
| 1339 | PyModule_AddIntMacro(m, FORMAT_RAW) == -1 || |
| 1340 | ADD_INT_PREFIX_MACRO(m, CHECK_NONE) == -1 || |
| 1341 | ADD_INT_PREFIX_MACRO(m, CHECK_CRC32) == -1 || |
| 1342 | ADD_INT_PREFIX_MACRO(m, CHECK_CRC64) == -1 || |
| 1343 | ADD_INT_PREFIX_MACRO(m, CHECK_SHA256) == -1 || |
| 1344 | ADD_INT_PREFIX_MACRO(m, CHECK_ID_MAX) == -1 || |
| 1345 | ADD_INT_PREFIX_MACRO(m, CHECK_UNKNOWN) == -1 || |
| 1346 | ADD_INT_PREFIX_MACRO(m, FILTER_LZMA1) == -1 || |
| 1347 | ADD_INT_PREFIX_MACRO(m, FILTER_LZMA2) == -1 || |
| 1348 | ADD_INT_PREFIX_MACRO(m, FILTER_DELTA) == -1 || |
| 1349 | ADD_INT_PREFIX_MACRO(m, FILTER_X86) == -1 || |
| 1350 | ADD_INT_PREFIX_MACRO(m, FILTER_IA64) == -1 || |
| 1351 | ADD_INT_PREFIX_MACRO(m, FILTER_ARM) == -1 || |
| 1352 | ADD_INT_PREFIX_MACRO(m, FILTER_ARMTHUMB) == -1 || |
| 1353 | ADD_INT_PREFIX_MACRO(m, FILTER_SPARC) == -1 || |
| 1354 | ADD_INT_PREFIX_MACRO(m, FILTER_POWERPC) == -1 || |
| 1355 | ADD_INT_PREFIX_MACRO(m, MF_HC3) == -1 || |
| 1356 | ADD_INT_PREFIX_MACRO(m, MF_HC4) == -1 || |
| 1357 | ADD_INT_PREFIX_MACRO(m, MF_BT2) == -1 || |
| 1358 | ADD_INT_PREFIX_MACRO(m, MF_BT3) == -1 || |
| 1359 | ADD_INT_PREFIX_MACRO(m, MF_BT4) == -1 || |
| 1360 | ADD_INT_PREFIX_MACRO(m, MODE_FAST) == -1 || |
| 1361 | ADD_INT_PREFIX_MACRO(m, MODE_NORMAL) == -1 || |
| 1362 | ADD_INT_PREFIX_MACRO(m, PRESET_DEFAULT) == -1 || |
| 1363 | ADD_INT_PREFIX_MACRO(m, PRESET_EXTREME) == -1) |
| 1364 | return NULL; |
| 1365 | |
| 1366 | Error = PyErr_NewExceptionWithDoc( |
| 1367 | "_lzma.LZMAError", "Call to liblzma failed.", NULL, NULL); |
| 1368 | if (Error == NULL) |
| 1369 | return NULL; |
| 1370 | Py_INCREF(Error); |
| 1371 | if (PyModule_AddObject(m, "LZMAError", Error) == -1) |
| 1372 | return NULL; |
| 1373 | |
| 1374 | if (PyType_Ready(&Compressor_type) == -1) |
| 1375 | return NULL; |
| 1376 | Py_INCREF(&Compressor_type); |
| 1377 | if (PyModule_AddObject(m, "LZMACompressor", |
| 1378 | (PyObject *)&Compressor_type) == -1) |
| 1379 | return NULL; |
| 1380 | |
| 1381 | if (PyType_Ready(&Decompressor_type) == -1) |
| 1382 | return NULL; |
| 1383 | Py_INCREF(&Decompressor_type); |
| 1384 | if (PyModule_AddObject(m, "LZMADecompressor", |
| 1385 | (PyObject *)&Decompressor_type) == -1) |
| 1386 | return NULL; |
| 1387 | |
| 1388 | return m; |
| 1389 | } |