General structure of a GIF file
A GIF file is a sequence of blocks laid end to end. Each starts with a byte that says what it is, so a decoder can read the file from start to finish without seeking and without a table of contents.
Layout
The exact ordering rules are on the block order page.
Identifying bytes
| Byte | ASCII | Block |
|---|---|---|
| 2C | , | image descriptor |
| 21 | ! | extension, the next byte is its label |
| 3B | ; | trailer |
The values were chosen to be readable characters: comma, exclamation mark and semicolon. In the early days that made files easier to inspect and to send over text links.
Data sub-blocks
Variable-length data (the compressed image, comment text, application data) is split into sub-blocks. Each sub-block starts with a length byte from 1 to 255, followed by that many data bytes. The sequence ends with a sub-block of length 0, the terminator:
This lets a decoder skip a block it does not understand: it reads the length bytes and jumps over the data until it hits 00. It does not need to know the contents to know where the block ends.
Little-endian numbers
16-bit numbers (dimensions, positions, delays) are stored least significant byte first:
Packed fields
Several blocks have a flag byte (packed fields) in which individual bits mean different things. Bits are numbered from the most significant (7) to the least (0). An example from the logical screen descriptor:
Each field is detailed on the page for its block. The complete example file, byte by byte, is in the introduction.