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