问题
How to decode the application extension block of GIF?
0000300: 73e7 d639 bdad 10ad 9c08 b5a5 0021 ff0b s..9.........!..
0000310: 4e45 5453 4341 5045 322e 3003 0100 0000 NETSCAPE2.0.....
0000320: 21f9 0409 1900 f600 2c00 0000 0016 01b7 !.......,.......
this "
21 ff0b s..9.........!..
0000310: 4e45 5453 4341 5045 322e 30
" is known, but what is "03 0100 0000
"?
回答1:
The following describes GIF Netscape Application extension, taken from here.
The block is 19 bytes long. First 14 bytes belongs to general Application Extension format, syntax is described in GIF89a Specification, section "26. Application Extension".
Syntax
0 | 0x21 | Extension Label +---------------+ 1 | 0xFF | Application Extension Label +---------------+ 2 | 0x0B | Block Size +---------------+ 3 | | +- -+ 4 | | +- -+ 5 | | +- -+ 6 | | +- NETSCAPE -+ Application Identifier (8 bytes) 7 | | +- -+ 8 | | +- -+ 9 | | +- -+ 10 | | +---------------+ 11 | | +- -+ 12 | 2.0 | Application Authentication Code (3 bytes) +- -+ 13 | | +===============+ --+ 14 | 0x03 | Sub-block Data Size | +---------------+ | 15 | 0x01 | Sub-block ID | +---------------+ | Application Data Sub-block 16 | | | +- -+ Loop Count (2 bytes) | 17 | | | +===============+ --+ 18 | 0x00 | Block Terminator
You already know the data up to NETSCAPE2.0
. The next byte 0x03
tells us the next data sub-block length which is always 3 bytes. The following 0x01
is the sub-block ID. For Netscape block, there is only one data sub-block and the ID is 1
.
The following 2 bytes specify the loop count in little endian — how many times the image frames should be looped, which is 0, and 0 means loop forever.
The last byte 0x00
is used to terminate the data block. So when we meet a 0x00
where data sub-block length should be, we know there are no sub-blocks left and we need to stop reading the block.
来源:https://stackoverflow.com/questions/26352546/how-to-decode-the-application-extension-block-of-gif