-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpng.go
161 lines (125 loc) · 2.8 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
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
/*
Portable-stripped down implementation of the PNG file format for simple verification
*/
package main
import (
"bufio"
"encoding/binary"
"errors"
"hash/crc32"
"io"
)
var (
ErrorMissingBytes = errors.New("missing bytes")
ErrorCRCMismatch = errors.New("crc mismatch")
ErrorNotPNG = errors.New("not a png file")
ErrorInvalidHeaderLength = errors.New("invalid header length")
ErrorDOSToUnixConversion = errors.New("dos to unix conversion")
ErrorUnixToDOSConversion = errors.New("unix to dos conversion")
)
//Chunk represents a chunk within the PNG file
type Chunk struct {
Length int32
Type string
Data []byte
CRC uint32
}
//Verify attempts to verify the chunk with the CRC & Length of the file
func (c *Chunk) Verify() (uint32, error) {
if int(c.Length) != len(c.Data) {
// woop. missing bytes
return 0, ErrorMissingBytes
}
dataCrc := crc32.ChecksumIEEE(c.Data)
if dataCrc != c.CRC {
return dataCrc, ErrorCRCMismatch
}
return 0, nil
}
type Header struct {
HeaderBytes []byte
}
func (h *Header) Verify() error {
if h.HeaderBytes[0] != 0x89 && string(h.HeaderBytes[1:4]) != "PNG" {
return ErrorNotPNG
}
if len(h.HeaderBytes) < 8 {
if h.HeaderBytes[5] != 0x0D {
return ErrorDOSToUnixConversion
}
return ErrorInvalidHeaderLength
}
if h.HeaderBytes[7] != 0x0A {
return ErrorUnixToDOSConversion
}
return nil
}
//PNG represents the PNG file structure
type PNG struct {
FileHeader *Header
Chunks map[string][]*Chunk
}
func Read(reader io.Reader) (*PNG, error) {
buf := bufio.NewReader(reader)
var header []byte
var chunks = map[string][]*Chunk{}
readingHeader := true
lfDetection := 0
for {
if readingHeader {
byteRead, err := buf.ReadByte()
if err != nil {
return nil, err
}
if byteRead == 0x0A {
lfDetection++
}
header = append(header, byteRead)
if lfDetection >= 2 {
readingHeader = false
}
} else {
localBuf := make([]byte, 4)
var dataLength int32
var chunkType string
var crc uint32
err := binary.Read(buf, binary.BigEndian, &dataLength)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
_, err = buf.Read(localBuf)
if err != nil {
return nil, err
}
chunkType = string(localBuf)
chunkData := make([]byte, dataLength)
_, err = buf.Read(chunkData)
if err != nil {
return nil, err
}
err = binary.Read(buf, binary.BigEndian, &crc)
if err != nil {
return nil, err
}
chunk := &Chunk{
Length: dataLength,
Type: chunkType,
Data: chunkData,
CRC: crc,
}
if _, ok := chunks[chunkType]; !ok {
chunks[chunkType] = make([]*Chunk, 0)
}
v := chunks[chunkType]
v = append(v, chunk)
chunks[chunkType] = v
}
}
return &PNG{
FileHeader: &Header{header},
Chunks: chunks,
}, nil
}