From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0a-002e3701.pphosted.com (mx0a-002e3701.pphosted.com [148.163.147.86]) by mx.groups.io with SMTP id smtpd.web08.10820.1607062842864218893 for ; Thu, 03 Dec 2020 22:20:43 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@hpe.com header.s=pps0720 header.b=P9CynIfP; spf=pass (domain: hpe.com, ip: 148.163.147.86, mailfrom: prvs=0607c9e77c=abner.chang@hpe.com) Received: from pps.filterd (m0148663.ppops.net [127.0.0.1]) by mx0a-002e3701.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0B468BaC015649; Fri, 4 Dec 2020 06:20:37 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=hpe.com; h=from : to : cc : subject : date : message-id : references : in-reply-to : content-type : content-transfer-encoding : mime-version; s=pps0720; bh=/Yi09eSyNlgkkEc7CGNX83//a+9fr13FuGLgZE5BBqI=; b=P9CynIfPaWJpbxgeafrpBccaG+HosMhy0pcppuxgPLuZGNhoVxCRFrKkjf1y/idF0t7w WuUkXaZPkvn46TOkSrH0v9fKfG9ML9KZjhtBeiM+XCcAEgaveW2ZsJ1VHOi3I6GSXKIX PcmGjcWysgzQXKbWNNgdhz63ddHBY2oR31IpjFxUOvNZ9wGA7noFPAHfUw3uR7SX2LpM wG0j7h0ihhZlXiBXaTQDe93w3Zif7fihYguW7P5uda9iaLwnHJuD2PI/pC6N9V3H9htz nZ+8yVlJX0vm0CDMCrSaTJsC886fPLlR8kGR/Ay9pQwJY4MhSvx9sOoK42GezS6Ap/Xl fg== Received: from g2t2354.austin.hpe.com (g2t2354.austin.hpe.com [15.233.44.27]) by mx0a-002e3701.pphosted.com with ESMTP id 3563vg2mjj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 04 Dec 2020 06:20:36 +0000 Received: from G2W6311.americas.hpqcorp.net (g2w6311.austin.hp.com [16.197.64.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by g2t2354.austin.hpe.com (Postfix) with ESMTPS id 61EB8A0; Fri, 4 Dec 2020 06:20:35 +0000 (UTC) Received: from G4W9325.americas.hpqcorp.net (16.208.32.94) by G2W6311.americas.hpqcorp.net (16.197.64.53) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 4 Dec 2020 06:20:09 +0000 Received: from G9W9210.americas.hpqcorp.net (2002:10dc:429b::10dc:429b) by G4W9325.americas.hpqcorp.net (2002:10d0:205e::10d0:205e) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Fri, 4 Dec 2020 06:20:08 +0000 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (15.241.52.10) by G9W9210.americas.hpqcorp.net (16.220.66.155) with Microsoft SMTP Server (TLS) id 15.0.1497.2 via Frontend Transport; Fri, 4 Dec 2020 06:20:08 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Xwj4y+xjdPVtpuajvuWuhPHCTfd2+6Gg0s/gMATNR+6NOcy9FoRgNCEp/274INo698GyDCfVhLzpmtMcM243kTTgdtuQWz4M/qLfg660x+XBFkztRwIw50wF291LwfeCRAi/FkMdks67mHFb0Lt+oYXwDTLZb5+ZAbyIHDonKSVsKxcqwjv9Ql2OkFFOaX5iOBncc5SYodDssH+YpXHIGLvAzBfg19/Mu/960xnYRDRWexlR+NnXqyfAPsTS3i3eeB0+IDVLsTq0M8gdlEL0bxoW/471sFWynWoBTxVsHftwM3D6Oa0xukmXjYVqXSeABuNMDvS5ZSHa1V25gMgnlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xoGBAgDmE95tADk2gG5dfjdhLHQLUcrVX1B1IQjz34U=; b=kZZZ5lQV7pKFtdvwhD4yDnovm/2lRGavQXkEdZtjl16IVEp/8+Jkgxv9BSFcZWdws5gESh+QUdfiIbMaEvhAIVHNGHP1cnt8kojBKeVstTH3VlNgwo8zc1WvHwa4WpRjnOICTRJFYxEnnUqJG6vItY+IdLB8Jqg7Lm9y7kLJzFtARQWGyU6Sk6Dl0g2RPoD2tJITToBKMI1UV6f3UzSSEOjnonHJwsSUZFokkXICFhSHzIQZ/Gx5BWCQS/KWopc7pcJMbO92o57qCLxlxxcL1ET2U78Zt62WZyt2wCEc19VN6vAreX0RB9ajLjVj/pUxoYdXDPBG4eUsKZREgIEjuQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=hpe.com; dmarc=pass action=none header.from=hpe.com; dkim=pass header.d=hpe.com; arc=none Received: from CS1PR8401MB1144.NAMPRD84.PROD.OUTLOOK.COM (2a01:111:e400:7508::16) by CS1PR8401MB1000.NAMPRD84.PROD.OUTLOOK.COM (2a01:111:e400:750e::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.19; Fri, 4 Dec 2020 06:20:03 +0000 Received: from CS1PR8401MB1144.NAMPRD84.PROD.OUTLOOK.COM ([fe80::a094:ed67:fb40:340e]) by CS1PR8401MB1144.NAMPRD84.PROD.OUTLOOK.COM ([fe80::a094:ed67:fb40:340e%10]) with mapi id 15.20.3632.021; Fri, 4 Dec 2020 06:20:03 +0000 From: "Abner Chang" To: Leif Lindholm CC: "devel@edk2.groups.io" , Andrew Fish , Laszlo Ersek , Michael D Kinney , "Wang, Nickle (HPS SW)" , "O'Hanley, Peter (EXL)" Subject: Re: [PATCH v3 2/4] RedfishPkg/library: EDK2 port of jansson library Thread-Topic: [PATCH v3 2/4] RedfishPkg/library: EDK2 port of jansson library Thread-Index: AQHWyLGuVzATkv6nYkywucZSt5rY7qnlZRdggAA2zYCAANpEoA== Date: Fri, 4 Dec 2020 06:20:03 +0000 Message-ID: References: <20201202071557.4688-1-abner.chang@hpe.com> <20201202071557.4688-3-abner.chang@hpe.com> <20201202134713.GI1664@vanye> <20201203170629.GT1664@vanye> In-Reply-To: <20201203170629.GT1664@vanye> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: nuviainc.com; dkim=none (message not signed) header.d=none;nuviainc.com; dmarc=none action=none header.from=hpe.com; x-originating-ip: [1.34.113.40] x-ms-publictraffictype: Email x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: f33a4dd4-8f97-4c9b-82b2-08d8981ca302 x-ms-traffictypediagnostic: CS1PR8401MB1000: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:10000; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: DusSXnx9zqoE6fhsVxn3d5ZtvLbIXQRZn7f53fA+H8IC0hEgs0DvNloNra9KPEyotDhag2/y+BqltdGYuqF3ykPmP27wa2xSZYd+W21RyXDmrA6Z7u+gVRg/DynW0IQFsB1kj83ApmwknRbkLlUb/aQaoECPGniuZkAZns8UQynoi1a0pgxmPcdZeaP9LWuD/DwjqhNYYDF36ZUXuLIhGA3UwrNsaC/z7KL+PGWrIWqGaZ0adLtydjRBPKbU0FpOIaCxWOlCqwUQD2kdH9+wikSwyCM0YMKHRt7hfSL8ksQI7tilFK4oDgEFDHY/uuULG4SOkjfGE8JsyGaRac75Ocbd3N2yYNZ+FgyVdMPvY+fSaVvqekvbzH/K30/RK59vLt4svbvQjaeUARu0dbNBaxfwTRKx/1HpAIc8Stg0DOk8Tv2SXF3SgNpCyDQf1hhuOQaI3OlnjxHuhFDzykt0KMfqK/DUwHoW+1gvl22jVtgxIp8X7tGIohCxlce0iGUEoEYvcI8++s1QR6Ekv/sQ4w== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CS1PR8401MB1144.NAMPRD84.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(396003)(366004)(376002)(136003)(346002)(39860400002)(86362001)(45080400002)(966005)(6916009)(30864003)(7696005)(19627235002)(2906002)(478600001)(8676002)(83380400001)(54906003)(66556008)(33656002)(186003)(71200400001)(66476007)(76116006)(9686003)(64756008)(52536014)(26005)(5660300002)(66946007)(6506007)(316002)(53546011)(66446008)(4326008)(8936002)(55016002)(461764006)(2004002)(21314003)(579004)(559001)(569008);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata: =?us-ascii?Q?rcR9e6qOX4YjZ1lOXrGtw3WVwR0h6J+Gz/6Lbkvcp1Dg5F0uaS8ckSEm+nh8?= =?us-ascii?Q?8U/Y1uYUJsjGHwQIVKXHIAPz4GAflOSh2RCqWdMQqWOpx4OPFUiGeLbyNLN7?= =?us-ascii?Q?Nnp+I2++VRQ15tkvdFCM9f1LtWE1kCmjq9EHY6ueVwu+2pkYKrqcDoeUYrmr?= =?us-ascii?Q?QxYoi91SdzSaMrqKtsNddldGHd85LixkdFbiB2WnCie/QS6WnH178a8CspHl?= =?us-ascii?Q?T7+da3IyYCdNggpsg+PiRaAGY4lfU2xWBDnMeqcAJ6i6276kqwquW5j+VYnn?= =?us-ascii?Q?qoHzUHlC0ZMHeNG97ruwAJBrWiUClaTt3qy+5zO+bXvNI9kmm32pA1riKWVf?= =?us-ascii?Q?RpYosnErVsf0zJJi44V4GJUunofntbjMFwfqEDqOPxnMv9k3ok2owHcdKvzy?= =?us-ascii?Q?KYvXazjzHCKoQ96L+I+nYlUw9/5puJ+cBIJqu+dhByj1KTDiyYaEwvLnDlo3?= =?us-ascii?Q?jj5TQcBZaq8YYO/lmoPb/CBYEPzaB3coOpf0mhG7J8Y8h1yg7c0AQHmb+7Mp?= =?us-ascii?Q?PwgNvptUJvDTREQvMYEHkRm8j8AxR0MvRAx+kAjMbxLo7Z/8MFAm2pznc6qZ?= =?us-ascii?Q?iqKnipmSADkjMjS92WHV/lNatZGxQmCmzfGrXfQV5PdFkaDyehEfICiHxGdC?= =?us-ascii?Q?YmEwcR5FM167eIB9uR+E3cezUe/nTaghHeGfYU7MNukwz/MIeDR7v1JkonL6?= =?us-ascii?Q?6nDEnKiW8H/2Q2xmuEqRdLi/RXjBuxFFV5ZFKksTCM8I45l/A3R3w+ytIPk/?= =?us-ascii?Q?+jwIjZgs9/E1JD/0zmPuOueZ/HmmTJS8oSYK6vG4gGR8kaw2W/89fUUuRwh1?= =?us-ascii?Q?5FG4sxQlfBpKEeoCPWNfgEnw0hlPCX70v3czxse0m+E4XRUk2FwAIxCAUGLw?= =?us-ascii?Q?CuE0rePO0TzBxFGV1Cr/r7Mr1evcrkYI4J2/asOXRDUkJgw3TYBSDcMn64A1?= =?us-ascii?Q?WHNQkcE9Lqrh3zELUmujSiBwg/7khw66shdGnB6QhoU=3D?= X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: CS1PR8401MB1144.NAMPRD84.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: f33a4dd4-8f97-4c9b-82b2-08d8981ca302 X-MS-Exchange-CrossTenant-originalarrivaltime: 04 Dec 2020 06:20:03.1863 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 105b2061-b669-4b31-92ac-24d304d195dc X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: ChKWasVYPKnMlDRiHeFZinILXR+P5maskRdN3XmwugxUeXMF+pQdbYPH4qhLsC2ZetQ3tXFXYAeVh+/nqxip5g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CS1PR8401MB1000 X-OriginatorOrg: 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-12-04_01:2020-12-04,2020-12-04 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 impostorscore=0 clxscore=1015 mlxlogscore=999 adultscore=0 spamscore=0 malwarescore=0 lowpriorityscore=0 suspectscore=0 mlxscore=0 priorityscore=1501 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2012040035 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable > -----Original Message----- > From: Leif Lindholm [mailto:leif@nuviainc.com] > Sent: Friday, December 4, 2020 1:06 AM > To: Chang, Abner (HPS SW/FW Technologist) > Cc: devel@edk2.groups.io; Andrew Fish ; Laszlo Ersek > ; Michael D Kinney ; > Wang, Nickle (HPS SW) ; O'Hanley, Peter (EXL) > > Subject: Re: [PATCH v3 2/4] RedfishPkg/library: EDK2 port of jansson libr= ary >=20 > On Thu, Dec 03, 2020 at 15:13:27 +0000, Chang, Abner (HPS SW/FW > Technologist) wrote: > > > I'm not going to force you to do the work here, but I am going to > > > state an opinion: > > > When we bring in external code, using a differing coding style, it is > > > very helpful to do what we *didn't* do with libfdt and provide a > > > coding-style compatible way of accessing it. You do that here, and > > > that is excellent, but... > > > > > > Only doing itn through macros means we're following the superficial > > > form without following the underlying design ideals. > > > In a perfect world, when we import an external module, I would like to > > > see it wrapped as a protocol, discoverable for anyone to use. > > > > That is always good to design a protocol interface which can be used > > by modules. Also we can have the different implementations for the > > protocol API. > > > > However, this methodology is not perfect fit to the edk2 port of > > JSON lib. If the external module uses jansson lib, that just links > > with the edk2 JsonLib. But if the external module uses another JSON > > library such as Parson then it has to link with the Parson edk2 port > > lib (someone has to implement it). The JSON EFI protocol may not > > easy to be standardized because those open source JSON lib project > > have the different API definitions. Multiple EFI JSON protocols with > > different protocol GUID would be introduced for different JSON lib > > implementations. To define a protocol for JSON functions seems to me > > not very useful and there will have many APIs :) > > > > And even we warp it as a protocol, we still need the macros to map > > native JSON lib APIs used in the external module to the code which > > invokes edk2 JSON protocol API. >=20 > I agree it's not a trivial problem to solve. > But what you're creating here is something that acts like an os-style > shared library: > - It's not what EDK2 considers a library. > - It identifies itself as a UEFI Driver, but installs no protocols. Module type BASE makes sense right? >=20 > What we end up with is in effect a driver that has only side effects - > the side effect of making certain symbols resolveable. >=20 > But I'm not a hyper purist. And I agree it's probably the only way to > solve the problem of making it usable to *other* external imported > projects depending on this. >=20 > *However*, the method in which this is done means that there is no way > for code that wants to use these functions to do so by the traditional > and expected UEFI/PI mechanisms. We probably can design a protocol on top of JsonLib. But the protocol APIs = would be many and we have to try to standardize protocol API to accommodate= different JSON lib implementations. > And it does make me wonder if code that does that, or depends on it, > belongs in the main edk2 repository. >=20 > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Include/Library/JsonLib.h > > > b/RedfishPkg/Include/Library/JsonLib.h > > > > new file mode 100644 > > > > index 0000000000..adadcbf33f > > > > --- /dev/null > > > > +++ b/RedfishPkg/Include/Library/JsonLib.h > > > > @@ -0,0 +1,767 @@ > > > > +/** @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 JSON_LIB_H_ > > > > +#define JSON_LIB_H_ > > > > + > > > > +#include > > > > + > > > > +#include > > > > +#include > > > > + > > > > +typedef VOID* EDKII_JSON_VALUE; > > > > +typedef VOID* EDKII_JSON_ARRAY; > > > > +typedef VOID* EDKII_JSON_OBJECT; > > > > + > > > > +/// > > > > +/// Map to json_int_t in jansson.h > > > > +/// > > > > +typedef long long EDKII_JSON_INT_T; // > > > #JSON_INTEGER_IS_LONG_LONG is set to 1 > > > > + // in jansson_Config.h > > > > > > Is this the right thing to do, or should this be explicitly UINT64? > > Will check this. > > > > > > > + > > > > +/// > > > > +/// Map to the definitions in jansson.h > > > > +/// > > > > +#define EDKII_JSON_MAX_INDENT 0x1f > > > > +#define EDKII_JSON_INDENT(n) ((n)&EDKII_JSON_MAX_INDENT) > > > > > > Spaces around & please. > > > > > > > +#define EDKII_JSON_COMPACT 0x20 > > > > +#define EDKII_JSON_ENSURE_ASCII 0x40 > > > > +#define EDKII_JSON_SORT_KEYS 0x80 > > > > +#define EDKII_JSON_PRESERVE_ORDER 0x100 > > > > +#define EDKII_JSON_ENCODE_ANY 0x200 > > > > +#define EDKII_JSON_ESCAPE_SLASH 0x400 > > > > +#define EDKII_JSON_REAL_PRECISION(n) (((n)&0x1F) << 11) > > > > > > Is this the same 0x1f as above? > > > > Did you refer to EDKII_JSON_MAX_INDENT? This is used for pretty print. >=20 > Yes. Just saw two identical live-coded values near each other. > If unrelated, fine. (But do make sure hex digits a-f are consistently > upper or lover case.) done >=20 > > > Spaces around & please. > > ok > > > > > > > +#define EDKII_JSON_EMBED 0x10000 > > > > + > > > > +#define EDKII_JSON_ARRAY_FOREACH(Array, Index, Value) \ > > > > + for(Index =3D 0; \ > > > > + Index < JsonArrayCount(Array) && (Value =3D > JsonArrayGetValue(Array, > > > Index)); \ > > > > + Index++) > > > > + > > > > +/// > > > > +/// Map to the json_error_t in jansson.h > > > > +/// > > > > +#define EDKII_JSON_ERROR_TEXT_LENGTH 160 > > > > +#define EDKII_JSON_ERROR_SOURCE_LENGTH 80 > > > > +typedef struct { > > > > + INTN Line; > > > > + INTN Column; > > > > + INTN Position; > > > > + CHAR8 Source [EDKII_JSON_ERROR_SOURCE_LENGTH]; > > > > + CHAR8 Text [EDKII_JSON_ERROR_TEXT_LENGTH]; > > > > +} EDKII_JSON_ERROR; > > > > + > > > > +/// > > > > +/// Map to the json_type in jansson.h > > > > +/// > > > > +typedef enum { > > > > + EdkiiJsonTypeObject, > > > > + EdkiiJsonTypeArray, > > > > + EdkiiJsonTypeString, > > > > + EdkiiJsonTypeInteger, > > > > + EdkiiJsonTypeReal, > > > > + EdkiiJsonTypeTrue, > > > > + EdkiiJsonTypeFalse, > > > > + EdkiiJsonTypeNull > > > > +} EDKII_JSON_TYPE; > > > > + > > > > +/** > > > > + The function is used to convert a NULL terminated UTF8 encoded > string > > > to a JSON > > > > + value. Only object and array represented strings can be converted > > > successfully, > > > > + since they are the only valid root values of a JSON text for UEF= I usage. > > > > + > > > > + 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 s= tring to > > > convert > > > > + > > > > + @retval Array JSON value or object JSON value, or NULL when= any > > > error 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 converti= ng, > and > > > 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 > JSON > > > array, > > > > + or NULL on error. Initially, the array is empty. > > > > + > > > > + The reference count of this value will be set to 1, and caller n= eeds to > > > cleanup the > > > > + value by calling JsonValueFree(). > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @retval The created JSON value which contains a JSON array = or > NULL. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitArray ( > > > > + VOID > > > > + ); > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > object, > > > > + or NULL on error. Initially, the object is empty. > > > > + > > > > + The reference count of this value will be set to 1, and caller n= eeds to > > > cleanup the > > > > + value by calling JsonValueFree(). > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @retval The created JSON value which contains a JSON object= or > NULL. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitObject ( > > > > + VOID > > > > + ); > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > string, > > > > + or NULL on error. > > > > + > > > > + The input string must be NULL terminated Ascii format, non-Ascii > > > characters will > > > > + be processed as an error. Unicode characters can also be > represented by > > > Ascii string > > > > + as the format: \u + 4 hexadecimal digits, like \u3E5A, or \u003F. > > > > + > > > > + The reference count of this value will be set to 1, and caller n= eeds to > > > cleanup the > > > > + value by calling JsonValueFree(). > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] String The Ascii string to initialize to JSON = value > > > > + > > > > + @retval The created JSON value which contains a JSON string= or > NULL. > > > Select a > > > > + Getter API for a specific encoding format. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitAsciiString ( > > > > + IN CONST CHAR8 *String > > > > + ); > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > string, > > > > + or NULL on error. > > > > + > > > > + The input must be a NULL terminated UCS2 format Unicode string. > > > > + > > > > + The reference count of this value will be set to 1, and caller n= eeds to > > > cleanup the > > > > + value by calling JsonValueFree(). > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] String The Unicode string to initialize to JSO= N value > > > > + > > > > + @retval The created JSON value which contains a JSON string= or > NULL. > > > Select a > > > > + Getter API for a specific encoding format. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitUnicodeString ( > > > > + IN CHAR16 *String > > > > + ); > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > integer, > > > > + or NULL on error. > > > > + > > > > + The reference count of this value will be set to 1, and caller n= eeds to > > > cleanup the > > > > + value by calling JsonValueFree(). > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] Value The integer to initialize to JSON value > > > > + > > > > + @retval The created JSON value which contains a JSON number= or > > > NULL. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitNumber ( > > > > + IN INT64 Value > > > > + ); > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > boolean, > > > > + or NULL on error. > > > > + > > > > + Boolean JSON value is kept as static value, and no need to do any > > > cleanup work. > > > > + > > > > + @param[in] Value The boolean value to initialize. > > > > + > > > > + @retval The created JSON value which contains a JSON boolea= n or > > > NULL. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitBoolean ( > > > > + IN BOOLEAN Value > > > > + ); > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > NULL, > > > > + or NULL on error. > > > > + > > > > + NULL JSON value is kept as static value, and no need to do any > cleanup > > > work. > > > > + > > > > + @retval The created NULL JSON value. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitNull ( > > > > + VOID > > > > + ); > > > > + > > > > +/** > > > > + The function is used to decrease the reference count of a JSON v= alue > by > > > one, and once > > > > + this reference count drops to zero, the value is destroyed and i= t can > no > > > longer be used. > > > > + If this destroyed value is object type or array type, reference = counts > for > > > all containing > > > > + JSON values will be decreased by 1. Boolean JSON value and NULL > JSON > > > value won't be destroyed > > > > + since they are static values kept in memory. > > > > + > > > > + Reference Count Strategy: BaseJsonLib uses this strategy to track > > > whether a value is still > > > > + in use or not. When a value is created, it's reference count is = set to 1. > If a > > > reference to a > > > > + value is kept for use, its reference count is incremented, and w= hen > the > > > value is no longer > > > > + needed, the reference count is decremented. When the reference > > > count drops to zero, there are > > > > + no references left, and the value can be destroyed. > > > > + > > > > + The given JSON value maybe NULL and not causing any problem. Just > > > output the debug message > > > > + to inform caller the NULL value is passed in. > > > > + > > > > + @param[in] Json The JSON value to be freed. json_d= ecref > may > > > return without any > > > > + changes if Json is NULL. > > > > + > > > > +**/ > > > > +VOID > > > > +EFIAPI > > > > +JsonValueFree ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to create a fresh copy of a JSON value, and= all > child > > > values are deep > > > > + copied in a recursive fashion. It should be called when this JSO= N value > > > might be modified > > > > + in later use, but the original still wants to be used in somewhe= re else. > > > > + > > > > + Reference counts of the returned root JSON value and all child v= alues > 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, t= oo > many > > > calls may cause some > > > > + performance issues, user should avoid unnecessary calls to this > function > > > unless it is really > > > > + needed. > > > > + > > > > + @param[in] Json The JSON value to be cloned. > > > > + > > > > + @retval Return the cloned JSON value, or NULL on error. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueClone ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON value contai= ns a > JSON > > > array. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value contains a JSON arr= ay. > > > > + @retval FALSE The JSON value doesn't contain a J= SON array. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsArray ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON value contai= ns a > JSON > > > object. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value contains a JSON obj= ect. > > > > + @retval FALSE The JSON value doesn't contain a J= SON object. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsObject ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON Value contai= ns a > > > string, Ascii or > > > > + Unicode format is not differentiated. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value contains a JSON str= ing. > > > > + @retval FALSE The JSON value doesn't contain a J= SON string. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsString ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON value contai= ns a > JSON > > > number. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value is contains JSON nu= mber. > > > > + @retval FALSE The JSON value doesn't contain a J= SON > number. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsNumber ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON value contai= ns a > JSON > > > boolean. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value contains a JSON boo= lean. > > > > + @retval FALSE The JSON value doesn't contain a J= SON > boolean. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsBoolean ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON value contai= ns a > JSON > > > NULL. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value contains a JSON NUL= L. > > > > + @retval FALSE The JSON value doesn't contain a J= SON NULL. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsNull ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to retrieve the associated array in an arra= y 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 obj= ect > type > > > JSON value. > > > > + > > > > + Any changes to the returned object will impact the original JSON > value. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval Return the associated object in JSON value or NULL. > > > > + > > > > +**/ > > > > +EDKII_JSON_OBJECT > > > > +EFIAPI > > > > +JsonValueGetObject ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to retrieve the associated Ascii string in = a string > type > > > JSON value. > > > > + > > > > + Any changes to the returned string will impact the original JSON= value. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval Return the associated Ascii string in JSON value or= NULL. > > > > + > > > > +**/ > > > > +CHAR8* > > > > +EFIAPI > > > > +JsonValueGetAsciiString ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to retrieve the associated Unicode string i= n a > string > > > type JSON value. > > > > + > > > > + Caller can do any changes to the returned string without any imp= act > 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 > NULL. > > > > + > > > > +**/ > > > > +CHAR16* > > > > +EFIAPI > > > > +JsonValueGetUnicodeString ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to retrieve the associated integer in a num= ber > type > > > JSON value. > > > > + > > > > + The input JSON value should not be NULL or contain no JSON numbe= r, > > > otherwise it will > > > > + ASSERT() and return 0. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval Return the associated number in JSON value. > > > > + > > > > +**/ > > > > +INT64 > > > > +EFIAPI > > > > +JsonValueGetNumber ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to retrieve the associated boolean in a boo= lean > type > > > JSON value. > > > > + > > > > + The input JSON value should not be NULL or contain no JSON boole= an, > > > otherwise it will > > > > + ASSERT() and return FALSE. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval Return the associated value of JSON boolean. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueGetBoolean ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to retrieve the associated string in a 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. > > > > + > > > > +**/ > > > > +CONST CHAR8* > > > > +EFIAPI > > > > +JsonValueGetString ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to get the number of elements in a JSON obj= ect, > or 0 > > > if it is NULL or > > > > + not a JSON object. > > > > + > > > > + @param[in] JsonObject The provided JSON object. > > > > + > > > > + @retval Return the number of elements in this JSON object o= r 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 > enumeration. > > > > + @param[out] KeyCount The count of keys in this JS= ON object. > > > > + > > > > + @retval Return an array of the enumerated keys in this JSON > object or > > > NULL. > > > > + > > > > +**/ > > > > +CHAR8** > > > > +JsonObjectGetKeys ( > > > > + IN EDKII_JSON_OBJECT JsonObj, > > > > + OUT UINTN *KeyCount > > > > + ); > > > > + > > > > +/** > > > > + The function is used to get a JSON value corresponding to the in= put > key > > > from a JSON object. > > > > + > > > > + It only returns a reference to this value and any changes on thi= s value > will > > > impact the > > > > + original JSON object. If that is not expected, please call > JsonValueClone() > > > to clone it to > > > > + use. > > > > + > > > > + Input key must be a valid NULL terminated UTF8 encoded string. > NULL > > > will be returned when > > > > + Key-Value is not found in this JSON object. > > > > + > > > > + @param[in] JsonObj The provided JSON object. > > > > + @param[in] Key The key of the JSON value to be r= etrieved. > > > > + > > > > + @retval Return the corresponding JSON value to key, or NULL= on > error. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonObjectGetValue ( > > > > + IN CONST EDKII_JSON_OBJECT JsonObj, > > > > + IN CONST CHAR8 *Key > > > > + ); > > > > + > > > > +/** > > > > + The function is used to set a JSON value corresponding to the in= put > key > > > from a JSON object, > > > > + and the reference count of this value will be increased by 1. > > > > + > > > > + Input key must be a valid NULL terminated UTF8 encoded string. If > there > > > already is a value for > > > > + this key, this key will be assigned to the new JSON value. The o= ld > JSON > > > value will be removed > > > > + from this object and thus its' reference count will be decreased= by 1. > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] JsonObj The provided JSON object. > > > > + @param[in] Key The key of the JSON value to= be set. > > > > + @param[in] Json The JSON value to set to thi= s JSON object > > > mapped by key. > > > > + > > > > + @retval EFI_ABORTED Some error occur and operati= on > aborted. > > > > + @retval EFI_SUCCESS The JSON value has been set = to this > JSON > > > object. > > > > + > > > > +**/ > > > > +EFI_STATUS > > > > +EFIAPI > > > > +JsonObjectSetValue ( > > > > + IN EDKII_JSON_OBJECT JsonObj, > > > > + IN CONST CHAR8 *Key, > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to get the number of elements in a JSON arr= ay, > 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 po= sition > > > 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 thi= s value > will > > > impact the > > > > + original JSON object. If that is not expected, please call > JsonValueClone() > > > to clone it to > > > > + use. > > > > + > > > > + If this array is NULL or not a JSON array, or if index is out of= range, > NULL > > > will be returned. > > > > + > > > > + @param[in] JsonArray The provided JSON Array. > > > > + > > > > + @retval Return the JSON value located in the Index position= or > NULL. > > > > + > > > > +**/ > > > > +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 JS= ON > array, > > > and grow the size of > > > > + array by 1. The reference count of this value will be increased = by 1. > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] JsonArray The provided JSON object. > > > > + @param[in] Json The JSON value to append. > > > > + > > > > + @retval EFI_ABORTED Some error occur and operati= on > aborted. > > > > + @retval EFI_SUCCESS JSON value has been appended= to the > end > > > of the JSON array. > > > > + > > > > +**/ > > > > +EFI_STATUS > > > > +EFIAPI > > > > +JsonArrayAppendValue ( > > > > + IN EDKII_JSON_ARRAY JsonArray, > > > > + IN EDKII_JSON_VALUE Json > > > > + ); > > > > + > > > > +/** > > > > + The function is used to remove a JSON value at position index, > shifting > > > the elements after index > > > > + one position towards the start of the array. The reference count= of > this > > > value will be decreased > > > > + by 1. > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] JsonArray The provided JSON array. > > > > + @param[in] Index The Index position before re= movement. > > > > + > > > > + @retval EFI_ABORTED Some error occur and operati= on > aborted. > > > > + @retval EFI_SUCCESS The JSON array has been remo= ved at > > > position index. > > > > + > > > > +**/ > > > > +EFI_STATUS > > > > +EFIAPI > > > > +JsonArrayRemoveValue ( > > > > + IN EDKII_JSON_ARRAY JsonArray, > > > > + IN UINTN Index > > > > + ); > > > > + > > > > +/** > > > > + Dump JSON to a buffer > > > > + > > > > + @param[in] JsonValue The provided JSON array. > > > > + @param[in] Index The Index position before removemen= t. > > > > + > > > > + @retval NULL Dump fail if NULL returned, otherwi= se the > buffer > > > > + contain JSON paylaod in ASCII strin= g. > > > > +**/ > > > > +CHAR8 * > > > > +EFIAPI > > > > +JsonDumpString ( > > > > + IN EDKII_JSON_ARRAY JsonValue, > > > > + IN UINTN Flags > > > > + ); > > > > + > > > > +/** > > > > + Load JSON from a buffer > > > > + > > > > + @param[in] Buffer Bufffer to the JSON payload > > > > + @param[in] BufferLen Length of the buffer > > > > + @param[in] Flags Flag of loading JSON buffer > > > > + > > > > + @retval EDKII_JSON_VALUE NULL means fail to load JSON payl= oad. > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonLoadBuffer ( > > > > + IN CONST CHAR8 *Buffer, > > > > + IN UINTN BufferLen, > > > > + IN UINTN Flags, > > > > + IN EDKII_JSON_ERROR *Error > > > > + ); > > > > + > > > > +/** > > > > + Decrease reference > > > > + > > > > + @param[in] JsonValue JSON value > > > > +**/ > > > > +VOID > > > > +EFIAPI > > > > +JsonDecreaseReference ( > > > > + IN EDKII_JSON_VALUE JsonValue > > > > + ); > > > > + > > > > +/** > > > > + Increase reference > > > > + > > > > + @param[in] JsonValue JSON value > > > > + @retval EDKII_JSON_VALUE of itself > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonIncreaseReference ( > > > > + IN EDKII_JSON_VALUE JsonValue > > > > + ); > > > > +/** > > > > + Returns an opaque iterator which can be used to iterate over all= key- > > > value pairs > > > > + in object, or NULL if object is empty > > > > + > > > > + @param[in] JsonValue JSON value > > > > +**/ > > > > +VOID * > > > > +EFIAPI > > > > +JsonObjectIterator ( > > > > + IN EDKII_JSON_VALUE JsonValue > > > > + ); > > > > + > > > > +/** > > > > + Extract the associated value from iterator. > > > > + > > > > + @param[in] Iterator Iterator pointer > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonObjectIteratorValue ( > > > > + IN VOID *Iterator > > > > + ); > > > > + > > > > +/** > > > > + Returns an iterator pointing to the next key-value pair in objec= t after > iter, > > > > + or NULL if the whole object has been iterated through. > > > > + > > > > + @param[in] JsonValue JSON value > > > > + @param[in] Iterator Iterator pointer > > > > + @retval Iterator pointer > > > > +**/ > > > > +VOID * > > > > +JsonObjectIteratorNext ( > > > > + IN EDKII_JSON_VALUE JsonValue, > > > > + IN VOID *Iterator > > > > + ); > > > > + > > > > +/** > > > > + Returns the json type of this json value > > > > + > > > > + @param[in] JsonValue JSON value > > > > + @retval JSON type returned > > > > +**/ > > > > +EDKII_JSON_TYPE > > > > +EFIAPI > > > > +JsonGetType( > > > > + IN EDKII_JSON_VALUE JsonValue > > > > + ); > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/JanssonCrtLibSupport.c > > > b/RedfishPkg/Library/JsonLib/JanssonCrtLibSupport.c > > > > new file mode 100644 > > > > index 0000000000..907634a05b > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/JanssonCrtLibSupport.c > > > > > > I would howver very much prefer to see this code broken out as a > > > separate library (not protocol), and hence patch. It seems very much > > > like a minimalistic subset of https://github.com/tianocore/edk2-libc. > > > > Separate lib under RedfishPkg? >=20 > As a starting point, yes. > If it turns out to be useful for other code, we can always move it later. Done. The separate library can be leverage by another open source project "= libredfish". >=20 > > > > @@ -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 > returns 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 retur= ns 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 transpa= rently. > > > 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 > string. > > > > + > > > > + @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 and > > > > + there are one or more corresponding characters, as s= pecified > by > > > > + the current locale, for which isupper is true, the t= oupper > > > > + function returns one of the corresponding characters= (always > the > > > > + same one for any given locale); otherwise, the argum= ent 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, inclusi= ve. > > > > +} > > > > + > > > > + > > > > +/** The strtoll function converts the initial portion of the string > pointed 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 a= ny. If > no > > > > + conversion could be performed, zero is returned. If th= e correct > > > > + value is outside the range of representable values, LL= ONG_MIN > or > > > > + LLONG_MAX is returned (according to the sign of the va= lue, if > 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 leadi= ng zeros */ > > > > + 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 t= he initial > > > > + portion of the string pointed to by nptr to long int, long lon= g int, > > > > + unsigned long int, and unsigned long long int representation, > > > respectively. > > > > + First, they decompose the input string into three parts: an in= itial, > > > > + possibly empty, sequence of white-space characters (as specifi= ed by > > > the > > > > + isspace function), a subject sequence resembling an integer > > > represented in > > > > + some radix determined by the value of base, and a final string= of > one or > > > > + more unrecognized characters, including the terminating null > character > > > 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 > sequence > > > 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 > > > subject > > > > + sequence is a sequence of letters and digits representing an i= nteger > > > 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)= through > z > > > > + (or Z) are ascribed the values 10 through 35; only letters and= digits > > > whose > > > > + ascribed values are less than that of base are permitted. If t= he value > of > > > > + base is 16, the characters 0x or 0X may optionally precede the > sequence > > > of > > > > + letters and digits, following the sign if present. > > > > + > > > > + The subject sequence is defined as the longest initial subsequ= ence > of > > > the > > > > + input string, starting with the first non-white-space characte= r, that is > of > > > > + the expected form. The subject sequence contains no characters= if > the > > > 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 permissibl= e letter > 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 ha= s 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 giv= en above. > If > > > > + the subject sequence begins with a minus sign, the value resul= ting > from > > > the > > > > + conversion is negated (in the return type). A pointer to the f= inal > string > > > > + is stored in the object pointed to by endptr, provided that en= dptr is > > > > + not a null pointer. > > > > + > > > > + In other than the "C" locale, additional locale-specific subje= ct > sequence > > > > + 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 ob= ject > > > pointed > > > > + to by endptr, provided that endptr is not a null pointer. > > > > + > > > > + @return The strtol, strtoll, strtoul, and strtoull functions r= eturn the > > > > + converted value, if any. If no conversion could be per= formed, > zero > > > > + is returned. If the correct value is outside the range= of > > > > + representable values, LONG_MIN, LONG_MAX, LLONG_MIN, > > > LLONG_MAX, > > > > + ULONG_MAX, or ULLONG_MAX is returned (according to the > return > > > type > > > > + and sign of the value, if any), and the value of the m= acro > ERANGE > > > > + 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 leadi= ng zeros */ > > > > + 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 stri= ng > pointed > > > 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. If > no > > > > + conversion could be performed, zero is returned. If th= e correct > > > > + value is outside the range of representable values, > ULLONG_MAX is > > > > + returned and the value of the macro ERANGE is stored i= n errno. > > > > +**/ > > > > +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 leadi= ng zeros */ > > > > + 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 st= ring > > > > + pointed to by nptr to double, float, and long double representat= ion, > > > > + respectively. > > > > + > > > > + The strtod(), strtof(), and strtold() functions return the conve= rted > > > > + value, if any. > > > > + > > > > + If endptr is not NULL, a pointer to the character after the last= charac- > > > > + 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_VA= L, > > > > + 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 correc= t value > > > > + 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 y= ear. > > > > + // > > > > + for (Year =3D 1970, CalTime =3D 0; Year !=3D Time.Year; Year++) { > > > > + CalTime =3D CalTime + (time_t)(CumulativeDays[IsLeap(Year)][13= ] * > > > SECSPERDAY); > > > > + } > > > > + > > > > + // > > > > + // Add in number of seconds for current Month, Day, Hour, Minute, > > > Seconds, and TimeZone adjustment > > > > + // > > > > + CalTime =3D CalTime + > > > > + (time_t)((Time.TimeZone !=3D EFI_UNSPECIFIED_TIMEZONE)= ? > > > (Time.TimeZone * 60) : 0) + > > > > + (time_t)(CumulativeDays[IsLeap(Time.Year)][Time.Month]= * > > > SECSPERDAY) + > > > > + (time_t)(((Time.Day > 0) ? Time.Day - 1 : 0) * SECSPER= DAY) + > > > > + (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 sor= ted) > > > elements > > > > + on return a buffer of sorted elem= ents > > > > + @param[in] Count the number of elements in the buf= fer to > sort > > > > + @param[in] ElementSize Size of an element in bytes > > > > + @param[in] CompareFunction The function to call to perform t= he > > > comparison > > > > + of any 2 elements > > > > + @param[in] Buffer Buffer of size ElementSize for us= e in > swapping > > > > +**/ > > > > +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 * > > > ElementSize), ElementSize); > > > > + CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * > > > ElementSize), (UINT8 *)BufferToSort + ((LoopCount) * ElementSize), > > > ElementSize); > > > > + CopyMem ((UINT8 *)BufferToSort + ((LoopCount) * ElementSize), > > > Buffer, ElementSize); > > > > + > > > > + // > > > > + // Increment NextSwapLocation > > > > + // > > > > + NextSwapLocation++; > > > > + } > > > > + } > > > > + // > > > > + // Swap pivot to it's final position (NextSwapLocaiton) > > > > + // > > > > + CopyMem (Buffer, Pivot, ElementSize); > > > > + CopyMem (Pivot, (UINT8 *)BufferToSort + (NextSwapLocation * > > > ElementSize), ElementSize); > > > > + CopyMem ((UINT8 *)BufferToSort + (NextSwapLocation * > ElementSize), > > > Buffer, ElementSize); > > > > + > > > > + // > > > > + // Now recurse on 2 paritial lists. Neither of these will have = the 'pivot' > > > 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)(c= onst > > > void *, 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 > > > BaseSortLib. > > > > + // > > > > + QuickSortWorker (base, (UINTN)num, (UINTN)width, > > > (SORT_COMPARE)compare, Buffer); > > > > + > > > > + free (Buffer); > > > > + return; > > > > +} > > > > + > > > > +/** > > > > + Get character from stream, we don't support file operastion on e= dk2 > > > JSON library. > > > > + > > > > + @return Returns the character currently pointed by the internal = file > > > position indicator of the specified stream > > > > + > > > > +**/ > > > > +int fgetc(FILE * _File){ > > > > + return 0; > > > > +} > > > > diff --git a/RedfishPkg/Library/JsonLib/JanssonCrtLibSupport.h > > > b/RedfishPkg/Library/JsonLib/JanssonCrtLibSupport.h > > > > new file mode 100644 > > > > index 0000000000..40a98e485c > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/JanssonCrtLibSupport.h > > > > @@ -0,0 +1,196 @@ > > > > +/** @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) > > > > > > MIN_INT64 > > Define MIN_IN64 for "(-9223372036854775807LL - 1LL) >=20 > As in, use the existing definitions from BaseLib. Thanks for the infomation >=20 > > > > > > > + > > > > +// Maximum value for an object of type long long int. > > > > +#define LLONG_MAX 9223372036854775807LL // 2^63 - 1 > > > > > > MAX_INT64 > > Define MAX _IN64 for "(9223372036854775807LL - 1LL)"? >=20 > #define LLONG_MAX MAX_INT64 >=20 > > > > + > > > > +// We dont support double on edk2 > > > > +#define HUGE_VAL 0 > > > > + > > > > +#if defined(MDE_CPU_X64) || defined(MDE_CPU_AARCH64) || > > > defined(MDE_CPU_IA64) || defined(MDE_CPU_RISCV64) > > > > +// > > > > +// 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_EBC) > > > > +#define THIRTY_TWO_BIT > > > > +#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 valu= e */ > > > > +#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 = */ > > > > > > Take from ProcessorBind.h? > > > EINVAL should be separated frome variable properties by at least a > > > blank line. > > Will check this. > > > > > > > + > > > > +// 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 ** = __restrict > > > 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, a= nd > > > DebugLib functions > > > > +// > > > > +#define strcmp AsciiStrCmp > > > > +#define memcpy(dest,source,count) > > > CopyMem(dest,source,(UINTN)(count)) > > > > +#define memset(dest,ch,count) > > > SetMem(dest,(UINTN)(count),(UINT8)(ch)) > > > > +#define memchr(buf,ch,count) > > > ScanMem8(buf,(UINTN)(count),(UINT8)ch) > > > > +#define memcmp(buf1,buf2,count) > > > (int)(CompareMem(buf1,buf2,(UINTN)(count))) > > > > +#define memmove(dest,source,count) > > > CopyMem(dest,source,(UINTN)(count)) > > > > +#define strlen(str) > > > (size_t)(AsciiStrnLenS(str,MAX_STRING_SIZE)) > > > > +#define strcpy(strDest,strSource) > > > AsciiStrCpyS(strDest,(strlen(strSource)+1),strSource) > > > > +#define strncpy(strDest,strSource,count) > > > AsciiStrnCpyS(strDest,(UINTN)count,strSource,(UINTN)count) > > > > +#define strncpys(strDest, DestLen, strSource,count) > > > AsciiStrnCpyS(strDest,DestLen,strSource,(UINTN)count) > > > > +#define strcat(strDest,strSource) > > > AsciiStrCatS(strDest,(strlen(strSource)+strlen(strDest)+1),strSource) > > > > +#define strchr(str,ch) ScanMem8((VOID > > > *)(str),AsciiStrSize(str),(UINT8)ch) > > > > +#define strncmp(string1,string2,count) > > > (int)(AsciiStrnCmp(string1,string2,(UINTN)(count))) > > > > +#define strcasecmp(str1,str2) (int)AsciiStriCmp(str1,s= tr2) > > > > +#define strstr(s1,s2) AsciiStrStr(s1,s2) > > > > +#define sprintf(buf,...) > > > AsciiSPrint(buf,MAX_STRING_SIZE,__VA_ARGS__) > > > > +#define snprintf(buf,len,...) AsciiSPrint(buf,len,__VA= _ARGS__) > > > > +#define vsnprintf(buf,len,format,marker) > > > AsciiVSPrint((buf),(len),(format),(marker)) > > > > +#define assert(expression) > > > > > > Not ASSERT? > > Will add ASSERT to this macro. > > > > > > > +#define atoi(nptr) AsciiStrDecimalToUintn(n= ptr) > > > > +#define fabs(x) (((x)<0.0)?(-x):(x)) > > > > > > Indentation. > > ok > > > > > > > +#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/RedfishPkg/Library/JsonLib/JsonLib.c > > > b/RedfishPkg/Library/JsonLib/JsonLib.c > > > > new file mode 100644 > > > > index 0000000000..3fa834add3 > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/JsonLib.c > > > > @@ -0,0 +1,960 @@ > > > > +/** @file > > > > + APIs for JSON operations. > > > > + > > > > + Copyright (c) 2018 - 2019, Intel Corporation. All rights reserve= d.
> > > > + (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 > > > successfully, > > > > + since they are the only valid root values of a JSON text for UEF= I usage. > > > > + > > > > + 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 s= tring to > > > convert > > > > + > > > > + @retval Array JSON value or object JSON value, or NULL when= any > > > error 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 converti= ng, > and > > > 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); > > > > > > No space after cast (please sanity check throughout). > > ok > > > > > > > +} > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > array, > > > > + or NULL on error. Initially, the array is empty. > > > > + > > > > + The reference count of this value will be set to 1, and caller n= eeds to > > > cleanup the > > > > + value by calling JsonValueFree(). > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @retval The created JSON value which contains a JSON array = or > NULL. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitArray ( > > > > + VOID > > > > + ) > > > > +{ > > > > + return (EDKII_JSON_VALUE) json_array(); > > > > +} > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > object, > > > > + or NULL on error. Initially, the object is empty. > > > > + > > > > + The reference count of this value will be set to 1, and caller n= eeds to > > > cleanup the > > > > + value by calling JsonValueFree(). > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @retval The created JSON value which contains a JSON object= or > NULL. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitObject ( > > > > + VOID > > > > + ) > > > > +{ > > > > + return (EDKII_JSON_VALUE) json_object(); > > > > +} > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > string, > > > > + or NULL on error. > > > > + > > > > + The input string must be NULL terminated Ascii format, non-Ascii > > > characters will > > > > + be processed as an error. Unicode characters can also be > represented by > > > Ascii string > > > > + as the format: \u + 4 hexadecimal digits, like \u3E5A, or \u003F. > > > > + > > > > + The reference count of this value will be set to 1, and caller n= eeds to > > > cleanup the > > > > + value by calling JsonValueFree(). > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] String The Ascii string to initialize to JSON = value > > > > + > > > > + @retval The created JSON value which contains a JSON string= or > NULL. > > > Select a > > > > + Getter API for a specific encoding format. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitAsciiString ( > > > > + IN CONST CHAR8 *String > > > > + ) > > > > +{ > > > > + UINTN Index; > > > > + > > > > + if (String =3D=3D NULL) { > > > > + return NULL; > > > > + } > > > > + > > > > + Index =3D 0; > > > > + while (*(String + Index) !=3D '\0') { > > > > + if (((*(String + Index)) & 0x80) !=3D 0x00) { > > > > + return NULL; > > > > + } > > > > + > > > > + Index ++; > > > > > > No space before ++. > > ok > > > > > > > + } > > > > + > > > > + return (EDKII_JSON_VALUE) json_string (String); > > > > +} > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > string, > > > > + or NULL on error. > > > > + > > > > + The input must be a NULL terminated UCS2 format Unicode string. > > > > + > > > > + The reference count of this value will be set to 1, and caller n= eeds to > > > cleanup the > > > > + value by calling JsonValueFree(). > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] String The Unicode string to initialize to JSO= N value > > > > + > > > > + @retval The created JSON value which contains a JSON string= or > NULL. > > > Select a > > > > + Getter API for a specific encoding format. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitUnicodeString ( > > > > + IN CHAR16 *String > > > > + ) > > > > +{ > > > > + EFI_STATUS Status; > > > > + CHAR8 *Utf8Str; > > > > + > > > > + if (String =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 > JSON > > > integer, > > > > + or NULL on error. > > > > + > > > > + The reference count of this value will be set to 1, and caller n= eeds to > > > cleanup the > > > > + value by calling JsonValueFree(). > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] Value The integer to initialize to JSON value > > > > + > > > > + @retval The created JSON value which contains a JSON number= or > > > NULL. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitNumber ( > > > > + IN INT64 Value > > > > + ) > > > > +{ > > > > + return (EDKII_JSON_VALUE) json_integer (Value); > > > > +} > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > boolean, > > > > + or NULL on error. > > > > + > > > > + Boolean JSON value is kept as static value, and no need to do any > > > cleanup work. > > > > + > > > > + @param[in] Value The boolean value to initialize. > > > > + > > > > + @retval The created JSON value which contains a JSON boolea= n or > > > NULL. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitBoolean ( > > > > + IN BOOLEAN Value > > > > + ) > > > > +{ > > > > + return (EDKII_JSON_VALUE) json_boolean (Value); > > > > +} > > > > + > > > > +/** > > > > + The function is used to initialize a JSON value which contains a= new > JSON > > > NULL, > > > > + or NULL on error. > > > > + > > > > + NULL JSON value is kept as static value, and no need to do any > cleanup > > > work. > > > > + > > > > + @retval The created NULL JSON value. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueInitNull ( > > > > + VOID > > > > + ) > > > > +{ > > > > + return (EDKII_JSON_VALUE) json_null(); > > > > +} > > > > + > > > > +/** > > > > + The function is used to decrease the reference count of a JSON v= alue > by > > > one, and once > > > > + this reference count drops to zero, the value is destroyed and i= t can > no > > > longer be used. > > > > + If this destroyed value is object type or array type, reference = counts > for > > > all containing > > > > + JSON values will be decreased by 1. Boolean JSON value and NULL > JSON > > > value won't be destroyed > > > > + since they are static values kept in memory. > > > > + > > > > + Reference Count Strategy: BaseJsonLib uses this strategy to track > > > whether a value is still > > > > + in use or not. When a value is created, it's reference count is = set to 1. > If a > > > reference to a > > > > + value is kept for use, its reference count is incremented, and w= hen > the > > > value is no longer > > > > + needed, the reference count is decremented. When the reference > > > count drops to zero, there are > > > > + no references left, and the value can be destroyed. > > > > + > > > > + The given JSON value maybe NULL and not causing any problem. Just > > > output the debug message > > > > + to inform caller the NULL value is passed in. > > > > + > > > > + @param[in] Json The JSON value to be freed. json_d= ecref > may > > > return without any > > > > + changes if Json is NULL. > > > > + > > > > +**/ > > > > +VOID > > > > +EFIAPI > > > > +JsonValueFree ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + json_decref((json_t *)Json); > > > > +} > > > > + > > > > +/** > > > > + The function is used to create a fresh copy of a JSON value, and= all > child > > > values are deep > > > > + copied in a recursive fashion. It should be called when this JSO= N value > > > might be modified > > > > + in later use, but the original still wants to be used in somewhe= re else. > > > > + > > > > + Reference counts of the returned root JSON value and all child v= alues > 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, t= oo > many > > > calls may cause some > > > > + performance issues, user should avoid unnecessary calls to this > function > > > unless it is really > > > > + needed. > > > > + > > > > + @param[in] Json The JSON value to be cloned. > > > > + > > > > + @retval Return the cloned JSON value, or NULL on error. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonValueClone ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + return (EDKII_JSON_VALUE) json_deep_copy ((json_t *) Json); > > > > +} > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON value contai= ns a > JSON > > > array. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value contains a JSON arr= ay. > > > > + @retval FALSE The JSON value doesn't contain a J= SON array. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsArray ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + return json_is_array ((json_t *) Json); > > > > +} > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON value contai= ns a > JSON > > > object. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value contains a JSON obj= ect. > > > > + @retval FALSE The JSON value doesn't contain a J= SON object. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsObject ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + return json_is_object ((json_t *) Json); > > > > +} > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON Value contai= ns a > > > string, Ascii or > > > > + Unicode format is not differentiated. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value contains a JSON str= ing. > > > > + @retval FALSE The JSON value doesn't contain a J= SON string. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsString ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + return json_is_string ((json_t *) Json); > > > > +} > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON value contai= ns a > JSON > > > number. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value is contains JSON nu= mber. > > > > + @retval FALSE The JSON value doesn't contain a J= SON > number. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsNumber ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + return json_is_integer ((json_t *) Json); > > > > +} > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON value contai= ns a > JSON > > > boolean. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value contains a JSON boo= lean. > > > > + @retval FALSE The JSON value doesn't contain a J= SON > boolean. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsBoolean ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + return json_is_boolean ((json_t *) Json); > > > > +} > > > > + > > > > +/** > > > > + The function is used to return if the provided JSON value contai= ns a > JSON > > > NULL. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval TRUE The JSON value contains a JSON NUL= L. > > > > + @retval FALSE The JSON value doesn't contain a J= SON NULL. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueIsNull ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + return json_is_null ((json_t *) Json); > > > > +} > > > > + > > > > +/** > > > > + The function is used to retrieve the associated array in an arra= y 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 obj= ect > type > > > JSON value. > > > > + > > > > + Any changes to the returned object will impact the original JSON > value. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval Return the associated object in JSON value or NULL. > > > > + > > > > +**/ > > > > +EDKII_JSON_OBJECT > > > > +EFIAPI > > > > +JsonValueGetObject ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + if (Json =3D=3D NULL || !JsonValueIsObject (Json)) { > > > > + return NULL; > > > > + } > > > > + > > > > + return (EDKII_JSON_OBJECT) Json; > > > > +} > > > > + > > > > +/** > > > > + The function is used to retrieve the associated Ascii string in = a string > type > > > JSON value. > > > > + > > > > + Any changes to the returned string will impact the original JSON= value. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval Return the associated Ascii string in JSON value or= NULL. > > > > + > > > > +**/ > > > > +CHAR8* > > > > +EFIAPI > > > > +JsonValueGetAsciiString ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + CHAR8 *AsciiStr; > > > > + UINTN Index; > > > > + > > > > + AsciiStr =3D (CHAR8 *) ((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 i= n a > string > > > type JSON value. > > > > + > > > > + Caller can do any changes to the returned string without any imp= act > 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 > NULL. > > > > + > > > > +**/ > > > > +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 ((CHAR8*)Utf8Str, &Ucs2Str); > > > > + if (EFI_ERROR (Status)) { > > > > + return NULL; > > > > + } > > > > + > > > > + return Ucs2Str; > > > > +} > > > > + > > > > +/** > > > > + The function is used to retrieve the associated integer in a num= ber > type > > > JSON value. > > > > + > > > > + The input JSON value should not be NULL or contain no JSON numbe= r, > > > otherwise it will > > > > + ASSERT() and return 0. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval Return the associated number in JSON value. > > > > + > > > > +**/ > > > > +INT64 > > > > +EFIAPI > > > > +JsonValueGetNumber ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + ASSERT (Json !=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 boo= lean > type > > > JSON value. > > > > + > > > > + The input JSON value should not be NULL or contain no JSON boole= an, > > > otherwise it will > > > > + ASSERT() and return FALSE. > > > > + > > > > + @param[in] Json The provided JSON value. > > > > + > > > > + @retval Return the associated value of JSON boolean. > > > > + > > > > +**/ > > > > +BOOLEAN > > > > +EFIAPI > > > > +JsonValueGetBoolean ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + ASSERT (Json !=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 retrieve the associated 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. > > > > + > > > > +**/ > > > > +CONST CHAR8* > > > > +EFIAPI > > > > +JsonValueGetString ( > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + return json_string_value ((const json_t *)Json); > > > > +} > > > > + > > > > +/** > > > > + The function is used to get the number of elements in a JSON obj= ect, > or 0 > > > if it is NULL or > > > > + not a JSON object. > > > > + > > > > + @param[in] JsonObject The provided JSON object. > > > > + > > > > + @retval Return the number of elements in this JSON object o= r 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 > enumeration. > > > > + @param[out] KeyCount The count of keys in this JS= ON object. > > > > + > > > > + @retval Return an array of the enumerated keys in this JSON > object 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 (CONST CHAR8 **) AllocateZeroPool (*KeyCount * size= of > > > (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 (CHAR8 **)KeyArray; > > > > +} > > > > + > > > > +/** > > > > + The function is used to get a JSON value corresponding to the in= put > key > > > from a JSON object. > > > > + > > > > + It only returns a reference to this value and any changes on thi= s value > will > > > impact the > > > > + original JSON object. If that is not expected, please call > JsonValueClone() > > > to clone it to > > > > + use. > > > > + > > > > + Input key must be a valid NULL terminated UTF8 encoded string. > NULL > > > will be returned when > > > > + Key-Value is not found in this JSON object. > > > > + > > > > + @param[in] JsonObj The provided JSON object. > > > > + @param[in] Key The key of the JSON value to be r= etrieved. > > > > + > > > > + @retval Return the corresponding JSON value to key, or NULL= on > error. > > > > + > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonObjectGetValue ( > > > > + IN CONST EDKII_JSON_OBJECT JsonObj, > > > > + IN CONST CHAR8 *Key > > > > + ) > > > > +{ > > > > + return (EDKII_JSON_VALUE) json_object_get ((const json_t > *)JsonObj, > > > (const char *)Key); > > > > +} > > > > + > > > > +/** > > > > + The function is used to set a JSON value corresponding to the in= put > key > > > from a JSON object, > > > > + and the reference count of this value will be increased by 1. > > > > + > > > > + Input key must be a valid NULL terminated UTF8 encoded string. If > there > > > already is a value for > > > > + this key, this key will be assigned to the new JSON value. The o= ld > JSON > > > value will be removed > > > > + from this object and thus its' reference count will be decreased= by 1. > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] JsonObj The provided JSON object. > > > > + @param[in] Key The key of the JSON value to= be set. > > > > + @param[in] Json The JSON value to set to thi= s JSON object > > > mapped by key. > > > > + > > > > + @retval EFI_ABORTED Some error occur and operati= on > aborted. > > > > + @retval EFI_SUCCESS The JSON value has been set = to this > JSON > > > object. > > > > + > > > > +**/ > > > > +EFI_STATUS > > > > +EFIAPI > > > > +JsonObjectSetValue ( > > > > + IN EDKII_JSON_OBJECT JsonObj, > > > > + IN CONST CHAR8 *Key, > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + if (json_object_set ((json_t *) JsonObj, Key, (json_t *) Json) != =3D 0) { > > > > + return EFI_ABORTED; > > > > + } else { > > > > + return EFI_SUCCESS; > > > > + } > > > > +} > > > > + > > > > +/** > > > > + The function is used to get the number of elements in a JSON arr= ay, > 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 po= sition > > > 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 thi= s value > will > > > impact the > > > > + original JSON object. If that is not expected, please call > JsonValueClone() > > > to clone it to > > > > + use. > > > > + > > > > + If this array is NULL or not a JSON array, or if index is out of= range, > NULL > > > will be returned. > > > > + > > > > + @param[in] JsonArray The provided JSON Array. > > > > + > > > > + @retval Return the JSON value located in the Index position= or > NULL. > > > > + > > > > +**/ > > > > +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 JS= ON > array, > > > and grow the size of > > > > + array by 1. The reference count of this value will be increased = by 1. > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] JsonArray The provided JSON object. > > > > + @param[in] Json The JSON value to append. > > > > + > > > > + @retval EFI_ABORTED Some error occur and operati= on > aborted. > > > > + @retval EFI_SUCCESS JSON value has been appended= to the > end > > > of the JSON array. > > > > + > > > > +**/ > > > > +EFI_STATUS > > > > +EFIAPI > > > > +JsonArrayAppendValue ( > > > > + IN EDKII_JSON_ARRAY JsonArray, > > > > + IN EDKII_JSON_VALUE Json > > > > + ) > > > > +{ > > > > + if (json_array_append ((json_t *) JsonArray, (json_t *) Json) != =3D 0) { > > > > + return EFI_ABORTED; > > > > + } else { > > > > + return EFI_SUCCESS; > > > > + } > > > > +} > > > > + > > > > +/** > > > > + The function is used to remove a JSON value at position index, > shifting > > > the elements after index > > > > + one position towards the start of the array. The reference count= of > this > > > value will be decreased > > > > + by 1. > > > > > > These comment blocks all seem wrapped at 100 characters. > > > Plese rewrap at 80. The occasional overspill is fine, but this is con= sistent. > > > > > > > + > > > > + More details for reference count strategy can refer to the API > > > description for JsonValueFree(). > > > > + > > > > + @param[in] JsonArray The provided JSON array. > > > > + @param[in] Index The Index position before re= movement. > > > > + > > > > + @retval EFI_ABORTED Some error occur and operati= on > aborted. > > > > + @retval EFI_SUCCESS The JSON array has been remo= ved 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; > > > > + } > > > > +} > > > > + > > > > +/** > > > > + Dump JSON to a buffer. > > > > + > > > > + @param[in] JsonValue The provided JSON array. > > > > + @param[in] Flags The Index position before removemen= t. > > > > + > > > > + @retval NULL Dump fail if NULL returned, otherwi= se the > buffer > > > > + contain JSON paylaod in ASCII strin= g. > > > > +**/ > > > > +CHAR8 * > > > > +EFIAPI > > > > +JsonDumpString ( > > > > + IN EDKII_JSON_ARRAY JsonValue, > > > > + IN UINTN Flags > > > > + ) > > > > +{ > > > > + if (JsonValue =3D=3D NULL) { > > > > + return NULL; > > > > + } > > > > + return json_dumps((json_t *)JsonValue, Flags); > > > > +} > > > > + > > > > +/** > > > > + Load JSON from a buffer. > > > > + > > > > + @param[in] Buffer Bufffer to the JSON payload > > > > + @param[in] BufferLen Length of the buffer > > > > + @param[in] Flags Flag of loading JSON buffer > > > > + @param[in] Error Pointer to error structure > > > > + > > > > + @retval EDKII_JSON_VALUE NULL means fail to load JSON payl= oad. > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonLoadBuffer ( > > > > + IN CONST CHAR8 *Buffer, > > > > + IN UINTN BufferLen, > > > > + IN UINTN Flags, > > > > + IN EDKII_JSON_ERROR *Error > > > > + ) > > > > +{ > > > > + return json_loadb(Buffer, BufferLen, Flags, (json_error_t *)Erro= r); > > > > +} > > > > + > > > > +/** > > > > + Decrease reference. > > > > + > > > > + @param[in] JsonValue JSON value > > > > +**/ > > > > +VOID > > > > +EFIAPI > > > > +JsonDecreaseReference ( > > > > + IN EDKII_JSON_VALUE JsonValue > > > > + ) > > > > +{ > > > > + json_decref (JsonValue); > > > > +} > > > > + > > > > +/** > > > > + Increase reference. > > > > + > > > > + @param[in] JsonValue JSON value > > > > + @retval EDKII_JSON_VALUE of itself > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonIncreaseReference ( > > > > + IN EDKII_JSON_VALUE JsonValue > > > > + ) > > > > +{ > > > > + return json_incref (JsonValue); > > > > +} > > > > + > > > > +/** > > > > + Returns an opaque iterator which can be used to iterate over all= key- > > > value pairs > > > > + in object, or NULL if object is empty. > > > > + > > > > + @param[in] JsonValue JSON value > > > > + @retval Iterator pointer > > > > +**/ > > > > +VOID * > > > > +EFIAPI > > > > +JsonObjectIterator ( > > > > + IN EDKII_JSON_VALUE JsonValue > > > > + ) > > > > +{ > > > > + return json_object_iter (JsonValue); > > > > +} > > > > + > > > > +/** > > > > + Extract the associated value from iterator. > > > > + > > > > + @param[in] Iterator Iterator pointer > > > > + @retval EDKII_JSON_VALUE > > > > +**/ > > > > +EDKII_JSON_VALUE > > > > +EFIAPI > > > > +JsonObjectIteratorValue ( > > > > + IN VOID *Iterator > > > > + ) > > > > +{ > > > > + return json_object_iter_value(Iterator); > > > > +} > > > > + > > > > +/** > > > > + Returns an iterator pointing to the next key-value pair in objec= t after > iter, > > > > + or NULL if the whole object has been iterated through. > > > > + > > > > + @param[in] JsonValue JSON value > > > > + @param[in] Iterator Iterator pointer > > > > + @retval Iterator pointer > > > > +**/ > > > > +VOID * > > > > +JsonObjectIteratorNext ( > > > > + IN EDKII_JSON_VALUE JsonValue, > > > > + IN VOID *Iterator > > > > + ) > > > > +{ > > > > + return json_object_iter_next(JsonValue, Iterator); > > > > +} > > > > + > > > > +/** > > > > + Returns the json type of this json value. > > > > + > > > > + @param[in] JsonValue JSON value > > > > + @retval JSON type returned > > > > +**/ > > > > +EDKII_JSON_TYPE > > > > +EFIAPI > > > > +JsonGetType ( > > > > + IN EDKII_JSON_VALUE JsonValue > > > > + ) > > > > +{ > > > > + return ((json_t *)JsonValue)->type; > > > > +} > > > > diff --git a/RedfishPkg/Library/JsonLib/JsonLib.inf > > > b/RedfishPkg/Library/JsonLib/JsonLib.inf > > > > new file mode 100644 > > > > index 0000000000..16df2ae183 > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/JsonLib.inf > > > > @@ -0,0 +1,103 @@ > > > > +## @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 JsonLib > > > > + FILE_GUID =3D F5E36815-305A-4C5A-9D75-4F214= 9E45255 > > > > + MODULE_TYPE =3D UEFI_DRIVER > > > > + VERSION_STRING =3D 1.0 > > > > + LIBRARY_CLASS =3D JsonLib | DXE_DRIVER > DXE_RUNTIME_DRIVER > > > UEFI_APPLICATION UEFI_DRIVER > > > > + > > > > +# > > > > +# VALID_ARCHITECTURES =3D IA32 X64 ARM AARCH64 RISCV64 > > > > +# > > > > + > > > > +[Sources] > > > > + # > > > > + # Below are the source code of third > > > > + # party jansson library. > > > > + # > > > > + jansson/src/dump.c > > > > + jansson/src/error.c > > > > + jansson/src/hashtable.c > > > > + jansson/src/hashtable_seed.c > > > > + jansson/src/memory.c > > > > + jansson/src/pack_unpack.c > > > > + jansson/src/strbuffer.c > > > > + jansson/src/strconv.c > > > > + jansson/src/utf.c > > > > + jansson/src/value.c > > > > + jansson/src/version.c > > > > + # > > > > + # Below are the source of edk2 JsonLib. > > > > + # > > > > + JsonLib.c > > > > + JsonUtilities.c > > > > + JsonUtilities.h > > > > + JanssonCrtLibSupport.c > > > > + JanssonCrtLibSupport.h > > > > + jansson_config.h > > > > + jansson_private_config.h > > > > + sys/time.h > > > > + sys/types.h > > > > + assert.h > > > > + errno.h > > > > + limits.h > > > > + math.h > > > > + stdarg.h > > > > + stddef.h > > > > + stdio.h > > > > + stdlib.h > > > > + string.h > > > > + time.h > > > > + # > > > > + # Below is the source code override to fix the build issue. > > > > + # Add code in load.c to conditionally use stdin according > > > > + # to HAVE_UNISTD_H macro. The PR is submitted to jansson > > > > + # open source community. > > > > + # https://github.com/akheron/jansson/pull/558 > > > > + # > > > > + load.c > > > > > > I too have concerns over this. > > > I think if we *need* to include this in edk2 tree, then we *need* to > > > dual-license it as MIT + BSD+Patent. > > Ah, I missed it. I had BSD+patent for jansson_config.h >=20 > OK, that's fine then. >=20 > > > > + > > > > +[Packages] > > > > + MdePkg/MdePkg.dec > > > > + MdeModulePkg/MdeModulePkg.dec > > > > + RedfishPkg/RedfishPkg.dec > > > > + > > > > +[LibraryClasses] > > > > + UefiLib > > > > + BaseLib > > > > + BaseMemoryLib > > > > + MemoryAllocationLib > > > > + UefiRuntimeServicesTableLib > > > > + DebugLib > > > > + PrintLib > > > > > > Please sort classes alphabetically. > > sure > > > > > > > + > > > > +[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 > initializer > > > > + # C4267: 'var' : conversion from 'size_t' to 'type', possible = loss of data > > > > + # > > > > + # Define macro HAVE_CONFIG_H to include > jansson_private_config.h to > > > build. > > > > + # Undefined _WIN32, WIN64, _MSC_VER macros > > > > + # On GCC, no error on the unused-function and unused-but-set- > variable. > > > > + # > > > > + MSFT:*_*_*_CC_FLAGS =3D /wd4204 /wd4267 /wd4702 /wd4706 > /wd4244 > > > /wd4090 /wd4456 /wd4334 /DHAVE_CONFIG_H=3D1 /U_WIN32 /UWIN64 > > > /U_MSC_VER > > > > + GCC:*_*_*_CC_FLAGS =3D -Wno-unused-function -Wno-unused-but- > set- > > > variable > > > > > > Unused but set variable warning traps many real errors. > > > Are you finding lots of these in the upstream project? > > yes. one error of unused-but-set-variable, many of unused-function. >=20 >=20 >=20 > > > > + > > > > diff --git a/RedfishPkg/Library/JsonLib/JsonUtilities.c > > > b/RedfishPkg/Library/JsonLib/JsonUtilities.c > > > > new file mode 100644 > > > > index 0000000000..f84a133219 > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/JsonUtilities.c > > > > @@ -0,0 +1,417 @@ > > > > +/** @file > > > > + Utility functions for JSON operations. > > > > + > > > > + Copyright (c) 2018 - 2019, Intel Corporation. All rights reserve= d.
> > > > + (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 > > > characters, > > > > + this function is used to retrieve the UTF8 encoding size for a U= CS2 > > > character. > > > > + > > > > + @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. > > > > + > > > > +**/ > > > > > > At least some of these look like generically useful functions. > > > Consider including in MdeModulePkg? > > > > That is a good idea. This should be the totally new library and > > header file under MdeModulePkg right? Or can we add these new > > functions to the existing lib? >=20 > I mean, I'm kind of tempted to stuff them into BaseLib, but > MdeModulePkg maintainers may violently disagree :) >=20 > A separate BaseUcs2Utf8Lib would work too. Ok, just do it this way. >=20 > > > If not, or not right away, at least give it a more appropriate generic > > > name and break it out as a separate library, and commit, for UCS2-UTF8 > > > operations. > > > > > > > +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: \uXX= XX, > > > this 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 > buffer. > > > > + > > > > + @param[in] Utf8Buffer The buffer for UTF8 encoded= data. > > > > + @param[out] Ucs2Char The converted UCS2 characte= r. > > > > + > > > > + @retval EFI_INVALID_PARAMETER Non-Ascii characters found = in > the > > > hexadecimal > > > > + digits string, and can't be= converted to a UCS2 > > > > + character. > > > > + @retval EFI_SUCCESS The UCS2 character has been= retrieved. > > > > + > > > > +**/ > > > > +EFI_STATUS > > > > +GetUCS2CharByFormat ( > > > > + IN CHAR8 *Utf8Buffer, > > > > + OUT CHAR16 *Ucs2Char > > > > + ) > > > > +{ > > > > + UINT8 Num1; > > > > + UINT8 Num2; > > > > + UINT8 Index; > > > > + CHAR8 Ucs2CharFormat[JSON_UNICODE_FORMAT_CHAR_SIZE]; > /// > > > two Hexadecimal 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 > character. > > > > + > > > > +**/ > > > > +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 d= ata. > > > > + @param[out] Ucs2Char The converted UCS2 characte= r. > > > > + > > > > + @retval EFI_INVALID_PARAMETER The UTF8 encoded string is = not > > > valid or > > > > + not for UCS2 character. > > > > + @retval EFI_SUCCESS The converted UCS2 characte= r. > > > > + > > > > +**/ > > > > +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 a= ddress. > > > Caller > > > > + is responsible for Free thi= s string. > > > > + > > > > + @retval EFI_INVALID_PARAMETER One or more parameters are > > > invalid. > > > > + @retval EFI_OUT_OF_RESOURCES System runs out of resource= s. > > > > + @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; Ucs2StrIn= dex > ++) { > > > > + > > > > + 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; Ucs2StrIn= dex > ++) { > > > > + > > > > + 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 s= tring. > > > > + @param[out] Ucs2StrAddr The converted UCS2 string a= ddress. > > > Caller > > > > + is responsible for Free thi= s string. > > > > + > > > > + @retval EFI_INVALID_PARAMETER The UTF8 encoded string is = not > > > valid to > > > > + convert to UCS2 string. > > > > + One or more parameters are = invalid. > > > > + @retval EFI_OUT_OF_RESOURCES System runs out of resource= s. > > > > + @retval EFI_SUCCESS The UCS2 string has been co= nverted. > > > > + > > > > +**/ > > > > +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, > Ucs2StrTemp > > > + 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 + Utf8StrInde= x); > > > > + if (Utf8BufferSize =3D=3D 0 || Utf8StrLength - Utf8StrIndex < > > > Utf8BufferSize) { > > > > + > > > > + FreePool (Ucs2StrTemp); > > > > + return EFI_INVALID_PARAMETER; > > > > + } > > > > + > > > > + Status =3D UTF8ToUCS2Char (Utf8Str + Utf8StrIndex, Ucs2StrTe= mp + > > > Ucs2StrIndex); > > > > + 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/RedfishPkg/Library/JsonLib/JsonUtilities.h > > > b/RedfishPkg/Library/JsonLib/JsonUtilities.h > > > > new file mode 100644 > > > > index 0000000000..1aaa4d31e2 > > > > --- /dev/null > > > > +++ b/RedfishPkg/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 JSON_UTILITIES_H_ > > > > +#define 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 a= ddress. > > > Caller > > > > + is responsible for Free thi= s string. > > > > + > > > > + @retval EFI_INVALID_PARAMETER One or more parameters are > > > invalid. > > > > + @retval EFI_OUT_OF_RESOURCES System runs out of resource= s. > > > > + @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 s= tring. > > > > + @param[out] Ucs2StrAddr The converted UCS2 string a= ddress. > > > Caller > > > > + is responsible for Free thi= s string. > > > > + > > > > + @retval EFI_INVALID_PARAMETER The UTF8 encoded string is = not > > > valid to > > > > + convert to UCS2 string. > > > > + One or more parameters are = invalid. > > > > + @retval EFI_OUT_OF_RESOURCES System runs out of resource= s. > > > > + @retval EFI_SUCCESS The UCS2 string has been co= nverted. > > > > + > > > > +**/ > > > > +EFI_STATUS > > > > +UTF8StrToUCS2 ( > > > > + IN CHAR8 *Utf8Str, > > > > + OUT CHAR16 **Ucs2StrAddr > > > > + ); > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/Readme.txt > > > b/RedfishPkg/Library/JsonLib/Readme.txt > > > > new file mode 100644 > > > > index 0000000000..cc149196b9 > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/Readme.txt > > > > > > .rst? > > Must be rst? >=20 > .rst or .md. Ok Abner > / > Leif >=20 > > Abner > > > > > > / > > > Leif > > > > > > > @@ -0,0 +1,40 @@ > > > > > > > > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > > + Introduction > > > > > > > > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D > > > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > > > + Jansson is a C library for encoding, decoding and manipulating J= SON > data. > > > > +Its main features and design principles are: > > > > + > > > > + - Simple and intuitive API and data model > > > > + - Comprehensive documentation > > > > + - No dependencies on other libraries > > > > + - Full Unicode support (UTF-8) > > > > + - Extensive test suite > > > > + > > > > + Jansson is licensed under the MIT license(refer to ReadMe.rst un= der > > > edk2). > > > > +It is used in production and its API is stable. It works on numero= us > > > > +platforms, including numerous Unix like systems and Windows. It's > > > suitable > > > > +for use on any system, including desktop, server, and small embedd= ed > > > systems. > > > > + > > > > + In UEFI/EDKII environment, Redfish project consumes jansson to > achieve > > > JSON > > > > +operations. > > > > + > > > > +* Jansson version on edk2: 2.13.1 > > > > + > > > > +* EDKII jansson library wrapper: > > > > + - JsonLib.h: > > > > + This is the denifitions of EDKII JSON APIs which are mapped to > > > > + jannson funcitons accordingly. > > > > + > > > > + - JanssonJsonLibMapping.h: > > > > + This is the wrapper to map funcitons and definitions used in > > > > + native jannson applications to edk2 JsonLib. This avoids the > > > > + modifications on native jannson applications to be built under > > > > + edk2 environment. > > > > + > > > > +*Known issue: > > > > + Build fail with jansson/src/load.c, add code in load.c to condi= tionally > > > > + use stdin according to HAVE_UNISTD_H macro. The PR is submitted > to > > > > + jansson open source community. > > > > + https://github.com/akheron/jansson/pull/558 > > > > + > > > > + > > > > diff --git a/RedfishPkg/Library/JsonLib/assert.h > > > b/RedfishPkg/Library/JsonLib/assert.h > > > > new file mode 100644 > > > > index 0000000000..8f9eb748e4 > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/assert.h > > > > @@ -0,0 +1,16 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > + > > > > +#ifndef JANSSON_CRT_ASSERT_H_ > > > > +#define JANSSON_CRT_ASSERT_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/errno.h > > > b/RedfishPkg/Library/JsonLib/errno.h > > > > new file mode 100644 > > > > index 0000000000..b1191b225a > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/errno.h > > > > @@ -0,0 +1,16 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > + > > > > +#ifndef JANSSON_CRT_ERRNO_H_ > > > > +#define JANSSON_CRT_ERRNO_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/jansson_config.h > > > b/RedfishPkg/Library/JsonLib/jansson_config.h > > > > new file mode 100644 > > > > index 0000000000..6b154289df > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/jansson_config.h > > > > @@ -0,0 +1,60 @@ > > > > +/** @file > > > > + * 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 f= or > > > > + * 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 substitut= ion > > > > + * by hand. > > > > + > > > > + jansson open source license, > > > > + https://github.com/akheron/jansson/blob/master/LICENSE > > > > + > > > > + (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 alw= ays > > > > + 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 buil= tins > > > > + 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/RedfishPkg/Library/JsonLib/jansson_private_config.h > > > b/RedfishPkg/Library/JsonLib/jansson_private_config.h > > > > new file mode 100644 > > > > index 0000000000..268f91ef8a > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/jansson_private_config.h > > > > @@ -0,0 +1,19 @@ > > > > +/** @file > > > > + Jansson private configurations for UEFI support. > > > > + > > > > + Copyright (c) 2019, Intel Corporation. All rights reserved.
> > > > + (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/RedfishPkg/Library/JsonLib/limits.h > > > b/RedfishPkg/Library/JsonLib/limits.h > > > > new file mode 100644 > > > > index 0000000000..1a544adece > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/limits.h > > > > @@ -0,0 +1,16 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > + > > > > +#ifndef JANSSON_CRT_LIMITS_H_ > > > > +#define JANSSON_CRT_LIMITS_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/load.c > > > b/RedfishPkg/Library/JsonLib/load.c > > > > new file mode 100644 > > > > index 0000000000..92063e63cb > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/load.c > > > > @@ -0,0 +1,1111 @@ > > > > +/* > > > > + * Copyright (c) 2009-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. > > > > + > > > > + (C) Copyright 2020 Hewlett Packard Enterprise Development LP
> > > > + > > > > + SPDX-License-Identifier: BSD-2-Clause-Patent > > > > + */ > > > > + > > > > +#ifndef _GNU_SOURCE > > > > +#define _GNU_SOURCE > > > > +#endif > > > > + > > > > +#include "jansson_private.h" > > > > + > > > > +#include > > > > +#include > > > > +#include > > > > +#include > > > > +#include > > > > +#include > > > > +#ifdef HAVE_UNISTD_H > > > > +#include > > > > +#endif > > > > + > > > > +#include "jansson.h" > > > > +#include "strbuffer.h" > > > > +#include "utf.h" > > > > + > > > > +#define STREAM_STATE_OK 0 > > > > +#define STREAM_STATE_EOF -1 > > > > +#define STREAM_STATE_ERROR -2 > > > > + > > > > +#define TOKEN_INVALID -1 > > > > +#define TOKEN_EOF 0 > > > > +#define TOKEN_STRING 256 > > > > +#define TOKEN_INTEGER 257 > > > > +#define TOKEN_REAL 258 > > > > +#define TOKEN_TRUE 259 > > > > +#define TOKEN_FALSE 260 > > > > +#define TOKEN_NULL 261 > > > > + > > > > +/* Locale independent versions of isxxx() functions */ > > > > +#define l_isupper(c) ('A' <=3D (c) && (c) <=3D 'Z') > > > > +#define l_islower(c) ('a' <=3D (c) && (c) <=3D 'z') > > > > +#define l_isalpha(c) (l_isupper(c) || l_islower(c)) > > > > +#define l_isdigit(c) ('0' <=3D (c) && (c) <=3D '9') > > > > +#define l_isxdigit(c) = \ > > > > + (l_isdigit(c) || ('A' <=3D (c) && (c) <=3D 'F') || ('a' <=3D (= c) && (c) <=3D 'f')) > > > > + > > > > +/* Read one byte from stream, convert to unsigned char, then int, = and > > > > + return. return EOF on end of file. This corresponds to the > > > > + behaviour of fgetc(). */ > > > > +typedef int (*get_func)(void *data); > > > > + > > > > +typedef struct { > > > > + get_func get; > > > > + void *data; > > > > + char buffer[5]; > > > > + size_t buffer_pos; > > > > + int state; > > > > + int line; > > > > + int column, last_column; > > > > + size_t position; > > > > +} stream_t; > > > > + > > > > +typedef struct { > > > > + stream_t stream; > > > > + strbuffer_t saved_text; > > > > + size_t flags; > > > > + size_t depth; > > > > + int token; > > > > + union { > > > > + struct { > > > > + char *val; > > > > + size_t len; > > > > + } string; > > > > + json_int_t integer; > > > > + double real; > > > > + } value; > > > > +} lex_t; > > > > + > > > > +#define stream_to_lex(stream) container_of(stream, lex_t, stream) > > > > + > > > > +/*** error reporting ***/ > > > > + > > > > +static void error_set(json_error_t *error, const lex_t *lex, enum > > > json_error_code code, > > > > + const char *msg, ...) { > > > > + va_list ap; > > > > + char msg_text[JSON_ERROR_TEXT_LENGTH]; > > > > + char msg_with_context[JSON_ERROR_TEXT_LENGTH]; > > > > + > > > > + int line =3D -1, col =3D -1; > > > > + size_t pos =3D 0; > > > > + const char *result =3D msg_text; > > > > + > > > > + if (!error) > > > > + return; > > > > + > > > > + va_start(ap, msg); > > > > + vsnprintf(msg_text, JSON_ERROR_TEXT_LENGTH, msg, ap); > > > > + msg_text[JSON_ERROR_TEXT_LENGTH - 1] =3D '\0'; > > > > + va_end(ap); > > > > + > > > > + if (lex) { > > > > + const char *saved_text =3D strbuffer_value(&lex->saved_tex= t); > > > > + > > > > + line =3D lex->stream.line; > > > > + col =3D lex->stream.column; > > > > + pos =3D lex->stream.position; > > > > + > > > > + if (saved_text && saved_text[0]) { > > > > + if (lex->saved_text.length <=3D 20) { > > > > + snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,= "%s > > > near '%s'", > > > > + msg_text, saved_text); > > > > + msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] =3D '= \0'; > > > > + result =3D msg_with_context; > > > > + } > > > > + } else { > > > > + if (code =3D=3D json_error_invalid_syntax) { > > > > + /* More specific error code for premature end of f= ile. */ > > > > + code =3D json_error_premature_end_of_input; > > > > + } > > > > + if (lex->stream.state =3D=3D STREAM_STATE_ERROR) { > > > > + /* No context for UTF-8 decoding errors */ > > > > + result =3D msg_text; > > > > + } else { > > > > + snprintf(msg_with_context, JSON_ERROR_TEXT_LENGTH,= "%s > > > near end of file", > > > > + msg_text); > > > > + msg_with_context[JSON_ERROR_TEXT_LENGTH - 1] =3D '= \0'; > > > > + result =3D msg_with_context; > > > > + } > > > > + } > > > > + } > > > > + > > > > + jsonp_error_set(error, line, col, pos, code, "%s", result); > > > > +} > > > > + > > > > +/*** lexical analyzer ***/ > > > > + > > > > +static void stream_init(stream_t *stream, get_func get, void *data= ) { > > > > + stream->get =3D get; > > > > + stream->data =3D data; > > > > + stream->buffer[0] =3D '\0'; > > > > + stream->buffer_pos =3D 0; > > > > + > > > > + stream->state =3D STREAM_STATE_OK; > > > > + stream->line =3D 1; > > > > + stream->column =3D 0; > > > > + stream->position =3D 0; > > > > +} > > > > + > > > > +static int stream_get(stream_t *stream, json_error_t *error) { > > > > + int c; > > > > + > > > > + if (stream->state !=3D STREAM_STATE_OK) > > > > + return stream->state; > > > > + > > > > + if (!stream->buffer[stream->buffer_pos]) { > > > > + c =3D stream->get(stream->data); > > > > + if (c =3D=3D EOF) { > > > > + stream->state =3D STREAM_STATE_EOF; > > > > + return STREAM_STATE_EOF; > > > > + } > > > > + > > > > + stream->buffer[0] =3D c; > > > > + stream->buffer_pos =3D 0; > > > > + > > > > + if (0x80 <=3D c && c <=3D 0xFF) { > > > > + /* multi-byte UTF-8 sequence */ > > > > + size_t i, count; > > > > + > > > > + count =3D utf8_check_first(c); > > > > + if (!count) > > > > + goto out; > > > > + > > > > + assert(count >=3D 2); > > > > + > > > > + for (i =3D 1; i < count; i++) > > > > + stream->buffer[i] =3D stream->get(stream->data); > > > > + > > > > + if (!utf8_check_full(stream->buffer, count, NULL)) > > > > + goto out; > > > > + > > > > + stream->buffer[count] =3D '\0'; > > > > + } else > > > > + stream->buffer[1] =3D '\0'; > > > > + } > > > > + > > > > + c =3D stream->buffer[stream->buffer_pos++]; > > > > + > > > > + stream->position++; > > > > + if (c =3D=3D '\n') { > > > > + stream->line++; > > > > + stream->last_column =3D stream->column; > > > > + stream->column =3D 0; > > > > + } else if (utf8_check_first(c)) { > > > > + /* track the Unicode character column, so increment only if > > > > + this is the first character of a UTF-8 sequence */ > > > > + stream->column++; > > > > + } > > > > + > > > > + return c; > > > > + > > > > +out: > > > > + stream->state =3D STREAM_STATE_ERROR; > > > > + error_set(error, stream_to_lex(stream), json_error_invalid_utf= 8, > > > > + "unable to decode byte 0x%x", c); > > > > + return STREAM_STATE_ERROR; > > > > +} > > > > + > > > > +static void stream_unget(stream_t *stream, int c) { > > > > + if (c =3D=3D STREAM_STATE_EOF || c =3D=3D STREAM_STATE_ERROR) > > > > + return; > > > > + > > > > + stream->position--; > > > > + if (c =3D=3D '\n') { > > > > + stream->line--; > > > > + stream->column =3D stream->last_column; > > > > + } else if (utf8_check_first(c)) > > > > + stream->column--; > > > > + > > > > + assert(stream->buffer_pos > 0); > > > > + stream->buffer_pos--; > > > > + assert(stream->buffer[stream->buffer_pos] =3D=3D c); > > > > +} > > > > + > > > > +static int lex_get(lex_t *lex, json_error_t *error) { > > > > + return stream_get(&lex->stream, error); > > > > +} > > > > + > > > > +static void lex_save(lex_t *lex, int c) { strbuffer_append_byte(&l= ex- > > > >saved_text, c); } > > > > + > > > > +static int lex_get_save(lex_t *lex, json_error_t *error) { > > > > + int c =3D stream_get(&lex->stream, error); > > > > + if (c !=3D STREAM_STATE_EOF && c !=3D STREAM_STATE_ERROR) > > > > + lex_save(lex, c); > > > > + return c; > > > > +} > > > > + > > > > +static void lex_unget(lex_t *lex, int c) { stream_unget(&lex->stre= am, > c); } > > > > + > > > > +static void lex_unget_unsave(lex_t *lex, int c) { > > > > + if (c !=3D STREAM_STATE_EOF && c !=3D STREAM_STATE_ERROR) { > > > > +/* Since we treat warnings as errors, when assertions are turned > > > > + * off the "d" variable would be set but never used. Which is > > > > + * treated as an error by GCC. > > > > + */ > > > > +#ifndef NDEBUG > > > > + char d; > > > > +#endif > > > > + stream_unget(&lex->stream, c); > > > > +#ifndef NDEBUG > > > > + d =3D > > > > +#endif > > > > + strbuffer_pop(&lex->saved_text); > > > > + assert(c =3D=3D d); > > > > + } > > > > +} > > > > + > > > > +static void lex_save_cached(lex_t *lex) { > > > > + while (lex->stream.buffer[lex->stream.buffer_pos] !=3D '\0') { > > > > + lex_save(lex, lex->stream.buffer[lex->stream.buffer_pos]); > > > > + lex->stream.buffer_pos++; > > > > + lex->stream.position++; > > > > + } > > > > +} > > > > + > > > > +static void lex_free_string(lex_t *lex) { > > > > + jsonp_free(lex->value.string.val); > > > > + lex->value.string.val =3D NULL; > > > > + lex->value.string.len =3D 0; > > > > +} > > > > + > > > > +/* assumes that str points to 'u' plus at least 4 valid hex digits= */ > > > > +static int32_t decode_unicode_escape(const char *str) { > > > > + int i; > > > > + int32_t value =3D 0; > > > > + > > > > + assert(str[0] =3D=3D 'u'); > > > > + > > > > + for (i =3D 1; i <=3D 4; i++) { > > > > + char c =3D str[i]; > > > > + value <<=3D 4; > > > > + if (l_isdigit(c)) > > > > + value +=3D c - '0'; > > > > + else if (l_islower(c)) > > > > + value +=3D c - 'a' + 10; > > > > + else if (l_isupper(c)) > > > > + value +=3D c - 'A' + 10; > > > > + else > > > > + return -1; > > > > + } > > > > + > > > > + return value; > > > > +} > > > > + > > > > +static void lex_scan_string(lex_t *lex, json_error_t *error) { > > > > + int c; > > > > + const char *p; > > > > + char *t; > > > > + int i; > > > > + > > > > + lex->value.string.val =3D NULL; > > > > + lex->token =3D TOKEN_INVALID; > > > > + > > > > + c =3D lex_get_save(lex, error); > > > > + > > > > + while (c !=3D '"') { > > > > + if (c =3D=3D STREAM_STATE_ERROR) > > > > + goto out; > > > > + > > > > + else if (c =3D=3D STREAM_STATE_EOF) { > > > > + error_set(error, lex, json_error_premature_end_of_inpu= t, > > > > + "premature end of input"); > > > > + goto out; > > > > + } > > > > + > > > > + else if (0 <=3D c && c <=3D 0x1F) { > > > > + /* control character */ > > > > + lex_unget_unsave(lex, c); > > > > + if (c =3D=3D '\n') > > > > + error_set(error, lex, json_error_invalid_syntax, "= unexpected > > > newline"); > > > > + else > > > > + error_set(error, lex, json_error_invalid_syntax, "= control > character > > > 0x%x", > > > > + c); > > > > + goto out; > > > > + } > > > > + > > > > + else if (c =3D=3D '\\') { > > > > + c =3D lex_get_save(lex, error); > > > > + if (c =3D=3D 'u') { > > > > + c =3D lex_get_save(lex, error); > > > > + for (i =3D 0; i < 4; i++) { > > > > + if (!l_isxdigit(c)) { > > > > + error_set(error, lex, json_error_invalid_s= yntax, > > > > + "invalid escape"); > > > > + goto out; > > > > + } > > > > + c =3D lex_get_save(lex, error); > > > > + } > > > > + } else if (c =3D=3D '"' || c =3D=3D '\\' || c =3D=3D '= /' || c =3D=3D 'b' || c =3D=3D 'f' || > > > > + c =3D=3D 'n' || c =3D=3D 'r' || c =3D=3D 't= ') > > > > + c =3D lex_get_save(lex, error); > > > > + else { > > > > + error_set(error, lex, json_error_invalid_syntax, "= invalid > escape"); > > > > + goto out; > > > > + } > > > > + } else > > > > + c =3D lex_get_save(lex, error); > > > > + } > > > > + > > > > + /* the actual value is at most of the same length as the source > > > > + string, because: > > > > + - shortcut escapes (e.g. "\t") (length 2) are converted t= o 1 byte > > > > + - a single \uXXXX escape (length 6) is converted to at mo= st 3 bytes > > > > + - two \uXXXX escapes (length 12) forming an UTF-16 surrog= ate > pair > > > > + are converted to 4 bytes > > > > + */ > > > > + t =3D jsonp_malloc(lex->saved_text.length + 1); > > > > + if (!t) { > > > > + /* this is not very nice, since TOKEN_INVALID is returned = */ > > > > + goto out; > > > > + } > > > > + lex->value.string.val =3D t; > > > > + > > > > + /* + 1 to skip the " */ > > > > + p =3D strbuffer_value(&lex->saved_text) + 1; > > > > + > > > > + while (*p !=3D '"') { > > > > + if (*p =3D=3D '\\') { > > > > + p++; > > > > + if (*p =3D=3D 'u') { > > > > + size_t length; > > > > + int32_t value; > > > > + > > > > + value =3D decode_unicode_escape(p); > > > > + if (value < 0) { > > > > + error_set(error, lex, json_error_invalid_synta= x, > > > > + "invalid Unicode escape '%.6s'", p -= 1); > > > > + goto out; > > > > + } > > > > + p +=3D 5; > > > > + > > > > + if (0xD800 <=3D value && value <=3D 0xDBFF) { > > > > + /* surrogate pair */ > > > > + if (*p =3D=3D '\\' && *(p + 1) =3D=3D 'u') { > > > > + int32_t value2 =3D decode_unicode_escape(+= +p); > > > > + if (value2 < 0) { > > > > + error_set(error, lex, json_error_inval= id_syntax, > > > > + "invalid Unicode escape '%.6= s'", p - 1); > > > > + goto out; > > > > + } > > > > + p +=3D 5; > > > > + > > > > + if (0xDC00 <=3D value2 && value2 <=3D 0xDF= FF) { > > > > + /* valid second surrogate */ > > > > + value =3D > > > > + ((value - 0xD800) << 10) + (value2= - 0xDC00) + 0x10000; > > > > + } else { > > > > + /* invalid second surrogate */ > > > > + error_set(error, lex, json_error_inval= id_syntax, > > > > + "invalid Unicode '\\u%04X\\u= %04X'", value, value2); > > > > + goto out; > > > > + } > > > > + } else { > > > > + /* no second surrogate */ > > > > + error_set(error, lex, json_error_invalid_s= yntax, > > > > + "invalid Unicode '\\u%04X'", val= ue); > > > > + goto out; > > > > + } > > > > + } else if (0xDC00 <=3D value && value <=3D 0xDFFF)= { > > > > + error_set(error, lex, json_error_invalid_synta= x, > > > > + "invalid Unicode '\\u%04X'", value); > > > > + goto out; > > > > + } > > > > + > > > > + if (utf8_encode(value, t, &length)) > > > > + assert(0); > > > > + t +=3D length; > > > > + } else { > > > > + switch (*p) { > > > > + case '"': > > > > + case '\\': > > > > + case '/': > > > > + *t =3D *p; > > > > + break; > > > > + case 'b': > > > > + *t =3D '\b'; > > > > + break; > > > > + case 'f': > > > > + *t =3D '\f'; > > > > + break; > > > > + case 'n': > > > > + *t =3D '\n'; > > > > + break; > > > > + case 'r': > > > > + *t =3D '\r'; > > > > + break; > > > > + case 't': > > > > + *t =3D '\t'; > > > > + break; > > > > + default: > > > > + assert(0); > > > > + } > > > > + t++; > > > > + p++; > > > > + } > > > > + } else > > > > + *(t++) =3D *(p++); > > > > + } > > > > + *t =3D '\0'; > > > > + lex->value.string.len =3D t - lex->value.string.val; > > > > + lex->token =3D TOKEN_STRING; > > > > + return; > > > > + > > > > +out: > > > > + lex_free_string(lex); > > > > +} > > > > + > > > > +#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */ > > > > +#if JSON_INTEGER_IS_LONG_LONG > > > > +#ifdef _MSC_VER /* Microsoft Visual Studio */ > > > > +#define json_strtoint _strtoi64 > > > > +#else > > > > +#define json_strtoint strtoll > > > > +#endif > > > > +#else > > > > +#define json_strtoint strtol > > > > +#endif > > > > +#endif > > > > + > > > > +static int lex_scan_number(lex_t *lex, int c, json_error_t *error)= { > > > > + const char *saved_text; > > > > + char *end; > > > > + double doubleval; > > > > + > > > > + lex->token =3D TOKEN_INVALID; > > > > + > > > > + if (c =3D=3D '-') > > > > + c =3D lex_get_save(lex, error); > > > > + > > > > + if (c =3D=3D '0') { > > > > + c =3D lex_get_save(lex, error); > > > > + if (l_isdigit(c)) { > > > > + lex_unget_unsave(lex, c); > > > > + goto out; > > > > + } > > > > + } else if (l_isdigit(c)) { > > > > + do > > > > + c =3D lex_get_save(lex, error); > > > > + while (l_isdigit(c)); > > > > + } else { > > > > + lex_unget_unsave(lex, c); > > > > + goto out; > > > > + } > > > > + > > > > + if (!(lex->flags & JSON_DECODE_INT_AS_REAL) && c !=3D '.' && c= !=3D 'E' > && > > > c !=3D 'e') { > > > > + json_int_t intval; > > > > + > > > > + lex_unget_unsave(lex, c); > > > > + > > > > + saved_text =3D strbuffer_value(&lex->saved_text); > > > > + > > > > + errno =3D 0; > > > > + intval =3D json_strtoint(saved_text, &end, 10); > > > > + if (errno =3D=3D ERANGE) { > > > > + if (intval < 0) > > > > + error_set(error, lex, json_error_numeric_overflow, > > > > + "too big negative integer"); > > > > + else > > > > + error_set(error, lex, json_error_numeric_overflow,= "too big > > > integer"); > > > > + goto out; > > > > + } > > > > + > > > > + assert(end =3D=3D saved_text + lex->saved_text.length); > > > > + > > > > + lex->token =3D TOKEN_INTEGER; > > > > + lex->value.integer =3D intval; > > > > + return 0; > > > > + } > > > > + > > > > + if (c =3D=3D '.') { > > > > + c =3D lex_get(lex, error); > > > > + if (!l_isdigit(c)) { > > > > + lex_unget(lex, c); > > > > + goto out; > > > > + } > > > > + lex_save(lex, c); > > > > + > > > > + do > > > > + c =3D lex_get_save(lex, error); > > > > + while (l_isdigit(c)); > > > > + } > > > > + > > > > + if (c =3D=3D 'E' || c =3D=3D 'e') { > > > > + c =3D lex_get_save(lex, error); > > > > + if (c =3D=3D '+' || c =3D=3D '-') > > > > + c =3D lex_get_save(lex, error); > > > > + > > > > + if (!l_isdigit(c)) { > > > > + lex_unget_unsave(lex, c); > > > > + goto out; > > > > + } > > > > + > > > > + do > > > > + c =3D lex_get_save(lex, error); > > > > + while (l_isdigit(c)); > > > > + } > > > > + > > > > + lex_unget_unsave(lex, c); > > > > + > > > > + if (jsonp_strtod(&lex->saved_text, &doubleval)) { > > > > + error_set(error, lex, json_error_numeric_overflow, "real n= umber > > > overflow"); > > > > + goto out; > > > > + } > > > > + > > > > + lex->token =3D TOKEN_REAL; > > > > + lex->value.real =3D doubleval; > > > > + return 0; > > > > + > > > > +out: > > > > + return -1; > > > > +} > > > > + > > > > +static int lex_scan(lex_t *lex, json_error_t *error) { > > > > + int c; > > > > + > > > > + strbuffer_clear(&lex->saved_text); > > > > + > > > > + if (lex->token =3D=3D TOKEN_STRING) > > > > + lex_free_string(lex); > > > > + > > > > + do > > > > + c =3D lex_get(lex, error); > > > > + while (c =3D=3D ' ' || c =3D=3D '\t' || c =3D=3D '\n' || c =3D= =3D '\r'); > > > > + > > > > + if (c =3D=3D STREAM_STATE_EOF) { > > > > + lex->token =3D TOKEN_EOF; > > > > + goto out; > > > > + } > > > > + > > > > + if (c =3D=3D STREAM_STATE_ERROR) { > > > > + lex->token =3D TOKEN_INVALID; > > > > + goto out; > > > > + } > > > > + > > > > + lex_save(lex, c); > > > > + > > > > + if (c =3D=3D '{' || c =3D=3D '}' || c =3D=3D '[' || c =3D=3D '= ]' || c =3D=3D ':' || c =3D=3D ',') > > > > + lex->token =3D c; > > > > + > > > > + else if (c =3D=3D '"') > > > > + lex_scan_string(lex, error); > > > > + > > > > + else if (l_isdigit(c) || c =3D=3D '-') { > > > > + if (lex_scan_number(lex, c, error)) > > > > + goto out; > > > > + } > > > > + > > > > + else if (l_isalpha(c)) { > > > > + /* eat up the whole identifier for clearer error messages = */ > > > > + const char *saved_text; > > > > + > > > > + do > > > > + c =3D lex_get_save(lex, error); > > > > + while (l_isalpha(c)); > > > > + lex_unget_unsave(lex, c); > > > > + > > > > + saved_text =3D strbuffer_value(&lex->saved_text); > > > > + > > > > + if (strcmp(saved_text, "true") =3D=3D 0) > > > > + lex->token =3D TOKEN_TRUE; > > > > + else if (strcmp(saved_text, "false") =3D=3D 0) > > > > + lex->token =3D TOKEN_FALSE; > > > > + else if (strcmp(saved_text, "null") =3D=3D 0) > > > > + lex->token =3D TOKEN_NULL; > > > > + else > > > > + lex->token =3D TOKEN_INVALID; > > > > + } > > > > + > > > > + else { > > > > + /* save the rest of the input UTF-8 sequence to get an err= or > > > > + message of valid UTF-8 */ > > > > + lex_save_cached(lex); > > > > + lex->token =3D TOKEN_INVALID; > > > > + } > > > > + > > > > +out: > > > > + return lex->token; > > > > +} > > > > + > > > > +static char *lex_steal_string(lex_t *lex, size_t *out_len) { > > > > + char *result =3D NULL; > > > > + if (lex->token =3D=3D TOKEN_STRING) { > > > > + result =3D lex->value.string.val; > > > > + *out_len =3D lex->value.string.len; > > > > + lex->value.string.val =3D NULL; > > > > + lex->value.string.len =3D 0; > > > > + } > > > > + return result; > > > > +} > > > > + > > > > +static int lex_init(lex_t *lex, get_func get, size_t flags, void *= data) { > > > > + stream_init(&lex->stream, get, data); > > > > + if (strbuffer_init(&lex->saved_text)) > > > > + return -1; > > > > + > > > > + lex->flags =3D flags; > > > > + lex->token =3D TOKEN_INVALID; > > > > + return 0; > > > > +} > > > > + > > > > +static void lex_close(lex_t *lex) { > > > > + if (lex->token =3D=3D TOKEN_STRING) > > > > + lex_free_string(lex); > > > > + strbuffer_close(&lex->saved_text); > > > > +} > > > > + > > > > +/*** parser ***/ > > > > + > > > > +static json_t *parse_value(lex_t *lex, size_t flags, json_error_t > *error); > > > > + > > > > +static json_t *parse_object(lex_t *lex, size_t flags, json_error_t > *error) { > > > > + json_t *object =3D json_object(); > > > > + if (!object) > > > > + return NULL; > > > > + > > > > + lex_scan(lex, error); > > > > + if (lex->token =3D=3D '}') > > > > + return object; > > > > + > > > > + while (1) { > > > > + char *key; > > > > + size_t len; > > > > + json_t *value; > > > > + > > > > + if (lex->token !=3D TOKEN_STRING) { > > > > + error_set(error, lex, json_error_invalid_syntax, "stri= ng or '}' > > > expected"); > > > > + goto error; > > > > + } > > > > + > > > > + key =3D lex_steal_string(lex, &len); > > > > + if (!key) > > > > + return NULL; > > > > + if (memchr(key, '\0', len)) { > > > > + jsonp_free(key); > > > > + error_set(error, lex, json_error_null_byte_in_key, > > > > + "NUL byte in object key not supported"); > > > > + goto error; > > > > + } > > > > + > > > > + if (flags & JSON_REJECT_DUPLICATES) { > > > > + if (json_object_get(object, key)) { > > > > + jsonp_free(key); > > > > + error_set(error, lex, json_error_duplicate_key, "d= uplicate > object > > > key"); > > > > + goto error; > > > > + } > > > > + } > > > > + > > > > + lex_scan(lex, error); > > > > + if (lex->token !=3D ':') { > > > > + jsonp_free(key); > > > > + error_set(error, lex, json_error_invalid_syntax, "':' = expected"); > > > > + goto error; > > > > + } > > > > + > > > > + lex_scan(lex, error); > > > > + value =3D parse_value(lex, flags, error); > > > > + if (!value) { > > > > + jsonp_free(key); > > > > + goto error; > > > > + } > > > > + > > > > + if (json_object_set_new_nocheck(object, key, value)) { > > > > + jsonp_free(key); > > > > + goto error; > > > > + } > > > > + > > > > + jsonp_free(key); > > > > + > > > > + lex_scan(lex, error); > > > > + if (lex->token !=3D ',') > > > > + break; > > > > + > > > > + lex_scan(lex, error); > > > > + } > > > > + > > > > + if (lex->token !=3D '}') { > > > > + error_set(error, lex, json_error_invalid_syntax, "'}' expe= cted"); > > > > + goto error; > > > > + } > > > > + > > > > + return object; > > > > + > > > > +error: > > > > + json_decref(object); > > > > + return NULL; > > > > +} > > > > + > > > > +static json_t *parse_array(lex_t *lex, size_t flags, json_error_t = *error) > { > > > > + json_t *array =3D json_array(); > > > > + if (!array) > > > > + return NULL; > > > > + > > > > + lex_scan(lex, error); > > > > + if (lex->token =3D=3D ']') > > > > + return array; > > > > + > > > > + while (lex->token) { > > > > + json_t *elem =3D parse_value(lex, flags, error); > > > > + if (!elem) > > > > + goto error; > > > > + > > > > + if (json_array_append_new(array, elem)) { > > > > + goto error; > > > > + } > > > > + > > > > + lex_scan(lex, error); > > > > + if (lex->token !=3D ',') > > > > + break; > > > > + > > > > + lex_scan(lex, error); > > > > + } > > > > + > > > > + if (lex->token !=3D ']') { > > > > + error_set(error, lex, json_error_invalid_syntax, "']' expe= cted"); > > > > + goto error; > > > > + } > > > > + > > > > + return array; > > > > + > > > > +error: > > > > + json_decref(array); > > > > + return NULL; > > > > +} > > > > + > > > > +static json_t *parse_value(lex_t *lex, size_t flags, json_error_t = *error) > { > > > > + json_t *json; > > > > + > > > > + lex->depth++; > > > > + if (lex->depth > JSON_PARSER_MAX_DEPTH) { > > > > + error_set(error, lex, json_error_stack_overflow, "maximum > parsing > > > depth reached"); > > > > + return NULL; > > > > + } > > > > + > > > > + switch (lex->token) { > > > > + case TOKEN_STRING: { > > > > + const char *value =3D lex->value.string.val; > > > > + size_t len =3D lex->value.string.len; > > > > + > > > > + if (!(flags & JSON_ALLOW_NUL)) { > > > > + if (memchr(value, '\0', len)) { > > > > + error_set(error, lex, json_error_null_characte= r, > > > > + "\\u0000 is not allowed without JSON= _ALLOW_NUL"); > > > > + return NULL; > > > > + } > > > > + } > > > > + > > > > + json =3D jsonp_stringn_nocheck_own(value, len); > > > > + lex->value.string.val =3D NULL; > > > > + lex->value.string.len =3D 0; > > > > + break; > > > > + } > > > > + > > > > + case TOKEN_INTEGER: { > > > > + json =3D json_integer(lex->value.integer); > > > > + break; > > > > + } > > > > + > > > > + case TOKEN_REAL: { > > > > + json =3D json_real(lex->value.real); > > > > + break; > > > > + } > > > > + > > > > + case TOKEN_TRUE: > > > > + json =3D json_true(); > > > > + break; > > > > + > > > > + case TOKEN_FALSE: > > > > + json =3D json_false(); > > > > + break; > > > > + > > > > + case TOKEN_NULL: > > > > + json =3D json_null(); > > > > + break; > > > > + > > > > + case '{': > > > > + json =3D parse_object(lex, flags, error); > > > > + break; > > > > + > > > > + case '[': > > > > + json =3D parse_array(lex, flags, error); > > > > + break; > > > > + > > > > + case TOKEN_INVALID: > > > > + error_set(error, lex, json_error_invalid_syntax, "inva= lid token"); > > > > + return NULL; > > > > + > > > > + default: > > > > + error_set(error, lex, json_error_invalid_syntax, "unex= pected > > > token"); > > > > + return NULL; > > > > + } > > > > + > > > > + if (!json) > > > > + return NULL; > > > > + > > > > + lex->depth--; > > > > + return json; > > > > +} > > > > + > > > > +static json_t *parse_json(lex_t *lex, size_t flags, json_error_t *= error) > { > > > > + json_t *result; > > > > + > > > > + lex->depth =3D 0; > > > > + > > > > + lex_scan(lex, error); > > > > + if (!(flags & JSON_DECODE_ANY)) { > > > > + if (lex->token !=3D '[' && lex->token !=3D '{') { > > > > + error_set(error, lex, json_error_invalid_syntax, "'[' = or '{' > expected"); > > > > + return NULL; > > > > + } > > > > + } > > > > + > > > > + result =3D parse_value(lex, flags, error); > > > > + if (!result) > > > > + return NULL; > > > > + > > > > + if (!(flags & JSON_DISABLE_EOF_CHECK)) { > > > > + lex_scan(lex, error); > > > > + if (lex->token !=3D TOKEN_EOF) { > > > > + error_set(error, lex, json_error_end_of_input_expected, > > > > + "end of file expected"); > > > > + json_decref(result); > > > > + return NULL; > > > > + } > > > > + } > > > > + > > > > + if (error) { > > > > + /* Save the position even though there was no error */ > > > > + error->position =3D (int)lex->stream.position; > > > > + } > > > > + > > > > + return result; > > > > +} > > > > + > > > > +typedef struct { > > > > + const char *data; > > > > + size_t pos; > > > > +} string_data_t; > > > > + > > > > +static int string_get(void *data) { > > > > + char c; > > > > + string_data_t *stream =3D (string_data_t *)data; > > > > + c =3D stream->data[stream->pos]; > > > > + if (c =3D=3D '\0') > > > > + return EOF; > > > > + else { > > > > + stream->pos++; > > > > + return (unsigned char)c; > > > > + } > > > > +} > > > > + > > > > +json_t *json_loads(const char *string, size_t flags, json_error_t = *error) > { > > > > + lex_t lex; > > > > + json_t *result; > > > > + string_data_t stream_data; > > > > + > > > > + jsonp_error_init(error, ""); > > > > + > > > > + if (string =3D=3D NULL) { > > > > + error_set(error, NULL, json_error_invalid_argument, "wrong > > > arguments"); > > > > + return NULL; > > > > + } > > > > + > > > > + stream_data.data =3D string; > > > > + stream_data.pos =3D 0; > > > > + > > > > + if (lex_init(&lex, string_get, flags, (void *)&stream_data)) > > > > + return NULL; > > > > + > > > > + result =3D parse_json(&lex, flags, error); > > > > + > > > > + lex_close(&lex); > > > > + return result; > > > > +} > > > > + > > > > +typedef struct { > > > > + const char *data; > > > > + size_t len; > > > > + size_t pos; > > > > +} buffer_data_t; > > > > + > > > > +static int buffer_get(void *data) { > > > > + char c; > > > > + buffer_data_t *stream =3D data; > > > > + if (stream->pos >=3D stream->len) > > > > + return EOF; > > > > + > > > > + c =3D stream->data[stream->pos]; > > > > + stream->pos++; > > > > + return (unsigned char)c; > > > > +} > > > > + > > > > +json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, > > > json_error_t *error) { > > > > + lex_t lex; > > > > + json_t *result; > > > > + buffer_data_t stream_data; > > > > + > > > > + jsonp_error_init(error, ""); > > > > + > > > > + if (buffer =3D=3D NULL) { > > > > + error_set(error, NULL, json_error_invalid_argument, "wrong > > > arguments"); > > > > + return NULL; > > > > + } > > > > + > > > > + stream_data.data =3D buffer; > > > > + stream_data.pos =3D 0; > > > > + stream_data.len =3D buflen; > > > > + > > > > + if (lex_init(&lex, buffer_get, flags, (void *)&stream_data)) > > > > + return NULL; > > > > + > > > > + result =3D parse_json(&lex, flags, error); > > > > + > > > > + lex_close(&lex); > > > > + return result; > > > > +} > > > > + > > > > +json_t *json_loadf(FILE *input, size_t flags, json_error_t *error)= { > > > > + lex_t lex; > > > > + const char *source; > > > > + json_t *result; > > > > +#ifdef HAVE_UNISTD_H > > > > + if (input =3D=3D stdin) > > > > + source =3D ""; > > > > + else > > > > +#endif > > > > + source =3D ""; > > > > + > > > > + jsonp_error_init(error, source); > > > > + > > > > + if (input =3D=3D NULL) { > > > > + error_set(error, NULL, json_error_invalid_argument, "wrong > > > arguments"); > > > > + return NULL; > > > > + } > > > > + > > > > + if (lex_init(&lex, (get_func)fgetc, flags, input)) > > > > + return NULL; > > > > + > > > > + result =3D parse_json(&lex, flags, error); > > > > + > > > > + lex_close(&lex); > > > > + return result; > > > > +} > > > > + > > > > +static int fd_get_func(int *fd) { > > > > +#ifdef HAVE_UNISTD_H > > > > + uint8_t c; > > > > + if (read(*fd, &c, 1) =3D=3D 1) > > > > + return c; > > > > +#endif > > > > + return EOF; > > > > +} > > > > + > > > > +json_t *json_loadfd(int input, size_t flags, json_error_t *error) { > > > > + lex_t lex; > > > > + const char *source; > > > > + json_t *result; > > > > + > > > > +#ifdef HAVE_UNISTD_H > > > > + if (input =3D=3D STDIN_FILENO) > > > > + source =3D ""; > > > > + else > > > > +#endif > > > > + source =3D ""; > > > > + > > > > + jsonp_error_init(error, source); > > > > + > > > > + if (input < 0) { > > > > + error_set(error, NULL, json_error_invalid_argument, "wrong > > > arguments"); > > > > + return NULL; > > > > + } > > > > + > > > > + if (lex_init(&lex, (get_func)fd_get_func, flags, &input)) > > > > + return NULL; > > > > + > > > > + result =3D parse_json(&lex, flags, error); > > > > + > > > > + lex_close(&lex); > > > > + return result; > > > > +} > > > > + > > > > +json_t *json_load_file(const char *path, size_t flags, json_error_t > *error) > > > { > > > > + json_t *result; > > > > + FILE *fp; > > > > + > > > > + jsonp_error_init(error, path); > > > > + > > > > + if (path =3D=3D NULL) { > > > > + error_set(error, NULL, json_error_invalid_argument, "wrong > > > arguments"); > > > > + return NULL; > > > > + } > > > > + > > > > + fp =3D fopen(path, "rb"); > > > > + if (!fp) { > > > > + error_set(error, NULL, json_error_cannot_open_file, "unabl= e to > > > open %s: %s", path, > > > > + strerror(errno)); > > > > + return NULL; > > > > + } > > > > + > > > > + result =3D json_loadf(fp, flags, error); > > > > + > > > > + fclose(fp); > > > > + return result; > > > > +} > > > > + > > > > +#define MAX_BUF_LEN 1024 > > > > + > > > > +typedef struct { > > > > + char data[MAX_BUF_LEN]; > > > > + size_t len; > > > > + size_t pos; > > > > + json_load_callback_t callback; > > > > + void *arg; > > > > +} callback_data_t; > > > > + > > > > +static int callback_get(void *data) { > > > > + char c; > > > > + callback_data_t *stream =3D data; > > > > + > > > > + if (stream->pos >=3D stream->len) { > > > > + stream->pos =3D 0; > > > > + stream->len =3D stream->callback(stream->data, MAX_BUF_LEN, > > > stream->arg); > > > > + if (stream->len =3D=3D 0 || stream->len =3D=3D (size_t)-1) > > > > + return EOF; > > > > + } > > > > + > > > > + c =3D stream->data[stream->pos]; > > > > + stream->pos++; > > > > + return (unsigned char)c; > > > > +} > > > > + > > > > +json_t *json_load_callback(json_load_callback_t callback, void *ar= g, > > > size_t flags, > > > > + json_error_t *error) { > > > > + lex_t lex; > > > > + json_t *result; > > > > + > > > > + callback_data_t stream_data; > > > > + > > > > + memset(&stream_data, 0, sizeof(stream_data)); > > > > + stream_data.callback =3D callback; > > > > + stream_data.arg =3D arg; > > > > + > > > > + jsonp_error_init(error, ""); > > > > + > > > > + if (callback =3D=3D NULL) { > > > > + error_set(error, NULL, json_error_invalid_argument, "wrong > > > arguments"); > > > > + return NULL; > > > > + } > > > > + > > > > + if (lex_init(&lex, (get_func)callback_get, flags, &stream_data= )) > > > > + return NULL; > > > > + > > > > + result =3D parse_json(&lex, flags, error); > > > > + > > > > + lex_close(&lex); > > > > + return result; > > > > +} > > > > diff --git a/RedfishPkg/Library/JsonLib/math.h > > > b/RedfishPkg/Library/JsonLib/math.h > > > > new file mode 100644 > > > > index 0000000000..9a478c0f8a > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/math.h > > > > @@ -0,0 +1,16 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > + > > > > +#ifndef JANSSON_CRT_MATH_H_ > > > > +#define JANSSON_CRT_MATH_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/stdarg.h > > > b/RedfishPkg/Library/JsonLib/stdarg.h > > > > new file mode 100644 > > > > index 0000000000..868fcd5cca > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/stdarg.h > > > > @@ -0,0 +1,15 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > +#ifndef JANSSON_CRT_STDARG_H_ > > > > +#define JANSSON_CRT_STDARG_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/stddef.h > > > b/RedfishPkg/Library/JsonLib/stddef.h > > > > new file mode 100644 > > > > index 0000000000..8250f280b3 > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/stddef.h > > > > @@ -0,0 +1,16 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > + > > > > +#ifndef JANSSON_CRT_STDDEF_H_ > > > > +#define JANSSON_CRT_STDDEF_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/stdio.h > > > b/RedfishPkg/Library/JsonLib/stdio.h > > > > new file mode 100644 > > > > index 0000000000..5aa15b969f > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/stdio.h > > > > @@ -0,0 +1,15 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > +#ifndef JANSSON_CRT_STDIO_H_ > > > > +#define JANSSON_CRT_STDIO_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/stdlib.h > > > b/RedfishPkg/Library/JsonLib/stdlib.h > > > > new file mode 100644 > > > > index 0000000000..dfbfd85655 > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/stdlib.h > > > > @@ -0,0 +1,16 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > + > > > > +#ifndef JANSSON_CRT_STDLIB_H_ > > > > +#define JANSSON_CRT_STDLIB_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/string.h > > > b/RedfishPkg/Library/JsonLib/string.h > > > > new file mode 100644 > > > > index 0000000000..691de583a3 > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/string.h > > > > @@ -0,0 +1,16 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > + > > > > +#ifndef JANSSON_CRT_STRING_H_ > > > > +#define JANSSON_CRT_STRING_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/sys/time.h > > > b/RedfishPkg/Library/JsonLib/sys/time.h > > > > new file mode 100644 > > > > index 0000000000..5039c1991c > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/sys/time.h > > > > @@ -0,0 +1,15 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > +#ifndef JANSSON_CRT_SYS_TIME_H_ > > > > +#define JANSSON_CRT_SYS_TIME_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/sys/types.h > > > b/RedfishPkg/Library/JsonLib/sys/types.h > > > > new file mode 100644 > > > > index 0000000000..f2e23cf82f > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/sys/types.h > > > > @@ -0,0 +1,15 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > +#ifndef JANSSON_CRT_SYS_TYPES_H_ > > > > +#define JANSSON_CRT_SYS_TYPES_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/Library/JsonLib/time.h > > > b/RedfishPkg/Library/JsonLib/time.h > > > > new file mode 100644 > > > > index 0000000000..dd05279b24 > > > > --- /dev/null > > > > +++ b/RedfishPkg/Library/JsonLib/time.h > > > > @@ -0,0 +1,15 @@ > > > > +/** @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 > > > > + > > > > +**/ > > > > +#ifndef JANSSON_CRT_TIME_H_ > > > > +#define JANSSON_CRT_TIME_H_ > > > > + > > > > +#include > > > > + > > > > +#endif > > > > diff --git a/RedfishPkg/RedfishPkg.ci.yaml > b/RedfishPkg/RedfishPkg.ci.yaml > > > > index 20c297ad22..78c2814d04 100644 > > > > --- a/RedfishPkg/RedfishPkg.ci.yaml > > > > +++ b/RedfishPkg/RedfishPkg.ci.yaml > > > > @@ -17,6 +17,30 @@ > > > > ], > > > > ## Both file path and directory path are accepted. > > > > "IgnoreFiles": [ > > > > + ## Below are files incorporated with open source which= are > > > > + ## not edk2 coding standard compliant. > > > > + ## > > > > + ## For jansson library open source > > > > + ## load.c is overrided from open source. > > > > + "Library/JsonLib/load.c", > > > > + ## EDKII JsonLib API to jansson API mapping definition= s. > > > > + "Include/Library/JanssonJsonLibMapping.h", > > > > + ## C runtime library for EDKII JsonLib. > > > > + "Library/JsonLib/sys", > > > > + "Library/JsonLib/JanssonCrtLibSupport.c", > > > > + "Library/JsonLib/JanssonCrtLibSupport.h", > > > > + "Library/JsonLib/assert.h", > > > > + "Library/JsonLib/errno.h", > > > > + "Library/JsonLib/jansson_config.h", > > > > + "Library/JsonLib/jansson_private_config.h", > > > > + "Library/JsonLib/limits.h", > > > > + "Library/JsonLib/math.h", > > > > + "Library/JsonLib/stdarg.h", > > > > + "Library/JsonLib/stddef.h", > > > > + "Library/JsonLib/stdio.h", > > > > + "Library/JsonLib/stdlib.h", > > > > + "Library/JsonLib/string.h", > > > > + "Library/JsonLib/time.h" > > > > ] > > > > }, > > > > "CompilerPlugin": { > > > > @@ -50,7 +74,14 @@ > > > > "IgnoreFoldersAndFiles": [] > > > > }, > > > > "LibraryClassCheck": { > > > > - "IgnoreHeaderFile": [] > > > > + "IgnoreHeaderFile": [ > > > > + ## Below header file can be used by open source > > > > + ## project which uses jansson library. Use this > > > > + ## header file to map jansson native APIs to > > > > + ## edk2 JsonLib APIs.This header file doesn't > > > > + ## have the corresponding library class. > > > > + "Include/Library/JanssonJsonLibMapping.h" > > > > + ] > > > > }, > > > > > > > > ## options defined ci/Plugin/SpellCheck > > > > -- > > > > 2.17.1 > > > >