📄 file.c
字号:
/* Attribute count * Each item in the following sum is a boolean that returns 1 or 0. */ write_u2( (mb->code_length > 0) + (mb->nexceptions > 0) + (mb->fb.synthetic != 0) + (mb->fb.deprecated != 0)); if (mb->code_length > 0) { int j; int stack_map_attr_length = 0; /* Stack Map attribute length */ int line_no_attr_length = 0; /* line_number_table attr Length */ int localvar_attr_length = 0; /* localVar_table attr Length */ int code_attrs = 0; /* Attributes count for Code attribute */ if (stack_map_on && mb->n_stack_maps > 0) { stack_map_attr_length = 8; code_attrs++; /* increment code attributes */ for (j = 0; j < mb->n_stack_maps; j++) { stack_map_attr_length += 6 + stack_map_size(mb->stack_maps[j].nlocals, mb->stack_maps[j].locals) + stack_map_size(mb->stack_maps[j].nstacks, mb->stack_maps[j].stacks); } } /* attribute_name_index for "Code" attribute */ write_u2(lookup_utf8(cb, "Code")); if (mb->line_number_table_length > 0) { /* calculate the size of the line_number_table attribute */ /* Attribute length for line_number_table * sizeof line_number_table(4) * no. of table entries + 8 * 8 bytes = 2 bytes for attr_name_index + * 4 bytes for attr_length + * 2 bytes for line_number_table_length */ code_attrs++; /* increment code attributes */ line_no_attr_length = 4 * mb->line_number_table_length + 8; } if (mb->localvar_table_length > 0) { /* calculate the size of the localvar_table attribute */ /* Attribute length for localvar_table * sizeof localvar_table (10) * no. of table entries + 8 * 8 bytes = 2 bytes for attr_name_index + * 4 bytes for attr_length + * 2 bytes for line_number_table_length */ code_attrs++; /* increment code attributes */ localvar_attr_length = 10 * mb->localvar_table_length + 8; } /* Attribute Length */ write_u4(12 + mb->code_length + mb->exception_table_length * 8 + stack_map_attr_length + line_no_attr_length + localvar_attr_length); write_u2(mb->maxstack); write_u2(mb->nlocals); write_u4(mb->code_length); for (j = 0; j < (int)mb->code_length; j++) { write_u1(mb->code[j]); } write_u2(mb->exception_table_length); for (j = 0; j < (int)mb->exception_table_length; j++) { write_u2(mb->exception_table[j].start_pc); write_u2(mb->exception_table[j].end_pc); write_u2(mb->exception_table[j].handler_pc); write_u2(mb->exception_table[j].catchType); } /* Attributes count for Code attribute */ write_u2(code_attrs); /* check if we have a valid line_number_table entries and * if so, write out the pc and line_number entries. */ if (mb->line_number_table_length > 0) { /* line_number_table attribute exists */ /* attribute_name_index for "LineNumberTable" */ write_u2(lookup_utf8(cb, "LineNumberTable")); /* Attribute length for line_number_table * (exclude initial 6 bytes = 2 bytes for attr_name_index + * 4 bytes for attr_length) */ write_u4(line_no_attr_length - 6); /* Length of line_number_table */ write_u2(mb->line_number_table_length); /* write out the line_number_table entries */ for (j=0; j< (int) mb->line_number_table_length; j++) { if (mb->line_number_table != NULL) { write_u2(mb->line_number_table[j].pc); write_u2(mb->line_number_table[j].line_number); } } } /* check if we have a valid localvar_table entries and * if so, write out its entries */ if (mb->localvar_table_length > 0) { /* localvar_table attribute exists */ /* attribute_name_index for "LocalVariableTable" */ write_u2(lookup_utf8(cb, "LocalVariableTable")); /* Attribute length for localvar_table * (exclude initial 6 bytes = 2 bytes for attr_name_index + * 4 bytes for attr_length) */ write_u4(localvar_attr_length - 6); /* Length of localvar_table */ write_u2(mb->localvar_table_length); /* write out the localvar_table entries */ for (j=0; j< (int) mb->localvar_table_length; j++) { if (mb->localvar_table != NULL) { write_u2(mb->localvar_table[j].pc0); write_u2(mb->localvar_table[j].length); write_u2(lookup_utf8(cb,mb->localvar_table[j].name)); write_u2(lookup_utf8(cb,mb->localvar_table[j].signature)); write_u2(mb->localvar_table[j].slot); } } } if (stack_map_on && mb->n_stack_maps > 0) { write_u2(lookup_utf8(cb, "StackMap")); write_u4(stack_map_attr_length - 6); write_u2(mb->n_stack_maps); for (j = 0; j < mb->n_stack_maps; j++) { write_u2(mb->stack_maps[j].offset); write_u2(mb->stack_maps[j].nlocals); write_stack_map(mb->stack_maps[j].nlocals, mb->stack_maps[j].locals); write_u2(mb->stack_maps[j].nstacks); write_stack_map(mb->stack_maps[j].nstacks, mb->stack_maps[j].stacks); } } } if (mb->nexceptions > 0) { int j; write_u2(lookup_utf8(cb, "Exceptions")); write_u4(2 + mb->nexceptions * 2); write_u2(mb->nexceptions); for (j = 0; j < mb->nexceptions; j++) { write_u2(mb->exceptions[j]); } } if (mb->fb.deprecated) { write_u2(lookup_utf8(cb, "Deprecated")); write_u4(0); /* Length */ } if (mb->fb.synthetic) { write_u2(lookup_utf8(cb, "Synthetic")); write_u4(0); /* Length */ } }}void ensure_dir_exists(char *dir){ struct stat stat_buf; char *parent; char *q; if (dir[0] == 0) { return; } parent = strdup(dir); q = strrchr(parent, '/'); if (q) { *q = 0; ensure_dir_exists(parent); } if (stat(dir, &stat_buf) < 0) { if (JAR_DEBUG && verbose) { jio_fprintf(stderr, "Creating output directory [%s]\n", dir); }#ifdef WIN32 mkdir(dir);#endif#ifdef UNIX mkdir(dir, 0755);#endif } free(parent);}void ensure_dir_writable(char *dir){ struct stat stat_buf; stat(dir, &stat_buf); if ((stat_buf.st_mode & S_IFMT) == S_IFDIR) { /* is dir ? */#ifdef WIN32 if (access(dir, 06) < 0) {#endif#ifdef UNIX if (access(dir, R_OK | W_OK) < 0) {#endif panic("%s is write protected\n", dir); } } else { panic("%s is not a directory\n", dir); }}voidWriteClass(ClassClass *cb){ int fd; char fname[1024]; char buff[BUFSIZ]; char *nativeName = &buff[0]; class_buf = (unsigned char*)malloc(INIT_CLASS_BUF_SIZE); if (class_buf == NULL) { panic("out of memory"); } class_buf_size = INIT_CLASS_BUF_SIZE; class_index = 0; write_u4(0xcafebabe); write_u2(cbMinorVersion(cb)); write_u2(cbMajorVersion(cb)); write_constant_pool(cb); write_u2(cbAccess(cb) & ACC_WRITTEN_FLAGS); write_u2(lookup_class(cb, cbName(cb))); write_u2(cbSuperclass(cb) ? lookup_class(cb, cbName(cbSuperclass(cb))) : 0); write_interfaces(cb); write_fields(cb); write_methods(cb); /* Output number of attributes * Each item in the following sum is a boolean that returns 1 or 0. */ write_u2( (cbSourceName(cb) != NULL) + (cbAbsoluteSourceName(cb) != NULL) + (unhand(cb)->hasTimeStamp) + (unhand(cb)->deprecated) + (unhand(cb)->synthetic) + (cbInnerClasses(cb) != NULL) ); if (cbSourceName(cb) != NULL) { /* write the source file attribute used for debugging purposes */ write_u2(lookup_utf8(cb, "SourceFile")); /* SourceFile attribute */ write_u4(2); /* Length */ /* CP entry containing the source name */ write_u2(lookup_utf8(cb, cbSourceName(cb))); } if (cbAbsoluteSourceName(cb) != NULL) { /* write the source file attribute used for debugging purposes */ write_u2(lookup_utf8(cb, "AbsoluteSourcePath")); write_u4(2); write_u2(lookup_utf8(cb, cbAbsoluteSourceName(cb))); } if (unhand(cb)->hasTimeStamp) { write_u2(lookup_utf8(cb, "TimeStamp")); write_u4(8); write_u4(cbTimestamp(cb).high); write_u4(cbTimestamp(cb).low); } if (unhand(cb)->deprecated) { write_u2(lookup_utf8(cb, "Deprecated")); /* attribute name */ write_u4(0); /* Length */ } if (unhand(cb)->synthetic) { write_u2(lookup_utf8(cb, "Synthetic")); /* attribute name */ write_u4(0); /* Length */ } if (cbInnerClasses(cb) != NULL) { int count = cbInnerClassesCount(cb); struct innerClasses *thisInnerClass= cbInnerClasses(cb); struct innerClasses *lastInnerClass = thisInnerClass + count; write_u2(lookup_utf8(cb, "InnerClasses")); /* Attribute name */ write_u4(8 * count + 2); /* Length */ write_u2(count); for ( ; thisInnerClass < lastInnerClass; thisInnerClass++) { char *innerName = thisInnerClass->inner_name; write_u2(thisInnerClass->inner_class); write_u2(thisInnerClass->outer_class); write_u2( (innerName != 0) ? lookup_utf8(cb, innerName) : 0); write_u2(thisInnerClass->access); } } /* Conversion for Japanese filenames */ utf2native(cbName(cb), nativeName, BUFSIZ); if (JARfile) { /* classes need to be put in a JAR file */ sprintf(fname, "%s/%s.class", tmp_dir, nativeName); } else { sprintf(fname, "%s/%s.class", output_dir, nativeName); } { char *dir = strdup(fname); char *q; q = strrchr(dir, '/'); if (q) { *q = 0; ensure_dir_exists(dir); ensure_dir_writable(dir); } free(dir); }#ifdef UNIX fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC , 0644);#endif#ifdef WIN32 fd = open(fname, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);#endif if (fd < 0) { panic("failed to open %s", fname); } tmpDirExists = TRUE; /* tmpDir exists with verified classes */ write(fd, class_buf, class_index); close(fd); free(class_buf); class_buf_size = 0;}voidVerifyFile(char *fn){ /* If this is the first class, we'll run into problems if loading the * class forces Object to be loaded, when then forces this class to * be loaded. To prohibit such problems, we force Object to be loaded */ FindClass(0, "java/lang/Object", TRUE); { ClassClass *cb = FindClass(0, fn, TRUE); char *class_name = PrintableClassname(fn); if (cb == NULL) { errorCode = 1; /* set error status to indicate error */ jio_fprintf(stderr, "Error loading class %s\n", class_name); } else { if (no_native_methods) { /* Check for native methods in classes */ struct methodblock *mb; int size; mb = cbMethods(cb); for (size=0; size < (int) cbMethodsCount(cb); size++, mb++) { if (mb->fb.access & ACC_NATIVE) { current_class_name = fn; panic("native methods should not appear"); } } } WriteClass(cb); } }}char *PrintableClassname(char *class_name){ char *p; static char class_copy[257]; strncpy(class_copy, class_name, 256); /* Convert all slashes in the classname to periods */ for (p = class_copy; ((p = strchr(p, '/')) != 0); *p++ = '.'); return class_copy;}void printCurrentClassName(void){ if (current_class_name) { fprintf(stderr, "Error preverifying class %s\n ", PrintableClassname(current_class_name)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -