-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeyfunc.go
396 lines (357 loc) · 9.58 KB
/
keyfunc.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"fmt"
"io"
"os"
"strconv"
"github.com/nyaosorg/go-readline-ny"
"github.com/nyaosorg/go-readline-ny/simplehistory"
"github.com/hymkor/binview/internal/encoding"
"github.com/hymkor/binview/internal/large"
"github.com/hymkor/binview/internal/nonblock"
)
const (
_KEY_CTRL_A = "\x01"
_KEY_CTRL_B = "\x02"
_KEY_CTRL_E = "\x05"
_KEY_CTRL_F = "\x06"
_KEY_CTRL_L = "\x0C"
_KEY_CTRL_N = "\x0E"
_KEY_CTRL_P = "\x10"
_KEY_DOWN = "\x1B[B"
_KEY_ESC = "\x1B"
_KEY_LEFT = "\x1B[D"
_KEY_RIGHT = "\x1B[C"
_KEY_UP = "\x1B[A"
_KEY_F2 = "\x1B[OQ"
_KEY_DEL = "\x1B[3~"
_KEY_ALT_A = "\x1Ba"
_KEY_ALT_U = "\x1Bu"
_KEY_ALT_L = "\x1Bl"
_KEY_ALT_B = "\x1Bb"
)
// keyFuncNext moves the cursor to the the next 16-bytes block.
func keyFuncNext(this *Application) error {
if err := this.cursor.Skip(LINE_SIZE); err != nil {
if err != io.EOF {
return err
}
}
return nil
}
// keyFuncBackword move the cursor to the previous byte.
func keyFuncBackword(this *Application) error {
this.cursor.Prev()
return nil
}
// keyFuncPrevious moves the cursor the the previous 16-bytes block.
func keyFuncPrevious(this *Application) error {
this.cursor.Rewind(LINE_SIZE)
return nil
}
func keyFuncQuit(this *Application) error {
if yesNo(this.tty1, this.out, "Quit Sure ? [y/n]") {
io.WriteString(this.out, "\n")
return io.EOF
}
return nil
}
// keyFuncForward moves the cursor to the next one byte.
func keyFuncForward(this *Application) error {
this.cursor.Next()
return nil
}
// keyFuncGoBeginOfLine move the cursor the the top of the 16bytes-block.
func keyFuncGoBeginOfLine(this *Application) error {
n := this.cursor.Address() % LINE_SIZE
if n > 0 {
this.cursor.Rewind(n)
}
return nil
}
// keyFuncGoEndOfLine move the cursor to the end of the current 16 byte block.
func keyFuncGoEndOfLine(this *Application) error {
n := LINE_SIZE - this.cursor.Address()%LINE_SIZE - 1
if n > 0 {
this.cursor.Skip(n)
}
return nil
}
func keyFuncGoBeginOfFile(this *Application) error {
this.cursor = large.NewPointer(this.buffer)
this.window = large.NewPointer(this.buffer)
return nil
}
// keyFuncGoEndOfFile moves the cursor to the end of the file.
func keyFuncGoEndOfFile(this *Application) error {
this.cursor.GoEndOfFile()
return nil
}
// keyFuncPasteAfter inserts the top byte of clipboard after the cursor.
func keyFuncPasteAfter(this *Application) error {
if this.clipBoard.Len() <= 0 {
return nil
}
newByte := this.clipBoard.Pop()
orgAddress := this.cursor.Address() + 1
orgDirty := this.dirty
undo := func(app *Application) {
p := large.NewPointerAt(orgAddress, app.buffer)
p.Remove()
this.dirty = orgDirty
}
this.cursor.Append(newByte)
this.undoFuncs = append(this.undoFuncs, undo)
this.dirty = true
return nil
}
// keyFuncPasteBefore inserts the top of the clipboard at the cursor.
func keyFuncPasteBefore(this *Application) error {
if this.clipBoard.Len() <= 0 {
return nil
}
newByte := this.clipBoard.Pop()
orgAddress := this.cursor.Address()
orgDirty := this.dirty
undo := func(app *Application) {
p := large.NewPointerAt(orgAddress, app.buffer)
p.Remove()
this.dirty = orgDirty
}
this.cursor.Insert(newByte)
this.undoFuncs = append(this.undoFuncs, undo)
this.dirty = true
return nil
}
// keyFuncRemoveByte removes the byte where cursor exists.
func keyFuncRemoveByte(this *Application) error {
orgValue := this.cursor.Value()
address := this.cursor.Address()
orgDirty := this.dirty
undo := func(app *Application) {
p := large.NewPointerAt(address, app.buffer)
p.Insert(orgValue)
app.dirty = orgDirty
}
this.undoFuncs = append(this.undoFuncs, undo)
this.dirty = true
this.clipBoard.Push(this.cursor.Value())
switch this.cursor.Remove() {
case large.RemoveAll:
return io.EOF
case large.RemoveRefresh:
this.window = this.cursor
return nil
default:
return nil
}
}
var overWritten = map[string]struct{}{}
func getlineOr(out io.Writer, prompt string, defaultString string, history readline.IHistory, f func() bool) (string, error) {
worker := nonblock.New(func() (string, error) {
return getline(out, prompt, defaultString, history)
})
result, err := worker.GetOr(f)
worker.Close()
return result, err
}
var fnameHistory = simplehistory.New()
func writeFile(buffer *large.Buffer, tty1 Tty, out io.Writer, fname string) (string, error) {
var err error
fname, err = getlineOr(out, "write to>", fname, fnameHistory, func() bool { return buffer.Fetch() == nil })
if err != nil {
return "", err
}
fd, err := os.OpenFile(fname, os.O_WRONLY|os.O_EXCL|os.O_CREATE, 0666)
if os.IsExist(err) {
if _, ok := overWritten[fname]; ok {
os.Remove(fname)
} else {
if !yesNo(tty1, out, "Overwrite as \""+fname+"\" [y/n] ?") {
return "", err
}
backupName := fname + "~"
os.Remove(backupName)
os.Rename(fname, backupName)
overWritten[fname] = struct{}{}
}
fd, err = os.OpenFile(fname, os.O_WRONLY|os.O_EXCL|os.O_CREATE, 0666)
}
if err != nil {
return "", err
}
buffer.WriteTo(fd)
fnameHistory.Add(fname)
return fname, fd.Close()
}
func keyFuncWriteFile(this *Application) error {
newfname, err := writeFile(this.buffer, this.tty1, this.out, this.savePath)
if err != nil {
this.message = err.Error()
} else {
this.dirty = false
this.savePath = newfname
}
return nil
}
var byteHistory = simplehistory.New()
func keyFuncReplaceByte(this *Application) error {
bytes, err := getlineOr(this.out, "replace>",
fmt.Sprintf("0x%02X", this.cursor.Value()),
byteHistory,
func() bool { return this.buffer.Fetch() == nil })
if err != nil {
this.message = err.Error()
return nil
}
if n, err := strconv.ParseUint(bytes, 0, 8); err == nil {
address := this.cursor.Address()
orgValue := this.cursor.Value()
orgDirty := this.dirty
undo := func(app *Application) {
p := large.NewPointerAt(address, app.buffer)
p.SetValue(orgValue)
app.dirty = orgDirty
}
this.undoFuncs = append(this.undoFuncs, undo)
this.cursor.SetValue(byte(n))
this.dirty = true
byteHistory.Add(bytes)
} else {
this.message = err.Error()
}
return nil
}
func keyFuncRepaint(this *Application) error {
this.cache = map[int]string{}
return nil
}
func gotoAddress(app *Application, address int64) error {
prevousAddress := app.cursor.Address()
if address > prevousAddress {
app.cursor.Skip(address - prevousAddress)
} else if address < prevousAddress {
app.cursor.Rewind(prevousAddress - address)
}
return nil
}
var addressHistory = simplehistory.New()
func keyFuncGoTo(app *Application) error {
addressStr, err := getlineOr(app.out, "Goto Offset>", "0x", addressHistory, func() bool {
return app.buffer.Fetch() == nil
})
if err != nil {
app.message = err.Error()
return nil
}
address, err := strconv.ParseInt(addressStr, 0, 64)
if err != nil {
app.message = err.Error()
return nil
}
addressHistory.Add(addressStr)
return gotoAddress(app, address)
}
func keyFuncDbcsMode(app *Application) error {
app.encoding = encoding.DBCSEncoding{}
return nil
}
func keyFuncUtf8Mode(app *Application) error {
app.encoding = encoding.UTF8Encoding{}
return nil
}
func keyFuncUtf16LeMode(app *Application) error {
app.encoding = encoding.UTF16LE()
return nil
}
func keyFuncUtf16BeMode(app *Application) error {
app.encoding = encoding.UTF16BE()
return nil
}
var expHistory = simplehistory.New()
func readExpression(app *Application, prompt string) (string, error) {
exp, err := getlineOr(app.out, prompt, "0x00", expHistory, func() bool { return app.buffer.Fetch() == nil })
if err != nil {
return "", err
}
expHistory.Add(exp)
return exp, err
}
func keyFuncInsertExp(app *Application) error {
exp, err := readExpression(app, "insert>")
if err != nil {
app.message = err.Error()
return nil
}
err = app.InsertExp(exp)
if err != nil {
app.message = err.Error()
}
return nil
}
func keyFuncAppendExp(app *Application) error {
exp, err := readExpression(app, "append>")
if err != nil {
app.message = err.Error()
return nil
}
err = app.AppendExp(exp)
if err != nil {
app.message = err.Error()
}
return nil
}
func keyFuncUndo(app *Application) error {
if len(app.undoFuncs) <= 0 {
return nil
}
addressSave := app.cursor.Address()
undoFunc1 := app.undoFuncs[len(app.undoFuncs)-1]
app.undoFuncs = app.undoFuncs[:len(app.undoFuncs)-1]
undoFunc1(app)
app.cursor = large.NewPointer(app.buffer)
app.window = app.cursor.Clone()
app.cursor.Skip(addressSave)
return nil
}
var jumpTable = map[string]func(this *Application) error{
"u": keyFuncUndo,
"i": keyFuncInsertExp,
"a": keyFuncAppendExp,
_KEY_ALT_A: keyFuncDbcsMode,
_KEY_ALT_U: keyFuncUtf8Mode,
_KEY_ALT_L: keyFuncUtf16LeMode,
_KEY_ALT_B: keyFuncUtf16BeMode,
"&": keyFuncGoTo,
"q": keyFuncQuit,
_KEY_ESC: keyFuncQuit,
"j": keyFuncNext,
_KEY_DOWN: keyFuncNext,
_KEY_CTRL_N: keyFuncNext,
"h": keyFuncBackword,
"\b": keyFuncBackword,
_KEY_LEFT: keyFuncBackword,
_KEY_CTRL_B: keyFuncBackword,
"k": keyFuncPrevious,
_KEY_UP: keyFuncPrevious,
_KEY_CTRL_P: keyFuncPrevious,
"l": keyFuncForward,
" ": keyFuncForward,
_KEY_RIGHT: keyFuncForward,
_KEY_CTRL_F: keyFuncForward,
"0": keyFuncGoBeginOfLine,
"^": keyFuncGoBeginOfLine,
_KEY_CTRL_A: keyFuncGoBeginOfLine,
"$": keyFuncGoEndOfLine,
_KEY_CTRL_E: keyFuncGoEndOfLine,
"<": keyFuncGoBeginOfFile,
">": keyFuncGoEndOfFile,
"G": keyFuncGoEndOfFile,
"p": keyFuncPasteAfter,
"P": keyFuncPasteBefore,
"x": keyFuncRemoveByte,
_KEY_DEL: keyFuncRemoveByte,
"w": keyFuncWriteFile,
"r": keyFuncReplaceByte,
_KEY_CTRL_L: keyFuncRepaint,
}