binary_parser 1.1.2 → 1.2.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
  SHA1:
3
- metadata.gz: fc0580a7a3667f74608fc328897ce3ee3772b94a
4
- data.tar.gz: d9c1e1b204e9813ff8239cd2d36f45a509430c54
3
+ metadata.gz: b0307ae683d47cfe6a2525daadd6e2243ef6c243
4
+ data.tar.gz: 701289dee15d26ccea833011bdd85cb280153965
5
5
  SHA512:
6
- metadata.gz: 658d10fb088312e15f51965c4a8a9f366b60a58e3be00a61d0dc70fc243f0f124e8adac9b5ef5f113eb2e43ca7b0fa7f83b0998684a7001492819cec4f94547e
7
- data.tar.gz: c8347f938c25674e959014f1bcc1f4097a6d8efdde741f819a5c72f490297d372415c70c68121eea7a14cb6cfffab3a24f46ba394a64163472548ec6a8dc850e
6
+ metadata.gz: 095aedb2598057330badc1037371333fb7ca6ef5edfb37ad26747984ba95297883883a32575e151d0f66238c48c5bb04d7834c8a2383a9368c59d06e17b1cb9e
7
+ data.tar.gz: 28e0a0fe38de2617a8e90c900389c26bfdbea4d3bfac2413bbef888e0d3475c468bf3a1a3c695089dea47bcef41ec6a4b6a9aedd90a45f25cc0d05a8a473b101
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  *.*~
2
+ *#
2
3
  *.gem
3
4
  *.rbc
4
5
  .bundle
@@ -17,3 +18,6 @@ spec/reports
17
18
  test/tmp
18
19
  test/version_tmp
19
20
  tmp
21
+ test.rb
22
+ test2.rb
23
+ test3.rb
data/README.md CHANGED
@@ -102,54 +102,60 @@ Consider the following (temporary) binary structures which describe Image data.
102
102
 
103
103
  You can define MyImage structure in ruby program as following code.
104
104
 
105
-
106
- require 'binary_parser'
107
-
108
- class MyDate < BinaryParser::TemplateBase
109
- require 'date'
110
-
111
- Def do
112
- data :year, UInt, 13
113
- data :month, UInt, 9
114
- data :day, UInt, 9
115
- end
116
-
117
- def to_date
118
- return Date.new(year.to_i, month.to_i, day.to_i)
105
+ ```ruby
106
+ require 'binary_parser'
107
+
108
+ class MyDate < BinaryParser::TemplateBase
109
+ require 'date'
110
+
111
+ Def do
112
+ data :year, UInt, 13
113
+ data :month, UInt, 9
114
+ data :day, UInt, 9
115
+ end
116
+
117
+ def to_date
118
+ return Date.new(year.to_i, month.to_i, day.to_i)
119
+ end
120
+ end
121
+
122
+ class MyImage < BinaryParser::TemplateBase
123
+ Def do
124
+ data :height, UInt, 8
125
+ data :width, UInt, 8
126
+
127
+ # Loop statement
128
+ TIMES var(:height), :i do
129
+ TIMES var(:width), :j do
130
+ data :R, UInt, 8
131
+ data :G, UInt, 8
132
+ data :B, UInt, 8
119
133
  end
120
134
  end
121
135
 
122
- class MyImage < BinaryParser::TemplateBase
123
- Def do
124
- data :height, UInt, 8
125
- data :width, UInt, 8
126
-
127
- TIMES var(:height), :i do
128
- TIMES var(:width), :j do
129
- data :R, UInt, 8
130
- data :G, UInt, 8
131
- data :B, UInt, 8
132
- end
133
- end
134
-
135
- data :has_date, Flag, 1
136
- IF cond(:has_date){|v| v.flagged?} do
137
- data :date, MyDate, 31
138
- end
139
- end
140
- end
136
+ data :has_date, Flag, 1
141
137
 
138
+ # Condition statement
139
+ # * If you want check whether variable-name is valid, alternative expression
140
+ # IF cond(:has_date){|v| v.on?} do ~ end
141
+ # is also available.
142
+ IF E{ has_date.on? } do
143
+ data :date, MyDate, 31
144
+ end
145
+ end
146
+ end
147
+ ```
142
148
 
143
149
  And then you can parse and read binay-data of MyImage as follows.
144
150
 
