1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9
10
11 PyDoc_STRVAR(binascii_a2b_uu__doc__,
12 "a2b_uu($module, data, /)\n"
13 "--\n"
14 "\n"
15 "Decode a line of uuencoded data.");
16
17 #define BINASCII_A2B_UU_METHODDEF \
18 {"a2b_uu", (PyCFunction)binascii_a2b_uu, METH_O, binascii_a2b_uu__doc__},
19
20 static PyObject *
21 binascii_a2b_uu_impl(PyObject *module, Py_buffer *data);
22
23 static PyObject *
24 binascii_a2b_uu(PyObject *module, PyObject *arg)
25 {
26 PyObject *return_value = NULL;
27 Py_buffer data = {NULL, NULL};
28
29 if (!ascii_buffer_converter(arg, &data)) {
30 goto exit;
31 }
32 return_value = binascii_a2b_uu_impl(module, &data);
33
34 exit:
35 /* Cleanup for data */
36 if (data.obj)
37 PyBuffer_Release(&data);
38
39 return return_value;
40 }
41
42 PyDoc_STRVAR(binascii_b2a_uu__doc__,
43 "b2a_uu($module, data, /, *, backtick=False)\n"
44 "--\n"
45 "\n"
46 "Uuencode line of data.");
47
48 #define BINASCII_B2A_UU_METHODDEF \
49 {"b2a_uu", _PyCFunction_CAST(binascii_b2a_uu), METH_FASTCALL|METH_KEYWORDS, binascii_b2a_uu__doc__},
50
51 static PyObject *
52 binascii_b2a_uu_impl(PyObject *module, Py_buffer *data, int backtick);
53
54 static PyObject *
55 binascii_b2a_uu(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
56 {
57 PyObject *return_value = NULL;
58 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
59
60 #define NUM_KEYWORDS 1
61 static struct {
62 PyGC_Head _this_is_not_used;
63 PyObject_VAR_HEAD
64 PyObject *ob_item[NUM_KEYWORDS];
65 } _kwtuple = {
66 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
67 .ob_item = { &_Py_ID(backtick), },
68 };
69 #undef NUM_KEYWORDS
70 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
71
72 #else // !Py_BUILD_CORE
73 # define KWTUPLE NULL
74 #endif // !Py_BUILD_CORE
75
76 static const char * const _keywords[] = {"", "backtick", NULL};
77 static _PyArg_Parser _parser = {
78 .keywords = _keywords,
79 .fname = "b2a_uu",
80 .kwtuple = KWTUPLE,
81 };
82 #undef KWTUPLE
83 PyObject *argsbuf[2];
84 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
85 Py_buffer data = {NULL, NULL};
86 int backtick = 0;
87
88 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
89 if (!args) {
90 goto exit;
91 }
92 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
93 goto exit;
94 }
95 if (!PyBuffer_IsContiguous(&data, 'C')) {
96 _PyArg_BadArgument("b2a_uu", "argument 1", "contiguous buffer", args[0]);
97 goto exit;
98 }
99 if (!noptargs) {
100 goto skip_optional_kwonly;
101 }
102 backtick = PyObject_IsTrue(args[1]);
103 if (backtick < 0) {
104 goto exit;
105 }
106 skip_optional_kwonly:
107 return_value = binascii_b2a_uu_impl(module, &data, backtick);
108
109 exit:
110 /* Cleanup for data */
111 if (data.obj) {
112 PyBuffer_Release(&data);
113 }
114
115 return return_value;
116 }
117
118 PyDoc_STRVAR(binascii_a2b_base64__doc__,
119 "a2b_base64($module, data, /, *, strict_mode=False)\n"
120 "--\n"
121 "\n"
122 "Decode a line of base64 data.\n"
123 "\n"
124 " strict_mode\n"
125 " When set to True, bytes that are not part of the base64 standard are not allowed.\n"
126 " The same applies to excess data after padding (= / ==).");
127
128 #define BINASCII_A2B_BASE64_METHODDEF \
129 {"a2b_base64", _PyCFunction_CAST(binascii_a2b_base64), METH_FASTCALL|METH_KEYWORDS, binascii_a2b_base64__doc__},
130
131 static PyObject *
132 binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode);
133
134 static PyObject *
135 binascii_a2b_base64(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
136 {
137 PyObject *return_value = NULL;
138 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
139
140 #define NUM_KEYWORDS 1
141 static struct {
142 PyGC_Head _this_is_not_used;
143 PyObject_VAR_HEAD
144 PyObject *ob_item[NUM_KEYWORDS];
145 } _kwtuple = {
146 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
147 .ob_item = { &_Py_ID(strict_mode), },
148 };
149 #undef NUM_KEYWORDS
150 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
151
152 #else // !Py_BUILD_CORE
153 # define KWTUPLE NULL
154 #endif // !Py_BUILD_CORE
155
156 static const char * const _keywords[] = {"", "strict_mode", NULL};
157 static _PyArg_Parser _parser = {
158 .keywords = _keywords,
159 .fname = "a2b_base64",
160 .kwtuple = KWTUPLE,
161 };
162 #undef KWTUPLE
163 PyObject *argsbuf[2];
164 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
165 Py_buffer data = {NULL, NULL};
166 int strict_mode = 0;
167
168 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
169 if (!args) {
170 goto exit;
171 }
172 if (!ascii_buffer_converter(args[0], &data)) {
173 goto exit;
174 }
175 if (!noptargs) {
176 goto skip_optional_kwonly;
177 }
178 strict_mode = PyObject_IsTrue(args[1]);
179 if (strict_mode < 0) {
180 goto exit;
181 }
182 skip_optional_kwonly:
183 return_value = binascii_a2b_base64_impl(module, &data, strict_mode);
184
185 exit:
186 /* Cleanup for data */
187 if (data.obj)
188 PyBuffer_Release(&data);
189
190 return return_value;
191 }
192
193 PyDoc_STRVAR(binascii_b2a_base64__doc__,
194 "b2a_base64($module, data, /, *, newline=True)\n"
195 "--\n"
196 "\n"
197 "Base64-code line of data.");
198
199 #define BINASCII_B2A_BASE64_METHODDEF \
200 {"b2a_base64", _PyCFunction_CAST(binascii_b2a_base64), METH_FASTCALL|METH_KEYWORDS, binascii_b2a_base64__doc__},
201
202 static PyObject *
203 binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, int newline);
204
205 static PyObject *
206 binascii_b2a_base64(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
207 {
208 PyObject *return_value = NULL;
209 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
210
211 #define NUM_KEYWORDS 1
212 static struct {
213 PyGC_Head _this_is_not_used;
214 PyObject_VAR_HEAD
215 PyObject *ob_item[NUM_KEYWORDS];
216 } _kwtuple = {
217 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
218 .ob_item = { &_Py_ID(newline), },
219 };
220 #undef NUM_KEYWORDS
221 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
222
223 #else // !Py_BUILD_CORE
224 # define KWTUPLE NULL
225 #endif // !Py_BUILD_CORE
226
227 static const char * const _keywords[] = {"", "newline", NULL};
228 static _PyArg_Parser _parser = {
229 .keywords = _keywords,
230 .fname = "b2a_base64",
231 .kwtuple = KWTUPLE,
232 };
233 #undef KWTUPLE
234 PyObject *argsbuf[2];
235 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
236 Py_buffer data = {NULL, NULL};
237 int newline = 1;
238
239 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
240 if (!args) {
241 goto exit;
242 }
243 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
244 goto exit;
245 }
246 if (!PyBuffer_IsContiguous(&data, 'C')) {
247 _PyArg_BadArgument("b2a_base64", "argument 1", "contiguous buffer", args[0]);
248 goto exit;
249 }
250 if (!noptargs) {
251 goto skip_optional_kwonly;
252 }
253 newline = PyObject_IsTrue(args[1]);
254 if (newline < 0) {
255 goto exit;
256 }
257 skip_optional_kwonly:
258 return_value = binascii_b2a_base64_impl(module, &data, newline);
259
260 exit:
261 /* Cleanup for data */
262 if (data.obj) {
263 PyBuffer_Release(&data);
264 }
265
266 return return_value;
267 }
268
269 PyDoc_STRVAR(binascii_crc_hqx__doc__,
270 "crc_hqx($module, data, crc, /)\n"
271 "--\n"
272 "\n"
273 "Compute CRC-CCITT incrementally.");
274
275 #define BINASCII_CRC_HQX_METHODDEF \
276 {"crc_hqx", _PyCFunction_CAST(binascii_crc_hqx), METH_FASTCALL, binascii_crc_hqx__doc__},
277
278 static PyObject *
279 binascii_crc_hqx_impl(PyObject *module, Py_buffer *data, unsigned int crc);
280
281 static PyObject *
282 binascii_crc_hqx(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
283 {
284 PyObject *return_value = NULL;
285 Py_buffer data = {NULL, NULL};
286 unsigned int crc;
287
288 if (!_PyArg_CheckPositional("crc_hqx", nargs, 2, 2)) {
289 goto exit;
290 }
291 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
292 goto exit;
293 }
294 if (!PyBuffer_IsContiguous(&data, 'C')) {
295 _PyArg_BadArgument("crc_hqx", "argument 1", "contiguous buffer", args[0]);
296 goto exit;
297 }
298 crc = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
299 if (crc == (unsigned int)-1 && PyErr_Occurred()) {
300 goto exit;
301 }
302 return_value = binascii_crc_hqx_impl(module, &data, crc);
303
304 exit:
305 /* Cleanup for data */
306 if (data.obj) {
307 PyBuffer_Release(&data);
308 }
309
310 return return_value;
311 }
312
313 PyDoc_STRVAR(binascii_crc32__doc__,
314 "crc32($module, data, crc=0, /)\n"
315 "--\n"
316 "\n"
317 "Compute CRC-32 incrementally.");
318
319 #define BINASCII_CRC32_METHODDEF \
320 {"crc32", _PyCFunction_CAST(binascii_crc32), METH_FASTCALL, binascii_crc32__doc__},
321
322 static unsigned int
323 binascii_crc32_impl(PyObject *module, Py_buffer *data, unsigned int crc);
324
325 static PyObject *
326 binascii_crc32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
327 {
328 PyObject *return_value = NULL;
329 Py_buffer data = {NULL, NULL};
330 unsigned int crc = 0;
331 unsigned int _return_value;
332
333 if (!_PyArg_CheckPositional("crc32", nargs, 1, 2)) {
334 goto exit;
335 }
336 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
337 goto exit;
338 }
339 if (!PyBuffer_IsContiguous(&data, 'C')) {
340 _PyArg_BadArgument("crc32", "argument 1", "contiguous buffer", args[0]);
341 goto exit;
342 }
343 if (nargs < 2) {
344 goto skip_optional;
345 }
346 crc = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
347 if (crc == (unsigned int)-1 && PyErr_Occurred()) {
348 goto exit;
349 }
350 skip_optional:
351 _return_value = binascii_crc32_impl(module, &data, crc);
352 if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
353 goto exit;
354 }
355 return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
356
357 exit:
358 /* Cleanup for data */
359 if (data.obj) {
360 PyBuffer_Release(&data);
361 }
362
363 return return_value;
364 }
365
366 PyDoc_STRVAR(binascii_b2a_hex__doc__,
367 "b2a_hex($module, /, data, sep=<unrepresentable>, bytes_per_sep=1)\n"
368 "--\n"
369 "\n"
370 "Hexadecimal representation of binary data.\n"
371 "\n"
372 " sep\n"
373 " An optional single character or byte to separate hex bytes.\n"
374 " bytes_per_sep\n"
375 " How many bytes between separators. Positive values count from the\n"
376 " right, negative values count from the left.\n"
377 "\n"
378 "The return value is a bytes object. This function is also\n"
379 "available as \"hexlify()\".\n"
380 "\n"
381 "Example:\n"
382 ">>> binascii.b2a_hex(b\'\\xb9\\x01\\xef\')\n"
383 "b\'b901ef\'\n"
384 ">>> binascii.hexlify(b\'\\xb9\\x01\\xef\', \':\')\n"
385 "b\'b9:01:ef\'\n"
386 ">>> binascii.b2a_hex(b\'\\xb9\\x01\\xef\', b\'_\', 2)\n"
387 "b\'b9_01ef\'");
388
389 #define BINASCII_B2A_HEX_METHODDEF \
390 {"b2a_hex", _PyCFunction_CAST(binascii_b2a_hex), METH_FASTCALL|METH_KEYWORDS, binascii_b2a_hex__doc__},
391
392 static PyObject *
393 binascii_b2a_hex_impl(PyObject *module, Py_buffer *data, PyObject *sep,
394 int bytes_per_sep);
395
396 static PyObject *
397 binascii_b2a_hex(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
398 {
399 PyObject *return_value = NULL;
400 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
401
402 #define NUM_KEYWORDS 3
403 static struct {
404 PyGC_Head _this_is_not_used;
405 PyObject_VAR_HEAD
406 PyObject *ob_item[NUM_KEYWORDS];
407 } _kwtuple = {
408 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
409 .ob_item = { &_Py_ID(data), &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
410 };
411 #undef NUM_KEYWORDS
412 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
413
414 #else // !Py_BUILD_CORE
415 # define KWTUPLE NULL
416 #endif // !Py_BUILD_CORE
417
418 static const char * const _keywords[] = {"data", "sep", "bytes_per_sep", NULL};
419 static _PyArg_Parser _parser = {
420 .keywords = _keywords,
421 .fname = "b2a_hex",
422 .kwtuple = KWTUPLE,
423 };
424 #undef KWTUPLE
425 PyObject *argsbuf[3];
426 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
427 Py_buffer data = {NULL, NULL};
428 PyObject *sep = NULL;
429 int bytes_per_sep = 1;
430
431 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
432 if (!args) {
433 goto exit;
434 }
435 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
436 goto exit;
437 }
438 if (!PyBuffer_IsContiguous(&data, 'C')) {
439 _PyArg_BadArgument("b2a_hex", "argument 'data'", "contiguous buffer", args[0]);
440 goto exit;
441 }
442 if (!noptargs) {
443 goto skip_optional_pos;
444 }
445 if (args[1]) {
446 sep = args[1];
447 if (!--noptargs) {
448 goto skip_optional_pos;
449 }
450 }
451 bytes_per_sep = _PyLong_AsInt(args[2]);
452 if (bytes_per_sep == -1 && PyErr_Occurred()) {
453 goto exit;
454 }
455 skip_optional_pos:
456 return_value = binascii_b2a_hex_impl(module, &data, sep, bytes_per_sep);
457
458 exit:
459 /* Cleanup for data */
460 if (data.obj) {
461 PyBuffer_Release(&data);
462 }
463
464 return return_value;
465 }
466
467 PyDoc_STRVAR(binascii_hexlify__doc__,
468 "hexlify($module, /, data, sep=<unrepresentable>, bytes_per_sep=1)\n"
469 "--\n"
470 "\n"
471 "Hexadecimal representation of binary data.\n"
472 "\n"
473 " sep\n"
474 " An optional single character or byte to separate hex bytes.\n"
475 " bytes_per_sep\n"
476 " How many bytes between separators. Positive values count from the\n"
477 " right, negative values count from the left.\n"
478 "\n"
479 "The return value is a bytes object. This function is also\n"
480 "available as \"b2a_hex()\".");
481
482 #define BINASCII_HEXLIFY_METHODDEF \
483 {"hexlify", _PyCFunction_CAST(binascii_hexlify), METH_FASTCALL|METH_KEYWORDS, binascii_hexlify__doc__},
484
485 static PyObject *
486 binascii_hexlify_impl(PyObject *module, Py_buffer *data, PyObject *sep,
487 int bytes_per_sep);
488
489 static PyObject *
490 binascii_hexlify(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
491 {
492 PyObject *return_value = NULL;
493 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
494
495 #define NUM_KEYWORDS 3
496 static struct {
497 PyGC_Head _this_is_not_used;
498 PyObject_VAR_HEAD
499 PyObject *ob_item[NUM_KEYWORDS];
500 } _kwtuple = {
501 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
502 .ob_item = { &_Py_ID(data), &_Py_ID(sep), &_Py_ID(bytes_per_sep), },
503 };
504 #undef NUM_KEYWORDS
505 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
506
507 #else // !Py_BUILD_CORE
508 # define KWTUPLE NULL
509 #endif // !Py_BUILD_CORE
510
511 static const char * const _keywords[] = {"data", "sep", "bytes_per_sep", NULL};
512 static _PyArg_Parser _parser = {
513 .keywords = _keywords,
514 .fname = "hexlify",
515 .kwtuple = KWTUPLE,
516 };
517 #undef KWTUPLE
518 PyObject *argsbuf[3];
519 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
520 Py_buffer data = {NULL, NULL};
521 PyObject *sep = NULL;
522 int bytes_per_sep = 1;
523
524 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
525 if (!args) {
526 goto exit;
527 }
528 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
529 goto exit;
530 }
531 if (!PyBuffer_IsContiguous(&data, 'C')) {
532 _PyArg_BadArgument("hexlify", "argument 'data'", "contiguous buffer", args[0]);
533 goto exit;
534 }
535 if (!noptargs) {
536 goto skip_optional_pos;
537 }
538 if (args[1]) {
539 sep = args[1];
540 if (!--noptargs) {
541 goto skip_optional_pos;
542 }
543 }
544 bytes_per_sep = _PyLong_AsInt(args[2]);
545 if (bytes_per_sep == -1 && PyErr_Occurred()) {
546 goto exit;
547 }
548 skip_optional_pos:
549 return_value = binascii_hexlify_impl(module, &data, sep, bytes_per_sep);
550
551 exit:
552 /* Cleanup for data */
553 if (data.obj) {
554 PyBuffer_Release(&data);
555 }
556
557 return return_value;
558 }
559
560 PyDoc_STRVAR(binascii_a2b_hex__doc__,
561 "a2b_hex($module, hexstr, /)\n"
562 "--\n"
563 "\n"
564 "Binary data of hexadecimal representation.\n"
565 "\n"
566 "hexstr must contain an even number of hex digits (upper or lower case).\n"
567 "This function is also available as \"unhexlify()\".");
568
569 #define BINASCII_A2B_HEX_METHODDEF \
570 {"a2b_hex", (PyCFunction)binascii_a2b_hex, METH_O, binascii_a2b_hex__doc__},
571
572 static PyObject *
573 binascii_a2b_hex_impl(PyObject *module, Py_buffer *hexstr);
574
575 static PyObject *
576 binascii_a2b_hex(PyObject *module, PyObject *arg)
577 {
578 PyObject *return_value = NULL;
579 Py_buffer hexstr = {NULL, NULL};
580
581 if (!ascii_buffer_converter(arg, &hexstr)) {
582 goto exit;
583 }
584 return_value = binascii_a2b_hex_impl(module, &hexstr);
585
586 exit:
587 /* Cleanup for hexstr */
588 if (hexstr.obj)
589 PyBuffer_Release(&hexstr);
590
591 return return_value;
592 }
593
594 PyDoc_STRVAR(binascii_unhexlify__doc__,
595 "unhexlify($module, hexstr, /)\n"
596 "--\n"
597 "\n"
598 "Binary data of hexadecimal representation.\n"
599 "\n"
600 "hexstr must contain an even number of hex digits (upper or lower case).");
601
602 #define BINASCII_UNHEXLIFY_METHODDEF \
603 {"unhexlify", (PyCFunction)binascii_unhexlify, METH_O, binascii_unhexlify__doc__},
604
605 static PyObject *
606 binascii_unhexlify_impl(PyObject *module, Py_buffer *hexstr);
607
608 static PyObject *
609 binascii_unhexlify(PyObject *module, PyObject *arg)
610 {
611 PyObject *return_value = NULL;
612 Py_buffer hexstr = {NULL, NULL};
613
614 if (!ascii_buffer_converter(arg, &hexstr)) {
615 goto exit;
616 }
617 return_value = binascii_unhexlify_impl(module, &hexstr);
618
619 exit:
620 /* Cleanup for hexstr */
621 if (hexstr.obj)
622 PyBuffer_Release(&hexstr);
623
624 return return_value;
625 }
626
627 PyDoc_STRVAR(binascii_a2b_qp__doc__,
628 "a2b_qp($module, /, data, header=False)\n"
629 "--\n"
630 "\n"
631 "Decode a string of qp-encoded data.");
632
633 #define BINASCII_A2B_QP_METHODDEF \
634 {"a2b_qp", _PyCFunction_CAST(binascii_a2b_qp), METH_FASTCALL|METH_KEYWORDS, binascii_a2b_qp__doc__},
635
636 static PyObject *
637 binascii_a2b_qp_impl(PyObject *module, Py_buffer *data, int header);
638
639 static PyObject *
640 binascii_a2b_qp(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
641 {
642 PyObject *return_value = NULL;
643 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
644
645 #define NUM_KEYWORDS 2
646 static struct {
647 PyGC_Head _this_is_not_used;
648 PyObject_VAR_HEAD
649 PyObject *ob_item[NUM_KEYWORDS];
650 } _kwtuple = {
651 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
652 .ob_item = { &_Py_ID(data), &_Py_ID(header), },
653 };
654 #undef NUM_KEYWORDS
655 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
656
657 #else // !Py_BUILD_CORE
658 # define KWTUPLE NULL
659 #endif // !Py_BUILD_CORE
660
661 static const char * const _keywords[] = {"data", "header", NULL};
662 static _PyArg_Parser _parser = {
663 .keywords = _keywords,
664 .fname = "a2b_qp",
665 .kwtuple = KWTUPLE,
666 };
667 #undef KWTUPLE
668 PyObject *argsbuf[2];
669 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
670 Py_buffer data = {NULL, NULL};
671 int header = 0;
672
673 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
674 if (!args) {
675 goto exit;
676 }
677 if (!ascii_buffer_converter(args[0], &data)) {
678 goto exit;
679 }
680 if (!noptargs) {
681 goto skip_optional_pos;
682 }
683 header = PyObject_IsTrue(args[1]);
684 if (header < 0) {
685 goto exit;
686 }
687 skip_optional_pos:
688 return_value = binascii_a2b_qp_impl(module, &data, header);
689
690 exit:
691 /* Cleanup for data */
692 if (data.obj)
693 PyBuffer_Release(&data);
694
695 return return_value;
696 }
697
698 PyDoc_STRVAR(binascii_b2a_qp__doc__,
699 "b2a_qp($module, /, data, quotetabs=False, istext=True, header=False)\n"
700 "--\n"
701 "\n"
702 "Encode a string using quoted-printable encoding.\n"
703 "\n"
704 "On encoding, when istext is set, newlines are not encoded, and white\n"
705 "space at end of lines is. When istext is not set, \\r and \\n (CR/LF)\n"
706 "are both encoded. When quotetabs is set, space and tabs are encoded.");
707
708 #define BINASCII_B2A_QP_METHODDEF \
709 {"b2a_qp", _PyCFunction_CAST(binascii_b2a_qp), METH_FASTCALL|METH_KEYWORDS, binascii_b2a_qp__doc__},
710
711 static PyObject *
712 binascii_b2a_qp_impl(PyObject *module, Py_buffer *data, int quotetabs,
713 int istext, int header);
714
715 static PyObject *
716 binascii_b2a_qp(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
717 {
718 PyObject *return_value = NULL;
719 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
720
721 #define NUM_KEYWORDS 4
722 static struct {
723 PyGC_Head _this_is_not_used;
724 PyObject_VAR_HEAD
725 PyObject *ob_item[NUM_KEYWORDS];
726 } _kwtuple = {
727 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
728 .ob_item = { &_Py_ID(data), &_Py_ID(quotetabs), &_Py_ID(istext), &_Py_ID(header), },
729 };
730 #undef NUM_KEYWORDS
731 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
732
733 #else // !Py_BUILD_CORE
734 # define KWTUPLE NULL
735 #endif // !Py_BUILD_CORE
736
737 static const char * const _keywords[] = {"data", "quotetabs", "istext", "header", NULL};
738 static _PyArg_Parser _parser = {
739 .keywords = _keywords,
740 .fname = "b2a_qp",
741 .kwtuple = KWTUPLE,
742 };
743 #undef KWTUPLE
744 PyObject *argsbuf[4];
745 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
746 Py_buffer data = {NULL, NULL};
747 int quotetabs = 0;
748 int istext = 1;
749 int header = 0;
750
751 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 4, 0, argsbuf);
752 if (!args) {
753 goto exit;
754 }
755 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
756 goto exit;
757 }
758 if (!PyBuffer_IsContiguous(&data, 'C')) {
759 _PyArg_BadArgument("b2a_qp", "argument 'data'", "contiguous buffer", args[0]);
760 goto exit;
761 }
762 if (!noptargs) {
763 goto skip_optional_pos;
764 }
765 if (args[1]) {
766 quotetabs = PyObject_IsTrue(args[1]);
767 if (quotetabs < 0) {
768 goto exit;
769 }
770 if (!--noptargs) {
771 goto skip_optional_pos;
772 }
773 }
774 if (args[2]) {
775 istext = PyObject_IsTrue(args[2]);
776 if (istext < 0) {
777 goto exit;
778 }
779 if (!--noptargs) {
780 goto skip_optional_pos;
781 }
782 }
783 header = PyObject_IsTrue(args[3]);
784 if (header < 0) {
785 goto exit;
786 }
787 skip_optional_pos:
788 return_value = binascii_b2a_qp_impl(module, &data, quotetabs, istext, header);
789
790 exit:
791 /* Cleanup for data */
792 if (data.obj) {
793 PyBuffer_Release(&data);
794 }
795
796 return return_value;
797 }
798 /*[clinic end generated code: output=ab156917c9db79d2 input=a9049054013a1b77]*/