_lzmamodule.c.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(_lzma_LZMACompressor_compress__doc__,
  9. "compress($self, data, /)\n"
  10. "--\n"
  11. "\n"
  12. "Provide data to the compressor object.\n"
  13. "\n"
  14. "Returns a chunk of compressed data if possible, or b\'\' otherwise.\n"
  15. "\n"
  16. "When you have finished providing data to the compressor, call the\n"
  17. "flush() method to finish the compression process.");
  18. #define _LZMA_LZMACOMPRESSOR_COMPRESS_METHODDEF \
  19. {"compress", (PyCFunction)_lzma_LZMACompressor_compress, METH_O, _lzma_LZMACompressor_compress__doc__},
  20. static PyObject *
  21. _lzma_LZMACompressor_compress_impl(Compressor *self, Py_buffer *data);
  22. static PyObject *
  23. _lzma_LZMACompressor_compress(Compressor *self, PyObject *arg)
  24. {
  25. PyObject *return_value = NULL;
  26. Py_buffer data = {NULL, NULL};
  27. if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
  28. goto exit;
  29. }
  30. if (!PyBuffer_IsContiguous(&data, 'C')) {
  31. _PyArg_BadArgument("compress", "argument", "contiguous buffer", arg);
  32. goto exit;
  33. }
  34. return_value = _lzma_LZMACompressor_compress_impl(self, &data);
  35. exit:
  36. /* Cleanup for data */
  37. if (data.obj) {
  38. PyBuffer_Release(&data);
  39. }
  40. return return_value;
  41. }
  42. PyDoc_STRVAR(_lzma_LZMACompressor_flush__doc__,
  43. "flush($self, /)\n"
  44. "--\n"
  45. "\n"
  46. "Finish the compression process.\n"
  47. "\n"
  48. "Returns the compressed data left in internal buffers.\n"
  49. "\n"
  50. "The compressor object may not be used after this method is called.");
  51. #define _LZMA_LZMACOMPRESSOR_FLUSH_METHODDEF \
  52. {"flush", (PyCFunction)_lzma_LZMACompressor_flush, METH_NOARGS, _lzma_LZMACompressor_flush__doc__},
  53. static PyObject *
  54. _lzma_LZMACompressor_flush_impl(Compressor *self);
  55. static PyObject *
  56. _lzma_LZMACompressor_flush(Compressor *self, PyObject *Py_UNUSED(ignored))
  57. {
  58. return _lzma_LZMACompressor_flush_impl(self);
  59. }
  60. PyDoc_STRVAR(_lzma_LZMADecompressor_decompress__doc__,
  61. "decompress($self, /, data, max_length=-1)\n"
  62. "--\n"
  63. "\n"
  64. "Decompress *data*, returning uncompressed data as bytes.\n"
  65. "\n"
  66. "If *max_length* is nonnegative, returns at most *max_length* bytes of\n"
  67. "decompressed data. If this limit is reached and further output can be\n"
  68. "produced, *self.needs_input* will be set to ``False``. In this case, the next\n"
  69. "call to *decompress()* may provide *data* as b\'\' to obtain more of the output.\n"
  70. "\n"
  71. "If all of the input data was decompressed and returned (either because this\n"
  72. "was less than *max_length* bytes, or because *max_length* was negative),\n"
  73. "*self.needs_input* will be set to True.\n"
  74. "\n"
  75. "Attempting to decompress data after the end of stream is reached raises an\n"
  76. "EOFError. Any data found after the end of the stream is ignored and saved in\n"
  77. "the unused_data attribute.");
  78. #define _LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF \
  79. {"decompress", _PyCFunction_CAST(_lzma_LZMADecompressor_decompress), METH_FASTCALL|METH_KEYWORDS, _lzma_LZMADecompressor_decompress__doc__},
  80. static PyObject *
  81. _lzma_LZMADecompressor_decompress_impl(Decompressor *self, Py_buffer *data,
  82. Py_ssize_t max_length);
  83. static PyObject *
  84. _lzma_LZMADecompressor_decompress(Decompressor *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  85. {
  86. PyObject *return_value = NULL;
  87. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  88. #define NUM_KEYWORDS 2
  89. static struct {
  90. PyGC_Head _this_is_not_used;
  91. PyObject_VAR_HEAD
  92. PyObject *ob_item[NUM_KEYWORDS];
  93. } _kwtuple = {
  94. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  95. .ob_item = { &_Py_ID(data), &_Py_ID(max_length), },
  96. };
  97. #undef NUM_KEYWORDS
  98. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  99. #else // !Py_BUILD_CORE
  100. # define KWTUPLE NULL
  101. #endif // !Py_BUILD_CORE
  102. static const char * const _keywords[] = {"data", "max_length", NULL};
  103. static _PyArg_Parser _parser = {
  104. .keywords = _keywords,
  105. .fname = "decompress",
  106. .kwtuple = KWTUPLE,
  107. };
  108. #undef KWTUPLE
  109. PyObject *argsbuf[2];
  110. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  111. Py_buffer data = {NULL, NULL};
  112. Py_ssize_t max_length = -1;
  113. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
  114. if (!args) {
  115. goto exit;
  116. }
  117. if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
  118. goto exit;
  119. }
  120. if (!PyBuffer_IsContiguous(&data, 'C')) {
  121. _PyArg_BadArgument("decompress", "argument 'data'", "contiguous buffer", args[0]);
  122. goto exit;
  123. }
  124. if (!noptargs) {
  125. goto skip_optional_pos;
  126. }
  127. {
  128. Py_ssize_t ival = -1;
  129. PyObject *iobj = _PyNumber_Index(args[1]);
  130. if (iobj != NULL) {
  131. ival = PyLong_AsSsize_t(iobj);
  132. Py_DECREF(iobj);
  133. }
  134. if (ival == -1 && PyErr_Occurred()) {
  135. goto exit;
  136. }
  137. max_length = ival;
  138. }
  139. skip_optional_pos:
  140. return_value = _lzma_LZMADecompressor_decompress_impl(self, &data, max_length);
  141. exit:
  142. /* Cleanup for data */
  143. if (data.obj) {
  144. PyBuffer_Release(&data);
  145. }
  146. return return_value;
  147. }
  148. PyDoc_STRVAR(_lzma_LZMADecompressor__doc__,
  149. "LZMADecompressor(format=FORMAT_AUTO, memlimit=None, filters=None)\n"
  150. "--\n"
  151. "\n"
  152. "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. static PyObject *
  171. _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
  172. PyObject *memlimit, PyObject *filters);
  173. static PyObject *
  174. _lzma_LZMADecompressor(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  175. {
  176. PyObject *return_value = NULL;
  177. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  178. #define NUM_KEYWORDS 3
  179. static struct {
  180. PyGC_Head _this_is_not_used;
  181. PyObject_VAR_HEAD
  182. PyObject *ob_item[NUM_KEYWORDS];
  183. } _kwtuple = {
  184. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  185. .ob_item = { &_Py_ID(format), &_Py_ID(memlimit), &_Py_ID(filters), },
  186. };
  187. #undef NUM_KEYWORDS
  188. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  189. #else // !Py_BUILD_CORE
  190. # define KWTUPLE NULL
  191. #endif // !Py_BUILD_CORE
  192. static const char * const _keywords[] = {"format", "memlimit", "filters", NULL};
  193. static _PyArg_Parser _parser = {
  194. .keywords = _keywords,
  195. .fname = "LZMADecompressor",
  196. .kwtuple = KWTUPLE,
  197. };
  198. #undef KWTUPLE
  199. PyObject *argsbuf[3];
  200. PyObject * const *fastargs;
  201. Py_ssize_t nargs = PyTuple_GET_SIZE(args);
  202. Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
  203. int format = FORMAT_AUTO;
  204. PyObject *memlimit = Py_None;
  205. PyObject *filters = Py_None;
  206. fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 3, 0, argsbuf);
  207. if (!fastargs) {
  208. goto exit;
  209. }
  210. if (!noptargs) {
  211. goto skip_optional_pos;
  212. }
  213. if (fastargs[0]) {
  214. format = _PyLong_AsInt(fastargs[0]);
  215. if (format == -1 && PyErr_Occurred()) {
  216. goto exit;
  217. }
  218. if (!--noptargs) {
  219. goto skip_optional_pos;
  220. }
  221. }
  222. if (fastargs[1]) {
  223. memlimit = fastargs[1];
  224. if (!--noptargs) {
  225. goto skip_optional_pos;
  226. }
  227. }
  228. filters = fastargs[2];
  229. skip_optional_pos:
  230. return_value = _lzma_LZMADecompressor_impl(type, format, memlimit, filters);
  231. exit:
  232. return return_value;
  233. }
  234. PyDoc_STRVAR(_lzma_is_check_supported__doc__,
  235. "is_check_supported($module, check_id, /)\n"
  236. "--\n"
  237. "\n"
  238. "Test whether the given integrity check is supported.\n"
  239. "\n"
  240. "Always returns True for CHECK_NONE and CHECK_CRC32.");
  241. #define _LZMA_IS_CHECK_SUPPORTED_METHODDEF \
  242. {"is_check_supported", (PyCFunction)_lzma_is_check_supported, METH_O, _lzma_is_check_supported__doc__},
  243. static PyObject *
  244. _lzma_is_check_supported_impl(PyObject *module, int check_id);
  245. static PyObject *
  246. _lzma_is_check_supported(PyObject *module, PyObject *arg)
  247. {
  248. PyObject *return_value = NULL;
  249. int check_id;
  250. check_id = _PyLong_AsInt(arg);
  251. if (check_id == -1 && PyErr_Occurred()) {
  252. goto exit;
  253. }
  254. return_value = _lzma_is_check_supported_impl(module, check_id);
  255. exit:
  256. return return_value;
  257. }
  258. PyDoc_STRVAR(_lzma__decode_filter_properties__doc__,
  259. "_decode_filter_properties($module, filter_id, encoded_props, /)\n"
  260. "--\n"
  261. "\n"
  262. "Return a bytes object encoding the options (properties) of the filter specified by *filter* (a dict).\n"
  263. "\n"
  264. "The result does not include the filter ID itself, only the options.");
  265. #define _LZMA__DECODE_FILTER_PROPERTIES_METHODDEF \
  266. {"_decode_filter_properties", _PyCFunction_CAST(_lzma__decode_filter_properties), METH_FASTCALL, _lzma__decode_filter_properties__doc__},
  267. static PyObject *
  268. _lzma__decode_filter_properties_impl(PyObject *module, lzma_vli filter_id,
  269. Py_buffer *encoded_props);
  270. static PyObject *
  271. _lzma__decode_filter_properties(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  272. {
  273. PyObject *return_value = NULL;
  274. lzma_vli filter_id;
  275. Py_buffer encoded_props = {NULL, NULL};
  276. if (!_PyArg_CheckPositional("_decode_filter_properties", nargs, 2, 2)) {
  277. goto exit;
  278. }
  279. if (!lzma_vli_converter(args[0], &filter_id)) {
  280. goto exit;
  281. }
  282. if (PyObject_GetBuffer(args[1], &encoded_props, PyBUF_SIMPLE) != 0) {
  283. goto exit;
  284. }
  285. if (!PyBuffer_IsContiguous(&encoded_props, 'C')) {
  286. _PyArg_BadArgument("_decode_filter_properties", "argument 2", "contiguous buffer", args[1]);
  287. goto exit;
  288. }
  289. return_value = _lzma__decode_filter_properties_impl(module, filter_id, &encoded_props);
  290. exit:
  291. /* Cleanup for encoded_props */
  292. if (encoded_props.obj) {
  293. PyBuffer_Release(&encoded_props);
  294. }
  295. return return_value;
  296. }
  297. /*[clinic end generated code: output=96c1fbdada1ef232 input=a9049054013a1b77]*/