summaryrefslogtreecommitdiffstats
path: root/RedfishPkg
diff options
context:
space:
mode:
authorAbner Chang <abner.chang@hpe.com>2020-12-04 23:59:59 +0800
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>2021-01-09 03:08:51 +0000
commitea830b96fd271ab141a2e9d375fb13ba2f845d16 (patch)
tree600018bfd6a51b8cbcd176b40c99f38e8797197b /RedfishPkg
parent6e9233f968735219b2038c5dd23a46be2c021807 (diff)
downloadedk2-ea830b96fd271ab141a2e9d375fb13ba2f845d16.tar.gz
edk2-ea830b96fd271ab141a2e9d375fb13ba2f845d16.tar.bz2
edk2-ea830b96fd271ab141a2e9d375fb13ba2f845d16.zip
RedfishPkg/library: EDK2 port of jansson library
edk2 JsonLib which is the edk2 port of open source jansson library. (https://github.com/akheron/jansson) jansson library is the open source project to manipulate JSON data structure. Signed-off-by: Abner Chang <abner.chang@hpe.com> Cc: Leif Lindholm <leif@nuviainc.com> Cc: Nickle Wang <nickle.wang@hpe.com> Cc: Peter O'Hanley <peter.ohanley@hpe.com> Reviewed-by: Nickle Wang <nickle.wang@hpe.com> Acked-by: Leif Lindholm <leif@nuviainc.com> Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com>
Diffstat (limited to 'RedfishPkg')
-rw-r--r--RedfishPkg/Include/Library/JsonLib.h779
-rw-r--r--RedfishPkg/Library/JsonLib/JsonLib.c957
-rw-r--r--RedfishPkg/Library/JsonLib/JsonLib.inf86
-rw-r--r--RedfishPkg/Library/JsonLib/Readme.rst35
-rw-r--r--RedfishPkg/Library/JsonLib/jansson_config.h41
-rw-r--r--RedfishPkg/Library/JsonLib/jansson_private_config.h19
-rw-r--r--RedfishPkg/Library/JsonLib/load.c1111
-rw-r--r--RedfishPkg/RedfishPkg.ci.yaml8
-rw-r--r--RedfishPkg/RedfishPkg.dec12
9 files changed, 3047 insertions, 1 deletions
diff --git a/RedfishPkg/Include/Library/JsonLib.h b/RedfishPkg/Include/Library/JsonLib.h
new file mode 100644
index 0000000000..3c10f67d27
--- /dev/null
+++ b/RedfishPkg/Include/Library/JsonLib.h
@@ -0,0 +1,779 @@
+/** @file
+ APIs for JSON operations.
+
+ Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+#ifndef JSON_LIB_H_
+#define JSON_LIB_H_
+
+typedef VOID* EDKII_JSON_VALUE;
+typedef VOID* EDKII_JSON_ARRAY;
+typedef VOID* EDKII_JSON_OBJECT;
+
+///
+/// Map to json_int_t in jansson.h
+///
+typedef INT64 EDKII_JSON_INT_T; // #JSON_INTEGER_IS_LONG_LONG is set to 1
+ // in jansson_Config.h
+
+///
+/// Map to the definitions in jansson.h
+/// See below URI for the JSON encoding flags reference.
+/// https://jansson.readthedocs.io/en/2.13/apiref.html#encoding
+///
+#define EDKII_JSON_MAX_INDENT 0x1F
+#define EDKII_JSON_INDENT(n) ((n) & EDKII_JSON_MAX_INDENT)
+
+#define EDKII_JSON_COMPACT 0x20
+#define EDKII_JSON_ENSURE_ASCII 0x40
+#define EDKII_JSON_SORT_KEYS 0x80
+#define EDKII_JSON_PRESERVE_ORDER 0x100
+#define EDKII_JSON_ENCODE_ANY 0x200
+#define EDKII_JSON_ESCAPE_SLASH 0x400
+#define EDKII_JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11)
+#define EDKII_JSON_EMBED 0x10000
+
+///
+/// Map to the definitions in jansson.h
+/// See below URI for the JSON decoding flags reference.
+/// https://jansson.readthedocs.io/en/2.13/apiref.html?highlight=json_loadb#decoding
+///
+#define EDKII_JSON_REJECT_DUPLICATES 0x1
+#define EDKII_JSON_DISABLE_EOF_CHECK 0x2
+#define EDKII_JSON_DECODE_ANY 0x4
+#define EDKII_JSON_DECODE_INT_AS_REAL 0x8
+#define EDKII_JSON_ALLOW_NUL 0x10
+
+#define EDKII_JSON_ARRAY_FOREACH(Array, Index, Value) \
+ for(Index = 0; \
+ Index < JsonArrayCount(Array) && (Value = JsonArrayGetValue(Array, Index)); \
+ Index++)
+
+///
+/// Map to the json_error_t in jansson.h
+///
+#define EDKII_JSON_ERROR_TEXT_LENGTH 160
+#define EDKII_JSON_ERROR_SOURCE_LENGTH 80
+typedef struct {
+ INTN Line;
+ INTN Column;
+ INTN Position;
+ CHAR8 Source [EDKII_JSON_ERROR_SOURCE_LENGTH];
+ CHAR8 Text [EDKII_JSON_ERROR_TEXT_LENGTH];
+} EDKII_JSON_ERROR;
+
+///
+/// Map to the json_type in jansson.h
+///
+typedef enum {
+ EdkiiJsonTypeObject,
+ EdkiiJsonTypeArray,
+ EdkiiJsonTypeString,
+ EdkiiJsonTypeInteger,
+ EdkiiJsonTypeReal,
+ EdkiiJsonTypeTrue,
+ EdkiiJsonTypeFalse,
+ EdkiiJsonTypeNull
+} EDKII_JSON_TYPE;
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON array,
+ or NULL on error. Initially, the array is empty.
+
+ The reference count of this value will be set to 1, and caller needs to cleanup the
+ value by calling JsonValueFree().
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @retval The created JSON value which contains a JSON array or NULL if intial a JSON array
+ is failed.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitArray (
+ VOID
+ );
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON object,
+ or NULL on error. Initially, the object is empty.
+
+ The reference count of this value will be set to 1, and caller needs to cleanup the
+ value by calling JsonValueFree().
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @retval The created JSON value which contains a JSON object or NULL if intial a JSON object
+ is failed.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitObject (
+ VOID
+ );
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON string,
+ or NULL on error.
+
+ The input string must be NULL terminated Ascii format, non-Ascii characters will
+ be processed as an error. Unicode characters can also be represented by Ascii string
+ as the format: \u + 4 hexadecimal digits, like \u3E5A, or \u003F.
+
+ The reference count of this value will be set to 1, and caller needs to cleanup the
+ value by calling JsonValueFree().
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] String The Ascii string to initialize to JSON value
+
+ @retval The created JSON value which contains a JSON string or NULL. Select a
+ Getter API for a specific encoding format.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitAsciiString (
+ IN CONST CHAR8 *String
+ );
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON string,
+ or NULL on error.
+
+ The input must be a NULL terminated UCS2 format Unicode string.
+
+ The reference count of this value will be set to 1, and caller needs to cleanup the
+ value by calling JsonValueFree().
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] String The Unicode string to initialize to JSON value
+
+ @retval The created JSON value which contains a JSON string or NULL. Select a
+ Getter API for a specific encoding format.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitUnicodeString (
+ IN CHAR16 *String
+ );
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON integer,
+ or NULL on error.
+
+ The reference count of this value will be set to 1, and caller needs to cleanup the
+ value by calling JsonValueFree().
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] Value The integer to initialize to JSON value
+
+ @retval The created JSON value which contains a JSON number or NULL.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitNumber (
+ IN INT64 Value
+ );
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON boolean,
+ or NULL on error.
+
+ Boolean JSON value is kept as static value, and no need to do any cleanup work.
+
+ @param[in] Value The boolean value to initialize.
+
+ @retval The created JSON value which contains a JSON boolean or NULL.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitBoolean (
+ IN BOOLEAN Value
+ );
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON NULL,
+ or NULL on error.
+
+ NULL JSON value is kept as static value, and no need to do any cleanup work.
+
+ @retval The created NULL JSON value.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitNull (
+ VOID
+ );
+
+/**
+ The function is used to decrease the reference count of a JSON value by one, and once
+ this reference count drops to zero, the value is destroyed and it can no longer be used.
+ If this destroyed value is object type or array type, reference counts for all containing
+ JSON values will be decreased by 1. Boolean JSON value and NULL JSON value won't be destroyed
+ since they are static values kept in memory.
+
+ Reference Count Strategy: BaseJsonLib uses this strategy to track whether a value is still
+ in use or not. When a value is created, it's reference count is set to 1. If a reference to a
+ value is kept for use, its reference count is incremented, and when the value is no longer
+ needed, the reference count is decremented. When the reference count drops to zero, there are
+ no references left, and the value can be destroyed.
+
+ The given JSON value maybe NULL and not causing any problem. Just output the debug message
+ to inform caller the NULL value is passed in.
+
+ @param[in] Json The JSON value to be freed. json_decref may return without any
+ changes if Json is NULL.
+
+**/
+VOID
+EFIAPI
+JsonValueFree (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to create a fresh copy of a JSON value, and all child values are deep
+ copied in a recursive fashion. It should be called when this JSON value might be modified
+ in later use, but the original still wants to be used in somewhere else.
+
+ Reference counts of the returned root JSON value and all child values will be set to 1, and
+ caller needs to cleanup the root value by calling JsonValueFree().
+
+ * Note: Since this function performs a copy from bottom to up, too many calls may cause some
+ performance issues, user should avoid unnecessary calls to this function unless it is really
+ needed.
+
+ @param[in] Json The JSON value to be cloned.
+
+ @retval Return the cloned JSON value, or NULL on error.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueClone (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to return if the provided JSON value contains a JSON array.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value contains a JSON array.
+ @retval FALSE The JSON value doesn't contain a JSON array.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsArray (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to return if the provided JSON value contains a JSON object.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value contains a JSON object.
+ @retval FALSE The JSON value doesn't contain a JSON object.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsObject (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to return if the provided JSON Value contains a string, Ascii or
+ Unicode format is not differentiated.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value contains a JSON string.
+ @retval FALSE The JSON value doesn't contain a JSON string.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsString (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to return if the provided JSON value contains a JSON number.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value is contains JSON number.
+ @retval FALSE The JSON value doesn't contain a JSON number.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsNumber (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to return if the provided JSON value contains a JSON boolean.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value contains a JSON boolean.
+ @retval FALSE The JSON value doesn't contain a JSON boolean.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsBoolean (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to return if the provided JSON value contains a JSON NULL.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value contains a JSON NULL.
+ @retval FALSE The JSON value doesn't contain a JSON NULL.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsNull (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to retrieve the associated array in an array type JSON value.
+
+ Any changes to the returned array will impact the original JSON value.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated array in JSON value or NULL.
+
+**/
+EDKII_JSON_ARRAY
+EFIAPI
+JsonValueGetArray (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to retrieve the associated object in an object type JSON value.
+
+ Any changes to the returned object will impact the original JSON value.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated object in JSON value or NULL.
+
+**/
+EDKII_JSON_OBJECT
+EFIAPI
+JsonValueGetObject (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to retrieve the associated Ascii string in a string type JSON value.
+
+ Any changes to the returned string will impact the original JSON value.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated Ascii string in JSON value or NULL.
+
+**/
+CONST CHAR8 *
+EFIAPI
+JsonValueGetAsciiString (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to retrieve the associated Unicode string in a string type JSON value.
+
+ Caller can do any changes to the returned string without any impact to the original JSON
+ value, and caller needs to free the returned string using FreePool().
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated Unicode string in JSON value or NULL.
+
+**/
+CHAR16*
+EFIAPI
+JsonValueGetUnicodeString (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to retrieve the associated integer in a number type JSON value.
+
+ The input JSON value should not be NULL or contain no JSON number, otherwise it will
+ ASSERT() and return 0.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated number in JSON value.
+
+**/
+INT64
+EFIAPI
+JsonValueGetNumber (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to retrieve the associated boolean in a boolean type JSON value.
+
+ The input JSON value should not be NULL or contain no JSON boolean, otherwise it will
+ ASSERT() and return FALSE.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated value of JSON boolean.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueGetBoolean (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to retrieve the associated string in a string type JSON value.
+
+ Any changes to the returned string will impact the original JSON value.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated Ascii string in JSON value or NULL on errors.
+
+**/
+CONST CHAR8*
+EFIAPI
+JsonValueGetString (
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to get the number of elements in a JSON object, or 0 if it is NULL or
+ not a JSON object.
+
+ @param[in] JsonObject The provided JSON object.
+
+ @retval Return the number of elements in this JSON object or 0.
+
+**/
+UINTN
+EFIAPI
+JsonObjectSize (
+ IN EDKII_JSON_OBJECT JsonObject
+ );
+
+/**
+ The function is used to enumerate all keys in a JSON object.
+
+ Caller should be responsible to free the returned key array reference using
+ FreePool(). But contained keys are read only and must not be modified or freed.
+
+ @param[in] JsonObj The provided JSON object for enumeration.
+ @param[out] KeyCount The count of keys in this JSON object.
+
+ @retval Return an array of the enumerated keys in this JSON object or NULL if
+ JsonObj is not an JSON object, key count is zero or on other errors.
+
+**/
+CHAR8**
+JsonObjectGetKeys (
+ IN EDKII_JSON_OBJECT JsonObj,
+ OUT UINTN *KeyCount
+ );
+
+/**
+ The function is used to get a JSON value corresponding to the input key from a JSON object.
+
+ It only returns a reference to this value and any changes on this value will impact the
+ original JSON object. If that is not expected, please call JsonValueClone() to clone it to
+ use.
+
+ Input key must be a valid NULL terminated UTF8 encoded string. NULL will be returned when
+ Key-Value is not found in this JSON object.
+
+ @param[in] JsonObj The provided JSON object.
+ @param[in] Key The key of the JSON value to be retrieved.
+
+ @retval Return the corresponding JSON value to key, or NULL on error.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonObjectGetValue (
+ IN CONST EDKII_JSON_OBJECT JsonObj,
+ IN CONST CHAR8 *Key
+ );
+
+/**
+ The function is used to set a JSON value corresponding to the input key from a JSON object,
+ and the reference count of this value will be increased by 1.
+
+ Input key must be a valid NULL terminated UTF8 encoded string. If there already is a value for
+ this key, this key will be assigned to the new JSON value. The old JSON value will be removed
+ from this object and thus its' reference count will be decreased by 1.
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] JsonObj The provided JSON object.
+ @param[in] Key The key of the JSON value to be set.
+ @param[in] Json The JSON value to set to this JSON object mapped by key.
+
+ @retval EFI_ABORTED Some error occur and operation aborted.
+ @retval EFI_SUCCESS The JSON value has been set to this JSON object.
+
+**/
+EFI_STATUS
+EFIAPI
+JsonObjectSetValue (
+ IN EDKII_JSON_OBJECT JsonObj,
+ IN CONST CHAR8 *Key,
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to get the number of elements in a JSON array. Returns or 0 if JsonArray
+ is NULL or not a JSON array.
+
+ @param[in] JsonArray The provided JSON array.
+
+ @retval Return the number of elements in this JSON array or 0.
+
+**/
+UINTN
+EFIAPI
+JsonArrayCount (
+ IN EDKII_JSON_ARRAY JsonArray
+ );
+
+/**
+ The function is used to return the JSON value in the array at position index. The valid range
+ for this index is from 0 to the return value of JsonArrayCount() minus 1.
+
+ It only returns a reference to this value and any changes on this value will impact the
+ original JSON object. If that is not expected, please call JsonValueClone() to clone it to
+ use.
+
+ If this array is NULL or not a JSON array, or if index is out of range, NULL will be returned.
+
+ @param[in] JsonArray The provided JSON Array.
+
+ @retval Return the JSON value located in the Index position or
+ NULL if JsonArray is not an array or no items in the array.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonArrayGetValue (
+ IN EDKII_JSON_ARRAY JsonArray,
+ IN UINTN Index
+ );
+
+/**
+ The function is used to append a JSON value to the end of the JSON array, and grow the size of
+ array by 1. The reference count of this value will be increased by 1.
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] JsonArray The provided JSON object.
+ @param[in] Json The JSON value to append.
+
+ @retval EFI_ABORTED Some error occur and operation aborted.
+ @retval EFI_SUCCESS JSON value has been appended to the end of the JSON array.
+
+**/
+EFI_STATUS
+EFIAPI
+JsonArrayAppendValue (
+ IN EDKII_JSON_ARRAY JsonArray,
+ IN EDKII_JSON_VALUE Json
+ );
+
+/**
+ The function is used to remove a JSON value at position index, shifting the elements after index
+ one position towards the start of the array. The reference count of this value will be decreased
+ by 1.
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] JsonArray The provided JSON array.
+ @param[in] Index The Index position before removement.
+
+ @retval EFI_ABORTED Some error occur and operation aborted.
+ @retval EFI_SUCCESS The JSON array has been removed at position index.
+
+**/
+EFI_STATUS
+EFIAPI
+JsonArrayRemoveValue (
+ IN EDKII_JSON_ARRAY JsonArray,
+ IN UINTN Index
+ );
+
+/**
+ Dump JSON to a buffer.
+
+ @param[in] JsonValue The provided JSON value.
+ @param[in] Flags The Index position before removement. The value
+ could be the combination of below flags.
+ - EDKII_JSON_INDENT(n)
+ - EDKII_JSON_COMPACT
+ - EDKII_JSON_ENSURE_ASCII
+ - EDKII_JSON_SORT_KEYS
+ - EDKII_JSON_PRESERVE_ORDER
+ - EDKII_JSON_ENCODE_ANY
+ - EDKII_JSON_ESCAPE_SLASH
+ - EDKII_JSON_REAL_PRECISION(n)
+ - EDKII_JSON_EMBED
+ See below URI for the JSON encoding flags reference.
+ https://jansson.readthedocs.io/en/2.13/apiref.html#encoding
+
+ @retval CHAR8 * Dump fail if NULL returned, otherwise the buffer
+ contain JSON paylaod in ASCII string. The return
+ value must be freed by the caller FreePool().
+**/
+CHAR8 *
+EFIAPI
+JsonDumpString (
+ IN EDKII_JSON_VALUE JsonValue,
+ IN UINTN Flags
+ );
+
+/**
+ Load JSON from a buffer.
+
+ @param[in] Buffer Bufffer to the JSON payload
+ @param[in] BufferLen Length of the buffer
+ @param[in] Flags Flag of loading JSON buffer, the value
+ could be the combination of below flags.
+ - EDKII_JSON_REJECT_DUPLICATES
+ - EDKII_JSON_DISABLE_EOF_CHECK
+ - EDKII_JSON_DECODE_ANY
+ - EDKII_JSON_DECODE_INT_AS_REAL
+ - EDKII_JSON_ALLOW_NUL
+ See below URI for the JSON encoding flags reference.
+ https://jansson.readthedocs.io/en/2.13/apiref.html?highlight=json_loadb#decoding
+
+ @param[in,out] Error Pointer EDKII_JSON_ERROR structure
+
+ @retval EDKII_JSON_VALUE NULL means fail to load JSON payload.
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonLoadBuffer (
+ IN CONST CHAR8 *Buffer,
+ IN UINTN BufferLen,
+ IN UINTN Flags,
+ IN OUT EDKII_JSON_ERROR *Error
+ );
+
+/**
+ The reference count is used to track whether a value is still in use or not.
+ When a value is created, it's reference count is set to 1.
+ when the value is no longer needed, the reference count is decremented.
+ When the reference count drops to zero, there are no references left and the
+ value can be destroyed.
+
+ This funciton decrement the reference count of EDKII_JSON_VALUE. As soon as
+ a call to json_decref() drops the reference count to zero, the value is
+ destroyed and it can no longer be used.
+
+ @param[in] JsonValue JSON value
+**/
+VOID
+EFIAPI
+JsonDecreaseReference (
+ IN EDKII_JSON_VALUE JsonValue
+ );
+
+/**
+ The reference count is used to track whether a value is still in use or not.
+ When a value is created, it's reference count is set to 1.
+ If a reference to a value is kept (e.g. a value is stored somewhere for later use),
+ its reference count is incremented.
+
+ This function increment the reference count of json if it's not NULL.
+ Returns EDKII_JSON_VALUE.
+
+ @param[in] JsonValue JSON value
+ @retval EDKII_JSON_VALUE of itself
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonIncreaseReference (
+ IN EDKII_JSON_VALUE JsonValue
+ );
+/**
+ Returns an opaque iterator which can be used to iterate over all key-value pairs
+ in object, or NULL if object is empty
+
+ @param[in] JsonValue JSON value
+**/
+VOID *
+EFIAPI
+JsonObjectIterator (
+ IN EDKII_JSON_VALUE JsonValue
+ );
+
+/**
+ Extract the associated value from iterator.
+
+ @param[in] Iterator Iterator pointer
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonObjectIteratorValue (
+ IN VOID *Iterator
+ );
+
+/**
+ Returns an iterator pointing to the next key-value pair in object after iter,
+ or NULL if the whole object has been iterated through.
+
+ @param[in] JsonValue JSON value
+ @param[in] Iterator Iterator pointer
+ @retval Iterator pointer
+**/
+VOID *
+JsonObjectIteratorNext (
+ IN EDKII_JSON_VALUE JsonValue,
+ IN VOID *Iterator
+ );
+
+/**
+ Returns the json type of this json value
+
+ @param[in] JsonValue JSON value
+ @retval JSON type returned
+**/
+EDKII_JSON_TYPE
+EFIAPI
+JsonGetType(
+ IN EDKII_JSON_VALUE JsonValue
+ );
+#endif
diff --git a/RedfishPkg/Library/JsonLib/JsonLib.c b/RedfishPkg/Library/JsonLib/JsonLib.c
new file mode 100644
index 0000000000..34ff381aee
--- /dev/null
+++ b/RedfishPkg/Library/JsonLib/JsonLib.c
@@ -0,0 +1,957 @@
+/** @file
+ APIs for JSON operations. The fuctions provided by this library are the
+ wrapper to native open source jansson library. See below document for
+ the API reference.
+ https://jansson.readthedocs.io/en/2.13/apiref.html
+
+ Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.<BR>
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+**/
+
+#include <Uefi.h>
+#include <Library/JsonLib.h>
+#include <Library/BaseUcs2Utf8Lib.h>
+#include <Library/MemoryAllocationLib.h>
+
+#include "jansson.h"
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON array,
+ or NULL on error. Initially, the array is empty.
+
+ The reference count of this value will be set to 1, and caller needs to cleanup the
+ value by calling JsonValueFree().
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @retval The created JSON value which contains a JSON array or NULL if intial a JSON array
+ is failed.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitArray (
+ VOID
+ )
+{
+ return (EDKII_JSON_VALUE)json_array();
+}
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON object,
+ or NULL on error. Initially, the object is empty.
+
+ The reference count of this value will be set to 1, and caller needs to cleanup the
+ value by calling JsonValueFree().
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @retval The created JSON value which contains a JSON object or NULL if intial a JSON object
+ is failed.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitObject (
+ VOID
+ )
+{
+ return (EDKII_JSON_VALUE)json_object();
+}
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON string,
+ or NULL on error.
+
+ The input string must be NULL terminated Ascii format, non-Ascii characters will
+ be processed as an error. Unicode characters can also be represented by Ascii string
+ as the format: \u + 4 hexadecimal digits, like \u3E5A, or \u003F.
+
+ The reference count of this value will be set to 1, and caller needs to cleanup the
+ value by calling JsonValueFree().
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] String The Ascii string to initialize to JSON value
+
+ @retval The created JSON value which contains a JSON string or NULL. Select a
+ Getter API for a specific encoding format.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitAsciiString (
+ IN CONST CHAR8 *String
+ )
+{
+ UINTN Index;
+
+ if (String == NULL) {
+ return NULL;
+ }
+
+ Index = 0;
+ while (*(String + Index) != '\0') {
+ if (((*(String + Index)) & 0x80) != 0x00) {
+ return NULL;
+ }
+
+ Index++;
+ }
+
+ return (EDKII_JSON_VALUE)json_string (String);
+}
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON string,
+ or NULL on error.
+
+ The input must be a NULL terminated UCS2 format Unicode string.
+
+ The reference count of this value will be set to 1, and caller needs to cleanup the
+ value by calling JsonValueFree().
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] String The Unicode string to initialize to JSON value
+
+ @retval The created JSON value which contains a JSON string or NULL. Select a
+ Getter API for a specific encoding format.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitUnicodeString (
+ IN CHAR16 *String
+ )
+{
+ EFI_STATUS Status;
+ CHAR8 *Utf8Str;
+
+ if (String == NULL) {
+ return NULL;
+ }
+
+ Utf8Str = NULL;
+ Status = UCS2StrToUTF8 (String, &Utf8Str);
+ if (EFI_ERROR (Status)) {
+ return NULL;
+ }
+
+ return (EDKII_JSON_VALUE)json_string (Utf8Str);
+}
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON integer,
+ or NULL on error.
+
+ The reference count of this value will be set to 1, and caller needs to cleanup the
+ value by calling JsonValueFree().
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] Value The integer to initialize to JSON value
+
+ @retval The created JSON value which contains a JSON number or NULL.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitNumber (
+ IN INT64 Value
+ )
+{
+ return (EDKII_JSON_VALUE)json_integer (Value);
+}
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON boolean,
+ or NULL on error.
+
+ Boolean JSON value is kept as static value, and no need to do any cleanup work.
+
+ @param[in] Value The boolean value to initialize.
+
+ @retval The created JSON value which contains a JSON boolean or NULL.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitBoolean (
+ IN BOOLEAN Value
+ )
+{
+ return (EDKII_JSON_VALUE)json_boolean (Value);
+}
+
+/**
+ The function is used to initialize a JSON value which contains a new JSON NULL,
+ or NULL on error.
+
+ NULL JSON value is kept as static value, and no need to do any cleanup work.
+
+ @retval The created NULL JSON value.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueInitNull (
+ VOID
+ )
+{
+ return (EDKII_JSON_VALUE)json_null();
+}
+
+/**
+ The function is used to decrease the reference count of a JSON value by one, and once
+ this reference count drops to zero, the value is destroyed and it can no longer be used.
+ If this destroyed value is object type or array type, reference counts for all containing
+ JSON values will be decreased by 1. Boolean JSON value and NULL JSON value won't be destroyed
+ since they are static values kept in memory.
+
+ Reference Count Strategy: BaseJsonLib uses this strategy to track whether a value is still
+ in use or not. When a value is created, it's reference count is set to 1. If a reference to a
+ value is kept for use, its reference count is incremented, and when the value is no longer
+ needed, the reference count is decremented. When the reference count drops to zero, there are
+ no references left, and the value can be destroyed.
+
+ The given JSON value maybe NULL and not causing any problem. Just output the debug message
+ to inform caller the NULL value is passed in.
+
+ @param[in] Json The JSON value to be freed. json_decref may return without any
+ changes if Json is NULL.
+
+**/
+VOID
+EFIAPI
+JsonValueFree (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ json_decref((json_t *)Json);
+}
+
+/**
+ The function is used to create a fresh copy of a JSON value, and all child values are deep
+ copied in a recursive fashion. It should be called when this JSON value might be modified
+ in later use, but the original still wants to be used in somewhere else.
+
+ Reference counts of the returned root JSON value and all child values will be set to 1, and
+ caller needs to cleanup the root value by calling JsonValueFree().
+
+ * Note: Since this function performs a copy from bottom to up, too many calls may cause some
+ performance issues, user should avoid unnecessary calls to this function unless it is really
+ needed.
+
+ @param[in] Json The JSON value to be cloned.
+
+ @retval Return the cloned JSON value, or NULL on error.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonValueClone (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ return (EDKII_JSON_VALUE)json_deep_copy ((json_t *) Json);
+}
+
+/**
+ The function is used to return if the provided JSON value contains a JSON array.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value contains a JSON array.
+ @retval FALSE The JSON value doesn't contain a JSON array.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsArray (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ return json_is_array ((json_t *) Json);
+}
+
+/**
+ The function is used to return if the provided JSON value contains a JSON object.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value contains a JSON object.
+ @retval FALSE The JSON value doesn't contain a JSON object.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsObject (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ return json_is_object ((json_t *) Json);
+}
+
+/**
+ The function is used to return if the provided JSON Value contains a string, Ascii or
+ Unicode format is not differentiated.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value contains a JSON string.
+ @retval FALSE The JSON value doesn't contain a JSON string.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsString (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ return json_is_string ((json_t *) Json);
+}
+
+/**
+ The function is used to return if the provided JSON value contains a JSON number.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value is contains JSON number.
+ @retval FALSE The JSON value doesn't contain a JSON number.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsNumber (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ return json_is_integer ((json_t *) Json);
+}
+
+/**
+ The function is used to return if the provided JSON value contains a JSON boolean.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value contains a JSON boolean.
+ @retval FALSE The JSON value doesn't contain a JSON boolean.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsBoolean (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ return json_is_boolean ((json_t *) Json);
+}
+
+/**
+ The function is used to return if the provided JSON value contains a JSON NULL.
+
+ @param[in] Json The provided JSON value.
+
+ @retval TRUE The JSON value contains a JSON NULL.
+ @retval FALSE The JSON value doesn't contain a JSON NULL.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueIsNull (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ return json_is_null ((json_t *) Json);
+}
+
+/**
+ The function is used to retrieve the associated array in an array type JSON value.
+
+ Any changes to the returned array will impact the original JSON value.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated array in JSON value or NULL.
+
+**/
+EDKII_JSON_ARRAY
+EFIAPI
+JsonValueGetArray (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ if (Json == NULL || !JsonValueIsArray (Json)) {
+ return NULL;
+ }
+
+ return (EDKII_JSON_ARRAY)Json;
+}
+
+/**
+ The function is used to retrieve the associated object in an object type JSON value.
+
+ Any changes to the returned object will impact the original JSON value.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated object in JSON value or NULL.
+
+**/
+EDKII_JSON_OBJECT
+EFIAPI
+JsonValueGetObject (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ if (Json == NULL || !JsonValueIsObject (Json)) {
+ return NULL;
+ }
+
+ return (EDKII_JSON_OBJECT)Json;
+}
+
+/**
+ The function is used to retrieve the associated Ascii string in a string type JSON value.
+
+ Any changes to the returned string will impact the original JSON value.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated Ascii string in JSON value or NULL.
+
+**/
+CONST CHAR8 *
+EFIAPI
+JsonValueGetAsciiString (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ CHAR8 *AsciiStr;
+ UINTN Index;
+
+ AsciiStr = (CHAR8 *) ((json_t *) Json);
+ if (AsciiStr == NULL) {
+ return NULL;
+ }
+
+ Index = 0;
+ while (*(AsciiStr + Index) != '\0') {
+ if (((*(AsciiStr + Index)) & 0x80) != 0x00) {
+ return NULL;
+ }
+
+ Index++;
+ }
+
+ return AsciiStr;
+}
+
+/**
+ The function is used to retrieve the associated Unicode string in a string type JSON value.
+
+ Caller can do any changes to the returned string without any impact to the original JSON
+ value, and caller needs to free the returned string using FreePool().
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated Unicode string in JSON value or NULL.
+
+**/
+CHAR16*
+EFIAPI
+JsonValueGetUnicodeString (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ EFI_STATUS Status;
+ CONST CHAR8 *Utf8Str;
+ CHAR16 *Ucs2Str;
+
+ Utf8Str = json_string_value ((json_t *) Json);
+ if (Utf8Str == NULL) {
+ return NULL;
+ }
+
+ Status = UTF8StrToUCS2 ((CHAR8*)Utf8Str, &Ucs2Str);
+ if (EFI_ERROR (Status)) {
+ return NULL;
+ }
+
+ return Ucs2Str;
+}
+
+/**
+ The function is used to retrieve the associated integer in a number type JSON value.
+
+ The input JSON value should not be NULL or contain no JSON number, otherwise it will
+ ASSERT() and return 0.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated number in JSON value.
+
+**/
+INT64
+EFIAPI
+JsonValueGetNumber (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ ASSERT (Json != NULL && JsonValueIsNumber (Json));
+ if (Json == NULL || !JsonValueIsNumber (Json)) {
+ return 0;
+ }
+
+ return json_integer_value ((json_t *) Json);
+}
+
+/**
+ The function is used to retrieve the associated boolean in a boolean type JSON value.
+
+ The input JSON value should not be NULL or contain no JSON boolean, otherwise it will
+ ASSERT() and return FALSE.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated value of JSON boolean.
+
+**/
+BOOLEAN
+EFIAPI
+JsonValueGetBoolean (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ ASSERT (Json != NULL && JsonValueIsBoolean (Json));
+ if (Json == NULL || !JsonValueIsBoolean (Json)) {
+ return FALSE;
+ }
+
+ return json_is_true ((json_t *) Json);
+}
+
+/**
+ The function is used to retrieve the associated string in a string type JSON value.
+
+ Any changes to the returned string will impact the original JSON value.
+
+ @param[in] Json The provided JSON value.
+
+ @retval Return the associated Ascii string in JSON value or NULL on errors.
+
+**/
+CONST CHAR8*
+EFIAPI
+JsonValueGetString (
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ return json_string_value ((const json_t *)Json);
+}
+
+/**
+ The function is used to get the number of elements in a JSON object, or 0 if it is NULL or
+ not a JSON object.
+
+ @param[in] JsonObject The provided JSON object.
+
+ @retval Return the number of elements in this JSON object or 0.
+
+**/
+UINTN
+EFIAPI
+JsonObjectSize (
+ IN EDKII_JSON_OBJECT JsonObject
+ )
+{
+ return json_object_size ((json_t *) JsonObject);
+}
+
+/**
+ The function is used to enumerate all keys in a JSON object.
+
+ Caller should be responsible to free the returned key array reference using
+ FreePool(). But contained keys are read only and must not be modified or freed.
+
+ @param[in] JsonObj The provided JSON object for enumeration.
+ @param[out] KeyCount The count of keys in this JSON object.
+
+ @retval Return an array of the enumerated keys in this JSON object or NULL if
+ JsonObj is not an JSON object, key count is zero or on other errors.
+
+**/
+CHAR8**
+JsonObjectGetKeys (
+ IN EDKII_JSON_OBJECT JsonObj,
+ OUT UINTN *KeyCount
+ )
+{
+
+ UINTN Index;
+ CONST CHAR8 **KeyArray;
+ CONST CHAR8 *Key;
+ EDKII_JSON_VALUE Value;
+
+ if (JsonObj == NULL || KeyCount == NULL) {
+ return NULL;
+ }
+
+ Index = 0;
+ json_object_foreach(JsonObj, Key, Value) {
+ Index++;
+ }
+ if (Index == 0) {
+ *KeyCount = 0;
+ return NULL;
+ }
+
+ *KeyCount = Index;
+ KeyArray = (CONST CHAR8 **) AllocateZeroPool (*KeyCount * sizeof (CHAR8 *));
+ if (KeyArray == NULL) {
+ return NULL;
+ }
+
+ Key = NULL;
+ Value = NULL;
+ Index = 0;
+ json_object_foreach((json_t *) JsonObj, Key, Value) {
+ KeyArray[Index] = Key;
+ Index++;
+ }
+
+ return (CHAR8 **)KeyArray;
+}
+
+/**
+ The function is used to get a JSON value corresponding to the input key from a JSON object.
+
+ It only returns a reference to this value and any changes on this value will impact the
+ original JSON object. If that is not expected, please call JsonValueClone() to clone it to
+ use.
+
+ Input key must be a valid NULL terminated UTF8 encoded string. NULL will be returned when
+ Key-Value is not found in this JSON object.
+
+ @param[in] JsonObj The provided JSON object.
+ @param[in] Key The key of the JSON value to be retrieved.
+
+ @retval Return the corresponding JSON value to key, or NULL on error.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonObjectGetValue (
+ IN CONST EDKII_JSON_OBJECT JsonObj,
+ IN CONST CHAR8 *Key
+ )
+{
+ return (EDKII_JSON_VALUE)json_object_get ((const json_t *)JsonObj, (const char *)Key);
+}
+
+/**
+ The function is used to set a JSON value corresponding to the input key from a JSON object,
+ and the reference count of this value will be increased by 1.
+
+ Input key must be a valid NULL terminated UTF8 encoded string. If there already is a value for
+ this key, this key will be assigned to the new JSON value. The old JSON value will be removed
+ from this object and thus its' reference count will be decreased by 1.
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] JsonObj The provided JSON object.
+ @param[in] Key The key of the JSON value to be set.
+ @param[in] Json The JSON value to set to this JSON object mapped by key.
+
+ @retval EFI_ABORTED Some error occur and operation aborted.
+ @retval EFI_SUCCESS The JSON value has been set to this JSON object.
+
+**/
+EFI_STATUS
+EFIAPI
+JsonObjectSetValue (
+ IN EDKII_JSON_OBJECT JsonObj,
+ IN CONST CHAR8 *Key,
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ if (json_object_set ((json_t *) JsonObj, Key, (json_t *) Json) != 0) {
+ return EFI_ABORTED;
+ } else {
+ return EFI_SUCCESS;
+ }
+}
+
+/**
+ The function is used to get the number of elements in a JSON array. Returns or 0 if JsonArray
+ is NULL or not a JSON array.
+
+ @param[in] JsonArray The provided JSON array.
+
+ @retval Return the number of elements in this JSON array or 0.
+
+**/
+UINTN
+EFIAPI
+JsonArrayCount (
+ IN EDKII_JSON_ARRAY JsonArray
+ )
+{
+ return json_array_size ((json_t *) JsonArray);
+}
+
+/**
+ The function is used to return the JSON value in the array at position index. The valid range
+ for this index is from 0 to the return value of JsonArrayCount() minus 1.
+
+ It only returns a reference to this value and any changes on this value will impact the
+ original JSON object. If that is not expected, please call JsonValueClone() to clone it to
+ use.
+
+ If this array is NULL or not a JSON array, or if index is out of range, NULL will be returned.
+
+ @param[in] JsonArray The provided JSON Array.
+
+ @retval Return the JSON value located in the Index position or
+ NULL if JsonArray is not an array or no items in the array.
+
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonArrayGetValue (
+ IN EDKII_JSON_ARRAY JsonArray,
+ IN UINTN Index
+ )
+{
+ return (EDKII_JSON_VALUE)json_array_get ((json_t *) JsonArray, Index);
+}
+
+/**
+ The function is used to append a JSON value to the end of the JSON array, and grow the size of
+ array by 1. The reference count of this value will be increased by 1.
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] JsonArray The provided JSON object.
+ @param[in] Json The JSON value to append.
+
+ @retval EFI_ABORTED Some error occur and operation aborted.
+ @retval EFI_SUCCESS JSON value has been appended to the end of the JSON array.
+
+**/
+EFI_STATUS
+EFIAPI
+JsonArrayAppendValue (
+ IN EDKII_JSON_ARRAY JsonArray,
+ IN EDKII_JSON_VALUE Json
+ )
+{
+ if (json_array_append ((json_t *) JsonArray, (json_t *) Json) != 0) {
+ return EFI_ABORTED;
+ } else {
+ return EFI_SUCCESS;
+ }
+}
+
+/**
+ The function is used to remove a JSON value at position index, shifting the elements after index
+ one position towards the start of the array. The reference count of this value will be decreased
+ by 1.
+
+ More details for reference count strategy can refer to the API description for JsonValueFree().
+
+ @param[in] JsonArray The provided JSON array.
+ @param[in] Index The Index position before removement.
+
+ @retval EFI_ABORTED Some error occur and operation aborted.
+ @retval EFI_SUCCESS The JSON array has been removed at position index.
+
+**/
+EFI_STATUS
+EFIAPI
+JsonArrayRemoveValue (
+ IN EDKII_JSON_ARRAY JsonArray,
+ IN UINTN Index
+ )
+{
+ if (json_array_remove ((json_t *) JsonArray, Index) != 0) {
+ return EFI_ABORTED;
+ } else {
+ return EFI_SUCCESS;
+ }
+}
+
+/**
+ Dump JSON to a buffer.
+
+ @param[in] JsonValue The provided JSON value.
+ @param[in] Flags The Index position before removement. The value
+ could be the combination of below flags.
+ - EDKII_JSON_INDENT(n)
+ - EDKII_JSON_COMPACT
+ - EDKII_JSON_ENSURE_ASCII
+ - EDKII_JSON_SORT_KEYS
+ - EDKII_JSON_PRESERVE_ORDER
+ - EDKII_JSON_ENCODE_ANY
+ - EDKII_JSON_ESCAPE_SLASH
+ - EDKII_JSON_REAL_PRECISION(n)
+ - EDKII_JSON_EMBED
+ See below URI for the JSON encoding flags reference.
+ https://jansson.readthedocs.io/en/2.13/apiref.html#encoding
+
+ @retval CHAR8 * Dump fail if NULL returned, otherwise the buffer
+ contain JSON paylaod in ASCII string. The return
+ value must be freed by the caller using FreePool().
+**/
+CHAR8 *
+EFIAPI
+JsonDumpString (
+ IN EDKII_JSON_VALUE JsonValue,
+ IN UINTN Flags
+ )
+{
+ if (JsonValue == NULL) {
+ return NULL;
+ }
+ return json_dumps((json_t *)JsonValue, Flags);
+}
+
+/**
+ Load JSON from a buffer.
+
+ @param[in] Buffer Bufffer to the JSON payload
+ @param[in] BufferLen Length of the buffer
+ @param[in] Flags Flag of loading JSON buffer, the value
+ could be the combination of below flags.
+ - EDKII_JSON_REJECT_DUPLICATES
+ - EDKII_JSON_DISABLE_EOF_CHECK
+ - EDKII_JSON_DECODE_ANY
+ - EDKII_JSON_DECODE_INT_AS_REAL
+ - EDKII_JSON_ALLOW_NUL
+ See below URI for the JSON encoding flags reference.
+ https://jansson.readthedocs.io/en/2.13/apiref.html?highlight=json_loadb#decoding
+
+ @param[in,out] Error Pointer EDKII_JSON_ERROR structure
+
+ @retval EDKII_JSON_VALUE NULL means fail to load JSON payload.
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonLoadBuffer (
+ IN CONST CHAR8 *Buffer,
+ IN UINTN BufferLen,
+ IN UINTN Flags,
+ IN OUT EDKII_JSON_ERROR *Error
+ )
+{
+ return json_loadb(Buffer, BufferLen, Flags, (json_error_t *)Error);
+}
+
+/**
+ The reference count is used to track whether a value is still in use or not.
+ When a value is created, it's reference count is set to 1.
+ when the value is no longer needed, the reference count is decremented.
+ When the reference count drops to zero, there are no references left and the
+ value can be destroyed.
+
+ This funciton decrement the reference count of EDKII_JSON_VALUE. As soon as
+ a call to json_decref() drops the reference count to zero, the value is
+ destroyed and it can no longer be used.
+
+ @param[in] JsonValue JSON value
+**/
+VOID
+EFIAPI
+JsonDecreaseReference (
+ IN EDKII_JSON_VALUE JsonValue
+ )
+{
+ json_decref (JsonValue);
+}
+
+/**
+ The reference count is used to track whether a value is still in use or not.
+ When a value is created, it's reference count is set to 1.
+ If a reference to a value is kept (e.g. a value is stored somewhere for later use),
+ its reference count is incremented.
+
+ This function increment the reference count of json if it's not NULL.
+ Returns EDKII_JSON_VALUE.
+
+ @param[in] JsonValue JSON value
+ @retval EDKII_JSON_VALUE of itself
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonIncreaseReference (
+ IN EDKII_JSON_VALUE JsonValue
+ )
+{
+ return json_incref (JsonValue);
+}
+
+/**
+ Returns an opaque iterator which can be used to iterate over all key-value pairs
+ in object, or NULL if object is empty.
+
+ @param[in] JsonValue JSON value
+ @retval Iterator pointer
+**/
+VOID *
+EFIAPI
+JsonObjectIterator (
+ IN EDKII_JSON_VALUE JsonValue
+ )
+{
+ return json_object_iter (JsonValue);
+}
+
+/**
+ Extract the associated value from iterator.
+
+ @param[in] Iterator Iterator pointer
+ @retval EDKII_JSON_VALUE
+**/
+EDKII_JSON_VALUE
+EFIAPI
+JsonObjectIteratorValue (
+ IN VOID *Iterator
+ )
+{
+ return json_object_iter_value(Iterator);
+}
+
+/**
+ Returns an iterator pointing to the next key-value pair in object after iter,
+ or NULL if the whole object has been iterated through.
+
+ @param[in] JsonValue JSON value
+ @param[in] Iterator Iterator pointer
+ @retval Iterator pointer
+**/
+VOID *
+JsonObjectIteratorNext (
+ IN EDKII_JSON_VALUE JsonValue,
+ IN VOID *Iterator
+ )
+{
+ return json_object_iter_next(JsonValue, Iterator);
+}
+
+/**
+ Returns the json type of this json value.
+
+ @param[in] JsonValue JSON value
+ @retval JSON type returned
+**/
+EDKII_JSON_TYPE
+EFIAPI
+JsonGetType (
+ IN EDKII_JSON_VALUE JsonValue
+ )
+{
+ return ((json_t *)JsonValue)->type;
+}
diff --git a/RedfishPkg/Library/JsonLib/JsonLib.inf b/RedfishPkg/Library/JsonLib/JsonLib.inf
new file mode 100644
index 0000000000..48b094a78a
--- /dev/null
+++ b/RedfishPkg/Library/JsonLib/JsonLib.inf
@@ -0,0 +1,86 @@
+## @file
+# Thirty party Jansson library for JSON operations.
+#
+# Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+# (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+ INF_VERSION = 0x0001001b
+ BASE_NAME = JsonLib
+ FILE_GUID = F5E36815-305A-4C5A-9D75-4F2149E45255
+ MODULE_TYPE = DXE_DRIVER
+ VERSION_STRING = 1.0
+ LIBRARY_CLASS = JsonLib|DXE_DRIVER UEFI_APPLICATION UEFI_DRIVER
+
+#
+# VALID_ARCHITECTURES = IA32 X64 ARM AARCH64 RISCV64
+#
+
+[Sources]
+ #
+ # Below are the source code of third
+ # party jansson library.
+ #
+ jansson/src/dump.c
+ jansson/src/error.c
+ jansson/src/hashtable.c
+ jansson/src/hashtable_seed.c
+ jansson/src/memory.c
+ jansson/src/pack_unpack.c
+ jansson/src/strbuffer.c
+ jansson/src/strconv.c
+ jansson/src/utf.c
+ jansson/src/value.c
+ jansson/src/version.c
+ #
+ # Below are the source of edk2 JsonLib.
+ #
+ JsonLib.c
+ jansson_config.h
+ jansson_private_config.h
+ #
+ # Below is the source code override to fix the build issue.
+ # Add code in load.c to conditionally use stdin according
+ # to HAVE_UNISTD_H macro. The PR is submitted to jansson
+ # open source community.
+ # https://github.com/akheron/jansson/pull/558
+ #
+ load.c
+
+[Packages]
+ MdePkg/MdePkg.dec
+ MdeModulePkg/MdeModulePkg.dec
+ RedfishPkg/RedfishPkg.dec
+
+[LibraryClasses]
+ BaseLib
+ BaseMemoryLib
+ Ucs2Utf8Lib
+ RedfishCrtLib
+ DebugLib
+ MemoryAllocationLib
+ PrintLib
+ UefiRuntimeServicesTableLib
+ UefiLib
+
+[BuildOptions]
+ #
+ # Disables the following Visual Studio compiler warnings
+ # so we do not break the build with /WX option:
+ # C4090: 'function' : different 'const' qualifiers
+ # C4244: conversion from type1 to type2, possible loss of data
+ # C4334: 32-bit shift implicitly converted to 64-bit
+ # C4204: nonstandard extension used: non-constant aggregate initializer
+ #
+ # Define macro HAVE_CONFIG_H to include jansson_private_config.h to build.
+ # Undefined _WIN32, WIN64, _MSC_VER macros
+ # On GCC, no error on the unused-function and unused-but-set-variable.
+ #
+ MSFT:*_*_X64_CC_FLAGS = /wd4204 /wd4244 /wd4090 /wd4334 /DHAVE_CONFIG_H=1 /U_WIN32 /UWIN64 /U_MSC_VER
+ MSFT:*_*_IA32_CC_FLAGS = /wd4204 /wd4244 /wd4090 /DHAVE_CONFIG_H=1 /U_WIN32 /UWIN64 /U_MSC_VER
+ GCC:*_*_*_CC_FLAGS = -Wno-unused-function -Wno-unused-but-set-variable
+
diff --git a/RedfishPkg/Library/JsonLib/Readme.rst b/RedfishPkg/Library/JsonLib/Readme.rst
new file mode 100644
index 0000000000..1c652591c7
--- /dev/null
+++ b/RedfishPkg/Library/JsonLib/Readme.rst
@@ -0,0 +1,35 @@
+=============================================================================
+ Introduction
+=============================================================================
+ Jansson is a C library for encoding, decoding and manipulating JSON data.
+Its main features and design principles are:
+
+ - Simple and intuitive API and data model
+ - Comprehensive documentation
+ - No dependencies on other libraries
+ - Full Unicode support (UTF-8)
+ - Extensive test suite
+
+ Jansson is licensed under the MIT license(refer to ReadMe.rst under edk2).
+It is used in production and its API is stable. It works on numerous
+platforms, including numerous Unix like systems and Windows. It's suitable
+for use on any system, including desktop, server, and small embedded systems.
+
+ In UEFI/EDKII environment, Redfish project consumes jansson to achieve JSON
+operations.
+
+* Jansson version on edk2: 2.13.1, API reference is on the below URL,
+ https://jansson.readthedocs.io/en/2.13/apiref.html
+
+* EDKII jansson library wrapper:
+ - JsonLib.h:
+ This is the denifitions of EDKII JSON APIs which are mapped to
+ jannson funcitons accordingly.
+
+*Known issue:
+ Build fail with jansson/src/load.c, add code in load.c to conditionally
+ use stdin according to HAVE_UNISTD_H macro. The PR is submitted to
+ jansson open source community.
+ https://github.com/akheron/jansson/pull/558
+
+
diff --git a/RedfishPkg/Library/JsonLib/jansson_config.h b/RedfishPkg/Library/JsonLib/jansson_config.h
new file mode 100644
index 0000000000..c66d3ced9b
--- /dev/null
+++ b/RedfishPkg/Library/JsonLib/jansson_config.h
@@ -0,0 +1,41 @@
+/** @file This is the configuration file for building jansson library.
+
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+ **/
+
+#ifndef JANSSON_CONFIG_H_
+#define JANSSON_CONFIG_H_
+
+///
+/// We don't support inline JSON on edk2
+///
+#define JSON_INLINE
+
+///
+/// We support long long on edk2
+///
+#define JSON_INTEGER_IS_LONG_LONG 1
+
+///
+/// We don't support locale on edk2
+///
+#define JSON_HAVE_LOCALECONV 0
+
+///
+/// We don't support atomic builtins on edk2
+///
+#define JSON_HAVE_ATOMIC_BUILTINS 0
+
+///
+/// We don't support sync builtins on edk2
+///
+#define JSON_HAVE_SYNC_BUILTINS 0
+
+///
+/// Mzximum deepth is set to 2048
+///
+#define JSON_PARSER_MAX_DEPTH 2048
+
+#endif
diff --git a/RedfishPkg/Library/JsonLib/jansson_private_config.h b/RedfishPkg/Library/JsonLib/jansson_private_config.h
new file mode 100644
index 0000000000..268f91ef8a
--- /dev/null
+++ b/RedfishPkg/Library/JsonLib/jansson_private_config.h
@@ -0,0 +1,19 @@
+/** @file
+ Jansson private configurations for UEFI support.
+
+ Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef JANSSON_PRIVATE_CONFIG_H_
+#define JANSSON_PRIVATE_CONFIG_H_
+
+#define HAVE_SYS_TIME_H 1
+#define HAVE_SYS_TYPES_H 1
+
+#define INITIAL_HASHTABLE_ORDER 3
+
+#endif
diff --git a/RedfishPkg/Library/JsonLib/load.c b/RedfishPkg/Library/JsonLib/load.c
new file mode 100644
index 0000000000..37e0ba4271
--- /dev/null
+++ b/RedfishPkg/Library/JsonLib/load.c
@@ -0,0 +1,1111 @@
+/*
+ * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
+ *
+ * Jansson is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent AND MIT
+ */
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
+#include "jansson_private.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#include "jansson.h"
+#include "strbuffer.h"
+#include "utf.h"
+
+#define STREAM_STATE_OK 0
+#define STREAM_STATE_EOF -1
+#define STREAM_STATE_ERROR -2
+
+#define TOKEN_INVALID -1
+#define TOKEN_EOF 0
+#define TOKEN_STRING 256
+#define TOKEN_INTEGER 257
+#define TOKEN_REAL 258
+#define TOKEN_TRUE 259
+#define TOKEN_FALSE 260
+#define TOKEN_NULL 261
+
+/* Locale independent versions of isxxx() functions */
+#define l_isupper(c) ('A' <= (c) && (c) <= 'Z')
+#define l_islower(c) ('a' <= (c) && (c) <= 'z')
+#define l_isalpha(c) (l_isupper(c) || l_islower(c))
+#define l_isdigit(c) ('0' <= (c) && (c) <= '9')
+#define l_isxdigit(c) \
+ (l_isdigit(c) || ('A' <= (c) && (c) <= 'F') || ('a' <= (c) && (c) <= 'f'))
+
+/* Read one byte from stream, convert to unsigned char, then int, and
+ return. return EOF on end of file. This corresponds to the
+ behaviour of fgetc(). */
+typedef int (*get_func)(void *data);
+
+typedef struct {
+ get_func get;
+ void *data;
+ char buffer[5];
+ size_t buffer_pos;
+ int state;
+ int line;
+ int column, last_column;
+ size_t position;
+} stream_t;
+
+typedef struct {
+ stream_t stream;
+ strbuffer_t saved_text;
+ size_t flags;
+ size_t depth;
+ int token;
+ union {
+ struct {
+ char *val;
+ size_t len;
+ } string;
+ json_int_t integer;
+ double real;
+ } value;
+} lex_t;
+
+#define stream_to_lex(stream) container_of(stream, lex_t, stream)
+
+/*** error reporting ***/
+
+static void error_set(json_error_t *error, const lex_t *lex, enum json_error_code code,
+ const char *msg, ...) {
+ va_list ap;
+ char msg_text[JSON_ERROR_TEXT_LENGTH];
+ char msg_with_context[JSON_ERROR_TEXT_LENGTH];
+
+ int line = -1, col = -1;
+ size_t pos = 0;
+ const char *result = msg_text;
+
+ if (!error)
+ return;
+
+ va_start(ap, msg);
+ vsnprintf(msg_text, JSON_ERROR_TEXT_LENGTH, msg, ap);
+ msg_text[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
+ va_end(ap);
+
+ if (lex) {
+ const char *saved_text = strbuffer_value(&lex->saved_text);
+
+ line = lex->stream.line;
+ col = lex->stream.column;
+ pos = lex->stream.position;
+
+ if (saved_text && saved_text[0]) {
+ if (lex->saved_text.length <= 20) {
+ snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, "%s near '%s'",
+ msg_text, saved_text);
+ msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
+ result = msg_with_context;
+ }
+ } else {
+ if (code == json_error_invalid_syntax) {
+ /* More specific error code for premature end of file. */
+ code = json_error_premature_end_of_input;
+ }
+ if (lex->stream.state == STREAM_STATE_ERROR) {
+ /* No context for UTF-8 decoding errors */
+ result = msg_text;
+ } else {
+ snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH, "%s near end of file",
+ msg_text);
+ msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] = '\0';
+ result = msg_with_context;
+ }
+ }
+ }
+
+ jsonp_error_set(error, line, col, pos, code, "%s", result);
+}
+
+/*** lexical analyzer ***/
+
+static void stream_init(stream_t *stream, get_func get, void *data) {
+ stream->get = get;
+ stream->data = data;
+ stream->buffer[0] = '\0';
+ stream->buffer_pos = 0;
+
+ stream->state = STREAM_STATE_OK;
+ stream->line = 1;
+ stream->column = 0;
+ stream->position = 0;
+}
+
+static int stream_get(stream_t *stream, json_error_t *error) {
+ int c;
+
+ if (stream->state != STREAM_STATE_OK)
+ return stream->state;
+
+ if (!stream->buffer[stream->buffer_pos]) {
+ c = stream->get(stream->data);
+ if (c == EOF) {
+ stream->state = STREAM_STATE_EOF;
+ return STREAM_STATE_EOF;
+ }
+
+ stream->buffer[0] = c;
+ stream->buffer_pos = 0;
+
+ if (0x80 <= c && c <= 0xFF) {
+ /* multi-byte UTF-8 sequence */
+ size_t i, count;
+
+ count = utf8_check_first(c);
+ if (!count)
+ goto out;
+
+ assert(count >= 2);
+
+ for (i = 1; i < count; i++)
+ stream->buffer[i] = stream->get(stream->data);
+
+ if (!utf8_check_full(stream->buffer, count, NULL))
+ goto out;
+
+ stream->buffer[count] = '\0';
+ } else
+ stream->buffer[1] = '\0';
+ }
+
+ c = stream->buffer[stream->buffer_pos++];
+
+ stream->position++;
+ if (c == '\n') {
+ stream->line++;
+ stream->last_column = stream->column;
+ stream->column = 0;
+ } else if (utf8_check_first(c)) {
+ /* track the Unicode character column, so increment only if
+ this is the first character of a UTF-8 sequence */
+ stream->column++;
+ }
+
+ return c;
+
+out:
+ stream->state = STREAM_STATE_ERROR;
+ error_set(error, stream_to_lex(stream), json_error_invalid_utf8,
+ "unable to decode byte 0x%x", c);
+ return STREAM_STATE_ERROR;
+}
+
+static void stream_unget(stream_t *stream, int c) {
+ if (c == STREAM_STATE_EOF || c == STREAM_STATE_ERROR)
+ return;
+
+ stream->position--;
+ if (c == '\n') {
+ stream->line--;
+ stream->column = stream->last_column;
+ } else if (utf8_check_first(c))
+ stream->column--;
+
+ assert(stream->buffer_pos > 0);
+ stream->buffer_pos--;
+ assert(stream->buffer[stream->buffer_pos] == c);
+}
+
+static int lex_get(lex_t *lex, json_error_t *error) {
+ return stream_get(&lex->stream, error);
+}
+
+static void lex_save(lex_t *lex, int c) { strbuffer_append_byte(&lex->saved_text, c); }
+
+static int lex_get_save(lex_t *lex, json_error_t *error) {
+ int c = stream_get(&lex->stream, error);
+ if (c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR)
+ lex_save(lex, c);
+ return c;
+}
+
+static void lex_unget(lex_t *lex, int c) { stream_unget(&lex->stream, c); }
+
+static void lex_unget_unsave(lex_t *lex, int c) {
+ if (c != STREAM_STATE_EOF && c != STREAM_STATE_ERROR) {
+/* Since we treat warnings as errors, when assertions are turned
+ * off the "d" variable would be set but never used. Which is
+ * treated as an error by GCC.
+ */
+#ifndef NDEBUG
+ char d;
+#endif
+ stream_unget(&lex->stream, c);
+#ifndef NDEBUG
+ d =
+#endif
+ strbuffer_pop(&lex->saved_text);
+ assert(c == d);
+ }
+}
+
+static void lex_save_cached(lex_t *lex) {
+ while (lex->stream.buffer[lex->stream.buffer_pos] != '\0') {
+ lex_save(lex, lex->stream.buffer[lex->stream.buffer_pos]);
+ lex->stream.buffer_pos++;
+ lex->stream.position++;
+ }
+}
+
+static void lex_free_string(lex_t *lex) {
+ jsonp_free(lex->value.string.val);
+ lex->value.string.val = NULL;
+ lex->value.string.len = 0;
+}
+
+/* assumes that str points to 'u' plus at least 4 valid hex digits */
+static int32_t decode_unicode_escape(const char *str) {
+ int i;
+ int32_t value = 0;
+
+ assert(str[0] == 'u');
+
+ for (i = 1; i <= 4; i++) {
+ char c = str[i];
+ value <<= 4;
+ if (l_isdigit(c))
+ value += c - '0';
+ else if (l_islower(c))
+ value += c - 'a' + 10;
+ else if (l_isupper(c))
+ value += c - 'A' + 10;
+ else
+ return -1;
+ }
+
+ return value;
+}
+
+static void lex_scan_string(lex_t *lex, json_error_t *error) {
+ int c;
+ const char *p;
+ char *t;
+ int i;
+
+ lex->value.string.val = NULL;
+ lex->token = TOKEN_INVALID;
+
+ c = lex_get_save(lex, error);
+
+ while (c != '"') {
+ if (c == STREAM_STATE_ERROR)
+ goto out;
+
+ else if (c == STREAM_STATE_EOF) {
+ error_set(error, lex, json_error_premature_end_of_input,
+ "premature end of input");
+ goto out;
+ }
+
+ else if (0 <= c && c <= 0x1F) {
+ /* control character */
+ lex_unget_unsave(lex, c);
+ if (c == '\n')
+ error_set(error, lex, json_error_invalid_syntax, "unexpected newline");
+ else
+ error_set(error, lex, json_error_invalid_syntax, "control character 0x%x",
+ c);
+ goto out;
+ }
+
+ else if (c == '\\') {
+ c = lex_get_save(lex, error);
+ if (c == 'u') {
+ c = lex_get_save(lex, error);
+ for (i = 0; i < 4; i++) {
+ if (!l_isxdigit(c)) {
+ error_set(error, lex, json_error_invalid_syntax,
+ "invalid escape");
+ goto out;
+ }
+ c = lex_get_save(lex, error);
+ }
+ } else if (c == '"' || c == '\\' || c == '/' || c == 'b' || c == 'f' ||
+ c == 'n' || c == 'r' || c == 't')
+ c = lex_get_save(lex, error);
+ else {
+ error_set(error, lex, json_error_invalid_syntax, "invalid escape");
+ goto out;
+ }
+ } else
+ c = lex_get_save(lex, error);
+ }
+
+ /* the actual value is at most of the same length as the source
+ string, because:
+ - shortcut escapes (e.g. "\t") (length 2) are converted to 1 byte
+ - a single \uXXXX escape (length 6) is converted to at most 3 bytes
+ - two \uXXXX escapes (length 12) forming an UTF-16 surrogate pair
+ are converted to 4 bytes
+ */
+ t = jsonp_malloc(lex->saved_text.length + 1);
+ if (!t) {
+ /* this is not very nice, since TOKEN_INVALID is returned */
+ goto out;
+ }
+ lex->value.string.val = t;
+
+ /* + 1 to skip the " */
+ p = strbuffer_value(&lex->saved_text) + 1;
+
+ while (*p != '"') {
+ if (*p == '\\') {
+ p++;
+ if (*p == 'u') {
+ size_t length;
+ int32_t value;
+
+ value = decode_unicode_escape(p);
+ if (value < 0) {
+ error_set(error, lex, json_error_invalid_syntax,
+ "invalid Unicode escape '%.6s'", p - 1);
+ goto out;
+ }
+ p += 5;
+
+ if (0xD800 <= value && value <= 0xDBFF) {
+ /* surrogate pair */
+ if (*p == '\\' && *(p + 1) == 'u') {
+ int32_t value2 = decode_unicode_escape(++p);
+ if (value2 < 0) {
+ error_set(error, lex, json_error_invalid_syntax,
+ "invalid Unicode escape '%.6s'", p - 1);
+ goto out;
+ }
+ p += 5;
+
+ if (0xDC00 <= value2 && value2 <= 0xDFFF) {
+ /* valid second surrogate */
+ value =
+ ((value - 0xD800) << 10) + (value2 - 0xDC00) + 0x10000;
+ } else {
+ /* invalid second surrogate */
+ error_set(error, lex, json_error_invalid_syntax,
+ "invalid Unicode '\\u%04X\\u%04X'", value, value2);
+ goto out;
+ }
+ } else {
+ /* no second surrogate */
+ error_set(error, lex, json_error_invalid_syntax,
+ "invalid Unicode '\\u%04X'", value);
+ goto out;
+ }
+ } else if (0xDC00 <= value && value <= 0xDFFF) {
+ error_set(error, lex, json_error_invalid_syntax,
+ "invalid Unicode '\\u%04X'", value);
+ goto out;
+ }
+
+ if (utf8_encode(value, t, &length))
+ assert(0);
+ t += length;
+ } else {
+ switch (*p) {
+ case '"':
+ case '\\':
+ case '/':
+ *t = *p;
+ break;
+ case 'b':
+ *t = '\b';
+ break;
+ case 'f':
+ *t = '\f';
+ break;
+ case 'n':
+ *t = '\n';
+ break;
+ case 'r':
+ *t = '\r';
+ break;
+ case 't':
+ *t = '\t';
+ break;
+ default:
+ assert(0);
+ }
+ t++;
+ p++;
+ }
+ } else
+ *(t++) = *(p++);
+ }
+ *t = '\0';
+ lex->value.string.len = t - lex->value.string.val;
+ lex->token = TOKEN_STRING;
+ return;
+
+out:
+ lex_free_string(lex);
+}
+
+#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */
+#if JSON_INTEGER_IS_LONG_LONG
+#ifdef _MSC_VER /* Microsoft Visual Studio */
+#define json_strtoint _strtoi64
+#else
+#define json_strtoint strtoll
+#endif
+#else
+#define json_strtoint strtol
+#endif
+#endif
+
+static int lex_scan_number(lex_t *lex, int c, json_error_t *error) {
+ const char *saved_text;
+ char *end;
+ double doubleval;
+
+ lex->token = TOKEN_INVALID;
+
+ if (c == '-')
+ c = lex_get_save(lex, error);
+
+ if (c == '0') {
+ c = lex_get_save(lex, error);
+ if (l_isdigit(c)) {
+ lex_unget_unsave(lex, c);
+ goto out;
+ }
+ } else if (l_isdigit(c)) {
+ do
+ c = lex_get_save(lex, error);
+ while (l_isdigit(c));
+ } else {
+ lex_unget_unsave(lex, c);
+ goto out;
+ }
+
+ if (!(lex->flags & JSON_DECODE_INT_AS_REAL) && c != '.' && c != 'E' && c != 'e') {
+ json_int_t intval;
+
+ lex_unget_unsave(lex, c);
+
+ saved_text = strbuffer_value(&lex->saved_text);
+
+ errno = 0;
+ intval = json_strtoint(saved_text, &end, 10);
+ if (errno == ERANGE) {
+ if (intval < 0)
+ error_set(error, lex, json_error_numeric_overflow,
+ "too big negative integer");
+ else
+ error_set(error, lex, json_error_numeric_overflow, "too big integer");
+ goto out;
+ }
+
+ assert(end == saved_text + lex->saved_text.length);
+
+ lex->token = TOKEN_INTEGER;
+ lex->value.integer = intval;
+ return 0;
+ }
+
+ if (c == '.') {
+ c = lex_get(lex, error);
+ if (!l_isdigit(c)) {
+ lex_unget(lex, c);
+ goto out;
+ }
+ lex_save(lex, c);
+
+ do
+ c = lex_get_save(lex, error);
+ while (l_isdigit(c));
+ }
+
+ if (c == 'E' || c == 'e') {
+ c = lex_get_save(lex, error);
+ if (c == '+' || c == '-')
+ c = lex_get_save(lex, error);
+
+ if (!l_isdigit(c)) {
+ lex_unget_unsave(lex, c);
+ goto out;
+ }
+
+ do
+ c = lex_get_save(lex, error);
+ while (l_isdigit(c));
+ }
+
+ lex_unget_unsave(lex, c);
+
+ if (jsonp_strtod(&lex->saved_text, &doubleval)) {
+ error_set(error, lex, json_error_numeric_overflow, "real number overflow");
+ goto out;
+ }
+
+ lex->token = TOKEN_REAL;
+ lex->value.real = doubleval;
+ return 0;
+
+out:
+ return -1;
+}
+
+static int lex_scan(lex_t *lex, json_error_t *error) {
+ int c;
+
+ strbuffer_clear(&lex->saved_text);
+
+ if (lex->token == TOKEN_STRING)
+ lex_free_string(lex);
+
+ do
+ c = lex_get(lex, error);
+ while (c == ' ' || c == '\t' || c == '\n' || c == '\r');
+
+ if (c == STREAM_STATE_EOF) {
+ lex->token = TOKEN_EOF;
+ goto out;
+ }
+
+ if (c == STREAM_STATE_ERROR) {
+ lex->token = TOKEN_INVALID;
+ goto out;
+ }
+
+ lex_save(lex, c);
+
+ if (c == '{' || c == '}' || c == '[' || c == ']' || c == ':' || c == ',')
+ lex->token = c;
+
+ else if (c == '"')
+ lex_scan_string(lex, error);
+
+ else if (l_isdigit(c) || c == '-') {
+ if (lex_scan_number(lex, c, error))
+ goto out;
+ }
+
+ else if (l_isalpha(c)) {
+ /* eat up the whole identifier for clearer error messages */
+ const char *saved_text;
+
+ do
+ c = lex_get_save(lex, error);
+ while (l_isalpha(c));
+ lex_unget_unsave(lex, c);
+
+ saved_text = strbuffer_value(&lex->saved_text);
+
+ if (strcmp(saved_text, "true") == 0)
+ lex->token = TOKEN_TRUE;
+ else if (strcmp(saved_text, "false") == 0)
+ lex->token = TOKEN_FALSE;
+ else if (strcmp(saved_text, "null") == 0)
+ lex->token = TOKEN_NULL;
+ else
+ lex->token = TOKEN_INVALID;
+ }
+
+ else {
+ /* save the rest of the input UTF-8 sequence to get an error
+ message of valid UTF-8 */
+ lex_save_cached(lex);
+ lex->token = TOKEN_INVALID;
+ }
+
+out:
+ return lex->token;
+}
+
+static char *lex_steal_string(lex_t *lex, size_t *out_len) {
+ char *result = NULL;
+ if (lex->token == TOKEN_STRING) {
+ result = lex->value.string.val;
+ *out_len = lex->value.string.len;
+ lex->value.string.val = NULL;
+ lex->value.string.len = 0;
+ }
+ return result;
+}
+
+static int lex_init(lex_t *lex, get_func get, size_t flags, void *data) {
+ stream_init(&lex->stream, get, data);
+ if (strbuffer_init(&lex->saved_text))
+ return -1;
+
+ lex->flags = flags;
+ lex->token = TOKEN_INVALID;
+ return 0;
+}
+
+static void lex_close(lex_t *lex) {
+ if (lex->token == TOKEN_STRING)
+ lex_free_string(lex);
+ strbuffer_close(&lex->saved_text);
+}
+
+/*** parser ***/
+
+static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error);
+
+static json_t *parse_object(lex_t *lex, size_t flags, json_error_t *error) {
+ json_t *object = json_object();
+ if (!object)
+ return NULL;
+
+ lex_scan(lex, error);
+ if (lex->token == '}')
+ return object;
+
+ while (1) {
+ char *key;
+ size_t len;
+ json_t *value;
+
+ if (lex->token != TOKEN_STRING) {
+ error_set(error, lex, json_error_invalid_syntax, "string or '}' expected");
+ goto error;
+ }
+
+ key = lex_steal_string(lex, &len);
+ if (!key)
+ return NULL;
+ if (memchr(key, '\0', len)) {
+ jsonp_free(key);
+ error_set(error, lex, json_error_null_byte_in_key,
+ "NUL byte in object key not supported");
+ goto error;
+ }
+
+ if (flags & JSON_REJECT_DUPLICATES) {
+ if (json_object_get(object, key)) {
+ jsonp_free(key);
+ error_set(error, lex, json_error_duplicate_key, "duplicate object key");
+ goto error;
+ }
+ }
+
+ lex_scan(lex, error);
+ if (lex->token != ':') {
+ jsonp_free(key);
+ error_set(error, lex, json_error_invalid_syntax, "':' expected");
+ goto error;
+ }
+
+ lex_scan(lex, error);
+ value = parse_value(lex, flags, error);
+ if (!value) {
+ jsonp_free(key);
+ goto error;
+ }
+
+ if (json_object_set_new_nocheck(object, key, value)) {
+ jsonp_free(key);
+ goto error;
+ }
+
+ jsonp_free(key);
+
+ lex_scan(lex, error);
+ if (lex->token != ',')
+ break;
+
+ lex_scan(lex, error);
+ }
+
+ if (lex->token != '}') {
+ error_set(error, lex, json_error_invalid_syntax, "'}' expected");
+ goto error;
+ }
+
+ return object;
+
+error:
+ json_decref(object);
+ return NULL;
+}
+
+static json_t *parse_array(lex_t *lex, size_t flags, json_error_t *error) {
+ json_t *array = json_array();
+ if (!array)
+ return NULL;
+
+ lex_scan(lex, error);
+ if (lex->token == ']')
+ return array;
+
+ while (lex->token) {
+ json_t *elem = parse_value(lex, flags, error);
+ if (!elem)
+ goto error;
+
+ if (json_array_append_new(array, elem)) {
+ goto error;
+ }
+
+ lex_scan(lex, error);
+ if (lex->token != ',')
+ break;
+
+ lex_scan(lex, error);
+ }
+
+ if (lex->token != ']') {
+ error_set(error, lex, json_error_invalid_syntax, "']' expected");
+ goto error;
+ }
+
+ return array;
+
+error:
+ json_decref(array);
+ return NULL;
+}
+
+static json_t *parse_value(lex_t *lex, size_t flags, json_error_t *error) {
+ json_t *json;
+
+ lex->depth++;
+ if (lex->depth > JSON_PARSER_MAX_DEPTH) {
+ error_set(error, lex, json_error_stack_overflow, "maximum parsing depth reached");
+ return NULL;
+ }
+
+ switch (lex->token) {
+ case TOKEN_STRING: {
+ const char *value = lex->value.string.val;
+ size_t len = lex->value.string.len;
+
+ if (!(flags & JSON_ALLOW_NUL)) {
+ if (memchr(value, '\0', len)) {
+ error_set(error, lex, json_error_null_character,
+ "\\u0000 is not allowed without JSON_ALLOW_NUL");
+ return NULL;
+ }
+ }
+
+ json = jsonp_stringn_nocheck_own(value, len);
+ lex->value.string.val = NULL;
+ lex->value.string.len = 0;
+ break;
+ }
+
+ case TOKEN_INTEGER: {
+ json = json_integer(lex->value.integer);
+ break;
+ }
+
+ case TOKEN_REAL: {
+ json = json_real(lex->value.real);
+ break;
+ }
+
+ case TOKEN_TRUE:
+ json = json_true();
+ break;
+
+ case TOKEN_FALSE:
+ json = json_false();
+ break;
+
+ case TOKEN_NULL:
+ json = json_null();
+ break;
+
+ case '{':
+ json = parse_object(lex, flags, error);
+ break;
+
+ case '[':
+ json = parse_array(lex, flags, error);
+ break;
+
+ case TOKEN_INVALID:
+ error_set(error, lex, json_error_invalid_syntax, "invalid token");
+ return NULL;
+
+ default:
+ error_set(error, lex, json_error_invalid_syntax, "unexpected token");
+ return NULL;
+ }
+
+ if (!json)
+ return NULL;
+
+ lex->depth--;
+ return json;
+}
+
+static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *error) {
+ json_t *result;
+
+ lex->depth = 0;
+
+ lex_scan(lex, error);
+ if (!(flags & JSON_DECODE_ANY)) {
+ if (lex->token != '[' && lex->token != '{') {
+ error_set(error, lex, json_error_invalid_syntax, "'[' or '{' expected");
+ return NULL;
+ }
+ }
+
+ result = parse_value(lex, flags, error);
+ if (!result)
+ return NULL;
+
+ if (!(flags & JSON_DISABLE_EOF_CHECK)) {
+ lex_scan(lex, error);
+ if (lex->token != TOKEN_EOF) {
+ error_set(error, lex, json_error_end_of_input_expected,
+ "end of file expected");
+ json_decref(result);
+ return NULL;
+ }
+ }
+
+ if (error) {
+ /* Save the position even though there was no error */
+ error->position = (int)lex->stream.position;
+ }
+
+ return result;
+}
+
+typedef struct {
+ const char *data;
+ size_t pos;
+} string_data_t;
+
+static int string_get(void *data) {
+ char c;
+ string_data_t *stream = (string_data_t *)data;
+ c = stream->data[stream->pos];
+ if (c == '\0')
+ return EOF;
+ else {
+ stream->pos++;
+ return (unsigned char)c;
+ }
+}
+
+json_t *json_loads(const char *string, size_t flags, json_error_t *error) {
+ lex_t lex;
+ json_t *result;
+ string_data_t stream_data;
+
+ jsonp_error_init(error, "<string>");
+
+ if (string == NULL) {
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
+ return NULL;
+ }
+
+ stream_data.data = string;
+ stream_data.pos = 0;
+
+ if (lex_init(&lex, string_get, flags, (void *)&stream_data))
+ return NULL;
+
+ result = parse_json(&lex, flags, error);
+
+ lex_close(&lex);
+ return result;
+}
+
+typedef struct {
+ const char *data;
+ size_t len;
+ size_t pos;
+} buffer_data_t;
+
+static int buffer_get(void *data) {
+ char c;
+ buffer_data_t *stream = data;
+ if (stream->pos >= stream->len)
+ return EOF;
+
+ c = stream->data[stream->pos];
+ stream->pos++;
+ return (unsigned char)c;
+}
+
+json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error) {
+ lex_t lex;
+ json_t *result;
+ buffer_data_t stream_data;
+
+ jsonp_error_init(error, "<buffer>");
+
+ if (buffer == NULL) {
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
+ return NULL;
+ }
+
+ stream_data.data = buffer;
+ stream_data.pos = 0;
+ stream_data.len = buflen;
+
+ if (lex_init(&lex, buffer_get, flags, (void *)&stream_data))
+ return NULL;
+
+ result = parse_json(&lex, flags, error);
+
+ lex_close(&lex);
+ return result;
+}
+
+json_t *json_loadf(FILE *input, size_t flags, json_error_t *error) {
+ lex_t lex;
+ const char *source;
+ json_t *result;
+#ifdef HAVE_UNISTD_H
+ if (input == stdin)
+ source = "<stdin>";
+ else
+#endif
+ source = "<stream>";
+
+ jsonp_error_init(error, source);
+
+ if (input == NULL) {
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
+ return NULL;
+ }
+
+ if (lex_init(&lex, (get_func)fgetc, flags, input))
+ return NULL;
+
+ result = parse_json(&lex, flags, error);
+
+ lex_close(&lex);
+ return result;
+}
+
+static int fd_get_func(int *fd) {
+#ifdef HAVE_UNISTD_H
+ uint8_t c;
+ if (read(*fd, &c, 1) == 1)
+ return c;
+#endif
+ return EOF;
+}
+
+json_t *json_loadfd(int input, size_t flags, json_error_t *error) {
+ lex_t lex;
+ const char *source;
+ json_t *result;
+
+#ifdef HAVE_UNISTD_H
+ if (input == STDIN_FILENO)
+ source = "<stdin>";
+ else
+#endif
+ source = "<stream>";
+
+ jsonp_error_init(error, source);
+
+ if (input < 0) {
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
+ return NULL;
+ }
+
+ if (lex_init(&lex, (get_func)fd_get_func, flags, &input))
+ return NULL;
+
+ result = parse_json(&lex, flags, error);
+
+ lex_close(&lex);
+ return result;
+}
+
+json_t *json_load_file(const char *path, size_t flags, json_error_t *error) {
+ json_t *result;
+ FILE *fp;
+
+ jsonp_error_init(error, path);
+
+ if (path == NULL) {
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
+ return NULL;
+ }
+
+ fp = fopen(path, "rb");
+ if (!fp) {
+ error_set(error, NULL, json_error_cannot_open_file, "unable to open %s: %s", path,
+ strerror(errno));
+ return NULL;
+ }
+
+ result = json_loadf(fp, flags, error);
+
+ fclose(fp);
+ return result;
+}
+
+#define MAX_BUF_LEN 1024
+
+typedef struct {
+ char data[MAX_BUF_LEN];
+ size_t len;
+ size_t pos;
+ json_load_callback_t callback;
+ void *arg;
+} callback_data_t;
+
+static int callback_get(void *data) {
+ char c;
+ callback_data_t *stream = data;
+
+ if (stream->pos >= stream->len) {
+ stream->pos = 0;
+ stream->len = stream->callback(stream->data, MAX_BUF_LEN, stream->arg);
+ if (stream->len == 0 || stream->len == (size_t)-1)
+ return EOF;
+ }
+
+ c = stream->data[stream->pos];
+ stream->pos++;
+ return (unsigned char)c;
+}
+
+json_t *json_load_callback(json_load_callback_t callback, void *arg, size_t flags,
+ json_error_t *error) {
+ lex_t lex;
+ json_t *result;
+
+ callback_data_t stream_data;
+
+ memset(&stream_data, 0, sizeof(stream_data));
+ stream_data.callback = callback;
+ stream_data.arg = arg;
+
+ jsonp_error_init(error, "<callback>");
+
+ if (callback == NULL) {
+ error_set(error, NULL, json_error_invalid_argument, "wrong arguments");
+ return NULL;
+ }
+
+ if (lex_init(&lex, (get_func)callback_get, flags, &stream_data))
+ return NULL;
+
+ result = parse_json(&lex, flags, error);
+
+ lex_close(&lex);
+ return result;
+}
diff --git a/RedfishPkg/RedfishPkg.ci.yaml b/RedfishPkg/RedfishPkg.ci.yaml
index e410d1a608..fde6fa89bc 100644
--- a/RedfishPkg/RedfishPkg.ci.yaml
+++ b/RedfishPkg/RedfishPkg.ci.yaml
@@ -35,7 +35,13 @@
"PrivateInclude/Crt/string.h",
"PrivateInclude/Crt/time.h",
"PrivateInclude/Library/RedfishCrtLib.h",
- "PrivateLibrary/RedfishCrtLib/RedfishCrtLib.c"
+ "PrivateLibrary/RedfishCrtLib/RedfishCrtLib.c",
+ ##
+ ## For jansson library open source
+ ## load.c is overrided from open source.
+ "Library/JsonLib/load.c",
+ "Library/JsonLib/jansson_config.h",
+ "Library/JsonLib/jansson_private_config.h"
]
},
"CompilerPlugin": {
diff --git a/RedfishPkg/RedfishPkg.dec b/RedfishPkg/RedfishPkg.dec
index 9f8b85778d..2985676b60 100644
--- a/RedfishPkg/RedfishPkg.dec
+++ b/RedfishPkg/RedfishPkg.dec
@@ -19,6 +19,13 @@
[Includes.Common.Private]
PrivateInclude # Private header files for C RTL.
PrivateInclude/Crt # Private header files for C RTL.
+ Library/JsonLib # Private header files for jansson
+ # configuration files.
+ # - jansson_config.h
+ # - jansson_private_config.h
+ # jansson.h refers to above two configuration
+ # files for building platform jansson library.
+ Library/JsonLib/jansson/src # For referring to jannson.h
[LibraryClasses]
## @libraryclass Platform Redfish Host Interface Library
@@ -38,6 +45,11 @@
# This library is only intended to be used by UEFI network stack modules.
RestExLib|Include/Library/RestExLib.h
+ ## @libraryclass Provides the library functions based on third party
+ # jansson library to manipulate JSON data structure.
+ #
+ JsonLib|Include/Library/JsonLib.h
+
[LibraryClasses.Common.Private]
## @libraryclass Provides the private C runtime library functions.
# CRT library is currently used by edk2 JsonLib (open source