145
- File.open('my_image.bin', 'rb') do |f|
146
- image = MyImage.new(f.read)
147
- print "Image size: #{image.height.to_i}x#{image.width.to_i}\n"
148
- ul = image.i[0].j[0]
149
- print "RGB color at the first is (#{ul.R.to_i}, #{ul.G.to_i}, #{ul.B.to_i})\n"
150
- print "Image date: #{image.date.to_date}\n"
151
- end
152
-
151
+ ```ruby
152
+ File.open('my_image.bin', 'rb') do |f|
153
+ print "Image size: #{image.height}x#{image.width}\n"
154
+ ul = image.i[0].j[0]
155
+ print "RGB color at the first is (#{ul.R}, #{ul.G}, #{ul.B})\n"
156
+ print "Image date: #{image.date.to_date}\n"
157
+ end
158
+ ```
153
159
 
154
160
  If 'my_image.bin' is binary-data-file of [0x02, 0x02, 0xe7,0x39,0x62, 0x00,0x00,0x00, 0xe7,0x39,0x62, 0x00,0x00,0x00, 0x9f, 0x78, 0x08, 0x03],
155
161
  you can get output as follows.
@@ -161,62 +167,93 @@ you can get output as follows.
161
167
 
162
168
  For your information, you can dump all binary-data's information as follows.
163
169
 
164
- File.open('my_image.bin', 'rb') do |f|
165
- image = MyImage.new(f.read)
166
- image.show(true)
167
- end
168
-
170
+ ```ruby
171
+ File.open('my_image.bin', 'rb') do |f|
172
+ image = MyImage.new(f.read)
173
+ image.show(true)
174
+ end
175
+ ```
169
176
 
170
177
  ### Example 2 ###
171
178
  You can also define other structures as follows.
172
179
 
173
- class DefExample < BinaryParser::TemplateBase
174
- Def do
175
- data :loop_byte_length, UInt, 8
176
-
177
- # Loop until 'loop_byte_length' * 8 bits are parsed.
178
- SPEND var(:loop_byte_length) * 8, :list do
179
- data :length, UInt, 8
180
- # Specifying length by neigbor value.
181
- data :data, Binary, var(:length) * 8
182
- end
183
-
184
- data :v1, UInt, 8
185
- data :v2, UInt, 8
186
-
187
- # Number of Condition variables is arbitary.
188
- IF cond(:v1, :v2){|v1, v2| v1.to_i == v2.to_i} do
189
- # +, -, *, / is available for var. (Order of [Integer op Variable] is NG.)
190
- data :v3, UInt, (var(:v1) + var(:v2)) * 8
191
- end
192
- end
180
+ ```ruby
181
+ class DefExample < BinaryParser::TemplateBase
182
+ Def do
183
+ data :loop_byte_length, UInt, 8
184
+
185
+ # Loop until 'loop_byte_length' * 8 bits are parsed.
186
+ SPEND var(:loop_byte_length) * 8, :list do
187
+
188
+ data :length, UInt, 8
189
+
190
+ # You can specify length by neigbor value.
191
+ data :data, Binary, var(:length) * 8
193
192
  end
194
193
 
195
- Check this definition by giving some binary-data and calling show method as follows.
194
+ data :v1, UInt, 8
195
+ data :v2, UInt, 8
196
196
 
197
- i = DefExample.new([0x05, 0x01, 0xff, 0x02, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01].pack("C*"))
198
- i.show(true)
197
+ # Number of Condition variables is arbitary.
198
+ IF cond(:v1, :v2){|v1, v2| v1 == v2} do
199
+
200
+ # +, -, *, / is available to specify length with variable.
201
+ data :v3, UInt, 8 * (var(:v1) + var(:v2))
202
+ end
203
+ end
204
+ end
205
+ ```
206
+
207
+ You can check this definition by giving some binary-data and calling show method as follows.
208
+
209
+ ```ruby
210
+ i = DefExample.new([0x05, 0x01, 0xff, 0x02, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01].pack("C*"))
211
+ i.show(true)
212
+ ```
213
+
214
+ Output for above checking-code is shown below.
215
+
216
+ ```
217
+ *--------------------------------------------------------------------------------
218
+ loop_byte_length Pos: 0 Len: 8 Type: UInt Cont: 5
219
+ list Pos: 8 Len: 40 Type: LoopList Cont: list with 2 elements
220
+ [0]
221
+ *--------------------------------------------------------------------------------
222
+ length Pos: 0 Len: 8 Type: UInt Cont: 1
223
+ data Pos: 8 Len: 8 Type: Binary Cont: [0xff]
224
+ [1]
225
+ *--------------------------------------------------------------------------------
226
+ length Pos: 0 Len: 8 Type: UInt Cont: 2
227
+ data Pos: 8 Len: 16 Type: Binary Cont: [0xff, 0xff]
228
+ v1 Pos: 48 Len: 8 Type: UInt Cont: 1
229
+ v2 Pos: 56 Len: 8 Type: UInt Cont: 1
230
+ v3 Pos: 64 Len: 16 Type: UInt Cont: 257
231
+ ```
199
232
 
