-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
439 lines (387 loc) · 10.5 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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"unicode"
"unicode/utf8"
"github.com/mattn/go-colorable"
"github.com/mattn/go-runewidth"
"github.com/mattn/go-tty"
"github.com/nyaosorg/go-readline-ny"
"github.com/hymkor/binview/internal/argf"
"github.com/hymkor/binview/internal/encoding"
"github.com/hymkor/binview/internal/large"
"github.com/hymkor/binview/internal/nonblock"
)
const LINE_SIZE = 16
const (
_ANSI_CURSOR_OFF = "\x1B[?25l"
_ANSI_CURSOR_ON = "\x1B[?25h"
_ANSI_YELLOW = "\x1B[0;33;1m"
_ANSI_RESET = "\x1B[0m"
_ANSI_UNDERLINE_ON = "\x1B[4m"
_ANSI_UNDERLINE_OFF = "\x1B[24m"
_ANSI_ERASE_LINE = "\x1B[0K"
_ANSI_ERASE_SCRN_AFTER = "\x1B[0J"
_CURSOR_COLOR_ON = "\x1B[37;40;1;7m"
_CURSOR_COLOR_OFF = "\x1B[27;22m"
_CELL1_COLOR_ON = "\x1B[37;40;22m"
_CELL1_COLOR_OFF = ""
_CELL2_COLOR_ON = "\x1B[37;40;1m"
_CELL2_COLOR_OFF = "\x1B[22m"
)
const (
// for Line feed
_ARROW_POINTING_DOWNWARDS_THEN_CURVING_LEFTWARDS = '\u2936'
_DOWNWARDS_ARROW_WITH_CORNER_LEFTWARDS = '\u21B5'
_DOWNWARDS_ARROW_WITH_TIP_LEFTWARDS = '\u21B2'
_RETURN_SYMBOL = '\u23CE'
_SYMBOL_FOR_NEWLINE = '\u2424'
_SYMBOL_FOR_LINE_FEED = '\u240A'
_DOWNWARDS_ARROW = '\u2193' // wide
_HALFWIDTH_DOWNWARDS_ARROW = '\uFFEC'
// for carriage return
_SYMBOL_FOR_CARRIAGE_RETURN = '\u240D' // CR
_LEFTWARDS_ARROW = '\u2190' // wide
_HALFWIDTH_LEFTWARDS_ARROW = '\uFFE9' // <-
// for tab
_SYMBOL_FOR_HORIZONTAL_TABULATION = '\u2409' // HT
_RIGHTWARDS_ARROW_TO_BAR = '\u21E5' // ->|
_RIGHTWARDS_TRIANGLE_HEADED_ARROW_TO_BAR = '\u2B72' // ->|
)
var version string = "snapshot"
// See. en.wikipedia.org/wiki/Unicode_control_characters#Control_pictures
func makeHexPart(pointer *large.Pointer, cursorAddress int64, out *strings.Builder) bool {
fmt.Fprintf(out, "%s%08X%s ", _CELL2_COLOR_ON, pointer.Address(), _CELL2_COLOR_OFF)
var fieldSeperator string
for i := 0; i < LINE_SIZE; i++ {
var on, off string
if pointer.Address() == cursorAddress {
on = _CURSOR_COLOR_ON
off = _CURSOR_COLOR_OFF
} else if ((i >> 2) & 1) == 0 {
on = _CELL1_COLOR_ON
off = _CELL1_COLOR_OFF
} else {
on = _CELL2_COLOR_ON
off = _CELL2_COLOR_OFF
}
fmt.Fprintf(out, "%s%s%02X%s", fieldSeperator, on, pointer.Value(), off)
if err := pointer.Next(); err != nil {
for ; i < LINE_SIZE-1; i++ {
out.WriteString(" ")
}
return false
}
fieldSeperator = " "
}
return true
}
var dontview = map[rune]rune{
'\u000a': _HALFWIDTH_DOWNWARDS_ARROW,
'\u000d': _HALFWIDTH_LEFTWARDS_ARROW,
'\t': _RIGHTWARDS_ARROW_TO_BAR,
'\u202e': '.', // Right-to-Left override
'\u202d': '.', // Left-to-Right override
'\u202c': '.', // Pop Directional Formatting
}
func makeAsciiPart(enc encoding.Encoding, pointer *large.Pointer, cursorAddress int64, out *strings.Builder) bool {
for i := 0; i < LINE_SIZE; {
var c rune
startAddress := pointer.Address()
b := pointer.Value()
var runeBuffer [utf8.UTFMax]byte
savePointer := pointer.Clone()
length := enc.Count(b, pointer.Address())
runeBuffer[0] = b
readCount := 1
for j := 1; j < length && pointer.Next() == nil; j++ {
runeBuffer[j] = pointer.Value()
readCount++
}
c = enc.Decode(runeBuffer[:readCount])
if c == utf8.RuneError {
c = '.'
length = 1
pointer = savePointer
}
if _c, ok := dontview[c]; ok {
c = _c
} else if unicode.IsControl(c) {
c = '.'
}
if startAddress <= cursorAddress && cursorAddress <= pointer.Address() {
out.WriteString(_CURSOR_COLOR_ON)
out.WriteRune(c)
out.WriteString(_CURSOR_COLOR_OFF)
} else {
out.WriteString(_CELL1_COLOR_ON)
out.WriteRune(c)
out.WriteString(_CELL1_COLOR_OFF)
}
if length == 3 {
out.WriteByte(' ')
} else if length == 4 {
out.WriteString(" ")
}
i += length
if pointer.Next() != nil {
return false
}
}
return true
}
func makeLineImage(enc encoding.Encoding, pointer *large.Pointer, cursorAddress int64) (string, bool) {
var out strings.Builder
off := ""
if p := pointer.Address(); p <= cursorAddress && cursorAddress < p+LINE_SIZE {
out.WriteString(_ANSI_UNDERLINE_ON)
off = _ANSI_UNDERLINE_OFF
}
asciiPointer := *pointer
hasNextLine := makeHexPart(pointer, cursorAddress, &out)
out.WriteByte(' ')
makeAsciiPart(enc, &asciiPointer, cursorAddress, &out)
out.WriteString(_ANSI_ERASE_LINE)
out.WriteString(off)
return out.String(), hasNextLine
}
func (app *Application) View() (int, error) {
h := app.screenHeight - 1
out := app.out
count := 0
cursor := app.window.Clone()
cursorAddress := app.cursor.Address()
for {
line, cont := makeLineImage(app.encoding, cursor, cursorAddress)
if f := app.cache[count]; f != line {
io.WriteString(out, line)
app.cache[count] = line
}
if !cont || count+1 >= h {
return count, nil
}
count++
io.WriteString(out, "\r\n") // "\r" is for Linux and go-tty
}
}
type Application struct {
tty1 *tty.TTY
in io.Reader
out io.Writer
screenWidth int
screenHeight int
cursor *large.Pointer
window *large.Pointer
buffer *large.Buffer
clipBoard *Clip
dirty bool
savePath string
message string
cache map[int]string
encoding encoding.Encoding
undoFuncs []func(app *Application)
}
func (app *Application) dataHeight() int {
return app.screenHeight - 1
}
func detectEncoding(p *large.Pointer) encoding.Encoding {
p = p.Clone()
byte1 := p.Value()
if p.Next() == nil {
byte2 := p.Value()
if byte1 == 0xFF && byte2 == 0xFE {
return encoding.UTF16LE()
}
if byte1 == 0xFE && byte2 == 0xFF {
return encoding.UTF16BE()
}
}
return encoding.UTF8Encoding{}
}
func NewApplication(in io.Reader, out io.Writer, defaultName string) (*Application, error) {
this := &Application{
savePath: defaultName,
in: in,
out: out,
buffer: large.NewBuffer(in),
clipBoard: NewClip(),
}
this.window = large.NewPointer(this.buffer)
if this.window == nil {
return nil, io.EOF
}
this.cursor = large.NewPointer(this.buffer)
if this.cursor == nil {
return nil, io.EOF
}
this.encoding = detectEncoding(this.cursor)
var err error
this.tty1, err = tty.Open()
if err != nil {
return nil, err
}
io.WriteString(out, _ANSI_CURSOR_OFF)
return this, nil
}
func (app *Application) Close() error {
io.WriteString(app.out, _ANSI_CURSOR_ON)
io.WriteString(app.out, _ANSI_RESET)
if app.tty1 != nil {
app.tty1.Close()
}
return nil
}
var unicodeName = map[rune]string{
'\uFEFF': "ByteOrderMark",
'\uFFFE': "Reverted ByteOrderMark",
'\u200D': "ZeroWidthJoin",
'\u202E': "RightToLeftOverride",
'\u202D': "LeftToRightOverride",
}
func (app *Application) printDefaultStatusBar() {
io.WriteString(app.out, _ANSI_YELLOW)
if app.dirty {
io.WriteString(app.out, "*")
} else {
io.WriteString(app.out, " ")
}
fmt.Fprintf(app.out, "[%s]", app.encoding.ModeString())
fmt.Fprintf(app.out, "%4[1]d='\\x%02[1]X'", app.cursor.Value())
theRune, thePosInRune, theLenOfRune := app.encoding.RuneOver(app.cursor.Clone())
if theRune != utf8.RuneError {
fmt.Fprintf(app.out, "(%d/%d:U+%04X",
thePosInRune+1,
theLenOfRune,
theRune)
if name, ok := unicodeName[theRune]; ok {
fmt.Fprintf(app.out, ":%s", name)
}
app.out.Write([]byte{')'})
} else {
fmt.Fprintf(app.out, "(bin:'\\x%02X')", app.cursor.Value())
}
fmt.Fprintf(app.out,
" @ %[1]d=0x%[1]X/%[2]d=0x%[2]X",
app.cursor.Address(),
app.buffer.Len())
io.WriteString(app.out, _ANSI_ERASE_SCRN_AFTER)
io.WriteString(app.out, _ANSI_RESET)
}
func (app *Application) shiftWindowToSeeCursorLine() {
if app.cursor.Address() < app.window.Address() {
app.window = app.cursor.Clone()
if n := app.window.Address() % LINE_SIZE; n > 0 {
app.window.Rewind(n)
}
} else if app.cursor.Address() >= app.window.Address()+LINE_SIZE*int64(app.dataHeight()) {
app.window = app.cursor.Clone()
app.window.Rewind(
app.window.Address()%LINE_SIZE +
int64(LINE_SIZE*(app.dataHeight()-1)))
}
}
func mains(args []string) error {
disable := colorable.EnableColorsStdout(nil)
if disable != nil {
defer disable()
}
out := colorable.NewColorableStdout()
fmt.Fprintf(out, "binview %s-%s-%s by %s\n",
version, runtime.GOOS, runtime.GOARCH, runtime.Version())
in, err := argf.New(args)
if err != nil {
return err
}
defer in.Close()
savePath := "output.new"
if len(args) > 0 {
savePath, err = filepath.Abs(args[0])
if err != nil {
return err
}
}
app, err := NewApplication(in, out, savePath)
if err != nil {
return err
}
defer app.Close()
keyWorker := nonblock.New(func() (string, error) { return readline.GetKey(app.tty1) })
defer keyWorker.Close()
var lastWidth, lastHeight int
for {
app.screenWidth, app.screenHeight, err = app.tty1.Size()
if err != nil {
return err
}
if lastWidth != app.screenWidth || lastHeight != app.screenHeight {
app.cache = map[int]string{}
lastWidth = app.screenWidth
lastHeight = app.screenHeight
io.WriteString(app.out, _ANSI_CURSOR_OFF)
}
lf, err := app.View()
if err != nil {
return err
}
if app.buffer.Len() <= 0 {
return nil
}
io.WriteString(app.out, "\r\n") // \r is for Linux & go-tty
lf++
if app.message != "" {
io.WriteString(app.out, _ANSI_YELLOW)
io.WriteString(app.out, runewidth.Truncate(app.message, app.screenWidth-1, ""))
io.WriteString(app.out, _ANSI_ERASE_SCRN_AFTER)
io.WriteString(app.out, _ANSI_RESET)
} else {
app.printDefaultStatusBar()
}
const interval = 10
displayUpdateTime := time.Now().Add(time.Second / interval)
ch, err := keyWorker.GetOr(func() bool {
err := app.buffer.Fetch()
if err != nil && err != io.EOF {
return false
}
if app.message != "" {
return err == nil
}
if err == io.EOF || time.Now().After(displayUpdateTime) {
app.out.Write([]byte{'\r'})
app.printDefaultStatusBar()
displayUpdateTime = time.Now().Add(time.Second / interval)
}
return err == nil
})
if err != nil {
return err
}
app.message = ""
if hander, ok := jumpTable[ch]; ok {
if err := hander(app); err != nil {
return err
}
}
if app.buffer.Len() <= 0 {
return nil
}
app.shiftWindowToSeeCursorLine()
if lf > 0 {
fmt.Fprintf(app.out, "\r\x1B[%dA", lf)
} else {
io.WriteString(app.out, "\r")
}
}
}
func main() {
if err := mains(os.Args[1:]); err != nil && err != io.EOF {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}