pngzlibdeflate

Invalid BTYPE value in png parsing (zlib/deflate)


I'm trying to parse a png file but I seem to be missing something.

My test image was the following: https://www.w3.org/Graphics/PNG/alphatest.png

When parsing the file my BTYPE value (https://www.ietf.org/rfc/rfc1951.txt) ends up as 3 which is reserved/not valid.

In the following image you can see where I thought the BFINAL and BTYPE value are specified:

enter image description here

I thought the first byte after the IDAT value specifies zlib method flag and the second byte specifies the additional flag leading me to the third byte (selected in the image) which I believed to be the correct byte for BFINAL and BTYPE. You can see that the value is 0x62 or 98 which has the binary representation 01100010.

This means BFINAL=0 and BTYPE=0b11=3 which is not allowed.

I was expecting the BTYPEvalue to either be 0,1, or 2.


Solution

  • It is important to read the RFC in its entirety.

                 * Data elements are packed into bytes in order of
                   increasing bit number within the byte, i.e., starting
                   with the least-significant bit of the byte.
    

    So for 01100010, BFINAL is 0 (not final) and BTYPE is 01 (fixed). Then the low five bits of the first fixed Huffman code is 01100.