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

📄 compress.c

📁 一个基于Zlib算法的
💻 C
📖 第 1 页 / 共 2 页
字号:
            pSrcBuf += 8;
            pSrcBuf += srcLen;

            unCmpLen += uncmpSize;

            unCmpSrcSize -= 8;
            unCmpSrcSize -= srcLen;
        }
        else
        {
            return Z_DATA_ERROR;
        }
    }while(unCmpSrcSize > 0);

    *w_pDstBufSize = unCmpLen;

    return Z_OK;
}

/**    Uncompresses the u_pSrcBuf buffer into the destination buffer.
*
*    u_pSrcBuf      -- the address pointer of the u_source buffer
*    u_srcBufSize   -- the byte length of the u_source buffer.
*    v_pDstBuf      -- the address pointer of the destination buffer
*    w_pDstBufSize  -- Upon entry, the total size of the destination buffer,
                   which must be large enough to hold the entire uncompressed data.
                   (The size of the uncompressed data must have been saved previously
                   by the compressor and transmitted to the decompressor
                   by some mechanism outside the scope of this compression library.)
                   Upon exit, w_destLen is the actual size of the compressed buffer.

*    return    -- the already uncompressed source data length
*/
unsigned long uncompress8k(unsigned char *u_pSrcBuf, unsigned long u_srcBufSize, \
                           unsigned char *v_pDstBuf, unsigned long *w_pDstBufSize)
{
    int temp;
    int timesCnt;
    unsigned long uncmpSize;
    unsigned long unCmpSrcSize;

    unsigned char *pSrcBuf;
    unsigned char *pDstBuf;

    unsigned long srcLen;
    unsigned long unCmpLen;
    unsigned long unCmpSrcLen;
    unsigned long flag;

    unCmpSrcSize = u_srcBufSize;

    pSrcBuf = u_pSrcBuf;
    pDstBuf = v_pDstBuf;

    timesCnt = 0;
    srcLen = 0;
    unCmpLen = 0;
    unCmpSrcLen = 0;
    flag = 0;

    do
    {   flag = (unsigned long)(*((unsigned long *)pSrcBuf));

        if ((unsigned long)CMP_BLOCK_FLAG == flag)
        {
            srcLen = (unsigned long)(*((unsigned long *)(pSrcBuf + 4)));

            uncmpSize = 0x2100;  /* 8K bytes every running */

            temp = uncompress((UINT8 *)(pSrcBuf + 8), srcLen, \
                              (UINT8 *)pDstBuf, &uncmpSize);
            if (Z_OK != temp)
            {
                *w_pDstBufSize = unCmpLen;
                return unCmpSrcLen;
            }

            pDstBuf += uncmpSize;

            pSrcBuf += 8;
            pSrcBuf += srcLen;

            unCmpLen += uncmpSize;
            unCmpSrcLen += 8;
            unCmpSrcLen += srcLen;

            unCmpSrcSize -= 8;
            unCmpSrcSize -= srcLen;

            timesCnt++;
            if (timesCnt > 250)
            {
                timesCnt = 0;
                taskDelay(5);   /*the 250 times about 2M souce data ~ 8k/times ~ 4s watch dog reset*/
            }
        }
        else
        {
            *w_pDstBufSize = unCmpLen;
            return unCmpSrcLen;
        }
    }while((unCmpSrcSize > 0) && (unCmpSrcSize <= u_srcBufSize));

    *w_pDstBufSize = unCmpLen;
    return unCmpSrcLen;
}



/**    runCompress the "u_pSrcBuf" buffer into the destination buffer.
*      deal with 8k bytes every compress,and taskDelay(5),that's 5 ticks
*      about 75ms
*
*    u_pSrcBuf      -- the address pointer of the u_source buffer
*    u_srcBufSize   -- the byte length of the u_source buffer,
*    v_pDstBuf      -- the address pointer of the destination buffer
*    w_pDstBufSize  -- Upon entry,the total size of the destination buffer,
                  which must be at least 0.1% larger than u_sourceLen plus 12 bytes,
                  Upon exit, w_destLen is the actual size of the compressed buffer.

*    return    -- Z_OK,  if success,
                  Z_MEM_ERROR,  if there was not enough memory,
                  Z_BUF_ERROR,  if there was not enough room in the output buffer,
                  Z_STREAM_ERROR,  if the level parameter is invalid.
     Note: it's the best compression
*/
int runCompress(unsigned char *u_pSrcBuf, unsigned long u_srcBufSize, \
                      unsigned char *v_pDstBuf, unsigned long *w_pDstBufSize)
{
    int temp;
    unsigned long cmpSize;

    unsigned char *pSrcBuf;
    unsigned char *pDstBuf;

    unsigned long dstBufLen;
    unsigned long srcBufLen;
    unsigned long srcBlockSize;

    pSrcBuf = u_pSrcBuf;
    pDstBuf = v_pDstBuf;
    dstBufLen = 0;
    srcBufLen = u_srcBufSize;

    do {
        srcBlockSize = (srcBufLen > CMP_BLOCK_SIZE) ? CMP_BLOCK_SIZE : srcBufLen;
        cmpSize = CMP_BLOCK_SIZE + 100;

        *((unsigned long *)pDstBuf) = (unsigned long)CMP_BLOCK_FLAG;

        temp = compress((UINT8 *)(pSrcBuf), srcBlockSize, \
                        (UINT8 *)(pDstBuf+8), &cmpSize );

        *((unsigned long *)(pDstBuf + 4)) =  cmpSize;

        switch(temp)
        {
            case Z_OK:
                break;
            case Z_MEM_ERROR:
                return Z_MEM_ERROR;
            case Z_BUF_ERROR:
                return Z_BUF_ERROR;
            case Z_STREAM_ERROR:
                return Z_STREAM_ERROR;
        }

        pDstBuf += 8;
        pDstBuf += cmpSize;

        dstBufLen += 8;
        dstBufLen += cmpSize;
        pSrcBuf += srcBlockSize;

        srcBufLen -= srcBlockSize;
        
        if(CMP_BLOCK_SIZE <= srcBlockSize)
        {
          taskDelay(CMP_DELAY_TICKS);  /*delay 5 ticks about 75ms for 8K bytes*/
        }

    }while(srcBufLen > 0);

    *w_pDstBufSize = (unsigned long)(dstBufLen);

    return Z_OK;
}


