about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJoe DF <joedf@live.ca>2018-02-08 09:38:50 -0500
committerJoe DF <joedf@live.ca>2018-02-08 09:38:50 -0500
commitf2c2b2bb0d2aa0046784cfb708e3c918e8f24d4b (patch)
treec5431c61a7410c7168fe4c27818c435d1b480bcf
parent3569f2172a281a6342649dd56c009a3ebe09fd05 (diff)
downloadbase64-f2c2b2bb0d2aa0046784cfb708e3c918e8f24d4b.tar.gz
Issue #1 - Prevent potential segfault or "out of bounds"
Thanks to @arre525 for reporting this.
-rw-r--r--base64.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/base64.c b/base64.c
index 73546d1..1d6bd73 100644
--- a/base64.c
+++ b/base64.c
@@ -60,7 +60,7 @@ unsigned int b64_encode(const unsigned int* in, unsigned int in_len, unsigned ch
 	for (i=0;i<in_len;i++) {
 		s[j++]=*(in+i);
 		if (j==3) {
-			out[k+0] = b64_chr[ s[0]>>2 ];
+			out[k+0] = b64_chr[ (s[0]&255)>>2 ];
 			out[k+1] = b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ];
 			out[k+2] = b64_chr[ ((s[1]&0x0F)<<2)+((s[2]&0xC0)>>6) ];
 			out[k+3] = b64_chr[ s[2]&0x3F ];
@@ -71,7 +71,7 @@ unsigned int b64_encode(const unsigned int* in, unsigned int in_len, unsigned ch
 	if (j) {
 		if (j==1)
 			s[1] = 0;
-		out[k+0] = b64_chr[ s[0]>>2 ];
+		out[k+0] = b64_chr[ (s[0]&255)>>2 ];
 		out[k+1] = b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ];
 		if (j==2)
 			out[k+2] = b64_chr[ ((s[1]&0x0F)<<2) ];
@@ -93,7 +93,7 @@ unsigned int b64_decode(const unsigned char* in, unsigned int in_len, unsigned i
 	for (i=0;i<in_len;i++) {
 		s[j++]=b64_int(*(in+i));
 		if (j==4) {
-			out[k+0] = (s[0]<<2)+((s[1]&0x30)>>4);
+			out[k+0] = ((s[0]&255)<<2)+((s[1]&0x30)>>4);
 			if (s[2]!=64) {
 				out[k+1] = ((s[1]&0x0F)<<4)+((s[2]&0x3C)>>2);
 				if ((s[3]!=64)) {
@@ -126,7 +126,7 @@ unsigned int b64_encodef(char *InFile, char *OutFile) {
 		break;
 		s[j++]=c;
 		if (j==3) {
-			fputc(b64_chr[ s[0]>>2 ],pOutFile);
+			fputc(b64_chr[ (s[0]&255)>>2 ],pOutFile);
 			fputc(b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ],pOutFile);
 			fputc(b64_chr[ ((s[1]&0x0F)<<2)+((s[2]&0xC0)>>6) ],pOutFile);
 			fputc(b64_chr[ s[2]&0x3F ],pOutFile);
@@ -137,7 +137,7 @@ unsigned int b64_encodef(char *InFile, char *OutFile) {
 	if (j) {
 		if (j==1)
 			s[1] = 0;
-		fputc(b64_chr[ s[0]>>2 ],pOutFile);
+		fputc(b64_chr[ (s[0]&255)>>2 ],pOutFile);
 		fputc(b64_chr[ ((s[0]&0x03)<<4)+((s[1]&0xF0)>>4) ],pOutFile);
 		if (j==2)
 			fputc(b64_chr[ ((s[1]&0x0F)<<2) ],pOutFile);
@@ -168,7 +168,7 @@ unsigned int b64_decodef(char *InFile, char *OutFile) {
 		break;
 		s[j++]=b64_int(c);
 		if (j==4) {
-			fputc((s[0]<<2)+((s[1]&0x30)>>4),pOutFile);
+			fputc(((s[0]&255)<<2)+((s[1]&0x30)>>4),pOutFile);
 			if (s[2]!=64) {
 				fputc(((s[1]&0x0F)<<4)+((s[2]&0x3C)>>2),pOutFile);
 				if ((s[3]!=64)) {