Browse Source

Return correct size from b64_decode.

The current implementation does not take into account padding (and yet it does
actually depend on padding), or skipped whitespace/gibberish characters.

The actual size can be computed by subtracting the padding and gibberish bytes,
but in this case it's easier to just return the actual number of bytes written.
rude 9 years ago
parent
commit
f66011de2f
1 changed files with 7 additions and 3 deletions
  1. 7 3
      src/common/b64.cpp

+ 7 - 3
src/common/b64.cpp

@@ -34,9 +34,11 @@ static void b64_decode_block(char in[4], char out[3])
 
 char *b64_decode(const char *src, int slen, int &size)
 {
-	size = (slen / 4) * 3;
+	// Actual output may be smaller due to padding and/or whitespace in the
+	// base64-encoded string.
+	int max_size = (slen / 4) * 3;
 
-	char *dst = new char[size];
+	char *dst = new char[max_size];
 	char *d = dst;
 
 	char in[4] = {0}, out[3], v;
@@ -70,10 +72,12 @@ char *b64_decode(const char *src, int slen, int &size)
 		{
 			b64_decode_block(in, out);
 			for (i = 0; i < len - 1; i++)
-				 *(d++) = out[i];
+				*(d++) = out[i];
 		}
 	}
 
+	size = int(d - dst);
+
 	return dst;
 }