-
Notifications
You must be signed in to change notification settings - Fork 1
/
png.go
67 lines (55 loc) · 1.63 KB
/
png.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
package metascrubber
import (
"bytes"
"encoding/binary"
"errors"
"io"
pngimagestructure "github.com/dsoprea/go-png-image-structure"
)
var chunkTypesToScrub = []string{"eXIf", "tEXt", "iTXt", "zTXt"}
type pngSegmentReader struct {
reader io.Reader
wroteSignature bool
}
func (pr *pngSegmentReader) isMetadataType(chunkType []byte) bool {
for _, test := range chunkTypesToScrub {
if bytes.Equal(chunkType, []byte(test)) {
return true
}
}
return false
}
func (pr *pngSegmentReader) nextSegment() (r io.Reader, isMetadata bool, err error) {
if !pr.wroteSignature {
r = io.LimitReader(pr.reader, int64(len(pngimagestructure.PngSignature)))
pr.wroteSignature = true
return
}
chunkHeaderBuffer := new(bytes.Buffer)
teedReader := io.TeeReader(pr.reader, chunkHeaderBuffer)
var chunkDataLength uint32
if err = binary.Read(teedReader, binary.BigEndian, &chunkDataLength); err != nil {
if !errors.Is(err, io.EOF) {
err = &MalformedDataError{"can't parse png chunk data length", err}
}
return
}
var chunkLength int64
if chunkDataLength == 0 {
// apparently some chunks have a 0 here and then an EOF after
chunkLength = 4
} else {
chunkType := make([]byte, 4)
if _, err = io.ReadFull(teedReader, chunkType); err != nil {
if !errors.Is(err, io.EOF) {
err = &MalformedDataError{"can't parse png chunk type", err}
}
return
}
isMetadata = pr.isMetadataType(chunkType)
// 4 byte chunkType Header + 4 byte chunkDataLength Header + chunkDataLength + 4 byte CRC length
chunkLength = int64(12 + chunkDataLength)
}
r = io.LimitReader(io.MultiReader(chunkHeaderBuffer, pr.reader), chunkLength)
return
}