grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 2/6] jsmn: Add convenience functions


From: Patrick Steinhardt
Subject: [PATCH 2/6] jsmn: Add convenience functions
Date: Sat, 2 Nov 2019 19:06:51 +0100

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 <address@hidden>
---
 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]