aboutsummaryrefslogtreecommitdiff
path: root/src/json/json_generator.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/json/json_generator.c')
-rw-r--r--src/json/json_generator.c219
1 files changed, 0 insertions, 219 deletions
diff --git a/src/json/json_generator.c b/src/json/json_generator.c
deleted file mode 100644
index 5421b9527..000000000
--- a/src/json/json_generator.c
+++ /dev/null
@@ -1,219 +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
30json_t *
31GNUNET_JSON_from_data (const void *data,
32 size_t size)
33{
34 char *buf;
35 json_t *json;
36
37 if ((size * 8 + 4) / 5 + 1 >=
38 GNUNET_MAX_MALLOC_CHECKED)
39 {
40 GNUNET_break (0);
41 return NULL;
42 }
43 buf = GNUNET_STRINGS_data_to_string_alloc (data,
44 size);
45 json = json_string (buf);
46 GNUNET_free (buf);
47 GNUNET_break (NULL != json);
48 return json;
49}
50
51
52json_t *
53GNUNET_JSON_from_timestamp (struct GNUNET_TIME_Timestamp stamp)
54{
55 json_t *j;
56
57 j = json_object ();
58 if (NULL == j)
59 {
60 GNUNET_break (0);
61 return NULL;
62 }
63 if (GNUNET_TIME_absolute_is_never (stamp.abs_time))
64 {
65 if (0 !=
66 json_object_set_new (j,
67 "t_s",
68 json_string ("never")))
69 {
70 GNUNET_break (0);
71 json_decref (j);
72 return NULL;
73 }
74 if (0 !=
75 json_object_set_new (j,
76 "t_ms",
77 json_string ("never")))
78 {
79 GNUNET_break (0);
80 json_decref (j);
81 return NULL;
82 }
83 return j;
84 }
85 GNUNET_assert (
86 0 ==
87 (stamp.abs_time.abs_value_us
88 % GNUNET_TIME_UNIT_SECONDS.rel_value_us));
89 if (0 !=
90 json_object_set_new (
91 j,
92 "t_s",
93 json_integer (
94 (json_int_t) (stamp.abs_time.abs_value_us
95 / GNUNET_TIME_UNIT_SECONDS.rel_value_us))))
96 {
97 GNUNET_break (0);
98 json_decref (j);
99 return NULL;
100 }
101 if (0 !=
102 json_object_set_new (
103 j,
104 "t_ms",
105 json_integer (
106 (json_int_t) (stamp.abs_time.abs_value_us
107 / GNUNET_TIME_UNIT_MILLISECONDS.rel_value_us))))
108 {
109 GNUNET_break (0);
110 json_decref (j);
111 return NULL;
112 }
113 return j;
114}
115
116
117json_t *
118GNUNET_JSON_from_timestamp_nbo (struct GNUNET_TIME_TimestampNBO stamp)
119{
120 return GNUNET_JSON_from_timestamp (GNUNET_TIME_timestamp_ntoh (stamp));
121}
122
123
124json_t *
125GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
126{
127 json_t *j;
128
129 j = json_object ();
130 if (NULL == j)
131 {
132 GNUNET_break (0);
133 return NULL;
134 }
135 if (GNUNET_TIME_relative_is_forever (stamp))
136 {
137 if (0 !=
138 json_object_set_new (j,
139 "d_us",
140 json_string ("forever")))
141 {
142 GNUNET_break (0);
143 json_decref (j);
144 return NULL;
145 }
146 if (0 !=
147 json_object_set_new (j,
148 "d_ms",
149 json_string ("forever")))
150 {
151 GNUNET_break (0);
152 json_decref (j);
153 return NULL;
154 }
155 return j;
156 }
157 if (stamp.rel_value_us >= (1LLU << 53))
158 {
159 /* value is larger than allowed */
160 GNUNET_break (0);
161 return NULL;
162 }
163 if (0 !=
164 json_object_set_new (
165 j,
166 "d_us",
167 json_integer ((json_int_t) stamp.rel_value_us)))
168 {
169 GNUNET_break (0);
170 json_decref (j);
171 return NULL;
172 }
173 if (0 !=
174 json_object_set_new (
175 j,
176 "d_ms",
177 json_integer (((json_int_t) stamp.rel_value_us)/1000LL)))
178 {
179 GNUNET_break (0);
180 json_decref (j);
181 return NULL;
182 }
183 return j;
184}
185
186
187json_t *
188GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
189{
190 void *buf;
191 size_t buf_len;
192 json_t *ret;
193
194 buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
195 &buf);
196 ret = GNUNET_JSON_from_data (buf,
197 buf_len);
198 GNUNET_free (buf);
199 return ret;
200}
201
202
203json_t *
204GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
205{
206 void *buf;
207 size_t buf_len;
208 json_t *ret;
209
210 buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
211 &buf);
212 ret = GNUNET_JSON_from_data (buf,
213 buf_len);
214 GNUNET_free (buf);
215 return ret;
216}
217
218
219/* End of json/json_generator.c */