/
entry.go
154 lines (117 loc) · 3.13 KB
/
entry.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
package chunk
import (
"bytes"
"encoding/binary"
"fmt"
"hash/crc32"
)
// Chunk represents one of the many chunks of a png file
type Chunk struct {
id int // chunk index
dataSize []byte // 4 bytes - data field size
tipo []byte // 4 bytes - [a-Z] letters only (chunk type)
Data []byte // $dataSize bytes
crc []byte // 4 bytes - CRC algorithm
}
// GetDataSize returns dataSize in integer
func (r *Chunk) GetDataSize() uint32 {
if len(r.dataSize) == 0 {
return 0
}
return binary.BigEndian.Uint32(r.dataSize)
}
// SetDataSize set chunk dataSize field
func (r *Chunk) SetDataSize(size []byte) {
r.dataSize = size
}
// GetType returns chunk type
func (r *Chunk) GetType() string {
if len(r.dataSize) == 0 {
return "undefined"
}
return string(r.tipo)
}
// GetCRC returns CRC in integer
func (r *Chunk) GetCRC() uint32 {
if len(r.crc) == 0 {
return 0
}
return binary.BigEndian.Uint32(r.crc)
}
// SetCRC sets the CRC
func (r *Chunk) SetCRC(ncrc []byte) {
r.crc = ncrc
}
// CalcCRC returns a recalculated version of the CRC
func (r *Chunk) CalcCRC() uint32 {
if len(r.tipo) == 0 {
return 0
}
return crc32.ChecksumIEEE(append(r.tipo, r.Data...))
}
// String String representation of chunk
func (r Chunk) String() string {
s := fmt.Sprint("Data size: ", r.GetDataSize()) + "\n"
s += "Type: " + r.GetType() + "\n"
s += "Data: [...]\n"
s += fmt.Sprintf("CRC: %v", r.GetCRC()) + "\n"
s += fmt.Sprintf("CRC': %v", r.CalcCRC()) + "\n"
return s
}
// ToBytes Chunk byte representation
func (r *Chunk) ToBytes() []byte {
raw := []byte{}
raw = append(raw, r.dataSize...)
raw = append(raw, r.tipo...)
raw = append(raw, r.Data...)
raw = append(raw, r.crc...)
return raw
}
// Parse converts a byte array into chunk
func Parse(index *uint32, data []byte) Chunk {
chunk := Chunk{}
chunk.dataSize = append(chunk.dataSize, data[*index:*index+4]...)
*index = *index + 4
chunk.tipo = append(chunk.tipo, data[*index:*index+4]...)
*index = *index + 4
chunk.Data = append(chunk.Data, data[*index:*index+chunk.GetDataSize()]...)
*index = *index + chunk.GetDataSize()
chunk.crc = append(chunk.crc, data[*index:*index+4]...)
*index = *index + 4
return chunk
}
// CreateChunk create a new chunk with the desired options
func CreateChunk(data []byte, tipo []byte) Chunk {
chunk := Chunk{}
chunk.Data = data
chunk.tipo = tipo
newSize := make([]byte, 4)
binary.BigEndian.PutUint32(newSize, uint32(len(data)))
chunk.dataSize = newSize
newCRC := make([]byte, 4)
binary.BigEndian.PutUint32(newCRC, chunk.CalcCRC())
chunk.crc = newCRC
return chunk
}
// BuildIDATChunks will create a bunch of IDAT chunks from a compressed rawBytes array
func BuildIDATChunks(bytesBuff *bytes.Buffer, chunkSize uint32) []Chunk {
defer bytesBuff.Reset()
var rawBytes []byte = bytesBuff.Bytes()
var pointer uint32 = 0
var chunks []Chunk
var length uint32 = uint32(len(rawBytes) - 1)
var tipo []byte = []byte("IDAT")
for {
if pointer >= length {
break
}
b := pointer
e := pointer + chunkSize
if e > length {
e = length + 1
}
chunks = append(chunks, CreateChunk(rawBytes[b:e], tipo))
pointer = e
}
return chunks
}