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 =
"%08x: %-#{PER_LINE * 3}s| %-#{PER_LINE}s\n"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of HexViewer.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 9

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

  @embedded = !tree.nil?
  @max_scr_ln = @ui.rows - 3

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

Instance Attribute Details

#addrObject

Returns the value of attribute addr.



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

def addr
  @addr
end

#buf=(value) ⇒ Object (writeonly)

Sets the attribute buf

Parameters:

  • value

    the value to set the attribute buf to.



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

def buf=(value)
  @buf = value
end

#shift_xObject

Returns the value of attribute shift_x.



7
8
9
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 7

def shift_x
  @shift_x
end

Class Method Details

.line_widthObject



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

def self.line_width
  # 8 + 2 + 3 * PER_LINE + 2 + PER_LINE
  12 + 4 * PER_LINE
end

Instance Method Details

#addr_to_col(addr) ⇒ Object



294
295
296
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 294

def addr_to_col(addr)
  addr % PER_LINE
end

#addr_to_row(addr) ⇒ Object



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

def addr_to_row(addr)
  addr / PER_LINE
end

#byte_at(i) ⇒ Object



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

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

#byte_to_display_char(x) ⇒ Object



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

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

#clamp_cursorObject



136
137
138
139
140
141
142
143
144
145
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 136

def clamp_cursor
  if @addr.negative?
    @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



162
163
164
165
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 162

def col_to_col_char(c)
  # 8 + 2 + 3 * PER_LINE + 2
  @shift_x + 12 + 3 * PER_LINE + c
end

#col_to_col_hex(c) ⇒ Object



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

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

#each_highlight_regionObject



190
191
192
193
194
195
196
197
198
199
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 190

def each_highlight_region
  return if @hl_regions.nil?

  n = @hl_regions.size
  (n - 1).downto(0).each do |i|
    p1 = @hl_regions[i][0]
    p2 = @hl_regions[i][1]
    yield i, p1, p2 unless p1.nil?
  end
end

#ensure_visibleObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 44

def ensure_visible
  scr_y = row_to_scr(@cur_y)
  if scr_y.negative?
    @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



38
39
40
41
42
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 38

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

#highlight_draw(p1, p2) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 216

def highlight_draw(p1, p2)
  r = row_to_scr(addr_to_row(p1))
  return if r > @max_scr_ln

  if r.negative?
    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



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 253

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



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 234

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



201
202
203
204
205
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 201

def highlight_hide
  each_highlight_region do |_i, p1, p2|
    highlight_draw(p1, p2)
  end
end

#highlight_showObject



207
208
209
210
211
212
213
214
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 207

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

#redrawObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 171

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

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

    @ui.goto(@shift_x, row)

    hex = line.bytes.map { |x| format('%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



33
34
35
36
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 33

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

#row_col_to_addr(row, col) ⇒ Object



298
299
300
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 298

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

#row_to_scr(r) ⇒ Object



167
168
169
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 167

def row_to_scr(r)
  r - @scroll_y
end

#runObject



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

def run
  c = nil
  loop do
    @ui.goto(0, @max_scr_ln + 1)
    printf '%08x (%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.positive?
        @addr -= 1
        @cur_x -= 1
        if @cur_x.negative?
          @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.zero?
        @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 'w'
      fn = @ui.input_str('Write buffer to file', 'Filename')
      File.open(fn, 'w') do |out|
        out.write(@buf)
      end
      @ui.clear
      redraw
    when 'q'
      @tree&.do_exit
      return
    end

    ensure_visible
  end
end