[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnunet] branch master updated: add JSON routines for base64 encoded val
From: |
gnunet |
Subject: |
[gnunet] branch master updated: add JSON routines for base64 encoded values |
Date: |
Mon, 15 Aug 2022 12:21:48 +0200 |
This is an automated email from the git hooks/post-receive script.
grothoff pushed a commit to branch master
in repository gnunet.
The following commit(s) were added to refs/heads/master by this push:
new 69844eacf add JSON routines for base64 encoded values
69844eacf is described below
commit 69844eacf3e43ad882c38f4d954fb5f5dd5a848b
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Mon Aug 15 12:19:49 2022 +0200
add JSON routines for base64 encoded values
---
contrib/gana | 2 +-
src/include/gnunet_json_lib.h | 81 ++++++++++++++++++++++++++++++++++++++++++-
src/json/json_generator.c | 30 ++++++++++++++++
src/json/json_helper.c | 65 +++++++++++++++++++++++++++++++++-
src/json/json_pack.c | 17 +++++++++
5 files changed, 192 insertions(+), 3 deletions(-)
diff --git a/contrib/gana b/contrib/gana
index 7bfe1654e..1ed41552f 160000
--- a/contrib/gana
+++ b/contrib/gana
@@ -1 +1 @@
-Subproject commit 7bfe1654eeab7e7eacb4f6eb45ad52ffe4511c4d
+Subproject commit 1ed41552f6013e5f48c68389d60e3d5d61892a05
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h
index 2002a0130..af63c5e42 100644
--- a/src/include/gnunet_json_lib.h
+++ b/src/include/gnunet_json_lib.h
@@ -196,6 +196,29 @@ GNUNET_JSON_spec_fixed (const char *name,
GNUNET_JSON_spec_fixed (name, obj, sizeof(*obj))
+/**
+ * Variable size object (in network byte order, encoded using base64 encoding).
+ *
+ * @param name name of the JSON field
+ * @param[out] obj pointer where to write the data, must have @a size bytes
+ * @param size number of bytes expected in @a obj
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_fixed64 (const char *name,
+ void *obj,
+ size_t size);
+
+
+/**
+ * Fixed size object (in network byte order, encoded using base64 encoding).
+ *
+ * @param name name of the JSON field
+ * @param obj pointer where to write the data (type of `*obj` will determine
size)
+ */
+#define GNUNET_JSON_spec_fixed64_auto(name, obj) \
+ GNUNET_JSON_spec_fixed (name, obj, sizeof(*obj))
+
+
/**
* Variable size object (in network byte order, encoded using
* Crockford Base32hex encoding).
@@ -378,7 +401,21 @@ GNUNET_JSON_spec_rsa_signature (const char *name,
* @return json string that encodes @a data
*/
json_t *
-GNUNET_JSON_from_data (const void *data, size_t size);
+GNUNET_JSON_from_data (const void *data,
+ size_t size);
+
+
+/**
+ * Convert binary data to a JSON string with base64
+ * encoding.
+ *
+ * @param data binary data
+ * @param size size of @a data in bytes
+ * @return json string that encodes @a data
+ */
+json_t *
+GNUNET_JSON_from_data64 (const void *data,
+ size_t size);
/**
@@ -392,6 +429,17 @@ GNUNET_JSON_from_data (const void *data, size_t size);
GNUNET_JSON_from_data (ptr, sizeof(*ptr))
+/**
+ * Convert binary data to a JSON string with base64
+ * encoding.
+ *
+ * @param ptr binary data, sizeof (*ptr) must yield correct size
+ * @return json string that encodes @a data
+ */
+#define GNUNET_JSON_from_data64_auto(ptr) \
+ GNUNET_JSON_from_data64 (ptr, sizeof(*ptr))
+
+
/**
* Convert timestamp to a json string.
*
@@ -753,6 +801,37 @@ GNUNET_JSON_pack_data_varsize (const char *name,
GNUNET_JSON_pack_data_varsize (name, blob, sizeof (*blob))
+/**
+ * Generate packer instruction for a JSON field of type
+ * variable size binary blob.
+ * Use base64-encoding, instead of the more common
+ * Crockford base32-encoding.
+ *
+ * @param name name of the field to add to the object
+ * @param blob binary data to pack
+ * @param blob_size number of bytes in @a blob
+ * @return json pack specification
+ */
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_data64_varsize (const char *name,
+ const void *blob,
+ size_t blob_size);
+
+
+/**
+ * Generate packer instruction for a JSON field where the
+ * size is automatically determined from the argument.
+ * Use base64-encoding, instead of the more common
+ * Crockford base32-encoding.
+ *
+ * @param name name of the field to add to the object
+ * @param blob data to pack, must not be an array
+ * @return json pack specification
+ */
+#define GNUNET_JSON_pack_data64_auto(name,blob) \
+ GNUNET_JSON_pack_data64_varsize (name, blob, sizeof (*blob))
+
+
/**
* Generate packer instruction for a JSON field of type
* timestamp.
diff --git a/src/json/json_generator.c b/src/json/json_generator.c
index eb275712c..4fda86e32 100644
--- a/src/json/json_generator.c
+++ b/src/json/json_generator.c
@@ -49,6 +49,36 @@ GNUNET_JSON_from_data (const void *data,
}
+json_t *
+GNUNET_JSON_from_data64 (const void *data,
+ size_t size)
+{
+ char *buf = NULL;
+ json_t *json;
+ size_t len;
+
+ if ((size * 8 + 5) / 6 + 1 >=
+ GNUNET_MAX_MALLOC_CHECKED)
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+ len = GNUNET_STRINGS_base64_encode (data,
+ size,
+ &buf);
+ if (NULL == buf)
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+ json = json_stringn (buf,
+ len);
+ GNUNET_free (buf);
+ GNUNET_break (NULL != json);
+ return json;
+}
+
+
json_t *
GNUNET_JSON_from_timestamp (struct GNUNET_TIME_Timestamp stamp)
{
diff --git a/src/json/json_helper.c b/src/json/json_helper.c
index a15dc74c0..39949819f 100644
--- a/src/json/json_helper.c
+++ b/src/json/json_helper.c
@@ -1,6 +1,6 @@
/*
This file is part of GNUnet
- Copyright (C) 2014, 2015, 2016 GNUnet e.V.
+ Copyright (C) 2014-2022 GNUnet e.V.
GNUnet is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
@@ -103,6 +103,69 @@ GNUNET_JSON_spec_fixed (const char *name,
}
+/**
+ * Parse given JSON object to fixed size data
+ *
+ * @param cls closure, NULL
+ * @param root the json object representing data
+ * @param[out] spec where to write the data
+ * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
+ */
+static enum GNUNET_GenericReturnValue
+parse_fixed64_data (void *cls,
+ json_t *root,
+ struct GNUNET_JSON_Specification *spec)
+{
+ const char *enc;
+ unsigned int len;
+ void *output;
+ size_t olen;
+
+ if (NULL == (enc = json_string_value (root)))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ len = strlen (enc);
+ output = NULL;
+ olen = GNUNET_STRINGS_base64_decode (enc,
+ len,
+ &output);
+ if (olen != spec->ptr_size)
+ {
+ GNUNET_break_op (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ "Field `%s' has wrong length\n",
+ spec->field);
+ return GNUNET_SYSERR;
+ }
+ memcpy (spec->ptr,
+ output,
+ olen);
+ GNUNET_free (output);
+ return GNUNET_OK;
+}
+
+
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_fixed64 (const char *name,
+ void *obj,
+ size_t size)
+{
+ struct GNUNET_JSON_Specification ret = {
+ .parser = &parse_fixed64_data,
+ .cleaner = NULL,
+ .cls = NULL,
+ .field = name,
+ .ptr = obj,
+ .ptr_size = size,
+ .size_ptr = NULL
+ };
+
+ return ret;
+}
+
+
/**
* Parse given JSON object to variable size data
*
diff --git a/src/json/json_pack.c b/src/json/json_pack.c
index cc1ca3e97..8fc806086 100644
--- a/src/json/json_pack.c
+++ b/src/json/json_pack.c
@@ -250,6 +250,23 @@ GNUNET_JSON_pack_data_varsize (const char *name,
}
+struct GNUNET_JSON_PackSpec
+GNUNET_JSON_pack_data64_varsize (const char *name,
+ const void *blob,
+ size_t blob_size)
+{
+ struct GNUNET_JSON_PackSpec ps = {
+ .field_name = name,
+ .object = (NULL != blob)
+ ? GNUNET_JSON_from_data64 (blob,
+ blob_size)
+ : NULL
+ };
+
+ return ps;
+}
+
+
struct GNUNET_JSON_PackSpec
GNUNET_JSON_pack_timestamp (const char *name,
struct GNUNET_TIME_Timestamp t)
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnunet] branch master updated: add JSON routines for base64 encoded values,
gnunet <=