-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolinktest.go
177 lines (156 loc) · 3.35 KB
/
golinktest.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
package main
import (
"encoding/binary"
"errors"
"fmt"
"io"
"os"
)
const (
magic = 0x56bb4200ff847612
)
type Info struct {
Size uint64
Magic uint64
}
func readInfo(input *os.File) (Info, error) {
i := Info{}
input.Seek(-int64(binary.Size(i)), io.SeekEnd)
err := binary.Read(input, binary.LittleEndian, &i)
if err != nil {
return i, err
}
return i, nil
}
func hasInfo() (bool, error) {
program, err := os.Open(os.Args[0])
if err != nil {
return false, err
}
defer program.Close()
i, err := readInfo(program)
if err != nil {
return false, err
}
return i.Magic == magic, nil
}
func create(program_filename string, data_filename string, output_filename string) error {
// open input files
program, err := os.Open(program_filename)
if err != nil {
return err
}
defer program.Close()
data, err := os.Open(data_filename)
if err != nil {
return err
}
defer data.Close()
// create output file
output, err := os.Create(output_filename)
if err != nil {
return err
}
defer output.Close()
// copy program file to output
_, err = io.Copy(output, program)
if err != nil {
return err
}
// append data to output
_, err = io.Copy(output, data)
if err != nil {
return err
}
// append size of data and magic ID to the output
fi, err := data.Stat()
if err != nil {
return err
}
i := Info{Size: uint64(fi.Size()), Magic: magic}
err = binary.Write(output, binary.LittleEndian, i)
if err != nil {
return err
}
fmt.Printf("data file %v with size %v appended to program file and written to %v\n", data_filename, fi.Size(), output_filename)
return nil
}
func extract(program_filename string, data_filename string) error {
// open program file
program, err := os.Open(program_filename)
if err != nil {
return err
}
defer program.Close()
// create output file
output, err := os.Create(data_filename)
if err != nil {
return err
}
defer output.Close()
// read size of data from the end of the file
i, err := readInfo(program)
if err != nil {
return err
}
if i.Magic != magic {
return errors.New("no added data detected")
}
// go to start of data and copy it to the output
program.Seek(int64(-(i.Size + uint64(binary.Size(i)))), io.SeekEnd)
io.CopyN(output, program, int64(i.Size))
if err != nil {
return err
}
fmt.Printf("data file %v with size %v extracted\n", data_filename, i.Size)
return nil
}
func usage(hasInfo bool) {
if hasInfo {
fmt.Printf("usage: %v datafile\n", os.Args[0])
fmt.Printf("extracts the linked data\n")
os.Exit(2)
} else {
fmt.Printf("usage: %v datafile outputfile\n", os.Args[0])
fmt.Printf("links the data from 'datafile' to the program and creates a new executable named 'outputfile'\n")
os.Exit(2)
}
}
func main() {
hasInfo, err := hasInfo()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if hasInfo {
// extract linked data
if len(os.Args) != 2 {
usage(hasInfo)
}
dataFilename := os.Args[1]
if dataFilename == "" {
usage(hasInfo)
}
err := extract(os.Args[0], dataFilename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
// link data
if len(os.Args) != 3 {
usage(hasInfo)
}
dataFilename := os.Args[1]
outputFilename := os.Args[2]
if dataFilename == "" || outputFilename == "" {
usage(hasInfo)
}
err := create(os.Args[0], dataFilename, outputFilename)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
os.Exit(0)
}