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

📄 compress.c

📁 一个基于Zlib算法的
💻 C
📖 第 1 页 / 共 2 页
字号:

#include <vxWorks.h>

#define _REScompressh
#include <sdk/exlib/compress/compress.h>

#define ZLIB_INTERNAL
#include <sdk/exlib/compress/zlib.h>

/*##################################################################################
	define globle variable.
*/


/*##################################################################################
	declare funtions.
*/


/*##################################################################################
	define functions.
*/

/**    Compresses the u_source buffer into the destination buffer.
*
*    v_dest      -- the address pointer of the destination buffer
*    w_destLen   -- 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.
*    u_source    -- the address pointer of the u_source buffer
*    u_sourceLen -- the byte length of the u_source 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.
*    level     -- Z_DEFAULT_COMPRESSION, Z_BEST_COMPRESSION...
*/
int compress2(unsigned char *v_dest,  unsigned long *w_destLen, \
              unsigned char *u_source,  unsigned long u_sourceLen, \
              int level )
{
    z_stream stream;
    int err;

    stream.next_in = (Bytef*)u_source;
    stream.avail_in = (uInt)u_sourceLen;
#ifdef MAXSEG_64K
    /* Check for u_source > 64K on 16-bit machine: */
    if ((uLong)stream.avail_in != u_sourceLen) return Z_BUF_ERROR;
#endif
    stream.next_out = v_dest;
    stream.avail_out = (uInt)*w_destLen;
    if ((uLong)stream.avail_out != *w_destLen) return Z_BUF_ERROR;

    stream.zalloc = (alloc_func)0;
    stream.zfree = (free_func)0;
    stream.opaque = (voidpf)0;

    err = deflateInit(&stream, level);
    if (err != Z_OK) return err;

    err = deflate(&stream, Z_FINISH);
    if (err != Z_STREAM_END) {
        deflateEnd(&stream);
        return err == Z_OK ? Z_BUF_ERROR : err;
    }
    *w_destLen = stream.total_out;

    err = deflateEnd(&stream);
    return err;
}

/**    Compresses the u_source buffer into the destination buffer.
*
*    u_source    -- the address pointer of the u_source buffer
*    u_sourceLen -- the byte length of the u_source buffer,
*    v_dest      -- the address pointer of the destination buffer
*    w_destLen   -- 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 compress(unsigned char *u_source,  unsigned long u_sourceLen, \
             unsigned char *v_dest, unsigned long *w_destLen )
{
    /*
    return compress2(v_dest, w_destLen, u_source, u_sourceLen, Z_DEFAULT_COMPRESSION);
    */
    return compress2(v_dest, w_destLen, u_source, u_sourceLen, Z_BEST_COMPRESSION);
}


/**    Uncompresses the u_source buffer into the destination buffer.
*
*    u_source    -- the address pointer of the u_source buffer
*    u_sourceLen -- the byte length of the u_source buffer.
*    v_dest      -- the address pointer of the destination buffer
*    w_destLen   -- 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    -- 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_DATA_ERROR, if the input data was corrupted.
*/
int uncompress(unsigned char *u_source,  unsigned long u_sourceLen,  \
               unsigned char *v_dest, unsigned long *w_destLen)
{
    z_stream stream;
    int err;

    stream.next_in = (Bytef*)u_source;
    stream.avail_in = (uInt)u_sourceLen;
    /* Check for u_source > 64K on 16-bit machine: */
    if ((uLong)stream.avail_in != u_sourceLen) return Z_BUF_ERROR;

    stream.next_out = v_dest;
    stream.avail_out = (uInt)*w_destLen;
    if ((uLong)stream.avail_out != *w_destLen) return Z_BUF_ERROR;

    stream.zalloc = (alloc_func)0;
    stream.zfree = (free_func)0;

    err = inflateInit(&stream);
    if (err != Z_OK) return err;

    err = inflate(&stream, Z_FINISH);
    if (err != Z_STREAM_END) {
        inflateEnd(&stream);
        if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
            return Z_DATA_ERROR;
        return err;
    }
    *w_destLen = stream.total_out;

    err = inflateEnd(&stream);
    return err;
}

/* ===========================================================================
     If the default memLevel or windowBits for deflateInit() is changed, then
   this function needs to be updated.
 */
uLong ZEXPORT compressBound(uLong u_sourceLen)
{
    return u_sourceLen + (u_sourceLen >> 12) + (u_sourceLen >> 14) + 11;
}


/**    Compresses 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 compress8k(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 > 0x2000) ? 0x2000 : srcBufLen;
        cmpSize = 0x2100;

        *((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;

        taskDelay(5);  /*delay 5 ticks about 75ms for 8K bytes*/

    }while(srcBufLen > 0);

    *w_pDstBufSize = (unsigned long)(dstBufLen);

    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    -- 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_DATA_ERROR, if the input data,or the block flag was corrupted
*/
int uncompress8k1(unsigned char *u_pSrcBuf, unsigned long u_srcBufSize, \
                  unsigned char *v_pDstBuf, unsigned long *w_pDstBufSize)
{
    int temp;
    unsigned long uncmpSize;
    unsigned long unCmpSrcSize;

    unsigned char *pSrcBuf;
    unsigned char *pDstBuf;

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

    unCmpSrcSize = u_srcBufSize;

    pSrcBuf = u_pSrcBuf;
    pDstBuf = v_pDstBuf;

    srcLen = 0;
    unCmpLen = 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);
            switch(temp)
            {
                case Z_OK:
                    break;
                case Z_MEM_ERROR:
                    return Z_MEM_ERROR;
                case Z_BUF_ERROR:
                    return Z_BUF_ERROR;
                case Z_DATA_ERROR:
                    return Z_DATA_ERROR;
            }

            pDstBuf += uncmpSize;

⌨️ 快捷键说明

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