bindata 1.2.2 → 1.3.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.

Potentially problematic release.


This version of bindata might be problematic. Click here for more details.

Files changed (52) hide show
  1. data/ChangeLog +12 -0
  2. data/NEWS +53 -0
  3. data/Rakefile +2 -1
  4. data/examples/NBT.txt +149 -0
  5. data/examples/ip_address.rb +1 -2
  6. data/examples/list.rb +124 -0
  7. data/examples/nbt.rb +195 -0
  8. data/lib/bindata.rb +4 -3
  9. data/lib/bindata/alignment.rb +86 -0
  10. data/lib/bindata/array.rb +21 -29
  11. data/lib/bindata/base.rb +82 -81
  12. data/lib/bindata/base_primitive.rb +66 -48
  13. data/lib/bindata/choice.rb +18 -28
  14. data/lib/bindata/deprecated.rb +17 -0
  15. data/lib/bindata/dsl.rb +25 -15
  16. data/lib/bindata/int.rb +2 -2
  17. data/lib/bindata/io.rb +8 -6
  18. data/lib/bindata/offset.rb +91 -0
  19. data/lib/bindata/primitive.rb +22 -11
  20. data/lib/bindata/record.rb +40 -10
  21. data/lib/bindata/sanitize.rb +15 -30
  22. data/lib/bindata/string.rb +16 -17
  23. data/lib/bindata/stringz.rb +0 -1
  24. data/lib/bindata/struct.rb +17 -6
  25. data/lib/bindata/trace.rb +52 -0
  26. data/lib/bindata/wrapper.rb +28 -6
  27. data/manual.haml +56 -10
  28. data/manual.md +318 -113
  29. data/spec/alignment_spec.rb +61 -0
  30. data/spec/array_spec.rb +139 -178
  31. data/spec/base_primitive_spec.rb +86 -111
  32. data/spec/base_spec.rb +200 -172
  33. data/spec/bits_spec.rb +45 -53
  34. data/spec/choice_spec.rb +91 -87
  35. data/spec/deprecated_spec.rb +36 -14
  36. data/spec/float_spec.rb +16 -68
  37. data/spec/int_spec.rb +26 -27
  38. data/spec/io_spec.rb +105 -105
  39. data/spec/lazy_spec.rb +50 -50
  40. data/spec/primitive_spec.rb +36 -36
  41. data/spec/record_spec.rb +134 -134
  42. data/spec/registry_spec.rb +34 -38
  43. data/spec/rest_spec.rb +8 -11
  44. data/spec/skip_spec.rb +9 -17
  45. data/spec/spec_common.rb +4 -0
  46. data/spec/string_spec.rb +92 -115
  47. data/spec/stringz_spec.rb +41 -74
  48. data/spec/struct_spec.rb +132 -153
  49. data/spec/system_spec.rb +115 -60
  50. data/spec/wrapper_spec.rb +63 -31
  51. data/tasks/pkg.rake +1 -1
  52. metadata +15 -7
@@ -23,8 +23,8 @@ describe "lambdas with index" do
23
23
  end
24
24
 
25
25
  it "should fail if there is no containing array" do
26
- obj = NestedLambdaWithIndex.new
27
- lambda { obj.a.value }.should raise_error(NoMethodError)
26
+ subject = NestedLambdaWithIndex.new
27
+ lambda { subject.a.to_s }.should raise_error(NoMethodError)
28
28
  end
29
29
  end
30
30
 
@@ -40,8 +40,8 @@ describe "lambdas with parent" do
40
40
  nested_lambda_without_parent :x
41
41
  end
42
42
 
43
- obj = TestLambdaWithoutParent.new
44
- obj.x.b.should == 5
43
+ subject = TestLambdaWithoutParent.new
44
+ subject.x.b.should == 5
45
45
  end
46
46
 
47
47
  it "should access parent's parent when parent is specified" do
@@ -55,8 +55,8 @@ describe "lambdas with parent" do
55
55
  nested_lambda_with_parent :x
56
56
  end
57
57
 
58
- obj = TestLambdaWithParent.new
59
- obj.x.b.should == 3
58
+ subject = TestLambdaWithParent.new
59
+ subject.x.b.should == 3
60
60
  end
61
61
  end
62
62
 
@@ -81,60 +81,117 @@ describe BinData::Record, "with choice field" do
81
81
  end
82
82
 
83
83
  it "should treat choice object transparently " do
84
- obj = RecordWithChoiceField.new
84
+ subject = RecordWithChoiceField.new
85
85
 
