-
Notifications
You must be signed in to change notification settings - Fork 1
/
hide.go
296 lines (245 loc) · 10.1 KB
/
hide.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package steg
import (
"bufio"
"fmt"
"io"
"math"
"os"
"github.com/zedseven/bch"
"github.com/zedseven/binmani"
"github.com/zedseven/steg/internal/algos"
"github.com/zedseven/steg/internal/util"
)
// HideConfig stores the configuration options for the Hide operation.
type HideConfig struct {
// ImagePath is the path on disk to a supported image.
ImagePath string
// FilePath is the path on disk to the file to hide.
FilePath string
// OutPath is the path on disk to write the output image.
OutPath string
// PatternPath is the path on disk to the pattern file used in encoding.
PatternPath string
// Algorithm is the algorithm to use in the operation.
Algorithm algos.Algo
// MaxCorrectableErrors is the number of bit errors to be able to correct for per file chunk. Setting it to 0 disables bit ECC.
MaxCorrectableErrors uint8
// MaxBitsPerChannel is the maximum number of bits to write per pixel channel.
// The minimum of this and the supported max of the image format is used.
MaxBitsPerChannel uint8
// DecodeAlpha is whether or not to encode the alpha channel.
EncodeAlpha bool
// EncodeMsb is whether to encode the most-significant bits instead - mostly for debugging.
EncodeMsb bool
}
// Hide hides the binary data of a file in a provided image on disk, and saves the result to a new image.
// It has the option of using one of several different encoding algorithms, depending on user needs.
func Hide(config *HideConfig, outputLevel OutputLevel) error {
// Input validation
if len(config.ImagePath) <= 0 {
return &InvalidFormatError{"ImagePath is empty."}
}
if len(config.FilePath) <= 0 {
return &InvalidFormatError{"FilePath is empty."}
}
if len(config.OutPath) <= 0 {
return &InvalidFormatError{"OutPath is empty."}
}
if len(config.PatternPath) <= 0 {
return &InvalidFormatError{"PatternPath is empty."}
}
if !config.Algorithm.IsValid() {
return &InvalidFormatError{"Algorithm is invalid."}
}
if config.MaxCorrectableErrors < 0 {
return &InvalidFormatError{"MaxCorrectableErrors must be non-negative."}
}
if config.MaxBitsPerChannel < 0 || config.MaxBitsPerChannel > 16 {
return &InvalidFormatError{fmt.Sprintf("MaxBitsPerChannel is outside the allowed range of 0-16: Provided %d.", config.MaxBitsPerChannel)}
}
printlnLvl(outputLevel, OutputSteps, fmt.Sprintf("Steg v%d.%d.%d by Zacchary Dempsey-Plante.", VersionMax, VersionMid, VersionMin))
printlnLvl(outputLevel, OutputDebug, "This tool has been set to display debug output.")
printlnLvl(outputLevel, OutputSteps, fmt.Sprintf("Loading the image from '%v'...", config.ImagePath))
pixels, info, err := loadImage(config.ImagePath, outputLevel)
if err != nil {
printlnLvl(outputLevel, OutputSteps, fmt.Sprintf("Unable to load the image at '%v'!", config.ImagePath))
return err
}
config.MaxBitsPerChannel = uint8(util.Min(int(config.MaxBitsPerChannel), int(info.Format.BitsPerChannel)))
printlnLvl(outputLevel, OutputInfo,
fmt.Sprintf("Image info:\n\tDimensions: %dx%dpx\n\tColour model: %v\n\tChannels per pixel: %d\n\tBits per channel: %d",
info.W, info.H, colourModelToStr(info.Format.Model), info.Format.ChannelsPerPix, info.Format.BitsPerChannel))
printlnLvl(outputLevel, OutputSteps, fmt.Sprintf("Opening the file at '%v'...", config.FilePath))
fileReader, err := os.Open(config.FilePath)
if err != nil {
printlnLvl(outputLevel, OutputSteps, fmt.Sprintf("Unable to open the file at '%v'.", config.FilePath))
return err
}
defer func() {
if err = fileReader.Close(); err != nil {
printlnLvl(outputLevel, OutputSteps, fmt.Sprintf("Error closing the file '%v': %v", config.FilePath, err.Error()))
}
}()
printlnLvl(outputLevel, OutputSteps, "Loading up the pattern key...")
pHash, err := hashPatternFile(config.PatternPath)
if err != nil {
printlnLvl(outputLevel, OutputSteps,
fmt.Sprintf("Something went wrong while attempting to hash the pattern file '%v'.", config.PatternPath))
return err
}
printlnLvl(outputLevel, OutputInfo, "Pattern hash:", pHash)
printlnLvl(outputLevel, OutputSteps, "Encoding the file into the image...")
r := bufio.NewReader(fileReader)
b := make([]byte, util.Max(int(encodeChunkSize), int(encodeHeaderSize)))
channelsPerPix := info.Format.ChannelsPerPix
if info.Format.supportsAlpha() && !config.EncodeAlpha {
channelsPerPix--
}
if channelsPerPix <= 0 { // In the case of Alpha & Alpha16 models
return &InsufficientHidingSpotsError{AdditionalInfo:fmt.Sprintf("The provided image is of the %v colour" +
"model, but since alpha-channel encoding was not specified, there are no channels to hide data within.",
colourModelToStr(info.Format.Model))}
}
channelCount := int64(len(*pixels)) * int64(channelsPerPix)
maxWritableBits := channelCount * int64(config.MaxBitsPerChannel)
printlnLvl(outputLevel, OutputInfo, "Maximum writable bits:", maxWritableBits)
f, err := algos.AlgoAddressor(config.Algorithm, pHash, channelCount, config.MaxBitsPerChannel)
if err != nil {
return err
}
printlnLvl(outputLevel, OutputSteps, "Writing steg header...")
fileInfo, err := fileReader.Stat()
if err != nil {
printlnLvl(outputLevel, OutputSteps, "Unable to retrieve file info!")
return err
}
//b = []byte(fmt.Sprintf("steg%02d.%02d.%02d%v%019d", VersionMax, VersionMid, VersionMin, encodeHeaderSeparator, fileInfo.Size()))
fsize := fileInfo.Size()
b[0] = VersionMax
b[1] = VersionMid
b[2] = VersionMin
b[3] = byte(0xff & (fsize >> 24))
b[4] = byte(0xff & (fsize >> 16))
b[5] = byte(0xff & (fsize >> 8))
b[6] = byte(0xff & fsize)
bitsToWrite := fileInfo.Size() * int64(bitsPerByte)
printlnLvl(outputLevel, OutputInfo, fmt.Sprintf("Input file size: %d B", fileInfo.Size()))
printlnLvl(outputLevel, OutputInfo, "File bits to write:", bitsToWrite)
var eccConfig *bch.EncodingConfig = nil
if config.MaxCorrectableErrors > 0 {
printlnLvl(outputLevel, OutputSteps, "Setting up data ECC...")
chunkBitSize := util.Max(int(encodeChunkSize), int(encodeHeaderSize)) * int(bitsPerByte)
codeLength, err := bch.TotalBitsForConfig(chunkBitSize, int(config.MaxCorrectableErrors))
if err != nil {
return err
}
eccConfig, err = bch.CreateConfig(codeLength, int(config.MaxCorrectableErrors))
if err != nil {
return err
}
printlnLvl(outputLevel, OutputInfo, fmt.Sprintf("Using a %v. This has a ratio (errors : bits) of %2.2f%%.",
eccConfig, 100 * eccConfig.ECCRatio()))
bitsToWrite = int64(math.Ceil(float64(bitsToWrite) / float64(eccConfig.StorageBits))) * int64(eccConfig.ChecksumBits()) + bitsToWrite
printlnLvl(outputLevel, OutputSteps, "Actual bits to write (including ECC):", bitsToWrite)
}
if bitsToWrite > maxWritableBits {
return &InsufficientHidingSpotsError{AdditionalInfo:fmt.Sprintf("Since the number of bits to write is %d " +
"and the maximum possible with this configuration is %d, there is no way the input file will fit.", bitsToWrite, maxWritableBits)}
}
printlnLvl(outputLevel, OutputDebug, "Encoding header:", string(b[0:]))
if err = encodeChunk(config, eccConfig, info, &f, pixels, channelsPerPix, &b, int(encodeHeaderSize), outputLevel); err != nil {
switch err.(type) {
case *algos.EmptyPoolError:
return &InsufficientHidingSpotsError{InnerError:err}
default:
return err
}
}
printlnLvl(outputLevel, OutputSteps, "Writing file data...")
if outputLevel >= OutputDebug {
for _, v := range b {
fmt.Printf("%#08b\n", v)
}
}
for {
n, err := r.Read(b)
if n > 0 {
if err = encodeChunk(config, eccConfig, info, &f, pixels, channelsPerPix, &b, n, outputLevel); err != nil {
switch err.(type) {
case *algos.EmptyPoolError:
return &InsufficientHidingSpotsError{InnerError:err}
default:
return err
}
}
}
if err != nil {
if err != io.EOF {
printlnLvl(outputLevel, OutputSteps, fmt.Sprintf("An error occurred while reading the file '%v'.", config.FilePath))
return err
}
break
}
}
printlnLvl(outputLevel, OutputSteps, fmt.Sprintf("Writing the encoded image to '%v' now...", config.OutPath))
if err = writeImage(pixels, info, config.OutPath, outputLevel); err != nil {
printlnLvl(outputLevel, OutputSteps, "An error occurred while writing to the final image.")
return err
}
printlnLvl(outputLevel, OutputSteps, "All done! c:")
return nil
}
// Helper functions
func encodeChunk(config *HideConfig, eccConfig *bch.EncodingConfig, info imgInfo, pos *func() (int64, error), pixels *[]pixel, channelCount uint8, buf *[]byte, n int, outputLevel OutputLevel) error {
supportsAlpha := info.Format.supportsAlpha()
alphaChannel := info.Format.alphaChannel()
var writeBits []uint8
if eccConfig != nil {
dataBits := binmani.BytesToBits((*buf)[:n])
printlnLvl(outputLevel, OutputDebug, dataBits)
if eccConfig.StorageBits < n * int(bitsPerByte) {
panic("Provided with a mismatched bch.EncodingConfig for the data to be encoded!")
} else if eccConfig.StorageBits > n * int(bitsPerByte) {
padBits := make([]uint8, eccConfig.StorageBits - n * int(bitsPerByte))
*dataBits = append(*dataBits, padBits...)
}
encodedBits, err := bch.Encode(eccConfig, dataBits)
if err != nil {
return err
}
writeBits = encodedBits[:n * int(bitsPerByte) + eccConfig.ChecksumBits()]
printlnLvl(outputLevel, OutputDebug, writeBits)
} else {
writeBits = *binmani.BytesToBits((*buf)[:n])
}
n = len(writeBits)
for i := 0; i < n; i++ {
for {
addr, err := (*pos)()
if err != nil {
return err
}
p, c, b := bitAddrToPCB(addr, channelCount, config.MaxBitsPerChannel)
if outputLevel >= OutputDebug {
fmt.Printf("addr: %d, pixel: %d, channel: %d, bit: %d, RGBA: %v\n", addr, p, c, b, (*pixels)[p])
}
if supportsAlpha && (*pixels)[p][alphaChannel] <= 0 {
continue
}
if outputLevel >= OutputDebug {
fmt.Printf(" Writing %d...\n", writeBits[i])
fmt.Printf(" Channel before: %#016b - %v\n", (*pixels)[p][c], (*pixels)[p])
}
bitPos := b
if config.EncodeMsb {
bitPos = info.Format.BitsPerChannel - b - 1
}
(*pixels)[p][c] = binmani.WriteTo((*pixels)[p][c], bitPos, 1, uint16(writeBits[i]))
if outputLevel >= OutputDebug {
fmt.Printf(" Channel after: %#016b - %v\n", (*pixels)[p][c], (*pixels)[p])
}
break
}
}
return nil
}