/**  runUncompress the u_pSrcBuf buffer into the destination buffer.
*
*    u_pSrcBuf      -- the address pointer of the u_source buffer
*    u_srcBufSize   -- the byte length of the u_source buffer.
*    v_pDstBuf      -- the address pointer of the destination buffer
*    w_pDstBufSize  -- Upon entry, the total size of the destination buffer,
                   which must be large enough to hold the entire uncompressed data.
                   (The size of the uncompressed data must have been saved previously
                   by the compressor and transmitted to the decompressor
                   by some mechanism outside the scope of this compression library.)
                   Upon exit, w_destLen is the actual size of the compressed buffer.

*    return    -- the uncompressed data length, if success,
                  Z_MEM_ERROR, if there was not enough memory,
                  Z_BUF_ERROR, if there was not enough room in the output buffer,
                  Z_DATA_ERROR, if the input data,or the block flag was corrupted
                  Z_FLAG_ERROR, the data block flag is error,maybe the data corrupt

*/
long runUncompress(unsigned char *u_pSrcBuf, unsigned long u_srcBufSize, \
                   unsigned char *v_pDstBuf, unsigned long *w_pDstBufSize)
{
    int temp;
    int timesCnt;
    unsigned long uncmpSize;
    unsigned long unCmpSrcSize;

    unsigned char *pSrcBuf;
    unsigned char *pDstBuf;

    unsigned long srcLen;
    unsigned long unCmpLen;
    unsigned long unCmpSrcLen;
    unsigned long flag;

    unCmpSrcSize = u_srcBufSize;

    pSrcBuf = u_pSrcBuf;
    pDstBuf = v_pDstBuf;

    timesCnt = 0;
    srcLen = 0;
    unCmpLen = 0;
    unCmpSrcLen = 0;
    flag = 0;

    do
    {   flag = (unsigned long)(*((unsigned long *)pSrcBuf));

        if ((unsigned long)CMP_BLOCK_FLAG == flag)
        {
            srcLen = (unsigned long)(*((unsigned long *)(pSrcBuf + 4)));

            uncmpSize = CMP_BLOCK_SIZE + 100;  /* 8K bytes every running */

            temp = uncompress((UINT8 *)(pSrcBuf + 8), srcLen, \
                              (UINT8 *)pDstBuf, &uncmpSize);
            /*if (Z_OK != temp)
            {
                *w_pDstBufSize = unCmpLen;
                return unCmpSrcLen;
            }*/
            switch(temp)
            {
                case Z_OK:
                    break;
                case Z_MEM_ERROR:
                    *w_pDstBufSize = 0;
                    return Z_MEM_ERROR;
                case Z_BUF_ERROR:
                    *w_pDstBufSize = 0;
                    return Z_BUF_ERROR;
                case Z_DATA_ERROR:
                    *w_pDstBufSize = 0;
                    return Z_DATA_ERROR;
            }

            pDstBuf += uncmpSize;

            pSrcBuf += 8;
            pSrcBuf += srcLen;

            unCmpLen += uncmpSize;
            unCmpSrcLen += 8;
            unCmpSrcLen += srcLen;

            unCmpSrcSize -= 8;
            unCmpSrcSize -= srcLen;

            timesCnt++;
            if (timesCnt > UNCMP_BLOCK_TIMES)
            {
                timesCnt = 0;
                taskDelay(UNCMP_DELAY_TICKS);   /*the 250 times about 2M souce data ~ 8k/times ~ 4s watch dog reset*/
            }
        }
        else
        {
            if(CMP_BLOCK_SIZE < unCmpLen)
            {
                *w_pDstBufSize = 0;
                return Z_FLAG_ERROR;
            }
            else
                break;
        }
    }while((unCmpSrcSize > 0) && (unCmpSrcSize <= u_srcBufSize));

    *w_pDstBufSize = unCmpLen;
    return (long)unCmpSrcLen;
}

⌨️ 快捷键说明

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