200
233
 
201
234
  ### Example 3 ###
202
235
  If you want to operate Stream-data, StreamTemplateBase class is useful. Define stream as follows.
203
236
 
204
- class StreamExample < BinaryParser::StreamTemplateBase
205
- # Stream which consists of every 4 byte binary-data.
206
- Def(4) do
207
- data :data1, UInt, 8
208
- data :data2, Binary, 24
209
- end
210
- end
237
+ ```ruby
238
+ class StreamExample < BinaryParser::StreamTemplateBase
239
+ # Stream which consists of every 4 byte binary-data.
240
+ Def(4) do
241
+ data :data1, UInt, 8
242
+ data :data2, Binary, 24
243
+ end
244
+ end
245
+ ```
211
246
 
212
247
  And then, get structures from the stream as follows.
213
248
 
214
- File.open('my_image.bin', 'rb') do |f|
215
- stream = StreamExample.new(f)
216
- packet = stream.get_next
217
- puts "data1: #{packet.data1.to_i}, data2: #{packet.data2.to_s}"
218
- stream.get_next.show(true)
219
- end
249
+ ```ruby
250
+ File.open('my_image.bin', 'rb') do |f|
251
+ stream = StreamExample.new(f)
252
+ packet = stream.get_next
253
+ puts "data1: #{packet.data1}, data2: #{packet.data2}"
254
+ stream.get_next.show(true)
255
+ end
256
+ ```
220
257
 
221
258
  StreamTemplateBase has many useful methods to choose structures from the stream.
222
259
  If you want to know detail of these methods, please read documentation or concerned source-files.
@@ -236,4 +273,5 @@ Access shown address by web browser.
236
273
 
237
274
  Versions
238
275
  --------
239
- 1.0.0 April 6, 2014
276
+ 1.0.0 April 6, 2014
277
+ 1.2.0 November 7, 2014
@@ -0,0 +1,49 @@
1
+ require File.dirname(File.expand_path(File.dirname(__FILE__))) + "/lib/binary_parser.rb"
2
+
3
+ class MyDate < BinaryParser::TemplateBase
4
+ require 'date'
5
+
6
+ Def do
7
+ data :year, UInt, 13
8
+ data :month, UInt, 9
9
+ data :day, UInt, 9
10
+ end
11
+
12
+ def to_date
13
+ return Date.new(year.to_i, month.to_i, day.to_i)
14
+ end
15
+ end
16
+
17
+ class MyImage < BinaryParser::TemplateBase
18
+ Def do
19
+ data :height, UInt, 8
20
+ data :width, UInt, 8
21
+
22
+ # Loop statement
23
+ TIMES var(:height), :i do
24
+ TIMES var(:width), :j do
25
+ data :R, UInt, 8
26
+ data :G, UInt, 8
27
+ data :B, UInt, 8
28
+ end
29
+ end
30
+
31
+ data :has_date, Flag, 1
32
+
33
+ # Condition statement
34
+ # * If you want check whether variable-name is valid, alternative expression
35
+ # IF cond(:has_date){|v| v.on?} do ~ end
36
+ # is also available.
37
+ IF E{ has_date.on? } do
38
+ data :date, MyDate, 31
39
+ end
40
+ end
41
+ end
42
+
43
+ bin = [0x02, 0x02, 0xe7,0x39,0x62, 0x00,0x00,0x00, 0xe7,0x39,0x62, 0x00,0x00,0x00, 0x9f, 0x78, 0x08, 0x03]
44
+
45
+ image = MyImage.new(bin.pack("C*"))
46
+ print "Image size: #{image.height}x#{image.width}\n"
47
+ ul = image.i[0].j[0]
48
+ print "RGB color at the first is (#{ul.R}, #{ul.G}, #{ul.B})\n"
49
+ print "Image date: #{image.date.to_date}\n"
@@ -0,0 +1,29 @@
1
+ require File.dirname(File.expand_path(File.dirname(__FILE__))) + "/lib/binary_parser.rb"
2
+
3
+ class DefExample < BinaryParser::TemplateBase
4
+ Def do
5
+ data :loop_byte_length, UInt, 8
6
+
7
+ # Loop until 'loop_byte_length' * 8 bits are parsed.
8
+ SPEND var(:loop_byte_length) * 8, :list do
9
+
10
+ data :length, UInt, 8
11
+
12
+ # You can specify length by neigbor value.
13
+ data :data, Binary, var(:length) * 8
14
+ end
15
+
16
+ data :v1, UInt, 8
17
+ data :v2, UInt, 8
18
+
19
+ # Number of Condition variables is arbitary.
20
+ IF cond(:v1, :v2){|v1, v2| v1 == v2} do
21
+
22
+ # +, -, *, / is available to specify length with variable.
23
+ data :v3, UInt, 8 * (var(:v1) + var(:v2))
24
+ end
25
+ end
26
+ end
27
+
28
+ i = DefExample.new([0x05, 0x01, 0xff, 0x02, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01].pack("C*"))
29
+ i.show(true)
@@ -1,3 +1,3 @@
1
1
  module BinaryParser
