bindata 1.4.3 → 1.4.4
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/BSDL +1 -1
- data/ChangeLog +8 -0
- data/NEWS +7 -0
- data/lib/bindata.rb +3 -3
- data/lib/bindata/base.rb +7 -0
- data/lib/bindata/base_primitive.rb +2 -2
- data/lib/bindata/choice.rb +4 -0
- data/lib/bindata/io.rb +1 -1
- data/lib/bindata/lazy.rb +2 -2
- data/lib/bindata/sanitize.rb +9 -0
- data/lib/bindata/string.rb +33 -16
- data/manual.haml +55 -53
- data/manual.md +346 -253
- data/spec/alignment_spec.rb +7 -7
- data/spec/array_spec.rb +67 -66
- data/spec/base_primitive_spec.rb +47 -49
- data/spec/base_spec.rb +70 -73
- data/spec/bits_spec.rb +14 -14
- data/spec/choice_spec.rb +44 -41
- data/spec/count_bytes_remaining_spec.rb +8 -11
- data/spec/deprecated_spec.rb +29 -29
- data/spec/float_spec.rb +12 -12
- data/spec/int_spec.rb +24 -24
- data/spec/io_spec.rb +56 -56
- data/spec/lazy_spec.rb +41 -39
- data/spec/primitive_spec.rb +31 -33
- data/spec/record_spec.rb +55 -57
- data/spec/registry_spec.rb +25 -25
- data/spec/rest_spec.rb +5 -5
- data/spec/skip_spec.rb +3 -3
- data/spec/spec_common.rb +2 -2
- data/spec/string_spec.rb +38 -38
- data/spec/stringz_spec.rb +18 -18
- data/spec/struct_spec.rb +48 -48
- data/spec/system_spec.rb +38 -37
- data/spec/wrapper_spec.rb +21 -23
- data/tasks/pkg.rake +3 -3
- data/tasks/rdoc.rake +1 -1
- data/tasks/rspec.rake +7 -7
- metadata +51 -53
data/spec/registry_spec.rb
CHANGED
@@ -14,7 +14,7 @@ describe BinData::Registry do
|
|
14
14
|
|
15
15
|
let(:r) { BinData::Registry.new }
|
16
16
|
|
17
|
-
it "
|
17
|
+
it "lookups registered names" do
|
18
18
|
r.register('ASubClass', A)
|
19
19
|
r.register('AnotherSubClass', B)
|
20
20
|
|
@@ -24,37 +24,37 @@ describe BinData::Registry do
|
|
24
24
|
r.lookup('another_sub_class').should == B
|
25
25
|
end
|
26
26
|
|
27
|
-
it "
|
28
|
-
|
27
|
+
it "does not lookup unregistered names" do
|
28
|
+
expect {
|
29
29
|
r.lookup('a_non_existent_sub_class')
|
30
|
-
}.
|
30
|
+
}.to raise_error(BinData::UnRegisteredTypeError)
|
31
31
|
end
|
32
32
|
|
33
|
-
it "
|
33
|
+
it "unregisters names" do
|
34
34
|
r.register('ASubClass', A)
|
35
35
|
r.unregister('ASubClass')
|
36
36
|
|
37
|
-
|
37
|
+
expect {
|
38
38
|
r.lookup('ASubClass')
|
39
|
-
}.
|
39
|
+
}.to raise_error(BinData::UnRegisteredTypeError)
|
40
40
|
end
|
41
41
|
|
42
|
-
it "
|
42
|
+
it "allows overriding of registered classes" do
|
43
43
|
r.register('A', A)
|
44
44
|
r.register('A', B)
|
45
45
|
|
46
46
|
r.lookup('a').should == B
|
47
47
|
end
|
48
48
|
|
49
|
-
it "
|
49
|
+
it "converts CamelCase to underscores" do
|
50
50
|
r.underscore_name('CamelCase').should == 'camel_case'
|
51
51
|
end
|
52
52
|
|
53
|
-
it "
|
53
|
+
it "converts adjacent caps camelCase to underscores" do
|
54
54
|
r.underscore_name('XYZCamelCase').should == 'xyz_camel_case'
|
55
55
|
end
|
56
56
|
|
57
|
-
it "
|
57
|
+
it "ignores the outer nestings of classes" do
|
58
58
|
r.underscore_name('A::B::C').should == 'c'
|
59
59
|
end
|
60
60
|
end
|
@@ -62,44 +62,44 @@ end
|
|
62
62
|
describe BinData::Registry, "with numerics" do
|
63
63
|
let(:r) { BinData::RegisteredClasses }
|
64
64
|
|
65
|
-
it "
|
65
|
+
it "lookup integers with endian" do
|
66
66
|
r.lookup("int24", :big).to_s.should == "BinData::Int24be"
|
67
67
|
r.lookup("int24", :little).to_s.should == "BinData::Int24le"
|
68
68
|
r.lookup("uint24", :big).to_s.should == "BinData::Uint24be"
|
69
69
|
r.lookup("uint24", :little).to_s.should == "BinData::Uint24le"
|
70
70
|
end
|
71
71
|
|
72
|
-
it "
|
73
|
-
|
72
|
+
it "does not lookup integers without endian" do
|
73
|
+
expect {
|
74
74
|
r.lookup("int24")
|
75
|
-
}.
|
75
|
+
}.to raise_error(BinData::UnRegisteredTypeError)
|
76
76
|
end
|
77
77
|
|
78
|
-
it "
|
79
|
-
|
78
|
+
it "does not lookup non byte based integers" do
|
79
|
+
expect {
|
80
80
|
r.lookup("int3")
|
81
|
-
}.
|
82
|
-
|
81
|
+
}.to raise_error(BinData::UnRegisteredTypeError)
|
82
|
+
expect {
|
83
83
|
r.lookup("int3", :big)
|
84
|
-
}.
|
85
|
-
|
84
|
+
}.to raise_error(BinData::UnRegisteredTypeError)
|
85
|
+
expect {
|
86
86
|
r.lookup("int3", :little)
|
87
|
-
}.
|
87
|
+
}.to raise_error(BinData::UnRegisteredTypeError)
|
88
88
|
end
|
89
89
|
|
90
|
-
it "
|
90
|
+
it "lookup floats with endian" do
|
91
91
|
r.lookup("float", :big).to_s.should == "BinData::FloatBe"
|
92
92
|
r.lookup("float", :little).to_s.should == "BinData::FloatLe"
|
93
93
|
r.lookup("double", :big).to_s.should == "BinData::DoubleBe"
|
94
94
|
r.lookup("double", :little).to_s.should == "BinData::DoubleLe"
|
95
95
|
end
|
96
96
|
|
97
|
-
it "
|
97
|
+
it "lookup bits" do
|
98
98
|
r.lookup("bit5").to_s.should == "BinData::Bit5"
|
99
99
|
r.lookup("bit6le").to_s.should == "BinData::Bit6le"
|
100
100
|
end
|
101
101
|
|
102
|
-
it "
|
102
|
+
it "lookup bits by ignoring endian" do
|
103
103
|
r.lookup("bit2", :big).to_s.should == "BinData::Bit2"
|
104
104
|
r.lookup("bit3le", :big).to_s.should == "BinData::Bit3le"
|
105
105
|
r.lookup("bit2", :little).to_s.should == "BinData::Bit2"
|
data/spec/rest_spec.rb
CHANGED
@@ -6,21 +6,21 @@ require 'bindata/rest'
|
|
6
6
|
describe BinData::Rest do
|
7
7
|
it { should == "" }
|
8
8
|
|
9
|
-
it "
|
9
|
+
it "reads till end of stream" do
|
10
10
|
data = "abcdefghij"
|
11
11
|
subject.read(data).should == data
|
12
12
|
end
|
13
13
|
|
14
|
-
it "
|
14
|
+
it "allows setting value for completeness" do
|
15
15
|
subject.assign("123")
|
16
16
|
subject.should == "123"
|
17
17
|
subject.to_binary_s.should == "123"
|
18
18
|
end
|
19
19
|
|
20
|
-
it "
|
20
|
+
it "accepts BinData::BasePrimitive parameters" do
|
21
21
|
rest = BinData::Rest.new(:check_value => "abc")
|
22
|
-
|
22
|
+
expect {
|
23
23
|
rest.read("xyz")
|
24
|
-
}.
|
24
|
+
}.to raise_error(BinData::ValidityError)
|
25
25
|
end
|
26
26
|
end
|
data/spec/skip_spec.rb
CHANGED
@@ -10,17 +10,17 @@ describe BinData::Skip do
|
|
10
10
|
it { should == "" }
|
11
11
|
its(:to_binary_s) { should == "\000" * 5 }
|
12
12
|
|
13
|
-
it "
|
13
|
+
it "skips bytes" do
|
14
14
|
subject.read(io)
|
15
15
|
io.pos.should == 5
|
16
16
|
end
|
17
17
|
|
18
|
-
it "
|
18
|
+
it "has expected binary representation after setting value" do
|
19
19
|
subject.assign("123")
|
20
20
|
subject.to_binary_s.should == "\000" * 5
|
21
21
|
end
|
22
22
|
|
23
|
-
it "
|
23
|
+
it "has expected binary representation after reading" do
|
24
24
|
subject.read(io)
|
25
25
|
subject.to_binary_s.should == "\000" * 5
|
26
26
|
end
|
data/spec/spec_common.rb
CHANGED
data/spec/string_spec.rb
CHANGED
@@ -7,30 +7,30 @@ require 'bindata/string'
|
|
7
7
|
describe BinData::String, "with mutually exclusive parameters" do
|
8
8
|
it ":value and :initial_value" do
|
9
9
|
params = {:value => "", :initial_value => ""}
|
10
|
-
|
10
|
+
expect { BinData::String.new(params) }.to raise_error(ArgumentError)
|
11
11
|
end
|
12
12
|
|
13
13
|
it ":length and :read_length" do
|
14
14
|
params = {:length => 5, :read_length => 5}
|
15
|
-
|
15
|
+
expect { BinData::String.new(params) }.to raise_error(ArgumentError)
|
16
16
|
end
|
17
17
|
|
18
18
|
it ":value and :length" do
|
19
19
|
params = {:value => "", :length => 5}
|
20
|
-
|
20
|
+
expect { BinData::String.new(params) }.to raise_error(ArgumentError)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
describe BinData::String, "when assigning" do
|
25
|
-
let(:small) { BinData::String.new(:length => 3, :
|
26
|
-
let(:large) { BinData::String.new(:length => 5, :
|
25
|
+
let(:small) { BinData::String.new(:length => 3, :pad_byte => "A") }
|
26
|
+
let(:large) { BinData::String.new(:length => 5, :pad_byte => "B") }
|
27
27
|
|
28
|
-
it "
|
28
|
+
it "copies data from small to large" do
|
29
29
|
large.assign(small)
|
30
30
|
large.should == "AAABB"
|
31
31
|
end
|
32
32
|
|
33
|
-
it "
|
33
|
+
it "copies data from large to small" do
|
34
34
|
small.assign(large)
|
35
35
|
small.should == "BBB"
|
36
36
|
end
|
@@ -39,11 +39,11 @@ end
|
|
39
39
|
describe BinData::String do
|
40
40
|
subject { BinData::String.new("testing") }
|
41
41
|
|
42
|
-
it "
|
42
|
+
it "compares with regexp" do
|
43
43
|
(/es/ =~ subject).should == 1
|
44
44
|
end
|
45
45
|
|
46
|
-
it "
|
46
|
+
it "compares with regexp" do
|
47
47
|
(subject =~ /es/).should == 1
|
48
48
|
end
|
49
49
|
end
|
@@ -54,12 +54,12 @@ describe BinData::String, "with :read_length" do
|
|
54
54
|
its(:num_bytes) { should == 0 }
|
55
55
|
its(:value) { should == "" }
|
56
56
|
|
57
|
-
it "
|
57
|
+
it "reads :read_length bytes" do
|
58
58
|
subject.read("abcdefghij")
|
59
59
|
subject.should == "abcde"
|
60
60
|
end
|
61
61
|
|
62
|
-
it "
|
62
|
+
it "remembers :read_length after value is cleared" do
|
63
63
|
subject.assign("abc")
|
64
64
|
subject.num_bytes.should == 3
|
65
65
|
subject.clear
|
@@ -75,27 +75,27 @@ describe BinData::String, "with :length" do
|
|
75
75
|
its(:num_bytes) { should == 5 }
|
76
76
|
its(:value) { should == "\0\0\0\0\0" }
|
77
77
|
|
78
|
-
it "
|
78
|
+
it "retains :length after value is set" do
|
79
79
|
subject.assign("abcdefghij")
|
80
80
|
subject.num_bytes.should == 5
|
81
81
|
end
|
82
82
|
|
83
|
-
it "
|
83
|
+
it "reads :length bytes" do
|
84
84
|
subject.read("abcdefghij")
|
85
85
|
subject.should == "abcde"
|
86
86
|
end
|
87
87
|
|
88
|
-
it "
|
88
|
+
it "pads values less than :length" do
|
89
89
|
subject.assign("abc")
|
90
90
|
subject.should == "abc\0\0"
|
91
91
|
end
|
92
92
|
|
93
|
-
it "
|
93
|
+
it "accepts values exactly :length" do
|
94
94
|
subject.assign("abcde")
|
95
95
|
subject.should == "abcde"
|
96
96
|
end
|
97
97
|
|
98
|
-
it "
|
98
|
+
it "truncates values greater than :length" do
|
99
99
|
subject.assign("abcdefghij")
|
100
100
|
subject.should == "abcde"
|
101
101
|
end
|
@@ -107,13 +107,13 @@ describe BinData::String, "with :read_length and :initial_value" do
|
|
107
107
|
its(:num_bytes) { should == 10 }
|
108
108
|
its(:value) { should == "abcdefghij" }
|
109
109
|
|
110
|
-
it "
|
110
|
+
it "uses :read_length for reading" do
|
111
111
|
io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
|
112
112
|
subject.read(io)
|
113
113
|
io.pos.should == 5
|
114
114
|
end
|
115
115
|
|
116
|
-
it "
|
116
|
+
it "forgets :initial_value after reading" do
|
117
117
|
subject.read("ABCDEFGHIJKLMNOPQRST")
|
118
118
|
subject.num_bytes.should == 5
|
119
119
|
subject.should == "ABCDE"
|
@@ -126,7 +126,7 @@ describe BinData::String, "with :read_length and :value" do
|
|
126
126
|
its(:num_bytes) { should == 10 }
|
127
127
|
its(:value) { should == "abcdefghij" }
|
128
128
|
|
129
|
-
it "
|
129
|
+
it "uses :read_length for reading" do
|
130
130
|
io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
|
131
131
|
subject.read(io)
|
132
132
|
io.pos.should == 5
|
@@ -137,12 +137,12 @@ describe BinData::String, "with :read_length and :value" do
|
|
137
137
|
subject.read("ABCDEFGHIJKLMNOPQRST")
|
138
138
|
end
|
139
139
|
|
140
|
-
it "
|
140
|
+
it "is not affected by :read_length after reading" do
|
141
141
|
subject.num_bytes.should == 10
|
142
142
|
subject.should == "abcdefghij"
|
143
143
|
end
|
144
144
|
|
145
|
-
it "
|
145
|
+
it "returns read value while reading" do
|
146
146
|
subject.stub(:reading?).and_return(true)
|
147
147
|
subject.should == "ABCDE"
|
148
148
|
end
|
@@ -155,7 +155,7 @@ describe BinData::String, "with :length and :initial_value" do
|
|
155
155
|
its(:num_bytes) { should == 5 }
|
156
156
|
its(:value) { should == "abcde" }
|
157
157
|
|
158
|
-
it "
|
158
|
+
it "forgets :initial_value after reading" do
|
159
159
|
io = StringIO.new("ABCDEFGHIJKLMNOPQRST")
|
160
160
|
subject.read(io)
|
161
161
|
io.pos.should == 5
|
@@ -164,21 +164,21 @@ describe BinData::String, "with :length and :initial_value" do
|
|
164
164
|
end
|
165
165
|
end
|
166
166
|
|
167
|
-
describe BinData::String, "with :
|
168
|
-
it "
|
169
|
-
str = BinData::String.new(:length => 5, :
|
167
|
+
describe BinData::String, "with :pad_byte" do
|
168
|
+
it "accepts a numeric value for :pad_byte" do
|
169
|
+
str = BinData::String.new(:length => 5, :pad_byte => 6)
|
170
170
|
str.assign("abc")
|
171
171
|
str.should == "abc\x06\x06"
|
172
172
|
end
|
173
173
|
|
174
|
-
it "
|
175
|
-
str = BinData::String.new(:length => 5, :
|
174
|
+
it "accepts a character for :pad_byte" do
|
175
|
+
str = BinData::String.new(:length => 5, :pad_byte => "R")
|
176
176
|
str.assign("abc")
|
177
177
|
str.should == "abcRR"
|
178
178
|
end
|
179
179
|
|
180
|
-
it "
|
181
|
-
params = {:length => 5, :
|
180
|
+
it "does not accept a string for :pad_byte" do
|
181
|
+
params = {:length => 5, :pad_byte => "RR"}
|
182
182
|
lambda { BinData::String.new(params) }.should raise_error(ArgumentError)
|
183
183
|
end
|
184
184
|
end
|
@@ -194,24 +194,24 @@ describe BinData::String, "with :trim_padding" do
|
|
194
194
|
end
|
195
195
|
|
196
196
|
context "trim padding set" do
|
197
|
-
subject { BinData::String.new(:
|
197
|
+
subject { BinData::String.new(:pad_byte => 'R', :trim_padding => true) }
|
198
198
|
|
199
|
-
it "
|
199
|
+
it "trims the value" do
|
200
200
|
subject.assign("abcRR")
|
201
201
|
subject.should == "abc"
|
202
202
|
end
|
203
203
|
|
204
|
-
it "
|
204
|
+
it "does not affect num_bytes" do
|
205
205
|
subject.assign("abcRR")
|
206
206
|
subject.num_bytes.should == 5
|
207
207
|
end
|
208
208
|
|
209
|
-
it "
|
209
|
+
it "trims if last char is :pad_byte" do
|
210
210
|
subject.assign("abcRR")
|
211
211
|
subject.should == "abc"
|
212
212
|
end
|
213
213
|
|
214
|
-
it "
|
214
|
+
it "does not trim if value contains :pad_byte not at the end" do
|
215
215
|
subject.assign("abcRRde")
|
216
216
|
subject.should == "abcRRde"
|
217
217
|
end
|
@@ -230,25 +230,25 @@ describe BinData::String, "with Ruby 1.9 encodings" do
|
|
230
230
|
let(:binary_str) { "\xC3\x85\xC3\x84\xC3\x96" }
|
231
231
|
let(:utf8_str) { binary_str.dup.force_encoding('UTF-8') }
|
232
232
|
|
233
|
-
it "
|
233
|
+
it "stores assigned values as binary" do
|
234
234
|
subject.assign(utf8_str)
|
235
235
|
subject.to_binary_s.should == binary_str
|
236
236
|
end
|
237
237
|
|
238
|
-
it "
|
238
|
+
it "stores read values as binary" do
|
239
239
|
subject = UTF8String.new(:read_length => binary_str.length)
|
240
240
|
subject.read(binary_str)
|
241
241
|
|
242
242
|
subject.to_binary_s.should == binary_str
|
243
243
|
end
|
244
244
|
|
245
|
-
it "
|
245
|
+
it "returns values in correct encoding" do
|
246
246
|
subject.assign(utf8_str)
|
247
247
|
|
248
248
|
subject.snapshot.should == utf8_str
|
249
249
|
end
|
250
250
|
|
251
|
-
it "
|
251
|
+
it "has correct num_bytes" do
|
252
252
|
subject.assign(utf8_str)
|
253
253
|
|
254
254
|
subject.num_bytes.should == binary_str.length
|
data/spec/stringz_spec.rb
CHANGED
@@ -18,47 +18,47 @@ describe BinData::Stringz, "with value set" do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
describe BinData::Stringz, "when reading" do
|
21
|
-
it "
|
21
|
+
it "stops at the first zero byte" do
|
22
22
|
io = StringIO.new("abcd\0xyz\0")
|
23
23
|
subject.read(io)
|
24
24
|
io.pos.should == 5
|
25
25
|
subject.should == "abcd"
|
26
26
|
end
|
27
27
|
|
28
|
-
it "
|
28
|
+
it "handles a zero length string" do
|
29
29
|
io = StringIO.new("\0abcd")
|
30
30
|
subject.read(io)
|
31
31
|
io.pos.should == 1
|
32
32
|
subject.should == ""
|
33
33
|
end
|
34
34
|
|
35
|
-
it "
|
36
|
-
|
35
|
+
it "fails if no zero byte is found" do
|
36
|
+
expect {subject.read("abcd") }.to raise_error(EOFError)
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
40
|
describe BinData::Stringz, "when setting the value" do
|
41
|
-
it "
|
41
|
+
it "includes the zero byte in num_bytes total" do
|
42
42
|
subject.assign("abcd")
|
43
43
|
subject.num_bytes.should == 5
|
44
44
|
end
|
45
45
|
|
46
|
-
it "
|
46
|
+
it "accepts empty strings" do
|
47
47
|
subject.assign("")
|
48
48
|
subject.should == ""
|
49
49
|
end
|
50
50
|
|
51
|
-
it "
|
51
|
+
it "accepts strings that aren't zero terminated" do
|
52
52
|
subject.assign("abcd")
|
53
53
|
subject.should == "abcd"
|
54
54
|
end
|
55
55
|
|
56
|
-
it "
|
56
|
+
it "accepts strings that are zero terminated" do
|
57
57
|
subject.assign("abcd\0")
|
58
58
|
subject.should == "abcd"
|
59
59
|
end
|
60
60
|
|
61
|
-
it "
|
61
|
+
it "accepts up to the first zero byte" do
|
62
62
|
subject.assign("abcd\0xyz\0")
|
63
63
|
subject.should == "abcd"
|
64
64
|
end
|
@@ -67,51 +67,51 @@ end
|
|
67
67
|
describe BinData::Stringz, "with max_length" do
|
68
68
|
subject { BinData::Stringz.new(:max_length => 5) }
|
69
69
|
|
70
|
-
it "
|
70
|
+
it "reads less than max_length" do
|
71
71
|
io = StringIO.new("abc\0xyz")
|
72
72
|
subject.read(io)
|
73
73
|
subject.should == "abc"
|
74
74
|
end
|
75
75
|
|
76
|
-
it "
|
76
|
+
it "reads exactly max_length" do
|
77
77
|
io = StringIO.new("abcd\0xyz")
|
78
78
|
subject.read(io)
|
79
79
|
subject.should == "abcd"
|
80
80
|
end
|
81
81
|
|
82
|
-
it "
|
82
|
+
it "reads no more than max_length" do
|
83
83
|
io = StringIO.new("abcdefg\0xyz")
|
84
84
|
subject.read(io)
|
85
85
|
io.pos.should == 5
|
86
86
|
subject.should == "abcd"
|
87
87
|
end
|
88
88
|
|
89
|
-
it "
|
89
|
+
it "accepts values less than max_length" do
|
90
90
|
subject.assign("abc")
|
91
91
|
subject.should == "abc"
|
92
92
|
end
|
93
93
|
|
94
|
-
it "
|
94
|
+
it "accepts values exactly max_length" do
|
95
95
|
subject.assign("abcd")
|
96
96
|
subject.should == "abcd"
|
97
97
|
end
|
98
98
|
|
99
|
-
it "
|
99
|
+
it "trims values greater than max_length" do
|
100
100
|
subject.assign("abcde")
|
101
101
|
subject.should == "abcd"
|
102
102
|
end
|
103
103
|
|
104
|
-
it "
|
104
|
+
it "writes values greater than max_length" do
|
105
105
|
subject.assign("abcde")
|
106
106
|
subject.to_binary_s.should == "abcd\0"
|
107
107
|
end
|
108
108
|
|
109
|
-
it "
|
109
|
+
it "writes values less than max_length" do
|
110
110
|
subject.assign("abc")
|
111
111
|
subject.to_binary_s.should == "abc\0"
|
112
112
|
end
|
113
113
|
|
114
|
-
it "
|
114
|
+
it "writes values exactly max_length" do
|
115
115
|
subject.assign("abcd")
|
116
116
|
subject.to_binary_s.should == "abcd\0"
|
117
117
|
end
|