hamlit 1.7.2 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (283) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -3
  3. data/.gitmodules +3 -0
  4. data/.travis.yml +25 -37
  5. data/CHANGELOG.md +18 -0
  6. data/Gemfile +16 -0
  7. data/LICENSE.txt +23 -2
  8. data/README.md +106 -48
  9. data/REFERENCE.md +222 -0
  10. data/Rakefile +77 -19
  11. data/benchmark/boolean_attribute.haml +6 -0
  12. data/benchmark/class_attribute.haml +5 -0
  13. data/benchmark/common_attribute.haml +3 -0
  14. data/benchmark/data_attribute.haml +4 -0
  15. data/benchmark/dynamic_attributes/boolean_attribute.haml +4 -0
  16. data/benchmark/dynamic_attributes/class_attribute.haml +4 -0
  17. data/benchmark/dynamic_attributes/common_attribute.haml +2 -0
  18. data/benchmark/dynamic_attributes/data_attribute.haml +2 -0
  19. data/benchmark/dynamic_attributes/id_attribute.haml +2 -0
  20. data/benchmark/etc/attribute_builder.haml +5 -0
  21. data/benchmark/etc/real_sample.haml +888 -0
  22. data/benchmark/etc/real_sample.rb +11 -0
  23. data/benchmark/etc/static_analyzer.haml +1 -0
  24. data/benchmark/etc/tags.haml +3 -0
  25. data/benchmark/ext/build_data.rb +15 -0
  26. data/benchmark/ext/build_id.rb +13 -0
  27. data/benchmark/id_attribute.haml +3 -0
  28. data/benchmark/plain.haml +4 -0
  29. data/benchmark/script.haml +4 -0
  30. data/benchmark/slim/LICENSE +21 -0
  31. data/{benchmarks → benchmark/slim}/context.rb +2 -4
  32. data/benchmark/slim/run-benchmarks.rb +94 -0
  33. data/{benchmarks → benchmark/slim}/view.erb +3 -3
  34. data/{benchmarks → benchmark/slim}/view.haml +0 -0
  35. data/{benchmarks/view.escaped.slim → benchmark/slim/view.slim} +1 -1
  36. data/benchmark/string_interpolation.haml +2 -0
  37. data/benchmark/utils/benchmark_ips_extension.rb +43 -0
  38. data/bin/bench +85 -0
  39. data/bin/clone +14 -0
  40. data/bin/console +11 -0
  41. data/bin/lineprof +48 -0
  42. data/bin/ruby +3 -0
  43. data/bin/setup +7 -0
  44. data/bin/stackprof +27 -0
  45. data/{test → bin/test} +6 -10
  46. data/{bin → exe}/hamlit +0 -0
  47. data/ext/hamlit/extconf.rb +14 -0
  48. data/ext/hamlit/hamlit.c +512 -0
  49. data/ext/hamlit/houdini/.gitignore +3 -0
  50. data/ext/hamlit/houdini/COPYING +7 -0
  51. data/ext/hamlit/houdini/Makefile +79 -0
  52. data/ext/hamlit/houdini/README.md +59 -0
  53. data/ext/hamlit/houdini/buffer.c +249 -0
  54. data/ext/hamlit/houdini/buffer.h +113 -0
  55. data/ext/hamlit/houdini/houdini.h +46 -0
  56. data/ext/hamlit/houdini/houdini_href_e.c +115 -0
  57. data/ext/hamlit/houdini/houdini_html_e.c +90 -0
  58. data/ext/hamlit/houdini/houdini_html_u.c +122 -0
  59. data/ext/hamlit/houdini/houdini_js_e.c +90 -0
  60. data/ext/hamlit/houdini/houdini_js_u.c +60 -0
  61. data/ext/hamlit/houdini/houdini_uri_e.c +107 -0
  62. data/ext/hamlit/houdini/houdini_uri_u.c +68 -0
  63. data/ext/hamlit/houdini/houdini_xml_e.c +136 -0
  64. data/ext/hamlit/houdini/html_unescape.gperf +258 -0
  65. data/ext/hamlit/houdini/html_unescape.h +754 -0
  66. data/ext/hamlit/houdini/tools/build_table.py +13 -0
  67. data/ext/hamlit/houdini/tools/build_tables.c +51 -0
  68. data/ext/hamlit/houdini/tools/wikipedia_table.txt +2025 -0
  69. data/hamlit.gemspec +30 -31
  70. data/lib/hamlit.rb +3 -1
  71. data/lib/hamlit/attribute_builder.rb +12 -0
  72. data/lib/hamlit/cli.rb +44 -43
  73. data/lib/hamlit/compiler.rb +92 -16
  74. data/lib/hamlit/compiler/attribute_compiler.rb +148 -0
  75. data/lib/hamlit/compiler/children_compiler.rb +111 -0
  76. data/lib/hamlit/compiler/comment_compiler.rb +36 -0
  77. data/lib/hamlit/compiler/doctype_compiler.rb +45 -0
  78. data/lib/hamlit/compiler/script_compiler.rb +97 -0
  79. data/lib/hamlit/compiler/silent_script_compiler.rb +24 -0
  80. data/lib/hamlit/compiler/tag_compiler.rb +69 -0
  81. data/lib/hamlit/engine.rb +12 -7
  82. data/lib/hamlit/error.rb +14 -0
  83. data/lib/hamlit/escapable.rb +12 -0
  84. data/lib/hamlit/filters.rb +65 -0
  85. data/lib/hamlit/filters/base.rb +4 -62
  86. data/lib/hamlit/filters/coffee.rb +9 -7
  87. data/lib/hamlit/filters/css.rb +25 -8
  88. data/lib/hamlit/filters/erb.rb +4 -6
  89. data/lib/hamlit/filters/escaped.rb +11 -9
  90. data/lib/hamlit/filters/javascript.rb +25 -8
  91. data/lib/hamlit/filters/less.rb +9 -7
  92. data/lib/hamlit/filters/markdown.rb +5 -6
  93. data/lib/hamlit/filters/plain.rb +11 -15
  94. data/lib/hamlit/filters/preserve.rb +15 -5
  95. data/lib/hamlit/filters/ruby.rb +3 -5
  96. data/lib/hamlit/filters/sass.rb +9 -7
  97. data/lib/hamlit/filters/scss.rb +9 -7
  98. data/lib/hamlit/filters/text_base.rb +24 -0
  99. data/lib/hamlit/filters/tilt_base.rb +47 -0
  100. data/lib/hamlit/hash_parser.rb +107 -0
  101. data/lib/hamlit/html.rb +9 -6
  102. data/lib/hamlit/identity.rb +12 -0
  103. data/lib/hamlit/object_ref.rb +29 -0
  104. data/lib/hamlit/parser.rb +25 -142
  105. data/lib/hamlit/parser/MIT-LICENSE +20 -0
  106. data/lib/hamlit/parser/README.md +28 -0
  107. data/lib/hamlit/parser/haml_buffer.rb +348 -0
  108. data/lib/hamlit/parser/haml_compiler.rb +553 -0
  109. data/lib/hamlit/parser/haml_error.rb +61 -0
  110. data/lib/hamlit/parser/haml_helpers.rb +727 -0
  111. data/lib/hamlit/parser/haml_options.rb +286 -0
  112. data/lib/hamlit/parser/haml_parser.rb +801 -0
  113. data/lib/hamlit/parser/haml_util.rb +283 -0
  114. data/lib/hamlit/parser/haml_xss_mods.rb +109 -0
  115. data/lib/hamlit/{helpers.rb → rails_helpers.rb} +2 -7
  116. data/lib/hamlit/rails_template.rb +30 -0
  117. data/lib/hamlit/railtie.rb +1 -12
  118. data/lib/hamlit/ruby_expression.rb +31 -0
  119. data/lib/hamlit/static_analyzer.rb +49 -0
  120. data/lib/hamlit/string_interpolation.rb +69 -0
  121. data/lib/hamlit/template.rb +8 -0
  122. data/lib/hamlit/utils.rb +9 -0
  123. data/lib/hamlit/version.rb +1 -1
  124. metadata +116 -324
  125. data/.rspec +0 -2
  126. data/benchmarks/benchmark.rb +0 -110
  127. data/benchmarks/view.slim +0 -17
  128. data/doc/README.md +0 -19
  129. data/doc/engine/indent.md +0 -48
  130. data/doc/engine/new_attribute.md +0 -77
  131. data/doc/engine/old_attributes.md +0 -198
  132. data/doc/engine/silent_script.md +0 -97
  133. data/doc/engine/tag.md +0 -48
  134. data/doc/engine/text.md +0 -64
  135. data/doc/faml/README.md +0 -16
  136. data/doc/faml/engine/indent.md +0 -48
  137. data/doc/faml/engine/old_attributes.md +0 -111
  138. data/doc/faml/engine/silent_script.md +0 -97
  139. data/doc/faml/engine/text.md +0 -59
  140. data/doc/faml/filters/erb.md +0 -24
  141. data/doc/faml/filters/javascript.md +0 -27
  142. data/doc/faml/filters/less.md +0 -57
  143. data/doc/faml/filters/plain.md +0 -25
  144. data/doc/filters/erb.md +0 -31
  145. data/doc/filters/javascript.md +0 -83
  146. data/doc/filters/less.md +0 -57
  147. data/doc/filters/markdown.md +0 -31
  148. data/doc/filters/plain.md +0 -25
  149. data/doc/haml/README.md +0 -15
  150. data/doc/haml/engine/new_attribute.md +0 -77
  151. data/doc/haml/engine/old_attributes.md +0 -142
  152. data/doc/haml/engine/tag.md +0 -48
  153. data/doc/haml/engine/text.md +0 -29
  154. data/doc/haml/filters/erb.md +0 -26
  155. data/doc/haml/filters/javascript.md +0 -76
  156. data/doc/haml/filters/markdown.md +0 -31
  157. data/lib/hamlit/attribute.rb +0 -78
  158. data/lib/hamlit/compilers/attributes.rb +0 -108
  159. data/lib/hamlit/compilers/comment.rb +0 -13
  160. data/lib/hamlit/compilers/doctype.rb +0 -39
  161. data/lib/hamlit/compilers/filter.rb +0 -53
  162. data/lib/hamlit/compilers/new_attribute.rb +0 -115
  163. data/lib/hamlit/compilers/old_attribute.rb +0 -241
  164. data/lib/hamlit/compilers/runtime_attribute.rb +0 -58
  165. data/lib/hamlit/compilers/script.rb +0 -31
  166. data/lib/hamlit/compilers/strip.rb +0 -19
  167. data/lib/hamlit/compilers/text.rb +0 -111
  168. data/lib/hamlit/concerns/attribute_builder.rb +0 -22
  169. data/lib/hamlit/concerns/balanceable.rb +0 -68
  170. data/lib/hamlit/concerns/deprecation.rb +0 -20
  171. data/lib/hamlit/concerns/error.rb +0 -31
  172. data/lib/hamlit/concerns/escapable.rb +0 -17
  173. data/lib/hamlit/concerns/included.rb +0 -28
  174. data/lib/hamlit/concerns/indentable.rb +0 -117
  175. data/lib/hamlit/concerns/lexable.rb +0 -32
  176. data/lib/hamlit/concerns/line_reader.rb +0 -62
  177. data/lib/hamlit/concerns/registerable.rb +0 -24
  178. data/lib/hamlit/concerns/string_interpolation.rb +0 -48
  179. data/lib/hamlit/concerns/whitespace.rb +0 -91
  180. data/lib/hamlit/filters/tilt.rb +0 -41
  181. data/lib/hamlit/parsers/attribute.rb +0 -71
  182. data/lib/hamlit/parsers/comment.rb +0 -30
  183. data/lib/hamlit/parsers/doctype.rb +0 -18
  184. data/lib/hamlit/parsers/filter.rb +0 -18
  185. data/lib/hamlit/parsers/multiline.rb +0 -58
  186. data/lib/hamlit/parsers/script.rb +0 -126
  187. data/lib/hamlit/parsers/tag.rb +0 -83
  188. data/lib/hamlit/parsers/text.rb +0 -28
  189. data/lib/hamlit/temple.rb +0 -9
  190. data/release +0 -6
  191. data/spec/Rakefile +0 -72
  192. data/spec/hamlit/engine/comment_spec.rb +0 -56
  193. data/spec/hamlit/engine/doctype_spec.rb +0 -19
  194. data/spec/hamlit/engine/error_spec.rb +0 -135
  195. data/spec/hamlit/engine/indent_spec.rb +0 -42
  196. data/spec/hamlit/engine/multiline_spec.rb +0 -44
  197. data/spec/hamlit/engine/new_attribute_spec.rb +0 -110
  198. data/spec/hamlit/engine/old_attributes_spec.rb +0 -404
  199. data/spec/hamlit/engine/script_spec.rb +0 -116
  200. data/spec/hamlit/engine/silent_script_spec.rb +0 -213
  201. data/spec/hamlit/engine/tag_spec.rb +0 -295
  202. data/spec/hamlit/engine/text_spec.rb +0 -239
  203. data/spec/hamlit/engine_spec.rb +0 -58
  204. data/spec/hamlit/filters/coffee_spec.rb +0 -60
  205. data/spec/hamlit/filters/css_spec.rb +0 -33
  206. data/spec/hamlit/filters/erb_spec.rb +0 -16
  207. data/spec/hamlit/filters/javascript_spec.rb +0 -82
  208. data/spec/hamlit/filters/less_spec.rb +0 -37
  209. data/spec/hamlit/filters/markdown_spec.rb +0 -30
  210. data/spec/hamlit/filters/plain_spec.rb +0 -15
  211. data/spec/hamlit/filters/ruby_spec.rb +0 -24
  212. data/spec/hamlit/filters/sass_spec.rb +0 -33
  213. data/spec/hamlit/filters/scss_spec.rb +0 -37
  214. data/spec/hamlit/haml_spec.rb +0 -910
  215. data/spec/rails/.gitignore +0 -18
  216. data/spec/rails/.rspec +0 -2
  217. data/spec/rails/Gemfile +0 -19
  218. data/spec/rails/README.rdoc +0 -28
  219. data/spec/rails/Rakefile +0 -6
  220. data/spec/rails/app/assets/images/.keep +0 -0
  221. data/spec/rails/app/assets/javascripts/application.js +0 -15
  222. data/spec/rails/app/assets/stylesheets/application.css +0 -15
  223. data/spec/rails/app/controllers/application_controller.rb +0 -8
  224. data/spec/rails/app/controllers/concerns/.keep +0 -0
  225. data/spec/rails/app/controllers/users_controller.rb +0 -23
  226. data/spec/rails/app/helpers/application_helper.rb +0 -2
  227. data/spec/rails/app/mailers/.keep +0 -0
  228. data/spec/rails/app/models/.keep +0 -0
  229. data/spec/rails/app/models/concerns/.keep +0 -0
  230. data/spec/rails/app/views/application/index.html.haml +0 -18
  231. data/spec/rails/app/views/layouts/application.html.haml +0 -12
  232. data/spec/rails/app/views/users/capture.html.haml +0 -5
  233. data/spec/rails/app/views/users/capture_haml.html.haml +0 -5
  234. data/spec/rails/app/views/users/form.html.haml +0 -2
  235. data/spec/rails/app/views/users/helpers.html.haml +0 -10
  236. data/spec/rails/app/views/users/index.html.haml +0 -9
  237. data/spec/rails/app/views/users/inline.html.haml +0 -6
  238. data/spec/rails/app/views/users/old_attributes.html.haml +0 -5
  239. data/spec/rails/app/views/users/safe_buffer.html.haml +0 -4
  240. data/spec/rails/app/views/users/whitespace.html.haml +0 -4
  241. data/spec/rails/bin/bundle +0 -3
  242. data/spec/rails/bin/rails +0 -8
  243. data/spec/rails/bin/rake +0 -8
  244. data/spec/rails/bin/setup +0 -29
  245. data/spec/rails/bin/spring +0 -15
  246. data/spec/rails/config.ru +0 -4
  247. data/spec/rails/config/application.rb +0 -34
  248. data/spec/rails/config/boot.rb +0 -3
  249. data/spec/rails/config/database.yml +0 -25
  250. data/spec/rails/config/environment.rb +0 -5
  251. data/spec/rails/config/environments/development.rb +0 -41
  252. data/spec/rails/config/environments/production.rb +0 -79
  253. data/spec/rails/config/environments/test.rb +0 -42
  254. data/spec/rails/config/initializers/assets.rb +0 -11
  255. data/spec/rails/config/initializers/backtrace_silencers.rb +0 -7
  256. data/spec/rails/config/initializers/cookies_serializer.rb +0 -3
  257. data/spec/rails/config/initializers/filter_parameter_logging.rb +0 -4
  258. data/spec/rails/config/initializers/inflections.rb +0 -16
  259. data/spec/rails/config/initializers/mime_types.rb +0 -4
  260. data/spec/rails/config/initializers/session_store.rb +0 -3
  261. data/spec/rails/config/initializers/wrap_parameters.rb +0 -14
  262. data/spec/rails/config/locales/en.yml +0 -24
  263. data/spec/rails/config/routes.rb +0 -16
  264. data/spec/rails/config/secrets.yml +0 -22
  265. data/spec/rails/db/schema.rb +0 -16
  266. data/spec/rails/db/seeds.rb +0 -7
  267. data/spec/rails/lib/assets/.keep +0 -0
  268. data/spec/rails/lib/tasks/.keep +0 -0
  269. data/spec/rails/log/.keep +0 -0
  270. data/spec/rails/public/404.html +0 -67
  271. data/spec/rails/public/422.html +0 -67
  272. data/spec/rails/public/500.html +0 -66
  273. data/spec/rails/public/favicon.ico +0 -0
  274. data/spec/rails/public/robots.txt +0 -5
  275. data/spec/rails/spec/hamlit_spec.rb +0 -123
  276. data/spec/rails/spec/rails_helper.rb +0 -56
  277. data/spec/rails/spec/spec_helper.rb +0 -91
  278. data/spec/rails/vendor/assets/javascripts/.keep +0 -0
  279. data/spec/rails/vendor/assets/stylesheets/.keep +0 -0
  280. data/spec/spec_helper.rb +0 -36
  281. data/spec/spec_helper/document_generator.rb +0 -93
  282. data/spec/spec_helper/render_helper.rb +0 -120
  283. data/spec/spec_helper/test_case.rb +0 -55