2
- VERSION = "1.1.2"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/binary_parser.rb CHANGED
@@ -53,4 +53,16 @@ module BinaryParser
53
53
  LIB_FILES.each do |path|
54
54
  require LIBRARY_ROOT_PATH + LIB_DIR + path
55
55
  end
56
+
57
+
58
+ # load sub-built-in template file
59
+ SUB_BUILT_IN_TEMPLATE_FILES =
60
+ [
61
+ 'uint_n.rb',
62
+ ]
63
+
64
+ SUB_BUILT_IN_TEMPLATE_FILES.each do |path|
65
+ require LIBRARY_ROOT_PATH + BUILT_IN_TEMPLATE_DIR + path
66
+ end
67
+
56
68
  end
@@ -0,0 +1,21 @@
1
+ module BinaryParser
2
+ module BuiltInTemplate
3
+ class UIntN < UInt
4
+ def to_i
5
+ entity.to_i
6
+ end
7
+ end
8
+
9
+ class UInt8 < UIntN
10
+ Def do
11
+ data :entity, UInt, 8
12
+ end
13
+ end
14
+
15
+ class UInt16 < UIntN
16
+ Def do
17
+ data :entity, UInt, 16
18
+ end
19
+ end
20
+ end
21
+ end
@@ -228,8 +228,8 @@ module BinaryParser
228
228
  stack + [token.value]
229
229
  elsif token.is_a?(Expression::Token::Variable)
230
230
  eval_value = token_eval_proc.call(token)
231
- unless eval_value.is_a?(Integer)
232
- raise BadManipulationError, "Evaluation is faild. #{eval_value} is not Integer."
231
+ unless eval_value.is_a?(Integer) || eval_value.is_a?(::BinaryParser::BuiltInTemplate::UInt)
232
+ raise BadManipulationError, "Evaluation is faild. #{eval_value} is not Integer (or UInt) (#{eval_value.class})."
233
233
  end
234
234
  stack + [eval_value]
235
235
  end
data/lib/scope.rb CHANGED
@@ -61,7 +61,7 @@ module BinaryParser
61
61
  unless @definition[name].conditions.all?{|cond| eval(cond, name)}
62
62
  return 0
63
63
  end
64
- return eval(@definition[name].bit_length, name)
64
+ return eval(@definition[name].bit_length, name).to_i
65
65
  end
66
66
 
67
67
  def eval_entire_bit_length
@@ -86,7 +86,7 @@ module BinaryParser
86
86
  raise ParsingError, "Variable '#{token.symbol}' assigned to Nil is referenced" +
87
87
  "at the time of resolving '#{name}'."
88
88
  end
89
- val.to_i
89
+ val
90
90
  end
91
91
  end
92
92
 
@@ -31,16 +31,16 @@ module BinaryParser
31
31
  define(name, DataDefinition.new(bit_at, bit_length, @conditions.dup, klass))
32
32
  end
33
33
 
34
- def SPEND(bit_length, name, &block)
34
+ def SPEND(bit_length, name, klass=nil, &block)
35
35
  __check_new_def_name(name)
36
36
  bit_at, bit_length = __process_bit_length(bit_length, name)
37
- klass = NamelessTemplateMaker.new(self, block)
37
+ klass ||= NamelessTemplateMaker.new(self, block)
38
38
  define(name, LoopDefinition.new(bit_at, bit_length, @conditions.dup, klass))
