libyajl2 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -0
  3. data/.gitmodules +3 -0
  4. data/.kitchen.cloud.yml +87 -0
  5. data/.kitchen.yml +38 -0
  6. data/.reek.yml +3 -0
  7. data/.rspec +1 -0
  8. data/.rubocop.yml +38 -0
  9. data/.travis.yml +8 -0
  10. data/CONTRIBUTING.md +4 -0
  11. data/Gemfile +12 -0
  12. data/LICENSE +201 -0
  13. data/README.md +9 -0
  14. data/Rakefile +144 -0
  15. data/bootstrap.sh +60 -0
  16. data/ext/libyajl2/Makefile +7 -0
  17. data/ext/libyajl2/extconf.rb +66 -0
  18. data/ext/libyajl2/vendor/yajl/.gitignore +3 -0
  19. data/ext/libyajl2/vendor/yajl/BUILDING +23 -0
  20. data/ext/libyajl2/vendor/yajl/BUILDING.win32 +27 -0
  21. data/ext/libyajl2/vendor/yajl/CMakeLists.txt +81 -0
  22. data/ext/libyajl2/vendor/yajl/COPYING +13 -0
  23. data/ext/libyajl2/vendor/yajl/ChangeLog +189 -0
  24. data/ext/libyajl2/vendor/yajl/README +74 -0
  25. data/ext/libyajl2/vendor/yajl/TODO +9 -0
  26. data/ext/libyajl2/vendor/yajl/YAJLDoc.cmake +26 -0
  27. data/ext/libyajl2/vendor/yajl/configure +81 -0
  28. data/ext/libyajl2/vendor/yajl/example/CMakeLists.txt +23 -0
  29. data/ext/libyajl2/vendor/yajl/example/README.md +7 -0
  30. data/ext/libyajl2/vendor/yajl/example/parse_config.c +69 -0
  31. data/ext/libyajl2/vendor/yajl/example/sample.config +101 -0
  32. data/ext/libyajl2/vendor/yajl/perf/CMakeLists.txt +23 -0
  33. data/ext/libyajl2/vendor/yajl/perf/documents.c +1418 -0
  34. data/ext/libyajl2/vendor/yajl/perf/documents.h +28 -0
  35. data/ext/libyajl2/vendor/yajl/perf/perftest.c +134 -0
  36. data/ext/libyajl2/vendor/yajl/reformatter/CMakeLists.txt +43 -0
  37. data/ext/libyajl2/vendor/yajl/reformatter/json_reformat.c +210 -0
  38. data/ext/libyajl2/vendor/yajl/src/CMakeLists.txt +87 -0
  39. data/ext/libyajl2/vendor/yajl/src/YAJL.dxy +1258 -0
  40. data/ext/libyajl2/vendor/yajl/src/api/yajl_common.h +75 -0
  41. data/ext/libyajl2/vendor/yajl/src/api/yajl_gen.h +167 -0
  42. data/ext/libyajl2/vendor/yajl/src/api/yajl_parse.h +226 -0
  43. data/ext/libyajl2/vendor/yajl/src/api/yajl_tree.h +186 -0
  44. data/ext/libyajl2/vendor/yajl/src/api/yajl_version.h.cmake +23 -0
  45. data/ext/libyajl2/vendor/yajl/src/yajl +33 -0
  46. data/ext/libyajl2/vendor/yajl/src/yajl.c +175 -0
  47. data/ext/libyajl2/vendor/yajl/src/yajl.pc.cmake +9 -0
  48. data/ext/libyajl2/vendor/yajl/src/yajl_alloc.c +52 -0
  49. data/ext/libyajl2/vendor/yajl/src/yajl_alloc.h +34 -0
  50. data/ext/libyajl2/vendor/yajl/src/yajl_buf.c +103 -0
  51. data/ext/libyajl2/vendor/yajl/src/yajl_buf.h +57 -0
  52. data/ext/libyajl2/vendor/yajl/src/yajl_bytestack.h +69 -0
  53. data/ext/libyajl2/vendor/yajl/src/yajl_encode.c +220 -0
  54. data/ext/libyajl2/vendor/yajl/src/yajl_encode.h +34 -0
  55. data/ext/libyajl2/vendor/yajl/src/yajl_gen.c +362 -0
  56. data/ext/libyajl2/vendor/yajl/src/yajl_lex.c +763 -0
  57. data/ext/libyajl2/vendor/yajl/src/yajl_lex.h +117 -0
  58. data/ext/libyajl2/vendor/yajl/src/yajl_parser.c +498 -0
  59. data/ext/libyajl2/vendor/yajl/src/yajl_parser.h +78 -0
  60. data/ext/libyajl2/vendor/yajl/src/yajl_tree.c +503 -0
  61. data/ext/libyajl2/vendor/yajl/src/yajl_version.c +7 -0
  62. data/ext/libyajl2/vendor/yajl/test/CMakeLists.txt +16 -0
  63. data/ext/libyajl2/vendor/yajl/test/api/CMakeLists.txt +25 -0
  64. data/ext/libyajl2/vendor/yajl/test/api/gen-extra-close.c +19 -0
  65. data/ext/libyajl2/vendor/yajl/test/api/run_tests.sh +23 -0
  66. data/ext/libyajl2/vendor/yajl/test/parsing/CMakeLists.txt +23 -0
  67. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ac_difficult_json_c_test_case_with_comments.json +1 -0
  68. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ac_difficult_json_c_test_case_with_comments.json.gold +36 -0
  69. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ac_simple_with_comments.json +11 -0
  70. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ac_simple_with_comments.json.gold +9 -0
  71. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ag_false_then_garbage.json +1 -0
  72. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ag_false_then_garbage.json.gold +2 -0
  73. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ag_null_then_garbage.json +1 -0
  74. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ag_null_then_garbage.json.gold +2 -0
  75. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ag_true_then_garbage.json +1 -0
  76. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ag_true_then_garbage.json.gold +2 -0
  77. data/ext/libyajl2/vendor/yajl/test/parsing/cases/am_eof.json +1 -0
  78. data/ext/libyajl2/vendor/yajl/test/parsing/cases/am_eof.json.gold +4 -0
  79. data/ext/libyajl2/vendor/yajl/test/parsing/cases/am_integers.json +1 -0
  80. data/ext/libyajl2/vendor/yajl/test/parsing/cases/am_integers.json.gold +3 -0
  81. data/ext/libyajl2/vendor/yajl/test/parsing/cases/am_multiple.json +3 -0
  82. data/ext/libyajl2/vendor/yajl/test/parsing/cases/am_multiple.json.gold +5 -0
  83. data/ext/libyajl2/vendor/yajl/test/parsing/cases/am_stuff.json +7 -0
  84. data/ext/libyajl2/vendor/yajl/test/parsing/cases/am_stuff.json.gold +14 -0
  85. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ap_array_open.json +1 -0
  86. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ap_array_open.json.gold +2 -0
  87. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ap_eof_str.json +1 -0
  88. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ap_eof_str.json.gold +1 -0
  89. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ap_map_open.json +1 -0
  90. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ap_map_open.json.gold +2 -0
  91. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ap_partial_ok.json +1 -0
  92. data/ext/libyajl2/vendor/yajl/test/parsing/cases/ap_partial_ok.json.gold +4 -0
  93. data/ext/libyajl2/vendor/yajl/test/parsing/cases/array.json +6 -0
  94. data/ext/libyajl2/vendor/yajl/test/parsing/cases/array.json.gold +22 -0
  95. data/ext/libyajl2/vendor/yajl/test/parsing/cases/array_close.json +1 -0
  96. data/ext/libyajl2/vendor/yajl/test/parsing/cases/array_close.json.gold +2 -0
  97. data/ext/libyajl2/vendor/yajl/test/parsing/cases/bignums.json +1 -0
  98. data/ext/libyajl2/vendor/yajl/test/parsing/cases/bignums.json.gold +5 -0
  99. data/ext/libyajl2/vendor/yajl/test/parsing/cases/bogus_char.json +4 -0
  100. data/ext/libyajl2/vendor/yajl/test/parsing/cases/bogus_char.json.gold +10 -0
  101. data/ext/libyajl2/vendor/yajl/test/parsing/cases/codepoints_from_unicode_org.json +1 -0
  102. data/ext/libyajl2/vendor/yajl/test/parsing/cases/codepoints_from_unicode_org.json.gold +2 -0
  103. data/ext/libyajl2/vendor/yajl/test/parsing/cases/deep_arrays.json +1 -0
  104. data/ext/libyajl2/vendor/yajl/test/parsing/cases/deep_arrays.json.gold +2049 -0
  105. data/ext/libyajl2/vendor/yajl/test/parsing/cases/difficult_json_c_test_case.json +1 -0
  106. data/ext/libyajl2/vendor/yajl/test/parsing/cases/difficult_json_c_test_case.json.gold +36 -0
  107. data/ext/libyajl2/vendor/yajl/test/parsing/cases/doubles.json +1 -0
  108. data/ext/libyajl2/vendor/yajl/test/parsing/cases/doubles.json.gold +7 -0
  109. data/ext/libyajl2/vendor/yajl/test/parsing/cases/doubles_in_array.json +1 -0
  110. data/ext/libyajl2/vendor/yajl/test/parsing/cases/doubles_in_array.json.gold +8 -0
  111. data/ext/libyajl2/vendor/yajl/test/parsing/cases/empty_array.json +1 -0
  112. data/ext/libyajl2/vendor/yajl/test/parsing/cases/empty_array.json.gold +3 -0
  113. data/ext/libyajl2/vendor/yajl/test/parsing/cases/empty_string.json +1 -0
  114. data/ext/libyajl2/vendor/yajl/test/parsing/cases/empty_string.json.gold +2 -0
  115. data/ext/libyajl2/vendor/yajl/test/parsing/cases/escaped_bulgarian.json +4 -0
  116. data/ext/libyajl2/vendor/yajl/test/parsing/cases/escaped_bulgarian.json.gold +7 -0
  117. data/ext/libyajl2/vendor/yajl/test/parsing/cases/escaped_foobar.json +1 -0
  118. data/ext/libyajl2/vendor/yajl/test/parsing/cases/escaped_foobar.json.gold +2 -0
  119. data/ext/libyajl2/vendor/yajl/test/parsing/cases/false.json +1 -0
  120. data/ext/libyajl2/vendor/yajl/test/parsing/cases/false.json.gold +2 -0
  121. data/ext/libyajl2/vendor/yajl/test/parsing/cases/fg_false_then_garbage.json +1 -0
  122. data/ext/libyajl2/vendor/yajl/test/parsing/cases/fg_false_then_garbage.json.gold +3 -0
  123. data/ext/libyajl2/vendor/yajl/test/parsing/cases/fg_issue_7.json +1 -0
  124. data/ext/libyajl2/vendor/yajl/test/parsing/cases/fg_issue_7.json.gold +3 -0
  125. data/ext/libyajl2/vendor/yajl/test/parsing/cases/fg_null_then_garbage.json +1 -0
  126. data/ext/libyajl2/vendor/yajl/test/parsing/cases/fg_null_then_garbage.json.gold +3 -0
  127. data/ext/libyajl2/vendor/yajl/test/parsing/cases/fg_true_then_garbage.json +1 -0
  128. data/ext/libyajl2/vendor/yajl/test/parsing/cases/fg_true_then_garbage.json.gold +3 -0
  129. data/ext/libyajl2/vendor/yajl/test/parsing/cases/four_byte_utf8.json +2 -0
  130. data/ext/libyajl2/vendor/yajl/test/parsing/cases/four_byte_utf8.json.gold +5 -0
  131. data/ext/libyajl2/vendor/yajl/test/parsing/cases/high_overflow.json +1 -0
  132. data/ext/libyajl2/vendor/yajl/test/parsing/cases/high_overflow.json.gold +2 -0
  133. data/ext/libyajl2/vendor/yajl/test/parsing/cases/integers.json +3 -0
  134. data/ext/libyajl2/vendor/yajl/test/parsing/cases/integers.json.gold +14 -0
  135. data/ext/libyajl2/vendor/yajl/test/parsing/cases/invalid_utf8.json +1 -0
  136. data/ext/libyajl2/vendor/yajl/test/parsing/cases/invalid_utf8.json.gold +3 -0
  137. data/ext/libyajl2/vendor/yajl/test/parsing/cases/isolated_surrogate_marker.json +1 -0
  138. data/ext/libyajl2/vendor/yajl/test/parsing/cases/isolated_surrogate_marker.json.gold +2 -0
  139. data/ext/libyajl2/vendor/yajl/test/parsing/cases/leading_zero_in_number.json +1 -0
  140. data/ext/libyajl2/vendor/yajl/test/parsing/cases/leading_zero_in_number.json.gold +5 -0
  141. data/ext/libyajl2/vendor/yajl/test/parsing/cases/lonely_minus_sign.json +7 -0
  142. data/ext/libyajl2/vendor/yajl/test/parsing/cases/lonely_minus_sign.json.gold +9 -0
  143. data/ext/libyajl2/vendor/yajl/test/parsing/cases/lonely_number.json +1 -0
  144. data/ext/libyajl2/vendor/yajl/test/parsing/cases/lonely_number.json.gold +2 -0
  145. data/ext/libyajl2/vendor/yajl/test/parsing/cases/low_overflow.json +1 -0
  146. data/ext/libyajl2/vendor/yajl/test/parsing/cases/low_overflow.json.gold +2 -0
  147. data/ext/libyajl2/vendor/yajl/test/parsing/cases/map_close.json +1 -0
  148. data/ext/libyajl2/vendor/yajl/test/parsing/cases/map_close.json.gold +2 -0
  149. data/ext/libyajl2/vendor/yajl/test/parsing/cases/missing_integer_after_decimal_point.json +1 -0
  150. data/ext/libyajl2/vendor/yajl/test/parsing/cases/missing_integer_after_decimal_point.json.gold +2 -0
  151. data/ext/libyajl2/vendor/yajl/test/parsing/cases/missing_integer_after_exponent.json +1 -0
  152. data/ext/libyajl2/vendor/yajl/test/parsing/cases/missing_integer_after_exponent.json.gold +2 -0
  153. data/ext/libyajl2/vendor/yajl/test/parsing/cases/multiple.json +3 -0
  154. data/ext/libyajl2/vendor/yajl/test/parsing/cases/multiple.json.gold +4 -0
  155. data/ext/libyajl2/vendor/yajl/test/parsing/cases/non_utf8_char_in_string.json +1 -0
  156. data/ext/libyajl2/vendor/yajl/test/parsing/cases/non_utf8_char_in_string.json.gold +8 -0
  157. data/ext/libyajl2/vendor/yajl/test/parsing/cases/np_partial_bad.json +1 -0
  158. data/ext/libyajl2/vendor/yajl/test/parsing/cases/np_partial_bad.json.gold +5 -0
  159. data/ext/libyajl2/vendor/yajl/test/parsing/cases/null.json +1 -0
  160. data/ext/libyajl2/vendor/yajl/test/parsing/cases/null.json.gold +2 -0
  161. data/ext/libyajl2/vendor/yajl/test/parsing/cases/nulls_and_bools.json +5 -0
  162. data/ext/libyajl2/vendor/yajl/test/parsing/cases/nulls_and_bools.json.gold +9 -0
  163. data/ext/libyajl2/vendor/yajl/test/parsing/cases/simple.json +5 -0
  164. data/ext/libyajl2/vendor/yajl/test/parsing/cases/simple.json.gold +9 -0
  165. data/ext/libyajl2/vendor/yajl/test/parsing/cases/simple_with_comments.json +11 -0
  166. data/ext/libyajl2/vendor/yajl/test/parsing/cases/simple_with_comments.json.gold +5 -0
  167. data/ext/libyajl2/vendor/yajl/test/parsing/cases/string_invalid_escape.json +1 -0
  168. data/ext/libyajl2/vendor/yajl/test/parsing/cases/string_invalid_escape.json.gold +3 -0
  169. data/ext/libyajl2/vendor/yajl/test/parsing/cases/string_invalid_hex_char.json +1 -0
  170. data/ext/libyajl2/vendor/yajl/test/parsing/cases/string_invalid_hex_char.json.gold +2 -0
  171. data/ext/libyajl2/vendor/yajl/test/parsing/cases/string_with_escapes.json +3 -0
  172. data/ext/libyajl2/vendor/yajl/test/parsing/cases/string_with_escapes.json.gold +7 -0
  173. data/ext/libyajl2/vendor/yajl/test/parsing/cases/string_with_invalid_newline.json +2 -0
  174. data/ext/libyajl2/vendor/yajl/test/parsing/cases/string_with_invalid_newline.json.gold +2 -0
  175. data/ext/libyajl2/vendor/yajl/test/parsing/cases/three_byte_utf8.json +1 -0
  176. data/ext/libyajl2/vendor/yajl/test/parsing/cases/three_byte_utf8.json.gold +7 -0
  177. data/ext/libyajl2/vendor/yajl/test/parsing/cases/true.json +1 -0
  178. data/ext/libyajl2/vendor/yajl/test/parsing/cases/true.json.gold +2 -0
  179. data/ext/libyajl2/vendor/yajl/test/parsing/cases/unescaped_bulgarian.json +1 -0
  180. data/ext/libyajl2/vendor/yajl/test/parsing/cases/unescaped_bulgarian.json.gold +4 -0
  181. data/ext/libyajl2/vendor/yajl/test/parsing/cases/zerobyte.json +1 -0
  182. data/ext/libyajl2/vendor/yajl/test/parsing/cases/zerobyte.json.gold +0 -0
  183. data/ext/libyajl2/vendor/yajl/test/parsing/run_tests.sh +94 -0
  184. data/ext/libyajl2/vendor/yajl/test/parsing/yajl_test.c +291 -0
  185. data/ext/libyajl2/vendor/yajl/verify/CMakeLists.txt +37 -0
  186. data/ext/libyajl2/vendor/yajl/verify/json_verify.c +120 -0
  187. data/lib/libyajl2.rb +13 -0
  188. data/lib/libyajl2/version.rb +3 -0
  189. data/libyajl2.gemspec +29 -0
  190. data/spec/ffi_spec.rb +20 -0
  191. data/spec/path_spec.rb +15 -0
  192. data/spec/spec_helper.rb +13 -0
  193. metadata +296 -0
