json_generator.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. return json;
  42. }
  43. /**
  44. * Convert absolute timestamp to a json string.
  45. *
  46. * @param stamp the time stamp
  47. * @return a json string with the timestamp in @a stamp
  48. */
  49. json_t *
  50. GNUNET_JSON_from_time_abs (struct GNUNET_TIME_Absolute stamp)
  51. {
  52. json_t *j;
  53. GNUNET_assert (GNUNET_OK ==
  54. GNUNET_TIME_round_abs (&stamp));
  55. j = json_object ();
  56. if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
  57. {
  58. json_object_set_new (j,
  59. "t_ms",
  60. json_string ("never"));
  61. return j;
  62. }
  63. json_object_set_new (j,
  64. "t_ms",
  65. json_integer ((json_int_t) (stamp.abs_value_us / 1000LL)));
  66. return j;
  67. }
  68. /**
  69. * Convert absolute timestamp to a json string.
  70. *
  71. * @param stamp the time stamp
  72. * @return a json string with the timestamp in @a stamp
  73. */
  74. json_t *
  75. GNUNET_JSON_from_time_abs_nbo (struct GNUNET_TIME_AbsoluteNBO stamp)
  76. {
  77. return GNUNET_JSON_from_time_abs (GNUNET_TIME_absolute_ntoh (stamp));
  78. }
  79. /**
  80. * Convert relative 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_rel (struct GNUNET_TIME_Relative stamp)
  87. {
  88. json_t *j;
  89. GNUNET_assert (GNUNET_OK ==
  90. GNUNET_TIME_round_rel (&stamp));
  91. j = json_object ();
  92. if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
  93. {
  94. json_object_set_new (j,
  95. "d_ms",
  96. json_string ("forever"));
  97. return j;
  98. }
  99. json_object_set_new (j,
  100. "d_ms",
  101. json_integer ((json_int_t) (stamp.rel_value_us / 1000LL)));
  102. return j;
  103. }
  104. /**
  105. * Convert RSA public key to JSON.
  106. *
  107. * @param pk public key to convert
  108. * @return corresponding JSON encoding
  109. */
  110. json_t *
  111. GNUNET_JSON_from_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *pk)
  112. {
  113. char *buf;
  114. size_t buf_len;
  115. json_t *ret;
  116. buf_len = GNUNET_CRYPTO_rsa_public_key_encode (pk,
  117. &buf);
  118. ret = GNUNET_JSON_from_data (buf,
  119. buf_len);
  120. GNUNET_free (buf);
  121. return ret;
  122. }
  123. /**
  124. * Convert RSA signature to JSON.
  125. *
  126. * @param sig signature to convert
  127. * @return corresponding JSON encoding
  128. */
  129. json_t *
  130. GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig)
  131. {
  132. char *buf;
  133. size_t buf_len;
  134. json_t *ret;
  135. buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
  136. &buf);
  137. ret = GNUNET_JSON_from_data (buf,
  138. buf_len);
  139. GNUNET_free (buf);
  140. return ret;
  141. }
  142. /**
  143. * Convert GNS record to JSON.
  144. *
  145. * @param rname name of record
  146. * @param rd record data
  147. * @return corresponding JSON encoding
  148. */
  149. json_t *
  150. GNUNET_JSON_from_gnsrecord (const char*rname,
  151. const struct GNUNET_GNSRECORD_Data *rd,
  152. unsigned int rd_count)
  153. {
  154. struct GNUNET_TIME_Absolute expiration_time;
  155. const char *expiration_time_str;
  156. const char *record_type_str;
  157. char *value_str;
  158. json_t *data;
  159. json_t *record;
  160. json_t *records;
  161. data = json_object ();
  162. json_object_set_new (data,
  163. "record_name",
  164. json_string (rname));
  165. records = json_array ();
  166. for (int i = 0; i < rd_count; i++)
  167. {
  168. value_str = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
  169. rd[i].data,
  170. rd[i].data_size);
  171. expiration_time = GNUNET_GNSRECORD_record_get_expiration_time (1, &rd[i]);
  172. expiration_time_str = GNUNET_STRINGS_absolute_time_to_string (
  173. expiration_time);
  174. record_type_str = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
  175. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  176. "Packing %s %s %s %d\n",
  177. value_str, record_type_str, expiration_time_str, rd[i].flags);
  178. record = json_pack ("{s:s,s:s,s:s,s:i}",
  179. "value",
  180. value_str,
  181. "record_type",
  182. record_type_str,
  183. "expiration_time",
  184. expiration_time_str,
  185. "flag",
  186. rd[i].flags);
  187. GNUNET_assert (NULL != record);
  188. GNUNET_free (value_str);
  189. json_array_append_new (records, record);
  190. }
  191. json_object_set_new (data, "data", records);
  192. return data;
  193. }
  194. /* End of json/json_generator.c */