Debian Codegraph @

Modules/clinic/binascii.c.h

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