binary_parser 1.2.0 → 1.2.1

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.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -7
  3. data/examples/example1.rb +1 -1
  4. data/examples/example4.rb +10 -0
  5. data/lib/binary_parser/built_in_template/bcd.rb +52 -0
  6. data/lib/{built_in_template → binary_parser/built_in_template}/binary.rb +0 -0
  7. data/lib/{built_in_template → binary_parser/built_in_template}/flag.rb +0 -0
  8. data/lib/{built_in_template → binary_parser/built_in_template}/uint.rb +1 -1
  9. data/lib/{built_in_template → binary_parser/built_in_template}/uint_n.rb +10 -0
  10. data/lib/{error.rb → binary_parser/error.rb} +0 -0
  11. data/lib/{general_class → binary_parser/general_class}/abstract_binary.rb +0 -0
  12. data/lib/{general_class → binary_parser/general_class}/binary_manipulate_function.rb +0 -0
  13. data/lib/{general_class → binary_parser/general_class}/bit_position.rb +0 -0
  14. data/lib/{general_class → binary_parser/general_class}/buffered_stream.rb +0 -0
  15. data/lib/{general_class → binary_parser/general_class}/condition.rb +0 -0
  16. data/lib/{general_class → binary_parser/general_class}/expression.rb +0 -0
  17. data/lib/{general_class → binary_parser/general_class}/free_condition.rb +0 -0
  18. data/lib/{general_class → binary_parser/general_class}/memorize.rb +0 -0
  19. data/lib/{general_class → binary_parser/general_class}/proxy.rb +0 -0
  20. data/lib/{loop_list.rb → binary_parser/loop_list.rb} +5 -1
  21. data/lib/{nameless_template_maker.rb → binary_parser/nameless_template_maker.rb} +0 -0
  22. data/lib/{scope.rb → binary_parser/scope.rb} +0 -0
  23. data/lib/{stream_template_base.rb → binary_parser/stream_template_base.rb} +0 -0
  24. data/lib/{structure_definition.rb → binary_parser/structure_definition.rb} +0 -0
  25. data/lib/{template_base.rb → binary_parser/template_base.rb} +0 -0
  26. data/lib/binary_parser/version.rb +1 -1
  27. data/lib/{while_list.rb → binary_parser/while_list.rb} +0 -0
  28. data/lib/binary_parser.rb +6 -4
  29. data/unit_test/built_in_template/test_bcd.rb +43 -0
  30. metadata +26 -23
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0307ae683d47cfe6a2525daadd6e2243ef6c243
4
- data.tar.gz: 701289dee15d26ccea833011bdd85cb280153965
3
+ metadata.gz: a266f80f4fbcd0daa0c318a5cfaa13d167eb96e4
4
+ data.tar.gz: b8f1fe4f896e61d159ae5d4cdb010e01a0b6dc53
5
5
  SHA512:
6
- metadata.gz: 095aedb2598057330badc1037371333fb7ca6ef5edfb37ad26747984ba95297883883a32575e151d0f66238c48c5bb04d7834c8a2383a9368c59d06e17b1cb9e
7
- data.tar.gz: 28e0a0fe38de2617a8e90c900389c26bfdbea4d3bfac2413bbef888e0d3475c468bf3a1a3c695089dea47bcef41ec6a4b6a9aedd90a45f25cc0d05a8a473b101
6
+ metadata.gz: fcecba5a4b014c53b276ce5422f894788429bb6124ae7acaf8a4e5043f5d253ecd26a919939afd8932ba67fde298340b8c15ad26f11fe89f8de57e7727143b9e
7
+ data.tar.gz: de24b1e689b71dfdc407341ebb842d56ee8b819cc1c08c1ca0166a1c645bc6aa4f8ac3f77740dca8dea65611017e92914f7baaa9937910acd2c3c25d7aff5c34
data/README.md CHANGED
@@ -136,7 +136,7 @@ class MyImage < BinaryParser::TemplateBase
136
136
  data :has_date, Flag, 1
137
137
 
138
138
  # Condition statement
139
- # * If you want check whether variable-name is valid, alternative expression
139
+ # * If you want to check whether variable-name is valid, alternative expression
140
140
  # IF cond(:has_date){|v| v.on?} do ~ end
