yajl.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_parse.h"
  17. #include "yajl_lex.h"
  18. #include "yajl_parser.h"
  19. #include "yajl_alloc.h"
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <stdarg.h>
  23. #include <assert.h>
  24. const char *
  25. yajl_status_to_string(yajl_status stat)
  26. {
  27. const char * statStr = "unknown";
  28. switch (stat) {
  29. case yajl_status_ok:
  30. statStr = "ok, no error";
  31. break;
  32. case yajl_status_client_canceled:
  33. statStr = "client canceled parse";
  34. break;
  35. case yajl_status_error:
  36. statStr = "parse error";
  37. break;
  38. }
  39. return statStr;
  40. }
  41. yajl_handle
  42. yajl_alloc(const yajl_callbacks * callbacks,
  43. yajl_alloc_funcs * afs,
  44. void * ctx)
  45. {
  46. yajl_handle hand = NULL;
  47. yajl_alloc_funcs afsBuffer;
  48. /* first order of business is to set up memory allocation routines */
  49. if (afs != NULL) {
  50. if (afs->malloc == NULL || afs->realloc == NULL || afs->free == NULL)
  51. {
  52. return NULL;
  53. }
  54. } else {
  55. yajl_set_default_alloc_funcs(&afsBuffer);
  56. afs = &afsBuffer;
  57. }
  58. hand = (yajl_handle) YA_MALLOC(afs, sizeof(struct yajl_handle_t));
  59. if (hand == NULL) {
  60. return NULL;
  61. }
  62. /* copy in pointers to allocation routines */
  63. memcpy((void *) &(hand->alloc), (void *) afs, sizeof(yajl_alloc_funcs));
  64. hand->callbacks = callbacks;
  65. hand->ctx = ctx;
  66. hand->lexer = NULL;
  67. hand->bytesConsumed = 0;
  68. hand->decodeBuf = yajl_buf_alloc(&(hand->alloc));
  69. hand->flags = 0;
  70. hand->memoryLimit = 0;
  71. yajl_bs_init(hand->stateStack, &(hand->alloc));
  72. yajl_bs_push(hand->stateStack, yajl_state_start);
  73. return hand;
  74. }
  75. int
  76. yajl_config(yajl_handle h, yajl_option opt, ...)
  77. {
  78. int rv = 1;
  79. va_list ap;
  80. va_start(ap, opt);
  81. switch(opt) {
  82. case yajl_allow_comments:
  83. case yajl_dont_validate_strings:
  84. case yajl_allow_trailing_garbage:
  85. case yajl_allow_multiple_values:
  86. case yajl_allow_partial_values:
  87. if (va_arg(ap, int)) h->flags |= opt;
  88. else h->flags &= ~opt;
  89. break;
  90. default:
  91. rv = 0;
  92. }
  93. va_end(ap);
  94. return rv;
  95. }
  96. /** set limit for total size of internal buffers */
  97. void yajl_set_memory_limit(yajl_handle h, unsigned long limit)
  98. {
  99. h->memoryLimit = limit;
  100. }
  101. void
  102. yajl_free(yajl_handle handle)
  103. {
  104. yajl_bs_free(handle->stateStack);
  105. yajl_buf_free(handle->decodeBuf);
  106. if (handle->lexer) {
  107. yajl_lex_free(handle->lexer);
  108. handle->lexer = NULL;
  109. }
  110. YA_FREE(&(handle->alloc), handle);
  111. }
  112. yajl_status
  113. yajl_parse(yajl_handle hand, const unsigned char * jsonText,
  114. size_t jsonTextLen)
  115. {
  116. yajl_status status;
  117. /* lazy allocation of the lexer */
  118. if (hand->lexer == NULL) {
  119. hand->lexer = yajl_lex_alloc(&(hand->alloc),
  120. hand->flags & yajl_allow_comments,
  121. !(hand->flags & yajl_dont_validate_strings));
  122. }
  123. status = yajl_do_parse(hand, jsonText, jsonTextLen);
  124. if (status == yajl_status_ok &&
  125. hand->memoryLimit != 0 &&
  126. yajl_buf_capacity(hand->decodeBuf) + yajl_lex_buf_capacity(hand->lexer) > hand->memoryLimit)
  127. {
  128. hand->parseError = "Out of memory (this is typically caused by an inefficient representation of strings in JSON)";
  129. status = yajl_status_error;
  130. yajl_bs_push(hand->stateStack, status);
  131. }
  132. return status;
  133. }
  134. yajl_status
  135. yajl_complete_parse(yajl_handle hand)
  136. {
  137. /* The lexer is lazy allocated in the first call to parse. if parse is
  138. * never called, then no data was provided to parse at all. This is a
  139. * "premature EOF" error unless yajl_allow_partial_values is specified.
  140. * allocating the lexer now is the simplest possible way to handle this
  141. * case while preserving all the other semantics of the parser
  142. * (multiple values, partial values, etc). */
  143. if (hand->lexer == NULL) {
  144. hand->lexer = yajl_lex_alloc(&(hand->alloc),
  145. hand->flags & yajl_allow_comments,
  146. !(hand->flags & yajl_dont_validate_strings));
  147. }
  148. return yajl_do_finish(hand);
  149. }
  150. unsigned char *
  151. yajl_get_error(yajl_handle hand, int verbose,
  152. const unsigned char * jsonText, size_t jsonTextLen)
  153. {
  154. return yajl_render_error_string(hand, jsonText, jsonTextLen, verbose);
  155. }
  156. size_t
  157. yajl_get_bytes_consumed(yajl_handle hand)
  158. {
  159. if (!hand) return 0;
  160. else return hand->bytesConsumed;
  161. }
  162. void
  163. yajl_free_error(yajl_handle hand, unsigned char * str)
  164. {
  165. /* use memory allocation functions if set */
  166. YA_FREE(&(hand->alloc), str);
  167. }
  168. /* XXX: add utility routines to parse from file */