C ++ LZSS压缩

时间:2013-05-10 00:24:48

标签: c++ algorithm

有人可以找到此解压缩代码的压缩算法。算法不是我的强项而且我的朋友制作代码没有放入注释并且在离开之前以某种方式丢失了压缩代码。 HAA。

static inline unsigned char getbit_le(unsigned char byte, unsigned int pos)
{
    return !!(byte & (1 << pos));
}

static inline unsigned char getbyte_le(unsigned char byte)
{
    return byte;
}

static int lzss_decompress(BYTE *uncompr, DWORD uncomprLen,
                           BYTE *compr, DWORD comprLen)
{
    unsigned int act_uncomprlen = 0;
    unsigned int curbyte = 0;
    unsigned int nCurWindowByte = 0xfee;
    unsigned char window[4096];
    unsigned int win_size = 4096;

    memset(window, 0, win_size);    
    while (1) {
        unsigned char bitmap;

        if (curbyte >= comprLen)
            break;

        bitmap = getbyte_le(compr[curbyte++]);
        for (unsigned int curbit_bitmap = 0; curbit_bitmap < 8; curbit_bitmap++) {
            if (getbit_le(bitmap, curbit_bitmap)) {
                unsigned char data;

                if (curbyte >= comprLen)
                    goto out;

                if (act_uncomprlen >= uncomprLen)
                    goto out;

                data = ~getbyte_le(compr[curbyte++]);
                uncompr[act_uncomprlen++] = data;       
                window[nCurWindowByte++] = data;
                nCurWindowByte &= win_size - 1;
            } else {
                unsigned int copy_bytes, win_offset;

                if (curbyte >= comprLen)
                    goto out;
                win_offset = getbyte_le(compr[curbyte++]);

                if (curbyte >= comprLen)
                    goto out;
                copy_bytes = getbyte_le(compr[curbyte++]);

                win_offset |= (copy_bytes >> 4) << 8;
                copy_bytes &= 0x0f;
                copy_bytes += 3;
                for (unsigned int i = 0; i < copy_bytes; i++) {
                    unsigned char data;

                    if (act_uncomprlen >= uncomprLen)
                        goto out;

                    data = window[(win_offset + i) & (win_size - 1)];
                    uncompr[act_uncomprlen++] = data;       
                    window[nCurWindowByte++] = data;
                    nCurWindowByte &= win_size - 1;     
                }
            }
        }
    }
out:
    return act_uncomprlen;  
}

0 个答案:

没有答案