141
141
  # is also available.
142
142
  IF E{ has_date.on? } do
@@ -150,10 +150,11 @@ And then you can parse and read binay-data of MyImage as follows.
150
150
 
151
151
  ```ruby
152
152
  File.open('my_image.bin', 'rb') do |f|
153
- print "Image size: #{image.height}x#{image.width}\n"
153
+ image = MyImage.new(f.read)
154
+ puts "Image size: #{image.height}x#{image.width}"
154
155
  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"
156
+ puts "RGB color at the first is (#{ul.R}, #{ul.G}, #{ul.B})"
157
+ puts "Image date: #{image.date.to_date}"
157
158
  end
158
159
  ```
159
160
 
@@ -207,7 +208,8 @@ end
207
208
  You can check this definition by giving some binary-data and calling show method as follows.
208
209
 
209
210
  ```ruby
210
- i = DefExample.new([0x05, 0x01, 0xff, 0x02, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01].pack("C*"))
211
+ binary = [0x05, 0x01, 0xff, 0x02, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01].pack("C*")
212
+ i = DefExample.new(binary)
211
213
  i.show(true)
212
214
  ```
213
215
 
@@ -236,7 +238,7 @@ If you want to operate Stream-data, StreamTemplateBase class is useful. Define s
236
238
 
