nxo 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a4f23bfcea1b0fba1cb2cf3596faa33d82587b1059256cd06c05fadb433098f3
4
+ data.tar.gz: 8c78539e9f2a00b725d10881dbd41c7fe1e155d23e70f2bf4e380f19a6e3f29e
5
+ SHA512:
6
+ metadata.gz: 2808d178d483b5591345862379d8a9cc464b1c45a980859761739ea7b3d6f18e1e84ec329c6b0edfd2344cc5bc4fbf0f72d18d486d516c6adda87fe645d1503f
7
+ data.tar.gz: f54358bbd308e65578d8441e5623e33b18a287f15cc0860aaaa7af87d7ecb5daf7dce7ea5babb56bf36f547f02bf022e94dbe085b6af6bbc3519d98f4da1dd20
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in nxo.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nxo (0.1.0)
5
+ lz4-ruby
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ lz4-ruby (0.3.3)
12
+ rake (10.5.0)
13
+ rspec (3.8.0)
14
+ rspec-core (~> 3.8.0)
15
+ rspec-expectations (~> 3.8.0)
16
+ rspec-mocks (~> 3.8.0)
17
+ rspec-core (3.8.0)
18
+ rspec-support (~> 3.8.0)
19
+ rspec-expectations (3.8.2)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.8.0)
22
+ rspec-mocks (3.8.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.8.0)
25
+ rspec-support (3.8.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.16)
32
+ nxo!
33
+ rake (~> 10.0)
34
+ rspec (~> 3.0)
35
+
36
+ BUNDLED WITH
37
+ 1.16.2
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Nxo
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/nxo`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'nxo'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install nxo
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/nxo.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nxo"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/nso_info ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nxo"
5
+
6
+ nso = Nxo::NsoFile.new(File.open(ARGV[0], "rb"))
7
+
8
+ puts "Name: " + nso.name_heuristic
9
+ puts "FS SDK Versions:"
10
+ nso.fs_sdk_versions.each do |v|
11
+ puts " - " + v
12
+ end
13
+ puts nso.rodata.contents.scan(/SDK MW[ -~]*/)
data/lib/nxo.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "nxo/version"
2
+ require "nxo/nxo"
3
+ require "nxo/nso"
4
+
5
+ module Nxo
6
+ end
@@ -0,0 +1,94 @@
1
+ module Nxo
2
+ module Dynamic
3
+ module DT
4
+ NULL = 0
5
+ NEEDED = 1
6
+ PLTRELSZ = 2
7
+ PLTGOT = 3
8
+ HASH = 4
9
+ STRTAB = 5
10
+ SYMTAB = 6
11
+ RELA = 7
12
+ RELASZ = 8
13
+ RELAENT = 9
14
+ STRSZ = 10
15
+ SYMENT = 11
16
+ # 12-15
17
+ SYMBOLIC = 16
18
+ REL = 17
19
+ RELSZ = 18
20
+ RELENT = 19
21
+ PLTREL = 20
22
+ # 21-22
23
+ JMPREL = 23
24
+ # 24
25
+ INIT_ARRAY = 25
26
+ FINI_ARRAY = 26
27
+ INIT_ARRAYSZ = 27
28
+ FINI_ARRAYSZ = 28
29
+ FLAGS = 30
30
+ GNU_HASH = 0x6ffffef5
31
+ RELACOUNT = 0x6ffffff9
32
+ end
33
+
34
+ class Entry
35
+ def initialize(nxo, tag, value)
36
+ @nxo = nxo
37
+ @tag = tag
38
+ @value = value
39
+ end
40
+
41
+ attr_reader :nxo, :tag, :value
42
+ end
43
+
44
+
45
+ class Symbol
46
+ def initialize(name, info, other, shndx, value, size)
47
+ @name = name
48
+ @info = info
49
+ @other = other
50
+ @shndx = shndx
51
+ @value = value
52
+ @size = size
53
+ end
54
+
55
+ attr_reader :name, :info, :other, :shndx, :value, :size
56
+ end
57
+
58
+ class DynamicSection
59
+ def initialize(nxo, offset)
60
+ @nxo = nxo
61
+ @offset = offset
62
+ @entries = []
63
+
64
+ lc = offset
65
+ while((entry = Entry.new(self, *@nxo[lc, 0x10].unpack("Q<Q<"))).tag != DT::NULL) do
66
+ @entries.push(entry)
67
+ lc+= 0x10
68
+ end
69
+ end
70
+
71
+ def [](tag)
72
+ return @entries.find do |entry|
73
+ entry.tag == tag
74
+ end
75
+ end
76
+
77
+ def nchain
78
+ return @nxo[self[DT::HASH].value + 4, 4].unpack("L<")[0]
79
+ end
80
+
81
+ def string(i)
82
+ @nxo[self[DT::STRTAB].value + i, 1024].unpack("Z1024")[0]
83
+ end
84
+
85
+ SYMBOL_SIZE = 0x18
86
+
87
+ def symbol(i)
88
+ pack = @nxo[self[DT::SYMTAB].value + i * SYMBOL_SIZE, SYMBOL_SIZE]
89
+ name, info, other, shndx, value, size = pack.unpack("L<CCS<Q<Q<")
90
+ return Symbol.new(string(name), info, other, shndx, value, size)
91
+ end
92
+ end
93
+ end
94
+ end
data/lib/nxo/error.rb ADDED
@@ -0,0 +1,52 @@
1
+ module Nxo
2
+ module Error
3
+ class InvalidMagicError < RuntimeError
4
+ def initialize(actual, expected)
5
+ super("Invalid magic number (got \"#{actual}\", expected \"#{expected}\")")
6
+ @actual = actual
7
+ @expected = expected
8
+ end
9
+
10
+ attr_reader :actual, :expected
11
+ end
12
+
13
+ class UnknownVersionError < RuntimeError
14
+ def initialize(actual, expected)
15
+ super("Invalid version (got \"#{actual}\", expected \"#{expected}\")")
16
+ @actual = actual
17
+ @expected = expected
18
+ end
19
+
20
+ attr_reader :actual, :expected
21
+ end
22
+
23
+ class HashCheckError < RuntimeError
24
+ def initialize(segment)
25
+ super("Hash check failed while loading segment \"#{segment}\"")
26
+ @segment = segment
27
+ end
28
+
29
+ attr_reader :segment
30
+ end
31
+
32
+ class AddressMismatchError < RuntimeError
33
+ def initialize(segment, expected, actual)
34
+ super("Attempted to place segment \"#{segment}\" at 0x#{actual.to_s(16)}, but headers puts it at 0x#{expected.to_s(16)}")
35
+ @segment = segment
36
+ @expected = expected
37
+ @actual = actual
38
+ end
39
+
40
+ attr_reader :segment, :expected, :actual
41
+ end
42
+
43
+ class UnmappedAddressError < RuntimeError
44
+ def initialize(address)
45
+ super("No segment at 0x#{address.to_s(166)}")
46
+ @address = address
47
+ end
48
+
49
+ attr_reader :address
50
+ end
51
+ end
52
+ end
data/lib/nxo/nso.rb ADDED
@@ -0,0 +1,77 @@
1
+ require "digest"
2
+ require "lz4-ruby"
3
+
4
+ require "nxo/nxo"
5
+ require "nxo/error"
6
+
7
+ module Nxo
8
+ class NsoFile < NxoFile
9
+ NSO_MAGIC = "NSO0"
10
+ NSO_VERSION = 0
11
+
12
+ def initialize(f, options = {})
13
+ @f = f
14
+
15
+ @f.pos = 0
16
+ magic, version, flags = @f.read(0x10).unpack("a4L<x4L<")
17
+
18
+ if magic != NSO_MAGIC then
19
+ raise Error::InvalidMagicError.new(magic, NSO_MAGIC)
20
+ end
21
+
22
+ if version != NSO_VERSION then
23
+ raise Error::UnknownVersionError.new(version, NSO_VERSION)
24
+ end
25
+
26
+ segment_headers = 3.times.map do
27
+ Hash[[:file_offset, :memory_offset, :decompressed_size, :misc].zip(@f.read(0x10).unpack("L<L<L<L<"))]
28
+ end
29
+
30
+ module_offset, module_file_size, bss_size = segment_headers.map do |sh| sh[:misc] end
31
+
32
+ @build_id = @f.read(0x20)
33
+
34
+ @f.read(3 * 0x4).unpack("L<L<L<").each_with_index do |size, i|
35
+ segment_headers[i][:compressed_size] = size
36
+ end
37
+
38
+ @f.pos = 0x88
39
+ @extents = Hash[[:api_info, :dynstr, :dynsym].zip(@f.read(0x18).unpack("Q<Q<Q<"))]
40
+
41
+ @f.read(3 * 0x20).unpack("a32a32a32").each_with_index do |hash, i|
42
+ segment_headers[i][:hash] = hash
43
+ end
44
+
45
+ location_counter = 0
46
+ segments = [:text, :rodata, :data].each_with_index.map do |name, i|
47
+ sh = segment_headers[i]
48
+ @f.pos = sh[:file_offset]
49
+ contents = @f.read(sh[:compressed_size])
50
+
51
+ if flags[i] == 1 then # is compressed
52
+ contents = LZ4::Raw.decompress(contents, sh[:decompressed_size])[0]
53
+ end
54
+
55
+ if flags[i+3] == 1 && !options[:disable_hash_check] then # check hash
56
+ if Digest::SHA256.digest(contents) != sh[:hash] then
57
+ raise Error::HashCheckError.new(name)
58
+ end
59
+ end
60
+
61
+ if sh[:memory_offset] < location_counter then
62
+ raise Error::AddressMismatchError.new(name, sh[:memory_offset], location_counter)
63
+ end
64
+
65
+ location_counter = sh[:memory_offset] + contents.bytesize
66
+
67
+ Segment.new(name, sh[:memory_offset], contents)
68
+ end + [Segment.new("bss", location_counter, 0.chr * bss_size)]
69
+
70
+ super(segments)
71
+ end
72
+
73
+ attr_reader :build_id
74
+ attr_reader :extents
75
+ attr_reader :segments
76
+ end
77
+ end
data/lib/nxo/nxo.rb ADDED
@@ -0,0 +1,94 @@
1
+ require "nxo/error"
2
+ require "nxo/dynamic"
3
+
4
+ module Nxo
5
+ class NxoFile
6
+ MOD_MAGIC = "MOD0"
7
+
8
+ class Segment
9
+ def initialize(name, address, contents)
10
+ @name = name
11
+ @address = address
12
+ @contents = contents
13
+ end
14
+
15
+ attr_reader :name
16
+ attr_reader :address
17
+ attr_reader :contents
18
+
19
+ def size
20
+ @contents.bytesize
21
+ end
22
+
23
+ def contains?(addr)
24
+ return addr >= @address && addr < @address + contents.bytesize
25
+ end
26
+
27
+ def [](offset, size)
28
+ return @contents[offset, size]
29
+ end
30
+ end
31
+
32
+ def initialize(segments)
33
+ @segments = segments
34
+
35
+ module_offset = self[4, 4].unpack("L<")[0]
36
+
37
+ magic, o_dynamic, o_bss_b, o_bss_e, o_eh_frame_hdr_b, o_eh_frame_hdr_e, o_ro_module = self[module_offset, 0x1c].unpack("a4L<L<L<L<L<L<")
38
+
39
+ if magic != MOD_MAGIC then
40
+ raise Error::InvalidMagicError.new(module_magic, MOD_MAGIC)
41
+ end
42
+
43
+ if module_offset + o_bss_b != bss.address then # sanity check
44
+ # raise Error::AddressMismatchError.new(:module_bss, module_offset + o_bss_b, bss.address)
45
+ end
46
+
47
+ @dynamic = Dynamic::DynamicSection.new(self, module_offset + o_dynamic)
48
+ end
49
+
50
+ attr_reader :dynamic
51
+
52
+ def [](address, size)
53
+ @segments.each do |seg|
54
+ if seg.contains?(address) then
55
+ offset = address - seg.address
56
+ available_size = seg.size - offset
57
+ safe_size = [size, available_size].min
58
+ str = seg[offset, safe_size]
59
+ if safe_size < size then
60
+ return str + self[address + safe_size, size - safe_size]
61
+ else
62
+ return str
63
+ end
64
+ end
65
+ end
66
+ raise Error::UnmappedAddressError.new(address)
67
+ end
68
+
69
+ def text
70
+ @segments[0]
71
+ end
72
+
73
+ def rodata
74
+ @segments[1]
75
+ end
76
+
77
+ def data
78
+ @segments[2]
79
+ end
80
+
81
+ def bss
82
+ @segments[3]
83
+ end
84
+
85
+ def fs_sdk_versions
86
+ rodata.contents.scan(/sdk_version: ([0-9.]*)/).map do |m| m[0] end
87
+ end
88
+
89
+ def name_heuristic
90
+ l = rodata[4, 4].unpack("L<")[0]
91
+ return rodata[8, l]
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ module Nxo
2
+ VERSION = "0.1.0"
3
+ end
data/nxo.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "nxo/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "nxo"
7
+ spec.version = Nxo::VERSION
8
+ spec.authors = ["misson20000"]
9
+ spec.email = ["xenotoad@xenotoad.net"]
10
+
11
+ spec.summary = "Small gem for working with Nintendo Switch executable objects"
12
+ spec.homepage = "https://github.com/misson20000/nxo-rb"
13
+
14
+ # Specify which files should be added to the gem when it is released.
15
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "lz4-ruby"
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nxo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - misson20000
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lz4-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - xenotoad@xenotoad.net
72
+ executables:
73
+ - nso_info
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - exe/nso_info
87
+ - lib/nxo.rb
88
+ - lib/nxo/dynamic.rb
89
+ - lib/nxo/error.rb
90
+ - lib/nxo/nso.rb
91
+ - lib/nxo/nxo.rb
92
+ - lib/nxo/version.rb
93
+ - nxo.gemspec
94
+ homepage: https://github.com/misson20000/nxo-rb
95
+ licenses: []
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.7.7
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Small gem for working with Nintendo Switch executable objects
117
+ test_files: []