diff options
Diffstat (limited to 'src/json/json_generator.c')
-rw-r--r-- | src/json/json_generator.c | 168 |
1 files changed, 84 insertions, 84 deletions
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 | */ |
38 | json_t * | 38 | json_t * |
39 | GNUNET_JSON_from_data (const void *data, | 39 | GNUNET_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 | */ |
58 | json_t * | 58 | json_t * |
59 | GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp) | 59 | GNUNET_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 | */ |
85 | json_t * | 85 | json_t * |
86 | GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp) | 86 | GNUNET_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 | */ |
98 | json_t * | 98 | json_t * |
99 | GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp) | 99 | GNUNET_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 | */ |
125 | json_t * | 125 | json_t * |
126 | GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk) | 126 | GNUNET_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 | */ |
147 | json_t * | 147 | json_t * |
148 | GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig) | 148 | GNUNET_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 | */ |
169 | json_t * | 169 | json_t * |
170 | GNUNET_JSON_from_gnsrecord (const char* rname, | 170 | GNUNET_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 | ||