json_generator.c 5.9 KB

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