Example #1
0
ZipFile::ZipFile(FileSymbol *file_symbol) : buffer(NULL)
{
    Zip *zip = file_symbol -> Zipfile();

    assert(zip -> IsValid());

#ifdef UNIX_FILE_SYSTEM
    zipfile = zip -> zipfile;
    int rc = fseek(zipfile, file_symbol -> offset, SEEK_SET);

    assert(rc == 0);

#elif defined(WIN32_FILE_SYSTEM)
    file_buffer = &zip -> zipbuffer[file_symbol -> offset];
#endif

    Skip(8); // u4 magic                     = GetU4();
             // u2 version_needed_to_extract = GetU2();
             // u2 general_purpose_bits      = GetU2();
    u2 compression_method                    = GetU2();
    Skip(16); // u2 time                     = GetU2();
              // u2 date                     = GetU2();
              // u4 crc32                    = GetU4();
              // u4 compressed_size          = GetU4();
              // u4 uncompressed_size        = GetU4();
    u2 filename_length                       = GetU2();
    u2 extra_field_length                    = GetU2();
    Skip(filename_length + extra_field_length);

#ifdef UNIX_FILE_SYSTEM
    this -> buffer = new char[file_symbol -> uncompressed_size];
    if (! uncompress_file[compression_method < 9 ? compression_method : 9](zipfile, this -> buffer, file_symbol -> uncompressed_size))
    {
        delete [] this -> buffer;
        this -> buffer = NULL;
    }
#elif defined(WIN32_FILE_SYSTEM)
    if (compression_method > 0)
    {
        this -> buffer = new char[file_symbol -> uncompressed_size];
        if (! uncompress_file[compression_method < 9 ? compression_method : 9](file_buffer,
                                                                               this -> buffer,
                                                                               file_symbol -> uncompressed_size))
        {
            delete [] this -> buffer;
            this -> buffer = NULL;
            this -> file_buffer = NULL;
        }
    }
#endif
}
Example #2
0
void Operators::OpDmp(Tuple<cp_info *> &constant_pool, Tuple<u1> &code)
{
    int pc = 0;
    while (pc < code.Length())
    {
        int info_kind = INFO_NONE; // assume no extra info
        int info_index = 0;
        int pc_start = pc;
        Opcode opc = (Opcode) GetU1(code, pc);
        const char *name, *desc; // set to name (mnemonic) and description of opcode.
        OpDesc((Opcode) code[pc_start], &name, &desc);
        pc++;

        char argdesc[100];
        argdesc[0] = U_NULL; // assume no argument description needed

        unsigned au1;
        int ai1,
            ai2,
            ai4;
        switch (opc)
        {
            case OP_BIPUSH:
                 ai1 = GetI1(code, pc); pc++;
                 sprintf(argdesc, "%d", ai1);
                 break;
            case OP_SIPUSH:
                 ai2 = GetI2(code, pc); pc +=2;
                 sprintf(argdesc, "%d", ai2);
                 break;
            case OP_LDC:
                 info_index = GetU1(code, pc); pc++;
                 sprintf(argdesc,"%d", info_index);
                 info_kind = INFO_CONST;
                 break;
            case OP_LDC_W:
            case OP_LDC2_W:
                 info_index = GetU2(code, pc);pc +=2;
                 sprintf(argdesc, "%u", info_index);
                 info_kind = INFO_CONST;
                 break;
            case OP_ILOAD:
            case OP_LLOAD:
            case OP_FLOAD:
            case OP_DLOAD:
            case OP_ALOAD:
            case OP_ISTORE:
            case OP_LSTORE:
            case OP_FSTORE:
            case OP_DSTORE:
            case OP_ASTORE:
                 info_index = GetU1(code, pc);pc++;
                 sprintf(argdesc, "%u", info_index);
                 info_kind = INFO_LOCAL;
                 break;
            case OP_ILOAD_0:
            case OP_LLOAD_0:
            case OP_FLOAD_0:
            case OP_DLOAD_0:
            case OP_ALOAD_0:
            case OP_ISTORE_0:
            case OP_LSTORE_0:
            case OP_FSTORE_0:
            case OP_DSTORE_0:
            case OP_ASTORE_0:
                 info_kind = INFO_LOCAL;
                 info_index = 0;
                 break;
            case OP_ILOAD_1:
            case OP_LLOAD_1:
            case OP_FLOAD_1:
            case OP_DLOAD_1:
            case OP_ALOAD_1:
            case OP_ISTORE_1:
            case OP_LSTORE_1:
            case OP_FSTORE_1:
            case OP_DSTORE_1:
            case OP_ASTORE_1:
                 info_kind = INFO_LOCAL;
                 info_index = 1;
                 break;
            case OP_ILOAD_2:
            case OP_LLOAD_2:
            case OP_FLOAD_2:
            case OP_DLOAD_2:
            case OP_ALOAD_2:
            case OP_ISTORE_2:
            case OP_LSTORE_2:
            case OP_DSTORE_2:
            case OP_FSTORE_2:
            case OP_ASTORE_2:
                 info_kind = INFO_LOCAL;
                 info_index = 2;
                 break;
            case OP_ILOAD_3:
            case OP_LLOAD_3:
            case OP_FLOAD_3:
            case OP_DLOAD_3:
            case OP_ALOAD_3:
            case OP_ISTORE_3:
            case OP_LSTORE_3:
            case OP_FSTORE_3:
            case OP_DSTORE_3:
            case OP_ASTORE_3:
                 info_kind= INFO_LOCAL;
                 info_index = 3;
                 break;
            case OP_IINC:
                 info_index = GetU1(code, pc); pc++;
                 au1 = GetU1(code, pc); pc++;
                 ai1 = GetI1(code, pc); pc++;
                 info_kind = INFO_LOCAL;
                 sprintf(argdesc, "%d %d", au1, ai1);
                 break;
            case OP_IFEQ:
            case OP_IFNE:
            case OP_IFLT:
            case OP_IFGE:
            case OP_IFGT:
            case OP_IFLE:
            case OP_IF_ICMPEQ:
            case OP_IF_ICMPNE:
            case OP_IF_ICMPLT:
            case OP_IF_ICMPGE:
            case OP_IF_ICMPGT:
            case OP_IF_ICMPLE:
            case OP_IF_ACMPEQ:
            case OP_IF_ACMPNE:
            case OP_GOTO:
            case OP_JSR:
            case OP_IFNULL:
            case OP_IFNONNULL:
                 ai2 = GetI2(code, pc);
                 sprintf(argdesc, "%d", (int) ( ai2+pc_start)); // compute branch target
                 pc +=2;
                 break;
            case OP_RET:
                 au1 = GetU1(code, pc);
                 pc++;
                 sprintf(argdesc, "%d", (int) au1);
                 break;
            case OP_TABLESWITCH:
                 {
                     int def,
                         low,
                         high,
                         len,
                         val,
                         pc_this;
                     Opcode op_this;
                     unsigned iu1;
                     op_this = OP_TABLESWITCH;
                     // account for padding
                     while (pc % 4)
                     {
                         iu1 = GetU1(code, pc);
                         pc++;
                     }
                     def = GetI4(code, pc); pc += 4;
                     low = GetI4(code, pc); pc +=4;
                     high = GetI4(code, pc); pc += 4;
                     sprintf(argdesc, "default:%d low:%d high:%d",
                             def + pc_start, low, high);
                     OpLine(constant_pool, " ", pc_start, opc, name,
                            argdesc, desc, info_kind, info_index);
                     len =  high - low + 1;
                     while (len)
                     {
                         pc_this = pc;
                         val = GetI4(code, pc);
                         sprintf(argdesc, "match:%d offset:%d",
                                 low++, val + pc_start);
                         OpLine(constant_pool,"*",pc_this, op_this, name,
                                argdesc, desc, INFO_NONE, 0);
                         pc += 4;
                         len--;
                     }
                     info_kind = INFO_DONE;
                 }
                 break;
            case OP_LOOKUPSWITCH:
                 {
                     int def,
                         npairs,
                         len,
                         match,
                         offset;
                     Opcode op_this;

                     // account for padding
                     unsigned iu1;
                     op_this = OP_LOOKUPSWITCH;
                     while (pc % 4)
                     {
                         iu1 = GetU1(code, pc);
                         pc++;
                     }
                     def = GetI4(code, pc); pc += 4;
                     npairs = GetI4(code, pc); pc +=4;
                     sprintf(argdesc, "default:%d npairs:%d",
                             def + pc_start, npairs);
                     OpLine(constant_pool, " ", pc_start, op_this, name,
                            argdesc, desc, info_kind, info_index);
                     len = npairs;
                     while (npairs)
                     {
                         int pcb = pc;
                         match = GetI4(code, pc); pc +=4 ;
                         offset = GetI4(code, pc); pc +=4;
                         sprintf(argdesc, "match:%d offset:%d ",
                                 match, offset + pc_start);
                         OpLine(constant_pool, "*", pcb, op_this, name,
                                argdesc, desc, INFO_NONE, 0);
                         npairs--;
                     }
                     info_kind = INFO_DONE;
                 }
                 break;
            case OP_GETSTATIC:
            case OP_PUTSTATIC:
            case OP_GETFIELD:
            case OP_PUTFIELD:
            case OP_INVOKEVIRTUAL:
            case OP_INVOKESPECIAL:
            case OP_INVOKESTATIC:
            case OP_NEW:
            case OP_ANEWARRAY:
            case OP_CHECKCAST:
            case OP_INSTANCEOF:
                 info_index = GetU2(code, pc); pc += 2;
                 sprintf(argdesc, "%d", info_index);
                 info_kind = INFO_CONST;
                 break;
            case OP_INVOKEINTERFACE:
                 {
                     int nargs;
                     info_index = GetU2(code, pc); pc += 2;
                     au1 = GetU1(code, pc); pc++;
                     nargs = au1;
                     au1 = GetU1(code, pc); pc++;

                     assert((! au1) && "...zero byte required in this position");

                     sprintf(argdesc, "%d %d", nargs,info_index);
                     info_kind=INFO_CONST;
                 }
                 break;
            case OP_NEWARRAY:
                 au1 = GetU1(code, pc); pc++;
                 switch (au1)
                 {
                     case 4: sprintf(argdesc, "%s", "BOOLEAN");break;
                     case 5: sprintf(argdesc, "%s", "CHAR");break;
                     case 6: sprintf(argdesc, "%s", "FLOAT");break;
                     case 7: sprintf(argdesc, "%s", "DOUBLE");break;
                     case 8: sprintf(argdesc, "%s", "BYTE");break;
                     case 9: sprintf(argdesc, "%s", "SHORT");break;
                     case 10: sprintf(argdesc, "%s", "INT");break;
                     case 11: sprintf(argdesc, "%s", "LONG");break;
                     default:
                         sprintf(argdesc, "%s", "<UNKNOWN>");break;
                 }
                 break;
            case OP_WIDE:
                 assert(false && "dmp for op_wide not supported yet");
                 break;
            case OP_MULTIANEWARRAY:
                 info_index = GetU2(code, pc); pc += 2;
                 au1 = GetU1(code, pc); pc++;
                 info_kind = INFO_CONST;
                 // au1 gives dimensions
                 sprintf(argdesc, "%u", au1);
                 break;
            case OP_GOTO_W:
            case OP_JSR_W:
                 ai4 = GetI4(code, pc); pc += 4;
                 // ai4 gives offset (wide) of branch target
                 sprintf(argdesc, "%d", pc_start + ai4);
                 break;
            default:
                 break;
        }

        // output first part of description
        if (info_kind != INFO_DONE)
            OpLine(constant_pool, " ", pc_start, opc, name,
                   argdesc, desc, info_kind, info_index);
    }
}
Example #3
0
inline void Zip::ProcessDirectoryEntry()
{
    Skip(8); // u2 version_made_by           = GetU2();
             // u2 version_needed_to_extract = GetU2();
             // u2 general_purpose_bits      = GetU2();
             // u2 compression_method        = GetU2();
    u2 last_mod_file_time                    = GetU2();
    u2 last_mod_file_date                    = GetU2();
    Skip(4); // u4 crc32                     = GetU4();
    Skip(4); // u4 compressed_size           = GetU4();
    u4 uncompressed_size                     = GetU4();
    u2 file_name_length                      = GetU2();
    u2 extra_field_length                    = GetU2();
    u2 file_comment_length                   = GetU2();
    Skip(8); // u2 disk_number_start         = GetU2();
             // u2 internal_file_attributes  = GetU2();
             // u4 external_file_attributes  = GetU4();
    u4 relative_offset_of_local_header       = GetU4();

    u4 date_time = ((u4) last_mod_file_date) << 16 | last_mod_file_time;
    char *name = buffer_ptr;

    Skip(file_name_length + extra_field_length + file_comment_length);

    //
    // Note that we need to process all subdirectory entries
    // that appear in the zip file, and not just the ones that
    // contain java and class files. Recall that in java the
    // dot notation is used in specifying a package. Therefore,
    // in processing a qualified-name that represents a package,
    // we need to recognize each name as a subpackage. E.g.,
    // when processing "java.lang", we need to recognize "java"
    // as a package before looking for "lang"...

    // start at the "." directory.
    DirectorySymbol *directory_symbol = root_directory;
    // -1 to remove last '/'
    if (name[file_name_length - 1] == U_SLASH)
        ProcessSubdirectoryEntries(directory_symbol,
                                   name,
                                   file_name_length - 1);
    else
    {
        bool java_file = (file_name_length >= FileSymbol::java_suffix_length &&
                          FileSymbol::IsJavaSuffix(&name[file_name_length - FileSymbol::java_suffix_length])),
             class_file = (file_name_length >= FileSymbol::class_suffix_length &&
                           FileSymbol::IsClassSuffix(&name[file_name_length - FileSymbol::class_suffix_length]));

        if (java_file || class_file)
        {
            int name_length = file_name_length - (java_file ? FileSymbol::java_suffix_length : FileSymbol::class_suffix_length);
            int i;
            for (i = name_length - 1; i >= 0 && name[i] != U_SLASH; i--)
                ;
            if (i > 0) // directory specified?
                directory_symbol = ProcessSubdirectoryEntries(directory_symbol,
                                                              name, i);
            NameSymbol *name_symbol = ProcessFilename(&name[i + 1],
                                                      name_length - (i + 1));

            //
            // Search for a file of that name in the directory.
            // If one is not found, then insert ... Otherwise,
            // either a class file of that name was previously
            // processed and now we found a java file with the
            // same name or vice-versa... In that case keep
            // (or replace with) the file with the most recent
            // date stamp.
            //
            FileSymbol *file_symbol = directory_symbol ->
                FindFileSymbol(name_symbol);
            if (! file_symbol)
            {
                file_symbol = directory_symbol -> InsertFileSymbol(name_symbol);

                file_symbol -> directory_symbol = directory_symbol;
                if (java_file)
                     file_symbol -> SetJava();
                else file_symbol -> SetClassOnly();

                file_symbol -> uncompressed_size = uncompressed_size;
                file_symbol -> offset = relative_offset_of_local_header;
                file_symbol -> date_time = date_time;
            }
            else if (file_symbol -> date_time < date_time)
            {
                if (java_file)
                     file_symbol -> SetJava();
                else file_symbol -> SetClass();

                file_symbol -> uncompressed_size = uncompressed_size;
                file_symbol -> offset = relative_offset_of_local_header;
                file_symbol -> date_time = date_time;
            }
        }
    }
}