»Core Development>Code coverage>Modules/_lzmamodule.c

Python code coverage for Modules/_lzmamodule.c

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