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