Browse code

jsnorm api.

Török Edvin authored on 2010/03/31 16:53:11
Showing 6 changed files
... ...
@@ -25,6 +25,7 @@
25 25
 #endif
26 26
 
27 27
 #include <assert.h>
28
+#include <fcntl.h>
28 29
 #include "dconf.h"
29 30
 #include "clamav.h"
30 31
 #include "others.h"
... ...
@@ -110,11 +111,36 @@ static int cli_bytecode_context_reset(struct cli_bc_ctx *ctx)
110 110
 	ctx->tempfile = NULL;
111 111
 	ctx->outfd = 0;
112 112
     }
113
+    if (ctx->jsnormdir) {
114
+	char fullname[1025];
115
+	cli_ctx *cctx = ctx->ctx;
116
+	int fd, ret = CL_CLEAN;
117
+
118
+	if (!ctx->found) {
119
+	    snprintf(fullname, 1024, "%s"PATHSEP"javascript", ctx->jsnormdir);
120
+	    fd = open(fullname, O_RDONLY|O_BINARY);
121
+	    if(fd >= 0) {
122
+		ret = cli_scandesc(fd, cctx, CL_TYPE_HTML, 0, NULL, AC_SCAN_VIR);
123
+		if (ret == CL_CLEAN) {
124
+		    lseek(fd, 0, SEEK_SET);
125
+		    ret = cli_scandesc(fd, cctx, CL_TYPE_TEXT_ASCII, 0, NULL, AC_SCAN_VIR);
126
+		}
127
+		close(fd);
128
+	    }
129
+	}
130
+	if (!cctx || !cctx->engine->keeptmp) {
131
+	    cli_rmdirs(ctx->jsnormdir);
132
+	}
133
+	free(ctx->jsnormdir);
134
+	if (ret != CL_CLEAN)
135
+	    ctx->found = 1;
136
+    }
113 137
     ctx->numParams = 0;
114 138
     ctx->funcid = 0;
115 139
     ctx->file_size = 0;
116 140
     ctx->off = 0;
117 141
     ctx->written = 0;
142
+    ctx->jsnormwritten = 0;
118 143
 #if USE_MPOOL
119 144
     if (ctx->mpool) {
120 145
 	mpool_destroy(ctx->mpool);
... ...
@@ -140,6 +166,13 @@ static int cli_bytecode_context_reset(struct cli_bc_ctx *ctx)
140 140
     free(ctx->hashsets);
141 141
     ctx->hashsets = NULL;
142 142
     ctx->nhashsets = 0;
143
+
144
+    for (i=0;i<ctx->njsnorms;i++)
145
+	cli_bcapi_jsnorm_done(ctx, i);
146
+    free(ctx->jsnorms);
147
+    ctx->jsnorms = NULL;
148
+    ctx->njsnorms = 0;
149
+    ctx->jsnormdir = NULL;
143 150
     return CL_SUCCESS;
144 151
 }
145 152
 
... ...
@@ -42,6 +42,7 @@
42 42
 #include "pe.h"
43 43
 #include "disasm.h"
44 44
 #include "scanners.h"
45
+#include "jsparse/js-norm.h"
45 46
 
46 47
 uint32_t cli_bcapi_test1(struct cli_bc_ctx *ctx, uint32_t a, uint32_t b)
47 48
 {
... ...
@@ -817,3 +818,80 @@ int32_t cli_bcapi_bytecode_rt_error(struct cli_bc_ctx *ctx , int32_t id)
817 817
     return 0;
818 818
 }
819 819
 
