From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-002e3701.pphosted.com (mx0b-002e3701.pphosted.com [148.163.143.35]) by mx.groups.io with SMTP id smtpd.web12.35767.1606142026047926006 for ; Mon, 23 Nov 2020 06:33:46 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@hpe.com header.s=pps0720 header.b=CpBpxPIr; spf=pass (domain: hpe.com, ip: 148.163.143.35, mailfrom: prvs=0596f266c1=abner.chang@hpe.com) Received: from pps.filterd (m0150245.ppops.net [127.0.0.1]) by mx0b-002e3701.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0ANENWKQ001613; Mon, 23 Nov 2020 14:33:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hpe.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-type : content-transfer-encoding : mime-version; s=pps0720; bh=/qzRgwYP1leYHgLZTY1kPGX0OX3USIfe2ZOwI4wHnhU=; b=CpBpxPIrV9FlNZhOIKqCqG5d6AD/ZItk3w0pR3ZbZNpWxdpoYQxKC078e0JOx4Jpht8u ZAwfpT9744mdI7a6b0hw+O9GjCEluFVwAsXCfEIp2dLiqfK6DOExM1Oty5Trd8CVNk0n eWscqXcGi/MczTf7E9AP+RMjAAwO6BkexidhE6lPEBAXDxx+eq0esSglHKfYoHh51m/S O/TO8snnhXd7Kdt5AdzP2ViH4IuMlkJeG9mBBKp2MXtQcyJ90mIXvmT46FngNv+Kw47o AYoq88I43kz4YTibxS+DQAI5zqXEJ2qBnUSaqHM9J0ZSBRc/rDB3QOlMLRGHThuA7+G1 /w== Received: from g9t5008.houston.hpe.com (g9t5008.houston.hpe.com [15.241.48.72]) by mx0b-002e3701.pphosted.com with ESMTP id 34xv3dxdtq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 23 Nov 2020 14:33:44 +0000 Received: from g9t2301.houston.hpecorp.net (g9t2301.houston.hpecorp.net [16.220.97.129]) by g9t5008.houston.hpe.com (Postfix) with ESMTP id 1E53253; Mon, 23 Nov 2020 14:33:44 +0000 (UTC) Received: from abner-virtual-machine.asiapacific.hpqcorp.net (abner-virtual-machine.asiapacific.hpqcorp.net [15.119.210.153]) by g9t2301.houston.hpecorp.net (Postfix) with ESMTP id 85AF14D; Mon, 23 Nov 2020 14:33:42 +0000 (UTC) From: "Abner Chang" To: devel@edk2.groups.io Cc: Jian J Wang , Hao A Wu , Nickle Wang , Peter O'Hanley Subject: [PATCH 2/3] MdeModulePkg/library: EDK2 port jansson library Date: Mon, 23 Nov 2020 21:47:21 +0800 Message-Id: <20201123134722.15980-3-abner.chang@hpe.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201123134722.15980-1-abner.chang@hpe.com> References: <20201123134722.15980-1-abner.chang@hpe.com> X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-HPE-SCL: -1 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312,18.0.737 definitions=2020-11-23_11:2020-11-23,2020-11-23 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 mlxlogscore=999 priorityscore=1501 mlxscore=0 suspectscore=0 clxscore=1015 impostorscore=0 lowpriorityscore=0 adultscore=0 malwarescore=0 bulkscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011230098 X-MIME-Autoconverted: from 8bit to quoted-printable by mx0b-002e3701.pphosted.com id 0ANENWKQ001613 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable This is the edk2 port of open source jansson library. https://github.com/akheron/jansson Signed-off-by: Abner Chang Cc: Jian J Wang Cc: Hao A Wu Cc: Nickle Wang Cc: Peter O'Hanley --- MdeModulePkg/Include/Library/JsonLib.h | 596 +++++++++++++ .../Library/JsonLib/JanssonCrtLibSupport.c | 705 +++++++++++++++ .../Library/JsonLib/JanssonCrtLibSupport.h | 198 +++++ MdeModulePkg/Library/JsonLib/JsonLib.c | 805 ++++++++++++++++++ MdeModulePkg/Library/JsonLib/JsonLib.inf | 81 ++ MdeModulePkg/Library/JsonLib/JsonUtilities.c | 417 +++++++++ MdeModulePkg/Library/JsonLib/JsonUtilities.h | 69 ++ MdeModulePkg/Library/JsonLib/assert.h | 11 + MdeModulePkg/Library/JsonLib/errno.h | 11 + MdeModulePkg/Library/JsonLib/jansson_config.h | 57 ++ .../Library/JsonLib/jansson_private_config.h | 19 + MdeModulePkg/Library/JsonLib/limits.h | 11 + MdeModulePkg/Library/JsonLib/math.h | 11 + MdeModulePkg/Library/JsonLib/stdarg.h | 11 + MdeModulePkg/Library/JsonLib/stddef.h | 11 + MdeModulePkg/Library/JsonLib/stdio.h | 11 + MdeModulePkg/Library/JsonLib/stdlib.h | 11 + MdeModulePkg/Library/JsonLib/string.h | 11 + MdeModulePkg/Library/JsonLib/sys/time.h | 11 + MdeModulePkg/Library/JsonLib/sys/types.h | 10 + MdeModulePkg/Library/JsonLib/time.h | 11 + 21 files changed, 3078 insertions(+) create mode 100644 MdeModulePkg/Include/Library/JsonLib.h create mode 100644 MdeModulePkg/Library/JsonLib/JanssonCrtLibSupport.c create mode 100644 MdeModulePkg/Library/JsonLib/JanssonCrtLibSupport.h create mode 100644 MdeModulePkg/Library/JsonLib/JsonLib.c create mode 100644 MdeModulePkg/Library/JsonLib/JsonLib.inf create mode 100644 MdeModulePkg/Library/JsonLib/JsonUtilities.c create mode 100644 MdeModulePkg/Library/JsonLib/JsonUtilities.h create mode 100644 MdeModulePkg/Library/JsonLib/assert.h create mode 100644 MdeModulePkg/Library/JsonLib/errno.h create mode 100644 MdeModulePkg/Library/JsonLib/jansson_config.h create mode 100644 MdeModulePkg/Library/JsonLib/jansson_private_config.h create mode 100644 MdeModulePkg/Library/JsonLib/limits.h create mode 100644 MdeModulePkg/Library/JsonLib/math.h create mode 100644 MdeModulePkg/Library/JsonLib/stdarg.h create mode 100644 MdeModulePkg/Library/JsonLib/stddef.h create mode 100644 MdeModulePkg/Library/JsonLib/stdio.h create mode 100644 MdeModulePkg/Library/JsonLib/stdlib.h create mode 100644 MdeModulePkg/Library/JsonLib/string.h create mode 100644 MdeModulePkg/Library/JsonLib/sys/time.h create mode 100644 MdeModulePkg/Library/JsonLib/sys/types.h create mode 100644 MdeModulePkg/Library/JsonLib/time.h diff --git a/MdeModulePkg/Include/Library/JsonLib.h b/MdeModulePkg/Includ= e/Library/JsonLib.h new file mode 100644 index 0000000000..b140a8723c --- /dev/null +++ b/MdeModulePkg/Include/Library/JsonLib.h @@ -0,0 +1,596 @@ +/** @file + APIs for JSON operations. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#ifndef BASE_JSON_LIB_H_ +#define BASE_JSON_LIB_H_ + +#include + +#include +#include + +typedef VOID* EDKII_JSON_VALUE; +typedef VOID* EDKII_JSON_ARRAY; +typedef VOID* EDKII_JSON_OBJECT; + +#define EDKII_JSON_ARRAY_FOREACH(Array, Index, Value) \ + for(Index =3D 0; \ + Index < JsonArrayCount(Array) && (Value =3D JsonArrayGetValue(Array,= Index)); \ + Index++) + +/** + The function is used to convert a NULL terminated UTF8 encoded string = to a JSON + value. Only object and array represented strings can be converted succ= essfully, + since they are the only valid root values of a JSON text for UEFI usag= e. + + Real number and number with exponent part are not supportted by UEFI. + + Caller needs to cleanup the root value by calling JsonValueFree(). + + @param[in] Text The NULL terminated UTF8 encoded string = to convert + + @retval Array JSON value or object JSON value, or NULL when any e= rror occurs. + +**/ +EDKII_JSON_VALUE +EFIAPI +TextToJson ( + IN CHAR8* Text + ); + +/** + The function is used to convert the JSON root value to a UTF8 encoded = string which + is terminated by NULL, or return NULL on error. + + Only array JSON value or object JSON value is valid for converting, an= d caller is + responsible for free converted string. + + @param[in] Json The JSON value to be converted + + @retval The JSON value converted UTF8 string or NULL. + +**/ +CHAR8* +EFIAPI +JsonToText ( + IN EDKII_JSON_VALUE Json + ); + +/** + The function is used to initialize a JSON value which contains a new J= SON array, + or NULL on error. Initially, the array is empty. + + The reference count of this value will be set to 1, and caller needs t= o cleanup the + value by calling JsonValueFree(). + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @retval The created JSON value which contains a JSON array or NUL= L. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonValueInitArray ( + VOID + ); + +/** + The function is used to initialize a JSON value which contains a new J= SON object, + or NULL on error. Initially, the object is empty. + + The reference count of this value will be set to 1, and caller needs t= o cleanup the + value by calling JsonValueFree(). + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @retval The created JSON value which contains a JSON object or NU= LL. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonValueInitObject ( + VOID + ); + +/** + The function is used to initialize a JSON value which contains a new J= SON string, + or NULL on error. + + The input string must be NULL terminated Ascii format, non-Ascii chara= cters will + be processed as an error. Unicode characters can also be represented b= y 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 t= o cleanup the + value by calling JsonValueFree(). + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @param[in] String The Ascii string to initialize to JSON value + + @retval The created JSON value which contains a JSON string or NU= LL. Select a + Getter API for a specific encoding format. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonValueInitAsciiString ( + IN CHAR8 *String + ); + +/** + The function is used to initialize a JSON value which contains a new J= SON 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 t= o cleanup the + value by calling JsonValueFree(). + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @param[in] String The Unicode string to initialize to JSON valu= e + + @retval The created JSON value which contains a JSON string or NU= LL. 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 J= SON integer, + or NULL on error. + + The reference count of this value will be set to 1, and caller needs t= o cleanup the + value by calling JsonValueFree(). + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @param[in] Value The integer to initialize to JSON value + + @retval The created JSON value which contains a JSON number or NU= LL. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonValueInitNumber ( + IN INT64 Value + ); + +/** + The function is used to initialize a JSON value which contains a new J= SON boolean, + or NULL on error. + + Boolean JSON value is kept as static value, and no need to do any clea= nup work. + + @param[in] Value The boolean value to initialize. + + @retval The created JSON value which contains a JSON boolean or N= ULL. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonValueInitBoolean ( + IN BOOLEAN Value + ); + +/** + The function is used to initialize a JSON value which contains a new J= SON 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 b= y 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 v= alue won't be destroyed + since they are static values kept in memory. + + Reference Count Strategy: BaseJsonLib uses this strategy to track whet= her a value is still + in use or not. When a value is created, it=C2=92s reference count is s= et to 1. If a reference + to a value is kept for use, its reference count is incremented, and wh= en 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. + + @param[in] Json The JSON value to be freed. + +**/ +VOID +EFIAPI +JsonValueFree ( + IN EDKII_JSON_VALUE Json + ); + +/** + The function is used to create a fresh copy of a JSON value, and all c= hild values are deep + copied in a recursive fashion. It should be called when this JSON valu= e might be modified + in later use, but the original still wants to be used in somewhere els= e. + + 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 man= y calls may cause some + performance issues, user should avoid unnecessary calls to this functi= on 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 J= SON 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 ar= ray. + +**/ +BOOLEAN +EFIAPI +JsonValueIsArray ( + IN EDKII_JSON_VALUE Json + ); + +/** + The function is used to return if the provided JSON value contains a J= SON 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 ob= ject. + +**/ +BOOLEAN +EFIAPI +JsonValueIsObject ( + IN EDKII_JSON_VALUE Json + ); + +/** + The function is used to return if the provided JSON Value contains a s= tring, 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 st= ring. + +**/ +BOOLEAN +EFIAPI +JsonValueIsString ( + IN EDKII_JSON_VALUE Json + ); + +/** + The function is used to return if the provided JSON value contains a J= SON 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 nu= mber. + +**/ +BOOLEAN +EFIAPI +JsonValueIsNumber ( + IN EDKII_JSON_VALUE Json + ); + +/** + The function is used to return if the provided JSON value contains a J= SON 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 bo= olean. + +**/ +BOOLEAN +EFIAPI +JsonValueIsBoolean ( + IN EDKII_JSON_VALUE Json + ); + +/** + The function is used to return if the provided JSON value contains a J= SON 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 NU= LL. + +**/ +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 ty= pe 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 stri= ng 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. + +**/ +CHAR8* +EFIAPI +JsonValueGetAsciiString ( + IN EDKII_JSON_VALUE Json + ); + +/** + The function is used to retrieve the associated Unicode string in a st= ring 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. + + @param[in] Json The provided JSON value. + + @retval Return the associated Unicode string in JSON value or NUL= L. + +**/ +CHAR16* +EFIAPI +JsonValueGetUnicodeString ( + IN EDKII_JSON_VALUE Json + ); + +/** + The function is used to retrieve the associated integer in a number ty= pe JSON value. + + The input JSON value should not be NULL or contain no JSON number, oth= erwise 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 t= ype JSON value. + + The input JSON value should not be NULL or contain no JSON boolean, ot= herwise 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 get the number of elements in a JSON object, o= r 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 refference= . But contained keys + are read only and must not be modified or freed. + + @param[in] JsonObj The provided JSON object for enume= ration. + @param[out] KeyCount The count of keys in this JSON obj= ect. + + @retval Return an array of the enumerated keys in this JSON objec= t or NULL. + +**/ +CHAR8** +JsonObjectGetKeys ( + IN EDKII_JSON_OBJECT JsonObj, + OUT UINTN *KeyCount + ); + +/** + The function is used to get a JSON value corresponding to the input ke= y from a JSON object. + + It only returns a reference to this value and any changes on this valu= e will impact the + original JSON object. If that is not expected, please call JsonValueCl= one() to clone it to + use. + + Input key must be a valid NULL terminated UTF8 encoded string. NULL wi= ll 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 retriev= ed. + + @retval Return the corresponding JSON value to key, or NULL on er= ror. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonObjectGetValue ( + IN EDKII_JSON_OBJECT JsonObj, + IN CHAR8 *Key + ); + +/** + The function is used to set a JSON value corresponding to the input ke= y 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 ther= e already is a value for + this key, this key will be assigned to the new JSON value. The old JSO= N 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 descrip= tion for JsonValueFree(). + + @param[in] JsonObj The provided JSON object. + @param[in] Key The key of the JSON value to be se= t. + @param[in] Json The JSON value to set to this JSON= object mapped by key. + + @retval EFI_ABORTED Some error occur and operation abo= rted. + @retval EFI_SUCCESS The JSON value has been set to thi= s JSON object. + +**/ +EFI_STATUS +EFIAPI +JsonObjectSetValue ( + IN EDKII_JSON_OBJECT JsonObj, + IN CHAR8 *Key, + IN EDKII_JSON_VALUE Json + ); + +/** + The function is used to get the number of elements in a JSON array, or= 0 if it 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 valu= e will impact the + original JSON object. If that is not expected, please call JsonValueCl= one() 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 NU= LL. + +**/ +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 arr= ay, 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 descrip= tion 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 abo= rted. + @retval EFI_SUCCESS JSON value has been appended to th= e 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, shiftin= g the elements after index + one position towards the start of the array. The reference count of th= is value will be decreased + by 1. + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @param[in] JsonArray The provided JSON array. + @param[in] Index The Index position before removeme= nt. + + @retval EFI_ABORTED Some error occur and operation abo= rted. + @retval EFI_SUCCESS The JSON array has been removed at= position index. + +**/ +EFI_STATUS +EFIAPI +JsonArrayRemoveValue ( + IN EDKII_JSON_ARRAY JsonArray, + IN UINTN Index + ); + +#endif diff --git a/MdeModulePkg/Library/JsonLib/JanssonCrtLibSupport.c b/MdeMod= ulePkg/Library/JsonLib/JanssonCrtLibSupport.c new file mode 100644 index 0000000000..907634a05b --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/JanssonCrtLibSupport.c @@ -0,0 +1,705 @@ +/** @file + CRT wrapper functions for Jansson system call. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include + +int errno =3D 0; + +/** + Determine if a particular character is an alphanumeric character + @return Returns 1 if c is an alphanumeric character, otherwise return= s 0. +**/ +int isalnum (int c) +{ + // + // ::=3D [0-9] | [a-z] | [A-Z] + // + return ((('0' <=3D (c)) && ((c) <=3D '9')) || + (('a' <=3D (c)) && ((c) <=3D 'z')) || + (('A' <=3D (c)) && ((c) <=3D 'Z'))); +} + +/** + Determine if a particular character is a digital character + + @return Returns 1 if c is an digital character, otherwise returns 0. +**/ +int isdchar (int c) +{ + // + // [0-9] | [e +-.] + // + return ((('0' <=3D (c)) && ((c) <=3D '9')) || + (c =3D=3D 'e') || (c =3D=3D 'E') || + (c =3D=3D '+') || (c =3D=3D '-') || + (c =3D=3D '.')); +} + +/** + Determine if a particular character is a space character + + @return Returns 1 if c is a space character +**/ +int isspace (int c) +{ + // + // ::=3D [ ] + // + return ((c) =3D=3D ' ') || ((c) =3D=3D '\t') || ((c) =3D=3D '\r') || (= (c) =3D=3D '\n') || ((c) =3D=3D '\v') || ((c) =3D=3D '\f'); +} + +/** + Allocates memory blocks +*/ +void *malloc (size_t size) +{ + return AllocatePool ((UINTN) size); +} + +/** + De-allocates or frees a memory block +*/ +void free (void *ptr) +{ + // + // In Standard C, free() handles a null pointer argument transparently= . This + // is not true of FreePool() below, so protect it. + // + if (ptr !=3D NULL) { + FreePool (ptr); + } +} + +/** + NetBSD Compatibility Function strdup creates a duplicate copy of a str= ing. + + @return Returns the pointer to duplicated string. +**/ +char * strdup(const char *str) +{ + size_t len; + char *copy; + + len =3D strlen(str) + 1; + if ((copy =3D malloc(len)) =3D=3D NULL) + return (NULL); + memcpy(copy, str, len); + return (copy); +} + +/** The toupper function converts a lowercase letter to a corresponding + uppercase letter. + + @param[in] c The character to be converted. + + @return If the argument is a character for which islower is true a= nd + there are one or more corresponding characters, as specifi= ed by + the current locale, for which isupper is true, the toupper + function returns one of the corresponding characters (alwa= ys the + same one for any given locale); otherwise, the argument is + returned unchanged. +**/ +int +toupper( + IN int c + ) +{ + if ( (c >=3D 'a') && (c <=3D 'z') ) { + c =3D c - ('a' - 'A'); + } + return c; +} + +/** + Digit to a value. + + @return Returns the value of digit. +**/ +int +Digit2Val( int c) +{ + if (((c >=3D 'a') && (c <=3D 'z')) || ((c >=3D 'A') && (c <=3D 'Z'))) = { /* If c is one of [A-Za-z]... */ + c =3D toupper(c) - 7; // Adjust so 'A' is ('9' + 1) + } + return c - '0'; // Value returned is between 0 and 35, inclusive. +} + + +/** The strtoll function converts the initial portion of the string poin= ted to + by nptr to long long int representation. + + See the description for strtol for more information. + + @return The strtoll function returns the converted value, if any. If= no + conversion could be performed, zero is returned. If the corr= ect + value is outside the range of representable values, LLONG_MI= N or + LLONG_MAX is returned (according to the sign of the value, i= f any), + and the value of the macro ERANGE is stored in errno. +**/ +long long +strtoll(const char * nptr, char ** endptr, int base) +{ + const char *pEnd; + long long Result =3D 0; + long long Previous; + int temp; + BOOLEAN Negative =3D FALSE; + + pEnd =3D nptr; + + if((base < 0) || (base =3D=3D 1) || (base > 36)) { + if(endptr !=3D NULL) { + *endptr =3D NULL; + } + return 0; + } + // Skip leading spaces. + while(isspace(*nptr)) ++nptr; + + // Process Subject sequence: optional sign followed by digits. + if(*nptr =3D=3D '+') { + Negative =3D FALSE; + ++nptr; + } + else if(*nptr =3D=3D '-') { + Negative =3D TRUE; + ++nptr; + } + + if(*nptr =3D=3D '0') { /* Might be Octal or Hex */ + if(toupper(nptr[1]) =3D=3D 'X') { /* Looks like Hex */ + if((base =3D=3D 0) || (base =3D=3D 16)) { + nptr +=3D 2; /* Skip the "0X" */ + base =3D 16; /* In case base was 0 */ + } + } + else { /* Looks like Octal */ + if((base =3D=3D 0) || (base =3D=3D 8)) { + ++nptr; /* Skip the leading "0" */ + base =3D 8; /* In case base was 0 */ + } + } + } + if(base =3D=3D 0) { /* If still zero then must be decimal */ + base =3D 10; + } + if(*nptr =3D=3D '0') { + for( ; *nptr =3D=3D '0'; ++nptr); /* Skip any remaining leading zer= os */ + pEnd =3D nptr; + } + + while( isalnum(*nptr) && ((temp =3D Digit2Val(*nptr)) < base)) { + Previous =3D Result; + Result =3D MultS64x64 (Result, base) + (long long int)temp; + if( Result <=3D Previous) { // Detect Overflow + if(Negative) { + Result =3D LLONG_MIN; + } + else { + Result =3D LLONG_MAX; + } + Negative =3D FALSE; + errno =3D ERANGE; + break; + } + pEnd =3D ++nptr; + } + if(Negative) { + Result =3D -Result; + } + + // Save pointer to final sequence + if(endptr !=3D NULL) { + *endptr =3D (char *)pEnd; + } + return Result; +} + +/** The strtol, strtoll, strtoul, and strtoull functions convert the ini= tial + portion of the string pointed to by nptr to long int, long long int, + unsigned long int, and unsigned long long int representation, respec= tively. + First, they decompose the input string into three parts: an initial, + possibly empty, sequence of white-space characters (as specified by = the + isspace function), a subject sequence resembling an integer represen= ted in + some radix determined by the value of base, and a final string of on= e or + more unrecognized characters, including the terminating null charact= er of + the input string. Then, they attempt to convert the subject sequence= to an + integer, and return the result. + + If the value of base is zero, the expected form of the subject seque= nce is + that of an integer constant, optionally preceded + by a plus or minus sign, but not including an integer suffix. If the= value + of base is between 2 and 36 (inclusive), the expected form of the su= bject + sequence is a sequence of letters and digits representing an integer= with + the radix specified by base, optionally preceded by a plus or minus = sign, + but not including an integer suffix. The letters from a (or A) throu= gh z + (or Z) are ascribed the values 10 through 35; only letters and digit= s whose + ascribed values are less than that of base are permitted. If the val= ue of + base is 16, the characters 0x or 0X may optionally precede the seque= nce of + letters and digits, following the sign if present. + + The subject sequence is defined as the longest initial subsequence o= f the + input string, starting with the first non-white-space character, tha= t is of + the expected form. The subject sequence contains no characters if th= e input + string is empty or consists entirely of white space, or if the first + non-white-space character is other than a sign or a permissible lett= er or digit. + + If the subject sequence has the expected form and the value of base = is + zero, the sequence of characters starting with the first digit is + interpreted as an integer constant. If the subject sequence has the + expected form and the value of base is between 2 and 36, it is used = as the + base for conversion, ascribing to each letter its value as given abo= ve. If + the subject sequence begins with a minus sign, the value resulting f= rom the + conversion is negated (in the return type). A pointer to the final s= tring + is stored in the object pointed to by endptr, provided that endptr i= s + not a null pointer. + + In other than the "C" locale, additional locale-specific subject seq= uence + forms may be accepted. + + If the subject sequence is empty or does not have the expected form,= no + conversion is performed; the value of nptr is stored in the object p= ointed + to by endptr, provided that endptr is not a null pointer. + + @return The strtol, strtoll, strtoul, and strtoull functions return = the + converted value, if any. If no conversion could be performed= , zero + is returned. If the correct value is outside the range of + representable values, LONG_MIN, LONG_MAX, LLONG_MIN, LLONG_M= AX, + ULONG_MAX, or ULLONG_MAX is returned (according to the retur= n type + and sign of the value, if any), and the value of the macro E= RANGE + is stored in errno. +**/ +long +strtol(const char * nptr, char ** endptr, int base) +{ + const char *pEnd; + long Result =3D 0; + long Previous; + int temp; + BOOLEAN Negative =3D FALSE; + + pEnd =3D nptr; + + if((base < 0) || (base =3D=3D 1) || (base > 36)) { + if(endptr !=3D NULL) { + *endptr =3D NULL; + } + return 0; + } + // Skip leading spaces. + while(isspace(*nptr)) ++nptr; + + // Process Subject sequence: optional sign followed by digits. + if(*nptr =3D=3D '+') { + Negative =3D FALSE; + ++nptr; + } + else if(*nptr =3D=3D '-') { + Negative =3D TRUE; + ++nptr; + } + + if(*nptr =3D=3D '0') { /* Might be Octal or Hex */ + if(toupper(nptr[1]) =3D=3D 'X') { /* Looks like Hex */ + if((base =3D=3D 0) || (base =3D=3D 16)) { + nptr +=3D 2; /* Skip the "0X" */ + base =3D 16; /* In case base was 0 */ + } + } + else { /* Looks like Octal */ + if((base =3D=3D 0) || (base =3D=3D 8)) { + ++nptr; /* Skip the leading "0" */ + base =3D 8; /* In case base was 0 */ + } + } + } + if(base =3D=3D 0) { /* If still zero then must be decimal */ + base =3D 10; + } + if(*nptr =3D=3D '0') { + for( ; *nptr =3D=3D '0'; ++nptr); /* Skip any remaining leading zer= os */ + pEnd =3D nptr; + } + + while( isalnum(*nptr) && ((temp =3D Digit2Val(*nptr)) < base)) { + Previous =3D Result; + Result =3D (Result * base) + (long int)temp; + if( Result <=3D Previous) { // Detect Overflow + if(Negative) { + Result =3D LONG_MIN; + } + else { + Result =3D LONG_MAX; + } + Negative =3D FALSE; + errno =3D ERANGE; + break; + } + pEnd =3D ++nptr; + } + if(Negative) { + Result =3D -Result; + } + + // Save pointer to final sequence + if(endptr !=3D NULL) { + *endptr =3D (char *)pEnd; + } + return Result; +} + +/** The strtoull function converts the initial portion of the string poi= nted to + by nptr to unsigned long long int representation. + + See the description for strtol for more information. + + @return The strtoull function returns the converted value, if any. I= f no + conversion could be performed, zero is returned. If the corr= ect + value is outside the range of representable values, ULLONG_M= AX is + returned and the value of the macro ERANGE is stored in errn= o. +**/ +unsigned long long +strtoull(const char * nptr, char ** endptr, int base) +{ + const char *pEnd; + unsigned long long Result =3D 0; + unsigned long long Previous; + int temp; + + pEnd =3D nptr; + + if((base < 0) || (base =3D=3D 1) || (base > 36)) { + if(endptr !=3D NULL) { + *endptr =3D NULL; + } + return 0; + } + // Skip leading spaces. + while(isspace(*nptr)) ++nptr; + + // Process Subject sequence: optional + sign followed by digits. + if(*nptr =3D=3D '+') { + ++nptr; + } + + if(*nptr =3D=3D '0') { /* Might be Octal or Hex */ + if(toupper(nptr[1]) =3D=3D 'X') { /* Looks like Hex */ + if((base =3D=3D 0) || (base =3D=3D 16)) { + nptr +=3D 2; /* Skip the "0X" */ + base =3D 16; /* In case base was 0 */ + } + } + else { /* Looks like Octal */ + if((base =3D=3D 0) || (base =3D=3D 8)) { + ++nptr; /* Skip the leading "0" */ + base =3D 8; /* In case base was 0 */ + } + } + } + if(base =3D=3D 0) { /* If still zero then must be decimal */ + base =3D 10; + } + if(*nptr =3D=3D '0') { + for( ; *nptr =3D=3D '0'; ++nptr); /* Skip any remaining leading zer= os */ + pEnd =3D nptr; + } + + while( isalnum(*nptr) && ((temp =3D Digit2Val(*nptr)) < base)) { + Previous =3D Result; + Result =3D DivU64x32 (Result, base) + (unsigned long long)temp; + if( Result < Previous) { // If we overflowed + Result =3D ULLONG_MAX; + errno =3D ERANGE; + break; + } + pEnd =3D ++nptr; + } + + // Save pointer to final sequence + if(endptr !=3D NULL) { + *endptr =3D (char *)pEnd; + } + return Result; +} + +/** + edk2 Jansson port does not support doubles, simply return 0. + + These conversion functions convert the initial portion of the string + pointed to by nptr to double, float, and long double representation, + respectively. + + The strtod(), strtof(), and strtold() functions return the converted + value, if any. + + If endptr is not NULL, a pointer to the character after the last chara= c- + ter used in the conversion is stored in the location referenced by + endptr. + + If no conversion is performed, zero is returned and the value of nptr = is + stored in the location referenced by endptr. + + If the correct value would cause overflow, plus or minus HUGE_VAL, + HUGE_VALF, or HUGE_VALL is returned (according to the sign and type of + the return value), and ERANGE is stored in errno. If the correct valu= e + would cause underflow, zero is returned and ERANGE is stored in errno. + + @return Return 0. +**/ +double +strtod (const char * __restrict nptr, char ** __restrict endptr) { + if(endptr) + *endptr =3D (char *)(nptr + strlen(nptr)); + + return (double)0; +} + +/** + Allocate and zero-initialize array. +**/ +void * +calloc(size_t Num, size_t Size) +{ + void *RetVal; + size_t NumSize; + + NumSize =3D Num * Size; + RetVal =3D NULL; + if (NumSize !=3D 0) { + RetVal =3D malloc(NumSize); + if( RetVal !=3D NULL) { + (VOID)ZeroMem( RetVal, NumSize); + } + } + DEBUG((DEBUG_POOL, "0x%p =3D calloc(%d, %d)\n", RetVal, Num, Size)); + + return RetVal; +} + +// +// The arrays give the cumulative number of days up to the first of the +// month number used as the index (1 -> 12) for regular and leap years. +// The value at index 13 is for the whole year. +// +UINTN CumulativeDays[2][14] =3D { + { + 0, + 0, + 31, + 31 + 28, + 31 + 28 + 31, + 31 + 28 + 31 + 30, + 31 + 28 + 31 + 30 + 31, + 31 + 28 + 31 + 30 + 31 + 30, + 31 + 28 + 31 + 30 + 31 + 30 + 31, + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, + 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + }, + { + 0, + 0, + 31, + 31 + 29, + 31 + 29 + 31, + 31 + 29 + 31 + 30, + 31 + 29 + 31 + 30 + 31, + 31 + 29 + 31 + 30 + 31 + 30, + 31 + 29 + 31 + 30 + 31 + 30 + 31, + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31, + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30, + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, + 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 + } +}; + +#define IsLeap(y) (((y) % 4) =3D=3D 0 && (((y) % 100) !=3D 0 || ((y) %= 400) =3D=3D 0)) +#define SECSPERMIN (60) +#define SECSPERHOUR (60 * 60) +#define SECSPERDAY (24 * SECSPERHOUR) + +/** + Get the system time as seconds elapsed since midnight, January 1, 1970. +**/ +time_t time (time_t *timer) +{ + EFI_TIME Time; + time_t CalTime; + UINTN Year; + + // + // Get the current time and date information + // + gRT->GetTime (&Time, NULL); + + // + // Years Handling + // UTime should now be set to 00:00:00 on Jan 1 of the current year. + // + for (Year =3D 1970, CalTime =3D 0; Year !=3D Time.Year; Year++) { + CalTime =3D CalTime + (time_t)(CumulativeDays[IsLeap(Year)][13] * SE= CSPERDAY); + } + + // + // Add in number of seconds for current Month, Day, Hour, Minute, Seco= nds, and TimeZone adjustment + // + CalTime =3D CalTime + + (time_t)((Time.TimeZone !=3D EFI_UNSPECIFIED_TIMEZONE) ? (Ti= me.TimeZone * 60) : 0) + + (time_t)(CumulativeDays[IsLeap(Time.Year)][Time.Month] * SEC= SPERDAY) + + (time_t)(((Time.Day > 0) ? Time.Day - 1 : 0) * SECSPERDAY) + + (time_t)(Time.Hour * SECSPERHOUR) + + (time_t)(Time.Minute * 60) + + (time_t)Time.Second; + + if (timer !=3D NULL) { + *timer =3D CalTime; + } + + return CalTime; +} + +typedef +int +(*SORT_COMPARE)( + IN VOID *Buffer1, + IN VOID *Buffer2 + ); + +/** + Duplicated from EDKII BaseSortLib for qsort() wrapper + @param[in, out] BufferToSort on call a Buffer of (possibly sorted) e= lements + on return a buffer of sorted elements + @param[in] Count the number of elements in the buffer to= sort + @param[in] ElementSize Size of an element in bytes + @param[in] CompareFunction The function to call to perform the com= parison + of any 2 elements + @param[in] Buffer Buffer of size ElementSize for use in s= wapping +**/ +STATIC +VOID +QuickSortWorker ( + IN OUT VOID *BufferToSort, + IN CONST UINTN Count, + IN CONST UINTN ElementSize, + IN SORT_COMPARE CompareFunction, + IN VOID *Buffer + ) +{ + VOID *Pivot; + UINTN LoopCount; + UINTN NextSwapLocation; + + ASSERT(BufferToSort !=3D NULL); + ASSERT(CompareFunction !=3D NULL); + ASSERT(Buffer !=3D NULL); + + if (Count < 2 || ElementSize < 1) { + return; + } + + NextSwapLocation =3D 0; + + // + // Pick a pivot (we choose last element) + // + Pivot =3D ((UINT8 *)BufferToSort + ((Count - 1) * ElementSize)); + + // + // Now get the pivot such that all on "left" are below it + // and everything "right" are above it + // + for (LoopCount =3D 0; LoopCount < Count - 1; LoopCount++) + { + // + // If the element is less than the pivot + // + if (CompareFunction ((VOID *)((UINT8 *)BufferToSort + ((LoopCount) *= ElementSize)), Pivot) <=3D 0) { + // + // Swap + // + CopyMem (Buffer, (UINT8 *)BufferToSort + (NextSwapLocation * Eleme= ntSize), ElementSize); + CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize),= (UINT8 *)BufferToSort + ((LoopCount) * ElementSize), ElementSize); + CopyMem ((UINT8 *)BufferToSort + ((LoopCount) * ElementSize), Buff= er, ElementSize); + + // + // Increment NextSwapLocation + // + NextSwapLocation++; + } + } + // + // Swap pivot to it's final position (NextSwapLocaiton) + // + CopyMem (Buffer, Pivot, ElementSize); + CopyMem (Pivot, (UINT8 *)BufferToSort + (NextSwapLocation * ElementSiz= e), ElementSize); + CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * ElementSize), Buf= fer, ElementSize); + + // + // Now recurse on 2 paritial lists. Neither of these will have the 'p= ivot' element. + // IE list is sorted left half, pivot element, sorted right half... + // + QuickSortWorker ( + BufferToSort, + NextSwapLocation, + ElementSize, + CompareFunction, + Buffer + ); + + QuickSortWorker ( + (UINT8 *)BufferToSort + (NextSwapLocation + 1) * ElementSize, + Count - NextSwapLocation - 1, + ElementSize, + CompareFunction, + Buffer + ); + + return; +} + +/** + Performs a quick sort +**/ +void qsort (void *base, size_t num, size_t width, int (*compare)(const v= oid *, const void *)) +{ + VOID *Buffer; + + ASSERT (base !=3D NULL); + ASSERT (compare !=3D NULL); + + // + // Use CRT-style malloc to cover BS and RT memory allocation. + // + Buffer =3D malloc (width); + ASSERT (Buffer !=3D NULL); + + // + // Re-use PerformQuickSort() function Implementation in EDKII BaseSort= Lib. + // + QuickSortWorker (base, (UINTN)num, (UINTN)width, (SORT_COMPARE)compare= , Buffer); + + free (Buffer); + return; +} + +/** + Get character from stream, we don't support file operastion on edk2 JS= ON library. + + @return Returns the character currently pointed by the internal file p= osition indicator of the specified stream + +**/ +int fgetc(FILE * _File){ + return 0; +} diff --git a/MdeModulePkg/Library/JsonLib/JanssonCrtLibSupport.h b/MdeMod= ulePkg/Library/JsonLib/JanssonCrtLibSupport.h new file mode 100644 index 0000000000..f17ee6397f --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/JanssonCrtLibSupport.h @@ -0,0 +1,198 @@ +/** @file + CRT wrapper head functions for jansson system call. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef JANSSON_CRT_LIB_SUPPORT_H_ +#define JANSSON_CRT_LIB_SUPPORT_H_ + +#include +#include +#include +#include + +#define MAX_STRING_SIZE 0x10000000 + +// Minimum value for an object of type long long int. +#define LLONG_MIN (-9223372036854775807LL - 1LL) // -(2^63 - 1) + +// Maximum value for an object of type long long int. +#define LLONG_MAX 9223372036854775807LL // 2^63 - 1 + +// We dont support double on edk2 +#define HUGE_VAL 0 + +#if defined(MDE_CPU_X64) || defined(MDE_CPU_AARCH64) || defined(MDE_CPU_= IA64) +// +// With GCC we would normally use SIXTY_FOUR_BIT_LONG, but MSVC needs +// SIXTY_FOUR_BIT, because 'long' is 32-bit and only 'long long' is +// 64-bit. Since using 'long long' works fine on GCC too, just do that. +// +#define SIXTY_FOUR_BIT +#elif defined(MDE_CPU_IA32) || defined(MDE_CPU_ARM) || defined(MDE_CPU_E= BC) +#define THIRTY_TWO_BIT +#else +#error Unknown target architecture +#endif + +// +// Map all va_xxxx elements to VA_xxx defined in MdePkg/Include/Base.h +// +#if !defined(__CC_ARM) // if va_list is not already defined +#define va_list VA_LIST +#define va_arg VA_ARG +#define va_start VA_START +#define va_end VA_END +#else // __CC_ARM +#define va_start(Marker, Parameter) __va_start(Marker, Parameter) +#define va_arg(Marker, TYPE) __va_arg(Marker, TYPE) +#define va_end(Marker) ((void)0) +#endif + +// +// Definitions for global constants used by CRT library routines +// +#define EINVAL 22 /* Invalid argument */ +#define INT_MAX 0x7FFFFFFF /* Maximum (signed) int value */ +#define LONG_MAX 0X7FFFFFFFL /* max value for a long */ +#define LONG_MIN (-LONG_MAX-1) /* min value for a long */ +#define ULONG_MAX 0xFFFFFFFF /* Maximum unsigned long value */ +#define CHAR_BIT 8 /* Number of bits in a char */ + +// Maximum value for an object of type unsigned long long int. +#define ULLONG_MAX 0xFFFFFFFFFFFFFFFFULL // 2^64 - 1 +// Maximum value for an object of type unsigned char. +#define UCHAR_MAX 255 // 2^8 - 1 + +// +// Basic types mapping +// +typedef UINTN size_t; +typedef INTN ssize_t; +typedef INT32 time_t; +typedef UINT8 __uint8_t; +typedef UINT8 sa_family_t; +typedef UINT32 uid_t; +typedef UINT32 gid_t; +typedef INT32 int32_t; +typedef UINT32 uint32_t; +typedef UINT16 uint16_t; +typedef UINT8 uint8_t; +typedef enum {false, true} bool; + +// +// File operations are not required for EFI building, +// so FILE is mapped to VOID * to pass build +// +typedef VOID *FILE; + +// +// Global variables +// +extern int errno; +extern FILE *stderr; + +// +// Function prototypes of CRT Library routines +// +void *malloc (size_t); +void *realloc (void *, size_t); +void *calloc (size_t Num, size_t Size); +void free (void *); +void *memset (void *, int, size_t); +int memcmp (const void *, const void *, size_t); +int isdigit (int); +int isspace (int); +int tolower (int); +int isupper (int); +int isxdigit (int); +int isalnum (int); +void *memcpy (void *, const void *, size_t); +void *memset (void *, int, size_t); +void *memchr (const void *, int, size_t); +int memcmp (const void *, const void *, size_t); +void *memmove (void *, const void *, size_t); +int strcmp (const char *, const char *); +int strncmp (const char *, const char *, size_t); +char *strcpy (char *, const char *); +size_t strlen (const char *); +char *strcat (char *, const char *); +char *strchr (const char *, int); +int strcasecmp (const char *, const char *); +int strncasecmp (const char *, const char *, size_t); +char *strncpy (char *, size_t, const char *, size_t); +int strncmp (const char *, const char *, size_t); +char *strrchr (const char *, int); +unsigned long strtoul (const char *, char **, int); +char * strstr (const char *s1 , const char *s2); +long strtol (const char *, char **, int); +char *strerror (int); +size_t strspn (const char *, const char *); +char * strdup (const char *str); +char * strpbrk (const char *s1, const char *s2); +unsigned long long strtoull(const char * nptr, char ** endptr, int base)= ; +long long strtoll (const char * nptr, char ** endptr, int base)= ; +long strtol (const char * nptr, char ** endptr, int base)= ; +double strtod (const char * __restrict nptr, char ** __rest= rict endptr); +size_t strcspn (const char *, const char *); +int printf (const char *, ...); +int sscanf (const char *, const char *, ...); +FILE *fopen (const char *, const char *); +size_t fread (void *, size_t, size_t, FILE *); +size_t fwrite (const void *, size_t, size_t, FILE *); +int fclose (FILE *); +int fprintf (FILE *, const char *, ...); +int fgetc (FILE * _File); +uid_t getuid (void); +uid_t geteuid (void); +gid_t getgid (void); +gid_t getegid (void); +void qsort (void *, size_t, size_t, int (*)(const void *= , const void *)); +char *getenv (const char *); +#if defined(__GNUC__) && (__GNUC__ >=3D 2) +void abort (void) __attribute__((__noreturn__)); +#else +void abort (void); +#endif +int toupper (int); +int Digit2Val (int); +time_t time (time_t *); + +// +// Macros that directly map functions to BaseLib, BaseMemoryLib, and Deb= ugLib functions +// +#define strcmp AsciiStrCmp +#define memcpy(dest,source,count) CopyMem(dest,source,(UINTN)(co= unt)) +#define memset(dest,ch,count) SetMem(dest,(UINTN)(count),(UI= NT8)(ch)) +#define memchr(buf,ch,count) ScanMem8(buf,(UINTN)(count),(U= INT8)ch) +#define memcmp(buf1,buf2,count) (int)(CompareMem(buf1,buf2,(UI= NTN)(count))) +#define memmove(dest,source,count) CopyMem(dest,source,(UINTN)(co= unt)) +#define strlen(str) (size_t)(AsciiStrnLenS(str,MAX= _STRING_SIZE)) +#define strcpy(strDest,strSource) AsciiStrCpyS(strDest,(strlen(s= trSource)+1),strSource) +#define strncpy(strDest,strSource,count) AsciiStrnCpyS(strDest,(UINTN)c= ount,strSource,(UINTN)count) +#define strncpys(strDest, DestLen, strSource,count) AsciiStrnCpyS(strDe= st,DestLen,strSource,(UINTN)count) +#define strcat(strDest,strSource) AsciiStrCatS(strDest,(strlen(s= trSource)+strlen(strDest)+1),strSource) +#define strchr(str,ch) ScanMem8((VOID *)(str),AsciiSt= rSize(str),(UINT8)ch) +#define strncmp(string1,string2,count) (int)(AsciiStrnCmp(string1,str= ing2,(UINTN)(count))) +#define strcasecmp(str1,str2) (int)AsciiStriCmp(str1,str2) +#define strstr(s1,s2) AsciiStrStr(s1,s2) +#define sprintf(buf,...) AsciiSPrint(buf,MAX_STRING_SIZ= E,__VA_ARGS__) +#define snprintf(buf,len,...) AsciiSPrint(buf,len,__VA_ARGS_= _) +#define vsnprintf(buf,len,format,marker) AsciiVSPrint((buf),(len),(form= at),(marker)) +#define assert(expression) +#define atoi(nptr) AsciiStrDecimalToUintn(nptr) +#define fabs(x) (((x)<0.0)?(-x):(x)) +#define offsetof(type,member) OFFSET_OF(type,member) + +#define EOF (-1) + +extern int errno; + +#define ERANGE 34 /* 34 Result too large */ + +#endif diff --git a/MdeModulePkg/Library/JsonLib/JsonLib.c b/MdeModulePkg/Librar= y/JsonLib/JsonLib.c new file mode 100644 index 0000000000..8cf276c892 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/JsonLib.c @@ -0,0 +1,805 @@ +/** @file + APIs for JSON operations. + + Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include +#include "JsonUtilities.h" + +/** + The function is used to convert a NULL terminated UTF8 encoded string = to a JSON + value. Only object and array represented strings can be converted succ= essfully, + since they are the only valid root values of a JSON text for UEFI usag= e. + + Real number and number with exponent part are not supportted by UEFI. + + Caller needs to cleanup the root value by calling JsonValueFree(). + + @param[in] Text The NULL terminated UTF8 encoded string = to convert + + @retval Array JSON value or object JSON value, or NULL when any e= rror occurs. + +**/ +EDKII_JSON_VALUE +EFIAPI +TextToJson ( + IN CHAR8* Text + ) +{ + json_error_t JsonError; + + return (EDKII_JSON_VALUE) json_loads (Text, 0, &JsonError); +} + +/** + The function is used to convert the JSON root value to a UTF8 encoded = string which + is terminated by NULL, or return NULL on error. + + Only array JSON value or object JSON value is valid for converting, an= d caller is + responsible for free converted string. + + @param[in] Json The JSON value to be converted + + @retval The JSON value converted UTF8 string or NULL. + +**/ +CHAR8* +EFIAPI +JsonToText ( + IN EDKII_JSON_VALUE Json + ) +{ + if (!JsonValueIsArray (Json) && !JsonValueIsObject (Json)) { + return NULL; + } + + return json_dumps ((json_t *) Json, 0); +} + +/** + The function is used to initialize a JSON value which contains a new J= SON array, + or NULL on error. Initially, the array is empty. + + The reference count of this value will be set to 1, and caller needs t= o cleanup the + value by calling JsonValueFree(). + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @retval The created JSON value which contains a JSON array or NUL= L. + +**/ +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 J= SON object, + or NULL on error. Initially, the object is empty. + + The reference count of this value will be set to 1, and caller needs t= o cleanup the + value by calling JsonValueFree(). + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @retval The created JSON value which contains a JSON object or NU= LL. + +**/ +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 J= SON string, + or NULL on error. + + The input string must be NULL terminated Ascii format, non-Ascii chara= cters will + be processed as an error. Unicode characters can also be represented b= y 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 t= o cleanup the + value by calling JsonValueFree(). + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @param[in] String The Ascii string to initialize to JSON value + + @retval The created JSON value which contains a JSON string or NU= LL. Select a + Getter API for a specific encoding format. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonValueInitAsciiString ( + IN CHAR8 *String + ) +{ + UINTN Index; + + if (String =3D=3D NULL) { + return NULL; + } + + Index =3D 0; + while (*(String + Index) !=3D '\0') { + if (((*(String + Index)) & 0x80) !=3D 0x00) { + return NULL; + } + + Index ++; + } + + return (EDKII_JSON_VALUE) json_string (String); +} + +/** + The function is used to initialize a JSON value which contains a new J= SON 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 t= o cleanup the + value by calling JsonValueFree(). + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @param[in] String The Unicode string to initialize to JSON valu= e + + @retval The created JSON value which contains a JSON string or NU= LL. Select a + Getter API for a specific encoding format. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonValueInitUnicodeString ( + IN CHAR16 *String + ) +{ + EFI_STATUS Status; + CHAR8 *Utf8Str; + + if (String =3D=3D NULL) { + return NULL; + } + + Utf8Str =3D NULL; + Status =3D 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 J= SON integer, + or NULL on error. + + The reference count of this value will be set to 1, and caller needs t= o cleanup the + value by calling JsonValueFree(). + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @param[in] Value The integer to initialize to JSON value + + @retval The created JSON value which contains a JSON number or NU= LL. + +**/ +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 J= SON boolean, + or NULL on error. + + Boolean JSON value is kept as static value, and no need to do any clea= nup work. + + @param[in] Value The boolean value to initialize. + + @retval The created JSON value which contains a JSON boolean or N= ULL. + +**/ +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 J= SON 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 b= y 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 v= alue won't be destroyed + since they are static values kept in memory. + + Reference Count Strategy: BaseJsonLib uses this strategy to track whet= her a value is still + in use or not. When a value is created, it=C2=92s reference count is s= et to 1. If a reference + to a value is kept for use, its reference count is incremented, and wh= en 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 outp= ut 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 c= hild values are deep + copied in a recursive fashion. It should be called when this JSON valu= e might be modified + in later use, but the original still wants to be used in somewhere els= e. + + 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 man= y calls may cause some + performance issues, user should avoid unnecessary calls to this functi= on 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 J= SON 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 ar= ray. + +**/ +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 J= SON 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 ob= ject. + +**/ +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 s= tring, 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 st= ring. + +**/ +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 J= SON 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 nu= mber. + +**/ +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 J= SON 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 bo= olean. + +**/ +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 J= SON 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 NU= LL. + +**/ +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 =3D=3D NULL || !JsonValueIsArray (Json)) { + return NULL; + } + + return (EDKII_JSON_ARRAY) Json; +} + +/** + The function is used to retrieve the associated object in an object ty= pe 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 =3D=3D NULL || !JsonValueIsObject (Json)) { + return NULL; + } + + return (EDKII_JSON_OBJECT) Json; +} + +/** + The function is used to retrieve the associated Ascii string in a stri= ng 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. + +**/ +CHAR8* +EFIAPI +JsonValueGetAsciiString ( + IN EDKII_JSON_VALUE Json + ) +{ + CHAR8 *AsciiStr; + UINTN Index; + + AsciiStr =3D json_string_value ((json_t *) Json); + if (AsciiStr =3D=3D NULL) { + return NULL; + } + + Index =3D 0; + while (*(AsciiStr + Index) !=3D '\0') { + if (((*(AsciiStr + Index)) & 0x80) !=3D 0x00) { + return NULL; + } + + Index ++; + } + + return AsciiStr; +} + +/** + The function is used to retrieve the associated Unicode string in a st= ring 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. + + @param[in] Json The provided JSON value. + + @retval Return the associated Unicode string in JSON value or NUL= L. + +**/ +CHAR16* +EFIAPI +JsonValueGetUnicodeString ( + IN EDKII_JSON_VALUE Json + ) +{ + EFI_STATUS Status; + CONST CHAR8 *Utf8Str; + CHAR16 *Ucs2Str; + + Utf8Str =3D json_string_value ((json_t *) Json); + if (Utf8Str =3D=3D NULL) { + return NULL; + } + + Status =3D UTF8StrToUCS2 (Utf8Str, &Ucs2Str); + if (EFI_ERROR (Status)) { + return NULL; + } + + return Ucs2Str; +} + +/** + The function is used to retrieve the associated integer in a number ty= pe JSON value. + + The input JSON value should not be NULL or contain no JSON number, oth= erwise 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 !=3D NULL && JsonValueIsNumber (Json)); + if (Json =3D=3D NULL || !JsonValueIsNumber (Json)) { + return 0; + } + + return json_integer_value ((json_t *) Json); +} + +/** + The function is used to retrieve the associated boolean in a boolean t= ype JSON value. + + The input JSON value should not be NULL or contain no JSON boolean, ot= herwise 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 !=3D NULL && JsonValueIsBoolean (Json)); + if (Json =3D=3D NULL || !JsonValueIsBoolean (Json)) { + return FALSE; + } + + return json_is_true ((json_t *) Json); +} + +/** + The function is used to get the number of elements in a JSON object, o= r 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 refference= . But contained keys + are read only and must not be modified or freed. + + @param[in] JsonObj The provided JSON object for enume= ration. + @param[out] KeyCount The count of keys in this JSON obj= ect. + + @retval Return an array of the enumerated keys in this JSON objec= t or NULL. + +**/ +CHAR8** +JsonObjectGetKeys ( + IN EDKII_JSON_OBJECT JsonObj, + OUT UINTN *KeyCount + ) +{ + + UINTN Index; + CONST CHAR8 **KeyArray; + CONST CHAR8 *Key; + EDKII_JSON_VALUE Value; + + if (JsonObj =3D=3D NULL || KeyCount =3D=3D NULL) { + return NULL; + } + + Index =3D 0; + json_object_foreach(JsonObj, Key, Value) { + Index ++; + } + if (Index =3D=3D 0) { + *KeyCount =3D 0; + return NULL; + } + + *KeyCount =3D Index; + KeyArray =3D (CHAR8 **) AllocateZeroPool (*KeyCount * sizeof (CHAR8 *)= ); + if (KeyArray =3D=3D NULL) { + return NULL; + } + + Key =3D NULL; + Value =3D NULL; + Index =3D 0; + json_object_foreach((json_t *) JsonObj, Key, Value) { + KeyArray[Index] =3D Key; + Index ++; + } + + return KeyArray; +} + +/** + The function is used to get a JSON value corresponding to the input ke= y from a JSON object. + + It only returns a reference to this value and any changes on this valu= e will impact the + original JSON object. If that is not expected, please call JsonValueCl= one() to clone it to + use. + + Input key must be a valid NULL terminated UTF8 encoded string. NULL wi= ll 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 retriev= ed. + + @retval Return the corresponding JSON value to key, or NULL on er= ror. + +**/ +EDKII_JSON_VALUE +EFIAPI +JsonObjectGetValue ( + IN EDKII_JSON_OBJECT JsonObj, + IN CHAR8 *Key + ) +{ + return (EDKII_JSON_VALUE) json_object_get ((json_t *) JsonObj, Key); +} + +/** + The function is used to set a JSON value corresponding to the input ke= y 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 ther= e already is a value for + this key, this key will be assigned to the new JSON value. The old JSO= N 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 descrip= tion for JsonValueFree(). + + @param[in] JsonObj The provided JSON object. + @param[in] Key The key of the JSON value to be se= t. + @param[in] Json The JSON value to set to this JSON= object mapped by key. + + @retval EFI_ABORTED Some error occur and operation abo= rted. + @retval EFI_SUCCESS The JSON value has been set to thi= s JSON object. + +**/ +EFI_STATUS +EFIAPI +JsonObjectSetValue ( + IN EDKII_JSON_OBJECT JsonObj, + IN CHAR8 *Key, + IN EDKII_JSON_VALUE Json + ) +{ + if (json_object_set ((json_t *) JsonObj, Key, (json_t *) Json) !=3D 0)= { + return EFI_ABORTED; + } else { + return EFI_SUCCESS; + } +} + +/** + The function is used to get the number of elements in a JSON array, or= 0 if it 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 valu= e will impact the + original JSON object. If that is not expected, please call JsonValueCl= one() 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 NU= LL. + +**/ +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 arr= ay, 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 descrip= tion 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 abo= rted. + @retval EFI_SUCCESS JSON value has been appended to th= e 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) !=3D 0) = { + return EFI_ABORTED; + } else { + return EFI_SUCCESS; + } +} + +/** + The function is used to remove a JSON value at position index, shiftin= g the elements after index + one position towards the start of the array. The reference count of th= is value will be decreased + by 1. + + More details for reference count strategy can refer to the API descrip= tion for JsonValueFree(). + + @param[in] JsonArray The provided JSON array. + @param[in] Index The Index position before removeme= nt. + + @retval EFI_ABORTED Some error occur and operation abo= rted. + @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) !=3D 0) { + return EFI_ABORTED; + } else { + return EFI_SUCCESS; + } +} diff --git a/MdeModulePkg/Library/JsonLib/JsonLib.inf b/MdeModulePkg/Libr= ary/JsonLib/JsonLib.inf new file mode 100644 index 0000000000..d411a2ae86 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/JsonLib.inf @@ -0,0 +1,81 @@ +## @file +# Thirty party Jansson library for JSON operations. +# +# Copyright (c) 2019, Intel Corporation. All rights reserved.
+# (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x0001001b + BASE_NAME =3D JanssonLib + FILE_GUID =3D F5E36815-305A-4C5A-9D75-4F2149E4525= 5 + MODULE_TYPE =3D UEFI_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D JanssonLib | DXE_DRIVER DXE_RUNTIME= _DRIVER UEFI_APPLICATION UEFI_DRIVER + +# +# VALID_ARCHITECTURES =3D IA32 X64 IPF EBC +# + +[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/load.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 + JsonUtilities.c + JsonUtilities.h + JanssonCrtLibSupport.c + JanssonCrtLibSupport.h + jansson_config.h + jansson_private_config.h + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[LibraryClasses] + UefiLib + BaseLib + BaseMemoryLib + MemoryAllocationLib + UefiRuntimeServicesTableLib + DebugLib + PrintLib + +[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 + # C4702: unreachable code + # C4706: assignment within conditional expression + # C4456: declaration hides previous local declaration + # C4334: 32-bit shift implicitly converted to 64-bit + # C4204: nonstandard extension used: non-constant aggregate initiali= zer + # C4267: 'var' : conversion from 'size_t' to 'type', possible loss o= f data + # + # Define macro HAVE_CONFIG_H to include jansson_private_config.h to bu= ild. + # Undefined _WIN32 macro + # + MSFT:*_*_*_CC_FLAGS =3D /wd4204 /wd4267 /wd4702 /wd4706 /wd4244 /wd409= 0 /wd4456 /wd4334 /DHAVE_CONFIG_H=3D1 /U_WIN32 /UWIN64 /U_MSC_VER + diff --git a/MdeModulePkg/Library/JsonLib/JsonUtilities.c b/MdeModulePkg/= Library/JsonLib/JsonUtilities.c new file mode 100644 index 0000000000..f84a133219 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/JsonUtilities.c @@ -0,0 +1,417 @@ +/** @file + Utility functions for JSON operations. + + Copyright (c) 2018 - 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "JsonUtilities.h" + +/** + Since each UCS2 character can be represented by 1-3 UTF8 encoded chara= cters, + this function is used to retrieve the UTF8 encoding size for a UCS2 ch= aracter. + + @param[in] Utf8Buffer The buffer for UTF8 encoded data. + + @retval Return the size of UTF8 encoding string or 0 if it is not= for + UCS2 format. + +**/ +UINT8 +GetUTF8SizeForUCS2 ( + IN CHAR8 *Utf8Buffer + ) +{ + CHAR8 TempChar; + UINT8 Utf8Size; + + ASSERT (Utf8Buffer !=3D NULL); + + TempChar =3D *Utf8Buffer; + if ((TempChar & 0xF0) =3D=3D 0xF0) { + + // + // This format is not for UCS2. + // + return 0; + } + + Utf8Size =3D 1; + if ((TempChar & 0x80) =3D=3D 0x80) { + if ((TempChar & 0xC0) =3D=3D 0xC0) { + + Utf8Size ++; + if ((TempChar & 0xE0) =3D=3D 0xE0) { + + Utf8Size ++; + } + } + } + + return Utf8Size; +} + +/** + Since each UCS2 character can be represented by the format: \uXXXX, th= is function + is used to retrieve the UCS2 character from a Unicode format. + Call MUST make sure there are at least 6 Bytes in the input UTF8 buffe= r. + + @param[in] Utf8Buffer The buffer for UTF8 encoded data. + @param[out] Ucs2Char The converted UCS2 character. + + @retval EFI_INVALID_PARAMETER Non-Ascii characters found in the= hexadecimal + digits string, and can't be conve= rted to a UCS2 + character. + @retval EFI_SUCCESS The UCS2 character has been retri= eved. + +**/ +EFI_STATUS +GetUCS2CharByFormat ( + IN CHAR8 *Utf8Buffer, + OUT CHAR16 *Ucs2Char + ) +{ + UINT8 Num1; + UINT8 Num2; + UINT8 Index; + CHAR8 Ucs2CharFormat[JSON_UNICODE_FORMAT_CHAR_SIZE]; /// two Hexa= decimal digits Ascii string, like "3F" + + for (Index =3D 0; Index < 4; Index ++) { + if ((*(Utf8Buffer + 2 + Index) & 0x80) !=3D 0x00) { + return EFI_INVALID_PARAMETER; + } + } + + ZeroMem (Ucs2CharFormat, JSON_UNICODE_FORMAT_CHAR_SIZE); + + // + // Get the First Number, Offset is 2 + // + CopyMem (Ucs2CharFormat, Utf8Buffer + 2, JSON_UNICODE_FORMAT_CHAR_LEN)= ; + Num1 =3D (UINT8) AsciiStrHexToUintn (Ucs2CharFormat); + + // + // Get the Second Number, Offset is 4 + // + CopyMem (Ucs2CharFormat, Utf8Buffer + 4, JSON_UNICODE_FORMAT_CHAR_LEN)= ; + Num2 =3D (UINT8) AsciiStrHexToUintn (Ucs2CharFormat); + + // + // Ucs2Char is Little-Endian + // + *((CHAR8 *) Ucs2Char) =3D Num2; + *(((CHAR8 *) Ucs2Char) + 1) =3D Num1; + + return EFI_SUCCESS; +} + +/** + Convert a UCS2 character to UTF8 encoding string. + + @param[in] Ucs2Char The provided UCS2 character. + @param[out] Utf8Buffer The converted UTF8 encoded data. + + @retval Return the size of UTF8 encoding data for this UCS2 chara= cter. + +**/ +UINT8 +UCS2CharToUTF8 ( + IN CHAR16 Ucs2Char, + OUT CHAR8 *Utf8Buffer + ) +{ + UINT16 Ucs2Number; + + ASSERT (Utf8Buffer !=3D NULL); + + Ucs2Number =3D (UINT16) Ucs2Char; + if (Ucs2Number <=3D 0x007F) { + + // + // UTF8 format: 0xxxxxxx + // + *Utf8Buffer =3D Ucs2Char & 0x7F; + return 1; + + } else if (Ucs2Number >=3D 0x0080 && Ucs2Number <=3D 0x07FF) { + + // + // UTF8 format: 110xxxxx 10xxxxxx + // + *(Utf8Buffer + 1) =3D (Ucs2Char & 0x3F) | 0x80; + *Utf8Buffer =3D ((Ucs2Char >> 6) & 0x1F) | 0xC0; + return 2; + + } else { /// Ucs2Number >=3D 0x0800 && Ucs2Number <=3D 0xFFFF + + // + // UTF8 format: 1110xxxx 10xxxxxx 10xxxxxx + // + *(Utf8Buffer + 2) =3D (Ucs2Char & 0x3F) | 0x80; + *(Utf8Buffer + 1) =3D ((Ucs2Char >> 6) & 0x3F) | 0x80; + *Utf8Buffer =3D ((Ucs2Char >> 12) & 0x0F) | 0xE0; + return 3; + } +} + +/** + Convert a UTF8 encoded data to a UCS2 character. + + @param[in] Utf8Buffer The provided UTF8 encoded data. + @param[out] Ucs2Char The converted UCS2 character. + + @retval EFI_INVALID_PARAMETER The UTF8 encoded string is not va= lid or + not for UCS2 character. + @retval EFI_SUCCESS The converted UCS2 character. + +**/ +EFI_STATUS +UTF8ToUCS2Char ( + IN CHAR8 *Utf8Buffer, + OUT CHAR16 *Ucs2Char + ) +{ + UINT8 Utf8Size; + CHAR8 *Ucs2Buffer; + CHAR8 TempChar1; + CHAR8 TempChar2; + CHAR8 TempChar3; + + ASSERT (Utf8Buffer !=3D NULL && Ucs2Char !=3D NULL); + ZeroMem (Ucs2Char, sizeof (CHAR16)); + Ucs2Buffer =3D (CHAR8 *) Ucs2Char; + + Utf8Size =3D GetUTF8SizeForUCS2 (Utf8Buffer); + switch (Utf8Size) { + + case 1: + + // + // UTF8 format: 0xxxxxxx + // + TempChar1 =3D *Utf8Buffer; + if ((TempChar1 & 0x80) !=3D 0x00) { + return EFI_INVALID_PARAMETER; + } + + *Ucs2Buffer =3D TempChar1; + *(Ucs2Buffer + 1) =3D 0; + break; + + case 2: + + // + // UTF8 format: 110xxxxx 10xxxxxx + // + TempChar1 =3D *Utf8Buffer; + if ((TempChar1 & 0xE0) !=3D 0xC0) { + return EFI_INVALID_PARAMETER; + } + + TempChar2 =3D *(Utf8Buffer + 1); + if ((TempChar2 & 0xC0) !=3D 0x80) { + return EFI_INVALID_PARAMETER; + } + + *Ucs2Buffer =3D (TempChar1 << 6) + (TempChar2 & 0x3F); + *(Ucs2Buffer + 1) =3D (TempChar1 >> 2) & 0x07; + break; + + case 3: + + // + // UTF8 format: 1110xxxx 10xxxxxx 10xxxxxx + // + TempChar1 =3D *Utf8Buffer; + if ((TempChar1 & 0xF0) !=3D 0xE0) { + return EFI_INVALID_PARAMETER; + } + + TempChar2 =3D *(Utf8Buffer + 1); + if ((TempChar2 & 0xC0) !=3D 0x80) { + return EFI_INVALID_PARAMETER; + } + + TempChar3 =3D *(Utf8Buffer + 2); + if ((TempChar3 & 0xC0) !=3D 0x80) { + return EFI_INVALID_PARAMETER; + } + + *Ucs2Buffer =3D (TempChar2 << 6) + (TempChar3 & 0x3F); + *(Ucs2Buffer + 1) =3D (TempChar1 << 4) + ((TempChar2 >> 2) & 0x0F)= ; + + break; + + default: + + return EFI_INVALID_PARAMETER; + } + + return EFI_SUCCESS; +} + +/** + Convert a UCS2 string to a UTF8 encoded string. + + @param[in] Ucs2Str The provided UCS2 string. + @param[out] Utf8StrAddr The converted UTF8 string address= . Caller + is responsible for Free this stri= ng. + + @retval EFI_INVALID_PARAMETER One or more parameters are invali= d. + @retval EFI_OUT_OF_RESOURCES System runs out of resources. + @retval EFI_SUCCESS The UTF8 encoded string has been = converted. + +**/ +EFI_STATUS +UCS2StrToUTF8 ( + IN CHAR16 *Ucs2Str, + OUT CHAR8 **Utf8StrAddr + ) +{ + UINTN Ucs2StrIndex; + UINTN Ucs2StrLength; + CHAR8 *Utf8Str; + UINTN Utf8StrLength; + UINTN Utf8StrIndex; + CHAR8 Utf8Buffer[UTF8_BUFFER_FOR_UCS2_MAX_SIZE]; + UINT8 Utf8BufferSize; + + if (Ucs2Str =3D=3D NULL || Utf8StrAddr =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + Ucs2StrLength =3D StrLen (Ucs2Str); + Utf8StrLength =3D 0; + + for (Ucs2StrIndex =3D 0; Ucs2StrIndex < Ucs2StrLength; Ucs2StrIndex ++= ) { + + ZeroMem (Utf8Buffer, sizeof (Utf8Buffer)); + Utf8BufferSize =3D UCS2CharToUTF8 (Ucs2Str[Ucs2StrIndex], Utf8Buffer= ); + Utf8StrLength +=3D Utf8BufferSize; + } + + Utf8Str =3D AllocateZeroPool (Utf8StrLength + 1); + if (Utf8Str =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Utf8StrIndex =3D 0; + for (Ucs2StrIndex =3D 0; Ucs2StrIndex < Ucs2StrLength; Ucs2StrIndex ++= ) { + + ZeroMem (Utf8Buffer, sizeof (Utf8Buffer)); + Utf8BufferSize =3D UCS2CharToUTF8 (Ucs2Str[Ucs2StrIndex], Utf8Buffer= ); + + CopyMem (Utf8Str + Utf8StrIndex, Utf8Buffer, Utf8BufferSize); + Utf8StrIndex +=3D Utf8BufferSize; + } + + Utf8Str[Utf8StrIndex] =3D '\0'; + *Utf8StrAddr =3D Utf8Str; + + return EFI_SUCCESS; +} + +/** + Convert a UTF8 encoded string to a UCS2 string. + + @param[in] Utf8Str The provided UTF8 encoded string. + @param[out] Ucs2StrAddr The converted UCS2 string address= . Caller + is responsible for Free this stri= ng. + + @retval EFI_INVALID_PARAMETER The UTF8 encoded string is not va= lid to + convert to UCS2 string. + One or more parameters are invali= d. + @retval EFI_OUT_OF_RESOURCES System runs out of resources. + @retval EFI_SUCCESS The UCS2 string has been converte= d. + +**/ +EFI_STATUS +UTF8StrToUCS2 ( + IN CHAR8 *Utf8Str, + OUT CHAR16 **Ucs2StrAddr + ) +{ + EFI_STATUS Status; + UINTN Utf8StrIndex; + UINTN Utf8StrLength; + UINTN Ucs2StrIndex; + UINT8 Utf8BufferSize; + CHAR16 *Ucs2StrTemp; + + if (Utf8Str =3D=3D NULL || Ucs2StrAddr =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // + // It is not an Ascii string, calculate string length. + // + Utf8StrLength =3D 0; + while (*(Utf8Str + Utf8StrLength) !=3D '\0') { + Utf8StrLength ++; + } + + // + // UCS2 string shall not be longer than the UTF8 string. + // + Ucs2StrTemp =3D AllocateZeroPool ((Utf8StrLength + 1) * sizeof (CHAR16= )); + if (Ucs2StrTemp =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Utf8StrIndex =3D 0; + Ucs2StrIndex =3D 0; + while (Utf8Str[Utf8StrIndex] !=3D '\0') { + + if (CompareMem (Utf8Str + Utf8StrIndex, "\\u", 2) =3D=3D 0 && + Utf8StrLength - Utf8StrIndex >=3D JSON_UNICODE_FORMAT_LEN) { + + Status =3D GetUCS2CharByFormat (Utf8Str + Utf8StrIndex, Ucs2StrTem= p + Ucs2StrIndex); + if (!EFI_ERROR (Status)) { + + Utf8StrIndex +=3D JSON_UNICODE_FORMAT_LEN; + Ucs2StrIndex ++; + } else { + + StrCpyS (Ucs2StrTemp + Ucs2StrIndex, 3, L"\\u"); + + Ucs2StrIndex +=3D 2; + Utf8StrIndex +=3D 2; + } + } else { + + Utf8BufferSize =3D GetUTF8SizeForUCS2 (Utf8Str + Utf8StrIndex); + if (Utf8BufferSize =3D=3D 0 || Utf8StrLength - Utf8StrIndex < Utf8= BufferSize) { + + FreePool (Ucs2StrTemp); + return EFI_INVALID_PARAMETER; + } + + Status =3D UTF8ToUCS2Char (Utf8Str + Utf8StrIndex, Ucs2StrTemp + U= cs2StrIndex); + if (EFI_ERROR (Status)) { + + FreePool (Ucs2StrTemp); + return EFI_INVALID_PARAMETER; + } + + Ucs2StrIndex ++; + Utf8StrIndex +=3D Utf8BufferSize; + } + } + + *Ucs2StrAddr =3D AllocateZeroPool ((Ucs2StrIndex + 1) * sizeof (CHAR16= )); + if (*Ucs2StrAddr =3D=3D NULL) { + + FreePool (Ucs2StrTemp); + return EFI_OUT_OF_RESOURCES; + } + + StrCpyS (*Ucs2StrAddr, Ucs2StrIndex + 1, Ucs2StrTemp); + *(*Ucs2StrAddr + Ucs2StrIndex) =3D L'\0'; + FreePool (Ucs2StrTemp); + + return EFI_SUCCESS; +} + diff --git a/MdeModulePkg/Library/JsonLib/JsonUtilities.h b/MdeModulePkg/= Library/JsonLib/JsonUtilities.h new file mode 100644 index 0000000000..3a3bd092ab --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/JsonUtilities.h @@ -0,0 +1,69 @@ +/** @file + Utility functions for JSON operations. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef REDFISH_JSON_UTILITIES_H_ +#define REDFISH_JSON_UTILITIES_H_ + +#include +#include +#include +#include +#include +#include +#include +#include "jansson.h" + +#define JSON_UNICODE_FORMAT_LEN 6 /// L"\u0000" +#define JSON_UNICODE_FORMAT_SIZE 7 + +#define JSON_UNICODE_FORMAT_CHAR_LEN 2 +#define JSON_UNICODE_FORMAT_CHAR_SIZE 3 + +#define UTF8_BUFFER_FOR_UCS2_MAX_SIZE 3 + +/** + Convert a UCS2 string to a UTF8 encoded string. + + @param[in] Ucs2Str The provided UCS2 string. + @param[out] Utf8StrAddr The converted UTF8 string address= . Caller + is responsible for Free this stri= ng. + + @retval EFI_INVALID_PARAMETER One or more parameters are invali= d. + @retval EFI_OUT_OF_RESOURCES System runs out of resources. + @retval EFI_SUCCESS The UTF8 encoded string has been = converted. + +**/ +EFI_STATUS +UCS2StrToUTF8 ( + IN CHAR16 *Ucs2Str, + OUT CHAR8 **Utf8StrAddr + ); + +/** + Convert a UTF8 encoded string to a UCS2 string. + + @param[in] Utf8Str The provided UTF8 encoded string. + @param[out] Ucs2StrAddr The converted UCS2 string address= . Caller + is responsible for Free this stri= ng. + + @retval EFI_INVALID_PARAMETER The UTF8 encoded string is not va= lid to + convert to UCS2 string. + One or more parameters are invali= d. + @retval EFI_OUT_OF_RESOURCES System runs out of resources. + @retval EFI_SUCCESS The UCS2 string has been converte= d. + +**/ +EFI_STATUS +UTF8StrToUCS2 ( + IN CHAR8 *Utf8Str, + OUT CHAR16 **Ucs2StrAddr + ); + +#endif diff --git a/MdeModulePkg/Library/JsonLib/assert.h b/MdeModulePkg/Library= /JsonLib/assert.h new file mode 100644 index 0000000000..5bc7ccd7e5 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/assert.h @@ -0,0 +1,11 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include diff --git a/MdeModulePkg/Library/JsonLib/errno.h b/MdeModulePkg/Library/= JsonLib/errno.h new file mode 100644 index 0000000000..5bc7ccd7e5 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/errno.h @@ -0,0 +1,11 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include diff --git a/MdeModulePkg/Library/JsonLib/jansson_config.h b/MdeModulePkg= /Library/JsonLib/jansson_config.h new file mode 100644 index 0000000000..d445f4bc91 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/jansson_config.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2010-2016 Petri Lehtinen + * + * Jansson is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + * + * + * This file specifies a part of the site-specific configuration for + * Jansson, namely those things that affect the public API in + * jansson.h. + * + * The configure script copies this file to jansson_config.h and + * replaces @var@ substitutions by values that fit your system. If you + * cannot run the configure script, you can do the value substitution + * by hand. + + (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + */ + +#ifndef JANSSON_CONFIG_H +#define JANSSON_CONFIG_H + +/* If your compiler supports the inline keyword in C, JSON_INLINE is + defined to `inline', otherwise empty. In C++, the inline is always + supported. */ +#ifdef __cplusplus +#define JSON_INLINE inline +#else +#define JSON_INLINE +#endif + +/* If your compiler supports the `long long` type and the strtoll() + library function, JSON_INTEGER_IS_LONG_LONG is defined to 1, + otherwise to 0. */ +#define JSON_INTEGER_IS_LONG_LONG 1 + +/* If locale.h and localeconv() are available, define to 1, + otherwise to 0. */ +#define JSON_HAVE_LOCALECONV 0 + +/* If __atomic builtins are available they will be used to manage + reference counts of json_t. */ +#define JSON_HAVE_ATOMIC_BUILTINS 0 + +/* If __atomic builtins are not available we try using __sync builtins + to manage reference counts of json_t. */ +#define JSON_HAVE_SYNC_BUILTINS 0 + +/* Maximum recursion depth for parsing JSON input. + This limits the depth of e.g. array-within-array constructions. */ +#define JSON_PARSER_MAX_DEPTH 2048 + +#define SUPPORT_JANSSON_JSON_REAL 1 + +#endif diff --git a/MdeModulePkg/Library/JsonLib/jansson_private_config.h b/MdeM= odulePkg/Library/JsonLib/jansson_private_config.h new file mode 100644 index 0000000000..268f91ef8a --- /dev/null +++ b/MdeModulePkg/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.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + 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/MdeModulePkg/Library/JsonLib/limits.h b/MdeModulePkg/Library= /JsonLib/limits.h new file mode 100644 index 0000000000..5bc7ccd7e5 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/limits.h @@ -0,0 +1,11 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include diff --git a/MdeModulePkg/Library/JsonLib/math.h b/MdeModulePkg/Library/J= sonLib/math.h new file mode 100644 index 0000000000..5bc7ccd7e5 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/math.h @@ -0,0 +1,11 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include diff --git a/MdeModulePkg/Library/JsonLib/stdarg.h b/MdeModulePkg/Library= /JsonLib/stdarg.h new file mode 100644 index 0000000000..5bc7ccd7e5 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/stdarg.h @@ -0,0 +1,11 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include diff --git a/MdeModulePkg/Library/JsonLib/stddef.h b/MdeModulePkg/Library= /JsonLib/stddef.h new file mode 100644 index 0000000000..5bc7ccd7e5 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/stddef.h @@ -0,0 +1,11 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include diff --git a/MdeModulePkg/Library/JsonLib/stdio.h b/MdeModulePkg/Library/= JsonLib/stdio.h new file mode 100644 index 0000000000..5bc7ccd7e5 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/stdio.h @@ -0,0 +1,11 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include diff --git a/MdeModulePkg/Library/JsonLib/stdlib.h b/MdeModulePkg/Library= /JsonLib/stdlib.h new file mode 100644 index 0000000000..5bc7ccd7e5 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/stdlib.h @@ -0,0 +1,11 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include diff --git a/MdeModulePkg/Library/JsonLib/string.h b/MdeModulePkg/Library= /JsonLib/string.h new file mode 100644 index 0000000000..5bc7ccd7e5 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/string.h @@ -0,0 +1,11 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include diff --git a/MdeModulePkg/Library/JsonLib/sys/time.h b/MdeModulePkg/Libra= ry/JsonLib/sys/time.h new file mode 100644 index 0000000000..5bc7ccd7e5 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/sys/time.h @@ -0,0 +1,11 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include diff --git a/MdeModulePkg/Library/JsonLib/sys/types.h b/MdeModulePkg/Libr= ary/JsonLib/sys/types.h new file mode 100644 index 0000000000..9e27294a82 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/sys/types.h @@ -0,0 +1,10 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include diff --git a/MdeModulePkg/Library/JsonLib/time.h b/MdeModulePkg/Library/J= sonLib/time.h new file mode 100644 index 0000000000..5bc7ccd7e5 --- /dev/null +++ b/MdeModulePkg/Library/JsonLib/time.h @@ -0,0 +1,11 @@ +/** @file + Include file to support building the third-party jansson library. + + Copyright (c) 2019, Intel Corporation. All rights reserved.
+ (C) Copyright 2020 Hewlett Packard Enterprise Development LP
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include --=20 2.17.1