0
votes

After getting my first problem solved at C++ zLib compress byte array i faced another problem

void CGGCBotDlg::OnBnClickedButtonCompress()
{
// TODO: Add your control notification handler code here
z_const char hello[256];
hello[0] = 0x0A;
hello[1] = 0x0A;
hello[2] = 0x0A;
hello[3] = 0x0A;
hello[4] = 0x0A;
hello[5] = PKT_END;
hello[6] = PKT_END;
hello[7] = PKT_END;

Byte compr[256];
uLong comprLen = sizeof(compr);

int ReturnCode;

ReturnCode = Compress(compr, comprLen, hello, Z_DEFAULT_COMPRESSION);
g_CS.Send(&compr,comprLen);

}



int CGGCBotDlg::Compress(Byte Compressed[], uLong CompressedLength, CHAR YourByte[], int CompressionLevel)
 {

int zReturnCode;
int Len;

    for (int i = 0 ; i <= 10240 ; i++)
    {
        if (YourByte[i] == PKT_END && YourByte[i+1] == PKT_END && YourByte[i+2] == PKT_END)
            {
                Len = i - 1;
                break;
            }
    }

uLong Length = (uLong)(sizeof(YourByte) * 1.0001) + 12;

zReturnCode = compress2(Compressed, &Length, (const Bytef*)YourByte, Len,CompressionLevel);

return zReturnCode;

 }

Im trying to compress hello[] wich is actually 5 bytes (I want to compress first 5 bytes)
The expected result after compressing is : 0x78,0x9C,0xE3,0xE2,0x02,0x02,0x06,0x00,0x00,0xCE,0x00,0x33
But what I get after compressing is just the first 4 bytes of expected result and another bytes are just something else.

And my second problem is that i want to replcae 256 in Byte compr[256] with the exact number of bytes after decompressing original buffer (which is 12 in my case)

Would be great if someone correct me
Thanks

1

1 Answers

2
votes

this line is wrong:

Len = i - 1;

because when i is 5, you do Len = i - 1; so len will be 4, but you want compress 5 bytes. just use:

Len = i;

another problem comprLen is never been assigned a value. In Compress(Byte Compressed[], uLong CompressedLength..), itCompressedLength is not used. I assume you want its value back. you should define like this:

Compress(Byte Compressed[], uLong& CompressedLength..)

and change the line to use CompressedLength instead of using length:

zReturnCode = compress2(Compressed, &CompressedLength, (const Bytef*)YourByte, Len,CompressionLevel);