Branch data Line data Source code
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 : 0 : binascii_a2b_uu(PyObject *module, PyObject *arg)
25 : : {
26 : 0 : PyObject *return_value = NULL;
27 : 0 : Py_buffer data = {NULL, NULL};
28 : :
29 [ # # ]: 0 : if (!ascii_buffer_converter(arg, &data)) {
30 : 0 : goto exit;
31 : : }
32 : 0 : return_value = binascii_a2b_uu_impl(module, &data);
33 : :
34 : 0 : exit:
35 : : /* Cleanup for data */
36 [ # # ]: 0 : if (data.obj)
37 : 0 : PyBuffer_Release(&data);
38 : :
39 : 0 : 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 : 0 : binascii_b2a_uu(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
56 : : {
57 : 0 : 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 [ # # ]: 0 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
85 : 0 : Py_buffer data = {NULL, NULL};
86 : 0 : int backtick = 0;
87 : :
88 [ # # # # : 0 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
# # # # ]
89 [ # # ]: 0 : if (!args) {
90 : 0 : goto exit;
91 : : }
92 [ # # ]: 0 : if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
93 : 0 : goto exit;
94 : : }
95 [ # # ]: 0 : if (!PyBuffer_IsContiguous(&data, 'C')) {
96 : 0 : _PyArg_BadArgument("b2a_uu", "argument 1", "contiguous buffer", args[0]);
97 : 0 : goto exit;
98 : : }
99 [ # # ]: 0 : if (!noptargs) {
100 : 0 : goto skip_optional_kwonly;
101 : : }
102 : 0 : backtick = PyObject_IsTrue(args[1]);
103 [ # # ]: 0 : if (backtick < 0) {
104 : 0 : goto exit;
105 : : }
106 : 0 : skip_optional_kwonly:
107 : 0 : return_value = binascii_b2a_uu_impl(module, &data, backtick);
108 : :
109 : 0 : exit:
110 : : /* Cleanup for data */
111 [ # # ]: 0 : if (data.obj) {
112 : 0 : PyBuffer_Release(&data);
113 : : }
114 : :
115 : 0 : 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 : 0 : binascii_a2b_base64(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
136 : : {
137 : 0 : 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 [ # # ]: 0 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
165 : 0 : Py_buffer data = {NULL, NULL};
166 : 0 : int strict_mode = 0;
167 : :
168 [ # # # # : 0 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
# # # # ]
169 [ # # ]: 0 : if (!args) {
170 : 0 : goto exit;
171 : : }
172 [ # # ]: 0 : if (!ascii_buffer_converter(args[0], &data)) {
173 : 0 : goto exit;
174 : : }
175 [ # # ]: 0 : if (!noptargs) {
176 : 0 : goto skip_optional_kwonly;
177 : : }
178 : 0 : strict_mode = PyObject_IsTrue(args[1]);
179 [ # # ]: 0 : if (strict_mode < 0) {
180 : 0 : goto exit;
181 : : }
182 : 0 : skip_optional_kwonly:
183 : 0 : return_value = binascii_a2b_base64_impl(module, &data, strict_mode);
184 : :
185 : 0 : exit:
186 : : /* Cleanup for data */
187 [ # # ]: 0 : if (data.obj)
188 : 0 : PyBuffer_Release(&data);
189 : :
190 : 0 : 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 : 0 : binascii_b2a_base64(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
207 : : {
208 : 0 : 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 [ # # ]: 0 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
236 : 0 : Py_buffer data = {NULL, NULL};
237 : 0 : int newline = 1;
238 : :
239 [ # # # # : 0 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
# # # # ]
240 [ # # ]: 0 : if (!args) {
241 : 0 : goto exit;
242 : : }
243 [ # # ]: 0 : if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
244 : 0 : goto exit;
245 : : }
246 [ # # ]: 0 : if (!PyBuffer_IsContiguous(&data, 'C')) {
247 : 0 : _PyArg_BadArgument("b2a_base64", "argument 1", "contiguous buffer", args[0]);
248 : 0 : goto exit;
249 : : }
250 [ # # ]: 0 : if (!noptargs) {
251 : 0 : goto skip_optional_kwonly;
252 : : }
253 : 0 : newline = PyObject_IsTrue(args[1]);
254 [ # # ]: 0 : if (newline < 0) {
255 : 0 : goto exit;
256 : : }
257 : 0 : skip_optional_kwonly:
258 : 0 : return_value = binascii_b2a_base64_impl(module, &data, newline);
259 : :
260 : 0 : exit:
261 : : /* Cleanup for data */
262 [ # # ]: 0 : if (data.obj) {
263 : 0 : PyBuffer_Release(&data);
264 : : }
265 : :
266 : 0 : 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 : 0 : binascii_crc_hqx(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
283 : : {
284 : 0 : PyObject *return_value = NULL;
285 : 0 : Py_buffer data = {NULL, NULL};
286 : : unsigned int crc;
287 : :
288 [ # # # # : 0 : if (!_PyArg_CheckPositional("crc_hqx", nargs, 2, 2)) {
# # ]
289 : 0 : goto exit;
290 : : }
291 [ # # ]: 0 : if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
292 : 0 : goto exit;
293 : : }
294 [ # # ]: 0 : if (!PyBuffer_IsContiguous(&data, 'C')) {
295 : 0 : _PyArg_BadArgument("crc_hqx", "argument 1", "contiguous buffer", args[0]);
296 : 0 : goto exit;
297 : : }
298 : 0 : crc = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
299 [ # # # # ]: 0 : if (crc == (unsigned int)-1 && PyErr_Occurred()) {
300 : 0 : goto exit;
301 : : }
302 : 0 : return_value = binascii_crc_hqx_impl(module, &data, crc);
303 : :
304 : 0 : exit:
305 : : /* Cleanup for data */
306 [ # # ]: 0 : if (data.obj) {
307 : 0 : PyBuffer_Release(&data);
308 : : }
309 : :
310 : 0 : 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 : 0 : binascii_crc32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
327 : : {
328 : 0 : PyObject *return_value = NULL;
329 : 0 : Py_buffer data = {NULL, NULL};
330 : 0 : unsigned int crc = 0;
331 : : unsigned int _return_value;
332 : :
333 [ # # # # : 0 : if (!_PyArg_CheckPositional("crc32", nargs, 1, 2)) {
# # ]
334 : 0 : goto exit;
335 : : }
336 [ # # ]: 0 : if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
337 : 0 : goto exit;
338 : : }
339 [ # # ]: 0 : if (!PyBuffer_IsContiguous(&data, 'C')) {
340 : 0 : _PyArg_BadArgument("crc32", "argument 1", "contiguous buffer", args[0]);
341 : 0 : goto exit;
342 : : }
343 [ # # ]: 0 : if (nargs < 2) {
344 : 0 : goto skip_optional;
345 : : }
346 : 0 : crc = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
347 [ # # # # ]: 0 : if (crc == (unsigned int)-1 && PyErr_Occurred()) {
348 : 0 : goto exit;
349 : : }
350 : 0 : skip_optional:
351 : 0 : _return_value = binascii_crc32_impl(module, &data, crc);
352 [ # # # # ]: 0 : if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
353 : 0 : goto exit;
354 : : }
355 : 0 : return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
356 : :
357 : 0 : exit:
358 : : /* Cleanup for data */
359 [ # # ]: 0 : if (data.obj) {
360 : 0 : PyBuffer_Release(&data);
361 : : }
362 : :
363 : 0 : 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 : 0 : binascii_b2a_hex(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
398 : : {
399 : 0 : 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 [ # # ]: 0 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
427 : 0 : Py_buffer data = {NULL, NULL};
428 : 0 : PyObject *sep = NULL;
429 : 0 : int bytes_per_sep = 1;
430 : :
431 [ # # # # : 0 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
# # # # ]
432 [ # # ]: 0 : if (!args) {
433 : 0 : goto exit;
434 : : }
435 [ # # ]: 0 : if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
436 : 0 : goto exit;
437 : : }
438 [ # # ]: 0 : if (!PyBuffer_IsContiguous(&data, 'C')) {
439 : 0 : _PyArg_BadArgument("b2a_hex", "argument 'data'", "contiguous buffer", args[0]);
440 : 0 : goto exit;
441 : : }
442 [ # # ]: 0 : if (!noptargs) {
443 : 0 : goto skip_optional_pos;
444 : : }
445 [ # # ]: 0 : if (args[1]) {
446 : 0 : sep = args[1];
447 [ # # ]: 0 : if (!--noptargs) {
448 : 0 : goto skip_optional_pos;
449 : : }
450 : : }
451 : 0 : bytes_per_sep = _PyLong_AsInt(args[2]);
452 [ # # # # ]: 0 : if (bytes_per_sep == -1 && PyErr_Occurred()) {
453 : 0 : goto exit;
454 : : }
455 : 0 : skip_optional_pos:
456 : 0 : return_value = binascii_b2a_hex_impl(module, &data, sep, bytes_per_sep);
457 : :
458 : 0 : exit:
459 : : /* Cleanup for data */
460 [ # # ]: 0 : if (data.obj) {
461 : 0 : PyBuffer_Release(&data);
462 : : }
463 : :
464 : 0 : 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 : 0 : binascii_hexlify(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
491 : : {
492 : 0 : 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 [ # # ]: 0 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
520 : 0 : Py_buffer data = {NULL, NULL};
521 : 0 : PyObject *sep = NULL;
522 : 0 : int bytes_per_sep = 1;
523 : :
524 [ # # # # : 0 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
# # # # ]
525 [ # # ]: 0 : if (!args) {
526 : 0 : goto exit;
527 : : }
528 [ # # ]: 0 : if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
529 : 0 : goto exit;
530 : : }
531 [ # # ]: 0 : if (!PyBuffer_IsContiguous(&data, 'C')) {
532 : 0 : _PyArg_BadArgument("hexlify", "argument 'data'", "contiguous buffer", args[0]);
533 : 0 : goto exit;
534 : : }
535 [ # # ]: 0 : if (!noptargs) {
536 : 0 : goto skip_optional_pos;
537 : : }
538 [ # # ]: 0 : if (args[1]) {
539 : 0 : sep = args[1];
540 [ # # ]: 0 : if (!--noptargs) {
541 : 0 : goto skip_optional_pos;
542 : : }
543 : : }
544 : 0 : bytes_per_sep = _PyLong_AsInt(args[2]);
545 [ # # # # ]: 0 : if (bytes_per_sep == -1 && PyErr_Occurred()) {
546 : 0 : goto exit;
547 : : }
548 : 0 : skip_optional_pos:
549 : 0 : return_value = binascii_hexlify_impl(module, &data, sep, bytes_per_sep);
550 : :
551 : 0 : exit:
552 : : /* Cleanup for data */
553 [ # # ]: 0 : if (data.obj) {
554 : 0 : PyBuffer_Release(&data);
555 : : }
556 : :
557 : 0 : 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 : 0 : binascii_a2b_hex(PyObject *module, PyObject *arg)
577 : : {
578 : 0 : PyObject *return_value = NULL;
579 : 0 : Py_buffer hexstr = {NULL, NULL};
580 : :
581 [ # # ]: 0 : if (!ascii_buffer_converter(arg, &hexstr)) {
582 : 0 : goto exit;
583 : : }
584 : 0 : return_value = binascii_a2b_hex_impl(module, &hexstr);
585 : :
586 : 0 : exit:
587 : : /* Cleanup for hexstr */
588 [ # # ]: 0 : if (hexstr.obj)
589 : 0 : PyBuffer_Release(&hexstr);
590 : :
591 : 0 : 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 : 0 : binascii_unhexlify(PyObject *module, PyObject *arg)
610 : : {
611 : 0 : PyObject *return_value = NULL;
612 : 0 : Py_buffer hexstr = {NULL, NULL};
613 : :
614 [ # # ]: 0 : if (!ascii_buffer_converter(arg, &hexstr)) {
615 : 0 : goto exit;
616 : : }
617 : 0 : return_value = binascii_unhexlify_impl(module, &hexstr);
618 : :
619 : 0 : exit:
620 : : /* Cleanup for hexstr */
621 [ # # ]: 0 : if (hexstr.obj)
622 : 0 : PyBuffer_Release(&hexstr);
623 : :
624 : 0 : 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 : 0 : binascii_a2b_qp(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
641 : : {
642 : 0 : 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 [ # # ]: 0 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
670 : 0 : Py_buffer data = {NULL, NULL};
671 : 0 : int header = 0;
672 : :
673 [ # # # # : 0 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
# # # # ]
674 [ # # ]: 0 : if (!args) {
675 : 0 : goto exit;
676 : : }
677 [ # # ]: 0 : if (!ascii_buffer_converter(args[0], &data)) {
678 : 0 : goto exit;
679 : : }
680 [ # # ]: 0 : if (!noptargs) {
681 : 0 : goto skip_optional_pos;
682 : : }
683 : 0 : header = PyObject_IsTrue(args[1]);
684 [ # # ]: 0 : if (header < 0) {
685 : 0 : goto exit;
686 : : }
687 : 0 : skip_optional_pos:
688 : 0 : return_value = binascii_a2b_qp_impl(module, &data, header);
689 : :
690 : 0 : exit:
691 : : /* Cleanup for data */
692 [ # # ]: 0 : if (data.obj)
693 : 0 : PyBuffer_Release(&data);
694 : :
695 : 0 : 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 : 0 : binascii_b2a_qp(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
717 : : {
718 : 0 : 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 [ # # ]: 0 : Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
746 : 0 : Py_buffer data = {NULL, NULL};
747 : 0 : int quotetabs = 0;
748 : 0 : int istext = 1;
749 : 0 : int header = 0;
750 : :
751 [ # # # # : 0 : args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 4, 0, argsbuf);
# # # # ]
752 [ # # ]: 0 : if (!args) {
753 : 0 : goto exit;
754 : : }
755 [ # # ]: 0 : if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
756 : 0 : goto exit;
757 : : }
758 [ # # ]: 0 : if (!PyBuffer_IsContiguous(&data, 'C')) {
759 : 0 : _PyArg_BadArgument("b2a_qp", "argument 'data'", "contiguous buffer", args[0]);
760 : 0 : goto exit;
761 : : }
762 [ # # ]: 0 : if (!noptargs) {
763 : 0 : goto skip_optional_pos;
764 : : }
765 [ # # ]: 0 : if (args[1]) {
766 : 0 : quotetabs = PyObject_IsTrue(args[1]);
767 [ # # ]: 0 : if (quotetabs < 0) {
768 : 0 : goto exit;
769 : : }
770 [ # # ]: 0 : if (!--noptargs) {
771 : 0 : goto skip_optional_pos;
772 : : }
773 : : }
774 [ # # ]: 0 : if (args[2]) {
775 : 0 : istext = PyObject_IsTrue(args[2]);
776 [ # # ]: 0 : if (istext < 0) {
777 : 0 : goto exit;
778 : : }
779 [ # # ]: 0 : if (!--noptargs) {
780 : 0 : goto skip_optional_pos;
781 : : }
782 : : }
783 : 0 : header = PyObject_IsTrue(args[3]);
784 [ # # ]: 0 : if (header < 0) {
785 : 0 : goto exit;
786 : : }
787 : 0 : skip_optional_pos:
788 : 0 : return_value = binascii_b2a_qp_impl(module, &data, quotetabs, istext, header);
789 : :
790 : 0 : exit:
791 : : /* Cleanup for data */
792 [ # # ]: 0 : if (data.obj) {
793 : 0 : PyBuffer_Release(&data);
794 : : }
795 : :
796 : 0 : return return_value;
797 : : }
798 : : /*[clinic end generated code: output=ab156917c9db79d2 input=a9049054013a1b77]*/
|