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/Rakefile
CHANGED
data/TODO
CHANGED
@@ -1,22 +1,6 @@
|
|
1
|
-
== Documentation
|
2
|
-
|
3
|
-
* Write a detailed tutorial (probably as a web page).
|
4
|
-
|
5
|
-
* Need more examples.
|
6
|
-
|
7
1
|
== Pending refactorings
|
8
2
|
|
9
3
|
* Refactor registry into registry and numeric registry
|
10
4
|
update specs accordingly
|
11
5
|
|
12
6
|
* Perhaps refactor snapshot -> _snapshot et al
|
13
|
-
|
14
|
-
== Bugs
|
15
|
-
|
16
|
-
class A < BinData::Record
|
17
|
-
array :a, :type => :unknown
|
18
|
-
end
|
19
|
-
|
20
|
-
should give correct error message
|
21
|
-
|
22
|
-
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'bindata'
|
2
|
+
|
3
|
+
# A custom type representing an IP address.
|
4
|
+
# The underlying binary representation is a sequence of four octets.
|
5
|
+
# The human accessible representation is a dotted quad.
|
6
|
+
class IPAddr < BinData::Primitive
|
7
|
+
array :octets, :type => :uint8, :initial_length => 4
|
8
|
+
|
9
|
+
def set(val)
|
10
|
+
ints = val.split(/\./).collect { |int| int.to_i }
|
11
|
+
self.octets = ints
|
12
|
+
end
|
13
|
+
|
14
|
+
def get
|
15
|
+
self.octets.collect { |octet| "%d" % octet }.join(".")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
ip = IPAddr.new
|
20
|
+
ip.value = "127.0.0.1"
|
21
|
+
|
22
|
+
puts "human readable value: #{ip}" #=> 127.0.0.1
|
23
|
+
puts "binary representation: #{ip.to_binary_s.inspect}" #=> "\177\000\000\001"
|
data/lib/bindata.rb
CHANGED
@@ -20,6 +20,14 @@ require 'bindata/deprecated'
|
|
20
20
|
#
|
21
21
|
# A declarative way to read and write structured binary data.
|
22
22
|
#
|
23
|
+
# A full reference manual is available online at
|
24
|
+
# http://bindata.rubyforge.org.
|
25
|
+
#
|
26
|
+
# == License
|
27
|
+
#
|
28
|
+
# BinData is released under the same license as Ruby.
|
29
|
+
#
|
30
|
+
# Copyright (c) 2007 - 2009 Dion Mendel.
|
23
31
|
module BinData
|
24
|
-
VERSION = "0.
|
32
|
+
VERSION = "1.0.0"
|
25
33
|
end
|
data/lib/bindata/array.rb
CHANGED
@@ -62,7 +62,7 @@ module BinData
|
|
62
62
|
|
63
63
|
class << self
|
64
64
|
|
65
|
-
def sanitize_parameters!(params, sanitizer)
|
65
|
+
def sanitize_parameters!(params, sanitizer) #:nodoc:
|
66
66
|
unless params.has_parameter?(:initial_length) or
|
67
67
|
params.has_parameter?(:read_until)
|
68
68
|
# ensure one of :initial_length and :read_until exists
|
@@ -213,18 +213,16 @@ module BinData
|
|
213
213
|
elements.each { |el| yield el }
|
214
214
|
end
|
215
215
|
|
216
|
-
def debug_name_of(child)
|
216
|
+
def debug_name_of(child) #:nodoc:
|
217
217
|
index = find_index_of(child)
|
218
218
|
"#{debug_name}[#{index}]"
|
219
219
|
end
|
220
220
|
|
221
|
-
def offset_of(child)
|
221
|
+
def offset_of(child) #:nodoc:
|
222
222
|
index = find_index_of(child)
|
223
223
|
sum = sum_num_bytes_below_index(index)
|
224
224
|
|
225
|
-
|
226
|
-
|
227
|
-
offset + child_offset
|
225
|
+
child.do_num_bytes.is_a?(Integer) ? sum.ceil : sum.floor
|
228
226
|
end
|
229
227
|
|
230
228
|
#---------------
|
@@ -293,7 +291,7 @@ module BinData
|
|
293
291
|
elements.each { |el| el.do_write(io) }
|
294
292
|
end
|
295
293
|
|
296
|
-
def _do_num_bytes
|
294
|
+
def _do_num_bytes
|
297
295
|
sum_num_bytes_for_all_elements.ceil
|
298
296
|
end
|
299
297
|
|
data/lib/bindata/base.rb
CHANGED
@@ -3,7 +3,6 @@ require 'bindata/lazy'
|
|
3
3
|
require 'bindata/params'
|
4
4
|
require 'bindata/registry'
|
5
5
|
require 'bindata/sanitize'
|
6
|
-
require 'stringio'
|
7
6
|
|
8
7
|
module BinData
|
9
8
|
# Error raised when unexpected results occur when reading data from IO.
|
@@ -70,7 +69,7 @@ module BinData
|
|
70
69
|
@accepted_parameters
|
71
70
|
end
|
72
71
|
|
73
|
-
def sanitize_parameters!(params, sanitizer)
|
72
|
+
def sanitize_parameters!(params, sanitizer) #:nodoc:
|
74
73
|
end
|
75
74
|
|
76
75
|
#-------------
|
@@ -133,13 +132,13 @@ module BinData
|
|
133
132
|
self
|
134
133
|
end
|
135
134
|
|
136
|
-
def do_read(io)
|
135
|
+
def do_read(io) #:nodoc:
|
137
136
|
check_or_adjust_offset(io)
|
138
137
|
clear
|
139
138
|
_do_read(io)
|
140
139
|
end
|
141
140
|
|
142
|
-
def done_read
|
141
|
+
def done_read #:nodoc:
|
143
142
|
_done_read
|
144
143
|
end
|
145
144
|
protected :do_read, :done_read
|
@@ -153,19 +152,18 @@ module BinData
|
|
153
152
|
self
|
154
153
|
end
|
155
154
|
|
156
|
-
def do_write(io)
|
155
|
+
def do_write(io) #:nodoc:
|
157
156
|
_do_write(io)
|
158
157
|
end
|
159
158
|
protected :do_write
|
160
159
|
|
161
160
|
# Returns the number of bytes it will take to write this data.
|
162
|
-
def num_bytes
|
163
|
-
|
164
|
-
num.ceil
|
161
|
+
def num_bytes
|
162
|
+
do_num_bytes.ceil
|
165
163
|
end
|
166
164
|
|
167
|
-
def do_num_bytes
|
168
|
-
_do_num_bytes
|
165
|
+
def do_num_bytes #:nodoc:
|
166
|
+
_do_num_bytes
|
169
167
|
end
|
170
168
|
protected :do_num_bytes
|
171
169
|
|
@@ -182,7 +180,7 @@ module BinData
|
|
182
180
|
|
183
181
|
# Returns the string representation of this data object.
|
184
182
|
def to_binary_s
|
185
|
-
io =
|
183
|
+
io = BinData::IO.create_string_io
|
186
184
|
write(io)
|
187
185
|
io.rewind
|
188
186
|
io.read
|
@@ -198,6 +196,11 @@ module BinData
|
|
198
196
|
snapshot.to_s
|
199
197
|
end
|
200
198
|
|
199
|
+
# Work with Ruby's pretty-printer library.
|
200
|
+
def pretty_print(pp) #:nodoc:
|
201
|
+
pp.pp(snapshot)
|
202
|
+
end
|
203
|
+
|
201
204
|
# Returns a user friendly name of this object for debugging purposes.
|
202
205
|
def debug_name
|
203
206
|
if parent
|
@@ -209,6 +212,15 @@ module BinData
|
|
209
212
|
|
210
213
|
# Returns the offset of this object wrt to its most distant ancestor.
|
211
214
|
def offset
|
215
|
+
if parent
|
216
|
+
parent.offset + parent.offset_of(self)
|
217
|
+
else
|
218
|
+
0
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
# Returns the offset of this object wrt to its parent.
|
223
|
+
def rel_offset
|
212
224
|
if parent
|
213
225
|
parent.offset_of(self)
|
214
226
|
else
|
@@ -216,7 +228,7 @@ module BinData
|
|
216
228
|
end
|
217
229
|
end
|
218
230
|
|
219
|
-
def ==(other)
|
231
|
+
def ==(other) #:nodoc:
|
220
232
|
# double dispatch
|
221
233
|
other == snapshot
|
222
234
|
end
|
@@ -276,14 +288,14 @@ module BinData
|
|
276
288
|
|
277
289
|
# Returns the debug name of +child+. This only needs to be implemented
|
278
290
|
# by objects that contain child objects.
|
279
|
-
def debug_name_of(child)
|
280
|
-
|
291
|
+
def debug_name_of(child) #:nodoc:
|
292
|
+
debug_name
|
281
293
|
end
|
282
294
|
|
283
295
|
# Returns the offset of +child+. This only needs to be implemented
|
284
296
|
# by objects that contain child objects.
|
285
|
-
def offset_of(child)
|
286
|
-
|
297
|
+
def offset_of(child) #:nodoc:
|
298
|
+
0
|
287
299
|
end
|
288
300
|
|
289
301
|
# Reads the data for this data object from +io+.
|
@@ -302,7 +314,7 @@ module BinData
|
|
302
314
|
end
|
303
315
|
|
304
316
|
# Returns the number of bytes it will take to write this data.
|
305
|
-
def _do_num_bytes
|
317
|
+
def _do_num_bytes
|
306
318
|
raise NotImplementedError
|
307
319
|
end
|
308
320
|
|
@@ -58,12 +58,12 @@ module BinData
|
|
58
58
|
@in_read = false
|
59
59
|
end
|
60
60
|
|
61
|
-
def clear
|
61
|
+
def clear #:nodoc:
|
62
62
|
@value = nil
|
63
63
|
@in_read = false
|
64
64
|
end
|
65
65
|
|
66
|
-
def clear?
|
66
|
+
def clear? #:nodoc:
|
67
67
|
@value.nil?
|
68
68
|
end
|
69
69
|
|
@@ -77,11 +77,11 @@ module BinData
|
|
77
77
|
assign(val)
|
78
78
|
end
|
79
79
|
|
80
|
-
def respond_to?(symbol, include_private=false)
|
80
|
+
def respond_to?(symbol, include_private=false) #:nodoc:
|
81
81
|
super || value.respond_to?(symbol, include_private)
|
82
82
|
end
|
83
83
|
|
84
|
-
def method_missing(symbol, *args, &block)
|
84
|
+
def method_missing(symbol, *args, &block) #:nodoc:
|
85
85
|
if value.respond_to?(symbol)
|
86
86
|
value.__send__(symbol, *args, &block)
|
87
87
|
else
|
@@ -144,7 +144,7 @@ module BinData
|
|
144
144
|
io.writebytes(value_to_binary_string(_value))
|
145
145
|
end
|
146
146
|
|
147
|
-
def _do_num_bytes
|
147
|
+
def _do_num_bytes
|
148
148
|
value_to_binary_string(_value).length
|
149
149
|
end
|
150
150
|
|
data/lib/bindata/bits.rb
CHANGED
data/lib/bindata/choice.rb
CHANGED
@@ -61,7 +61,7 @@ module BinData
|
|
61
61
|
|
62
62
|
class << self
|
63
63
|
|
64
|
-
def sanitize_parameters!(params, sanitizer)
|
64
|
+
def sanitize_parameters!(params, sanitizer) #:nodoc:
|
65
65
|
if params.needs_sanitizing?(:choices)
|
66
66
|
choices = choices_as_hash(params[:choices])
|
67
67
|
ensure_valid_keys(choices)
|
@@ -137,27 +137,19 @@ module BinData
|
|
137
137
|
raise NoMethodError
|
138
138
|
end
|
139
139
|
|
140
|
-
def clear
|
140
|
+
def clear #:nodoc:
|
141
141
|
current_choice.clear
|
142
142
|
end
|
143
143
|
|
144
|
-
def clear?
|
144
|
+
def clear? #:nodoc:
|
145
145
|
current_choice.clear?
|
146
146
|
end
|
147
147
|
|
148
|
-
def
|
149
|
-
debug_name
|
150
|
-
end
|
151
|
-
|
152
|
-
def offset_of(child)
|
153
|
-
offset
|
154
|
-
end
|
155
|
-
|
156
|
-
def respond_to?(symbol, include_private = false)
|
148
|
+
def respond_to?(symbol, include_private = false) #:nodoc:
|
157
149
|
super || current_choice.respond_to?(symbol, include_private)
|
158
150
|
end
|
159
151
|
|
160
|
-
def method_missing(symbol, *args, &block)
|
152
|
+
def method_missing(symbol, *args, &block) #:nodoc:
|
161
153
|
current_choice.__send__(symbol, *args, &block)
|
162
154
|
end
|
163
155
|
|
@@ -188,8 +180,8 @@ module BinData
|
|
188
180
|
current_choice.do_write(io)
|
189
181
|
end
|
190
182
|
|
191
|
-
def _do_num_bytes
|
192
|
-
current_choice.do_num_bytes
|
183
|
+
def _do_num_bytes
|
184
|
+
current_choice.do_num_bytes
|
193
185
|
end
|
194
186
|
|
195
187
|
def _assign(val)
|
data/lib/bindata/deprecated.rb
CHANGED
@@ -1,148 +1,17 @@
|
|
1
1
|
module BinData
|
2
|
-
class
|
3
|
-
def single_value?
|
4
|
-
warn "#single_value? is deprecated. It should no longer be needed"
|
5
|
-
false
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
class SingleValue < Primitive
|
2
|
+
class SingleValue
|
10
3
|
class << self
|
11
4
|
def inherited(subclass) #:nodoc:
|
12
|
-
|
13
|
-
super
|
5
|
+
fail "BinData::SingleValue is deprecated. Downgrade to BinData 0.11.1.\nYou will need to make changes to your code before you can use BinData 1.0.0"
|
14
6
|
end
|
15
7
|
end
|
16
8
|
end
|
17
9
|
|
18
|
-
class MultiValue
|
10
|
+
class MultiValue
|
19
11
|
class << self
|
20
12
|
def inherited(subclass) #:nodoc:
|
21
|
-
|
22
|
-
super
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
class Registry
|
28
|
-
def Registry.instance #:nodoc:
|
29
|
-
warn "'Registry.instance' is deprecated. Replacing with 'RegisteredClasses'"
|
30
|
-
RegisteredClasses
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
class Array
|
35
|
-
alias_method :orig_clear?, :clear?
|
36
|
-
def clear?(index = nil) #:nodoc:
|
37
|
-
if index.nil?
|
38
|
-
orig_clear?
|
39
|
-
elsif index < elements.length
|
40
|
-
warn "'obj.clear?(n)' is deprecated. Replacing with 'obj[n].clear?'"
|
41
|
-
elements[index].clear?
|
42
|
-
else
|
43
|
-
true
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
alias_method :orig_clear, :clear
|
48
|
-
def clear(index = nil) #:nodoc:
|
49
|
-
if index.nil?
|
50
|
-
orig_clear
|
51
|
-
elsif index < elements.length
|
52
|
-
warn "'obj.clear(n)' is deprecated. Replacing with 'obj[n].clear'"
|
53
|
-
elements[index].clear
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
alias_method :orig__do_num_bytes, :_do_num_bytes
|
58
|
-
def _do_num_bytes(index) #:nodoc:
|
59
|
-
if index.nil?
|
60
|
-
orig__do_num_bytes(nil)
|
61
|
-
elsif index < elements.length
|
62
|
-
warn "'obj.num_bytes(n)' is deprecated. Replacing with 'obj[n].num_bytes'"
|
63
|
-
elements[index].do_num_bytes
|
64
|
-
else
|
65
|
-
0
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def append(value = nil) #:nodoc:
|
70
|
-
warn "#append is deprecated, use push or slice instead"
|
71
|
-
if value.nil?
|
72
|
-
slice(length)
|
73
|
-
else
|
74
|
-
push(value)
|
75
|
-
end
|
76
|
-
self.last
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
class String < BinData::BasePrimitive
|
81
|
-
class << self
|
82
|
-
def deprecate!(params, old_key, new_key) #:nodoc:
|
83
|
-
if params.has_parameter?(old_key)
|
84
|
-
warn ":#{old_key} is deprecated. Replacing with :#{new_key}"
|
85
|
-
params[new_key] = params.delete(old_key)
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
alias_method :orig_sanitize_parameters!, :sanitize_parameters!
|
90
|
-
def sanitize_parameters!(params, sanitizer) #:nodoc:
|
91
|
-
deprecate!(params, :trim_value, :trim_padding)
|
92
|
-
orig_sanitize_parameters!(params, sanitizer)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
|
98
|
-
class Struct < BinData::Base
|
99
|
-
class << self
|
100
|
-
def inherited(subclass) #:nodoc:
|
101
|
-
if subclass != Record
|
102
|
-
fail "error: inheriting from BinData::Struct has been deprecated. Inherit from BinData::Record instead."
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
alias_method :orig_clear, :clear
|
108
|
-
def clear(name = nil) #:nodoc:
|
109
|
-
if name.nil?
|
110
|
-
orig_clear
|
111
|
-
else
|
112
|
-
warn "'obj.clear(name)' is deprecated. Replacing with 'obj.name.clear'"
|
113
|
-
obj = find_obj_for_name(name)
|
114
|
-
obj.clear unless obj.nil?
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
alias_method :orig_clear?, :clear?
|
119
|
-
def clear?(name = nil) #:nodoc:
|
120
|
-
if name.nil?
|
121
|
-
orig_clear?
|
122
|
-
else
|
123
|
-
warn "'obj.clear?(name)' is deprecated. Replacing with 'obj.name.clear?'"
|
124
|
-
obj = find_obj_for_name(name)
|
125
|
-
obj.nil? ? true : obj.clear?
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
alias_method :orig__do_num_bytes, :_do_num_bytes
|
130
|
-
def _do_num_bytes(name) #:nodoc:
|
131
|
-
if name.nil?
|
132
|
-
orig__do_num_bytes(nil)
|
133
|
-
else
|
134
|
-
warn "'obj.num_bytes(name)' is deprecated. Replacing with 'obj.name.num_bytes'"
|
135
|
-
obj = find_obj_for_name(name)
|
136
|
-
obj.nil? ? 0 : obj.do_num_bytes
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
alias_method :orig_offset_of, :offset_of
|
141
|
-
def offset_of(child)
|
142
|
-
if child.is_a?(::String) or child.is_a?(Symbol)
|
143
|
-
fail "error: 'offset_of(#{child.inspect})' is deprecated. Use '#{child.to_s}.offset' instead"
|
13
|
+
fail "BinData::MultiValue is deprecated. Downgrade to BinData 0.11.1.\nYou will need to make changes to your code before you can use BinData 1.0.0"
|
144
14
|
end
|
145
|
-
orig_offset_of(child)
|
146
15
|
end
|
147
16
|
end
|
148
17
|
end
|