oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package imagefile
2
3import (
4 "encoding/binary"
5)
6
7// The WebP walker (§5). It keeps VP8, VP8L, VP8X, ALPH, ANIM, ANMF and
8// ICCP, drops EXIF, XMP and unknown chunks, rewrites VP8X's flags and
9// RIFF's size to match, and ends the file at the RIFF length.
10
11var webpKept = map[string]bool{
12 "VP8 ": true, "VP8L": true, "VP8X": true, "ALPH": true, "ANIM": true,
13 "ANMF": true, "ICCP": true,
14}
15
16// VP8X's flags for the two chunks the walker drops.
17const (
18 webpFlagExif = 0x08
19 webpFlagXMP = 0x04
20)
21
22func cleanWebP(raw []byte) ([]byte, int, error) {
23 if len(raw) < 12 {
24 return nil, 0, ErrMalformed
25 }
26 size := binary.LittleEndian.Uint32(raw[4:])
27 if size < 4 || uint64(size) > uint64(len(raw)-8) {
28 return nil, 0, ErrMalformed
29 }
30 end := 8 + int(size)
31 // One buffer, the header first (Fable L9).
32 out := append(make([]byte, 0, end), "RIFF\x00\x00\x00\x00WEBP"...)
33 for at := 12; at < end; {
34 kind, next, err := webpChunk(raw, at, end)
35 if err != nil {
36 return nil, 0, err
37 }
38 if out, err = keepWebP(out, raw, kind, at, next); err != nil {
39 return nil, 0, err
40 }
41 at = next
42 }
43 binary.LittleEndian.PutUint32(out[4:], uint32(len(out)-8))
44 return out, 1, nil
45}
46
47// keepWebP appends one top-level chunk to out, if it is kept: VP8X with
48// the dropped chunks' flags cleared, and ANMF with only its picture.
49func keepWebP(out, raw []byte, kind string, at, next int) ([]byte,
50 error) {
51 if !webpKept[kind] {
52 return out, nil
53 }
54 start := len(out)
55 switch kind {
56 case "ANMF":
57 return keepFrame(out, raw[at:next])
58 case "VP8X":
59 out = append(out, raw[at:next]...)
60 out[start+8] &^= webpFlagExif | webpFlagXMP
61 return out, nil
62 }
63 return append(out, raw[at:next]...), nil
64}
65
66// frameKept is what an ANMF frame may hold after its 16-byte header:
67// its picture, and nothing else (Fable L10).
68var frameKept = map[string]bool{"ALPH": true, "VP8 ": true, "VP8L": true}
69
70// keepFrame appends an ANMF chunk holding only its header and picture
71// chunks, its size written again.
72func keepFrame(out, chunk []byte) ([]byte, error) {
73 if len(chunk) < 8+16 {
74 return nil, ErrMalformed
75 }
76 start := len(out)
77 out = append(out, chunk[:8+16]...)
78 end := 8 + int(binary.LittleEndian.Uint32(chunk[4:]))
79 if end < 8+16 || end > len(chunk) {
80 return nil, ErrMalformed
81 }
82 for at := 8 + 16; at < end; {
83 kind, next, err := webpChunk(chunk, at, end)
84 if err != nil {
85 return nil, err
86 }
87 if frameKept[kind] {
88 out = append(out, chunk[at:next]...)
89 }
90 at = next
91 }
92 binary.LittleEndian.PutUint32(out[start+4:], uint32(len(out)-start-8))
93 return out, nil
94}
95
96// webpChunk reads the chunk at raw[at], inside the RIFF's end: its
97// FourCC and where the next chunk starts, after any padding byte.
98func webpChunk(raw []byte, at, end int) (string, int, error) {
99 if at+8 > end {
100 return "", 0, ErrMalformed
101 }
102 kind := string(raw[at : at+4])
103 length := binary.LittleEndian.Uint32(raw[at+4:])
104 if uint64(length) > uint64(end-at-8) {
105 return "", 0, ErrMalformed
106 }
107 next := at + 8 + int(length)
108 if length%2 == 1 {
109 if next >= end {
110 return "", 0, ErrMalformed
111 }
112 next++
113 }
114 if kind == "VP8X" && length < 10 {
115 return "", 0, ErrMalformed
116 }
117 return kind, next, nil
118}
119