| 1 | package imagefile |
| 2 | |
| 3 | import "encoding/binary" |
| 4 | |
| 5 | // The GIF walker (§5). It keeps the image data, the graphic control |
| 6 | // extensions and the NETSCAPE2.0 loop extension; it drops comments, |
| 7 | // plain-text extensions and every other application extension (XMP |
| 8 | // rides in one), and ends the file at the trailer. |
| 9 | |
| 10 | const ( |
| 11 | gifImage = 0x2c |
| 12 | gifExtension = 0x21 |
| 13 | gifTrailer = 0x3b |
| 14 | gifControl = 0xf9 |
| 15 | gifApp = 0xff |
| 16 | ) |
| 17 | |
| 18 | func cleanGIF(raw []byte) ([]byte, int, error) { |
| 19 | if len(raw) < 13 { |
| 20 | return nil, 0, ErrMalformed |
| 21 | } |
| 22 | at := 13 + colourTable(raw[10]) |
| 23 | if at > len(raw) { |
| 24 | return nil, 0, ErrMalformed |
| 25 | } |
| 26 | screen := [2]int{int(binary.LittleEndian.Uint16(raw[6:])), |
| 27 | int(binary.LittleEndian.Uint16(raw[8:]))} |
| 28 | out := append(make([]byte, 0, len(raw)), raw[:at]...) |
| 29 | for { |
| 30 | if at >= len(raw) { |
| 31 | return nil, 0, ErrMalformed |
| 32 | } |
| 33 | var next int |
| 34 | var keep bool |
| 35 | var err error |
| 36 | switch raw[at] { |
| 37 | case gifTrailer: |
| 38 | return append(out, gifTrailer), 1, nil |
| 39 | case gifImage: |
| 40 | next, err = gifPicture(raw, at, screen) |
| 41 | keep = true |
| 42 | case gifExtension: |
| 43 | var loop []byte |
| 44 | next, loop, keep, err = gifExtensionBlock(raw, at) |
| 45 | if loop != nil { |
| 46 | out, keep = append(out, loop...), false |
| 47 | } |
| 48 | default: |
| 49 | return nil, 0, ErrMalformed |
| 50 | } |
| 51 | if err != nil { |
| 52 | return nil, 0, err |
| 53 | } |
| 54 | if keep { |
| 55 | out = append(out, raw[at:next]...) |
| 56 | } |
| 57 | at = next |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // colourTable is the size of the colour table a packed field announces. |
| 62 | func colourTable(packed byte) int { |
| 63 | if packed&0x80 == 0 { |
| 64 | return 0 |
| 65 | } |
| 66 | return 3 * (1 << (int(packed&0x07) + 1)) |
| 67 | } |
| 68 | |
| 69 | // gifPicture is an image descriptor, its local colour table, its LZW |
| 70 | // code size and its data sub-blocks. A frame must lie inside the |
| 71 | // logical screen, which is all the limits read (Fable M2): a GIF that |
| 72 | // says 1×1 may not carry a 60,000×60,000 frame. |
| 73 | func gifPicture(raw []byte, at int, screen [2]int) (int, error) { |
| 74 | if at+11 > len(raw) { |
| 75 | return 0, ErrMalformed |
| 76 | } |
| 77 | left := int(binary.LittleEndian.Uint16(raw[at+1:])) |
| 78 | top := int(binary.LittleEndian.Uint16(raw[at+3:])) |
| 79 | width := int(binary.LittleEndian.Uint16(raw[at+5:])) |
| 80 | height := int(binary.LittleEndian.Uint16(raw[at+7:])) |
| 81 | if left+width > screen[0] || top+height > screen[1] { |
| 82 | return 0, ErrMalformed |
| 83 | } |
| 84 | at += 10 + colourTable(raw[at+9]) + 1 |
| 85 | return subBlocks(raw, at) |
| 86 | } |
| 87 | |
| 88 | // gifExtensionBlock is one extension and whether it is kept as it is; |
| 89 | // for NETSCAPE2.0's loop, the extension written again with its loop |
| 90 | // count alone, any further sub-block dropped (Fable L10). |
| 91 | func gifExtensionBlock(raw []byte, at int) (int, []byte, bool, error) { |
| 92 | if at+2 > len(raw) { |
| 93 | return 0, nil, false, ErrMalformed |
| 94 | } |
| 95 | label := raw[at+1] |
| 96 | next, err := subBlocks(raw, at+2) |
| 97 | if err != nil { |
| 98 | return 0, nil, false, err |
| 99 | } |
| 100 | switch label { |
| 101 | case gifControl: |
| 102 | return next, nil, true, nil |
| 103 | case gifApp: |
| 104 | return next, netscapeLoop(raw[at:next]), false, nil |
| 105 | } |
| 106 | return next, nil, false, nil |
| 107 | } |
| 108 | |
| 109 | // netscapeLoop is a NETSCAPE2.0 extension's header and its 3-byte loop |
| 110 | // sub-block, then the terminator, or nil for any other extension. |
| 111 | func netscapeLoop(block []byte) []byte { |
| 112 | head := block[2:] |
| 113 | if len(head) < 17 || head[0] != 11 || |
| 114 | string(head[1:12]) != "NETSCAPE2.0" || head[12] != 3 || |
| 115 | head[13] != 1 { |
| 116 | return nil |
| 117 | } |
| 118 | return append(append([]byte{}, block[:2+16]...), 0) |
| 119 | } |
| 120 | |
| 121 | // subBlocks is where a run of data sub-blocks ends, after its |
| 122 | // terminator. |
| 123 | func subBlocks(raw []byte, at int) (int, error) { |
| 124 | for { |
| 125 | if at >= len(raw) { |
| 126 | return 0, ErrMalformed |
| 127 | } |
| 128 | size := int(raw[at]) |
| 129 | at++ |
| 130 | if size == 0 { |
| 131 | return at, nil |
| 132 | } |
| 133 | at += size |
| 134 | } |
| 135 | } |
| 136 | |