86
- obj.x.a.should == 3
86
+ subject.x.a.should == 3
87
87
  end
88
88
 
89
89
  it "should treat nested choice object transparently " do
90
- obj = RecordWithNestedChoiceField.new
90
+ subject = RecordWithNestedChoiceField.new
91
91
 
92
- obj.x.a.should == 3
92
+ subject.x.a.should == 3
93
93
  end
94
94
 
95
95
  it "should have correct offset" do
96
- obj = RecordWithNestedChoiceField.new
97
- obj.x.b.offset.should == 1
96
+ subject = RecordWithNestedChoiceField.new
97
+ subject.x.b.offset.should == 1
98
98
  end
99
99
  end
100
100
 
101
101
  describe BinData::Array, "of bits" do
102
- before(:each) do
103
- @data = BinData::Array.new(:type => :bit1, :initial_length => 15)
104
- end
102
+ let(:data) { BinData::Array.new(:type => :bit1, :initial_length => 15) }
105
103
 
106
104
  it "should read" do
107
105
  str = [0b0001_0100, 0b1000_1000].pack("CC")
108
- @data.read(str)
109
- @data[0].should == 0
110
- @data[1].should == 0
111
- @data[2].should == 0
112
- @data[3].should == 1
113
- @data[4].should == 0
114
- @data[5].should == 1
115
- @data[6].should == 0
116
- @data[7].should == 0
117
- @data[8].should == 1
118
- @data[9].should == 0
119
- @data[10].should == 0
120
- @data[11].should == 0
121
- @data[12].should == 1
122
- @data[13].should == 0
123
- @data[14].should == 0
106
+ data.read(str)
107
+ data[0].should == 0
108
+ data[1].should == 0
109
+ data[2].should == 0
110
+ data[3].should == 1
111
+ data[4].should == 0
112
+ data[5].should == 1
113
+ data[6].should == 0
114
+ data[7].should == 0
115
+ data[8].should == 1
116
+ data[9].should == 0
117
+ data[10].should == 0
118
+ data[11].should == 0
119
+ data[12].should == 1
120
+ data[13].should == 0
121
+ data[14].should == 0
124
122
  end
125
123
 
126
124
  it "should write" do
127
- @data[3] = 1
128
- @data.to_binary_s.should == [0b0001_0000, 0b0000_0000].pack("CC")
125
+ data[3] = 1
126
+ data.to_binary_s.should == [0b0001_0000, 0b0000_0000].pack("CC")
129
127
  end
130
128
 
131
129
  it "should return num_bytes" do
132
- @data.num_bytes.should == 2
130
+ data.num_bytes.should == 2
133
131
  end
134
132
 
135
133
  it "should have correct offset" do
136
- @data[7].offset.should == 0
137
- @data[8].offset.should == 1
134
+ data[7].offset.should == 0
135
+ data[8].offset.should == 1
136
+ end
137
+ end
138
+
139
+ describe BinData::Record, "containing bitfields" do
140
+ class BCD < BinData::Primitive
141
+ bit4 :d1
142
+ bit4 :d2
143
+ bit4 :d3
144
+
145
+ def set(v)
146
+ self.d1 = (v / 100) % 10
147
+ self.d2 = (v / 10) % 10
148
+ self.d3 = v % 10
149
+ end
150
+
151
+ def get()
152
+ d1 * 100 + d2 * 10 + d3
153
+ end
154
+ end
155
+
156
+ class BitfieldRecord < BinData::Record
157
+ struct :a do
158
+ bit4 :w
159
+ end
160
+
161
+ array :b, :type => :bit1, :initial_length => 9
162
+
163
+ struct :c do
164
+ bit2 :x
165
+ end
166
+
167
+ bcd :d
168
+ bit6 :e
169
+ end
170
+
171
+ subject { BitfieldRecord.new }
172
+
173
+ it "should have correct num_bytes" do
174
+ subject.num_bytes.should == 5
175
+ end
176
+
177
+ it "should read across bitfield boundaries" do
178
+ subject.read [0b0111_0010, 0b0110_0101, 0b0010_1010, 0b1000_0101, 0b1000_0000].pack("CCCCC")
179
+
180
+ subject.a.w.should == 7
181
+ subject.b.should == [0, 0, 1, 0, 0, 1, 1, 0, 0]
182
+ subject.c.x.should == 2
183
+ subject.d.should == 954
184
+ subject.e.should == 11
185
+ end
186
+
187
+ it "should write across bitfield boundaries" do
188
+ subject.a.w = 3
189
+ subject.b[2] = 1
190
+ subject.b[5] = 1
191
+ subject.c.x = 1
192
+ subject.d = 850
193
+ subject.e = 35
194
+ subject.to_binary_s.should == [0b0011_0010, 0b0100_0011, 0b0000_1010, 0b0001_0001, 0b1000_0000].pack("CCCCC")
138
195
  end
