|
| 1 | +#define PY_SSIZE_T_CLEAN |
| 2 | +#include <Python.h> |
| 3 | +#include "librt_base64.h" |
| 4 | +#include "pythoncapi_compat.h" |
| 5 | + |
| 6 | +#ifdef MYPYC_EXPERIMENTAL |
| 7 | + |
| 8 | +// b64encode_internal below is adapted from the CPython 3.14.0 binascii module |
| 9 | + |
| 10 | +static const unsigned char table_b2a_base64[] = |
| 11 | +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 12 | + |
| 13 | +#define BASE64_PAD '=' |
| 14 | + |
| 15 | +/* Max binary chunk size; limited only by available memory */ |
| 16 | +#define BASE64_MAXBIN ((PY_SSIZE_T_MAX - 3) / 2) |
| 17 | + |
| 18 | +static PyObject * |
| 19 | +b64encode_internal(PyObject *obj) { |
| 20 | + unsigned char *ascii_data; |
| 21 | + const unsigned char *bin_data; |
| 22 | + int leftbits = 0; |
| 23 | + unsigned char this_ch; |
| 24 | + unsigned int leftchar = 0; |
| 25 | + Py_ssize_t bin_len, out_len; |
| 26 | + PyBytesWriter *writer; |
| 27 | + int newline = 0; // TODO |
| 28 | + |
| 29 | + if (!PyBytes_Check(obj)) { |
| 30 | + PyErr_SetString(PyExc_TypeError, "base64() expects a bytes object"); |
| 31 | + return NULL; |
| 32 | + } |
| 33 | + |
| 34 | + bin_data = (const unsigned char *)PyBytes_AS_STRING(obj); |
| 35 | + bin_len = PyBytes_GET_SIZE(obj); |
| 36 | + |
| 37 | + assert(bin_len >= 0); |
| 38 | + |
| 39 | + if ( bin_len > BASE64_MAXBIN ) { |
| 40 | + PyErr_SetString(PyExc_ValueError, "Too much data for base64 line"); |
| 41 | + return NULL; |
| 42 | + } |
| 43 | + |
| 44 | + /* We're lazy and allocate too much (fixed up later). |
| 45 | + "+2" leaves room for up to two pad characters. |
| 46 | + Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */ |
| 47 | + out_len = bin_len*2 + 2; |
| 48 | + if (newline) |
| 49 | + out_len++; |
| 50 | + writer = PyBytesWriter_Create(out_len); |
| 51 | + ascii_data = PyBytesWriter_GetData(writer); |
| 52 | + if (writer == NULL) |
| 53 | + return NULL; |
| 54 | + |
| 55 | + for( ; bin_len > 0 ; bin_len--, bin_data++ ) { |
| 56 | + /* Shift the data into our buffer */ |
| 57 | + leftchar = (leftchar << 8) | *bin_data; |
| 58 | + leftbits += 8; |
| 59 | + |
| 60 | + /* See if there are 6-bit groups ready */ |
| 61 | + while ( leftbits >= 6 ) { |
| 62 | + this_ch = (leftchar >> (leftbits-6)) & 0x3f; |
| 63 | + leftbits -= 6; |
| 64 | + *ascii_data++ = table_b2a_base64[this_ch]; |
| 65 | + } |
| 66 | + } |
| 67 | + if ( leftbits == 2 ) { |
| 68 | + *ascii_data++ = table_b2a_base64[(leftchar&3) << 4]; |
| 69 | + *ascii_data++ = BASE64_PAD; |
| 70 | + *ascii_data++ = BASE64_PAD; |
| 71 | + } else if ( leftbits == 4 ) { |
| 72 | + *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2]; |
| 73 | + *ascii_data++ = BASE64_PAD; |
| 74 | + } |
| 75 | + if (newline) |
| 76 | + *ascii_data++ = '\n'; /* Append a courtesy newline */ |
| 77 | + |
| 78 | + return PyBytesWriter_FinishWithSize(writer, ascii_data - (unsigned char *)PyBytesWriter_GetData(writer)); |
| 79 | +} |
| 80 | + |
| 81 | +static PyObject* |
| 82 | +b64encode(PyObject *self, PyObject *const *args, size_t nargs) { |
| 83 | + if (nargs != 1) { |
| 84 | + PyErr_SetString(PyExc_TypeError, "b64encode() takes exactly one argument"); |
| 85 | + return 0; |
| 86 | + } |
| 87 | + return b64encode_internal(args[0]); |
| 88 | +} |
| 89 | + |
| 90 | +#endif |
| 91 | + |
| 92 | +static PyMethodDef librt_base64_module_methods[] = { |
| 93 | +#ifdef MYPYC_EXPERIMENTAL |
| 94 | + {"b64encode", (PyCFunction)b64encode, METH_FASTCALL, PyDoc_STR("Encode bytes-like object using Base64.")}, |
| 95 | +#endif |
| 96 | + {NULL, NULL, 0, NULL} |
| 97 | +}; |
| 98 | + |
| 99 | +static int |
| 100 | +base64_abi_version(void) { |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +static int |
| 105 | +base64_api_version(void) { |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | +static int |
| 110 | +librt_base64_module_exec(PyObject *m) |
| 111 | +{ |
| 112 | +#ifdef MYPYC_EXPERIMENTAL |
| 113 | + // Export mypy internal C API, be careful with the order! |
| 114 | + static void *base64_api[LIBRT_BASE64_API_LEN] = { |
| 115 | + (void *)base64_abi_version, |
| 116 | + (void *)base64_api_version, |
| 117 | + (void *)b64encode_internal, |
| 118 | + }; |
| 119 | + PyObject *c_api_object = PyCapsule_New((void *)base64_api, "librt.base64._C_API", NULL); |
| 120 | + if (PyModule_Add(m, "_C_API", c_api_object) < 0) { |
| 121 | + return -1; |
| 122 | + } |
| 123 | +#endif |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | +static PyModuleDef_Slot librt_base64_module_slots[] = { |
| 128 | + {Py_mod_exec, librt_base64_module_exec}, |
| 129 | +#ifdef Py_MOD_GIL_NOT_USED |
| 130 | + {Py_mod_gil, Py_MOD_GIL_NOT_USED}, |
| 131 | +#endif |
| 132 | + {0, NULL} |
| 133 | +}; |
| 134 | + |
| 135 | +static PyModuleDef librt_base64_module = { |
| 136 | + .m_base = PyModuleDef_HEAD_INIT, |
| 137 | + .m_name = "base64", |
| 138 | + .m_doc = "base64 encoding and decoding optimized for mypyc", |
| 139 | + .m_size = 0, |
| 140 | + .m_methods = librt_base64_module_methods, |
| 141 | + .m_slots = librt_base64_module_slots, |
| 142 | +}; |
| 143 | + |
| 144 | +PyMODINIT_FUNC |
| 145 | +PyInit_base64(void) |
| 146 | +{ |
| 147 | + return PyModuleDef_Init(&librt_base64_module); |
| 148 | +} |
0 commit comments