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