changeset 743:b7e409c72ad8

base64: move optimized invalid check into a helper macro This moves the optimization into a single place allowing for reuse. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 29 May 2019 11:10:09 -0400
parents 337fad78b8ae
children 888e284aac25
files base64.c
diffstat 1 files changed, 11 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/base64.c	Wed May 29 10:58:19 2019 -0400
+++ b/base64.c	Wed May 29 11:10:09 2019 -0400
@@ -33,6 +33,16 @@
 	"0123456789-_";
 
 #define INV	0xff
+
+/*
+ * Check for any one of the four chars being INV.
+ *
+ * Optimization: Since no valid char translates to a value with with 0x40 or
+ * 0x80 set, the only way to get 0xff out is if one of the four values was
+ * 0xff - aka. INV.
+ */
+#define CHECK_INV(a, b, c, d)	(((a) | (b) | (c) | (d)) == INV)
+
 static const uint8_t b64_decode_table[256] = {
 	 INV,  INV,  INV,  INV,  INV,  INV,  INV,  INV, /* 0x00..0x07 */
 	 INV,  INV,  INV,  INV,  INV,  INV,  INV,  INV, /* 0x08..0x0f */
@@ -188,15 +198,7 @@
 		const uint8_t d = table[in[(i * 4) + 3]];
 		uint32_t v;
 
-		/*
-		 * If any one of the four chars in this group is INV, error
-		 * out.
-		 *
-		 * Optimization: Since no valid char translates to a value
-		 * with with 0x40 or 0x80 set, the only way to get 0xff out
-		 * is if one of the four values was 0xff - aka. INV.
-		 */
-		if ((a | b | c | d) == INV)
+		if (CHECK_INV(a, b, c, d))
 			return -1;
 
 		v = (a << 18) | (b << 12) | (c << 6) | d;