-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathksdump
executable file
·97 lines (79 loc) · 2.91 KB
/
ksdump
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'optparse'
# Some additional magic to make it work right after repo checkout,
# without installation to proper Ruby library dirs
# The `__dir__` method is supposed to provide the real absolute path of the
# `bin/` directory (which this script is in), but it can technically return
# `nil` or a dot `"."` (in an "irb" session) instead. Normally, it shouldn't
# happen unless some very unusual startup method is used, but if it happens,
# we'll skip adding the dirs to $LOAD_PATH and wait for a miracle (which might
# be that the dependencies we need are already available via $LOAD_PATH because
# they are installed as gems, for example).
#
# See also
# * https://stackoverflow.com/a/2206731
# * https://makandracards.com/makandra/42122-ruby-__file__-__dir__-and-symlinks
script_dir = __dir__
unless script_dir.nil? || script_dir == '.'
$LOAD_PATH << File.expand_path('../lib', script_dir)
$LOAD_PATH << File.expand_path('../../runtime/ruby/lib', script_dir)
end
require 'kaitai/struct/visualizer'
require 'kaitai/struct/visualizer/obj_to_h'
# ======================================================================
FORMATS = %w[json xml yaml].freeze
options = { format: 'yaml' }
parser = OptionParser.new do |opts|
prog_name = File.basename($PROGRAM_NAME)
opts.banner = "Usage: #{prog_name} [options] <file_to_parse.bin> <format.ksy>..."
opts.separator ''
opts.on('-I', '--import-path [DIRECTORIES]', '.ksy library search path(s) for imports (see also KSPATH env variable)') do |v|
options[:import_path] = v
end
opts.on('-f', '--format FORMAT', FORMATS, "choose dump format - #{FORMATS.join(', ')} (default: #{options[:format]})") do |v|
options[:format] = v
end
opts.on('--version', "show versions of #{prog_name}, ksc and kaitai-struct (Kaitai Struct runtime library for Ruby)") do |_v|
puts "#{prog_name} #{Kaitai::Struct::Visualizer::VERSION}"
if system('kaitai-struct-compiler', '--version').nil?
$stderr.puts 'ksv: unable to find and run kaitai-struct-compiler in your PATH'
exit 1
end
require 'kaitai/struct/struct'
puts "kaitai-struct #{Kaitai::Struct::VERSION} (Kaitai Struct runtime library for Ruby)"
exit 0
end
end
begin
parser.parse!
rescue OptionParser::InvalidOption => e
puts e
puts parser
exit 1
end
if ARGV.size < 2
puts parser
exit 1
end
c = Kaitai::Struct::Visualizer::KSYCompiler.new(options)
app = Kaitai::Struct::Visualizer::Parser.new(c, ARGV[0], ARGV[1..-1], options)
exc = app.load
raise exc if exc
# ======================================================================
tree = Kaitai::Struct::Visualizer.obj_to_h(app.data)
r = nil
case options[:format]
when 'json'
require 'json'
r = JSON.pretty_generate(tree)
when 'xml'
require 'active_support'
require 'active_support/core_ext'
r = tree.to_xml
when 'yaml'
require 'yaml'
r = tree.to_yaml
r = r[4..-1] if r[0..3] == "---\n"
end
puts r