blob: e7b1ed9fb2bce7a178dadeb0476e2c0232a8e4e8 [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);
Serhiy Storchaka04f17f12016-10-31 08:30:09 +0200530 if (lzret == LZMA_BUF_ERROR && len == 0 && c->lzs.avail_out > 0)
531 lzret = LZMA_OK; /* That wasn't a real error */
Larry Hastingsf256c222014-01-25 21:30:37 -0800532 Py_END_ALLOW_THREADS
533 if (catch_lzma_error(lzret))
534 goto error;
535 if ((action == LZMA_RUN && c->lzs.avail_in == 0) ||
536 (action == LZMA_FINISH && lzret == LZMA_STREAM_END)) {
537 break;
538 } else if (c->lzs.avail_out == 0) {
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100539 if (grow_buffer(&result, -1) == -1)
Larry Hastingsf256c222014-01-25 21:30:37 -0800540 goto error;
541 c->lzs.next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size;
542 c->lzs.avail_out = PyBytes_GET_SIZE(result) - data_size;
543 }
544 }
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100545 if (data_size != PyBytes_GET_SIZE(result))
Larry Hastingsf256c222014-01-25 21:30:37 -0800546 if (_PyBytes_Resize(&result, data_size) == -1)
547 goto error;
548 return result;
549
550error:
551 Py_XDECREF(result);
552 return NULL;
553}
554
555/*[clinic input]
556_lzma.LZMACompressor.compress
557
558 self: self(type="Compressor *")
559 data: Py_buffer
560 /
561
562Provide data to the compressor object.
563
564Returns a chunk of compressed data if possible, or b'' otherwise.
565
566When you have finished providing data to the compressor, call the
567flush() method to finish the compression process.
568[clinic start generated code]*/
569
570static PyObject *
571_lzma_LZMACompressor_compress_impl(Compressor *self, Py_buffer *data)
Larry Hastings581ee362014-01-28 05:00:08 -0800572/*[clinic end generated code: output=31f615136963e00f input=8b60cb13e0ce6420]*/
Larry Hastingsf256c222014-01-25 21:30:37 -0800573{
574 PyObject *result = NULL;
575
576 ACQUIRE_LOCK(self);
577 if (self->flushed)
578 PyErr_SetString(PyExc_ValueError, "Compressor has been flushed");
579 else
580 result = compress(self, data->buf, data->len, LZMA_RUN);
581 RELEASE_LOCK(self);
582 return result;
583}
584
585/*[clinic input]
586_lzma.LZMACompressor.flush
587
588 self: self(type="Compressor *")
589
590Finish the compression process.
591
592Returns the compressed data left in internal buffers.
593
594The compressor object may not be used after this method is called.
595[clinic start generated code]*/
596
597static PyObject *
598_lzma_LZMACompressor_flush_impl(Compressor *self)
Larry Hastings581ee362014-01-28 05:00:08 -0800599/*[clinic end generated code: output=fec21f3e22504f50 input=3060fb26f9b4042c]*/
Larry Hastingsf256c222014-01-25 21:30:37 -0800600{
601 PyObject *result = NULL;
602
603 ACQUIRE_LOCK(self);
604 if (self->flushed) {
605 PyErr_SetString(PyExc_ValueError, "Repeated call to flush()");
606 } else {
607 self->flushed = 1;
608 result = compress(self, NULL, 0, LZMA_FINISH);
609 }
610 RELEASE_LOCK(self);
611 return result;
612}
613
614static PyObject *
615Compressor_getstate(Compressor *self, PyObject *noargs)
616{
617 PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
618 Py_TYPE(self)->tp_name);
619 return NULL;
620}
621
622static int
623Compressor_init_xz(lzma_stream *lzs, int check, uint32_t preset,
624 PyObject *filterspecs)
625{
626 lzma_ret lzret;
627
628 if (filterspecs == Py_None) {
629 lzret = lzma_easy_encoder(lzs, preset, check);
630 } else {
631 lzma_filter filters[LZMA_FILTERS_MAX + 1];
632
633 if (parse_filter_chain_spec(filters, filterspecs) == -1)
634 return -1;
635 lzret = lzma_stream_encoder(lzs, filters, check);
636 free_filter_chain(filters);
637 }
638 if (catch_lzma_error(lzret))
639 return -1;
640 else
641 return 0;
642}
643
644static int
645Compressor_init_alone(lzma_stream *lzs, uint32_t preset, PyObject *filterspecs)
646{
647 lzma_ret lzret;
648
649 if (filterspecs == Py_None) {
650 lzma_options_lzma options;
651
652 if (lzma_lzma_preset(&options, preset)) {
653 PyErr_Format(Error, "Invalid compression preset: %d", preset);
654 return -1;
655 }
656 lzret = lzma_alone_encoder(lzs, &options);
657 } else {
658 lzma_filter filters[LZMA_FILTERS_MAX + 1];
659
660 if (parse_filter_chain_spec(filters, filterspecs) == -1)
661 return -1;
662 if (filters[0].id == LZMA_FILTER_LZMA1 &&
663 filters[1].id == LZMA_VLI_UNKNOWN) {
664 lzret = lzma_alone_encoder(lzs, filters[0].options);
665 } else {
666 PyErr_SetString(PyExc_ValueError,
667 "Invalid filter chain for FORMAT_ALONE - "
668 "must be a single LZMA1 filter");
669 lzret = LZMA_PROG_ERROR;
670 }
671 free_filter_chain(filters);
672 }
673 if (PyErr_Occurred() || catch_lzma_error(lzret))
674 return -1;
675 else
676 return 0;
677}
678
679static int
680Compressor_init_raw(lzma_stream *lzs, PyObject *filterspecs)
681{
682 lzma_filter filters[LZMA_FILTERS_MAX + 1];
683 lzma_ret lzret;
684
685 if (filterspecs == Py_None) {
686 PyErr_SetString(PyExc_ValueError,
687 "Must specify filters for FORMAT_RAW");
688 return -1;
689 }
690 if (parse_filter_chain_spec(filters, filterspecs) == -1)
691 return -1;
692 lzret = lzma_raw_encoder(lzs, filters);
693 free_filter_chain(filters);
694 if (catch_lzma_error(lzret))
695 return -1;
696 else
697 return 0;
698}
699
700/*[-clinic input]
701_lzma.LZMACompressor.__init__
702
703 self: self(type="Compressor *")
704 format: int(c_default="FORMAT_XZ") = FORMAT_XZ
705 The container format to use for the output. This can
706 be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW.
707
708 check: int(c_default="-1") = unspecified
709 The integrity check to use. For FORMAT_XZ, the default
Martin Pantere26da7c2016-06-02 10:07:09 +0000710 is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not support integrity
Larry Hastingsf256c222014-01-25 21:30:37 -0800711 checks; for these formats, check must be omitted, or be CHECK_NONE.
712
713 preset: object = None
714 If provided should be an integer in the range 0-9, optionally
715 OR-ed with the constant PRESET_EXTREME.
716
717 filters: object = None
718 If provided should be a sequence of dicts. Each dict should
719 have an entry for "id" indicating the ID of the filter, plus
720 additional entries for options to the filter.
721
722Create a compressor object for compressing data incrementally.
723
724The settings used by the compressor can be specified either as a
725preset compression level (with the 'preset' argument), or in detail
726as a custom filter chain (with the 'filters' argument). For FORMAT_XZ
727and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset
728level. For FORMAT_RAW, the caller must always specify a filter chain;
729the raw compressor does not support preset compression levels.
730
731For one-shot compression, use the compress() function instead.
732[-clinic start generated code]*/
733static int
734Compressor_init(Compressor *self, PyObject *args, PyObject *kwargs)
735{
736 static char *arg_names[] = {"format", "check", "preset", "filters", NULL};
737 int format = FORMAT_XZ;
738 int check = -1;
739 uint32_t preset = LZMA_PRESET_DEFAULT;
740 PyObject *preset_obj = Py_None;
741 PyObject *filterspecs = Py_None;
742
743 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
744 "|iiOO:LZMACompressor", arg_names,
745 &format, &check, &preset_obj,
746 &filterspecs))
747 return -1;
748
749 if (format != FORMAT_XZ && check != -1 && check != LZMA_CHECK_NONE) {
750 PyErr_SetString(PyExc_ValueError,
751 "Integrity checks are only supported by FORMAT_XZ");
752 return -1;
753 }
754
755 if (preset_obj != Py_None && filterspecs != Py_None) {
756 PyErr_SetString(PyExc_ValueError,
757 "Cannot specify both preset and filter chain");
758 return -1;
759 }
760
761 if (preset_obj != Py_None)
762 if (!uint32_converter(preset_obj, &preset))
763 return -1;
764
765 self->alloc.opaque = NULL;
766 self->alloc.alloc = PyLzma_Malloc;
767 self->alloc.free = PyLzma_Free;
768 self->lzs.allocator = &self->alloc;
769
770#ifdef WITH_THREAD
771 self->lock = PyThread_allocate_lock();
772 if (self->lock == NULL) {
773 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
774 return -1;
775 }
776#endif
777
778 self->flushed = 0;
779 switch (format) {
780 case FORMAT_XZ:
781 if (check == -1)
782 check = LZMA_CHECK_CRC64;
783 if (Compressor_init_xz(&self->lzs, check, preset, filterspecs) != 0)
784 break;
785 return 0;
786
787 case FORMAT_ALONE:
788 if (Compressor_init_alone(&self->lzs, preset, filterspecs) != 0)
789 break;
790 return 0;
791
792 case FORMAT_RAW:
793 if (Compressor_init_raw(&self->lzs, filterspecs) != 0)
794 break;
795 return 0;
796
797 default:
798 PyErr_Format(PyExc_ValueError,
799 "Invalid container format: %d", format);
800 break;
801 }
802
803#ifdef WITH_THREAD
804 PyThread_free_lock(self->lock);
805 self->lock = NULL;
806#endif
807 return -1;
808}
809
810static void
811Compressor_dealloc(Compressor *self)
812{
813 lzma_end(&self->lzs);
814#ifdef WITH_THREAD
815 if (self->lock != NULL)
816 PyThread_free_lock(self->lock);
817#endif
818 Py_TYPE(self)->tp_free((PyObject *)self);
819}
820
821static PyMethodDef Compressor_methods[] = {
822 _LZMA_LZMACOMPRESSOR_COMPRESS_METHODDEF
823 _LZMA_LZMACOMPRESSOR_FLUSH_METHODDEF
824 {"__getstate__", (PyCFunction)Compressor_getstate, METH_NOARGS},
825 {NULL}
826};
827
828PyDoc_STRVAR(Compressor_doc,
829"LZMACompressor(format=FORMAT_XZ, check=-1, preset=None, filters=None)\n"
830"\n"
831"Create a compressor object for compressing data incrementally.\n"
832"\n"
833"format specifies the container format to use for the output. This can\n"
834"be FORMAT_XZ (default), FORMAT_ALONE, or FORMAT_RAW.\n"
835"\n"
836"check specifies the integrity check to use. For FORMAT_XZ, the default\n"
837"is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not suport integrity\n"
838"checks; for these formats, check must be omitted, or be CHECK_NONE.\n"
839"\n"
840"The settings used by the compressor can be specified either as a\n"
841"preset compression level (with the 'preset' argument), or in detail\n"
842"as a custom filter chain (with the 'filters' argument). For FORMAT_XZ\n"
843"and FORMAT_ALONE, the default is to use the PRESET_DEFAULT preset\n"
844"level. For FORMAT_RAW, the caller must always specify a filter chain;\n"
845"the raw compressor does not support preset compression levels.\n"
846"\n"
847"preset (if provided) should be an integer in the range 0-9, optionally\n"
848"OR-ed with the constant PRESET_EXTREME.\n"
849"\n"
850"filters (if provided) should be a sequence of dicts. Each dict should\n"
851"have an entry for \"id\" indicating the ID of the filter, plus\n"
852"additional entries for options to the filter.\n"
853"\n"
854"For one-shot compression, use the compress() function instead.\n");
855
856static PyTypeObject Compressor_type = {
857 PyVarObject_HEAD_INIT(NULL, 0)
858 "_lzma.LZMACompressor", /* tp_name */
859 sizeof(Compressor), /* tp_basicsize */
860 0, /* tp_itemsize */
861 (destructor)Compressor_dealloc, /* tp_dealloc */
862 0, /* tp_print */
863 0, /* tp_getattr */
864 0, /* tp_setattr */
865 0, /* tp_reserved */
866 0, /* tp_repr */
867 0, /* tp_as_number */
868 0, /* tp_as_sequence */
869 0, /* tp_as_mapping */
870 0, /* tp_hash */
871 0, /* tp_call */
872 0, /* tp_str */
873 0, /* tp_getattro */
874 0, /* tp_setattro */
875 0, /* tp_as_buffer */
876 Py_TPFLAGS_DEFAULT, /* tp_flags */
877 Compressor_doc, /* tp_doc */
878 0, /* tp_traverse */
879 0, /* tp_clear */
880 0, /* tp_richcompare */
881 0, /* tp_weaklistoffset */
882 0, /* tp_iter */
883 0, /* tp_iternext */
884 Compressor_methods, /* tp_methods */
885 0, /* tp_members */
886 0, /* tp_getset */
887 0, /* tp_base */
888 0, /* tp_dict */
889 0, /* tp_descr_get */
890 0, /* tp_descr_set */
891 0, /* tp_dictoffset */
892 (initproc)Compressor_init, /* tp_init */
893 0, /* tp_alloc */
894 PyType_GenericNew, /* tp_new */
895};
896
897
898/* LZMADecompressor class. */
899
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100900/* Decompress data of length d->lzs.avail_in in d->lzs.next_in. The output
901 buffer is allocated dynamically and returned. At most max_length bytes are
902 returned, so some of the input may not be consumed. d->lzs.next_in and
903 d->lzs.avail_in are updated to reflect the consumed input. */
904static PyObject*
905decompress_buf(Decompressor *d, Py_ssize_t max_length)
Larry Hastingsf256c222014-01-25 21:30:37 -0800906{
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100907 Py_ssize_t data_size = 0;
Larry Hastingsf256c222014-01-25 21:30:37 -0800908 PyObject *result;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100909 lzma_stream *lzs = &d->lzs;
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200910
Serhiy Storchaka04f17f12016-10-31 08:30:09 +0200911 if (lzs->avail_in == 0)
912 return PyBytes_FromStringAndSize(NULL, 0);
913
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100914 if (max_length < 0 || max_length >= INITIAL_BUFFER_SIZE)
915 result = PyBytes_FromStringAndSize(NULL, INITIAL_BUFFER_SIZE);
916 else
917 result = PyBytes_FromStringAndSize(NULL, max_length);
Larry Hastingsf256c222014-01-25 21:30:37 -0800918 if (result == NULL)
919 return NULL;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100920
921 lzs->next_out = (uint8_t *)PyBytes_AS_STRING(result);
922 lzs->avail_out = PyBytes_GET_SIZE(result);
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200923
Larry Hastingsf256c222014-01-25 21:30:37 -0800924 for (;;) {
925 lzma_ret lzret;
926
927 Py_BEGIN_ALLOW_THREADS
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100928 lzret = lzma_code(lzs, LZMA_RUN);
929 data_size = (char *)lzs->next_out - PyBytes_AS_STRING(result);
Larry Hastingsf256c222014-01-25 21:30:37 -0800930 Py_END_ALLOW_THREADS
931 if (catch_lzma_error(lzret))
932 goto error;
933 if (lzret == LZMA_GET_CHECK || lzret == LZMA_NO_CHECK)
934 d->check = lzma_get_check(&d->lzs);
935 if (lzret == LZMA_STREAM_END) {
936 d->eof = 1;
Larry Hastingsf256c222014-01-25 21:30:37 -0800937 break;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100938 } else if (lzs->avail_in == 0) {
Larry Hastingsf256c222014-01-25 21:30:37 -0800939 break;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100940 } else if (lzs->avail_out == 0) {
941 if (data_size == max_length)
942 break;
943 if (grow_buffer(&result, max_length) == -1)
Larry Hastingsf256c222014-01-25 21:30:37 -0800944 goto error;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100945 lzs->next_out = (uint8_t *)PyBytes_AS_STRING(result) + data_size;
946 lzs->avail_out = PyBytes_GET_SIZE(result) - data_size;
Larry Hastingsf256c222014-01-25 21:30:37 -0800947 }
948 }
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100949 if (data_size != PyBytes_GET_SIZE(result))
Larry Hastingsf256c222014-01-25 21:30:37 -0800950 if (_PyBytes_Resize(&result, data_size) == -1)
951 goto error;
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100952
953 return result;
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200954
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100955error:
956 Py_XDECREF(result);
957 return NULL;
958}
959
960static PyObject *
961decompress(Decompressor *d, uint8_t *data, size_t len, Py_ssize_t max_length)
962{
963 char input_buffer_in_use;
964 PyObject *result;
965 lzma_stream *lzs = &d->lzs;
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200966
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100967 /* Prepend unconsumed input if necessary */
968 if (lzs->next_in != NULL) {
969 size_t avail_now, avail_total;
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200970
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100971 /* Number of bytes we can append to input buffer */
972 avail_now = (d->input_buffer + d->input_buffer_size)
973 - (lzs->next_in + lzs->avail_in);
974
975 /* Number of bytes we can append if we move existing
976 contents to beginning of buffer (overwriting
977 consumed input) */
978 avail_total = d->input_buffer_size - lzs->avail_in;
979
980 if (avail_total < len) {
981 size_t offset = lzs->next_in - d->input_buffer;
982 uint8_t *tmp;
983 size_t new_size = d->input_buffer_size + len - avail_now;
984
985 /* Assign to temporary variable first, so we don't
986 lose address of allocated buffer if realloc fails */
987 tmp = PyMem_Realloc(d->input_buffer, new_size);
988 if (tmp == NULL) {
989 PyErr_SetNone(PyExc_MemoryError);
990 return NULL;
991 }
992 d->input_buffer = tmp;
993 d->input_buffer_size = new_size;
Serhiy Storchaka009b8112015-03-18 21:53:15 +0200994
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100995 lzs->next_in = d->input_buffer + offset;
996 }
997 else if (avail_now < len) {
998 memmove(d->input_buffer, lzs->next_in,
999 lzs->avail_in);
1000 lzs->next_in = d->input_buffer;
1001 }
1002 memcpy((void*)(lzs->next_in + lzs->avail_in), data, len);
1003 lzs->avail_in += len;
1004 input_buffer_in_use = 1;
1005 }
1006 else {
1007 lzs->next_in = data;
1008 lzs->avail_in = len;
1009 input_buffer_in_use = 0;
1010 }
1011
1012 result = decompress_buf(d, max_length);
Serhiy Storchakac0b70372016-09-27 20:14:26 +03001013 if (result == NULL) {
1014 lzs->next_in = NULL;
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001015 return NULL;
Serhiy Storchakac0b70372016-09-27 20:14:26 +03001016 }
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001017
1018 if (d->eof) {
1019 d->needs_input = 0;
1020 if (lzs->avail_in > 0) {
Serhiy Storchaka48842712016-04-06 09:45:48 +03001021 Py_XSETREF(d->unused_data,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +02001022 PyBytes_FromStringAndSize((char *)lzs->next_in, lzs->avail_in));
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001023 if (d->unused_data == NULL)
1024 goto error;
1025 }
1026 }
1027 else if (lzs->avail_in == 0) {
1028 lzs->next_in = NULL;
1029 d->needs_input = 1;
1030 }
1031 else {
1032 d->needs_input = 0;
1033
1034 /* If we did not use the input buffer, we now have
1035 to copy the tail from the caller's buffer into the
1036 input buffer */
1037 if (!input_buffer_in_use) {
1038
1039 /* Discard buffer if it's too small
1040 (resizing it may needlessly copy the current contents) */
1041 if (d->input_buffer != NULL &&
1042 d->input_buffer_size < lzs->avail_in) {
1043 PyMem_Free(d->input_buffer);
1044 d->input_buffer = NULL;
1045 }
1046
1047 /* Allocate if necessary */
1048 if (d->input_buffer == NULL) {
1049 d->input_buffer = PyMem_Malloc(lzs->avail_in);
1050 if (d->input_buffer == NULL) {
1051 PyErr_SetNone(PyExc_MemoryError);
1052 goto error;
1053 }
1054 d->input_buffer_size = lzs->avail_in;
1055 }
1056
1057 /* Copy tail */
1058 memcpy(d->input_buffer, lzs->next_in, lzs->avail_in);
1059 lzs->next_in = d->input_buffer;
1060 }
1061 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +02001062
Larry Hastingsf256c222014-01-25 21:30:37 -08001063 return result;
1064
1065error:
1066 Py_XDECREF(result);
1067 return NULL;
1068}
1069
1070/*[clinic input]
1071_lzma.LZMADecompressor.decompress
1072
1073 self: self(type="Decompressor *")
1074 data: Py_buffer
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001075 max_length: Py_ssize_t=-1
Larry Hastingsf256c222014-01-25 21:30:37 -08001076
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001077Decompress *data*, returning uncompressed data as bytes.
Larry Hastingsf256c222014-01-25 21:30:37 -08001078
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001079If *max_length* is nonnegative, returns at most *max_length* bytes of
1080decompressed data. If this limit is reached and further output can be
1081produced, *self.needs_input* will be set to ``False``. In this case, the next
1082call to *decompress()* may provide *data* as b'' to obtain more of the output.
Larry Hastingsf256c222014-01-25 21:30:37 -08001083
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001084If all of the input data was decompressed and returned (either because this
1085was less than *max_length* bytes, or because *max_length* was negative),
1086*self.needs_input* will be set to True.
1087
1088Attempting to decompress data after the end of stream is reached raises an
1089EOFError. Any data found after the end of the stream is ignored and saved in
1090the unused_data attribute.
Larry Hastingsf256c222014-01-25 21:30:37 -08001091[clinic start generated code]*/
1092
1093static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04001094_lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data,
1095 Py_ssize_t max_length)
1096/*[clinic end generated code: output=ef4e20ec7122241d input=f2bb902cc1caf203]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001097{
1098 PyObject *result = NULL;
1099
1100 ACQUIRE_LOCK(self);
1101 if (self->eof)
1102 PyErr_SetString(PyExc_EOFError, "Already at end of stream");
1103 else
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001104 result = decompress(self, data->buf, data->len, max_length);
Larry Hastingsf256c222014-01-25 21:30:37 -08001105 RELEASE_LOCK(self);
1106 return result;
1107}
1108
1109static PyObject *
1110Decompressor_getstate(Decompressor *self, PyObject *noargs)
1111{
1112 PyErr_Format(PyExc_TypeError, "cannot serialize '%s' object",
1113 Py_TYPE(self)->tp_name);
1114 return NULL;
1115}
1116
1117static int
1118Decompressor_init_raw(lzma_stream *lzs, PyObject *filterspecs)
1119{
1120 lzma_filter filters[LZMA_FILTERS_MAX + 1];
1121 lzma_ret lzret;
1122
1123 if (parse_filter_chain_spec(filters, filterspecs) == -1)
1124 return -1;
1125 lzret = lzma_raw_decoder(lzs, filters);
1126 free_filter_chain(filters);
1127 if (catch_lzma_error(lzret))
1128 return -1;
1129 else
1130 return 0;
1131}
1132
1133/*[clinic input]
1134_lzma.LZMADecompressor.__init__
1135
1136 self: self(type="Decompressor *")
1137 format: int(c_default="FORMAT_AUTO") = FORMAT_AUTO
1138 Specifies the container format of the input stream. If this is
1139 FORMAT_AUTO (the default), the decompressor will automatically detect
1140 whether the input is FORMAT_XZ or FORMAT_ALONE. Streams created with
1141 FORMAT_RAW cannot be autodetected.
1142
1143 memlimit: object = None
1144 Limit the amount of memory used by the decompressor. This will cause
1145 decompression to fail if the input cannot be decompressed within the
1146 given limit.
1147
1148 filters: object = None
1149 A custom filter chain. This argument is required for FORMAT_RAW, and
1150 not accepted with any other format. When provided, this should be a
1151 sequence of dicts, each indicating the ID and options for a single
1152 filter.
1153
1154Create a decompressor object for decompressing data incrementally.
1155
1156For one-shot decompression, use the decompress() function instead.
1157[clinic start generated code]*/
1158
1159static int
Larry Hastings89964c42015-04-14 18:07:59 -04001160_lzma_LZMADecompressor___init___impl(Decompressor *self, int format,
1161 PyObject *memlimit, PyObject *filters)
1162/*[clinic end generated code: output=3e1821f8aa36564c input=458ca6132ef29801]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001163{
1164 const uint32_t decoder_flags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK;
1165 uint64_t memlimit_ = UINT64_MAX;
1166 lzma_ret lzret;
1167
1168 if (memlimit != Py_None) {
1169 if (format == FORMAT_RAW) {
1170 PyErr_SetString(PyExc_ValueError,
1171 "Cannot specify memory limit with FORMAT_RAW");
1172 return -1;
1173 }
1174 memlimit_ = PyLong_AsUnsignedLongLong(memlimit);
1175 if (PyErr_Occurred())
1176 return -1;
1177 }
1178
1179 if (format == FORMAT_RAW && filters == Py_None) {
1180 PyErr_SetString(PyExc_ValueError,
1181 "Must specify filters for FORMAT_RAW");
1182 return -1;
1183 } else if (format != FORMAT_RAW && filters != Py_None) {
1184 PyErr_SetString(PyExc_ValueError,
1185 "Cannot specify filters except with FORMAT_RAW");
1186 return -1;
1187 }
1188
1189 self->alloc.opaque = NULL;
1190 self->alloc.alloc = PyLzma_Malloc;
1191 self->alloc.free = PyLzma_Free;
1192 self->lzs.allocator = &self->alloc;
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001193 self->lzs.next_in = NULL;
Larry Hastingsf256c222014-01-25 21:30:37 -08001194
1195#ifdef WITH_THREAD
1196 self->lock = PyThread_allocate_lock();
1197 if (self->lock == NULL) {
1198 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
1199 return -1;
1200 }
1201#endif
1202
1203 self->check = LZMA_CHECK_UNKNOWN;
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001204 self->needs_input = 1;
1205 self->input_buffer = NULL;
1206 self->input_buffer_size = 0;
Larry Hastingsf256c222014-01-25 21:30:37 -08001207 self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
1208 if (self->unused_data == NULL)
1209 goto error;
1210
1211 switch (format) {
1212 case FORMAT_AUTO:
1213 lzret = lzma_auto_decoder(&self->lzs, memlimit_, decoder_flags);
1214 if (catch_lzma_error(lzret))
1215 break;
1216 return 0;
1217
1218 case FORMAT_XZ:
1219 lzret = lzma_stream_decoder(&self->lzs, memlimit_, decoder_flags);
1220 if (catch_lzma_error(lzret))
1221 break;
1222 return 0;
1223
1224 case FORMAT_ALONE:
1225 self->check = LZMA_CHECK_NONE;
1226 lzret = lzma_alone_decoder(&self->lzs, memlimit_);
1227 if (catch_lzma_error(lzret))
1228 break;
1229 return 0;
1230
1231 case FORMAT_RAW:
1232 self->check = LZMA_CHECK_NONE;
1233 if (Decompressor_init_raw(&self->lzs, filters) == -1)
1234 break;
1235 return 0;
1236
1237 default:
1238 PyErr_Format(PyExc_ValueError,
1239 "Invalid container format: %d", format);
1240 break;
1241 }
1242
1243error:
1244 Py_CLEAR(self->unused_data);
1245#ifdef WITH_THREAD
1246 PyThread_free_lock(self->lock);
1247 self->lock = NULL;
1248#endif
1249 return -1;
1250}
1251
1252static void
1253Decompressor_dealloc(Decompressor *self)
1254{
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001255 if(self->input_buffer != NULL)
1256 PyMem_Free(self->input_buffer);
Serhiy Storchaka009b8112015-03-18 21:53:15 +02001257
Larry Hastingsf256c222014-01-25 21:30:37 -08001258 lzma_end(&self->lzs);
1259 Py_CLEAR(self->unused_data);
1260#ifdef WITH_THREAD
1261 if (self->lock != NULL)
1262 PyThread_free_lock(self->lock);
1263#endif
1264 Py_TYPE(self)->tp_free((PyObject *)self);
1265}
1266
1267static PyMethodDef Decompressor_methods[] = {
1268 _LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF
1269 {"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS},
1270 {NULL}
1271};
1272
1273PyDoc_STRVAR(Decompressor_check_doc,
1274"ID of the integrity check used by the input stream.");
1275
1276PyDoc_STRVAR(Decompressor_eof_doc,
1277"True if the end-of-stream marker has been reached.");
1278
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001279PyDoc_STRVAR(Decompressor_needs_input_doc,
1280"True if more input is needed before more decompressed data can be produced.");
1281
Larry Hastingsf256c222014-01-25 21:30:37 -08001282PyDoc_STRVAR(Decompressor_unused_data_doc,
1283"Data found after the end of the compressed stream.");
1284
1285static PyMemberDef Decompressor_members[] = {
1286 {"check", T_INT, offsetof(Decompressor, check), READONLY,
1287 Decompressor_check_doc},
1288 {"eof", T_BOOL, offsetof(Decompressor, eof), READONLY,
1289 Decompressor_eof_doc},
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001290 {"needs_input", T_BOOL, offsetof(Decompressor, needs_input), READONLY,
1291 Decompressor_needs_input_doc},
Larry Hastingsf256c222014-01-25 21:30:37 -08001292 {"unused_data", T_OBJECT_EX, offsetof(Decompressor, unused_data), READONLY,
1293 Decompressor_unused_data_doc},
1294 {NULL}
1295};
1296
1297static PyTypeObject Decompressor_type = {
1298 PyVarObject_HEAD_INIT(NULL, 0)
1299 "_lzma.LZMADecompressor", /* tp_name */
1300 sizeof(Decompressor), /* tp_basicsize */
1301 0, /* tp_itemsize */
1302 (destructor)Decompressor_dealloc, /* tp_dealloc */
1303 0, /* tp_print */
1304 0, /* tp_getattr */
1305 0, /* tp_setattr */
1306 0, /* tp_reserved */
1307 0, /* tp_repr */
1308 0, /* tp_as_number */
1309 0, /* tp_as_sequence */
1310 0, /* tp_as_mapping */
1311 0, /* tp_hash */
1312 0, /* tp_call */
1313 0, /* tp_str */
1314 0, /* tp_getattro */
1315 0, /* tp_setattro */
1316 0, /* tp_as_buffer */
1317 Py_TPFLAGS_DEFAULT, /* tp_flags */
1318 _lzma_LZMADecompressor___init____doc__, /* tp_doc */
1319 0, /* tp_traverse */
1320 0, /* tp_clear */
1321 0, /* tp_richcompare */
1322 0, /* tp_weaklistoffset */
1323 0, /* tp_iter */
1324 0, /* tp_iternext */
1325 Decompressor_methods, /* tp_methods */
1326 Decompressor_members, /* tp_members */
1327 0, /* tp_getset */
1328 0, /* tp_base */
1329 0, /* tp_dict */
1330 0, /* tp_descr_get */
1331 0, /* tp_descr_set */
1332 0, /* tp_dictoffset */
1333 _lzma_LZMADecompressor___init__, /* tp_init */
1334 0, /* tp_alloc */
1335 PyType_GenericNew, /* tp_new */
1336};
1337
1338
1339/* Module-level functions. */
1340
1341/*[clinic input]
1342_lzma.is_check_supported
1343 check_id: int
1344 /
1345
1346Test whether the given integrity check is supported.
1347
1348Always returns True for CHECK_NONE and CHECK_CRC32.
1349[clinic start generated code]*/
1350
1351static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001352_lzma_is_check_supported_impl(PyObject *module, int check_id)
1353/*[clinic end generated code: output=e4f14ba3ce2ad0a5 input=5518297b97b2318f]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001354{
1355 return PyBool_FromLong(lzma_check_is_supported(check_id));
1356}
1357
1358
1359/*[clinic input]
1360_lzma._encode_filter_properties
1361 filter: lzma_filter(c_default="{LZMA_VLI_UNKNOWN, NULL}")
1362 /
1363
1364Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict).
1365
1366The result does not include the filter ID itself, only the options.
1367[clinic start generated code]*/
1368
1369static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001370_lzma__encode_filter_properties_impl(PyObject *module, lzma_filter filter)
1371/*[clinic end generated code: output=5c93c8e14e7be5a8 input=d4c64f1b557c77d4]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001372{
1373 lzma_ret lzret;
1374 uint32_t encoded_size;
1375 PyObject *result = NULL;
1376
1377 lzret = lzma_properties_size(&encoded_size, &filter);
1378 if (catch_lzma_error(lzret))
1379 goto error;
1380
1381 result = PyBytes_FromStringAndSize(NULL, encoded_size);
1382 if (result == NULL)
1383 goto error;
1384
1385 lzret = lzma_properties_encode(
1386 &filter, (uint8_t *)PyBytes_AS_STRING(result));
1387 if (catch_lzma_error(lzret))
1388 goto error;
1389
1390 return result;
1391
1392error:
1393 Py_XDECREF(result);
1394 return NULL;
1395}
1396
1397
1398/*[clinic input]
1399_lzma._decode_filter_properties
1400 filter_id: lzma_vli
1401 encoded_props: Py_buffer
1402 /
1403
1404Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict).
1405
1406The result does not include the filter ID itself, only the options.
1407[clinic start generated code]*/
1408
1409static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001410_lzma__decode_filter_properties_impl(PyObject *module, lzma_vli filter_id,
Larry Hastings89964c42015-04-14 18:07:59 -04001411 Py_buffer *encoded_props)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001412/*[clinic end generated code: output=714fd2ef565d5c60 input=246410800782160c]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001413{
1414 lzma_filter filter;
1415 lzma_ret lzret;
1416 PyObject *result = NULL;
1417 filter.id = filter_id;
1418
1419 lzret = lzma_properties_decode(
1420 &filter, NULL, encoded_props->buf, encoded_props->len);
1421 if (catch_lzma_error(lzret))
1422 return NULL;
1423
1424 result = build_filter_spec(&filter);
1425
1426 /* We use vanilla free() here instead of PyMem_Free() - filter.options was
1427 allocated by lzma_properties_decode() using the default allocator. */
1428 free(filter.options);
1429 return result;
1430}
1431
1432
1433/* Module initialization. */
1434
1435static PyMethodDef module_methods[] = {
1436 _LZMA_IS_CHECK_SUPPORTED_METHODDEF
1437 _LZMA__ENCODE_FILTER_PROPERTIES_METHODDEF
1438 _LZMA__DECODE_FILTER_PROPERTIES_METHODDEF
1439 {NULL}
1440};
1441
1442static PyModuleDef _lzmamodule = {
1443 PyModuleDef_HEAD_INIT,
1444 "_lzma",
1445 NULL,
1446 -1,
1447 module_methods,
1448 NULL,
1449 NULL,
1450 NULL,
1451 NULL,
1452};
1453
1454/* Some of our constants are more than 32 bits wide, so PyModule_AddIntConstant
1455 would not work correctly on platforms with 32-bit longs. */
1456static int
1457module_add_int_constant(PyObject *m, const char *name, PY_LONG_LONG value)
1458{
1459 PyObject *o = PyLong_FromLongLong(value);
1460 if (o == NULL)
1461 return -1;
1462 if (PyModule_AddObject(m, name, o) == 0)
1463 return 0;
1464 Py_DECREF(o);
1465 return -1;
1466}
1467
1468#define ADD_INT_PREFIX_MACRO(m, macro) \
1469 module_add_int_constant(m, #macro, LZMA_ ## macro)
1470
1471PyMODINIT_FUNC
1472PyInit__lzma(void)
1473{
1474 PyObject *m;
1475
1476 empty_tuple = PyTuple_New(0);
1477 if (empty_tuple == NULL)
1478 return NULL;
1479
1480 m = PyModule_Create(&_lzmamodule);
1481 if (m == NULL)
1482 return NULL;
1483
1484 if (PyModule_AddIntMacro(m, FORMAT_AUTO) == -1 ||
1485 PyModule_AddIntMacro(m, FORMAT_XZ) == -1 ||
1486 PyModule_AddIntMacro(m, FORMAT_ALONE) == -1 ||
1487 PyModule_AddIntMacro(m, FORMAT_RAW) == -1 ||
1488 ADD_INT_PREFIX_MACRO(m, CHECK_NONE) == -1 ||
1489 ADD_INT_PREFIX_MACRO(m, CHECK_CRC32) == -1 ||
1490 ADD_INT_PREFIX_MACRO(m, CHECK_CRC64) == -1 ||
1491 ADD_INT_PREFIX_MACRO(m, CHECK_SHA256) == -1 ||
1492 ADD_INT_PREFIX_MACRO(m, CHECK_ID_MAX) == -1 ||
1493 ADD_INT_PREFIX_MACRO(m, CHECK_UNKNOWN) == -1 ||
1494 ADD_INT_PREFIX_MACRO(m, FILTER_LZMA1) == -1 ||
1495 ADD_INT_PREFIX_MACRO(m, FILTER_LZMA2) == -1 ||
1496 ADD_INT_PREFIX_MACRO(m, FILTER_DELTA) == -1 ||
1497 ADD_INT_PREFIX_MACRO(m, FILTER_X86) == -1 ||
1498 ADD_INT_PREFIX_MACRO(m, FILTER_IA64) == -1 ||
1499 ADD_INT_PREFIX_MACRO(m, FILTER_ARM) == -1 ||
1500 ADD_INT_PREFIX_MACRO(m, FILTER_ARMTHUMB) == -1 ||
1501 ADD_INT_PREFIX_MACRO(m, FILTER_SPARC) == -1 ||
1502 ADD_INT_PREFIX_MACRO(m, FILTER_POWERPC) == -1 ||
1503 ADD_INT_PREFIX_MACRO(m, MF_HC3) == -1 ||
1504 ADD_INT_PREFIX_MACRO(m, MF_HC4) == -1 ||
1505 ADD_INT_PREFIX_MACRO(m, MF_BT2) == -1 ||
1506 ADD_INT_PREFIX_MACRO(m, MF_BT3) == -1 ||
1507 ADD_INT_PREFIX_MACRO(m, MF_BT4) == -1 ||
1508 ADD_INT_PREFIX_MACRO(m, MODE_FAST) == -1 ||
1509 ADD_INT_PREFIX_MACRO(m, MODE_NORMAL) == -1 ||
1510 ADD_INT_PREFIX_MACRO(m, PRESET_DEFAULT) == -1 ||
1511 ADD_INT_PREFIX_MACRO(m, PRESET_EXTREME) == -1)
1512 return NULL;
1513
1514 Error = PyErr_NewExceptionWithDoc(
1515 "_lzma.LZMAError", "Call to liblzma failed.", NULL, NULL);
1516 if (Error == NULL)
1517 return NULL;
1518 Py_INCREF(Error);
1519 if (PyModule_AddObject(m, "LZMAError", Error) == -1)
1520 return NULL;
1521
1522 if (PyType_Ready(&Compressor_type) == -1)
1523 return NULL;
1524 Py_INCREF(&Compressor_type);
1525 if (PyModule_AddObject(m, "LZMACompressor",
1526 (PyObject *)&Compressor_type) == -1)
1527 return NULL;
1528
1529 if (PyType_Ready(&Decompressor_type) == -1)
1530 return NULL;
1531 Py_INCREF(&Decompressor_type);
1532 if (PyModule_AddObject(m, "LZMADecompressor",
1533 (PyObject *)&Decompressor_type) == -1)
1534 return NULL;
1535
1536 return m;
1537}