From 0f79bf321b8c9ee3b06fbcf6a7f464a1b2111b39 Mon Sep 17 00:00:00 2001
From: Alexey Stratulat <alexey.stratulat@demlabs.net>
Date: Fri, 5 Jul 2019 23:32:44 +0700
Subject: [PATCH] [*] The Base64 algorithm for the input accepts a PyByteObject
 object returns a tuple with an encoded value and long source data. The
 decryption function accepts this tuple and returns a PyBytesObject object.
 Exceptions do not arise, but redundant data appears.

---
 include/wrapping_base64.h | 11 ++++++++++-
 src/wrapping_base64.c     | 35 ++++++++++++++++++++---------------
 2 files changed, 30 insertions(+), 16 deletions(-)

diff --git a/include/wrapping_base64.h b/include/wrapping_base64.h
index 2fcedda2..4133504d 100644
--- a/include/wrapping_base64.h
+++ b/include/wrapping_base64.h
@@ -1,6 +1,15 @@
 #include "Python.h"
 #include "dap_enc_base64.h"
+#include "py_bytes_object_worker.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 PyObject *dap_encode_base64_py(PyObject *self, PyObject *args);
 
-PyObject *dap_decode_base64_py(PyObject *self, PyObject *args);
\ No newline at end of file
+PyObject *dap_decode_base64_py(PyObject *self, PyObject *args);
+
+#ifdef __cplusplus
+}
+#endif
\ No newline at end of file
diff --git a/src/wrapping_base64.c b/src/wrapping_base64.c
index 7447a64a..c2fe4765 100644
--- a/src/wrapping_base64.c
+++ b/src/wrapping_base64.c
@@ -1,22 +1,27 @@
 #include "wrapping_base64.h"
 
  PyObject *dap_encode_base64_py(PyObject *self, PyObject *args){
-    const char* data;
-    if (!PyArg_ParseTuple(args, "s", &data)){
-        return NULL;
-    }
-    char res[DAP_ENC_BASE64_ENCODE_SIZE(strlen(data))];
-    dap_enc_base64_encode(data, strlen(data),res, DAP_ENC_DATA_TYPE_B64);
-    return Py_BuildValue("s", res);
+     PyObject *in_data;
+     if (!PyArg_ParseTuple(args, "S", & in_data)){
+         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);
 }
 
  PyObject *dap_decode_base64_py(PyObject *self, PyObject *args){
-    const char* data;
-    int size_source;
-    if (!PyArg_ParseTuple(args, "s|i", &data, &size_source)){
-        return NULL;
-    }
-    char res[size_source];
-    dap_enc_base64_decode(data, strlen(data), res, DAP_ENC_DATA_TYPE_B64);
-    return Py_BuildValue("s", res);
+     PyObject *data;
+     int size_t_str;
+     if (!PyArg_ParseTuple(args, "S|i", &data, &size_t_str)) {
+         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);
 }
\ No newline at end of file
-- 
GitLab