reduce loop counters to more practical levels
[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 under the
6   terms of the GNU General Public License as published by the Free Software
7   Foundation; either version 3, or (at your option) any later version.
8
9   GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
12
13   You should have received a copy of the GNU General Public License along with
14   GNUnet; see the file COPYING.  If not, If not, see <http://www.gnu.org/licenses/>
15 */
16 /**
17  * @file json/json_generator.c
18  * @brief helper functions for generating JSON from GNUnet data structures
19  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20  */
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23 #include "gnunet_json_lib.h"
24
25
26 /**
27  * Convert binary data to a JSON string
28  * with the base32crockford encoding.
29  *
30  * @param data binary data
31  * @param size size of @a data in bytes
32  * @return json string that encodes @a data
33  */
34 json_t *
35 GNUNET_JSON_from_data (const void *data,
36                        size_t size)
37 {
38   char *buf;
39   json_t *json;
40
41   buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
42   json = json_string (buf);
43   GNUNET_free (buf);
44   return json;
45 }
46
47
48 /**
49  * Convert absolute timestamp to a json string.
50  *
51  * @param stamp the time stamp
52  * @return a json string with the timestamp in @a stamp
53  */
54 json_t *
55 GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
56 {
57   json_t *j;
58   char *mystr;
59   int ret;
60
61   GNUNET_assert (GNUNET_OK ==
62                  GNUNET_TIME_round_abs (&stamp));
63   if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
64     return json_string ("/never/");
65   ret = GNUNET_asprintf (&mystr,
66                          "/Date(%llu)/",
67                          (unsigned long long) (stamp.abs_value_us / (1000LL * 1000LL)));
68   GNUNET_assert (ret > 0);
69   j = json_string (mystr);
70   GNUNET_free (mystr);
71   return j;
72 }
73
74
75 /**
76  * Convert absolute timestamp to a json string.
77  *
78  * @param stamp the time stamp
79  * @return a json string with the timestamp in @a stamp
80  */
81 json_t *
82 GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
83 {
84   return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp));
85 }
86
87
88 /**
89  * Convert relative timestamp to a json string.
90  *
91  * @param stamp the time stamp
92  * @return a json string with the timestamp in @a stamp
93  */
94 json_t *
95 GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
96 {
97   json_t *j;
98   char *mystr;
99   int ret;
100
101   GNUNET_assert (GNUNET_OK ==
102                  GNUNET_TIME_round_rel (&stamp));
103   if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
104     return json_string ("/forever/");
105   ret = GNUNET_asprintf (&mystr,
106                          "/Delay(%llu)/",
107                          (unsigned long long) (stamp.rel_value_us / (1000LL * 1000LL)));
108   GNUNET_assert (ret > 0);
109   j = json_string (mystr);
110   GNUNET_free (mystr);
111   return j;
112 }
113
114
115 /**
116  * Convert RSA public key to JSON.
117  *
118  * @param pk public key to convert
119  * @return corresponding JSON encoding
120  */
121 json_t *
122 GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
123 {
124   char *buf;
125   size_t buf_len;
126   json_t *ret;
127
128   buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
129                                                  &buf);
130   ret = GNUNET_JSON_from_data (buf,
131                                buf_len);
132   GNUNET_free (buf);
133   return ret;
134 }
135
136
137 /**
138  * Convert RSA signature to JSON.
139  *
140  * @param sig signature to convert
141  * @return corresponding JSON encoding
142  */
143 json_t *
144 GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
145 {
146   char *buf;
147   size_t buf_len;
148   json_t *ret;
149
150   buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
151                                                 &buf);
152   ret = GNUNET_JSON_from_data (buf,
153                                buf_len);
154   GNUNET_free (buf);
155   return ret;
156 }
157
158
159 /* End of json/json_generator.c */