blob: 82942f08a68681fe98f4b8d7735ab9247373c061 [file] [log] [blame]
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(binascii_a2b_uu__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08006"a2b_uu($module, data, /)\n"
7"--\n"
8"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +02009"Decode a line of uuencoded data.");
10
11#define BINASCII_A2B_UU_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +030012 {"a2b_uu", (PyCFunction)binascii_a2b_uu, METH_O, binascii_a2b_uu__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020013
14static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030015binascii_a2b_uu_impl(PyObject *module, Py_buffer *data);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020016
17static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030018binascii_a2b_uu(PyObject *module, PyObject *arg)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020019{
20 PyObject *return_value = NULL;
Benjamin Petersonb62deac2014-01-26 10:41:58 -050021 Py_buffer data = {NULL, NULL};
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020022
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020023 if (!ascii_buffer_converter(arg, &data)) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020024 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030025 }
Serhiy Storchaka12785612014-01-25 11:49:49 +020026 return_value = binascii_a2b_uu_impl(module, &data);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020027
28exit:
Benjamin Petersonb62deac2014-01-26 10:41:58 -050029 /* Cleanup for data */
30 if (data.obj)
31 PyBuffer_Release(&data);
32
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020033 return return_value;
34}
35
36PyDoc_STRVAR(binascii_b2a_uu__doc__,
Xiang Zhang13f1f422017-05-03 11:16:21 +080037"b2a_uu($module, data, /, *, backtick=False)\n"
Larry Hastings2623c8c2014-02-08 22:15:29 -080038"--\n"
39"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020040"Uuencode line of data.");
41
42#define BINASCII_B2A_UU_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020043 {"b2a_uu", (PyCFunction)(void(*)(void))binascii_b2a_uu, METH_FASTCALL|METH_KEYWORDS, binascii_b2a_uu__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020044
45static PyObject *
Xiang Zhang13f1f422017-05-03 11:16:21 +080046binascii_b2a_uu_impl(PyObject *module, Py_buffer *data, int backtick);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020047
48static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020049binascii_b2a_uu(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020050{
51 PyObject *return_value = NULL;
Xiang Zhang13f1f422017-05-03 11:16:21 +080052 static const char * const _keywords[] = {"", "backtick", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020053 static _PyArg_Parser _parser = {NULL, _keywords, "b2a_uu", 0};
54 PyObject *argsbuf[2];
55 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020056 Py_buffer data = {NULL, NULL};
Xiang Zhang13f1f422017-05-03 11:16:21 +080057 int backtick = 0;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020058
Serhiy Storchaka31913912019-03-14 10:32:22 +020059 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
60 if (!args) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020061 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030062 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020063 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
64 goto exit;
65 }
66 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +020067 _PyArg_BadArgument("b2a_uu", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +020068 goto exit;
69 }
70 if (!noptargs) {
71 goto skip_optional_kwonly;
72 }
73 if (PyFloat_Check(args[1])) {
74 PyErr_SetString(PyExc_TypeError,
75 "integer argument expected, got float" );
76 goto exit;
77 }
78 backtick = _PyLong_AsInt(args[1]);
79 if (backtick == -1 && PyErr_Occurred()) {
80 goto exit;
81 }
82skip_optional_kwonly:
Xiang Zhang13f1f422017-05-03 11:16:21 +080083 return_value = binascii_b2a_uu_impl(module, &data, backtick);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020084
85exit:
86 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030087 if (data.obj) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020088 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030089 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020090
91 return return_value;
92}
93
94PyDoc_STRVAR(binascii_a2b_base64__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080095"a2b_base64($module, data, /)\n"
96"--\n"
97"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +020098"Decode a line of base64 data.");
99
100#define BINASCII_A2B_BASE64_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300101 {"a2b_base64", (PyCFunction)binascii_a2b_base64, METH_O, binascii_a2b_base64__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200102
103static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300104binascii_a2b_base64_impl(PyObject *module, Py_buffer *data);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200105
106static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300107binascii_a2b_base64(PyObject *module, PyObject *arg)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200108{
109 PyObject *return_value = NULL;
Benjamin Petersonb62deac2014-01-26 10:41:58 -0500110 Py_buffer data = {NULL, NULL};
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200111
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200112 if (!ascii_buffer_converter(arg, &data)) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200113 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300114 }
Serhiy Storchaka12785612014-01-25 11:49:49 +0200115 return_value = binascii_a2b_base64_impl(module, &data);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200116
117exit:
Benjamin Petersonb62deac2014-01-26 10:41:58 -0500118 /* Cleanup for data */
119 if (data.obj)
120 PyBuffer_Release(&data);
121
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200122 return return_value;
123}
124
125PyDoc_STRVAR(binascii_b2a_base64__doc__,
Xiang Zhang1374dbb2017-05-01 13:12:07 +0800126"b2a_base64($module, data, /, *, newline=True)\n"
Larry Hastings2623c8c2014-02-08 22:15:29 -0800127"--\n"
128"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200129"Base64-code line of data.");
130
131#define BINASCII_B2A_BASE64_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200132 {"b2a_base64", (PyCFunction)(void(*)(void))binascii_b2a_base64, METH_FASTCALL|METH_KEYWORDS, binascii_b2a_base64__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200133
134static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +0300135binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200136
137static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200138binascii_b2a_base64(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200139{
140 PyObject *return_value = NULL;
Xiang Zhang1374dbb2017-05-01 13:12:07 +0800141 static const char * const _keywords[] = {"", "newline", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200142 static _PyArg_Parser _parser = {NULL, _keywords, "b2a_base64", 0};
143 PyObject *argsbuf[2];
144 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200145 Py_buffer data = {NULL, NULL};
Victor Stinnere84c9762015-10-11 11:01:02 +0200146 int newline = 1;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200147
Serhiy Storchaka31913912019-03-14 10:32:22 +0200148 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
149 if (!args) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200150 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300151 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200152 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
153 goto exit;
154 }
155 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200156 _PyArg_BadArgument("b2a_base64", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200157 goto exit;
158 }
159 if (!noptargs) {
160 goto skip_optional_kwonly;
161 }
162 if (PyFloat_Check(args[1])) {
163 PyErr_SetString(PyExc_TypeError,
164 "integer argument expected, got float" );
165 goto exit;
166 }
167 newline = _PyLong_AsInt(args[1]);
168 if (newline == -1 && PyErr_Occurred()) {
169 goto exit;
170 }
171skip_optional_kwonly:
Victor Stinnere84c9762015-10-11 11:01:02 +0200172 return_value = binascii_b2a_base64_impl(module, &data, newline);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200173
174exit:
175 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300176 if (data.obj) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200177 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300178 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200179
180 return return_value;
181}
182
183PyDoc_STRVAR(binascii_a2b_hqx__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800184"a2b_hqx($module, data, /)\n"
185"--\n"
186"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200187"Decode .hqx coding.");
188
189#define BINASCII_A2B_HQX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300190 {"a2b_hqx", (PyCFunction)binascii_a2b_hqx, METH_O, binascii_a2b_hqx__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200191
192static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300193binascii_a2b_hqx_impl(PyObject *module, Py_buffer *data);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200194
195static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300196binascii_a2b_hqx(PyObject *module, PyObject *arg)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200197{
198 PyObject *return_value = NULL;
Benjamin Petersonb62deac2014-01-26 10:41:58 -0500199 Py_buffer data = {NULL, NULL};
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200200
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200201 if (!ascii_buffer_converter(arg, &data)) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200202 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300203 }
Serhiy Storchaka12785612014-01-25 11:49:49 +0200204 return_value = binascii_a2b_hqx_impl(module, &data);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200205
206exit:
Benjamin Petersonb62deac2014-01-26 10:41:58 -0500207 /* Cleanup for data */
208 if (data.obj)
209 PyBuffer_Release(&data);
210
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200211 return return_value;
212}
213
214PyDoc_STRVAR(binascii_rlecode_hqx__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800215"rlecode_hqx($module, data, /)\n"
216"--\n"
217"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200218"Binhex RLE-code binary data.");
219
220#define BINASCII_RLECODE_HQX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300221 {"rlecode_hqx", (PyCFunction)binascii_rlecode_hqx, METH_O, binascii_rlecode_hqx__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200222
223static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300224binascii_rlecode_hqx_impl(PyObject *module, Py_buffer *data);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200225
226static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300227binascii_rlecode_hqx(PyObject *module, PyObject *arg)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200228{
229 PyObject *return_value = NULL;
230 Py_buffer data = {NULL, NULL};
231
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200232 if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
233 goto exit;
234 }
235 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200236 _PyArg_BadArgument("rlecode_hqx", "argument", "contiguous buffer", arg);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200237 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300238 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200239 return_value = binascii_rlecode_hqx_impl(module, &data);
240
241exit:
242 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300243 if (data.obj) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200244 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300245 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200246
247 return return_value;
248}
249
250PyDoc_STRVAR(binascii_b2a_hqx__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800251"b2a_hqx($module, data, /)\n"
252"--\n"
253"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200254"Encode .hqx data.");
255
256#define BINASCII_B2A_HQX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300257 {"b2a_hqx", (PyCFunction)binascii_b2a_hqx, METH_O, binascii_b2a_hqx__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200258
259static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300260binascii_b2a_hqx_impl(PyObject *module, Py_buffer *data);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200261
262static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300263binascii_b2a_hqx(PyObject *module, PyObject *arg)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200264{
265 PyObject *return_value = NULL;
266 Py_buffer data = {NULL, NULL};
267
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200268 if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
269 goto exit;
270 }
271 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200272 _PyArg_BadArgument("b2a_hqx", "argument", "contiguous buffer", arg);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200273 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300274 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200275 return_value = binascii_b2a_hqx_impl(module, &data);
276
277exit:
278 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300279 if (data.obj) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200280 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300281 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200282
283 return return_value;
284}
285
286PyDoc_STRVAR(binascii_rledecode_hqx__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800287"rledecode_hqx($module, data, /)\n"
288"--\n"
289"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200290"Decode hexbin RLE-coded string.");
291
292#define BINASCII_RLEDECODE_HQX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300293 {"rledecode_hqx", (PyCFunction)binascii_rledecode_hqx, METH_O, binascii_rledecode_hqx__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200294
295static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300296binascii_rledecode_hqx_impl(PyObject *module, Py_buffer *data);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200297
298static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300299binascii_rledecode_hqx(PyObject *module, PyObject *arg)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200300{
301 PyObject *return_value = NULL;
302 Py_buffer data = {NULL, NULL};
303
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200304 if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
305 goto exit;
306 }
307 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200308 _PyArg_BadArgument("rledecode_hqx", "argument", "contiguous buffer", arg);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200309 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300310 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200311 return_value = binascii_rledecode_hqx_impl(module, &data);
312
313exit:
314 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300315 if (data.obj) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200316 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300317 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200318
319 return return_value;
320}
321
322PyDoc_STRVAR(binascii_crc_hqx__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800323"crc_hqx($module, data, crc, /)\n"
324"--\n"
325"\n"
Martin Panter3310e142016-12-24 07:36:44 +0000326"Compute CRC-CCITT incrementally.");
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200327
328#define BINASCII_CRC_HQX_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200329 {"crc_hqx", (PyCFunction)(void(*)(void))binascii_crc_hqx, METH_FASTCALL, binascii_crc_hqx__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200330
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300331static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300332binascii_crc_hqx_impl(PyObject *module, Py_buffer *data, unsigned int crc);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200333
334static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200335binascii_crc_hqx(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200336{
337 PyObject *return_value = NULL;
338 Py_buffer data = {NULL, NULL};
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300339 unsigned int crc;
340 unsigned int _return_value;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200341
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200342 if (!_PyArg_CheckPositional("crc_hqx", nargs, 2, 2)) {
343 goto exit;
344 }
345 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
346 goto exit;
347 }
348 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200349 _PyArg_BadArgument("crc_hqx", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200350 goto exit;
351 }
352 if (PyFloat_Check(args[1])) {
353 PyErr_SetString(PyExc_TypeError,
354 "integer argument expected, got float" );
355 goto exit;
356 }
357 crc = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
358 if (crc == (unsigned int)-1 && PyErr_Occurred()) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100359 goto exit;
360 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200361 _return_value = binascii_crc_hqx_impl(module, &data, crc);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300362 if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200363 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300364 }
Serhiy Storchaka2ef7c472015-04-20 09:26:49 +0300365 return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200366
367exit:
368 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300369 if (data.obj) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200370 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300371 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200372
373 return return_value;
374}
375
376PyDoc_STRVAR(binascii_crc32__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800377"crc32($module, data, crc=0, /)\n"
378"--\n"
379"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200380"Compute CRC-32 incrementally.");
381
382#define BINASCII_CRC32_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200383 {"crc32", (PyCFunction)(void(*)(void))binascii_crc32, METH_FASTCALL, binascii_crc32__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200384
385static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300386binascii_crc32_impl(PyObject *module, Py_buffer *data, unsigned int crc);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200387
388static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200389binascii_crc32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200390{
391 PyObject *return_value = NULL;
392 Py_buffer data = {NULL, NULL};
393 unsigned int crc = 0;
394 unsigned int _return_value;
395
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200396 if (!_PyArg_CheckPositional("crc32", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100397 goto exit;
398 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200399 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
400 goto exit;
401 }
402 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200403 _PyArg_BadArgument("crc32", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200404 goto exit;
405 }
406 if (nargs < 2) {
407 goto skip_optional;
408 }
409 if (PyFloat_Check(args[1])) {
410 PyErr_SetString(PyExc_TypeError,
411 "integer argument expected, got float" );
412 goto exit;
413 }
414 crc = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
415 if (crc == (unsigned int)-1 && PyErr_Occurred()) {
416 goto exit;
417 }
418skip_optional:
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200419 _return_value = binascii_crc32_impl(module, &data, crc);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300420 if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200421 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300422 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200423 return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
424
425exit:
426 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300427 if (data.obj) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200428 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300429 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200430
431 return return_value;
432}
433
434PyDoc_STRVAR(binascii_b2a_hex__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300435"b2a_hex($module, /, data, sep=<unrepresentable>, bytes_per_sep=1)\n"
Larry Hastings2623c8c2014-02-08 22:15:29 -0800436"--\n"
437"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200438"Hexadecimal representation of binary data.\n"
439"\n"
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700440" sep\n"
441" An optional single character or byte to separate hex bytes.\n"
442" bytes_per_sep\n"
443" How many bytes between separators. Positive values count from the\n"
444" right, negative values count from the left.\n"
445"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200446"The return value is a bytes object. This function is also\n"
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700447"available as \"hexlify()\".\n"
448"\n"
449"Example:\n"
450">>> binascii.b2a_hex(b\'\\xb9\\x01\\xef\')\n"
451"b\'b901ef\'\n"
452">>> binascii.hexlify(b\'\\xb9\\x01\\xef\', \':\')\n"
453"b\'b9:01:ef\'\n"
454">>> binascii.b2a_hex(b\'\\xb9\\x01\\xef\', b\'_\', 2)\n"
455"b\'b9_01ef\'");
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200456
457#define BINASCII_B2A_HEX_METHODDEF \
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700458 {"b2a_hex", (PyCFunction)(void(*)(void))binascii_b2a_hex, METH_FASTCALL|METH_KEYWORDS, binascii_b2a_hex__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200459
460static PyObject *
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700461binascii_b2a_hex_impl(PyObject *module, Py_buffer *data, PyObject *sep,
462 int bytes_per_sep);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200463
464static PyObject *
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700465binascii_b2a_hex(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200466{
467 PyObject *return_value = NULL;
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700468 static const char * const _keywords[] = {"data", "sep", "bytes_per_sep", NULL};
469 static _PyArg_Parser _parser = {NULL, _keywords, "b2a_hex", 0};
470 PyObject *argsbuf[3];
471 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200472 Py_buffer data = {NULL, NULL};
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700473 PyObject *sep = NULL;
474 int bytes_per_sep = 1;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200475
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700476 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
477 if (!args) {
478 goto exit;
479 }
480 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200481 goto exit;
482 }
483 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200484 _PyArg_BadArgument("b2a_hex", "argument 'data'", "contiguous buffer", args[0]);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200485 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300486 }
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700487 if (!noptargs) {
488 goto skip_optional_pos;
489 }
490 if (args[1]) {
491 sep = args[1];
492 if (!--noptargs) {
493 goto skip_optional_pos;
494 }
495 }
496 if (PyFloat_Check(args[2])) {
497 PyErr_SetString(PyExc_TypeError,
498 "integer argument expected, got float" );
499 goto exit;
500 }
501 bytes_per_sep = _PyLong_AsInt(args[2]);
502 if (bytes_per_sep == -1 && PyErr_Occurred()) {
503 goto exit;
504 }
505skip_optional_pos:
506 return_value = binascii_b2a_hex_impl(module, &data, sep, bytes_per_sep);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200507
508exit:
509 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300510 if (data.obj) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200511 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300512 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200513
514 return return_value;
515}
516
Zachary Wareb176d402015-01-20 13:59:46 -0600517PyDoc_STRVAR(binascii_hexlify__doc__,
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300518"hexlify($module, /, data, sep=<unrepresentable>, bytes_per_sep=1)\n"
Zachary Wareb176d402015-01-20 13:59:46 -0600519"--\n"
520"\n"
521"Hexadecimal representation of binary data.\n"
522"\n"
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700523" sep\n"
524" An optional single character or byte to separate hex bytes.\n"
525" bytes_per_sep\n"
526" How many bytes between separators. Positive values count from the\n"
527" right, negative values count from the left.\n"
528"\n"
529"The return value is a bytes object. This function is also\n"
530"available as \"b2a_hex()\".");
Zachary Wareb176d402015-01-20 13:59:46 -0600531
532#define BINASCII_HEXLIFY_METHODDEF \
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700533 {"hexlify", (PyCFunction)(void(*)(void))binascii_hexlify, METH_FASTCALL|METH_KEYWORDS, binascii_hexlify__doc__},
Zachary Wareb176d402015-01-20 13:59:46 -0600534
535static PyObject *
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700536binascii_hexlify_impl(PyObject *module, Py_buffer *data, PyObject *sep,
537 int bytes_per_sep);
Zachary Wareb176d402015-01-20 13:59:46 -0600538
539static PyObject *
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700540binascii_hexlify(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Zachary Wareb176d402015-01-20 13:59:46 -0600541{
542 PyObject *return_value = NULL;
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700543 static const char * const _keywords[] = {"data", "sep", "bytes_per_sep", NULL};
544 static _PyArg_Parser _parser = {NULL, _keywords, "hexlify", 0};
545 PyObject *argsbuf[3];
546 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Zachary Wareb176d402015-01-20 13:59:46 -0600547 Py_buffer data = {NULL, NULL};
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700548 PyObject *sep = NULL;
549 int bytes_per_sep = 1;
Zachary Wareb176d402015-01-20 13:59:46 -0600550
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700551 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
552 if (!args) {
553 goto exit;
554 }
555 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200556 goto exit;
557 }
558 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200559 _PyArg_BadArgument("hexlify", "argument 'data'", "contiguous buffer", args[0]);
Zachary Wareb176d402015-01-20 13:59:46 -0600560 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300561 }
Gregory P. Smith0c2f9302019-05-29 11:46:58 -0700562 if (!noptargs) {
563 goto skip_optional_pos;
564 }
565 if (args[1]) {
566 sep = args[1];
567 if (!--noptargs) {
568 goto skip_optional_pos;
569 }
570 }
571 if (PyFloat_Check(args[2])) {
572 PyErr_SetString(PyExc_TypeError,
573 "integer argument expected, got float" );
574 goto exit;
575 }
576 bytes_per_sep = _PyLong_AsInt(args[2]);
577 if (bytes_per_sep == -1 && PyErr_Occurred()) {
578 goto exit;
579 }
580skip_optional_pos:
581 return_value = binascii_hexlify_impl(module, &data, sep, bytes_per_sep);
Zachary Wareb176d402015-01-20 13:59:46 -0600582
583exit:
584 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300585 if (data.obj) {
Zachary Wareb176d402015-01-20 13:59:46 -0600586 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300587 }
Zachary Wareb176d402015-01-20 13:59:46 -0600588
589 return return_value;
590}
591
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200592PyDoc_STRVAR(binascii_a2b_hex__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800593"a2b_hex($module, hexstr, /)\n"
594"--\n"
595"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200596"Binary data of hexadecimal representation.\n"
597"\n"
598"hexstr must contain an even number of hex digits (upper or lower case).\n"
599"This function is also available as \"unhexlify()\".");
600
601#define BINASCII_A2B_HEX_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300602 {"a2b_hex", (PyCFunction)binascii_a2b_hex, METH_O, binascii_a2b_hex__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200603
604static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300605binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200606
607static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300608binascii_a2b_hex(PyObject *module, PyObject *arg)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200609{
610 PyObject *return_value = NULL;
Benjamin Petersonb62deac2014-01-26 10:41:58 -0500611 Py_buffer hexstr = {NULL, NULL};
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200612
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200613 if (!ascii_buffer_converter(arg, &hexstr)) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200614 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300615 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200616 return_value = binascii_a2b_hex_impl(module, &hexstr);
617
618exit:
Benjamin Petersonb62deac2014-01-26 10:41:58 -0500619 /* Cleanup for hexstr */
620 if (hexstr.obj)
621 PyBuffer_Release(&hexstr);
622
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200623 return return_value;
624}
625
Zachary Wareb176d402015-01-20 13:59:46 -0600626PyDoc_STRVAR(binascii_unhexlify__doc__,
627"unhexlify($module, hexstr, /)\n"
628"--\n"
629"\n"
630"Binary data of hexadecimal representation.\n"
631"\n"
632"hexstr must contain an even number of hex digits (upper or lower case).");
633
634#define BINASCII_UNHEXLIFY_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300635 {"unhexlify", (PyCFunction)binascii_unhexlify, METH_O, binascii_unhexlify__doc__},
Zachary Wareb176d402015-01-20 13:59:46 -0600636
637static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300638binascii_unhexlify_impl(PyObject *module, Py_buffer *hexstr);
Zachary Wareb176d402015-01-20 13:59:46 -0600639
640static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300641binascii_unhexlify(PyObject *module, PyObject *arg)
Zachary Wareb176d402015-01-20 13:59:46 -0600642{
643 PyObject *return_value = NULL;
644 Py_buffer hexstr = {NULL, NULL};
645
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200646 if (!ascii_buffer_converter(arg, &hexstr)) {
Zachary Wareb176d402015-01-20 13:59:46 -0600647 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300648 }
Zachary Wareb176d402015-01-20 13:59:46 -0600649 return_value = binascii_unhexlify_impl(module, &hexstr);
650
651exit:
652 /* Cleanup for hexstr */
653 if (hexstr.obj)
654 PyBuffer_Release(&hexstr);
655
656 return return_value;
657}
658
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200659PyDoc_STRVAR(binascii_a2b_qp__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800660"a2b_qp($module, /, data, header=False)\n"
661"--\n"
662"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200663"Decode a string of qp-encoded data.");
664
665#define BINASCII_A2B_QP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200666 {"a2b_qp", (PyCFunction)(void(*)(void))binascii_a2b_qp, METH_FASTCALL|METH_KEYWORDS, binascii_a2b_qp__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200667
668static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300669binascii_a2b_qp_impl(PyObject *module, Py_buffer *data, int header);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200670
671static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200672binascii_a2b_qp(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200673{
674 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300675 static const char * const _keywords[] = {"data", "header", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200676 static _PyArg_Parser _parser = {NULL, _keywords, "a2b_qp", 0};
677 PyObject *argsbuf[2];
678 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Benjamin Petersonb62deac2014-01-26 10:41:58 -0500679 Py_buffer data = {NULL, NULL};
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200680 int header = 0;
681
Serhiy Storchaka31913912019-03-14 10:32:22 +0200682 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
683 if (!args) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200684 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300685 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200686 if (!ascii_buffer_converter(args[0], &data)) {
687 goto exit;
688 }
689 if (!noptargs) {
690 goto skip_optional_pos;
691 }
692 if (PyFloat_Check(args[1])) {
693 PyErr_SetString(PyExc_TypeError,
694 "integer argument expected, got float" );
695 goto exit;
696 }
697 header = _PyLong_AsInt(args[1]);
698 if (header == -1 && PyErr_Occurred()) {
699 goto exit;
700 }
701skip_optional_pos:
Serhiy Storchaka12785612014-01-25 11:49:49 +0200702 return_value = binascii_a2b_qp_impl(module, &data, header);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200703
704exit:
Benjamin Petersonb62deac2014-01-26 10:41:58 -0500705 /* Cleanup for data */
706 if (data.obj)
707 PyBuffer_Release(&data);
708
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200709 return return_value;
710}
711
712PyDoc_STRVAR(binascii_b2a_qp__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800713"b2a_qp($module, /, data, quotetabs=False, istext=True, header=False)\n"
714"--\n"
715"\n"
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200716"Encode a string using quoted-printable encoding.\n"
717"\n"
718"On encoding, when istext is set, newlines are not encoded, and white\n"
719"space at end of lines is. When istext is not set, \\r and \\n (CR/LF)\n"
720"are both encoded. When quotetabs is set, space and tabs are encoded.");
721
722#define BINASCII_B2A_QP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200723 {"b2a_qp", (PyCFunction)(void(*)(void))binascii_b2a_qp, METH_FASTCALL|METH_KEYWORDS, binascii_b2a_qp__doc__},
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200724
725static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300726binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
Larry Hastings89964c42015-04-14 18:07:59 -0400727 int istext, int header);
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200728
729static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200730binascii_b2a_qp(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200731{
732 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300733 static const char * const _keywords[] = {"data", "quotetabs", "istext", "header", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200734 static _PyArg_Parser _parser = {NULL, _keywords, "b2a_qp", 0};
735 PyObject *argsbuf[4];
736 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200737 Py_buffer data = {NULL, NULL};
738 int quotetabs = 0;
739 int istext = 1;
740 int header = 0;
741
Serhiy Storchaka31913912019-03-14 10:32:22 +0200742 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 4, 0, argsbuf);
743 if (!args) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200744 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300745 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200746 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
747 goto exit;
748 }
749 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200750 _PyArg_BadArgument("b2a_qp", "argument 'data'", "contiguous buffer", args[0]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200751 goto exit;
752 }
753 if (!noptargs) {
754 goto skip_optional_pos;
755 }
756 if (args[1]) {
757 if (PyFloat_Check(args[1])) {
758 PyErr_SetString(PyExc_TypeError,
759 "integer argument expected, got float" );
760 goto exit;
761 }
762 quotetabs = _PyLong_AsInt(args[1]);
763 if (quotetabs == -1 && PyErr_Occurred()) {
764 goto exit;
765 }
766 if (!--noptargs) {
767 goto skip_optional_pos;
768 }
769 }
770 if (args[2]) {
771 if (PyFloat_Check(args[2])) {
772 PyErr_SetString(PyExc_TypeError,
773 "integer argument expected, got float" );
774 goto exit;
775 }
776 istext = _PyLong_AsInt(args[2]);
777 if (istext == -1 && PyErr_Occurred()) {
778 goto exit;
779 }
780 if (!--noptargs) {
781 goto skip_optional_pos;
782 }
783 }
784 if (PyFloat_Check(args[3])) {
785 PyErr_SetString(PyExc_TypeError,
786 "integer argument expected, got float" );
787 goto exit;
788 }
789 header = _PyLong_AsInt(args[3]);
790 if (header == -1 && PyErr_Occurred()) {
791 goto exit;
792 }
793skip_optional_pos:
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200794 return_value = binascii_b2a_qp_impl(module, &data, quotetabs, istext, header);
795
796exit:
797 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300798 if (data.obj) {
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200799 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300800 }
Serhiy Storchaka3ffd9132014-01-25 11:21:23 +0200801
802 return return_value;
803}
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300804/*[clinic end generated code: output=ec26d03c2007eaac input=a9049054013a1b77]*/