-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
125 lines (114 loc) · 3.94 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/Watchdog0x/chunkHide/PNGChunkModifie"
)
func main() {
const version = "1.0.0"
var (
imagePath string
chunktype string
keyword string
text string
output string
validate bool
read bool
)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "By Watchdog0x Version %s \n\n", version)
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n", os.Args[0])
fmt.Fprintln(os.Stderr, "Options:")
fmt.Fprintln(os.Stderr, " -i [image.png]\t Path to the PNG image file.")
fmt.Fprintln(os.Stderr, " -t [tEXt]\t\t Specify chunk types:")
fmt.Fprintln(os.Stderr, "\t\t\t tEXt (text chunk)")
fmt.Fprintln(os.Stderr, "\t\t\t zTXt (compressed text chunk)")
fmt.Fprintln(os.Stderr, "\t\t\t PLTE (palette chunk)")
fmt.Fprintln(os.Stderr, " -o [output.png]\t Where to output the modified image. Default: output.png")
fmt.Fprintln(os.Stderr, " -v [image.png]\t Validate the image chunks.")
fmt.Fprintln(os.Stderr, " -r\t\t\t Read the chunk data. Requires -t option.")
fmt.Fprintln(os.Stderr, " -keyword\t\t Keyword for the chunk. Required with -t tEXt or -t zTXt.")
fmt.Fprintln(os.Stderr, " -text\t\t\t Text for the chunk. Required with -t tEXt, -t zTXt or -t PLTE.")
fmt.Println("\nExamples:")
fmt.Println(" -t tEXt -keyword mykey -text Hello -i input.png -o output.png")
fmt.Println(" -t zTXt -keyword mykey -text Hello -i input.png -o output.png")
fmt.Println(" -r -t tEXt -i input.png")
fmt.Println(" -v input.png")
}
flag.StringVar(&imagePath, "i", "", "Path to the PNG image file")
flag.StringVar(&output, "o", "output.png", "Output")
flag.StringVar(&chunktype, "t", "", "Chunk type")
flag.BoolVar(&read, "r", false, "Read the chunk data. Requires -t option.")
flag.BoolVar(&validate, "v", false, "Validate PNG. Example: -v image.png")
flag.StringVar(&keyword, "keyword", "", "Keyword for the chunk. Required with -t tEXt or -t zTXt.")
flag.StringVar(&text, "text", "", "Text for the chunk. Required with -t tEXt, -t zTXt or -t PLTE.")
flag.Parse()
if imagePath != "" {
chunks := strings.ToLower(chunktype)
file, err := os.Open(imagePath)
if err != nil {
fmt.Println("Error: Unable to open the file:", err)
return
}
defer file.Close()
if validate {
PNGChunkModifie.Validate(file)
} else if read {
if chunks != "" {
switch chunks {
case "text":
PNGChunkModifie.ReadData(file, []byte("tEXt"))
case "ztxt":
PNGChunkModifie.ReadData(file, []byte("zTXt"))
case "plte":
PNGChunkModifie.ReadData(file, []byte("PLTE"))
default:
fmt.Println("Error: Unsupported chunk type.")
os.Exit(2)
}
} else {
fmt.Println("Error: You need to specify a chunk type with -t.")
os.Exit(2)
}
} else if chunktype != "" {
switch chunks {
case "text":
if keyword != "" && text != "" {
imageData := PNGChunkModifie.Constructor(file, PNGChunkModifie.NewtEXt(keyword, text), []byte("IDAT"))
os.WriteFile(output, imageData, 0644)
} else {
fmt.Println("Error: You need to specify both -keyword and -text.")
os.Exit(2)
}
case "ztxt":
if keyword != "" && text != "" {
imageData := PNGChunkModifie.Constructor(file, PNGChunkModifie.NewZTXt(keyword, text), []byte("IDAT"))
os.WriteFile(output, imageData, 0644)
} else {
fmt.Println("Error: You need to specify both -keyword and -text.")
os.Exit(2)
}
case "plte":
if text != "" {
imageData := PNGChunkModifie.Constructor(file, PNGChunkModifie.NewPLTE(text), []byte("IDAT"))
os.WriteFile(output, imageData, 0644)
} else {
fmt.Println("Error: You need to specify -text.")
os.Exit(2)
}
default:
fmt.Println("Error: Unsupported chunk type.")
os.Exit(2)
}
} else {
fmt.Println("Error: You need to use -v, -r, or -t with the -i flag.")
os.Exit(2)
}
} else {
fmt.Println("Error: Please provide an image path using the -i flag.")
flag.Usage()
os.Exit(2)
}
}