immunio 1.2.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (291) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -5
  3. data/ext/immunio/Rakefile +14 -6
  4. data/lib/immunio/context.rb +2 -0
  5. data/lib/immunio/plugins/action_view.rb +7 -668
  6. data/lib/immunio/plugins/action_view/action_view.rb +22 -0
  7. data/lib/immunio/plugins/action_view/active_support_hash.rb +29 -0
  8. data/lib/immunio/plugins/action_view/cache_store.rb +24 -0
  9. data/lib/immunio/plugins/action_view/erubi.rb +38 -0
  10. data/lib/immunio/plugins/action_view/erubis.rb +39 -0
  11. data/lib/immunio/plugins/action_view/fragment_caching.rb +29 -0
  12. data/lib/immunio/plugins/action_view/haml.rb +46 -0
  13. data/lib/immunio/plugins/action_view/slim.rb +42 -0
  14. data/lib/immunio/plugins/action_view/template.rb +431 -0
  15. data/lib/immunio/plugins/action_view/template_rendering.rb +45 -0
  16. data/lib/immunio/plugins/http_tracker.rb +2 -0
  17. data/lib/immunio/plugins/io.rb +34 -0
  18. data/lib/immunio/version.rb +1 -1
  19. data/lua-hooks/Makefile +36 -9
  20. data/lua-hooks/ext/luajit/COPYRIGHT +1 -1
  21. data/lua-hooks/ext/luajit/Makefile +22 -15
  22. data/lua-hooks/ext/luajit/README +2 -2
  23. data/lua-hooks/ext/luajit/doc/bluequad-print.css +1 -1
  24. data/lua-hooks/ext/luajit/doc/bluequad.css +1 -1
  25. data/lua-hooks/ext/luajit/doc/changes.html +69 -3
  26. data/lua-hooks/ext/luajit/doc/contact.html +10 -3
  27. data/lua-hooks/ext/luajit/doc/ext_c_api.html +2 -2
  28. data/lua-hooks/ext/luajit/doc/ext_ffi.html +2 -2
  29. data/lua-hooks/ext/luajit/doc/ext_ffi_api.html +2 -2
  30. data/lua-hooks/ext/luajit/doc/ext_ffi_semantics.html +3 -4
  31. data/lua-hooks/ext/luajit/doc/ext_ffi_tutorial.html +2 -2
  32. data/lua-hooks/ext/luajit/doc/ext_jit.html +3 -3
  33. data/lua-hooks/ext/luajit/doc/ext_profiler.html +2 -2
  34. data/lua-hooks/ext/luajit/doc/extensions.html +47 -20
  35. data/lua-hooks/ext/luajit/doc/faq.html +2 -2
  36. data/lua-hooks/ext/luajit/doc/install.html +74 -45
  37. data/lua-hooks/ext/luajit/doc/luajit.html +5 -5
  38. data/lua-hooks/ext/luajit/doc/running.html +3 -3
  39. data/lua-hooks/ext/luajit/doc/status.html +13 -8
  40. data/lua-hooks/ext/luajit/dynasm/dasm_arm.h +1 -1
  41. data/lua-hooks/ext/luajit/dynasm/dasm_arm.lua +1 -1
  42. data/lua-hooks/ext/luajit/dynasm/dasm_arm64.h +1 -1
  43. data/lua-hooks/ext/luajit/dynasm/dasm_arm64.lua +1 -1
  44. data/lua-hooks/ext/luajit/dynasm/dasm_mips.h +8 -5
  45. data/lua-hooks/ext/luajit/dynasm/dasm_mips.lua +66 -11
  46. data/lua-hooks/ext/luajit/dynasm/dasm_mips64.lua +12 -0
  47. data/lua-hooks/ext/luajit/dynasm/dasm_ppc.h +1 -1
  48. data/lua-hooks/ext/luajit/dynasm/dasm_ppc.lua +1 -1
  49. data/lua-hooks/ext/luajit/dynasm/dasm_proto.h +1 -1
  50. data/lua-hooks/ext/luajit/dynasm/dasm_x64.lua +1 -1
  51. data/lua-hooks/ext/luajit/dynasm/dasm_x86.h +1 -1
  52. data/lua-hooks/ext/luajit/dynasm/dasm_x86.lua +5 -1
  53. data/lua-hooks/ext/luajit/dynasm/dynasm.lua +2 -2
  54. data/lua-hooks/ext/luajit/etc/luajit.1 +1 -1
  55. data/lua-hooks/ext/luajit/etc/luajit.pc +1 -1
  56. data/lua-hooks/ext/luajit/src/Makefile +15 -11
  57. data/lua-hooks/ext/luajit/src/Makefile.dep +16 -16
  58. data/lua-hooks/ext/luajit/src/host/buildvm.c +2 -2
  59. data/lua-hooks/ext/luajit/src/host/buildvm.h +1 -1
  60. data/lua-hooks/ext/luajit/src/host/buildvm_asm.c +9 -4
  61. data/lua-hooks/ext/luajit/src/host/buildvm_fold.c +2 -2
  62. data/lua-hooks/ext/luajit/src/host/buildvm_lib.c +1 -1
  63. data/lua-hooks/ext/luajit/src/host/buildvm_libbc.h +14 -3
  64. data/lua-hooks/ext/luajit/src/host/buildvm_peobj.c +27 -3
  65. data/lua-hooks/ext/luajit/src/host/genlibbc.lua +1 -1
  66. data/lua-hooks/ext/luajit/src/host/genminilua.lua +6 -5
  67. data/lua-hooks/ext/luajit/src/host/minilua.c +1 -1
  68. data/lua-hooks/ext/luajit/src/jit/bc.lua +1 -1
  69. data/lua-hooks/ext/luajit/src/jit/bcsave.lua +8 -8
  70. data/lua-hooks/ext/luajit/src/jit/dis_arm.lua +2 -2
  71. data/lua-hooks/ext/luajit/src/jit/dis_arm64.lua +1216 -0
  72. data/lua-hooks/ext/luajit/src/jit/dis_arm64be.lua +12 -0
  73. data/lua-hooks/ext/luajit/src/jit/dis_mips.lua +35 -20
  74. data/lua-hooks/ext/luajit/src/jit/dis_mips64.lua +17 -0
  75. data/lua-hooks/ext/luajit/src/jit/dis_mips64el.lua +17 -0
  76. data/lua-hooks/ext/luajit/src/jit/dis_mipsel.lua +1 -1
  77. data/lua-hooks/ext/luajit/src/jit/dis_ppc.lua +2 -2
  78. data/lua-hooks/ext/luajit/src/jit/dis_x64.lua +1 -1
  79. data/lua-hooks/ext/luajit/src/jit/dis_x86.lua +7 -4
  80. data/lua-hooks/ext/luajit/src/jit/dump.lua +17 -12
  81. data/lua-hooks/ext/luajit/src/jit/p.lua +3 -2
  82. data/lua-hooks/ext/luajit/src/jit/v.lua +2 -2
  83. data/lua-hooks/ext/luajit/src/jit/zone.lua +1 -1
  84. data/lua-hooks/ext/luajit/src/lauxlib.h +14 -20
  85. data/lua-hooks/ext/luajit/src/lib_aux.c +38 -27
  86. data/lua-hooks/ext/luajit/src/lib_base.c +12 -5
  87. data/lua-hooks/ext/luajit/src/lib_bit.c +1 -1
  88. data/lua-hooks/ext/luajit/src/lib_debug.c +5 -5
  89. data/lua-hooks/ext/luajit/src/lib_ffi.c +2 -2
  90. data/lua-hooks/ext/luajit/src/lib_init.c +16 -16
  91. data/lua-hooks/ext/luajit/src/lib_io.c +6 -7
  92. data/lua-hooks/ext/luajit/src/lib_jit.c +14 -4
  93. data/lua-hooks/ext/luajit/src/lib_math.c +1 -5
  94. data/lua-hooks/ext/luajit/src/lib_os.c +1 -1
  95. data/lua-hooks/ext/luajit/src/lib_package.c +14 -23
  96. data/lua-hooks/ext/luajit/src/lib_string.c +1 -5
  97. data/lua-hooks/ext/luajit/src/lib_table.c +21 -1
  98. data/lua-hooks/ext/luajit/src/lj.supp +3 -3
  99. data/lua-hooks/ext/luajit/src/lj_alloc.c +174 -83
  100. data/lua-hooks/ext/luajit/src/lj_api.c +97 -18
  101. data/lua-hooks/ext/luajit/src/lj_arch.h +54 -22
  102. data/lua-hooks/ext/luajit/src/lj_asm.c +172 -53
  103. data/lua-hooks/ext/luajit/src/lj_asm.h +1 -1
  104. data/lua-hooks/ext/luajit/src/lj_asm_arm.h +19 -16
  105. data/lua-hooks/ext/luajit/src/lj_asm_arm64.h +2022 -0
  106. data/lua-hooks/ext/luajit/src/lj_asm_mips.h +564 -158
  107. data/lua-hooks/ext/luajit/src/lj_asm_ppc.h +19 -18
  108. data/lua-hooks/ext/luajit/src/lj_asm_x86.h +578 -92
  109. data/lua-hooks/ext/luajit/src/lj_bc.c +1 -1
  110. data/lua-hooks/ext/luajit/src/lj_bc.h +1 -1
  111. data/lua-hooks/ext/luajit/src/lj_bcdump.h +1 -1
  112. data/lua-hooks/ext/luajit/src/lj_bcread.c +1 -1
  113. data/lua-hooks/ext/luajit/src/lj_bcwrite.c +1 -1
  114. data/lua-hooks/ext/luajit/src/lj_buf.c +1 -1
  115. data/lua-hooks/ext/luajit/src/lj_buf.h +1 -1
  116. data/lua-hooks/ext/luajit/src/lj_carith.c +1 -1
  117. data/lua-hooks/ext/luajit/src/lj_carith.h +1 -1
  118. data/lua-hooks/ext/luajit/src/lj_ccall.c +172 -7
  119. data/lua-hooks/ext/luajit/src/lj_ccall.h +21 -5
  120. data/lua-hooks/ext/luajit/src/lj_ccallback.c +71 -17
  121. data/lua-hooks/ext/luajit/src/lj_ccallback.h +1 -1
  122. data/lua-hooks/ext/luajit/src/lj_cconv.c +4 -2
  123. data/lua-hooks/ext/luajit/src/lj_cconv.h +1 -1
  124. data/lua-hooks/ext/luajit/src/lj_cdata.c +7 -5
  125. data/lua-hooks/ext/luajit/src/lj_cdata.h +1 -1
  126. data/lua-hooks/ext/luajit/src/lj_clib.c +5 -5
  127. data/lua-hooks/ext/luajit/src/lj_clib.h +1 -1
  128. data/lua-hooks/ext/luajit/src/lj_cparse.c +11 -6
  129. data/lua-hooks/ext/luajit/src/lj_cparse.h +1 -1
  130. data/lua-hooks/ext/luajit/src/lj_crecord.c +70 -14
  131. data/lua-hooks/ext/luajit/src/lj_crecord.h +1 -1
  132. data/lua-hooks/ext/luajit/src/lj_ctype.c +1 -1
  133. data/lua-hooks/ext/luajit/src/lj_ctype.h +8 -8
  134. data/lua-hooks/ext/luajit/src/lj_debug.c +1 -1
  135. data/lua-hooks/ext/luajit/src/lj_debug.h +1 -1
  136. data/lua-hooks/ext/luajit/src/lj_def.h +6 -9
  137. data/lua-hooks/ext/luajit/src/lj_dispatch.c +3 -3
  138. data/lua-hooks/ext/luajit/src/lj_dispatch.h +2 -1
  139. data/lua-hooks/ext/luajit/src/lj_emit_arm.h +5 -4
  140. data/lua-hooks/ext/luajit/src/lj_emit_arm64.h +419 -0
  141. data/lua-hooks/ext/luajit/src/lj_emit_mips.h +100 -20
  142. data/lua-hooks/ext/luajit/src/lj_emit_ppc.h +4 -4
  143. data/lua-hooks/ext/luajit/src/lj_emit_x86.h +116 -25
  144. data/lua-hooks/ext/luajit/src/lj_err.c +34 -13
  145. data/lua-hooks/ext/luajit/src/lj_err.h +1 -1
  146. data/lua-hooks/ext/luajit/src/lj_errmsg.h +1 -1
  147. data/lua-hooks/ext/luajit/src/lj_ff.h +1 -1
  148. data/lua-hooks/ext/luajit/src/lj_ffrecord.c +58 -49
  149. data/lua-hooks/ext/luajit/src/lj_ffrecord.h +1 -1
  150. data/lua-hooks/ext/luajit/src/lj_frame.h +33 -6
  151. data/lua-hooks/ext/luajit/src/lj_func.c +4 -2
  152. data/lua-hooks/ext/luajit/src/lj_func.h +1 -1
  153. data/lua-hooks/ext/luajit/src/lj_gc.c +16 -7
  154. data/lua-hooks/ext/luajit/src/lj_gc.h +1 -1
  155. data/lua-hooks/ext/luajit/src/lj_gdbjit.c +31 -1
  156. data/lua-hooks/ext/luajit/src/lj_gdbjit.h +1 -1
  157. data/lua-hooks/ext/luajit/src/lj_ir.c +69 -96
  158. data/lua-hooks/ext/luajit/src/lj_ir.h +29 -18
  159. data/lua-hooks/ext/luajit/src/lj_ircall.h +24 -30
  160. data/lua-hooks/ext/luajit/src/lj_iropt.h +9 -9
  161. data/lua-hooks/ext/luajit/src/lj_jit.h +67 -9
  162. data/lua-hooks/ext/luajit/src/lj_lex.c +1 -1
  163. data/lua-hooks/ext/luajit/src/lj_lex.h +1 -1
  164. data/lua-hooks/ext/luajit/src/lj_lib.c +1 -1
  165. data/lua-hooks/ext/luajit/src/lj_lib.h +1 -1
  166. data/lua-hooks/ext/luajit/src/lj_load.c +1 -1
  167. data/lua-hooks/ext/luajit/src/lj_mcode.c +11 -10
  168. data/lua-hooks/ext/luajit/src/lj_mcode.h +1 -1
  169. data/lua-hooks/ext/luajit/src/lj_meta.c +1 -1
  170. data/lua-hooks/ext/luajit/src/lj_meta.h +1 -1
  171. data/lua-hooks/ext/luajit/src/lj_obj.c +1 -1
  172. data/lua-hooks/ext/luajit/src/lj_obj.h +7 -3
  173. data/lua-hooks/ext/luajit/src/lj_opt_dce.c +1 -1
  174. data/lua-hooks/ext/luajit/src/lj_opt_fold.c +84 -17
  175. data/lua-hooks/ext/luajit/src/lj_opt_loop.c +1 -1
  176. data/lua-hooks/ext/luajit/src/lj_opt_mem.c +3 -3
  177. data/lua-hooks/ext/luajit/src/lj_opt_narrow.c +24 -22
  178. data/lua-hooks/ext/luajit/src/lj_opt_sink.c +11 -6
  179. data/lua-hooks/ext/luajit/src/lj_opt_split.c +11 -2
  180. data/lua-hooks/ext/luajit/src/lj_parse.c +9 -7
  181. data/lua-hooks/ext/luajit/src/lj_parse.h +1 -1
  182. data/lua-hooks/ext/luajit/src/lj_profile.c +1 -1
  183. data/lua-hooks/ext/luajit/src/lj_profile.h +1 -1
  184. data/lua-hooks/ext/luajit/src/lj_record.c +201 -117
  185. data/lua-hooks/ext/luajit/src/lj_record.h +1 -1
  186. data/lua-hooks/ext/luajit/src/lj_snap.c +72 -26
  187. data/lua-hooks/ext/luajit/src/lj_snap.h +1 -1
  188. data/lua-hooks/ext/luajit/src/lj_state.c +6 -6
  189. data/lua-hooks/ext/luajit/src/lj_state.h +2 -2
  190. data/lua-hooks/ext/luajit/src/lj_str.c +1 -1
  191. data/lua-hooks/ext/luajit/src/lj_str.h +1 -1
  192. data/lua-hooks/ext/luajit/src/lj_strfmt.c +7 -3
  193. data/lua-hooks/ext/luajit/src/lj_strfmt.h +1 -1
  194. data/lua-hooks/ext/luajit/src/lj_strfmt_num.c +4 -3
  195. data/lua-hooks/ext/luajit/src/lj_strscan.c +1 -1
  196. data/lua-hooks/ext/luajit/src/lj_strscan.h +1 -1
  197. data/lua-hooks/ext/luajit/src/lj_tab.c +1 -2
  198. data/lua-hooks/ext/luajit/src/lj_tab.h +1 -1
  199. data/lua-hooks/ext/luajit/src/lj_target.h +3 -3
  200. data/lua-hooks/ext/luajit/src/lj_target_arm.h +1 -1
  201. data/lua-hooks/ext/luajit/src/lj_target_arm64.h +239 -7
  202. data/lua-hooks/ext/luajit/src/lj_target_mips.h +111 -22
  203. data/lua-hooks/ext/luajit/src/lj_target_ppc.h +1 -1
  204. data/lua-hooks/ext/luajit/src/lj_target_x86.h +21 -4
  205. data/lua-hooks/ext/luajit/src/lj_trace.c +63 -18
  206. data/lua-hooks/ext/luajit/src/lj_trace.h +2 -1
  207. data/lua-hooks/ext/luajit/src/lj_traceerr.h +1 -1
  208. data/lua-hooks/ext/luajit/src/lj_udata.c +1 -1
  209. data/lua-hooks/ext/luajit/src/lj_udata.h +1 -1
  210. data/lua-hooks/ext/luajit/src/lj_vm.h +5 -1
  211. data/lua-hooks/ext/luajit/src/lj_vmevent.c +1 -1
  212. data/lua-hooks/ext/luajit/src/lj_vmevent.h +1 -1
  213. data/lua-hooks/ext/luajit/src/lj_vmmath.c +1 -1
  214. data/lua-hooks/ext/luajit/src/ljamalg.c +1 -1
  215. data/lua-hooks/ext/luajit/src/lua.h +9 -1
  216. data/lua-hooks/ext/luajit/src/luaconf.h +3 -7
  217. data/lua-hooks/ext/luajit/src/luajit.c +69 -54
  218. data/lua-hooks/ext/luajit/src/luajit.h +4 -4
  219. data/lua-hooks/ext/luajit/src/lualib.h +1 -1
  220. data/lua-hooks/ext/luajit/src/msvcbuild.bat +12 -4
  221. data/lua-hooks/ext/luajit/src/vm_arm.dasc +1 -1
  222. data/lua-hooks/ext/luajit/src/vm_arm64.dasc +255 -32
  223. data/lua-hooks/ext/luajit/src/vm_mips.dasc +26 -23
  224. data/lua-hooks/ext/luajit/src/vm_mips64.dasc +5062 -0
  225. data/lua-hooks/ext/luajit/src/vm_ppc.dasc +1 -1
  226. data/lua-hooks/ext/luajit/src/vm_x64.dasc +24 -25
  227. data/lua-hooks/ext/luajit/src/vm_x86.dasc +77 -4
  228. data/lua-hooks/libluahooks.darwin.a +0 -0
  229. data/lua-hooks/libluahooks.linux.a +0 -0
  230. data/lua-hooks/options.mk +1 -1
  231. metadata +37 -77
  232. data/lua-hooks/ext/all.c +0 -69
  233. data/lua-hooks/ext/libinjection/COPYING +0 -37
  234. data/lua-hooks/ext/libinjection/libinjection.h +0 -65
  235. data/lua-hooks/ext/libinjection/libinjection_html5.c +0 -847
  236. data/lua-hooks/ext/libinjection/libinjection_html5.h +0 -54
  237. data/lua-hooks/ext/libinjection/libinjection_sqli.c +0 -2301
  238. data/lua-hooks/ext/libinjection/libinjection_sqli.h +0 -295
  239. data/lua-hooks/ext/libinjection/libinjection_sqli_data.h +0 -9349
  240. data/lua-hooks/ext/libinjection/libinjection_xss.c +0 -531
  241. data/lua-hooks/ext/libinjection/libinjection_xss.h +0 -21
  242. data/lua-hooks/ext/libinjection/lualib.c +0 -145
  243. data/lua-hooks/ext/libinjection/module.mk +0 -5
  244. data/lua-hooks/ext/lpeg/HISTORY +0 -96
  245. data/lua-hooks/ext/lpeg/lpcap.c +0 -537
  246. data/lua-hooks/ext/lpeg/lpcap.h +0 -56
  247. data/lua-hooks/ext/lpeg/lpcode.c +0 -1014
  248. data/lua-hooks/ext/lpeg/lpcode.h +0 -40
  249. data/lua-hooks/ext/lpeg/lpeg-128.gif +0 -0
  250. data/lua-hooks/ext/lpeg/lpeg.html +0 -1445
  251. data/lua-hooks/ext/lpeg/lpprint.c +0 -244
  252. data/lua-hooks/ext/lpeg/lpprint.h +0 -36
  253. data/lua-hooks/ext/lpeg/lptree.c +0 -1303
  254. data/lua-hooks/ext/lpeg/lptree.h +0 -82
  255. data/lua-hooks/ext/lpeg/lptypes.h +0 -149
  256. data/lua-hooks/ext/lpeg/lpvm.c +0 -364
  257. data/lua-hooks/ext/lpeg/lpvm.h +0 -58
  258. data/lua-hooks/ext/lpeg/makefile +0 -55
  259. data/lua-hooks/ext/lpeg/module.mk +0 -6
  260. data/lua-hooks/ext/lpeg/re.html +0 -498
  261. data/lua-hooks/ext/lua-cmsgpack/.gitignore +0 -13
  262. data/lua-hooks/ext/lua-cmsgpack/CMakeLists.txt +0 -45
  263. data/lua-hooks/ext/lua-cmsgpack/README.md +0 -115
  264. data/lua-hooks/ext/lua-cmsgpack/lua_cmsgpack.c +0 -970
  265. data/lua-hooks/ext/lua-cmsgpack/module.mk +0 -2
  266. data/lua-hooks/ext/lua-cmsgpack/test.lua +0 -570
  267. data/lua-hooks/ext/lua-snapshot/LICENSE +0 -7
  268. data/lua-hooks/ext/lua-snapshot/Makefile +0 -12
  269. data/lua-hooks/ext/lua-snapshot/README.md +0 -18
  270. data/lua-hooks/ext/lua-snapshot/dump.lua +0 -15
  271. data/lua-hooks/ext/lua-snapshot/module.mk +0 -2
  272. data/lua-hooks/ext/lua-snapshot/snapshot.c +0 -462
  273. data/lua-hooks/ext/luautf8/README.md +0 -152
  274. data/lua-hooks/ext/luautf8/lutf8lib.c +0 -1274
  275. data/lua-hooks/ext/luautf8/module.mk +0 -2
  276. data/lua-hooks/ext/luautf8/unidata.h +0 -3064
  277. data/lua-hooks/ext/module.mk +0 -15
  278. data/lua-hooks/ext/modules.h +0 -17
  279. data/lua-hooks/ext/perf/luacpu.c +0 -114
  280. data/lua-hooks/ext/perf/lualoadavg.c +0 -40
  281. data/lua-hooks/ext/perf/luameminfo.c +0 -38
  282. data/lua-hooks/ext/perf/luaoslib.c +0 -203
  283. data/lua-hooks/ext/perf/module.mk +0 -5
  284. data/lua-hooks/ext/sha1/luasha1.c +0 -74
  285. data/lua-hooks/ext/sha1/module.mk +0 -5
  286. data/lua-hooks/ext/sha1/sha1.c +0 -145
  287. data/lua-hooks/ext/sha2/luasha256.c +0 -77
  288. data/lua-hooks/ext/sha2/module.mk +0 -5
  289. data/lua-hooks/ext/sha2/sha256.c +0 -196
  290. data/lua-hooks/ext/sysutils/lua_utils.c +0 -56
  291. data/lua-hooks/ext/sysutils/module.mk +0 -2
