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