820
+int32_t cli_bcapi_jsnorm_init(struct cli_bc_ctx *ctx, int32_t from)
821
+{
822
+    struct parser_state *state;
823
+    struct bc_jsnorm *b;
824
+    unsigned  n = ctx->njsnorms + 1;
825
+    if (!get_buffer(ctx, from)) {
826
+	cli_dbgmsg("bytecode api: jsnorm_init: invalid buffers!\n");
827
+	return -1;
828
+    }
829
+    state = cli_js_init();
830
+    if (!state)
831
+	return -1;
832
+    b = cli_realloc(ctx->jsnorms, sizeof(*ctx->jsnorms)*n);
833
+    if (!b) {
834
+	cli_js_destroy(state);
835
+	return -1;
836
+    }
837
+    ctx->jsnorms = b;
838
+    ctx->njsnorms = n;
839
+    b = &b[n-1];
840
+    b->from = from;
841
+    b->state = state;
842
+    if (!ctx->jsnormdir) {
843
+	cli_ctx *cctx = (cli_ctx*)ctx->ctx;
844
+	ctx->jsnormdir = cli_gentemp(cctx ? cctx->engine->tmpdir : NULL);
845
+	if (ctx->jsnormdir) {
846
+	    if (mkdir(ctx->jsnormdir, 0700)) {
847
+		cli_dbgmsg("js: can't create temp dir %s\n", ctx->jsnormdir);
848
+		free(ctx->jsnormdir);
849
+		return CL_ETMPDIR;
850
+	    }
851
+	}
852
+    }
853
+    return n-1;
854
+}
855
+
856
+static struct bc_jsnorm *get_jsnorm(struct cli_bc_ctx *ctx, int32_t id)
857
+{
858
+    if (id < 0 || id >= ctx->njsnorms || !ctx->jsnorms)
859
+	return NULL;
860
+    return &ctx->jsnorms[id];
861
+}
862
+
863
+int32_t cli_bcapi_jsnorm_process(struct cli_bc_ctx *ctx, int32_t id)
864
+{
865
+    unsigned avail;
866
+    char *in;
867
+    cli_ctx *cctx = ctx->ctx;
868
+    struct bc_jsnorm *b = get_jsnorm(ctx, id);
869
+    if (!b || b->from == -1 || !b->state)
870
+	return -1;
871
+
872
+    avail = cli_bcapi_buffer_pipe_read_avail(ctx, b->from);
873
+    in = cli_bcapi_buffer_pipe_read_get(ctx, b->from, avail);
874
+    if (!avail || !in)
875
+	return -1;
876
+    if (cctx && cli_checklimits("bytecode js api", cctx, ctx->jsnormwritten + avail, 0, 0))
877
+	return -1;
878
+    cli_bcapi_buffer_pipe_read_stopped(ctx, b->from, avail);
879
+    cli_js_process_buffer(b->state, in, avail);
880
+    return 0;
881
+}
882
+
883
+int32_t cli_bcapi_jsnorm_done(struct cli_bc_ctx *ctx , int32_t id)
884
+{
885
+    struct bc_jsnorm *b = get_jsnorm(ctx, id);
886
+    if (!b || b->from == -1)
887
+	return -1;
888
+    if (ctx->ctx && cli_updatelimits(ctx->ctx, ctx->jsnormwritten))
889
+	return -1;
890
+    ctx->jsnormwritten = 0;
891
+    cli_js_parse_done(b->state);
892
+    cli_js_output(b->state, ctx->jsnormdir);
893
+    cli_js_destroy(b->state);
894
+    b->from = -1;
895
+    return 0;
896
+}
... ...
@@ -247,5 +247,9 @@ int32_t inflate_done(int32_t id);
247 247
 
248 248
 int32_t bytecode_rt_error(int32_t locationid);
249 249
 
250
+int32_t jsnorm_init(int32_t from_buffer);
251
+int32_t jsnorm_process(int32_t id);
252
+int32_t jsnorm_done(int32_t id);
253
+
250 254
 #endif
251 255
 #endif
