glitch in the license text detected by hyazinthe, thank you!
[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 /**
16  * @file json/json_generator.c
17  * @brief helper functions for generating JSON from GNUnet data structures
18  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
19  */
20 #include "platform.h"
21 #include "gnunet_util_lib.h"
22 #include "gnunet_json_lib.h"
23
24
25 /**
26  * Convert binary data to a JSON string
27  * with the base32crockford encoding.
28  *
29  * @param data binary data
30  * @param size size of @a data in bytes
31  * @return json string that encodes @a data
32  */
33 json_t *
34 GNUNET_JSON_from_data (const void *data,
35                        size_t size)
36 {
37   char *buf;
38   json_t *json;
39
40   buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
41   json = json_string (buf);
42   GNUNET_free (buf);
43   return json;
44 }
45
46
47 /**
48  * Convert absolute timestamp to a json string.
49  *
50  * @param stamp the time stamp
51  * @return a json string with the timestamp in @a stamp
52  */
53 json_t *
54 GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
55 {
56   json_t *j;
57   char *mystr;
58   int ret;
59
60   GNUNET_assert (GNUNET_OK ==
61                  GNUNET_TIME_round_abs (&stamp));
62   if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
63     return json_string ("/never/");
64   ret = GNUNET_asprintf (&mystr,
65                          "/Date(%llu)/",
66                          (unsigned long long) (stamp.abs_value_us / (1000LL * 1000LL)));
67   GNUNET_assert (ret > 0);
68   j = json_string (mystr);
69   GNUNET_free (mystr);
70   return j;
71 }
72
73
74 /**
75  * Convert absolute timestamp to a json string.
76  *
77  * @param stamp the time stamp
78  * @return a json string with the timestamp in @a stamp
79  */
80 json_t *
81 GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
82 {
83   return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp));
84 }
85
86
87 /**
88  * Convert relative timestamp to a json string.
89  *
90  * @param stamp the time stamp
91  * @return a json string with the timestamp in @a stamp
92  */
93 json_t *
94 GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
95 {
96   json_t *j;
97   char *mystr;
98   int ret;
99
100   GNUNET_assert (GNUNET_OK ==
101                  GNUNET_TIME_round_rel (&stamp));
102   if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
103     return json_string ("/forever/");
104   ret = GNUNET_asprintf (&mystr,
105                          "/Delay(%llu)/",
106                          (unsigned long long) (stamp.rel_value_us / (1000LL * 1000LL)));
107   GNUNET_assert (ret > 0);
108   j = json_string (mystr);
109   GNUNET_free (mystr);
110   return j;
111 }
112
113
114 /**
115  * Convert RSA public key to JSON.
116  *
117  * @param pk public key to convert
118  * @return corresponding JSON encoding
119  */
120 json_t *
121 GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
122 {
123   char *buf;
124   size_t buf_len;
125   json_t *ret;
126
127   buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
128                                                  &buf);
129   ret = GNUNET_JSON_from_data (buf,
130                                buf_len);
131   GNUNET_free (buf);
132   return ret;
133 }
134
135
136 /**
137  * Convert RSA signature to JSON.
138  *
139  * @param sig signature to convert
140  * @return corresponding JSON encoding
141  */
142 json_t *
143 GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
144 {
145   char *buf;
146   size_t buf_len;
147   json_t *ret;
148
149   buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
150                                                 &buf);
151   ret = GNUNET_JSON_from_data (buf,
152                                buf_len);
153   GNUNET_free (buf);
154   return ret;
155 }
156
157
158 /* End of json/json_generator.c */