bindata 2.4.10 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog.rdoc +39 -0
  3. data/LICENSE +25 -0
  4. data/NEWS.rdoc +5 -0
  5. data/README.md +6 -9
  6. data/bindata.gemspec +9 -4
  7. data/examples/NBT.txt +1 -1
  8. data/examples/list.rb +1 -1
  9. data/lib/bindata/alignment.rb +15 -7
  10. data/lib/bindata/array.rb +54 -54
  11. data/lib/bindata/base.rb +14 -25
  12. data/lib/bindata/base_primitive.rb +24 -20
  13. data/lib/bindata/bits.rb +15 -15
  14. data/lib/bindata/buffer.rb +89 -11
  15. data/lib/bindata/choice.rb +9 -6
  16. data/lib/bindata/count_bytes_remaining.rb +1 -1
  17. data/lib/bindata/delayed_io.rb +18 -10
  18. data/lib/bindata/dsl.rb +37 -35
  19. data/lib/bindata/float.rb +3 -3
  20. data/lib/bindata/framework.rb +8 -10
  21. data/lib/bindata/int.rb +14 -16
  22. data/lib/bindata/io.rb +276 -253
  23. data/lib/bindata/name.rb +1 -1
  24. data/lib/bindata/params.rb +9 -7
  25. data/lib/bindata/primitive.rb +3 -3
  26. data/lib/bindata/registry.rb +18 -18
  27. data/lib/bindata/rest.rb +1 -1
  28. data/lib/bindata/sanitize.rb +9 -16
  29. data/lib/bindata/section.rb +97 -0
  30. data/lib/bindata/skip.rb +140 -51
  31. data/lib/bindata/string.rb +9 -9
  32. data/lib/bindata/stringz.rb +12 -10
  33. data/lib/bindata/struct.rb +92 -68
  34. data/lib/bindata/trace.rb +35 -42
  35. data/lib/bindata/transform/brotli.rb +35 -0
  36. data/lib/bindata/transform/lz4.rb +35 -0
  37. data/lib/bindata/transform/lzma.rb +35 -0
  38. data/lib/bindata/transform/xor.rb +19 -0
  39. data/lib/bindata/transform/xz.rb +35 -0
  40. data/lib/bindata/transform/zlib.rb +33 -0
  41. data/lib/bindata/transform/zstd.rb +35 -0
  42. data/lib/bindata/uint8_array.rb +2 -2
  43. data/lib/bindata/version.rb +1 -1
  44. data/lib/bindata/virtual.rb +4 -7
  45. data/lib/bindata/warnings.rb +1 -1
  46. data/lib/bindata.rb +1 -0
  47. data/test/alignment_test.rb +8 -8
  48. data/test/array_test.rb +98 -96
  49. data/test/base_primitive_test.rb +47 -47
  50. data/test/base_test.rb +24 -24
  51. data/test/bits_test.rb +15 -15
  52. data/test/buffer_test.rb +31 -22
  53. data/test/choice_test.rb +32 -32
  54. data/test/count_bytes_remaining_test.rb +8 -8
  55. data/test/delayed_io_test.rb +91 -30
  56. data/test/float_test.rb +8 -8
  57. data/test/int_test.rb +14 -14
  58. data/test/io_test.rb +110 -302
  59. data/test/lazy_test.rb +38 -38
  60. data/test/params_test.rb +19 -19
  61. data/test/primitive_test.rb +26 -26
  62. data/test/record_test.rb +99 -99
  63. data/test/registry_test.rb +43 -43
  64. data/test/rest_test.rb +5 -5
  65. data/test/section_test.rb +111 -0
  66. data/test/skip_test.rb +71 -26
  67. data/test/string_test.rb +60 -60
  68. data/test/stringz_test.rb +34 -26
  69. data/test/struct_test.rb +167 -92
  70. data/test/system_test.rb +159 -41
  71. data/test/test_helper.rb +24 -13
  72. data/test/uint8_array_test.rb +6 -6
  73. data/test/virtual_test.rb +7 -7
  74. data/test/warnings_test.rb +14 -2
  75. metadata +19 -22
  76. data/.gitignore +0 -2
  77. data/.travis.yml +0 -15
  78. data/BSDL +0 -22
  79. data/COPYING +0 -52
  80. data/INSTALL +0 -12
  81. data/lib/bindata/offset.rb +0 -94
  82. data/test/offset_test.rb +0 -100
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 651bb61168cb520dbec6550dc4ee3d566e950a0a4e3c373752a37d9ced6dc17e
4
- data.tar.gz: 543f3849f0502255c976520e3d11f2e988f0733314b905d098b8617025ef2d99
3
+ metadata.gz: 6fb38e8b0ef072a6acb74e9a623307791ba9619423e445635bf949c6ede9a90a
4
+ data.tar.gz: 026e68ad13001b0ae2922bbe909882d5bec2c44c3380ab894cf73e3fbce8de61
5
5
  SHA512:
