DEADSOFTWARE

Patched for Linux
[mp3cc.git] / MPC.3.5.LINUX / preverifier / main.c
1 /* * @(#)main.c 1.6 00/08/26
2 *
3 * Copyright 1997, 1998 by Sun Microsystems, Inc.,
4 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
5 * All rights reserved.
6 *
7 * This software is the confidential and proprietary information
8 * of Sun Microsystems, Inc. ("Confidential Information"). You
9 * shall not disclose such Confidential Information and shall use
10 * it only in accordance with the terms of the license agreement
11 * you entered into with Sun.
12 * Use is subject to license terms.
13 */
15 /*=========================================================================
16 * SYSTEM: Verifier
17 * SUBSYSTEM: main program
18 * FILE: main.c
19 * OVERVIEW: Runs the Java class verifier.
20 * AUTHOR: Sheng Liang, Sun Microsystems, Inc.
21 * Modifications for JAR support, CLDC compliance, and comments,
22 * Tasneem Sayeed, Sun Microsystems
23 *=======================================================================*/
26 /*=========================================================================
27 * Include files
28 *=======================================================================*/
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <string.h>
37 #include "sys_api.h"
38 #include "path_md.h"
39 #include "path.h"
40 #include "oobj.h"
41 //#include "jar.h"
42 #include "locale_md.h"
44 /*
45 #include "../util/strings.h"
46 #include "../util/error.h"
47 #include "../util/message.h"
48 #include "../structures/type_list.h"
49 #include "../structures/string_list.h"
50 #include "../structures/type.h"
51 #include "../structures/identifier.h"
52 #include "../structures/name_table.h"
53 #include "../classgen/bytecode.h"
54 #include "../structures/block.h"
56 #include "../parser/parser.h"
58 #include "../util/memory.h"
60 #include "../main/static_entry.h"
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 */
66 /*=========================================================================
67 * Globals and extern declarations
68 *=======================================================================*/
70 char str_buffer[STRINGBUFFERSIZE]; /* shared string buffer */
72 int processedfile = 0;
73 int errorCode = 0; /* Error code returned by program */
75 bool_t no_native_methods = FALSE;
76 bool_t no_floating_point = FALSE;
77 bool_t no_finalizers = FALSE;
78 char tmp_dir[32]; /* temporary directory for storing
79 verified classes */
80 extern char *output_dir; /* output directory */
81 bool_t tmpDirExists = FALSE;
83 extern void VerifyFile(register char *fn);
84 //extern bool_t ProcessJARfile(char *buf, int len);
85 //extern bool_t isJARfile(char *fn, int length);
87 //static void usage(char *progname);
90 /*=========================================================================
91 * FUNCTION: recurse_dir()
92 * TYPE: Handles recursive directories
93 * OVERVIEW: Internal function called by ProcessInputs().
94 *
95 * This function reads a directory, searching for either another directory,
96 * JAR file or an individual class name that is to be verified.
97 *
98 * INTERFACE:
99 * parameters: dirname name of the directory entry.
100 * pkgname name of the package
101 * returns: nothing
102 *=======================================================================*/
103 /*
104 static void recurse_dir(char *dirname, char *pkgname)
106 struct dirent *ent;
107 char buf[MAXPACKAGENAME];
108 char pkgbuf[MAXPACKAGENAME];
109 DIR *dir = opendir(dirname);
111 if (dir == NULL) {
112 fprintf(stderr, "Can't open dir %s\n", dirname);
113 exit(1);
115 for (ent = readdir(dir); ent; ent = readdir(dir)) {
116 struct stat stat_buf;
117 char *name = ent->d_name;
118 int len;
120 if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
121 continue;
124 strcpy(pkgbuf, pkgname);
125 if (pkgname[0] != 0) {
126 strcat(pkgbuf, "/");
129 if (JAR_DEBUG && verbose)
130 jio_fprintf(stderr,
131 "recurse_dir: Reading filename [%s] from directory [%s]\n",
132 name, dirname);
134 strcat(pkgbuf, name);
136 strcpy(buf, dirname);
137 strcat(buf, name);
139 stat(buf, &stat_buf);
140 len = strlen(buf);
142 if (stat_buf.st_mode & S_IFDIR) {
143 // handle the recursive directory found
144 strcat(buf, "/");
145 if (JAR_DEBUG && verbose)
146 jio_fprintf(stderr,
147 "recurse_dir: Recursive directory found, calling recurse_dir ([%s] [%s])\n",
148 buf, pkgbuf);
149 recurse_dir(buf, pkgbuf);
150 continue;
151 } else if (isJARfile (buf, len)) {
153 //
154 // this directory contains a JAR file which contains
155 // the classes to be verified
156 //
158 if (JAR_DEBUG && verbose)
159 jio_fprintf(stderr,
160 "recurse_dir: Found JAR file [%s] in dir!\n", buf);
162 if (!ProcessJARfile(buf, len)) {
164 fprintf(stderr, "Not a valid JAR file [%s]\n", buf);
165 exit(1);
169 // we just have a class file that needs verification
171 len = strlen(pkgbuf);
172 if (len > 6 && strcmp(pkgbuf + len - 6, ".class") == 0) {
174 pkgbuf[len - 6] = 0;
176 if (JAR_DEBUG && verbose)
177 jio_fprintf(stderr,
178 "recurse_dir: Verifying Class [%s] \n", pkgbuf);
179 VerifyFile(pkgbuf);
183 closedir(dir);
185 */
187 /*=========================================================================
188 * FUNCTION: ProcessInputs()
189 * TYPE: Processes inputs
190 * OVERVIEW: Internal function called by main().
192 * This function processes input entries for specifying classes that are to
193 * be verified. The inputs may be in 3 forms: a directory, or nested
194 * directories, one or more JAR files, or one or more individual class files.
195 * If a directory entry is specified, it may also contain one or more JAR
196 * files, or one or more individual class files.
198 * INTERFACE:
199 * parameters: argname: directory, JAR file or an individual class file.
200 * returns: nothing
201 *=======================================================================*/
202 /*
203 static void ProcessInputs(char *argname)
205 char buf[MAXPACKAGENAME];
206 struct stat stat_buf;
207 int res = stat(argname, &stat_buf);
208 int len;
211 strcpy(buf, argname);
212 len = strlen(argname);
215 if ((res == 0) && (stat_buf.st_mode & S_IFDIR)) {
216 /* Append dir separator if it does not yet exist.
217 if (buf[len - 1] != LOCAL_DIR_SEPARATOR &&
218 buf[len - 1] != DIR_SEPARATOR) {
219 buf[len] = DIR_SEPARATOR;
220 buf[len + 1] = 0;
222 pushDirectoryOntoClassPath(buf);
223 recurse_dir(buf, "");
224 popClassPath();
225 } else if ((res == 0) && (isJARfile (buf, len))) {
226 /* the classes to be verified are in a JAR file
227 if (!ProcessJARfile(buf, len)) {
228 fprintf(stderr, "Not a valid JAR file [%s]\n", buf);
229 exit(1);
231 } else {
232 char *p;
233 /* Convert all periods in the argname to slashes
234 for (p = buf; ((p = strchr(p, '.')) != 0); *p++ = '/');
235 if (JAR_DEBUG && verbose)
236 jio_fprintf(stderr,
237 "ProcessInputs: Verifying file [%s]\n", buf );
238 VerifyFile(buf);
240 }*/
243 /*=========================================================================
244 * FUNCTION: main()
245 * TYPE: Runs the verifier.
246 * OVERVIEW: Main function.
248 * This is the main function that invokes the Java class verifier.
250 * INTERFACE:
251 * parameters: argc: arg count.
252 * argv: arg value(s)
253 * returns: nothing
254 *=======================================================================*/
255 /*
256 int main(argc, argv)
257 register char **argv;
259 char *progname;
260 char *argv0 = argv[0];
262 SET_DEFAULT_LOCALE;
264 if ((progname = strrchr(argv[0], LOCAL_DIR_SEPARATOR)) != 0) {
265 progname++;
266 } else {
267 progname = argv[0];
270 // initialize tmp_dir
271 memset(tmp_dir, 0, 32);
273 // initialize the seed for the random number
274 srand(time(NULL));
276 // generate a random temp directory name
277 sprintf(tmp_dir, "%s%d%c", "tmp", rand(), '\0');
279 while (--argc > 0)
280 if ((++argv)[0][0] == '-') {
281 if (strcmp(argv[0], "-verbose") == 0) {
282 extern bool_t verbose;
283 verbose = TRUE;
284 #ifdef DEBUG_VERIFIER
285 } else if (strcmp(argv[0], "-verify-verbose") == 0) {
286 extern int verify_verbose;
287 verify_verbose++;
288 #endif
289 } else if (strcmp(argv[0], "-cldc") == 0) {
290 no_native_methods = TRUE;
291 no_floating_point = TRUE;
292 no_finalizers = TRUE;
293 } else if (strcmp(argv[0], "-nofinalize") == 0) {
294 no_finalizers = TRUE;
295 } else if (strcmp(argv[0], "-nonative") == 0) {
296 no_native_methods = TRUE;
297 } else if (strcmp(argv[0], "-nofp") == 0) {
298 no_floating_point = TRUE;
299 } else if (strcmp(argv[0], "-Xns") == 0) {
300 extern bool_t stack_map_on;
301 stack_map_on = FALSE;
302 } else if (strcmp(argv[0], "-Xni") == 0) {
303 extern bool_t inline_jsr_on;
304 inline_jsr_on = FALSE;
305 } else if (strcmp(argv[0], "-d") == 0) {
306 output_dir = strdup(argv[1]);
307 argc--; argv++;
308 } else if (strcmp(argv[0], "-classpath") == 0) {
309 if (argc > 1) {
310 char *buf = (char *)malloc(strlen(argv[1]) + 32);
311 sprintf(buf, "CLASSPATH=%s", argv[1]);
312 putenv(buf);
313 argc--; argv++;
314 } else {
315 //fprintf(stderr,
316 // "-classpath requires class path specification\n");
317 // usage(progname);
318 exit(1);
320 } else {
321 //fprintf(stderr, "%s: Illegal option %s\n\n", progname, argv[0]);
322 // usage(progname);
323 exit(1);
325 } else if (argv[0][0] == '@') {
326 char *new_argv[MAXOPTIONS];
327 char *buffer;
328 int new_argc = 1;
329 char *token;
330 bool_t done = FALSE;
331 struct stat sbuf;
332 FILE *fp;
333 int buflen;
334 int filelength;
335 int i;
337 new_argv[0] = argv0;
339 fp = fopen(&argv[0][1], "rt");
340 if (fp == NULL) {
341 fprintf(stderr, "Can't open %s\n", &argv[0][1]);
342 exit(1);
345 // get the size of the file
346 stat(&argv[0][1], &sbuf);
347 filelength = sbuf.st_size;
349 // allocate the buffer
350 if ((buffer = (char *) malloc(filelength+1)) == NULL) {
351 fprintf(stderr, "Out of memory\n");
352 exit(1);
355 if (fgets(buffer, filelength+1, fp) != NULL) {
356 buflen = strlen(buffer);
358 if (DEBUG_READFROMFILE) {
359 fprintf(stderr, "Buffer = [%s]\n", buffer);
361 // get the first parameter
362 token = strtok(buffer, " \0\n\r\t\"");
364 // Search for parameters until none can be found
365 while (token != NULL && !done) {
367 if ((strcmp(token, "\n") == 0) ||
368 (strcmp(token, "\r") == 0) ||
369 (strcmp(token, "\t") == 0)) {
370 // <NL>, <CR> or <TAB>
371 done = TRUE;
372 } else {
373 // On Win32, it's possible to have a <CR> <LF>
374 // appended at the end of the token, which needs
375 // to be extracted before restoring the arg
376 //
377 char *p;
378 // Convert all <CR> <LF> to NULL
379 for (p = token; *p != '\0'; p++) {
380 if (*p == '\n' || *p == '\r')
381 *p = '\0';
384 // save the parameter
385 new_argv[new_argc++] = token;
387 if ((strcmp(token, "-classpath") == 0) ||
388 (strcmp(token, "-d") == 0)) {
389 // search for the beginning of a string literal
390 token = strtok(NULL, "\"");
392 } else { // get the next token
393 token = strtok(NULL," \0\n\r\t\"");
398 if (DEBUG_READFROMFILE) {
399 // print the arguments after restoring all of them
400 for (i=0; i < new_argc; i++)
401 fprintf(stderr, "new_argv[%d] = [%s]\n", i, new_argv[i]);
405 fclose(fp);
406 main(new_argc, new_argv);
408 if (buffer != NULL) {
409 free(buffer);
411 argc--; argv++;
412 } else {
413 processedfile++;
414 if (strlen(argv[0]) > MAXPACKAGENAME) {
415 // fprintf(stderr,
416 // "Package name for class exceeds max allowed size [1024]\n");
417 // usage(progname);
418 exit(1);
423 ProcessInputs(argv[0]);
425 if (processedfile == 0) {
426 usage(progname);
427 exit(1);
429 return errorCode; // normal errorCode=0 indicates successful completion
430 }*/
431 /*
432 static void usage(char *progname) {
433 fprintf(stderr, "Usage: %s [options] classnames|dirnames ...\n", progname);
434 fprintf(stderr, "\n");
435 fprintf(stderr, "where options include:\n");
436 fprintf(stderr, " -classpath <directories separated by '%c'>\n", (char)PATH_SEPARATOR);
437 fprintf(stderr, " Directories in which to look for classes\n");
438 fprintf(stderr, " -d <directory> Directory in which output is written (default is ./output/)\n");
440 fprintf(stderr, " -cldc Checks for existence of language features prohibited\n");
441 fprintf(stderr, " by CLDC (native methods, floating point and finalizers)\n");
443 fprintf(stderr, " -nofinalize No finalizers allowed\n");
444 fprintf(stderr, " -nonative No native methods allowed\n");
445 fprintf(stderr, " -nofp No floating point operations allowed\n");
446 fprintf(stderr, " @<filename> Read command line arguments from a text file\n");
447 fprintf(stderr, " Command line arguments must all be on a single line\n");
448 fprintf(stderr, " Directory names must be enclosed in double quotes (\")\n");
450 fprintf(stderr, "\n");
452 */
455 #ifndef O_BINARY
456 #define O_BINARY 0
457 #endif
459 //static
460 int
461 OpenCode(char *fn, char *sfn, char *dir, struct stat * st)
463 long codefd;
464 char *fullname = (char*) malloc(strlen(fn) + strlen(dir) + 10);
465 #ifdef WIN32
466 sprintf(fullname, "%s\\%s", dir, fn);
467 #endif
468 #ifdef UNIX
469 sprintf(fullname, "%s/%s", dir, fn);
470 #endif
471 if (fn == 0 || (codefd = open(fullname, O_BINARY, 0644)) < 0
472 || fstat(codefd, st) < 0)
474 free(fullname);
475 return -2;
477 free(fullname);
478 return codefd;