changeset 476:1ff22c1cb32e

Base64: fix invalid comparison because rlookup can return '=' ASCII value
author David Demelier <markand@malikania.fr>
date Tue, 10 Nov 2015 11:08:38 +0100
parents fb3158aca358
children 65b567c8de54 c7d83d2b462b
files C++/modules/Base64/Base64.h
diffstat 1 files changed, 10 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/C++/modules/Base64/Base64.h	Fri Nov 06 15:09:00 2015 +0100
+++ b/C++/modules/Base64/Base64.h	Tue Nov 10 11:08:38 2015 +0100
@@ -91,19 +91,22 @@
 		int count;
 
 		for (count = 0; count < 4 && input != end; ++count) {
-			inputbuf[count] = (*input == '=') ? '=' : rlookup(*input);
+			inputbuf[count] = (*input == '=') ? -1: rlookup(*input);
 			input++;
 		}
 
-		if (count != 4)
+		if (count != 4) {
 			throw std::invalid_argument("truncated string");
+		}
 
-		*output++ = (inputbuf[0] << 2 & 0xfc) | (inputbuf[1] >> 4 & 0x03);
+		*output++ = static_cast<unsigned char>(((inputbuf[0] << 2) & 0xfc) | ((inputbuf[1] >> 4) & 0x03));
 
-		if (inputbuf[2] != '=')
-			*output++ = (inputbuf[1] << 4 & 0xf0) | (inputbuf[2] >> 2 & 0x0f);
-		if (inputbuf[3] != '=')
-			*output++ = (inputbuf[2] << 6 & 0xc0) | (inputbuf[3] & 0x3f);
+		if (inputbuf[2] != -1) {
+			*output++ = static_cast<unsigned char>(((inputbuf[1] << 4) & 0xf0) | ((inputbuf[2] >> 2) & 0x0f));
+		}
+		if (inputbuf[3] != -1) {
+			*output++ = static_cast<unsigned char>(((inputbuf[2] << 6) & 0xc0) | (inputbuf[3] & 0x3f));
+		}
 	}
 
 	return output;