237
239
  ```ruby
238
240
  class StreamExample < BinaryParser::StreamTemplateBase
239
- # Stream which consists of every 4 byte binary-data.
241
+ # Stream which consists of 4bytes-binary-datas.
240
242
  Def(4) do
241
243
  data :data1, UInt, 8
242
244
  data :data2, Binary, 24
@@ -273,5 +275,5 @@ Access shown address by web browser.
273
275
 
274
276
  Versions
275
277
  --------
276
- 1.0.0 April 6, 2014
278
+ 1.0.0 April 6, 2014
277
279
  1.2.0 November 7, 2014
data/examples/example1.rb CHANGED
@@ -31,7 +31,7 @@ class MyImage < BinaryParser::TemplateBase
31
31
  data :has_date, Flag, 1
32
32
 
33
33
  # Condition statement
34
- # * If you want check whether variable-name is valid, alternative expression
34
+ # * If you want to check whether variable-name is valid, alternative expression
35
35
  # IF cond(:has_date){|v| v.on?} do ~ end
36
36
  # is also available.
37
37
  IF E{ has_date.on? } do
@@ -0,0 +1,10 @@
1
+ require File.dirname(File.expand_path(File.dirname(__FILE__))) + "/lib/binary_parser.rb"
2
+
3
+ class UseUInt8 < BinaryParser::TemplateBase
4
+ Def do
5
+ SPEND 24, :spends, UInt8
6
+ end
7
+ end
8
+
9
+ bin = [0x01, 0x02, 0x03].pack("C*")
10
+ UseUInt8.new(bin).show(true)
@@ -0,0 +1,52 @@
1
+ module BinaryParser
2
+ module BuiltInTemplate
3
+ def self.bcd_make(floating_point)
4
+ klass = Class.new(TemplateBase) do
5
+ Def do
6
+ SPEND rest, :decimals, UInt4
7
+ end
8
+
9
+ def self.floating_point
10
+ @floating_point
11
+ end
12
+
13
+ def floating_point
14
+ self.class.floating_point
15
+ end
16
+
17
+ def to_i
18
+ decimals.inject(0){|acc, n| acc * 10 + n.to_i}
19
+ end
20
+
21
+ def to_s
22
+ return to_i.to_s if floating_point == 0
23
+ to_i.to_s.insert(-(floating_point + 1), ".")
24
+ end
25
+
26
+ def to_f
27
+ to_s.to_f
28
+ end
29
+
30
+ def content_description
31
+ to_s
32
+ end
33
+ end
34
+
35
+ klass.instance_variable_set(:@floating_point, floating_point)
36
+ return klass
37
+ end
38
+
39
+
40
+ BCD = bcd_make(0)
41
+ BCD_f1 = bcd_make(1)
42
+ BCD_f2 = bcd_make(2)
43
+ BCD_f3 = bcd_make(3)
44
+ BCD_f4 = bcd_make(4)
45
+ BCD_f5 = bcd_make(5)
46
+ BCD_f6 = bcd_make(6)
47
+ BCD_f7 = bcd_make(7)
48
+ BCD_f8 = bcd_make(8)
49
+ BCD_f9 = bcd_make(9)
50
+ BCD_f10 = bcd_make(10)
51
+ end
52
+ end
@@ -5,7 +5,7 @@ module BinaryParser
5
5
  include Comparable
6
6
 
7
7
  def content_description
8
- self.to_i.to_s
8
+ "#{self.to_i.to_s} (0x#{self.to_i.to_s(16)})"
9
9
  end
10
10
 
11
11
  def to_s(base=10)
@@ -4,6 +4,16 @@ module BinaryParser
4
4
  def to_i
5
5
  entity.to_i
6
6
  end
7
+
8
+ def names
9
+ []
10
+ end
11
+ end
12
+
13
+ class UInt4 < UIntN
14
+ Def do
15
+ data :entity, UInt, 4
16
+ end
7
17
  end
8
18
 
9
19
  class UInt8 < UIntN
File without changes
@@ -44,7 +44,11 @@ module BinaryParser
44
44
  # out => Print target. Default is STDOUT.
45
45
  def show(recursively=false, out=STDOUT, depth=0)
46
46
  @list.each_with_index do |element, i|
47
- out.puts sprintf(" " * (depth * 2) + "%-5s", "[#{i}]")
47
+ out.puts sprintf(" " * (depth * 2) + "%-5s::%20s Len: %s Cont: %s",
48
+ "[#{i}]",
49
+ element.class.name ? element.class.name.split("::").last : "AnonymouseTemplate",
50
+ element.structure_bit_length,
51
+ element.content_description)
48
52
  element.show(true, out, depth + 1) if recursively
49
53
  end
50
54
  end
File without changes
@@ -1,3 +1,3 @@
1
1
  module BinaryParser
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
File without changes
data/lib/binary_parser.rb CHANGED
@@ -5,7 +5,7 @@ module BinaryParser
5
5
  LIBRARY_ROOT_PATH = File.dirname(File.expand_path(File.dirname(__FILE__)))
6
6
 
7
7
  # load general class file
8
- GENERAL_CLASS_DIR = '/lib/general_class/'
8
+ GENERAL_CLASS_DIR = '/lib/binary_parser/general_class/'
9
9
  GENERAL_CLASS_FILES =
10
10
  ['binary_manipulate_function.rb',
11
11
  'abstract_binary',
@@ -25,11 +25,12 @@ module BinaryParser
25
25
 
26
26
  # load built-in template file
27
27
  class TemplateBase; end
28
- BUILT_IN_TEMPLATE_DIR = '/lib/built_in_template/'
28
+ BUILT_IN_TEMPLATE_DIR = '/lib/binary_parser/built_in_template/'
29
29
  BUILT_IN_TEMPLATE_FILES =
30
30
  ['uint.rb',
31
31
  'flag.rb',
32
- 'binary.rb'
32
+ 'binary.rb',
33
+
33
34
  ]
34
35
 
35
36
  BUILT_IN_TEMPLATE_FILES.each do |path|
@@ -38,7 +39,7 @@ module BinaryParser
38
39
 
39
40
 
40
41
  # load library main file
41
- LIB_DIR = '/lib/'
42
+ LIB_DIR = '/lib/binary_parser/'
42
43
  LIB_FILES =
43
44
  ['loop_list.rb',
44
45
  'while_list.rb',
@@ -59,6 +60,7 @@ module BinaryParser
59
60
  SUB_BUILT_IN_TEMPLATE_FILES =
60
61
  [
61
62
  'uint_n.rb',
63
+ 'bcd.rb',
62
64
  ]
63
65
 
64
66
  SUB_BUILT_IN_TEMPLATE_FILES.each do |path|
@@ -0,0 +1,43 @@
1
+ # -*- coding: utf-8 -*-
2
+ $LIBRARY_ROOT_PATH = File.dirname(File.dirname(File.expand_path(File.dirname(__FILE__))))
3
+
4
+ module BinaryParser
5
+ module UnitTest
6
+ require 'test/unit'
7
+
8
+ # load testing target
9
+ require $LIBRARY_ROOT_PATH + '/lib/binary_parser.rb'
10
+
11
+ class BCDTemplateTest < Test::Unit::TestCase
12
+
13
+ class TestingTemplate < TemplateBase
14
+ def_structure do
15
+ data :n1, BCD, 16
16
+ data :n2, BCD_f1, 16
17
+ data :n3, BCD_f5, 32
18
+ end
19
+ end
20
+
21
+ def test_to_i
22
+ t = gen(0x20, 0x14, 0x11, 0x26, 0x01, 0x23, 0x45, 0x67)
23
+
24
+ assert_equal(2014, t.n1.to_i)
25
+ assert_equal(1126, t.n2.to_i)
26
+ assert_equal(1234567, t.n3.to_i)
27
+ end
28
+
29
+ def test_to_s
30
+ t = gen(0x20, 0x14, 0x11, 0x26, 0x01, 0x23, 0x45, 0x67)
31
+
32
+ assert_equal("2014", t.n1.to_s)
33
+ assert_equal("112.6", t.n2.to_s)
34
+ assert_equal("12.34567", t.n3.to_s)
35
+ end
36
+
37
+ # helper for generating binary
38
+ def gen(*chars)
39
+ return TestingTemplate.new(chars.pack("C*"))
40
+ end
41
+ end
42
+ end
43
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binary_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sawaken
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-07 00:00:00.000000000 Z
11
+ date: 2015-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,29 +59,32 @@ files:
59
59
  - binary_parser.gemspec
60
60
  - examples/example1.rb
61
61
  - examples/example2.rb
62
+ - examples/example4.rb
62
63
  - lib/binary_parser.rb
64
+ - lib/binary_parser/built_in_template/bcd.rb
65
+ - lib/binary_parser/built_in_template/binary.rb
66
+ - lib/binary_parser/built_in_template/flag.rb
67
+ - lib/binary_parser/built_in_template/uint.rb
68
+ - lib/binary_parser/built_in_template/uint_n.rb
69
+ - lib/binary_parser/error.rb
70
+ - lib/binary_parser/general_class/abstract_binary.rb
71
+ - lib/binary_parser/general_class/binary_manipulate_function.rb
72
+ - lib/binary_parser/general_class/bit_position.rb
73
+ - lib/binary_parser/general_class/buffered_stream.rb
74
+ - lib/binary_parser/general_class/condition.rb
75
+ - lib/binary_parser/general_class/expression.rb
76
+ - lib/binary_parser/general_class/free_condition.rb
77
+ - lib/binary_parser/general_class/memorize.rb
78
+ - lib/binary_parser/general_class/proxy.rb
79
+ - lib/binary_parser/loop_list.rb
80
+ - lib/binary_parser/nameless_template_maker.rb
81
+ - lib/binary_parser/scope.rb
82
+ - lib/binary_parser/stream_template_base.rb
83
+ - lib/binary_parser/structure_definition.rb
84
+ - lib/binary_parser/template_base.rb
63
85
  - lib/binary_parser/version.rb
64
- - lib/built_in_template/binary.rb
65
- - lib/built_in_template/flag.rb
66
- - lib/built_in_template/uint.rb
67
- - lib/built_in_template/uint_n.rb
68
- - lib/error.rb
69
- - lib/general_class/abstract_binary.rb
70
- - lib/general_class/binary_manipulate_function.rb
71
- - lib/general_class/bit_position.rb
72
- - lib/general_class/buffered_stream.rb
73
- - lib/general_class/condition.rb
74
- - lib/general_class/expression.rb
75
- - lib/general_class/free_condition.rb
76
- - lib/general_class/memorize.rb
77
- - lib/general_class/proxy.rb
78
- - lib/loop_list.rb
79
- - lib/nameless_template_maker.rb
80
- - lib/scope.rb
81
- - lib/stream_template_base.rb
82
- - lib/structure_definition.rb
83
- - lib/template_base.rb
84
- - lib/while_list.rb
86
+ - lib/binary_parser/while_list.rb
87
+ - unit_test/built_in_template/test_bcd.rb
85
88
  - unit_test/built_in_template/test_binary.rb
86
89
  - unit_test/built_in_template/test_uint.rb
87
90
  - unit_test/general_class/test_abstract_binary.rb