package attach import ( "bytes" "encoding/binary" "errors" "image" _ "image/gif" // DecodeConfig for GIF _ "image/jpeg" // DecodeConfig for JPEG _ "image/png" // DecodeConfig for PNG ) var errFormat = errors.New("attach: malformed image") // StripLocation removes where a photo was taken. JPEG: the EXIF GPS // directory is zeroed in place (orientation and the rest stay) and XMP // and Photoshop (IPTC) segments, which can carry a place, are dropped. // PNG: eXIf and XMP text chunks go. WebP: the EXIF and XMP chunks go. // GIF carries none. A file that cannot be walked is refused. func StripLocation(mediaType string, b []byte) ([]byte, error) { switch mediaType { case JPEG: return stripJPEG(b) case PNG: return stripPNG(b) case WebP: return stripWebP(b) case GIF: return b, nil } return nil, errFormat } const ( tagGPS = 0x8825 tagOrientation = 0x0112 ) func stripJPEG(b []byte) ([]byte, error) { if len(b) < 4 || b[0] != 0xff || b[1] != 0xd8 { return nil, errFormat } out := bytes.NewBuffer(make([]byte, 0, len(b))) out.Write(b[:2]) i := 2 scanned := false for i < len(b) { if b[i] != 0xff { return nil, errFormat } for i < len(b) && b[i] == 0xff { i++ } if i >= len(b) { return nil, errFormat } marker := b[i] i++ switch { case marker == 0xd9: // EOI: anything after it (appended images) goes out.Write([]byte{0xff, marker}) return out.Bytes(), nil case marker >= 0xd0 && marker <= 0xd7, marker == 0x01: out.Write([]byte{0xff, marker}) continue } if i+2 > len(b) { return nil, errFormat } n := int(binary.BigEndian.Uint16(b[i:])) if n < 2 || i+n > len(b) { return nil, errFormat } seg := b[i+2 : i+n] if marker == 0xda { // Start of scan: its header, then the entropy-coded data up to // the next marker (stuffed bytes and restart markers are data). // The walk goes on after it: a progressive JPEG has more scans, // and a segment after a scan is read like any other. out.Write([]byte{0xff, marker}) out.Write(b[i : i+n]) j := i + n for j < len(b) { if b[j] == 0xff && j+1 < len(b) { next := b[j+1] if next == 0x00 || next >= 0xd0 && next <= 0xd7 { out.Write(b[j : j+2]) j += 2 continue } if next != 0xff { break } } out.WriteByte(b[j]) j++ } i, scanned = j, true continue } drop := false switch marker { case 0xe1: if bytes.HasPrefix(seg, []byte("Exif\x00\x00")) { fixed := append([]byte(nil), seg...) if err := zeroGPS(fixed[6:]); err != nil { return nil, err } seg = fixed } else if bytes.HasPrefix(seg, []byte("http://ns.adobe.com/")) { drop = true } case 0xed: // APP13: Photoshop IRB with IPTC (city, sublocation) drop = true } // Metadata after a scan has no business there: it goes. if scanned && marker >= 0xe1 && marker <= 0xef { drop = true } if !drop { out.Write([]byte{0xff, marker}) out.Write(b[i : i+2]) out.Write(seg) } i += n } return nil, errFormat } // tiff reads a TIFF structure in either byte order. type tiff struct { b []byte bo binary.ByteOrder } func newTIFF(b []byte) (tiff, uint32, error) { if len(b) < 8 { return tiff{}, 0, errFormat } t := tiff{b: b} switch string(b[:2]) { case "II": t.bo = binary.LittleEndian case "MM": t.bo = binary.BigEndian default: return tiff{}, 0, errFormat } if t.bo.Uint16(b[2:]) != 42 { return tiff{}, 0, errFormat } return t, t.bo.Uint32(b[4:]), nil } var 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} // entries returns the offset of an IFD's first entry and its count. func (t tiff) entries(off uint32) (uint32, int, error) { if uint64(off)+2 > uint64(len(t.b)) { return 0, 0, errFormat } n := int(t.bo.Uint16(t.b[off:])) if uint64(off)+2+uint64(n)*12 > uint64(len(t.b)) { return 0, 0, errFormat } return off + 2, n, nil } // zeroGPS finds IFD0's GPS pointer and blanks the GPS directory: every // value it points at, every entry, and its count. func zeroGPS(b []byte) error { t, ifd0, err := newTIFF(b) if err != nil { return err } first, n, err := t.entries(ifd0) if err != nil { return err } for k := 0; k < n; k++ { e := first + uint32(k)*12 if t.bo.Uint16(b[e:]) != tagGPS { continue } gps := t.bo.Uint32(b[e+8:]) gfirst, gn, err := t.entries(gps) if err != nil { return err } for j := 0; j < gn; j++ { ge := gfirst + uint32(j)*12 size := typeSize[t.bo.Uint16(b[ge+2:])] * t.bo.Uint32(b[ge+4:]) if size > 4 { at := t.bo.Uint32(b[ge+8:]) if uint64(at)+uint64(size) > uint64(len(b)) { return errFormat } clear(b[at : at+size]) } clear(b[ge : ge+12]) } t.bo.PutUint16(b[gps:], 0) } return nil } // Orientation is a JPEG's EXIF orientation (1 to 8), 1 when absent. func Orientation(b []byte) int { i := 2 for i+4 <= len(b) && b[i] == 0xff { marker := b[i+1] if marker == 0xda || marker == 0xd9 { break } n := int(binary.BigEndian.Uint16(b[i+2:])) if n < 2 || i+2+n > len(b) { break } seg := b[i+4 : i+2+n] if marker == 0xe1 && bytes.HasPrefix(seg, []byte("Exif\x00\x00")) { t, ifd0, err := newTIFF(seg[6:]) if err != nil { return 1 } first, count, err := t.entries(ifd0) if err != nil { return 1 } for k := 0; k < count; k++ { e := first + uint32(k)*12 if t.bo.Uint16(t.b[e:]) == tagOrientation { if v := int(t.bo.Uint16(t.b[e+8:])); v >= 1 && v <= 8 { return v } } } return 1 } i += 2 + n } return 1 } func stripPNG(b []byte) ([]byte, error) { const sig = 8 if len(b) < sig { return nil, errFormat } out := bytes.NewBuffer(make([]byte, 0, len(b))) out.Write(b[:sig]) for i := sig; i < len(b); { if i+12 > len(b) { return nil, errFormat } n := int(binary.BigEndian.Uint32(b[i:])) if n < 0 || i+12+n > len(b) { return nil, errFormat } kind := string(b[i+4 : i+8]) data := b[i+8 : i+8+n] drop := kind == "eXIf" || ((kind == "iTXt" || kind == "tEXt" || kind == "zTXt") && bytes.HasPrefix(data, []byte("XML:com.adobe.xmp\x00"))) if !drop { out.Write(b[i : i+12+n]) } i += 12 + n if kind == "IEND" { return out.Bytes(), nil } } return nil, errFormat } func stripWebP(b []byte) ([]byte, error) { if len(b) < 12 { return nil, errFormat } var body bytes.Buffer vp8x := -1 for i := 12; i < len(b); { if i+8 > len(b) { return nil, errFormat } n := int(binary.LittleEndian.Uint32(b[i+4:])) padded := n + n&1 if n < 0 || i+8+n > len(b) { return nil, errFormat } end := min(i+8+padded, len(b)) kind := string(b[i : i+4]) if kind != "EXIF" && kind != "XMP " { if kind == "VP8X" { vp8x = body.Len() + 8 } body.Write(b[i:end]) } i = end } out := body.Bytes() if vp8x >= 0 && vp8x < len(out) { out[vp8x] &^= 0x08 | 0x04 // the EXIF and XMP flags } head := make([]byte, 12) copy(head, "RIFF") binary.LittleEndian.PutUint32(head[4:], uint32(4+len(out))) copy(head[8:], "WEBP") return append(head, out...), nil } // Dimensions is an image's pixel size, 0, 0 when it cannot be read. func Dimensions(mediaType string, b []byte) (int, int) { if mediaType == WebP { return webpSize(b) } c, _, err := image.DecodeConfig(bytes.NewReader(b)) if err != nil { return 0, 0 } if mediaType == JPEG && Orientation(b) >= 5 { return c.Height, c.Width } return c.Width, c.Height } func webpSize(b []byte) (int, int) { for i := 12; i+8 <= len(b); { n := int(binary.LittleEndian.Uint32(b[i+4:])) if n < 0 || i+8+n > len(b) { return 0, 0 } p := b[i+8 : i+8+n] switch string(b[i : i+4]) { case "VP8X": if len(p) >= 10 { w := int(p[4]) | int(p[5])<<8 | int(p[6])<<16 h := int(p[7]) | int(p[8])<<8 | int(p[9])<<16 return w + 1, h + 1 } case "VP8 ": if len(p) >= 10 && p[3] == 0x9d && p[4] == 0x01 && p[5] == 0x2a { return int(binary.LittleEndian.Uint16(p[6:]) & 0x3fff), int(binary.LittleEndian.Uint16(p[8:]) & 0x3fff) } case "VP8L": if len(p) >= 5 && p[0] == 0x2f { v := binary.LittleEndian.Uint32(p[1:]) return int(v&0x3fff) + 1, int((v>>14)&0x3fff) + 1 } } i += 8 + n + n&1 } return 0, 0 }