@@ -1,21 +0,0 @@
1
- #ifndef LIBINJECTION_XSS
2
- #define LIBINJECTION_XSS
3
-
4
- #ifdef __cplusplus
5
- extern "C" {
6
- #endif
7
-
8
- /**
9
- * HEY THIS ISN'T DONE
10
- */
11
-
12
- /* pull in size_t */
13
-
14
- #include <string.h>
15
-
16
- int libinjection_is_xss(const char* s, size_t len, int flags);
17
-
18
- #ifdef __cplusplus
19
- }
20
- #endif
21
- #endif
@@ -1,145 +0,0 @@
1
- #include <stdlib.h>
2
-
3
- #define LUA_LIB
4
- #include "lua.h"
5
- #include "lauxlib.h"
6
-
7
- #include "libinjection.h"
8
- #include "libinjection_sqli.h"
9
-
10
- #define MAX_FINGERPRINT_STACK_SIZE 4096
11
-
12
- int sqli(lua_State *L) {
13
- sfilter state;
14
-
15
- size_t slen = 0;
16
- const char *input = luaL_checklstring(L, 1, &slen);
17
-
18
- libinjection_sqli_init(&state, input, slen, FLAG_NONE);
19
- int issqli = libinjection_is_sqli(&state);
20
-
21
- lua_pushboolean(L, issqli);
22
-
23
- return 1;
24
- }
25
-
26
- int fingerprint(lua_State *L) {
27
- char stack_result[MAX_FINGERPRINT_STACK_SIZE];
28
- char *result = stack_result;
29
- struct libinjection_sqli_state state;
30
- size_t slen = 0;
31
- const char *input = luaL_checklstring(L, 1, &slen);
32
-
33
- libinjection_sqli_init(&state, input, slen, 0);
34
-
35
- // libinjection tokenizes in a streaming fashion, meaning we won't know until
36
- // the end of the process how long the resulting string of tokens will be. But
37
- // we do know that it will be less than or equal to the length of the SQL
38
- // string.
39
- //
40
- // For speed, use stack-allocated fingerprint result strings unless we need
41
- // more than 4KB.
42
- if (slen > sizeof(stack_result)) {
43
- result = malloc(slen + 1);
44
- if (!result) {
45
- lua_pushstring(L, ""); // Push something at least...
46
- return 1;
47
- }
48
- }
49
-
50
- int fp_idx = 0;
51
- while (state.pos < state.slen) {
52
- libinjection_sqli_tokenize(&state);
53
- result[fp_idx++] = state.tokenvec[0].type;
54
- }
55
- result[fp_idx] = '\0';
56
-
57
- lua_pushstring(L, result);
58
-
59
- // If we dynamically allocated the result above, free it
60
- if (result != stack_result) free(result);
61
-
62
- return 1;
63
- }
64
-
65
- int xss(lua_State *L) {
66
- size_t slen = 0;
67
- const char *input = luaL_checklstring(L, 1, &slen);
68
-
69
- int is_xss = libinjection_xss(input, slen);
70
-
71
- lua_pushboolean(L, is_xss);
72
- return 1;
73
- }
74
-
75
- /*
76
- * Tokenize the SQL into an array where each element
77
- * is a table with a type and value. For example:
78
- * {type: "E", value: "SELECT"}
79
- */
80
- int sqli_tokenize(lua_State *L) {
81
- int token_cnt = 1;
82
- size_t slen = 0;
83
- const char *input = luaL_checklstring(L, 1, &slen);
84
- struct libinjection_sqli_state state;
85
- int var_symbol_count;
86
-
87
- libinjection_sqli_init(&state, input, slen, 0);
88
-
89
- lua_newtable(L); /* Tokens array */
90
- while (state.pos < state.slen) {
91
- libinjection_sqli_tokenize(&state);
92
-
93
- lua_newtable(L); /* Inner token table */
94
-
95
- /* Token.type = type */
96
- lua_pushlstring(L, &state.tokenvec[0].type, 1);
97
- lua_setfield(L, -2, "type");
98
-
99
- if (state.tokenvec[0].str_open != '\0') {
100
- /* Token.str_open = str_open */
101
- lua_pushlstring(L, &state.tokenvec[0].str_open, 1);
102
- lua_setfield(L, -2, "str_open");
103
- }
104
-
105
- if (state.tokenvec[0].str_close != '\0') {
106
- /* Token.str_close = str_open */
107
- lua_pushlstring(L, &state.tokenvec[0].str_close, 1);
108
- lua_setfield(L, -2, "str_close");
109
- }
110
-
111
- /* Token.var_symbol_count = count for variable tokens */
112
- if (state.tokenvec[0].type == 'v') {
113
- lua_pushinteger(L, state.tokenvec[0].count);
114
- lua_setfield(L, -2, "var_symbol_count");
115
- }
116
-
117
- /* Token.value = value */
118
- lua_pushlstring(L, &input[state.tokenvec[0].pos],
119
- state.tokenvec[0].len);
120
- lua_setfield(L, -2, "value");
121
-
122
- /* Token.pos = pos */
123
- lua_pushinteger(L, state.tokenvec[0].pos);
124
- lua_setfield(L, -2, "pos");
125
-
126
- /* Tokens.append(Token) */
127
- lua_pushinteger(L, token_cnt++);
128
- lua_insert(L, -2); /* [..., token, index] --> [..., index, token] */
129
- lua_settable(L, -3);
130
- }
131
- return 1;
132
- }
133
-
134
- static const luaL_Reg libinjection[] = {
135
- {"sqli", sqli},
136
- {"fingerprint", fingerprint},
137
- {"xss", xss},
138
- {"sqli_tokenize", sqli_tokenize},
139
- {NULL, NULL}
140
- };
141
-
142
- int luaopen_libinjection(lua_State *L) {
143
- luaL_register(L, "libinjection", libinjection);
144
- return 1;
145
- }
@@ -1,5 +0,0 @@
1
- SRC += \
2
- ext/libinjection/libinjection_html5.c \
3
- ext/libinjection/libinjection_xss.c \
4
- ext/libinjection/libinjection_sqli.c\
5
- ext/libinjection/lualib.c
@@ -1,96 +0,0 @@
1
- HISTORY for LPeg 1.0
2
-
3
- * Changes from version 0.12 to 1.0
4
- ---------------------------------
5
- + group "names" can be any Lua value
6
- + some bugs fixed
7
- + other small improvements
8
-
9
- * Changes from version 0.11 to 0.12
10
- ---------------------------------
11
- + no "unsigned short" limit for pattern sizes
12
- + mathtime captures considered nullable
13
- + some bugs fixed
14
-
15
- * Changes from version 0.10 to 0.11
16
- -------------------------------
17
- + complete reimplementation of the code generator
18
- + new syntax for table captures
19
- + new functions in module 're'
20
- + other small improvements
21
-
22
- * Changes from version 0.9 to 0.10
23
- -------------------------------
24
- + backtrack stack has configurable size
25
- + better error messages
26
- + Notation for non-terminals in 're' back to A instead o <A>
27
- + experimental look-behind pattern
28
- + support for external extensions
29
- + works with Lua 5.2
30
- + consumes less C stack
31
-
32
- - "and" predicates do not keep captures
33
-
34
- * Changes from version 0.8 to 0.9
35
- -------------------------------
36
- + The accumulator capture was replaced by a fold capture;
37
- programs that used the old 'lpeg.Ca' will need small changes.
38
- + Some support for character classes from old C locales.
39
- + A new named-group capture.
40
-
41
- * Changes from version 0.7 to 0.8
42
- -------------------------------
43
- + New "match-time" capture.
44
- + New "argument capture" that allows passing arguments into the pattern.
45
- + Better documentation for 're'.
46
- + Several small improvements for 're'.
47
- + The 're' module has an incompatibility with previous versions:
48
- now, any use of a non-terminal must be enclosed in angle brackets
49
- (like <B>).
50
-
51
- * Changes from version 0.6 to 0.7
52
- -------------------------------
53
- + Several improvements in module 're':
54
- - better documentation;
55
- - support for most captures (all but accumulator);
56
- - limited repetitions p{n,m}.
57
- + Small improvements in efficiency.
58
- + Several small bugs corrected (special thanks to Hans Hagen
59
- and Taco Hoekwater).
60
-
61
- * Changes from version 0.5 to 0.6
62
- -------------------------------
63
- + Support for non-numeric indices in grammars.
64
- + Some bug fixes (thanks to the luatex team).
65
- + Some new optimizations; (thanks to Mike Pall).
66
- + A new page layout (thanks to Andre Carregal).
67
- + Minimal documentation for module 're'.
68
-
69
- * Changes from version 0.4 to 0.5
70
- -------------------------------
71
- + Several optimizations.
72
- + lpeg.P now accepts booleans.
73
- + Some new examples.
74
- + A proper license.
75
- + Several small improvements.
76
-
77
- * Changes from version 0.3 to 0.4
78
- -------------------------------
79
- + Static check for loops in repetitions and grammars.
80
- + Removed label option in captures.
81
- + The implementation of captures uses less memory.
82
-
83
- * Changes from version 0.2 to 0.3
84
- -------------------------------
85
- + User-defined patterns in Lua.
86
- + Several new captures.
87
-
88
- * Changes from version 0.1 to 0.2
89
- -------------------------------
90
- + Several small corrections.
91
- + Handles embedded zeros like any other character.
92
- + Capture "name" can be any Lua value.
93
- + Unlimited number of captures.
94
- + Match gets an optional initial position.
95
-
96
- (end of HISTORY)
@@ -1,537 +0,0 @@
1
- /*
2
- ** $Id: lpcap.c,v 1.6 2015/06/15 16:09:57 roberto Exp $
3
- ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license)
4
- */
5
-
6
- #include "lua.h"
7
- #include "lauxlib.h"
8
-
9
- #include "lpcap.h"
10
- #include "lptypes.h"
11
-
12
-
13
- #define captype(cap) ((cap)->kind)
14
-
15
- #define isclosecap(cap) (captype(cap) == Cclose)
16
-
17
- #define closeaddr(c) ((c)->s + (c)->siz - 1)
18
-
19
- #define isfullcap(cap) ((cap)->siz != 0)
20
-
21
- #define getfromktable(cs,v) lua_rawgeti((cs)->L, ktableidx((cs)->ptop), v)
22
-
23
- #define pushluaval(cs) getfromktable(cs, (cs)->cap->idx)
24
-
25
-
26
-
27
- /*
28
- ** Put at the cache for Lua values the value indexed by 'v' in ktable
29
- ** of the running pattern (if it is not there yet); returns its index.
30
- */
31
- static int updatecache (CapState *cs, int v) {
32
- int idx = cs->ptop + 1; /* stack index of cache for Lua values */
33
- if (v != cs->valuecached) { /* not there? */
34
- getfromktable(cs, v); /* get value from 'ktable' */
35
- lua_replace(cs->L, idx); /* put it at reserved stack position */
36
- cs->valuecached = v; /* keep track of what is there */
37
- }
38
- return idx;
39
- }
40
-
41
-
42
- static int pushcapture (CapState *cs);
43
-
44
-
45
- /*
46
- ** Goes back in a list of captures looking for an open capture
47
- ** corresponding to a close
48
- */
49
- static Capture *findopen (Capture *cap) {
50
- int n = 0; /* number of closes waiting an open */
51
- for (;;) {
52
- cap--;
53
- if (isclosecap(cap)) n++; /* one more open to skip */
54
- else if (!isfullcap(cap))
55
- if (n-- == 0) return cap;
56
- }
57
- }
58
-
59
-
60
- /*
61
- ** Go to the next capture
62
- */
63
- static void nextcap (CapState *cs) {
64
- Capture *cap = cs->cap;
65
- if (!isfullcap(cap)) { /* not a single capture? */
66
- int n = 0; /* number of opens waiting a close */
67
- for (;;) { /* look for corresponding close */
68
- cap++;
69
- if (isclosecap(cap)) {
70
- if (n-- == 0) break;
71
- }
72
- else if (!isfullcap(cap)) n++;
73
- }
74
- }
75
- cs->cap = cap + 1; /* + 1 to skip last close (or entire single capture) */
76
- }
77
-
78
-
79
- /*
80
- ** Push on the Lua stack all values generated by nested captures inside
81
- ** the current capture. Returns number of values pushed. 'addextra'
82
- ** makes it push the entire match after all captured values. The
83
- ** entire match is pushed also if there are no other nested values,
84
- ** so the function never returns zero.
85
- */
86
- static int pushnestedvalues (CapState *cs, int addextra) {
87
- Capture *co = cs->cap;
88
- if (isfullcap(cs->cap++)) { /* no nested captures? */
89
- lua_pushlstring(cs->L, co->s, co->siz - 1); /* push whole match */
90
- return 1; /* that is it */
91
- }
92
- else {
93
- int n = 0;
94
- while (!isclosecap(cs->cap)) /* repeat for all nested patterns */
95
- n += pushcapture(cs);
96
- if (addextra || n == 0) { /* need extra? */
97
- lua_pushlstring(cs->L, co->s, cs->cap->s - co->s); /* push whole match */
98
- n++;
99
- }
100
- cs->cap++; /* skip close entry */
101
- return n;
102
- }
103
- }
104
-
105
-
106
- /*
107
- ** Push only the first value generated by nested captures
108
- */
109
- static void pushonenestedvalue (CapState *cs) {
110
- int n = pushnestedvalues(cs, 0);
111
- if (n > 1)
112
- lua_pop(cs->L, n - 1); /* pop extra values */
113
- }
114
-
115
-
116
- /*
117
- ** Try to find a named group capture with the name given at the top of
118
- ** the stack; goes backward from 'cap'.
119
- */
120
- static Capture *findback (CapState *cs, Capture *cap) {
121
- lua_State *L = cs->L;
122
- while (cap-- > cs->ocap) { /* repeat until end of list */
123
- if (isclosecap(cap))
124
- cap = findopen(cap); /* skip nested captures */
125
- else if (!isfullcap(cap))
126
- continue; /* opening an enclosing capture: skip and get previous */
127
- if (captype(cap) == Cgroup) {
128
- getfromktable(cs, cap->idx); /* get group name */
129
- if (lp_equal(L, -2, -1)) { /* right group? */
130
- lua_pop(L, 2); /* remove reference name and group name */
131
- return cap;
132
- }
133
- else lua_pop(L, 1); /* remove group name */
134
- }
135
- }
136
- luaL_error(L, "back reference '%s' not found", lua_tostring(L, -1));
137
- return NULL; /* to avoid warnings */
138
- }
139
-
140
-
141
- /*
142
- ** Back-reference capture. Return number of values pushed.
143
- */
144
- static int backrefcap (CapState *cs) {
145
- int n;
146
- Capture *curr = cs->cap;
147
- pushluaval(cs); /* reference name */
148
- cs->cap = findback(cs, curr); /* find corresponding group */
149
- n = pushnestedvalues(cs, 0); /* push group's values */
150
- cs->cap = curr + 1;
151
- return n;
152
- }
153
-
154
-
155
- /*
156
- ** Table capture: creates a new table and populates it with nested
157
- ** captures.
158
- */
159
- static int tablecap (CapState *cs) {
160
- lua_State *L = cs->L;
161
- int n = 0;
162
- lua_newtable(L);
163
- if (isfullcap(cs->cap++))
164
- return 1; /* table is empty */
165
- while (!isclosecap(cs->cap)) {
166
- if (captype(cs->cap) == Cgroup && cs->cap->idx != 0) { /* named group? */
167
- pushluaval(cs); /* push group name */
168
- pushonenestedvalue(cs);
169
- lua_settable(L, -3);
170
- }
171
- else { /* not a named group */
172
- int i;
173
- int k = pushcapture(cs);
174
- for (i = k; i > 0; i--) /* store all values into table */
175
- lua_rawseti(L, -(i + 1), n + i);
176
- n += k;
177
- }
178
- }
179
- cs->cap++; /* skip close entry */
180
- return 1; /* number of values pushed (only the table) */
181
- }
182
-
183
-
184
- /*
185
- ** Table-query capture
186
- */
187
- static int querycap (CapState *cs) {
188
- int idx = cs->cap->idx;
189
- pushonenestedvalue(cs); /* get nested capture */
190
- lua_gettable(cs->L, updatecache(cs, idx)); /* query cap. value at table */
191
- if (!lua_isnil(cs->L, -1))
192
- return 1;
193
- else { /* no value */
194
- lua_pop(cs->L, 1); /* remove nil */
195
- return 0;
196
- }
197
- }
198
-
199
-
200
- /*
201
- ** Fold capture
202
- */
203
- static int foldcap (CapState *cs) {
204
- int n;
205
- lua_State *L = cs->L;
206
- int idx = cs->cap->idx;
207
- if (isfullcap(cs->cap++) || /* no nested captures? */
208
- isclosecap(cs->cap) || /* no nested captures (large subject)? */
209
- (n = pushcapture(cs)) == 0) /* nested captures with no values? */
210
- return luaL_error(L, "no initial value for fold capture");
211
- if (n > 1)
212
- lua_pop(L, n - 1); /* leave only one result for accumulator */
213
- while (!isclosecap(cs->cap)) {
214
- lua_pushvalue(L, updatecache(cs, idx)); /* get folding function */
215
- lua_insert(L, -2); /* put it before accumulator */
216
- n = pushcapture(cs); /* get next capture's values */
217
- lua_call(L, n + 1, 1); /* call folding function */
218
- }
219
- cs->cap++; /* skip close entry */
220
- return 1; /* only accumulator left on the stack */
221
- }
222
-
223
-
224
- /*
225
- ** Function capture
226
- */
227
- static int functioncap (CapState *cs) {
228
- int n;
229
- int top = lua_gettop(cs->L);
230
- pushluaval(cs); /* push function */
231
- n = pushnestedvalues(cs, 0); /* push nested captures */
232
- lua_call(cs->L, n, LUA_MULTRET); /* call function */
233
- return lua_gettop(cs->L) - top; /* return function's results */
234
- }
235
-
236
-
237
- /*
238
- ** Select capture
239
- */
240
- static int numcap (CapState *cs) {
241
- int idx = cs->cap->idx; /* value to select */
242
- if (idx == 0) { /* no values? */
243
- nextcap(cs); /* skip entire capture */
244
- return 0; /* no value produced */
245
- }
246
- else {
247
- int n = pushnestedvalues(cs, 0);
248
- if (n < idx) /* invalid index? */
249
- return luaL_error(cs->L, "no capture '%d'", idx);
250
- else {
251
- lua_pushvalue(cs->L, -(n - idx + 1)); /* get selected capture */
252
- lua_replace(cs->L, -(n + 1)); /* put it in place of 1st capture */
253
- lua_pop(cs->L, n - 1); /* remove other captures */
254
- return 1;
255
- }
256
- }
257
- }
258
-
259
-
260
- /*
261
- ** Return the stack index of the first runtime capture in the given
262
- ** list of captures (or zero if no runtime captures)
263
- */
264
- int finddyncap (Capture *cap, Capture *last) {
265
- for (; cap < last; cap++) {
266
- if (cap->kind == Cruntime)
267
- return cap->idx; /* stack position of first capture */
268
- }
269
- return 0; /* no dynamic captures in this segment */
270
- }
271
-
272
-
273
- /*
274
- ** Calls a runtime capture. Returns number of captures removed by
275
- ** the call, including the initial Cgroup. (Captures to be added are
276
- ** on the Lua stack.)
277
- */
278
- int runtimecap (CapState *cs, Capture *close, const char *s, int *rem) {
279
- int n, id;
280
- lua_State *L = cs->L;
281
- int otop = lua_gettop(L);
282
- Capture *open = findopen(close);
283
- assert(captype(open) == Cgroup);
284
- id = finddyncap(open, close); /* get first dynamic capture argument */
285
- close->kind = Cclose; /* closes the group */
286
- close->s = s;
287
- cs->cap = open; cs->valuecached = 0; /* prepare capture state */
288
- luaL_checkstack(L, 4, "too many runtime captures");
289
- pushluaval(cs); /* push function to be called */
290
- lua_pushvalue(L, SUBJIDX); /* push original subject */
291
- lua_pushinteger(L, s - cs->s + 1); /* push current position */
292
- n = pushnestedvalues(cs, 0); /* push nested captures */
293
- lua_call(L, n + 2, LUA_MULTRET); /* call dynamic function */
294
- if (id > 0) { /* are there old dynamic captures to be removed? */
295
- int i;
296
- for (i = id; i <= otop; i++)
297
- lua_remove(L, id); /* remove old dynamic captures */
298
- *rem = otop - id + 1; /* total number of dynamic captures removed */
299
- }
300
- else
301
- *rem = 0; /* no dynamic captures removed */
302
- return close - open; /* number of captures of all kinds removed */
303
- }
304
-
305
-
306
- /*
307
- ** Auxiliary structure for substitution and string captures: keep
308
- ** information about nested captures for future use, avoiding to push
309
- ** string results into Lua
310
- */
311
- typedef struct StrAux {
312
- int isstring; /* whether capture is a string */
313
- union {
314
- Capture *cp; /* if not a string, respective capture */
315
- struct { /* if it is a string... */
316
- const char *s; /* ... starts here */
317
- const char *e; /* ... ends here */
318
- } s;
319
- } u;
320
- } StrAux;
321
-
322
- #define MAXSTRCAPS 10
323
-
324
- /*
325
- ** Collect values from current capture into array 'cps'. Current
326
- ** capture must be Cstring (first call) or Csimple (recursive calls).
327
- ** (In first call, fills %0 with whole match for Cstring.)
328
- ** Returns number of elements in the array that were filled.
329
- */
330
- static int getstrcaps (CapState *cs, StrAux *cps, int n) {
331
- int k = n++;
332
- cps[k].isstring = 1; /* get string value */
333
- cps[k].u.s.s = cs->cap->s; /* starts here */
334
- if (!isfullcap(cs->cap++)) { /* nested captures? */
335
- while (!isclosecap(cs->cap)) { /* traverse them */
336
- if (n >= MAXSTRCAPS) /* too many captures? */
337
- nextcap(cs); /* skip extra captures (will not need them) */
338
- else if (captype(cs->cap) == Csimple) /* string? */
339
- n = getstrcaps(cs, cps, n); /* put info. into array */
340
- else {
341
- cps[n].isstring = 0; /* not a string */
342
- cps[n].u.cp = cs->cap; /* keep original capture */
343
- nextcap(cs);
344
- n++;
345
- }
346
- }
347
- cs->cap++; /* skip close */
348
- }
349
- cps[k].u.s.e = closeaddr(cs->cap - 1); /* ends here */
350
- return n;
351
- }
352
-
353
-
354
- /*
355
- ** add next capture value (which should be a string) to buffer 'b'
356
- */
357
- static int addonestring (luaL_Buffer *b, CapState *cs, const char *what);
358
-
359
-
360
- /*
361
- ** String capture: add result to buffer 'b' (instead of pushing
362
- ** it into the stack)
363
- */
364
- static void stringcap (luaL_Buffer *b, CapState *cs) {
365
- StrAux cps[MAXSTRCAPS];
366
- int n;
367
- size_t len, i;
368
- const char *fmt; /* format string */
369
- fmt = lua_tolstring(cs->L, updatecache(cs, cs->cap->idx), &len);
370
- n = getstrcaps(cs, cps, 0) - 1; /* collect nested captures */
371
- for (i = 0; i < len; i++) { /* traverse them */
372
- if (fmt[i] != '%') /* not an escape? */
373
- luaL_addchar(b, fmt[i]); /* add it to buffer */
374
- else if (fmt[++i] < '0' || fmt[i] > '9') /* not followed by a digit? */
375
- luaL_addchar(b, fmt[i]); /* add to buffer */
376
- else {
377
- int l = fmt[i] - '0'; /* capture index */
378
- if (l > n)
379
- luaL_error(cs->L, "invalid capture index (%d)", l);
380
- else if (cps[l].isstring)
381
- luaL_addlstring(b, cps[l].u.s.s, cps[l].u.s.e - cps[l].u.s.s);
382
- else {
383
- Capture *curr = cs->cap;
384
- cs->cap = cps[l].u.cp; /* go back to evaluate that nested capture */
385
- if (!addonestring(b, cs, "capture"))
386
- luaL_error(cs->L, "no values in capture index %d", l);
387
- cs->cap = curr; /* continue from where it stopped */
388
- }
389
- }
390
- }
391
- }
392
-
393
-
394
- /*
395
- ** Substitution capture: add result to buffer 'b'
396
- */
397
- static void substcap (luaL_Buffer *b, CapState *cs) {
398
- const char *curr = cs->cap->s;
399
- if (isfullcap(cs->cap)) /* no nested captures? */
400
- luaL_addlstring(b, curr, cs->cap->siz - 1); /* keep original text */
401
- else {
402
- cs->cap++; /* skip open entry */
403
- while (!isclosecap(cs->cap)) { /* traverse nested captures */
404
- const char *next = cs->cap->s;
405
- luaL_addlstring(b, curr, next - curr); /* add text up to capture */
406
- if (addonestring(b, cs, "replacement"))
407
- curr = closeaddr(cs->cap - 1); /* continue after match */
408
- else /* no capture value */
409
- curr = next; /* keep original text in final result */
410
- }
411
- luaL_addlstring(b, curr, cs->cap->s - curr); /* add last piece of text */
412
- }
413
- cs->cap++; /* go to next capture */
414
- }
415
-
416
-
417
- /*
418
- ** Evaluates a capture and adds its first value to buffer 'b'; returns
419
- ** whether there was a value
420
- */
421
- static int addonestring (luaL_Buffer *b, CapState *cs, const char *what) {
422
- switch (captype(cs->cap)) {
423
- case Cstring:
424
- stringcap(b, cs); /* add capture directly to buffer */
425
- return 1;
426
- case Csubst:
427
- substcap(b, cs); /* add capture directly to buffer */
428
- return 1;
429
- default: {
430
- lua_State *L = cs->L;
431
- int n = pushcapture(cs);
432
- if (n > 0) {
433
- if (n > 1) lua_pop(L, n - 1); /* only one result */
434
- if (!lua_isstring(L, -1))
435
- luaL_error(L, "invalid %s value (a %s)", what, luaL_typename(L, -1));
436
- luaL_addvalue(b);
437
- }
438
- return n;
439
- }
440
- }
441
- }
442
-
443
-
444
- /*
445
- ** Push all values of the current capture into the stack; returns
446
- ** number of values pushed
447
- */
448
- static int pushcapture (CapState *cs) {
449
- lua_State *L = cs->L;
450
- luaL_checkstack(L, 4, "too many captures");
451
- switch (captype(cs->cap)) {
452
- case Cposition: {
453
- lua_pushinteger(L, cs->cap->s - cs->s + 1);
454
- cs->cap++;
455
- return 1;
456
- }
457
- case Cconst: {
458
- pushluaval(cs);
459
- cs->cap++;
460
- return 1;
461
- }
462
- case Carg: {
463
- int arg = (cs->cap++)->idx;
464
- if (arg + FIXEDARGS > cs->ptop)
465
- return luaL_error(L, "reference to absent extra argument #%d", arg);
466
- lua_pushvalue(L, arg + FIXEDARGS);
467
- return 1;
468
- }
469
- case Csimple: {
470
- int k = pushnestedvalues(cs, 1);
471
- lua_insert(L, -k); /* make whole match be first result */
472
- return k;
473
- }
474
- case Cruntime: {
475
- lua_pushvalue(L, (cs->cap++)->idx); /* value is in the stack */
476
- return 1;
477
- }
478
- case Cstring: {
479
- luaL_Buffer b;
480
- luaL_buffinit(L, &b);
481
- stringcap(&b, cs);
482
- luaL_pushresult(&b);
483
- return 1;
484
- }
485
- case Csubst: {
486
- luaL_Buffer b;
487
- luaL_buffinit(L, &b);
488
- substcap(&b, cs);
489
- luaL_pushresult(&b);
490
- return 1;
491
- }
492
- case Cgroup: {
493
- if (cs->cap->idx == 0) /* anonymous group? */
494
- return pushnestedvalues(cs, 0); /* add all nested values */
495
- else { /* named group: add no values */
496
- nextcap(cs); /* skip capture */
497
- return 0;
498
- }
499
- }
500
- case Cbackref: return backrefcap(cs);
501
- case Ctable: return tablecap(cs);
502
- case Cfunction: return functioncap(cs);
503
- case Cnum: return numcap(cs);
504
- case Cquery: return querycap(cs);
505
- case Cfold: return foldcap(cs);
506
- default: assert(0); return 0;
507
- }
508
- }
509
-
510
-
511
- /*
512
- ** Prepare a CapState structure and traverse the entire list of
513
- ** captures in the stack pushing its results. 's' is the subject
514
- ** string, 'r' is the final position of the match, and 'ptop'
515
- ** the index in the stack where some useful values were pushed.
516
- ** Returns the number of results pushed. (If the list produces no
517
- ** results, push the final position of the match.)
518
- */
519
- int getcaptures (lua_State *L, const char *s, const char *r, int ptop) {
520
- Capture *capture = (Capture *)lua_touserdata(L, caplistidx(ptop));
521
- int n = 0;
522
- if (!isclosecap(capture)) { /* is there any capture? */
523
- CapState cs;
524
- cs.ocap = cs.cap = capture; cs.L = L;
525
- cs.s = s; cs.valuecached = 0; cs.ptop = ptop;
526
- do { /* collect their values */
527
- n += pushcapture(&cs);
528
- } while (!isclosecap(cs.cap));
529
- }
530
- if (n == 0) { /* no capture values? */
531
- lua_pushinteger(L, r - s + 1); /* return only end position */
532
- n = 1;
533
- }
534
- return n;
535
- }
536
-
537
-