6
- metadata.gz: 5ab4ddc205727d64b5b4f1354c6c738965745ff6736030f440c3982799351e68af06c9f1e0faff28d6e52830a0abf55d678a0ea7ffefa160f9f7bf3c309b6e7b
7
- data.tar.gz: 834bdfa27436e578043e8b92ded99a02e6fc5ca2d0e11f0503bf3d8d68c1c7c9e491e66fe59f264156fb9675437cc9f014193f42132a8e6aec3a11b6f84f5312
6
+ metadata.gz: 3a46d236c6206417f271782956e3aaa376f793ef9cc58f88de92577b10537a5cd2ce8cc8f2399e1a58d0bb1f1ab749d57f0ea6aa8532c78fd80dc0e998b3abb4
7
+ data.tar.gz: a1d2988f1bcfda8c3f1177338d34b5af0e7f62dd4eea55905ef3c49e5cb989a7b07e30d1b7d42606a4ca005bcfa3ef034829520b52df86ca9bafe25cb7f2bf1c
data/ChangeLog.rdoc CHANGED
@@ -1,5 +1,44 @@
1
1
  = BinData Changelog
2
2
 
3
+ == Version 2.5.0 (2024-02-16)
4
+
5
+ * Removed experimental :check_offset and :adjust_offset parameters.
6
+ * Ruby 2.5 is now required.
7
+ * Allow for nested tracing.
8
+ * Skip :until_valid is now fast for :asserted_value.
9
+ * Added Section - a way to transform the data stream.
10
+ * Added transforms for brotli, lz4, xor, zlib, zstd.
11
+ * Updated to current minitest
12
+ * Fixed typos. Thanks to Patrick Linnane.
13
+
14
+ == Version 2.4.15 (2023-02-07)
15
+
16
+ * Added ruby 2.4.0 requirement to gemspec. Thanks to theldoria.
17
+ * Added github CI action. Thanks to Peter Goldstein.
18
+ * Convert file from latin1 to UTF-8. Thanks to Cédric Boutillier.
19
+ * Move license from dual license to solely BSD-2-Clause.
20
+ * Remove unnecessary files from gem. Requested by Orien Madgwick.
21
+ * Allow multiple calls to auto_call_delayed_io. Thanks to dwelch-r7.
22
+
23
+ == Version 2.4.14 (2022-10-31)
24
+
25
+ * Use Comparable#clamp instead of manual calculations.
26
+ * Update tests to new minitest requirements.
27
+ * Adjust tests due to changes in ruby 3.1
28
+
29
+ == Version 2.4.13 (2022-10-16)
30
+
31
+ * Relax over-strict parameter naming requirements. Requested by
32
+ vinayak3qilabs.
33
+
34
+ == Version 2.4.12 (2022-10-03)
35
+
36
+ * Do not include DelayedIO objects when :onlyif is false.
37
+
38
+ == Version 2.4.11 (2022-09-27)
39
+
40
+ * Make DelayedIO work with :onlyif. Reported by Spencer McIntyre.
41
+
3
42
  == Version 2.4.10 (2021-05-18)
