Add ability to use lib without json-c dependency

Adds 2 wrapper APIs to parse raw CPER & return char* for json
cperbuf_to_str_ir - parses a full CPER
cperbuf_single_section_to_str_ir - parses a single-section CPER

Change-Id: Ief018cc421497a8c366157a21e83ef60641e2646
Signed-off-by: Karthik Rajagopalan <krajagopalan@nvidia.com>
diff --git a/cper-parse-str.h b/cper-parse-str.h
new file mode 100644
index 0000000..268a4b7
--- /dev/null
+++ b/cper-parse-str.h
@@ -0,0 +1,16 @@
+#ifndef CPER_PARSE_STR_H
+#define CPER_PARSE_STR_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+char *cperbuf_to_str_ir(const unsigned char *cper, size_t size);
+char *cperbuf_single_section_to_str_ir(const unsigned char *cper_section,
+				       size_t size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/cper-parse.c b/cper-parse.c
index 4181a64..8c8722d 100644
--- a/cper-parse.c
+++ b/cper-parse.c
@@ -6,10 +6,12 @@
  **/
 
 #include <stdio.h>
+#include <string.h>
 #include <json.h>
 #include "base64.h"
 #include "edk/Cper.h"
 #include "cper-parse.h"
+#include "cper-parse-str.h"
 #include "cper-utils.h"
 #include "sections/cper-section.h"
 
@@ -81,6 +83,22 @@
 	return parent;
 }
 
+char *cper_to_str_ir(FILE *cper_file)
+{
+	json_object *jobj = cper_to_ir(cper_file);
+	char *str = jobj ? strdup(json_object_to_json_string(jobj)) : NULL;
+
+	json_object_put(jobj);
+	return str;
+}
+
+char *cperbuf_to_str_ir(const unsigned char *cper, size_t size)
+{
+	FILE *cper_file = fmemopen((void *)cper, size, "r");
+
+	return cper_file ? cper_to_str_ir(cper_file) : NULL;
+}
+
 //Converts a parsed CPER record header into intermediate JSON object format.
 json_object *cper_header_to_ir(EFI_COMMON_ERROR_RECORD_HEADER *header)
 {
@@ -385,3 +403,22 @@
 
 	return ir;
 }
+
+char *cper_single_section_to_str_ir(FILE *cper_section_file)
+{
+	json_object *jobj = cper_single_section_to_ir(cper_section_file);
+	char *str = jobj ? strdup(json_object_to_json_string(jobj)) : NULL;
+
+	json_object_put(jobj);
+	return str;
+}
+
+char *cperbuf_single_section_to_str_ir(const unsigned char *cper_section,
+				       size_t size)
+{
+	FILE *cper_section_file = fmemopen((void *)cper_section, size, "r");
+
+	return cper_section_file ?
+		       cper_single_section_to_str_ir(cper_section_file) :
+		       NULL;
+}
diff --git a/meson.build b/meson.build
index 1ef4da0..10c05fe 100644
--- a/meson.build
+++ b/meson.build
@@ -125,6 +125,7 @@
 )
 
 install_headers('cper-parse.h')
+install_headers('cper-parse-str.h')
 install_headers('cper-utils.h')
 install_headers('common-utils.h')
 install_headers('generator/cper-generate.h', subdir: 'generator')
@@ -159,4 +160,4 @@
 
 if get_option('tests').allowed()
     subdir('tests')
-endif
\ No newline at end of file
+endif