-
Notifications
You must be signed in to change notification settings - Fork 2
/
reverse.go
130 lines (116 loc) · 2.14 KB
/
reverse.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
package main
import (
"bufio"
"errors"
"fmt"
"io"
)
func HexxyReverse(r io.Reader, w io.Writer) error {
var (
cols int
octs int
char = make([]byte, 1)
)
if opts.Columns != -1 {
cols = opts.Columns
}
switch dumpType {
case dumpBinary:
octs = 8
case dumpCformat:
octs = 4
default:
octs = 2
}
if opts.Len != -1 {
if opts.Len < int64(cols) {
cols = int(opts.Len)
}
}
if octs < 1 {
octs = cols
}
// character count
c := int64(0)
rd := bufio.NewReader(r)
for {
// TODO this is causing issues with plain
line, err := rd.ReadBytes('\n')
n := len(line)
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
return fmt.Errorf("hexxy: %v", err)
}
if n == 0 {
return nil
}
if dumpType == dumpHex {
// line = line[9:48]
line = line[9 : len(line)-19]
n := len(line)
for i := 0; i <= n; {
// print(string(line[i : i+4]))
if rv, _ := hexDecode(char, line[i:i+octs]); rv != 0 {
w.Write(char)
}
i += 5
// time.Sleep(time.Millisecond * 500)
}
// for i := 0; n >= octs+1; {
// print(string(line[i : i+octs]))
// time.Sleep(time.Millisecond * 500)
// if rv, _ := hexDecode(char, line[i:i+octs]); rv != 0 {
// w.Write(char)
// i += 2
// n -= 2
// c++
// } else if rv == -1 {
// i++
// n--
// } else {
// // rv == -2
// i += 2
// n -= 2
// }
// }
} else if dumpType == dumpBinary {
for i := 0; n >= octs; {
if binaryDecode(char, line[i:i+octs]) != -1 {
i++
n--
continue
} else {
w.Write(char)
i += 8
n -= 8
c++
}
}
} else if dumpType == dumpPlain {
for i := 0; n >= octs; i++ {
if rv, _ := hexDecode(char, line[i:i+octs]); rv != 0 {
w.Write(char)
c++
}
n--
}
} else if dumpType == dumpCformat {
for i := 0; n >= octs; {
if rv, _ := hexDecode(char, line[i:i+octs]); rv == 0 {
w.Write(char)
i += 4
n -= 4
c++
} else if rv == -1 {
i++
n--
} else { // rv == -2
i += 2
n -= 2
}
}
}
if c == int64(cols) && cols > 0 {
return nil
}
}
}