139
196
  end
140
197
 
@@ -186,28 +243,28 @@ describe "Tracing" do
186
243
  def set(val) self.ex = val; end
187
244
  end
188
245
 
189
- obj = DebugNamePrimitive.new
246
+ subject = DebugNamePrimitive.new
190
247
 
191
248
  io = StringIO.new
192
- BinData::trace_reading(io) { obj.read("\x01") }
249
+ BinData::trace_reading(io) { subject.read("\x01") }
193
250
 
194
251
  io.value.should == ["obj-internal-.ex => 1\n", "obj => 1\n"].join("")
195
252
  end
196
253
 
197
254
  it "should trace choice selection" do
198
- obj = BinData::Choice.new(:choices => [:int8, :int16be], :selection => 0)
255
+ subject = BinData::Choice.new(:choices => [:int8, :int16be], :selection => 0)
199
256
 
200
257
  io = StringIO.new
201
- BinData::trace_reading(io) { obj.read("\x01") }
258
+ BinData::trace_reading(io) { subject.read("\x01") }
202
259
 
203
260
  io.value.should == ["obj-selection- => 0\n", "obj => 1\n"].join("")
204
261
  end
205
262
 
206
263
  it "should trim long trace values" do
207
- obj = BinData::String.new(:read_length => 40)
264
+ subject = BinData::String.new(:read_length => 40)
208
265
 
209
266
  io = StringIO.new
210
- BinData::trace_reading(io) { obj.read("0000000000111111111122222222223333333333") }
267
+ BinData::trace_reading(io) { subject.read("0000000000111111111122222222223333333333") }
211
268
 
212
269
  io.value.should == "obj => \"000000000011111111112222222222...\n"
213
270
  end
@@ -219,21 +276,20 @@ describe "Forward referencing with Primitive" do
219
276
  string :data, :read_length => :len
220
277
  end
221
278
 
279
+ subject { FRPrimitive.new }
280
+
222
281
  it "should initialise" do
223
- @obj = FRPrimitive.new
224
- @obj.snapshot.should == {"len" => 0, "data" => ""}
282
+ subject.snapshot.should == {"len" => 0, "data" => ""}
225
283
  end
226
284
 
227
285
  it "should read" do
228
- @obj = FRPrimitive.new
229
- @obj.read("\x04test")
230
- @obj.snapshot.should == {"len" => 4, "data" => "test"}
286
+ subject.read("\x04test")
287
+ subject.snapshot.should == {"len" => 4, "data" => "test"}
231
288
  end
232
289
 
233
290
  it "should set value" do
234
- @obj = FRPrimitive.new
235
- @obj.data = "hello"
236
- @obj.snapshot.should == {"len" => 5, "data" => "hello"}
291
+ subject.data = "hello"
292
+ subject.snapshot.should == {"len" => 5, "data" => "hello"}
237
293
  end
238
294
  end
239
295
 
@@ -243,21 +299,20 @@ describe "Forward referencing with Array" do
243
299
  array :data, :type => :uint8, :initial_length => :len
244
300
  end
245
301
 
302
+ subject { FRArray.new }
303
+
246
304
  it "should initialise" do
247
- @obj = FRArray.new
248
- @obj.snapshot.should == {"len" => 0, "data" => []}
305
+ subject.snapshot.should == {"len" => 0, "data" => []}
249
306
  end
250
307
 
251
308
  it "should read" do
252
- @obj = FRArray.new
253
- @obj.read("\x04\x01\x02\x03\x04")
254
- @obj.snapshot.should == {"len" => 4, "data" => [1, 2, 3, 4]}
309
+ subject.read("\x04\x01\x02\x03\x04")
310
+ subject.snapshot.should == {"len" => 4, "data" => [1, 2, 3, 4]}
255
311
  end
256
312
 
257
313
  it "should set value" do
258
- @obj = FRArray.new
259
- @obj.data = [1, 2, 3]
260
- @obj.snapshot.should == {"len" => 3, "data" => [1, 2, 3]}
314
+ subject.data = [1, 2, 3]
315
+ subject.snapshot.should == {"len" => 3, "data" => [1, 2, 3]}
261
316
  end
