Allow parsing base64 files

Redfish outputs base64 strings.  It would be useful if the CLI app could
read those strings in directly.

This commit breaks out a new method "header_valid" to allow tooling to
do an initial reading of a buffer to determine if it appears to be
correct before going further.  This allows the CLI app to attempt to
parse as a buffer, if that fails, attempt to parse as base64.

To support as many inputs as possible, this commit makes padding
optional.  It also allows a trailing \n as is present in many files.

Change-Id: I4fb759ecefc8ce1c757f1a9e7c4a2b2d220105d0
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/tests/base64_test.c b/tests/base64_test.c
index abc540c..6fc7825 100644
--- a/tests/base64_test.c
+++ b/tests/base64_test.c
@@ -5,23 +5,49 @@
 #include <assert.h>
 #include <string.h>
 
+const char *good_encode_outputs[] = {
+	"Zg==", "Zm8=", "Zm9v", "Zm9vYg==", "Zm9vYmE=", "Zm9vYmFy",
+};
+const char *good_encode_inputs[] = {
+	"f", "fo", "foo", "foob", "fooba", "foobar",
+};
+
 void test_base64_encode_good()
 {
 	int32_t encoded_len = 0;
-	const char *data = "f";
-	char *encoded = base64_encode((unsigned char *)data, strlen(data),
-				      &encoded_len);
-	assert(encoded_len == 4);
-	assert(memcmp(encoded, "Zg==", encoded_len) == 0);
-	free(encoded);
+	for (long unsigned int i = 0;
+	     i < sizeof(good_encode_inputs) / sizeof(good_encode_inputs[0]);
+	     i++) {
+		const char *data = good_encode_inputs[i];
+		char *encoded = base64_encode((unsigned char *)data,
+					      strlen(data), &encoded_len);
+		assert((size_t)encoded_len == strlen(good_encode_outputs[i]));
+		assert(memcmp(encoded, good_encode_outputs[i], encoded_len) ==
+		       0);
+		free(encoded);
+	}
 }
 
+const char *good_decode_inputs[] = {
+	"Zg==", "Zg", "Zm8=", "Zm8", "Zm9v",
+};
+const char *good_decode_outputs[] = {
+	"f", "f", "fo", "fo", "foo",
+};
+
 void test_base64_decode_good()
 {
-	int32_t decoded_len = 0;
-	const char *data = "Zg==";
-	UINT8 *decoded = base64_decode(data, strlen(data), &decoded_len);
-	assert(decoded_len == 1);
-	assert(decoded[0] == 'f');
-	free(decoded);
+	for (long unsigned int i = 0;
+	     i < sizeof(good_decode_inputs) / sizeof(good_decode_inputs[0]);
+	     i++) {
+		int32_t decoded_len = 0;
+		const char *data = good_decode_inputs[i];
+		UINT8 *decoded =
+			base64_decode(data, strlen(data), &decoded_len);
+		assert(decoded != NULL);
+		assert((size_t)decoded_len == strlen(good_decode_outputs[i]));
+		assert(memcmp(decoded, good_decode_outputs[i], decoded_len) ==
+		       0);
+		free(decoded);
+	}
 }