4
43
 
5
44
  * Improve speed of dynamic object creation. Reported by Charlie Ablett.
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ BSD 2-Clause License
2
+
3
+ Copyright (c) 2007-2022, Dion Mendel
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/NEWS.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ = 2.4.15
2
+
3
+ This software was originally dual licensed under the Ruby license and the
4
+ BSD-2-Clause license. It is now licensed solely as BSD-2-Clause.
5
+
1
6
  = 2.4.0
2
7
 
3
8
  This release changes the internal API for sanitizing parameters. This only
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # What is BinData?
2
2
 
3
- [![Version ](http://img.shields.io/gem/v/bindata.svg) ](https://rubygems.org/gems/bindata)
4
- [![Travis CI ](http://img.shields.io/travis/dmendel/bindata/master.svg) ](https://travis-ci.org/dmendel/bindata)
5
- [![Coverage ](http://img.shields.io/coveralls/dmendel/bindata.svg) ](https://coveralls.io/r/dmendel/bindata)
3
+ [![Github CI](https://github.com/dmendel/bindata/actions/workflows/ci.yml/badge.svg)](https://github.com/dmendel/bindata/actions/workflows/ci.yml)
4
+ [![Version](https://img.shields.io/gem/v/bindata.svg)](https://rubygems.org/gems/bindata)
5
+ [![Downloads](https://img.shields.io/gem/dt/bindata.svg)](https://rubygems.org/gems/bindata)
6
+ [![Coverage](https://img.shields.io/coveralls/dmendel/bindata.svg)](https://coveralls.io/r/dmendel/bindata)
6
7
 
7
8
  Do you ever find yourself writing code like this?
8
9
 
@@ -14,7 +15,7 @@ width, height = io.read(8).unpack("VV")
14
15
  puts "Rectangle #{name} is #{width} x #{height}"
15
16
  ```
16
17
 
17
- It’s ugly, violates DRY and feels like you’re writing Perl, not Ruby.
18
+ It’s ugly, violates DRY and doesn't feel like Ruby.
18
19
 
19
20
  There is a better way. Here’s how you’d write the above using BinData.
20
21
 
@@ -47,13 +48,9 @@ for dependent and variable length fields is built in.
47
48
 
48
49
  $ gem install bindata
49
50
 
50
- or if running ruby 1.8
51
-
52
- $ gem install bindata -v '~> 1.8.0'
53
-
54
51
  # Documentation
55
52
 
56
- [Read the wiki](http://github.com/dmendel/bindata/wiki).
53
+ [BinData manual](http://github.com/dmendel/bindata/wiki).
57
54
 
58
55
  # Contact
59
56
 
data/bindata.gemspec CHANGED
@@ -12,12 +12,17 @@ Gem::Specification.new do |s|
12
12
  s.require_path = 'lib'
13
13
  s.extra_rdoc_files = ['NEWS.rdoc']
14
14
  s.rdoc_options << '--main' << 'NEWS.rdoc'
15
- s.files = `git ls-files`.split("\n")
16
- s.license = 'Ruby'
15
+ s.files = Dir.chdir(__dir__) do
16
+ `git ls-files -z`.split("\x0").reject do |file|
17
+ file.start_with?(*%w[.git INSTALL])
18
+ end
19
+ end
20
+ s.license = 'BSD-2-Clause'
21
+ s.required_ruby_version = ">= 2.5.0"
17
22
 
18
23
  s.add_development_dependency('rake')
19
- s.add_development_dependency('minitest', "> 5.0.0", "< 5.12.0")
20
- s.add_development_dependency('coveralls')
24
+ s.add_development_dependency('minitest', "> 5.0.0")
25
+ s.add_development_dependency('simplecov')
21
26
  s.description = <<-END.gsub(/^ +/, "")
22
27
  BinData is a declarative way to read and write binary file formats.
23
28
 
data/examples/NBT.txt CHANGED
@@ -107,7 +107,7 @@ You should end up with this:
107
107
  TAG_Short("shortTest"): 32767
108
108
  TAG_Long("longTest"): 9223372036854775807
109
109
  TAG_Float("floatTest"): 0.49823147
110
- TAG_String("stringTest"): HELLO WORLD THIS IS A TEST STRING ���!
110
+ TAG_String("stringTest"): HELLO WORLD THIS IS A TEST STRING ÅÄÖ!
111
111
  TAG_Int("intTest"): 2147483647
112
112
  TAG_Compound("nested compound test"): 2 entries
113
113
  {
data/examples/list.rb CHANGED
@@ -35,7 +35,7 @@ require 'bindata'
35
35
  # end
36
36
  # end
37
37
  #
38
- # Notice how we get stuck on attemping to write a declaration for
38
+ # Notice how we get stuck on attempting to write a declaration for
39
39
  # the contents of the list. We can't determine if the list item is
40
40
  # an atom or list because we haven't read it yet. It appears that
41
41
  # we can't proceed.
@@ -19,11 +19,11 @@ module BinData
19
19
  def do_num_bytes; 0; end
20
20
 
21
21
  def do_read(io)
22
- io.reset_read_bits
22
+ io.readbytes(0)
23
23
  end
24
24
 
25
25
  def do_write(io)
26
- io.flushbits
26
+ io.writebytes("")
27
27
  end
28
28
  end
29
29
 
@@ -45,18 +45,26 @@ module BinData
45
45
  def initialize(io)
46
46
  @io = io
47
47
  end
48
+
49
+ def binary_string(str)
50
+ str.to_s.dup.force_encoding(Encoding::BINARY)
51
+ end
52
+
48
53
  def readbytes(n)
49
- n.times.inject("") do |bytes, _|
50
- bytes += @io.readbits(8, :big).chr
54
+ n.times.inject(binary_string("")) do |bytes, _|
55
+ bytes + @io.readbits(8, :big).chr
51
56
  end
52
57
  end
58
+ def writebytes(str)
59
+ str.each_byte { |v| @io.writebits(v, 8, :big) }
60
+ end
53
61
  end
54
62
 
55
63
  def bit_aligned?
56
64
  true
57
65
  end
58
66
 
59
- def read_and_return_value(io)
67
+ def do_read(io)
60
68
  super(BitAlignedIO.new(io))
61
69
  end
62
70
 
@@ -65,7 +73,7 @@ module BinData
65
73
  end
66
74
 
67
75
  def do_write(io)
68
- value_to_binary_string(_value).each_byte { |v| io.writebits(v, 8, :big) }
76
+ super(BitAlignedIO.new(io))
69
77
  end
70
78
  end
71
79
 
@@ -74,6 +82,6 @@ module BinData
74
82
  end
75
83
 
76
84
  def Primitive.bit_aligned
77
- fail "'bit_aligned' is not needed for BinData::Primitives"
85
+ fail "'bit_aligned' is not supported for BinData::Primitives"
78
86
  end
79
87
  end
data/lib/bindata/array.rb CHANGED
@@ -72,18 +72,18 @@ module BinData
72
72
  end
73
73
 
74
74
  def initialize_instance
75
- @element_list = nil
75
+ @elements = nil
76
76
  end
77
77
 
78
78
  def clear?
79
- @element_list.nil? || elements.all?(&:clear?)
79
+ @elements.nil? || elements.all?(&:clear?)
80
80
  end
81
81
 
82
82
  def assign(array)
83
83
  return if self.equal?(array) # prevent self assignment
84
84
  raise ArgumentError, "can't set a nil value for #{debug_name}" if array.nil?
85
85
 
86
- @element_list = []
86
+ @elements = []
87
87
  concat(array)
88
88
  end
89
89
 
@@ -220,23 +220,23 @@ module BinData
220
220
  elements.each { |el| yield el }
221
221
  end
222
222
 
223
- def debug_name_of(child) #:nodoc:
223
+ def debug_name_of(child) # :nodoc:
224
224
  index = find_index_of(child)
225
225
  "#{debug_name}[#{index}]"
226
226
  end
227
227
 
228
- def offset_of(child) #:nodoc:
228
+ def offset_of(child) # :nodoc:
229
229
  index = find_index_of(child)
230
230
  sum = sum_num_bytes_below_index(index)
231
231
 
232
232
  child.bit_aligned? ? sum.floor : sum.ceil
233
233
  end
234
234
 
235
- def do_write(io) #:nodoc:
235
+ def do_write(io) # :nodoc:
236
236
  elements.each { |el| el.do_write(io) }
237
237
  end
238
238
 
239
- def do_num_bytes #:nodoc:
239
+ def do_num_bytes # :nodoc:
240
240
  sum_num_bytes_for_all_elements
241
241
  end
242
242
 
@@ -251,7 +251,7 @@ module BinData
251
251
  end
252
252
 
253
253
  def elements
254
- @element_list ||= []
254
+ @elements ||= []
255
255
  end
256
256
 
257
257
  def append_new_element
@@ -279,66 +279,66 @@ module BinData
279
279
  end
280
280
  end
281
281
  end
282
- end
283
282
 
284
- class ArrayArgProcessor < BaseArgProcessor
285
- def sanitize_parameters!(obj_class, params) #:nodoc:
286
- # ensure one of :initial_length and :read_until exists
287
- unless params.has_at_least_one_of?(:initial_length, :read_until)
288
- params[:initial_length] = 0
283
+ # Logic for the :read_until parameter
284
+ module ReadUntilPlugin
285
+ def do_read(io)
286
+ loop do
287
+ element = append_new_element
288
+ element.do_read(io)
289
+ variables = { index: self.length - 1, element: self.last, array: self }
290
+ break if eval_parameter(:read_until, variables)
291
+ end
289
292
  end
290
-
291
- params.warn_replacement_parameter(:length, :initial_length)
292
- params.warn_replacement_parameter(:read_length, :initial_length)
293
- params.must_be_integer(:initial_length)
294
-
295
- params.merge!(obj_class.dsl_params)
296
- params.sanitize_object_prototype(:type)
297
293
  end
298
- end
299
294
 
300
- # Logic for the :read_until parameter
301
- module ReadUntilPlugin
302
- def do_read(io)
303
- loop do
304
- element = append_new_element
305
- element.do_read(io)
306
- variables = { index: self.length - 1, element: self.last, array: self }
307
- break if eval_parameter(:read_until, variables)
295
+ # Logic for the read_until: :eof parameter
296
+ module ReadUntilEOFPlugin
297
+ def do_read(io)
298
+ loop do
299
+ element = append_new_element
300
+ begin
301
+ element.do_read(io)
302
+ rescue EOFError, IOError
303
+ elements.pop
304
+ break
305
+ end
306
+ end
308
307
  end
309
308
  end
310
- end
311
309
 
312
- # Logic for the read_until: :eof parameter
313
- module ReadUntilEOFPlugin
314
- def do_read(io)
315
- loop do
316
- element = append_new_element
317
- begin
318
- element.do_read(io)
319
- rescue EOFError, IOError
320
- elements.pop
321
- break
310
+ # Logic for the :initial_length parameter
311
+ module InitialLengthPlugin
312
+ def do_read(io)
313
+ elements.each { |el| el.do_read(io) }
314
+ end
315
+
316
+ def elements
317
+ if @elements.nil?
318
+ @elements = []
319
+ eval_parameter(:initial_length).times do
320
+ @elements << new_element
321
+ end
322
322
  end
323
+
324
+ @elements
323
325
  end
324
326
  end
325
327
  end
326
328
 
327
- # Logic for the :initial_length parameter
328
- module InitialLengthPlugin
329
- def do_read(io)
330
- elements.each { |el| el.do_read(io) }
331
- end
332
-
333
- def elements
334
- if @element_list.nil?
335
- @element_list = []
336
- eval_parameter(:initial_length).times do
337
- @element_list << new_element
338
- end
329
+ class ArrayArgProcessor < BaseArgProcessor
330
+ def sanitize_parameters!(obj_class, params) # :nodoc:
331
+ # ensure one of :initial_length and :read_until exists
332
+ unless params.has_at_least_one_of?(:initial_length, :read_until)
333
+ params[:initial_length] = 0
339
334
  end
340
335
 
341
- @element_list
336
+ params.warn_replacement_parameter(:length, :initial_length)
337
+ params.warn_replacement_parameter(:read_length, :initial_length)
338
+ params.must_be_integer(:initial_length)
339
+
340
+ params.merge!(obj_class.dsl_params)
341
+ params.sanitize_object_prototype(:type)
342
342
  end
343
343
  end
344
344
  end
data/lib/bindata/base.rb CHANGED
@@ -17,7 +17,7 @@ module BinData
17
17
  # Instantiates this class and reads from +io+, returning the newly
18
18
  # created data object. +args+ will be used when instantiating.
19
19
  def read(io, *args, &block)
20
- obj = self.new(*args)
20
+ obj = new(*args)
21
21
  obj.read(io, &block)
22
22
  obj
23
23
  end
@@ -48,7 +48,7 @@ module BinData
48
48
  end
49
49
 
50
50
  # Registers all subclasses of this class for use
51
- def register_subclasses #:nodoc:
51
+ def register_subclasses # :nodoc:
52
52
  singleton_class.send(:undef_method, :inherited)
53
53
  define_singleton_method(:inherited) do |subclass|
54
54
  RegisteredClasses.register(subclass.name, subclass)
@@ -90,6 +90,8 @@ module BinData
90
90
 
91
91
  # Creates a new data object based on this instance.
92
92
  #
93
+ # This implements the prototype design pattern.
94
+ #
93
95
  # All parameters will be be duplicated. Use this method
94
96
  # when creating multiple objects with the same parameters.
95
97
  def new(value = nil, parent = nil)
@@ -117,8 +119,8 @@ module BinData
117
119
  end
118
120
 
119
121
  # Returns a lazy evaluator for this object.
120
- def lazy_evaluator #:nodoc:
121
- @lazy ||= LazyEvaluator.new(self)
122
+ def lazy_evaluator # :nodoc:
123
+ @lazy_evaluator ||= LazyEvaluator.new(self)
122
124
  end
123
125
 
124
126
  # Returns the parameter referenced by +key+.
@@ -177,7 +179,7 @@ module BinData
177
179
 
178
180
  # Returns the hexadecimal string representation of this data object.
179
181
  def to_hex(&block)
180
- to_binary_s(&block).unpack('H*')[0]
182
+ to_binary_s(&block).unpack1('H*')
181
183
  end
182
184
 
183
185
  # Return a human readable representation of this data object.
@@ -191,7 +193,7 @@ module BinData
191
193
  end
192
194
 
193
195
  # Work with Ruby's pretty-printer library.
194
- def pretty_print(pp) #:nodoc:
196
+ def pretty_print(pp) # :nodoc:
195
197
  pp.pp(snapshot)
196
198
  end
197
199
 
@@ -202,40 +204,28 @@ module BinData
202
204
 
203
205
  # Returns a user friendly name of this object for debugging purposes.
204
206
  def debug_name
205
- if @parent
206
- @parent.debug_name_of(self)
207
- else
208
- "obj"
209
- end
207
+ @parent ? @parent.debug_name_of(self) : 'obj'
210
208
  end
211
209
 
212
210
  # Returns the offset (in bytes) of this object with respect to its most
213
211
  # distant ancestor.
214
212
  def abs_offset
215
- if @parent
216
- @parent.abs_offset + @parent.offset_of(self)
217
- else
218
- 0
219
- end
213
+ @parent ? @parent.abs_offset + @parent.offset_of(self) : 0
220
214
  end
221
215
 
222
216
  # Returns the offset (in bytes) of this object with respect to its parent.
223
217
  def rel_offset
224
- if @parent
225
- @parent.offset_of(self)
226
- else
227
- 0
228
- end
218
+ @parent ? @parent.offset_of(self) : 0
229
219
  end
230
220
 
231
- def ==(other) #:nodoc:
221
+ def ==(other) # :nodoc:
232
222
  # double dispatch
233
223
  other == snapshot
234
224
  end
235
225
 
236
226
  # A version of +respond_to?+ used by the lazy evaluator. It doesn't
237
227
  # reinvoke the evaluator so as to avoid infinite evaluation loops.
238
- def safe_respond_to?(symbol, include_private = false) #:nodoc:
228
+ def safe_respond_to?(symbol, include_private = false) # :nodoc:
239
229
  base_respond_to?(symbol, include_private)
240
230
  end
241
231
 
@@ -329,7 +319,6 @@ module BinData
329
319
  # Performs sanity checks on the given parameters.
330
320
  # This method converts the parameters to the form expected
331
321
  # by the data object.
332
- def sanitize_parameters!(obj_class, obj_params)
333
- end
322
+ def sanitize_parameters!(obj_class, obj_params); end
334
323
  end
335
324
  end
@@ -65,7 +65,7 @@ module BinData
65
65
  @value = nil
66
66
  end
67
67
 
68
- def clear? #:nodoc:
68
+ def clear? # :nodoc:
69
69
  @value.nil?
70
70
  end
71
71
 
@@ -73,13 +73,7 @@ module BinData
73
73
  raise ArgumentError, "can't set a nil value for #{debug_name}" if val.nil?
74
74
 
75
75
  raw_val = val.respond_to?(:snapshot) ? val.snapshot : val
76
- @value =
77
- begin
78
- raw_val.dup
79
- rescue TypeError
80
- # can't dup Fixnums
81
- raw_val
82
- end
76
+ @value = raw_val.dup
83
77
  end
84
78
 
85
79
  def snapshot
@@ -94,18 +88,19 @@ module BinData
94
88
  assign(val)
95
89
  end
96
90
 
97
- def respond_to?(symbol, include_private = false) #:nodoc:
91
+ def respond_to_missing?(symbol, include_all = false) # :nodoc:
98
92
  child = snapshot
99
- child.respond_to?(symbol, include_private) || super
93
+ child.respond_to?(symbol, include_all) || super
100
94
  end
101
95
 
102
- def method_missing(symbol, *args, &block) #:nodoc:
96
+ def method_missing(symbol, *args, &block) # :nodoc:
103
97
  child = snapshot
104
98
  if child.respond_to?(symbol)
105
- self.class.class_eval \
106
- "def #{symbol}(*args, &block);" \
107
- " snapshot.#{symbol}(*args, &block);" \
108
- "end"
99
+ self.class.class_eval <<-END, __FILE__, __LINE__ + 1
100
+ def #{symbol}(*args, &block) # def clamp(*args, &block)
101
+ snapshot.#{symbol}(*args, &block) # snapshot.clamp(*args, &block)
102
+ end # end
103
+ END
109
104
  child.__send__(symbol, *args, &block)
110
105
  else
111
106
  super
@@ -125,15 +120,15 @@ module BinData
125
120
  snapshot.hash
126
121
  end
127
122
 
128
- def do_read(io) #:nodoc:
123
+ def do_read(io) # :nodoc:
129
124
  @value = read_and_return_value(io)
130
125
  end
131
126
 
132
- def do_write(io) #:nodoc:
127
+ def do_write(io) # :nodoc:
133
128
  io.writebytes(value_to_binary_string(_value))
134
129
  end
135
130
 
136
- def do_num_bytes #:nodoc:
131
+ def do_num_bytes # :nodoc:
137
132
  value_to_binary_string(_value).length
138
133
  end
139
134
 
@@ -172,7 +167,7 @@ module BinData
172
167
  assert!
173
168
  end
174
169
 
175
- def do_read(io) #:nodoc:
170
+ def do_read(io) # :nodoc:
176
171
  super(io)
177
172
  assert!
178
173
  end
@@ -205,7 +200,16 @@ module BinData
205
200
  reading? ? @value : eval_parameter(:asserted_value)
206
201
  end
207
202
 
208
- def do_read(io) #:nodoc:
203
+ # The asserted value as a binary string.
204
+ #
205
+ # Rationale: while reading, +#to_binary_s+ will use the
206
+ # value read in, rather than the +:asserted_value+.
207
+ # This feature is used by Skip.
208
+ def asserted_binary_s
209
+ value_to_binary_string(eval_parameter(:asserted_value))
210
+ end
211
+
212
+ def do_read(io) # :nodoc:
209
213
  super(io)
210
214
  assert!
211
215
  end
data/lib/bindata/bits.rb CHANGED
@@ -5,7 +5,7 @@ module BinData
5
5
  # Defines a number of classes that contain a bit based integer.
6
6
  # The integer is defined by endian and number of bits.
7
7
 
8
- module BitField #:nodoc: all
8
+ module BitField # :nodoc: all
9
9
  @@mutex = Mutex.new
10
10
 
11
11
  class << self
@@ -99,14 +99,14 @@ module BinData
99
99
 
100
100
  def create_dynamic_clamp_code(signed)
101
101
  if signed == :signed
102
- max = "max = (1 << (nbits - 1)) - 1"
103
- min = "min = -(max + 1)"
102
+ max = "(1 << (nbits - 1)) - 1"
103
+ min = "-((#{max}) + 1)"
104
104
  else
105
- max = "max = (1 << nbits) - 1"
106
- min = "min = 0"
105
+ max = "(1 << nbits) - 1"
106
+ min = "0"
107
107
  end
108
108
 
109
- "#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val"
109
+ "val = val.clamp(#{min}, #{max})"
110
110
  end
111
111
 
112
112
  def create_fixed_clamp_code(nbits, signed)
@@ -115,14 +115,14 @@ module BinData
115
115
  end
116
116
 
117
117
  if signed == :signed
118
- max = "max = (1 << (#{nbits} - 1)) - 1"
119
- min = "min = -(max + 1)"
118
+ max = "(1 << (#{nbits} - 1)) - 1"
119
+ min = "-((#{max}) + 1)"
120
120
  else
121
- min = "min = 0"
122
- max = "max = (1 << #{nbits}) - 1"
121
+ min = "0"
122
+ max = "(1 << #{nbits}) - 1"
123
123
  end
124
124
 
125
- clamp = "(#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val)"
125
+ clamp = "(val = val.clamp(#{min}, #{max}))"
126
126
 
127
127
  if nbits == 1
128
128
  # allow single bits to be used as booleans
@@ -156,10 +156,10 @@ module BinData
156
156
 
157
157
  # Create classes for dynamic bitfields
158
158
  {
159
- "Bit" => :big,
160
- "BitLe" => :little,
161
- "Sbit" => [:big, :signed],
162
- "SbitLe" => [:little, :signed],
159
+ 'Bit' => :big,
160
+ 'BitLe' => :little,
161
+ 'Sbit' => [:big, :signed],
162
+ 'SbitLe' => [:little, :signed]
163
163
  }.each_pair { |name, args| BitField.define_class(name, :nbits, *args) }
164
164
 
165
165
  # Create classes on demand