Formatting .c/.h files and fix memory leakage issues

Signed-off-by: John Chung <john.chung@arm.com>
Change-Id: Id8328f412c2724992d80c0b3f895c8f85bc4ae68
diff --git a/sections/cper-section-cxl-protocol.c b/sections/cper-section-cxl-protocol.c
index 7cb1bbe..277731a 100644
--- a/sections/cper-section-cxl-protocol.c
+++ b/sections/cper-section-cxl-protocol.c
@@ -6,16 +6,13 @@
  **/
 #include <stdio.h>
 #include <string.h>
-#include <json.h>
-#include "b64.h"
+#include "libbase64.h"
 #include "../edk/Cper.h"
 #include "../cper-utils.h"
 #include "cper-section-cxl-protocol.h"
 
 //Converts a single CXL protocol error CPER section into JSON IR.
-json_object *
-cper_section_cxl_protocol_to_ir(void *section,
-				EFI_ERROR_SECTION_DESCRIPTOR *descriptor)
+json_object *cper_section_cxl_protocol_to_ir(void *section)
 {
 	EFI_CXL_PROTOCOL_ERROR_DATA *cxl_protocol_error =
 		(EFI_CXL_PROTOCOL_ERROR_DATA *)section;
@@ -106,11 +103,20 @@
 		//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 = b64_encode(
-			cxl_protocol_error->CapabilityStructure.PcieCap, 60);
-		json_object_object_add(section_ir, "capabilityStructure",
-				       json_object_new_string(encoded));
-		free(encoded);
+		char *encoded = malloc(2 * 60);
+		size_t encoded_len = 0;
+		if (!encoded) {
+			printf("Failed to allocate encode output buffer. \n");
+		} else {
+			base64_encode((const char *)cxl_protocol_error
+					      ->CapabilityStructure.PcieCap,
+				      60, encoded, &encoded_len, 0);
+			json_object_object_add(section_ir,
+					       "capabilityStructure",
+					       json_object_new_string_len(
+						       encoded, encoded_len));
+			free(encoded);
+		}
 	}
 
 	//CXL DVSEC & error log length.
@@ -124,20 +130,36 @@
 	//CXL DVSEC
 	//For CXL 1.1 devices, this is the "CXL DVSEC For Flex Bus Device" structure as in CXL 1.1 spec.
 	//For CXL 1.1 host downstream ports, this is the "CXL DVSEC For Flex Bus Port" structure as in CXL 1.1 spec.
-	unsigned char *cur_pos = (unsigned char *)(cxl_protocol_error + 1);
-	char *encoded = b64_encode(cur_pos, cxl_protocol_error->CxlDvsecLength);
-	json_object_object_add(section_ir, "cxlDVSEC",
-			       json_object_new_string(encoded));
-	free(encoded);
+	const char *cur_pos = (const char *)(cxl_protocol_error + 1);
+	char *encoded = malloc(2 * cxl_protocol_error->CxlDvsecLength);
+	size_t encoded_len = 0;
+	if (!encoded) {
+		printf("Failed to allocate encode output buffer. \n");
+	} else {
+		base64_encode(cur_pos, cxl_protocol_error->CxlDvsecLength,
+			      encoded, &encoded_len, 0);
+		json_object_object_add(section_ir, "cxlDVSEC",
+				       json_object_new_string_len(encoded,
+								  encoded_len));
+
+		free(encoded);
+	}
 	cur_pos += cxl_protocol_error->CxlDvsecLength;
 
 	//CXL Error Log
 	//This is the "CXL RAS Capability Structure" as in CXL 1.1 spec.
-	encoded = b64_encode(cur_pos, cxl_protocol_error->CxlErrorLogLength);
-	json_object_object_add(section_ir, "cxlErrorLog",
-			       json_object_new_string(encoded));
-	free(encoded);
-
+	encoded = malloc(2 * cxl_protocol_error->CxlErrorLogLength);
+	encoded_len = 0;
+	if (!encoded) {
+		printf("Failed to allocate encode output buffer. \n");
+	} else {
+		base64_encode(cur_pos, cxl_protocol_error->CxlErrorLogLength,
+			      encoded, &encoded_len, 0);
+		json_object_object_add(section_ir, "cxlErrorLog",
+				       json_object_new_string_len(encoded,
+								  encoded_len));
+		free(encoded);
+	}
 	return section_ir;
 }
 
@@ -207,10 +229,18 @@
 
 		json_object *encoded =
 			json_object_object_get(section, "capabilityStructure");
-		char *decoded = b64_decode(json_object_get_string(encoded),
-					   json_object_get_string_len(encoded));
-		memcpy(section_cper->CapabilityStructure.PcieCap, decoded, 60);
-		free(decoded);
+		char *decoded = malloc(json_object_get_string_len(encoded));
+		size_t decoded_len = 0;
+		if (!decoded) {
+			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->CapabilityStructure.PcieCap,
+			       decoded, decoded_len);
+			free(decoded);
+		}
 	}
 
 	//DVSEC length & error log length.
@@ -225,19 +255,33 @@
 
 	//DVSEC out to stream.
 	json_object *encoded = json_object_object_get(section, "cxlDVSEC");
-	char *decoded = b64_decode(json_object_get_string(encoded),
-				   json_object_get_string_len(encoded));
-	fwrite(decoded, section_cper->CxlDvsecLength, 1, out);
-	fflush(out);
-	free(decoded);
+	char *decoded = malloc(json_object_get_string_len(encoded));
+	size_t decoded_len = 0;
+	if (!decoded) {
+		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);
+		fwrite(decoded, decoded_len, 1, out);
+		fflush(out);
+		free(decoded);
+	}
 
 	//Error log out to stream.
 	encoded = json_object_object_get(section, "cxlErrorLog");
-	decoded = b64_decode(json_object_get_string(encoded),
-			     json_object_get_string_len(encoded));
-	fwrite(decoded, section_cper->CxlErrorLogLength, 1, out);
-	fflush(out);
-	free(decoded);
+	decoded = malloc(json_object_get_string_len(encoded));
+	decoded_len = 0;
+	if (!decoded) {
+		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);
+		fwrite(decoded, decoded_len, 1, out);
+		fflush(out);
+		free(decoded);
+	}
 
 	free(section_cper);
-}
\ No newline at end of file
+}