blob: 5d610bf3e4b92d178791733b971184d7f653c347 [file] [log] [blame]
Nadeem Vawda59bb0e02011-12-01 01:18:27 +02001/* _lzma - Low-level Python interface to liblzma.
2
3 Initial implementation by Per Øyvind Karlsen.
4 Rewritten by Nadeem Vawda.
5
6*/
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02007
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: */
42enum {
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
52typedef struct {
53 PyObject_HEAD
Victor Stinner5064a522013-07-07 16:50:27 +020054 lzma_allocator alloc;
Nadeem Vawda3ff069e2011-11-30 00:25:06 +020055 lzma_stream lzs;
56 int flushed;
57#ifdef WITH_THREAD
58 PyThread_type_lock lock;
59#endif
60} Compressor;
61
62typedef struct {
63 PyObject_HEAD
Victor Stinner5064a522013-07-07 16:50:27 +020064 lzma_allocator alloc;
Nadeem Vawda3ff069e2011-11-30 00:25:06 +020065 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. */
75static PyObject *Error;
76
77/* An empty tuple, used by the filter specifier parsing code. */
78static PyObject *empty_tuple;
79
80
81/* Helper functions. */
82
83static int
84catch_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
Victor Stinner5064a522013-07-07 16:50:27 +0200122static void*
123PyLzma_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
132static void
133PyLzma_Free(void *opaque, void *ptr)
134{
Victor Stinnerb7f1f652013-07-07 17:10:34 +0200135 PyMem_RawFree(ptr);
Victor Stinner5064a522013-07-07 16:50:27 +0200136}
137
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200138#if BUFSIZ < 8192
139#define INITIAL_BUFFER_SIZE 8192
140#else
141#define INITIAL_BUFFER_SIZE BUFSIZ
142#endif
143
144static int
145grow_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
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200158 lzma_vli - the "K" (unsigned PY_LONG_LONG) specifier is the right
159 size, but like "I" it silently ignores overflows on conversion.
160
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200161 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 { \
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200171 unsigned PY_LONG_LONG val; \
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200172 \
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200173 val = PyLong_AsUnsignedLongLong(obj); \
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200174 if (PyErr_Occurred()) \
175 return 0; \
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200176 if ((unsigned PY_LONG_LONG)(TYPE)val != val) { \
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200177 PyErr_SetString(PyExc_OverflowError, \
178 "Value too large for " #TYPE " type"); \
179 return 0; \
180 } \
Martin v. Löwisd1b7f392012-05-15 14:06:21 +0200181 *(TYPE *)ptr = (TYPE)val; \
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200182 return 1; \
183 }
184
185INT_TYPE_CONVERTER_FUNC(uint32_t, uint32_converter)
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200186INT_TYPE_CONVERTER_FUNC(lzma_vli, lzma_vli_converter)
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200187INT_TYPE_CONVERTER_FUNC(lzma_mode, lzma_mode_converter)
188INT_TYPE_CONVERTER_FUNC(lzma_match_finder, lzma_mf_converter)
189
190#undef INT_TYPE_CONVERTER_FUNC
191
192
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200193/* Filter specifier parsing.
194
195 This code handles converting filter specifiers (Python dicts) into
196 the C lzma_filter structs expected by liblzma. */
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200197
198static void *
199parse_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);
Nadeem Vawda54c74ec2012-05-06 13:35:47 +0200231 PyErr_Format(Error, "Invalid compression preset: %d", preset);
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200232 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
254static void *
255parse_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
278static void *
279parse_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
301static void *
302parse_filter_spec(lzma_filter *f, PyObject *spec)
303{
304 PyObject *id_obj;
305
306 if (!PyMapping_Check(spec)) {
307 PyErr_SetString(PyExc_TypeError,
308 "Filter specifier must be a dict or dict-like object");
309 return NULL;
310 }
311 id_obj = PyMapping_GetItemString(spec, "id");
312 if (id_obj == NULL) {
313 if (PyErr_ExceptionMatches(PyExc_KeyError))
314 PyErr_SetString(PyExc_ValueError,
315 "Filter specifier must have an \"id\" entry");
316 return NULL;
317 }
318 f->id = PyLong_AsUnsignedLongLong(id_obj);
319 Py_DECREF(id_obj);
320 if (PyErr_Occurred())
321 return NULL;
322
323 switch (f->id) {
324 case LZMA_FILTER_LZMA1:
325 case LZMA_FILTER_LZMA2:
326 f->options = parse_filter_spec_lzma(spec);
327 return f->options;
328 case LZMA_FILTER_DELTA:
329 f->options = parse_filter_spec_delta(spec);
330 return f->options;
331 case LZMA_FILTER_X86:
332 case LZMA_FILTER_POWERPC:
333 case LZMA_FILTER_IA64:
334 case LZMA_FILTER_ARM:
335 case LZMA_FILTER_ARMTHUMB:
336 case LZMA_FILTER_SPARC:
337 f->options = parse_filter_spec_bcj(spec);
338 return f->options;
339 default:
340 PyErr_Format(PyExc_ValueError, "Invalid filter ID: %llu", f->id);
341 return NULL;
342 }
343}
344
345static void
346free_filter_chain(lzma_filter filters[])
347{
348 int i;
349
350 for (i = 0; filters[i].id != LZMA_VLI_UNKNOWN; i++)
351 PyMem_Free(filters[i].options);
352}
353
354static int
355parse_filter_chain_spec(lzma_filter filters[], PyObject *filterspecs)
356{
357 Py_ssize_t i, num_filters;
358
359 num_filters = PySequence_Length(filterspecs);
360 if (num_filters == -1)
361 return -1;
362 if (num_filters > LZMA_FILTERS_MAX) {
363 PyErr_Format(PyExc_ValueError,
364 "Too many filters - liblzma supports a maximum of %d",
365 LZMA_FILTERS_MAX);
366 return -1;
367 }
368
369 for (i = 0; i < num_filters; i++) {
370 int ok = 1;
371 PyObject *spec = PySequence_GetItem(filterspecs, i);
372 if (spec == NULL || parse_filter_spec(&filters[i], spec) == NULL)
373 ok = 0;
374 Py_XDECREF(spec);
375 if (!ok) {
376 filters[i].id = LZMA_VLI_UNKNOWN;
377 free_filter_chain(filters);
378 return -1;
379 }
380 }
381 filters[num_filters].id = LZMA_VLI_UNKNOWN;
382 return 0;
383}
384
385
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200386/* Filter specifier construction.
387
388 This code handles converting C lzma_filter structs into
389 Python-level filter specifiers (represented as dicts). */
390
391static int
392spec_add_field(PyObject *spec, _Py_Identifier *key, unsigned PY_LONG_LONG value)
393{
394 int status;
395 PyObject *value_object;
396
397 value_object = PyLong_FromUnsignedLongLong(value);
398 if (value_object == NULL)
399 return -1;
400
401 status = _PyDict_SetItemId(spec, key, value_object);
402 Py_DECREF(value_object);
403 return status;
404}
405
406static PyObject *
407build_filter_spec(const lzma_filter *f)
408{
409 PyObject *spec;
410
411 spec = PyDict_New();
412 if (spec == NULL)
413 return NULL;
414
415#define ADD_FIELD(SOURCE, FIELD) \
416 do { \
417 _Py_IDENTIFIER(FIELD); \
418 if (spec_add_field(spec, &PyId_##FIELD, SOURCE->FIELD) == -1) \
419 goto error;\
420 } while (0)
421
422 ADD_FIELD(f, id);
423
424 switch (f->id) {
Nadeem Vawda486a0452012-05-07 00:40:57 +0200425 /* For LZMA1 filters, lzma_properties_{encode,decode}() only look at the
426 lc, lp, pb, and dict_size fields. For LZMA2 filters, only the
427 dict_size field is used. */
428 case LZMA_FILTER_LZMA1: {
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200429 lzma_options_lzma *options = f->options;
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200430 ADD_FIELD(options, lc);
431 ADD_FIELD(options, lp);
432 ADD_FIELD(options, pb);
Ross Lagerwall4d688e32012-05-12 08:30:33 +0200433 ADD_FIELD(options, dict_size);
434 break;
435 }
436 case LZMA_FILTER_LZMA2: {
437 lzma_options_lzma *options = f->options;
Nadeem Vawda486a0452012-05-07 00:40:57 +0200438 ADD_FIELD(options, dict_size);
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200439 break;
440 }
441 case LZMA_FILTER_DELTA: {
442 lzma_options_delta *options = f->options;
443 ADD_FIELD(options, dist);
444 break;
445 }
446 case LZMA_FILTER_X86:
447 case LZMA_FILTER_POWERPC:
448 case LZMA_FILTER_IA64:
449 case LZMA_FILTER_ARM:
450 case LZMA_FILTER_ARMTHUMB:
451 case LZMA_FILTER_SPARC: {
452 lzma_options_bcj *options = f->options;
453 ADD_FIELD(options, start_offset);
454 break;
455 }
456 default:
457 PyErr_Format(PyExc_ValueError, "Invalid filter ID: %llu", f->id);
458 goto error;
459 }
460
461#undef ADD_FIELD
462
463 return spec;
464
465error:
466 Py_DECREF(spec);
467 return NULL;
468}
469
470
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200471/* LZMACompressor class. */
472
473static PyObject *
474compress(Compressor *c, uint8_t *data, size_t len, lzma_action action)
475{
476 size_t data_size = 0;
477 PyObject *result;
478
479 result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
480 if (result == NULL)
481 return NULL;
482 c->lzs.next_in = data;
483 c->lzs.avail_in = len;
484 c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result);
485 c->lzs.avail_out = PyBytes_GET_SIZE(result);
486 for (;;) {
487 lzma_ret lzret;
488
489 Py_BEGIN_ALLOW_THREADS
490 lzret = lzma_code(&c->lzs, action);
491 data_size = (char *)c->lzs.next_out - PyBytes_AS_STRING(result);
492 Py_END_ALLOW_THREADS
493 if (catch_lzma_error(lzret))
494 goto error;
495 if ((action == LZMA_RUN && c->lzs.avail_in == 0) ||
496 (action == LZMA_FINISH && lzret == LZMA_STREAM_END)) {
497 break;
498 } else if (c->lzs.avail_out == 0) {
499 if (grow_buffer(&result) == -1)
500 goto error;
501 c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size;
502 c->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
503 }
504 }
505 if (data_size != PyBytes_GET_SIZE(result))
506 if (_PyBytes_Resize(&result, data_size) == -1)
507 goto error;
508 return result;
509
510error:
511 Py_XDECREF(result);
512 return NULL;
513}
514
515PyDoc_STRVAR(Compressor_compress_doc,
516"compress(data) -> bytes\n"
517"\n"
518"Provide data to the compressor object. Returns a chunk of\n"
519"compressed data if possible, or b\"\" otherwise.\n"
520"\n"
521"When you have finished providing data to the compressor, call the\n"
522"flush() method to finish the conversion process.\n");
523
524static PyObject *
525Compressor_compress(Compressor *self, PyObject *args)
526{
527 Py_buffer buffer;
528 PyObject *result = NULL;
529
530 if (!PyArg_ParseTuple(args, "y*:compress", &buffer))
531 return NULL;
532
533 ACQUIRE_LOCK(self);
534 if (self->flushed)
535 PyErr_SetString(PyExc_ValueError, "Compressor has been flushed");
536 else
537 result = compress(self, buffer.buf, buffer.len, LZMA_RUN);
538 RELEASE_LOCK(self);
539 PyBuffer_Release(&buffer);
540 return result;
541}
542
543PyDoc_STRVAR(Compressor_flush_doc,
544"flush() -> bytes\n"
545"\n"
546"Finish the compression process. Returns the compressed data left\n"
547"in internal buffers.\n"
548"\n"
549"The compressor object cannot be used after this method is called.\n");
550
551static PyObject *
552Compressor_flush(Compressor *self, PyObject *noargs)
553{
554 PyObject *result = NULL;
555
556 ACQUIRE_LOCK(self);
557 if (self->flushed) {
558 PyErr_SetString(PyExc_ValueError, "Repeated call to flush()");
559 } else {
560 self->flushed = 1;
561 result = compress(self, NULL, 0, LZMA_FINISH);
562 }
563 RELEASE_LOCK(self);
564 return result;
565}
566
567static int
568Compressor_init_xz(lzma_stream *lzs, int check, uint32_t preset,
569 PyObject *filterspecs)
570{
571 lzma_ret lzret;
572
573 if (filterspecs == Py_None) {
574 lzret = lzma_easy_encoder(lzs, preset, check);
575 } else {
576 lzma_filter filters[LZMA_FILTERS_MAX + 1];
577
578 if (parse_filter_chain_spec(filters, filterspecs) == -1)
579 return -1;
580 lzret = lzma_stream_encoder(lzs, filters, check);
581 free_filter_chain(filters);
582 }
583 if (catch_lzma_error(lzret))
584 return -1;
585 else
586 return 0;
587}
588
589static int
590Compressor_init_alone(lzma_stream *lzs, uint32_t preset, PyObject *filterspecs)
591{
592 lzma_ret lzret;
593
594 if (filterspecs == Py_None) {
595 lzma_options_lzma options;
596
597 if (lzma_lzma_preset(&options, preset)) {
Nadeem Vawda54c74ec2012-05-06 13:35:47 +0200598 PyErr_Format(Error, "Invalid compression preset: %d", preset);
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200599 return -1;
600 }
601 lzret = lzma_alone_encoder(lzs, &options);
602 } else {
603 lzma_filter filters[LZMA_FILTERS_MAX + 1];
604
605 if (parse_filter_chain_spec(filters, filterspecs) == -1)
606 return -1;
607 if (filters[0].id == LZMA_FILTER_LZMA1 &&
608 filters[1].id == LZMA_VLI_UNKNOWN) {
609 lzret = lzma_alone_encoder(lzs, filters[0].options);
610 } else {
611 PyErr_SetString(PyExc_ValueError,
612 "Invalid filter chain for FORMAT_ALONE - "
613 "must be a single LZMA1 filter");
614 lzret = LZMA_PROG_ERROR;
615 }
616 free_filter_chain(filters);
617 }
618 if (PyErr_Occurred() || catch_lzma_error(lzret))
619 return -1;
620 else
621 return 0;
622}
623
624static int
625Compressor_init_raw(lzma_stream *lzs, PyObject *filterspecs)
626{
627 lzma_filter filters[LZMA_FILTERS_MAX + 1];
628 lzma_ret lzret;
629
630 if (filterspecs == Py_None) {
631 PyErr_SetString(PyExc_ValueError,
632 "Must specify filters for FORMAT_RAW");
633 return -1;
634 }
635 if (parse_filter_chain_spec(filters, filterspecs) == -1)
636 return -1;
637 lzret = lzma_raw_encoder(lzs, filters);
638 free_filter_chain(filters);
639 if (catch_lzma_error(lzret))
640 return -1;
641 else
642 return 0;
643}
644
645static int
646Compressor_init(Compressor *self, PyObject *args, PyObject *kwargs)
647{
648 static char *arg_names[] = {"format", "check", "preset", "filters", NULL};
649 int format = FORMAT_XZ;
650 int check = -1;
651 uint32_t preset = LZMA_PRESET_DEFAULT;
652 PyObject *preset_obj = Py_None;
653 PyObject *filterspecs = Py_None;
654
655 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
656 "|iiOO:LZMACompressor", arg_names,
657 &format, &check, &preset_obj,
658 &filterspecs))
659 return -1;
660
661 if (format != FORMAT_XZ && check != -1 && check != LZMA_CHECK_NONE) {
662 PyErr_SetString(PyExc_ValueError,
663 "Integrity checks are only supported by FORMAT_XZ");
664 return -1;
665 }
666
667 if (preset_obj != Py_None && filterspecs != Py_None) {
668 PyErr_SetString(PyExc_ValueError,
669 "Cannot specify both preset and filter chain");
670 return -1;
671 }
672
673 if (preset_obj != Py_None)
674 if (!uint32_converter(preset_obj, &preset))
675 return -1;
676
Victor Stinner5064a522013-07-07 16:50:27 +0200677 self->alloc.opaque = NULL;
678 self->alloc.alloc = PyLzma_Malloc;
679 self->alloc.free = PyLzma_Free;
680 self->lzs.allocator = &self->alloc;
681
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200682#ifdef WITH_THREAD
683 self->lock = PyThread_allocate_lock();
684 if (self->lock == NULL) {
685 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
686 return -1;
687 }
688#endif
689
690 self->flushed = 0;
691 switch (format) {
692 case FORMAT_XZ:
693 if (check == -1)
694 check = LZMA_CHECK_CRC64;
695 if (Compressor_init_xz(&self->lzs, check, preset, filterspecs) != 0)
696 break;
697 return 0;
698
699 case FORMAT_ALONE:
700 if (Compressor_init_alone(&self->lzs, preset, filterspecs) != 0)
701 break;
702 return 0;
703
704 case FORMAT_RAW:
705 if (Compressor_init_raw(&self->lzs, filterspecs) != 0)
706 break;
707 return 0;
708
709 default:
710 PyErr_Format(PyExc_ValueError,
711 "Invalid container format: %d", format);
712 break;
713 }
714
715#ifdef WITH_THREAD
716 PyThread_free_lock(self->lock);
717 self->lock = NULL;
718#endif
719 return -1;
720}
721
722static void
723Compressor_dealloc(Compressor *self)
724{
725 lzma_end(&self->lzs);
726#ifdef WITH_THREAD
727 if (self->lock != NULL)
728 PyThread_free_lock(self->lock);
729#endif
730 Py_TYPE(self)->tp_free((PyObject *)self);
731}
732
733static PyMethodDef Compressor_methods[] = {
734 {"compress", (PyCFunction)Compressor_compress, METH_VARARGS,
735 Compressor_compress_doc},
736 {"flush", (PyCFunction)Compressor_flush, METH_NOARGS,
737 Compressor_flush_doc},
738 {NULL}
739};
740
741PyDoc_STRVAR(Compressor_doc,
742"LZMACompressor(format=FORMAT_XZ, check=-1, preset=None, filters=None)\n"
743"\n"
744"Create a compressor object for compressing data incrementally.\n"
745"\n"
746"format specifies the container format to use for the output. This can\n"
747"be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW.\n"
748"\n"
749"check specifies the integrity check to use. For FORMAT_XZ, the default\n"
750"is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not suport integrity\n"
751"checks; for these formats, check must be omitted, or be CHECK_NONE.\n"
752"\n"
753"The settings used by the compressor can be specified either as a\n"
754"preset compression level (with the 'preset' argument), or in detail\n"
755"as a custom filter chain (with the 'filters' argument). For FORMAT_XZ\n"
756"and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset\n"
757"level. For FORMAT_RAW, the caller must always specify a filter chain;\n"
758"the raw compressor does not support preset compression levels.\n"
759"\n"
760"preset (if provided) should be an integer in the range 0-9, optionally\n"
761"OR-ed with the constant PRESET_EXTREME.\n"
762"\n"
763"filters (if provided) should be a sequence of dicts. Each dict should\n"
764"have an entry for \"id\" indicating the ID of the filter, plus\n"
765"additional entries for options to the filter.\n"
766"\n"
767"For one-shot compression, use the compress() function instead.\n");
768
769static PyTypeObject Compressor_type = {
770 PyVarObject_HEAD_INIT(NULL, 0)
771 "_lzma.LZMACompressor", /* tp_name */
772 sizeof(Compressor), /* tp_basicsize */
773 0, /* tp_itemsize */
774 (destructor)Compressor_dealloc, /* tp_dealloc */
775 0, /* tp_print */
776 0, /* tp_getattr */
777 0, /* tp_setattr */
778 0, /* tp_reserved */
779 0, /* tp_repr */
780 0, /* tp_as_number */
781 0, /* tp_as_sequence */
782 0, /* tp_as_mapping */
783 0, /* tp_hash */
784 0, /* tp_call */
785 0, /* tp_str */
786 0, /* tp_getattro */
787 0, /* tp_setattro */
788 0, /* tp_as_buffer */
789 Py_TPFLAGS_DEFAULT, /* tp_flags */
790 Compressor_doc, /* tp_doc */
791 0, /* tp_traverse */
792 0, /* tp_clear */
793 0, /* tp_richcompare */
794 0, /* tp_weaklistoffset */
795 0, /* tp_iter */
796 0, /* tp_iternext */
797 Compressor_methods, /* tp_methods */
798 0, /* tp_members */
799 0, /* tp_getset */
800 0, /* tp_base */
801 0, /* tp_dict */
802 0, /* tp_descr_get */
803 0, /* tp_descr_set */
804 0, /* tp_dictoffset */
805 (initproc)Compressor_init, /* tp_init */
806 0, /* tp_alloc */
807 PyType_GenericNew, /* tp_new */
808};
809
810
811/* LZMADecompressor class. */
812
813static PyObject *
814decompress(Decompressor *d, uint8_t *data, size_t len)
815{
816 size_t data_size = 0;
817 PyObject *result;
818
819 result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
820 if (result == NULL)
821 return NULL;
822 d->lzs.next_in = data;
823 d->lzs.avail_in = len;
824 d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result);
825 d->lzs.avail_out = PyBytes_GET_SIZE(result);
826 for (;;) {
827 lzma_ret lzret;
828
829 Py_BEGIN_ALLOW_THREADS
830 lzret = lzma_code(&d->lzs, LZMA_RUN);
831 data_size = (char *)d->lzs.next_out - PyBytes_AS_STRING(result);
832 Py_END_ALLOW_THREADS
833 if (catch_lzma_error(lzret))
834 goto error;
835 if (lzret == LZMA_GET_CHECK || lzret == LZMA_NO_CHECK)
836 d->check = lzma_get_check(&d->lzs);
837 if (lzret == LZMA_STREAM_END) {
838 d->eof = 1;
839 if (d->lzs.avail_in > 0) {
840 Py_CLEAR(d->unused_data);
841 d->unused_data = PyBytes_FromStringAndSize(
842 (char *)d->lzs.next_in, d->lzs.avail_in);
843 if (d->unused_data == NULL)
844 goto error;
845 }
846 break;
847 } else if (d->lzs.avail_in == 0) {
848 break;
849 } else if (d->lzs.avail_out == 0) {
850 if (grow_buffer(&result) == -1)
851 goto error;
852 d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size;
853 d->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
854 }
855 }
856 if (data_size != PyBytes_GET_SIZE(result))
857 if (_PyBytes_Resize(&result, data_size) == -1)
858 goto error;
859 return result;
860
861error:
862 Py_XDECREF(result);
863 return NULL;
864}
865
866PyDoc_STRVAR(Decompressor_decompress_doc,
867"decompress(data) -> bytes\n"
868"\n"
869"Provide data to the decompressor object. Returns a chunk of\n"
870"decompressed data if possible, or b\"\" otherwise.\n"
871"\n"
872"Attempting to decompress data after the end of the stream is\n"
873"reached raises an EOFError. Any data found after the end of the\n"
874"stream is ignored, and saved in the unused_data attribute.\n");
875
876static PyObject *
877Decompressor_decompress(Decompressor *self, PyObject *args)
878{
879 Py_buffer buffer;
880 PyObject *result = NULL;
881
882 if (!PyArg_ParseTuple(args, "y*:decompress", &buffer))
883 return NULL;
884
885 ACQUIRE_LOCK(self);
886 if (self->eof)
887 PyErr_SetString(PyExc_EOFError, "Already at end of stream");
888 else
889 result = decompress(self, buffer.buf, buffer.len);
890 RELEASE_LOCK(self);
891 PyBuffer_Release(&buffer);
892 return result;
893}
894
895static int
896Decompressor_init_raw(lzma_stream *lzs, PyObject *filterspecs)
897{
898 lzma_filter filters[LZMA_FILTERS_MAX + 1];
899 lzma_ret lzret;
900
901 if (parse_filter_chain_spec(filters, filterspecs) == -1)
902 return -1;
903 lzret = lzma_raw_decoder(lzs, filters);
904 free_filter_chain(filters);
905 if (catch_lzma_error(lzret))
906 return -1;
907 else
908 return 0;
909}
910
911static int
912Decompressor_init(Decompressor *self, PyObject *args, PyObject *kwargs)
913{
914 static char *arg_names[] = {"format", "memlimit", "filters", NULL};
915 const uint32_t decoder_flags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK;
916 int format = FORMAT_AUTO;
917 uint64_t memlimit = UINT64_MAX;
918 PyObject *memlimit_obj = Py_None;
919 PyObject *filterspecs = Py_None;
920 lzma_ret lzret;
921
922 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
923 "|iOO:LZMADecompressor", arg_names,
924 &format, &memlimit_obj, &filterspecs))
925 return -1;
926
927 if (memlimit_obj != Py_None) {
928 if (format == FORMAT_RAW) {
929 PyErr_SetString(PyExc_ValueError,
930 "Cannot specify memory limit with FORMAT_RAW");
931 return -1;
932 }
933 memlimit = PyLong_AsUnsignedLongLong(memlimit_obj);
934 if (PyErr_Occurred())
935 return -1;
936 }
937
938 if (format == FORMAT_RAW && filterspecs == Py_None) {
939 PyErr_SetString(PyExc_ValueError,
940 "Must specify filters for FORMAT_RAW");
941 return -1;
942 } else if (format != FORMAT_RAW && filterspecs != Py_None) {
943 PyErr_SetString(PyExc_ValueError,
944 "Cannot specify filters except with FORMAT_RAW");
945 return -1;
946 }
947
Victor Stinner5064a522013-07-07 16:50:27 +0200948 self->alloc.opaque = NULL;
949 self->alloc.alloc = PyLzma_Malloc;
950 self->alloc.free = PyLzma_Free;
951 self->lzs.allocator = &self->alloc;
952
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200953#ifdef WITH_THREAD
954 self->lock = PyThread_allocate_lock();
955 if (self->lock == NULL) {
956 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
957 return -1;
958 }
959#endif
960
961 self->check = LZMA_CHECK_UNKNOWN;
962 self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
963 if (self->unused_data == NULL)
964 goto error;
965
966 switch (format) {
967 case FORMAT_AUTO:
968 lzret = lzma_auto_decoder(&self->lzs, memlimit, decoder_flags);
969 if (catch_lzma_error(lzret))
970 break;
971 return 0;
972
973 case FORMAT_XZ:
974 lzret = lzma_stream_decoder(&self->lzs, memlimit, decoder_flags);
975 if (catch_lzma_error(lzret))
976 break;
977 return 0;
978
979 case FORMAT_ALONE:
980 self->check = LZMA_CHECK_NONE;
981 lzret = lzma_alone_decoder(&self->lzs, memlimit);
982 if (catch_lzma_error(lzret))
983 break;
984 return 0;
985
986 case FORMAT_RAW:
987 self->check = LZMA_CHECK_NONE;
988 if (Decompressor_init_raw(&self->lzs, filterspecs) == -1)
989 break;
990 return 0;
991
992 default:
993 PyErr_Format(PyExc_ValueError,
994 "Invalid container format: %d", format);
995 break;
996 }
997
998error:
999 Py_CLEAR(self->unused_data);
1000#ifdef WITH_THREAD
1001 PyThread_free_lock(self->lock);
1002 self->lock = NULL;
1003#endif
1004 return -1;
1005}
1006
1007static void
1008Decompressor_dealloc(Decompressor *self)
1009{
1010 lzma_end(&self->lzs);
1011 Py_CLEAR(self->unused_data);
1012#ifdef WITH_THREAD
1013 if (self->lock != NULL)
1014 PyThread_free_lock(self->lock);
1015#endif
1016 Py_TYPE(self)->tp_free((PyObject *)self);
1017}
1018
1019static PyMethodDef Decompressor_methods[] = {
1020 {"decompress", (PyCFunction)Decompressor_decompress, METH_VARARGS,
1021 Decompressor_decompress_doc},
1022 {NULL}
1023};
1024
1025PyDoc_STRVAR(Decompressor_check_doc,
1026"ID of the integrity check used by the input stream.");
1027
1028PyDoc_STRVAR(Decompressor_eof_doc,
1029"True if the end-of-stream marker has been reached.");
1030
1031PyDoc_STRVAR(Decompressor_unused_data_doc,
1032"Data found after the end of the compressed stream.");
1033
1034static PyMemberDef Decompressor_members[] = {
1035 {"check", T_INT, offsetof(Decompressor, check), READONLY,
1036 Decompressor_check_doc},
1037 {"eof", T_BOOL, offsetof(Decompressor, eof), READONLY,
1038 Decompressor_eof_doc},
1039 {"unused_data", T_OBJECT_EX, offsetof(Decompressor, unused_data), READONLY,
1040 Decompressor_unused_data_doc},
1041 {NULL}
1042};
1043
1044PyDoc_STRVAR(Decompressor_doc,
1045"LZMADecompressor(format=FORMAT_AUTO, memlimit=None, filters=None)\n"
1046"\n"
1047"Create a decompressor object for decompressing data incrementally.\n"
1048"\n"
1049"format specifies the container format of the input stream. If this is\n"
1050"FORMAT_AUTO (the default), the decompressor will automatically detect\n"
1051"whether the input is FORMAT_XZ or FORMAT_ALONE. Streams created with\n"
1052"FORMAT_RAW cannot be autodetected.\n"
1053"\n"
1054"memlimit can be specified to limit the amount of memory used by the\n"
1055"decompressor. This will cause decompression to fail if the input\n"
1056"cannot be decompressed within the given limit.\n"
1057"\n"
1058"filters specifies a custom filter chain. This argument is required for\n"
1059"FORMAT_RAW, and not accepted with any other format. When provided,\n"
1060"this should be a sequence of dicts, each indicating the ID and options\n"
1061"for a single filter.\n"
1062"\n"
1063"For one-shot decompression, use the decompress() function instead.\n");
1064
1065static PyTypeObject Decompressor_type = {
1066 PyVarObject_HEAD_INIT(NULL, 0)
1067 "_lzma.LZMADecompressor", /* tp_name */
1068 sizeof(Decompressor), /* tp_basicsize */
1069 0, /* tp_itemsize */
1070 (destructor)Decompressor_dealloc, /* tp_dealloc */
1071 0, /* tp_print */
1072 0, /* tp_getattr */
1073 0, /* tp_setattr */
1074 0, /* tp_reserved */
1075 0, /* tp_repr */
1076 0, /* tp_as_number */
1077 0, /* tp_as_sequence */
1078 0, /* tp_as_mapping */
1079 0, /* tp_hash */
1080 0, /* tp_call */
1081 0, /* tp_str */
1082 0, /* tp_getattro */
1083 0, /* tp_setattro */
1084 0, /* tp_as_buffer */
1085 Py_TPFLAGS_DEFAULT, /* tp_flags */
1086 Decompressor_doc, /* tp_doc */
1087 0, /* tp_traverse */
1088 0, /* tp_clear */
1089 0, /* tp_richcompare */
1090 0, /* tp_weaklistoffset */
1091 0, /* tp_iter */
1092 0, /* tp_iternext */
1093 Decompressor_methods, /* tp_methods */
1094 Decompressor_members, /* tp_members */
1095 0, /* tp_getset */
1096 0, /* tp_base */
1097 0, /* tp_dict */
1098 0, /* tp_descr_get */
1099 0, /* tp_descr_set */
1100 0, /* tp_dictoffset */
1101 (initproc)Decompressor_init, /* tp_init */
1102 0, /* tp_alloc */
1103 PyType_GenericNew, /* tp_new */
1104};
1105
1106
1107/* Module-level functions. */
1108
Nadeem Vawdabc459bb2012-05-06 23:01:51 +02001109PyDoc_STRVAR(is_check_supported_doc,
1110"is_check_supported(check_id) -> bool\n"
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001111"\n"
1112"Test whether the given integrity check is supported.\n"
1113"\n"
1114"Always returns True for CHECK_NONE and CHECK_CRC32.\n");
1115
1116static PyObject *
Nadeem Vawdabc459bb2012-05-06 23:01:51 +02001117is_check_supported(PyObject *self, PyObject *args)
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001118{
1119 int check_id;
1120
Nadeem Vawdabc459bb2012-05-06 23:01:51 +02001121 if (!PyArg_ParseTuple(args, "i:is_check_supported", &check_id))
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001122 return NULL;
1123
1124 return PyBool_FromLong(lzma_check_is_supported(check_id));
1125}
1126
1127
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001128PyDoc_STRVAR(_encode_filter_properties_doc,
1129"_encode_filter_properties(filter) -> bytes\n"
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001130"\n"
1131"Return a bytes object encoding the options (properties) of the filter\n"
1132"specified by *filter* (a dict).\n"
1133"\n"
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001134"The result does not include the filter ID itself, only the options.\n");
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001135
1136static PyObject *
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001137_encode_filter_properties(PyObject *self, PyObject *args)
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001138{
1139 PyObject *filterspec;
1140 lzma_filter filter;
1141 lzma_ret lzret;
1142 uint32_t encoded_size;
1143 PyObject *result = NULL;
1144
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001145 if (!PyArg_ParseTuple(args, "O:_encode_filter_properties", &filterspec))
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001146 return NULL;
1147
1148 if (parse_filter_spec(&filter, filterspec) == NULL)
1149 return NULL;
1150
1151 lzret = lzma_properties_size(&encoded_size, &filter);
1152 if (catch_lzma_error(lzret))
1153 goto error;
1154
1155 result = PyBytes_FromStringAndSize(NULL, encoded_size);
1156 if (result == NULL)
1157 goto error;
1158
1159 lzret = lzma_properties_encode(
1160 &filter, (uint8_t *)PyBytes_AS_STRING(result));
1161 if (catch_lzma_error(lzret))
1162 goto error;
1163
1164 PyMem_Free(filter.options);
1165 return result;
1166
1167error:
1168 Py_XDECREF(result);
1169 PyMem_Free(filter.options);
1170 return NULL;
1171}
1172
1173
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001174PyDoc_STRVAR(_decode_filter_properties_doc,
1175"_decode_filter_properties(filter_id, encoded_props) -> dict\n"
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001176"\n"
1177"Return a dict describing a filter with ID *filter_id*, and options\n"
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001178"(properties) decoded from the bytes object *encoded_props*.\n");
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001179
1180static PyObject *
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001181_decode_filter_properties(PyObject *self, PyObject *args)
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001182{
1183 Py_buffer encoded_props;
1184 lzma_filter filter;
1185 lzma_ret lzret;
1186 PyObject *result = NULL;
1187
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001188 if (!PyArg_ParseTuple(args, "O&y*:_decode_filter_properties",
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001189 lzma_vli_converter, &filter.id, &encoded_props))
1190 return NULL;
1191
1192 lzret = lzma_properties_decode(
1193 &filter, NULL, encoded_props.buf, encoded_props.len);
1194 PyBuffer_Release(&encoded_props);
1195 if (catch_lzma_error(lzret))
1196 return NULL;
1197
1198 result = build_filter_spec(&filter);
1199
1200 /* We use vanilla free() here instead of PyMem_Free() - filter.options was
1201 allocated by lzma_properties_decode() using the default allocator. */
1202 free(filter.options);
1203 return result;
1204}
1205
1206
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001207/* Module initialization. */
1208
1209static PyMethodDef module_methods[] = {
Nadeem Vawdabc459bb2012-05-06 23:01:51 +02001210 {"is_check_supported", (PyCFunction)is_check_supported,
1211 METH_VARARGS, is_check_supported_doc},
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001212 {"_encode_filter_properties", (PyCFunction)_encode_filter_properties,
1213 METH_VARARGS, _encode_filter_properties_doc},
1214 {"_decode_filter_properties", (PyCFunction)_decode_filter_properties,
1215 METH_VARARGS, _decode_filter_properties_doc},
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001216 {NULL}
1217};
1218
1219static PyModuleDef _lzmamodule = {
1220 PyModuleDef_HEAD_INIT,
1221 "_lzma",
1222 NULL,
1223 -1,
1224 module_methods,
1225 NULL,
1226 NULL,
1227 NULL,
1228 NULL,
1229};
1230
1231/* Some of our constants are more than 32 bits wide, so PyModule_AddIntConstant
1232 would not work correctly on platforms with 32-bit longs. */
1233static int
1234module_add_int_constant(PyObject *m, const char *name, PY_LONG_LONG value)
1235{
1236 PyObject *o = PyLong_FromLongLong(value);
1237 if (o == NULL)
1238 return -1;
1239 if (PyModule_AddObject(m, name, o) == 0)
1240 return 0;
1241 Py_DECREF(o);
1242 return -1;
1243}
1244
1245#define ADD_INT_PREFIX_MACRO(m, macro) \
1246 module_add_int_constant(m, #macro, LZMA_ ## macro)
1247
1248PyMODINIT_FUNC
1249PyInit__lzma(void)
1250{
1251 PyObject *m;
1252
1253 empty_tuple = PyTuple_New(0);
1254 if (empty_tuple == NULL)
1255 return NULL;
1256
1257 m = PyModule_Create(&_lzmamodule);
1258 if (m == NULL)
1259 return NULL;
1260
1261 if (PyModule_AddIntMacro(m, FORMAT_AUTO) == -1 ||
1262 PyModule_AddIntMacro(m, FORMAT_XZ) == -1 ||
1263 PyModule_AddIntMacro(m, FORMAT_ALONE) == -1 ||
1264 PyModule_AddIntMacro(m, FORMAT_RAW) == -1 ||
1265 ADD_INT_PREFIX_MACRO(m, CHECK_NONE) == -1 ||
1266 ADD_INT_PREFIX_MACRO(m, CHECK_CRC32) == -1 ||
1267 ADD_INT_PREFIX_MACRO(m, CHECK_CRC64) == -1 ||
1268 ADD_INT_PREFIX_MACRO(m, CHECK_SHA256) == -1 ||
1269 ADD_INT_PREFIX_MACRO(m, CHECK_ID_MAX) == -1 ||
1270 ADD_INT_PREFIX_MACRO(m, CHECK_UNKNOWN) == -1 ||
1271 ADD_INT_PREFIX_MACRO(m, FILTER_LZMA1) == -1 ||
1272 ADD_INT_PREFIX_MACRO(m, FILTER_LZMA2) == -1 ||
1273 ADD_INT_PREFIX_MACRO(m, FILTER_DELTA) == -1 ||
1274 ADD_INT_PREFIX_MACRO(m, FILTER_X86) == -1 ||
1275 ADD_INT_PREFIX_MACRO(m, FILTER_IA64) == -1 ||
1276 ADD_INT_PREFIX_MACRO(m, FILTER_ARM) == -1 ||
1277 ADD_INT_PREFIX_MACRO(m, FILTER_ARMTHUMB) == -1 ||
1278 ADD_INT_PREFIX_MACRO(m, FILTER_SPARC) == -1 ||
1279 ADD_INT_PREFIX_MACRO(m, FILTER_POWERPC) == -1 ||
1280 ADD_INT_PREFIX_MACRO(m, MF_HC3) == -1 ||
1281 ADD_INT_PREFIX_MACRO(m, MF_HC4) == -1 ||
1282 ADD_INT_PREFIX_MACRO(m, MF_BT2) == -1 ||
1283 ADD_INT_PREFIX_MACRO(m, MF_BT3) == -1 ||
1284 ADD_INT_PREFIX_MACRO(m, MF_BT4) == -1 ||
1285 ADD_INT_PREFIX_MACRO(m, MODE_FAST) == -1 ||
1286 ADD_INT_PREFIX_MACRO(m, MODE_NORMAL) == -1 ||
1287 ADD_INT_PREFIX_MACRO(m, PRESET_DEFAULT) == -1 ||
1288 ADD_INT_PREFIX_MACRO(m, PRESET_EXTREME) == -1)
1289 return NULL;
1290
1291 Error = PyErr_NewExceptionWithDoc(
1292 "_lzma.LZMAError", "Call to liblzma failed.", NULL, NULL);
1293 if (Error == NULL)
1294 return NULL;
1295 Py_INCREF(Error);
1296 if (PyModule_AddObject(m, "LZMAError", Error) == -1)
1297 return NULL;
1298
1299 if (PyType_Ready(&Compressor_type) == -1)
1300 return NULL;
1301 Py_INCREF(&Compressor_type);
1302 if (PyModule_AddObject(m, "LZMACompressor",
1303 (PyObject *)&Compressor_type) == -1)
1304 return NULL;
1305
1306 if (PyType_Ready(&Decompressor_type) == -1)
1307 return NULL;
1308 Py_INCREF(&Decompressor_type);
1309 if (PyModule_AddObject(m, "LZMADecompressor",
1310 (PyObject *)&Decompressor_type) == -1)
1311 return NULL;
1312
1313 return m;
1314}