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/Rakefile CHANGED
@@ -10,6 +10,7 @@ PKG_FILES = FileList[
10
10
  "{examples,spec,lib}/**/*.rb",
11
11
  "tasks/**/*.rake",
12
12
  "setup.rb",
13
+ "manual.haml",
13
14
  ]
14
15
 
15
16
  task :default => :spec
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"
@@ -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.11.1"
32
+ VERSION = "1.0.0"
25
33
  end
@@ -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
- child_offset = child.do_num_bytes.is_a?(Integer) ? sum.ceil : sum.floor
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(deprecated)
294
+ def _do_num_bytes
297
295
  sum_num_bytes_for_all_elements.ceil
298
296
  end
299
297
 
@@ -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(deprecated = nil)
163
- num = do_num_bytes(deprecated)
164
- num.ceil
161
+ def num_bytes
162
+ do_num_bytes.ceil
165
163
  end
166
164
 
167
- def do_num_bytes(deprecated = nil)
168
- _do_num_bytes(deprecated)
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 = StringIO.new
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
- raise NotImplementedError
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
- raise NotImplementedError
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(deprecated)
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(ignored)
147
+ def _do_num_bytes
148
148
  value_to_binary_string(_value).length
149
149
  end
150
150
 
@@ -45,7 +45,7 @@ module BinData
45
45
  io.writebits(_value, #{nbits}, :#{endian})
46
46
  end
47
47
 
48
- def _do_num_bytes(ignored)
48
+ def _do_num_bytes
49
49
  #{nbits} / 8.0
50
50
  end
51
51
 
@@ -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 debug_name_of(child)
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(deprecated)
192
- current_choice.do_num_bytes(deprecated)
183
+ def _do_num_bytes
184
+ current_choice.do_num_bytes
193
185
  end
194
186
 
195
187
  def _assign(val)
@@ -1,148 +1,17 @@
1
1
  module BinData
2
- class Base
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
- warn "BinData::SingleValue is deprecated. Replacing with BinData::Primitive"
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 < Record
10
+ class MultiValue
19
11
  class << self
20
12
  def inherited(subclass) #:nodoc:
21
- warn "BinData::MultiValue is deprecated. Replacing with BinData::Record"
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