binp 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2d49a717a9a7b1fcd593c87782a690933f3c9a3042bf334dd11599bdec38f2c9
4
- data.tar.gz: 9947217a434b646fee53adbe3b5aa36f0be3139fccdd2942ca80ea7efb1a7de8
3
+ metadata.gz: 5de1859ad3bbdf310f87399636229f383e4f40dd5e85624d61ae65d6ac52c3ed
4
+ data.tar.gz: ac7a4cc8b986aa22022e6b3205d9d11d0c06d83504c2ca30b0788a3f7356993e
5
5
  SHA512:
6
- metadata.gz: a9da24affaff3ac7a84476674ebafc5eb13dabba3ada9c2e18ba7e3b1c0a382d008f1bef9405a1c4b8907a4558a9e2ff32461a516d7b5dbeb30c37bea39a3b16
7
- data.tar.gz: b2ceebf7ce2c19abe698e45617407acd379818052fdd6b6f3a95ac58a38b63653f4ed34399f68a3bb5c03d47772383ea5a209c96f5c5fc860d85f3e92a0b0207
6
+ metadata.gz: 651c3478c7d470f7e25fcaa7f1ef15a9c508a92ee8835e5c9a05380f2662e211036195172e80499df85d0a6ee61503dbcc22db554207781fda2b1e3769e00b1d
7
+ data.tar.gz: 150b67bc6b8a9502bf7a204e63afb3d6260f7c536cd5f4a89dd24090155678fb1d1a1f886c0bb4ba2fd99437e507cfb41e8a16f67ba685268b52e92de8f26816
data/.gitignore CHANGED
File without changes
File without changes
data/Gemfile CHANGED
File without changes
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- binp (2.0.0)
4
+ binp (3.0.0)
5
5
  formatador
6
6
 
7
7
  GEM
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: 2
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
- json 形式で出力されるので、 `jq` 等でよしなに pretty-print してください。
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 | 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
- ]
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
File without changes
data/bin/setup CHANGED
File without changes
File without changes
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
Binary file
@@ -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 | FF |
12
+ # | 01 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 01 | 00 | 00 | 00 | 01 | 00 | 88 | AA |
13
13
  # +---------------------------------------+-------------------+---------+-------+-------+
14
- # | UINT64 | UINT32 | UINT16 | UINT8 | 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: UINT8_255
57
+ - name: UINT8_136
58
58
  offset: 30
59
59
  size: 1
60
60
  type: UINT8
61
61
  endianness: BIG
62
- - name: UINT8_136
62
+ - name: FLAGS
63
63
  offset: 31
64
- size: 1
65
- type: UINT8
66
- endianness: BIG
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
@@ -3,8 +3,13 @@ require "binp/version"
3
3
 
4
4
  class BinParser
5
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'])
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
 
@@ -21,7 +21,14 @@ class Main
21
21
  # 出力用に加工して出力
22
22
  result = raw_result.map { |e|
23
23
  e['type'] = e['type'][:name]
24
- e['endianness'] = e['endianness'][:name]
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 endianness
69
- e['endianness'] = BinParserElement::Endianness.const_get(endianness)
70
- else
71
- STDERR.puts "#{i} 番目の endianness の値が不正です('#{e['endianness']}')。LITTLE or BIG を指定してください。"
72
- exit(1)
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
 
@@ -1,3 +1,3 @@
1
1
  module Binp
2
- VERSION = "2.0.0"
2
+ VERSION = "3.0.0"
3
3
  end
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: 2.0.0
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-19 00:00:00.000000000 Z
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