From cc53c9b1124b464556ba29a4a91a33628b3efe14 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Tue, 17 Oct 2023 00:04:41 +0200
Subject: Fix some bugs in the base64 implementation.

---
 plugins/encodings/base64.c                |  6 ++++--
 plugins/encodings/python/Makefile.am      |  3 ++-
 plugins/encodings/python/rost/Makefile.am |  3 ++-
 tests/plugins/encodings/all.py            | 23 +++++++++++++++++++++++
 4 files changed, 31 insertions(+), 4 deletions(-)
 create mode 100644 tests/plugins/encodings/all.py

diff --git a/plugins/encodings/base64.c b/plugins/encodings/base64.c
index 61b88e0..c749a87 100644
--- a/plugins/encodings/base64.c
+++ b/plugins/encodings/base64.c
@@ -54,8 +54,8 @@ bool _base64_encode(const sized_binary_t *input, sized_string_t *output, const s
 
     output->len = input->len * 4 / 3;
 
-    if (input->len % 3 != 0)
-        output->len++;
+    if (output->len % 4 != 0)
+        output->len += (4 - output->len % 4);
 
     output->data = malloc((output->len + 1) * sizeof(bin_t));
 
@@ -78,6 +78,8 @@ bool _base64_encode(const sized_binary_t *input, sized_string_t *output, const s
             *iter++ = alpha[src[i + 2] & 0x3f];
 
         }
+    else
+        i = 0;
 
     /* Bourrage final ? */
 
diff --git a/plugins/encodings/python/Makefile.am b/plugins/encodings/python/Makefile.am
index f999f9f..523a6f4 100644
--- a/plugins/encodings/python/Makefile.am
+++ b/plugins/encodings/python/Makefile.am
@@ -8,7 +8,8 @@ libencodingspython_la_SOURCES =				\
 libencodingspython_la_LIBADD =	 			\
         rost/libencodingspythonrost.la
 
-libencodingspython_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \
+libencodingspython_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBXML_CFLAGS) \
+	$(LIBPYTHON_INTERPRETER_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \
 	-I$(top_srcdir)/src -DNO_IMPORT_PYGOBJECT
 
 
diff --git a/plugins/encodings/python/rost/Makefile.am b/plugins/encodings/python/rost/Makefile.am
index a5a2969..531fb26 100644
--- a/plugins/encodings/python/rost/Makefile.am
+++ b/plugins/encodings/python/rost/Makefile.am
@@ -5,7 +5,8 @@ libencodingspythonrost_la_SOURCES = 		\
 	base64.h base64.c						\
 	module.h module.c
 
-libencodingspythonrost_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBXML_CFLAGS) $(LIBPYTHON_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \
+libencodingspythonrost_la_CFLAGS = $(TOOLKIT_CFLAGS) $(LIBXML_CFLAGS) \
+	$(LIBPYTHON_INTERPRETER_CFLAGS) $(LIBPYGOBJECT_CFLAGS) \
 	-I$(top_srcdir)/src -DNO_IMPORT_PYGOBJECT
 
 
diff --git a/tests/plugins/encodings/all.py b/tests/plugins/encodings/all.py
new file mode 100644
index 0000000..a856ccb
--- /dev/null
+++ b/tests/plugins/encodings/all.py
@@ -0,0 +1,23 @@
+
+from chrysacase import ChrysalideTestCase
+from pychrysalide.plugins import encodings
+
+import base64
+
+
+class TestEncodingsModule(ChrysalideTestCase):
+    """TestCase for encodings plugin."""
+
+    def testBase64Encoding(self):
+        """Validate the base64 implementation."""
+
+        text = '0123456789abcdef'
+
+        for i in range(len(text) + 1):
+
+            src = text[:i].encode('ascii')
+
+            encoded = encodings.base64_encode(src)
+            ref = base64.b64encode(src)
+
+            self.assertEqual(encoded, ref)
-- 
cgit v0.11.2-87-g4458