binascii.c.h 22 KB

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