immunio 0.15.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +234 -0
- data/README.md +147 -0
- data/bin/immunio +5 -0
- data/lib/immunio.rb +29 -0
- data/lib/immunio/agent.rb +260 -0
- data/lib/immunio/authentication.rb +96 -0
- data/lib/immunio/blocked_app.rb +38 -0
- data/lib/immunio/channel.rb +432 -0
- data/lib/immunio/cli.rb +39 -0
- data/lib/immunio/context.rb +114 -0
- data/lib/immunio/errors.rb +43 -0
- data/lib/immunio/immunio_ca.crt +45 -0
- data/lib/immunio/logger.rb +87 -0
- data/lib/immunio/plugins/action_dispatch.rb +45 -0
- data/lib/immunio/plugins/action_view.rb +431 -0
- data/lib/immunio/plugins/active_record.rb +707 -0
- data/lib/immunio/plugins/active_record_relation.rb +370 -0
- data/lib/immunio/plugins/authlogic.rb +80 -0
- data/lib/immunio/plugins/csrf.rb +24 -0
- data/lib/immunio/plugins/devise.rb +40 -0
- data/lib/immunio/plugins/environment_reporter.rb +69 -0
- data/lib/immunio/plugins/eval.rb +51 -0
- data/lib/immunio/plugins/exception_handler.rb +55 -0
- data/lib/immunio/plugins/gems_tracker.rb +5 -0
- data/lib/immunio/plugins/haml.rb +36 -0
- data/lib/immunio/plugins/http_finisher.rb +50 -0
- data/lib/immunio/plugins/http_tracker.rb +203 -0
- data/lib/immunio/plugins/io.rb +96 -0
- data/lib/immunio/plugins/redirect.rb +42 -0
- data/lib/immunio/plugins/warden.rb +66 -0
- data/lib/immunio/processor.rb +234 -0
- data/lib/immunio/rails.rb +26 -0
- data/lib/immunio/request.rb +139 -0
- data/lib/immunio/rufus_lua_ext/ref.rb +27 -0
- data/lib/immunio/rufus_lua_ext/state.rb +157 -0
- data/lib/immunio/rufus_lua_ext/table.rb +137 -0
- data/lib/immunio/rufus_lua_ext/utils.rb +13 -0
- data/lib/immunio/version.rb +5 -0
- data/lib/immunio/vm.rb +291 -0
- data/lua-hooks/ext/all.c +78 -0
- data/lua-hooks/ext/bitop/README +22 -0
- data/lua-hooks/ext/bitop/bit.c +189 -0
- data/lua-hooks/ext/extconf.rb +38 -0
- data/lua-hooks/ext/libinjection/COPYING +37 -0
- data/lua-hooks/ext/libinjection/libinjection.h +65 -0
- data/lua-hooks/ext/libinjection/libinjection_html5.c +847 -0
- data/lua-hooks/ext/libinjection/libinjection_html5.h +54 -0
- data/lua-hooks/ext/libinjection/libinjection_sqli.c +2301 -0
- data/lua-hooks/ext/libinjection/libinjection_sqli.h +295 -0
- data/lua-hooks/ext/libinjection/libinjection_sqli_data.h +9349 -0
- data/lua-hooks/ext/libinjection/libinjection_xss.c +531 -0
- data/lua-hooks/ext/libinjection/libinjection_xss.h +21 -0
- data/lua-hooks/ext/libinjection/lualib.c +109 -0
- data/lua-hooks/ext/lpeg/HISTORY +90 -0
- data/lua-hooks/ext/lpeg/lpcap.c +537 -0
- data/lua-hooks/ext/lpeg/lpcap.h +43 -0
- data/lua-hooks/ext/lpeg/lpcode.c +986 -0
- data/lua-hooks/ext/lpeg/lpcode.h +34 -0
- data/lua-hooks/ext/lpeg/lpeg-128.gif +0 -0
- data/lua-hooks/ext/lpeg/lpeg.html +1429 -0
- data/lua-hooks/ext/lpeg/lpprint.c +244 -0
- data/lua-hooks/ext/lpeg/lpprint.h +35 -0
- data/lua-hooks/ext/lpeg/lptree.c +1238 -0
- data/lua-hooks/ext/lpeg/lptree.h +77 -0
- data/lua-hooks/ext/lpeg/lptypes.h +149 -0
- data/lua-hooks/ext/lpeg/lpvm.c +355 -0
- data/lua-hooks/ext/lpeg/lpvm.h +58 -0
- data/lua-hooks/ext/lpeg/makefile +55 -0
- data/lua-hooks/ext/lpeg/re.html +498 -0
- data/lua-hooks/ext/lpeg/test.lua +1409 -0
- data/lua-hooks/ext/lua-cmsgpack/CMakeLists.txt +45 -0
- data/lua-hooks/ext/lua-cmsgpack/README.md +115 -0
- data/lua-hooks/ext/lua-cmsgpack/lua_cmsgpack.c +957 -0
- data/lua-hooks/ext/lua-cmsgpack/test.lua +570 -0
- data/lua-hooks/ext/lua-snapshot/LICENSE +7 -0
- data/lua-hooks/ext/lua-snapshot/Makefile +12 -0
- data/lua-hooks/ext/lua-snapshot/README.md +18 -0
- data/lua-hooks/ext/lua-snapshot/dump.lua +15 -0
- data/lua-hooks/ext/lua-snapshot/snapshot.c +455 -0
- data/lua-hooks/ext/lua/COPYRIGHT +34 -0
- data/lua-hooks/ext/lua/lapi.c +1087 -0
- data/lua-hooks/ext/lua/lapi.h +16 -0
- data/lua-hooks/ext/lua/lauxlib.c +652 -0
- data/lua-hooks/ext/lua/lauxlib.h +174 -0
- data/lua-hooks/ext/lua/lbaselib.c +659 -0
- data/lua-hooks/ext/lua/lcode.c +831 -0
- data/lua-hooks/ext/lua/lcode.h +76 -0
- data/lua-hooks/ext/lua/ldblib.c +398 -0
- data/lua-hooks/ext/lua/ldebug.c +638 -0
- data/lua-hooks/ext/lua/ldebug.h +33 -0
- data/lua-hooks/ext/lua/ldo.c +519 -0
- data/lua-hooks/ext/lua/ldo.h +57 -0
- data/lua-hooks/ext/lua/ldump.c +164 -0
- data/lua-hooks/ext/lua/lfunc.c +174 -0
- data/lua-hooks/ext/lua/lfunc.h +34 -0
- data/lua-hooks/ext/lua/lgc.c +710 -0
- data/lua-hooks/ext/lua/lgc.h +110 -0
- data/lua-hooks/ext/lua/linit.c +38 -0
- data/lua-hooks/ext/lua/liolib.c +556 -0
- data/lua-hooks/ext/lua/llex.c +463 -0
- data/lua-hooks/ext/lua/llex.h +81 -0
- data/lua-hooks/ext/lua/llimits.h +128 -0
- data/lua-hooks/ext/lua/lmathlib.c +263 -0
- data/lua-hooks/ext/lua/lmem.c +86 -0
- data/lua-hooks/ext/lua/lmem.h +49 -0
- data/lua-hooks/ext/lua/loadlib.c +705 -0
- data/lua-hooks/ext/lua/loadlib_rel.c +760 -0
- data/lua-hooks/ext/lua/lobject.c +214 -0
- data/lua-hooks/ext/lua/lobject.h +381 -0
- data/lua-hooks/ext/lua/lopcodes.c +102 -0
- data/lua-hooks/ext/lua/lopcodes.h +268 -0
- data/lua-hooks/ext/lua/loslib.c +243 -0
- data/lua-hooks/ext/lua/lparser.c +1339 -0
- data/lua-hooks/ext/lua/lparser.h +82 -0
- data/lua-hooks/ext/lua/lstate.c +214 -0
- data/lua-hooks/ext/lua/lstate.h +169 -0
- data/lua-hooks/ext/lua/lstring.c +111 -0
- data/lua-hooks/ext/lua/lstring.h +31 -0
- data/lua-hooks/ext/lua/lstrlib.c +871 -0
- data/lua-hooks/ext/lua/ltable.c +588 -0
- data/lua-hooks/ext/lua/ltable.h +40 -0
- data/lua-hooks/ext/lua/ltablib.c +287 -0
- data/lua-hooks/ext/lua/ltm.c +75 -0
- data/lua-hooks/ext/lua/ltm.h +54 -0
- data/lua-hooks/ext/lua/lua.c +392 -0
- data/lua-hooks/ext/lua/lua.def +131 -0
- data/lua-hooks/ext/lua/lua.h +388 -0
- data/lua-hooks/ext/lua/lua.rc +28 -0
- data/lua-hooks/ext/lua/lua_dll.rc +26 -0
- data/lua-hooks/ext/lua/luac.c +200 -0
- data/lua-hooks/ext/lua/luac.rc +1 -0
- data/lua-hooks/ext/lua/luaconf.h +763 -0
- data/lua-hooks/ext/lua/luaconf.h.in +724 -0
- data/lua-hooks/ext/lua/luaconf.h.orig +763 -0
- data/lua-hooks/ext/lua/lualib.h +53 -0
- data/lua-hooks/ext/lua/lundump.c +227 -0
- data/lua-hooks/ext/lua/lundump.h +36 -0
- data/lua-hooks/ext/lua/lvm.c +767 -0
- data/lua-hooks/ext/lua/lvm.h +36 -0
- data/lua-hooks/ext/lua/lzio.c +82 -0
- data/lua-hooks/ext/lua/lzio.h +67 -0
- data/lua-hooks/ext/lua/print.c +227 -0
- data/lua-hooks/ext/luautf8/README.md +152 -0
- data/lua-hooks/ext/luautf8/lutf8lib.c +1274 -0
- data/lua-hooks/ext/luautf8/unidata.h +3064 -0
- data/lua-hooks/lib/boot.lua +254 -0
- data/lua-hooks/lib/encode.lua +4 -0
- data/lua-hooks/lib/lexers/LICENSE +21 -0
- data/lua-hooks/lib/lexers/bash.lua +134 -0
- data/lua-hooks/lib/lexers/bash_dqstr.lua +62 -0
- data/lua-hooks/lib/lexers/css.lua +216 -0
- data/lua-hooks/lib/lexers/html.lua +106 -0
- data/lua-hooks/lib/lexers/javascript.lua +68 -0
- data/lua-hooks/lib/lexers/lexer.lua +1575 -0
- data/lua-hooks/lib/lexers/markers.lua +33 -0
- metadata +308 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
/*
|
2
|
+
** $Id: lvm.h,v 2.5.1.1 2007/12/27 13:02:25 roberto Exp $
|
3
|
+
** Lua virtual machine
|
4
|
+
** See Copyright Notice in lua.h
|
5
|
+
*/
|
6
|
+
|
7
|
+
#ifndef lvm_h
|
8
|
+
#define lvm_h
|
9
|
+
|
10
|
+
|
11
|
+
#include "ldo.h"
|
12
|
+
#include "lobject.h"
|
13
|
+
#include "ltm.h"
|
14
|
+
|
15
|
+
|
16
|
+
#define tostring(L,o) ((ttype(o) == LUA_TSTRING) || (luaV_tostring(L, o)))
|
17
|
+
|
18
|
+
#define tonumber(o,n) (ttype(o) == LUA_TNUMBER || \
|
19
|
+
(((o) = luaV_tonumber(o,n)) != NULL))
|
20
|
+
|
21
|
+
#define equalobj(L,o1,o2) \
|
22
|
+
(ttype(o1) == ttype(o2) && luaV_equalval(L, o1, o2))
|
23
|
+
|
24
|
+
|
25
|
+
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
|
26
|
+
LUAI_FUNC int luaV_equalval (lua_State *L, const TValue *t1, const TValue *t2);
|
27
|
+
LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n);
|
28
|
+
LUAI_FUNC int luaV_tostring (lua_State *L, StkId obj);
|
29
|
+
LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
|
30
|
+
StkId val);
|
31
|
+
LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,
|
32
|
+
StkId val);
|
33
|
+
LUAI_FUNC void luaV_execute (lua_State *L, int nexeccalls);
|
34
|
+
LUAI_FUNC void luaV_concat (lua_State *L, int total, int last);
|
35
|
+
|
36
|
+
#endif
|
@@ -0,0 +1,82 @@
|
|
1
|
+
/*
|
2
|
+
** $Id: lzio.c,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $
|
3
|
+
** a generic input stream interface
|
4
|
+
** See Copyright Notice in lua.h
|
5
|
+
*/
|
6
|
+
|
7
|
+
|
8
|
+
#include <string.h>
|
9
|
+
|
10
|
+
#define lzio_c
|
11
|
+
#define LUA_CORE
|
12
|
+
|
13
|
+
#include "lua.h"
|
14
|
+
|
15
|
+
#include "llimits.h"
|
16
|
+
#include "lmem.h"
|
17
|
+
#include "lstate.h"
|
18
|
+
#include "lzio.h"
|
19
|
+
|
20
|
+
|
21
|
+
int luaZ_fill (ZIO *z) {
|
22
|
+
size_t size;
|
23
|
+
lua_State *L = z->L;
|
24
|
+
const char *buff;
|
25
|
+
lua_unlock(L);
|
26
|
+
buff = z->reader(L, z->data, &size);
|
27
|
+
lua_lock(L);
|
28
|
+
if (buff == NULL || size == 0) return EOZ;
|
29
|
+
z->n = size - 1;
|
30
|
+
z->p = buff;
|
31
|
+
return char2int(*(z->p++));
|
32
|
+
}
|
33
|
+
|
34
|
+
|
35
|
+
int luaZ_lookahead (ZIO *z) {
|
36
|
+
if (z->n == 0) {
|
37
|
+
if (luaZ_fill(z) == EOZ)
|
38
|
+
return EOZ;
|
39
|
+
else {
|
40
|
+
z->n++; /* luaZ_fill removed first byte; put back it */
|
41
|
+
z->p--;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
return char2int(*z->p);
|
45
|
+
}
|
46
|
+
|
47
|
+
|
48
|
+
void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
|
49
|
+
z->L = L;
|
50
|
+
z->reader = reader;
|
51
|
+
z->data = data;
|
52
|
+
z->n = 0;
|
53
|
+
z->p = NULL;
|
54
|
+
}
|
55
|
+
|
56
|
+
|
57
|
+
/* --------------------------------------------------------------- read --- */
|
58
|
+
size_t luaZ_read (ZIO *z, void *b, size_t n) {
|
59
|
+
while (n) {
|
60
|
+
size_t m;
|
61
|
+
if (luaZ_lookahead(z) == EOZ)
|
62
|
+
return n; /* return number of missing bytes */
|
63
|
+
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
|
64
|
+
memcpy(b, z->p, m);
|
65
|
+
z->n -= m;
|
66
|
+
z->p += m;
|
67
|
+
b = (char *)b + m;
|
68
|
+
n -= m;
|
69
|
+
}
|
70
|
+
return 0;
|
71
|
+
}
|
72
|
+
|
73
|
+
/* ------------------------------------------------------------------------ */
|
74
|
+
char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n) {
|
75
|
+
if (n > buff->buffsize) {
|
76
|
+
if (n < LUA_MINBUFFER) n = LUA_MINBUFFER;
|
77
|
+
luaZ_resizebuffer(L, buff, n);
|
78
|
+
}
|
79
|
+
return buff->buffer;
|
80
|
+
}
|
81
|
+
|
82
|
+
|
@@ -0,0 +1,67 @@
|
|
1
|
+
/*
|
2
|
+
** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $
|
3
|
+
** Buffered streams
|
4
|
+
** See Copyright Notice in lua.h
|
5
|
+
*/
|
6
|
+
|
7
|
+
|
8
|
+
#ifndef lzio_h
|
9
|
+
#define lzio_h
|
10
|
+
|
11
|
+
#include "lua.h"
|
12
|
+
|
13
|
+
#include "lmem.h"
|
14
|
+
|
15
|
+
|
16
|
+
#define EOZ (-1) /* end of stream */
|
17
|
+
|
18
|
+
typedef struct Zio ZIO;
|
19
|
+
|
20
|
+
#define char2int(c) cast(int, cast(unsigned char, (c)))
|
21
|
+
|
22
|
+
#define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z))
|
23
|
+
|
24
|
+
typedef struct Mbuffer {
|
25
|
+
char *buffer;
|
26
|
+
size_t n;
|
27
|
+
size_t buffsize;
|
28
|
+
} Mbuffer;
|
29
|
+
|
30
|
+
#define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0)
|
31
|
+
|
32
|
+
#define luaZ_buffer(buff) ((buff)->buffer)
|
33
|
+
#define luaZ_sizebuffer(buff) ((buff)->buffsize)
|
34
|
+
#define luaZ_bufflen(buff) ((buff)->n)
|
35
|
+
|
36
|
+
#define luaZ_resetbuffer(buff) ((buff)->n = 0)
|
37
|
+
|
38
|
+
|
39
|
+
#define luaZ_resizebuffer(L, buff, size) \
|
40
|
+
(luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \
|
41
|
+
(buff)->buffsize = size)
|
42
|
+
|
43
|
+
#define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0)
|
44
|
+
|
45
|
+
|
46
|
+
LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
|
47
|
+
LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
|
48
|
+
void *data);
|
49
|
+
LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
|
50
|
+
LUAI_FUNC int luaZ_lookahead (ZIO *z);
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
/* --------- Private Part ------------------ */
|
55
|
+
|
56
|
+
struct Zio {
|
57
|
+
size_t n; /* bytes still unread */
|
58
|
+
const char *p; /* current position in buffer */
|
59
|
+
lua_Reader reader;
|
60
|
+
void* data; /* additional data */
|
61
|
+
lua_State *L; /* Lua state (for reader) */
|
62
|
+
};
|
63
|
+
|
64
|
+
|
65
|
+
LUAI_FUNC int luaZ_fill (ZIO *z);
|
66
|
+
|
67
|
+
#endif
|
@@ -0,0 +1,227 @@
|
|
1
|
+
/*
|
2
|
+
** $Id: print.c,v 1.55a 2006/05/31 13:30:05 lhf Exp $
|
3
|
+
** print bytecodes
|
4
|
+
** See Copyright Notice in lua.h
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <ctype.h>
|
8
|
+
#include <stdio.h>
|
9
|
+
|
10
|
+
#define luac_c
|
11
|
+
#define LUA_CORE
|
12
|
+
|
13
|
+
#include "ldebug.h"
|
14
|
+
#include "lobject.h"
|
15
|
+
#include "lopcodes.h"
|
16
|
+
#include "lundump.h"
|
17
|
+
|
18
|
+
#define PrintFunction luaU_print
|
19
|
+
|
20
|
+
#define Sizeof(x) ((int)sizeof(x))
|
21
|
+
#define VOID(p) ((const void*)(p))
|
22
|
+
|
23
|
+
static void PrintString(const TString* ts)
|
24
|
+
{
|
25
|
+
const char* s=getstr(ts);
|
26
|
+
size_t i,n=ts->tsv.len;
|
27
|
+
putchar('"');
|
28
|
+
for (i=0; i<n; i++)
|
29
|
+
{
|
30
|
+
int c=s[i];
|
31
|
+
switch (c)
|
32
|
+
{
|
33
|
+
case '"': printf("\\\""); break;
|
34
|
+
case '\\': printf("\\\\"); break;
|
35
|
+
case '\a': printf("\\a"); break;
|
36
|
+
case '\b': printf("\\b"); break;
|
37
|
+
case '\f': printf("\\f"); break;
|
38
|
+
case '\n': printf("\\n"); break;
|
39
|
+
case '\r': printf("\\r"); break;
|
40
|
+
case '\t': printf("\\t"); break;
|
41
|
+
case '\v': printf("\\v"); break;
|
42
|
+
default: if (isprint((unsigned char)c))
|
43
|
+
putchar(c);
|
44
|
+
else
|
45
|
+
printf("\\%03u",(unsigned char)c);
|
46
|
+
}
|
47
|
+
}
|
48
|
+
putchar('"');
|
49
|
+
}
|
50
|
+
|
51
|
+
static void PrintConstant(const Proto* f, int i)
|
52
|
+
{
|
53
|
+
const TValue* o=&f->k[i];
|
54
|
+
switch (ttype(o))
|
55
|
+
{
|
56
|
+
case LUA_TNIL:
|
57
|
+
printf("nil");
|
58
|
+
break;
|
59
|
+
case LUA_TBOOLEAN:
|
60
|
+
printf(bvalue(o) ? "true" : "false");
|
61
|
+
break;
|
62
|
+
case LUA_TNUMBER:
|
63
|
+
printf(LUA_NUMBER_FMT,nvalue(o));
|
64
|
+
break;
|
65
|
+
case LUA_TSTRING:
|
66
|
+
PrintString(rawtsvalue(o));
|
67
|
+
break;
|
68
|
+
default: /* cannot happen */
|
69
|
+
printf("? type=%d",ttype(o));
|
70
|
+
break;
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
static void PrintCode(const Proto* f)
|
75
|
+
{
|
76
|
+
const Instruction* code=f->code;
|
77
|
+
int pc,n=f->sizecode;
|
78
|
+
for (pc=0; pc<n; pc++)
|
79
|
+
{
|
80
|
+
Instruction i=code[pc];
|
81
|
+
OpCode o=GET_OPCODE(i);
|
82
|
+
int a=GETARG_A(i);
|
83
|
+
int b=GETARG_B(i);
|
84
|
+
int c=GETARG_C(i);
|
85
|
+
int bx=GETARG_Bx(i);
|
86
|
+
int sbx=GETARG_sBx(i);
|
87
|
+
int line=lgetline(f,pc);
|
88
|
+
printf("\t%d\t",pc+1);
|
89
|
+
if (line>0) printf("[%d]\t",line); else printf("[-]\t");
|
90
|
+
printf("%-9s\t",luaP_opnames[o]);
|
91
|
+
switch (getOpMode(o))
|
92
|
+
{
|
93
|
+
case iABC:
|
94
|
+
printf("%d",a);
|
95
|
+
if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b);
|
96
|
+
if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c);
|
97
|
+
break;
|
98
|
+
case iABx:
|
99
|
+
if (getBMode(o)==OpArgK) printf("%d %d",a,-1-bx); else printf("%d %d",a,bx);
|
100
|
+
break;
|
101
|
+
case iAsBx:
|
102
|
+
if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
|
103
|
+
break;
|
104
|
+
}
|
105
|
+
switch (o)
|
106
|
+
{
|
107
|
+
case OP_LOADK:
|
108
|
+
printf("\t; "); PrintConstant(f,bx);
|
109
|
+
break;
|
110
|
+
case OP_GETUPVAL:
|
111
|
+
case OP_SETUPVAL:
|
112
|
+
printf("\t; %s", (f->sizeupvalues>0) ? getstr(f->upvalues[b]) : "-");
|
113
|
+
break;
|
114
|
+
case OP_GETGLOBAL:
|
115
|
+
case OP_SETGLOBAL:
|
116
|
+
printf("\t; %s",svalue(&f->k[bx]));
|
117
|
+
break;
|
118
|
+
case OP_GETTABLE:
|
119
|
+
case OP_SELF:
|
120
|
+
if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
|
121
|
+
break;
|
122
|
+
case OP_SETTABLE:
|
123
|
+
case OP_ADD:
|
124
|
+
case OP_SUB:
|
125
|
+
case OP_MUL:
|
126
|
+
case OP_DIV:
|
127
|
+
case OP_POW:
|
128
|
+
case OP_EQ:
|
129
|
+
case OP_LT:
|
130
|
+
case OP_LE:
|
131
|
+
if (ISK(b) || ISK(c))
|
132
|
+
{
|
133
|
+
printf("\t; ");
|
134
|
+
if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
|
135
|
+
printf(" ");
|
136
|
+
if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
|
137
|
+
}
|
138
|
+
break;
|
139
|
+
case OP_JMP:
|
140
|
+
case OP_FORLOOP:
|
141
|
+
case OP_FORPREP:
|
142
|
+
printf("\t; to %d",sbx+pc+2);
|
143
|
+
break;
|
144
|
+
case OP_CLOSURE:
|
145
|
+
printf("\t; %p",VOID(f->p[bx]));
|
146
|
+
break;
|
147
|
+
case OP_SETLIST:
|
148
|
+
if (c==0) printf("\t; %d",(int)code[++pc]);
|
149
|
+
else printf("\t; %d",c);
|
150
|
+
break;
|
151
|
+
default:
|
152
|
+
break;
|
153
|
+
}
|
154
|
+
printf("\n");
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
#define SS(x) (x==1)?"":"s"
|
159
|
+
#define S(x) x,SS(x)
|
160
|
+
|
161
|
+
static void PrintHeader(const Proto* f)
|
162
|
+
{
|
163
|
+
const char* s=getstr(f->source);
|
164
|
+
if (*s=='@' || *s=='=')
|
165
|
+
s++;
|
166
|
+
else if (*s==LUA_SIGNATURE[0])
|
167
|
+
s="(bstring)";
|
168
|
+
else
|
169
|
+
s="(string)";
|
170
|
+
printf("\n%s <%s:%d,%d> (%d instruction%s, %d bytes at %p)\n",
|
171
|
+
(f->linedefined==0)?"main":"function",s,
|
172
|
+
f->linedefined,f->lastlinedefined,
|
173
|
+
S(f->sizecode),f->sizecode*Sizeof(Instruction),VOID(f));
|
174
|
+
printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
|
175
|
+
f->numparams,f->is_vararg?"+":"",SS(f->numparams),
|
176
|
+
S(f->maxstacksize),S(f->nups));
|
177
|
+
printf("%d local%s, %d constant%s, %d function%s\n",
|
178
|
+
S(f->sizelocvars),S(f->sizek),S(f->sizep));
|
179
|
+
}
|
180
|
+
|
181
|
+
static void PrintConstants(const Proto* f)
|
182
|
+
{
|
183
|
+
int i,n=f->sizek;
|
184
|
+
printf("constants (%d) for %p:\n",n,VOID(f));
|
185
|
+
for (i=0; i<n; i++)
|
186
|
+
{
|
187
|
+
printf("\t%d\t",i+1);
|
188
|
+
PrintConstant(f,i);
|
189
|
+
printf("\n");
|
190
|
+
}
|
191
|
+
}
|
192
|
+
|
193
|
+
static void PrintLocals(const Proto* f)
|
194
|
+
{
|
195
|
+
int i,n=f->sizelocvars;
|
196
|
+
printf("locals (%d) for %p:\n",n,VOID(f));
|
197
|
+
for (i=0; i<n; i++)
|
198
|
+
{
|
199
|
+
printf("\t%d\t%s\t%d\t%d\n",
|
200
|
+
i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
|
201
|
+
}
|
202
|
+
}
|
203
|
+
|
204
|
+
static void PrintUpvalues(const Proto* f)
|
205
|
+
{
|
206
|
+
int i,n=f->sizeupvalues;
|
207
|
+
printf("upvalues (%d) for %p:\n",n,VOID(f));
|
208
|
+
if (f->upvalues==NULL) return;
|
209
|
+
for (i=0; i<n; i++)
|
210
|
+
{
|
211
|
+
printf("\t%d\t%s\n",i,getstr(f->upvalues[i]));
|
212
|
+
}
|
213
|
+
}
|
214
|
+
|
215
|
+
void PrintFunction(const Proto* f, int full)
|
216
|
+
{
|
217
|
+
int i,n=f->sizep;
|
218
|
+
PrintHeader(f);
|
219
|
+
PrintCode(f);
|
220
|
+
if (full)
|
221
|
+
{
|
222
|
+
PrintConstants(f);
|
223
|
+
PrintLocals(f);
|
224
|
+
PrintUpvalues(f);
|
225
|
+
}
|
226
|
+
for (i=0; i<n; i++) PrintFunction(f->p[i],full);
|
227
|
+
}
|
@@ -0,0 +1,152 @@
|
|
1
|
+
UTF-8 module for Lua 5.x
|
2
|
+
========================
|
3
|
+
|
4
|
+
This module is add UTF-8 support to Lua.
|
5
|
+
|
6
|
+
It use data extracted from [Unicode Character Database](http://www.unicode.org/reports/tr44/), and tested on Lua
|
7
|
+
5.2.3 and LuaJIT.
|
8
|
+
|
9
|
+
parseucd.lua is a pure Lua script generate unidata.h, to support convert
|
10
|
+
characters and check characters' category.
|
11
|
+
|
12
|
+
It mainly used to compatible with Lua's own string module, it passed all
|
13
|
+
string and pattern matching test in lua test suite[2].
|
14
|
+
|
15
|
+
It also add some useful routines against UTF-8 features, some like:
|
16
|
+
- a convenient interface to escape Unicode sequence in string.
|
17
|
+
- string insert/remove, since UTF-8 substring extract may expensive.
|
18
|
+
- calculate Unicode width, useful when implement e.g. console emulator.
|
19
|
+
- a useful interface to translate Unicode offset and byte offset.
|
20
|
+
|
21
|
+
[2]: http://www.lua.org/tests/5.2/
|
22
|
+
|
23
|
+
|
24
|
+
LuaRocks Installation
|
25
|
+
---------------------
|
26
|
+
`luarocks install utf8`
|
27
|
+
|
28
|
+
Usage
|
29
|
+
-----
|
30
|
+
|
31
|
+
Many routines are same as Lua's string module:
|
32
|
+
- `utf8.byte`
|
33
|
+
- `utf8.char`
|
34
|
+
- `utf8.find`
|
35
|
+
- `utf8.gmatch`
|
36
|
+
- `utf8.gsub`
|
37
|
+
- `utf8.len`
|
38
|
+
- `utf8.lower`
|
39
|
+
- `utf8.match`
|
40
|
+
- `utf8.reverse`
|
41
|
+
- `utf8.sub`
|
42
|
+
- `utf8.upper`
|
43
|
+
|
44
|
+
The document of these functions can be find in Lua manual[3].
|
45
|
+
|
46
|
+
[3]: http://www.lua.org/manual/5.2/manual.html#6.4
|
47
|
+
|
48
|
+
|
49
|
+
Some routines in string module needn't support Unicode:
|
50
|
+
- `string.dump`
|
51
|
+
- `string.format`
|
52
|
+
- `string.rep`
|
53
|
+
|
54
|
+
They are NOT in utf8 module.
|
55
|
+
|
56
|
+
Some routines are new, with some Unicode-spec functions:
|
57
|
+
|
58
|
+
###utf8.escape(str) -> utf8 string
|
59
|
+
escape a str to UTF-8 format string. It support several escape format:
|
60
|
+
|
61
|
+
%ddd - which ddd is a decimal number at any length:
|
62
|
+
change Unicode code point to UTF-8 format.
|
63
|
+
%{ddd} - same as %nnn but has bracket around.
|
64
|
+
%uddd - same as %ddd, u stands Unicode
|
65
|
+
%u{ddd} - same as %{ddd}
|
66
|
+
%xhhh - hexadigit version of %ddd
|
67
|
+
%x{hhh} same as %xhhh.
|
68
|
+
%? - '?' stands for any other character: escape this character.
|
69
|
+
|
70
|
+
####Examples:
|
71
|
+
```
|
72
|
+
local u = utf8.escape
|
73
|
+
print(u"%123%u123%{123}%u{123}%xABC%x{ABC}")
|
74
|
+
print(u"%%123%?%d%%u")
|
75
|
+
```
|
76
|
+
|
77
|
+
###utf8.charpos(s[[, charpos], offset]) -> charpos, code point
|
78
|
+
convert UTF-8 position to byte offset.
|
79
|
+
if only offset is given, return byte offset of this UTF-8 char index.
|
80
|
+
if charpos and offset is given, a new charpos will calculate, by
|
81
|
+
add/subtract UTF-8 char offset to current charpos.
|
82
|
+
in all case, it return a new char position, and code point (a number) at
|
83
|
+
this position.
|
84
|
+
|
85
|
+
###utf8.next(s[, charpos[, offset]]) -> charpos, code point
|
86
|
+
iterate though the UTF-8 string s.
|
87
|
+
If only s is given, it can used as a iterator:
|
88
|
+
```
|
89
|
+
for pos, code in utf8.next, "utf8-string" do
|
90
|
+
-- ...
|
91
|
+
end
|
92
|
+
```
|
93
|
+
if only charpos is given, return the next byte offset of in string.
|
94
|
+
if charpos and offset is given, a new charpos will calculate, by
|
95
|
+
add/subtract UTF-8 char offset to current charpos.
|
96
|
+
in all case, it return a new char position, and code point (a number) at
|
97
|
+
this position.
|
98
|
+
|
99
|
+
|
100
|
+
###utf8.insert(s[, idx], substring) -> new_string
|
101
|
+
insert a substring to s. If idx is given, insert substring before char at
|
102
|
+
this index, otherwise substring will concat to s. idx can be negative.
|
103
|
+
|
104
|
+
|
105
|
+
###utf8.remove(s[, start[, stop]]) -> new_string
|
106
|
+
delete a substring in s. If neither start nor stop is given, delete the
|
107
|
+
last UTF-8 char in s, otherwise delete char from start to end of s. if
|
108
|
+
stop is given, delete char from start to stop (include start and stop).
|
109
|
+
start and stop can be negative.
|
110
|
+
|
111
|
+
|
112
|
+
###utf8.width(s[, ambi_is_double[, default_width]]) -> width
|
113
|
+
calculate the width of UTF-8 string s. if ambi_is_double is given, the
|
114
|
+
ambiguous width character's width is 2, otherwise it's 1.
|
115
|
+
fullwidth/doublewidth character's width is 2, and other character's width
|
116
|
+
is 1.
|
117
|
+
if default_width is given, it will be the width of unprintable character,
|
118
|
+
used display a non-character mark for these characters.
|
119
|
+
if s is a code point, return the width of this code point.
|
120
|
+
|
121
|
+
|
122
|
+
###utf8.widthindex(s, location[, ambi_is_double[, default_width]]) -> idx, offset, width
|
123
|
+
return the character index at given location in string s. this is a
|
124
|
+
reverse operation of utf8.width().
|
125
|
+
this function return a index of location, and a offset in in UTF-8
|
126
|
+
encoding. e.g. if cursor is at the second column (middle) of the wide
|
127
|
+
char, offset will be 2. the width of character at idx is returned, also.
|
128
|
+
|
129
|
+
|
130
|
+
###utf8.title(s) -> new_string
|
131
|
+
###utf8.fold(s) -> new_string
|
132
|
+
convert UTF-8 string s to title-case, or folded case used to compare by
|
133
|
+
ignore case.
|
134
|
+
if s is a number, it's treat as a code point and return a convert code
|
135
|
+
point (number). utf8.lower/utf8.upper has the same extension.
|
136
|
+
|
137
|
+
|
138
|
+
###utf8.ncasecmp(a, b) -> [-1,0,1]
|
139
|
+
compare a and b without case, -1 means a < b, 0 means a == b and 1 means a > b.
|
140
|
+
|
141
|
+
|
142
|
+
Improvement needed
|
143
|
+
------------------
|
144
|
+
|
145
|
+
- more test case.
|
146
|
+
- grapheme-compose support, and affect in utf8.reverse and utf8.width
|
147
|
+
- Unicode normalize algorithm implement.
|
148
|
+
|
149
|
+
|
150
|
+
License
|
151
|
+
-------
|
152
|
+
It use same license with Lua: http://www.lua.org/license.html
|