This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
exif_remove.go
129 lines (102 loc) · 2.62 KB
/
exif_remove.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package exifremove
import (
"bytes"
"encoding/binary"
"errors"
"image/jpeg"
"image/png"
"github.com/dsoprea/go-exif"
"github.com/dsoprea/go-jpeg-image-structure"
"github.com/dsoprea/go-png-image-structure"
)
func Remove(data []byte) ([]byte, error) {
const (
JpegMediaType = "jpeg"
PngMediaType = "png"
OtherMediaType = "other"
StartBytes = 0
EndBytes = 0
)
type MediaContext struct {
MediaType string
RootIfd *exif.Ifd
RawExif []byte
Media interface{}
}
jmp := jpegstructure.NewJpegMediaParser()
pmp := pngstructure.NewPngMediaParser()
filtered := []byte{}
if jmp.LooksLikeFormat(data) {
sl, err := jmp.ParseBytes(data)
if err != nil {
return nil, err
}
_, rawExif, err := sl.Exif()
if err != nil {
return data, nil
}
startExifBytes := StartBytes
endExifBytes := EndBytes
if bytes.Contains(data, rawExif) {
for i := 0; i < len(data)-len(rawExif); i++ {
if bytes.Compare(data[i:i+len(rawExif)], rawExif) == 0 {
startExifBytes = i
endExifBytes = i + len(rawExif)
break
}
}
fill := make([]byte, len(data[startExifBytes:endExifBytes]))
copy(data[startExifBytes:endExifBytes], fill)
}
filtered = data
_, err = jpeg.Decode(bytes.NewReader(filtered))
if err != nil {
return nil, errors.New("EXIF removal corrupted " + err.Error())
}
} else if pmp.LooksLikeFormat(data) {
cs, err := pmp.ParseBytes(data)
if err != nil {
return nil, err
}
_, rawExif, err := cs.Exif()
if err != nil {
return data, nil
}
startExifBytes := StartBytes
endExifBytes := EndBytes
if bytes.Contains(data, rawExif) {
for i := 0; i < len(data)-len(rawExif); i++ {
if bytes.Compare(data[i:i+len(rawExif)], rawExif) == 0 {
startExifBytes = i
endExifBytes = i + len(rawExif)
break
}
}
fill := make([]byte, len(data[startExifBytes:endExifBytes]))
copy(data[startExifBytes:endExifBytes], fill)
}
filtered = data
chunks := readPNGChunks(bytes.NewReader(filtered))
for _, chunk := range chunks {
if !chunk.CRCIsValid() {
offset := int(chunk.Offset) + 8 + int(chunk.Length)
crc := chunk.CalculateCRC()
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, crc)
crcBytes := buf.Bytes()
copy(filtered[offset:], crcBytes)
}
}
chunks = readPNGChunks(bytes.NewReader(filtered))
for _, chunk := range chunks {
if !chunk.CRCIsValid() {
return nil, errors.New("EXIF removal failed CRC")
}
}
_, err = png.Decode(bytes.NewReader(filtered))
if err != nil {
return nil, errors.New("EXIF removal corrupted " + err.Error())
}
}
return filtered, nil
}