blob: 643616052c5d07b53a04169263b366eae2e27ece [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
54 lzma_stream lzs;
55 int flushed;
56#ifdef WITH_THREAD
57 PyThread_type_lock lock;
58#endif
59} Compressor;
60
61typedef 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. */
73static PyObject *Error;
74
75/* An empty tuple, used by the filter specifier parsing code. */
76static PyObject *empty_tuple;
77
78
79/* Helper functions. */
80
81static int
82catch_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
126static int
127grow_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 Vawdaf55b3292012-05-06 23:01:27 +0200140 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 Vawda3ff069e2011-11-30 00:25:06 +0200143 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 Vawdaf55b3292012-05-06 23:01:27 +0200153 unsigned PY_LONG_LONG val; \
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200154 \
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200155 val = PyLong_AsUnsignedLongLong(obj); \
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200156 if (PyErr_Occurred()) \
157 return 0; \
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200158 if ((unsigned PY_LONG_LONG)(TYPE)val != val) { \
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200159 PyErr_SetString(PyExc_OverflowError, \
160 "Value too large for " #TYPE " type"); \
161 return 0; \
162 } \
Martin v. Löwisd1b7f392012-05-15 14:06:21 +0200163 *(TYPE *)ptr = (TYPE)val; \
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200164 return 1; \
165 }
166
167INT_TYPE_CONVERTER_FUNC(uint32_t, uint32_converter)
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200168INT_TYPE_CONVERTER_FUNC(lzma_vli, lzma_vli_converter)
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200169INT_TYPE_CONVERTER_FUNC(lzma_mode, lzma_mode_converter)
170INT_TYPE_CONVERTER_FUNC(lzma_match_finder, lzma_mf_converter)
171
172#undef INT_TYPE_CONVERTER_FUNC
173
174
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200175/* Filter specifier parsing.
176
177 This code handles converting filter specifiers (Python dicts) into
178 the C lzma_filter structs expected by liblzma. */
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200179
180static void *
181parse_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 Vawda54c74ec2012-05-06 13:35:47 +0200213 PyErr_Format(Error, "Invalid compression preset: %d", preset);
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200214 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
236static void *
237parse_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
260static void *
261parse_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
283static void *
284parse_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
327static void
328free_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
336static int
337parse_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 Vawdaf55b3292012-05-06 23:01:27 +0200368/* Filter specifier construction.
369
370 This code handles converting C lzma_filter structs into
371 Python-level filter specifiers (represented as dicts). */
372
373static int
374spec_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
388static PyObject *
389build_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 Vawda486a0452012-05-07 00:40:57 +0200407 /* 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 Vawdaf55b3292012-05-06 23:01:27 +0200411 lzma_options_lzma *options = f->options;
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200412 ADD_FIELD(options, lc);
413 ADD_FIELD(options, lp);
414 ADD_FIELD(options, pb);
Ross Lagerwall4d688e32012-05-12 08:30:33 +0200415 ADD_FIELD(options, dict_size);
416 break;
417 }
418 case LZMA_FILTER_LZMA2: {
419 lzma_options_lzma *options = f->options;
Nadeem Vawda486a0452012-05-07 00:40:57 +0200420 ADD_FIELD(options, dict_size);
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200421 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
447error:
448 Py_DECREF(spec);
449 return NULL;
450}
451
452
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200453/* LZMACompressor class. */
454
455static PyObject *
456compress(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
492error:
493 Py_XDECREF(result);
494 return NULL;
495}
496
497PyDoc_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
506static PyObject *
507Compressor_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
525PyDoc_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
533static PyObject *
534Compressor_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
Nadeem Vawda37970652013-10-28 21:35:23 +0100549static PyObject *
550Compressor_getstate(Compressor *self, PyObject *noargs)
551{
552 PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
553 Py_TYPE(self)->tp_name);
554 return NULL;
555}
556
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200557static int
558Compressor_init_xz(lzma_stream *lzs, int check, uint32_t preset,
559 PyObject *filterspecs)
560{
561 lzma_ret lzret;
562
563 if (filterspecs == Py_None) {
564 lzret = lzma_easy_encoder(lzs, preset, check);
565 } else {
566 lzma_filter filters[LZMA_FILTERS_MAX + 1];
567
568 if (parse_filter_chain_spec(filters, filterspecs) == -1)
569 return -1;
570 lzret = lzma_stream_encoder(lzs, filters, check);
571 free_filter_chain(filters);
572 }
573 if (catch_lzma_error(lzret))
574 return -1;
575 else
576 return 0;
577}
578
579static int
580Compressor_init_alone(lzma_stream *lzs, uint32_t preset, PyObject *filterspecs)
581{
582 lzma_ret lzret;
583
584 if (filterspecs == Py_None) {
585 lzma_options_lzma options;
586
587 if (lzma_lzma_preset(&options, preset)) {
Nadeem Vawda54c74ec2012-05-06 13:35:47 +0200588 PyErr_Format(Error, "Invalid compression preset: %d", preset);
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200589 return -1;
590 }
591 lzret = lzma_alone_encoder(lzs, &options);
592 } else {
593 lzma_filter filters[LZMA_FILTERS_MAX + 1];
594
595 if (parse_filter_chain_spec(filters, filterspecs) == -1)
596 return -1;
597 if (filters[0].id == LZMA_FILTER_LZMA1 &&
598 filters[1].id == LZMA_VLI_UNKNOWN) {
599 lzret = lzma_alone_encoder(lzs, filters[0].options);
600 } else {
601 PyErr_SetString(PyExc_ValueError,
602 "Invalid filter chain for FORMAT_ALONE - "
603 "must be a single LZMA1 filter");
604 lzret = LZMA_PROG_ERROR;
605 }
606 free_filter_chain(filters);
607 }
608 if (PyErr_Occurred() || catch_lzma_error(lzret))
609 return -1;
610 else
611 return 0;
612}
613
614static int
615Compressor_init_raw(lzma_stream *lzs, PyObject *filterspecs)
616{
617 lzma_filter filters[LZMA_FILTERS_MAX + 1];
618 lzma_ret lzret;
619
620 if (filterspecs == Py_None) {
621 PyErr_SetString(PyExc_ValueError,
622 "Must specify filters for FORMAT_RAW");
623 return -1;
624 }
625 if (parse_filter_chain_spec(filters, filterspecs) == -1)
626 return -1;
627 lzret = lzma_raw_encoder(lzs, filters);
628 free_filter_chain(filters);
629 if (catch_lzma_error(lzret))
630 return -1;
631 else
632 return 0;
633}
634
635static int
636Compressor_init(Compressor *self, PyObject *args, PyObject *kwargs)
637{
638 static char *arg_names[] = {"format", "check", "preset", "filters", NULL};
639 int format = FORMAT_XZ;
640 int check = -1;
641 uint32_t preset = LZMA_PRESET_DEFAULT;
642 PyObject *preset_obj = Py_None;
643 PyObject *filterspecs = Py_None;
644
645 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
646 "|iiOO:LZMACompressor", arg_names,
647 &format, &check, &preset_obj,
648 &filterspecs))
649 return -1;
650
651 if (format != FORMAT_XZ && check != -1 && check != LZMA_CHECK_NONE) {
652 PyErr_SetString(PyExc_ValueError,
653 "Integrity checks are only supported by FORMAT_XZ");
654 return -1;
655 }
656
657 if (preset_obj != Py_None && filterspecs != Py_None) {
658 PyErr_SetString(PyExc_ValueError,
659 "Cannot specify both preset and filter chain");
660 return -1;
661 }
662
663 if (preset_obj != Py_None)
664 if (!uint32_converter(preset_obj, &preset))
665 return -1;
666
667#ifdef WITH_THREAD
668 self->lock = PyThread_allocate_lock();
669 if (self->lock == NULL) {
670 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
671 return -1;
672 }
673#endif
674
675 self->flushed = 0;
676 switch (format) {
677 case FORMAT_XZ:
678 if (check == -1)
679 check = LZMA_CHECK_CRC64;
680 if (Compressor_init_xz(&self->lzs, check, preset, filterspecs) != 0)
681 break;
682 return 0;
683
684 case FORMAT_ALONE:
685 if (Compressor_init_alone(&self->lzs, preset, filterspecs) != 0)
686 break;
687 return 0;
688
689 case FORMAT_RAW:
690 if (Compressor_init_raw(&self->lzs, filterspecs) != 0)
691 break;
692 return 0;
693
694 default:
695 PyErr_Format(PyExc_ValueError,
696 "Invalid container format: %d", format);
697 break;
698 }
699
700#ifdef WITH_THREAD
701 PyThread_free_lock(self->lock);
702 self->lock = NULL;
703#endif
704 return -1;
705}
706
707static void
708Compressor_dealloc(Compressor *self)
709{
710 lzma_end(&self->lzs);
711#ifdef WITH_THREAD
712 if (self->lock != NULL)
713 PyThread_free_lock(self->lock);
714#endif
715 Py_TYPE(self)->tp_free((PyObject *)self);
716}
717
718static PyMethodDef Compressor_methods[] = {
719 {"compress", (PyCFunction)Compressor_compress, METH_VARARGS,
720 Compressor_compress_doc},
721 {"flush", (PyCFunction)Compressor_flush, METH_NOARGS,
722 Compressor_flush_doc},
Nadeem Vawda37970652013-10-28 21:35:23 +0100723 {"__getstate__", (PyCFunction)Compressor_getstate, METH_NOARGS},
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200724 {NULL}
725};
726
727PyDoc_STRVAR(Compressor_doc,
728"LZMACompressor(format=FORMAT_XZ, check=-1, preset=None, filters=None)\n"
729"\n"
730"Create a compressor object for compressing data incrementally.\n"
731"\n"
732"format specifies the container format to use for the output. This can\n"
733"be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW.\n"
734"\n"
735"check specifies the integrity check to use. For FORMAT_XZ, the default\n"
736"is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not suport integrity\n"
737"checks; for these formats, check must be omitted, or be CHECK_NONE.\n"
738"\n"
739"The settings used by the compressor can be specified either as a\n"
740"preset compression level (with the 'preset' argument), or in detail\n"
741"as a custom filter chain (with the 'filters' argument). For FORMAT_XZ\n"
742"and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset\n"
743"level. For FORMAT_RAW, the caller must always specify a filter chain;\n"
744"the raw compressor does not support preset compression levels.\n"
745"\n"
746"preset (if provided) should be an integer in the range 0-9, optionally\n"
747"OR-ed with the constant PRESET_EXTREME.\n"
748"\n"
749"filters (if provided) should be a sequence of dicts. Each dict should\n"
750"have an entry for \"id\" indicating the ID of the filter, plus\n"
751"additional entries for options to the filter.\n"
752"\n"
753"For one-shot compression, use the compress() function instead.\n");
754
755static PyTypeObject Compressor_type = {
756 PyVarObject_HEAD_INIT(NULL, 0)
757 "_lzma.LZMACompressor", /* tp_name */
758 sizeof(Compressor), /* tp_basicsize */
759 0, /* tp_itemsize */
760 (destructor)Compressor_dealloc, /* tp_dealloc */
761 0, /* tp_print */
762 0, /* tp_getattr */
763 0, /* tp_setattr */
764 0, /* tp_reserved */
765 0, /* tp_repr */
766 0, /* tp_as_number */
767 0, /* tp_as_sequence */
768 0, /* tp_as_mapping */
769 0, /* tp_hash */
770 0, /* tp_call */
771 0, /* tp_str */
772 0, /* tp_getattro */
773 0, /* tp_setattro */
774 0, /* tp_as_buffer */
775 Py_TPFLAGS_DEFAULT, /* tp_flags */
776 Compressor_doc, /* tp_doc */
777 0, /* tp_traverse */
778 0, /* tp_clear */
779 0, /* tp_richcompare */
780 0, /* tp_weaklistoffset */
781 0, /* tp_iter */
782 0, /* tp_iternext */
783 Compressor_methods, /* tp_methods */
784 0, /* tp_members */
785 0, /* tp_getset */
786 0, /* tp_base */
787 0, /* tp_dict */
788 0, /* tp_descr_get */
789 0, /* tp_descr_set */
790 0, /* tp_dictoffset */
791 (initproc)Compressor_init, /* tp_init */
792 0, /* tp_alloc */
793 PyType_GenericNew, /* tp_new */
794};
795
796
797/* LZMADecompressor class. */
798
799static PyObject *
800decompress(Decompressor *d, uint8_t *data, size_t len)
801{
802 size_t data_size = 0;
803 PyObject *result;
804
805 result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
806 if (result == NULL)
807 return NULL;
808 d->lzs.next_in = data;
809 d->lzs.avail_in = len;
810 d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result);
811 d->lzs.avail_out = PyBytes_GET_SIZE(result);
812 for (;;) {
813 lzma_ret lzret;
814
815 Py_BEGIN_ALLOW_THREADS
816 lzret = lzma_code(&d->lzs, LZMA_RUN);
817 data_size = (char *)d->lzs.next_out - PyBytes_AS_STRING(result);
818 Py_END_ALLOW_THREADS
819 if (catch_lzma_error(lzret))
820 goto error;
821 if (lzret == LZMA_GET_CHECK || lzret == LZMA_NO_CHECK)
822 d->check = lzma_get_check(&d->lzs);
823 if (lzret == LZMA_STREAM_END) {
824 d->eof = 1;
825 if (d->lzs.avail_in > 0) {
826 Py_CLEAR(d->unused_data);
827 d->unused_data = PyBytes_FromStringAndSize(
828 (char *)d->lzs.next_in, d->lzs.avail_in);
829 if (d->unused_data == NULL)
830 goto error;
831 }
832 break;
833 } else if (d->lzs.avail_in == 0) {
834 break;
835 } else if (d->lzs.avail_out == 0) {
836 if (grow_buffer(&result) == -1)
837 goto error;
838 d->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size;
839 d->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
840 }
841 }
842 if (data_size != PyBytes_GET_SIZE(result))
843 if (_PyBytes_Resize(&result, data_size) == -1)
844 goto error;
845 return result;
846
847error:
848 Py_XDECREF(result);
849 return NULL;
850}
851
852PyDoc_STRVAR(Decompressor_decompress_doc,
853"decompress(data) -> bytes\n"
854"\n"
855"Provide data to the decompressor object. Returns a chunk of\n"
856"decompressed data if possible, or b\"\" otherwise.\n"
857"\n"
858"Attempting to decompress data after the end of the stream is\n"
859"reached raises an EOFError. Any data found after the end of the\n"
860"stream is ignored, and saved in the unused_data attribute.\n");
861
862static PyObject *
863Decompressor_decompress(Decompressor *self, PyObject *args)
864{
865 Py_buffer buffer;
866 PyObject *result = NULL;
867
868 if (!PyArg_ParseTuple(args, "y*:decompress", &buffer))
869 return NULL;
870
871 ACQUIRE_LOCK(self);
872 if (self->eof)
873 PyErr_SetString(PyExc_EOFError, "Already at end of stream");
874 else
875 result = decompress(self, buffer.buf, buffer.len);
876 RELEASE_LOCK(self);
877 PyBuffer_Release(&buffer);
878 return result;
879}
880
Nadeem Vawda37970652013-10-28 21:35:23 +0100881static PyObject *
882Decompressor_getstate(Decompressor *self, PyObject *noargs)
883{
884 PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
885 Py_TYPE(self)->tp_name);
886 return NULL;
887}
888
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200889static int
890Decompressor_init_raw(lzma_stream *lzs, PyObject *filterspecs)
891{
892 lzma_filter filters[LZMA_FILTERS_MAX + 1];
893 lzma_ret lzret;
894
895 if (parse_filter_chain_spec(filters, filterspecs) == -1)
896 return -1;
897 lzret = lzma_raw_decoder(lzs, filters);
898 free_filter_chain(filters);
899 if (catch_lzma_error(lzret))
900 return -1;
901 else
902 return 0;
903}
904
905static int
906Decompressor_init(Decompressor *self, PyObject *args, PyObject *kwargs)
907{
908 static char *arg_names[] = {"format", "memlimit", "filters", NULL};
909 const uint32_t decoder_flags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK;
910 int format = FORMAT_AUTO;
911 uint64_t memlimit = UINT64_MAX;
912 PyObject *memlimit_obj = Py_None;
913 PyObject *filterspecs = Py_None;
914 lzma_ret lzret;
915
916 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
917 "|iOO:LZMADecompressor", arg_names,
918 &format, &memlimit_obj, &filterspecs))
919 return -1;
920
921 if (memlimit_obj != Py_None) {
922 if (format == FORMAT_RAW) {
923 PyErr_SetString(PyExc_ValueError,
924 "Cannot specify memory limit with FORMAT_RAW");
925 return -1;
926 }
927 memlimit = PyLong_AsUnsignedLongLong(memlimit_obj);
928 if (PyErr_Occurred())
929 return -1;
930 }
931
932 if (format == FORMAT_RAW && filterspecs == Py_None) {
933 PyErr_SetString(PyExc_ValueError,
934 "Must specify filters for FORMAT_RAW");
935 return -1;
936 } else if (format != FORMAT_RAW && filterspecs != Py_None) {
937 PyErr_SetString(PyExc_ValueError,
938 "Cannot specify filters except with FORMAT_RAW");
939 return -1;
940 }
941
942#ifdef WITH_THREAD
943 self->lock = PyThread_allocate_lock();
944 if (self->lock == NULL) {
945 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
946 return -1;
947 }
948#endif
949
950 self->check = LZMA_CHECK_UNKNOWN;
951 self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
952 if (self->unused_data == NULL)
953 goto error;
954
955 switch (format) {
956 case FORMAT_AUTO:
957 lzret = lzma_auto_decoder(&self->lzs, memlimit, decoder_flags);
958 if (catch_lzma_error(lzret))
959 break;
960 return 0;
961
962 case FORMAT_XZ:
963 lzret = lzma_stream_decoder(&self->lzs, memlimit, decoder_flags);
964 if (catch_lzma_error(lzret))
965 break;
966 return 0;
967
968 case FORMAT_ALONE:
969 self->check = LZMA_CHECK_NONE;
970 lzret = lzma_alone_decoder(&self->lzs, memlimit);
971 if (catch_lzma_error(lzret))
972 break;
973 return 0;
974
975 case FORMAT_RAW:
976 self->check = LZMA_CHECK_NONE;
977 if (Decompressor_init_raw(&self->lzs, filterspecs) == -1)
978 break;
979 return 0;
980
981 default:
982 PyErr_Format(PyExc_ValueError,
983 "Invalid container format: %d", format);
984 break;
985 }
986
987error:
988 Py_CLEAR(self->unused_data);
989#ifdef WITH_THREAD
990 PyThread_free_lock(self->lock);
991 self->lock = NULL;
992#endif
993 return -1;
994}
995
996static void
997Decompressor_dealloc(Decompressor *self)
998{
999 lzma_end(&self->lzs);
1000 Py_CLEAR(self->unused_data);
1001#ifdef WITH_THREAD
1002 if (self->lock != NULL)
1003 PyThread_free_lock(self->lock);
1004#endif
1005 Py_TYPE(self)->tp_free((PyObject *)self);
1006}
1007
1008static PyMethodDef Decompressor_methods[] = {
1009 {"decompress", (PyCFunction)Decompressor_decompress, METH_VARARGS,
1010 Decompressor_decompress_doc},
Nadeem Vawda37970652013-10-28 21:35:23 +01001011 {"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS},
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001012 {NULL}
1013};
1014
1015PyDoc_STRVAR(Decompressor_check_doc,
1016"ID of the integrity check used by the input stream.");
1017
1018PyDoc_STRVAR(Decompressor_eof_doc,
1019"True if the end-of-stream marker has been reached.");
1020
1021PyDoc_STRVAR(Decompressor_unused_data_doc,
1022"Data found after the end of the compressed stream.");
1023
1024static PyMemberDef Decompressor_members[] = {
1025 {"check", T_INT, offsetof(Decompressor, check), READONLY,
1026 Decompressor_check_doc},
1027 {"eof", T_BOOL, offsetof(Decompressor, eof), READONLY,
1028 Decompressor_eof_doc},
1029 {"unused_data", T_OBJECT_EX, offsetof(Decompressor, unused_data), READONLY,
1030 Decompressor_unused_data_doc},
1031 {NULL}
1032};
1033
1034PyDoc_STRVAR(Decompressor_doc,
1035"LZMADecompressor(format=FORMAT_AUTO, memlimit=None, filters=None)\n"
1036"\n"
1037"Create a decompressor object for decompressing data incrementally.\n"
1038"\n"
1039"format specifies the container format of the input stream. If this is\n"
1040"FORMAT_AUTO (the default), the decompressor will automatically detect\n"
1041"whether the input is FORMAT_XZ or FORMAT_ALONE. Streams created with\n"
1042"FORMAT_RAW cannot be autodetected.\n"
1043"\n"
1044"memlimit can be specified to limit the amount of memory used by the\n"
1045"decompressor. This will cause decompression to fail if the input\n"
1046"cannot be decompressed within the given limit.\n"
1047"\n"
1048"filters specifies a custom filter chain. This argument is required for\n"
1049"FORMAT_RAW, and not accepted with any other format. When provided,\n"
1050"this should be a sequence of dicts, each indicating the ID and options\n"
1051"for a single filter.\n"
1052"\n"
1053"For one-shot decompression, use the decompress() function instead.\n");
1054
1055static PyTypeObject Decompressor_type = {
1056 PyVarObject_HEAD_INIT(NULL, 0)
1057 "_lzma.LZMADecompressor", /* tp_name */
1058 sizeof(Decompressor), /* tp_basicsize */
1059 0, /* tp_itemsize */
1060 (destructor)Decompressor_dealloc, /* tp_dealloc */
1061 0, /* tp_print */
1062 0, /* tp_getattr */
1063 0, /* tp_setattr */
1064 0, /* tp_reserved */
1065 0, /* tp_repr */
1066 0, /* tp_as_number */
1067 0, /* tp_as_sequence */
1068 0, /* tp_as_mapping */
1069 0, /* tp_hash */
1070 0, /* tp_call */
1071 0, /* tp_str */
1072 0, /* tp_getattro */
1073 0, /* tp_setattro */
1074 0, /* tp_as_buffer */
1075 Py_TPFLAGS_DEFAULT, /* tp_flags */
1076 Decompressor_doc, /* tp_doc */
1077 0, /* tp_traverse */
1078 0, /* tp_clear */
1079 0, /* tp_richcompare */
1080 0, /* tp_weaklistoffset */
1081 0, /* tp_iter */
1082 0, /* tp_iternext */
1083 Decompressor_methods, /* tp_methods */
1084 Decompressor_members, /* tp_members */
1085 0, /* tp_getset */
1086 0, /* tp_base */
1087 0, /* tp_dict */
1088 0, /* tp_descr_get */
1089 0, /* tp_descr_set */
1090 0, /* tp_dictoffset */
1091 (initproc)Decompressor_init, /* tp_init */
1092 0, /* tp_alloc */
1093 PyType_GenericNew, /* tp_new */
1094};
1095
1096
1097/* Module-level functions. */
1098
Nadeem Vawdabc459bb2012-05-06 23:01:51 +02001099PyDoc_STRVAR(is_check_supported_doc,
1100"is_check_supported(check_id) -> bool\n"
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001101"\n"
1102"Test whether the given integrity check is supported.\n"
1103"\n"
1104"Always returns True for CHECK_NONE and CHECK_CRC32.\n");
1105
1106static PyObject *
Nadeem Vawdabc459bb2012-05-06 23:01:51 +02001107is_check_supported(PyObject *self, PyObject *args)
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001108{
1109 int check_id;
1110
Nadeem Vawdabc459bb2012-05-06 23:01:51 +02001111 if (!PyArg_ParseTuple(args, "i:is_check_supported", &check_id))
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001112 return NULL;
1113
1114 return PyBool_FromLong(lzma_check_is_supported(check_id));
1115}
1116
1117
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001118PyDoc_STRVAR(_encode_filter_properties_doc,
1119"_encode_filter_properties(filter) -> bytes\n"
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001120"\n"
1121"Return a bytes object encoding the options (properties) of the filter\n"
1122"specified by *filter* (a dict).\n"
1123"\n"
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001124"The result does not include the filter ID itself, only the options.\n");
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001125
1126static PyObject *
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001127_encode_filter_properties(PyObject *self, PyObject *args)
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001128{
1129 PyObject *filterspec;
1130 lzma_filter filter;
1131 lzma_ret lzret;
1132 uint32_t encoded_size;
1133 PyObject *result = NULL;
1134
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001135 if (!PyArg_ParseTuple(args, "O:_encode_filter_properties", &filterspec))
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001136 return NULL;
1137
1138 if (parse_filter_spec(&filter, filterspec) == NULL)
1139 return NULL;
1140
1141 lzret = lzma_properties_size(&encoded_size, &filter);
1142 if (catch_lzma_error(lzret))
1143 goto error;
1144
1145 result = PyBytes_FromStringAndSize(NULL, encoded_size);
1146 if (result == NULL)
1147 goto error;
1148
1149 lzret = lzma_properties_encode(
1150 &filter, (uint8_t *)PyBytes_AS_STRING(result));
1151 if (catch_lzma_error(lzret))
1152 goto error;
1153
1154 PyMem_Free(filter.options);
1155 return result;
1156
1157error:
1158 Py_XDECREF(result);
1159 PyMem_Free(filter.options);
1160 return NULL;
1161}
1162
1163
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001164PyDoc_STRVAR(_decode_filter_properties_doc,
1165"_decode_filter_properties(filter_id, encoded_props) -> dict\n"
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001166"\n"
1167"Return a dict describing a filter with ID *filter_id*, and options\n"
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001168"(properties) decoded from the bytes object *encoded_props*.\n");
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001169
1170static PyObject *
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001171_decode_filter_properties(PyObject *self, PyObject *args)
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001172{
1173 Py_buffer encoded_props;
1174 lzma_filter filter;
1175 lzma_ret lzret;
1176 PyObject *result = NULL;
1177
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001178 if (!PyArg_ParseTuple(args, "O&y*:_decode_filter_properties",
Nadeem Vawdaf55b3292012-05-06 23:01:27 +02001179 lzma_vli_converter, &filter.id, &encoded_props))
1180 return NULL;
1181
1182 lzret = lzma_properties_decode(
1183 &filter, NULL, encoded_props.buf, encoded_props.len);
1184 PyBuffer_Release(&encoded_props);
1185 if (catch_lzma_error(lzret))
1186 return NULL;
1187
1188 result = build_filter_spec(&filter);
1189
1190 /* We use vanilla free() here instead of PyMem_Free() - filter.options was
1191 allocated by lzma_properties_decode() using the default allocator. */
1192 free(filter.options);
1193 return result;
1194}
1195
1196
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001197/* Module initialization. */
1198
1199static PyMethodDef module_methods[] = {
Nadeem Vawdabc459bb2012-05-06 23:01:51 +02001200 {"is_check_supported", (PyCFunction)is_check_supported,
1201 METH_VARARGS, is_check_supported_doc},
Nadeem Vawdaa425c3d2012-06-21 23:36:48 +02001202 {"_encode_filter_properties", (PyCFunction)_encode_filter_properties,
1203 METH_VARARGS, _encode_filter_properties_doc},
1204 {"_decode_filter_properties", (PyCFunction)_decode_filter_properties,
1205 METH_VARARGS, _decode_filter_properties_doc},
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02001206 {NULL}
1207};
1208
1209static PyModuleDef _lzmamodule = {
1210 PyModuleDef_HEAD_INIT,
1211 "_lzma",
1212 NULL,
1213 -1,
1214 module_methods,
1215 NULL,
1216 NULL,
1217 NULL,
1218 NULL,
1219};
1220
1221/* Some of our constants are more than 32 bits wide, so PyModule_AddIntConstant
1222 would not work correctly on platforms with 32-bit longs. */
1223static int
1224module_add_int_constant(PyObject *m, const char *name, PY_LONG_LONG value)
1225{
1226 PyObject *o = PyLong_FromLongLong(value);
1227 if (o == NULL)
1228 return -1;
1229 if (PyModule_AddObject(m, name, o) == 0)
1230 return 0;
1231 Py_DECREF(o);
1232 return -1;
1233}
1234
1235#define ADD_INT_PREFIX_MACRO(m, macro) \
1236 module_add_int_constant(m, #macro, LZMA_ ## macro)
1237
1238PyMODINIT_FUNC
1239PyInit__lzma(void)
1240{
1241 PyObject *m;
1242
1243 empty_tuple = PyTuple_New(0);
1244 if (empty_tuple == NULL)
1245 return NULL;
1246
1247 m = PyModule_Create(&_lzmamodule);
1248 if (m == NULL)
1249 return NULL;
1250
1251 if (PyModule_AddIntMacro(m, FORMAT_AUTO) == -1 ||
1252 PyModule_AddIntMacro(m, FORMAT_XZ) == -1 ||
1253 PyModule_AddIntMacro(m, FORMAT_ALONE) == -1 ||
1254 PyModule_AddIntMacro(m, FORMAT_RAW) == -1 ||
1255 ADD_INT_PREFIX_MACRO(m, CHECK_NONE) == -1 ||
1256 ADD_INT_PREFIX_MACRO(m, CHECK_CRC32) == -1 ||
1257 ADD_INT_PREFIX_MACRO(m, CHECK_CRC64) == -1 ||
1258 ADD_INT_PREFIX_MACRO(m, CHECK_SHA256) == -1 ||
1259 ADD_INT_PREFIX_MACRO(m, CHECK_ID_MAX) == -1 ||
1260 ADD_INT_PREFIX_MACRO(m, CHECK_UNKNOWN) == -1 ||
1261 ADD_INT_PREFIX_MACRO(m, FILTER_LZMA1) == -1 ||
1262 ADD_INT_PREFIX_MACRO(m, FILTER_LZMA2) == -1 ||
1263 ADD_INT_PREFIX_MACRO(m, FILTER_DELTA) == -1 ||
1264 ADD_INT_PREFIX_MACRO(m, FILTER_X86) == -1 ||
1265 ADD_INT_PREFIX_MACRO(m, FILTER_IA64) == -1 ||
1266 ADD_INT_PREFIX_MACRO(m, FILTER_ARM) == -1 ||
1267 ADD_INT_PREFIX_MACRO(m, FILTER_ARMTHUMB) == -1 ||
1268 ADD_INT_PREFIX_MACRO(m, FILTER_SPARC) == -1 ||
1269 ADD_INT_PREFIX_MACRO(m, FILTER_POWERPC) == -1 ||
1270 ADD_INT_PREFIX_MACRO(m, MF_HC3) == -1 ||
1271 ADD_INT_PREFIX_MACRO(m, MF_HC4) == -1 ||
1272 ADD_INT_PREFIX_MACRO(m, MF_BT2) == -1 ||
1273 ADD_INT_PREFIX_MACRO(m, MF_BT3) == -1 ||
1274 ADD_INT_PREFIX_MACRO(m, MF_BT4) == -1 ||
1275 ADD_INT_PREFIX_MACRO(m, MODE_FAST) == -1 ||
1276 ADD_INT_PREFIX_MACRO(m, MODE_NORMAL) == -1 ||
1277 ADD_INT_PREFIX_MACRO(m, PRESET_DEFAULT) == -1 ||
1278 ADD_INT_PREFIX_MACRO(m, PRESET_EXTREME) == -1)
1279 return NULL;
1280
1281 Error = PyErr_NewExceptionWithDoc(
1282 "_lzma.LZMAError", "Call to liblzma failed.", NULL, NULL);
1283 if (Error == NULL)
1284 return NULL;
1285 Py_INCREF(Error);
1286 if (PyModule_AddObject(m, "LZMAError", Error) == -1)
1287 return NULL;
1288
1289 if (PyType_Ready(&Compressor_type) == -1)
1290 return NULL;
1291 Py_INCREF(&Compressor_type);
1292 if (PyModule_AddObject(m, "LZMACompressor",
1293 (PyObject *)&Compressor_type) == -1)
1294 return NULL;
1295
1296 if (PyType_Ready(&Decompressor_type) == -1)
1297 return NULL;
1298 Py_INCREF(&Decompressor_type);
1299 if (PyModule_AddObject(m, "LZMADecompressor",
1300 (PyObject *)&Decompressor_type) == -1)
1301 return NULL;
1302
1303 return m;
1304}