json_generator.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2014, 2015, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  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. * Convert binary data to a JSON string
  26. * with the base32crockford encoding.
  27. *
  28. * @param data binary data
  29. * @param size size of @a data in bytes
  30. * @return json string that encodes @a data
  31. */
  32. json_t *
  33. GNUNET_JSON_from_data (const void *data,
  34. size_t size)
  35. {
  36. char *buf;
  37. json_t *json;
  38. buf = GNUNET_STRINGS_data_to_string_alloc (data, size);
  39. json = json_string (buf);
  40. GNUNET_free (buf);
  41. GNUNET_break (NULL != json);
  42. return json;
  43. }
  44. /**
  45. * Convert absolute timestamp to a json string.
  46. *
  47. * @param stamp the time stamp
  48. * @return a json string with the timestamp in @a stamp
  49. */
  50. json_t *
  51. GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
  52. {
  53. json_t *j;
  54. GNUNET_assert (GNUNET_OK ==
  55. GNUNET_TIME_round_abs (&stamp));
  56. j = json_object ();
  57. if (NULL == j)
  58. {
  59. GNUNET_break (0);
  60. return NULL;
  61. }
  62. if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
  63. {
  64. if (0 !=
  65. json_object_set_new (j,
  66. "t_ms",
  67. json_string ("never")))
  68. {
  69. GNUNET_break (0);
  70. json_decref (j);
  71. return NULL;
  72. }
  73. return j;
  74. }
  75. if (0 !=
  76. json_object_set_new (j,
  77. "t_ms",
  78. json_integer ((json_int_t) (stamp.abs_value_us
  79. / 1000LL))))
  80. {
  81. GNUNET_break (0);
  82. json_decref (j);
  83. return NULL;
  84. }
  85. return j;
  86. }
  87. /**
  88. * Convert absolute 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_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
  95. {
  96. return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp));
  97. }
  98. /**
  99. * Convert relative timestamp to a json string.
  100. *
  101. * @param stamp the time stamp
  102. * @return a json string with the timestamp in @a stamp
  103. */
  104. json_t *
  105. GNUNET_JSON_from_time_rel (struct GNUNET_TIME_Relative stamp)
  106. {
  107. json_t *j;
  108. GNUNET_assert (GNUNET_OK ==
  109. GNUNET_TIME_round_rel (&stamp));
  110. j = json_object ();
  111. if (NULL == j)
  112. {
  113. GNUNET_break (0);
  114. return NULL;
  115. }
  116. if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
  117. {
  118. if (0 !=
  119. json_object_set_new (j,
  120. "d_ms",
  121. json_string ("forever")))
  122. {
  123. GNUNET_break (0);
  124. json_decref (j);
  125. return NULL;
  126. }
  127. return j;
  128. }
  129. if (0 !=
  130. json_object_set_new (j,
  131. "d_ms",
  132. json_integer ((json_int_t) (stamp.rel_value_us
  133. / 1000LL))))
  134. {
  135. GNUNET_break (0);
  136. json_decref (j);
  137. return NULL;
  138. }
  139. return j;
  140. }
  141. /**
  142. * Convert RSA public key to JSON.
  143. *
  144. * @param pk public key to convert
  145. * @return corresponding JSON encoding
  146. */
  147. json_t *
  148. GNUNET_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. buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
  154. &buf);
  155. ret = GNUNET_JSON_from_data (buf,
  156. buf_len);
  157. GNUNET_free (buf);
  158. return ret;
  159. }
  160. /**
  161. * Convert RSA signature to JSON.
  162. *
  163. * @param sig signature to convert
  164. * @return corresponding JSON encoding
  165. */
  166. json_t *
  167. GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
  168. {
  169. void *buf;
  170. size_t buf_len;
  171. json_t *ret;
  172. buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
  173. &buf);
  174. ret = GNUNET_JSON_from_data (buf,
  175. buf_len);
  176. GNUNET_free (buf);
  177. return ret;
  178. }
  179. /**
  180. * Convert GNS record to JSON.
  181. *
  182. * @param rname name of record
  183. * @param rd record data
  184. * @return corresponding JSON encoding
  185. */
  186. json_t *
  187. GNUNET_JSON_from_gnsrecord (const char*rname,
  188. const struct GNUNET_GNSRECORD_Data *rd,
  189. unsigned int rd_count)
  190. {
  191. struct GNUNET_TIME_Absolute abs_exp;
  192. struct GNUNET_TIME_Relative rel_exp;
  193. const char *expiration_time_str;
  194. const char *record_type_str;
  195. char *value_str;
  196. json_t *data;
  197. json_t *record;
  198. json_t *records;
  199. data = json_object ();
  200. if (NULL == data)
  201. {
  202. GNUNET_break (0);
  203. return NULL;
  204. }
  205. if (0 !=
  206. json_object_set_new (data,
  207. "record_name",
  208. json_string (rname)))
  209. {
  210. GNUNET_break (0);
  211. json_decref (data);
  212. return NULL;
  213. }
  214. records = json_array ();
  215. if (NULL == records)
  216. {
  217. GNUNET_break (0);
  218. json_decref (data);
  219. return NULL;
  220. }
  221. for (int i = 0; i < rd_count; i++)
  222. {
  223. value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
  224. rd[i].data,
  225. rd[i].data_size);
  226. if (GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION & rd[i].flags)
  227. {
  228. rel_exp.rel_value_us = rd[i].expiration_time;
  229. expiration_time_str = GNUNET_STRINGS_relative_time_to_string (rel_exp,
  230. GNUNET_NO);
  231. } else {
  232. abs_exp.abs_value_us = rd[i].expiration_time;
  233. expiration_time_str = GNUNET_STRINGS_absolute_time_to_string (abs_exp);
  234. }
  235. record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
  236. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  237. "Packing %s %s %s %d\n",
  238. value_str, record_type_str, expiration_time_str, rd[i].flags);
  239. record = json_pack ("{s:s,s:s,s:s,s:b,s:b,s:b,s:b}",
  240. "value",
  241. value_str,
  242. "record_type",
  243. record_type_str,
  244. "expiration_time",
  245. expiration_time_str,
  246. "private",
  247. rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE,
  248. "relative_expiration",
  249. rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION,
  250. "supplemental",
  251. rd[i].flags & GNUNET_GNSRECORD_RF_SUPPLEMENTAL,
  252. "shadow",
  253. rd[i].flags & GNUNET_GNSRECORD_RF_SHADOW_RECORD);
  254. GNUNET_free (value_str);
  255. if (NULL == record)
  256. {
  257. GNUNET_break (0);
  258. json_decref (records);
  259. json_decref (data);
  260. return NULL;
  261. }
  262. if (0 !=
  263. json_array_append_new (records,
  264. record))
  265. {
  266. GNUNET_break (0);
  267. json_decref (records);
  268. json_decref (data);
  269. return NULL;
  270. }
  271. }
  272. if (0 !=
  273. json_object_set_new (data,
  274. "data",
  275. records))
  276. {
  277. GNUNET_break (0);
  278. json_decref (data);
  279. return NULL;
  280. }
  281. return data;
  282. }
  283. /* End of json/json_generator.c */