aboutsummaryrefslogtreecommitdiff
path: root/src/json
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
committerng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
commitd41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb (patch)
tree9efd18ea7d425652085ed0bd5e8e45604bc5f6b9 /src/json
parenta0fce305c565c0937d917a92712f15e9c5736260 (diff)
downloadgnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.tar.gz
gnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.zip
uncrustify as demanded.
Diffstat (limited to 'src/json')
-rw-r--r--src/json/json.c126
-rw-r--r--src/json/json_generator.c168
-rw-r--r--src/json/json_gnsrecord.c233
-rw-r--r--src/json/json_helper.c725
-rw-r--r--src/json/json_mhd.c356
-rw-r--r--src/json/test_json.c192
-rw-r--r--src/json/test_json_mhd.c240
7 files changed, 1032 insertions, 1008 deletions
diff --git a/src/json/json.c b/src/json/json.c
index fe6117c20..ecdea0294 100644
--- a/src/json/json.c
+++ b/src/json/json.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2014-2017 GNUnet e.V. 3 Copyright (C) 2014-2017 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
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file json/json.c 21 * @file json/json.c
22 * @brief functions to parse JSON snippets 22 * @brief functions to parse JSON snippets
@@ -42,34 +42,34 @@
42 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 42 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
43 */ 43 */
44int 44int
45GNUNET_JSON_parse (const json_t *root, 45GNUNET_JSON_parse(const json_t *root,
46 struct GNUNET_JSON_Specification *spec, 46 struct GNUNET_JSON_Specification *spec,
47 const char **error_json_name, 47 const char **error_json_name,
48 unsigned int *error_line) 48 unsigned int *error_line)
49{ 49{
50 if (NULL == root) 50 if (NULL == root)
51 return GNUNET_SYSERR; 51 return GNUNET_SYSERR;
52 for (unsigned int i = 0; NULL != spec[i].parser; i++) 52 for (unsigned int i = 0; NULL != spec[i].parser; i++)
53 {
54 json_t *pos;
55
56 if (NULL == spec[i].field)
57 pos = (json_t *) root;
58 else
59 pos = json_object_get (root, spec[i].field);
60 if ((NULL == pos) && (spec[i].is_optional))
61 continue;
62 if ((NULL == pos) ||
63 (GNUNET_OK != spec[i].parser (spec[i].cls, pos, &spec[i])))
64 { 53 {
65 if (NULL != error_json_name) 54 json_t *pos;
66 *error_json_name = spec[i].field; 55
67 if (NULL != error_line) 56 if (NULL == spec[i].field)
68 *error_line = i; 57 pos = (json_t *)root;
69 GNUNET_JSON_parse_free (spec); 58 else
70 return GNUNET_SYSERR; 59 pos = json_object_get(root, spec[i].field);
60 if ((NULL == pos) && (spec[i].is_optional))
61 continue;
62 if ((NULL == pos) ||
63 (GNUNET_OK != spec[i].parser(spec[i].cls, pos, &spec[i])))
64 {
65 if (NULL != error_json_name)
66 *error_json_name = spec[i].field;
67 if (NULL != error_line)
68 *error_line = i;
69 GNUNET_JSON_parse_free(spec);
70 return GNUNET_SYSERR;
71 }
71 } 72 }
72 }
73 return GNUNET_OK; /* all OK! */ 73 return GNUNET_OK; /* all OK! */
74} 74}
75 75
@@ -81,7 +81,7 @@ GNUNET_JSON_parse (const json_t *root,
81 * @return spec copy of @a spec with optional bit set 81 * @return spec copy of @a spec with optional bit set
82 */ 82 */
83struct GNUNET_JSON_Specification 83struct GNUNET_JSON_Specification
84GNUNET_JSON_spec_mark_optional (struct GNUNET_JSON_Specification spec) 84GNUNET_JSON_spec_mark_optional(struct GNUNET_JSON_Specification spec)
85{ 85{
86 struct GNUNET_JSON_Specification ret = spec; 86 struct GNUNET_JSON_Specification ret = spec;
87 87
@@ -97,11 +97,11 @@ GNUNET_JSON_spec_mark_optional (struct GNUNET_JSON_Specification spec)
97 * @param spec specification of the parse operation 97 * @param spec specification of the parse operation
98 */ 98 */
99void 99void
100GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec) 100GNUNET_JSON_parse_free(struct GNUNET_JSON_Specification *spec)
101{ 101{
102 for (unsigned int i = 0; NULL != spec[i].parser; i++) 102 for (unsigned int i = 0; NULL != spec[i].parser; i++)
103 if (NULL != spec[i].cleaner) 103 if (NULL != spec[i].cleaner)
104 spec[i].cleaner (spec[i].cls, &spec[i]); 104 spec[i].cleaner(spec[i].cls, &spec[i]);
105} 105}
106 106
107 107
@@ -118,24 +118,24 @@ GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec)
118 * @return #GNUNET_OK if parsing the value worked 118 * @return #GNUNET_OK if parsing the value worked
119 */ 119 */
120static int 120static int
121set_json (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx, 121set_json(struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
122 void *scls, 122 void *scls,
123 const char *option, 123 const char *option,
124 const char *value) 124 const char *value)
125{ 125{
126 json_t **json = scls; 126 json_t **json = scls;
127 json_error_t error; 127 json_error_t error;
128 128
129 *json = json_loads (value, JSON_REJECT_DUPLICATES, &error); 129 *json = json_loads(value, JSON_REJECT_DUPLICATES, &error);
130 if (NULL == *json) 130 if (NULL == *json)
131 { 131 {
132 fprintf (stderr, 132 fprintf(stderr,
133 _ ("Failed to parse JSON in option `%s': %s (%s)\n"), 133 _("Failed to parse JSON in option `%s': %s (%s)\n"),
134 option, 134 option,
135 error.text, 135 error.text,
136 error.source); 136 error.source);
137 return GNUNET_SYSERR; 137 return GNUNET_SYSERR;
138 } 138 }
139 return GNUNET_OK; 139 return GNUNET_OK;
140} 140}
141 141
@@ -150,19 +150,19 @@ set_json (struct GNUNET_GETOPT_CommandLineProcessorContext *ctx,
150 * @param[out] val set to the JSON specified at the command line 150 * @param[out] val set to the JSON specified at the command line
151 */ 151 */
152struct GNUNET_GETOPT_CommandLineOption 152struct GNUNET_GETOPT_CommandLineOption
153GNUNET_JSON_getopt (char shortName, 153GNUNET_JSON_getopt(char shortName,
154 const char *name, 154 const char *name,
155 const char *argumentHelp, 155 const char *argumentHelp,
156 const char *description, 156 const char *description,
157 json_t **json) 157 json_t **json)
158{ 158{
159 struct GNUNET_GETOPT_CommandLineOption clo = {.shortName = shortName, 159 struct GNUNET_GETOPT_CommandLineOption clo = { .shortName = shortName,
160 .name = name, 160 .name = name,
161 .argumentHelp = argumentHelp, 161 .argumentHelp = argumentHelp,
162 .description = description, 162 .description = description,
163 .require_argument = 1, 163 .require_argument = 1,
164 .processor = &set_json, 164 .processor = &set_json,
165 .scls = (void *) json}; 165 .scls = (void *)json };
166 166
167 return clo; 167 return clo;
168} 168}
diff --git a/src/json/json_generator.c b/src/json/json_generator.c
index de6898da6..61993c86a 100644
--- a/src/json/json_generator.c
+++ b/src/json/json_generator.c
@@ -1,22 +1,22 @@
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, 2015, 2016 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
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file json/json_generator.c 21 * @file json/json_generator.c
22 * @brief helper functions for generating JSON from GNUnet data structures 22 * @brief helper functions for generating JSON from GNUnet data structures
@@ -36,15 +36,15 @@
36 * @return json string that encodes @a data 36 * @return json string that encodes @a data
37 */ 37 */
38json_t * 38json_t *
39GNUNET_JSON_from_data (const void *data, 39GNUNET_JSON_from_data(const void *data,
40 size_t size) 40 size_t size)
41{ 41{
42 char *buf; 42 char *buf;
43 json_t *json; 43 json_t *json;
44 44
45 buf = GNUNET_STRINGS_data_to_string_alloc (data, size); 45 buf = GNUNET_STRINGS_data_to_string_alloc(data, size);
46 json = json_string (buf); 46 json = json_string(buf);
47 GNUNET_free (buf); 47 GNUNET_free(buf);
48 return json; 48 return json;
49} 49}
50 50
@@ -56,22 +56,22 @@ GNUNET_JSON_from_data (const void *data,
56 * @return a json string with the timestamp in @a stamp 56 * @return a json string with the timestamp in @a stamp
57 */ 57 */
58json_t * 58json_t *
59GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp) 59GNUNET_JSON_from_time_abs(struct GNUNET_TIME_Absolute stamp)
60{ 60{
61 json_t *j; 61 json_t *j;
62 char *mystr; 62 char *mystr;
63 int ret; 63 int ret;
64 64
65 GNUNET_assert (GNUNET_OK == 65 GNUNET_assert(GNUNET_OK ==
66 GNUNET_TIME_round_abs (&stamp)); 66 GNUNET_TIME_round_abs(&stamp));
67 if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) 67 if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
68 return json_string ("/never/"); 68 return json_string("/never/");
69 ret = GNUNET_asprintf (&mystr, 69 ret = GNUNET_asprintf(&mystr,
70 "/Date(%llu)/", 70 "/Date(%llu)/",
71 (unsigned long long) (stamp.abs_value_us / (1000LL * 1000LL))); 71 (unsigned long long)(stamp.abs_value_us / (1000LL * 1000LL)));
72 GNUNET_assert (ret > 0); 72 GNUNET_assert(ret > 0);
73 j = json_string (mystr); 73 j = json_string(mystr);
74 GNUNET_free (mystr); 74 GNUNET_free(mystr);
75 return j; 75 return j;
76} 76}
77 77
@@ -83,9 +83,9 @@ GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
83 * @return a json string with the timestamp in @a stamp 83 * @return a json string with the timestamp in @a stamp
84 */ 84 */
85json_t * 85json_t *
86GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp) 86GNUNET_JSON_from_time_abs_nbo(struct GNUNET_TIME_AbsoluteNBO stamp)
87{ 87{
88 return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp)); 88 return GNUNET_JSON_from_time_abs(GNUNET_TIME_absolute_ntoh(stamp));
89} 89}
90 90
91 91
@@ -96,22 +96,22 @@ GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
96 * @return a json string with the timestamp in @a stamp 96 * @return a json string with the timestamp in @a stamp
97 */ 97 */
98json_t * 98json_t *
99GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp) 99GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
100{ 100{
101 json_t *j; 101 json_t *j;
102 char *mystr; 102 char *mystr;
103 int ret; 103 int ret;
104 104
105 GNUNET_assert (GNUNET_OK == 105 GNUNET_assert(GNUNET_OK ==
106 GNUNET_TIME_round_rel (&stamp)); 106 GNUNET_TIME_round_rel(&stamp));
107 if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) 107 if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
108 return json_string ("/forever/"); 108 return json_string("/forever/");
109 ret = GNUNET_asprintf (&mystr, 109 ret = GNUNET_asprintf(&mystr,
110 "/Delay(%llu)/", 110 "/Delay(%llu)/",
111 (unsigned long long) (stamp.rel_value_us / (1000LL * 1000LL))); 111 (unsigned long long)(stamp.rel_value_us / (1000LL * 1000LL)));
112 GNUNET_assert (ret > 0); 112 GNUNET_assert(ret > 0);
113 j = json_string (mystr); 113 j = json_string(mystr);
114 GNUNET_free (mystr); 114 GNUNET_free(mystr);
115 return j; 115 return j;
116} 116}
117 117
@@ -123,17 +123,17 @@ GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
123 * @return corresponding JSON encoding 123 * @return corresponding JSON encoding
124 */ 124 */
125json_t * 125json_t *
126GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk) 126GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
127{ 127{
128 char *buf; 128 char *buf;
129 size_t buf_len; 129 size_t buf_len;
130 json_t *ret; 130 json_t *ret;
131 131
132 buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk, 132 buf_len = GNUNET_CRYPTO_rsa_public_key_encode(pk,
133 &buf); 133 &buf);
134 ret = GNUNET_JSON_from_data (buf, 134 ret = GNUNET_JSON_from_data(buf,
135 buf_len); 135 buf_len);
136 GNUNET_free (buf); 136 GNUNET_free(buf);
137 return ret; 137 return ret;
138} 138}
139 139
@@ -145,17 +145,17 @@ GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
145 * @return corresponding JSON encoding 145 * @return corresponding JSON encoding
146 */ 146 */
147json_t * 147json_t *
148GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig) 148GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
149{ 149{
150 char *buf; 150 char *buf;
151 size_t buf_len; 151 size_t buf_len;
152 json_t *ret; 152 json_t *ret;
153 153
154 buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig, 154 buf_len = GNUNET_CRYPTO_rsa_signature_encode(sig,
155 &buf); 155 &buf);
156 ret = GNUNET_JSON_from_data (buf, 156 ret = GNUNET_JSON_from_data(buf,
157 buf_len); 157 buf_len);
158 GNUNET_free (buf); 158 GNUNET_free(buf);
159 return ret; 159 return ret;
160} 160}
161 161
@@ -167,9 +167,9 @@ GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
167 * @return corresponding JSON encoding 167 * @return corresponding JSON encoding
168 */ 168 */
169json_t * 169json_t *
170GNUNET_JSON_from_gnsrecord (const char* rname, 170GNUNET_JSON_from_gnsrecord(const char* rname,
171 const struct GNUNET_GNSRECORD_Data *rd, 171 const struct GNUNET_GNSRECORD_Data *rd,
172 unsigned int rd_count) 172 unsigned int rd_count)
173{ 173{
174 struct GNUNET_TIME_Absolute expiration_time; 174 struct GNUNET_TIME_Absolute expiration_time;
175 const char *expiration_time_str; 175 const char *expiration_time_str;
@@ -179,36 +179,36 @@ GNUNET_JSON_from_gnsrecord (const char* rname,
179 json_t *record; 179 json_t *record;
180 json_t *records; 180 json_t *records;
181 181
182 data = json_object (); 182 data = json_object();
183 json_object_set_new (data, 183 json_object_set_new(data,
184 "record_name", 184 "record_name",
185 json_string (rname)); 185 json_string(rname));
186 records = json_array (); 186 records = json_array();
187 for (int i = 0; i < rd_count; i++) 187 for (int i = 0; i < rd_count; i++)
188 { 188 {
189 value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type, 189 value_str = GNUNET_GNSRECORD_value_to_string(rd[i].record_type,
190 rd[i].data, 190 rd[i].data,
191 rd[i].data_size); 191 rd[i].data_size);
192 expiration_time = GNUNET_GNSRECORD_record_get_expiration_time(1, &rd[i]); 192 expiration_time = GNUNET_GNSRECORD_record_get_expiration_time(1, &rd[i]);
193 expiration_time_str = GNUNET_STRINGS_absolute_time_to_string (expiration_time); 193 expiration_time_str = GNUNET_STRINGS_absolute_time_to_string(expiration_time);
194 record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type); 194 record_type_str = GNUNET_GNSRECORD_number_to_typename(rd[i].record_type);
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 195 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
196 "Packing %s %s %s %d\n", 196 "Packing %s %s %s %d\n",
197 value_str, record_type_str, expiration_time_str, rd[i].flags); 197 value_str, record_type_str, expiration_time_str, rd[i].flags);
198 record = json_pack ("{s:s,s:s,s:s,s:i}", 198 record = json_pack("{s:s,s:s,s:s,s:i}",
199 "value", 199 "value",
200 value_str, 200 value_str,
201 "record_type", 201 "record_type",
202 record_type_str, 202 record_type_str,
203 "expiration_time", 203 "expiration_time",
204 expiration_time_str, 204 expiration_time_str,
205 "flag", 205 "flag",
206 rd[i].flags); 206 rd[i].flags);
207 GNUNET_assert (NULL != record); 207 GNUNET_assert(NULL != record);
208 GNUNET_free (value_str); 208 GNUNET_free(value_str);
209 json_array_append_new (records, record); 209 json_array_append_new(records, record);
210 } 210 }
211 json_object_set_new (data, "data", records); 211 json_object_set_new(data, "data", records);
212 return data; 212 return data;
213} 213}
214 214
diff --git a/src/json/json_gnsrecord.c b/src/json/json_gnsrecord.c
index 2ba4da591..5991683bb 100644
--- a/src/json/json_gnsrecord.c
+++ b/src/json/json_gnsrecord.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file json/json_gnsrecord.c 22 * @file json/json_gnsrecord.c
@@ -35,8 +35,7 @@
35#define GNUNET_JSON_GNSRECORD_RECORD_NAME "record_name" 35#define GNUNET_JSON_GNSRECORD_RECORD_NAME "record_name"
36#define GNUNET_JSON_GNSRECORD_NEVER "never" 36#define GNUNET_JSON_GNSRECORD_NEVER "never"
37 37
38struct GnsRecordInfo 38struct GnsRecordInfo {
39{
40 char **name; 39 char **name;
41 40
42 unsigned int *rd_count; 41 unsigned int *rd_count;
@@ -46,20 +45,20 @@ struct GnsRecordInfo
46 45
47 46
48static void 47static void
49cleanup_recordinfo (struct GnsRecordInfo *gnsrecord_info) 48cleanup_recordinfo(struct GnsRecordInfo *gnsrecord_info)
50{ 49{
51 if (NULL != *(gnsrecord_info->rd)) 50 if (NULL != *(gnsrecord_info->rd))
52 {
53 for (int i = 0; i < *(gnsrecord_info->rd_count); i++)
54 { 51 {
55 if (NULL != (*(gnsrecord_info->rd))[i].data) 52 for (int i = 0; i < *(gnsrecord_info->rd_count); i++)
56 GNUNET_free ((char *) (*(gnsrecord_info->rd))[i].data); 53 {
54 if (NULL != (*(gnsrecord_info->rd))[i].data)
55 GNUNET_free((char *)(*(gnsrecord_info->rd))[i].data);
56 }
57 GNUNET_free(*(gnsrecord_info->rd));
58 *(gnsrecord_info->rd) = NULL;
57 } 59 }
58 GNUNET_free (*(gnsrecord_info->rd));
59 *(gnsrecord_info->rd) = NULL;
60 }
61 if (NULL != *(gnsrecord_info->name)) 60 if (NULL != *(gnsrecord_info->name))
62 GNUNET_free (*(gnsrecord_info->name)); 61 GNUNET_free(*(gnsrecord_info->name));
63 *(gnsrecord_info->name) = NULL; 62 *(gnsrecord_info->name) = NULL;
64} 63}
65 64
@@ -73,7 +72,7 @@ cleanup_recordinfo (struct GnsRecordInfo *gnsrecord_info)
73 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 72 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
74 */ 73 */
75static int 74static int
76parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd) 75parse_record(json_t *data, struct GNUNET_GNSRECORD_Data *rd)
77{ 76{
78 struct GNUNET_TIME_Absolute abs_expiration_time; 77 struct GNUNET_TIME_Absolute abs_expiration_time;
79 struct GNUNET_TIME_Relative rel_expiration_time; 78 struct GNUNET_TIME_Relative rel_expiration_time;
@@ -84,59 +83,59 @@ parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd)
84 int unpack_state = 0; 83 int unpack_state = 0;
85 84
86 //interpret single gns record 85 //interpret single gns record
87 unpack_state = json_unpack (data, 86 unpack_state = json_unpack(data,
88 "{s:s, s:s, s:s, s?:i!}", 87 "{s:s, s:s, s:s, s?:i!}",
89 GNUNET_JSON_GNSRECORD_VALUE, 88 GNUNET_JSON_GNSRECORD_VALUE,
90 &value, 89 &value,
91 GNUNET_JSON_GNSRECORD_TYPE, 90 GNUNET_JSON_GNSRECORD_TYPE,
92 &record_type, 91 &record_type,
93 GNUNET_JSON_GNSRECORD_EXPIRATION_TIME, 92 GNUNET_JSON_GNSRECORD_EXPIRATION_TIME,
94 &expiration_time, 93 &expiration_time,
95 GNUNET_JSON_GNSRECORD_FLAG, 94 GNUNET_JSON_GNSRECORD_FLAG,
96 &flag); 95 &flag);
97 if (0 != unpack_state) 96 if (0 != unpack_state)
98 { 97 {
99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 98 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
100 "Error gnsdata object has a wrong format!\n"); 99 "Error gnsdata object has a wrong format!\n");
101 return GNUNET_SYSERR; 100 return GNUNET_SYSERR;
102 } 101 }
103 rd->record_type = GNUNET_GNSRECORD_typename_to_number (record_type); 102 rd->record_type = GNUNET_GNSRECORD_typename_to_number(record_type);
104 if (UINT32_MAX == rd->record_type) 103 if (UINT32_MAX == rd->record_type)
105 { 104 {
106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n"); 105 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n");
107 return GNUNET_SYSERR; 106 return GNUNET_SYSERR;
108 } 107 }
109 if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value (rd->record_type, 108 if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value(rd->record_type,
110 value, 109 value,
111 (void**)&rd->data, 110 (void**)&rd->data,
112 &rd->data_size)) 111 &rd->data_size))
113 { 112 {
114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n"); 113 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n");
115 return GNUNET_SYSERR; 114 return GNUNET_SYSERR;
116 } 115 }
117 116
118 if (0 == strcmp (expiration_time, GNUNET_JSON_GNSRECORD_NEVER)) 117 if (0 == strcmp(expiration_time, GNUNET_JSON_GNSRECORD_NEVER))
119 { 118 {
120 rd->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; 119 rd->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
121 } 120 }
122 else if (GNUNET_OK == 121 else if (GNUNET_OK ==
123 GNUNET_STRINGS_fancy_time_to_absolute (expiration_time, 122 GNUNET_STRINGS_fancy_time_to_absolute(expiration_time,
124 &abs_expiration_time)) 123 &abs_expiration_time))
125 { 124 {
126 rd->expiration_time = abs_expiration_time.abs_value_us; 125 rd->expiration_time = abs_expiration_time.abs_value_us;
127 } 126 }
128 else if (GNUNET_OK == 127 else if (GNUNET_OK ==
129 GNUNET_STRINGS_fancy_time_to_relative (expiration_time, 128 GNUNET_STRINGS_fancy_time_to_relative(expiration_time,
130 &rel_expiration_time)) 129 &rel_expiration_time))
131 { 130 {
132 rd->expiration_time = rel_expiration_time.rel_value_us; 131 rd->expiration_time = rel_expiration_time.rel_value_us;
133 } 132 }
134 else 133 else
135 { 134 {
136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expiration time invalid\n"); 135 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Expiration time invalid\n");
137 return GNUNET_SYSERR; 136 return GNUNET_SYSERR;
138 } 137 }
139 rd->flags = (enum GNUNET_GNSRECORD_Flags) flag; 138 rd->flags = (enum GNUNET_GNSRECORD_Flags)flag;
140 return GNUNET_OK; 139 return GNUNET_OK;
141} 140}
142 141
@@ -150,23 +149,23 @@ parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd)
150 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 149 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
151 */ 150 */
152static int 151static int
153parse_record_data (struct GnsRecordInfo *gnsrecord_info, json_t *data) 152parse_record_data(struct GnsRecordInfo *gnsrecord_info, json_t *data)
154{ 153{
155 GNUNET_assert (NULL != data); 154 GNUNET_assert(NULL != data);
156 if (! json_is_array (data)) 155 if (!json_is_array(data))
157 { 156 {
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 157 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
159 "Error gns record data JSON is not an array!\n"); 158 "Error gns record data JSON is not an array!\n");
160 return GNUNET_SYSERR; 159 return GNUNET_SYSERR;
161 } 160 }
162 *(gnsrecord_info->rd_count) = json_array_size (data); 161 *(gnsrecord_info->rd_count) = json_array_size(data);
163 *(gnsrecord_info->rd) = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * 162 *(gnsrecord_info->rd) = GNUNET_malloc(sizeof(struct GNUNET_GNSRECORD_Data) *
164 json_array_size (data)); 163 json_array_size(data));
165 size_t index; 164 size_t index;
166 json_t *value; 165 json_t *value;
167 json_array_foreach (data, index, value) 166 json_array_foreach(data, index, value)
168 { 167 {
169 if (GNUNET_OK != parse_record (value, &(*(gnsrecord_info->rd))[index])) 168 if (GNUNET_OK != parse_record(value, &(*(gnsrecord_info->rd))[index]))
170 return GNUNET_SYSERR; 169 return GNUNET_SYSERR;
171 } 170 }
172 return GNUNET_OK; 171 return GNUNET_OK;
@@ -174,42 +173,42 @@ parse_record_data (struct GnsRecordInfo *gnsrecord_info, json_t *data)
174 173
175 174
176static int 175static int
177parse_gnsrecordobject (void *cls, 176parse_gnsrecordobject(void *cls,
178 json_t *root, 177 json_t *root,
179 struct GNUNET_JSON_Specification *spec) 178 struct GNUNET_JSON_Specification *spec)
180{ 179{
181 struct GnsRecordInfo *gnsrecord_info; 180 struct GnsRecordInfo *gnsrecord_info;
182 int unpack_state = 0; 181 int unpack_state = 0;
183 const char *name; 182 const char *name;
184 json_t *data; 183 json_t *data;
185 184
186 GNUNET_assert (NULL != root); 185 GNUNET_assert(NULL != root);
187 if (! json_is_object (root)) 186 if (!json_is_object(root))
188 { 187 {
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 188 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
190 "Error record JSON is not an object!\n"); 189 "Error record JSON is not an object!\n");
191 return GNUNET_SYSERR; 190 return GNUNET_SYSERR;
192 } 191 }
193 //interpret single gns record 192 //interpret single gns record
194 unpack_state = json_unpack (root, 193 unpack_state = json_unpack(root,
195 "{s:s, s:o!}", 194 "{s:s, s:o!}",
196 GNUNET_JSON_GNSRECORD_RECORD_NAME, 195 GNUNET_JSON_GNSRECORD_RECORD_NAME,
197 &name, 196 &name,
198 GNUNET_JSON_GNSRECORD_RECORD_DATA, 197 GNUNET_JSON_GNSRECORD_RECORD_DATA,
199 &data); 198 &data);
200 if (0 != unpack_state) 199 if (0 != unpack_state)
201 { 200 {
202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 201 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
203 "Error namestore records object has a wrong format!\n"); 202 "Error namestore records object has a wrong format!\n");
204 return GNUNET_SYSERR; 203 return GNUNET_SYSERR;
205 } 204 }
206 gnsrecord_info = (struct GnsRecordInfo *) spec->ptr; 205 gnsrecord_info = (struct GnsRecordInfo *)spec->ptr;
207 *(gnsrecord_info->name) = GNUNET_strdup (name); 206 *(gnsrecord_info->name) = GNUNET_strdup(name);
208 if (GNUNET_OK != parse_record_data (gnsrecord_info, data)) 207 if (GNUNET_OK != parse_record_data(gnsrecord_info, data))
209 { 208 {
210 cleanup_recordinfo (gnsrecord_info); 209 cleanup_recordinfo(gnsrecord_info);
211 return GNUNET_SYSERR; 210 return GNUNET_SYSERR;
212 } 211 }
213 return GNUNET_OK; 212 return GNUNET_OK;
214} 213}
215 214
@@ -221,10 +220,11 @@ parse_gnsrecordobject (void *cls,
221 * @param[out] spec where to free the data 220 * @param[out] spec where to free the data
222 */ 221 */
223static void 222static void
224clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec) 223clean_gnsrecordobject(void *cls, struct GNUNET_JSON_Specification *spec)
225{ 224{
226 struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *) spec->ptr; 225 struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *)spec->ptr;
227 GNUNET_free (gnsrecord_info); 226
227 GNUNET_free(gnsrecord_info);
228} 228}
229 229
230 230
@@ -235,21 +235,22 @@ clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec)
235 * @return JSON Specification 235 * @return JSON Specification
236 */ 236 */
237struct GNUNET_JSON_Specification 237struct GNUNET_JSON_Specification
238GNUNET_JSON_spec_gnsrecord (struct GNUNET_GNSRECORD_Data **rd, 238GNUNET_JSON_spec_gnsrecord(struct GNUNET_GNSRECORD_Data **rd,
239 unsigned int *rd_count, 239 unsigned int *rd_count,
240 char **name) 240 char **name)
241{ 241{
242 struct GnsRecordInfo *gnsrecord_info = GNUNET_new (struct GnsRecordInfo); 242 struct GnsRecordInfo *gnsrecord_info = GNUNET_new(struct GnsRecordInfo);
243
243 gnsrecord_info->rd = rd; 244 gnsrecord_info->rd = rd;
244 gnsrecord_info->name = name; 245 gnsrecord_info->name = name;
245 gnsrecord_info->rd_count = rd_count; 246 gnsrecord_info->rd_count = rd_count;
246 struct GNUNET_JSON_Specification ret = {.parser = &parse_gnsrecordobject, 247 struct GNUNET_JSON_Specification ret = { .parser = &parse_gnsrecordobject,
247 .cleaner = &clean_gnsrecordobject, 248 .cleaner = &clean_gnsrecordobject,
248 .cls = NULL, 249 .cls = NULL,
249 .field = NULL, 250 .field = NULL,
250 .ptr = (struct GnsRecordInfo *) 251 .ptr = (struct GnsRecordInfo *)
251 gnsrecord_info, 252 gnsrecord_info,
252 .ptr_size = 0, 253 .ptr_size = 0,
253 .size_ptr = NULL}; 254 .size_ptr = NULL };
254 return ret; 255 return ret;
255} 256}
diff --git a/src/json/json_helper.c b/src/json/json_helper.c
index fdbb13267..a732219c1 100644
--- a/src/json/json_helper.c
+++ b/src/json/json_helper.c
@@ -1,22 +1,22 @@
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, 2015, 2016 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
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file json/json_helper.c 21 * @file json/json_helper.c
22 * @brief functions to generate specifciations for JSON parsing 22 * @brief functions to generate specifciations for JSON parsing
@@ -32,13 +32,14 @@
32 * End of a parser specification. 32 * End of a parser specification.
33 */ 33 */
34struct GNUNET_JSON_Specification 34struct GNUNET_JSON_Specification
35GNUNET_JSON_spec_end () 35GNUNET_JSON_spec_end()
36{ 36{
37 struct GNUNET_JSON_Specification ret = { 37 struct GNUNET_JSON_Specification ret = {
38 .parser = NULL, 38 .parser = NULL,
39 .cleaner = NULL, 39 .cleaner = NULL,
40 .cls = NULL 40 .cls = NULL
41 }; 41 };
42
42 return ret; 43 return ret;
43} 44}
44 45
@@ -52,33 +53,33 @@ GNUNET_JSON_spec_end ()
52 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 53 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
53 */ 54 */
54static int 55static int
55parse_fixed_data (void *cls, 56parse_fixed_data(void *cls,
56 json_t *root, 57 json_t *root,
57 struct GNUNET_JSON_Specification *spec) 58 struct GNUNET_JSON_Specification *spec)
58{ 59{
59 const char *enc; 60 const char *enc;
60 unsigned int len; 61 unsigned int len;
61 62
62 if (NULL == (enc = json_string_value (root))) 63 if (NULL == (enc = json_string_value(root)))
63 { 64 {
64 GNUNET_break_op (0); 65 GNUNET_break_op(0);
65 return GNUNET_SYSERR; 66 return GNUNET_SYSERR;
66 } 67 }
67 len = strlen (enc); 68 len = strlen(enc);
68 if (((len * 5) / 8) != spec->ptr_size) 69 if (((len * 5) / 8) != spec->ptr_size)
69 { 70 {
70 GNUNET_break_op (0); 71 GNUNET_break_op(0);
71 return GNUNET_SYSERR; 72 return GNUNET_SYSERR;
72 } 73 }
73 if (GNUNET_OK != 74 if (GNUNET_OK !=
74 GNUNET_STRINGS_string_to_data (enc, 75 GNUNET_STRINGS_string_to_data(enc,
75 len, 76 len,
76 spec->ptr, 77 spec->ptr,
77 spec->ptr_size)) 78 spec->ptr_size))
78 { 79 {
79 GNUNET_break_op (0); 80 GNUNET_break_op(0);
80 return GNUNET_SYSERR; 81 return GNUNET_SYSERR;
81 } 82 }
82 return GNUNET_OK; 83 return GNUNET_OK;
83} 84}
84 85
@@ -92,9 +93,9 @@ parse_fixed_data (void *cls,
92 * @param size number of bytes expected in @a obj 93 * @param size number of bytes expected in @a obj
93 */ 94 */
94struct GNUNET_JSON_Specification 95struct GNUNET_JSON_Specification
95GNUNET_JSON_spec_fixed (const char *name, 96GNUNET_JSON_spec_fixed(const char *name,
96 void *obj, 97 void *obj,
97 size_t size) 98 size_t size)
98{ 99{
99 struct GNUNET_JSON_Specification ret = { 100 struct GNUNET_JSON_Specification ret = {
100 .parser = &parse_fixed_data, 101 .parser = &parse_fixed_data,
@@ -105,6 +106,7 @@ GNUNET_JSON_spec_fixed (const char *name,
105 .ptr_size = size, 106 .ptr_size = size,
106 .size_ptr = NULL 107 .size_ptr = NULL
107 }; 108 };
109
108 return ret; 110 return ret;
109} 111}
110 112
@@ -118,39 +120,39 @@ GNUNET_JSON_spec_fixed (const char *name,
118 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 120 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
119 */ 121 */
120static int 122static int
121parse_variable_data (void *cls, 123parse_variable_data(void *cls,
122 json_t *root, 124 json_t *root,
123 struct GNUNET_JSON_Specification *spec) 125 struct GNUNET_JSON_Specification *spec)
124{ 126{
125 const char *str; 127 const char *str;
126 size_t size; 128 size_t size;
127 void *data; 129 void *data;
128 int res; 130 int res;
129 131
130 str = json_string_value (root); 132 str = json_string_value(root);
131 if (NULL == str) 133 if (NULL == str)
132 { 134 {
133 GNUNET_break_op (0); 135 GNUNET_break_op(0);
134 return GNUNET_SYSERR; 136 return GNUNET_SYSERR;
135 } 137 }
136 size = (strlen (str) * 5) / 8; 138 size = (strlen(str) * 5) / 8;
137 if (size >= 1024) 139 if (size >= 1024)
138 { 140 {
139 GNUNET_break_op (0); 141 GNUNET_break_op(0);
140 return GNUNET_SYSERR; 142 return GNUNET_SYSERR;
141 } 143 }
142 data = GNUNET_malloc (size); 144 data = GNUNET_malloc(size);
143 res = GNUNET_STRINGS_string_to_data (str, 145 res = GNUNET_STRINGS_string_to_data(str,
144 strlen (str), 146 strlen(str),
145 data, 147 data,
146 size); 148 size);
147 if (GNUNET_OK != res) 149 if (GNUNET_OK != res)
148 { 150 {
149 GNUNET_break_op (0); 151 GNUNET_break_op(0);
150 GNUNET_free (data); 152 GNUNET_free(data);
151 return GNUNET_SYSERR; 153 return GNUNET_SYSERR;
152 } 154 }
153 *(void**) spec->ptr = data; 155 *(void**)spec->ptr = data;
154 *spec->size_ptr = size; 156 *spec->size_ptr = size;
155 return GNUNET_OK; 157 return GNUNET_OK;
156} 158}
@@ -163,15 +165,15 @@ parse_variable_data (void *cls,
163 * @param[out] spec where to free the data 165 * @param[out] spec where to free the data
164 */ 166 */
165static void 167static void
166clean_variable_data (void *cls, 168clean_variable_data(void *cls,
167 struct GNUNET_JSON_Specification *spec) 169 struct GNUNET_JSON_Specification *spec)
168{ 170{
169 if (0 != *spec->size_ptr) 171 if (0 != *spec->size_ptr)
170 { 172 {
171 GNUNET_free (*(void **) spec->ptr); 173 GNUNET_free(*(void **)spec->ptr);
172 *(void**) spec->ptr = NULL; 174 *(void**)spec->ptr = NULL;
173 *spec->size_ptr = 0; 175 *spec->size_ptr = 0;
174 } 176 }
175} 177}
176 178
177 179
@@ -184,9 +186,9 @@ clean_variable_data (void *cls,
184 * @param[out] size where to store the number of bytes allocated for @a obj 186 * @param[out] size where to store the number of bytes allocated for @a obj
185 */ 187 */
186struct GNUNET_JSON_Specification 188struct GNUNET_JSON_Specification
187GNUNET_JSON_spec_varsize (const char *name, 189GNUNET_JSON_spec_varsize(const char *name,
188 void **obj, 190 void **obj,
189 size_t *size) 191 size_t *size)
190{ 192{
191 struct GNUNET_JSON_Specification ret = { 193 struct GNUNET_JSON_Specification ret = {
192 .parser = &parse_variable_data, 194 .parser = &parse_variable_data,
@@ -197,6 +199,7 @@ GNUNET_JSON_spec_varsize (const char *name,
197 .ptr_size = 0, 199 .ptr_size = 0,
198 .size_ptr = size 200 .size_ptr = size
199 }; 201 };
202
200 *obj = NULL; 203 *obj = NULL;
201 *size = 0; 204 *size = 0;
202 return ret; 205 return ret;
@@ -212,19 +215,19 @@ GNUNET_JSON_spec_varsize (const char *name,
212 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 215 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
213 */ 216 */
214static int 217static int
215parse_string (void *cls, 218parse_string(void *cls,
216 json_t *root, 219 json_t *root,
217 struct GNUNET_JSON_Specification *spec) 220 struct GNUNET_JSON_Specification *spec)
218{ 221{
219 const char *str; 222 const char *str;
220 223
221 str = json_string_value (root); 224 str = json_string_value(root);
222 if (NULL == str) 225 if (NULL == str)
223 { 226 {
224 GNUNET_break_op (0); 227 GNUNET_break_op(0);
225 return GNUNET_SYSERR; 228 return GNUNET_SYSERR;
226 } 229 }
227 *(const char **) spec->ptr = str; 230 *(const char **)spec->ptr = str;
228 return GNUNET_OK; 231 return GNUNET_OK;
229} 232}
230 233
@@ -236,8 +239,8 @@ parse_string (void *cls,
236 * @param strptr where to store a pointer to the field 239 * @param strptr where to store a pointer to the field
237 */ 240 */
238struct GNUNET_JSON_Specification 241struct GNUNET_JSON_Specification
239GNUNET_JSON_spec_string (const char *name, 242GNUNET_JSON_spec_string(const char *name,
240 const char **strptr) 243 const char **strptr)
241{ 244{
242 struct GNUNET_JSON_Specification ret = { 245 struct GNUNET_JSON_Specification ret = {
243 .parser = &parse_string, 246 .parser = &parse_string,
@@ -248,6 +251,7 @@ GNUNET_JSON_spec_string (const char *name,
248 .ptr_size = 0, 251 .ptr_size = 0,
249 .size_ptr = NULL 252 .size_ptr = NULL
250 }; 253 };
254
251 *strptr = NULL; 255 *strptr = NULL;
252 return ret; 256 return ret;
253} 257}
@@ -262,17 +266,17 @@ GNUNET_JSON_spec_string (const char *name,
262 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 266 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
263 */ 267 */
264static int 268static int
265parse_object (void *cls, 269parse_object(void *cls,
266 json_t *root, 270 json_t *root,
267 struct GNUNET_JSON_Specification *spec) 271 struct GNUNET_JSON_Specification *spec)
268{ 272{
269 if (! (json_is_object (root) || json_is_array (root)) ) 273 if (!(json_is_object(root) || json_is_array(root)))
270 { 274 {
271 GNUNET_break_op (0); 275 GNUNET_break_op(0);
272 return GNUNET_SYSERR; 276 return GNUNET_SYSERR;
273 } 277 }
274 json_incref (root); 278 json_incref(root);
275 *(json_t **) spec->ptr = root; 279 *(json_t **)spec->ptr = root;
276 return GNUNET_OK; 280 return GNUNET_OK;
277} 281}
278 282
@@ -284,16 +288,16 @@ parse_object (void *cls,
284 * @param[out] spec where to free the data 288 * @param[out] spec where to free the data
285 */ 289 */
286static void 290static void
287clean_object (void *cls, 291clean_object(void *cls,
288 struct GNUNET_JSON_Specification *spec) 292 struct GNUNET_JSON_Specification *spec)
289{ 293{
290 json_t **ptr = (json_t **) spec->ptr; 294 json_t **ptr = (json_t **)spec->ptr;
291 295
292 if (NULL != *ptr) 296 if (NULL != *ptr)
293 { 297 {
294 json_decref (*ptr); 298 json_decref(*ptr);
295 *ptr = NULL; 299 *ptr = NULL;
296 } 300 }
297} 301}
298 302
299 303
@@ -304,8 +308,8 @@ clean_object (void *cls,
304 * @param[out] jsonp where to store the JSON found under @a name 308 * @param[out] jsonp where to store the JSON found under @a name
305 */ 309 */
306struct GNUNET_JSON_Specification 310struct GNUNET_JSON_Specification
307GNUNET_JSON_spec_json (const char *name, 311GNUNET_JSON_spec_json(const char *name,
308 json_t **jsonp) 312 json_t **jsonp)
309{ 313{
310 struct GNUNET_JSON_Specification ret = { 314 struct GNUNET_JSON_Specification ret = {
311 .parser = &parse_object, 315 .parser = &parse_object,
@@ -316,6 +320,7 @@ GNUNET_JSON_spec_json (const char *name,
316 .ptr_size = 0, 320 .ptr_size = 0,
317 .size_ptr = NULL 321 .size_ptr = NULL
318 }; 322 };
323
319 *jsonp = NULL; 324 *jsonp = NULL;
320 return ret; 325 return ret;
321} 326}
@@ -330,25 +335,25 @@ GNUNET_JSON_spec_json (const char *name,
330 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 335 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
331 */ 336 */
332static int 337static int
333parse_u8 (void *cls, 338parse_u8(void *cls,
334 json_t *root, 339 json_t *root,
335 struct GNUNET_JSON_Specification *spec) 340 struct GNUNET_JSON_Specification *spec)
336{ 341{
337 json_int_t val; 342 json_int_t val;
338 uint8_t *up = spec->ptr; 343 uint8_t *up = spec->ptr;
339 344
340 if (! json_is_integer (root)) 345 if (!json_is_integer(root))
341 { 346 {
342 GNUNET_break_op (0); 347 GNUNET_break_op(0);
343 return GNUNET_SYSERR; 348 return GNUNET_SYSERR;
344 } 349 }
345 val = json_integer_value (root); 350 val = json_integer_value(root);
346 if ( (0 > val) || (val > UINT8_MAX) ) 351 if ((0 > val) || (val > UINT8_MAX))
347 { 352 {
348 GNUNET_break_op (0); 353 GNUNET_break_op(0);
349 return GNUNET_SYSERR; 354 return GNUNET_SYSERR;
350 } 355 }
351 *up = (uint8_t) val; 356 *up = (uint8_t)val;
352 return GNUNET_OK; 357 return GNUNET_OK;
353} 358}
354 359
@@ -360,8 +365,8 @@ parse_u8 (void *cls,
360 * @param[out] u8 where to store the integer found under @a name 365 * @param[out] u8 where to store the integer found under @a name
361 */ 366 */
362struct GNUNET_JSON_Specification 367struct GNUNET_JSON_Specification
363GNUNET_JSON_spec_uint8 (const char *name, 368GNUNET_JSON_spec_uint8(const char *name,
364 uint8_t *u8) 369 uint8_t *u8)
365{ 370{
366 struct GNUNET_JSON_Specification ret = { 371 struct GNUNET_JSON_Specification ret = {
367 .parser = &parse_u8, 372 .parser = &parse_u8,
@@ -369,9 +374,10 @@ GNUNET_JSON_spec_uint8 (const char *name,
369 .cls = NULL, 374 .cls = NULL,
370 .field = name, 375 .field = name,
371 .ptr = u8, 376 .ptr = u8,
372 .ptr_size = sizeof (uint8_t), 377 .ptr_size = sizeof(uint8_t),
373 .size_ptr = NULL 378 .size_ptr = NULL
374 }; 379 };
380
375 return ret; 381 return ret;
376} 382}
377 383
@@ -385,25 +391,25 @@ GNUNET_JSON_spec_uint8 (const char *name,
385 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 391 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
386 */ 392 */
387static int 393static int
388parse_u16 (void *cls, 394parse_u16(void *cls,
389 json_t *root, 395 json_t *root,
390 struct GNUNET_JSON_Specification *spec) 396 struct GNUNET_JSON_Specification *spec)
391{ 397{
392 json_int_t val; 398 json_int_t val;
393 uint16_t *up = spec->ptr; 399 uint16_t *up = spec->ptr;
394 400
395 if (! json_is_integer (root)) 401 if (!json_is_integer(root))
396 { 402 {
397 GNUNET_break_op (0); 403 GNUNET_break_op(0);
398 return GNUNET_SYSERR; 404 return GNUNET_SYSERR;
399 } 405 }
400 val = json_integer_value (root); 406 val = json_integer_value(root);
401 if ( (0 > val) || (val > UINT16_MAX) ) 407 if ((0 > val) || (val > UINT16_MAX))
402 { 408 {
403 GNUNET_break_op (0); 409 GNUNET_break_op(0);
404 return GNUNET_SYSERR; 410 return GNUNET_SYSERR;
405 } 411 }
406 *up = (uint16_t) val; 412 *up = (uint16_t)val;
407 return GNUNET_OK; 413 return GNUNET_OK;
408} 414}
409 415
@@ -415,8 +421,8 @@ parse_u16 (void *cls,
415 * @param[out] u16 where to store the integer found under @a name 421 * @param[out] u16 where to store the integer found under @a name
416 */ 422 */
417struct GNUNET_JSON_Specification 423struct GNUNET_JSON_Specification
418GNUNET_JSON_spec_uint16 (const char *name, 424GNUNET_JSON_spec_uint16(const char *name,
419 uint16_t *u16) 425 uint16_t *u16)
420{ 426{
421 struct GNUNET_JSON_Specification ret = { 427 struct GNUNET_JSON_Specification ret = {
422 .parser = &parse_u16, 428 .parser = &parse_u16,
@@ -424,9 +430,10 @@ GNUNET_JSON_spec_uint16 (const char *name,
424 .cls = NULL, 430 .cls = NULL,
425 .field = name, 431 .field = name,
426 .ptr = u16, 432 .ptr = u16,
427 .ptr_size = sizeof (uint16_t), 433 .ptr_size = sizeof(uint16_t),
428 .size_ptr = NULL 434 .size_ptr = NULL
429 }; 435 };
436
430 return ret; 437 return ret;
431} 438}
432 439
@@ -440,25 +447,25 @@ GNUNET_JSON_spec_uint16 (const char *name,
440 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 447 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
441 */ 448 */
442static int 449static int
443parse_u32 (void *cls, 450parse_u32(void *cls,
444 json_t *root, 451 json_t *root,
445 struct GNUNET_JSON_Specification *spec) 452 struct GNUNET_JSON_Specification *spec)
446{ 453{
447 json_int_t val; 454 json_int_t val;
448 uint32_t *up = spec->ptr; 455 uint32_t *up = spec->ptr;
449 456
450 if (! json_is_integer (root)) 457 if (!json_is_integer(root))
451 { 458 {
452 GNUNET_break_op (0); 459 GNUNET_break_op(0);
453 return GNUNET_SYSERR; 460 return GNUNET_SYSERR;
454 } 461 }
455 val = json_integer_value (root); 462 val = json_integer_value(root);
456 if ( (0 > val) || (val > UINT32_MAX) ) 463 if ((0 > val) || (val > UINT32_MAX))
457 { 464 {
458 GNUNET_break_op (0); 465 GNUNET_break_op(0);
459 return GNUNET_SYSERR; 466 return GNUNET_SYSERR;
460 } 467 }
461 *up = (uint32_t) val; 468 *up = (uint32_t)val;
462 return GNUNET_OK; 469 return GNUNET_OK;
463} 470}
464 471
@@ -470,8 +477,8 @@ parse_u32 (void *cls,
470 * @param[out] u32 where to store the integer found under @a name 477 * @param[out] u32 where to store the integer found under @a name
471 */ 478 */
472struct GNUNET_JSON_Specification 479struct GNUNET_JSON_Specification
473GNUNET_JSON_spec_uint32 (const char *name, 480GNUNET_JSON_spec_uint32(const char *name,
474 uint32_t *u32) 481 uint32_t *u32)
475{ 482{
476 struct GNUNET_JSON_Specification ret = { 483 struct GNUNET_JSON_Specification ret = {
477 .parser = &parse_u32, 484 .parser = &parse_u32,
@@ -479,9 +486,10 @@ GNUNET_JSON_spec_uint32 (const char *name,
479 .cls = NULL, 486 .cls = NULL,
480 .field = name, 487 .field = name,
481 .ptr = u32, 488 .ptr = u32,
482 .ptr_size = sizeof (uint32_t), 489 .ptr_size = sizeof(uint32_t),
483 .size_ptr = NULL 490 .size_ptr = NULL
484 }; 491 };
492
485 return ret; 493 return ret;
486} 494}
487 495
@@ -495,20 +503,20 @@ GNUNET_JSON_spec_uint32 (const char *name,
495 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 503 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
496 */ 504 */
497static int 505static int
498parse_u64 (void *cls, 506parse_u64(void *cls,
499 json_t *root, 507 json_t *root,
500 struct GNUNET_JSON_Specification *spec) 508 struct GNUNET_JSON_Specification *spec)
501{ 509{
502 json_int_t val; 510 json_int_t val;
503 uint64_t *up = spec->ptr; 511 uint64_t *up = spec->ptr;
504 512
505 if (! json_is_integer (root)) 513 if (!json_is_integer(root))
506 { 514 {
507 GNUNET_break_op (0); 515 GNUNET_break_op(0);
508 return GNUNET_SYSERR; 516 return GNUNET_SYSERR;
509 } 517 }
510 val = json_integer_value (root); 518 val = json_integer_value(root);
511 *up = (uint64_t) val; 519 *up = (uint64_t)val;
512 return GNUNET_OK; 520 return GNUNET_OK;
513} 521}
514 522
@@ -520,8 +528,8 @@ parse_u64 (void *cls,
520 * @param[out] u64 where to store the integer found under @a name 528 * @param[out] u64 where to store the integer found under @a name
521 */ 529 */
522struct GNUNET_JSON_Specification 530struct GNUNET_JSON_Specification
523GNUNET_JSON_spec_uint64 (const char *name, 531GNUNET_JSON_spec_uint64(const char *name,
524 uint64_t *u64) 532 uint64_t *u64)
525{ 533{
526 struct GNUNET_JSON_Specification ret = { 534 struct GNUNET_JSON_Specification ret = {
527 .parser = &parse_u64, 535 .parser = &parse_u64,
@@ -529,9 +537,10 @@ GNUNET_JSON_spec_uint64 (const char *name,
529 .cls = NULL, 537 .cls = NULL,
530 .field = name, 538 .field = name,
531 .ptr = u64, 539 .ptr = u64,
532 .ptr_size = sizeof (uint64_t), 540 .ptr_size = sizeof(uint64_t),
533 .size_ptr = NULL 541 .size_ptr = NULL
534 }; 542 };
543
535 return ret; 544 return ret;
536} 545}
537 546
@@ -547,45 +556,45 @@ GNUNET_JSON_spec_uint64 (const char *name,
547 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 556 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
548 */ 557 */
549static int 558static int
550parse_abs_time (void *cls, 559parse_abs_time(void *cls,
551 json_t *root, 560 json_t *root,
552 struct GNUNET_JSON_Specification *spec) 561 struct GNUNET_JSON_Specification *spec)
553{ 562{
554 struct GNUNET_TIME_Absolute *abs = spec->ptr; 563 struct GNUNET_TIME_Absolute *abs = spec->ptr;
555 const char *val; 564 const char *val;
556 unsigned long long int tval; 565 unsigned long long int tval;
557 566
558 val = json_string_value (root); 567 val = json_string_value(root);
559 if (NULL == val) 568 if (NULL == val)
560 { 569 {
561 GNUNET_break_op (0); 570 GNUNET_break_op(0);
562 return GNUNET_SYSERR; 571 return GNUNET_SYSERR;
563 } 572 }
564 if ( (0 == strcasecmp (val, 573 if ((0 == strcasecmp(val,
565 "/forever/")) || 574 "/forever/")) ||
566 (0 == strcasecmp (val, 575 (0 == strcasecmp(val,
567 "/end of time/")) || 576 "/end of time/")) ||
568 (0 == strcasecmp (val, 577 (0 == strcasecmp(val,
569 "/never/")) ) 578 "/never/")))
570 { 579 {
571 *abs = GNUNET_TIME_UNIT_FOREVER_ABS; 580 *abs = GNUNET_TIME_UNIT_FOREVER_ABS;
572 return GNUNET_OK; 581 return GNUNET_OK;
573 } 582 }
574 if (1 != sscanf (val, 583 if (1 != sscanf(val,
575 "/Date(%llu)/", 584 "/Date(%llu)/",
576 &tval)) 585 &tval))
577 { 586 {
578 GNUNET_break_op (0); 587 GNUNET_break_op(0);
579 return GNUNET_SYSERR; 588 return GNUNET_SYSERR;
580 } 589 }
581 /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ 590 /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */
582 abs->abs_value_us = tval * 1000LL * 1000LL; 591 abs->abs_value_us = tval * 1000LL * 1000LL;
583 if ( (abs->abs_value_us) / 1000LL / 1000LL != tval) 592 if ((abs->abs_value_us) / 1000LL / 1000LL != tval)
584 { 593 {
585 /* Integer overflow */ 594 /* Integer overflow */
586 GNUNET_break_op (0); 595 GNUNET_break_op(0);
587 return GNUNET_SYSERR; 596 return GNUNET_SYSERR;
588 } 597 }
589 return GNUNET_OK; 598 return GNUNET_OK;
590} 599}
591 600
@@ -597,8 +606,8 @@ parse_abs_time (void *cls,
597 * @param[out] at where to store the absolute time found under @a name 606 * @param[out] at where to store the absolute time found under @a name
598 */ 607 */
599struct GNUNET_JSON_Specification 608struct GNUNET_JSON_Specification
600GNUNET_JSON_spec_absolute_time (const char *name, 609GNUNET_JSON_spec_absolute_time(const char *name,
601 struct GNUNET_TIME_Absolute *at) 610 struct GNUNET_TIME_Absolute *at)
602{ 611{
603 struct GNUNET_JSON_Specification ret = { 612 struct GNUNET_JSON_Specification ret = {
604 .parser = &parse_abs_time, 613 .parser = &parse_abs_time,
@@ -606,9 +615,10 @@ GNUNET_JSON_spec_absolute_time (const char *name,
606 .cls = NULL, 615 .cls = NULL,
607 .field = name, 616 .field = name,
608 .ptr = at, 617 .ptr = at,
609 .ptr_size = sizeof (uint64_t), 618 .ptr_size = sizeof(uint64_t),
610 .size_ptr = NULL 619 .size_ptr = NULL
611 }; 620 };
621
612 return ret; 622 return ret;
613} 623}
614 624
@@ -622,47 +632,47 @@ GNUNET_JSON_spec_absolute_time (const char *name,
622 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 632 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
623 */ 633 */
624static int 634static int
625parse_abs_time_nbo (void *cls, 635parse_abs_time_nbo(void *cls,
626 json_t *root, 636 json_t *root,
627 struct GNUNET_JSON_Specification *spec) 637 struct GNUNET_JSON_Specification *spec)
628{ 638{
629 struct GNUNET_TIME_AbsoluteNBO *abs = spec->ptr; 639 struct GNUNET_TIME_AbsoluteNBO *abs = spec->ptr;
630 const char *val; 640 const char *val;
631 unsigned long long int tval; 641 unsigned long long int tval;
632 struct GNUNET_TIME_Absolute a; 642 struct GNUNET_TIME_Absolute a;
633 643
634 val = json_string_value (root); 644 val = json_string_value(root);
635 if (NULL == val) 645 if (NULL == val)
636 { 646 {
637 GNUNET_break_op (0); 647 GNUNET_break_op(0);
638 return GNUNET_SYSERR; 648 return GNUNET_SYSERR;
639 } 649 }
640 if ( (0 == strcasecmp (val, 650 if ((0 == strcasecmp(val,
641 "/forever/")) || 651 "/forever/")) ||
642 (0 == strcasecmp (val, 652 (0 == strcasecmp(val,
643 "/end of time/")) || 653 "/end of time/")) ||
644 (0 == strcasecmp (val, 654 (0 == strcasecmp(val,
645 "/never/")) ) 655 "/never/")))
646 { 656 {
647 *abs = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS); 657 *abs = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_FOREVER_ABS);
648 return GNUNET_OK; 658 return GNUNET_OK;
649 } 659 }
650 if (1 != sscanf (val, 660 if (1 != sscanf(val,
651 "/Date(%llu)/", 661 "/Date(%llu)/",
652 &tval)) 662 &tval))
653 { 663 {
654 GNUNET_break_op (0); 664 GNUNET_break_op(0);
655 return GNUNET_SYSERR; 665 return GNUNET_SYSERR;
656 } 666 }
657 /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */ 667 /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Absolute */
658 a.abs_value_us = tval * 1000LL * 1000LL; 668 a.abs_value_us = tval * 1000LL * 1000LL;
659 if ( (a.abs_value_us) / 1000LL / 1000LL != tval) 669 if ((a.abs_value_us) / 1000LL / 1000LL != tval)
660 { 670 {
661 /* Integer overflow */ 671 /* Integer overflow */
662 GNUNET_break_op (0); 672 GNUNET_break_op(0);
663 return GNUNET_SYSERR; 673 return GNUNET_SYSERR;
664 } 674 }
665 *abs = GNUNET_TIME_absolute_hton (a); 675 *abs = GNUNET_TIME_absolute_hton(a);
666 return GNUNET_OK; 676 return GNUNET_OK;
667} 677}
668 678
@@ -674,8 +684,8 @@ parse_abs_time_nbo (void *cls,
674 * @param[out] at where to store the absolute time found under @a name 684 * @param[out] at where to store the absolute time found under @a name
675 */ 685 */
676struct GNUNET_JSON_Specification 686struct GNUNET_JSON_Specification
677GNUNET_JSON_spec_absolute_time_nbo (const char *name, 687GNUNET_JSON_spec_absolute_time_nbo(const char *name,
678 struct GNUNET_TIME_AbsoluteNBO *at) 688 struct GNUNET_TIME_AbsoluteNBO *at)
679{ 689{
680 struct GNUNET_JSON_Specification ret = { 690 struct GNUNET_JSON_Specification ret = {
681 .parser = &parse_abs_time_nbo, 691 .parser = &parse_abs_time_nbo,
@@ -683,9 +693,10 @@ GNUNET_JSON_spec_absolute_time_nbo (const char *name,
683 .cls = NULL, 693 .cls = NULL,
684 .field = name, 694 .field = name,
685 .ptr = at, 695 .ptr = at,
686 .ptr_size = sizeof (uint64_t), 696 .ptr_size = sizeof(uint64_t),
687 .size_ptr = NULL 697 .size_ptr = NULL
688 }; 698 };
699
689 return ret; 700 return ret;
690} 701}
691 702
@@ -699,41 +710,41 @@ GNUNET_JSON_spec_absolute_time_nbo (const char *name,
699 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 710 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
700 */ 711 */
701static int 712static int
702parse_rel_time (void *cls, 713parse_rel_time(void *cls,
703 json_t *root, 714 json_t *root,
704 struct GNUNET_JSON_Specification *spec) 715 struct GNUNET_JSON_Specification *spec)
705{ 716{
706 struct GNUNET_TIME_Relative *rel = spec->ptr; 717 struct GNUNET_TIME_Relative *rel = spec->ptr;
707 const char *val; 718 const char *val;
708 unsigned long long int tval; 719 unsigned long long int tval;
709 720
710 val = json_string_value (root); 721 val = json_string_value(root);
711 if (NULL == val) 722 if (NULL == val)
712 { 723 {
713 GNUNET_break_op (0); 724 GNUNET_break_op(0);
714 return GNUNET_SYSERR; 725 return GNUNET_SYSERR;
715 } 726 }
716 if ( (0 == strcasecmp (val, 727 if ((0 == strcasecmp(val,
717 "/forever/")) ) 728 "/forever/")))
718 { 729 {
719 *rel = GNUNET_TIME_UNIT_FOREVER_REL; 730 *rel = GNUNET_TIME_UNIT_FOREVER_REL;
720 return GNUNET_OK; 731 return GNUNET_OK;
721 } 732 }
722 if (1 != sscanf (val, 733 if (1 != sscanf(val,
723 "/Delay(%llu)/", 734 "/Delay(%llu)/",
724 &tval)) 735 &tval))
725 { 736 {
726 GNUNET_break_op (0); 737 GNUNET_break_op(0);
727 return GNUNET_SYSERR; 738 return GNUNET_SYSERR;
728 } 739 }
729 /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Relative */ 740 /* Time is in seconds in JSON, but in microseconds in GNUNET_TIME_Relative */
730 rel->rel_value_us = tval * 1000LL * 1000LL; 741 rel->rel_value_us = tval * 1000LL * 1000LL;
731 if ( (rel->rel_value_us) / 1000LL / 1000LL != tval) 742 if ((rel->rel_value_us) / 1000LL / 1000LL != tval)
732 { 743 {
733 /* Integer overflow */ 744 /* Integer overflow */
734 GNUNET_break_op (0); 745 GNUNET_break_op(0);
735 return GNUNET_SYSERR; 746 return GNUNET_SYSERR;
736 } 747 }
737 return GNUNET_OK; 748 return GNUNET_OK;
738} 749}
739 750
@@ -745,8 +756,8 @@ parse_rel_time (void *cls,
745 * @param[out] rt where to store the relative time found under @a name 756 * @param[out] rt where to store the relative time found under @a name
746 */ 757 */
747struct GNUNET_JSON_Specification 758struct GNUNET_JSON_Specification
748GNUNET_JSON_spec_relative_time (const char *name, 759GNUNET_JSON_spec_relative_time(const char *name,
749 struct GNUNET_TIME_Relative *rt) 760 struct GNUNET_TIME_Relative *rt)
750{ 761{
751 struct GNUNET_JSON_Specification ret = { 762 struct GNUNET_JSON_Specification ret = {
752 .parser = &parse_rel_time, 763 .parser = &parse_rel_time,
@@ -754,9 +765,10 @@ GNUNET_JSON_spec_relative_time (const char *name,
754 .cls = NULL, 765 .cls = NULL,
755 .field = name, 766 .field = name,
756 .ptr = rt, 767 .ptr = rt,
757 .ptr_size = sizeof (uint64_t), 768 .ptr_size = sizeof(uint64_t),
758 .size_ptr = NULL 769 .size_ptr = NULL
759 }; 770 };
771
760 return ret; 772 return ret;
761} 773}
762 774
@@ -770,9 +782,9 @@ GNUNET_JSON_spec_relative_time (const char *name,
770 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 782 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
771 */ 783 */
772static int 784static int
773parse_rsa_public_key (void *cls, 785parse_rsa_public_key(void *cls,
774 json_t *root, 786 json_t *root,
775 struct GNUNET_JSON_Specification *spec) 787 struct GNUNET_JSON_Specification *spec)
776{ 788{
777 struct GNUNET_CRYPTO_RsaPublicKey **pk = spec->ptr; 789 struct GNUNET_CRYPTO_RsaPublicKey **pk = spec->ptr;
778 const char *enc; 790 const char *enc;
@@ -780,32 +792,32 @@ parse_rsa_public_key (void *cls,
780 size_t len; 792 size_t len;
781 size_t buf_len; 793 size_t buf_len;
782 794
783 if (NULL == (enc = json_string_value (root))) 795 if (NULL == (enc = json_string_value(root)))
784 { 796 {
785 GNUNET_break_op (0); 797 GNUNET_break_op(0);
786 return GNUNET_SYSERR; 798 return GNUNET_SYSERR;
787 } 799 }
788 len = strlen (enc); 800 len = strlen(enc);
789 buf_len = (len * 5) / 8; 801 buf_len = (len * 5) / 8;
790 buf = GNUNET_malloc (buf_len); 802 buf = GNUNET_malloc(buf_len);
791 if (GNUNET_OK != 803 if (GNUNET_OK !=
792 GNUNET_STRINGS_string_to_data (enc, 804 GNUNET_STRINGS_string_to_data(enc,
793 len, 805 len,
794 buf, 806 buf,
795 buf_len)) 807 buf_len))
796 { 808 {
797 GNUNET_break_op (0); 809 GNUNET_break_op(0);
798 GNUNET_free (buf); 810 GNUNET_free(buf);
799 return GNUNET_SYSERR; 811 return GNUNET_SYSERR;
800 } 812 }
801 if (NULL == (*pk = GNUNET_CRYPTO_rsa_public_key_decode (buf, 813 if (NULL == (*pk = GNUNET_CRYPTO_rsa_public_key_decode(buf,
802 buf_len))) 814 buf_len)))
803 { 815 {
804 GNUNET_break_op (0); 816 GNUNET_break_op(0);
805 GNUNET_free (buf); 817 GNUNET_free(buf);
806 return GNUNET_SYSERR; 818 return GNUNET_SYSERR;
807 } 819 }
808 GNUNET_free (buf); 820 GNUNET_free(buf);
809 return GNUNET_OK; 821 return GNUNET_OK;
810} 822}
811 823
@@ -817,16 +829,16 @@ parse_rsa_public_key (void *cls,
817 * @param[out] spec where to free the data 829 * @param[out] spec where to free the data
818 */ 830 */
819static void 831static void
820clean_rsa_public_key (void *cls, 832clean_rsa_public_key(void *cls,
821 struct GNUNET_JSON_Specification *spec) 833 struct GNUNET_JSON_Specification *spec)
822{ 834{
823 struct GNUNET_CRYPTO_RsaPublicKey **pk = spec->ptr; 835 struct GNUNET_CRYPTO_RsaPublicKey **pk = spec->ptr;
824 836
825 if (NULL != *pk) 837 if (NULL != *pk)
826 { 838 {
827 GNUNET_CRYPTO_rsa_public_key_free (*pk); 839 GNUNET_CRYPTO_rsa_public_key_free(*pk);
828 *pk = NULL; 840 *pk = NULL;
829 } 841 }
830} 842}
831 843
832 844
@@ -837,8 +849,8 @@ clean_rsa_public_key (void *cls,
837 * @param pk where to store the RSA key found under @a name 849 * @param pk where to store the RSA key found under @a name
838 */ 850 */
839struct GNUNET_JSON_Specification 851struct GNUNET_JSON_Specification
840GNUNET_JSON_spec_rsa_public_key (const char *name, 852GNUNET_JSON_spec_rsa_public_key(const char *name,
841 struct GNUNET_CRYPTO_RsaPublicKey **pk) 853 struct GNUNET_CRYPTO_RsaPublicKey **pk)
842{ 854{
843 struct GNUNET_JSON_Specification ret = { 855 struct GNUNET_JSON_Specification ret = {
844 .parser = &parse_rsa_public_key, 856 .parser = &parse_rsa_public_key,
@@ -849,6 +861,7 @@ GNUNET_JSON_spec_rsa_public_key (const char *name,
849 .ptr_size = 0, 861 .ptr_size = 0,
850 .size_ptr = NULL 862 .size_ptr = NULL
851 }; 863 };
864
852 *pk = NULL; 865 *pk = NULL;
853 return ret; 866 return ret;
854} 867}
@@ -863,9 +876,9 @@ GNUNET_JSON_spec_rsa_public_key (const char *name,
863 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 876 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
864 */ 877 */
865static int 878static int
866parse_rsa_signature (void *cls, 879parse_rsa_signature(void *cls,
867 json_t *root, 880 json_t *root,
868 struct GNUNET_JSON_Specification *spec) 881 struct GNUNET_JSON_Specification *spec)
869{ 882{
870 struct GNUNET_CRYPTO_RsaSignature **sig = spec->ptr; 883 struct GNUNET_CRYPTO_RsaSignature **sig = spec->ptr;
871 size_t size; 884 size_t size;
@@ -873,32 +886,32 @@ parse_rsa_signature (void *cls,
873 int res; 886 int res;
874 void *buf; 887 void *buf;
875 888
876 str = json_string_value (root); 889 str = json_string_value(root);
877 if (NULL == str) 890 if (NULL == str)
878 { 891 {
879 GNUNET_break_op (0); 892 GNUNET_break_op(0);
880 return GNUNET_SYSERR; 893 return GNUNET_SYSERR;
881 } 894 }
882 size = (strlen (str) * 5) / 8; 895 size = (strlen(str) * 5) / 8;
883 buf = GNUNET_malloc (size); 896 buf = GNUNET_malloc(size);
884 res = GNUNET_STRINGS_string_to_data (str, 897 res = GNUNET_STRINGS_string_to_data(str,
885 strlen (str), 898 strlen(str),
886 buf, 899 buf,
887 size); 900 size);
888 if (GNUNET_OK != res) 901 if (GNUNET_OK != res)
889 { 902 {
890 GNUNET_free (buf); 903 GNUNET_free(buf);
891 GNUNET_break_op (0); 904 GNUNET_break_op(0);
892 return GNUNET_SYSERR; 905 return GNUNET_SYSERR;
893 } 906 }
894 if (NULL == (*sig = GNUNET_CRYPTO_rsa_signature_decode (buf, 907 if (NULL == (*sig = GNUNET_CRYPTO_rsa_signature_decode(buf,
895 size))) 908 size)))
896 { 909 {
897 GNUNET_break_op (0); 910 GNUNET_break_op(0);
898 GNUNET_free (buf); 911 GNUNET_free(buf);
899 return GNUNET_SYSERR; 912 return GNUNET_SYSERR;
900 } 913 }
901 GNUNET_free (buf); 914 GNUNET_free(buf);
902 return GNUNET_OK; 915 return GNUNET_OK;
903} 916}
904 917
@@ -910,16 +923,16 @@ parse_rsa_signature (void *cls,
910 * @param[out] spec where to free the data 923 * @param[out] spec where to free the data
911 */ 924 */
912static void 925static void
913clean_rsa_signature (void *cls, 926clean_rsa_signature(void *cls,
914 struct GNUNET_JSON_Specification *spec) 927 struct GNUNET_JSON_Specification *spec)
915{ 928{
916 struct GNUNET_CRYPTO_RsaSignature **sig = spec->ptr; 929 struct GNUNET_CRYPTO_RsaSignature **sig = spec->ptr;
917 930
918 if (NULL != *sig) 931 if (NULL != *sig)
919 { 932 {
920 GNUNET_CRYPTO_rsa_signature_free (*sig); 933 GNUNET_CRYPTO_rsa_signature_free(*sig);
921 *sig = NULL; 934 *sig = NULL;
922 } 935 }
923} 936}
924 937
925 938
@@ -930,8 +943,8 @@ clean_rsa_signature (void *cls,
930 * @param sig where to store the RSA signature found under @a name 943 * @param sig where to store the RSA signature found under @a name
931 */ 944 */
932struct GNUNET_JSON_Specification 945struct GNUNET_JSON_Specification
933GNUNET_JSON_spec_rsa_signature (const char *name, 946GNUNET_JSON_spec_rsa_signature(const char *name,
934 struct GNUNET_CRYPTO_RsaSignature **sig) 947 struct GNUNET_CRYPTO_RsaSignature **sig)
935{ 948{
936 struct GNUNET_JSON_Specification ret = { 949 struct GNUNET_JSON_Specification ret = {
937 .parser = &parse_rsa_signature, 950 .parser = &parse_rsa_signature,
@@ -942,6 +955,7 @@ GNUNET_JSON_spec_rsa_signature (const char *name,
942 .ptr_size = 0, 955 .ptr_size = 0,
943 .size_ptr = NULL 956 .size_ptr = NULL
944 }; 957 };
958
945 *sig = NULL; 959 *sig = NULL;
946 return ret; 960 return ret;
947} 961}
@@ -956,18 +970,18 @@ GNUNET_JSON_spec_rsa_signature (const char *name,
956 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 970 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
957 */ 971 */
958static int 972static int
959parse_boolean (void *cls, 973parse_boolean(void *cls,
960 json_t *root, 974 json_t *root,
961 struct GNUNET_JSON_Specification *spec) 975 struct GNUNET_JSON_Specification *spec)
962{ 976{
963 int *bp = spec->ptr; 977 int *bp = spec->ptr;
964 978
965 if (! json_is_boolean (root)) 979 if (!json_is_boolean(root))
966 { 980 {
967 GNUNET_break_op (0); 981 GNUNET_break_op(0);
968 return GNUNET_SYSERR; 982 return GNUNET_SYSERR;
969 } 983 }
970 *bp = json_boolean_value (root) ? GNUNET_YES : GNUNET_NO; 984 *bp = json_boolean_value(root) ? GNUNET_YES : GNUNET_NO;
971 return GNUNET_OK; 985 return GNUNET_OK;
972} 986}
973 987
@@ -979,8 +993,8 @@ parse_boolean (void *cls,
979 * @param[out] boolean where to store the boolean found under @a name 993 * @param[out] boolean where to store the boolean found under @a name
980 */ 994 */
981struct GNUNET_JSON_Specification 995struct GNUNET_JSON_Specification
982GNUNET_JSON_spec_boolean (const char *name, 996GNUNET_JSON_spec_boolean(const char *name,
983 int *boolean) 997 int *boolean)
984{ 998{
985 struct GNUNET_JSON_Specification ret = { 999 struct GNUNET_JSON_Specification ret = {
986 .parser = &parse_boolean, 1000 .parser = &parse_boolean,
@@ -988,9 +1002,10 @@ GNUNET_JSON_spec_boolean (const char *name,
988 .cls = NULL, 1002 .cls = NULL,
989 .field = name, 1003 .field = name,
990 .ptr = boolean, 1004 .ptr = boolean,
991 .ptr_size = sizeof (int), 1005 .ptr_size = sizeof(int),
992 .size_ptr = NULL 1006 .size_ptr = NULL
993 }; 1007 };
1008
994 return ret; 1009 return ret;
995} 1010}
996 1011
diff --git a/src/json/json_mhd.c b/src/json/json_mhd.c
index 2112d4646..0b4dcfee8 100644
--- a/src/json/json_mhd.c
+++ b/src/json/json_mhd.c
@@ -1,22 +1,22 @@
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, 2015, 2016 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
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file json/json_mhd.c 21 * @file json/json_mhd.c
22 * @brief functions to parse JSON snippets we receive via MHD 22 * @brief functions to parse JSON snippets we receive via MHD
@@ -40,8 +40,7 @@
40/** 40/**
41 * Buffer for POST requests. 41 * Buffer for POST requests.
42 */ 42 */
43struct Buffer 43struct Buffer {
44{
45 /** 44 /**
46 * Allocated memory 45 * Allocated memory
47 */ 46 */
@@ -75,19 +74,19 @@ struct Buffer
75 * @return a GNUnet result code 74 * @return a GNUnet result code
76 */ 75 */
77static int 76static int
78buffer_init (struct Buffer *buf, 77buffer_init(struct Buffer *buf,
79 const void *data, 78 const void *data,
80 size_t data_size, 79 size_t data_size,
81 size_t alloc_size, 80 size_t alloc_size,
82 size_t max_size) 81 size_t max_size)
83{ 82{
84 if ((data_size > max_size) || (alloc_size > max_size)) 83 if ((data_size > max_size) || (alloc_size > max_size))
85 return GNUNET_SYSERR; 84 return GNUNET_SYSERR;
86 if (data_size > alloc_size) 85 if (data_size > alloc_size)
87 alloc_size = data_size; 86 alloc_size = data_size;
88 buf->data = GNUNET_malloc (alloc_size); 87 buf->data = GNUNET_malloc(alloc_size);
89 buf->alloc = alloc_size; 88 buf->alloc = alloc_size;
90 GNUNET_memcpy (buf->data, data, data_size); 89 GNUNET_memcpy(buf->data, data, data_size);
91 buf->fill = data_size; 90 buf->fill = data_size;
92 buf->max = max_size; 91 buf->max = max_size;
93 return GNUNET_OK; 92 return GNUNET_OK;
@@ -101,9 +100,9 @@ buffer_init (struct Buffer *buf,
101 * @param buf buffer to de-initialize 100 * @param buf buffer to de-initialize
102 */ 101 */
103static void 102static void
104buffer_deinit (struct Buffer *buf) 103buffer_deinit(struct Buffer *buf)
105{ 104{
106 GNUNET_free (buf->data); 105 GNUNET_free(buf->data);
107 buf->data = NULL; 106 buf->data = NULL;
108} 107}
109 108
@@ -119,28 +118,28 @@ buffer_deinit (struct Buffer *buf)
119 * #GNUNET_NO if the buffer can't accomodate for the new data 118 * #GNUNET_NO if the buffer can't accomodate for the new data
120 */ 119 */
121static int 120static int
122buffer_append (struct Buffer *buf, 121buffer_append(struct Buffer *buf,
123 const void *data, 122 const void *data,
124 size_t data_size, 123 size_t data_size,
125 size_t max_size) 124 size_t max_size)
126{ 125{
127 if (buf->fill + data_size > max_size) 126 if (buf->fill + data_size > max_size)
128 return GNUNET_NO; 127 return GNUNET_NO;
129 if (buf->fill + data_size > buf->alloc) 128 if (buf->fill + data_size > buf->alloc)
130 { 129 {
131 char *new_buf; 130 char *new_buf;
132 size_t new_size = buf->alloc; 131 size_t new_size = buf->alloc;
133 while (new_size < buf->fill + data_size) 132 while (new_size < buf->fill + data_size)
134 new_size += 2; 133 new_size += 2;
135 if (new_size > max_size) 134 if (new_size > max_size)
136 return GNUNET_NO; 135 return GNUNET_NO;
137 new_buf = GNUNET_malloc (new_size); 136 new_buf = GNUNET_malloc(new_size);
138 GNUNET_memcpy (new_buf, buf->data, buf->fill); 137 GNUNET_memcpy(new_buf, buf->data, buf->fill);
139 GNUNET_free (buf->data); 138 GNUNET_free(buf->data);
140 buf->data = new_buf; 139 buf->data = new_buf;
141 buf->alloc = new_size; 140 buf->alloc = new_size;
142 } 141 }
143 GNUNET_memcpy (buf->data + buf->fill, data, data_size); 142 GNUNET_memcpy(buf->data + buf->fill, data, data_size);
144 buf->fill += data_size; 143 buf->fill += data_size;
145 return GNUNET_OK; 144 return GNUNET_OK;
146} 145}
@@ -153,89 +152,95 @@ buffer_append (struct Buffer *buf,
153 * @return result code indicating the status of the operation 152 * @return result code indicating the status of the operation
154 */ 153 */
155static enum GNUNET_JSON_PostResult 154static enum GNUNET_JSON_PostResult
156inflate_data (struct Buffer *buf) 155inflate_data(struct Buffer *buf)
157{ 156{
158 z_stream z; 157 z_stream z;
159 char *tmp; 158 char *tmp;
160 size_t tmp_size; 159 size_t tmp_size;
161 int ret; 160 int ret;
162 161
163 memset (&z, 0, sizeof (z)); 162 memset(&z, 0, sizeof(z));
164 z.next_in = (Bytef *) buf->data; 163 z.next_in = (Bytef *)buf->data;
165 z.avail_in = buf->fill; 164 z.avail_in = buf->fill;
166 tmp_size = GNUNET_MIN (buf->max, buf->fill * 4); 165 tmp_size = GNUNET_MIN(buf->max, buf->fill * 4);
167 tmp = GNUNET_malloc (tmp_size); 166 tmp = GNUNET_malloc(tmp_size);
168 z.next_out = (Bytef *) tmp; 167 z.next_out = (Bytef *)tmp;
169 z.avail_out = tmp_size; 168 z.avail_out = tmp_size;
170 ret = inflateInit (&z); 169 ret = inflateInit(&z);
171 switch (ret) 170 switch (ret)
172 {
173 case Z_MEM_ERROR:
174 GNUNET_break (0);
175 return GNUNET_JSON_PR_OUT_OF_MEMORY;
176 case Z_STREAM_ERROR:
177 GNUNET_break_op (0);
178 return GNUNET_JSON_PR_JSON_INVALID;
179 case Z_OK:
180 break;
181 }
182 while (1)
183 {
184 ret = inflate (&z, 0);
185 switch (ret)
186 { 171 {
187 case Z_MEM_ERROR: 172 case Z_MEM_ERROR:
188 GNUNET_break (0); 173 GNUNET_break(0);
189 GNUNET_break (Z_OK == inflateEnd (&z));
190 GNUNET_free (tmp);
191 return GNUNET_JSON_PR_OUT_OF_MEMORY; 174 return GNUNET_JSON_PR_OUT_OF_MEMORY;
192 case Z_DATA_ERROR: 175
193 GNUNET_break (0); 176 case Z_STREAM_ERROR:
194 GNUNET_break (Z_OK == inflateEnd (&z)); 177 GNUNET_break_op(0);
195 GNUNET_free (tmp);
196 return GNUNET_JSON_PR_JSON_INVALID;
197 case Z_NEED_DICT:
198 GNUNET_break (0);
199 GNUNET_break (Z_OK == inflateEnd (&z));
200 GNUNET_free (tmp);
201 return GNUNET_JSON_PR_JSON_INVALID; 178 return GNUNET_JSON_PR_JSON_INVALID;
179
202 case Z_OK: 180 case Z_OK:
203 if ((0 < z.avail_out) && (0 == z.avail_in)) 181 break;
204 {
205 /* truncated input stream */
206 GNUNET_break (0);
207 GNUNET_break (Z_OK == inflateEnd (&z));
208 GNUNET_free (tmp);
209 return GNUNET_JSON_PR_JSON_INVALID;
210 }
211 if (0 < z.avail_out)
212 continue; /* just call it again */
213 /* output buffer full, can we grow it? */
214 if (tmp_size == buf->max)
215 {
216 /* already at max */
217 GNUNET_break (0);
218 GNUNET_break (Z_OK == inflateEnd (&z));
219 GNUNET_free (tmp);
220 return GNUNET_JSON_PR_OUT_OF_MEMORY;
221 }
222 if (tmp_size * 2 < tmp_size)
223 tmp_size = buf->max;
224 else
225 tmp_size = GNUNET_MIN (buf->max, tmp_size * 2);
226 tmp = GNUNET_realloc (tmp, tmp_size);
227 z.next_out = (Bytef *) &tmp[z.total_out];
228 continue;
229 case Z_STREAM_END:
230 /* decompression successful, make 'tmp' the new 'data' */
231 GNUNET_free (buf->data);
232 buf->data = tmp;
233 buf->alloc = tmp_size;
234 buf->fill = z.total_out;
235 GNUNET_break (Z_OK == inflateEnd (&z));
236 return GNUNET_JSON_PR_SUCCESS; /* at least for now */
237 } 182 }
238 } /* while (1) */ 183 while (1)
184 {
185 ret = inflate(&z, 0);
186 switch (ret)
187 {
188 case Z_MEM_ERROR:
189 GNUNET_break(0);
190 GNUNET_break(Z_OK == inflateEnd(&z));
191 GNUNET_free(tmp);
192 return GNUNET_JSON_PR_OUT_OF_MEMORY;
193
194 case Z_DATA_ERROR:
195 GNUNET_break(0);
196 GNUNET_break(Z_OK == inflateEnd(&z));
197 GNUNET_free(tmp);
198 return GNUNET_JSON_PR_JSON_INVALID;
199
200 case Z_NEED_DICT:
201 GNUNET_break(0);
202 GNUNET_break(Z_OK == inflateEnd(&z));
203 GNUNET_free(tmp);
204 return GNUNET_JSON_PR_JSON_INVALID;
205
206 case Z_OK:
207 if ((0 < z.avail_out) && (0 == z.avail_in))
208 {
209 /* truncated input stream */
210 GNUNET_break(0);
211 GNUNET_break(Z_OK == inflateEnd(&z));
212 GNUNET_free(tmp);
213 return GNUNET_JSON_PR_JSON_INVALID;
214 }
215 if (0 < z.avail_out)
216 continue; /* just call it again */
217 /* output buffer full, can we grow it? */
218 if (tmp_size == buf->max)
219 {
220 /* already at max */
221 GNUNET_break(0);
222 GNUNET_break(Z_OK == inflateEnd(&z));
223 GNUNET_free(tmp);
224 return GNUNET_JSON_PR_OUT_OF_MEMORY;
225 }
226 if (tmp_size * 2 < tmp_size)
227 tmp_size = buf->max;
228 else
229 tmp_size = GNUNET_MIN(buf->max, tmp_size * 2);
230 tmp = GNUNET_realloc(tmp, tmp_size);
231 z.next_out = (Bytef *)&tmp[z.total_out];
232 continue;
233
234 case Z_STREAM_END:
235 /* decompression successful, make 'tmp' the new 'data' */
236 GNUNET_free(buf->data);
237 buf->data = tmp;
238 buf->alloc = tmp_size;
239 buf->fill = z.total_out;
240 GNUNET_break(Z_OK == inflateEnd(&z));
241 return GNUNET_JSON_PR_SUCCESS; /* at least for now */
242 }
243 } /* while (1) */
239} 244}
240 245
241 246
@@ -255,12 +260,12 @@ inflate_data (struct Buffer *buf)
255 * @return result code indicating the status of the operation 260 * @return result code indicating the status of the operation
256 */ 261 */
257enum GNUNET_JSON_PostResult 262enum GNUNET_JSON_PostResult
258GNUNET_JSON_post_parser (size_t buffer_max, 263GNUNET_JSON_post_parser(size_t buffer_max,
259 struct MHD_Connection *connection, 264 struct MHD_Connection *connection,
260 void **con_cls, 265 void **con_cls,
261 const char *upload_data, 266 const char *upload_data,
262 size_t *upload_data_size, 267 size_t *upload_data_size,
263 json_t **json) 268 json_t **json)
264{ 269{
265 struct Buffer *r = *con_cls; 270 struct Buffer *r = *con_cls;
266 const char *ce; 271 const char *ce;
@@ -268,72 +273,71 @@ GNUNET_JSON_post_parser (size_t buffer_max,
268 273
269 *json = NULL; 274 *json = NULL;
270 if (NULL == *con_cls) 275 if (NULL == *con_cls)
271 {
272
273 /* We are seeing a fresh POST request. */
274 r = GNUNET_new (struct Buffer);
275 if (GNUNET_OK != buffer_init (r,
276 upload_data,
277 *upload_data_size,
278 REQUEST_BUFFER_INITIAL,
279 buffer_max))
280 { 276 {
281 *con_cls = NULL; 277 /* We are seeing a fresh POST request. */
282 buffer_deinit (r); 278 r = GNUNET_new(struct Buffer);
283 GNUNET_free (r); 279 if (GNUNET_OK != buffer_init(r,
284 return GNUNET_JSON_PR_OUT_OF_MEMORY; 280 upload_data,
281 *upload_data_size,
282 REQUEST_BUFFER_INITIAL,
283 buffer_max))
284 {
285 *con_cls = NULL;
286 buffer_deinit(r);
287 GNUNET_free(r);
288 return GNUNET_JSON_PR_OUT_OF_MEMORY;
289 }
290 /* everything OK, wait for more POST data */
291 *upload_data_size = 0;
292 *con_cls = r;
293 return GNUNET_JSON_PR_CONTINUE;
285 } 294 }
286 /* everything OK, wait for more POST data */
287 *upload_data_size = 0;
288 *con_cls = r;
289 return GNUNET_JSON_PR_CONTINUE;
290 }
291 if (0 != *upload_data_size) 295 if (0 != *upload_data_size)
292 {
293 /* We are seeing an old request with more data available. */
294
295 if (GNUNET_OK !=
296 buffer_append (r, upload_data, *upload_data_size, buffer_max))
297 { 296 {
298 /* Request too long */ 297 /* We are seeing an old request with more data available. */
299 *con_cls = NULL; 298
300 buffer_deinit (r); 299 if (GNUNET_OK !=
301 GNUNET_free (r); 300 buffer_append(r, upload_data, *upload_data_size, buffer_max))
302 return GNUNET_JSON_PR_REQUEST_TOO_LARGE; 301 {
302 /* Request too long */
303 *con_cls = NULL;
304 buffer_deinit(r);
305 GNUNET_free(r);
306 return GNUNET_JSON_PR_REQUEST_TOO_LARGE;
307 }
308 /* everything OK, wait for more POST data */
309 *upload_data_size = 0;
310 return GNUNET_JSON_PR_CONTINUE;
303 } 311 }
304 /* everything OK, wait for more POST data */
305 *upload_data_size = 0;
306 return GNUNET_JSON_PR_CONTINUE;
307 }
308 312
309 /* We have seen the whole request. */ 313 /* We have seen the whole request. */
310 ce = MHD_lookup_connection_value (connection, 314 ce = MHD_lookup_connection_value(connection,
311 MHD_HEADER_KIND, 315 MHD_HEADER_KIND,
312 MHD_HTTP_HEADER_CONTENT_ENCODING); 316 MHD_HTTP_HEADER_CONTENT_ENCODING);
313 if ((NULL != ce) && (0 == strcasecmp ("deflate", ce))) 317 if ((NULL != ce) && (0 == strcasecmp("deflate", ce)))
314 {
315 ret = inflate_data (r);
316 if (GNUNET_JSON_PR_SUCCESS != ret)
317 { 318 {
318 buffer_deinit (r); 319 ret = inflate_data(r);
319 GNUNET_free (r); 320 if (GNUNET_JSON_PR_SUCCESS != ret)
320 *con_cls = NULL; 321 {
321 return ret; 322 buffer_deinit(r);
323 GNUNET_free(r);
324 *con_cls = NULL;
325 return ret;
326 }
322 } 327 }
323 }
324 328
325 *json = json_loadb (r->data, r->fill, 0, NULL); 329 *json = json_loadb(r->data, r->fill, 0, NULL);
326 if (NULL == *json) 330 if (NULL == *json)
327 { 331 {
328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 332 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
329 "Failed to parse JSON request body\n"); 333 "Failed to parse JSON request body\n");
330 buffer_deinit (r); 334 buffer_deinit(r);
331 GNUNET_free (r); 335 GNUNET_free(r);
332 *con_cls = NULL; 336 *con_cls = NULL;
333 return GNUNET_JSON_PR_JSON_INVALID; 337 return GNUNET_JSON_PR_JSON_INVALID;
334 } 338 }
335 buffer_deinit (r); 339 buffer_deinit(r);
336 GNUNET_free (r); 340 GNUNET_free(r);
337 *con_cls = NULL; 341 *con_cls = NULL;
338 342
339 return GNUNET_JSON_PR_SUCCESS; 343 return GNUNET_JSON_PR_SUCCESS;
@@ -348,15 +352,15 @@ GNUNET_JSON_post_parser (size_t buffer_max,
348 * #GNUNET_JSON_post_parser(), to be cleaned up 352 * #GNUNET_JSON_post_parser(), to be cleaned up
349 */ 353 */
350void 354void
351GNUNET_JSON_post_parser_cleanup (void *con_cls) 355GNUNET_JSON_post_parser_cleanup(void *con_cls)
352{ 356{
353 struct Buffer *r = con_cls; 357 struct Buffer *r = con_cls;
354 358
355 if (NULL != r) 359 if (NULL != r)
356 { 360 {
357 buffer_deinit (r); 361 buffer_deinit(r);
358 GNUNET_free (r); 362 GNUNET_free(r);
359 } 363 }
360} 364}
361 365
362/* end of mhd_json.c */ 366/* end of mhd_json.c */
diff --git a/src/json/test_json.c b/src/json/test_json.c
index 40fb7f090..4b9fbc710 100644
--- a/src/json/test_json.c
+++ b/src/json/test_json.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 (C) 2015, 2016 GNUnet e.V. 3 (C) 2015, 2016 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
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file json/test_json.c 22 * @file json/test_json.c
@@ -34,32 +34,32 @@
34 * @return 0 on success 34 * @return 0 on success
35 */ 35 */
36static int 36static int
37test_abs_time () 37test_abs_time()
38{ 38{
39 json_t *j; 39 json_t *j;
40 struct GNUNET_TIME_Absolute a1; 40 struct GNUNET_TIME_Absolute a1;
41 struct GNUNET_TIME_Absolute a2; 41 struct GNUNET_TIME_Absolute a2;
42 struct GNUNET_JSON_Specification s1[] = {GNUNET_JSON_spec_absolute_time (NULL, 42 struct GNUNET_JSON_Specification s1[] = { GNUNET_JSON_spec_absolute_time(NULL,
43 &a2), 43 &a2),
44 GNUNET_JSON_spec_end ()}; 44 GNUNET_JSON_spec_end() };
45 struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_absolute_time (NULL, 45 struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_absolute_time(NULL,
46 &a2), 46 &a2),
47 GNUNET_JSON_spec_end ()}; 47 GNUNET_JSON_spec_end() };
48 48
49 a1 = GNUNET_TIME_absolute_get (); 49 a1 = GNUNET_TIME_absolute_get();
50 GNUNET_TIME_round_abs (&a1); 50 GNUNET_TIME_round_abs(&a1);
51 j = GNUNET_JSON_from_time_abs (a1); 51 j = GNUNET_JSON_from_time_abs(a1);
52 GNUNET_assert (NULL != j); 52 GNUNET_assert(NULL != j);
53 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL)); 53 GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(j, s1, NULL, NULL));
54 GNUNET_assert (a1.abs_value_us == a2.abs_value_us); 54 GNUNET_assert(a1.abs_value_us == a2.abs_value_us);
55 json_decref (j); 55 json_decref(j);
56 56
57 a1 = GNUNET_TIME_UNIT_FOREVER_ABS; 57 a1 = GNUNET_TIME_UNIT_FOREVER_ABS;
58 j = GNUNET_JSON_from_time_abs (a1); 58 j = GNUNET_JSON_from_time_abs(a1);
59 GNUNET_assert (NULL != j); 59 GNUNET_assert(NULL != j);
60 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL)); 60 GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(j, s2, NULL, NULL));
61 GNUNET_assert (a1.abs_value_us == a2.abs_value_us); 61 GNUNET_assert(a1.abs_value_us == a2.abs_value_us);
62 json_decref (j); 62 json_decref(j);
63 return 0; 63 return 0;
64} 64}
65 65
@@ -70,31 +70,31 @@ test_abs_time ()
70 * @return 0 on success 70 * @return 0 on success
71 */ 71 */
72static int 72static int
73test_rel_time () 73test_rel_time()
74{ 74{
75 json_t *j; 75 json_t *j;
76 struct GNUNET_TIME_Relative r1; 76 struct GNUNET_TIME_Relative r1;
77 struct GNUNET_TIME_Relative r2; 77 struct GNUNET_TIME_Relative r2;
78 struct GNUNET_JSON_Specification s1[] = {GNUNET_JSON_spec_relative_time (NULL, 78 struct GNUNET_JSON_Specification s1[] = { GNUNET_JSON_spec_relative_time(NULL,
79 &r2), 79 &r2),
80 GNUNET_JSON_spec_end ()}; 80 GNUNET_JSON_spec_end() };
81 struct GNUNET_JSON_Specification s2[] = {GNUNET_JSON_spec_relative_time (NULL, 81 struct GNUNET_JSON_Specification s2[] = { GNUNET_JSON_spec_relative_time(NULL,
82 &r2), 82 &r2),
83 GNUNET_JSON_spec_end ()}; 83 GNUNET_JSON_spec_end() };
84 84
85 r1 = GNUNET_TIME_UNIT_SECONDS; 85 r1 = GNUNET_TIME_UNIT_SECONDS;
86 j = GNUNET_JSON_from_time_rel (r1); 86 j = GNUNET_JSON_from_time_rel(r1);
87 GNUNET_assert (NULL != j); 87 GNUNET_assert(NULL != j);
88 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s1, NULL, NULL)); 88 GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(j, s1, NULL, NULL));
89 GNUNET_assert (r1.rel_value_us == r2.rel_value_us); 89 GNUNET_assert(r1.rel_value_us == r2.rel_value_us);
90 json_decref (j); 90 json_decref(j);
91 91
92 r1 = GNUNET_TIME_UNIT_FOREVER_REL; 92 r1 = GNUNET_TIME_UNIT_FOREVER_REL;
93 j = GNUNET_JSON_from_time_rel (r1); 93 j = GNUNET_JSON_from_time_rel(r1);
94 GNUNET_assert (NULL != j); 94 GNUNET_assert(NULL != j);
95 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, s2, NULL, NULL)); 95 GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(j, s2, NULL, NULL));
96 GNUNET_assert (r1.rel_value_us == r2.rel_value_us); 96 GNUNET_assert(r1.rel_value_us == r2.rel_value_us);
97 json_decref (j); 97 json_decref(j);
98 return 0; 98 return 0;
99} 99}
100 100
@@ -105,26 +105,26 @@ test_rel_time ()
105 * @return 0 on success 105 * @return 0 on success
106 */ 106 */
107static int 107static int
108test_raw () 108test_raw()
109{ 109{
110 char blob[256]; 110 char blob[256];
111 unsigned int i; 111 unsigned int i;
112 json_t *j; 112 json_t *j;
113 113
114 for (i = 0; i <= 256; i++) 114 for (i = 0; i <= 256; i++)
115 { 115 {
116 char blob2[256]; 116 char blob2[256];
117 struct GNUNET_JSON_Specification spec[] = {GNUNET_JSON_spec_fixed (NULL, 117 struct GNUNET_JSON_Specification spec[] = { GNUNET_JSON_spec_fixed(NULL,
118 blob2, 118 blob2,
119 i), 119 i),
120 GNUNET_JSON_spec_end ()}; 120 GNUNET_JSON_spec_end() };
121 121
122 memset (blob, i, i); 122 memset(blob, i, i);
123 j = GNUNET_JSON_from_data (blob, i); 123 j = GNUNET_JSON_from_data(blob, i);
124 GNUNET_assert (NULL != j); 124 GNUNET_assert(NULL != j);
125 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (j, spec, NULL, NULL)); 125 GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(j, spec, NULL, NULL));
126 GNUNET_assert (0 == memcmp (blob, blob2, i)); 126 GNUNET_assert(0 == memcmp(blob, blob2, i));
127 } 127 }
128 return 0; 128 return 0;
129} 129}
130 130
@@ -135,36 +135,36 @@ test_raw ()
135 * @return 0 on success 135 * @return 0 on success
136 */ 136 */
137static int 137static int
138test_rsa () 138test_rsa()
139{ 139{
140 struct GNUNET_CRYPTO_RsaPublicKey *pub; 140 struct GNUNET_CRYPTO_RsaPublicKey *pub;
141 struct GNUNET_CRYPTO_RsaPublicKey *pub2; 141 struct GNUNET_CRYPTO_RsaPublicKey *pub2;
142 struct GNUNET_JSON_Specification pspec[] = 142 struct GNUNET_JSON_Specification pspec[] =
143 {GNUNET_JSON_spec_rsa_public_key (NULL, &pub2), GNUNET_JSON_spec_end ()}; 143 { GNUNET_JSON_spec_rsa_public_key(NULL, &pub2), GNUNET_JSON_spec_end() };
144 struct GNUNET_CRYPTO_RsaSignature *sig; 144 struct GNUNET_CRYPTO_RsaSignature *sig;
145 struct GNUNET_CRYPTO_RsaSignature *sig2; 145 struct GNUNET_CRYPTO_RsaSignature *sig2;
146 struct GNUNET_JSON_Specification sspec[] = 146 struct GNUNET_JSON_Specification sspec[] =
147 {GNUNET_JSON_spec_rsa_signature (NULL, &sig2), GNUNET_JSON_spec_end ()}; 147 { GNUNET_JSON_spec_rsa_signature(NULL, &sig2), GNUNET_JSON_spec_end() };
148 struct GNUNET_CRYPTO_RsaPrivateKey *priv; 148 struct GNUNET_CRYPTO_RsaPrivateKey *priv;
149 struct GNUNET_HashCode msg; 149 struct GNUNET_HashCode msg;
150 json_t *jp; 150 json_t *jp;
151 json_t *js; 151 json_t *js;
152 152
153 priv = GNUNET_CRYPTO_rsa_private_key_create (1024); 153 priv = GNUNET_CRYPTO_rsa_private_key_create(1024);
154 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); 154 pub = GNUNET_CRYPTO_rsa_private_key_get_public(priv);
155 memset (&msg, 42, sizeof (msg)); 155 memset(&msg, 42, sizeof(msg));
156 sig = GNUNET_CRYPTO_rsa_sign_fdh (priv, &msg); 156 sig = GNUNET_CRYPTO_rsa_sign_fdh(priv, &msg);
157 GNUNET_assert (NULL != (jp = GNUNET_JSON_from_rsa_public_key (pub))); 157 GNUNET_assert(NULL != (jp = GNUNET_JSON_from_rsa_public_key(pub)));
158 GNUNET_assert (NULL != (js = GNUNET_JSON_from_rsa_signature (sig))); 158 GNUNET_assert(NULL != (js = GNUNET_JSON_from_rsa_signature(sig)));
159 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (jp, pspec, NULL, NULL)); 159 GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(jp, pspec, NULL, NULL));
160 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (js, sspec, NULL, NULL)); 160 GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(js, sspec, NULL, NULL));
161 GNUNET_break (0 == GNUNET_CRYPTO_rsa_signature_cmp (sig, sig2)); 161 GNUNET_break(0 == GNUNET_CRYPTO_rsa_signature_cmp(sig, sig2));
162 GNUNET_break (0 == GNUNET_CRYPTO_rsa_public_key_cmp (pub, pub2)); 162 GNUNET_break(0 == GNUNET_CRYPTO_rsa_public_key_cmp(pub, pub2));
163 GNUNET_CRYPTO_rsa_signature_free (sig); 163 GNUNET_CRYPTO_rsa_signature_free(sig);
164 GNUNET_CRYPTO_rsa_signature_free (sig2); 164 GNUNET_CRYPTO_rsa_signature_free(sig2);
165 GNUNET_CRYPTO_rsa_private_key_free (priv); 165 GNUNET_CRYPTO_rsa_private_key_free(priv);
166 GNUNET_CRYPTO_rsa_public_key_free (pub); 166 GNUNET_CRYPTO_rsa_public_key_free(pub);
167 GNUNET_CRYPTO_rsa_public_key_free (pub2); 167 GNUNET_CRYPTO_rsa_public_key_free(pub2);
168 return 0; 168 return 0;
169} 169}
170 170
@@ -175,47 +175,47 @@ test_rsa ()
175 * @return 0 on success 175 * @return 0 on success
176 */ 176 */
177static int 177static int
178test_boolean () 178test_boolean()
179{ 179{
180 int b1; 180 int b1;
181 int b2; 181 int b2;
182 json_t *json; 182 json_t *json;
183 struct GNUNET_JSON_Specification pspec[] = {GNUNET_JSON_spec_boolean ("b1", 183 struct GNUNET_JSON_Specification pspec[] = { GNUNET_JSON_spec_boolean("b1",
184 &b1), 184 &b1),
185 GNUNET_JSON_spec_boolean ("b2", 185 GNUNET_JSON_spec_boolean("b2",
186 &b2), 186 &b2),
187 GNUNET_JSON_spec_end ()}; 187 GNUNET_JSON_spec_end() };
188 188
189 json = json_object (); 189 json = json_object();
190 json_object_set_new (json, "b1", json_true ()); 190 json_object_set_new(json, "b1", json_true());
191 json_object_set_new (json, "b2", json_false ()); 191 json_object_set_new(json, "b2", json_false());
192 192
193 GNUNET_assert (GNUNET_OK == GNUNET_JSON_parse (json, pspec, NULL, NULL)); 193 GNUNET_assert(GNUNET_OK == GNUNET_JSON_parse(json, pspec, NULL, NULL));
194 194
195 GNUNET_assert (GNUNET_YES == b1); 195 GNUNET_assert(GNUNET_YES == b1);
196 GNUNET_assert (GNUNET_NO == b2); 196 GNUNET_assert(GNUNET_NO == b2);
197 197
198 json_object_set_new (json, "b1", json_integer (42)); 198 json_object_set_new(json, "b1", json_integer(42));
199 199
200 GNUNET_assert (GNUNET_OK != GNUNET_JSON_parse (json, pspec, NULL, NULL)); 200 GNUNET_assert(GNUNET_OK != GNUNET_JSON_parse(json, pspec, NULL, NULL));
201 201
202 return 0; 202 return 0;
203} 203}
204 204
205 205
206int 206int
207main (int argc, const char *const argv[]) 207main(int argc, const char *const argv[])
208{ 208{
209 GNUNET_log_setup ("test-json", "WARNING", NULL); 209 GNUNET_log_setup("test-json", "WARNING", NULL);
210 if (0 != test_abs_time ()) 210 if (0 != test_abs_time())
211 return 1; 211 return 1;
212 if (0 != test_rel_time ()) 212 if (0 != test_rel_time())
213 return 1; 213 return 1;
214 if (0 != test_raw ()) 214 if (0 != test_raw())
215 return 1; 215 return 1;
216 if (0 != test_rsa ()) 216 if (0 != test_rsa())
217 return 1; 217 return 1;
218 if (0 != test_boolean ()) 218 if (0 != test_boolean())
219 return 1; 219 return 1;
220 /* FIXME: test EdDSA signature conversion... */ 220 /* FIXME: test EdDSA signature conversion... */
221 return 0; 221 return 0;
diff --git a/src/json/test_json_mhd.c b/src/json/test_json_mhd.c
index 73efc4d97..213b95e12 100644
--- a/src/json/test_json_mhd.c
+++ b/src/json/test_json_mhd.c
@@ -1,22 +1,22 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 (C) 2019 GNUnet e.V. 3 (C) 2019 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
7 by the Free Software Foundation, either version 3 of the License, 7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file json/test_json_mhd.c 22 * @file json/test_json_mhd.c
@@ -37,65 +37,69 @@ static int global_ret;
37 37
38 38
39static int 39static int
40access_handler_cb (void *cls, 40access_handler_cb(void *cls,
41 struct MHD_Connection *connection, 41 struct MHD_Connection *connection,
42 const char *url, 42 const char *url,
43 const char *method, 43 const char *method,
44 const char *version, 44 const char *version,
45 const char *upload_data, 45 const char *upload_data,
46 size_t *upload_data_size, 46 size_t *upload_data_size,
47 void **con_cls) 47 void **con_cls)
48{ 48{
49 int ret; 49 int ret;
50 json_t *json; 50 json_t *json;
51 struct MHD_Response *resp; 51 struct MHD_Response *resp;
52 52
53 json = NULL; 53 json = NULL;
54 ret = GNUNET_JSON_post_parser (MAX_SIZE, 54 ret = GNUNET_JSON_post_parser(MAX_SIZE,
55 connection, 55 connection,
56 con_cls, 56 con_cls,
57 upload_data, 57 upload_data,
58 upload_data_size, 58 upload_data_size,
59 &json); 59 &json);
60 switch (ret) 60 switch (ret)
61 {
62 case GNUNET_JSON_PR_SUCCESS:
63 if (json_equal (bigj, json))
64 {
65 global_ret = 0;
66 }
67 else
68 { 61 {
69 GNUNET_break (0); 62 case GNUNET_JSON_PR_SUCCESS:
70 global_ret = 6; 63 if (json_equal(bigj, json))
64 {
65 global_ret = 0;
66 }
67 else
68 {
69 GNUNET_break(0);
70 global_ret = 6;
71 }
72 json_decref(json);
73 resp = MHD_create_response_from_buffer(3, "OK\n", MHD_RESPMEM_PERSISTENT);
74 ret = MHD_queue_response(connection, MHD_HTTP_OK, resp);
75 MHD_destroy_response(resp);
76 return ret;
77
78 case GNUNET_JSON_PR_CONTINUE:
79 return MHD_YES;
80
81 case GNUNET_JSON_PR_OUT_OF_MEMORY:
82 GNUNET_break(0);
83 global_ret = 3;
84 break;
85
86 case GNUNET_JSON_PR_REQUEST_TOO_LARGE:
87 GNUNET_break(0);
88 global_ret = 4;
89 break;
90
91 case GNUNET_JSON_PR_JSON_INVALID:
92 GNUNET_break(0);
93 global_ret = 5;
94 break;
71 } 95 }
72 json_decref (json); 96 GNUNET_break(0);
73 resp = MHD_create_response_from_buffer (3, "OK\n", MHD_RESPMEM_PERSISTENT);
74 ret = MHD_queue_response (connection, MHD_HTTP_OK, resp);
75 MHD_destroy_response (resp);
76 return ret;
77 case GNUNET_JSON_PR_CONTINUE:
78 return MHD_YES;
79 case GNUNET_JSON_PR_OUT_OF_MEMORY:
80 GNUNET_break (0);
81 global_ret = 3;
82 break;
83 case GNUNET_JSON_PR_REQUEST_TOO_LARGE:
84 GNUNET_break (0);
85 global_ret = 4;
86 break;
87 case GNUNET_JSON_PR_JSON_INVALID:
88 GNUNET_break (0);
89 global_ret = 5;
90 break;
91 }
92 GNUNET_break (0);
93 return MHD_NO; 97 return MHD_NO;
94} 98}
95 99
96 100
97int 101int
98main (int argc, const char *const argv[]) 102main(int argc, const char *const argv[])
99{ 103{
100 struct MHD_Daemon *daemon; 104 struct MHD_Daemon *daemon;
101 uint16_t port; 105 uint16_t port;
@@ -108,79 +112,79 @@ main (int argc, const char *const argv[])
108 uLongf dlen; 112 uLongf dlen;
109 struct curl_slist *json_header; 113 struct curl_slist *json_header;
110 114
111 GNUNET_log_setup ("test-json-mhd", "WARNING", NULL); 115 GNUNET_log_setup("test-json-mhd", "WARNING", NULL);
112 global_ret = 2; 116 global_ret = 2;
113 daemon = MHD_start_daemon (MHD_USE_DUAL_STACK | MHD_USE_AUTO_INTERNAL_THREAD, 117 daemon = MHD_start_daemon(MHD_USE_DUAL_STACK | MHD_USE_AUTO_INTERNAL_THREAD,
114 0, 118 0,
115 NULL, 119 NULL,
116 NULL, 120 NULL,
117 &access_handler_cb, 121 &access_handler_cb,
118 NULL, 122 NULL,
119 MHD_OPTION_END); 123 MHD_OPTION_END);
120 if (NULL == daemon) 124 if (NULL == daemon)
121 return 77; 125 return 77;
122 bigj = json_object (); 126 bigj = json_object();
123 json_object_set_new (bigj, "test", json_string ("value")); 127 json_object_set_new(bigj, "test", json_string("value"));
124 for (unsigned int i = 0; i < 1000; i++) 128 for (unsigned int i = 0; i < 1000; i++)
125 { 129 {
126 char tmp[5]; 130 char tmp[5];
127 131
128 GNUNET_snprintf (tmp, sizeof (tmp), "%u", i); 132 GNUNET_snprintf(tmp, sizeof(tmp), "%u", i);
129 json_object_set_new (bigj, tmp, json_string (tmp)); 133 json_object_set_new(bigj, tmp, json_string(tmp));
130 } 134 }
131 str = json_dumps (bigj, JSON_INDENT (2)); 135 str = json_dumps(bigj, JSON_INDENT(2));
132 slen = strlen (str); 136 slen = strlen(str);
133 137
134#ifdef compressBound 138#ifdef compressBound
135 dlen = compressBound (slen); 139 dlen = compressBound(slen);
136#else 140#else
137 dlen = slen + slen / 100 + 20; 141 dlen = slen + slen / 100 + 20;
138 /* documentation says 100.1% oldSize + 12 bytes, but we 142 /* documentation says 100.1% oldSize + 12 bytes, but we
139 * should be able to overshoot by more to be safe */ 143 * should be able to overshoot by more to be safe */
140#endif 144#endif
141 post_data = GNUNET_malloc (dlen); 145 post_data = GNUNET_malloc(dlen);
142 if (Z_OK != 146 if (Z_OK !=
143 compress2 ((Bytef *) post_data, &dlen, (const Bytef *) str, slen, 9)) 147 compress2((Bytef *)post_data, &dlen, (const Bytef *)str, slen, 9))
144 { 148 {
145 GNUNET_break (0); 149 GNUNET_break(0);
146 MHD_stop_daemon (daemon); 150 MHD_stop_daemon(daemon);
147 json_decref (bigj); 151 json_decref(bigj);
148 GNUNET_free (post_data); 152 GNUNET_free(post_data);
149 GNUNET_free (str); 153 GNUNET_free(str);
150 return 1; 154 return 1;
151 } 155 }
152 post_data_size = (long) dlen; 156 post_data_size = (long)dlen;
153 port = MHD_get_daemon_info (daemon, MHD_DAEMON_INFO_BIND_PORT)->port; 157 port = MHD_get_daemon_info(daemon, MHD_DAEMON_INFO_BIND_PORT)->port;
154 easy = curl_easy_init (); 158 easy = curl_easy_init();
155 GNUNET_asprintf (&url, "http://localhost:%u/", (unsigned int) port); 159 GNUNET_asprintf(&url, "http://localhost:%u/", (unsigned int)port);
156 curl_easy_setopt (easy, CURLOPT_VERBOSE, 0); 160 curl_easy_setopt(easy, CURLOPT_VERBOSE, 0);
157 curl_easy_setopt (easy, CURLOPT_URL, url); 161 curl_easy_setopt(easy, CURLOPT_URL, url);
158 curl_easy_setopt (easy, CURLOPT_POST, 1); 162 curl_easy_setopt(easy, CURLOPT_POST, 1);
159 curl_easy_setopt (easy, CURLOPT_POSTFIELDS, post_data); 163 curl_easy_setopt(easy, CURLOPT_POSTFIELDS, post_data);
160 curl_easy_setopt (easy, CURLOPT_POSTFIELDSIZE, post_data_size); 164 curl_easy_setopt(easy, CURLOPT_POSTFIELDSIZE, post_data_size);
161 165
162 json_header = curl_slist_append (NULL, "Content-Type: application/json"); 166 json_header = curl_slist_append(NULL, "Content-Type: application/json");
163 json_header = curl_slist_append (json_header, "Content-Encoding: deflate"); 167 json_header = curl_slist_append(json_header, "Content-Encoding: deflate");
164 curl_easy_setopt (easy, CURLOPT_HTTPHEADER, json_header); 168 curl_easy_setopt(easy, CURLOPT_HTTPHEADER, json_header);
165 if (0 != curl_easy_perform (easy)) 169 if (0 != curl_easy_perform(easy))
166 { 170 {
167 GNUNET_break (0); 171 GNUNET_break(0);
168 MHD_stop_daemon (daemon); 172 MHD_stop_daemon(daemon);
169 GNUNET_free (url); 173 GNUNET_free(url);
170 json_decref (bigj); 174 json_decref(bigj);
171 GNUNET_free (post_data); 175 GNUNET_free(post_data);
172 GNUNET_free (str); 176 GNUNET_free(str);
173 curl_slist_free_all (json_header); 177 curl_slist_free_all(json_header);
174 curl_easy_cleanup (easy); 178 curl_easy_cleanup(easy);
175 return 1; 179 return 1;
176 } 180 }
177 MHD_stop_daemon (daemon); 181 MHD_stop_daemon(daemon);
178 GNUNET_free (url); 182 GNUNET_free(url);
179 json_decref (bigj); 183 json_decref(bigj);
180 GNUNET_free (post_data); 184 GNUNET_free(post_data);
181 GNUNET_free (str); 185 GNUNET_free(str);
182 curl_slist_free_all (json_header); 186 curl_slist_free_all(json_header);
183 curl_easy_cleanup (easy); 187 curl_easy_cleanup(easy);
184 return global_ret; 188 return global_ret;
185} 189}
186 190