aboutsummaryrefslogtreecommitdiff
path: root/src/json/json_helper.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2022-08-15 12:19:49 +0200
committerChristian Grothoff <christian@grothoff.org>2022-08-15 12:19:54 +0200
commit69844eacf3e43ad882c38f4d954fb5f5dd5a848b (patch)
treea915fccf9f3de905fa0682ee0ff5d5cff7da93e9 /src/json/json_helper.c
parentf3cd55336dab79f72770c0233164b4f62850d180 (diff)
downloadgnunet-69844eacf3e43ad882c38f4d954fb5f5dd5a848b.tar.gz
gnunet-69844eacf3e43ad882c38f4d954fb5f5dd5a848b.zip
add JSON routines for base64 encoded values
Diffstat (limited to 'src/json/json_helper.c')
-rw-r--r--src/json/json_helper.c65
1 files changed, 64 insertions, 1 deletions
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 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V. 3 Copyright (C) 2014-2022 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
@@ -104,6 +104,69 @@ GNUNET_JSON_spec_fixed (const char *name,
104 104
105 105
106/** 106/**
107 * Parse given JSON object to fixed size data
108 *
109 * @param cls closure, NULL
110 * @param root the json object representing data
111 * @param[out] spec where to write the data
112 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
113 */
114static enum GNUNET_GenericReturnValue
115parse_fixed64_data (void *cls,
116 json_t *root,
117 struct GNUNET_JSON_Specification *spec)
118{
119 const char *enc;
120 unsigned int len;
121 void *output;
122 size_t olen;
123
124 if (NULL == (enc = json_string_value (root)))
125 {
126 GNUNET_break_op (0);
127 return GNUNET_SYSERR;
128 }
129 len = strlen (enc);
130 output = NULL;
131 olen = GNUNET_STRINGS_base64_decode (enc,
132 len,
133 &output);
134 if (olen != spec->ptr_size)
135 {
136 GNUNET_break_op (0);
137 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
138 "Field `%s' has wrong length\n",
139 spec->field);
140 return GNUNET_SYSERR;
141 }
142 memcpy (spec->ptr,
143 output,
144 olen);
145 GNUNET_free (output);
146 return GNUNET_OK;
147}
148
149
150struct GNUNET_JSON_Specification
151GNUNET_JSON_spec_fixed64 (const char *name,
152 void *obj,
153 size_t size)
154{
155 struct GNUNET_JSON_Specification ret = {
156 .parser = &parse_fixed64_data,
157 .cleaner = NULL,
158 .cls = NULL,
159 .field = name,
160 .ptr = obj,
161 .ptr_size = size,
162 .size_ptr = NULL
163 };
164
165 return ret;
166}
167
168
169/**
107 * Parse given JSON object to variable size data 170 * Parse given JSON object to variable size data
108 * 171 *
109 * @param cls closure, NULL 172 * @param cls closure, NULL