Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_STRVAR(zlib_compress__doc__, |
Serhiy Storchaka | 95657cd | 2016-06-25 22:43:05 +0300 | [diff] [blame] | 6 | "compress($module, data, /, level=Z_DEFAULT_COMPRESSION)\n" |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 7 | "--\n" |
| 8 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 9 | "Returns a bytes object containing compressed data.\n" |
| 10 | "\n" |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 11 | " data\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 12 | " Binary data to be compressed.\n" |
| 13 | " level\n" |
Martin Panter | b0cb42d | 2016-02-10 10:45:54 +0000 | [diff] [blame] | 14 | " Compression level, in 0-9 or -1."); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 15 | |
| 16 | #define ZLIB_COMPRESS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 17 | {"compress", (PyCFunction)(void(*)(void))zlib_compress, METH_FASTCALL|METH_KEYWORDS, zlib_compress__doc__}, |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 18 | |
| 19 | static PyObject * |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 20 | zlib_compress_impl(PyObject *module, Py_buffer *data, int level); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 21 | |
| 22 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 23 | zlib_compress(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 24 | { |
| 25 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 26 | static const char * const _keywords[] = {"", "level", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 27 | static _PyArg_Parser _parser = {NULL, _keywords, "compress", 0}; |
| 28 | PyObject *argsbuf[2]; |
| 29 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 30 | Py_buffer data = {NULL, NULL}; |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 31 | int level = Z_DEFAULT_COMPRESSION; |
| 32 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 33 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf); |
| 34 | if (!args) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 35 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 36 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 37 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 38 | goto exit; |
| 39 | } |
| 40 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 41 | _PyArg_BadArgument("compress", 1, "contiguous buffer", args[0]); |
| 42 | goto exit; |
| 43 | } |
| 44 | if (!noptargs) { |
| 45 | goto skip_optional_pos; |
| 46 | } |
| 47 | if (PyFloat_Check(args[1])) { |
| 48 | PyErr_SetString(PyExc_TypeError, |
| 49 | "integer argument expected, got float" ); |
| 50 | goto exit; |
| 51 | } |
| 52 | level = _PyLong_AsInt(args[1]); |
| 53 | if (level == -1 && PyErr_Occurred()) { |
| 54 | goto exit; |
| 55 | } |
| 56 | skip_optional_pos: |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 57 | return_value = zlib_compress_impl(module, &data, level); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 58 | |
| 59 | exit: |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 60 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 61 | if (data.obj) { |
Martin Panter | 1fe0d13 | 2016-02-10 10:06:36 +0000 | [diff] [blame] | 62 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 63 | } |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 64 | |
| 65 | return return_value; |
| 66 | } |
| 67 | |
| 68 | PyDoc_STRVAR(zlib_decompress__doc__, |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 69 | "decompress($module, data, /, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)\n" |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 70 | "--\n" |
| 71 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 72 | "Returns a bytes object containing the uncompressed data.\n" |
| 73 | "\n" |
| 74 | " data\n" |
| 75 | " Compressed data.\n" |
| 76 | " wbits\n" |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 77 | " The window buffer size and container format.\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 78 | " bufsize\n" |
| 79 | " The initial output buffer size."); |
| 80 | |
| 81 | #define ZLIB_DECOMPRESS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 82 | {"decompress", (PyCFunction)(void(*)(void))zlib_decompress, METH_FASTCALL|METH_KEYWORDS, zlib_decompress__doc__}, |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 83 | |
| 84 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 85 | zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits, |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 86 | Py_ssize_t bufsize); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 87 | |
| 88 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 89 | zlib_decompress(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 90 | { |
| 91 | PyObject *return_value = NULL; |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 92 | static const char * const _keywords[] = {"", "wbits", "bufsize", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 93 | static _PyArg_Parser _parser = {NULL, _keywords, "decompress", 0}; |
| 94 | PyObject *argsbuf[3]; |
| 95 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 96 | Py_buffer data = {NULL, NULL}; |
| 97 | int wbits = MAX_WBITS; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 98 | Py_ssize_t bufsize = DEF_BUF_SIZE; |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 99 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 100 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf); |
| 101 | if (!args) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 102 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 103 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 104 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 105 | goto exit; |
| 106 | } |
| 107 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 108 | _PyArg_BadArgument("decompress", 1, "contiguous buffer", args[0]); |
| 109 | goto exit; |
| 110 | } |
| 111 | if (!noptargs) { |
| 112 | goto skip_optional_pos; |
| 113 | } |
| 114 | if (args[1]) { |
| 115 | if (PyFloat_Check(args[1])) { |
| 116 | PyErr_SetString(PyExc_TypeError, |
| 117 | "integer argument expected, got float" ); |
| 118 | goto exit; |
| 119 | } |
| 120 | wbits = _PyLong_AsInt(args[1]); |
| 121 | if (wbits == -1 && PyErr_Occurred()) { |
| 122 | goto exit; |
| 123 | } |
| 124 | if (!--noptargs) { |
| 125 | goto skip_optional_pos; |
| 126 | } |
| 127 | } |
| 128 | if (!ssize_t_converter(args[2], &bufsize)) { |
| 129 | goto exit; |
| 130 | } |
| 131 | skip_optional_pos: |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 132 | return_value = zlib_decompress_impl(module, &data, wbits, bufsize); |
| 133 | |
| 134 | exit: |
| 135 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 136 | if (data.obj) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 137 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 138 | } |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 139 | |
| 140 | return return_value; |
| 141 | } |
| 142 | |
| 143 | PyDoc_STRVAR(zlib_compressobj__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 144 | "compressobj($module, /, level=Z_DEFAULT_COMPRESSION, method=DEFLATED,\n" |
| 145 | " wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL,\n" |
| 146 | " strategy=Z_DEFAULT_STRATEGY, zdict=None)\n" |
| 147 | "--\n" |
| 148 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 149 | "Return a compressor object.\n" |
| 150 | "\n" |
| 151 | " level\n" |
Martin Panter | 567d513 | 2016-02-03 07:06:33 +0000 | [diff] [blame] | 152 | " The compression level (an integer in the range 0-9 or -1; default is\n" |
| 153 | " currently equivalent to 6). Higher compression levels are slower,\n" |
| 154 | " but produce smaller results.\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 155 | " method\n" |
| 156 | " The compression algorithm. If given, this must be DEFLATED.\n" |
| 157 | " wbits\n" |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 158 | " +9 to +15: The base-two logarithm of the window size. Include a zlib\n" |
| 159 | " container.\n" |
| 160 | " -9 to -15: Generate a raw stream.\n" |
| 161 | " +25 to +31: Include a gzip container.\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 162 | " memLevel\n" |
| 163 | " Controls the amount of memory used for internal compression state.\n" |
| 164 | " Valid values range from 1 to 9. Higher values result in higher memory\n" |
| 165 | " usage, faster compression, and smaller output.\n" |
| 166 | " strategy\n" |
| 167 | " Used to tune the compression algorithm. Possible values are\n" |
| 168 | " Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n" |
| 169 | " zdict\n" |
| 170 | " The predefined compression dictionary - a sequence of bytes\n" |
| 171 | " containing subsequences that are likely to occur in the input data."); |
| 172 | |
| 173 | #define ZLIB_COMPRESSOBJ_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 174 | {"compressobj", (PyCFunction)(void(*)(void))zlib_compressobj, METH_FASTCALL|METH_KEYWORDS, zlib_compressobj__doc__}, |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 175 | |
| 176 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 177 | zlib_compressobj_impl(PyObject *module, int level, int method, int wbits, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 178 | int memLevel, int strategy, Py_buffer *zdict); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 179 | |
| 180 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 181 | zlib_compressobj(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 182 | { |
| 183 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 184 | static const char * const _keywords[] = {"level", "method", "wbits", "memLevel", "strategy", "zdict", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 185 | static _PyArg_Parser _parser = {NULL, _keywords, "compressobj", 0}; |
| 186 | PyObject *argsbuf[6]; |
| 187 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 188 | int level = Z_DEFAULT_COMPRESSION; |
| 189 | int method = DEFLATED; |
| 190 | int wbits = MAX_WBITS; |
| 191 | int memLevel = DEF_MEM_LEVEL; |
| 192 | int strategy = Z_DEFAULT_STRATEGY; |
| 193 | Py_buffer zdict = {NULL, NULL}; |
| 194 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 195 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 6, 0, argsbuf); |
| 196 | if (!args) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 197 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 198 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 199 | if (!noptargs) { |
| 200 | goto skip_optional_pos; |
| 201 | } |
| 202 | if (args[0]) { |
| 203 | if (PyFloat_Check(args[0])) { |
| 204 | PyErr_SetString(PyExc_TypeError, |
| 205 | "integer argument expected, got float" ); |
| 206 | goto exit; |
| 207 | } |
| 208 | level = _PyLong_AsInt(args[0]); |
| 209 | if (level == -1 && PyErr_Occurred()) { |
| 210 | goto exit; |
| 211 | } |
| 212 | if (!--noptargs) { |
| 213 | goto skip_optional_pos; |
| 214 | } |
| 215 | } |
| 216 | if (args[1]) { |
| 217 | if (PyFloat_Check(args[1])) { |
| 218 | PyErr_SetString(PyExc_TypeError, |
| 219 | "integer argument expected, got float" ); |
| 220 | goto exit; |
| 221 | } |
| 222 | method = _PyLong_AsInt(args[1]); |
| 223 | if (method == -1 && PyErr_Occurred()) { |
| 224 | goto exit; |
| 225 | } |
| 226 | if (!--noptargs) { |
| 227 | goto skip_optional_pos; |
| 228 | } |
| 229 | } |
| 230 | if (args[2]) { |
| 231 | if (PyFloat_Check(args[2])) { |
| 232 | PyErr_SetString(PyExc_TypeError, |
| 233 | "integer argument expected, got float" ); |
| 234 | goto exit; |
| 235 | } |
| 236 | wbits = _PyLong_AsInt(args[2]); |
| 237 | if (wbits == -1 && PyErr_Occurred()) { |
| 238 | goto exit; |
| 239 | } |
| 240 | if (!--noptargs) { |
| 241 | goto skip_optional_pos; |
| 242 | } |
| 243 | } |
| 244 | if (args[3]) { |
| 245 | if (PyFloat_Check(args[3])) { |
| 246 | PyErr_SetString(PyExc_TypeError, |
| 247 | "integer argument expected, got float" ); |
| 248 | goto exit; |
| 249 | } |
| 250 | memLevel = _PyLong_AsInt(args[3]); |
| 251 | if (memLevel == -1 && PyErr_Occurred()) { |
| 252 | goto exit; |
| 253 | } |
| 254 | if (!--noptargs) { |
| 255 | goto skip_optional_pos; |
| 256 | } |
| 257 | } |
| 258 | if (args[4]) { |
| 259 | if (PyFloat_Check(args[4])) { |
| 260 | PyErr_SetString(PyExc_TypeError, |
| 261 | "integer argument expected, got float" ); |
| 262 | goto exit; |
| 263 | } |
| 264 | strategy = _PyLong_AsInt(args[4]); |
| 265 | if (strategy == -1 && PyErr_Occurred()) { |
| 266 | goto exit; |
| 267 | } |
| 268 | if (!--noptargs) { |
| 269 | goto skip_optional_pos; |
| 270 | } |
| 271 | } |
| 272 | if (PyObject_GetBuffer(args[5], &zdict, PyBUF_SIMPLE) != 0) { |
| 273 | goto exit; |
| 274 | } |
| 275 | if (!PyBuffer_IsContiguous(&zdict, 'C')) { |
| 276 | _PyArg_BadArgument("compressobj", 6, "contiguous buffer", args[5]); |
| 277 | goto exit; |
| 278 | } |
| 279 | skip_optional_pos: |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 280 | return_value = zlib_compressobj_impl(module, level, method, wbits, memLevel, strategy, &zdict); |
| 281 | |
| 282 | exit: |
| 283 | /* Cleanup for zdict */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 284 | if (zdict.obj) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 285 | PyBuffer_Release(&zdict); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 286 | } |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 287 | |
| 288 | return return_value; |
| 289 | } |
| 290 | |
| 291 | PyDoc_STRVAR(zlib_decompressobj__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 292 | "decompressobj($module, /, wbits=MAX_WBITS, zdict=b\'\')\n" |
| 293 | "--\n" |
| 294 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 295 | "Return a decompressor object.\n" |
| 296 | "\n" |
| 297 | " wbits\n" |
Martin Panter | 0fdf41d | 2016-05-27 07:32:11 +0000 | [diff] [blame] | 298 | " The window buffer size and container format.\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 299 | " zdict\n" |
| 300 | " The predefined compression dictionary. This must be the same\n" |
| 301 | " dictionary as used by the compressor that produced the input data."); |
| 302 | |
| 303 | #define ZLIB_DECOMPRESSOBJ_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 304 | {"decompressobj", (PyCFunction)(void(*)(void))zlib_decompressobj, METH_FASTCALL|METH_KEYWORDS, zlib_decompressobj__doc__}, |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 305 | |
| 306 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 307 | zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 308 | |
| 309 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 310 | zlib_decompressobj(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 311 | { |
| 312 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 313 | static const char * const _keywords[] = {"wbits", "zdict", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 314 | static _PyArg_Parser _parser = {NULL, _keywords, "decompressobj", 0}; |
| 315 | PyObject *argsbuf[2]; |
| 316 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 317 | int wbits = MAX_WBITS; |
| 318 | PyObject *zdict = NULL; |
| 319 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 320 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 321 | if (!args) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 322 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 323 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 324 | if (!noptargs) { |
| 325 | goto skip_optional_pos; |
| 326 | } |
| 327 | if (args[0]) { |
| 328 | if (PyFloat_Check(args[0])) { |
| 329 | PyErr_SetString(PyExc_TypeError, |
| 330 | "integer argument expected, got float" ); |
| 331 | goto exit; |
| 332 | } |
| 333 | wbits = _PyLong_AsInt(args[0]); |
| 334 | if (wbits == -1 && PyErr_Occurred()) { |
| 335 | goto exit; |
| 336 | } |
| 337 | if (!--noptargs) { |
| 338 | goto skip_optional_pos; |
| 339 | } |
| 340 | } |
| 341 | zdict = args[1]; |
| 342 | skip_optional_pos: |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 343 | return_value = zlib_decompressobj_impl(module, wbits, zdict); |
| 344 | |
| 345 | exit: |
| 346 | return return_value; |
| 347 | } |
| 348 | |
| 349 | PyDoc_STRVAR(zlib_Compress_compress__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 350 | "compress($self, data, /)\n" |
| 351 | "--\n" |
| 352 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 353 | "Returns a bytes object containing compressed data.\n" |
| 354 | "\n" |
| 355 | " data\n" |
| 356 | " Binary data to be compressed.\n" |
| 357 | "\n" |
| 358 | "After calling this function, some of the input data may still\n" |
| 359 | "be stored in internal buffers for later processing.\n" |
| 360 | "Call the flush() method to clear these buffers."); |
| 361 | |
| 362 | #define ZLIB_COMPRESS_COMPRESS_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 363 | {"compress", (PyCFunction)zlib_Compress_compress, METH_O, zlib_Compress_compress__doc__}, |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 364 | |
| 365 | static PyObject * |
| 366 | zlib_Compress_compress_impl(compobject *self, Py_buffer *data); |
| 367 | |
| 368 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 369 | zlib_Compress_compress(compobject *self, PyObject *arg) |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 370 | { |
| 371 | PyObject *return_value = NULL; |
| 372 | Py_buffer data = {NULL, NULL}; |
| 373 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 374 | if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) { |
| 375 | goto exit; |
| 376 | } |
| 377 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 378 | _PyArg_BadArgument("compress", 0, "contiguous buffer", arg); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 379 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 380 | } |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 381 | return_value = zlib_Compress_compress_impl(self, &data); |
| 382 | |
| 383 | exit: |
| 384 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 385 | if (data.obj) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 386 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 387 | } |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 388 | |
| 389 | return return_value; |
| 390 | } |
| 391 | |
| 392 | PyDoc_STRVAR(zlib_Decompress_decompress__doc__, |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 393 | "decompress($self, data, /, max_length=0)\n" |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 394 | "--\n" |
| 395 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 396 | "Return a bytes object containing the decompressed version of the data.\n" |
| 397 | "\n" |
| 398 | " data\n" |
| 399 | " The binary data to decompress.\n" |
| 400 | " max_length\n" |
| 401 | " The maximum allowable length of the decompressed data.\n" |
| 402 | " Unconsumed input data will be stored in\n" |
| 403 | " the unconsumed_tail attribute.\n" |
| 404 | "\n" |
| 405 | "After calling this function, some of the input data may still be stored in\n" |
| 406 | "internal buffers for later processing.\n" |
| 407 | "Call the flush() method to clear these buffers."); |
| 408 | |
| 409 | #define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 410 | {"decompress", (PyCFunction)(void(*)(void))zlib_Decompress_decompress, METH_FASTCALL|METH_KEYWORDS, zlib_Decompress_decompress__doc__}, |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 411 | |
| 412 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 413 | zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data, |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 414 | Py_ssize_t max_length); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 415 | |
| 416 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 417 | zlib_Decompress_decompress(compobject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 418 | { |
| 419 | PyObject *return_value = NULL; |
Serhiy Storchaka | 15f3228 | 2016-08-15 10:06:16 +0300 | [diff] [blame] | 420 | static const char * const _keywords[] = {"", "max_length", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 421 | static _PyArg_Parser _parser = {NULL, _keywords, "decompress", 0}; |
| 422 | PyObject *argsbuf[2]; |
| 423 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 424 | Py_buffer data = {NULL, NULL}; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 425 | Py_ssize_t max_length = 0; |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 426 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 427 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf); |
| 428 | if (!args) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 429 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 430 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 431 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 432 | goto exit; |
| 433 | } |
| 434 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 435 | _PyArg_BadArgument("decompress", 1, "contiguous buffer", args[0]); |
| 436 | goto exit; |
| 437 | } |
| 438 | if (!noptargs) { |
| 439 | goto skip_optional_pos; |
| 440 | } |
| 441 | if (!ssize_t_converter(args[1], &max_length)) { |
| 442 | goto exit; |
| 443 | } |
| 444 | skip_optional_pos: |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 445 | return_value = zlib_Decompress_decompress_impl(self, &data, max_length); |
| 446 | |
| 447 | exit: |
| 448 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 449 | if (data.obj) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 450 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 451 | } |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 452 | |
| 453 | return return_value; |
| 454 | } |
| 455 | |
| 456 | PyDoc_STRVAR(zlib_Compress_flush__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 457 | "flush($self, mode=zlib.Z_FINISH, /)\n" |
| 458 | "--\n" |
| 459 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 460 | "Return a bytes object containing any remaining compressed data.\n" |
| 461 | "\n" |
| 462 | " mode\n" |
| 463 | " One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.\n" |
| 464 | " If mode == Z_FINISH, the compressor object can no longer be\n" |
| 465 | " used after calling the flush() method. Otherwise, more data\n" |
| 466 | " can still be compressed."); |
| 467 | |
| 468 | #define ZLIB_COMPRESS_FLUSH_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 469 | {"flush", (PyCFunction)(void(*)(void))zlib_Compress_flush, METH_FASTCALL, zlib_Compress_flush__doc__}, |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 470 | |
| 471 | static PyObject * |
| 472 | zlib_Compress_flush_impl(compobject *self, int mode); |
| 473 | |
| 474 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 475 | zlib_Compress_flush(compobject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 476 | { |
| 477 | PyObject *return_value = NULL; |
| 478 | int mode = Z_FINISH; |
| 479 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 480 | if (!_PyArg_CheckPositional("flush", nargs, 0, 1)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 481 | goto exit; |
| 482 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 483 | if (nargs < 1) { |
| 484 | goto skip_optional; |
| 485 | } |
| 486 | if (PyFloat_Check(args[0])) { |
| 487 | PyErr_SetString(PyExc_TypeError, |
| 488 | "integer argument expected, got float" ); |
| 489 | goto exit; |
| 490 | } |
| 491 | mode = _PyLong_AsInt(args[0]); |
| 492 | if (mode == -1 && PyErr_Occurred()) { |
| 493 | goto exit; |
| 494 | } |
| 495 | skip_optional: |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 496 | return_value = zlib_Compress_flush_impl(self, mode); |
| 497 | |
| 498 | exit: |
| 499 | return return_value; |
| 500 | } |
| 501 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 502 | #if defined(HAVE_ZLIB_COPY) |
| 503 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 504 | PyDoc_STRVAR(zlib_Compress_copy__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 505 | "copy($self, /)\n" |
| 506 | "--\n" |
| 507 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 508 | "Return a copy of the compression object."); |
| 509 | |
| 510 | #define ZLIB_COMPRESS_COPY_METHODDEF \ |
| 511 | {"copy", (PyCFunction)zlib_Compress_copy, METH_NOARGS, zlib_Compress_copy__doc__}, |
| 512 | |
| 513 | static PyObject * |
| 514 | zlib_Compress_copy_impl(compobject *self); |
| 515 | |
| 516 | static PyObject * |
| 517 | zlib_Compress_copy(compobject *self, PyObject *Py_UNUSED(ignored)) |
| 518 | { |
| 519 | return zlib_Compress_copy_impl(self); |
| 520 | } |
| 521 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 522 | #endif /* defined(HAVE_ZLIB_COPY) */ |
| 523 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 524 | #if defined(HAVE_ZLIB_COPY) |
| 525 | |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 526 | PyDoc_STRVAR(zlib_Compress___copy____doc__, |
| 527 | "__copy__($self, /)\n" |
| 528 | "--\n" |
| 529 | "\n"); |
| 530 | |
| 531 | #define ZLIB_COMPRESS___COPY___METHODDEF \ |
| 532 | {"__copy__", (PyCFunction)zlib_Compress___copy__, METH_NOARGS, zlib_Compress___copy____doc__}, |
| 533 | |
| 534 | static PyObject * |
| 535 | zlib_Compress___copy___impl(compobject *self); |
| 536 | |
| 537 | static PyObject * |
| 538 | zlib_Compress___copy__(compobject *self, PyObject *Py_UNUSED(ignored)) |
| 539 | { |
| 540 | return zlib_Compress___copy___impl(self); |
| 541 | } |
| 542 | |
| 543 | #endif /* defined(HAVE_ZLIB_COPY) */ |
| 544 | |
| 545 | #if defined(HAVE_ZLIB_COPY) |
| 546 | |
| 547 | PyDoc_STRVAR(zlib_Compress___deepcopy____doc__, |
| 548 | "__deepcopy__($self, memo, /)\n" |
| 549 | "--\n" |
| 550 | "\n"); |
| 551 | |
| 552 | #define ZLIB_COMPRESS___DEEPCOPY___METHODDEF \ |
| 553 | {"__deepcopy__", (PyCFunction)zlib_Compress___deepcopy__, METH_O, zlib_Compress___deepcopy____doc__}, |
| 554 | |
| 555 | #endif /* defined(HAVE_ZLIB_COPY) */ |
| 556 | |
| 557 | #if defined(HAVE_ZLIB_COPY) |
| 558 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 559 | PyDoc_STRVAR(zlib_Decompress_copy__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 560 | "copy($self, /)\n" |
| 561 | "--\n" |
| 562 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 563 | "Return a copy of the decompression object."); |
| 564 | |
| 565 | #define ZLIB_DECOMPRESS_COPY_METHODDEF \ |
| 566 | {"copy", (PyCFunction)zlib_Decompress_copy, METH_NOARGS, zlib_Decompress_copy__doc__}, |
| 567 | |
| 568 | static PyObject * |
| 569 | zlib_Decompress_copy_impl(compobject *self); |
| 570 | |
| 571 | static PyObject * |
| 572 | zlib_Decompress_copy(compobject *self, PyObject *Py_UNUSED(ignored)) |
| 573 | { |
| 574 | return zlib_Decompress_copy_impl(self); |
| 575 | } |
| 576 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 577 | #endif /* defined(HAVE_ZLIB_COPY) */ |
| 578 | |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 579 | #if defined(HAVE_ZLIB_COPY) |
| 580 | |
| 581 | PyDoc_STRVAR(zlib_Decompress___copy____doc__, |
| 582 | "__copy__($self, /)\n" |
| 583 | "--\n" |
| 584 | "\n"); |
| 585 | |
| 586 | #define ZLIB_DECOMPRESS___COPY___METHODDEF \ |
| 587 | {"__copy__", (PyCFunction)zlib_Decompress___copy__, METH_NOARGS, zlib_Decompress___copy____doc__}, |
| 588 | |
| 589 | static PyObject * |
| 590 | zlib_Decompress___copy___impl(compobject *self); |
| 591 | |
| 592 | static PyObject * |
| 593 | zlib_Decompress___copy__(compobject *self, PyObject *Py_UNUSED(ignored)) |
| 594 | { |
| 595 | return zlib_Decompress___copy___impl(self); |
| 596 | } |
| 597 | |
| 598 | #endif /* defined(HAVE_ZLIB_COPY) */ |
| 599 | |
| 600 | #if defined(HAVE_ZLIB_COPY) |
| 601 | |
| 602 | PyDoc_STRVAR(zlib_Decompress___deepcopy____doc__, |
| 603 | "__deepcopy__($self, memo, /)\n" |
| 604 | "--\n" |
| 605 | "\n"); |
| 606 | |
| 607 | #define ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF \ |
| 608 | {"__deepcopy__", (PyCFunction)zlib_Decompress___deepcopy__, METH_O, zlib_Decompress___deepcopy____doc__}, |
| 609 | |
| 610 | #endif /* defined(HAVE_ZLIB_COPY) */ |
| 611 | |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 612 | PyDoc_STRVAR(zlib_Decompress_flush__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 613 | "flush($self, length=zlib.DEF_BUF_SIZE, /)\n" |
| 614 | "--\n" |
| 615 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 616 | "Return a bytes object containing any remaining decompressed data.\n" |
| 617 | "\n" |
| 618 | " length\n" |
| 619 | " the initial size of the output buffer."); |
| 620 | |
| 621 | #define ZLIB_DECOMPRESS_FLUSH_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 622 | {"flush", (PyCFunction)(void(*)(void))zlib_Decompress_flush, METH_FASTCALL, zlib_Decompress_flush__doc__}, |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 623 | |
| 624 | static PyObject * |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 625 | zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 626 | |
| 627 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 628 | zlib_Decompress_flush(compobject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 629 | { |
| 630 | PyObject *return_value = NULL; |
Martin Panter | 84544c1 | 2016-07-23 03:02:07 +0000 | [diff] [blame] | 631 | Py_ssize_t length = DEF_BUF_SIZE; |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 632 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 633 | if (!_PyArg_CheckPositional("flush", nargs, 0, 1)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 634 | goto exit; |
| 635 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 636 | if (nargs < 1) { |
| 637 | goto skip_optional; |
| 638 | } |
| 639 | if (!ssize_t_converter(args[0], &length)) { |
| 640 | goto exit; |
| 641 | } |
| 642 | skip_optional: |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 643 | return_value = zlib_Decompress_flush_impl(self, length); |
| 644 | |
| 645 | exit: |
| 646 | return return_value; |
| 647 | } |
| 648 | |
| 649 | PyDoc_STRVAR(zlib_adler32__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 650 | "adler32($module, data, value=1, /)\n" |
| 651 | "--\n" |
| 652 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 653 | "Compute an Adler-32 checksum of data.\n" |
| 654 | "\n" |
| 655 | " value\n" |
| 656 | " Starting value of the checksum.\n" |
| 657 | "\n" |
| 658 | "The returned checksum is an integer."); |
| 659 | |
| 660 | #define ZLIB_ADLER32_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 661 | {"adler32", (PyCFunction)(void(*)(void))zlib_adler32, METH_FASTCALL, zlib_adler32__doc__}, |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 662 | |
| 663 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 664 | zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 665 | |
| 666 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 667 | zlib_adler32(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 668 | { |
| 669 | PyObject *return_value = NULL; |
| 670 | Py_buffer data = {NULL, NULL}; |
| 671 | unsigned int value = 1; |
| 672 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 673 | if (!_PyArg_CheckPositional("adler32", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 674 | goto exit; |
| 675 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 676 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 677 | goto exit; |
| 678 | } |
| 679 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 680 | _PyArg_BadArgument("adler32", 1, "contiguous buffer", args[0]); |
| 681 | goto exit; |
| 682 | } |
| 683 | if (nargs < 2) { |
| 684 | goto skip_optional; |
| 685 | } |
| 686 | if (PyFloat_Check(args[1])) { |
| 687 | PyErr_SetString(PyExc_TypeError, |
| 688 | "integer argument expected, got float" ); |
| 689 | goto exit; |
| 690 | } |
| 691 | value = (unsigned int)PyLong_AsUnsignedLongMask(args[1]); |
| 692 | if (value == (unsigned int)-1 && PyErr_Occurred()) { |
| 693 | goto exit; |
| 694 | } |
| 695 | skip_optional: |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 696 | return_value = zlib_adler32_impl(module, &data, value); |
| 697 | |
| 698 | exit: |
| 699 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 700 | if (data.obj) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 701 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 702 | } |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 703 | |
| 704 | return return_value; |
| 705 | } |
| 706 | |
| 707 | PyDoc_STRVAR(zlib_crc32__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 708 | "crc32($module, data, value=0, /)\n" |
| 709 | "--\n" |
| 710 | "\n" |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 711 | "Compute a CRC-32 checksum of data.\n" |
| 712 | "\n" |
| 713 | " value\n" |
| 714 | " Starting value of the checksum.\n" |
| 715 | "\n" |
| 716 | "The returned checksum is an integer."); |
| 717 | |
| 718 | #define ZLIB_CRC32_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 719 | {"crc32", (PyCFunction)(void(*)(void))zlib_crc32, METH_FASTCALL, zlib_crc32__doc__}, |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 720 | |
| 721 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 722 | zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value); |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 723 | |
| 724 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 725 | zlib_crc32(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 726 | { |
| 727 | PyObject *return_value = NULL; |
| 728 | Py_buffer data = {NULL, NULL}; |
| 729 | unsigned int value = 0; |
| 730 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 731 | if (!_PyArg_CheckPositional("crc32", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 732 | goto exit; |
| 733 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 734 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 735 | goto exit; |
| 736 | } |
| 737 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 738 | _PyArg_BadArgument("crc32", 1, "contiguous buffer", args[0]); |
| 739 | goto exit; |
| 740 | } |
| 741 | if (nargs < 2) { |
| 742 | goto skip_optional; |
| 743 | } |
| 744 | if (PyFloat_Check(args[1])) { |
| 745 | PyErr_SetString(PyExc_TypeError, |
| 746 | "integer argument expected, got float" ); |
| 747 | goto exit; |
| 748 | } |
| 749 | value = (unsigned int)PyLong_AsUnsignedLongMask(args[1]); |
| 750 | if (value == (unsigned int)-1 && PyErr_Occurred()) { |
| 751 | goto exit; |
| 752 | } |
| 753 | skip_optional: |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 754 | return_value = zlib_crc32_impl(module, &data, value); |
| 755 | |
| 756 | exit: |
| 757 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 758 | if (data.obj) { |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 759 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 760 | } |
Serhiy Storchaka | 2c5ddbe | 2014-01-27 00:03:31 +0200 | [diff] [blame] | 761 | |
| 762 | return return_value; |
| 763 | } |
Larry Hastings | 0759f84 | 2015-04-03 13:09:02 -0700 | [diff] [blame] | 764 | |
| 765 | #ifndef ZLIB_COMPRESS_COPY_METHODDEF |
| 766 | #define ZLIB_COMPRESS_COPY_METHODDEF |
| 767 | #endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */ |
Tal Einat | 4f57409 | 2017-11-03 11:09:00 +0200 | [diff] [blame] | 768 | |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 769 | #ifndef ZLIB_COMPRESS___COPY___METHODDEF |
| 770 | #define ZLIB_COMPRESS___COPY___METHODDEF |
| 771 | #endif /* !defined(ZLIB_COMPRESS___COPY___METHODDEF) */ |
| 772 | |
| 773 | #ifndef ZLIB_COMPRESS___DEEPCOPY___METHODDEF |
| 774 | #define ZLIB_COMPRESS___DEEPCOPY___METHODDEF |
| 775 | #endif /* !defined(ZLIB_COMPRESS___DEEPCOPY___METHODDEF) */ |
| 776 | |
Tal Einat | 4f57409 | 2017-11-03 11:09:00 +0200 | [diff] [blame] | 777 | #ifndef ZLIB_DECOMPRESS_COPY_METHODDEF |
| 778 | #define ZLIB_DECOMPRESS_COPY_METHODDEF |
| 779 | #endif /* !defined(ZLIB_DECOMPRESS_COPY_METHODDEF) */ |
Zackery Spytz | d2cbfff | 2018-06-27 12:04:51 -0600 | [diff] [blame] | 780 | |
| 781 | #ifndef ZLIB_DECOMPRESS___COPY___METHODDEF |
| 782 | #define ZLIB_DECOMPRESS___COPY___METHODDEF |
| 783 | #endif /* !defined(ZLIB_DECOMPRESS___COPY___METHODDEF) */ |
| 784 | |
| 785 | #ifndef ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF |
| 786 | #define ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF |
| 787 | #endif /* !defined(ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF) */ |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 788 | /*[clinic end generated code: output=feb079cebbbaacd6 input=a9049054013a1b77]*/ |