... ...
@@ -75,6 +75,9 @@ int32_t cli_bcapi_inflate_init(struct cli_bc_ctx *ctx , int32_t, int32_t, int32_
75 75
 int32_t cli_bcapi_inflate_process(struct cli_bc_ctx *ctx , int32_t);
76 76
 int32_t cli_bcapi_inflate_done(struct cli_bc_ctx *ctx , int32_t);
77 77
 int32_t cli_bcapi_bytecode_rt_error(struct cli_bc_ctx *ctx , int32_t);
78
+int32_t cli_bcapi_jsnorm_init(struct cli_bc_ctx *ctx , int32_t);
79
+int32_t cli_bcapi_jsnorm_process(struct cli_bc_ctx *ctx , int32_t);
80
+int32_t cli_bcapi_jsnorm_done(struct cli_bc_ctx *ctx , int32_t);
78 81
 
79 82
 const struct cli_apiglobal cli_globals[] = {
80 83
 /* Bytecode globals BEGIN */
... ...
@@ -187,7 +190,10 @@ const struct cli_apicall cli_apicalls[]={
187 187
 	{"inflate_init", 9, 0, 7},
188 188
 	{"inflate_process", 8, 13, 2},
189 189
 	{"inflate_done", 8, 14, 2},
190
-	{"bytecode_rt_error", 8, 15, 2}
190
+	{"bytecode_rt_error", 8, 15, 2},
191
+	{"jsnorm_init", 8, 16, 2},
192
+	{"jsnorm_process", 8, 17, 2},
193
+	{"jsnorm_done", 8, 18, 2}
191 194
 /* Bytecode APIcalls END */
192 195
 };
193 196
 const cli_apicall_int2 cli_apicalls0[] = {
... ...
@@ -230,7 +236,10 @@ const cli_apicall_int1 cli_apicalls2[] = {
230 230
 	(cli_apicall_int1)cli_bcapi_buffer_pipe_done,
231 231
 	(cli_apicall_int1)cli_bcapi_inflate_process,
232 232
 	(cli_apicall_int1)cli_bcapi_inflate_done,
233
-	(cli_apicall_int1)cli_bcapi_bytecode_rt_error
233
+	(cli_apicall_int1)cli_bcapi_bytecode_rt_error,
234
+	(cli_apicall_int1)cli_bcapi_jsnorm_init,
235
+	(cli_apicall_int1)cli_bcapi_jsnorm_process,
236
+	(cli_apicall_int1)cli_bcapi_jsnorm_done
234 237
 };
235 238
 const cli_apicall_malloclike cli_apicalls3[] = {
236 239
 	(cli_apicall_malloclike)cli_bcapi_malloc
... ...
@@ -72,5 +72,8 @@ int32_t cli_bcapi_inflate_init(struct cli_bc_ctx *ctx , int32_t, int32_t, int32_
72 72
 int32_t cli_bcapi_inflate_process(struct cli_bc_ctx *ctx , int32_t);
73 73
 int32_t cli_bcapi_inflate_done(struct cli_bc_ctx *ctx , int32_t);
74 74
 int32_t cli_bcapi_bytecode_rt_error(struct cli_bc_ctx *ctx , int32_t);
75
+int32_t cli_bcapi_jsnorm_init(struct cli_bc_ctx *ctx , int32_t);
76
+int32_t cli_bcapi_jsnorm_process(struct cli_bc_ctx *ctx , int32_t);
77
+int32_t cli_bcapi_jsnorm_done(struct cli_bc_ctx *ctx , int32_t);
75 78
 
76 79
 #endif
... ...
@@ -132,6 +132,11 @@ struct bc_inflate {
132 132
     int8_t  needSync;
133 133
 };
134 134
 
135
+struct bc_jsnorm {
136
+    struct parser_state *state;
137
+    int32_t from;
138
+};
139
+
135 140
 struct cli_bc_ctx {
136 141
     uint8_t timeout;/* must be first byte in struct! */
137 142
     /* id and params of toplevel function called */
... ...
@@ -174,6 +179,10 @@ struct cli_bc_ctx {
174 174
     unsigned nbuffers;
175 175
     struct cli_hashset *hashsets;
176 176
     unsigned nhashsets;
177
+    struct bc_jsnorm* jsnorms;
178
+    unsigned njsnorms;
179
+    char *jsnormdir;
180
+    unsigned jsnormwritten;
177 181
 };
178 182
 struct cli_all_bc;
179 183
 int cli_vm_execute(const struct cli_bc *bc, struct cli_bc_ctx *ctx, const struct cli_bc_func *func, const struct cli_bc_inst *inst);