base64: use more idiomatic comparison

Mon, 17 May 2021 13:43:17 +0200

author
David Demelier <markand@malikania.fr>
date
Mon, 17 May 2021 13:43:17 +0200
changeset 29
daeb2f6e6b22
parent 28
94c8fd9ab0ee
child 30
74316437d97e

base64: use more idiomatic comparison

base64.c file | annotate | diff | comparison | revisions
--- a/base64.c	Mon May 17 12:57:27 2021 +0200
+++ b/base64.c	Mon May 17 13:43:17 2021 +0200
@@ -139,7 +139,7 @@
 		srcsz = strlen(src);
 
 	while (srcsz && dstsz) {
-		signed char inputbuf[4] = { -1, -1, -1, -1 };
+		unsigned char inputbuf[4] = {0};
 		size_t parsed = 0, required;
 
 		for (; srcsz && parsed < 4; parsed++) {
@@ -156,9 +156,9 @@
 		if (parsed != 4)
 			goto eilseq;
 
-		if (inputbuf[3] != -1)
+		if (inputbuf[3])
 			required = 3;
-		else if (inputbuf[2] != -1)
+		else if (inputbuf[2])
 			required = 2;
 		else
 			required = 1;
@@ -169,18 +169,16 @@
 		*dst++ = ((inputbuf[0] << 2) & 0xfc) |
 		         ((inputbuf[1] >> 4) & 0x03);
 
-		if (inputbuf[2] != -1) {
+		if (inputbuf[2])
 			*dst++ = ((inputbuf[1] << 4) & 0xf0) |
 			         ((inputbuf[2] >> 2) & 0x0f);
-			required = 2;
-		} if (inputbuf[3] != -1) {
+		if (inputbuf[3]) {
 			/* "XY=Z" is not allowed. */
-			if (inputbuf[2] == -1)
+			if (inputbuf[2] == 0)
 				goto eilseq;
 
 			*dst++ = ((inputbuf[2] << 6) & 0xc0) |
 			          (inputbuf[3] & 0x3f);
-			required = 3;
 		}
 
 		nwritten += required;

mercurial