⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kaeziparchiveimplement.cpp

📁 zip解压源码.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
 * ******************************************************************** */
inline UINT KAEZipArchive::get_code(int bits)
{
    UINT    a;                                  // accumulator

    a = lookat_code(bits);                      // prepare return value
    
	if (m_nZipFileCracked)
        return -1;

    bhold >>= bits;                             // ..shift out requested bits
    bsize -= bits;                              // ..decrement remaining bit count

    return(a);                                  // ..and return value
}


/* ******************************************************************** *
 *
 *  get_byte() -- get next byte from input file
 *
 * ******************************************************************** */
inline UINT KAEZipArchive::get_byte()
{
    if (rsize > 0)                              // q. anything left?
    {
        rsize--;                                // a. yes .. decrement count

        //seawind
        int a = fgetc(ifile);
        
		if(a == EOF)
        {
            m_nZipFileCracked = 1;
            return -1;                              // else .. return error 
        }
        return a;                                   // ..and return w/character
    }
    else
        return(-1);                                 // else .. return error
}

/* ******************************************************************** *
 *
 *  store_char() -- store character in output file
 *
 * ******************************************************************** */
inline void KAEZipArchive::store_char(char c)
{
    //static long far *timer = (long far *)MK_FP(0x40, 0x6c); // BIOS timer tick counter down in low memory
    crc_32(c, &crc);                            // determine current crc 32

    *sbp = c;                                   // a. yes .. put char in buffer

    if (++sbp >= sbe)                           // q. past end of buffer?
    {                                           // a. yes .. write buffer
        //if (fwrite(sb, 1, sb_size, ofile)       // q. write buffer ok?
        //    != sb_size)
        //  quit_with(write_error);             // a. no .. quit w/error message

        fwrite(sb, 1, sb_size, ofile);

        sbp = sb;                               // ..then back to start again
    }

    //if (last_kpal < *timer)                     // q. time to update user?
    //{
    //  last_kpal = *timer + 9;                 // a. yes .. set new timeout
    //  printf(kpal_msg, spin_char[spin++ & 3]);// ..then give user a little
    //}
}



/* ******************************************************************** *
 *
 *  scan_zip() -- scan a ZIP file's directory
 *
 * ******************************************************************** */
void KAEZipArchive::scan_zip()
{
    char   *b;                                  // work buffer pointer
    LF     *lfp;                                // local file header pointer

    b = (char *) malloc_chk(1024);              // allocate some memory
    
	if (!b)
    {
        m_nZipFileCracked = 1;
        return;
    }

    lfp = (LF *) b;                             // ..and set up pointer

    //proc(0, lfp);                             // make init call
    extract_zip(0, lfp);                        // make init call

    for (; !feof(ifile); )                      // loop till end of file
    {
        if (fread(b, 4, 1, ifile) != 1)         // q. read a sig ok?
        {
            m_nZipFileCracked = 1;              // seawind added,this zip file is cracked.
            goto Exit0;                         // a. no .. exit loop
        }

        //if (*(int *) b != 0x4b50)             // q. 'PK' zip signature?
        //  continue;                           // a. no .. try again
        
        //seawind modified
        if((b[0] != 'P') || (b[1] != 'K'))
            continue;

        //seawind added
        UINT16 sign = (UINT16)(b[3]);
        sign <<= 8;
        sign |= (BYTE)(b[2]);

        //sign = *(int *) &b[2];

        if (sign == LF_SIG)                     // q. local file header rec?
        {
            // a. yes .. get record
            if(!read_lf(lfp))                   //seawind added
            {
                m_nZipFileCracked = 1;
                goto Exit0;
            }
            
            //proc(1, lfp);                     // call processing routine
            extract_zip(1, lfp);                // call processing routine

            //seawind added
            if(m_nActionCancel || m_nZipFileCracked)
                goto Exit0;
 
        }
        else 
            if (sign == FH_SIG)                 // q. file header record?
            {
                if(!read_fh((FH *) b))          // a. yes .. get record
                {
                    m_nZipFileCracked = 1;
                    goto Exit0;
                }
            }
            else 
                if (sign == ED_SIG)             // q. end of central dir rec?
                {
                    if(!read_ed((ED *) b))      // a. yes .. get record
                    {
                        m_nZipFileCracked = 1;
                        goto Exit0;
                    }
                }

    }//for (; ! feof(ifile);)

    //proc(2, lfp);                             // competion call to proc rtn
    extract_zip(2, lfp);                        // competion call to proc rtn

Exit0:

    free(b);                                    // finally, release memory

}

/* ******************************************************************** *
 *
 *  read_lf() -- read in the remains of a local file header record
 *
 * ******************************************************************** */
