Merge branch 'license/spdx'
[oweals/gnunet.git] / src / json / json_generator.c
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   buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
46   json = json_string (buf);
47   GNUNET_free (buf);
48   return json;
49 }
50
51
52 /**
53  * Convert absolute timestamp to a json string.
54  *
55  * @param stamp the time stamp
56  * @return a json string with the timestamp in @a stamp
57  */
58 json_t *
59 GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
60 {
61   json_t *j;
62   char *mystr;
63   int ret;
64
65   GNUNET_assert (GNUNET_OK ==
66                  GNUNET_TIME_round_abs (&stamp));
67   if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
68     return json_string ("/never/");
69   ret = GNUNET_asprintf (&mystr,
70                          "/Date(%llu)/",
71                          (unsigned long long) (stamp.abs_value_us / (1000LL * 1000LL)));
72   GNUNET_assert (ret > 0);
73   j = json_string (mystr);
74   GNUNET_free (mystr);
75   return j;
76 }
77
78
79 /**
80  * Convert absolute timestamp to a json string.
81  *
82  * @param stamp the time stamp
83  * @return a json string with the timestamp in @a stamp
84  */
85 json_t *
86 GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
87 {
88   return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp));
89 }
90
91
92 /**
93  * Convert relative timestamp to a json string.
94  *
95  * @param stamp the time stamp
96  * @return a json string with the timestamp in @a stamp
97  */
98 json_t *
99 GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
100 {
101   json_t *j;
102   char *mystr;
103   int ret;
104
105   GNUNET_assert (GNUNET_OK ==
106                  GNUNET_TIME_round_rel (&stamp));
107   if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
108     return json_string ("/forever/");
109   ret = GNUNET_asprintf (&mystr,
110                          "/Delay(%llu)/",
111                          (unsigned long long) (stamp.rel_value_us / (1000LL * 1000LL)));
112   GNUNET_assert (ret > 0);
113   j = json_string (mystr);
114   GNUNET_free (mystr);
115   return j;
116 }
117
118
119 /**
120  * Convert RSA public key to JSON.
121  *
122  * @param pk public key to convert
123  * @return corresponding JSON encoding
124  */
125 json_t *
126 GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
127 {
128   char *buf;
129   size_t buf_len;
130   json_t *ret;
131
132   buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
133                                                  &buf);
134   ret = GNUNET_JSON_from_data (buf,
135                                buf_len);
136   GNUNET_free (buf);
137   return ret;
138 }
139
140
141 /**
142  * Convert RSA signature to JSON.
143  *
144  * @param sig signature to convert
145  * @return corresponding JSON encoding
146  */
147 json_t *
148 GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
149 {
150   char *buf;
151   size_t buf_len;
152   json_t *ret;
153
154   buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
155                                                 &buf);
156   ret = GNUNET_JSON_from_data (buf,
157                                buf_len);
158   GNUNET_free (buf);
159   return ret;
160 }
161
162 /**
163  * Convert Gns record to JSON.
164  *
165  * @param rname name of record
166  * @param rd record data
167  * @return corresponding JSON encoding
168  */
169 json_t *
170 GNUNET_JSON_from_gns_record (const char* rname,
171                              const struct GNUNET_GNSRECORD_Data *rd)
172 {
173   struct GNUNET_TIME_Absolute expiration_time;
174   const char *expiration_time_str;
175   const char *record_type_str;
176   char *value_str;
177   json_t *ret;
178   int flags;
179
180   value_str = GNUNET_GNSRECORD_value_to_string(rd->record_type,rd->data,rd->data_size);
181   expiration_time = GNUNET_GNSRECORD_record_get_expiration_time(1, rd);
182   expiration_time_str = GNUNET_STRINGS_absolute_time_to_string(expiration_time);
183   flags = (int)rd->flags; //maybe necessary
184   record_type_str = GNUNET_GNSRECORD_number_to_typename(rd->record_type);
185
186   // ? for possible NULL values
187   if (NULL != rname)
188   {
189     ret = json_pack ("{s:s?,s:s?,s:s?,s:i,s:s?}",
190                      "value",
191                      value_str,
192                      "record_type",
193                      record_type_str,
194                      "expiration_time",
195                      expiration_time_str,
196                      "flag",
197                      flags,
198                      "record_name",
199                      rname);
200   }
201   else
202   {
203     ret = json_pack ("{s:s?,s:s?,s:s?,s:i}",
204                      "value",
205                      value_str,
206                      "record_type",
207                      record_type_str,
208                      "expiration_time",
209                      expiration_time_str,
210                      "flag",
211                      flags);
212   }
213   GNUNET_free_non_null(value_str);
214   return ret;
215 }
216
217
218 /* End of json/json_generator.c */