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.c209
1 files changed, 0 insertions, 209 deletions
diff --git a/src/json/json_generator.c b/src/json/json_generator.c
deleted file mode 100644
index 4fda86e32..000000000
--- a/src/json/json_generator.c
+++ /dev/null
@@ -1,209 +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_data64 (const void *data,
54 size_t size)
55{
56 char *buf = NULL;
57 json_t *json;
58 size_t len;
59
60 if ((size * 8 + 5) / 6 + 1 >=
61 GNUNET_MAX_MALLOC_CHECKED)
62 {
63 GNUNET_break (0);
64 return NULL;
65 }
66 len = GNUNET_STRINGS_base64_encode (data,
67 size,
68 &buf);
69 if (NULL == buf)
70 {
71 GNUNET_break (0);
72 return NULL;
73 }
74 json = json_stringn (buf,
75 len);
76 GNUNET_free (buf);
77 GNUNET_break (NULL != json);
78 return json;
79}
80
81
82json_t *
83GNUNET_JSON_from_timestamp (struct GNUNET_TIME_Timestamp stamp)
84{
85 json_t *j;
86
87 j = json_object ();
88 if (NULL == j)
89 {
90 GNUNET_break (0);
91 return NULL;
92 }
93 if (GNUNET_TIME_absolute_is_never (stamp.abs_time))
94 {
95 if (0 !=
96 json_object_set_new (j,
97 "t_s",
98 json_string ("never")))
99 {
100 GNUNET_break (0);
101 json_decref (j);
102 return NULL;
103 }
104 return j;
105 }
106 GNUNET_assert (
107 0 ==
108 (stamp.abs_time.abs_value_us
109 % GNUNET_TIME_UNIT_SECONDS.rel_value_us));
110 if (0 !=
111 json_object_set_new (
112 j,
113 "t_s",
114 json_integer (
115 (json_int_t) (stamp.abs_time.abs_value_us
116 / GNUNET_TIME_UNIT_SECONDS.rel_value_us))))
117 {
118 GNUNET_break (0);
119 json_decref (j);
120 return NULL;
121 }
122 return j;
123}
124
125
126json_t *
127GNUNET_JSON_from_timestamp_nbo (struct GNUNET_TIME_TimestampNBO stamp)
128{
129 return GNUNET_JSON_from_timestamp (GNUNET_TIME_timestamp_ntoh (stamp));
130}
131
132
133json_t *
134GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
135{
136 json_t *j;
137
138 j = json_object ();
139 if (NULL == j)
140 {
141 GNUNET_break (0);
142 return NULL;
143 }
144 if (GNUNET_TIME_relative_is_forever (stamp))
145 {
146 if (0 !=
147 json_object_set_new (j,
148 "d_us",
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 return j;
174}
175
176
177json_t *
178GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
179{
180 void *buf;
181 size_t buf_len;
182 json_t *ret;
183
184 buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
185 &buf);
186 ret = GNUNET_JSON_from_data (buf,
187 buf_len);
188 GNUNET_free (buf);
189 return ret;
190}
191
192
193json_t *
194GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
195{
196 void *buf;
197 size_t buf_len;
198 json_t *ret;
199
200 buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
201 &buf);
202 ret = GNUNET_JSON_from_data (buf,
203 buf_len);
204 GNUNET_free (buf);
205 return ret;
206}
207
208
209/* End of json/json_generator.c */