From 0e531dce5788ac30c75d6a7a941204904326c02c Mon Sep 17 00:00:00 2001 From: Alexey Stratulat <alexey.stratulat@demlabs.net> Date: Tue, 9 Jul 2019 23:10:36 +0700 Subject: [PATCH] emoved the transfer to the decoding function of the initial size of PyBytesObject. Added one more parameter to the wrapper of the encoding/decoding function, a numeric parameter, it takes a value from 1 to 2, which correspond to DAP_ENC_DATA_TYPE_B64 and DAP_ENC_DATA_TYPE_B64_URLSAFE from the dap_enc_data_type enumeration. And corrected the errors, now neither after nor during the execution of the Python script exceptions occur --- src/wrapping_base64.c | 28 ++++++++++++++++++---------- test/test.py | 6 +++--- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/wrapping_base64.c b/src/wrapping_base64.c index c2fe4765..df14bf84 100644 --- a/src/wrapping_base64.c +++ b/src/wrapping_base64.c @@ -2,26 +2,34 @@ PyObject *dap_encode_base64_py(PyObject *self, PyObject *args){ PyObject *in_data; - if (!PyArg_ParseTuple(args, "S", & in_data)){ + short int l_dap_enc_data_type; + if (!PyArg_ParseTuple(args, "S|h", &in_data, &l_dap_enc_data_type)){ + return NULL; + } + if (l_dap_enc_data_type < 1 || l_dap_enc_data_type > 2){ return NULL; } char* data = PyBytes_AsString(in_data); size_t size_t_str = strlen(data); char res[DAP_ENC_BASE64_ENCODE_SIZE(size_t_str)]; - dap_enc_base64_encode(data, size_t_str,res, DAP_ENC_DATA_TYPE_B64); - PyBytesObject *out_obj = (PyBytesObject *)PyBytes_FromFormat("%s",res); - return Py_BuildValue("Si", out_obj, size_t_str); + dap_enc_base64_encode(data, size_t_str,res, l_dap_enc_data_type); + PyBytesObject *out_obj = (PyBytesObject *)PyBytes_FromFormat("%s", res); + return Py_BuildValue("S", out_obj); } PyObject *dap_decode_base64_py(PyObject *self, PyObject *args){ PyObject *data; - int size_t_str; - if (!PyArg_ParseTuple(args, "S|i", &data, &size_t_str)) { + short int l_dap_enc_data_type=1; + if (!PyArg_ParseTuple(args, "S|h", &data, &l_dap_enc_data_type)) { + return NULL; + } + if (l_dap_enc_data_type < 1 || l_dap_enc_data_type > 2){ return NULL; } char *in_data = PyBytes_AsString(data); - char res[size_t_str]; - dap_enc_base64_decode(in_data, strlen(in_data), res, DAP_ENC_DATA_TYPE_B64); - PyObject *pyBytesObject = PyBytes_FromFormat("%s", res); - return Py_BuildValue("O", pyBytesObject); + char *res = NULL; + res = DAP_NEW_SIZE(void, DAP_ENC_BASE64_ENCODE_SIZE(strlen(in_data))); + dap_enc_base64_decode(in_data, strlen(in_data), res, l_dap_enc_data_type); + PyBytesObject *pyBytesObject = (PyBytesObject *)PyBytes_FromFormat("%s", res); + return Py_BuildValue("S", pyBytesObject); } \ No newline at end of file diff --git a/test/test.py b/test/test.py index 0449baa7..1221f48c 100644 --- a/test/test.py +++ b/test/test.py @@ -13,9 +13,9 @@ class TestLibdapCryptoPythonModule(unittest.TestCase): self.assertTrue(s == decrypt, "Encoding and decoded information using the base58 algorithm does not match the original") def test_b64(self): s = "LindyfekrngFHJFGR23356fer" - crypt = crypto.encodeBase64(s) - decrypt = crypto.decodeBase64(crypt, len(s)) - self.assertTrue(s == decrypt, "Encoding and decoded information using the base64 algorithm does not match the original") + crypt = crypto.encodeBase64(bytes(s, "utf-8"), 1) + decrypt = crypto.decodeBase64(crypt, 1) + self.assertTrue(bytes(s, "utf-8") == decrypt, "Encoding and decoded information using the base64 algorithm does not match the original") if __name__ == '__main__': unittest.main() -- GitLab