diff options
Diffstat (limited to 'src/json/json_generator.c')
-rw-r--r-- | src/json/json_generator.c | 211 |
1 files changed, 0 insertions, 211 deletions
diff --git a/src/json/json_generator.c b/src/json/json_generator.c deleted file mode 100644 index 0c513ca9d..000000000 --- a/src/json/json_generator.c +++ /dev/null | |||
@@ -1,211 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2014, 2015, 2016 GNUnet e.V. | ||
4 | |||
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 | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file json/json_generator.c | ||
22 | * @brief helper functions for generating JSON from GNUnet data structures | ||
23 | * @author Sree Harsha Totakura <sreeharsha@totakura.in> | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_util_lib.h" | ||
27 | #include "gnunet_json_lib.h" | ||
28 | |||
29 | |||
30 | /** | ||
31 | * Convert binary data to a JSON string | ||
32 | * with the base32crockford encoding. | ||
33 | * | ||
34 | * @param data binary data | ||
35 | * @param size size of @a data in bytes | ||
36 | * @return json string that encodes @a data | ||
37 | */ | ||
38 | json_t * | ||
39 | GNUNET_JSON_from_data (const void *data, | ||
40 | size_t size) | ||
41 | { | ||
42 | char *buf; | ||
43 | json_t *json; | ||
44 | |||
45 | if ((size * 8 + 4) / 5 + 1 >= | ||
46 | GNUNET_MAX_MALLOC_CHECKED) | ||
47 | { | ||
48 | GNUNET_break (0); | ||
49 | return NULL; | ||
50 | } | ||
51 | buf = GNUNET_STRINGS_data_to_string_alloc (data, | ||
52 | size); | ||
53 | json = json_string (buf); | ||
54 | GNUNET_free (buf); | ||
55 | GNUNET_break (NULL != json); | ||
56 | return json; | ||
57 | } | ||
58 | |||
59 | |||
60 | /** | ||
61 | * Convert absolute timestamp to a json string. | ||
62 | * | ||
63 | * @param stamp the time stamp | ||
64 | * @return a json string with the timestamp in @a stamp | ||
65 | */ | ||
66 | json_t * | ||
67 | GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp) | ||
68 | { | ||
69 | json_t *j; | ||
70 | |||
71 | GNUNET_assert (GNUNET_OK == | ||
72 | GNUNET_TIME_round_abs (&stamp)); | ||
73 | |||
74 | j = json_object (); | ||
75 | if (NULL == j) | ||
76 | { | ||
77 | GNUNET_break (0); | ||
78 | return NULL; | ||
79 | } | ||
80 | if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) | ||
81 | { | ||
82 | if (0 != | ||
83 | json_object_set_new (j, | ||
84 | "t_ms", | ||
85 | json_string ("never"))) | ||
86 | { | ||
87 | GNUNET_break (0); | ||
88 | json_decref (j); | ||
89 | return NULL; | ||
90 | } | ||
91 | return j; | ||
92 | } | ||
93 | if (0 != | ||
94 | json_object_set_new (j, | ||
95 | "t_ms", | ||
96 | json_integer ((json_int_t) (stamp.abs_value_us | ||
97 | / 1000LL)))) | ||
98 | { | ||
99 | GNUNET_break (0); | ||
100 | json_decref (j); | ||
101 | return NULL; | ||
102 | } | ||
103 | return j; | ||
104 | } | ||
105 | |||
106 | |||
107 | /** | ||
108 | * Convert absolute timestamp to a json string. | ||
109 | * | ||
110 | * @param stamp the time stamp | ||
111 | * @return a json string with the timestamp in @a stamp | ||
112 | */ | ||
113 | json_t * | ||
114 | GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp) | ||
115 | { | ||
116 | return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp)); | ||
117 | } | ||
118 | |||
119 | |||
120 | /** | ||
121 | * Convert relative timestamp to a json string. | ||
122 | * | ||
123 | * @param stamp the time stamp | ||
124 | * @return a json string with the timestamp in @a stamp | ||
125 | */ | ||
126 | json_t * | ||
127 | GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp) | ||
128 | { | ||
129 | json_t *j; | ||
130 | |||
131 | GNUNET_assert (GNUNET_OK == | ||
132 | GNUNET_TIME_round_rel (&stamp)); | ||
133 | |||
134 | j = json_object (); | ||
135 | if (NULL == j) | ||
136 | { | ||
137 | GNUNET_break (0); | ||
138 | return NULL; | ||
139 | } | ||
140 | if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us) | ||
141 | { | ||
142 | if (0 != | ||
143 | json_object_set_new (j, | ||
144 | "d_ms", | ||
145 | json_string ("forever"))) | ||
146 | { | ||
147 | GNUNET_break (0); | ||
148 | json_decref (j); | ||
149 | return NULL; | ||
150 | } | ||
151 | return j; | ||
152 | } | ||
153 | if (0 != | ||
154 | json_object_set_new (j, | ||
155 | "d_ms", | ||
156 | json_integer ((json_int_t) (stamp.rel_value_us | ||
157 | / 1000LL)))) | ||
158 | { | ||
159 | GNUNET_break (0); | ||
160 | json_decref (j); | ||
161 | return NULL; | ||
162 | } | ||
163 | return j; | ||
164 | } | ||
165 | |||
166 | |||
167 | /** | ||
168 | * Convert RSA public key to JSON. | ||
169 | * | ||
170 | * @param pk public key to convert | ||
171 | * @return corresponding JSON encoding | ||
172 | */ | ||
173 | json_t * | ||
174 | GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk) | ||
175 | { | ||
176 | void *buf; | ||
177 | size_t buf_len; | ||
178 | json_t *ret; | ||
179 | |||
180 | buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk, | ||
181 | &buf); | ||
182 | ret = GNUNET_JSON_from_data (buf, | ||
183 | buf_len); | ||
184 | GNUNET_free (buf); | ||
185 | return ret; | ||
186 | } | ||
187 | |||
188 | |||
189 | /** | ||
190 | * Convert RSA signature to JSON. | ||
191 | * | ||
192 | * @param sig signature to convert | ||
193 | * @return corresponding JSON encoding | ||
194 | */ | ||
195 | json_t * | ||
196 | GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig) | ||
197 | { | ||
198 | void *buf; | ||
199 | size_t buf_len; | ||
200 | json_t *ret; | ||
201 | |||
202 | buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig, | ||
203 | &buf); | ||
204 | ret = GNUNET_JSON_from_data (buf, | ||
205 | buf_len); | ||
206 | GNUNET_free (buf); | ||
207 | return ret; | ||
208 | } | ||
209 | |||
210 | |||
211 | /* End of json/json_generator.c */ | ||