@@ -0,0 +1,94 @@
1
+ #!/bin/sh
2
+
3
+ ECHO=`which echo`
4
+
5
+ DIFF_FLAGS="-u"
6
+ case "$(uname)" in
7
+ *W32*)
8
+ DIFF_FLAGS="-wu"
9
+ ;;
10
+ esac
11
+
12
+ if [ -z "$testBin" ]; then
13
+ testBin="$1"
14
+ fi
15
+
16
+ # find test binary on both platforms. allow the caller to force a
17
+ # particular test binary (useful for non-cmake build systems).
18
+ if [ -z "$testBin" ]; then
19
+ testBin="../build/test/parsing/Release/yajl_test.exe"
20
+ if [ ! -x $testBin ] ; then
21
+ testBin="../build/test/parsing/Debug/yajl_test.exe"
22
+ if [ ! -x $testBin ] ; then
23
+ testBin="../build/test/parsing/yajl_test"
24
+ if [ ! -x $testBin ] ; then
25
+ ${ECHO} "cannot execute test binary: '$testBin'"
26
+ exit 1;
27
+ fi
28
+ fi
29
+ fi
30
+ fi
31
+
32
+ ${ECHO} "using test binary: $testBin"
33
+
34
+ testBinShort=`basename $testBin`
35
+
36
+ testsSucceeded=0
37
+ testsTotal=0
38
+
39
+ for file in cases/*.json ; do
40
+ allowComments=""
41
+ allowGarbage=""
42
+ allowMultiple=""
43
+ allowPartials=""
44
+
45
+ # if the filename starts with dc_, we disallow comments for this test
46
+ case $(basename $file) in
47
+ ac_*)
48
+ allowComments="-c "
49
+ ;;
50
+ ag_*)
51
+ allowGarbage="-g "
52
+ ;;
53
+ am_*)
54
+ allowMultiple="-m ";
55
+ ;;
56
+ ap_*)
57
+ allowPartials="-p ";
58
+ ;;
59
+ esac
60
+ fileShort=`basename $file`
61
+ testName=`echo $fileShort | sed -e 's/\.json$//'`
62
+
63
+ ${ECHO} -n " test ($testName): "
64
+ iter=1
65
+ success="SUCCESS"
66
+
67
+ # ${ECHO} -n "$testBinShort $allowPartials$allowComments$allowGarbage$allowMultiple-b $iter < $fileShort > ${fileShort}.test : "
68
+ # parse with a read buffer size ranging from 1-31 to stress stream parsing
69
+ while [ $iter -lt 32 ] && [ $success = "SUCCESS" ] ; do
70
+ $testBin $allowPartials $allowComments $allowGarbage $allowMultiple -b $iter < $file > ${file}.test 2>&1
71
+ diff ${DIFF_FLAGS} ${file}.gold ${file}.test > ${file}.out
72
+ if [ $? -eq 0 ] ; then
73
+ if [ $iter -eq 31 ] ; then testsSucceeded=$(( $testsSucceeded + 1 )) ; fi
74
+ else
75
+ success="FAILURE"
76
+ iter=32
77
+ ${ECHO}
78
+ cat ${file}.out
79
+ fi
80
+ iter=$(( iter + 1 ))
81
+ rm ${file}.test ${file}.out
82
+ done
83
+
84
+ ${ECHO} $success
85
+ testsTotal=$(( testsTotal + 1 ))
86
+ done
87
+
88
+ ${ECHO} $testsSucceeded/$testsTotal tests successful
89
+
90
+ if [ $testsSucceeded != $testsTotal ] ; then
91
+ exit 1
92
+ fi
93
+
94
+ exit 0
@@ -0,0 +1,291 @@
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
+
17
+ #include <yajl/yajl_parse.h>
18
+ #include <yajl/yajl_gen.h>
19
+
20
+ #include <stdio.h>
21
+ #include <stdlib.h>
22
+ #include <string.h>
23
+
24
+ #include <assert.h>
25
+
26
+ /* memory debugging routines */
27
+ typedef struct
28
+ {
29
+ unsigned int numFrees;
30
+ unsigned int numMallocs;
31
+ /* XXX: we really need a hash table here with per-allocation
32
+ * information */
33
+ } yajlTestMemoryContext;
34
+
35
+ /* cast void * into context */
36
+ #define TEST_CTX(vptr) ((yajlTestMemoryContext *) (vptr))
37
+
38
+ static void yajlTestFree(void * ctx, void * ptr)
39
+ {
40
+ assert(ptr != NULL);
41
+ TEST_CTX(ctx)->numFrees++;
42
+ free(ptr);
43
+ }
44
+
45
+ static void * yajlTestMalloc(void * ctx, size_t sz)
46
+ {
47
+ assert(sz != 0);
48
+ TEST_CTX(ctx)->numMallocs++;
49
+ return malloc(sz);
50
+ }
51
+
52
+ static void * yajlTestRealloc(void * ctx, void * ptr, size_t sz)
53
+ {
54
+ if (ptr == NULL) {
55
+ assert(sz != 0);
56
+ TEST_CTX(ctx)->numMallocs++;
57
+ } else if (sz == 0) {
58
+ TEST_CTX(ctx)->numFrees++;
59
+ }
60
+
61
+ return realloc(ptr, sz);
62
+ }
63
+
64
+
65
+ /* begin parsing callback routines */
66
+ #define BUF_SIZE 2048
67
+
68
+ static int test_yajl_null(void *ctx)
69
+ {
70
+ printf("null\n");
71
+ return 1;
72
+ }
73
+
74
+ static int test_yajl_boolean(void * ctx, int boolVal)
75
+ {
76
+ printf("bool: %s\n", boolVal ? "true" : "false");
77
+ return 1;
78
+ }
79
+
80
+ static int test_yajl_integer(void *ctx, long long integerVal)
81
+ {
82
+ printf("integer: %lld\n", integerVal);
83
+ return 1;
84
+ }
85
+
86
+ static int test_yajl_double(void *ctx, double doubleVal)
87
+ {
88
+ printf("double: %g\n", doubleVal);
89
+ return 1;
90
+ }
91
+
92
+ static int test_yajl_string(void *ctx, const unsigned char * stringVal,
93
+ size_t stringLen)
94
+ {
95
+ printf("string: '");
96
+ fwrite(stringVal, 1, stringLen, stdout);
97
+ printf("'\n");
98
+ return 1;
99
+ }
100
+
101
+ static int test_yajl_map_key(void *ctx, const unsigned char * stringVal,
102
+ size_t stringLen)
103
+ {
104
+ char * str = (char *) malloc(stringLen + 1);
105
+ str[stringLen] = 0;
106
+ memcpy(str, stringVal, stringLen);
107
+ printf("key: '%s'\n", str);
108
+ free(str);
109
+ return 1;
110
+ }
111
+
112
+ static int test_yajl_start_map(void *ctx)
113
+ {
114
+ printf("map open '{'\n");
115
+ return 1;
116
+ }
117
+
118
+
119
+ static int test_yajl_end_map(void *ctx)
120
+ {
121
+ printf("map close '}'\n");
122
+ return 1;
123
+ }
124
+
125
+ static int test_yajl_start_array(void *ctx)
126
+ {
127
+ printf("array open '['\n");
128
+ return 1;
129
+ }
130
+
131
+ static int test_yajl_end_array(void *ctx)
132
+ {
133
+ printf("array close ']'\n");
134
+ return 1;
135
+ }
136
+
137
+ static yajl_callbacks callbacks = {
138
+ test_yajl_null,
139
+ test_yajl_boolean,
140
+ test_yajl_integer,
141
+ test_yajl_double,
142
+ NULL,
143
+ test_yajl_string,
144
+ test_yajl_start_map,
145
+ test_yajl_map_key,
146
+ test_yajl_end_map,
147
+ test_yajl_start_array,
148
+ test_yajl_end_array
149
+ };
150
+
151
+ static void usage(const char * progname)
152
+ {
153
+ fprintf(stderr,
154
+ "usage: %s [options]\n"
155
+ "Parse input from stdin as JSON and ouput parsing details "
156
+ "to stdout\n"
157
+ " -b set the read buffer size\n"
158
+ " -c allow comments\n"
159
+ " -g allow *g*arbage after valid JSON text\n"
160
+ " -m allows the parser to consume multiple JSON values\n"
161
+ " from a single string separated by whitespace\n"
162
+ " -p partial JSON documents should not cause errors\n",
163
+ progname);
164
+ exit(1);
165
+ }
166
+
167
+ int
168
+ main(int argc, char ** argv)
169
+ {
170
+ yajl_handle hand;
171
+ const char * fileName = NULL;
172
+ static unsigned char * fileData = NULL;
173
+ FILE *file;
174
+ size_t bufSize = BUF_SIZE;
175
+ yajl_status stat;
176
+ size_t rd;
177
+ int i, j;
178
+
179
+ /* memory allocation debugging: allocate a structure which collects
180
+ * statistics */
181
+ yajlTestMemoryContext memCtx = { 0,0 };
182
+
183
+ /* memory allocation debugging: allocate a structure which holds
184
+ * allocation routines */
185
+ yajl_alloc_funcs allocFuncs = {
186
+ yajlTestMalloc,
187
+ yajlTestRealloc,
188
+ yajlTestFree,
189
+ (void *) NULL
190
+ };
191
+
192
+ allocFuncs.ctx = (void *) &memCtx;
193
+
194
+ /* allocate the parser */
195
+ hand = yajl_alloc(&callbacks, &allocFuncs, NULL);
196
+
197
+ /* check arguments. We expect exactly one! */
198
+ for (i=1;i<argc;i++) {
199
+ if (!strcmp("-c", argv[i])) {
200
+ yajl_config(hand, yajl_allow_comments, 1);
201
+ } else if (!strcmp("-b", argv[i])) {
202
+ if (++i >= argc) usage(argv[0]);
203
+
204
+ /* validate integer */
205
+ for (j=0;j<(int)strlen(argv[i]);j++) {
206
+ if (argv[i][j] <= '9' && argv[i][j] >= '0') continue;
207
+ fprintf(stderr, "-b requires an integer argument. '%s' "
208
+ "is invalid\n", argv[i]);
209
+ usage(argv[0]);
210
+ }
211
+
212
+ bufSize = atoi(argv[i]);
213
+ if (!bufSize) {
214
+ fprintf(stderr, "%zu is an invalid buffer size\n",
215
+ bufSize);
216
+ }
217
+ } else if (!strcmp("-g", argv[i])) {
218
+ yajl_config(hand, yajl_allow_trailing_garbage, 1);
219
+ } else if (!strcmp("-m", argv[i])) {
220
+ yajl_config(hand, yajl_allow_multiple_values, 1);
221
+ } else if (!strcmp("-p", argv[i])) {
222
+ yajl_config(hand, yajl_allow_partial_values, 1);
223
+ } else {
224
+ fileName = argv[i];
225
+ break;
226
+ }
227
+ }
228
+
229
+ fileData = (unsigned char *) malloc(bufSize);
230
+
231
+ if (fileData == NULL) {
232
+ fprintf(stderr,
233
+ "failed to allocate read buffer of %zu bytes, exiting.",
234
+ bufSize);
235
+ yajl_free(hand);
236
+ exit(2);
237
+ }
238
+
239
+ if (fileName)
240
+ {
241
+ file = fopen(fileName, "r");
242
+ }
243
+ else
244
+ {
245
+ file = stdin;
246
+ }
247
+ for (;;) {
248
+ rd = fread((void *) fileData, 1, bufSize, file);
249
+
250
+ if (rd == 0) {
251
+ if (!feof(stdin)) {
252
+ fprintf(stderr, "error reading from '%s'\n", fileName);
253
+ }
254
+ break;
255
+ }
256
+ /* read file data, now pass to parser */
257
+ stat = yajl_parse(hand, fileData, rd);
258
+
259
+ if (stat != yajl_status_ok) break;
260
+ }
261
+
262
+ stat = yajl_complete_parse(hand);
263
+ if (stat != yajl_status_ok)
264
+ {
265
+ unsigned char * str = yajl_get_error(hand, 0, fileData, rd);
266
+ fflush(stdout);
267
+ fprintf(stderr, "%s", (char *) str);
268
+ yajl_free_error(hand, str);
269
+ }
270
+
271
+ yajl_free(hand);
272
+ free(fileData);
273
+
274
+ if (fileName)
275
+ {
276
+ fclose(file);
277
+ }
278
+ /* finally, print out some memory statistics */
279
+
280
+ /* (lth) only print leaks here, as allocations and frees may vary depending
281
+ * on read buffer size, causing false failures.
282
+ *
283
+ * printf("allocations:\t%u\n", memCtx.numMallocs);
284
+ * printf("frees:\t\t%u\n", memCtx.numFrees);
285
+ */
286
+ fflush(stderr);
287
+ fflush(stdout);
288
+ printf("memory leaks:\t%u\n", memCtx.numMallocs - memCtx.numFrees);
289
+
290
+ return 0;
291
+ }
@@ -0,0 +1,37 @@
1
+ # Copyright (c) 2007-2014, Lloyd Hilaiel <me@lloyd.io>
2
+ #
3
+ # Permission to use, copy, modify, and/or distribute this software for any
4
+ # purpose with or without fee is hereby granted, provided that the above
5
+ # copyright notice and this permission notice appear in all copies.
6
+ #
7
+ # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+ # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
+ # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
+ # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
+
15
+ # set up some paths
16
+ SET (binDir ${CMAKE_CURRENT_BINARY_DIR}/../${YAJL_DIST_NAME}/bin)
17
+
18
+ # create some directories
19
+ FILE(MAKE_DIRECTORY ${binDir})
20
+
21
+ SET (SRCS json_verify.c)
22
+
23
+ # use the library we build, duh.
24
+ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../${YAJL_DIST_NAME}/include)
25
+ LINK_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}/../${YAJL_DIST_NAME}/lib)
26
+
27
+ ADD_EXECUTABLE(json_verify ${SRCS})
28
+
29
+ TARGET_LINK_LIBRARIES(json_verify yajl_s)
30
+
31
+ # copy in the binary
32
+ GET_TARGET_PROPERTY(binPath json_verify LOCATION)
33
+
34
+ ADD_CUSTOM_COMMAND(TARGET json_verify POST_BUILD
35
+ COMMAND ${CMAKE_COMMAND} -E copy_if_different ${binPath} ${binDir})
36
+
37
+ INSTALL(TARGETS json_verify RUNTIME DESTINATION bin)
@@ -0,0 +1,120 @@
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
+
17
+ #include <yajl/yajl_parse.h>
18
+
19
+ #include <stdio.h>
20
+ #include <stdlib.h>
21
+ #include <string.h>
22
+
23
+ static void
24
+ usage(const char * progname)
25
+ {
26
+ fprintf(stderr, "%s: validate json from stdin\n"
27
+ "usage: json_verify [options]\n"
28
+ " -c allow comments\n"
29
+ " -q quiet mode\n"
30
+ " -s verify a stream of multiple json entities\n"
31
+ " -u allow invalid utf8 inside strings\n",
32
+ progname);
33
+ exit(1);
34
+ }
35
+
36
+ int
37
+ main(int argc, char ** argv)
38
+ {
39
+ yajl_status stat;
40
+ size_t rd;
41
+ yajl_handle hand;
42
+ static unsigned char fileData[65536];
43
+ int quiet = 0;
44
+ int retval = 0;
45
+ int a = 1;
46
+
47
+ /* allocate a parser */
48
+ hand = yajl_alloc(NULL, NULL, NULL);
49
+
50
+ /* check arguments.*/
51
+ while ((a < argc) && (argv[a][0] == '-') && (strlen(argv[a]) > 1)) {
52
+ unsigned int i;
53
+ for ( i=1; i < strlen(argv[a]); i++) {
54
+ switch (argv[a][i]) {
55
+ case 'q':
56
+ quiet = 1;
57
+ break;
58
+ case 'c':
59
+ yajl_config(hand, yajl_allow_comments, 1);
60
+ break;
61
+ case 'u':
62
+ yajl_config(hand, yajl_dont_validate_strings, 1);
63
+ break;
64
+ case 's':
65
+ yajl_config(hand, yajl_allow_multiple_values, 1);
66
+ break;
67
+ default:
68
+ fprintf(stderr, "unrecognized option: '%c'\n\n", argv[a][i]);
69
+ usage(argv[0]);
70
+ }
71
+ }
72
+ ++a;
73
+ }
74
+ if (a < argc) {
75
+ usage(argv[0]);
76
+ }
77
+
78
+ for (;;) {
79
+ rd = fread((void *) fileData, 1, sizeof(fileData) - 1, stdin);
80
+
81
+ retval = 0;
82
+
83
+ if (rd == 0) {
84
+ if (!feof(stdin)) {
85
+ if (!quiet) {
86
+ fprintf(stderr, "error encountered on file read\n");
87
+ }
88
+ retval = 1;
89
+ }
90
+ break;
91
+ }
92
+ fileData[rd] = 0;
93
+
94
+ /* read file data, pass to parser */
95
+ stat = yajl_parse(hand, fileData, rd);
96
+
97
+ if (stat != yajl_status_ok) break;
98
+ }
99
+
100
+ /* parse any remaining buffered data */
101
+ stat = yajl_complete_parse(hand);
102
+
103
+ if (stat != yajl_status_ok)
104
+ {
105
+ if (!quiet) {
106
+ unsigned char * str = yajl_get_error(hand, 1, fileData, rd);
107
+ fprintf(stderr, "%s", (const char *) str);
108
+ yajl_free_error(hand, str);
109
+ }
110
+ retval = 1;
111
+ }
112
+
113
+ yajl_free(hand);
114
+
115
+ if (!quiet) {
116
+ printf("JSON is %s\n", retval ? "invalid" : "valid");
117
+ }
118
+
119
+ return retval;
120
+ }