ffi-yajl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +201 -0
  3. data/README.md +17 -0
  4. data/Rakefile +62 -0
  5. data/bin/ffi-yajl-bench +36 -0
  6. data/ext/ffi_yajl/ext/encoder/encoder.c +240 -0
  7. data/ext/ffi_yajl/ext/encoder/extconf.rb +29 -0
  8. data/ext/ffi_yajl/ext/parser/extconf.rb +29 -0
  9. data/ext/ffi_yajl/ext/parser/parser.c +199 -0
  10. data/ext/libyajl2/extconf.rb +65 -0
  11. data/ext/libyajl2/vendored/.gitignore +3 -0
  12. data/ext/libyajl2/vendored/BUILDING +23 -0
  13. data/ext/libyajl2/vendored/BUILDING.win32 +27 -0
  14. data/ext/libyajl2/vendored/CMakeLists.txt +79 -0
  15. data/ext/libyajl2/vendored/COPYING +13 -0
  16. data/ext/libyajl2/vendored/ChangeLog +175 -0
  17. data/ext/libyajl2/vendored/README +74 -0
  18. data/ext/libyajl2/vendored/TODO +9 -0
  19. data/ext/libyajl2/vendored/YAJLDoc.cmake +26 -0
  20. data/ext/libyajl2/vendored/configure +79 -0
  21. data/ext/libyajl2/vendored/example/CMakeLists.txt +23 -0
  22. data/ext/libyajl2/vendored/example/README.md +7 -0
  23. data/ext/libyajl2/vendored/example/parse_config.c +69 -0
  24. data/ext/libyajl2/vendored/example/sample.config +101 -0
  25. data/ext/libyajl2/vendored/perf/CMakeLists.txt +23 -0
  26. data/ext/libyajl2/vendored/perf/documents.c +1418 -0
  27. data/ext/libyajl2/vendored/perf/documents.h +28 -0
  28. data/ext/libyajl2/vendored/perf/perftest.c +134 -0
  29. data/ext/libyajl2/vendored/reformatter/CMakeLists.txt +39 -0
  30. data/ext/libyajl2/vendored/reformatter/json_reformat.c +194 -0
  31. data/ext/libyajl2/vendored/src/CMakeLists.txt +86 -0
  32. data/ext/libyajl2/vendored/src/YAJL.dxy +1258 -0
  33. data/ext/libyajl2/vendored/src/api/yajl_common.h +75 -0
  34. data/ext/libyajl2/vendored/src/api/yajl_gen.h +157 -0
  35. data/ext/libyajl2/vendored/src/api/yajl_parse.h +226 -0
  36. data/ext/libyajl2/vendored/src/api/yajl_tree.h +185 -0
  37. data/ext/libyajl2/vendored/src/api/yajl_version.h.cmake +23 -0
  38. data/ext/libyajl2/vendored/src/yajl +33 -0
  39. data/ext/libyajl2/vendored/src/yajl.c +175 -0
  40. data/ext/libyajl2/vendored/src/yajl.pc.cmake +9 -0
  41. data/ext/libyajl2/vendored/src/yajl_alloc.c +52 -0
  42. data/ext/libyajl2/vendored/src/yajl_alloc.h +34 -0
  43. data/ext/libyajl2/vendored/src/yajl_buf.c +103 -0
  44. data/ext/libyajl2/vendored/src/yajl_buf.h +57 -0
  45. data/ext/libyajl2/vendored/src/yajl_bytestack.h +69 -0
  46. data/ext/libyajl2/vendored/src/yajl_encode.c +220 -0
  47. data/ext/libyajl2/vendored/src/yajl_encode.h +34 -0
  48. data/ext/libyajl2/vendored/src/yajl_gen.c +354 -0
  49. data/ext/libyajl2/vendored/src/yajl_lex.c +763 -0
  50. data/ext/libyajl2/vendored/src/yajl_lex.h +117 -0
  51. data/ext/libyajl2/vendored/src/yajl_parser.c +498 -0
  52. data/ext/libyajl2/vendored/src/yajl_parser.h +78 -0
  53. data/ext/libyajl2/vendored/src/yajl_tree.c +503 -0
  54. data/ext/libyajl2/vendored/src/yajl_version.c +7 -0
  55. data/ext/libyajl2/vendored/test/CMakeLists.txt +23 -0
  56. data/ext/libyajl2/vendored/test/cases/ac_difficult_json_c_test_case_with_comments.json +1 -0
  57. data/ext/libyajl2/vendored/test/cases/ac_difficult_json_c_test_case_with_comments.json.gold +36 -0
  58. data/ext/libyajl2/vendored/test/cases/ac_simple_with_comments.json +11 -0
  59. data/ext/libyajl2/vendored/test/cases/ac_simple_with_comments.json.gold +9 -0
  60. data/ext/libyajl2/vendored/test/cases/ag_false_then_garbage.json +1 -0
  61. data/ext/libyajl2/vendored/test/cases/ag_false_then_garbage.json.gold +2 -0
  62. data/ext/libyajl2/vendored/test/cases/ag_null_then_garbage.json +1 -0
  63. data/ext/libyajl2/vendored/test/cases/ag_null_then_garbage.json.gold +2 -0
  64. data/ext/libyajl2/vendored/test/cases/ag_true_then_garbage.json +1 -0
  65. data/ext/libyajl2/vendored/test/cases/ag_true_then_garbage.json.gold +2 -0
  66. data/ext/libyajl2/vendored/test/cases/am_eof.json +1 -0
  67. data/ext/libyajl2/vendored/test/cases/am_eof.json.gold +4 -0
  68. data/ext/libyajl2/vendored/test/cases/am_integers.json +1 -0
  69. data/ext/libyajl2/vendored/test/cases/am_integers.json.gold +3 -0
  70. data/ext/libyajl2/vendored/test/cases/am_multiple.json +3 -0
  71. data/ext/libyajl2/vendored/test/cases/am_multiple.json.gold +5 -0
  72. data/ext/libyajl2/vendored/test/cases/am_stuff.json +7 -0
  73. data/ext/libyajl2/vendored/test/cases/am_stuff.json.gold +14 -0
  74. data/ext/libyajl2/vendored/test/cases/ap_array_open.json +1 -0
  75. data/ext/libyajl2/vendored/test/cases/ap_array_open.json.gold +2 -0
  76. data/ext/libyajl2/vendored/test/cases/ap_eof_str.json +1 -0
  77. data/ext/libyajl2/vendored/test/cases/ap_eof_str.json.gold +1 -0
  78. data/ext/libyajl2/vendored/test/cases/ap_map_open.json +1 -0
  79. data/ext/libyajl2/vendored/test/cases/ap_map_open.json.gold +2 -0
  80. data/ext/libyajl2/vendored/test/cases/ap_partial_ok.json +1 -0
  81. data/ext/libyajl2/vendored/test/cases/ap_partial_ok.json.gold +4 -0
  82. data/ext/libyajl2/vendored/test/cases/array.json +6 -0
  83. data/ext/libyajl2/vendored/test/cases/array.json.gold +22 -0
  84. data/ext/libyajl2/vendored/test/cases/array_close.json +1 -0
  85. data/ext/libyajl2/vendored/test/cases/array_close.json.gold +2 -0
  86. data/ext/libyajl2/vendored/test/cases/bignums.json +1 -0
  87. data/ext/libyajl2/vendored/test/cases/bignums.json.gold +5 -0
  88. data/ext/libyajl2/vendored/test/cases/bogus_char.json +4 -0
  89. data/ext/libyajl2/vendored/test/cases/bogus_char.json.gold +10 -0
  90. data/ext/libyajl2/vendored/test/cases/codepoints_from_unicode_org.json +1 -0
  91. data/ext/libyajl2/vendored/test/cases/codepoints_from_unicode_org.json.gold +2 -0
  92. data/ext/libyajl2/vendored/test/cases/deep_arrays.json +1 -0
  93. data/ext/libyajl2/vendored/test/cases/deep_arrays.json.gold +2049 -0
  94. data/ext/libyajl2/vendored/test/cases/difficult_json_c_test_case.json +1 -0
  95. data/ext/libyajl2/vendored/test/cases/difficult_json_c_test_case.json.gold +36 -0
  96. data/ext/libyajl2/vendored/test/cases/doubles.json +1 -0
  97. data/ext/libyajl2/vendored/test/cases/doubles.json.gold +7 -0
  98. data/ext/libyajl2/vendored/test/cases/doubles_in_array.json +1 -0
  99. data/ext/libyajl2/vendored/test/cases/doubles_in_array.json.gold +8 -0
  100. data/ext/libyajl2/vendored/test/cases/empty_array.json +1 -0
  101. data/ext/libyajl2/vendored/test/cases/empty_array.json.gold +3 -0
  102. data/ext/libyajl2/vendored/test/cases/empty_string.json +1 -0
  103. data/ext/libyajl2/vendored/test/cases/empty_string.json.gold +2 -0
  104. data/ext/libyajl2/vendored/test/cases/escaped_bulgarian.json +4 -0
  105. data/ext/libyajl2/vendored/test/cases/escaped_bulgarian.json.gold +7 -0
  106. data/ext/libyajl2/vendored/test/cases/escaped_foobar.json +1 -0
  107. data/ext/libyajl2/vendored/test/cases/escaped_foobar.json.gold +2 -0
  108. data/ext/libyajl2/vendored/test/cases/false.json +1 -0
  109. data/ext/libyajl2/vendored/test/cases/false.json.gold +2 -0
  110. data/ext/libyajl2/vendored/test/cases/fg_false_then_garbage.json +1 -0
  111. data/ext/libyajl2/vendored/test/cases/fg_false_then_garbage.json.gold +3 -0
  112. data/ext/libyajl2/vendored/test/cases/fg_issue_7.json +1 -0
  113. data/ext/libyajl2/vendored/test/cases/fg_issue_7.json.gold +3 -0
  114. data/ext/libyajl2/vendored/test/cases/fg_null_then_garbage.json +1 -0
  115. data/ext/libyajl2/vendored/test/cases/fg_null_then_garbage.json.gold +3 -0
  116. data/ext/libyajl2/vendored/test/cases/fg_true_then_garbage.json +1 -0
  117. data/ext/libyajl2/vendored/test/cases/fg_true_then_garbage.json.gold +3 -0
  118. data/ext/libyajl2/vendored/test/cases/four_byte_utf8.json +2 -0
  119. data/ext/libyajl2/vendored/test/cases/four_byte_utf8.json.gold +5 -0
  120. data/ext/libyajl2/vendored/test/cases/high_overflow.json +1 -0
  121. data/ext/libyajl2/vendored/test/cases/high_overflow.json.gold +2 -0
  122. data/ext/libyajl2/vendored/test/cases/integers.json +3 -0
  123. data/ext/libyajl2/vendored/test/cases/integers.json.gold +14 -0
  124. data/ext/libyajl2/vendored/test/cases/invalid_utf8.json +1 -0
  125. data/ext/libyajl2/vendored/test/cases/invalid_utf8.json.gold +3 -0
  126. data/ext/libyajl2/vendored/test/cases/isolated_surrogate_marker.json +1 -0
  127. data/ext/libyajl2/vendored/test/cases/isolated_surrogate_marker.json.gold +2 -0
  128. data/ext/libyajl2/vendored/test/cases/leading_zero_in_number.json +1 -0
  129. data/ext/libyajl2/vendored/test/cases/leading_zero_in_number.json.gold +5 -0
  130. data/ext/libyajl2/vendored/test/cases/lonely_minus_sign.json +7 -0
  131. data/ext/libyajl2/vendored/test/cases/lonely_minus_sign.json.gold +9 -0
  132. data/ext/libyajl2/vendored/test/cases/lonely_number.json +1 -0
  133. data/ext/libyajl2/vendored/test/cases/lonely_number.json.gold +2 -0
  134. data/ext/libyajl2/vendored/test/cases/low_overflow.json +1 -0
  135. data/ext/libyajl2/vendored/test/cases/low_overflow.json.gold +2 -0
  136. data/ext/libyajl2/vendored/test/cases/map_close.json +1 -0
  137. data/ext/libyajl2/vendored/test/cases/map_close.json.gold +2 -0
  138. data/ext/libyajl2/vendored/test/cases/missing_integer_after_decimal_point.json +1 -0
  139. data/ext/libyajl2/vendored/test/cases/missing_integer_after_decimal_point.json.gold +2 -0
  140. data/ext/libyajl2/vendored/test/cases/missing_integer_after_exponent.json +1 -0
  141. data/ext/libyajl2/vendored/test/cases/missing_integer_after_exponent.json.gold +2 -0
  142. data/ext/libyajl2/vendored/test/cases/multiple.json +3 -0
  143. data/ext/libyajl2/vendored/test/cases/multiple.json.gold +4 -0
  144. data/ext/libyajl2/vendored/test/cases/non_utf8_char_in_string.json +1 -0
  145. data/ext/libyajl2/vendored/test/cases/non_utf8_char_in_string.json.gold +8 -0
  146. data/ext/libyajl2/vendored/test/cases/np_partial_bad.json +1 -0
  147. data/ext/libyajl2/vendored/test/cases/np_partial_bad.json.gold +5 -0
  148. data/ext/libyajl2/vendored/test/cases/null.json +1 -0
  149. data/ext/libyajl2/vendored/test/cases/null.json.gold +2 -0
  150. data/ext/libyajl2/vendored/test/cases/nulls_and_bools.json +5 -0
  151. data/ext/libyajl2/vendored/test/cases/nulls_and_bools.json.gold +9 -0
  152. data/ext/libyajl2/vendored/test/cases/simple.json +5 -0
  153. data/ext/libyajl2/vendored/test/cases/simple.json.gold +9 -0
  154. data/ext/libyajl2/vendored/test/cases/simple_with_comments.json +11 -0
  155. data/ext/libyajl2/vendored/test/cases/simple_with_comments.json.gold +5 -0
  156. data/ext/libyajl2/vendored/test/cases/string_invalid_escape.json +1 -0
  157. data/ext/libyajl2/vendored/test/cases/string_invalid_escape.json.gold +3 -0
  158. data/ext/libyajl2/vendored/test/cases/string_invalid_hex_char.json +1 -0
  159. data/ext/libyajl2/vendored/test/cases/string_invalid_hex_char.json.gold +2 -0
  160. data/ext/libyajl2/vendored/test/cases/string_with_escapes.json +3 -0
  161. data/ext/libyajl2/vendored/test/cases/string_with_escapes.json.gold +7 -0
  162. data/ext/libyajl2/vendored/test/cases/string_with_invalid_newline.json +2 -0
  163. data/ext/libyajl2/vendored/test/cases/string_with_invalid_newline.json.gold +2 -0
  164. data/ext/libyajl2/vendored/test/cases/three_byte_utf8.json +1 -0
  165. data/ext/libyajl2/vendored/test/cases/three_byte_utf8.json.gold +7 -0
  166. data/ext/libyajl2/vendored/test/cases/true.json +1 -0
  167. data/ext/libyajl2/vendored/test/cases/true.json.gold +2 -0
  168. data/ext/libyajl2/vendored/test/cases/unescaped_bulgarian.json +1 -0
  169. data/ext/libyajl2/vendored/test/cases/unescaped_bulgarian.json.gold +4 -0
  170. data/ext/libyajl2/vendored/test/cases/zerobyte.json +1 -0
  171. data/ext/libyajl2/vendored/test/cases/zerobyte.json.gold +0 -0
  172. data/ext/libyajl2/vendored/test/run_tests.sh +94 -0
  173. data/ext/libyajl2/vendored/test/yajl_test.c +281 -0
  174. data/ext/libyajl2/vendored/verify/CMakeLists.txt +39 -0
  175. data/ext/libyajl2/vendored/verify/json_verify.c +116 -0
  176. data/lib/ffi_yajl.rb +14 -0
  177. data/lib/ffi_yajl/benchmark.rb +7 -0
  178. data/lib/ffi_yajl/benchmark/MIT-LICENSE +20 -0
  179. data/lib/ffi_yajl/benchmark/encode.rb +135 -0
  180. data/lib/ffi_yajl/benchmark/encode_json_and_marshal.rb +42 -0
  181. data/lib/ffi_yajl/benchmark/encode_json_and_yaml.rb +53 -0
  182. data/lib/ffi_yajl/benchmark/encode_profile.rb +38 -0
  183. data/lib/ffi_yajl/benchmark/http.rb +32 -0
  184. data/lib/ffi_yajl/benchmark/parse.rb +133 -0
  185. data/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb +50 -0
  186. data/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb +55 -0
  187. data/lib/ffi_yajl/benchmark/parse_profile.rb +37 -0
  188. data/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb +39 -0
  189. data/lib/ffi_yajl/benchmark/parse_stream.rb +54 -0
  190. data/lib/ffi_yajl/benchmark/subjects/item.json +1 -0
  191. data/lib/ffi_yajl/benchmark/subjects/ohai.json +1216 -0
  192. data/lib/ffi_yajl/benchmark/subjects/ohai.marshal_dump +0 -0
  193. data/lib/ffi_yajl/benchmark/subjects/ohai.yml +975 -0
  194. data/lib/ffi_yajl/benchmark/subjects/twitter_search.json +1 -0
  195. data/lib/ffi_yajl/benchmark/subjects/twitter_stream.json +430 -0
  196. data/lib/ffi_yajl/benchmark/subjects/unicode.json +1 -0
  197. data/lib/ffi_yajl/encoder.rb +53 -0
  198. data/lib/ffi_yajl/ext.rb +22 -0
  199. data/lib/ffi_yajl/ext/.keep +0 -0
  200. data/lib/ffi_yajl/ffi.rb +129 -0
  201. data/lib/ffi_yajl/ffi/encoder.rb +175 -0
  202. data/lib/ffi_yajl/ffi/parser.rb +145 -0
  203. data/lib/ffi_yajl/json_gem.rb +121 -0
  204. data/lib/ffi_yajl/parser.rb +23 -0
  205. data/lib/ffi_yajl/version.rb +3 -0
  206. data/lib/libyajl.so +0 -0
  207. data/lib/libyajl.so.2 +0 -0
  208. data/lib/libyajl.so.2.0.5 +0 -0
  209. data/lib/libyajl_s.a +0 -0
  210. data/spec/ffi_yajl/encoder_spec.rb +39 -0
  211. data/spec/ffi_yajl/json_gem_spec.rb +355 -0
  212. data/spec/ffi_yajl/parser_spec.rb +78 -0
  213. data/spec/spec_helper.rb +14 -0
  214. metadata +332 -0
