-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
280 lines (271 loc) · 6.36 KB
/
check.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package imagecheck
import (
"bufio"
"bytes"
"compress/zlib"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"github.com/ulikunitz/xz/lzma"
)
func wrap(f string, err error) error {
if err == nil {
return nil
}
return fmt.Errorf(f, err)
}
func checkJpeg(f *os.File) error {
_, err := f.Seek(0, io.SeekStart)
if err != nil {
return wrap("seeking to file start: %v", err)
}
bf := bufio.NewReader(f)
markerStart := false
for {
b, err := bf.ReadByte()
if err != nil {
return wrap("reading marker start or entropy-coded data: %v", err)
}
if b == 0xFF {
markerStart = true
continue
}
if b == 0x00 {
markerStart = false
continue
}
if markerStart {
if b == 0xD9 {
return nil
}
if b >= 0xD0 && b < 0xD9 {
markerStart = false
continue
}
buf := make([]byte, 2)
_, err := io.ReadFull(bf, buf)
if err != nil {
return wrap("reading marker size: %v", err)
}
size := int(buf[0])<<8 | int(buf[1])
_, err = bf.Discard(size)
if err != nil {
return wrap("skipping marker data: %v", err)
}
markerStart = false
}
}
}
func checkPng(f *os.File) error {
endSignature := []byte{0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82}
iend := []byte{0x49, 0x45, 0x4E, 0x44}
_, err := f.Seek(int64(-len(endSignature)), io.SeekEnd)
if err != nil {
return wrap("seeking to trailing IEND block: %v", err)
}
end := make([]byte, len(endSignature))
_, err = io.ReadFull(f, end)
if err != nil {
return wrap("reading trailing IEND block: %v", err)
}
if !bytes.Equal(end, endSignature) {
return errors.New("file does not end with IEND block")
}
_, err = f.Seek(8, io.SeekStart)
if err != nil {
return wrap("seeking to first block: %v", err)
}
bf := bufio.NewReader(f)
buf := make([]byte, 4)
for {
_, err := io.ReadFull(bf, buf)
if err != nil {
return wrap("reading block size: %v", err)
}
length := int(binary.BigEndian.Uint32(buf))
_, err = io.ReadFull(bf, buf)
if err != nil {
return wrap("reading block type: %v", err)
}
if bytes.Equal(buf, iend) {
// IEND chunk
_, err := bf.Discard(4)
if err != nil {
return wrap("skipping IEND block: %v", err)
}
return nil
}
_, err = bf.Discard(4 + length)
if err != nil {
return wrap("skipping block data: %v", err)
}
}
}
func checkGif(f *os.File) error {
_, err := f.Seek(-1, io.SeekEnd)
if err != nil {
return wrap("seeking to GIF trailer: %v", err)
}
end := make([]byte, 1)
_, err = io.ReadFull(f, end)
if err != nil {
return wrap("reading GIF trailer: %v", err)
}
if end[0] != 0x3B {
return errors.New("file does not end with GIF trailer")
}
_, err = f.Seek(10, io.SeekStart)
if err != nil {
return wrap("seeking to header bitflag: %v", err)
}
bf := bufio.NewReader(f)
bitflag := make([]byte, 1)
_, err = io.ReadFull(bf, bitflag)
if err != nil {
return wrap("reading header bitflag: %v", err)
}
skip := 2
if bitflag[0]&0b10000000 != 0 {
skip += 3 * (1 << (1 + int(bitflag[0]&0b111)))
}
_, err = bf.Discard(skip)
if err != nil {
return wrap("skipping header: %v", err)
}
for {
blockType, err := bf.ReadByte()
if err != nil {
return wrap("reading block type: %v", err)
}
if blockType == 0x3B {
return nil
}
var skip int
if blockType == 0x2C {
_, err = bf.Discard(8)
if err != nil {
return wrap("skipping image block data: %v", err)
}
skip = 1
if bitflag[0]&0b10000000 != 0 {
skip += 3 * (1 << (1 + int(bitflag[0]&0b1111)))
}
} else {
skip = 1
}
_, err = bf.Discard(skip)
if err != nil {
return wrap("skipping block data: %v", err)
}
for {
size, err := bf.ReadByte()
if err != nil {
return wrap("reading sub-block size: %v", err)
}
if size == 0 {
break
}
_, err = bf.Discard(int(size))
if err != nil {
return wrap("skipping sub-block data: %v", err)
}
}
}
}
func checkSwf(f *os.File) error {
_, err := f.Seek(0, io.SeekStart)
if err != nil {
return wrap("seeking to start of file: %v", err)
}
buf := make([]byte, 4)
_, err = io.ReadFull(f, buf[:1])
if err != nil {
return wrap("reading SWF file signature: %v", err)
}
compressionType := buf[0]
_, err = f.Seek(4, io.SeekStart)
if err != nil {
return wrap("seeking to file size header: %v", err)
}
_, err = io.ReadFull(f, buf)
if err != nil {
return wrap("reading file size header: %v", err)
}
size := int64(binary.LittleEndian.Uint32(buf))
if compressionType == 0x46 {
total, err := f.Seek(0, io.SeekEnd)
if err != nil {
return wrap("seeking to file end: %v", err)
}
if total < size {
return errors.New("file length is less than file size header")
}
return nil
}
if compressionType == 0x43 {
zr, err := zlib.NewReader(f)
if err != nil {
return wrap("parsing ZLIB compressed data signature: %v", err)
}
n, err := io.Copy(ioutil.Discard, zr)
if err != nil {
return wrap("decompressing ZLIB compressed data: %v", err)
}
n += 8
if n < size {
return errors.New("ZLIB-decompressed data length is less than file size header")
}
return nil
}
if compressionType == 0x5A {
lr, err := lzma.NewReader(f)
if err != nil {
return wrap("parsing LZMA compressed data signature: %v", err)
}
n, err := io.Copy(ioutil.Discard, lr)
if err != nil {
return wrap("decompressing LZMA compressed data: %v", err)
}
n += 8
if n < size {
return errors.New("LZMA-compressed data length is less than file size header")
}
return nil
}
return errors.New("file has unknown compression type")
}
func checkFile(f *os.File) error {
signature := make([]byte, 8)
_, err := io.ReadFull(f, signature)
if err != nil {
return wrap("reading file signature: %v", err)
}
pngSignature := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}
gifSignature := []byte{0x47, 0x49, 0x46, 0x38}
jpegSignature := []byte{0xFF, 0xD8}
swfSignature := []byte{0x57, 0x53}
if bytes.HasPrefix(signature, pngSignature) {
return wrap("PNG file: %v", checkPng(f))
}
if bytes.HasPrefix(signature, gifSignature) {
return wrap("GIF file: %v", checkGif(f))
}
if bytes.HasPrefix(signature, jpegSignature) {
return wrap("JPEG file: %v", checkJpeg(f))
}
if bytes.HasPrefix(signature[1:], swfSignature) {
return wrap("SWF file: %v", checkSwf(f))
}
return errors.New("unknown image file format")
}
func Check(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
return checkFile(f)
}