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