Merge branch 'gsoc2018/rest_api'
[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 /**
19  * @file json/json_generator.c
20  * @brief helper functions for generating JSON from GNUnet data structures
21  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_json_lib.h"
26
27
28 /**
29  * Convert binary data to a JSON string
30  * with the base32crockford encoding.
31  *
32  * @param data binary data
33  * @param size size of @a data in bytes
34  * @return json string that encodes @a data
35  */
36 json_t *
37 GNUNET_JSON_from_data (const void *data,
38                        size_t size)
39 {
40   char *buf;
41   json_t *json;
42
43   buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
44   json = json_string (buf);
45   GNUNET_free (buf);
46   return json;
47 }
48
49
50 /**
51  * Convert absolute timestamp to a json string.
52  *
53  * @param stamp the time stamp
54  * @return a json string with the timestamp in @a stamp
55  */
56 json_t *
57 GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
58 {
59   json_t *j;
60   char *mystr;
61   int ret;
62
63   GNUNET_assert (GNUNET_OK ==
64                  GNUNET_TIME_round_abs (&stamp));
65   if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
66     return json_string ("/never/");
67   ret = GNUNET_asprintf (&mystr,
68                          "/Date(%llu)/",
69                          (unsigned long long) (stamp.abs_value_us / (1000LL * 1000LL)));
70   GNUNET_assert (ret > 0);
71   j = json_string (mystr);
72   GNUNET_free (mystr);
73   return j;
74 }
75
76
77 /**
78  * Convert absolute timestamp to a json string.
79  *
80  * @param stamp the time stamp
81  * @return a json string with the timestamp in @a stamp
82  */
83 json_t *
84 GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
85 {
86   return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp));
87 }
88
89
90 /**
91  * Convert relative timestamp to a json string.
92  *
93  * @param stamp the time stamp
94  * @return a json string with the timestamp in @a stamp
95  */
96 json_t *
97 GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
98 {
99   json_t *j;
100   char *mystr;
101   int ret;
102
103   GNUNET_assert (GNUNET_OK ==
104                  GNUNET_TIME_round_rel (&stamp));
105   if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
106     return json_string ("/forever/");
107   ret = GNUNET_asprintf (&mystr,
108                          "/Delay(%llu)/",
109                          (unsigned long long) (stamp.rel_value_us / (1000LL * 1000LL)));
110   GNUNET_assert (ret > 0);
111   j = json_string (mystr);
112   GNUNET_free (mystr);
113   return j;
114 }
115
116
117 /**
118  * Convert RSA public key to JSON.
119  *
120  * @param pk public key to convert
121  * @return corresponding JSON encoding
122  */
123 json_t *
124 GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
125 {
126   char *buf;
127   size_t buf_len;
128   json_t *ret;
129
130   buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
131                                                  &buf);
132   ret = GNUNET_JSON_from_data (buf,
133                                buf_len);
134   GNUNET_free (buf);
135   return ret;
136 }
137
138
139 /**
140  * Convert RSA signature to JSON.
141  *
142  * @param sig signature to convert
143  * @return corresponding JSON encoding
144  */
145 json_t *
146 GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
147 {
148   char *buf;
149   size_t buf_len;
150   json_t *ret;
151
152   buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
153                                                 &buf);
154   ret = GNUNET_JSON_from_data (buf,
155                                buf_len);
156   GNUNET_free (buf);
157   return ret;
158 }
159
160 /**
161  * Convert Gns record to JSON.
162  *
163  * @param rname name of record
164  * @param rd record data
165  * @return corresponding JSON encoding
166  */
167 json_t *
168 GNUNET_JSON_from_gns_record (const char* rname,
169                              const struct GNUNET_GNSRECORD_Data *rd)
170 {
171   struct GNUNET_TIME_Absolute expiration_time;
172   const char *expiration_time_str;
173   const char *record_type_str;
174   char *value_str;
175   json_t *ret;
176   int flags;
177
178   value_str = GNUNET_GNSRECORD_value_to_string(rd->record_type,rd->data,rd->data_size);
179   expiration_time = GNUNET_GNSRECORD_record_get_expiration_time(1, rd);
180   expiration_time_str = GNUNET_STRINGS_absolute_time_to_string(expiration_time);
181   flags = (int)rd->flags; //maybe necessary
182   record_type_str = GNUNET_GNSRECORD_number_to_typename(rd->record_type);
183
184   // ? for possible NULL values
185   ret = json_pack("{s:s?,s:s?,s:s?,s:i,s:s?}",
186                   "value", value_str,
187                   "type", record_type_str,
188                   "expiration_time", expiration_time_str,
189                   "flag", flags,
190                   "label", rname);
191   GNUNET_free_non_null(value_str);
192   return ret;
193 }
194
195
196 /* End of json/json_generator.c */