libutil

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

AuthorJamozed <[email protected]>
Date2021-02-20 05:09:27
Commit3d88ccbf4444aee961720d50ee4019d0bcb952ac
Parent0f438d6bdc3b02a9da12d766819f9d98932dfa44

base32: Standardise formatting

Diffstat

M src/base32.c | 84 ++++++++++++++++++++++++++++++++++++++-----------------------------------------
M src/base32.h | 2 +-

2 files changed, 41 insertions, 45 deletions

diff --git a/src/base32.c b/src/base32.c
index 3a47101..beeb02f 100644
--- a/src/base32.c
+++ b/src/base32.c
@@ -1,4 +1,4 @@
-// base32.c, version 1.0.0
+// base32.c, version 1.0.1
 // Base32 source file for OMKOV lib
 // Copyright (C) 2021, Jakob Wakeling
 // All rights reserved.
@@ -54,41 +54,37 @@ size_t b32encode(uint8_t *dst, uint8_t *src, size_t len) {
 		*o++ = B32E[i[4] & 0x1F]; i += 5;
 	}
 
-	if (len) {
+	switch (len) {
+	case 1: {
+		*o++ = B32E[i[0] >> 3];
+		*o++ = B32E[(i[0] & 0x07) << 2];
+		memset(o, '=', 6); o += 6; break;
+	}
+	case 2: {
 		*o++ = B32E[i[0] >> 3];
-		
-		switch (len) {
-		case 1: {
-			*o++ = B32E[(i[0] & 0x07) << 2];
-			memset(o, '=', 6);
-			break;
-		}
-		case 2: {
-			*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
-			*o++ = B32E[(i[1] >> 1) & 0x1F];
-			*o++ = B32E[(i[1] & 0x01) << 4];
-			memset(o, '=', 4);
-			break;
-		}
-		case 3: {
-			*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
-			*o++ = B32E[(i[1] >> 1) & 0x1F];
-			*o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)];
-			*o++ = B32E[(i[2] & 0x0F) << 1];
-			memset(o, '=', 3);
-			break;
-		}
-		case 4: {
-			*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
-			*o++ = B32E[(i[1] >> 1) & 0x1F];
-			*o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)];
-			*o++ = B32E[((i[2] & 0x0F) << 1) | (i[3] >> 7)];
-			*o++ = B32E[(i[3] >> 2) & 0x1F];
-			*o++ = B32E[(i[3] & 0x03) << 3];
-			*o++ = '=';
-			break;
-		}
-		}
+		*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
+		*o++ = B32E[(i[1] >> 1) & 0x1F];
+		*o++ = B32E[(i[1] & 0x01) << 4];
+		memset(o, '=', 4); o += 4; break;
+	}
+	case 3: {
+		*o++ = B32E[i[0] >> 3];
+		*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
+		*o++ = B32E[(i[1] >> 1) & 0x1F];
+		*o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)];
+		*o++ = B32E[(i[2] & 0x0F) << 1];
+		memset(o, '=', 3); o += 3; break;
+	}
+	case 4: {
+		*o++ = B32E[i[0] >> 3];
+		*o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)];
+		*o++ = B32E[(i[1] >> 1) & 0x1F];
+		*o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)];
+		*o++ = B32E[((i[2] & 0x0F) << 1) | (i[3] >> 7)];
+		*o++ = B32E[(i[3] >> 2) & 0x1F];
+		*o++ = B32E[(i[3] & 0x03) << 3];
+		*o++ = '='; break;
+	}
 	}
 
 	return o - dst;
@@ -111,36 +107,36 @@ size_t b32decode(uint8_t *dst, uint8_t *src, size_t len) {
 	switch (len) {
 	case 2: {
 		*o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2);
-		*o++ = (B32D[i[1]] << 6);
-		break;
+		*o++ = (B32D[i[1]] << 6); break;
 	}
 	case 4: {
 		*o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2);
 		*o++ = (B32D[i[1]] << 6) | (B32D[i[2]] << 1) | (B32D[i[3]] >> 4);
-		*o++ = (B32D[i[3]] << 4);
-		break;
+		*o++ = (B32D[i[3]] << 4); break;
 	}
 	case 5: {
 		*o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2);
 		*o++ = (B32D[i[1]] << 6) | (B32D[i[2]] << 1) | (B32D[i[3]] >> 4);
 		*o++ = (B32D[i[3]] << 4) | (B32D[i[4]] >> 1);
-		*o++ = (B32D[i[4]] << 7);
-		break;
+		*o++ = (B32D[i[4]] << 7); break;
 	}
 	case 7: {
 		*o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2);
 		*o++ = (B32D[i[1]] << 6) | (B32D[i[2]] << 1) | (B32D[i[3]] >> 4);
 		*o++ = (B32D[i[3]] << 4) | (B32D[i[4]] >> 1);
 		*o++ = (B32D[i[4]] << 7) | (B32D[i[5]] << 2) | (B32D[i[6]] >> 3);
-		*o++ = (B32D[i[6]] << 5);
-		break;
+		*o++ = (B32D[i[6]] << 5); break;
 	}
 	}
 
 	return o - dst;
 }
 
-static const uint8_t B32E[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
+static const uint8_t B32E[] = {
+	'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',
+	'2', '3', '4', '5', '6', '7'
+};
 
 static const uint8_t B32D[] = {
 	 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
diff --git a/src/base32.h b/src/base32.h
index 5531b52..c4e3ef3 100644
--- a/src/base32.h
+++ b/src/base32.h
@@ -1,4 +1,4 @@
-// base32.h, version 1.0.0
+// base32.h, version 1.0.1
 // Base32 header file for OMKOV lib
 // Copyright (C) 2021, Jakob Wakeling
 // All rights reserved.