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.c179
1 files changed, 0 insertions, 179 deletions
diff --git a/src/json/json_generator.c b/src/json/json_generator.c
deleted file mode 100644
index eb275712c..000000000
--- a/src/json/json_generator.c
+++ /dev/null
@@ -1,179 +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 return j;
75 }
76 GNUNET_assert (
77 0 ==
78 (stamp.abs_time.abs_value_us
79 % GNUNET_TIME_UNIT_SECONDS.rel_value_us));
80 if (0 !=
81 json_object_set_new (
82 j,
83 "t_s",
84 json_integer (
85 (json_int_t) (stamp.abs_time.abs_value_us
86 / GNUNET_TIME_UNIT_SECONDS.rel_value_us))))
87 {
88 GNUNET_break (0);
89 json_decref (j);
90 return NULL;
91 }
92 return j;
93}
94
95
96json_t *
97GNUNET_JSON_from_timestamp_nbo (struct GNUNET_TIME_TimestampNBO stamp)
98{
99 return GNUNET_JSON_from_timestamp (GNUNET_TIME_timestamp_ntoh (stamp));
100}
101
102
103json_t *
104GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
105{
106 json_t *j;
107
108 j = json_object ();
109 if (NULL == j)
110 {
111 GNUNET_break (0);
112 return NULL;
113 }
114 if (GNUNET_TIME_relative_is_forever (stamp))
115 {
116 if (0 !=
117 json_object_set_new (j,
118 "d_us",
119 json_string ("forever")))
120 {
121 GNUNET_break (0);
122 json_decref (j);
123 return NULL;
124 }
125 return j;
126 }
127 if (stamp.rel_value_us >= (1LLU << 53))
128 {
129 /* value is larger than allowed */
130 GNUNET_break (0);
131 return NULL;
132 }
133 if (0 !=
134 json_object_set_new (
135 j,
136 "d_us",
137 json_integer ((json_int_t) stamp.rel_value_us)))
138 {
139 GNUNET_break (0);
140 json_decref (j);
141 return NULL;
142 }
143 return j;
144}
145
146
147json_t *
148GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
149{
150 void *buf;
151 size_t buf_len;
152 json_t *ret;
153
154 buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
155 &buf);
156 ret = GNUNET_JSON_from_data (buf,
157 buf_len);
158 GNUNET_free (buf);
159 return ret;
160}
161
162
163json_t *
164GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
165{
166 void *buf;
167 size_t buf_len;
168 json_t *ret;
169
170 buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
171 &buf);
172 ret = GNUNET_JSON_from_data (buf,
173 buf_len);
174 GNUNET_free (buf);
175 return ret;
176}
177
178
179/* End of json/json_generator.c */