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

📄 unzip.c

📁 zip在嵌入式系统上的实现
💻 C
字号:
#if defined(_MSC_VER)
#define MAKE_INCLUDE_COMPRESS_TYPE_ZIP
#endif

#ifdef MAKE_INCLUDE_COMPRESS_TYPE_ZIP

#include "stddef.h"
#include "stdio.h"
#include "stdlib.h"
#include "..\..\..\all\Typedef.h"
#include "..\include\zlib.h"

//#define VC
#undef VC

extern _U32 Drv_Print(char *pStr, ...);


typedef  struct DLH
{
    unsigned long  dwCodeLength;
    unsigned short wCheckSum;
    unsigned short reserved;
} DownLoadHeader;

/*+++++++++++++++++++++++++以下是计算CRC校验字的程序++++++++++++++++++++++*/

#define CRC16_POLY      0x1021
 /* Function init_crc_tab() will initialize it */
unsigned short  crc_tab[256];

/*========================================================================*/
/*    FUNCTION    : Calculate the CRC of a data block.                    */
/*    CALLS       : None                                                  */
/*    CALLED BY   : Where you need it.                                    */
/*    INPUT       : *buffer -- the data block.                            */
/*                  count ---- length of the data block in bytes.         */
/*    OUTPUT      : *crc ----- CRC.                                       */
/*    RETURN      : None.                                                 */
/*========================================================================*/
void Zip_make_crc( unsigned char *buffer, unsigned long count, unsigned short *crc )
{
    unsigned short temp;

    if (0 == count)
        return;

    temp = 0;

    while ( count-- != 0 )
    {
        temp = (unsigned short)(( temp<<8 ) ^ crc_tab[ ( temp>>8 ) ^ *buffer++ ]);
    }

    *crc = temp;

    return ;
}

/*========================================================================*/
/*    FUNCTION    : Initialize the data-table                             */
/*    CALLS       : None                                                  */
/*    CALLED BY   : The CRC initialization function.                      */
/*    INPUT       : None                                                  */
/*    OUTPUT      : None                                                  */
/*    RETURN      : None                                                  */
/*========================================================================*/
void Zip_init_crc_tab( void )
{
    unsigned short     i, j;
    unsigned short     value;


    for ( i=0; i<256; i++ )
    {
        value = (unsigned short)(i << 8);
        for ( j=0; j<8; j++ )
        {
            if ( (value&0x8000) != 0 )
                value = (unsigned short)((value<<1) ^ CRC16_POLY);
            else
                value <<= 1;
        }

        crc_tab[i] = value;
    }

}
/*========================================================================*/
/*                                                                        */
/*    Function Name    : CalCRC                                           */
/*    Function Func.   : Calculate the CRC                                */
/*    Input Parameter  :                                                  */
/*                       1. unsigned char * ptr : pointer to the data     */
/*                       2. int : length of data                          */
/*    Output Parameter : none                                             */
/*    Return           : unsigned short : CRC                             */
/*    Call Functions   : none                                             */
/*    Called By        :                                                  */
/*                       1. CRC (  )                                      */
/*                                                                        */
/*========================================================================*/
unsigned short Zip_CalCRC ( unsigned char *ptr, unsigned long count )
 {
    unsigned short crc;

    Zip_init_crc_tab();
    Zip_make_crc( ptr, count, &crc );
    return  ( crc & 0xFFFF ) ;
}

unsigned long UNZip(unsigned long DstAddr, unsigned long DstLen, unsigned long SrcAddr, unsigned long SrcLen)
{
    char  *ProgramBase,*ZipProBase,*Tmp;
	unsigned long FileLength = 0,ZipFileLength = 0;
    int retValue;
    DownLoadHeader *pstHead;
    unsigned short wCheckSum;


    {
        unsigned short rCheckSum = 0;
    #ifdef MAKE_BOOTROM
    	Drv_Print("\r\nZip file uncompressed!(addr:0x%1x)",DstAddr);
	#endif
    	FileLength = 0;
    	ZipFileLength = 0;
    	wCheckSum = 0;

        ProgramBase = (char *)DstAddr;

        ZipProBase  = (char *)SrcAddr;

        Tmp = ZipProBase ;
        ZipFileLength = 0;

        pstHead = (DownLoadHeader *)ZipProBase;
        ZipFileLength = ntohl(pstHead->dwCodeLength);


        wCheckSum = ntohs(pstHead->wCheckSum);

    #ifdef MAKE_BOOTROM
    	Drv_Print("\r\nZip File length:0x%x,checksum is :0x%x",ZipFileLength,wCheckSum);
    #endif
        rCheckSum = (unsigned short)Zip_CalCRC ((unsigned char *)(ZipProBase+sizeof(DownLoadHeader))
                                     ,(unsigned long)ZipFileLength);

    #ifdef MAKE_BOOTROM
        Drv_Print("\r\ncal crc is :0x%x",rCheckSum);
    #endif
        if(wCheckSum != rCheckSum)
        {
            Drv_Print("\r\nthe wCheckSum is 0x%x,the rCheckSum is 0x%x\r\n",wCheckSum,rCheckSum);

            return 0;
        }


        /* 解压缩 */
        FileLength = DstLen;

        retValue = uncompress ((char *)ProgramBase, &FileLength, (char *)(ZipProBase+sizeof(DownLoadHeader)), ZipFileLength);


    }
  	return FileLength;


}


#endif /* MAKE_INCLUDE_COMPRESS_TYPE_ZIP */

⌨️ 快捷键说明

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