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