bindata 2.5.1 → 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/ChangeLog.rdoc +7 -0
- data/LICENSE +1 -1
- data/NEWS.rdoc +53 -2
- data/README.md +1 -1
- data/bindata.gemspec +1 -1
- data/lib/bindata/array.rb +1 -1
- data/lib/bindata/base.rb +9 -2
- data/lib/bindata/base_primitive.rb +5 -5
- data/lib/bindata/bits.rb +41 -47
- data/lib/bindata/buffer.rb +3 -3
- data/lib/bindata/choice.rb +3 -3
- data/lib/bindata/delayed_io.rb +3 -3
- data/lib/bindata/dsl.rb +36 -9
- data/lib/bindata/int.rb +29 -43
- data/lib/bindata/lazy.rb +9 -9
- data/lib/bindata/name.rb +13 -3
- data/lib/bindata/primitive.rb +2 -2
- data/lib/bindata/record.rb +1 -1
- data/lib/bindata/registry.rb +72 -24
- data/lib/bindata/sanitize.rb +41 -16
- data/lib/bindata/section.rb +3 -3
- data/lib/bindata/skip.rb +2 -7
- data/lib/bindata/struct.rb +14 -6
- data/lib/bindata/version.rb +1 -1
- data/lib/bindata.rb +3 -3
- data/test/base_primitive_test.rb +1 -1
- data/test/primitive_test.rb +1 -1
- data/test/record_test.rb +117 -3
- data/test/registry_test.rb +113 -60
- data/test/string_test.rb +8 -0
- data/test/struct_test.rb +65 -3
- data/test/system_test.rb +25 -1
- metadata +6 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e50992a28fe499f3fc0c25e376060609a6f89abfa391973296601c3e81692c9f
|
|
4
|
+
data.tar.gz: 5822008067da279c8e6bb5787a1b720b2a3a563ff60826c543409686ca1a188b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1335d582aae17320deefc138b14ef101dbf6b02e8272a30b1c1e9e5eed935678e4fe6c4d7e55f99eb8cf7ab65b3c9f05b2b77bfcc95520a2e6da09455801569a
|
|
7
|
+
data.tar.gz: 95b9d293bc50ee2c7438bd60488725ab4711f3ef25ab6826c86e4b5937af588fc349ce73984d8b9cfe0919999046c8001b028c03bfcbb5a87918bbf0f16a6d7b
|
data/ChangeLog.rdoc
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
= BinData Changelog
|
|
2
2
|
|
|
3
|
+
== Version 3.0.0 (2026-07-13)
|
|
4
|
+
|
|
5
|
+
* Ruby 2.x now has its own separate branch.
|
|
6
|
+
* Namespacing now uses Ruby modules, rather than prefixes.
|
|
7
|
+
* Added class names to dynamically generated ints and bits.
|
|
8
|
+
* Forward keyword arguments. Thanks to Simon Coffey.
|
|
9
|
+
|
|
3
10
|
== Version 2.5.1 (2025-04-11)
|
|
4
11
|
|
|
5
12
|
* Added changelog link to gemspec. Thanks to Mark Young.
|
data/LICENSE
CHANGED
data/NEWS.rdoc
CHANGED
|
@@ -1,3 +1,54 @@
|
|
|
1
|
+
= 3.0.0
|
|
2
|
+
|
|
3
|
+
BinData 3.0.0 requires Ruby 3.0 or greater. Support for Ruby 2.x is found in
|
|
4
|
+
BinData versions 2.5.1.
|
|
5
|
+
|
|
6
|
+
This release introduces a new way to handle namespaces. It uses the Ruby
|
|
7
|
+
module hierarchy instead of the previous flat namespace. Code using the old
|
|
8
|
+
`:search_prefix` method should still work, but consider migrating to the new
|
|
9
|
+
method.
|
|
10
|
+
|
|
11
|
+
== 2.5.x
|
|
12
|
+
|
|
13
|
+
class Ns1Foo << BinData::Record; end
|
|
14
|
+
class Ns1Bar << BinData::Record; end
|
|
15
|
+
class Ns2Foo << BinData::Record; end
|
|
16
|
+
|
|
17
|
+
class MyClass < BinData::Record
|
|
18
|
+
search_prefix ns2
|
|
19
|
+
foo :a # resolves to ns2_foo
|
|
20
|
+
bar :b # resolves to ns1_bar
|
|
21
|
+
ns1_foo :c # explicitly ns1_foo
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
== 3.0.0
|
|
25
|
+
|
|
26
|
+
module Ns1
|
|
27
|
+
class Foo << BinData::Record; end
|
|
28
|
+
class Bar << BinData::Record; end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
module Ns2
|
|
32
|
+
class Foo << BinData::Record; end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
module Ns2
|
|
36
|
+
class MyClass < BinData::Record
|
|
37
|
+
foo :a
|
|
38
|
+
bar :b
|
|
39
|
+
ns1_foo :c
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
-or-
|
|
44
|
+
|
|
45
|
+
class MyClass < BinData::Record
|
|
46
|
+
search_namespace :ns2
|
|
47
|
+
foo :a
|
|
48
|
+
bar :b
|
|
49
|
+
ns1_foo :c
|
|
50
|
+
end
|
|
51
|
+
|
|
1
52
|
= 2.4.15
|
|
2
53
|
|
|
3
54
|
This software was originally dual licensed under the Ruby license and the
|
|
@@ -8,14 +59,14 @@ BSD-2-Clause license. It is now licensed solely as BSD-2-Clause.
|
|
|
8
59
|
This release changes the internal API for sanitizing parameters. This only
|
|
9
60
|
affects those that implement the `#sanitize_parameters` method.
|
|
10
61
|
|
|
11
|
-
|
|
62
|
+
== 2.3.x
|
|
12
63
|
|
|
13
64
|
if params.needs_sanitizing?(:type)
|
|
14
65
|
el_type, el_params = params[:type]
|
|
15
66
|
params[:type] = params.create_sanitized_object_prototype(el_type, el_params)
|
|
16
67
|
end
|
|
17
68
|
|
|
18
|
-
|
|
69
|
+
== 2.4.0
|
|
19
70
|
|
|
20
71
|
params.sanitize_object_prototype(:type)
|
|
21
72
|
|
data/README.md
CHANGED
|
@@ -38,7 +38,7 @@ BinData provides a _declarative_ way to read and write structured binary data.
|
|
|
38
38
|
This means the programmer specifies *what* the format of the binary
|
|
39
39
|
data is, and BinData works out *how* to read and write data in this
|
|
40
40
|
format. It is an easier (and more readable) alternative to
|
|
41
|
-
|
|
41
|
+
Ruby's `#pack` and `#unpack` methods.
|
|
42
42
|
|
|
43
43
|
BinData makes it easy to create new data types. It supports all the common
|
|
44
44
|
primitive datatypes that are found in structured binary data formats. Support
|
data/bindata.gemspec
CHANGED
|
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
|
|
|
21
21
|
s.required_ruby_version = ">= 2.5.0"
|
|
22
22
|
|
|
23
23
|
s.add_development_dependency('rake')
|
|
24
|
-
s.add_development_dependency('minitest', "
|
|
24
|
+
s.add_development_dependency('minitest', "~> 5.0")
|
|
25
25
|
s.add_development_dependency('simplecov')
|
|
26
26
|
s.description = <<-END.gsub(/^ +/, "")
|
|
27
27
|
BinData is a declarative way to read and write binary file formats.
|
data/lib/bindata/array.rb
CHANGED
|
@@ -337,7 +337,7 @@ module BinData
|
|
|
337
337
|
params.warn_replacement_parameter(:read_length, :initial_length)
|
|
338
338
|
params.must_be_integer(:initial_length)
|
|
339
339
|
|
|
340
|
-
params.
|
|
340
|
+
params.merge_dsl_params
|
|
341
341
|
params.sanitize_object_prototype(:type)
|
|
342
342
|
end
|
|
343
343
|
end
|
data/lib/bindata/base.rb
CHANGED
|
@@ -44,14 +44,21 @@ module BinData
|
|
|
44
44
|
|
|
45
45
|
# Call this method if this class is abstract and not to be used.
|
|
46
46
|
def unregister_self
|
|
47
|
-
|
|
47
|
+
m = /(.*)::([^:].*)/.match(name)
|
|
48
|
+
namespace = m ? m[1] : ""
|
|
49
|
+
shortname = m ? m[2] : name
|
|
50
|
+
|
|
51
|
+
RegisteredClasses.unregister(namespace, shortname)
|
|
48
52
|
end
|
|
49
53
|
|
|
50
54
|
# Registers all subclasses of this class for use
|
|
51
55
|
def register_subclasses # :nodoc:
|
|
52
56
|
singleton_class.send(:undef_method, :inherited)
|
|
53
57
|
define_singleton_method(:inherited) do |subclass|
|
|
54
|
-
|
|
58
|
+
m = /(.*)::([^:].*)/.match(subclass.name)
|
|
59
|
+
namespace = m ? m[1] : ""
|
|
60
|
+
shortname = m ? m[2] : subclass
|
|
61
|
+
RegisteredClasses.register(namespace, shortname, subclass)
|
|
55
62
|
register_subclasses
|
|
56
63
|
end
|
|
57
64
|
end
|
|
@@ -93,15 +93,15 @@ module BinData
|
|
|
93
93
|
child.respond_to?(symbol, include_all) || super
|
|
94
94
|
end
|
|
95
95
|
|
|
96
|
-
def method_missing(symbol, *args, &block) # :nodoc:
|
|
96
|
+
def method_missing(symbol, *args, **kwargs, &block) # :nodoc:
|
|
97
97
|
child = snapshot
|
|
98
98
|
if child.respond_to?(symbol)
|
|
99
99
|
self.class.class_eval <<-END, __FILE__, __LINE__ + 1
|
|
100
|
-
def #{symbol}(*args, &block) # def clamp(*args, &block)
|
|
101
|
-
snapshot.#{symbol}(*args, &block) # snapshot.clamp(*args, &block)
|
|
102
|
-
end
|
|
100
|
+
def #{symbol}(*args, **kwargs, &block) # def clamp(*args, **kwargs, &block)
|
|
101
|
+
snapshot.#{symbol}(*args, **kwargs, &block) # snapshot.clamp(*args, **kwargs, &block)
|
|
102
|
+
end # end
|
|
103
103
|
END
|
|
104
|
-
child.__send__(symbol, *args, &block)
|
|
104
|
+
child.__send__(symbol, *args, **kwargs, &block)
|
|
105
105
|
else
|
|
106
106
|
super
|
|
107
107
|
end
|
data/lib/bindata/bits.rb
CHANGED
|
@@ -12,59 +12,53 @@ module BinData
|
|
|
12
12
|
def define_class(name, nbits, endian, signed = :unsigned)
|
|
13
13
|
@@mutex.synchronize do
|
|
14
14
|
unless BinData.const_defined?(name)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
BinData.module_eval <<-END
|
|
16
|
+
class #{name} < BasePrimitive
|
|
17
|
+
#{create_params_code(nbits)}
|
|
18
|
+
|
|
19
|
+
def assign(val)
|
|
20
|
+
#{create_nbits_code(nbits)}
|
|
21
|
+
#{create_clamp_code(nbits, signed)}
|
|
22
|
+
super(val)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def do_write(io)
|
|
26
|
+
#{create_nbits_code(nbits)}
|
|
27
|
+
val = _value
|
|
28
|
+
#{create_int2uint_code(nbits, signed)}
|
|
29
|
+
io.writebits(val, #{nbits}, :#{endian})
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def do_num_bytes
|
|
33
|
+
#{create_nbits_code(nbits)}
|
|
34
|
+
#{create_do_num_bytes_code(nbits)}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def bit_aligned?
|
|
38
|
+
true
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
#---------------
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def read_and_return_value(io)
|
|
45
|
+
#{create_nbits_code(nbits)}
|
|
46
|
+
val = io.readbits(#{nbits}, :#{endian})
|
|
47
|
+
#{create_uint2int_code(nbits, signed)}
|
|
48
|
+
val
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def sensible_default
|
|
52
|
+
0
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
END
|
|
20
56
|
end
|
|
21
57
|
end
|
|
22
58
|
|
|
23
59
|
BinData.const_get(name)
|
|
24
60
|
end
|
|
25
61
|
|
|
26
|
-
def define_methods(bit_class, nbits, endian, signed)
|
|
27
|
-
bit_class.module_eval <<-END
|
|
28
|
-
#{create_params_code(nbits)}
|
|
29
|
-
|
|
30
|
-
def assign(val)
|
|
31
|
-
#{create_nbits_code(nbits)}
|
|
32
|
-
#{create_clamp_code(nbits, signed)}
|
|
33
|
-
super(val)
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def do_write(io)
|
|
37
|
-
#{create_nbits_code(nbits)}
|
|
38
|
-
val = _value
|
|
39
|
-
#{create_int2uint_code(nbits, signed)}
|
|
40
|
-
io.writebits(val, #{nbits}, :#{endian})
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def do_num_bytes
|
|
44
|
-
#{create_nbits_code(nbits)}
|
|
45
|
-
#{create_do_num_bytes_code(nbits)}
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def bit_aligned?
|
|
49
|
-
true
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
#---------------
|
|
53
|
-
private
|
|
54
|
-
|
|
55
|
-
def read_and_return_value(io)
|
|
56
|
-
#{create_nbits_code(nbits)}
|
|
57
|
-
val = io.readbits(#{nbits}, :#{endian})
|
|
58
|
-
#{create_uint2int_code(nbits, signed)}
|
|
59
|
-
val
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def sensible_default
|
|
63
|
-
0
|
|
64
|
-
end
|
|
65
|
-
END
|
|
66
|
-
end
|
|
67
|
-
|
|
68
62
|
def create_params_code(nbits)
|
|
69
63
|
if nbits == :nbits
|
|
70
64
|
"mandatory_parameter :nbits"
|
data/lib/bindata/buffer.rb
CHANGED
|
@@ -84,8 +84,8 @@ module BinData
|
|
|
84
84
|
@type.respond_to?(symbol, include_all) || super
|
|
85
85
|
end
|
|
86
86
|
|
|
87
|
-
def method_missing(symbol, *args, &block) # :nodoc:
|
|
88
|
-
@type.__send__(symbol, *args, &block)
|
|
87
|
+
def method_missing(symbol, *args, **kwargs, &block) # :nodoc:
|
|
88
|
+
@type.__send__(symbol, *args, **kwargs, &block)
|
|
89
89
|
end
|
|
90
90
|
|
|
91
91
|
def do_read(io) # :nodoc:
|
|
@@ -187,7 +187,7 @@ module BinData
|
|
|
187
187
|
include MultiFieldArgSeparator
|
|
188
188
|
|
|
189
189
|
def sanitize_parameters!(obj_class, params)
|
|
190
|
-
params.
|
|
190
|
+
params.merge_dsl_params
|
|
191
191
|
params.must_be_integer(:length)
|
|
192
192
|
params.sanitize_object_prototype(:type)
|
|
193
193
|
end
|
data/lib/bindata/choice.rb
CHANGED
|
@@ -90,8 +90,8 @@ module BinData
|
|
|
90
90
|
current_choice.respond_to?(symbol, include_all) || super
|
|
91
91
|
end
|
|
92
92
|
|
|
93
|
-
def method_missing(symbol, *args, &block) # :nodoc:
|
|
94
|
-
current_choice.__send__(symbol, *args, &block)
|
|
93
|
+
def method_missing(symbol, *args, **kwargs, &block) # :nodoc:
|
|
94
|
+
current_choice.__send__(symbol, *args, **kwargs, &block)
|
|
95
95
|
end
|
|
96
96
|
|
|
97
97
|
%w[clear? assign snapshot do_read do_write do_num_bytes].each do |m|
|
|
@@ -123,7 +123,7 @@ module BinData
|
|
|
123
123
|
|
|
124
124
|
class ChoiceArgProcessor < BaseArgProcessor
|
|
125
125
|
def sanitize_parameters!(obj_class, params) # :nodoc:
|
|
126
|
-
params.
|
|
126
|
+
params.merge_dsl_params
|
|
127
127
|
|
|
128
128
|
params.sanitize_choices(:choices) do |choices|
|
|
129
129
|
hash_choices = choices_as_hash(choices)
|
data/lib/bindata/delayed_io.rb
CHANGED
|
@@ -87,8 +87,8 @@ module BinData
|
|
|
87
87
|
@type.respond_to?(symbol, include_all) || super
|
|
88
88
|
end
|
|
89
89
|
|
|
90
|
-
def method_missing(symbol, *args, &block) # :nodoc:
|
|
91
|
-
@type.__send__(symbol, *args, &block)
|
|
90
|
+
def method_missing(symbol, *args, **kwargs, &block) # :nodoc:
|
|
91
|
+
@type.__send__(symbol, *args, **kwargs, &block)
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
def abs_offset
|
|
@@ -147,7 +147,7 @@ module BinData
|
|
|
147
147
|
include MultiFieldArgSeparator
|
|
148
148
|
|
|
149
149
|
def sanitize_parameters!(obj_class, params)
|
|
150
|
-
params.
|
|
150
|
+
params.merge_dsl_params
|
|
151
151
|
params.must_be_integer(:read_abs_offset)
|
|
152
152
|
params.sanitize_object_prototype(:type)
|
|
153
153
|
end
|
data/lib/bindata/dsl.rb
CHANGED
|
@@ -42,8 +42,8 @@ module BinData
|
|
|
42
42
|
end
|
|
43
43
|
end
|
|
44
44
|
|
|
45
|
-
def method_missing(symbol, *args, &block) # :nodoc:
|
|
46
|
-
dsl_parser.__send__(symbol, *args, &block)
|
|
45
|
+
def method_missing(symbol, *args, **kwargs, &block) # :nodoc:
|
|
46
|
+
dsl_parser.__send__(symbol, *args, **kwargs, &block)
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
# Assert object is not an array or string.
|
|
@@ -67,9 +67,13 @@ module BinData
|
|
|
67
67
|
@parser_type = parser_type
|
|
68
68
|
@validator = DSLFieldValidator.new(the_class, self)
|
|
69
69
|
@endian = nil
|
|
70
|
+
|
|
71
|
+
m = /(.*)::([^:].*)/.match(@the_class.name)
|
|
72
|
+
@namespace = m ? m[1] : ""
|
|
70
73
|
end
|
|
71
74
|
|
|
72
75
|
attr_reader :parser_type
|
|
76
|
+
attr_accessor :namespace
|
|
73
77
|
|
|
74
78
|
def endian(endian = nil)
|
|
75
79
|
if endian
|
|
@@ -80,6 +84,21 @@ module BinData
|
|
|
80
84
|
@endian
|
|
81
85
|
end
|
|
82
86
|
|
|
87
|
+
def search_namespace(*args)
|
|
88
|
+
@search_namespace ||= parent_attribute(:search_namespace, []).dup
|
|
89
|
+
|
|
90
|
+
namespace = args.collect(&:to_sym).compact
|
|
91
|
+
unless namespace.empty?
|
|
92
|
+
if fields?
|
|
93
|
+
dsl_raise SyntaxError, "search_namespace must be called before defining fields"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
@search_namespace = namespace.concat(@search_namespace)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
@search_namespace
|
|
100
|
+
end
|
|
101
|
+
|
|
83
102
|
def search_prefix(*args)
|
|
84
103
|
@search_prefix ||= parent_attribute(:search_prefix, []).dup
|
|
85
104
|
|
|
@@ -107,7 +126,7 @@ module BinData
|
|
|
107
126
|
end
|
|
108
127
|
|
|
109
128
|
def fields
|
|
110
|
-
@fields ||= SanitizedFields.new(hints, parent_fields)
|
|
129
|
+
@fields ||= SanitizedFields.new(namespace, hints, parent_fields)
|
|
111
130
|
end
|
|
112
131
|
|
|
113
132
|
def dsl_params
|
|
@@ -115,9 +134,9 @@ module BinData
|
|
|
115
134
|
send(abilities.at(0), abilities.at(1))
|
|
116
135
|
end
|
|
117
136
|
|
|
118
|
-
def method_missing(*args, &block)
|
|
137
|
+
def method_missing(*args, **kwargs, &block)
|
|
119
138
|
ensure_hints
|
|
120
|
-
parse_and_append_field(*args, &block)
|
|
139
|
+
parse_and_append_field(*args, **kwargs, &block)
|
|
121
140
|
end
|
|
122
141
|
|
|
123
142
|
#-------------
|
|
@@ -142,11 +161,12 @@ module BinData
|
|
|
142
161
|
|
|
143
162
|
def ensure_hints
|
|
144
163
|
endian
|
|
164
|
+
search_namespace
|
|
145
165
|
search_prefix
|
|
146
166
|
end
|
|
147
167
|
|
|
148
168
|
def hints
|
|
149
|
-
{ endian: endian, search_prefix: search_prefix }
|
|
169
|
+
{ endian: endian, search_namespace: search_namespace, search_prefix: search_prefix }
|
|
150
170
|
end
|
|
151
171
|
|
|
152
172
|
def set_endian(endian)
|
|
@@ -179,7 +199,7 @@ module BinData
|
|
|
179
199
|
end
|
|
180
200
|
|
|
181
201
|
def parse_and_append_field(*args, &block)
|
|
182
|
-
parser = DSLFieldParser.new(hints, *args, &block)
|
|
202
|
+
parser = DSLFieldParser.new(namespace, hints, *args, &block)
|
|
183
203
|
begin
|
|
184
204
|
@validator.validate_field(parser.name)
|
|
185
205
|
append_field(parser.type, parser.name, parser.params)
|
|
@@ -239,6 +259,9 @@ module BinData
|
|
|
239
259
|
if !endian.nil?
|
|
240
260
|
result[:endian] = endian
|
|
241
261
|
end
|
|
262
|
+
if !search_namespace.empty?
|
|
263
|
+
result[:search_namespace] = search_namespace
|
|
264
|
+
end
|
|
242
265
|
if !search_prefix.empty?
|
|
243
266
|
result[:search_prefix] = search_prefix
|
|
244
267
|
end
|
|
@@ -324,9 +347,10 @@ module BinData
|
|
|
324
347
|
def class_with_endian(class_name, endian)
|
|
325
348
|
hints = {
|
|
326
349
|
endian: endian,
|
|
350
|
+
search_namespace: class_name.dsl_parser.search_namespace,
|
|
327
351
|
search_prefix: class_name.dsl_parser.search_prefix
|
|
328
352
|
}
|
|
329
|
-
RegisteredClasses.lookup(class_name, hints)
|
|
353
|
+
RegisteredClasses.lookup("", class_name, hints)
|
|
330
354
|
end
|
|
331
355
|
|
|
332
356
|
def obj_attribute(obj, attr)
|
|
@@ -337,7 +361,8 @@ module BinData
|
|
|
337
361
|
|
|
338
362
|
# Extracts the details from a field declaration.
|
|
339
363
|
class DSLFieldParser
|
|
340
|
-
def initialize(hints, symbol, *args, &block)
|
|
364
|
+
def initialize(namespace, hints, symbol, *args, &block)
|
|
365
|
+
@namespace = namespace
|
|
341
366
|
@hints = hints
|
|
342
367
|
@type = symbol
|
|
343
368
|
@name = name_from_field_declaration(args)
|
|
@@ -385,7 +410,9 @@ module BinData
|
|
|
385
410
|
|
|
386
411
|
if bindata_classes.include?(@type)
|
|
387
412
|
parser = DSLParser.new(bindata_classes[@type], @type)
|
|
413
|
+
parser.namespace = @namespace
|
|
388
414
|
parser.endian(@hints[:endian])
|
|
415
|
+
parser.search_namespace(*@hints[:search_namespace])
|
|
389
416
|
parser.search_prefix(*@hints[:search_prefix])
|
|
390
417
|
parser.instance_eval(&block)
|
|
391
418
|
|
data/lib/bindata/int.rb
CHANGED
|
@@ -12,46 +12,38 @@ module BinData
|
|
|
12
12
|
def define_class(name, nbits, endian, signed)
|
|
13
13
|
@@mutex.synchronize do
|
|
14
14
|
unless BinData.const_defined?(name)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
BinData.const_get(name)
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
def define_methods(int_class, nbits, endian, signed)
|
|
27
|
-
raise "nbits must be divisible by 8" unless (nbits % 8).zero?
|
|
15
|
+
BinData.module_eval <<-END
|
|
16
|
+
class #{name} < BasePrimitive
|
|
17
|
+
def assign(val)
|
|
18
|
+
#{create_clamp_code(nbits, signed)}
|
|
19
|
+
super(val)
|
|
20
|
+
end
|
|
28
21
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
super(val)
|
|
33
|
-
end
|
|
22
|
+
def do_num_bytes
|
|
23
|
+
#{nbits / 8}
|
|
24
|
+
end
|
|
34
25
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
end
|
|
26
|
+
#---------------
|
|
27
|
+
private
|
|
38
28
|
|
|
39
|
-
|
|
40
|
-
|
|
29
|
+
def sensible_default
|
|
30
|
+
0
|
|
31
|
+
end
|
|
41
32
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
33
|
+
def value_to_binary_string(val)
|
|
34
|
+
#{create_clamp_code(nbits, signed)}
|
|
35
|
+
#{create_to_binary_s_code(nbits, endian, signed)}
|
|
36
|
+
end
|
|
45
37
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
38
|
+
def read_and_return_value(io)
|
|
39
|
+
#{create_read_code(nbits, endian, signed)}
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
END
|
|
49
43
|
end
|
|
44
|
+
end
|
|
50
45
|
|
|
51
|
-
|
|
52
|
-
#{create_read_code(nbits, endian, signed)}
|
|
53
|
-
end
|
|
54
|
-
END
|
|
46
|
+
BinData.const_get(name)
|
|
55
47
|
end
|
|
56
48
|
|
|
57
49
|
#-------------
|
|
@@ -176,18 +168,12 @@ module BinData
|
|
|
176
168
|
end
|
|
177
169
|
|
|
178
170
|
|
|
179
|
-
# Unsigned 1 byte integer.
|
|
180
|
-
class Uint8 < BinData::BasePrimitive
|
|
181
|
-
Int.define_methods(self, 8, :little, :unsigned)
|
|
182
|
-
end
|
|
183
|
-
|
|
184
|
-
# Signed 1 byte integer.
|
|
185
|
-
class Int8 < BinData::BasePrimitive
|
|
186
|
-
Int.define_methods(self, 8, :little, :signed)
|
|
187
|
-
end
|
|
188
|
-
|
|
189
171
|
# Create classes on demand
|
|
190
172
|
module IntFactory
|
|
173
|
+
# 1 byte integers
|
|
174
|
+
Int.define_class("Uint8", 8, :big, :unsigned)
|
|
175
|
+
Int.define_class("Int8", 8, :big, :signed)
|
|
176
|
+
|
|
191
177
|
def const_missing(name)
|
|
192
178
|
mappings = {
|
|
193
179
|
/^Uint(\d+)be$/ => [:big, :unsigned],
|
data/lib/bindata/lazy.rb
CHANGED
|
@@ -62,11 +62,11 @@ module BinData
|
|
|
62
62
|
raise NoMethodError, "no index found"
|
|
63
63
|
end
|
|
64
64
|
|
|
65
|
-
def method_missing(symbol, *args)
|
|
65
|
+
def method_missing(symbol, *args, **kwargs)
|
|
66
66
|
return @overrides[symbol] if defined?(@overrides) && @overrides.key?(symbol)
|
|
67
67
|
|
|
68
68
|
if @obj.parent
|
|
69
|
-
eval_symbol_in_parent_context(symbol, args)
|
|
69
|
+
eval_symbol_in_parent_context(symbol, args, kwargs)
|
|
70
70
|
else
|
|
71
71
|
super
|
|
72
72
|
end
|
|
@@ -75,26 +75,26 @@ module BinData
|
|
|
75
75
|
#---------------
|
|
76
76
|
private
|
|
77
77
|
|
|
78
|
-
def eval_symbol_in_parent_context(symbol, args)
|
|
79
|
-
result = resolve_symbol_in_parent_context(symbol, args)
|
|
80
|
-
recursively_eval(result, args)
|
|
78
|
+
def eval_symbol_in_parent_context(symbol, args, kwargs)
|
|
79
|
+
result = resolve_symbol_in_parent_context(symbol, args, kwargs)
|
|
80
|
+
recursively_eval(result, args, kwargs)
|
|
81
81
|
end
|
|
82
82
|
|
|
83
|
-
def resolve_symbol_in_parent_context(symbol, args)
|
|
83
|
+
def resolve_symbol_in_parent_context(symbol, args, kwargs)
|
|
84
84
|
obj_parent = @obj.parent
|
|
85
85
|
|
|
86
86
|
if obj_parent.has_parameter?(symbol)
|
|
87
87
|
obj_parent.get_parameter(symbol)
|
|
88
88
|
elsif obj_parent.safe_respond_to?(symbol, true)
|
|
89
|
-
obj_parent.__send__(symbol, *args)
|
|
89
|
+
obj_parent.__send__(symbol, *args, **kwargs)
|
|
90
90
|
else
|
|
91
91
|
symbol
|
|
92
92
|
end
|
|
93
93
|
end
|
|
94
94
|
|
|
95
|
-
def recursively_eval(val, args)
|
|
95
|
+
def recursively_eval(val, args, kwargs)
|
|
96
96
|
if val.is_a?(Symbol)
|
|
97
|
-
parent.__send__(val, *args)
|
|
97
|
+
parent.__send__(val, *args, **kwargs)
|
|
98
98
|
elsif callable?(val)
|
|
99
99
|
parent.instance_exec(&val)
|
|
100
100
|
else
|
data/lib/bindata/name.rb
CHANGED
|
@@ -11,16 +11,26 @@ module BinData
|
|
|
11
11
|
# BinData::Struct.new(name: :my_struct, fields: ...)
|
|
12
12
|
# array = BinData::Array.new(type: :my_struct)
|
|
13
13
|
# </pre></code>
|
|
14
|
+
# <tt>:namespace</tt>:: The namespace that this object belongs to may be
|
|
15
|
+
# set explicitly. This is only useful when dynamically
|
|
16
|
+
# generating types.
|
|
17
|
+
# <code><pre>
|
|
18
|
+
# BinData::Struct.new(name: :my_struct, namespace: :ns1, fields: ...)
|
|
19
|
+
# BinData::Struct.new(name: :my_struct, namespace: :ns2, fields: ...)
|
|
20
|
+
# array = BinData::Array.new(namespace: :ns1, type: :my_struct)
|
|
21
|
+
# </pre></code>
|
|
14
22
|
module RegisterNamePlugin
|
|
15
23
|
|
|
16
24
|
def self.included(base) # :nodoc:
|
|
17
|
-
#
|
|
18
|
-
base.optional_parameter :name
|
|
25
|
+
# +name+ and +namespace+ may be provided explicitly.
|
|
26
|
+
base.optional_parameter :name, :namespace
|
|
19
27
|
end
|
|
20
28
|
|
|
21
29
|
def initialize_shared_instance
|
|
22
30
|
if has_parameter?(:name)
|
|
23
|
-
|
|
31
|
+
name = get_parameter(:name)
|
|
32
|
+
namespace = get_parameter(:namespace) || ""
|
|
33
|
+
RegisteredClasses.register(namespace, name, self)
|
|
24
34
|
end
|
|
25
35
|
super
|
|
26
36
|
end
|
data/lib/bindata/primitive.rb
CHANGED
|
@@ -77,9 +77,9 @@ module BinData
|
|
|
77
77
|
@struct.respond_to?(symbol, include_private) || super
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
-
def method_missing(symbol, *args, &block) # :nodoc:
|
|
80
|
+
def method_missing(symbol, *args, **kwargs, &block) # :nodoc:
|
|
81
81
|
if @struct.respond_to?(symbol)
|
|
82
|
-
@struct.__send__(symbol, *args, &block)
|
|
82
|
+
@struct.__send__(symbol, *args, **kwargs, &block)
|
|
83
83
|
else
|
|
84
84
|
super
|
|
85
85
|
end
|