-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpatchfile.go
198 lines (173 loc) · 3.37 KB
/
patchfile.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
)
var (
status int
)
var flags struct {
Patches []Patch
}
func main() {
parseFlags()
ek(patchFile(flag.Arg(0), flags.Patches))
os.Exit(status)
}
func parseFlags() {
var rules, files MultiFlag
flag.Var(&rules, "r", "add a rule")
flag.Var(&files, "i", "add a rule file")
flag.Usage = usage
flag.Parse()
if flag.NArg() < 1 {
usage()
}
for _, file := range files {
patches, err := readPatchFile(file)
if ek(err) {
continue
}
flags.Patches = append(flags.Patches, patches...)
}
for _, rule := range rules {
patch, err := makePatch(rule)
if ek(err) {
continue
}
flags.Patches = append(flags.Patches, patch)
}
}
func ek(err error) bool {
if err != nil {
fmt.Fprintln(os.Stderr, "patchfile:", err)
status |= 1
return true
}
return false
}
func usage() {
fmt.Fprintln(os.Stderr, "usage: patchfile [options] file")
flag.PrintDefaults()
os.Exit(2)
}
func readPatchFile(name string) ([]Patch, error) {
fd, err := os.Open(name)
if err != nil {
return nil, err
}
defer fd.Close()
var patches []Patch
scan := bufio.NewScanner(fd)
for scan.Scan() {
line := scan.Text()
patch, err := makePatch(line)
if ek(err) {
continue
}
patches = append(patches, patch)
}
return patches, nil
}
func makePatch(rule string) (patch Patch, err error) {
var (
file string
data []byte
)
start, end, off := -1, -1, -1
switch {
case strings.HasPrefix(rule, "file"):
n, _ := fmt.Sscanf(rule, "file %s %v %v %v", &file, &start, &end, &off)
if n < 1 {
err = fmt.Errorf("file rule %q does not have a filename", rule)
return
}
data, err = os.ReadFile(file)
if err != nil {
return
}
case strings.HasPrefix(rule, "data"):
toks := strings.Split(rule, " ")
for toks = toks[1:]; len(toks) > 0; {
var val int
n, _ := fmt.Sscanf(toks[0], "%v", &val)
toks = toks[1:]
if n != 1 {
break
}
if val == -1 {
break
}
data = append(data, byte(val))
}
switch {
case len(toks) >= 3:
fmt.Sscanf(toks[2], "%v", &off)
fallthrough
case len(toks) >= 2:
fmt.Sscanf(toks[1], "%v", &end)
fallthrough
case len(toks) >= 1:
fmt.Sscanf(toks[0], "%v", &start)
}
default:
return patch, fmt.Errorf("unknown rule %q", rule)
}
if start < 0 || start >= len(data) {
start = 0
}
if end < 0 || end >= len(data) {
end = len(data)
}
if end < start {
start, end = end, start
}
patch = Patch{
Data: data[start:end],
Off: off,
}
return
}
func patchFile(name string, patches []Patch) error {
var maxAddr int
for _, patch := range patches {
size := len(patch.Data)
if patch.Off < 0 || patch.Off == maxAddr {
maxAddr += size
} else if patch.Off+size > maxAddr {
maxAddr = patch.Off + size
}
}
data, err := os.ReadFile(name)
if err != nil {
data = make([]byte, maxAddr)
} else if len(data) < maxAddr {
data = append(data, make([]byte, maxAddr-len(data))...)
}
pos := 0
for _, patch := range patches {
if patch.Off < 0 {
copy(data[pos:], patch.Data)
pos += len(patch.Data)
} else {
copy(data[patch.Off:], patch.Data)
pos = patch.Off + len(patch.Data)
}
}
return os.WriteFile(name, data, 0644)
}
type Patch struct {
Off int
Data []byte
}
type MultiFlag []string
func (m *MultiFlag) Set(s string) error {
*m = append(*m, s)
return nil
}
func (m MultiFlag) String() string {
return fmt.Sprint([]string(m))
}