binp 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d2eb04ce7bb1579b6b9cdc507a61047c368fe47e8acfdd6cd332a0091b67ae68
4
+ data.tar.gz: e3584dff37e2cc175bf0eaafd734f570e3b02de7d7b95c808759c35c5db4d68d
5
+ SHA512:
6
+ metadata.gz: 8fb9db8a044b041637f1ce5dfce53dd1df57ee9053fe5f6ebc855b2d11bd57bc0270f490e6c4a370179fe16e47ff5ac8c3fe1e7fcf44651d45dc6053b9f6f581
7
+ data.tar.gz: e34bee00576187bc9e17d862c16af026c0a7c640b3391789912975da23b8aba0e8f77a329d6b0d73f4903fbcedd53e1e80005f89390224de5b035f240cc57ba0
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /vendor/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in binp.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ binp (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ docile (1.3.2)
10
+ power_assert (1.2.0)
11
+ rake (12.3.3)
12
+ simplecov (0.18.5)
13
+ docile (~> 1.1)
14
+ simplecov-html (~> 0.11)
15
+ simplecov-html (0.12.2)
16
+ test-unit (3.3.6)
17
+ power_assert
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ binp!
24
+ rake (~> 12.0)
25
+ simplecov
26
+ test-unit
27
+
28
+ BUNDLED WITH
29
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 mikoto2000
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,88 @@
1
+ # Binp - Binary Parser
2
+
3
+ バイナリファイルからデータを抽出するツール。
4
+
5
+ `offset`, `size` `type`, `endianness` を指定して、バイナリファイルのどこからどこまでをどのように解釈するかを指定できる。
6
+
7
+
8
+ # 使い方
9
+
10
+ 以下内容のバイナリファイルをパースする場合について説明する。
11
+
12
+ ■ `example.bin`
13
+
14
+ ```
15
+ +----+----+----+----+----+----+----+----+----+----+----+----+----+----+-------+
16
+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
17
+ +----+----+----+----+----+----+----+----+----+----+----+----+----+----+-------+
18
+ | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 01 | 00 | 00 | 00 | 01 | 00 | 01 | 01 |
19
+ +---------------------------------------+-------------------+---------+-------+
20
+ | UINT64 | UINT32 | UINT16 | UINT8 |
21
+ +---------------------------------------+-------------------+---------+-------+
22
+ ※ エンディアンはリトルエンディアン
23
+ ```
24
+
25
+ 以下のように、設定ファイルに `name`, `offset`, `size`, `type` を記述する。
26
+
27
+ ■ `setting.yaml`
28
+
29
+ ```yaml
30
+ - name: UINT64_value
31
+ offset: 0
32
+ size: 8
33
+ type: UINT64
34
+ endianness: LITTLE
35
+ - name: UINT32_value
36
+ offset: 8
37
+ size: 4
38
+ type: UINT32
39
+ endianness: LITTLE
40
+ - name: UINT16_value
41
+ offset: 2
42
+ size: 2
43
+ type: UINT16
44
+ endianness: LITTLE
45
+ - name: UINT8_value
46
+ offset: 14
47
+ size: 1
48
+ type: UINT8
49
+ endianness: LITTLE
50
+ ```
51
+
52
+ `binary_parser.rb` に設定ファイルとバイナリファイルを指定して実行する。
53
+
54
+ 実行結果は以下のようになる。
55
+
56
+ json 形式で出力されるので、 `jq` 等でよしなに pretty-print してください。
57
+
58
+ ```sh
59
+ $ ruby binary_parser.rb -c setting.yaml example.bin | jq '.'
60
+ [
61
+ {
62
+ "name": "UINT64_72057594037927936",
63
+ "offset": 0,
64
+ "size": 8,
65
+ "type": "UINT64",
66
+ "endianness": "LITTLE",
67
+ "value": 72057594037927940
68
+ },
69
+ ...(snip)
70
+ {
71
+ "name": "UINT8_value",
72
+ "offset": 14,
73
+ "size": 1,
74
+ "type": "UINT8",
75
+ "endianness": "LITTLE",
76
+ "value": 1
77
+ }
78
+ ]
79
+ ```
80
+
81
+ # TODO:
82
+
83
+ - [ ] : テーブル形式で表示
84
+ - [ ] : 文字列型サポート
85
+ - [ ] : UTF8
86
+ - [ ] : ビットフラグサポート
87
+ - [ ] : type からの size 自動設定
88
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "binp"
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__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install --path=vendor/bundle
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ require_relative 'lib/binp/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "binp"
5
+ spec.version = Binp::VERSION
6
+ spec.authors = ["mikoto2000"]
7
+ spec.email = ["mikoto2000@gmail.com"]
8
+
9
+ spec.summary = %q{binary file parser.}
10
+ spec.description = %q{simple binary file parser.}
11
+ spec.homepage = "https://github.com/mikoto2000/binp"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://rubygems.org/gems/binp"
17
+ spec.metadata["changelog_uri"] = "https://github.com/mikoto2000/binp/releases"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency 'test-unit'
29
+ spec.add_development_dependency 'simplecov'
30
+ end
Binary file
@@ -0,0 +1,67 @@
1
+ # +----+----+----+----+----+----+----+----+----+----+----+----+----+----+-------+-------+
2
+ # | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
3
+ # +----+----+----+----+----+----+----+----+----+----+----+----+----+----+-------+-------+
4
+ # | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 01 | 00 | 00 | 00 | 01 | 00 | 01 | FF | 88 |
5
+ # +---------------------------------------+-------------------+---------+-------+-------+
6
+ # | UINT64 | UINT32 | UINT16 | UINT8 | UINT8 |
7
+ # +---------------------------------------+-------------------+---------+-------+-------+
8
+ #
9
+ # +----+----+----+----+----+----+----+----+----+----+----+----+----+----+-------+-------+
10
+ # | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
11
+ # +----+----+----+----+----+----+----+----+----+----+----+----+----+----+-------+-------+
12
+ # | 01 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 01 | 00 | 00 | 00 | 01 | 00 | 88 | FF |
13
+ # +---------------------------------------+-------------------+---------+-------+-------+
14
+ # | UINT64 | UINT32 | UINT16 | UINT8 | UINT8 |
15
+ # +---------------------------------------+-------------------+---------+-------+-------+
16
+ ---
17
+ - name: UINT64_72057594037927936
18
+ offset: 0
19
+ size: 8
20
+ type: UINT64
21
+ endianness: LITTLE
22
+ - name: UINT32_16777216
23
+ offset: 8
24
+ size: 4
25
+ type: UINT32
26
+ endianness: LITTLE
27
+ - name: UINT16_256
28
+ offset: 12
29
+ size: 2
30
+ type: UINT16
31
+ endianness: LITTLE
32
+ - name: UINT8_255
33
+ offset: 14
34
+ size: 1
35
+ type: UINT8
36
+ endianness: LITTLE
37
+ - name: UINT8_136
38
+ offset: 15
39
+ size: 1
40
+ type: UINT8
41
+ endianness: LITTLE
42
+ - name: UINT64_72057594037927936
43
+ offset: 0
44
+ size: 8
45
+ type: UINT64
46
+ endianness: BIG
47
+ - name: UINT32_16777216
48
+ offset: 8
49
+ size: 4
50
+ type: UINT32
51
+ endianness: BIG
52
+ - name: UINT16_256
53
+ offset: 12
54
+ size: 2
55
+ type: UINT16
56
+ endianness: BIG
57
+ - name: UINT8_255
58
+ offset: 14
59
+ size: 1
60
+ type: UINT8
61
+ endianness: BIG
62
+ - name: UINT8_136
63
+ offset: 15
64
+ size: 1
65
+ type: UINT8
66
+ endianness: BIG
67
+
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/binp/cli/main.rb'
4
+
5
+ if __FILE__ == $0
6
+ Main.run(ARGV)
7
+ end
8
+
@@ -0,0 +1,84 @@
1
+ # encoding: UTF-8
2
+ require "binp/version"
3
+
4
+ class BinParser
5
+ def self.parse(uint8_array, config)
6
+ config.map { |e|
7
+ e['value'] = BinParserElement.get_value(uint8_array, e['offset'], e['size'], e['type'], e['endianness'])
8
+ e
9
+ }
10
+ end
11
+ end
12
+
13
+ class BinParserElement
14
+ attr_reader :name, :offset, :size, :endianness
15
+
16
+ def initialize(name, offset, size, endianness)
17
+ @name = name
18
+ @offset = offset
19
+ @size = size
20
+ @endianness = endianness
21
+ end
22
+
23
+ module Type
24
+ UINT8 = { name: 'UINT8', value: 'C' }
25
+ UINT16 = { name: 'UINT16', value: 'S' }
26
+ UINT32 = { name: 'UINT32', value: 'L' }
27
+ UINT64 = { name: 'UINT64', value: 'Q' }
28
+ INT8 = { name: 'INT8', value: 'c' }
29
+ INT16 = { name: 'INT16', value: 's' }
30
+ INT32 = { name: 'INT32', value: 'l' }
31
+ INT64 = { name: 'INT64', value: 'q' }
32
+ FLAGS = { name: 'FLAGS', value: 'C' }
33
+ end
34
+
35
+ module Endianness
36
+ BIG_ENDIAN = { name: 'BIG', value: '>' }
37
+ LITTLE_ENDIAN = { name: 'LITTLE', value: '<' }
38
+ end
39
+
40
+ # see: https://docs.ruby-lang.org/ja/latest/doc/pack_template.html
41
+ def self.calculate_unpack_template_string(type, endianness)
42
+
43
+ # ����ȏ���
44
+ case type
45
+ when BinParserElement::Type::UINT16
46
+ if endianness == BinParserElement::Endianness::BIG_ENDIAN
47
+ # �r�b�O�G���f�B�A���A�����Ȃ� 16 bit ����
48
+ return 'n'
49
+ else
50
+ # ���g���G���f�B�A���A�����Ȃ� 16 bit ����
51
+ return 'v'
52
+ end
53
+ when BinParserElement::Type::UINT32
54
+ if endianness == BinParserElement::Endianness::BIG_ENDIAN
55
+ # �r�b�O�G���f�B�A���A�������� 32 bit ����
56
+ return 'N'
57
+ else
58
+ # ���g���G���f�B�A���A�������� 32 bit ����
59
+ return 'V'
60
+ end
61
+ when BinParserElement::Type::UINT8
62
+ # �����Ȃ� 8 bit ����
63
+ return 'C'
64
+ when BinParserElement::Type::INT8
65
+ # �������� 8 bit ����
66
+ return 'c'
67
+ end
68
+
69
+ # ���̑�
70
+ type[:value] + endianness[:value]
71
+ end
72
+
73
+ def self.calculate_pack_template_string(size)
74
+ 'C' * size
75
+ end
76
+
77
+ def self.get_value(uint8_array, offset, size, type, endianness)
78
+ target = uint8_array.slice(offset, size)
79
+ pack_template_string = calculate_pack_template_string(size)
80
+ unpack_template_string = calculate_unpack_template_string(type, endianness)
81
+ target.pack(pack_template_string).unpack(unpack_template_string)[0]
82
+ end
83
+ end
84
+
@@ -0,0 +1,77 @@
1
+ # encoding: UTF-8
2
+ require 'json'
3
+ require 'optparse'
4
+ require 'yaml'
5
+
6
+ require_relative '../../binp.rb'
7
+
8
+ class Main
9
+ def self.run(argv)
10
+ # 引数パース
11
+ opts, argv = parse_option(argv)
12
+
13
+ # ファイル読み込み
14
+ config = read_config_file(opts[:config])
15
+ uint8_array = read_binary_file(argv[0])
16
+
17
+ # バイナリパース
18
+ raw_result = BinParser.parse(uint8_array, config)
19
+
20
+ # 出力用に加工して出力
21
+ puts raw_result.map { |e|
22
+ e['type'] = e['type'][:name]
23
+ e['endianness'] = e['endianness'][:name]
24
+ e
25
+ }.to_json.to_s
26
+ end
27
+
28
+ def self.parse_option(argv)
29
+ options = {}
30
+ op = OptionParser.new
31
+
32
+ op.on('-c VALUE', '--config VALUE', '設定ファイルパス') { |v| options[:config] = v }
33
+ op.parse!(argv)
34
+
35
+ [options, argv]
36
+ end
37
+
38
+ def self.read_config_file(config_file_path)
39
+ config = File.open(config_file_path, 'rb') { |f|
40
+ YAML.load_file(f)
41
+ }
42
+ config.each_with_index { |e, i|
43
+ type = BinParserElement::Type.constants.find { |type|
44
+ BinParserElement::Type.const_get(type)[:name] == e['type']
45
+ }
46
+
47
+ # YAML 記載の type に対応した Type オブジェクトに差し替える
48
+ if type
49
+ e['type'] = BinParserElement::Type.const_get(type)
50
+ else
51
+ STDERR.puts "#{i} 番目の type の値が不正です('#{e['type']}')。UINT8, UINT16, UINT32, UINT64, INT8, INT16, INT32 or INT64."
52
+ exit(1)
53
+ end
54
+
55
+ endianness = BinParserElement::Endianness.constants.find { |endianness|
56
+ BinParserElement::Endianness.const_get(endianness)[:name] == e['endianness']
57
+ }
58
+
59
+ # YAML 記載の endianness に対応した Endianness オブジェクトに差し替える
60
+ if endianness
61
+ e['endianness'] = BinParserElement::Endianness.const_get(endianness)
62
+ else
63
+ STDERR.puts "#{i} 番目の endianness の値が不正です('#{e['endianness']}')。LITTLE or BIG を指定してください。"
64
+ exit(1)
65
+ end
66
+ }
67
+
68
+ config
69
+ end
70
+
71
+ def self.read_binary_file(binary_file_path)
72
+ File.open(binary_file_path, 'rb') { |f|
73
+ f.each_byte.to_a
74
+ }
75
+ end
76
+ end
77
+
@@ -0,0 +1,3 @@
1
+ module Binp
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: binp
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - mikoto2000
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: test-unit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: simplecov
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: simple binary file parser.
42
+ email:
43
+ - mikoto2000@gmail.com
44
+ executables:
45
+ - binp
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - binp.gemspec
59
+ - example/example_01.bin
60
+ - example/example_01.yaml
61
+ - exe/binp
62
+ - lib/binp.rb
63
+ - lib/binp/cli/main.rb
64
+ - lib/binp/version.rb
65
+ homepage: https://github.com/mikoto2000/binp
66
+ licenses:
67
+ - MIT
68
+ metadata:
69
+ homepage_uri: https://github.com/mikoto2000/binp
70
+ source_code_uri: https://rubygems.org/gems/binp
71
+ changelog_uri: https://github.com/mikoto2000/binp/releases
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.3.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.1.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: binary file parser.
91
+ test_files: []