binp 2.0.0 → 3.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.
- checksums.yaml +4 -4
- data/.gitignore +0 -0
- data/.travis.yml +0 -0
- data/Gemfile +0 -0
- data/Gemfile.lock +1 -1
- data/LICENSE +0 -0
- data/README.md +25 -24
- data/Rakefile +0 -0
- data/bin/console +0 -0
- data/bin/setup +0 -0
- data/binp.gemspec +0 -0
- data/example/example.bin +0 -0
- data/example/example.yaml +20 -0
- data/example/example_01.bin +0 -0
- data/example/example_01.yaml +24 -8
- data/lib/binp.rb +29 -2
- data/lib/binp/cli/main.rb +15 -6
- data/lib/binp/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5de1859ad3bbdf310f87399636229f383e4f40dd5e85624d61ae65d6ac52c3ed
|
4
|
+
data.tar.gz: ac7a4cc8b986aa22022e6b3205d9d11d0c06d83504c2ca30b0788a3f7356993e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 651c3478c7d470f7e25fcaa7f1ef15a9c508a92ee8835e5c9a05380f2662e211036195172e80499df85d0a6ee61503dbcc22db554207781fda2b1e3769e00b1d
|
7
|
+
data.tar.gz: 150b67bc6b8a9502bf7a204e63afb3d6260f7c536cd5f4a89dd24090155678fb1d1a1f886c0bb4ba2fd99437e507cfb41e8a16f67ba685268b52e92de8f26816
|
data/.gitignore
CHANGED
File without changes
|
data/.travis.yml
CHANGED
File without changes
|
data/Gemfile
CHANGED
File without changes
|
data/Gemfile.lock
CHANGED
data/LICENSE
CHANGED
File without changes
|
data/README.md
CHANGED
@@ -38,7 +38,7 @@
|
|
38
38
|
type: UINT32
|
39
39
|
endianness: LITTLE
|
40
40
|
- name: UINT16_value
|
41
|
-
offset:
|
41
|
+
offset: 12
|
42
42
|
size: 2
|
43
43
|
type: UINT16
|
44
44
|
endianness: LITTLE
|
@@ -53,29 +53,30 @@
|
|
53
53
|
|
54
54
|
実行結果は以下のようになる。
|
55
55
|
|
56
|
-
|
56
|
+
```sh
|
57
|
+
$ ruby binary_parser.rb -c setting.yaml example.bin
|
58
|
+
+--------------+-------------------+
|
59
|
+
| name | value |
|
60
|
+
+--------------+-------------------+
|
61
|
+
| UINT64_value | 72057594037927936 |
|
62
|
+
| UINT32_value | 16777216 |
|
63
|
+
| UINT16_value | 256 |
|
64
|
+
| UINT8_value | 1 |
|
65
|
+
+--------------+-------------------+
|
66
|
+
```
|
67
|
+
|
68
|
+
`-a` オプションで追加の情報が出力される。
|
57
69
|
|
58
70
|
```sh
|
59
|
-
$ ruby binary_parser.rb -c setting.yaml example.bin
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
-
]
|
71
|
+
$ ruby binary_parser.rb -a -c setting.yaml example.bin
|
72
|
+
+------------+--------------+--------+------+--------+-------------------+
|
73
|
+
| endianness | name | offset | size | type | value |
|
74
|
+
+------------+--------------+--------+------+--------+-------------------+
|
75
|
+
| LITTLE | UINT64_value | 0 | 8 | UINT64 | 72057594037927936 |
|
76
|
+
| LITTLE | UINT32_value | 8 | 4 | UINT32 | 16777216 |
|
77
|
+
| LITTLE | UINT16_value | 12 | 2 | UINT16 | 256 |
|
78
|
+
| LITTLE | UINT8_value | 14 | 1 | UINT8 | 1 |
|
79
|
+
+------------+--------------+--------+------+--------+-------------------+
|
79
80
|
```
|
80
81
|
|
81
82
|
# TODO:
|
@@ -83,6 +84,6 @@ $ ruby binary_parser.rb -c setting.yaml example.bin | jq '.'
|
|
83
84
|
- [x] : テーブル形式で表示
|
84
85
|
- [ ] : 文字列型サポート
|
85
86
|
- [ ] : UTF8
|
86
|
-
- [
|
87
|
+
- [x] : ビットフラグサポート
|
87
88
|
- [ ] : type からの size 自動設定
|
88
|
-
|
89
|
+
- [ ] : json 形式で表示
|
data/Rakefile
CHANGED
File without changes
|
data/bin/console
CHANGED
File without changes
|
data/bin/setup
CHANGED
File without changes
|
data/binp.gemspec
CHANGED
File without changes
|
data/example/example.bin
ADDED
Binary file
|
@@ -0,0 +1,20 @@
|
|
1
|
+
- name: UINT64_value
|
2
|
+
offset: 0
|
3
|
+
size: 8
|
4
|
+
type: UINT64
|
5
|
+
endianness: LITTLE
|
6
|
+
- name: UINT32_value
|
7
|
+
offset: 8
|
8
|
+
size: 4
|
9
|
+
type: UINT32
|
10
|
+
endianness: LITTLE
|
11
|
+
- name: UINT16_value
|
12
|
+
offset: 12
|
13
|
+
size: 2
|
14
|
+
type: UINT16
|
15
|
+
endianness: LITTLE
|
16
|
+
- name: UINT8_value
|
17
|
+
offset: 14
|
18
|
+
size: 1
|
19
|
+
type: UINT8
|
20
|
+
endianness: LITTLE
|
data/example/example_01.bin
CHANGED
Binary file
|
data/example/example_01.yaml
CHANGED
@@ -9,9 +9,9 @@
|
|
9
9
|
# +----+----+----+----+----+----+----+----+----+----+----+----+----+----+-------+-------+
|
10
10
|
# | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
|
11
11
|
# +----+----+----+----+----+----+----+----+----+----+----+----+----+----+-------+-------+
|
12
|
-
# | 01 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 01 | 00 | 00 | 00 | 01 | 00 | 88 |
|
12
|
+
# | 01 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 01 | 00 | 00 | 00 | 01 | 00 | 88 | AA |
|
13
13
|
# +---------------------------------------+-------------------+---------+-------+-------+
|
14
|
-
# | UINT64 | UINT32 | UINT16 | UINT8 |
|
14
|
+
# | UINT64 | UINT32 | UINT16 | UINT8 | FLAGS |
|
15
15
|
# +---------------------------------------+-------------------+---------+-------+-------+
|
16
16
|
---
|
17
17
|
- name: UINT64_72057594037927936
|
@@ -54,14 +54,30 @@
|
|
54
54
|
size: 2
|
55
55
|
type: UINT16
|
56
56
|
endianness: BIG
|
57
|
-
- name:
|
57
|
+
- name: UINT8_136
|
58
58
|
offset: 30
|
59
59
|
size: 1
|
60
60
|
type: UINT8
|
61
61
|
endianness: BIG
|
62
|
-
- name:
|
62
|
+
- name: FLAGS
|
63
63
|
offset: 31
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
64
|
+
type: FLAGS
|
65
|
+
layout:
|
66
|
+
- name: LED1
|
67
|
+
position: 0
|
68
|
+
- name: LED2
|
69
|
+
position: 1
|
70
|
+
true_label: "high"
|
71
|
+
false_label: "low"
|
72
|
+
- name: LED3
|
73
|
+
position: 2
|
74
|
+
- name: LED4
|
75
|
+
position: 3
|
76
|
+
- name: LED5
|
77
|
+
position: 4
|
78
|
+
- name: LED6
|
79
|
+
position: 5
|
80
|
+
- name: LED7
|
81
|
+
position: 6
|
82
|
+
- name: LED8
|
83
|
+
position: 7
|
data/lib/binp.rb
CHANGED
@@ -3,8 +3,13 @@ require "binp/version"
|
|
3
3
|
|
4
4
|
class BinParser
|
5
5
|
def self.parse(uint8_array, config)
|
6
|
-
config.
|
7
|
-
|
6
|
+
config.flat_map { |e|
|
7
|
+
# ビットフラグかどうかを確認
|
8
|
+
if e['type'] == BinParserElement::Type::FLAGS
|
9
|
+
e = BinParserElement.get_flags(uint8_array, e)
|
10
|
+
else
|
11
|
+
e['value'] = BinParserElement.get_value(uint8_array, e['offset'], e['size'], e['type'], e['endianness'])
|
12
|
+
end
|
8
13
|
e
|
9
14
|
}
|
10
15
|
end
|
@@ -80,5 +85,27 @@ class BinParserElement
|
|
80
85
|
unpack_template_string = calculate_unpack_template_string(type, endianness)
|
81
86
|
target.pack(pack_template_string).unpack(unpack_template_string)[0]
|
82
87
|
end
|
88
|
+
|
89
|
+
def self.get_flags(uint8_array, config)
|
90
|
+
offset = config['offset']
|
91
|
+
layout = config['layout']
|
92
|
+
target = uint8_array.slice(offset, 1)
|
93
|
+
layout.map { |e|
|
94
|
+
c = config.dup
|
95
|
+
c['name'] = e['name']
|
96
|
+
|
97
|
+
# ビットが立っているかの確認
|
98
|
+
c['value'] = target[0] & (1 << e['position'])
|
99
|
+
|
100
|
+
# true_label, false_label が存在すればそれに設定。
|
101
|
+
# 存在しなければデフォルト値('ON', 'OFF')に設定。
|
102
|
+
if c['value'] != 0
|
103
|
+
c['value'] = e.fetch('true_label', 'ON')
|
104
|
+
else
|
105
|
+
c['value'] = e.fetch('false_label', 'OFF')
|
106
|
+
end
|
107
|
+
c
|
108
|
+
}
|
109
|
+
end
|
83
110
|
end
|
84
111
|
|
data/lib/binp/cli/main.rb
CHANGED
@@ -21,7 +21,14 @@ class Main
|
|
21
21
|
# 出力用に加工して出力
|
22
22
|
result = raw_result.map { |e|
|
23
23
|
e['type'] = e['type'][:name]
|
24
|
-
|
24
|
+
|
25
|
+
# FLAGS では endianness が無い場合があるのでチェックしてから設定
|
26
|
+
if e['endianness']
|
27
|
+
e['endianness'] = e['endianness'][:name]
|
28
|
+
end
|
29
|
+
|
30
|
+
# layout は出力量が多すぎるので削除
|
31
|
+
e.delete('layout')
|
25
32
|
e
|
26
33
|
}
|
27
34
|
if !opts[:all]
|
@@ -65,11 +72,13 @@ class Main
|
|
65
72
|
}
|
66
73
|
|
67
74
|
# YAML 記載の endianness に対応した Endianness オブジェクトに差し替える
|
68
|
-
if
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
75
|
+
if e['type'] != BinParserElement::Type::FLAGS
|
76
|
+
if endianness
|
77
|
+
e['endianness'] = BinParserElement::Endianness.const_get(endianness)
|
78
|
+
else
|
79
|
+
STDERR.puts "#{i} 番目の endianness の値が不正です('#{e['endianness']}')。LITTLE or BIG を指定してください。"
|
80
|
+
exit(1)
|
81
|
+
end
|
73
82
|
end
|
74
83
|
}
|
75
84
|
|
data/lib/binp/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: binp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mikoto2000
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-08-
|
11
|
+
date: 2020-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: formatador
|
@@ -70,6 +70,8 @@ files:
|
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
72
|
- binp.gemspec
|
73
|
+
- example/example.bin
|
74
|
+
- example/example.yaml
|
73
75
|
- example/example_01.bin
|
74
76
|
- example/example_01.yaml
|
75
77
|
- exe/binp
|