blob: 82ef4d517d83c59b3f1409fb537147e358ddf8a2 [file] [log] [blame]
Serhiy Storchaka98c779e2014-01-25 14:02:29 +02001/*[clinic input]
Larry Hastingsf256c222014-01-25 21:30:37 -08002preserve
Serhiy Storchaka98c779e2014-01-25 14:02:29 +02003[clinic start generated code]*/
Nadeem Vawda3ff069e2011-11-30 00:25:06 +02004
Larry Hastingsf256c222014-01-25 21:30:37 -08005PyDoc_STRVAR(_lzma_LZMACompressor_compress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08006"compress($self, data, /)\n"
7"--\n"
8"\n"
Larry Hastingsf256c222014-01-25 21:30:37 -08009"Provide data to the compressor object.\n"
10"\n"
11"Returns a chunk of compressed data if possible, or b\'\' otherwise.\n"
12"\n"
13"When you have finished providing data to the compressor, call the\n"
14"flush() method to finish the compression process.");
Nadeem Vawda3ff069e2011-11-30 00:25:06 +020015
Larry Hastingsf256c222014-01-25 21:30:37 -080016#define _LZMA_LZMACOMPRESSOR_COMPRESS_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030017 {"compress", (PyCFunction)_lzma_LZMACompressor_compress, METH_O, _lzma_LZMACompressor_compress__doc__},
Nadeem Vawda3ff069e2011-11-30 00:25:06 +020018
19static PyObject *
Larry Hastingsf256c222014-01-25 21:30:37 -080020_lzma_LZMACompressor_compress_impl(Compressor *self, Py_buffer *data);
Nadeem Vawda3ff069e2011-11-30 00:25:06 +020021
Nadeem Vawda37970652013-10-28 21:35:23 +010022static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030023_lzma_LZMACompressor_compress(Compressor *self, PyObject *arg)
Nadeem Vawda37970652013-10-28 21:35:23 +010024{
Larry Hastingsf256c222014-01-25 21:30:37 -080025 PyObject *return_value = NULL;
26 Py_buffer data = {NULL, NULL};
27
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020028 if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
29 goto exit;
30 }
31 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +020032 _PyArg_BadArgument("compress", "argument", "contiguous buffer", arg);
Larry Hastingsf256c222014-01-25 21:30:37 -080033 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030034 }
Larry Hastingsf256c222014-01-25 21:30:37 -080035 return_value = _lzma_LZMACompressor_compress_impl(self, &data);
36
37exit:
38 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030039 if (data.obj) {
Larry Hastingsf256c222014-01-25 21:30:37 -080040 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030041 }
Larry Hastingsf256c222014-01-25 21:30:37 -080042
43 return return_value;
Nadeem Vawda37970652013-10-28 21:35:23 +010044}
45
Larry Hastingsf256c222014-01-25 21:30:37 -080046PyDoc_STRVAR(_lzma_LZMACompressor_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080047"flush($self, /)\n"
48"--\n"
49"\n"
Larry Hastingsf256c222014-01-25 21:30:37 -080050"Finish the compression process.\n"
51"\n"
52"Returns the compressed data left in internal buffers.\n"
53"\n"
54"The compressor object may not be used after this method is called.");
55
56#define _LZMA_LZMACOMPRESSOR_FLUSH_METHODDEF \
57 {"flush", (PyCFunction)_lzma_LZMACompressor_flush, METH_NOARGS, _lzma_LZMACompressor_flush__doc__},
58
59static PyObject *
60_lzma_LZMACompressor_flush_impl(Compressor *self);
61
62static PyObject *
63_lzma_LZMACompressor_flush(Compressor *self, PyObject *Py_UNUSED(ignored))
Nadeem Vawda3ff069e2011-11-30 00:25:06 +020064{
Larry Hastingsf256c222014-01-25 21:30:37 -080065 return _lzma_LZMACompressor_flush_impl(self);
Nadeem Vawda3ff069e2011-11-30 00:25:06 +020066}
67
Larry Hastingsf256c222014-01-25 21:30:37 -080068PyDoc_STRVAR(_lzma_LZMADecompressor_decompress__doc__,
Antoine Pitrou26795ba2015-01-17 16:22:18 +010069"decompress($self, /, data, max_length=-1)\n"
Larry Hastings2623c8c2014-02-08 22:15:29 -080070"--\n"
71"\n"
Serhiy Storchaka79d8f3f2015-02-20 12:46:11 +020072"Decompress *data*, returning uncompressed data as bytes.\n"
Larry Hastingsf256c222014-01-25 21:30:37 -080073"\n"
Antoine Pitrou26795ba2015-01-17 16:22:18 +010074"If *max_length* is nonnegative, returns at most *max_length* bytes of\n"
75"decompressed data. If this limit is reached and further output can be\n"
76"produced, *self.needs_input* will be set to ``False``. In this case, the next\n"
77"call to *decompress()* may provide *data* as b\'\' to obtain more of the output.\n"
Larry Hastingsf256c222014-01-25 21:30:37 -080078"\n"
Antoine Pitrou26795ba2015-01-17 16:22:18 +010079"If all of the input data was decompressed and returned (either because this\n"
80"was less than *max_length* bytes, or because *max_length* was negative),\n"
81"*self.needs_input* will be set to True.\n"
82"\n"
83"Attempting to decompress data after the end of stream is reached raises an\n"
84"EOFError. Any data found after the end of the stream is ignored and saved in\n"
85"the unused_data attribute.");
Larry Hastingsf256c222014-01-25 21:30:37 -080086
87#define _LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020088 {"decompress", (PyCFunction)(void(*)(void))_lzma_LZMADecompressor_decompress, METH_FASTCALL|METH_KEYWORDS, _lzma_LZMADecompressor_decompress__doc__},
Larry Hastingsf256c222014-01-25 21:30:37 -080089
90static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -040091_lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data,
92 Py_ssize_t max_length);
Larry Hastingsf256c222014-01-25 21:30:37 -080093
94static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020095_lzma_LZMADecompressor_decompress(Decompressor *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Nadeem Vawda3ff069e2011-11-30 00:25:06 +020096{
Larry Hastingsf256c222014-01-25 21:30:37 -080097 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030098 static const char * const _keywords[] = {"data", "max_length", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020099 static _PyArg_Parser _parser = {NULL, _keywords, "decompress", 0};
100 PyObject *argsbuf[2];
101 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Larry Hastingsf256c222014-01-25 21:30:37 -0800102 Py_buffer data = {NULL, NULL};
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100103 Py_ssize_t max_length = -1;
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200104
Serhiy Storchaka31913912019-03-14 10:32:22 +0200105 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
106 if (!args) {
Larry Hastingsf256c222014-01-25 21:30:37 -0800107 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300108 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200109 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
110 goto exit;
111 }
112 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200113 _PyArg_BadArgument("decompress", "argument 'data'", "contiguous buffer", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200114 goto exit;
115 }
116 if (!noptargs) {
117 goto skip_optional_pos;
118 }
119 if (PyFloat_Check(args[1])) {
120 PyErr_SetString(PyExc_TypeError,
121 "integer argument expected, got float" );
122 goto exit;
123 }
124 {
125 Py_ssize_t ival = -1;
126 PyObject *iobj = PyNumber_Index(args[1]);
127 if (iobj != NULL) {
128 ival = PyLong_AsSsize_t(iobj);
129 Py_DECREF(iobj);
130 }
131 if (ival == -1 && PyErr_Occurred()) {
132 goto exit;
133 }
134 max_length = ival;
135 }
136skip_optional_pos:
Antoine Pitrou26795ba2015-01-17 16:22:18 +0100137 return_value = _lzma_LZMADecompressor_decompress_impl(self, &data, max_length);
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200138
Larry Hastingsf256c222014-01-25 21:30:37 -0800139exit:
140 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300141 if (data.obj) {
Larry Hastingsf256c222014-01-25 21:30:37 -0800142 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300143 }
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200144
Larry Hastingsf256c222014-01-25 21:30:37 -0800145 return return_value;
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200146}
147
Larry Hastingsf256c222014-01-25 21:30:37 -0800148PyDoc_STRVAR(_lzma_LZMADecompressor___init____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800149"LZMADecompressor(format=FORMAT_AUTO, memlimit=None, filters=None)\n"
150"--\n"
151"\n"
Larry Hastingsf256c222014-01-25 21:30:37 -0800152"Create a decompressor object for decompressing data incrementally.\n"
153"\n"
154" format\n"
155" Specifies the container format of the input stream. If this is\n"
156" FORMAT_AUTO (the default), the decompressor will automatically detect\n"
157" whether the input is FORMAT_XZ or FORMAT_ALONE. Streams created with\n"
158" FORMAT_RAW cannot be autodetected.\n"
159" memlimit\n"
160" Limit the amount of memory used by the decompressor. This will cause\n"
161" decompression to fail if the input cannot be decompressed within the\n"
162" given limit.\n"
163" filters\n"
164" A custom filter chain. This argument is required for FORMAT_RAW, and\n"
165" not accepted with any other format. When provided, this should be a\n"
166" sequence of dicts, each indicating the ID and options for a single\n"
167" filter.\n"
168"\n"
169"For one-shot decompression, use the decompress() function instead.");
170
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200171static int
Larry Hastings89964c42015-04-14 18:07:59 -0400172_lzma_LZMADecompressor___init___impl(Decompressor *self, int format,
173 PyObject *memlimit, PyObject *filters);
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200174
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200175static int
Larry Hastingsf256c222014-01-25 21:30:37 -0800176_lzma_LZMADecompressor___init__(PyObject *self, PyObject *args, PyObject *kwargs)
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200177{
Larry Hastingsf256c222014-01-25 21:30:37 -0800178 int return_value = -1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300179 static const char * const _keywords[] = {"format", "memlimit", "filters", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200180 static _PyArg_Parser _parser = {NULL, _keywords, "LZMADecompressor", 0};
181 PyObject *argsbuf[3];
182 PyObject * const *fastargs;
183 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
184 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
Larry Hastingsf256c222014-01-25 21:30:37 -0800185 int format = FORMAT_AUTO;
186 PyObject *memlimit = Py_None;
187 PyObject *filters = Py_None;
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200188
Serhiy Storchaka31913912019-03-14 10:32:22 +0200189 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
190 if (!fastargs) {
Larry Hastingsf256c222014-01-25 21:30:37 -0800191 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300192 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200193 if (!noptargs) {
194 goto skip_optional_pos;
195 }
196 if (fastargs[0]) {
197 if (PyFloat_Check(fastargs[0])) {
198 PyErr_SetString(PyExc_TypeError,
199 "integer argument expected, got float" );
200 goto exit;
201 }
202 format = _PyLong_AsInt(fastargs[0]);
203 if (format == -1 && PyErr_Occurred()) {
204 goto exit;
205 }
206 if (!--noptargs) {
207 goto skip_optional_pos;
208 }
209 }
210 if (fastargs[1]) {
211 memlimit = fastargs[1];
212 if (!--noptargs) {
213 goto skip_optional_pos;
214 }
215 }
216 filters = fastargs[2];
217skip_optional_pos:
Larry Hastingsf256c222014-01-25 21:30:37 -0800218 return_value = _lzma_LZMADecompressor___init___impl((Decompressor *)self, format, memlimit, filters);
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200219
Larry Hastingsf256c222014-01-25 21:30:37 -0800220exit:
221 return return_value;
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200222}
223
Larry Hastingsf256c222014-01-25 21:30:37 -0800224PyDoc_STRVAR(_lzma_is_check_supported__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800225"is_check_supported($module, check_id, /)\n"
226"--\n"
227"\n"
Larry Hastingsf256c222014-01-25 21:30:37 -0800228"Test whether the given integrity check is supported.\n"
229"\n"
230"Always returns True for CHECK_NONE and CHECK_CRC32.");
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200231
Larry Hastingsf256c222014-01-25 21:30:37 -0800232#define _LZMA_IS_CHECK_SUPPORTED_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300233 {"is_check_supported", (PyCFunction)_lzma_is_check_supported, METH_O, _lzma_is_check_supported__doc__},
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200234
235static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300236_lzma_is_check_supported_impl(PyObject *module, int check_id);
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200237
238static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300239_lzma_is_check_supported(PyObject *module, PyObject *arg)
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200240{
Larry Hastingsf256c222014-01-25 21:30:37 -0800241 PyObject *return_value = NULL;
242 int check_id;
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200243
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200244 if (PyFloat_Check(arg)) {
245 PyErr_SetString(PyExc_TypeError,
246 "integer argument expected, got float" );
247 goto exit;
248 }
249 check_id = _PyLong_AsInt(arg);
250 if (check_id == -1 && PyErr_Occurred()) {
Larry Hastingsf256c222014-01-25 21:30:37 -0800251 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300252 }
Larry Hastingsf256c222014-01-25 21:30:37 -0800253 return_value = _lzma_is_check_supported_impl(module, check_id);
254
255exit:
256 return return_value;
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200257}
258
Larry Hastingsf256c222014-01-25 21:30:37 -0800259PyDoc_STRVAR(_lzma__encode_filter_properties__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800260"_encode_filter_properties($module, filter, /)\n"
261"--\n"
262"\n"
Larry Hastingsf256c222014-01-25 21:30:37 -0800263"Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict).\n"
264"\n"
265"The result does not include the filter ID itself, only the options.");
266
267#define _LZMA__ENCODE_FILTER_PROPERTIES_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300268 {"_encode_filter_properties", (PyCFunction)_lzma__encode_filter_properties, METH_O, _lzma__encode_filter_properties__doc__},
Larry Hastingsf256c222014-01-25 21:30:37 -0800269
Nadeem Vawda37970652013-10-28 21:35:23 +0100270static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300271_lzma__encode_filter_properties_impl(PyObject *module, lzma_filter filter);
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200272
273static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300274_lzma__encode_filter_properties(PyObject *module, PyObject *arg)
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200275{
Larry Hastingsf256c222014-01-25 21:30:37 -0800276 PyObject *return_value = NULL;
277 lzma_filter filter = {LZMA_VLI_UNKNOWN, NULL};
278
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200279 if (!lzma_filter_converter(arg, &filter)) {
Larry Hastingsf256c222014-01-25 21:30:37 -0800280 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300281 }
Larry Hastingsf256c222014-01-25 21:30:37 -0800282 return_value = _lzma__encode_filter_properties_impl(module, filter);
283
284exit:
285 /* Cleanup for filter */
286 if (filter.id != LZMA_VLI_UNKNOWN)
287 PyMem_Free(filter.options);
288
289 return return_value;
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200290}
291
Larry Hastingsf256c222014-01-25 21:30:37 -0800292PyDoc_STRVAR(_lzma__decode_filter_properties__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800293"_decode_filter_properties($module, filter_id, encoded_props, /)\n"
294"--\n"
295"\n"
Larry Hastingsf256c222014-01-25 21:30:37 -0800296"Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict).\n"
297"\n"
298"The result does not include the filter ID itself, only the options.");
Nadeem Vawda3ff069e2011-11-30 00:25:06 +0200299
Larry Hastingsf256c222014-01-25 21:30:37 -0800300#define _LZMA__DECODE_FILTER_PROPERTIES_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200301 {"_decode_filter_properties", (PyCFunction)(void(*)(void))_lzma__decode_filter_properties, METH_FASTCALL, _lzma__decode_filter_properties__doc__},
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200302
303static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300304_lzma__decode_filter_properties_impl(PyObject *module, lzma_vli filter_id,
Larry Hastings89964c42015-04-14 18:07:59 -0400305 Py_buffer *encoded_props);
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200306
307static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200308_lzma__decode_filter_properties(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200309{
Larry Hastingsf256c222014-01-25 21:30:37 -0800310 PyObject *return_value = NULL;
311 lzma_vli filter_id;
312 Py_buffer encoded_props = {NULL, NULL};
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200313
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200314 if (!_PyArg_CheckPositional("_decode_filter_properties", nargs, 2, 2)) {
315 goto exit;
316 }
317 if (!lzma_vli_converter(args[0], &filter_id)) {
318 goto exit;
319 }
320 if (PyObject_GetBuffer(args[1], &encoded_props, PyBUF_SIMPLE) != 0) {
321 goto exit;
322 }
323 if (!PyBuffer_IsContiguous(&encoded_props, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200324 _PyArg_BadArgument("_decode_filter_properties", "argument 2", "contiguous buffer", args[1]);
Victor Stinner259f0e42017-01-17 01:35:17 +0100325 goto exit;
326 }
Larry Hastingsf256c222014-01-25 21:30:37 -0800327 return_value = _lzma__decode_filter_properties_impl(module, filter_id, &encoded_props);
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200328
Larry Hastingsf256c222014-01-25 21:30:37 -0800329exit:
330 /* Cleanup for encoded_props */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300331 if (encoded_props.obj) {
Larry Hastingsf256c222014-01-25 21:30:37 -0800332 PyBuffer_Release(&encoded_props);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300333 }
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200334
Larry Hastingsf256c222014-01-25 21:30:37 -0800335 return return_value;
Nadeem Vawdaf55b3292012-05-06 23:01:27 +0200336}
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200337/*[clinic end generated code: output=f7477a10e86a717d input=a9049054013a1b77]*/