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