-
Notifications
You must be signed in to change notification settings - Fork 5
/
entry.go
173 lines (133 loc) · 3.86 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package png
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"github.com/pedrooaugusto/steganography-png/chunk"
scls "github.com/pedrooaugusto/steganography-png/scanlines"
)
// PNG Represents a PNG file as described at www.png.org
type PNG struct {
header []byte
Chunks []chunk.Chunk
}
// String Text representation of the PNG
func (r PNG) String() string {
s := "PORTABLE NETWORK GRAPHICS\n\n"
s += "Header: 137 PNG 13 10 26 10\n\n"
s += "Parsed IDHR:\n"
parsedidhr, _ := json.MarshalIndent(r.GetHeader(), "", " ")
s += string(parsedidhr) + "\n\n"
for _, element := range r.Chunks {
s += element.String()
s += "\n"
}
return s
}
// ToBytes Reduces this PNG structure to a byte array
func (r *PNG) ToBytes() []byte {
raw := []byte{}
raw = append(raw, r.header...)
for _, element := range r.Chunks {
raw = append(raw, element.ToBytes()...)
}
return raw
}
// HideData Hide some data in this png file
func (r *PNG) HideData(data []byte, dataType string, bitloss int) error {
scanlines, maxSize, err := scls.FromChunks(r.Chunks, r.GetHeader())
if err != nil {
return err
}
if err := scanlines.HideBytes(data, []byte(dataType), bitloss); err != nil {
return err
}
chunks, err := scanlines.ToChunks(maxSize)
if err != nil {
return err
}
r.setIdatChunks(chunks)
return nil
}
// RevealData Reveal hidden data in this png image
func (r *PNG) RevealData() (data []byte, dataType string, bitloss int, err error) {
scanlines, _, err := scls.FromChunks(r.Chunks, r.GetHeader())
if err != nil {
return nil, "", 0, err
}
return scanlines.RevealBytes()
}
// setIdatChunks Replaces the current IDAT chunks with diffrent ones
func (r *PNG) setIdatChunks(chunks []chunk.Chunk) {
// First we need to reorder the chunks
var chunks2 []chunk.Chunk
for i := 0; i < len(r.Chunks); i++ {
tipo := r.Chunks[i].GetType()
if tipo != "IDAT" && tipo != "IEND" {
chunks2 = append(chunks2, r.Chunks[i])
}
}
chunks = append(chunks2, chunks...)
chunks = append(chunks, r.Chunks[len(r.Chunks)-1])
r.Chunks = chunks
}
// GetHeight returns the image height
func (r *PNG) GetHeight() uint32 {
return binary.BigEndian.Uint32(r.Chunks[0].Data[4:8])
}
// GetHeader returns the image header (IDHR chunk)
func (r *PNG) GetHeader() map[string]interface{} {
header := make(map[string]interface{})
header["Width"] = binary.BigEndian.Uint32(r.Chunks[0].Data[0:4])
header["Height"] = binary.BigEndian.Uint32(r.Chunks[0].Data[4:8])
header["Bit depth"] = uint32(r.Chunks[0].Data[8])
header["Color type"] = uint32(r.Chunks[0].Data[9])
header["Compression method"] = uint32(r.Chunks[0].Data[10])
header["Filter method"] = uint32(r.Chunks[0].Data[11])
header["Interlace method"] = uint32(r.Chunks[0].Data[11])
header["bpp"] = calculateBPP(ColorType[header["Color type"].(uint32)], header["Bit depth"].(uint32))
return header
}
func (r *PNG) parseHeader(index *uint32, data []byte) error {
arr := []byte{137, 80, 78, 71, 13, 10, 26, 10}
res := bytes.Compare(arr, data[0:8])
if res != 0 {
return errors.New("this is simply not a PNG file, header does not contain the constant bytes")
}
r.header = append(r.header, data[0:8]...)
*index = uint32(len(arr))
return nil
}
// Parse Will parse a byte array to PNG structure
func Parse(file []byte) (PNG, error) {
var png PNG = PNG{}
var index uint32 = 0
err := png.parseHeader(&index, file)
if err != nil {
return png, err
}
for {
png.Chunks = append(png.Chunks, chunk.Parse(&index, file))
if index == uint32(len(file)) {
return png, nil
} else if index > uint32(len(file)) {
return png, errors.New("something went terrible wrong parsing the chunks")
}
}
}
// ColorType Mapping of color type to number of samples
var ColorType = []uint32{
0: 1,
2: 3,
3: 3,
4: 2,
6: 4,
}
func calculateBPP(colorType, bitDepth uint32) int {
bpp := int(colorType * bitDepth / 8)
if bpp <= 0 {
bpp = 1
}
return bpp
}