blob: f110f5b5ea19efe60910d8fb655ce3015b3314d4 [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
708 is CHECK_CRC64. FORMAT_ALONE and FORMAT_RAW do not suport integrity
709 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) {
1014 Py_CLEAR(d->unused_data);
1015 d->unused_data = PyBytes_FromStringAndSize(
1016 (char *)lzs->next_in, lzs->avail_in);
1017 if (d->unused_data == NULL)
1018 goto error;
1019 }
1020 }
1021 else if (lzs->avail_in == 0) {
1022 lzs->next_in = NULL;
1023 d->needs_input = 1;
1024 }
1025 else {
1026 d->needs_input = 0;
1027
1028 /* If we did not use the input buffer, we now have
1029 to copy the tail from the caller's buffer into the
1030 input buffer */
1031 if (!input_buffer_in_use) {
1032
1033 /* Discard buffer if it's too small
1034 (resizing it may needlessly copy the current contents) */
1035 if (d->input_buffer != NULL &&
1036 d->input_buffer_size < lzs->avail_in) {
1037 PyMem_Free(d->input_buffer);
1038 d->input_buffer = NULL;
1039 }
1040
1041 /* Allocate if necessary */
1042 if (d->input_buffer == NULL) {
1043 d->input_buffer = PyMem_Malloc(lzs->avail_in);
1044 if (d->input_buffer == NULL) {
1045 PyErr_SetNone(PyExc_MemoryError);
1046 goto error;
1047 }
1048 d->input_buffer_size = lzs->avail_in;
1049 }
1050
1051 /* Copy tail */
1052 memcpy(d->input_buffer, lzs->next_in, lzs->avail_in);
1053 lzs->next_in = d->input_buffer;
1054 }
1055 }
Serhiy Storchaka009b8112015-03-18 21:53:15 +02001056
Larry Hastingsf256c222014-01-25 21:30:37 -08001057 return result;
1058
1059error:
1060 Py_XDECREF(result);
1061 return NULL;
1062}
1063
1064/*[clinic input]
1065_lzma.LZMADecompressor.decompress
1066
1067 self: self(type="Decompressor *")
1068 data: Py_buffer
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001069 max_length: Py_ssize_t=-1
Larry Hastingsf256c222014-01-25 21:30:37 -08001070
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001071Decompress *data*, returning uncompressed data as bytes.
Larry Hastingsf256c222014-01-25 21:30:37 -08001072
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001073If *max_length* is nonnegative, returns at most *max_length* bytes of
1074decompressed data. If this limit is reached and further output can be
1075produced, *self.needs_input* will be set to ``False``. In this case, the next
1076call to *decompress()* may provide *data* as b'' to obtain more of the output.
Larry Hastingsf256c222014-01-25 21:30:37 -08001077
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001078If all of the input data was decompressed and returned (either because this
1079was less than *max_length* bytes, or because *max_length* was negative),
1080*self.needs_input* will be set to True.
1081
1082Attempting to decompress data after the end of stream is reached raises an
1083EOFError. Any data found after the end of the stream is ignored and saved in
1084the unused_data attribute.
Larry Hastingsf256c222014-01-25 21:30:37 -08001085[clinic start generated code]*/
1086
1087static PyObject *
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001088_lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data, Py_ssize_t max_length)
Serhiy Storchaka79d8f3f2015-02-20 12:46:11 +02001089/*[clinic end generated code: output=1532a5bb23629001 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
1153_lzma_LZMADecompressor___init___impl(Decompressor *self, int format, PyObject *memlimit, PyObject *filters)
Larry Hastings581ee362014-01-28 05:00:08 -08001154/*[clinic end generated code: output=9b119f6f2cc2d7a8 input=458ca6132ef29801]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001155{
1156 const uint32_t decoder_flags = LZMA_TELL_ANY_CHECK | LZMA_TELL_NO_CHECK;
1157 uint64_t memlimit_ = UINT64_MAX;
1158 lzma_ret lzret;
1159
1160 if (memlimit != Py_None) {
1161 if (format == FORMAT_RAW) {
1162 PyErr_SetString(PyExc_ValueError,
1163 "Cannot specify memory limit with FORMAT_RAW");
1164 return -1;
1165 }
1166 memlimit_ = PyLong_AsUnsignedLongLong(memlimit);
1167 if (PyErr_Occurred())
1168 return -1;
1169 }
1170
1171 if (format == FORMAT_RAW && filters == Py_None) {
1172 PyErr_SetString(PyExc_ValueError,
1173 "Must specify filters for FORMAT_RAW");
1174 return -1;
1175 } else if (format != FORMAT_RAW && filters != Py_None) {
1176 PyErr_SetString(PyExc_ValueError,
1177 "Cannot specify filters except with FORMAT_RAW");
1178 return -1;
1179 }
1180
1181 self->alloc.opaque = NULL;
1182 self->alloc.alloc = PyLzma_Malloc;
1183 self->alloc.free = PyLzma_Free;
1184 self->lzs.allocator = &self->alloc;
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001185 self->lzs.next_in = NULL;
Larry Hastingsf256c222014-01-25 21:30:37 -08001186
1187#ifdef WITH_THREAD
1188 self->lock = PyThread_allocate_lock();
1189 if (self->lock == NULL) {
1190 PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
1191 return -1;
1192 }
1193#endif
1194
1195 self->check = LZMA_CHECK_UNKNOWN;
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001196 self->needs_input = 1;
1197 self->input_buffer = NULL;
1198 self->input_buffer_size = 0;
Larry Hastingsf256c222014-01-25 21:30:37 -08001199 self->unused_data = PyBytes_FromStringAndSize(NULL, 0);
1200 if (self->unused_data == NULL)
1201 goto error;
1202
1203 switch (format) {
1204 case FORMAT_AUTO:
1205 lzret = lzma_auto_decoder(&self->lzs, memlimit_, decoder_flags);
1206 if (catch_lzma_error(lzret))
1207 break;
1208 return 0;
1209
1210 case FORMAT_XZ:
1211 lzret = lzma_stream_decoder(&self->lzs, memlimit_, decoder_flags);
1212 if (catch_lzma_error(lzret))
1213 break;
1214 return 0;
1215
1216 case FORMAT_ALONE:
1217 self->check = LZMA_CHECK_NONE;
1218 lzret = lzma_alone_decoder(&self->lzs, memlimit_);
1219 if (catch_lzma_error(lzret))
1220 break;
1221 return 0;
1222
1223 case FORMAT_RAW:
1224 self->check = LZMA_CHECK_NONE;
1225 if (Decompressor_init_raw(&self->lzs, filters) == -1)
1226 break;
1227 return 0;
1228
1229 default:
1230 PyErr_Format(PyExc_ValueError,
1231 "Invalid container format: %d", format);
1232 break;
1233 }
1234
1235error:
1236 Py_CLEAR(self->unused_data);
1237#ifdef WITH_THREAD
1238 PyThread_free_lock(self->lock);
1239 self->lock = NULL;
1240#endif
1241 return -1;
1242}
1243
1244static void
1245Decompressor_dealloc(Decompressor *self)
1246{
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001247 if(self->input_buffer != NULL)
1248 PyMem_Free(self->input_buffer);
Serhiy Storchaka009b8112015-03-18 21:53:15 +02001249
Larry Hastingsf256c222014-01-25 21:30:37 -08001250 lzma_end(&self->lzs);
1251 Py_CLEAR(self->unused_data);
1252#ifdef WITH_THREAD
1253 if (self->lock != NULL)
1254 PyThread_free_lock(self->lock);
1255#endif
1256 Py_TYPE(self)->tp_free((PyObject *)self);
1257}
1258
1259static PyMethodDef Decompressor_methods[] = {
1260 _LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF
1261 {"__getstate__", (PyCFunction)Decompressor_getstate, METH_NOARGS},
1262 {NULL}
1263};
1264
1265PyDoc_STRVAR(Decompressor_check_doc,
1266"ID of the integrity check used by the input stream.");
1267
1268PyDoc_STRVAR(Decompressor_eof_doc,
1269"True if the end-of-stream marker has been reached.");
1270
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001271PyDoc_STRVAR(Decompressor_needs_input_doc,
1272"True if more input is needed before more decompressed data can be produced.");
1273
Larry Hastingsf256c222014-01-25 21:30:37 -08001274PyDoc_STRVAR(Decompressor_unused_data_doc,
1275"Data found after the end of the compressed stream.");
1276
1277static PyMemberDef Decompressor_members[] = {
1278 {"check", T_INT, offsetof(Decompressor, check), READONLY,
1279 Decompressor_check_doc},
1280 {"eof", T_BOOL, offsetof(Decompressor, eof), READONLY,
1281 Decompressor_eof_doc},
Antoine Pitrou26795ba2015-01-17 16:22:18 +01001282 {"needs_input", T_BOOL, offsetof(Decompressor, needs_input), READONLY,
1283 Decompressor_needs_input_doc},
Larry Hastingsf256c222014-01-25 21:30:37 -08001284 {"unused_data", T_OBJECT_EX, offsetof(Decompressor, unused_data), READONLY,
1285 Decompressor_unused_data_doc},
1286 {NULL}
1287};
1288
1289static PyTypeObject Decompressor_type = {
1290 PyVarObject_HEAD_INIT(NULL, 0)
1291 "_lzma.LZMADecompressor", /* tp_name */
1292 sizeof(Decompressor), /* tp_basicsize */
1293 0, /* tp_itemsize */
1294 (destructor)Decompressor_dealloc, /* tp_dealloc */
1295 0, /* tp_print */
1296 0, /* tp_getattr */
1297 0, /* tp_setattr */
1298 0, /* tp_reserved */
1299 0, /* tp_repr */
1300 0, /* tp_as_number */
1301 0, /* tp_as_sequence */
1302 0, /* tp_as_mapping */
1303 0, /* tp_hash */
1304 0, /* tp_call */
1305 0, /* tp_str */
1306 0, /* tp_getattro */
1307 0, /* tp_setattro */
1308 0, /* tp_as_buffer */
1309 Py_TPFLAGS_DEFAULT, /* tp_flags */
1310 _lzma_LZMADecompressor___init____doc__, /* tp_doc */
1311 0, /* tp_traverse */
1312 0, /* tp_clear */
1313 0, /* tp_richcompare */
1314 0, /* tp_weaklistoffset */
1315 0, /* tp_iter */
1316 0, /* tp_iternext */
1317 Decompressor_methods, /* tp_methods */
1318 Decompressor_members, /* tp_members */
1319 0, /* tp_getset */
1320 0, /* tp_base */
1321 0, /* tp_dict */
1322 0, /* tp_descr_get */
1323 0, /* tp_descr_set */
1324 0, /* tp_dictoffset */
1325 _lzma_LZMADecompressor___init__, /* tp_init */
1326 0, /* tp_alloc */
1327 PyType_GenericNew, /* tp_new */
1328};
1329
1330
1331/* Module-level functions. */
1332
1333/*[clinic input]
1334_lzma.is_check_supported
1335 check_id: int
1336 /
1337
1338Test whether the given integrity check is supported.
1339
1340Always returns True for CHECK_NONE and CHECK_CRC32.
1341[clinic start generated code]*/
1342
1343static PyObject *
1344_lzma_is_check_supported_impl(PyModuleDef *module, int check_id)
Larry Hastings581ee362014-01-28 05:00:08 -08001345/*[clinic end generated code: output=bb828e90e00ad96e input=5518297b97b2318f]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001346{
1347 return PyBool_FromLong(lzma_check_is_supported(check_id));
1348}
1349
1350
1351/*[clinic input]
1352_lzma._encode_filter_properties
1353 filter: lzma_filter(c_default="{LZMA_VLI_UNKNOWN, NULL}")
1354 /
1355
1356Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict).
1357
1358The result does not include the filter ID itself, only the options.
1359[clinic start generated code]*/
1360
1361static PyObject *
1362_lzma__encode_filter_properties_impl(PyModuleDef *module, lzma_filter filter)
Larry Hastings581ee362014-01-28 05:00:08 -08001363/*[clinic end generated code: output=b5fe690acd6b61d1 input=d4c64f1b557c77d4]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001364{
1365 lzma_ret lzret;
1366 uint32_t encoded_size;
1367 PyObject *result = NULL;
1368
1369 lzret = lzma_properties_size(&encoded_size, &filter);
1370 if (catch_lzma_error(lzret))
1371 goto error;
1372
1373 result = PyBytes_FromStringAndSize(NULL, encoded_size);
1374 if (result == NULL)
1375 goto error;
1376
1377 lzret = lzma_properties_encode(
1378 &filter, (uint8_t *)PyBytes_AS_STRING(result));
1379 if (catch_lzma_error(lzret))
1380 goto error;
1381
1382 return result;
1383
1384error:
1385 Py_XDECREF(result);
1386 return NULL;
1387}
1388
1389
1390/*[clinic input]
1391_lzma._decode_filter_properties
1392 filter_id: lzma_vli
1393 encoded_props: Py_buffer
1394 /
1395
1396Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict).
1397
1398The result does not include the filter ID itself, only the options.
1399[clinic start generated code]*/
1400
1401static PyObject *
1402_lzma__decode_filter_properties_impl(PyModuleDef *module, lzma_vli filter_id, Py_buffer *encoded_props)
Larry Hastings581ee362014-01-28 05:00:08 -08001403/*[clinic end generated code: output=235f7f5345d48744 input=246410800782160c]*/
Larry Hastingsf256c222014-01-25 21:30:37 -08001404{
1405 lzma_filter filter;
1406 lzma_ret lzret;
1407 PyObject *result = NULL;
1408 filter.id = filter_id;
1409
1410 lzret = lzma_properties_decode(
1411 &filter, NULL, encoded_props->buf, encoded_props->len);
1412 if (catch_lzma_error(lzret))
1413 return NULL;
1414
1415 result = build_filter_spec(&filter);
1416
1417 /* We use vanilla free() here instead of PyMem_Free() - filter.options was
1418 allocated by lzma_properties_decode() using the default allocator. */
1419 free(filter.options);
1420 return result;
1421}
1422
1423
1424/* Module initialization. */
1425
1426static PyMethodDef module_methods[] = {
1427 _LZMA_IS_CHECK_SUPPORTED_METHODDEF
1428 _LZMA__ENCODE_FILTER_PROPERTIES_METHODDEF
1429 _LZMA__DECODE_FILTER_PROPERTIES_METHODDEF
1430 {NULL}
1431};
1432
1433static PyModuleDef _lzmamodule = {
1434 PyModuleDef_HEAD_INIT,
1435 "_lzma",
1436 NULL,
1437 -1,
1438 module_methods,
1439 NULL,
1440 NULL,
1441 NULL,
1442 NULL,
1443};
1444
1445/* Some of our constants are more than 32 bits wide, so PyModule_AddIntConstant
1446 would not work correctly on platforms with 32-bit longs. */
1447static int
1448module_add_int_constant(PyObject *m, const char *name, PY_LONG_LONG value)
1449{
1450 PyObject *o = PyLong_FromLongLong(value);
1451 if (o == NULL)
1452 return -1;
1453 if (PyModule_AddObject(m, name, o) == 0)
1454 return 0;
1455 Py_DECREF(o);
1456 return -1;
1457}
1458
1459#define ADD_INT_PREFIX_MACRO(m, macro) \
1460 module_add_int_constant(m, #macro, LZMA_ ## macro)
1461
1462PyMODINIT_FUNC
1463PyInit__lzma(void)
1464{
1465 PyObject *m;
1466
1467 empty_tuple = PyTuple_New(0);
1468 if (empty_tuple == NULL)
1469 return NULL;
1470
1471 m = PyModule_Create(&_lzmamodule);
1472 if (m == NULL)
1473 return NULL;
1474
1475 if (PyModule_AddIntMacro(m, FORMAT_AUTO) == -1 ||
1476 PyModule_AddIntMacro(m, FORMAT_XZ) == -1 ||
1477 PyModule_AddIntMacro(m, FORMAT_ALONE) == -1 ||
1478 PyModule_AddIntMacro(m, FORMAT_RAW) == -1 ||
1479 ADD_INT_PREFIX_MACRO(m, CHECK_NONE) == -1 ||
1480 ADD_INT_PREFIX_MACRO(m, CHECK_CRC32) == -1 ||
1481 ADD_INT_PREFIX_MACRO(m, CHECK_CRC64) == -1 ||
1482 ADD_INT_PREFIX_MACRO(m, CHECK_SHA256) == -1 ||
1483 ADD_INT_PREFIX_MACRO(m, CHECK_ID_MAX) == -1 ||
1484 ADD_INT_PREFIX_MACRO(m, CHECK_UNKNOWN) == -1 ||
1485 ADD_INT_PREFIX_MACRO(m, FILTER_LZMA1) == -1 ||
1486 ADD_INT_PREFIX_MACRO(m, FILTER_LZMA2) == -1 ||
1487 ADD_INT_PREFIX_MACRO(m, FILTER_DELTA) == -1 ||
1488 ADD_INT_PREFIX_MACRO(m, FILTER_X86) == -1 ||
1489 ADD_INT_PREFIX_MACRO(m, FILTER_IA64) == -1 ||
1490 ADD_INT_PREFIX_MACRO(m, FILTER_ARM) == -1 ||
1491 ADD_INT_PREFIX_MACRO(m, FILTER_ARMTHUMB) == -1 ||
1492 ADD_INT_PREFIX_MACRO(m, FILTER_SPARC) == -1 ||
1493 ADD_INT_PREFIX_MACRO(m, FILTER_POWERPC) == -1 ||
1494 ADD_INT_PREFIX_MACRO(m, MF_HC3) == -1 ||
1495 ADD_INT_PREFIX_MACRO(m, MF_HC4) == -1 ||
1496 ADD_INT_PREFIX_MACRO(m, MF_BT2) == -1 ||
1497 ADD_INT_PREFIX_MACRO(m, MF_BT3) == -1 ||
1498 ADD_INT_PREFIX_MACRO(m, MF_BT4) == -1 ||
1499 ADD_INT_PREFIX_MACRO(m, MODE_FAST) == -1 ||
1500 ADD_INT_PREFIX_MACRO(m, MODE_NORMAL) == -1 ||
1501 ADD_INT_PREFIX_MACRO(m, PRESET_DEFAULT) == -1 ||
1502 ADD_INT_PREFIX_MACRO(m, PRESET_EXTREME) == -1)
1503 return NULL;
1504
1505 Error = PyErr_NewExceptionWithDoc(
1506 "_lzma.LZMAError", "Call to liblzma failed.", NULL, NULL);
1507 if (Error == NULL)
1508 return NULL;
1509 Py_INCREF(Error);
1510 if (PyModule_AddObject(m, "LZMAError", Error) == -1)
1511 return NULL;
1512
1513 if (PyType_Ready(&Compressor_type) == -1)
1514 return NULL;
1515 Py_INCREF(&Compressor_type);
1516 if (PyModule_AddObject(m, "LZMACompressor",
1517 (PyObject *)&Compressor_type) == -1)
1518 return NULL;
1519
1520 if (PyType_Ready(&Decompressor_type) == -1)
1521 return NULL;
1522 Py_INCREF(&Decompressor_type);
1523 if (PyModule_AddObject(m, "LZMADecompressor",
1524 (PyObject *)&Decompressor_type) == -1)
1525 return NULL;
1526
1527 return m;
1528}