blob: 526dcef6f5bb7a2f5f4d7975349c02ef8cbef372 [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]
Larry Hastingsf256c222014-01-25 21:30:37 -0800481module _lzma
482class _lzma.LZMACompressor "Compressor *" "&Compressor_type"
483class _lzma.LZMADecompressor "Decompressor *" "&Decompressor_type"
484[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300485/*[clinic end generated code: output=da39a3ee5e6b4b0d input=2c14bbe05ff0c147]*/
Larry Hastingsf256c222014-01-25 21:30:37 -0800486
487#include "clinic/_lzmamodule.c.h"
488
489/*[python input]
490
491class lzma_vli_converter(CConverter):
492 type = 'lzma_vli'
493 converter = 'lzma_vli_converter'
494
495class lzma_filter_converter(CConverter):
496 type = 'lzma_filter'
497 converter = 'lzma_filter_converter'
498 c_default = c_ignored_default = "{LZMA_VLI_UNKNOWN, NULL}"
499
500 def cleanup(self):
501 name = ensure_legal_c_identifier(self.name)
502 return ('if (%(name)s.id != LZMA_VLI_UNKNOWN)\n'
503 ' PyMem_Free(%(name)s.options);\n') % {'name': name}
504
505[python start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -0800506/*[python end generated code: output=da39a3ee5e6b4b0d input=74fe7631ce377a94]*/
Larry Hastingsf256c222014-01-25 21:30:37 -0800507
508
509/* LZMACompressor class. */
510
511static PyObject *
512compress(Compressor *c, uint8_t *data, size_t len, lzma_action action)
513{
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100514 Py_ssize_t data_size = 0;
Larry Hastingsf256c222014-01-25 21:30:37 -0800515 PyObject *result;
516
517 result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
518 if (result == NULL)
519 return NULL;
520 c->lzs.next_in = data;
521 c->lzs.avail_in = len;
522 c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result);
523 c->lzs.avail_out = PyBytes_GET_SIZE(result);
524 for (;;) {
525 lzma_ret lzret;
526
527 Py_BEGIN_ALLOW_THREADS
528 lzret = lzma_code(&c->lzs, action);
529 data_size = (char *)c->lzs.next_out - PyBytes_AS_STRING(result);
530 Py_END_ALLOW_THREADS
531 if (catch_lzma_error(lzret))
532 goto error;
533 if ((action == LZMA_RUN && c->lzs.avail_in == 0) ||
534 (action == LZMA_FINISH && lzret == LZMA_STREAM_END)) {
535 break;
536 } else if (c->lzs.avail_out == 0) {
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100537 if (grow_buffer(&result, -1) == -1)
Larry Hastingsf256c222014-01-25 21:30:37 -0800538 goto error;
539 c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size;
540 c->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
541 }
542 }
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100543 if (data_size != PyBytes_GET_SIZE(result))
Larry Hastingsf256c222014-01-25 21:30:37 -0800544 if (_PyBytes_Resize(&result, data_size) == -1)
545 goto error;
546 return result;
547
548error:
549 Py_XDECREF(result);
550 return NULL;
551}
552
553/*[clinic input]
554_lzma.LZMACompressor.compress
555
Larry Hastingsf256c222014-01-25 21:30:37 -0800556 data: Py_buffer
557 /
558
559Provide data to the compressor object.
560
561Returns a chunk of compressed data if possible, or b'' otherwise.
562
563When you have finished providing data to the compressor, call the
564flush() method to finish the compression process.
565[clinic start generated code]*/
566
567static PyObject *
568_lzma_LZMACompressor_compress_impl(Compressor *self, Py_buffer *data)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300569/*[clinic end generated code: output=31f615136963e00f input=64019eac7f2cc8d0]*/
Larry Hastingsf256c222014-01-25 21:30:37 -0800570{
571 PyObject *result = NULL;
572
573 ACQUIRE_LOCK(self);
574 if (self->flushed)
575 PyErr_SetString(PyExc_ValueError, "Compressor has been flushed");
576 else
577 result = compress(self, data->buf, data->len, LZMA_RUN);
578 RELEASE_LOCK(self);
579 return result;
580}
581
582/*[clinic input]
583_lzma.LZMACompressor.flush
584
Larry Hastingsf256c222014-01-25 21:30:37 -0800585Finish the compression process.
586
587Returns the compressed data left in internal buffers.
588
589The compressor object may not be used after this method is called.
590[clinic start generated code]*/
591
592static PyObject *
593_lzma_LZMACompressor_flush_impl(Compressor *self)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +0300594/*[clinic end generated code: output=fec21f3e22504f50 input=6b369303f67ad0a8]*/
Larry Hastingsf256c222014-01-25 21:30:37 -0800595{
596 PyObject *result = NULL;
597
598 ACQUIRE_LOCK(self);
599 if (self->flushed) {
600 PyErr_SetString(PyExc_ValueError, "Repeated call to flush()");
601 } else {
602 self->flushed = 1;
603 result = compress(self, NULL, 0, LZMA_FINISH);
604 }
605 RELEASE_LOCK(self);
606 return result;
607}
608
609static PyObject *
610Compressor_getstate(Compressor *self, PyObject *noargs)
611{
612 PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
613 Py_TYPE(self)->tp_name);
614 return NULL;
615}
616
617static int
618Compressor_init_xz(lzma_stream *lzs, int check, uint32_t preset,
619 PyObject *filterspecs)
620{
621 lzma_ret lzret;
622
623 if (filterspecs == Py_None) {
624 lzret = lzma_easy_encoder(lzs, preset, check);
625 } else {
626 lzma_filter filters[LZMA_FILTERS_MAX + 1];
627
628 if (parse_filter_chain_spec(filters, filterspecs) == -1)
629 return -1;
630 lzret = lzma_stream_encoder(lzs, filters, check);
631 free_filter_chain(filters);
632 }
633 if (catch_lzma_error(lzret))
634 return -1;
635 else
636 return 0;
637}
638
639static int
640Compressor_init_alone(lzma_stream *lzs, uint32_t preset, PyObject *filterspecs)
641{
642 lzma_ret lzret;
643
644 if (filterspecs == Py_None) {
645 lzma_options_lzma options;
646
647 if (lzma_lzma_preset(&options, preset)) {
648 PyErr_Format(Error, "Invalid compression preset: %d", preset);
649 return -1;
650 }
651 lzret = lzma_alone_encoder(lzs, &options);
652 } else {
653 lzma_filter filters[LZMA_FILTERS_MAX + 1];
654
655 if (parse_filter_chain_spec(filters, filterspecs) == -1)
656 return -1;
657 if (filters[0].id == LZMA_FILTER_LZMA1 &&
658 filters[1].id == LZMA_VLI_UNKNOWN) {
659 lzret = lzma_alone_encoder(lzs, filters[0].options);
660 } else {
661 PyErr_SetString(PyExc_ValueError,
662 "Invalid filter chain for FORMAT_ALONE - "
663 "must be a single LZMA1 filter");
664 lzret = LZMA_PROG_ERROR;
665 }
666 free_filter_chain(filters);
667 }
668 if (PyErr_Occurred() || catch_lzma_error(lzret))
669 return -1;
670 else
671 return 0;
672}
673
674static int
675Compressor_init_raw(lzma_stream *lzs, PyObject *filterspecs)
676{
677 lzma_filter filters[LZMA_FILTERS_MAX + 1];
678 lzma_ret lzret;
679
680 if (filterspecs == Py_None) {
681 PyErr_SetString(PyExc_ValueError,
682 "Must specify filters for FORMAT_RAW");
683 return -1;
684 }
685 if (parse_filter_chain_spec(filters, filterspecs) == -1)
686 return -1;
687 lzret = lzma_raw_encoder(lzs, filters);
688 free_filter_chain(filters);
689 if (catch_lzma_error(lzret))
690 return -1;
691 else
692 return 0;
693}
694
695/*[-clinic input]
696_lzma.LZMACompressor.__init__
697
Larry Hastingsf256c222014-01-25 21:30:37 -0800698 format: int(c_default="FORMAT_XZ") = FORMAT_XZ
699 The container format to use for the output. This can
700 be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW.
701
702 check: int(c_default="-1") = unspecified
703 The integrity check to use. For FORMAT_XZ, the default
Martin Pantere26da7c2016-06-02 10:07:09 +0000704 is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not support integrity
Larry Hastingsf256c222014-01-25 21:30:37 -0800705 checks; for these formats, check must be omitted, or be CHECK_NONE.
706
707 preset: object = None
708 If provided should be an integer in the range 0-9, optionally
709 OR-ed with the constant PRESET_EXTREME.
710
711 filters: object = None
712 If provided should be a sequence of dicts. Each dict should
713 have an entry for "id" indicating the ID of the filter, plus
714 additional entries for options to the filter.
715
716Create a compressor object for compressing data incrementally.
717
718The settings used by the compressor can be specified either as a
719preset compression level (with the 'preset' argument), or in detail
720as a custom filter chain (with the 'filters' argument). For FORMAT_XZ
721and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset
722level. For FORMAT_RAW, the caller must always specify a filter chain;
723the raw compressor does not support preset compression levels.
724
725For one-shot compression, use the compress() function instead.
726[-clinic start generated code]*/
727static int
728Compressor_init(Compressor *self, PyObject *args, PyObject *kwargs)
729{
730 static char *arg_names[] = {"format", "check", "preset", "filters", NULL};
731 int format = FORMAT_XZ;
732 int check = -1;
733 uint32_t preset = LZMA_PRESET_DEFAULT;
734 PyObject *preset_obj = Py_None;
735 PyObject *filterspecs = Py_None;
736
737 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
738 "|iiOO:LZMACompressor", arg_names,
739 &format, &check, &preset_obj,
740 &filterspecs))
741 return -1;
742
743 if (format != FORMAT_XZ && check != -1 && check != LZMA_CHECK_NONE) {
744 PyErr_SetString(PyExc_ValueError,
745 "Integrity checks are only supported by FORMAT_XZ");
746 return -1;
747 }
748
749 if (preset_obj != Py_None && filterspecs != Py_None) {
750 PyErr_SetString(PyExc_ValueError,
751 "Cannot specify both preset and filter chain");
752 return -1;
753 }
754
755 if (preset_obj != Py_None)
756 if (!uint32_converter(preset_obj, &preset))
757 return -1;
758
759 self->alloc.opaque = NULL;
760 self->alloc.alloc = PyLzma_Malloc;
761 self->alloc.free = PyLzma_Free;
762 self->lzs.allocator = &self->alloc;
763
764#ifdef WITH_THREAD
765 self->lock = PyThread_allocate_lock();
766 if (self->lock == NULL) {
767 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
768 return -1;
769 }
770#endif
771
772 self->flushed = 0;
773 switch (format) {
774 case FORMAT_XZ:
775 if (check == -1)
776 check = LZMA_CHECK_CRC64;
777 if (Compressor_init_xz(&self->lzs, check, preset, filterspecs) != 0)
778 break;
779 return 0;
780
781 case FORMAT_ALONE:
782 if (Compressor_init_alone(&self->lzs, preset, filterspecs) != 0)
783 break;
784 return 0;
785
786 case FORMAT_RAW:
787 if (Compressor_init_raw(&self->lzs, filterspecs) != 0)
788 break;
789 return 0;
790
791 default:
792 PyErr_Format(PyExc_ValueError,
793 "Invalid container format: %d", format);
794 break;
795 }
796
797#ifdef WITH_THREAD
798 PyThread_free_lock(self->lock);
799 self->lock = NULL;
800#endif
801 return -1;
802}
803
804static void
805Compressor_dealloc(Compressor *self)
806{
807 lzma_end(&self->lzs);
808#ifdef WITH_THREAD
809 if (self->lock != NULL)
810 PyThread_free_lock(self->lock);
811#endif
812 Py_TYPE(self)->tp_free((PyObject *)self);
813}
814
815static PyMethodDef Compressor_methods[] = {
816 _LZMA_LZMACOMPRESSOR_COMPRESS_METHODDEF
817 _LZMA_LZMACOMPRESSOR_FLUSH_METHODDEF
818 {"__getstate__", (PyCFunction)Compressor_getstate, METH_NOARGS},
819 {NULL}
820};
821
822PyDoc_STRVAR(Compressor_doc,
823"LZMACompressor(format=FORMAT_XZ, check=-1, preset=None, filters=None)\n"
824"\n"
825"Create a compressor object for compressing data incrementally.\n"
826"\n"
827"format specifies the container format to use for the output. This can\n"
828"be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW.\n"
829"\n"
830"check specifies the integrity check to use. For FORMAT_XZ, the default\n"
831"is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not suport integrity\n"
832"checks; for these formats, check must be omitted, or be CHECK_NONE.\n"
833"\n"
834"The settings used by the compressor can be specified either as a\n"
835"preset compression level (with the 'preset' argument), or in detail\n"
836"as a custom filter chain (with the 'filters' argument). For FORMAT_XZ\n"
837"and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset\n"
838"level. For FORMAT_RAW, the caller must always specify a filter chain;\n"
839"the raw compressor does not support preset compression levels.\n"
840"\n"
841"preset (if provided) should be an integer in the range 0-9, optionally\n"
842"OR-ed with the constant PRESET_EXTREME.\n"
843"\n"
844"filters (if provided) should be a sequence of dicts. Each dict should\n"
845"have an entry for \"id\" indicating the ID of the filter, plus\n"
846"additional entries for options to the filter.\n"
847"\n"
848"For one-shot compression, use the compress() function instead.\n");
849
850static PyTypeObject Compressor_type = {
851 PyVarObject_HEAD_INIT(NULL, 0)
852 "_lzma.LZMACompressor", /* tp_name */
853 sizeof(Compressor), /* tp_basicsize */
854 0, /* tp_itemsize */
855 (destructor)Compressor_dealloc, /* tp_dealloc */
856 0, /* tp_print */
857 0, /* tp_getattr */
858 0, /* tp_setattr */
859 0, /* tp_reserved */
860 0, /* tp_repr */
861 0, /* tp_as_number */
862 0, /* tp_as_sequence */
863 0, /* tp_as_mapping */
864 0, /* tp_hash */
865 0, /* tp_call */
866 0, /* tp_str */
867 0, /* tp_getattro */
868 0, /* tp_setattro */
869 0, /* tp_as_buffer */
870 Py_TPFLAGS_DEFAULT, /* tp_flags */
871 Compressor_doc, /* tp_doc */
872 0, /* tp_traverse */
873 0, /* tp_clear */
874 0, /* tp_richcompare */
875 0, /* tp_weaklistoffset */
876 0, /* tp_iter */
877 0, /* tp_iternext */
878 Compressor_methods, /* tp_methods */
879 0, /* tp_members */
880 0, /* tp_getset */
881 0, /* tp_base */
882 0, /* tp_dict */
883 0, /* tp_descr_get */
884 0, /* tp_descr_set */
885 0, /* tp_dictoffset */
886 (initproc)Compressor_init, /* tp_init */
887 0, /* tp_alloc */
888 PyType_GenericNew, /* tp_new */
889};
890
891
892/* LZMADecompressor class. */
893
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100894/* Decompress data of length d->lzs.avail_in in d->lzs.next_in. The output
895 buffer is allocated dynamically and returned. At most max_length bytes are
896 returned, so some of the input may not be consumed. d->lzs.next_in and
897 d->lzs.avail_in are updated to reflect the consumed input. */
898static PyObject*
899decompress_buf(Decompressor *d, Py_ssize_t max_length)
Larry Hastingsf256c222014-01-25 21:30:37 -0800900{
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100901 Py_ssize_t data_size = 0;
Larry Hastingsf256c222014-01-25 21:30:37 -0800902 PyObject *result;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100903 lzma_stream *lzs = &d->lzs;
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200904
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100905 if (max_length < 0 || max_length >= INITIAL_BUFFER_SIZE)
906 result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
907 else
908 result = PyBytes_FromStringAndSize(NULL, max_length);
Larry Hastingsf256c222014-01-25 21:30:37 -0800909 if (result == NULL)
910 return NULL;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100911
912 lzs->next_out = (uint8_t *)PyBytes_AS_STRING(result);
913 lzs->avail_out = PyBytes_GET_SIZE(result);
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200914
Larry Hastingsf256c222014-01-25 21:30:37 -0800915 for (;;) {
916 lzma_ret lzret;
917
918 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100919 lzret = lzma_code(lzs, LZMA_RUN);
920 data_size = (char *)lzs->next_out - PyBytes_AS_STRING(result);
Larry Hastingsf256c222014-01-25 21:30:37 -0800921 Py_END_ALLOW_THREADS
922 if (catch_lzma_error(lzret))
923 goto error;
924 if (lzret == LZMA_GET_CHECK || lzret == LZMA_NO_CHECK)
925 d->check = lzma_get_check(&d->lzs);
926 if (lzret == LZMA_STREAM_END) {
927 d->eof = 1;
Larry Hastingsf256c222014-01-25 21:30:37 -0800928 break;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100929 } else if (lzs->avail_in == 0) {
Larry Hastingsf256c222014-01-25 21:30:37 -0800930 break;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100931 } else if (lzs->avail_out == 0) {
932 if (data_size == max_length)
933 break;
934 if (grow_buffer(&result, max_length) == -1)
Larry Hastingsf256c222014-01-25 21:30:37 -0800935 goto error;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100936 lzs->next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size;
937 lzs->avail_out = PyBytes_GET_SIZE(result) - data_size;
Larry Hastingsf256c222014-01-25 21:30:37 -0800938 }
939 }
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100940 if (data_size != PyBytes_GET_SIZE(result))
Larry Hastingsf256c222014-01-25 21:30:37 -0800941 if (_PyBytes_Resize(&result, data_size) == -1)
942 goto error;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100943
944 return result;
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200945
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100946error:
947 Py_XDECREF(result);
948 return NULL;
949}
950
951static PyObject *
952decompress(Decompressor *d, uint8_t *data, size_t len, Py_ssize_t max_length)
953{
954 char input_buffer_in_use;
955 PyObject *result;
956 lzma_stream *lzs = &d->lzs;
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200957
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100958 /* Prepend unconsumed input if necessary */
959 if (lzs->next_in != NULL) {
960 size_t avail_now, avail_total;
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200961
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100962 /* Number of bytes we can append to input buffer */
963 avail_now = (d->input_buffer + d->input_buffer_size)
964 - (lzs->next_in + lzs->avail_in);
965
966 /* Number of bytes we can append if we move existing
967 contents to beginning of buffer (overwriting
968 consumed input) */
969 avail_total = d->input_buffer_size - lzs->avail_in;
970
971 if (avail_total < len) {
972 size_t offset = lzs->next_in - d->input_buffer;
973 uint8_t *tmp;
974 size_t new_size = d->input_buffer_size + len - avail_now;
975
976 /* Assign to temporary variable first, so we don't
977 lose address of allocated buffer if realloc fails */
978 tmp = PyMem_Realloc(d->input_buffer, new_size);
979 if (tmp == NULL) {
980 PyErr_SetNone(PyExc_MemoryError);
981 return NULL;
982 }
983 d->input_buffer = tmp;
984 d->input_buffer_size = new_size;
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200985
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100986 lzs->next_in = d->input_buffer + offset;
987 }
988 else if (avail_now < len) {
989 memmove(d->input_buffer, lzs->next_in,
990 lzs->avail_in);
991 lzs->next_in = d->input_buffer;
992 }
993 memcpy((void*)(lzs->next_in + lzs->avail_in), data, len);
994 lzs->avail_in += len;
995 input_buffer_in_use = 1;
996 }
997 else {
998 lzs->next_in = data;
999 lzs->avail_in = len;
1000 input_buffer_in_use = 0;
1001 }
1002
1003 result = decompress_buf(d, max_length);
1004 if(result == NULL)
1005 return NULL;
1006
1007 if (d->eof) {
1008 d->needs_input = 0;
1009 if (lzs->avail_in > 0) {
Serhiy Storchaka48842712016-04-06 09:45:48 +03001010 Py_XSETREF(d->unused_data,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +02001011 PyBytes_FromStringAndSize((char *)lzs->next_in, lzs->avail_in));
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001012 if (d->unused_data == NULL)
1013 goto error;
1014 }
1015 }
1016 else if (lzs->avail_in == 0) {
1017 lzs->next_in = NULL;
1018 d->needs_input = 1;
1019 }
1020 else {
1021 d->needs_input = 0;
1022
1023 /* If we did not use the input buffer, we now have
1024 to copy the tail from the caller's buffer into the
1025 input buffer */
1026 if (!input_buffer_in_use) {
1027
1028 /* Discard buffer if it's too small
1029 (resizing it may needlessly copy the current contents) */
1030 if (d->input_buffer != NULL &&
1031 d->input_buffer_size < lzs->avail_in) {
1032 PyMem_Free(d->input_buffer);
1033 d->input_buffer = NULL;
1034 }
1035
1036 /* Allocate if necessary */
1037 if (d->input_buffer == NULL) {
1038 d->input_buffer = PyMem_Malloc(lzs->avail_in);
1039 if (d->input_buffer == NULL) {
1040 PyErr_SetNone(PyExc_MemoryError);
1041 goto error;
1042 }
1043 d->input_buffer_size = lzs->avail_in;
1044 }
1045
1046 /* Copy tail */
1047 memcpy(d->input_buffer, lzs->next_in, lzs->avail_in);
1048 lzs->next_in = d->input_buffer;
1049 }
1050 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +02001051
Larry Hastingsf256c222014-01-25 21:30:37 -08001052 return result;
1053
1054error:
1055 Py_XDECREF(result);
1056 return NULL;
1057}
1058
1059/*[clinic input]
1060_lzma.LZMADecompressor.decompress
1061
Larry Hastingsf256c222014-01-25 21:30:37 -08001062 data: Py_buffer
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001063 max_length: Py_ssize_t=-1
Larry Hastingsf256c222014-01-25 21:30:37 -08001064
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001065Decompress *data*, returning uncompressed data as bytes.
Larry Hastingsf256c222014-01-25 21:30:37 -08001066
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001067If *max_length* is nonnegative, returns at most *max_length* bytes of
1068decompressed data. If this limit is reached and further output can be
1069produced, *self.needs_input* will be set to ``False``. In this case, the next
1070call to *decompress()* may provide *data* as b'' to obtain more of the output.
Larry Hastingsf256c222014-01-25 21:30:37 -08001071
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001072If all of the input data was decompressed and returned (either because this
1073was less than *max_length* bytes, or because *max_length* was negative),
1074*self.needs_input* will be set to True.
1075
1076Attempting to decompress data after the end of stream is reached raises an
1077EOFError. Any data found after the end of the stream is ignored and saved in
1078the unused_data attribute.
Larry Hastingsf256c222014-01-25 21:30:37 -08001079[clinic start generated code]*/
1080
1081static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001082_lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data,
1083 Py_ssize_t max_length)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001084/*[clinic end generated code: output=ef4e20ec7122241d input=60c1f135820e309d]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001085{
1086 PyObject *result = NULL;
1087
1088 ACQUIRE_LOCK(self);
1089 if (self->eof)
1090 PyErr_SetString(PyExc_EOFError, "Already at end of stream");
1091 else
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001092 result = decompress(self, data->buf, data->len, max_length);
Larry Hastingsf256c222014-01-25 21:30:37 -08001093 RELEASE_LOCK(self);
1094 return result;
1095}
1096
1097static PyObject *
1098Decompressor_getstate(Decompressor *self, PyObject *noargs)
1099{
1100 PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
1101 Py_TYPE(self)->tp_name);
1102 return NULL;
1103}
1104
1105static int
1106Decompressor_init_raw(lzma_stream *lzs, PyObject *filterspecs)
1107{
1108 lzma_filter filters[LZMA_FILTERS_MAX + 1];
1109 lzma_ret lzret;
1110
1111 if (parse_filter_chain_spec(filters, filterspecs) == -1)
1112 return -1;
1113 lzret = lzma_raw_decoder(lzs, filters);
1114 free_filter_chain(filters);
1115 if (catch_lzma_error(lzret))
1116 return -1;
1117 else
1118 return 0;
1119}
1120
1121/*[clinic input]
1122_lzma.LZMADecompressor.__init__
1123
Larry Hastingsf256c222014-01-25 21:30:37 -08001124 format: int(c_default="FORMAT_AUTO") = FORMAT_AUTO
1125 Specifies the container format of the input stream. If this is
1126 FORMAT_AUTO (the default), the decompressor will automatically detect
1127 whether the input is FORMAT_XZ or FORMAT_ALONE. Streams created with
1128 FORMAT_RAW cannot be autodetected.
1129
1130 memlimit: object = None
1131 Limit the amount of memory used by the decompressor. This will cause
1132 decompression to fail if the input cannot be decompressed within the
1133 given limit.
1134
1135 filters: object = None
1136 A custom filter chain. This argument is required for FORMAT_RAW, and
1137 not accepted with any other format. When provided, this should be a
1138 sequence of dicts, each indicating the ID and options for a single
1139 filter.
1140
1141Create a decompressor object for decompressing data incrementally.
1142
1143For one-shot decompression, use the decompress() function instead.
1144[clinic start generated code]*/
1145
1146static int
Larry Hastings89964c42015-04-14 18:07:59 -04001147_lzma_LZMADecompressor___init___impl(Decompressor *self, int format,
1148 PyObject *memlimit, PyObject *filters)
Serhiy Storchaka7a9579c2016-05-02 13:45:20 +03001149/*[clinic end generated code: output=3e1821f8aa36564c input=81fe684a6c2f8a27]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001150{
1151 const uint32_t decoder_flags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK;
1152 uint64_t memlimit_ = UINT64_MAX;
1153 lzma_ret lzret;
1154
1155 if (memlimit != Py_None) {
1156 if (format == FORMAT_RAW) {
1157 PyErr_SetString(PyExc_ValueError,
1158 "Cannot specify memory limit with FORMAT_RAW");
1159 return -1;
1160 }
1161 memlimit_ = PyLong_AsUnsignedLongLong(memlimit);
1162 if (PyErr_Occurred())
1163 return -1;
1164 }
1165
1166 if (format == FORMAT_RAW && filters == Py_None) {
1167 PyErr_SetString(PyExc_ValueError,
1168 "Must specify filters for FORMAT_RAW");
1169 return -1;
1170 } else if (format != FORMAT_RAW && filters != Py_None) {
1171 PyErr_SetString(PyExc_ValueError,
1172 "Cannot specify filters except with FORMAT_RAW");
1173 return -1;
1174 }
1175
1176 self->alloc.opaque = NULL;
1177 self->alloc.alloc = PyLzma_Malloc;
1178 self->alloc.free = PyLzma_Free;
1179 self->lzs.allocator = &self->alloc;
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001180 self->lzs.next_in = NULL;
Larry Hastingsf256c222014-01-25 21:30:37 -08001181
1182#ifdef WITH_THREAD
1183 self->lock = PyThread_allocate_lock();
1184 if (self->lock == NULL) {
1185 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
1186 return -1;
1187 }
1188#endif
1189
1190 self->check = LZMA_CHECK_UNKNOWN;
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001191 self->needs_input = 1;
1192 self->input_buffer = NULL;
1193 self->input_buffer_size = 0;
Larry Hastingsf256c222014-01-25 21:30:37 -08001194 self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
1195 if (self->unused_data == NULL)
1196 goto error;
1197
1198 switch (format) {
1199 case FORMAT_AUTO:
1200 lzret = lzma_auto_decoder(&self->lzs, memlimit_, decoder_flags);
1201 if (catch_lzma_error(lzret))
1202 break;
1203 return 0;
1204
1205 case FORMAT_XZ:
1206 lzret = lzma_stream_decoder(&self->lzs, memlimit_, decoder_flags);
1207 if (catch_lzma_error(lzret))
1208 break;
1209 return 0;
1210
1211 case FORMAT_ALONE:
1212 self->check = LZMA_CHECK_NONE;
1213 lzret = lzma_alone_decoder(&self->lzs, memlimit_);
1214 if (catch_lzma_error(lzret))
1215 break;
1216 return 0;
1217
1218 case FORMAT_RAW:
1219 self->check = LZMA_CHECK_NONE;
1220 if (Decompressor_init_raw(&self->lzs, filters) == -1)
1221 break;
1222 return 0;
1223
1224 default:
1225 PyErr_Format(PyExc_ValueError,
1226 "Invalid container format: %d", format);
1227 break;
1228 }
1229
1230error:
1231 Py_CLEAR(self->unused_data);
1232#ifdef WITH_THREAD
1233 PyThread_free_lock(self->lock);
1234 self->lock = NULL;
1235#endif
1236 return -1;
1237}
1238
1239static void
1240Decompressor_dealloc(Decompressor *self)
1241{
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001242 if(self->input_buffer != NULL)
1243 PyMem_Free(self->input_buffer);
Serhiy Storchaka009b8112015-03-18 21:53:15 +02001244
Larry Hastingsf256c222014-01-25 21:30:37 -08001245 lzma_end(&self->lzs);
1246 Py_CLEAR(self->unused_data);
1247#ifdef WITH_THREAD
1248 if (self->lock != NULL)
1249 PyThread_free_lock(self->lock);
1250#endif
1251 Py_TYPE(self)->tp_free((PyObject *)self);
1252}
1253
1254static PyMethodDef Decompressor_methods[] = {
1255 _LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF
1256 {"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS},
1257 {NULL}
1258};
1259
1260PyDoc_STRVAR(Decompressor_check_doc,
1261"ID of the integrity check used by the input stream.");
1262
1263PyDoc_STRVAR(Decompressor_eof_doc,
1264"True if the end-of-stream marker has been reached.");
1265
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001266PyDoc_STRVAR(Decompressor_needs_input_doc,
1267"True if more input is needed before more decompressed data can be produced.");
1268
Larry Hastingsf256c222014-01-25 21:30:37 -08001269PyDoc_STRVAR(Decompressor_unused_data_doc,
1270"Data found after the end of the compressed stream.");
1271
1272static PyMemberDef Decompressor_members[] = {
1273 {"check", T_INT, offsetof(Decompressor, check), READONLY,
1274 Decompressor_check_doc},
1275 {"eof", T_BOOL, offsetof(Decompressor, eof), READONLY,
1276 Decompressor_eof_doc},
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001277 {"needs_input", T_BOOL, offsetof(Decompressor, needs_input), READONLY,
1278 Decompressor_needs_input_doc},
Larry Hastingsf256c222014-01-25 21:30:37 -08001279 {"unused_data", T_OBJECT_EX, offsetof(Decompressor, unused_data), READONLY,
1280 Decompressor_unused_data_doc},
1281 {NULL}
1282};
1283
1284static PyTypeObject Decompressor_type = {
1285 PyVarObject_HEAD_INIT(NULL, 0)
1286 "_lzma.LZMADecompressor", /* tp_name */
1287 sizeof(Decompressor), /* tp_basicsize */
1288 0, /* tp_itemsize */
1289 (destructor)Decompressor_dealloc, /* tp_dealloc */
1290 0, /* tp_print */
1291 0, /* tp_getattr */
1292 0, /* tp_setattr */
1293 0, /* tp_reserved */
1294 0, /* tp_repr */
1295 0, /* tp_as_number */
1296 0, /* tp_as_sequence */
1297 0, /* tp_as_mapping */
1298 0, /* tp_hash */
1299 0, /* tp_call */
1300 0, /* tp_str */
1301 0, /* tp_getattro */
1302 0, /* tp_setattro */
1303 0, /* tp_as_buffer */
1304 Py_TPFLAGS_DEFAULT, /* tp_flags */
1305 _lzma_LZMADecompressor___init____doc__, /* tp_doc */
1306 0, /* tp_traverse */
1307 0, /* tp_clear */
1308 0, /* tp_richcompare */
1309 0, /* tp_weaklistoffset */
1310 0, /* tp_iter */
1311 0, /* tp_iternext */
1312 Decompressor_methods, /* tp_methods */
1313 Decompressor_members, /* tp_members */
1314 0, /* tp_getset */
1315 0, /* tp_base */
1316 0, /* tp_dict */
1317 0, /* tp_descr_get */
1318 0, /* tp_descr_set */
1319 0, /* tp_dictoffset */
1320 _lzma_LZMADecompressor___init__, /* tp_init */
1321 0, /* tp_alloc */
1322 PyType_GenericNew, /* tp_new */
1323};
1324
1325
1326/* Module-level functions. */
1327
1328/*[clinic input]
1329_lzma.is_check_supported
1330 check_id: int
1331 /
1332
1333Test whether the given integrity check is supported.
1334
1335Always returns True for CHECK_NONE and CHECK_CRC32.
1336[clinic start generated code]*/
1337
1338static PyObject *
1339_lzma_is_check_supported_impl(PyModuleDef *module, int check_id)
Larry Hastings581ee362014-01-28 05:00:08 -08001340/*[clinic end generated code: output=bb828e90e00ad96e input=5518297b97b2318f]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001341{
1342 return PyBool_FromLong(lzma_check_is_supported(check_id));
1343}
1344
1345
1346/*[clinic input]
1347_lzma._encode_filter_properties
1348 filter: lzma_filter(c_default="{LZMA_VLI_UNKNOWN, NULL}")
1349 /
1350
1351Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict).
1352
1353The result does not include the filter ID itself, only the options.
1354[clinic start generated code]*/
1355
1356static PyObject *
1357_lzma__encode_filter_properties_impl(PyModuleDef *module, lzma_filter filter)
Larry Hastings581ee362014-01-28 05:00:08 -08001358/*[clinic end generated code: output=b5fe690acd6b61d1 input=d4c64f1b557c77d4]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001359{
1360 lzma_ret lzret;
1361 uint32_t encoded_size;
1362 PyObject *result = NULL;
1363
1364 lzret = lzma_properties_size(&encoded_size, &filter);
1365 if (catch_lzma_error(lzret))
1366 goto error;
1367
1368 result = PyBytes_FromStringAndSize(NULL, encoded_size);
1369 if (result == NULL)
1370 goto error;
1371
1372 lzret = lzma_properties_encode(
1373 &filter, (uint8_t *)PyBytes_AS_STRING(result));
1374 if (catch_lzma_error(lzret))
1375 goto error;
1376
1377 return result;
1378
1379error:
1380 Py_XDECREF(result);
1381 return NULL;
1382}
1383
1384
1385/*[clinic input]
1386_lzma._decode_filter_properties
1387 filter_id: lzma_vli
1388 encoded_props: Py_buffer
1389 /
1390
1391Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict).
1392
1393The result does not include the filter ID itself, only the options.
1394[clinic start generated code]*/
1395
1396static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001397_lzma__decode_filter_properties_impl(PyModuleDef *module, lzma_vli filter_id,
1398 Py_buffer *encoded_props)
1399/*[clinic end generated code: output=af248f570746668b input=246410800782160c]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001400{
1401 lzma_filter filter;
1402 lzma_ret lzret;
1403 PyObject *result = NULL;
1404 filter.id = filter_id;
1405
1406 lzret = lzma_properties_decode(
1407 &filter, NULL, encoded_props->buf, encoded_props->len);
1408 if (catch_lzma_error(lzret))
1409 return NULL;
1410
1411 result = build_filter_spec(&filter);
1412
1413 /* We use vanilla free() here instead of PyMem_Free() - filter.options was
1414 allocated by lzma_properties_decode() using the default allocator. */
1415 free(filter.options);
1416 return result;
1417}
1418
1419
1420/* Module initialization. */
1421
1422static PyMethodDef module_methods[] = {
1423 _LZMA_IS_CHECK_SUPPORTED_METHODDEF
1424 _LZMA__ENCODE_FILTER_PROPERTIES_METHODDEF
1425 _LZMA__DECODE_FILTER_PROPERTIES_METHODDEF
1426 {NULL}
1427};
1428
1429static PyModuleDef _lzmamodule = {
1430 PyModuleDef_HEAD_INIT,
1431 "_lzma",
1432 NULL,
1433 -1,
1434 module_methods,
1435 NULL,
1436 NULL,
1437 NULL,
1438 NULL,
1439};
1440
1441/* Some of our constants are more than 32 bits wide, so PyModule_AddIntConstant
1442 would not work correctly on platforms with 32-bit longs. */
1443static int
1444module_add_int_constant(PyObject *m, const char *name, PY_LONG_LONG value)
1445{
1446 PyObject *o = PyLong_FromLongLong(value);
1447 if (o == NULL)
1448 return -1;
1449 if (PyModule_AddObject(m, name, o) == 0)
1450 return 0;
1451 Py_DECREF(o);
1452 return -1;
1453}
1454
1455#define ADD_INT_PREFIX_MACRO(m, macro) \
1456 module_add_int_constant(m, #macro, LZMA_ ## macro)
1457
1458PyMODINIT_FUNC
1459PyInit__lzma(void)
1460{
1461 PyObject *m;
1462
1463 empty_tuple = PyTuple_New(0);
1464 if (empty_tuple == NULL)
1465 return NULL;
1466
1467 m = PyModule_Create(&_lzmamodule);
1468 if (m == NULL)
1469 return NULL;
1470
1471 if (PyModule_AddIntMacro(m, FORMAT_AUTO) == -1 ||
1472 PyModule_AddIntMacro(m, FORMAT_XZ) == -1 ||
1473 PyModule_AddIntMacro(m, FORMAT_ALONE) == -1 ||
1474 PyModule_AddIntMacro(m, FORMAT_RAW) == -1 ||
1475 ADD_INT_PREFIX_MACRO(m, CHECK_NONE) == -1 ||
1476 ADD_INT_PREFIX_MACRO(m, CHECK_CRC32) == -1 ||
1477 ADD_INT_PREFIX_MACRO(m, CHECK_CRC64) == -1 ||
1478 ADD_INT_PREFIX_MACRO(m, CHECK_SHA256) == -1 ||
1479 ADD_INT_PREFIX_MACRO(m, CHECK_ID_MAX) == -1 ||
1480 ADD_INT_PREFIX_MACRO(m, CHECK_UNKNOWN) == -1 ||
1481 ADD_INT_PREFIX_MACRO(m, FILTER_LZMA1) == -1 ||
1482 ADD_INT_PREFIX_MACRO(m, FILTER_LZMA2) == -1 ||
1483 ADD_INT_PREFIX_MACRO(m, FILTER_DELTA) == -1 ||
1484 ADD_INT_PREFIX_MACRO(m, FILTER_X86) == -1 ||
1485 ADD_INT_PREFIX_MACRO(m, FILTER_IA64) == -1 ||
1486 ADD_INT_PREFIX_MACRO(m, FILTER_ARM) == -1 ||
1487 ADD_INT_PREFIX_MACRO(m, FILTER_ARMTHUMB) == -1 ||
1488 ADD_INT_PREFIX_MACRO(m, FILTER_SPARC) == -1 ||
1489 ADD_INT_PREFIX_MACRO(m, FILTER_POWERPC) == -1 ||
1490 ADD_INT_PREFIX_MACRO(m, MF_HC3) == -1 ||
1491 ADD_INT_PREFIX_MACRO(m, MF_HC4) == -1 ||
1492 ADD_INT_PREFIX_MACRO(m, MF_BT2) == -1 ||
1493 ADD_INT_PREFIX_MACRO(m, MF_BT3) == -1 ||
1494 ADD_INT_PREFIX_MACRO(m, MF_BT4) == -1 ||
1495 ADD_INT_PREFIX_MACRO(m, MODE_FAST) == -1 ||
1496 ADD_INT_PREFIX_MACRO(m, MODE_NORMAL) == -1 ||
1497 ADD_INT_PREFIX_MACRO(m, PRESET_DEFAULT) == -1 ||
1498 ADD_INT_PREFIX_MACRO(m, PRESET_EXTREME) == -1)
1499 return NULL;
1500
1501 Error = PyErr_NewExceptionWithDoc(
1502 "_lzma.LZMAError", "Call to liblzma failed.", NULL, NULL);
1503 if (Error == NULL)
1504 return NULL;
1505 Py_INCREF(Error);
1506 if (PyModule_AddObject(m, "LZMAError", Error) == -1)
1507 return NULL;
1508
1509 if (PyType_Ready(&Compressor_type) == -1)
1510 return NULL;
1511 Py_INCREF(&Compressor_type);
1512 if (PyModule_AddObject(m, "LZMACompressor",
1513 (PyObject *)&Compressor_type) == -1)
1514 return NULL;
1515
1516 if (PyType_Ready(&Decompressor_type) == -1)
1517 return NULL;
1518 Py_INCREF(&Decompressor_type);
1519 if (PyModule_AddObject(m, "LZMADecompressor",
1520 (PyObject *)&Decompressor_type) == -1)
1521 return NULL;
1522
1523 return m;
1524}