@@ -0,0 +1,3 @@
1
+ *.[ao]
2
+ *.d
3
+ *~
@@ -0,0 +1,7 @@
1
+ Copyright (C) 2012 Vicent Martí
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,79 @@
1
+ # GNU Makefile
2
+ # Copyright (C) 2012 Przemyslaw Pawelczyk <przemoc@gmail.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a
5
+ # copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included
13
+ # in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
+ # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ SRCS := buffer.c houdini_href_e.c houdini_html_e.c houdini_html_u.c houdini_js_e.c houdini_js_u.c houdini_uri_e.c houdini_uri_u.c houdini_xml_e.c
24
+ OBJS := $(addsuffix .o,$(basename $(SRCS)))
25
+ DEPS := $(addsuffix .d,$(basename $(SRCS)))
26
+ NAME := houdini
27
+
28
+ SRCDIR := $(dir $(lastword $(MAKEFILE_LIST)))
29
+ OBJDIR ?= ./
30
+ OBJS := $(addprefix $(OBJDIR),$(OBJS))
31
+ SLIB := lib$(NAME).a
32
+
33
+ vpath %.c $(SRCDIR)
34
+ vpath %.h $(SRCDIR)
35
+
36
+ AR ?= ar
37
+ CC ?= gcc
38
+ GPERF ?= gperf
39
+ SED ?= sed
40
+
41
+ CFLAGS ?= -O2
42
+ # -Wno-missing-field-initializers - gperf's header doesn't pass on -Wextra
43
+ MANDATORY_FLAGS := -Wall -Wextra -Wno-missing-field-initializers
44
+ override CFLAGS := $(MANDATORY_FLAGS) $(CFLAGS)
45
+
46
+ .PHONY: all clean objects package
47
+
48
+ all: package
49
+
50
+ objects: $(OBJS)
51
+ package: $(SLIB)
52
+
53
+ $(SLIB): $(OBJS)
54
+ $(AR) rcs $@ $^
55
+
56
+ $(OBJS): | $(OBJDIR)
57
+
58
+ $(OBJDIR):
59
+ mkdir -p $@
60
+
61
+ $(OBJDIR)%.o: %.c
62
+ $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<
63
+
64
+ %.d: %.c
65
+ @set -e; \
66
+ $(RM) $@; \
67
+ $(CC) -MM $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) $< >$@.tmp; \
68
+ $(SED) 's,\($*\)\.o[ :]*,$$(OBJDIR)\1.o $@: ,g' <$@.tmp >$@; \
69
+ $(RM) $@.tmp
70
+
71
+ html_unescape.h: html_unescape.gperf
72
+ $(GPERF) -t -N find_entity -H hash_entity -K entity -C -l --null-strings -m100 $< >$@
73
+
74
+ clean:
75
+ $(RM) $(SLIB) $(OBJS) $(DEPS)
76
+
77
+ ifneq ($(MAKECMDGOALS),clean)
78
+ -include $(DEPS)
79
+ endif
@@ -0,0 +1,59 @@
1
+ Houdini - The Escapist
2
+ ======================
3
+
4
+ Houdini doesn't quite qualify as a library. In fact, I didn't even bother
5
+ to write a Makefile (well, others did). It's zero-dependency and modular.
6
+ Just stick the files you need in your project. Or go with all of them
7
+ (e.g. via git submodule) and use the static library that is built by
8
+ default when GNU make is invoked. Now you can freely escape some shit.
9
+
10
+ Houdini is a simple API for escaping text for the web. And unescaping it.
11
+ But that kind of breaks the joke in the name so nevermind.
12
+
13
+ - HTML escaping follows the OWASP suggestion. All other entities are left
14
+ as-is.
15
+
16
+ & --> &amp;
17
+ < --> &lt;
18
+ > --> &gt;
19
+ " --> &quot;
20
+ ' --> &#x27; &apos; is not recommended
21
+ / --> &#x2F; forward slash is included as it helps end an HTML entity
22
+
23
+ - HTML unescaping is fully RFC-compliant. Yes, that's the 253 different entities
24
+ for you, and decimal/hex code point specifiers.
25
+
26
+ - URI escaping and unescaping is fully RFC-compliant.
27
+
28
+ - URL escaping and unescaping is the same as generic URIs,
29
+ but spaces are changed to `+`.
30
+
31
+ **WARNING:** Houdini parses **only** UTF-8 strings, and generates **only**
32
+ UTF-8 strings. If you are using another encoding, you should probably transcode
33
+ *before* passing the buffer to Houdini.
34
+
35
+ ### Current API:
36
+
37
+ Do you really need docs for this?
38
+
39
+ ~~~~ c
40
+ int houdini_escape_html(gh_buf *ob, const uint8_t *src, size_t size);
41
+ int houdini_escape_html0(gh_buf *ob, const uint8_t *src, size_t size, int secure);
42
+ int houdini_unescape_html(gh_buf *ob, const uint8_t *src, size_t size);
43
+ int houdini_escape_xml(gh_buf *ob, const uint8_t *src, size_t size);
44
+ int houdini_escape_uri(gh_buf *ob, const uint8_t *src, size_t size);
45
+ int houdini_escape_url(gh_buf *ob, const uint8_t *src, size_t size);
46
+ int houdini_escape_href(gh_buf *ob, const uint8_t *src, size_t size);
47
+ int houdini_unescape_uri(gh_buf *ob, const uint8_t *src, size_t size);
48
+ int houdini_unescape_url(gh_buf *ob, const uint8_t *src, size_t size);
49
+ int houdini_escape_js(gh_buf *ob, const uint8_t *src, size_t size);
50
+ int houdini_unescape_js(gh_buf *ob, const uint8_t *src, size_t size);
51
+ ~~~~
52
+
53
+ Pass your string. It'll get escaped/unescaped in the target buffer, and the call will return 1.
54
+ If the given string has nothing to escape/unescape, the call will return 0 and the
55
+ output buffer will be empty.
56
+
57
+ ### Questions?
58
+
59
+ Open an issue. Or shout angrily at me on Twitter ([@vmg](https://twitter.com/vmg)).
@@ -0,0 +1,249 @@
1
+ /*
2
+ * Copyright (C) the libgit2 contributors. All rights reserved.
3
+ *
4
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
5
+ * a Linking Exception. For full terms see the included COPYING file.
6
+ */
7
+ #include <stdarg.h>
8
+ #include <ctype.h>
9
+ #include <string.h>
10
+ #include <assert.h>
11
+ #include <string.h>
12
+ #include <stdio.h>
13
+ #include <stdlib.h>
14
+ #include <sys/param.h>
15
+
16
+ #include "buffer.h"
17
+
18
+ /* Used as default value for gh_buf->ptr so that people can always
19
+ * assume ptr is non-NULL and zero terminated even for new gh_bufs.
20
+ */
21
+ char gh_buf__initbuf[1];
22
+ char gh_buf__oom[1];
23
+
24
+ #define ENSURE_SIZE(b, d) \
25
+ if ((d) > buf->asize && gh_buf_grow(b, (d)) < 0)\
26
+ return -1;
27
+
28
+ void gh_buf_init(gh_buf *buf, size_t initial_size)
29
+ {
30
+ buf->asize = 0;
31
+ buf->size = 0;
32
+ buf->ptr = gh_buf__initbuf;
33
+
34
+ if (initial_size)
35
+ gh_buf_grow(buf, initial_size);
36
+ }
37
+
38
+ int gh_buf_try_grow(gh_buf *buf, size_t target_size, bool mark_oom)
39
+ {
40
+ char *new_ptr;
41
+ size_t new_size;
42
+
43
+ if (buf->ptr == gh_buf__oom)
44
+ return -1;
45
+
46
+ if (target_size <= buf->asize)
47
+ return 0;
48
+
49
+ if (buf->asize == 0) {
50
+ new_size = target_size;
51
+ new_ptr = NULL;
52
+ } else {
53
+ new_size = buf->asize;
54
+ new_ptr = buf->ptr;
55
+ }
56
+
57
+ /* grow the buffer size by 1.5, until it's big enough
58
+ * to fit our target size */
59
+ while (new_size < target_size)
60
+ new_size = (new_size << 1) - (new_size >> 1);
61
+
62
+ /* round allocation up to multiple of 8 */
63
+ new_size = (new_size + 7) & ~7;
64
+
65
+ new_ptr = realloc(new_ptr, new_size);
66
+
67
+ if (!new_ptr) {
68
+ if (mark_oom)
69
+ buf->ptr = gh_buf__oom;
70
+ return -1;
71
+ }
72
+
73
+ buf->asize = new_size;
74
+ buf->ptr = new_ptr;
75
+
76
+ /* truncate the existing buffer size if necessary */
77
+ if (buf->size >= buf->asize)
78
+ buf->size = buf->asize - 1;
79
+ buf->ptr[buf->size] = '\0';
80
+
81
+ return 0;
82
+ }
83
+
84
+ void gh_buf_free(gh_buf *buf)
85
+ {
86
+ if (!buf) return;
87
+
88
+ if (buf->ptr != gh_buf__initbuf && buf->ptr != gh_buf__oom)
89
+ free(buf->ptr);
90
+
91
+ gh_buf_init(buf, 0);
92
+ }
93
+
94
+ void gh_buf_clear(gh_buf *buf)
95
+ {
96
+ buf->size = 0;
97
+ if (buf->asize > 0)
98
+ buf->ptr[0] = '\0';
99
+ }
100
+
101
+ int gh_buf_set(gh_buf *buf, const char *data, size_t len)
102
+ {
103
+ if (len == 0 || data == NULL) {
104
+ gh_buf_clear(buf);
105
+ } else {
106
+ if (data != buf->ptr) {
107
+ ENSURE_SIZE(buf, len + 1);
108
+ memmove(buf->ptr, data, len);
109
+ }
110
+ buf->size = len;
111
+ buf->ptr[buf->size] = '\0';
112
+ }
113
+ return 0;
114
+ }
115
+
116
+ int gh_buf_sets(gh_buf *buf, const char *string)
117
+ {
118
+ return gh_buf_set(buf, string, string ? strlen(string) : 0);
119
+ }
120
+
121
+ int gh_buf_putc(gh_buf *buf, char c)
122
+ {
123
+ ENSURE_SIZE(buf, buf->size + 2);
124
+ buf->ptr[buf->size++] = c;
125
+ buf->ptr[buf->size] = '\0';
126
+ return 0;
127
+ }
128
+
129
+ int gh_buf_put(gh_buf *buf, const void *data, size_t len)
130
+ {
131
+ ENSURE_SIZE(buf, buf->size + len + 1);
132
+ memmove(buf->ptr + buf->size, data, len);
133
+ buf->size += len;
134
+ buf->ptr[buf->size] = '\0';
135
+ return 0;
136
+ }
137
+
138
+ int gh_buf_puts(gh_buf *buf, const char *string)
139
+ {
140
+ assert(string);
141
+ return gh_buf_put(buf, string, strlen(string));
142
+ }
143
+
144
+ int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap)
145
+ {
146
+ int len;
147
+ const size_t expected_size = buf->size + (strlen(format) * 2);
148
+
149
+ ENSURE_SIZE(buf, expected_size);
150
+
151
+ while (1) {
152
+ va_list args;
153
+ va_copy(args, ap);
154
+
155
+ len = vsnprintf(
156
+ buf->ptr + buf->size,
157
+ buf->asize - buf->size,
158
+ format, args
159
+ );
160
+
161
+ if (len < 0) {
162
+ free(buf->ptr);
163
+ buf->ptr = gh_buf__oom;
164
+ return -1;
165
+ }
166
+
167
+ if ((size_t)len + 1 <= buf->asize - buf->size) {
168
+ buf->size += len;
169
+ break;
170
+ }
171
+
172
+ ENSURE_SIZE(buf, buf->size + len + 1);
173
+ }
174
+
175
+ return 0;
176
+ }
177
+
178
+ int gh_buf_printf(gh_buf *buf, const char *format, ...)
179
+ {
180
+ int r;
181
+ va_list ap;
182
+
183
+ va_start(ap, format);
184
+ r = gh_buf_vprintf(buf, format, ap);
185
+ va_end(ap);
186
+
187
+ return r;
188
+ }
189
+
190
+ void gh_buf_copy_cstr(char *data, size_t datasize, const gh_buf *buf)
191
+ {
192
+ size_t copylen;
193
+
194
+ assert(data && datasize && buf);
195
+
196
+ data[0] = '\0';
197
+
198
+ if (buf->size == 0 || buf->asize <= 0)
199
+ return;
200
+
201
+ copylen = buf->size;
202
+ if (copylen > datasize - 1)
203
+ copylen = datasize - 1;
204
+ memmove(data, buf->ptr, copylen);
205
+ data[copylen] = '\0';
206
+ }
207
+
208
+ void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b)
209
+ {
210
+ gh_buf t = *buf_a;
211
+ *buf_a = *buf_b;
212
+ *buf_b = t;
213
+ }
214
+
215
+ char *gh_buf_detach(gh_buf *buf)
216
+ {
217
+ char *data = buf->ptr;
218
+
219
+ if (buf->asize == 0 || buf->ptr == gh_buf__oom)
220
+ return NULL;
221
+
222
+ gh_buf_init(buf, 0);
223
+
224
+ return data;
225
+ }
226
+
227
+ void gh_buf_attach(gh_buf *buf, char *ptr, size_t asize)
228
+ {
229
+ gh_buf_free(buf);
230
+
231
+ if (ptr) {
232
+ buf->ptr = ptr;
233
+ buf->size = strlen(ptr);
234
+ if (asize)
235
+ buf->asize = (asize < buf->size) ? buf->size + 1 : asize;
236
+ else /* pass 0 to fall back on strlen + 1 */
237
+ buf->asize = buf->size + 1;
238
+ } else {
239
+ gh_buf_grow(buf, asize);
240
+ }
241
+ }
242
+
243
+ int gh_buf_cmp(const gh_buf *a, const gh_buf *b)
244
+ {
245
+ int result = memcmp(a->ptr, b->ptr, MIN(a->size, b->size));
246
+ return (result != 0) ? result :
247
+ (a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0;
248
+ }
249
+
@@ -0,0 +1,113 @@
1
+ /*
2
+ * Copyright (C) the libgit2 contributors. All rights reserved.
3
+ *
4
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
5
+ * a Linking Exception. For full terms see the included COPYING file.
6
+ */
7
+ #ifndef INCLUDE_buffer_h__
8
+ #define INCLUDE_buffer_h__
9
+
10
+ #include <stdbool.h>
11
+ #include <stddef.h>
12
+ #include <stdarg.h>
13
+ #include <sys/types.h>
14
+ #include <stdint.h>
15
+
16
+ typedef struct {
17
+ char *ptr;
18
+ size_t asize, size;
19
+ } gh_buf;
20
+
21
+ extern char gh_buf__initbuf[];
22
+ extern char gh_buf__oom[];
23
+
24
+ #define GH_BUF_INIT { gh_buf__initbuf, 0, 0 }
25
+
26
+ /**
27
+ * Initialize a gh_buf structure.
28
+ *
29
+ * For the cases where GH_BUF_INIT cannot be used to do static
30
+ * initialization.
31
+ */
32
+ extern void gh_buf_init(gh_buf *buf, size_t initial_size);
33
+
34
+ /**
35
+ * Attempt to grow the buffer to hold at least `target_size` bytes.
36
+ *
37
+ * If the allocation fails, this will return an error. If mark_oom is true,
38
+ * this will mark the buffer as invalid for future operations; if false,
39
+ * existing buffer content will be preserved, but calling code must handle
40
+ * that buffer was not expanded.
41
+ */
42
+ extern int gh_buf_try_grow(gh_buf *buf, size_t target_size, bool mark_oom);
43
+
44
+ /**
45
+ * Grow the buffer to hold at least `target_size` bytes.
46
+ *
47
+ * If the allocation fails, this will return an error and the buffer will be
48
+ * marked as invalid for future operations, invaliding contents.
49
+ *
50
+ * @return 0 on success or -1 on failure
51
+ */
52
+ static inline int gh_buf_grow(gh_buf *buf, size_t target_size)
53
+ {
54
+ return gh_buf_try_grow(buf, target_size, true);
55
+ }
56
+
57
+ extern void gh_buf_free(gh_buf *buf);
58
+ extern void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b);
59
+
60
+ /**
61
+ * Test if there have been any reallocation failures with this gh_buf.
62
+ *
63
+ * Any function that writes to a gh_buf can fail due to memory allocation
64
+ * issues. If one fails, the gh_buf will be marked with an OOM error and
65
+ * further calls to modify the buffer will fail. Check gh_buf_oom() at the
66
+ * end of your sequence and it will be true if you ran out of memory at any
67
+ * point with that buffer.
68
+ *
69
+ * @return false if no error, true if allocation error
70
+ */
71
+ static inline bool gh_buf_oom(const gh_buf *buf)
72
+ {
73
+ return (buf->ptr == gh_buf__oom);
74
+ }
75
+
76
+
77
+ static inline size_t gh_buf_len(const gh_buf *buf)
78
+ {
79
+ return buf->size;
80
+ }
81
+
82
+ extern int gh_buf_cmp(const gh_buf *a, const gh_buf *b);
83
+
84
+ extern void gh_buf_attach(gh_buf *buf, char *ptr, size_t asize);
85
+ extern char *gh_buf_detach(gh_buf *buf);
86
+ extern void gh_buf_copy_cstr(char *data, size_t datasize, const gh_buf *buf);
87
+
88
+ static inline const char *gh_buf_cstr(const gh_buf *buf)
89
+ {
90
+ return buf->ptr;
91
+ }
92
+
93
+ /*
94
+ * Functions below that return int value error codes will return 0 on
95
+ * success or -1 on failure (which generally means an allocation failed).
96
+ * Using a gh_buf where the allocation has failed with result in -1 from
97
+ * all further calls using that buffer. As a result, you can ignore the
98
+ * return code of these functions and call them in a series then just call
99
+ * gh_buf_oom at the end.
100
+ */
101
+ extern int gh_buf_set(gh_buf *buf, const char *data, size_t len);
102
+ extern int gh_buf_sets(gh_buf *buf, const char *string);
103
+ extern int gh_buf_putc(gh_buf *buf, char c);
104
+ extern int gh_buf_put(gh_buf *buf, const void *data, size_t len);
105
+ extern int gh_buf_puts(gh_buf *buf, const char *string);
106
+ extern int gh_buf_printf(gh_buf *buf, const char *format, ...)
107
+ __attribute__((format (printf, 2, 3)));
108
+ extern int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap);
109
+ extern void gh_buf_clear(gh_buf *buf);
110
+
111
+ #define gh_buf_PUTS(buf, str) gh_buf_put(buf, str, sizeof(str) - 1)
112
+
113
+ #endif