int KAEZipArchive::read_lf(LF *lfp)
{
    char *p = wf.path;                          // work string pointer

    // read in remains of record
    if(fread(&lfp->lf_extract, sizeof(struct tagLF) - 4, 1, ifile) != 1)
        return 0;

    // ..and filename
    if (lfp->lf_fn_len > MAX_PATH)
        return 0;

    if(fread(p, lfp->lf_fn_len, 1, ifile) != 1)
        return 0;

    p[lfp->lf_fn_len] = 0;                      // terminate filename string

    for (; *p; p++)                             // loop thru filename string
        if (*p == '/')                          // q. unix style dir separator?
            *p = '\\';                          // a. yes .. fix for MSDOS

    SPLIT(wf);                                  // split filename into pieces

    if (lfp->lf_ef_len)                         // q. any extra field data?
        if(fseek(ifile, lfp->lf_ef_len, SEEK_CUR) != 0)
            return 0;                           // a. yes .. seek past data

    if (
        (wf.path[0] == '\0') ||
        ((unsigned)StrLen(wf.path) > (unsigned)lfp->lf_fn_len)
    )
        return 0;

    //seawind added
    StrCpy(m_szFullNameInArchive, wf.path);

    m_uCompressedSize = lfp->lf_csize;
    m_uUnCompressedSize = lfp->lf_size;

    return 1;
}

/* ******************************************************************** *
 *
 *  read_fh() -- read in the remains of a file header record
 *
 * ******************************************************************** */
int KAEZipArchive::read_fh(FH *fhp)
{
    // read in remains of record
    if(fread(&fhp->fh_made, sizeof(struct tagFH) - 4, 1, ifile) != 1)
        return 0;

    // seek past filename
    if(fseek(ifile, fhp->fh_fn_len, SEEK_CUR) != 0)
        return 0;

    if (fhp->fh_ef_len)                         // q. any extra field data?
        if(fseek(ifile, fhp->fh_ef_len, SEEK_CUR) != 0) // a. yes .. seek past data
            return 0;

    if (fhp->fh_fc_len)                         // q. any file comments?
        if(fseek(ifile, fhp->fh_fc_len, SEEK_CUR) != 0) // a. yes .. seek past data
            return 0;

    return 1;
}

/* ******************************************************************** *
 *
 *  read_ed() -- read in the remains of the end of central dir record
 *
 * ******************************************************************** */
int KAEZipArchive::read_ed(ED *edp)
{
    // read in remains of record
    if(fread(&edp->ed_disk, sizeof(struct tagED) - 4, 1, ifile) != 1)
        return 0;

    if (edp->ed_zc_len)                         // q. any zip file comments?
        if(fseek(ifile, edp->ed_zc_len, SEEK_CUR) != 0) // a. yes .. seek past data
            return 0;

    return 1;
}

/* ******************************************************************** *
 *
 *  initialization() -- init interrupts
 *
 * ******************************************************************** */
int  KAEZipArchive::initialization(const char * szZipPathName)
{
    total_files = 0;
    total_size = 0;
    total_csize = 0;

    crc = 0;                               // running crc 32 value
    bhold = 0;                             // bits hold area
    
    rsize = 0;                             // remaining size to process
    last_kpal = 0;                         // keep alive message time
    total_size = 0;                        // uncompressed sum
    total_csize = 0;                       // compressed sum
    
    //int     rc /*= 1*/,                         // errorlevel return code
    pos_count = 0;                         // positional parms count
    sf_count = 0;                          // select file parms count
    e_count = 0;                           // end of data counter/flag
    total_files = 0;                       // files processed
    
    // because init in KAEZipArchive::KAEZipArchive
    //sb_size = 0;                         // sliding buffer size
    
    bsize = 0;                             // bits left to process

    m_nActionCancel = 0;
    m_nZipFileCracked = 0;

    StrCpy(zfn, szZipPathName);

    ifile = fopen(zfn, "rb");

    if(ifile == NULL)//seawind added
    {
        return 0;
    }

    StrCpy(output_path,"");

    /*
    StrCpy(output_path, m_szTempPath);
    
    if(LAST(output_path) == '\\')
        LAST(output_path) = '\0';
    */

    return 1;

    /*
    int i, j;                               // loop control

    init_path = getcwd(NULL, MAX_PATH);         // get current directory
    ctrlbrk(control_break);                     // set up ctrl break handler
    _dos_setvect(0x24, critical_routine);       // ..and DOS critical handler

    pos_count = parse_parms(ac, av,             // parse switches and
            COUNT(parm_table), parm_table,      // ..positional parameters
            &pos_parms);                        // ..and based on parm count

    if (pos_count < 1)                          // q. zip file name given?
        quit_with(help);                        // a. no .. quit w/help message

    if (pos_count > 1)                          // q. user selecting file?
    {
        if (NOT sw_view &&                      // q. extracting files
                LAST(pos_parms[1]) == '\\') // ..and 2nd parm a dir name?
        {
            StrCpy(output_path, pos_parms[1]);  // a. yes .. save this parm
            touppers(output_path);              // ..in uppercase form
            LAST(output_path) = 0;              // ..but nop the last backslash
            j = 2;                              // ..and start with the next
        }
        else
            j = 1;                              // else .. start with the 2nd

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -