Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitarPetrov committed Mar 1, 2020
1 parent b10e76e commit b48f4b8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ If multiple `Carrier` files are provided, the `Data` file will be split in piece
| ---------------------------------------------|--------------------------------------------|--------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|
| <img src="examples/street.jpeg" width="500"> | <img src="examples/lake.jpeg" width="500"> | <img src="examples/video.gif" width="500"> | <img src="examples/test_multi_carrier_decode1.jpeg" width="500"> | <img src="examples/test_multi_carrier_decode2.jpeg" width="500"> |

The `Result1` file contains a portion of the `Data` file hidden in it as well as `Result2` contains the rest. As always fully transparent.
The `Result1` file contains one half of the `Data` file hidden in it and `Result2` the other. As always fully transparent.

## Install
```
Expand Down
12 changes: 1 addition & 11 deletions steg/steg_decode.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package steg

import (
"bytes"
"encoding/binary"
"fmt"
"github.com/DimitarPetrov/stegify/bits"
"image"
"io"
"io/ioutil"
"os"
)

Expand Down Expand Up @@ -61,17 +59,9 @@ func Decode(carrier io.Reader, result io.Writer) error {
//NOTE: The order of the carriers MUST be the same as the one when encoding.
func MultiCarrierDecode(carriers []io.Reader, result io.Writer) error {
for i := 0; i < len(carriers); i++ {
var chunkResult bytes.Buffer
if err := Decode(carriers[i], &chunkResult); err != nil {
if err := Decode(carriers[i], result); err != nil {
return fmt.Errorf("error decoding chunk with index %d: %v", i, err)
}
chunkBytes, err := ioutil.ReadAll(&chunkResult)
if err != nil {
return err
}
if _, err := result.Write(chunkBytes); err != nil {
return err
}
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion steg/steg_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func MultiCarrierEncode(carriers []io.Reader, data io.Reader, results []io.Write
chunksCount := 0
for i := 0; i < len(dataBytes) && chunksCount < len(carriers); i += chunkSize {
chunksCount++
if i+chunkSize >= len(dataBytes) || chunksCount == len(carriers){ // last iteration
if i+chunkSize >= len(dataBytes) || chunksCount == len(carriers) { // last iteration
dataChunks = append(dataChunks, bytes.NewReader(dataBytes[i:]))
}
dataChunks = append(dataChunks, bytes.NewReader(dataBytes[i:i+chunkSize]))
Expand Down
4 changes: 2 additions & 2 deletions steg/steg_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,15 @@ func TestMultiCarrierEncodeShouldReturnErrorWhenDataFileTooLarge(t *testing.T) {

var result1 bytes.Buffer
var result2 bytes.Buffer
err = steg.MultiCarrierEncode([]io.Reader{carrier1,carrier2}, data, []io.Writer{&result1, &result2})
err = steg.MultiCarrierEncode([]io.Reader{carrier1, carrier2}, data, []io.Writer{&result1, &result2})
if err == nil {
t.FailNow()
}
t.Log(err)
}

func TestMultiCarrierEncodeByFileNamesShouldReturnErrorWhenDataFileTooLarge(t *testing.T) {
err := steg.MultiCarrierEncodeByFileNames([]string{"../examples/lake.jpeg","../examples/street.jpeg"}, "../examples/test_decode.jpeg", []string{"result1", "result2"})
err := steg.MultiCarrierEncodeByFileNames([]string{"../examples/lake.jpeg", "../examples/street.jpeg"}, "../examples/test_decode.jpeg", []string{"result1", "result2"})
if err == nil {
os.Remove("result1")
os.Remove("result2")
Expand Down
45 changes: 21 additions & 24 deletions stegify.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ func (sf *sliceFlag) Set(value string) error {
}

var carrierFilesSlice sliceFlag
var carrierFiles = flag.String("carriers", "", "carrier files in which the data is encoded (separated by space and surrounded by quotes)")
var dataFile = flag.String("data", "", "data file which is being encoded in carrier")
var carrierFiles = flag.String("carriers", "", "carrier files in which the data is encoded (separated by space)")
var dataFile = flag.String("data", "", "data file which is being encoded in the carrier")
var resultFilesSlice sliceFlag
var resultFiles = flag.String("results", "", "names of the result files (separated by space and surrounded by quotes)")
var resultFiles = flag.String("results", "", "names of the result files (separated by space)")

func init() {
flag.StringVar(carrierFiles, "c", "", "carrier files in which the data is encoded (separated by space surrounded by quotes, shorthand for --carriers)")
flag.StringVar(carrierFiles, "c", "", "carrier files in which the data is encoded (separated by space, shorthand for --carriers)")
flag.Var(&carrierFilesSlice, "carrier", "carrier file in which the data is encoded (could be used multiple times for multiple carriers)")
flag.StringVar(dataFile, "d", "", "data file which is being encoded in carrier (shorthand for --data)")
flag.StringVar(dataFile, "d", "", "data file which is being encoded in the carrier (shorthand for --data)")
flag.Var(&resultFilesSlice, "result", "name of the result file (could be used multiple times for multiple result file names)")
flag.StringVar(resultFiles, "r", "", "names of the result files (separated by space and surrounded by quotes, shorthand for --results)")
flag.StringVar(resultFiles, "r", "", "names of the result files (separated by space, shorthand for --results)")

flag.Usage = func() {
fmt.Fprintln(os.Stdout, "Usage: stegify [encode/decode] [flags...]")
flag.PrintDefaults()
fmt.Fprintln(os.Stdout, `NOTE: When multiple carriers are provided with different kinds of flags, the names provided through "carrier" flag are taken first and with "carriers"/"c" flags second. Same goes for the result/results flag.`)
fmt.Fprintln(os.Stdout, `NOTE: When multiple carriers are provided with different kinds of flags, the names provided through "carrier" flag are taken first and with "carriers"/"c" flags second. Same goes for the "result"/"results" flags.`)
fmt.Fprintln(os.Stdout, `NOTE: When no results are provided a default values will be used for the names of the results.`)
}
}
Expand All @@ -50,34 +50,31 @@ func main() {
carriers := parseCarriers()
results := parseResults()

if len(results) == 0 {
if operation == encode {
switch operation {
case encode:
if len(results) == 0 { // if no results provided use defaults
for i := range carriers {
results = append(results, fmt.Sprintf("result%d", i))
}
} else {
results = append(results, "result")
}
}

if len(results) != len(carriers) && operation == encode {
fmt.Fprintln(os.Stderr, "Carrier and result files count must be equal when encoding.")
os.Exit(1)
}

if (dataFile == nil || *dataFile == "") && operation == encode {
fmt.Fprintln(os.Stderr, "Data file must be specified. Use stegify --help for more information.")
os.Exit(1)
}
if len(results) != len(carriers) {
fmt.Fprintln(os.Stderr, "Carrier and result files count must be equal when encoding.")
os.Exit(1)
}
if dataFile == nil || *dataFile == "" {
fmt.Fprintln(os.Stderr, "Data file must be specified. Use stegify --help for more information.")
os.Exit(1)
}

switch operation {
case encode:
err := steg.MultiCarrierEncodeByFileNames(carriers, *dataFile, results)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
case decode:
if len(results) == 0 { // if no result provided use default
results = append(results, "result")
}
if len(results) != 1 {
fmt.Fprintln(os.Stderr, "Only one result file expected.")
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions stegify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ func TestDecode(t *testing.T) {
},
{
name: "Decode from multiple carriers using --cariers flag",
args: []string{"decode", "--carriers", "examples/test_multi_carrier_decode1.jpeg examples/test_multi_carrier_decode2.jpeg", "--result", "result1.mp4"},
args: []string{"decode", "--carriers", "examples/test_multi_carrier_decode1.jpeg examples/test_multi_carrier_decode2.jpeg", "--result", "result2.mp4"},
expected: "examples/video.mp4",
result: "result1.mp4",
result: "result2.mp4",
},
{
name: "Decode without result flag should add default",
Expand Down

0 comments on commit b48f4b8

Please sign in to comment.