From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1iQxnv-0006pa-Mz for mharc-grub-devel@gnu.org; Sat, 02 Nov 2019 14:07:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51676) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iQxns-0006oq-T9 for grub-devel@gnu.org; Sat, 02 Nov 2019 14:07:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iQxnq-0002br-IC for grub-devel@gnu.org; Sat, 02 Nov 2019 14:07:12 -0400 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]:52357) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iQxnq-0002Tb-Ap for grub-devel@gnu.org; Sat, 02 Nov 2019 14:07:10 -0400 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 641D5423 for ; Sat, 2 Nov 2019 14:07:07 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Sat, 02 Nov 2019 14:07:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm2; bh=3zXtpIqk4/uNv Hmh/1uSwq5Dbutp5/JRUKJ11bYtyb4=; b=MyzPO4I/pWDBd2DJIv9cvB8PXhpI4 sH8QZc0OyK8KQ0c2iP7lhnMXoxLvmg24Oa1DA3EiTc1idtzTL97Oigd73jXB0DZK v6YOtY9IM5fe2cRZjfeUtl5s/MUdQEgyO4vw7LE6aoXHqOrKybu2TQD2WHKVy8eL agmdns46SWUfHJFhv1yQPLX3AcUfgDc3AkHxxgQakpxwSZysIHyl0nrD4xWIRfJX 8DPy8aGY8SejTVV1H5Zyvo96n38pBYBSvN4wJvsF1jvvlC7P42f5sFSnw/6vp2w5 kMLnQdjGuF/XtFyv9nuZS4OSvP7g6KA3eji20Q2tK5vbURgtvPledUJQw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=3zXtpIqk4/uNvHmh/1uSwq5Dbutp5/JRUKJ11bYtyb4=; b=w99VYMaL pKi0UII4bU1IY3/zk1+ZVdfgFdkvC0T+Su8WqdTZ53/TSX6xQCzN5C/7W4Hqe/lP EqTEduc2Q3DF4s3Oqrooy5JRtzYGAg7cGnCJVpX96efi5OSXbRq8yqJLTMx46NAY 70O73HsLzVTb8B3eRKyDYAPKpUpB4aVISlgaZ6GOUfovxuTgK+K3krjfoaQIrIrf s03c16i66ztpB1D0jY9F8SfVvw8LjSYlnOI2mdVJKyGgXmPytPH1uuNk8RdwsDeK QfgAag+2zfHsPlwH3M+qgYV0VaQwT8ENRLUxsPjFYsB1W9EzFaBcNJ5AFxufpvY+ P1l4bWAqN3wEOQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedruddtledguddtjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhephffvufffkffojghfggfgsedtke ertdertddtnecuhfhrohhmpefrrghtrhhitghkucfuthgvihhnhhgrrhguthcuoehpshes phhkshdrihhmqeenucfkphepjeejrddukeefrddvtddvrdejfeenucfrrghrrghmpehmrg hilhhfrhhomhepphhssehpkhhsrdhimhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from NSJAIL (x4db7ca49.dyn.telefonica.de [77.183.202.73]) by mail.messagingengine.com (Postfix) with ESMTPA id 867EA80069 for ; Sat, 2 Nov 2019 14:07:06 -0400 (EDT) Received: from localhost (10.192.0.11 [10.192.0.11]) by NSJAIL (OpenSMTPD) with ESMTPSA id 542e70a6 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sat, 2 Nov 2019 18:07:03 +0000 (UTC) From: Patrick Steinhardt To: grub-devel@gnu.org Cc: Patrick Steinhardt Subject: [PATCH 2/6] jsmn: Add convenience functions Date: Sat, 2 Nov 2019 19:06:51 +0100 Message-Id: X-Mailer: git-send-email 2.23.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 64.147.123.21 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 02 Nov 2019 18:07:14 -0000 The newly added jsmn library is a really bare-bones library that focusses on simplicity. Because of that, it is lacking some functions for convenience to abstract away some of its inner workings and to make code easier to read. As such, we're now adding some functions that are going to be used by the LUKS2 implementation later on. Signed-off-by: Patrick Steinhardt --- include/grub/jsmn.h | 108 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/include/grub/jsmn.h b/include/grub/jsmn.h index cb27ca112..cd47c07fe 100644 --- a/include/grub/jsmn.h +++ b/include/grub/jsmn.h @@ -99,6 +99,34 @@ JSMN_API void jsmn_init(jsmn_parser *parser); JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len, jsmntok_t *tokens, const unsigned int num_tokens); +/** + * Get child of an object or array. + */ +JSMN_API int jsmn_get_child(const jsmntok_t **out, const jsmntok_t *object, int n); + +/** + * Get token with given key from the given JSON object. + */ +JSMN_API int jsmn_get_object(const jsmntok_t **out, const char *json, const jsmntok_t *object, const char *key); + +/** + * Get string of a given key of a JSON object. This modifies the original + * json string. + */ +JSMN_API int jsmn_get_string(const char **out, char *json, const jsmntok_t *object, const char *key); + +/** + * Get uint64 for a given key of a JSON object. This modifies the original + * json string. + */ +JSMN_API int jsmn_get_uint64(grub_uint64_t *out, char *json, const jsmntok_t *object, const char *key); + +/** + * Get uint64 for a given key of a JSON object. This modifies the original + * json string. + */ +JSMN_API int jsmn_get_int64(grub_int64_t *out, char *json, const jsmntok_t *object, const char *key); + #ifndef JSMN_HEADER /** * Allocates a fresh unused token from the token pool. @@ -462,6 +490,86 @@ JSMN_API void jsmn_init(jsmn_parser *parser) { parser->toksuper = -1; } +JSMN_API int jsmn_get_object(const jsmntok_t **out, const char *json, + const jsmntok_t *object, const char *key) { + int i, n = object->size, skip; + + if (object->type != JSMN_OBJECT) + return 1; + + object++; + + for (i = 0; i < n; i++) { + if (object->type == JSMN_STRING && + !grub_strncmp (json + object->start, key, object->end - object->start)) + { + *out = object + 1; + return 0; + } + + /* We need to skip over all immediate children of both key and value */ + for (skip = 1; skip; skip--) + skip += (object++)->size; + } + + return 1; +} + +JSMN_API int jsmn_get_child(const jsmntok_t **out, const jsmntok_t *object, int n) { + int i, skip; + + if (object->type != JSMN_OBJECT || n >= object->size) + return 1; + + object++; + + for (i = 0; i < n; i++) + for (skip = 1; skip; skip--) + skip += (object++)->size; + + *out = object; + return 0; +} + +JSMN_API int jsmn_get_string(const char **out, char *json, const jsmntok_t *object, const char *key) { + const jsmntok_t *o = object; + + if (key && jsmn_get_object (&o, json, object, key)) + return 1; + if (o->type != JSMN_STRING) + return 1; + + json[o->end] = '\0'; + *out = json + o->start; + return 0; +} + +JSMN_API int jsmn_get_uint64(grub_uint64_t *out, char *json, const jsmntok_t *object, const char *key) { + const jsmntok_t *o = object; + + if (key && jsmn_get_object (&o, json, object, key)) + return 1; + if (o->type != JSMN_STRING && o->type != JSMN_PRIMITIVE) + return 1; + + json[o->end] = '\0'; + *out = grub_strtoul (json + o->start, NULL, 10); + return 0; +} + +JSMN_API int jsmn_get_int64(grub_int64_t *out, char *json, const jsmntok_t *object, const char *key) { + const jsmntok_t *o = object; + + if (key && jsmn_get_object (&o, json, object, key)) + return 1; + if (o->type != JSMN_STRING && o->type != JSMN_PRIMITIVE) + return 1; + + json[o->end] = '\0'; + *out = grub_strtol (json + o->start, NULL, 10); + return 0; +} + #endif /* JSMN_HEADER */ #ifdef __cplusplus -- 2.23.0