bindata 0.11.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of bindata might be problematic. Click here for more details.
- data/ChangeLog +11 -4
- data/NEWS +11 -0
- data/README +1054 -211
- data/Rakefile +1 -0
- data/TODO +0 -16
- data/examples/ip_address.rb +23 -0
- data/lib/bindata.rb +9 -1
- data/lib/bindata/array.rb +5 -7
- data/lib/bindata/base.rb +29 -17
- data/lib/bindata/base_primitive.rb +5 -5
- data/lib/bindata/bits.rb +1 -1
- data/lib/bindata/choice.rb +7 -15
- data/lib/bindata/deprecated.rb +4 -135
- data/lib/bindata/float.rb +1 -1
- data/lib/bindata/int.rb +1 -1
- data/lib/bindata/io.rb +11 -1
- data/lib/bindata/params.rb +11 -2
- data/lib/bindata/primitive.rb +12 -11
- data/lib/bindata/record.rb +9 -8
- data/lib/bindata/sanitize.rb +3 -1
- data/lib/bindata/string.rb +8 -3
- data/lib/bindata/stringz.rb +7 -2
- data/lib/bindata/struct.rb +11 -13
- data/lib/bindata/wrapper.rb +10 -17
- data/manual.haml +301 -0
- data/spec/array_spec.rb +1 -1
- data/spec/base_primitive_spec.rb +6 -6
- data/spec/base_spec.rb +6 -29
- data/spec/choice_spec.rb +2 -2
- data/spec/deprecated_spec.rb +4 -73
- data/spec/io_spec.rb +1 -1
- data/spec/record_spec.rb +43 -11
- data/spec/struct_spec.rb +1 -1
- data/tasks/manual.rake +36 -0
- data/tasks/pkg.rake +2 -2
- data/tasks/rdoc.rake +2 -2
- metadata +7 -5
- data/TUTORIAL +0 -949
data/spec/array_spec.rb
CHANGED
@@ -15,7 +15,7 @@ describe BinData::Array, "when instantiating" do
|
|
15
15
|
|
16
16
|
it "should fail if a given type is unknown" do
|
17
17
|
args = {:type => :does_not_exist, :initial_length => 3}
|
18
|
-
lambda { BinData::Array.new(args) }.should raise_error(
|
18
|
+
lambda { BinData::Array.new(args) }.should raise_error(BinData::UnknownTypeError)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should not allow both :initial_length and :read_until" do
|
data/spec/base_primitive_spec.rb
CHANGED
@@ -36,10 +36,10 @@ describe BinData::BasePrimitive do
|
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should conform to rule 2 for returning a value" do
|
39
|
-
io =
|
39
|
+
io = BinData::IO.new([42].pack("V"))
|
40
40
|
data = ExampleSingle.new(:value => 5)
|
41
41
|
data.expose_methods_for_testing
|
42
|
-
data.do_read(
|
42
|
+
data.do_read(io)
|
43
43
|
data.should be_in_read
|
44
44
|
data.value.should == 42
|
45
45
|
end
|
@@ -218,14 +218,14 @@ describe BinData::BasePrimitive, "with :value" do
|
|
218
218
|
end
|
219
219
|
|
220
220
|
it "should change during reading" do
|
221
|
-
io =
|
222
|
-
@data.do_read(
|
221
|
+
io = BinData::IO.new([56].pack("V"))
|
222
|
+
@data.do_read(io)
|
223
223
|
@data.value.should == 56
|
224
224
|
@data.done_read
|
225
225
|
end
|
226
226
|
|
227
227
|
it "should not change after reading" do
|
228
|
-
io =
|
228
|
+
io = BinData::IO.new([56].pack("V"))
|
229
229
|
@data.read(io)
|
230
230
|
@data.value.should == 5
|
231
231
|
end
|
@@ -238,7 +238,7 @@ end
|
|
238
238
|
|
239
239
|
describe BinData::BasePrimitive, "with :check_value" do
|
240
240
|
before(:each) do
|
241
|
-
@io =
|
241
|
+
@io = BinData::IO.new([34].pack("V"))
|
242
242
|
end
|
243
243
|
|
244
244
|
it "should succeed when check_value is non boolean and correct" do
|
data/spec/base_spec.rb
CHANGED
@@ -10,7 +10,7 @@ class BaseStub < BinData::Base
|
|
10
10
|
def _do_read(io) end
|
11
11
|
def _done_read; end
|
12
12
|
def _do_write(io) end
|
13
|
-
def _do_num_bytes
|
13
|
+
def _do_num_bytes; end
|
14
14
|
def _assign(x); end
|
15
15
|
def _snapshot; end
|
16
16
|
|
@@ -24,7 +24,7 @@ class MockBaseStub < BaseStub
|
|
24
24
|
def _do_read(io) mock._do_read(io); end
|
25
25
|
def _done_read; mock._done_read; end
|
26
26
|
def _do_write(io) mock._do_write(io); end
|
27
|
-
def _do_num_bytes
|
27
|
+
def _do_num_bytes; mock._do_num_bytes; end
|
28
28
|
def _assign(x); mock._assign(x); end
|
29
29
|
def _snapshot; mock._snapshot; end
|
30
30
|
end
|
@@ -42,12 +42,10 @@ describe BinData::Base, "when subclassing" do
|
|
42
42
|
lambda { @obj.clear }.should raise_error(NotImplementedError)
|
43
43
|
lambda { @obj.clear? }.should raise_error(NotImplementedError)
|
44
44
|
lambda { @obj.assign(nil) }.should raise_error(NotImplementedError)
|
45
|
-
lambda { @obj.debug_name_of(nil) }.should raise_error(NotImplementedError)
|
46
|
-
lambda { @obj.offset_of(nil) }.should raise_error(NotImplementedError)
|
47
45
|
lambda { @obj._do_read(nil) }.should raise_error(NotImplementedError)
|
48
46
|
lambda { @obj._done_read }.should raise_error(NotImplementedError)
|
49
47
|
lambda { @obj._do_write(nil) }.should raise_error(NotImplementedError)
|
50
|
-
lambda { @obj._do_num_bytes
|
48
|
+
lambda { @obj._do_num_bytes }.should raise_error(NotImplementedError)
|
51
49
|
lambda { @obj._snapshot }.should raise_error(NotImplementedError)
|
52
50
|
end
|
53
51
|
end
|
@@ -168,27 +166,6 @@ describe BinData::Base, "with multiple parameters" do
|
|
168
166
|
lambda { WithParamBase.new(:p1 => 1, :p2 => nil) }.should raise_error(ArgumentError)
|
169
167
|
end
|
170
168
|
|
171
|
-
=begin
|
172
|
-
# TODO: how should we evaluate internal parameters? think about this
|
173
|
-
it "should only recall mandatory, default and optional parameters" do
|
174
|
-
obj = WithParamBase.new(:p1 => 1, :p3 => 3, :p4 => 4, :p5 => 5)
|
175
|
-
obj.should have_param(:p1)
|
176
|
-
obj.should_not have_param(:p2)
|
177
|
-
obj.should have_param(:p3)
|
178
|
-
obj.should_not have_param(:p4)
|
179
|
-
obj.should_not have_param(:p5)
|
180
|
-
end
|
181
|
-
|
182
|
-
it "should evaluate mandatory, default and optional parameters" do
|
183
|
-
obj = WithParamBase.new(:p1 => 1, :p3 => lambda {1 + 2}, :p4 => 4, :p5 => 5)
|
184
|
-
obj.eval_parameter(:p1).should == 1
|
185
|
-
obj.eval_parameter(:p2).should be_nil
|
186
|
-
obj.eval_parameter(:p3).should == 3
|
187
|
-
obj.eval_parameter(:p4).should be_nil
|
188
|
-
obj.eval_parameter(:p5).should be_nil
|
189
|
-
end
|
190
|
-
=end
|
191
|
-
|
192
169
|
it "should be able to access without evaluating" do
|
193
170
|
obj = WithParamBase.new(:p1 => :asym, :p3 => lambda {1 + 2})
|
194
171
|
obj.get_parameter(:p1).should == :asym
|
@@ -207,7 +184,7 @@ describe BinData::Base, "with :check_offset" do
|
|
207
184
|
end
|
208
185
|
|
209
186
|
before(:each) do
|
210
|
-
@io =
|
187
|
+
@io = BinData::IO.create_string_io("12345678901234567890")
|
211
188
|
end
|
212
189
|
|
213
190
|
it "should fail if offset is incorrect" do
|
@@ -245,7 +222,7 @@ describe BinData::Base, "with :adjust_offset" do
|
|
245
222
|
end
|
246
223
|
|
247
224
|
before(:each) do
|
248
|
-
@io =
|
225
|
+
@io = BinData::IO.create_string_io("12345678901234567890")
|
249
226
|
end
|
250
227
|
|
251
228
|
it "should be mutually exclusive with :check_offset" do
|
@@ -317,7 +294,7 @@ describe BinData::Base, "as black box" do
|
|
317
294
|
end
|
318
295
|
|
319
296
|
obj = WriteToSBase.new
|
320
|
-
io =
|
297
|
+
io = BinData::IO.create_string_io
|
321
298
|
obj.write(io)
|
322
299
|
io.rewind
|
323
300
|
written = io.read
|
data/spec/choice_spec.rb
CHANGED
@@ -20,12 +20,12 @@ describe BinData::Choice, "when instantiating" do
|
|
20
20
|
|
21
21
|
it "should fail if a given type is unknown" do
|
22
22
|
args = {:choices => [:does_not_exist], :selection => 0}
|
23
|
-
lambda { BinData::Choice.new(args) }.should raise_error(
|
23
|
+
lambda { BinData::Choice.new(args) }.should raise_error(BinData::UnknownTypeError)
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should fail if a given type is unknown" do
|
27
27
|
args = {:choices => {0 => :does_not_exist}, :selection => 0}
|
28
|
-
lambda { BinData::Choice.new(args) }.should raise_error(
|
28
|
+
lambda { BinData::Choice.new(args) }.should raise_error(BinData::UnknownTypeError)
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should fail if :choices Hash has a symbol as key" do
|
data/spec/deprecated_spec.rb
CHANGED
@@ -5,88 +5,19 @@ require File.expand_path(File.join(File.dirname(__FILE__), "example"))
|
|
5
5
|
require 'bindata'
|
6
6
|
|
7
7
|
describe BinData::SingleValue, "when defining" do
|
8
|
-
it "should
|
8
|
+
it "should fail when inheriting from deprecated SingleValue" do
|
9
9
|
lambda {
|
10
10
|
class SubclassSingleValue < BinData::SingleValue
|
11
11
|
end
|
12
|
-
}.
|
12
|
+
}.should raise_error
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
describe BinData::MultiValue, "when defining" do
|
17
|
-
it "should
|
17
|
+
it "should fail inheriting from deprecated MultiValue" do
|
18
18
|
lambda {
|
19
19
|
class SubclassMultiValue < BinData::MultiValue
|
20
20
|
end
|
21
|
-
}.
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe BinData::Array, "with several elements" do
|
26
|
-
before(:each) do
|
27
|
-
type = [:example_single, {:initial_value => lambda { index + 1 }}]
|
28
|
-
@data = BinData::Array.new(:type => type, :initial_length => 5)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should clear a single element" do
|
32
|
-
@data[1] = 8
|
33
|
-
@data.clear(1)
|
34
|
-
@data[1].should == 2
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should test clear status of individual elements" do
|
38
|
-
@data[1] = 8
|
39
|
-
@data.clear?(0).should be_true
|
40
|
-
@data.clear?(1).should be_false
|
41
|
-
end
|
42
|
-
|
43
|
-
it "should have correct num_bytes for individual elements" do
|
44
|
-
@data.num_bytes(0).should == ExampleSingle.new.num_bytes
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should not extend on clear" do
|
48
|
-
@data.clear(9)
|
49
|
-
@data.length.should == 5
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should not extend on clear?" do
|
53
|
-
@data.clear?(9).should be_true
|
54
|
-
@data.length.should == 5
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should not extend on num_bytes" do
|
58
|
-
@data.num_bytes(9).should == 0
|
59
|
-
@data.length.should == 5
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe BinData::String, "with deprecated parameters" do
|
64
|
-
it "should substitude :trim_padding for :trim_value" do
|
65
|
-
obj = BinData::String.new(:trim_value => true)
|
66
|
-
obj.value = "abc\0"
|
67
|
-
obj.value.should == "abc"
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe BinData::Struct, "with multiple fields" do
|
72
|
-
before(:each) do
|
73
|
-
@params = { :fields => [ [:int8, :a], [:int8, :b] ] }
|
74
|
-
@obj = BinData::Struct.new(@params)
|
75
|
-
@obj.a = 1
|
76
|
-
@obj.b = 2
|
77
|
-
end
|
78
|
-
|
79
|
-
it "should return num_bytes" do
|
80
|
-
@obj.num_bytes(:a).should == 1
|
81
|
-
@obj.num_bytes(:b).should == 1
|
82
|
-
@obj.num_bytes.should == 2
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should clear individual elements" do
|
86
|
-
@obj.a = 6
|
87
|
-
@obj.b = 7
|
88
|
-
@obj.clear(:a)
|
89
|
-
@obj.should be_clear(:a)
|
90
|
-
@obj.should_not be_clear(:b)
|
21
|
+
}.should raise_error
|
91
22
|
end
|
92
23
|
end
|
data/spec/io_spec.rb
CHANGED
data/spec/record_spec.rb
CHANGED
@@ -3,48 +3,71 @@
|
|
3
3
|
require File.expand_path(File.join(File.dirname(__FILE__), "spec_common"))
|
4
4
|
require 'bindata'
|
5
5
|
|
6
|
+
def capture_exception(exception_type, &block)
|
7
|
+
block.call
|
8
|
+
rescue Exception => err
|
9
|
+
err.class.should == exception_type
|
10
|
+
return err
|
11
|
+
else
|
12
|
+
lambda {}.should raise_error(exception_type)
|
13
|
+
end
|
14
|
+
|
6
15
|
describe BinData::Record, "when defining" do
|
7
16
|
it "should fail on non registered types" do
|
8
|
-
|
17
|
+
err = capture_exception(TypeError) {
|
9
18
|
class BadTypeRecord < BinData::Record
|
10
|
-
|
19
|
+
non_registered_type :a
|
11
20
|
end
|
12
|
-
}
|
21
|
+
}
|
22
|
+
err.message.should == "unknown type 'non_registered_type' for #{BadTypeRecord}"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should give correct error message for non registered nested types" do
|
26
|
+
err = capture_exception(TypeError) {
|
27
|
+
class BadNestedTypeRecord < BinData::Record
|
28
|
+
array :a, :type => :non_registered_type
|
29
|
+
end
|
30
|
+
}
|
31
|
+
err.message.should == "unknown type 'non_registered_type' for #{BadNestedTypeRecord}"
|
13
32
|
end
|
14
33
|
|
15
34
|
it "should fail on duplicate names" do
|
16
|
-
|
35
|
+
err = capture_exception(SyntaxError) {
|
17
36
|
class DuplicateNameRecord < BinData::Record
|
18
37
|
int8 :a
|
19
38
|
int8 :b
|
20
39
|
int8 :a
|
21
40
|
end
|
22
|
-
}
|
41
|
+
}
|
42
|
+
err.message.should == "duplicate field 'a' in #{DuplicateNameRecord}"
|
23
43
|
end
|
24
44
|
|
25
45
|
it "should fail on reserved names" do
|
26
|
-
|
46
|
+
err = capture_exception(NameError) {
|
27
47
|
class ReservedNameRecord < BinData::Record
|
28
48
|
int8 :a
|
29
49
|
int8 :invert # from Hash.instance_methods
|
30
50
|
end
|
31
|
-
}
|
51
|
+
}
|
52
|
+
err.message.should == "field 'invert' is a reserved name in #{ReservedNameRecord}"
|
32
53
|
end
|
33
54
|
|
34
55
|
it "should fail when field name shadows an existing method" do
|
35
|
-
|
56
|
+
err = capture_exception(NameError) {
|
36
57
|
class ExistingNameRecord < BinData::Record
|
37
58
|
int8 :object_id
|
38
59
|
end
|
39
|
-
}
|
60
|
+
}
|
61
|
+
err.message.should == "field 'object_id' shadows an existing method in #{ExistingNameRecord}"
|
40
62
|
end
|
41
63
|
|
42
64
|
it "should fail on unknown endian" do
|
43
|
-
|
65
|
+
err = capture_exception(ArgumentError) {
|
44
66
|
class BadEndianRecord < BinData::Record
|
45
67
|
endian 'a bad value'
|
46
68
|
end
|
47
|
-
}
|
69
|
+
}
|
70
|
+
err.message.should == "unknown value for endian 'a bad value' in #{BadEndianRecord}"
|
48
71
|
end
|
49
72
|
end
|
50
73
|
|
@@ -176,11 +199,20 @@ describe BinData::Record, "with nested structs" do
|
|
176
199
|
end
|
177
200
|
|
178
201
|
it "should return correct offset" do
|
202
|
+
@obj.offset.should == 0
|
179
203
|
@obj.b.offset.should == 1
|
180
204
|
@obj.b.w.offset.should == 1
|
181
205
|
@obj.c.offset.should == 3
|
182
206
|
@obj.c.z.offset.should == 4
|
183
207
|
end
|
208
|
+
|
209
|
+
it "should return correct rel_offset" do
|
210
|
+
@obj.rel_offset.should == 0
|
211
|
+
@obj.b.rel_offset.should == 1
|
212
|
+
@obj.b.w.rel_offset.should == 0
|
213
|
+
@obj.c.rel_offset.should == 3
|
214
|
+
@obj.c.z.rel_offset.should == 1
|
215
|
+
end
|
184
216
|
end
|
185
217
|
|
186
218
|
describe BinData::Record, "with an endian defined" do
|
data/spec/struct_spec.rb
CHANGED
@@ -8,7 +8,7 @@ describe BinData::Struct, "when initializing" do
|
|
8
8
|
params = {:fields => [[:non_registered_type, :a]]}
|
9
9
|
lambda {
|
10
10
|
BinData::Struct.new(params)
|
11
|
-
}.should raise_error(
|
11
|
+
}.should raise_error(BinData::UnknownTypeError)
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should fail on duplicate names" do
|
data/tasks/manual.rake
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
load_failed = false
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'haml'
|
5
|
+
rescue LoadError
|
6
|
+
puts "Haml must be installed to build documentation"
|
7
|
+
load_failed = true
|
8
|
+
end
|
9
|
+
|
10
|
+
begin
|
11
|
+
require 'maruku'
|
12
|
+
rescue LoadError
|
13
|
+
puts "Maruku must be installed to build documentation"
|
14
|
+
load_failed = true
|
15
|
+
end
|
16
|
+
|
17
|
+
begin
|
18
|
+
require 'syntax'
|
19
|
+
rescue LoadError
|
20
|
+
puts "Syntax must be installed to build documentation"
|
21
|
+
load_failed = true
|
22
|
+
end
|
23
|
+
|
24
|
+
unless load_failed
|
25
|
+
file "manual.html" => ["README", "manual.haml"] do |t|
|
26
|
+
require 'haml/exec'
|
27
|
+
|
28
|
+
opts = Haml::Exec::Haml.new(["manual.haml", "manual.html"])
|
29
|
+
opts.parse!
|
30
|
+
end
|
31
|
+
|
32
|
+
CLOBBER.include("manual.html")
|
33
|
+
|
34
|
+
desc "Build the reference manual"
|
35
|
+
task :manual => "manual.html"
|
36
|
+
end
|
data/tasks/pkg.rake
CHANGED
@@ -13,8 +13,8 @@ begin
|
|
13
13
|
s.rubyforge_project = 'bindata'
|
14
14
|
s.require_path = 'lib'
|
15
15
|
s.has_rdoc = true
|
16
|
-
s.extra_rdoc_files = ['
|
17
|
-
s.rdoc_options << '--main' << '
|
16
|
+
s.extra_rdoc_files = ['NEWS']
|
17
|
+
s.rdoc_options << '--main' << 'NEWS'
|
18
18
|
s.files = PKG_FILES
|
19
19
|
end
|
20
20
|
|
data/tasks/rdoc.rake
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bindata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dion Mendel
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-13 00:00:00 +08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -20,10 +20,9 @@ executables: []
|
|
20
20
|
extensions: []
|
21
21
|
|
22
22
|
extra_rdoc_files:
|
23
|
-
-
|
23
|
+
- NEWS
|
24
24
|
files:
|
25
25
|
- INSTALL
|
26
|
-
- TUTORIAL
|
27
26
|
- TODO
|
28
27
|
- GPL
|
29
28
|
- Rakefile
|
@@ -32,6 +31,7 @@ files:
|
|
32
31
|
- README
|
33
32
|
- ChangeLog
|
34
33
|
- examples/gzip.rb
|
34
|
+
- examples/ip_address.rb
|
35
35
|
- spec/primitive_spec.rb
|
36
36
|
- spec/record_spec.rb
|
37
37
|
- spec/float_spec.rb
|
@@ -76,15 +76,17 @@ files:
|
|
76
76
|
- lib/bindata/wrapper.rb
|
77
77
|
- lib/bindata.rb
|
78
78
|
- tasks/rdoc.rake
|
79
|
+
- tasks/manual.rake
|
79
80
|
- tasks/pkg.rake
|
80
81
|
- tasks/rspec.rake
|
81
82
|
- setup.rb
|
83
|
+
- manual.haml
|
82
84
|
has_rdoc: true
|
83
85
|
homepage: http://bindata.rubyforge.org
|
84
86
|
post_install_message:
|
85
87
|
rdoc_options:
|
86
88
|
- --main
|
87
|
-
-
|
89
|
+
- NEWS
|
88
90
|
require_paths:
|
89
91
|
- lib
|
90
92
|
required_ruby_version: !ruby/object:Gem::Requirement
|