39
39
  end
40
40
 
41
- def TIMES(times, name, &block)
41
+ def TIMES(times, name, klass=nil, &block)
42
42
  __check_new_def_name(name)
43
- klass = NamelessTemplateMaker.new(self, block)
43
+ klass ||= NamelessTemplateMaker.new(self, block)
44
44
  structure = klass.structure
45
45
  if structure.bit_at.names.empty?
46
46
  bit_at, bit_length = __process_bit_length(times * structure.bit_at.imm, name)
data/lib/template_base.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module BinaryParser
2
2
  class TemplateBase
3
+
3
4
  include BuiltInTemplate
4
5
 
5
6
  def self.def_structure(parent_structure=nil, &definition_proc)
@@ -259,6 +259,44 @@ module BinaryParser
259
259
  assert_equal(1 * 8, i.structure_bit_length)
260
260
  end
261
261
 
262
+ # TEST CASE STRUCTURE 10
263
+ # * new way of SPEND and TIMES
264
+ class ST10 < TemplateBase
265
+
266
+ class ST10SPEND < TemplateBase
267
+ Def do
268
+ data :dat, UInt, 8
269
+ end
270
+ end
271
+
272
+ class ST10TIMES < TemplateBase
273
+ Def do
274
+ data :dat, UInt, 8
275
+ end
276
+ end
277
+
278
+ Def do
279
+ data :spend_size, UInt, 8
280
+ SPEND var(:spend_size) * 8, :spend_datas, ST10SPEND
281
+ data :times, UInt, 8
282
+ TIMES var(:times), :times_datas, ST10TIMES
283
+ end
284
+ end
285
+
286
+ def test_ST10_CASE1
287
+ bin = gen_bin(2, 0xaa, 0xbb, 2, 0xcc, 0xdd)
288
+ i = ST10.new(bin)
289
+
290
+ assert_equal(2, i.spend_size.to_i)
291
+ assert_equal(0xaa, i.spend_datas[0].dat.to_i)
292
+ assert_equal(0xbb, i.spend_datas[1].dat.to_i)
293
+ assert_equal(2, i.times.to_i)
294
+ assert_equal(0xcc, i.times_datas[0].dat.to_i)
295
+ assert_equal(0xdd, i.times_datas[1].dat.to_i)
296
+ assert(i.hold_just_binary?)
297
+ end
298
+
299
+
262
300
  # helpers
263
301
  def gen_bin(*chars)
264
302
  return AbstractBinary.new(chars.pack("C*"))
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binary_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sawaken
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-25 00:00:00.000000000 Z
11
+ date: 2014-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: "This library can parse all kind of binary-data structures including
@@ -51,17 +51,20 @@ executables: []
51
51
  extensions: []
52
52
  extra_rdoc_files: []
53
53
  files:
54
- - .gitignore
54
+ - ".gitignore"
55
55
  - Gemfile
56
56
  - LICENSE.txt
57
57
  - README.md
58
58
  - Rakefile
59
59
  - binary_parser.gemspec
60
+ - examples/example1.rb
61
+ - examples/example2.rb
60
62
  - lib/binary_parser.rb
61
63
  - lib/binary_parser/version.rb
62
64
  - lib/built_in_template/binary.rb
63
65
  - lib/built_in_template/flag.rb
64
66
  - lib/built_in_template/uint.rb
67
+ - lib/built_in_template/uint_n.rb
65
68
  - lib/error.rb
66
69
  - lib/general_class/abstract_binary.rb
67
70
  - lib/general_class/binary_manipulate_function.rb
@@ -101,17 +104,17 @@ require_paths:
101
104
  - lib
102
105
  required_ruby_version: !ruby/object:Gem::Requirement
103
106
  requirements:
104
- - - '>='
107
+ - - ">="
105
108
  - !ruby/object:Gem::Version
106
109
  version: 2.0.0
107
110
  required_rubygems_version: !ruby/object:Gem::Requirement
108
111
  requirements:
109
- - - '>='
112
+ - - ">="
110
113
  - !ruby/object:Gem::Version
111
114
  version: '0'
112
115
  requirements: []
113
116
  rubyforge_project:
114
- rubygems_version: 2.0.3
117
+ rubygems_version: 2.2.2
115
118
  signing_key:
116
119
  specification_version: 4
117
120
  summary: An elegant DSL library for parsing binary-data.