262
317
  end
263
318
 
@@ -271,8 +326,8 @@ describe "Evaluating custom parameters" do
271
326
  end
272
327
 
273
328
  it "should recursively evaluate parameter" do
274
- obj = CustomParameterRecord.new(:zz => 5)
275
- obj.c.eval_parameter(:custom).should == 5
329
+ subject = CustomParameterRecord.new(:zz => 5)
330
+ subject.c.eval_parameter(:custom).should == 5
276
331
  end
277
332
  end
278
333
 
@@ -45,44 +45,44 @@ describe BinData::Wrapper, "around a Primitive" do
45
45
  end
46
46
 
47
47
  it "should access custom parameter" do
48
- obj = WrappedPrimitive.new
49
- obj.assign(3)
50
- obj.should == 3
48
+ subject = WrappedPrimitive.new
49
+ subject.assign(3)
50
+ subject.should == 3
51
51
  end
52
52
 
53
53
  it "should be able to override custom default parameter" do
54
- obj = WrappedPrimitive.new(:a => 5)
55
- obj.should == 5
54
+ subject = WrappedPrimitive.new(:a => 5)
55
+ subject.should == 5
56
56
  end
57
57
 
58
58
  it "should be able to override parameter" do
59
- obj = WrappedPrimitive.new(:initial_value => 7)
60
- obj.should == 7
59
+ subject = WrappedPrimitive.new(:initial_value => 7)
60
+ subject.should == 7
61
61
  end
62
62
 
63
63
  it "should clear" do
64
- obj = WrappedPrimitive.new
65
- obj.assign(3)
66
- obj.should_not be_clear
64
+ subject = WrappedPrimitive.new
65
+ subject.assign(3)
66
+ subject.should_not be_clear
67
67
 
68
- obj.clear
69
- obj.should be_clear
68
+ subject.clear
69
+ subject.should be_clear
70
70
  end
71
71
 
72
72
  it "should read" do
73
- obj = WrappedPrimitive.new
74
- obj.assign(3)
75
- str = obj.to_binary_s
73
+ subject = WrappedPrimitive.new
74
+ subject.assign(3)
75
+ str = subject.to_binary_s
76
76
 
77
77
  WrappedPrimitive.read(str).should == 3
78
78
  end
79
79
 
80
80
  it "should respond_to and forward messages to the wrapped object" do
81
- obj = WrappedPrimitive.new
82
- obj.assign(5)
81
+ subject = WrappedPrimitive.new
82
+ subject.assign(5)
83
83
 
84
- obj.should respond_to(:value)
85
- obj.value.should == 5
84
+ subject.should respond_to(:to_int)
85
+ subject.to_int.should == 5
86
86
  end
87
87
  end
88
88
 
@@ -90,17 +90,19 @@ describe BinData::Wrapper, "around an Array" do
90
90
  class WrappedIntArray < BinData::Wrapper
91
91
  endian :big
92
92
  default_parameter :initial_element_value => 0
93
- array :type => [:uint16, {:initial_value => :initial_element_value}]
93
+ array do
94
+ uint16 :initial_value => :initial_element_value
95
+ end
94
96
  end
95
97
 
96
98
  it "should forward parameters" do
97
- obj = WrappedIntArray.new(:initial_length => 7)
98
- obj.length.should == 7
99
+ subject = WrappedIntArray.new(:initial_length => 7)
100
+ subject.length.should == 7
99
101
  end
100
102
 
101
103
  it "should be able to override default parameters" do
102
- obj = WrappedIntArray.new(:initial_length => 3, :initial_element_value => 5)
103
- obj.to_binary_s.should == "\x00\x05\x00\x05\x00\x05"
104
+ subject = WrappedIntArray.new(:initial_length => 3, :initial_element_value => 5)
105
+ subject.to_binary_s.should == "\x00\x05\x00\x05\x00\x05"
104
106
  end
105
107
  end
106
108
 
@@ -111,22 +113,52 @@ describe BinData::Wrapper, "around a Choice" do
111
113
  end
112
114
 
113
115
  it "should forward parameters" do
114
- obj = WrappedChoice.new(:selection => 'b')
115
- obj.num_bytes.should == 2
116
+ subject = WrappedChoice.new(:selection => 'b')
117
+ subject.num_bytes.should == 2
116
118
  end
117
119
  end
118
120
 
