oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package attach
2
3import (
4 "bytes"
5 "encoding/binary"
6 "errors"
7 "image"
8 _ "image/gif" // DecodeConfig for GIF
9 _ "image/jpeg" // DecodeConfig for JPEG
10 _ "image/png" // DecodeConfig for PNG
11)
12
13var errFormat = errors.New("attach: malformed image")
14
15// StripLocation removes where a photo was taken. JPEG: the EXIF GPS
16// directory is zeroed in place (orientation and the rest stay) and XMP
17// and Photoshop (IPTC) segments, which can carry a place, are dropped.
18// PNG: eXIf and XMP text chunks go. WebP: the EXIF and XMP chunks go.
19// GIF carries none. A file that cannot be walked is refused.
20func StripLocation(mediaType string, b []byte) ([]byte, error) {
21 switch mediaType {
22 case JPEG:
23 return stripJPEG(b)
24 case PNG:
25 return stripPNG(b)
26 case WebP:
27 return stripWebP(b)
28 case GIF:
29 return b, nil
30 }
31 return nil, errFormat
32}
33
34const (
35 tagGPS = 0x8825
36 tagOrientation = 0x0112
37)
38
39func stripJPEG(b []byte) ([]byte, error) {
40 if len(b) < 4 || b[0] != 0xff || b[1] != 0xd8 {
41 return nil, errFormat
42 }
43 out := bytes.NewBuffer(make([]byte, 0, len(b)))
44 out.Write(b[:2])
45 i := 2
46 scanned := false
47 for i < len(b) {
48 if b[i] != 0xff {
49 return nil, errFormat
50 }
51 for i < len(b) && b[i] == 0xff {
52 i++
53 }
54 if i >= len(b) {
55 return nil, errFormat
56 }
57 marker := b[i]
58 i++
59 switch {
60 case marker == 0xd9: // EOI: anything after it (appended images) goes
61 out.Write([]byte{0xff, marker})
62 return out.Bytes(), nil
63 case marker >= 0xd0 && marker <= 0xd7, marker == 0x01:
64 out.Write([]byte{0xff, marker})
65 continue
66 }
67 if i+2 > len(b) {
68 return nil, errFormat
69 }
70 n := int(binary.BigEndian.Uint16(b[i:]))
71 if n < 2 || i+n > len(b) {
72 return nil, errFormat
73 }
74 seg := b[i+2 : i+n]
75 if marker == 0xda {
76 // Start of scan: its header, then the entropy-coded data up to
77 // the next marker (stuffed bytes and restart markers are data).
78 // The walk goes on after it: a progressive JPEG has more scans,
79 // and a segment after a scan is read like any other.
80 out.Write([]byte{0xff, marker})
81 out.Write(b[i : i+n])
82 j := i + n
83 for j < len(b) {
84 if b[j] == 0xff && j+1 < len(b) {
85 next := b[j+1]
86 if next == 0x00 || next >= 0xd0 && next <= 0xd7 {
87 out.Write(b[j : j+2])
88 j += 2
89 continue
90 }
91 if next != 0xff {
92 break
93 }
94 }
95 out.WriteByte(b[j])
96 j++
97 }
98 i, scanned = j, true
99 continue
100 }
101 drop := false
102 switch marker {
103 case 0xe1:
104 if bytes.HasPrefix(seg, []byte("Exif\x00\x00")) {
105 fixed := append([]byte(nil), seg...)
106 if err := zeroGPS(fixed[6:]); err != nil {
107 return nil, err
108 }
109 seg = fixed
110 } else if bytes.HasPrefix(seg, []byte("http://ns.adobe.com/")) {
111 drop = true
112 }
113 case 0xed: // APP13: Photoshop IRB with IPTC (city, sublocation)
114 drop = true
115 }
116 // Metadata after a scan has no business there: it goes.
117 if scanned && marker >= 0xe1 && marker <= 0xef {
118 drop = true
119 }
120 if !drop {
121 out.Write([]byte{0xff, marker})
122 out.Write(b[i : i+2])
123 out.Write(seg)
124 }
125 i += n
126 }
127 return nil, errFormat
128}
129
130// tiff reads a TIFF structure in either byte order.
131type tiff struct {
132 b []byte
133 bo binary.ByteOrder
134}
135
136func newTIFF(b []byte) (tiff, uint32, error) {
137 if len(b) < 8 {
138 return tiff{}, 0, errFormat
139 }
140 t := tiff{b: b}
141 switch string(b[:2]) {
142 case "II":
143 t.bo = binary.LittleEndian
144 case "MM":
145 t.bo = binary.BigEndian
146 default:
147 return tiff{}, 0, errFormat
148 }
149 if t.bo.Uint16(b[2:]) != 42 {
150 return tiff{}, 0, errFormat
151 }
152 return t, t.bo.Uint32(b[4:]), nil
153}
154
155var typeSize = map[uint16]uint32{1: 1, 2: 1, 3: 2, 4: 4, 5: 8, 6: 1, 7: 1, 8: 2, 9: 4, 10: 8, 11: 4, 12: 8}
156
157// entries returns the offset of an IFD's first entry and its count.
158func (t tiff) entries(off uint32) (uint32, int, error) {
159 if uint64(off)+2 > uint64(len(t.b)) {
160 return 0, 0, errFormat
161 }
162 n := int(t.bo.Uint16(t.b[off:]))
163 if uint64(off)+2+uint64(n)*12 > uint64(len(t.b)) {
164 return 0, 0, errFormat
165 }
166 return off + 2, n, nil
167}
168
169// zeroGPS finds IFD0's GPS pointer and blanks the GPS directory: every
170// value it points at, every entry, and its count.
171func zeroGPS(b []byte) error {
172 t, ifd0, err := newTIFF(b)
173 if err != nil {
174 return err
175 }
176 first, n, err := t.entries(ifd0)
177 if err != nil {
178 return err
179 }
180 for k := 0; k < n; k++ {
181 e := first + uint32(k)*12
182 if t.bo.Uint16(b[e:]) != tagGPS {
183 continue
184 }
185 gps := t.bo.Uint32(b[e+8:])
186 gfirst, gn, err := t.entries(gps)
187 if err != nil {
188 return err
189 }
190 for j := 0; j < gn; j++ {
191 ge := gfirst + uint32(j)*12
192 size := typeSize[t.bo.Uint16(b[ge+2:])] * t.bo.Uint32(b[ge+4:])
193 if size > 4 {
194 at := t.bo.Uint32(b[ge+8:])
195 if uint64(at)+uint64(size) > uint64(len(b)) {
196 return errFormat
197 }
198 clear(b[at : at+size])
199 }
200 clear(b[ge : ge+12])
201 }
202 t.bo.PutUint16(b[gps:], 0)
203 }
204 return nil
205}
206
207// Orientation is a JPEG's EXIF orientation (1 to 8), 1 when absent.
208func Orientation(b []byte) int {
209 i := 2
210 for i+4 <= len(b) && b[i] == 0xff {
211 marker := b[i+1]
212 if marker == 0xda || marker == 0xd9 {
213 break
214 }
215 n := int(binary.BigEndian.Uint16(b[i+2:]))
216 if n < 2 || i+2+n > len(b) {
217 break
218 }
219 seg := b[i+4 : i+2+n]
220 if marker == 0xe1 && bytes.HasPrefix(seg, []byte("Exif\x00\x00")) {
221 t, ifd0, err := newTIFF(seg[6:])
222 if err != nil {
223 return 1
224 }
225 first, count, err := t.entries(ifd0)
226 if err != nil {
227 return 1
228 }
229 for k := 0; k < count; k++ {
230 e := first + uint32(k)*12
231 if t.bo.Uint16(t.b[e:]) == tagOrientation {
232 if v := int(t.bo.Uint16(t.b[e+8:])); v >= 1 && v <= 8 {
233 return v
234 }
235 }
236 }
237 return 1
238 }
239 i += 2 + n
240 }
241 return 1
242}
243
244func stripPNG(b []byte) ([]byte, error) {
245 const sig = 8
246 if len(b) < sig {
247 return nil, errFormat
248 }
249 out := bytes.NewBuffer(make([]byte, 0, len(b)))
250 out.Write(b[:sig])
251 for i := sig; i < len(b); {
252 if i+12 > len(b) {
253 return nil, errFormat
254 }
255 n := int(binary.BigEndian.Uint32(b[i:]))
256 if n < 0 || i+12+n > len(b) {
257 return nil, errFormat
258 }
259 kind := string(b[i+4 : i+8])
260 data := b[i+8 : i+8+n]
261 drop := kind == "eXIf" || ((kind == "iTXt" || kind == "tEXt" || kind == "zTXt") && bytes.HasPrefix(data, []byte("XML:com.adobe.xmp\x00")))
262 if !drop {
263 out.Write(b[i : i+12+n])
264 }
265 i += 12 + n
266 if kind == "IEND" {
267 return out.Bytes(), nil
268 }
269 }
270 return nil, errFormat
271}
272
273func stripWebP(b []byte) ([]byte, error) {
274 if len(b) < 12 {
275 return nil, errFormat
276 }
277 var body bytes.Buffer
278 vp8x := -1
279 for i := 12; i < len(b); {
280 if i+8 > len(b) {
281 return nil, errFormat
282 }
283 n := int(binary.LittleEndian.Uint32(b[i+4:]))
284 padded := n + n&1
285 if n < 0 || i+8+n > len(b) {
286 return nil, errFormat
287 }
288 end := min(i+8+padded, len(b))
289 kind := string(b[i : i+4])
290 if kind != "EXIF" && kind != "XMP " {
291 if kind == "VP8X" {
292 vp8x = body.Len() + 8
293 }
294 body.Write(b[i:end])
295 }
296 i = end
297 }
298 out := body.Bytes()
299 if vp8x >= 0 && vp8x < len(out) {
300 out[vp8x] &^= 0x08 | 0x04 // the EXIF and XMP flags
301 }
302 head := make([]byte, 12)
303 copy(head, "RIFF")
304 binary.LittleEndian.PutUint32(head[4:], uint32(4+len(out)))
305 copy(head[8:], "WEBP")
306 return append(head, out...), nil
307}
308
309// Dimensions is an image's pixel size, 0, 0 when it cannot be read.
310func Dimensions(mediaType string, b []byte) (int, int) {
311 if mediaType == WebP {
312 return webpSize(b)
313 }
314 c, _, err := image.DecodeConfig(bytes.NewReader(b))
315 if err != nil {
316 return 0, 0
317 }
318 if mediaType == JPEG && Orientation(b) >= 5 {
319 return c.Height, c.Width
320 }
321 return c.Width, c.Height
322}
323
324func webpSize(b []byte) (int, int) {
325 for i := 12; i+8 <= len(b); {
326 n := int(binary.LittleEndian.Uint32(b[i+4:]))
327 if n < 0 || i+8+n > len(b) {
328 return 0, 0
329 }
330 p := b[i+8 : i+8+n]
331 switch string(b[i : i+4]) {
332 case "VP8X":
333 if len(p) >= 10 {
334 w := int(p[4]) | int(p[5])<<8 | int(p[6])<<16
335 h := int(p[7]) | int(p[8])<<8 | int(p[9])<<16
336 return w + 1, h + 1
337 }
338 case "VP8 ":
339 if len(p) >= 10 && p[3] == 0x9d && p[4] == 0x01 && p[5] == 0x2a {
340 return int(binary.LittleEndian.Uint16(p[6:]) & 0x3fff), int(binary.LittleEndian.Uint16(p[8:]) & 0x3fff)
341 }
342 case "VP8L":
343 if len(p) >= 5 && p[0] == 0x2f {
344 v := binary.LittleEndian.Uint32(p[1:])
345 return int(v&0x3fff) + 1, int((v>>14)&0x3fff) + 1
346 }
347 }
348 i += 8 + n + n&1
349 }
350 return 0, 0
351}
352