Class: Kaitai::Struct::Visualizer::HexViewer

Inherits:
Object
  • Object
show all
Defined in:
lib/kaitai/struct/visualizer/hex_viewer.rb

Constant Summary collapse

PER_LINE =
16
PER_GROUP =
4
PAGE_ROWS =
20
FMT =
"%06x: %-#{PER_LINE * 3}s| %-#{PER_LINE}s\n"
HIGHLIGHT_COLOR =
[
  :gray14,
  :gray11,
  :gray8,
  :gray5,
  :gray2,
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ui, buf, shift_x = 0, tree = nil) ⇒ HexViewer

Returns a new instance of HexViewer.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 5

def initialize(ui, buf, shift_x = 0, tree = nil)
  @ui = ui
  @buf = buf
  @shift_x = shift_x
  @tree = tree

  @embedded = not(tree.nil?)
  @max_scr_ln = @ui.rows - 3

  @addr = 0
  @scroll_y = 0
  reset_cur
  raise if @cur_x.nil?
end

Class Method Details

.line_widthObject



142
143
144
145
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 142

def self.line_width
  #6 + 2 + 3 * PER_LINE + 2 + PER_LINE
  10 + 4 * PER_LINE
end

Instance Method Details

#addrObject



24
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 24

def addr; @addr; end

#addr=(a) ⇒ Object



25
26
27
28
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 25

def addr=(a)
  @addr = a
  reset_cur
end

#addr_to_col(addr) ⇒ Object



286
287
288
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 286

def addr_to_col(addr)
  addr % PER_LINE
end

#addr_to_row(addr) ⇒ Object



282
283
284
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 282

def addr_to_row(addr)
  addr / PER_LINE
end

#buf=(buf) ⇒ Object



20
21
22
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 20

def buf=(buf)
  @buf = buf
end

#byte_at(i) ⇒ Object



265
266
267
268
269
270
271
272
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 265

def byte_at(i)
  v = @buf[i]
  if v.nil?
    nil
  else
    v.ord
  end
end

#byte_to_display_char(x) ⇒ Object



274
275
276
277
278
279
280
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 274

def byte_to_display_char(x)
  if x < 0x20 or x >= 0x7f
    '.'
  else
    x.chr
  end
end

#clamp_cursorObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 126

def clamp_cursor
  if @addr < 0
    @addr = 0
    @cur_x = 0
    @cur_y = 0
  elsif @addr >= @buf.size
    @addr = @buf.size - 1
    reset_cur
  end
end

#col_to_col_char(c) ⇒ Object



152
153
154
155
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 152

def col_to_col_char(c)
  #6 + 2 + 3 * PER_LINE + 2
  @shift_x + 10 + 3 * PER_LINE + c
end

#col_to_col_hex(c) ⇒ Object



147
148
149
150
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 147

def col_to_col_hex(c)
  #6 + 2 + 3 * c
  @shift_x + 8 + 3 * c
end

#each_highlight_regionObject



180
181
182
183
184
185
186
187
188
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 180

def each_highlight_region
  return if @hl_regions.nil?
  n = @hl_regions.size
  (n - 1).downto(0).each { |i|
    p1 = @hl_regions[i][0]
    p2 = @hl_regions[i][1]
    yield i, p1, p2 unless p1.nil?
  }
end

#ensure_visibleObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 41

def ensure_visible
  scr_y = row_to_scr(@cur_y)
  if scr_y < 0
    @scroll_y = @cur_y
    redraw
    highlight_show
  elsif scr_y > @max_scr_ln
    @scroll_y = @cur_y - @max_scr_ln
    redraw
    highlight_show
  end
end

#highlight(regions) ⇒ Object



35
36
37
38
39
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 35

def highlight(regions)
  highlight_hide
  @hl_regions = regions
  highlight_show
end

#highlight_draw(p1, p2) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 213

def highlight_draw(p1, p2)
  r = row_to_scr(addr_to_row(p1))
  return if r > @max_scr_ln
  if r < 0
    c = 0
    r = 0
    i = row_col_to_addr(@scroll_y, 0)
    return if i >= p2
  else
    c = addr_to_col(p1)
    i = p1
  end

  highlight_draw_hex(r, c, i, p2)
  highlight_draw_char(r, c, i, p2)