@@ -0,0 +1,34 @@
1
+ /*
2
+ * Copyright (c) 2007-2011, Lloyd Hilaiel <lloyd@hilaiel.com>
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
+
17
+ /**
18
+ * \file yajl_alloc.h
19
+ * default memory allocation routines for yajl which use malloc/realloc and
20
+ * free
21
+ */
22
+
23
+ #ifndef __YAJL_ALLOC_H__
24
+ #define __YAJL_ALLOC_H__
25
+
26
+ #include "api/yajl_common.h"
27
+
28
+ #define YA_MALLOC(afs, sz) (afs)->malloc((afs)->ctx, (sz))
29
+ #define YA_FREE(afs, ptr) (afs)->free((afs)->ctx, (ptr))
30
+ #define YA_REALLOC(afs, ptr, sz) (afs)->realloc((afs)->ctx, (ptr), (sz))
31
+
32
+ void yajl_set_default_alloc_funcs(yajl_alloc_funcs * yaf);
33
+
34
+ #endif
@@ -0,0 +1,103 @@
1
+ /*
2
+ * Copyright (c) 2007-2011, Lloyd Hilaiel <lloyd@hilaiel.com>
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
+
17
+ #include "yajl_buf.h"
18
+
19
+ #include <assert.h>
20
+ #include <stdlib.h>
21
+ #include <string.h>
22
+
23
+ #define YAJL_BUF_INIT_SIZE 2048
24
+
25
+ struct yajl_buf_t {
26
+ size_t len;
27
+ size_t used;
28
+ unsigned char * data;
29
+ yajl_alloc_funcs * alloc;
30
+ };
31
+
32
+ static
33
+ void yajl_buf_ensure_available(yajl_buf buf, size_t want)
34
+ {
35
+ size_t need;
36
+
37
+ assert(buf != NULL);
38
+
39
+ /* first call */
40
+ if (buf->data == NULL) {
41
+ buf->len = YAJL_BUF_INIT_SIZE;
42
+ buf->data = (unsigned char *) YA_MALLOC(buf->alloc, buf->len);
43
+ buf->data[0] = 0;
44
+ }
45
+
46
+ need = buf->len;
47
+
48
+ while (want >= (need - buf->used)) need <<= 1;
49
+
50
+ if (need != buf->len) {
51
+ buf->data = (unsigned char *) YA_REALLOC(buf->alloc, buf->data, need);
52
+ buf->len = need;
53
+ }
54
+ }
55
+
56
+ yajl_buf yajl_buf_alloc(yajl_alloc_funcs * alloc)
57
+ {
58
+ yajl_buf b = YA_MALLOC(alloc, sizeof(struct yajl_buf_t));
59
+ memset((void *) b, 0, sizeof(struct yajl_buf_t));
60
+ b->alloc = alloc;
61
+ return b;
62
+ }
63
+
64
+ void yajl_buf_free(yajl_buf buf)
65
+ {
66
+ assert(buf != NULL);
67
+ if (buf->data) YA_FREE(buf->alloc, buf->data);
68
+ YA_FREE(buf->alloc, buf);
69
+ }
70
+
71
+ void yajl_buf_append(yajl_buf buf, const void * data, size_t len)
72
+ {
73
+ yajl_buf_ensure_available(buf, len);
74
+ if (len > 0) {
75
+ assert(data != NULL);
76
+ memcpy(buf->data + buf->used, data, len);
77
+ buf->used += len;
78
+ buf->data[buf->used] = 0;
79
+ }
80
+ }
81
+
82
+ void yajl_buf_clear(yajl_buf buf)
83
+ {
84
+ buf->used = 0;
85
+ if (buf->data) buf->data[buf->used] = 0;
86
+ }
87
+
88
+ const unsigned char * yajl_buf_data(yajl_buf buf)
89
+ {
90
+ return buf->data;
91
+ }
92
+
93
+ size_t yajl_buf_len(yajl_buf buf)
94
+ {
95
+ return buf->used;
96
+ }
97
+
98
+ void
99
+ yajl_buf_truncate(yajl_buf buf, size_t len)
100
+ {
101
+ assert(len <= buf->used);
102
+ buf->used = len;
103
+ }
@@ -0,0 +1,57 @@
1
+ /*
2
+ * Copyright (c) 2007-2011, Lloyd Hilaiel <lloyd@hilaiel.com>
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
+
17
+ #ifndef __YAJL_BUF_H__
18
+ #define __YAJL_BUF_H__
19
+
20
+ #include "api/yajl_common.h"
21
+ #include "yajl_alloc.h"
22
+
23
+ /*
24
+ * Implementation/performance notes. If this were moved to a header
25
+ * only implementation using #define's where possible we might be
26
+ * able to sqeeze a little performance out of the guy by killing function
27
+ * call overhead. YMMV.
28
+ */
29
+
30
+ /**
31
+ * yajl_buf is a buffer with exponential growth. the buffer ensures that
32
+ * you are always null padded.
33
+ */
34
+ typedef struct yajl_buf_t * yajl_buf;
35
+
36
+ /* allocate a new buffer */
37
+ yajl_buf yajl_buf_alloc(yajl_alloc_funcs * alloc);
38
+
39
+ /* free the buffer */
40
+ void yajl_buf_free(yajl_buf buf);
41
+
42
+ /* append a number of bytes to the buffer */
43
+ void yajl_buf_append(yajl_buf buf, const void * data, size_t len);
44
+
45
+ /* empty the buffer */
46
+ void yajl_buf_clear(yajl_buf buf);
47
+
48
+ /* get a pointer to the beginning of the buffer */
49
+ const unsigned char * yajl_buf_data(yajl_buf buf);
50
+
51
+ /* get the length of the buffer */
52
+ size_t yajl_buf_len(yajl_buf buf);
53
+
54
+ /* truncate the buffer */
55
+ void yajl_buf_truncate(yajl_buf buf, size_t len);
56
+
57
+ #endif
@@ -0,0 +1,69 @@
1
+ /*
2
+ * Copyright (c) 2007-2011, Lloyd Hilaiel <lloyd@hilaiel.com>
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
+
17
+ /*
18
+ * A header only implementation of a simple stack of bytes, used in YAJL
19
+ * to maintain parse state.
20
+ */
21
+
22
+ #ifndef __YAJL_BYTESTACK_H__
23
+ #define __YAJL_BYTESTACK_H__
24
+
25
+ #include "api/yajl_common.h"
26
+
27
+ #define YAJL_BS_INC 128
28
+
29
+ typedef struct yajl_bytestack_t
30
+ {
31
+ unsigned char * stack;
32
+ size_t size;
33
+ size_t used;
34
+ yajl_alloc_funcs * yaf;
35
+ } yajl_bytestack;
36
+
37
+ /* initialize a bytestack */
38
+ #define yajl_bs_init(obs, _yaf) { \
39
+ (obs).stack = NULL; \
40
+ (obs).size = 0; \
41
+ (obs).used = 0; \
42
+ (obs).yaf = (_yaf); \
43
+ } \
44
+
45
+
46
+ /* initialize a bytestack */
47
+ #define yajl_bs_free(obs) \
48
+ if ((obs).stack) (obs).yaf->free((obs).yaf->ctx, (obs).stack);
49
+
50
+ #define yajl_bs_current(obs) \
51
+ (assert((obs).used > 0), (obs).stack[(obs).used - 1])
52
+
53
+ #define yajl_bs_push(obs, byte) { \
54
+ if (((obs).size - (obs).used) == 0) { \
55
+ (obs).size += YAJL_BS_INC; \
56
+ (obs).stack = (obs).yaf->realloc((obs).yaf->ctx,\
57
+ (void *) (obs).stack, (obs).size);\
58
+ } \
59
+ (obs).stack[((obs).used)++] = (byte); \
60
+ }
61
+
62
+ /* removes the top item of the stack, returns nothing */
63
+ #define yajl_bs_pop(obs) { ((obs).used)--; }
64
+
65
+ #define yajl_bs_set(obs, byte) \
66
+ (obs).stack[((obs).used) - 1] = (byte);
67
+
68
+
69
+ #endif
@@ -0,0 +1,220 @@
1
+ /*
2
+ * Copyright (c) 2007-2011, Lloyd Hilaiel <lloyd@hilaiel.com>
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
+
17
+ #include "yajl_encode.h"
18
+
19
+ #include <assert.h>
20
+ #include <stdlib.h>
21
+ #include <string.h>
22
+ #include <stdio.h>
23
+
24
+ static void CharToHex(unsigned char c, char * hexBuf)
25
+ {
26
+ const char * hexchar = "0123456789ABCDEF";
27
+ hexBuf[0] = hexchar[c >> 4];
28
+ hexBuf[1] = hexchar[c & 0x0F];
29
+ }
30
+
31
+ void
32
+ yajl_string_encode(const yajl_print_t print,
33
+ void * ctx,
34
+ const unsigned char * str,
35
+ size_t len,
36
+ int escape_solidus)
37
+ {
38
+ size_t beg = 0;
39
+ size_t end = 0;
40
+ char hexBuf[7];
41
+ hexBuf[0] = '\\'; hexBuf[1] = 'u'; hexBuf[2] = '0'; hexBuf[3] = '0';
42
+ hexBuf[6] = 0;
43
+
44
+ while (end < len) {
45
+ const char * escaped = NULL;
46
+ switch (str[end]) {
47
+ case '\r': escaped = "\\r"; break;
48
+ case '\n': escaped = "\\n"; break;
49
+ case '\\': escaped = "\\\\"; break;
50
+ /* it is not required to escape a solidus in JSON:
51
+ * read sec. 2.5: http://www.ietf.org/rfc/rfc4627.txt
52
+ * specifically, this production from the grammar:
53
+ * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF
54
+ */
55
+ case '/': if (escape_solidus) escaped = "\\/"; break;
56
+ case '"': escaped = "\\\""; break;
57
+ case '\f': escaped = "\\f"; break;
58
+ case '\b': escaped = "\\b"; break;
59
+ case '\t': escaped = "\\t"; break;
60
+ default:
61
+ if ((unsigned char) str[end] < 32) {
62
+ CharToHex(str[end], hexBuf + 4);
63
+ escaped = hexBuf;
64
+ }
65
+ break;
66
+ }
67
+ if (escaped != NULL) {
68
+ print(ctx, (const char *) (str + beg), end - beg);
69
+ print(ctx, escaped, (unsigned int)strlen(escaped));
70
+ beg = ++end;
71
+ } else {
72
+ ++end;
73
+ }
74
+ }
75
+ print(ctx, (const char *) (str + beg), end - beg);
76
+ }
77
+
78
+ static void hexToDigit(unsigned int * val, const unsigned char * hex)
79
+ {
80
+ unsigned int i;
81
+ for (i=0;i<4;i++) {
82
+ unsigned char c = hex[i];
83
+ if (c >= 'A') c = (c & ~0x20) - 7;
84
+ c -= '0';
85
+ assert(!(c & 0xF0));
86
+ *val = (*val << 4) | c;
87
+ }
88
+ }
89
+
90
+ static void Utf32toUtf8(unsigned int codepoint, char * utf8Buf)
91
+ {
92
+ if (codepoint < 0x80) {
93
+ utf8Buf[0] = (char) codepoint;
94
+ utf8Buf[1] = 0;
95
+ } else if (codepoint < 0x0800) {
96
+ utf8Buf[0] = (char) ((codepoint >> 6) | 0xC0);
97
+ utf8Buf[1] = (char) ((codepoint & 0x3F) | 0x80);
98
+ utf8Buf[2] = 0;
99
+ } else if (codepoint < 0x10000) {
100
+ utf8Buf[0] = (char) ((codepoint >> 12) | 0xE0);
101
+ utf8Buf[1] = (char) (((codepoint >> 6) & 0x3F) | 0x80);
102
+ utf8Buf[2] = (char) ((codepoint & 0x3F) | 0x80);
103
+ utf8Buf[3] = 0;
104
+ } else if (codepoint < 0x200000) {
105
+ utf8Buf[0] =(char)((codepoint >> 18) | 0xF0);
106
+ utf8Buf[1] =(char)(((codepoint >> 12) & 0x3F) | 0x80);
107
+ utf8Buf[2] =(char)(((codepoint >> 6) & 0x3F) | 0x80);
108
+ utf8Buf[3] =(char)((codepoint & 0x3F) | 0x80);
109
+ utf8Buf[4] = 0;
110
+ } else {
111
+ utf8Buf[0] = '?';
112
+ utf8Buf[1] = 0;
113
+ }
114
+ }
115
+
116
+ void yajl_string_decode(yajl_buf buf, const unsigned char * str,
117
+ size_t len)
118
+ {
119
+ size_t beg = 0;
120
+ size_t end = 0;
121
+
122
+ while (end < len) {
123
+ if (str[end] == '\\') {
124
+ char utf8Buf[5];
125
+ const char * unescaped = "?";
126
+ yajl_buf_append(buf, str + beg, end - beg);
127
+ switch (str[++end]) {
128
+ case 'r': unescaped = "\r"; break;
129
+ case 'n': unescaped = "\n"; break;
130
+ case '\\': unescaped = "\\"; break;
131
+ case '/': unescaped = "/"; break;
132
+ case '"': unescaped = "\""; break;
133
+ case 'f': unescaped = "\f"; break;
134
+ case 'b': unescaped = "\b"; break;
135
+ case 't': unescaped = "\t"; break;
136
+ case 'u': {
137
+ unsigned int codepoint = 0;
138
+ hexToDigit(&codepoint, str + ++end);
139
+ end+=3;
140
+ /* check if this is a surrogate */
141
+ if ((codepoint & 0xFC00) == 0xD800) {
142
+ end++;
143
+ if (str[end] == '\\' && str[end + 1] == 'u') {
144
+ unsigned int surrogate = 0;
145
+ hexToDigit(&surrogate, str + end + 2);
146
+ codepoint =
147
+ (((codepoint & 0x3F) << 10) |
148
+ ((((codepoint >> 6) & 0xF) + 1) << 16) |
149
+ (surrogate & 0x3FF));
150
+ end += 5;
151
+ } else {
152
+ unescaped = "?";
153
+ break;
154
+ }
155
+ }
156
+
157
+ Utf32toUtf8(codepoint, utf8Buf);
158
+ unescaped = utf8Buf;
159
+
160
+ if (codepoint == 0) {
161
+ yajl_buf_append(buf, unescaped, 1);
162
+ beg = ++end;
163
+ continue;
164
+ }
165
+
166
+ break;
167
+ }
168
+ default:
169
+ assert("this should never happen" == NULL);
170
+ }
171
+ yajl_buf_append(buf, unescaped, (unsigned int)strlen(unescaped));
172
+ beg = ++end;
173
+ } else {
174
+ end++;
175
+ }
176
+ }
177
+ yajl_buf_append(buf, str + beg, end - beg);
178
+ }
179
+
180
+ #define ADV_PTR s++; if (!(len--)) return 0;
181
+
182
+ int yajl_string_validate_utf8(const unsigned char * s, size_t len)
183
+ {
184
+ if (!len) return 1;
185
+ if (!s) return 0;
186
+
187
+ while (len--) {
188
+ /* single byte */
189
+ if (*s <= 0x7f) {
190
+ /* noop */
191
+ }
192
+ /* two byte */
193
+ else if ((*s >> 5) == 0x6) {
194
+ ADV_PTR;
195
+ if (!((*s >> 6) == 0x2)) return 0;
196
+ }
197
+ /* three byte */
198
+ else if ((*s >> 4) == 0x0e) {
199
+ ADV_PTR;
200
+ if (!((*s >> 6) == 0x2)) return 0;
201
+ ADV_PTR;
202
+ if (!((*s >> 6) == 0x2)) return 0;
203
+ }
204
+ /* four byte */
205
+ else if ((*s >> 3) == 0x1e) {
206
+ ADV_PTR;
207
+ if (!((*s >> 6) == 0x2)) return 0;
208
+ ADV_PTR;
209
+ if (!((*s >> 6) == 0x2)) return 0;
210
+ ADV_PTR;
211
+ if (!((*s >> 6) == 0x2)) return 0;
212
+ } else {
213
+ return 0;
214
+ }
215
+
216
+ s++;
217
+ }
218
+
219
+ return 1;
220
+ }