119
- describe BinData::Wrapper, "inside a struct" do
121
+ describe BinData::Wrapper, "inside a Record" do
120
122
  class WrappedUint32le < BinData::Wrapper
121
123
  uint32le
122
124
  end
123
125
 
126
+ class RecordWithWrapped < BinData::Record
127
+ wrapped_uint32le :a, :onlyif => false, :value => 1
128
+ wrapped_uint32le :b, :onlyif => true, :value => 2
129
+ end
130
+
124
131
  it "should handle onlyif" do
125
- field1 = [:wrapped_uint32le, :a, {:onlyif => false, :value => 1 }]
126
- field2 = [:wrapped_uint32le, :b, {:onlyif => true, :value => 2 }]
132
+ subject = RecordWithWrapped.new
133
+ subject.should == {'b' => 2}
134
+ end
135
+ end
136
+
137
+ describe BinData::Wrapper, "around a Record" do
138
+ class RecordToBeWrapped < BinData::Record
139
+ default_parameter :arg => 3
140
+ uint8 :a, :initial_value => :arg
141
+ uint8 :b
142
+ end
143
+
144
+ class WrappedRecord < BinData::Wrapper
145
+ record_to_be_wrapped
146
+ end
147
+
148
+ it "should forward parameters" do
149
+ subject = WrappedRecord.new(:arg => 5)
150
+ subject.a.should == 5
151
+ end
152
+
153
+ it "should assign value" do
154
+ subject = WrappedRecord.new(:b => 5)
155
+ subject.b.should == 5
156
+ end
127
157
 
128
- obj = BinData::Struct.new(:fields => [field1, field2])
129
- obj.should == {'b' => 2}
158
+ it "should assign value and forward parameters" do
159
+ subject = WrappedRecord.new({:b => 5}, :arg => 7)
160
+ subject.a.should == 7
161
+ subject.b.should == 5
130
162
  end
131
163
  end
132
164
 
@@ -16,7 +16,7 @@ begin
16
16
  s.extra_rdoc_files = ['NEWS']
17
17
  s.rdoc_options << '--main' << 'NEWS'
18
18
  s.files = PKG_FILES
19
- s.add_development_dependency('rspec')
19
+ s.add_development_dependency('rspec', [">= 1.3.0"])
20
20
  s.add_development_dependency('haml')
21
21
  s.add_development_dependency('maruku')
22
22
  s.add_development_dependency('syntax')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bindata
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 2
9
- - 2
10
- version: 1.2.2
8
+ - 3
9
+ - 1
10
+ version: 1.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dion Mendel
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-14 00:00:00 +08:00
18
+ date: 2011-01-25 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,10 +26,12 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- hash: 3
29
+ hash: 27
30
30
  segments:
31
+ - 1
32
+ - 3
31
33
  - 0
32
- version: "0"
34
+ version: 1.3.0
33
35
  type: :development
34
36
  version_requirements: *id001
35
37
  - !ruby/object:Gem::Dependency
@@ -99,6 +101,9 @@ files:
99
101
  - NEWS
100
102
  - examples/gzip.rb
101
103
  - examples/ip_address.rb
104
+ - examples/nbt.rb
105
+ - examples/NBT.txt
106
+ - examples/list.rb
102
107
  - spec/base_spec.rb
103
108
  - spec/string_spec.rb
104
109
  - spec/record_spec.rb
@@ -113,6 +118,7 @@ files:
113
118
  - spec/wrapper_spec.rb
114
119
  - spec/struct_spec.rb
115
120
  - spec/rest_spec.rb
121
+ - spec/alignment_spec.rb
116
122
  - spec/base_primitive_spec.rb
117
123
  - spec/registry_spec.rb
118
124
  - spec/int_spec.rb
@@ -133,6 +139,7 @@ files:
133
139
  - lib/bindata/base.rb
134
140
  - lib/bindata/stringz.rb
135
141
  - lib/bindata/string.rb
142
+ - lib/bindata/alignment.rb
136
143
  - lib/bindata/int.rb
137
144
  - lib/bindata/io.rb
138
145
  - lib/bindata/base_primitive.rb
@@ -141,6 +148,7 @@ files:
141
148
  - lib/bindata/record.rb
142
149
  - lib/bindata/primitive.rb
143
150
  - lib/bindata/sanitize.rb
151
+ - lib/bindata/offset.rb
144
152
  - lib/bindata/rest.rb
145
153
  - lib/bindata/deprecated.rb
146
154
  - lib/bindata/registry.rb