Move to embedded base64

Base64 encode/decode is a relatively simple algorithm, and currently
libcper takes a dependency on libb64 for this.  libb64 does not have
methods for determining the encoded size or decoded size, and rely on
the user to provide the right buffer sizes, which libcper currently
approximates as 2X the input size (which is incorrect).

This commit removes the libb64 dependency entirely, and inlines a
libcper specific base64 encoder and decoder, using EDK2-allowed types.

The implementation itself is unique to libcper and makes the following
design decisions.
1. Malloc is performed within the base64_<> functions.  This reduces the
   number of malloc calls total, and removes the need for separately
   determining the output size.
2. Arguments are passed in by EDK2-types under the assumption that this
   will keep compatibility with EDK2 implementations.
3. Incremental parsing is not supported.  CPER records are expected to
   be algorithmically small, and buffered such that the entire value
   fits in memory.  This was already an assumption, but dropping the
   support for incremental encoding significantly reduces the amount of
   code to support it.  It could be added back in the future if needed.

Change-Id: Idb010db105067ea317dbee05c2663511ab3c6611
Signed-off-by: Ed Tanous <ed@tanous.net>
diff --git a/sections/cper-section-pcie.c b/sections/cper-section-pcie.c
index fe82b16..1c6534f 100644
--- a/sections/cper-section-pcie.c
+++ b/sections/cper-section-pcie.c
@@ -7,7 +7,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <json.h>
-#include "libbase64.h"
+#include "base64.h"
 #include "../edk/Cper.h"
 #include "../cper-utils.h"
 #include "cper-section-pcie.h"
@@ -103,38 +103,39 @@
 	//The PCIe capability structure provided here could either be PCIe 1.1 Capability Structure
 	//(36-byte, padded to 60 bytes) or PCIe 2.0 Capability Structure (60-byte). There does not seem
 	//to be a way to differentiate these, so this is left as a b64 dump.
-	char *encoded = malloc(2 * 60);
-	size_t encoded_len = 0;
-	if (!encoded) {
+	int32_t encoded_len = 0;
+
+	char *encoded = base64_encode((UINT8 *)pcie_error->Capability.PcieCap,
+				      60, &encoded_len);
+	if (encoded == NULL) {
 		printf("Failed to allocate encode output buffer. \n");
 	} else {
-		base64_encode((const char *)pcie_error->Capability.PcieCap, 60,
-			      encoded, &encoded_len, 0);
 		json_object *capability = json_object_new_object();
 		json_object_object_add(capability, "data",
 				       json_object_new_string_len(encoded,
 								  encoded_len));
 		free(encoded);
+
 		json_object_object_add(section_ir, "capabilityStructure",
 				       capability);
 	}
 
 	//AER information.
 	json_object *aer_capability_ir = json_object_new_object();
-	encoded = malloc(2 * 96);
 	encoded_len = 0;
-	if (!encoded) {
+
+	encoded = base64_encode((UINT8 *)pcie_error->AerInfo.PcieAer, 96,
+				&encoded_len);
+	if (encoded == NULL) {
 		printf("Failed to allocate encode output buffer. \n");
 	} else {
-		base64_encode((const char *)pcie_error->AerInfo.PcieAer, 96,
-			      encoded, &encoded_len, 0);
 		json_object_object_add(aer_capability_ir, "data",
 				       json_object_new_string_len(encoded,
 								  encoded_len));
 		free(encoded);
-		json_object_object_add(section_ir, "aerInfo",
-				       aer_capability_ir);
 	}
+	json_object_object_add(section_ir, "aerInfo", aer_capability_ir);
+
 	return section_ir;
 }
 
@@ -206,14 +207,15 @@
 	json_object *capability =
 		json_object_object_get(section, "capabilityStructure");
 	json_object *encoded = json_object_object_get(capability, "data");
-	char *decoded = malloc(json_object_get_string_len(encoded));
-	size_t decoded_len = 0;
-	if (!decoded) {
+
+	int32_t decoded_len = 0;
+
+	UINT8 *decoded = base64_decode(json_object_get_string(encoded),
+				       json_object_get_string_len(encoded),
+				       &decoded_len);
+	if (decoded == NULL) {
 		printf("Failed to allocate decode output buffer. \n");
 	} else {
-		base64_decode(json_object_get_string(encoded),
-			      json_object_get_string_len(encoded), decoded,
-			      &decoded_len, 0);
 		memcpy(section_cper->Capability.PcieCap, decoded, decoded_len);
 		free(decoded);
 	}
@@ -221,14 +223,15 @@
 	//AER capability structure.
 	json_object *aer_info = json_object_object_get(section, "aerInfo");
 	encoded = json_object_object_get(aer_info, "data");
-	decoded = malloc(json_object_get_string_len(encoded));
 	decoded_len = 0;
-	if (!decoded) {
+
+	decoded = base64_decode(json_object_get_string(encoded),
+				json_object_get_string_len(encoded),
+				&decoded_len);
+
+	if (decoded == NULL) {
 		printf("Failed to allocate decode output buffer. \n");
 	} else {
-		base64_decode(json_object_get_string(encoded),
-			      json_object_get_string_len(encoded), decoded,
-			      &decoded_len, 0);
 		memcpy(section_cper->AerInfo.PcieAer, decoded, decoded_len);
 		free(decoded);
 	}