end

#highlight_draw_char(r, c, i, p2) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 247

def highlight_draw_char(r, c, i, p2)
  @ui.goto(col_to_col_char(c), r)

  while i < p2
    v = byte_at(i)
    return if v.nil?
    print byte_to_display_char(v)
    c += 1
    if c >= PER_LINE
      c = 0
      r += 1
      return if r > @max_scr_ln
      @ui.goto(col_to_col_char(c), r)
    end
    i += 1
  end
end

#highlight_draw_hex(r, c, i, p2) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 230

def highlight_draw_hex(r, c, i, p2)
  @ui.goto(col_to_col_hex(c), r)
  while i < p2
    v = byte_at(i)
    return if v.nil?
    printf('%02x ', v)
    c += 1
    if c >= PER_LINE
      c = 0
      r += 1
      return if r > @max_scr_ln
      @ui.goto(col_to_col_hex(c), r)
    end
    i += 1
  end
end

#highlight_hideObject



190
191
192
193
194
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 190

def highlight_hide
  each_highlight_region { |i, p1, p2|
    highlight_draw(p1, p2)
  }
end

#highlight_showObject



204
205
206
207
208
209
210
211
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 204

def highlight_show
  each_highlight_region { |i, p1, p2|
    @ui.bg_color = HIGHLIGHT_COLOR[i]
    @ui.fg_color = :black
    highlight_draw(p1, p2)
  }
  @ui.reset_colors
end

#redrawObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 161

def redraw
  i = row_col_to_addr(@scroll_y, 0)
  row = 0

  while row <= @max_scr_ln do
    line = @buf[i, PER_LINE]
    return unless line

    @ui.goto(@shift_x, row)

    hex = line.bytes.map { |x| sprintf('%02x', x) }.join(' ')
    char = line.bytes.map { |x| byte_to_display_char(x) }.join

    printf FMT, i, hex, char
    i += PER_LINE
    row += 1
  end
end

#reset_curObject



30
31
32
33
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 30

def reset_cur
  @cur_y = addr_to_row(@addr)
  @cur_x = addr_to_col(@addr)
end

#row_col_to_addr(row, col) ⇒ Object



290
291
292
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 290

def row_col_to_addr(row, col)
  row * PER_LINE + col
end

#row_to_scr(r) ⇒ Object



157
158
159
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 157

def row_to_scr(r)
  r - @scroll_y
end

#runObject



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
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 54

def run
  c = nil
  loop {
    @ui.goto(0, @max_scr_ln + 1)
    printf "%06x (%d, %d)", @addr, @cur_x, @cur_y

    @ui.goto(col_to_col_char(@cur_x), row_to_scr(@cur_y))
    c = @ui.read_char_mapped
    case c
    when :tab
      return if @embedded
    when :left_arrow
      if @addr > 0
        @addr -= 1
        @cur_x -= 1
        if @cur_x < 0
          @cur_y -= 1
          @cur_x = PER_LINE - 1
        end
      end
    when :right_arrow
      if @addr < @buf.size
        @addr += 1
        @cur_x += 1
        if @cur_x >= PER_LINE
          @cur_y += 1
          @cur_x = 0
        end
      end
    when :up_arrow
      @addr -= PER_LINE
      @cur_y -= 1
      clamp_cursor
    when :down_arrow
      @addr += PER_LINE
      @cur_y += 1
      clamp_cursor
    when :pg_dn
      @addr += PER_LINE * PAGE_ROWS
      @cur_y += PAGE_ROWS
      clamp_cursor
    when :pg_up
      @addr -= PER_LINE * PAGE_ROWS
      @cur_y -= PAGE_ROWS
      clamp_cursor
    when :home
      if @cur_x == 0
        @addr = 0
        @cur_y = 0
      else
        @addr -= @cur_x
        @cur_x = 0
      end
      clamp_cursor
    when :end
      if @cur_x == PER_LINE - 1
        @addr = @buf.size - 1
        reset_cur
      else
        @addr = @addr - @cur_x + PER_LINE - 1
        @cur_x = PER_LINE - 1
      end
      clamp_cursor
    when 'q'
      @tree.do_exit if @tree
      return
    end

    ensure_visible
  }
end