libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2021-02-05 04:52:29
Commitb8e17eee4619d3d32d9f2d0fa5817c1aaef96f69
Parent3fda51f0607d3ad97c1eb68740b4f06ff5fc8b22

base64: Return bytes output from encode or decode

Diffstat

M src/base64.c | 12 ++++++------
M src/base64.h | 8 +++-----

2 files changed, 9 insertions, 11 deletions

diff --git a/src/base64.c b/src/base64.c
index 19b6d0f..8446847 100644
--- a/src/base64.c
+++ b/src/base64.c
@@ -1,4 +1,4 @@
-// base64.c, version 1.0.0
+// base64.c, version 1.1.0
 // Base64 header file for OMKOV lib
 // Copyright (C) 2021, Jakob Wakeling
 // All rights reserved.
@@ -40,7 +40,7 @@ static const uint8_t B64E[];
 static const uint8_t B64D[];
 
 /* Encode Base64 */
-void b64encode(uint8_t *dst, uint8_t *src, size_t len) {
+size_t b64encode(uint8_t *dst, uint8_t *src, size_t len) {
 	register uint8_t *i = src, *o = dst;
 
 	for (; len >= 3; len -= 3) {
@@ -62,12 +62,12 @@ void b64encode(uint8_t *dst, uint8_t *src, size_t len) {
 		} *o++ = '=';
 	}
 
-	return;
+	return o - dst;
 }
 
 /* Decode Base64 */
-void b64decode(uint8_t *dst, uint8_t *src, size_t len) {
-	if (!len) { *src = 0; return; }
+size_t b64decode(uint8_t *dst, uint8_t *src, size_t len) {
+	if (!len) { return 0; }
 
 	register uint8_t *o = dst;
 
@@ -87,7 +87,7 @@ void b64decode(uint8_t *dst, uint8_t *src, size_t len) {
 		if (pad2) { n |= B64D[src[end + 2]] << 6; *o++ = n >> 8 & 0xFF; }
     }
 
-	return;
+	return o - dst;
 }
 
 static const uint8_t B64E[] =
diff --git a/src/base64.h b/src/base64.h
index 2717dce..c56d726 100644
--- a/src/base64.h
+++ b/src/base64.h
@@ -1,4 +1,4 @@
-// base64.h, version 1.0.0
+// base64.h, version 1.1.0
 // Base64 header file for OMKOV lib
 // Copyright (C) 2021, Jakob Wakeling
 // All rights reserved.
@@ -36,9 +36,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 #include <stdint.h>
 #include <stdlib.h>
 
-#define B64ELEN(x) ((x + 2) / 3 * 4)
-
-extern void b64encode(uint8_t *dst, uint8_t *src, size_t len);
-extern void b64decode(uint8_t *dst, uint8_t *src, size_t len);
+extern size_t b64encode(uint8_t *dst, uint8_t *src, size_t len);
+extern size_t b64decode(uint8_t *dst, uint8_t *src, size_t len);
 
 #endif // OMKOV_LIB_BASE64_H_5YQ80JRV