libutil

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

AuthorJamozed <[email protected]>
Date2021-02-20 05:11:25
Commit32a9a7435816cb3b29f5ce27d61fc12ba6d3aaf7
Parent3d88ccbf4444aee961720d50ee4019d0bcb952ac

base64: Use switch statement for encode padding

Diffstat

M src/base64.c | 36 ++++++++++++++++++++++--------------
M src/base64.h | 2 +-

2 files changed, 23 insertions, 15 deletions

diff --git a/src/base64.c b/src/base64.c
index ff3b46d..2714040 100644
--- a/src/base64.c
+++ b/src/base64.c
@@ -1,4 +1,4 @@
-// base64.c, version 1.1.0
+// base64.c, version 1.1.1
 // Base64 source file for OMKOV lib
 // Copyright (C) 2021, Jakob Wakeling
 // All rights reserved.
@@ -35,6 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdlib.h>
+#include <string.h>
 
 static const uint8_t B64E[];
 static const uint8_t B64D[];
@@ -50,16 +51,18 @@ size_t b64encode(uint8_t *dst, uint8_t *src, size_t len) {
 		*o++ = B64E[i[2] & 0x3F]; i += 3;
 	}
 
-	if (len) {
+	switch (len) {
+	case 1: {
 		*o++ = B64E[i[0] >> 2];
-		
-		if (len == 1) {
-			*o++ = B64E[(i[0] & 0x03) << 4]; *o++ = '=';
-		}
-		else {
-			*o++ = B64E[((i[0] & 0x03) << 4) | (i[1] >> 4)];
-			*o++ = B64E[(i[1] & 0x0F) << 2];
-		} *o++ = '=';
+		*o++ = B64E[(i[0] & 0x03) << 4];
+		memset(o, '=', 2); o += 2; break;
+	}
+	case 2: {
+		*o++ = B64E[i[0] >> 2];
+		*o++ = B64E[((i[0] & 0x03) << 4) | (i[1] >> 4)];
+		*o++ = B64E[(i[1] & 0x0F) << 2];
+		*o++ = '='; break;
+	}
 	}
 
 	return o - dst;
@@ -90,16 +93,21 @@ size_t b64decode(uint8_t *dst, uint8_t *src, size_t len) {
 	return o - dst;
 }
 
-static const uint8_t B64E[] =
-	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+static const uint8_t B64E[] = {
+	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+	'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+	'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
+};
 
 static const uint8_t B64D[] = {
 	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
 	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
-	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 62, 63, 62, 62, 63,
+	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 62,  0,  0,  0, 63,
 	52, 53, 54, 55, 56, 57, 58, 59, 60, 61,  0,  0,  0,  0,  0,  0,
 	 0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
-	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  0,  0,  0,  0, 63,
+	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  0,  0,  0,  0,  0,
 	 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
 	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
 };
diff --git a/src/base64.h b/src/base64.h
index c56d726..008e600 100644
--- a/src/base64.h
+++ b/src/base64.h
@@ -1,4 +1,4 @@
-// base64.h, version 1.1.0
+// base64.h, version 1.1.1
 // Base64 header file for OMKOV lib
 // Copyright (C) 2021, Jakob Wakeling
 // All rights reserved.