Browse code

Add portability

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@2496 77e5149b-7576-45b1-b177-96237e5ba77b

Nigel Horne authored on 2006/11/11 05:17:44
Showing 4 changed files
... ...
@@ -24,7 +24,7 @@
24 24
 
25 25
 /*
26 26
  * $Source: /tmp/cvsroot-15-2-2007/clamav-devel/libclamav/js/jsint.h,v $
27
- * $Id: jsint.h,v 1.3 2006/11/02 09:36:19 njh Exp $
27
+ * $Id: jsint.h,v 1.4 2006/11/10 20:17:43 njh Exp $
28 28
  */
29 29
 
30 30
 #ifndef JSINT_H
... ...
@@ -86,7 +86,7 @@
86 86
 
87 87
 #endif /* not WIN32 */
88 88
 
89
-#include <js.h>
89
+#include "js.h"
90 90
 
91 91
 #if __cplusplus
92 92
 extern "C" {
... ...
@@ -85,7 +85,7 @@ show_events_hook (int event, void *context)
85 85
 
86 86
 
87 87
 JSInterpPtr
88
-create_interp ()
88
+create_interp(JSIOFunc s_stdout)
89 89
 {
90 90
 	JSInterpOptions options;
91 91
 	JSInterpPtr interp;
... ...
@@ -136,6 +136,8 @@ create_interp ()
136 136
 		options.hook_operand_count_trigger = 1000000;
137 137
 	}
138 138
 
139
+	options.s_stdout = s_stdout;
140
+
139 141
 	interp = js_create_interp (&options);
140 142
 	if (interp == NULL) {
141 143
 		cli_errmsg("js: couldn't create interpreter\n");
... ...
@@ -19,7 +19,7 @@
19 19
  * Save the JavaScript embedded in an HTML file, then run the script, saving
20 20
  * the output in a file that is to be scanned, then remove the script file
21 21
  */
22
-static	char	const	rcsid[] = "$Id: jscript.c,v 1.3 2006/11/09 09:27:18 njh Exp $";
22
+static	char	const	rcsid[] = "$Id: jscript.c,v 1.4 2006/11/10 20:17:16 njh Exp $";
23 23
 
24 24
 #if HAVE_CONFIG_H
25 25
 #include "clamav-config.h"
... ...
@@ -61,7 +61,7 @@ static	char	const	rcsid[] = "$Id: jscript.c,v 1.3 2006/11/09 09:27:18 njh Exp $"
61 61
 # endif
62 62
 #endif
63 63
 
64
-static	void	run_js(const char *filename, const char *dir);
64
+static	int	run_js(const char *filename, const char *dir);
65 65
 static	const	char	*cli_pmemstr(const char *haystack, size_t hs, const char *needle, size_t ns);
66 66
 
67 67
 int
... ...
@@ -153,7 +153,7 @@ cli_scanjs(const char *dir, int desc)
153 153
 					if(fout) {
154 154
 						fclose(fout);
155 155
 						fout = NULL;
156
-						run_js(script_filename, dir);
156
+						(void)run_js(script_filename, dir);
157 157
 
158 158
 						if(!cli_leavetemps_flag)
159 159
 							unlink(script_filename);
... ...
@@ -236,7 +236,7 @@ cli_scanjs(const char *dir, int desc)
236 236
 		cli_dbgmsg("No javascript was detected\n");
237 237
 	else if(fout) {
238 238
 		fclose(fout);
239
-		run_js(script_filename, dir);
239
+		rc = run_js(script_filename, dir);
240 240
 
241 241
 		if(!cli_leavetemps_flag)
242 242
 			unlink(script_filename);
... ...
@@ -244,32 +244,47 @@ cli_scanjs(const char *dir, int desc)
244 244
 	return rc;
245 245
 }
246 246
 
247
-static void
247
+#include "js/compiler.c"
248
+#include "js/iostream.c"
249
+#include "js/js.c"
250
+#include "js/main.c"
251
+#include "js/debug.c"
252
+#include "js/crc32.c"
253
+
254
+static	FILE *fout;
255
+
256
+static	int
257
+write_to_fout(void *context, unsigned char *buf, unsigned int len)
258
+{
259
+	return (int)fwrite(buf, (size_t)len, 1, fout);
260
+}
261
+
262
+static int
248 263
 run_js(const char *filename, const char *dir)
249 264
 {
250 265
 	JSInterpPtr interp;
251
-	FILE *real_stdout, *temp_stdout;
252 266
 	char *outputfilename;
253 267
 
254 268
 	cli_dbgmsg("run_js(%s)\n", filename);
255 269
 
256
-	fflush(stdout);
257
-	real_stdout = stdout;
258 270
 	outputfilename = cli_gentemp(dir);
259
-	if(outputfilename) {
260
-		temp_stdout = fopen(outputfilename, "wb");
261
-		if(temp_stdout) {
262
-			cli_dbgmsg("Redirecting JS VM stdout to %s\n",
263
-				outputfilename);
264
-			stdout = temp_stdout;
265
-		}
266
-	} else
267
-		temp_stdout = NULL;
271
+	if(outputfilename == NULL)
272
+		return CL_ETMPFILE;
273
+
274
+	fout = fopen(outputfilename, "wb");
275
+	if(fout == NULL) {
276
+		cli_warnmsg("Can't create %s\n", outputfilename);
277
+		free(outputfilename);
278
+		return CL_ETMPFILE;
279
+	}
280
+
281
+	cli_dbgmsg("Redirecting JS VM stdout to %s\n", outputfilename);
282
+	free(outputfilename);
268 283
 
269 284
 	/*
270 285
 	 * Run NGS on the file
271 286
 	 */
272
-	interp = create_interp();
287
+	interp = create_interp(write_to_fout);
273 288
 
274 289
 	if(!js_eval_file(interp, filename)) {
275 290
 		cli_warnmsg("JS failed: %s\n", js_error_message(interp));
... ...
@@ -278,20 +293,10 @@ run_js(const char *filename, const char *dir)
278 278
 
279 279
 	js_destroy_interp(interp);
280 280
 
281
-	if(temp_stdout) {
282
-		fclose(temp_stdout);
283
-		stdout = real_stdout;
284
-	}
285
-	if(outputfilename)
286
-		free(outputfilename);
287
-}
281
+	fclose(fout);
288 282
 
289
-#include "js/compiler.c"
290
-#include "js/iostream.c"
291
-#include "js/js.c"
292
-#include "js/main.c"
293
-#include "js/debug.c"
294
-#include "js/crc32.c"
283
+	return CL_SUCCESS;
284
+}
295 285
 
296 286
 /* Copied from pdf.c :-( */
297 287
 /*
... ...
@@ -18,7 +18,7 @@
18 18
  */
19 19
 
20 20
 #include "js/js.h"
21
+#include "js/jsint.h"
21 22
 
22 23
 int	cli_scanjs(const char *dir, int desc);
23
-
24
-JSInterpPtr	create_interp(void);
24
+JSInterpPtr	create_interp(JSIOFunc s_stdout);