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,13 +0,0 @@
1
- *~
2
- .*~
3
- \#*\#
4
- .\#*\#
5
-
6
- .DS_Store
7
- .project
8
- .settings
9
-
10
- *.o
11
- *.so
12
-
13
- build/
@@ -1,45 +0,0 @@
1
- # If Lua is installed in a non-standard location, please set the LUA_DIR
2
- # environment variable to point to prefix for the install. Eg:
3
- # Unix: export LUA_DIR=/home/user/pkg
4
- # Windows: set LUA_DIR=c:\lua51
5
-
6
- project(lua-cmsgpack C)
7
- cmake_minimum_required(VERSION 2.6)
8
-
9
- if(NOT CMAKE_BUILD_TYPE)
10
- set(CMAKE_BUILD_TYPE Release CACHE STRING
11
- "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
12
- FORCE)
13
- endif()
14
-
15
- find_package(Lua51 REQUIRED)
16
- include_directories(${LUA_INCLUDE_DIR})
17
-
18
- if(APPLE)
19
- set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS
20
- "${CMAKE_SHARED_MODULE_CREATE_C_FLAGS} -undefined dynamic_lookup")
21
- endif()
22
-
23
- if(WIN32)
24
- # Win32 modules need to be linked to the Lua library.
25
- set(_MODULE_LINK ${LUA_LIBRARY} ${_MODULE_LINK})
26
- set(_lua_module_dir "${_lua_lib_dir}")
27
- else()
28
- set(_lua_module_dir "${_lua_lib_dir}/lua/5.1")
29
- endif()
30
-
31
- option(Build32Bit "Build 32-bit Library" OFF)
32
-
33
- set(CMAKE_C_FLAGS "-O2 -g -ggdb -Wall -pedantic -std=c99")
34
- add_library(cmsgpack MODULE lua_cmsgpack.c)
35
- set_target_properties(cmsgpack PROPERTIES PREFIX "")
36
-
37
- if(Build32Bit)
38
- set_target_properties(cmsgpack
39
- PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32")
40
- endif()
41
-
42
- target_link_libraries(cmsgpack ${_MODULE_LINK})
43
- install(TARGETS cmsgpack DESTINATION "${_lua_module_dir}")
44
-
45
- # vi:ai et sw=4 ts=4:
@@ -1,115 +0,0 @@
1
- README for lua-cmsgpack.c
2
- ===
3
-
4
- Lua-cmsgpack is a [MessagePack](http://msgpack.org) implementation and bindings for
5
- Lua 5.1/5.2/5.3 in a self contained C file without external dependencies.
6
-
7
- This library is open source software licensed under the BSD two-clause license.
8
-
9
- INSTALLATION
10
- ---
11
-
12
- Using LuaRocks (http://luarocks.org):
13
-
14
- * Install current stable release:
15
-
16
- sudo luarocks install lua-cmsgpack
17
-
18
- * Install current Git master head from GitHub:
19
-
20
- sudo luarocks install lua-cmsgpack --from=rocks-cvs
21
-
22
- * Install from current working copy
23
-
24
- cd lua-cmsgpack/
25
- sudo luarocks make rockspec/lua-cmsgpack-scm-1.rockspec
26
-
27
- If you embed Lua and all modules into your C project, just add the
28
- `lua_cmsgpack.c` file and call the following function after creating the Lua
29
- interpreter:
30
-
31
- luaopen_cmsgpack(L);
32
-
33
- USAGE
34
- ---
35
-
36
- The exported API is very simple, consisting of four functions:
37
-
38
- Basic API:
39
-
40
- msgpack = cmsgpack.pack(lua_object1, lua_object2, ..., lua_objectN)
41
- lua_object1, lua_object2, ..., lua_objectN = cmsgpack.unpack(msgpack)
42
-
43
- Detailed API giving you more control over unpacking multiple values:
44
-
45
- resume_offset, lua_object1 = cmsgpack.unpack_one(msgpack)
46
- resume_offset1, lua_object2 = cmsgpack.unpack_one(msgpack, resume_offset)
47
- ...
48
- -1, lua_objectN = cmsgpack.unpack_one(msgpack, resume_offset_previous)
49
-
50
- resume_offset, lua_object1, lua_object2 = cmsgpack.unpack_limit(msgpack, 2)
51
- resume_offset2, lua_object3 = cmsgpack.unpack_limit(msgpack, 1, resume_offset1)
52
-
53
- Functions:
54
-
55
- - `pack(arg1, arg2, ..., argn)` - pack any number of lua objects into one msgpack stream. returns: msgpack
56
- - `unpack(msgpack)` - unpack all objects in msgpack to individual return values. returns: object1, object2, ..., objectN
57
- - `unpack_one(msgpack); unpack_one(msgpack, offset)` - unpacks the first object after offset. returns: offset, object
58
- - `unpack_limit(msgpack, limit); unpack_limit(msgpack, limit, offset)` - unpacks the first `limit` objects and returns: offset, object1, objet2, ..., objectN (up to limit, but may return fewer than limit if not that many objects remain to be unpacked)
59
-
60
- When you reach the end of your input stream with `unpack_one` or `unpack_limit`, an offset of `-1` is returned.
61
-
62
- You may `require "msgpack"` or you may `require "msgpack.safe"`. The safe version returns errors as (nil, errstring).
63
-
64
- However because of the nature of Lua numerical and table type a few behavior
65
- of the library must be well understood to avoid problems:
66
-
67
- * A table is converted into a MessagePack array type only if *all* the keys are
68
- composed of incrementing integers starting at 1 end ending at N, without holes,
69
- without additional non numerical keys. All the other tables are converted into
70
- maps.
71
- * An empty table is always converted into a MessagePack array, the rationale is that empty lists are much more common than empty maps (usually used to represent objects with fields).
72
- * A Lua number is converted into an integer type if floor(number) == number, otherwise it is converted into the MessagePack float or double value.
73
- * When a Lua number is converted to float or double, the former is preferred if there is no loss of precision compared to the double representation.
74
- * When a MessagePack big integer (64 bit) is converted to a Lua number it is possible that the resulting number will not represent the original number but just an approximation. This is unavoidable because the Lua numerical type is usually a double precision floating point type.
75
-
76
- TESTING
77
- ---
78
-
79
- Build and test:
80
-
81
- mkdir build; cd build
82
- cmake ..
83
- make
84
- lua ../test.lua
85
-
86
- You can build a 32-bit module on a 64-bit platform with:
87
-
88
- mkdir build; cd build
89
- cmake -DBuild32Bit=ON ..
90
- make
91
- lua ../test.lua
92
-
93
- NESTED TABLES
94
- ---
95
- Nested tables are handled correctly up to `LUACMSGPACK_MAX_NESTING` levels of
96
- nesting (that is set to 16 by default).
97
- Every table that is nested at a greater level than the maxium is encoded
98
- as MessagePack nil value.
99
-
100
- It is worth to note that in Lua it is possible to create tables that mutually
101
- refer to each other, creating a cycle. For example:
102
-
103
- a = {x=nil,y=5}
104
- b = {x=a}
105
- a['x'] = b
106
-
107
- This condition will simply make the encoder reach the max level of nesting,
108
- thus avoiding an infinite loop.
109
-
110
- CREDITS
111
- ---
112
-
113
- This library was written by Salvatore Sanfilippo for Redis, but is maintained as a separated project by the author.
114
-
115
- Some of the test vectors in "test.lua" are obtained from the Javascript [MessagePack-JS library](https://github.com/cuzic/MessagePack-JS).
@@ -1,970 +0,0 @@
1
- #include <math.h>
2
- #include <stdlib.h>
3
- #include <stdint.h>
4
- #include <string.h>
5
- #include <assert.h>
6
-
7
- #include "lua.h"
8
- #include "lauxlib.h"
9
-
10
- #define LUACMSGPACK_NAME "cmsgpack"
11
- #define LUACMSGPACK_SAFE_NAME "cmsgpack_safe"
12
- #define LUACMSGPACK_VERSION "lua-cmsgpack 0.4.0.1"
13
- #define LUACMSGPACK_COPYRIGHT "Copyright (C) 2012, Salvatore Sanfilippo"
14
- #define LUACMSGPACK_DESCRIPTION "MessagePack C implementation for Lua"
15
-
16
- /* Allows a preprocessor directive to override MAX_NESTING */
17
- #ifndef LUACMSGPACK_MAX_NESTING
18
- #define LUACMSGPACK_MAX_NESTING 16 /* Max tables nesting. */
19
- #endif
20
-
21
- /* Check if float or double can be an integer without loss of precision */
22
- #define IS_INT_TYPE_EQUIVALENT(x, T) (!isinf(x) && (T)(x) == (x))
23
-
24
- #define IS_INT64_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int64_t)
25
- #define IS_INT_EQUIVALENT(x) IS_INT_TYPE_EQUIVALENT(x, int)
26
-
27
- /* If size of pointer is equal to a 4 byte integer, we're on 32 bits. */
28
- #if UINTPTR_MAX == UINT_MAX
29
- #define BITS_32 1
30
- #else
31
- #define BITS_32 0
32
- #endif
33
-
34
- #if BITS_32
35
- #define lua_pushunsigned(L, n) lua_pushnumber(L, n)
36
- #else
37
- #define lua_pushunsigned(L, n) lua_pushinteger(L, n)
38
- #endif
39
-
40
- /* =============================================================================
41
- * MessagePack implementation and bindings for Lua 5.1/5.2.
42
- * Copyright(C) 2012 Salvatore Sanfilippo <antirez@gmail.com>
43
- *
44
- * http://github.com/antirez/lua-cmsgpack
45
- *
46
- * For MessagePack specification check the following web site:
47
- * http://wiki.msgpack.org/display/MSGPACK/Format+specification
48
- *
49
- * See Copyright Notice at the end of this file.
50
- *
51
- * CHANGELOG:
52
- * 19-Feb-2012 (ver 0.1.0): Initial release.
53
- * 20-Feb-2012 (ver 0.2.0): Tables encoding improved.
54
- * 20-Feb-2012 (ver 0.2.1): Minor bug fixing.
55
- * 20-Feb-2012 (ver 0.3.0): Module renamed lua-cmsgpack (was lua-msgpack).
56
- * 04-Apr-2014 (ver 0.3.1): Lua 5.2 support and minor bug fix.
57
- * 07-Apr-2014 (ver 0.4.0): Multiple pack/unpack, lua allocator, efficiency.
58
- *
59
- * Immunio Changes:
60
- * 09-2015 (ver 0.4.0.1): Add an metatable check for tables.
61
- * ========================================================================== */
62
-
63
- /* -------------------------- Endian conversion --------------------------------
64
- * We use it only for floats and doubles, all the other conversions performed
65
- * in an endian independent fashion. So the only thing we need is a function
66
- * that swaps a binary string if arch is little endian (and left it untouched
67
- * otherwise). */
68
-
69
- /* Reverse memory bytes if arch is little endian. Given the conceptual
70
- * simplicity of the Lua build system we prefer check for endianess at runtime.
71
- * The performance difference should be acceptable. */
72
- void memrevifle(void *ptr, size_t len) {
73
- unsigned char *p = (unsigned char *)ptr,
74
- *e = (unsigned char *)p+len-1,
75
- aux;
76
- int test = 1;
77
- unsigned char *testp = (unsigned char*) &test;
78
-
79
- if (testp[0] == 0) return; /* Big endian, nothing to do. */
80
- len /= 2;
81
- while(len--) {
82
- aux = *p;
83
- *p = *e;
84
- *e = aux;
85
- p++;
86
- e--;
87
- }
88
- }
89
-
90
- /* ---------------------------- String buffer ----------------------------------
91
- * This is a simple implementation of string buffers. The only operation
92
- * supported is creating empty buffers and appending bytes to it.
93
- * The string buffer uses 2x preallocation on every realloc for O(N) append
94
- * behavior. */
95
-
96
- typedef struct mp_buf {
97
- lua_State *L;
98
- unsigned char *b;
99
- size_t len, free;
100
- } mp_buf;
101
-
102
- void *mp_realloc(lua_State *L, void *target, size_t osize,size_t nsize) {
103
- void *(*local_realloc) (void *, void *, size_t osize, size_t nsize) = NULL;
104
- void *ud;
105
-
106
- local_realloc = lua_getallocf(L, &ud);
107
-
108
- return local_realloc(ud, target, osize, nsize);
109
- }
110
-
111
- mp_buf *mp_buf_new(lua_State *L) {
112
- mp_buf *buf = NULL;
113
-
114
- /* Old size = 0; new size = sizeof(*buf) */
115
- buf = (mp_buf*)mp_realloc(L, NULL, 0, sizeof(*buf));
116
-
117
- buf->L = L;
118
- buf->b = NULL;
119
- buf->len = buf->free = 0;
120
- return buf;
121
- }
122
-
123
- void mp_buf_append(mp_buf *buf, const unsigned char *s, size_t len) {
124
- if (buf->free < len) {
125
- size_t newlen = buf->len+len;
126
-
127
- buf->b = (unsigned char*)mp_realloc(buf->L, buf->b, buf->len, newlen*2);
128
- buf->free = newlen;
129
- }
130
- memcpy(buf->b+buf->len,s,len);
131
- buf->len += len;
132
- buf->free -= len;
133
- }
134
-
135
- void mp_buf_free(mp_buf *buf) {
136
- mp_realloc(buf->L, buf->b, buf->len, 0); /* realloc to 0 = free */
137
- mp_realloc(buf->L, buf, sizeof(*buf), 0);
138
- }
139
-
140
- /* ---------------------------- String cursor ----------------------------------
141
- * This simple data structure is used for parsing. Basically you create a cursor
142
- * using a string pointer and a length, then it is possible to access the
143
- * current string position with cursor->p, check the remaining length
144
- * in cursor->left, and finally consume more string using
145
- * mp_cur_consume(cursor,len), to advance 'p' and subtract 'left'.
146
- * An additional field cursor->error is set to zero on initialization and can
147
- * be used to report errors. */
148
-
149
- #define MP_CUR_ERROR_NONE 0
150
- #define MP_CUR_ERROR_EOF 1 /* Not enough data to complete operation. */
151
- #define MP_CUR_ERROR_BADFMT 2 /* Bad data format */
152
-
153
- typedef struct mp_cur {
154
- const unsigned char *p;
155
- size_t left;
156
- int err;
157
- } mp_cur;
158
-
159
- void mp_cur_init(mp_cur *cursor, const unsigned char *s, size_t len) {
160
- cursor->p = s;
161
- cursor->left = len;
162
- cursor->err = MP_CUR_ERROR_NONE;
163
- }
164
-
165
- #define mp_cur_consume(_c,_len) do { _c->p += _len; _c->left -= _len; } while(0)
166
-
167
- /* When there is not enough room we set an error in the cursor and return. This
168
- * is very common across the code so we have a macro to make the code look
169
- * a bit simpler. */
170
- #define mp_cur_need(_c,_len) do { \
171
- if (_c->left < _len) { \
172
- _c->err = MP_CUR_ERROR_EOF; \
173
- return; \
174
- } \
175
- } while(0)
176
-
177
- /* ------------------------- Low level MP encoding -------------------------- */
178
-
179
- void mp_encode_bytes(mp_buf *buf, const unsigned char *s, size_t len) {
180
- unsigned char hdr[5];
181
- int hdrlen;
182
-
183
- if (len < 32) {
184
- hdr[0] = 0xa0 | (len&0xff); /* fix raw */
185
- hdrlen = 1;
186
- } else if (len <= 0xffff) {
187
- hdr[0] = 0xda;
188
- hdr[1] = (len&0xff00)>>8;
189
- hdr[2] = len&0xff;
190
- hdrlen = 3;
191
- } else {
192
- hdr[0] = 0xdb;
193
- hdr[1] = (len&0xff000000)>>24;
194
- hdr[2] = (len&0xff0000)>>16;
195
- hdr[3] = (len&0xff00)>>8;
196
- hdr[4] = len&0xff;
197
- hdrlen = 5;
198
- }
199
- mp_buf_append(buf,hdr,hdrlen);
200
- mp_buf_append(buf,s,len);
201
- }
202
-
203
- /* we assume IEEE 754 internal format for single and double precision floats. */
204
- void mp_encode_double(mp_buf *buf, double d) {
205
- unsigned char b[9];
206
- float f = d;
207
-
208
- assert(sizeof(f) == 4 && sizeof(d) == 8);
209
- if (d == (double)f) {
210
- b[0] = 0xca; /* float IEEE 754 */
211
- memcpy(b+1,&f,4);
212
- memrevifle(b+1,4);
213
- mp_buf_append(buf,b,5);
214
- } else if (sizeof(d) == 8) {
215
- b[0] = 0xcb; /* double IEEE 754 */
216
- memcpy(b+1,&d,8);
217
- memrevifle(b+1,8);
218
- mp_buf_append(buf,b,9);
219
- }
220
- }
221
-
222
- void mp_encode_int(mp_buf *buf, int64_t n) {
223
- unsigned char b[9];
224
- int enclen;
225
-
226
- if (n >= 0) {
227
- if (n <= 127) {
228
- b[0] = n & 0x7f; /* positive fixnum */
229
- enclen = 1;
230
- } else if (n <= 0xff) {
231
- b[0] = 0xcc; /* uint 8 */
232
- b[1] = n & 0xff;
233
- enclen = 2;
234
- } else if (n <= 0xffff) {
235
- b[0] = 0xcd; /* uint 16 */
236
- b[1] = (n & 0xff00) >> 8;
237
- b[2] = n & 0xff;
238
- enclen = 3;
239
- } else if (n <= 0xffffffffLL) {
240
- b[0] = 0xce; /* uint 32 */
241
- b[1] = (n & 0xff000000) >> 24;
242
- b[2] = (n & 0xff0000) >> 16;
243
- b[3] = (n & 0xff00) >> 8;
244
- b[4] = n & 0xff;
245
- enclen = 5;
246
- } else {
247
- b[0] = 0xcf; /* uint 64 */
248
- b[1] = (n & 0xff00000000000000LL) >> 56;
249
- b[2] = (n & 0xff000000000000LL) >> 48;
250
- b[3] = (n & 0xff0000000000LL) >> 40;
251
- b[4] = (n & 0xff00000000LL) >> 32;
252
- b[5] = (n & 0xff000000) >> 24;
253
- b[6] = (n & 0xff0000) >> 16;
254
- b[7] = (n & 0xff00) >> 8;
255
- b[8] = n & 0xff;
256
- enclen = 9;
257
- }
258
- } else {
259
- if (n >= -32) {
260
- b[0] = ((signed char)n); /* negative fixnum */
261
- enclen = 1;
262
- } else if (n >= -128) {
263
- b[0] = 0xd0; /* int 8 */
264
- b[1] = n & 0xff;
265
- enclen = 2;
266
- } else if (n >= -32768) {
267
- b[0] = 0xd1; /* int 16 */
268
- b[1] = (n & 0xff00) >> 8;
269
- b[2] = n & 0xff;
270
- enclen = 3;
271
- } else if (n >= -2147483648LL) {
272
- b[0] = 0xd2; /* int 32 */
273
- b[1] = (n & 0xff000000) >> 24;
274
- b[2] = (n & 0xff0000) >> 16;
275
- b[3] = (n & 0xff00) >> 8;
276
- b[4] = n & 0xff;
277
- enclen = 5;
278
- } else {
279
- b[0] = 0xd3; /* int 64 */
280
- b[1] = (n & 0xff00000000000000LL) >> 56;
281
- b[2] = (n & 0xff000000000000LL) >> 48;
282
- b[3] = (n & 0xff0000000000LL) >> 40;
283
- b[4] = (n & 0xff00000000LL) >> 32;
284
- b[5] = (n & 0xff000000) >> 24;
285
- b[6] = (n & 0xff0000) >> 16;
286
- b[7] = (n & 0xff00) >> 8;
287
- b[8] = n & 0xff;
288
- enclen = 9;
289
- }
290
- }
291
- mp_buf_append(buf,b,enclen);
292
- }
293
-
294
- void mp_encode_array(mp_buf *buf, int64_t n) {
295
- unsigned char b[5];
296
- int enclen;
297
-
298
- if (n <= 15) {
299
- b[0] = 0x90 | (n & 0xf); /* fix array */
300
- enclen = 1;
301
- } else if (n <= 65535) {
302
- b[0] = 0xdc; /* array 16 */
303
- b[1] = (n & 0xff00) >> 8;
304
- b[2] = n & 0xff;
305
- enclen = 3;
306
- } else {
307
- b[0] = 0xdd; /* array 32 */
308
- b[1] = (n & 0xff000000) >> 24;
309
- b[2] = (n & 0xff0000) >> 16;
310
- b[3] = (n & 0xff00) >> 8;
311
- b[4] = n & 0xff;
312
- enclen = 5;
313
- }
314
- mp_buf_append(buf,b,enclen);
315
- }
316
-
317
- void mp_encode_map(mp_buf *buf, int64_t n) {
318
- unsigned char b[5];
319
- int enclen;
320
-
321
- if (n <= 15) {
322
- b[0] = 0x80 | (n & 0xf); /* fix map */
323
- enclen = 1;
324
- } else if (n <= 65535) {
325
- b[0] = 0xde; /* map 16 */
326
- b[1] = (n & 0xff00) >> 8;
327
- b[2] = n & 0xff;
328
- enclen = 3;
329
- } else {
330
- b[0] = 0xdf; /* map 32 */
331
- b[1] = (n & 0xff000000) >> 24;
332
- b[2] = (n & 0xff0000) >> 16;
333
- b[3] = (n & 0xff00) >> 8;
334
- b[4] = n & 0xff;
335
- enclen = 5;
336
- }
337
- mp_buf_append(buf,b,enclen);
338
- }
339
-
340
- /* --------------------------- Lua types encoding --------------------------- */
341
-
342
- void mp_encode_lua_string(lua_State *L, mp_buf *buf) {
343
- size_t len;
344
- const char *s;
345
-
346
- s = lua_tolstring(L,-1,&len);
347
- mp_encode_bytes(buf,(const unsigned char*)s,len);
348
- }
349
-
350
- void mp_encode_lua_bool(lua_State *L, mp_buf *buf) {
351
- unsigned char b = lua_toboolean(L,-1) ? 0xc3 : 0xc2;
352
- mp_buf_append(buf,&b,1);
353
- }
354
-
355
- /* Lua 5.3 has a built in 64-bit integer type */
356
- void mp_encode_lua_integer(lua_State *L, mp_buf *buf) {
357
- #if (LUA_VERSION_NUM < 503) && BITS_32
358
- lua_Number i = lua_tonumber(L,-1);
359
- #else
360
- lua_Integer i = lua_tointeger(L,-1);
361
- #endif
362
- mp_encode_int(buf, (int64_t)i);
363
- }
364
-
365
- /* Lua 5.2 and lower only has 64-bit doubles, so we need to
366
- * detect if the double may be representable as an int
367
- * for Lua < 5.3 */
368
- void mp_encode_lua_number(lua_State *L, mp_buf *buf) {
369
- lua_Number n = lua_tonumber(L,-1);
370
-
371
- if (IS_INT64_EQUIVALENT(n)) {
372
- mp_encode_lua_integer(L, buf);
373
- } else {
374
- mp_encode_double(buf,(double)n);
375
- }
376
- }
377
-
378
- void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level);
379
-
380
- /* Convert a lua table into a message pack list. */
381
- void mp_encode_lua_table_as_array(lua_State *L, mp_buf *buf, int level) {
382
- #if LUA_VERSION_NUM < 502
383
- size_t len = lua_objlen(L,-1), j;
384
- #else
385
- size_t len = lua_rawlen(L,-1), j;
386
- #endif
387
-
388
- mp_encode_array(buf,len);
389
- for (j = 1; j <= len; j++) {
390
- lua_pushnumber(L,j);
391
- lua_gettable(L,-2);
392
- mp_encode_lua_type(L,buf,level+1);
393
- }
394
- }
395
-
396
- /* Convert a lua table into a message pack key-value map. */
397
- void mp_encode_lua_table_as_map(lua_State *L, mp_buf *buf, int level) {
398
- size_t len = 0;
399
-
400
- /* First step: count keys into table. No other way to do it with the
401
- * Lua API, we need to iterate a first time. Note that an alternative
402
- * would be to do a single run, and then hack the buffer to insert the
403
- * map opcodes for message pack. Too hackish for this lib. */
404
- lua_pushnil(L);
405
- while(lua_next(L,-2)) {
406
- lua_pop(L,1); /* remove value, keep key for next iteration. */
407
- len++;
408
- }
409
-
410
- /* Step two: actually encoding of the map. */
411
- mp_encode_map(buf,len);
412
- lua_pushnil(L);
413
- while(lua_next(L,-2)) {
414
- /* Stack: ... key value */
415
- lua_pushvalue(L,-2); /* Stack: ... key value key */
416
- mp_encode_lua_type(L,buf,level+1); /* encode key */
417
- mp_encode_lua_type(L,buf,level+1); /* encode val */
418
- }
419
- }
420
-
421
- /* Returns true if the Lua table on top of the stack is exclusively composed
422
- * of keys from numerical keys from 1 up to N, with N being the total number
423
- * of elements, without any hole in the middle. */
424
- int table_is_an_array(lua_State *L) {
425
- int count = 0, max = 0;
426
- #if LUA_VERSION_NUM < 503
427
- lua_Number n;
428
- #else
429
- lua_Integer n;
430
- #endif
431
-
432
- /* Stack top on function entry */
433
- int stacktop;
434
-
435
- stacktop = lua_gettop(L);
436
-
437
- if (lua_getmetatable(L, -1)) {
438
- lua_pushliteral(L, "__is_dict");
439
- lua_rawget(L, -2);
440
- if (lua_isboolean(L, -1) && lua_toboolean(L, -1)) {
441
- lua_settop(L, stacktop);
442
- return 0;
443
- }
444
- lua_settop(L, stacktop);
445
- }
446
-
447
- lua_pushnil(L);
448
- while(lua_next(L,-2)) {
449
- /* Stack: ... key value */
450
- lua_pop(L,1); /* Stack: ... key */
451
- /* The <= 0 check is valid here because we're comparing indexes. */
452
- #if LUA_VERSION_NUM < 503
453
- if ((LUA_TNUMBER != lua_type(L,-1)) || (n = lua_tonumber(L, -1)) <= 0 ||
454
- !IS_INT_EQUIVALENT(n))
455
- #else
456
- if (!lua_isinteger(L,-1) || (n = lua_tointeger(L, -1)) <= 0)
457
- #endif
458
- {
459
- lua_settop(L, stacktop);
460
- return 0;
461
- }
462
- max = (n > max ? n : max);
463
- count++;
464
- }
465
- /* We have the total number of elements in "count". Also we have
466
- * the max index encountered in "max". We can't reach this code
467
- * if there are indexes <= 0. If you also note that there can not be
468
- * repeated keys into a table, you have that if max==count you are sure
469
- * that there are all the keys form 1 to count (both included). */
470
- lua_settop(L, stacktop);
471
- return max == count;
472
- }
473
-
474
- /* If the length operator returns non-zero, that is, there is at least
475
- * an object at key '1', we serialize to message pack list. Otherwise
476
- * we use a map. */
477
- void mp_encode_lua_table(lua_State *L, mp_buf *buf, int level) {
478
- if (table_is_an_array(L))
479
- mp_encode_lua_table_as_array(L,buf,level);
480
- else
481
- mp_encode_lua_table_as_map(L,buf,level);
482
- }
483
-
484
- void mp_encode_lua_null(lua_State *L, mp_buf *buf) {
485
- unsigned char b[1];
486
- (void)L;
487
-
488
- b[0] = 0xc0;
489
- mp_buf_append(buf,b,1);
490
- }
491
-
492
- void mp_encode_lua_type(lua_State *L, mp_buf *buf, int level) {
493
- int t = lua_type(L,-1);
494
-
495
- /* Limit the encoding of nested tables to a specified maximum depth, so that
496
- * we survive when called against circular references in tables. */
497
- if (t == LUA_TTABLE && level == LUACMSGPACK_MAX_NESTING) t = LUA_TNIL;
498
- switch(t) {
499
- case LUA_TSTRING: mp_encode_lua_string(L,buf); break;
500
- case LUA_TBOOLEAN: mp_encode_lua_bool(L,buf); break;
501
- case LUA_TNUMBER:
502
- #if LUA_VERSION_NUM < 503
503
- mp_encode_lua_number(L,buf); break;
504
- #else
505
- if (lua_isinteger(L, -1)) {
506
- mp_encode_lua_integer(L, buf);
507
- } else {
508
- mp_encode_lua_number(L, buf);
509
- }
510
- break;
511
- #endif
512
- case LUA_TTABLE: mp_encode_lua_table(L,buf,level); break;
513
- default: mp_encode_lua_null(L,buf); break;
514
- }
515
- lua_pop(L,1);
516
- }
517
-
518
- /*
519
- * Packs all arguments as a stream for multiple upacking later.
520
- * Returns error if no arguments provided.
521
- */
522
- int mp_pack(lua_State *L) {
523
- int nargs = lua_gettop(L);
524
- int i;
525
- mp_buf *buf;
526
-
527
- if (nargs == 0)
528
- return luaL_argerror(L, 0, "MessagePack pack needs input.");
529
-
530
- buf = mp_buf_new(L);
531
- for(i = 1; i <= nargs; i++) {
532
- /* Copy argument i to top of stack for _encode processing;
533
- * the encode function pops it from the stack when complete. */
534
- lua_pushvalue(L, i);
535
-
536
- mp_encode_lua_type(L,buf,0);
537
-
538
- lua_pushlstring(L,(char*)buf->b,buf->len);
539
-
540
- /* Reuse the buffer for the next operation by
541
- * setting its free count to the total buffer size
542
- * and the current position to zero. */
543
- buf->free += buf->len;
544
- buf->len = 0;
545
- }
546
- mp_buf_free(buf);
547
-
548
- /* Concatenate all nargs buffers together */
549
- lua_concat(L, nargs);
550
- return 1;
551
- }
552
-
553
- /* ------------------------------- Decoding --------------------------------- */
554
-
555
- void mp_decode_to_lua_type(lua_State *L, mp_cur *c);
556
-
557
- void mp_decode_to_lua_array(lua_State *L, mp_cur *c, size_t len) {
558
- assert(len <= UINT_MAX);
559
- int index = 1;
560
-
561
- lua_newtable(L);
562
- while(len--) {
563
- lua_pushnumber(L,index++);
564
- mp_decode_to_lua_type(L,c);
565
- if (c->err) return;
566
- lua_settable(L,-3);
567
- }
568
- }
569
-
570
- void mp_decode_to_lua_hash(lua_State *L, mp_cur *c, size_t len) {
571
- assert(len <= UINT_MAX);
572
- lua_newtable(L);
573
- while(len--) {
574
- mp_decode_to_lua_type(L,c); /* key */
575
- if (c->err) return;
576
- mp_decode_to_lua_type(L,c); /* value */
577
- if (c->err) return;
578
- lua_settable(L,-3);
579
- }
580
- }
581
-
582
- /* Decode a Message Pack raw object pointed by the string cursor 'c' to
583
- * a Lua type, that is left as the only result on the stack. */
584
- void mp_decode_to_lua_type(lua_State *L, mp_cur *c) {
585
- mp_cur_need(c,1);
586
-
587
- /* If we return more than 18 elements, we must resize the stack to
588
- * fit all our return values. But, there is no way to
589
- * determine how many objects a msgpack will unpack to up front, so
590
- * we request a +1 larger stack on each iteration (noop if stack is
591
- * big enough, and when stack does require resize it doubles in size) */
592
- luaL_checkstack(L, 1,
593
- "too many return values at once; "
594
- "use unpack_one or unpack_limit instead.");
595
-
596
- switch(c->p[0]) {
597
- case 0xcc: /* uint 8 */
598
- mp_cur_need(c,2);
599
- lua_pushunsigned(L,c->p[1]);
600
- mp_cur_consume(c,2);
601
- break;
602
- case 0xd0: /* int 8 */
603
- mp_cur_need(c,2);
604
- lua_pushinteger(L,(signed char)c->p[1]);
605
- mp_cur_consume(c,2);
606
- break;
607
- case 0xcd: /* uint 16 */
608
- mp_cur_need(c,3);
609
- lua_pushunsigned(L,
610
- (c->p[1] << 8) |
611
- c->p[2]);
612
- mp_cur_consume(c,3);
613
- break;
614
- case 0xd1: /* int 16 */
615
- mp_cur_need(c,3);
616
- lua_pushinteger(L,(int16_t)
617
- (c->p[1] << 8) |
618
- c->p[2]);
619
- mp_cur_consume(c,3);
620
- break;
621
- case 0xce: /* uint 32 */
622
- mp_cur_need(c,5);
623
- lua_pushunsigned(L,
624
- ((uint32_t)c->p[1] << 24) |
625
- ((uint32_t)c->p[2] << 16) |
626
- ((uint32_t)c->p[3] << 8) |
627
- (uint32_t)c->p[4]);
628
- mp_cur_consume(c,5);
629
- break;
630
- case 0xd2: /* int 32 */
631
- mp_cur_need(c,5);
632
- lua_pushinteger(L,
633
- ((int32_t)c->p[1] << 24) |
634
- ((int32_t)c->p[2] << 16) |
635
- ((int32_t)c->p[3] << 8) |
636
- (int32_t)c->p[4]);
637
- mp_cur_consume(c,5);
638
- break;
639
- case 0xcf: /* uint 64 */
640
- mp_cur_need(c,9);
641
- lua_pushunsigned(L,
642
- ((uint64_t)c->p[1] << 56) |
643
- ((uint64_t)c->p[2] << 48) |
644
- ((uint64_t)c->p[3] << 40) |
645
- ((uint64_t)c->p[4] << 32) |
646
- ((uint64_t)c->p[5] << 24) |
647
- ((uint64_t)c->p[6] << 16) |
648
- ((uint64_t)c->p[7] << 8) |
649
- (uint64_t)c->p[8]);
650
- mp_cur_consume(c,9);
651
- break;
652
- case 0xd3: /* int 64 */
653
- mp_cur_need(c,9);
654
- #if LUA_VERSION_NUM < 503
655
- lua_pushnumber(L,
656
- #else
657
- lua_pushinteger(L,
658
- #endif
659
- ((int64_t)c->p[1] << 56) |
660
- ((int64_t)c->p[2] << 48) |
661
- ((int64_t)c->p[3] << 40) |
662
- ((int64_t)c->p[4] << 32) |
663
- ((int64_t)c->p[5] << 24) |
664
- ((int64_t)c->p[6] << 16) |
665
- ((int64_t)c->p[7] << 8) |
666
- (int64_t)c->p[8]);
667
- mp_cur_consume(c,9);
668
- break;
669
- case 0xc0: /* nil */
670
- lua_pushnil(L);
671
- mp_cur_consume(c,1);
672
- break;
673
- case 0xc3: /* true */
674
- lua_pushboolean(L,1);
675
- mp_cur_consume(c,1);
676
- break;
677
- case 0xc2: /* false */
678
- lua_pushboolean(L,0);
679
- mp_cur_consume(c,1);
680
- break;
681
- case 0xca: /* float */
682
- mp_cur_need(c,5);
683
- assert(sizeof(float) == 4);
684
- {
685
- float f;
686
- memcpy(&f,c->p+1,4);
687
- memrevifle(&f,4);
688
- lua_pushnumber(L,f);
689
- mp_cur_consume(c,5);
690
- }
691
- break;
692
- case 0xcb: /* double */
693
- mp_cur_need(c,9);
694
- assert(sizeof(double) == 8);
695
- {
696
- double d;
697
- memcpy(&d,c->p+1,8);
698
- memrevifle(&d,8);
699
- lua_pushnumber(L,d);
700
- mp_cur_consume(c,9);
701
- }
702
- break;
703
- case 0xda: /* raw 16 */
704
- mp_cur_need(c,3);
705
- {
706
- size_t l = (c->p[1] << 8) | c->p[2];
707
- mp_cur_need(c,3+l);
708
- lua_pushlstring(L,(char*)c->p+3,l);
709
- mp_cur_consume(c,3+l);
710
- }
711
- break;
712
- case 0xdb: /* raw 32 */
713
- mp_cur_need(c,5);
714
- {
715
- size_t l = ((size_t)c->p[1] << 24) |
716
- ((size_t)c->p[2] << 16) |
717
- ((size_t)c->p[3] << 8) |
718
- (size_t)c->p[4];
719
- mp_cur_consume(c,5);
720
- mp_cur_need(c,l);
721
- lua_pushlstring(L,(char*)c->p,l);
722
- mp_cur_consume(c,l);
723
- }
724
- break;
725
- case 0xdc: /* array 16 */
726
- mp_cur_need(c,3);
727
- {
728
- size_t l = (c->p[1] << 8) | c->p[2];
729
- mp_cur_consume(c,3);
730
- mp_decode_to_lua_array(L,c,l);
731
- }
732
- break;
733
- case 0xdd: /* array 32 */
734
- mp_cur_need(c,5);
735
- {
736
- size_t l = ((size_t)c->p[1] << 24) |
737
- ((size_t)c->p[2] << 16) |
738
- ((size_t)c->p[3] << 8) |
739
- (size_t)c->p[4];
740
- mp_cur_consume(c,5);
741
- mp_decode_to_lua_array(L,c,l);
742
- }
743
- break;
744
- case 0xde: /* map 16 */
745
- mp_cur_need(c,3);
746
- {
747
- size_t l = (c->p[1] << 8) | c->p[2];
748
- mp_cur_consume(c,3);
749
- mp_decode_to_lua_hash(L,c,l);
750
- }
751
- break;
752
- case 0xdf: /* map 32 */
753
- mp_cur_need(c,5);
754
- {
755
- size_t l = ((size_t)c->p[1] << 24) |
756
- ((size_t)c->p[2] << 16) |
757
- ((size_t)c->p[3] << 8) |
758
- (size_t)c->p[4];
759
- mp_cur_consume(c,5);
760
- mp_decode_to_lua_hash(L,c,l);
761
- }
762
- break;
763
- default: /* types that can't be idenitified by first byte value. */
764
- if ((c->p[0] & 0x80) == 0) { /* positive fixnum */
765
- lua_pushunsigned(L,c->p[0]);
766
- mp_cur_consume(c,1);
767
- } else if ((c->p[0] & 0xe0) == 0xe0) { /* negative fixnum */
768
- lua_pushinteger(L,(signed char)c->p[0]);
769
- mp_cur_consume(c,1);
770
- } else if ((c->p[0] & 0xe0) == 0xa0) { /* fix raw */
771
- size_t l = c->p[0] & 0x1f;
772
- mp_cur_need(c,1+l);
773
- lua_pushlstring(L,(char*)c->p+1,l);
774
- mp_cur_consume(c,1+l);
775
- } else if ((c->p[0] & 0xf0) == 0x90) { /* fix map */
776
- size_t l = c->p[0] & 0xf;
777
- mp_cur_consume(c,1);
778
- mp_decode_to_lua_array(L,c,l);
779
- } else if ((c->p[0] & 0xf0) == 0x80) { /* fix map */
780
- size_t l = c->p[0] & 0xf;
781
- mp_cur_consume(c,1);
782
- mp_decode_to_lua_hash(L,c,l);
783
- } else {
784
- c->err = MP_CUR_ERROR_BADFMT;
785
- }
786
- }
787
- }
788
-
789
- int mp_unpack_full(lua_State *L, int limit, int offset) {
790
- size_t len;
791
- const char *s;
792
- mp_cur c;
793
- int cnt; /* Number of objects unpacked */
794
- int decode_all = (!limit && !offset);
795
-
796
- s = luaL_checklstring(L,1,&len); /* if no match, exits */
797
-
798
- if (offset < 0 || limit < 0) /* requesting negative off or lim is invalid */
799
- return luaL_error(L,
800
- "Invalid request to unpack with offset of %d and limit of %d.",
801
- offset, len);
802
- else if (offset > len)
803
- return luaL_error(L,
804
- "Start offset %d greater than input length %d.", offset, len);
805
-
806
- if (decode_all) limit = INT_MAX;
807
-
808
- mp_cur_init(&c,(const unsigned char *)s+offset,len-offset);
809
-
810
- /* We loop over the decode because this could be a stream
811
- * of multiple top-level values serialized together */
812
- for(cnt = 0; c.left > 0 && cnt < limit; cnt++) {
813
- mp_decode_to_lua_type(L,&c);
814
-
815
- if (c.err == MP_CUR_ERROR_EOF) {
816
- return luaL_error(L,"Missing bytes in input.");
817
- } else if (c.err == MP_CUR_ERROR_BADFMT) {
818
- return luaL_error(L,"Bad data format in input.");
819
- }
820
- }
821
-
822
- if (!decode_all) {
823
- /* c->left is the remaining size of the input buffer.
824
- * subtract the entire buffer size from the unprocessed size
825
- * to get our next start offset */
826
- int offset = len - c.left;
827
- /* Return offset -1 when we have have processed the entire buffer. */
828
- lua_pushinteger(L, c.left == 0 ? -1 : offset);
829
- /* Results are returned with the arg elements still
830
- * in place. Lua takes care of only returning
831
- * elements above the args for us.
832
- * In this case, we have one arg on the stack
833
- * for this function, so we insert our first return
834
- * value at position 2. */
835
- lua_insert(L, 2);
836
- cnt += 1; /* increase return count by one to make room for offset */
837
- }
838
-
839
- return cnt;
840
- }
841
-
842
- int mp_unpack(lua_State *L) {
843
- return mp_unpack_full(L, 0, 0);
844
- }
845
-
846
- int mp_unpack_one(lua_State *L) {
847
- int offset = luaL_optinteger(L, 2, 0);
848
- /* Variable pop because offset may not exist */
849
- lua_pop(L, lua_gettop(L)-1);
850
- return mp_unpack_full(L, 1, offset);
851
- }
852
-
853
- int mp_unpack_limit(lua_State *L) {
854
- int limit = luaL_checkinteger(L, 2);
855
- int offset = luaL_optinteger(L, 3, 0);
856
- /* Variable pop because offset may not exist */
857
- lua_pop(L, lua_gettop(L)-1);
858
-
859
- return mp_unpack_full(L, limit, offset);
860
- }
861
-
862
- int mp_safe(lua_State *L) {
863
- int argc, err, total_results;
864
-
865
- argc = lua_gettop(L);
866
-
867
- /* This adds our function to the bottom of the stack
868
- * (the "call this function" position) */
869
- lua_pushvalue(L, lua_upvalueindex(1));
870
- lua_insert(L, 1);
871
-
872
- err = lua_pcall(L, argc, LUA_MULTRET, 0);
873
- total_results = lua_gettop(L);
874
-
875
- if (!err) {
876
- return total_results;
877
- } else {
878
- lua_pushnil(L);
879
- lua_insert(L,-2);
880
- return 2;
881
- }
882
- }
883
-
884
- /* -------------------------------------------------------------------------- */
885
- const struct luaL_Reg cmds[] = {
886
- {"pack", mp_pack},
887
- {"unpack", mp_unpack},
888
- {"unpack_one", mp_unpack_one},
889
- {"unpack_limit", mp_unpack_limit},
890
- {0}
891
- };
892
-
893
- int luaopen_create(lua_State *L) {
894
- int i;
895
- /* Manually construct our module table instead of
896
- * relying on _register or _newlib */
897
- lua_newtable(L);
898
-
899
- for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) {
900
- lua_pushcfunction(L, cmds[i].func);
901
- lua_setfield(L, -2, cmds[i].name);
902
- }
903
-
904
- /* Add metadata */
905
- lua_pushliteral(L, LUACMSGPACK_NAME);
906
- lua_setfield(L, -2, "_NAME");
907
- lua_pushliteral(L, LUACMSGPACK_VERSION);
908
- lua_setfield(L, -2, "_VERSION");
909
- lua_pushliteral(L, LUACMSGPACK_COPYRIGHT);
910
- lua_setfield(L, -2, "_COPYRIGHT");
911
- lua_pushliteral(L, LUACMSGPACK_DESCRIPTION);
912
- lua_setfield(L, -2, "_DESCRIPTION");
913
- return 1;
914
- }
915
-
916
- LUALIB_API int luaopen_cmsgpack(lua_State *L) {
917
- luaopen_create(L);
918
-
919
- #if LUA_VERSION_NUM < 502
920
- /* Register name globally for 5.1 */
921
- lua_pushvalue(L, -1);
922
- lua_setglobal(L, LUACMSGPACK_NAME);
923
- #endif
924
-
925
- return 1;
926
- }
927
-
928
- LUALIB_API int luaopen_cmsgpack_safe(lua_State *L) {
929
- int i;
930
-
931
- luaopen_cmsgpack(L);
932
-
933
- /* Wrap all functions in the safe handler */
934
- for (i = 0; i < (sizeof(cmds)/sizeof(*cmds) - 1); i++) {
935
- lua_getfield(L, -1, cmds[i].name);
936
- lua_pushcclosure(L, mp_safe, 1);
937
- lua_setfield(L, -2, cmds[i].name);
938
- }
939
-
940
- #if LUA_VERSION_NUM < 502
941
- /* Register name globally for 5.1 */
942
- lua_pushvalue(L, -1);
943
- lua_setglobal(L, LUACMSGPACK_SAFE_NAME);
944
- #endif
945
-
946
- return 1;
947
- }
948
-
949
- /******************************************************************************
950
- * Copyright (C) 2012 Salvatore Sanfilippo. All rights reserved.
951
- *
952
- * Permission is hereby granted, free of charge, to any person obtaining
953
- * a copy of this software and associated documentation files (the
954
- * "Software"), to deal in the Software without restriction, including
955
- * without limitation the rights to use, copy, modify, merge, publish,
956
- * distribute, sublicense, and/or sell copies of the Software, and to
957
- * permit persons to whom the Software is furnished to do so, subject to
958
- * the following conditions:
959
- *
960
- * The above copyright notice and this permission notice shall be
961
- * included in all copies or substantial portions of the Software.
962
- *
963
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
964
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
965
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
966
- * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
967
- * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
968
- * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
969
- * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
970
- ******************************************************************************/