-
Notifications
You must be signed in to change notification settings - Fork 34
/
cursor.go
265 lines (243 loc) Β· 6.27 KB
/
cursor.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
package main
import (
"fmt"
"math"
"time"
"github.com/nsf/termbox-go"
)
type CursorMode int
type TimeSinceEpochUnit int
const MAX_INTEGER_WIDTH = 8
const MIN_INTEGER_WIDTH = 1
const MAX_FLOATING_POINT_WIDTH = 8
const MIN_FLOATING_POINT_WIDTH = 4
const MAX_BIT_PATTERN_WIDTH = 2
const MIN_BIT_PATTERN_WIDTH = 1
const (
StringMode CursorMode = iota + 1
BitPatternMode
IntegerMode
FloatingPointMode
)
const (
SecondsSinceEpoch TimeSinceEpochUnit = iota + 1
DaysSinceEpoch
MilliSecondsSinceEpoch
)
type ByteRange struct {
pos int
length int
}
type Cursor struct {
pos int
max_pos int
int_length int
fp_length int
bit_length int
mode CursorMode
unsigned bool
big_endian bool
hex_mode bool
epoch_time time.Time
epoch_unit TimeSinceEpochUnit
}
func (cursor *Cursor) c_type() string {
if cursor.mode == IntegerMode {
if cursor.unsigned {
return fmt.Sprintf("uint%d_t", cursor.int_length*8)
} else {
return fmt.Sprintf(" int%d_t", cursor.int_length*8)
}
} else if cursor.mode == FloatingPointMode {
if cursor.fp_length == 4 {
return " float"
} else if cursor.fp_length == 8 {
return " double"
}
} else if cursor.mode == BitPatternMode {
return " char"
} else if cursor.mode == StringMode {
return " char *"
}
return ""
}
func (cursor *Cursor) length() int {
if cursor.mode == IntegerMode {
return cursor.int_length
}
if cursor.mode == FloatingPointMode {
return cursor.fp_length
}
if cursor.mode == BitPatternMode {
return cursor.bit_length
}
return 1
}
func (cursor *Cursor) maximumLength() int {
if cursor.mode == IntegerMode {
return MAX_INTEGER_WIDTH
}
if cursor.mode == FloatingPointMode {
return MAX_FLOATING_POINT_WIDTH
}
if cursor.mode == BitPatternMode {
return MAX_BIT_PATTERN_WIDTH
}
return 1
}
func (cursor *Cursor) minimumLength() int {
if cursor.mode == IntegerMode {
return MIN_INTEGER_WIDTH
}
if cursor.mode == FloatingPointMode {
return MIN_FLOATING_POINT_WIDTH
}
if cursor.mode == BitPatternMode {
return MIN_BIT_PATTERN_WIDTH
}
return 1
}
func (cursor *Cursor) grow() {
if cursor.length() < cursor.maximumLength() {
if cursor.mode == IntegerMode {
cursor.int_length *= 2
} else if cursor.mode == FloatingPointMode {
cursor.fp_length *= 2
} else if cursor.mode == BitPatternMode {
cursor.bit_length *= 2
}
}
}
func (cursor *Cursor) shrink() {
if cursor.length() > cursor.minimumLength() {
if cursor.mode == IntegerMode {
cursor.int_length /= 2
} else if cursor.mode == FloatingPointMode {
cursor.fp_length /= 2
} else if cursor.mode == BitPatternMode {
cursor.bit_length /= 2
}
}
}
func (cursor *Cursor) color(style Style) termbox.Attribute {
if cursor.mode == IntegerMode {
return style.int_cursor_hex_bg
}
if cursor.mode == FloatingPointMode {
return style.fp_cursor_hex_bg
}
if cursor.mode == BitPatternMode {
return style.bit_cursor_hex_bg
}
return style.text_cursor_hex_bg
}
func (cursor *Cursor) highlightRange(data []byte) ByteRange {
var hilite ByteRange
if cursor.mode != StringMode || !isPrintable(data[cursor.pos]) {
return hilite
}
hilite.pos = cursor.pos
for ; hilite.pos > 0 && isPrintable(data[hilite.pos-1]); hilite.pos-- {
}
for ; hilite.pos+hilite.length < len(data) && isPrintable(data[hilite.pos+hilite.length]); hilite.length++ {
}
return hilite
}
func (cursor *Cursor) interpretBytesAsInteger(data []byte) uint64 {
var integer uint64
if cursor.big_endian {
for i := 0; i < len(data); i++ {
integer = (integer * 256) + uint64(data[i])
}
} else {
for i := len(data) - 1; i >= 0; i-- {
integer = (integer * 256) + uint64(data[i])
}
}
return integer
}
func (cursor *Cursor) formatBytesAsNumber(data []byte) string {
integer := cursor.interpretBytesAsInteger(data)
if cursor.mode == IntegerMode {
if cursor.int_length == 1 {
if cursor.unsigned {
return fmt.Sprintf("%d", uint8(integer))
} else {
return fmt.Sprintf("%d", int8(integer))
}
} else if cursor.int_length == 2 {
if cursor.unsigned {
return fmt.Sprintf("%d", uint16(integer))
} else {
return fmt.Sprintf("%d", int16(integer))
}
} else if cursor.int_length == 4 {
if cursor.unsigned {
return fmt.Sprintf("%d", uint32(integer))
} else {
return fmt.Sprintf("%d", int32(integer))
}
} else if cursor.int_length == 8 {
if cursor.unsigned {
return fmt.Sprintf("%d", uint64(integer))
} else {
return fmt.Sprintf("%d", int64(integer))
}
}
} else if cursor.mode == FloatingPointMode {
if cursor.fp_length == 4 {
var integer32 uint32 = uint32(integer)
return fmt.Sprintf("%.5g", math.Float32frombits(integer32))
} else if cursor.fp_length == 8 {
return fmt.Sprintf("%g", math.Float64frombits(integer))
}
} else if cursor.mode == BitPatternMode {
str := ""
for i := 0; i < cursor.bit_length; i++ {
str = fmt.Sprintf("%s%02x", str, data[i])
}
return str
}
return string(data)
}
func (cursor *Cursor) interpretBytesAsTime(data []byte) time.Time {
integer := cursor.interpretBytesAsInteger(data)
var date_time time.Time
if cursor.mode == IntegerMode {
if cursor.epoch_unit == SecondsSinceEpoch {
date_time = cursor.epoch_time.Add(time.Duration(integer) * time.Second)
} else if cursor.epoch_unit == MilliSecondsSinceEpoch {
date_time = cursor.epoch_time.Add(time.Duration(integer) * time.Millisecond)
} else {
date_time = cursor.epoch_time.Add(time.Duration(integer) * 24 * time.Hour)
}
} else if cursor.mode == FloatingPointMode {
var float float64
if cursor.fp_length == 4 {
float = float64(math.Float32frombits(uint32(integer)))
} else {
float = math.Float64frombits(integer)
}
if cursor.epoch_unit == SecondsSinceEpoch {
date_time = cursor.epoch_time.Add(time.Duration(float * float64(time.Second)))
} else if cursor.epoch_unit == MilliSecondsSinceEpoch {
date_time = cursor.epoch_time.Add(time.Duration(float * float64(time.Millisecond)))
} else {
date_time = cursor.epoch_time.Add(time.Duration(float * 24 * float64(time.Hour)))
}
} else {
date_time = cursor.epoch_time
}
return date_time.UTC()
}
func (cursor *Cursor) setPos(pos int) {
if pos < 0 {
pos = 0
} else if pos+cursor.length() > cursor.max_pos {
pos = cursor.max_pos - cursor.length()
}
cursor.pos = pos
}
func (cursor *Cursor) move(delta int) {
cursor.setPos(cursor.pos + delta)
}