yajl_gen.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "api/yajl_gen.h"
  17. #include "yajl_buf.h"
  18. #include "yajl_encode.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <math.h>
  23. #include <stdarg.h>
  24. typedef enum {
  25. yajl_gen_start,
  26. yajl_gen_map_start,
  27. yajl_gen_map_key,
  28. yajl_gen_map_val,
  29. yajl_gen_array_start,
  30. yajl_gen_in_array,
  31. yajl_gen_complete,
  32. yajl_gen_error
  33. } yajl_gen_state;
  34. struct yajl_gen_t
  35. {
  36. unsigned int flags;
  37. unsigned int depth;
  38. const char * indentString;
  39. yajl_gen_state state[YAJL_MAX_DEPTH];
  40. yajl_print_t print;
  41. void * ctx; /* yajl_buf */
  42. /* memory allocation routines */
  43. yajl_alloc_funcs alloc;
  44. };
  45. int
  46. yajl_gen_config(yajl_gen g, yajl_gen_option opt, ...)
  47. {
  48. int rv = 1;
  49. va_list ap;
  50. va_start(ap, opt);
  51. switch(opt) {
  52. case yajl_gen_beautify:
  53. case yajl_gen_validate_utf8:
  54. case yajl_gen_escape_solidus:
  55. case yajl_gen_disable_yandex_double_format:
  56. case yajl_gen_skip_final_newline:
  57. case yajl_gen_support_infinity:
  58. if (va_arg(ap, int)) g->flags |= opt;
  59. else g->flags &= ~opt;
  60. break;
  61. case yajl_gen_indent_string: {
  62. const char *indent = va_arg(ap, const char *);
  63. g->indentString = indent;
  64. for (; *indent; indent++) {
  65. if (*indent != '\n'
  66. && *indent != '\v'
  67. && *indent != '\f'
  68. && *indent != '\t'
  69. && *indent != '\r'
  70. && *indent != ' ')
  71. {
  72. g->indentString = NULL;
  73. rv = 0;
  74. }
  75. }
  76. break;
  77. }
  78. case yajl_gen_print_callback:
  79. yajl_buf_free(g->ctx);
  80. g->print = va_arg(ap, const yajl_print_t);
  81. g->ctx = va_arg(ap, void *);
  82. break;
  83. default:
  84. rv = 0;
  85. }
  86. va_end(ap);
  87. return rv;
  88. }
  89. yajl_gen
  90. yajl_gen_alloc(const yajl_alloc_funcs * afs)
  91. {
  92. yajl_gen g = NULL;
  93. yajl_alloc_funcs afsBuffer;
  94. /* first order of business is to set up memory allocation routines */
  95. if (afs != NULL) {
  96. if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
  97. {
  98. return NULL;
  99. }
  100. } else {
  101. yajl_set_default_alloc_funcs(&afsBuffer);
  102. afs = &afsBuffer;
  103. }
  104. g = (yajl_gen) YA_MALLOC(afs, sizeof(struct yajl_gen_t));
  105. if (!g) return NULL;
  106. memset((void *) g, 0, sizeof(struct yajl_gen_t));
  107. /* copy in pointers to allocation routines */
  108. memcpy((void *) &(g->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
  109. g->print = (yajl_print_t)&yajl_buf_append;
  110. g->ctx = yajl_buf_alloc(&(g->alloc));
  111. g->indentString = " ";
  112. return g;
  113. }
  114. void
  115. yajl_gen_reset(yajl_gen g, const char * sep)
  116. {
  117. g->depth = 0;
  118. memset((void *) &(g->state), 0, sizeof(g->state));
  119. if (sep != NULL) g->print(g->ctx, sep, strlen(sep));
  120. }
  121. void
  122. yajl_gen_free(yajl_gen g)
  123. {
  124. if (g->print == (yajl_print_t)&yajl_buf_append) yajl_buf_free((yajl_buf)g->ctx);
  125. YA_FREE(&(g->alloc), g);
  126. }
  127. #define INSERT_SEP \
  128. if (g->state[g->depth] == yajl_gen_map_key || \
  129. g->state[g->depth] == yajl_gen_in_array) { \
  130. g->print(g->ctx, ",", 1); \
  131. if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1); \
  132. } else if (g->state[g->depth] == yajl_gen_map_val) { \
  133. g->print(g->ctx, ":", 1); \
  134. if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, " ", 1); \
  135. }
  136. #define INSERT_WHITESPACE \
  137. if ((g->flags & yajl_gen_beautify)) { \
  138. if (g->state[g->depth] != yajl_gen_map_val) { \
  139. unsigned int _i; \
  140. for (_i=0;_i<g->depth;_i++) \
  141. g->print(g->ctx, \
  142. g->indentString, \
  143. (unsigned int)strlen(g->indentString)); \
  144. } \
  145. }
  146. #define ENSURE_NOT_KEY \
  147. if (g->state[g->depth] == yajl_gen_map_key || \
  148. g->state[g->depth] == yajl_gen_map_start) { \
  149. return yajl_gen_keys_must_be_strings; \
  150. } \
  151. /* check that we're not complete, or in error state. in a valid state
  152. * to be generating */
  153. #define ENSURE_VALID_STATE \
  154. if (g->state[g->depth] == yajl_gen_error) { \
  155. return yajl_gen_in_error_state;\
  156. } else if (g->state[g->depth] == yajl_gen_complete) { \
  157. return yajl_gen_generation_complete; \
  158. }
  159. #define INCREMENT_DEPTH \
  160. if (++(g->depth) >= YAJL_MAX_DEPTH) return yajl_max_depth_exceeded;
  161. #define DECREMENT_DEPTH \
  162. if (--(g->depth) >= YAJL_MAX_DEPTH) return yajl_gen_generation_complete;
  163. #define APPENDED_ATOM \
  164. switch (g->state[g->depth]) { \
  165. case yajl_gen_start: \
  166. g->state[g->depth] = yajl_gen_complete; \
  167. break; \
  168. case yajl_gen_map_start: \
  169. case yajl_gen_map_key: \
  170. g->state[g->depth] = yajl_gen_map_val; \
  171. break; \
  172. case yajl_gen_array_start: \
  173. g->state[g->depth] = yajl_gen_in_array; \
  174. break; \
  175. case yajl_gen_map_val: \
  176. g->state[g->depth] = yajl_gen_map_key; \
  177. break; \
  178. default: \
  179. break; \
  180. } \
  181. #define FINAL_NEWLINE \
  182. if ((g->flags & yajl_gen_beautify) && \
  183. !(g->flags & yajl_gen_skip_final_newline) && \
  184. g->state[g->depth] == yajl_gen_complete) \
  185. { \
  186. g->print(g->ctx, "\n", 1); \
  187. } \
  188. yajl_gen_status
  189. yajl_gen_integer(yajl_gen g, long long int number)
  190. {
  191. char i[32];
  192. ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
  193. sprintf(i, "%lld", number);
  194. g->print(g->ctx, i, (unsigned int)strlen(i));
  195. APPENDED_ATOM;
  196. FINAL_NEWLINE;
  197. return yajl_gen_status_ok;
  198. }
  199. yajl_gen_status
  200. yajl_gen_uinteger(yajl_gen g, long long unsigned number)
  201. {
  202. char i[32];
  203. ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
  204. sprintf(i, "%llu", number);
  205. g->print(g->ctx, i, (unsigned int)strlen(i));
  206. APPENDED_ATOM;
  207. FINAL_NEWLINE;
  208. return yajl_gen_status_ok;
  209. }
  210. #if defined(_WIN32) || defined(WIN32)
  211. #include <float.h>
  212. #define isnan _isnan
  213. #define isinf !_finite
  214. #endif
  215. extern void FormatDoubleYandex(char* buf, size_t len, double val);
  216. yajl_gen_status
  217. yajl_gen_double(yajl_gen g, double number)
  218. {
  219. char i[32];
  220. ENSURE_VALID_STATE; ENSURE_NOT_KEY;
  221. if (isnan(number) ||
  222. (isinf(number) && !(g->flags & yajl_gen_support_infinity)))
  223. return yajl_gen_invalid_number;
  224. INSERT_SEP; INSERT_WHITESPACE;
  225. if (g->flags & yajl_gen_disable_yandex_double_format) {
  226. sprintf(i, "%.20g", number);
  227. if (strspn(i, "0123456789-") == strlen(i)) {
  228. strcat(i, ".0");
  229. }
  230. } else {
  231. FormatDoubleYandex(i, sizeof(i) - 1, number);
  232. }
  233. g->print(g->ctx, i, (unsigned int)strlen(i));
  234. APPENDED_ATOM;
  235. FINAL_NEWLINE;
  236. return yajl_gen_status_ok;
  237. }
  238. yajl_gen_status
  239. yajl_gen_number(yajl_gen g, const char * s, size_t l)
  240. {
  241. ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
  242. g->print(g->ctx, s, l);
  243. APPENDED_ATOM;
  244. FINAL_NEWLINE;
  245. return yajl_gen_status_ok;
  246. }
  247. yajl_gen_status
  248. yajl_gen_string(yajl_gen g, const unsigned char * str,
  249. size_t len)
  250. {
  251. // if validation is enabled, check that the string is valid utf8
  252. // XXX: This checking could be done a little faster, in the same pass as
  253. // the string encoding
  254. if (g->flags & yajl_gen_validate_utf8) {
  255. if (!yajl_string_validate_utf8(str, len)) {
  256. return yajl_gen_invalid_string;
  257. }
  258. }
  259. ENSURE_VALID_STATE; INSERT_SEP; INSERT_WHITESPACE;
  260. g->print(g->ctx, "\"", 1);
  261. yajl_string_encode(g->print, g->ctx, str, len, g->flags & yajl_gen_escape_solidus);
  262. g->print(g->ctx, "\"", 1);
  263. APPENDED_ATOM;
  264. FINAL_NEWLINE;
  265. return yajl_gen_status_ok;
  266. }
  267. yajl_gen_status
  268. yajl_gen_null(yajl_gen g)
  269. {
  270. ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
  271. g->print(g->ctx, "null", strlen("null"));
  272. APPENDED_ATOM;
  273. FINAL_NEWLINE;
  274. return yajl_gen_status_ok;
  275. }
  276. yajl_gen_status
  277. yajl_gen_bool(yajl_gen g, int boolean)
  278. {
  279. const char * val = boolean ? "true" : "false";
  280. ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
  281. g->print(g->ctx, val, (unsigned int)strlen(val));
  282. APPENDED_ATOM;
  283. FINAL_NEWLINE;
  284. return yajl_gen_status_ok;
  285. }
  286. yajl_gen_status
  287. yajl_gen_map_open(yajl_gen g)
  288. {
  289. ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
  290. INCREMENT_DEPTH;
  291. g->state[g->depth] = yajl_gen_map_start;
  292. g->print(g->ctx, "{", 1);
  293. if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
  294. FINAL_NEWLINE;
  295. return yajl_gen_status_ok;
  296. }
  297. yajl_gen_status
  298. yajl_gen_map_close(yajl_gen g)
  299. {
  300. ENSURE_VALID_STATE;
  301. DECREMENT_DEPTH;
  302. if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
  303. APPENDED_ATOM;
  304. INSERT_WHITESPACE;
  305. g->print(g->ctx, "}", 1);
  306. FINAL_NEWLINE;
  307. return yajl_gen_status_ok;
  308. }
  309. yajl_gen_status
  310. yajl_gen_array_open(yajl_gen g)
  311. {
  312. ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
  313. INCREMENT_DEPTH;
  314. g->state[g->depth] = yajl_gen_array_start;
  315. g->print(g->ctx, "[", 1);
  316. if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
  317. FINAL_NEWLINE;
  318. return yajl_gen_status_ok;
  319. }
  320. yajl_gen_status
  321. yajl_gen_array_close(yajl_gen g)
  322. {
  323. ENSURE_VALID_STATE;
  324. DECREMENT_DEPTH;
  325. if ((g->flags & yajl_gen_beautify)) g->print(g->ctx, "\n", 1);
  326. APPENDED_ATOM;
  327. INSERT_WHITESPACE;
  328. g->print(g->ctx, "]", 1);
  329. FINAL_NEWLINE;
  330. return yajl_gen_status_ok;
  331. }
  332. yajl_gen_status
  333. yajl_gen_get_buf(yajl_gen g, const unsigned char ** buf,
  334. size_t * len)
  335. {
  336. if (g->print != (yajl_print_t)&yajl_buf_append) return yajl_gen_no_buf;
  337. *buf = yajl_buf_data((yajl_buf)g->ctx);
  338. *len = yajl_buf_len((yajl_buf)g->ctx);
  339. return yajl_gen_status_ok;
  340. }
  341. void
  342. yajl_gen_clear(yajl_gen g)
  343. {
  344. if (g->print == (yajl_print_t)&yajl_buf_append) yajl_buf_clear((yajl_buf)g->ctx);
  345. }