Home > OS >  Zlib uncompress method not parsing an array of bytes from an image
Zlib uncompress method not parsing an array of bytes from an image

Time:01-03

Currently I'm trying to read the bytes from the IDAT chunk of a PNG image, in C. I am able to get all the other info, including the said array of bytes.

The problem arises whenever I try to decompress said array with zlib's uncompress() method.

[ ... ]

int decompress(Chunk * _chunk, Image * _image)
{
    uLongf compressedSize = _chunk->length;

    byte * uncompressedData = NULL;
    uLongf uncompressedSize = 0;

    int ret = uncompress(uncompressedData, &uncompressedSize, _chunk->data, compressedSize);

    if(ret != Z_OK)
    {
        fprintf(stderr, "Error: failed to uncompress IDAT chunk data. ERR CODE: %d\n", ret);
        return -1;
    }

    [ ... ]
}


The chunk struct is defined as such:

typedef struct chunk
{

    uint32_t length;
    byte chunkType[4];

    byte *data;

} Chunk;

The byte type is just an unsigned char, and the image struct is defined as follows:

typedef struct image
{

    uint32_t width;
    uint32_t height;
  
    byte bitDepth;
    byte colorType;
    byte compression;
    byte filter;
    byte interlace;

} Image;

The test image's HEX representation is:

89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52
00 00 00 11 00 00 00 12 04 03 00 00 00 4F D7 28
67 00 00 00 30 50 4C 54 45 00 00 00 80 00 00 00
80 00 80 80 00 00 00 80 80 00 80 00 80 80 80 80
80 C0 C0 C0 FF 00 00 00 FF 00 FF FF 00 00 00 FF
FF 00 FF 00 FF FF FF FF FF 7B 1F B1 C4 00 00 00
09 70 48 59 73 00 00 0E C4 00 00 0E C4 01 95 2B
0E 1B 00 00 00 28 49 44 41 54 08 D7 63 D8 0D 05
1B 18 36 30 00 01 FF FF FF 24 B1 FE FF FF C0 C0
40 0E 6B FF FF FF 20 73 48 60 C1 5D 0A 00 BB 1A
49 27 39 98 BC 6E 00 00 00 00 49 45 4E 44 AE 42
60 82

And the bytes of the IDAT chunk are:

08 D7 63 D8 0D 05 1B 18 36 30 00 01 FF FF FF 24 B1 FE FF FF C0 C0 40 0E 6B FF FF FF 20 73 48 60 C1 5D 0A 00 BB 1A 49 27

It must be noted that I'm not taking the CRC of the chunk as well; from my understanding it shouldn't be a problem.

Any idea as to why the uncompress() method is returning Z_DATA_ERROR?

CodePudding user response:

You're not giving uncompress() anywhere to put the uncompressed data! uncompressedData cannot be NULL.

  • Related