bindata 2.4.4 → 2.4.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc513e5824615b4b143e4a733bd6e8290bdfa831ab111267475a167f31125bb1
4
- data.tar.gz: 93b4a5203a1d641dd1af806b769f92f8daf50644e9815d01fa2dbfec9f30bc8e
3
+ metadata.gz: 651bb61168cb520dbec6550dc4ee3d566e950a0a4e3c373752a37d9ced6dc17e
4
+ data.tar.gz: 543f3849f0502255c976520e3d11f2e988f0733314b905d098b8617025ef2d99
5
5
  SHA512:
6
- metadata.gz: 88c701264083edfa92d193c33d394f7cb4bdc2f12722089ff27bb296bad28665f8e28a63158c98a8a45a189c68ce4792c027615ac0c9f1c61cacee1e02e817cc
7
- data.tar.gz: dd1f06f1b17cbc005ca5399d86b62cfe420787d8a2f820d24f589f30dd58af99ed539eb5d5873f90b3877f986093a3bc5f176dcb7aaaeea176381809272b20ff
6
+ metadata.gz: 5ab4ddc205727d64b5b4f1354c6c738965745ff6736030f440c3982799351e68af06c9f1e0faff28d6e52830a0abf55d678a0ea7ffefa160f9f7bf3c309b6e7b
7
+ data.tar.gz: 834bdfa27436e578043e8b92ded99a02e6fc5ca2d0e11f0503bf3d8d68c1c7c9e491e66fe59f264156fb9675437cc9f014193f42132a8e6aec3a11b6f84f5312
data/.travis.yml CHANGED
@@ -1,9 +1,11 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0
4
- - 2.1.10
5
3
  - 2.2
6
4
  - 2.3
5
+ - 2.4
6
+ - 2.5
7
+ - 2.6
8
+ - 2.7
7
9
  - jruby
8
10
  - ruby-head
9
11
 
data/ChangeLog.rdoc CHANGED
@@ -1,5 +1,33 @@
1
1
  = BinData Changelog
2
2
 
3
+ == Version 2.4.10 (2021-05-18)
4
+
5
+ * Improve speed of dynamic object creation. Reported by Charlie Ablett.
6
+
7
+ == Version 2.4.9 (2021-04-22)
8
+
9
+ * Change example from Fixnum to Integer. Thanks to Tim Chambers.
10
+ * Now works with frozen string literals. Requested by Jeremy Evans.
11
+
12
+ == Version 2.4.8 (2020-07-21)
13
+
14
+ * Bug fix array self assignment. Thanks to Spencer McIntyre.
15
+ * Bug fix Stringz max_length. Thanks to cdelafuente-r7.
16
+
17
+ == Version 2.4.7 (2020-03-31)
18
+
19
+ * Fix choice assignment inside arrays. Reported by Spencer McIntyre.
20
+
21
+ == Version 2.4.6 (2020-02-27)
22
+
23
+ * More encoding fixes. Thanks to Aaron Patterson.
24
+
25
+ == Version 2.4.5 (2020-02-21)
26
+
27
+ * Small typo fixes to examples.
28
+ * Fix encoding issue for ruby 2.7. Thanks to Aaron Patterson.
29
+ * Quieter test output.
30
+
3
31
  == Version 2.4.4 (2018-10-03)
4
32
 
5
33
  * Display a hint when endian is omitted. Requested by Tails.
data/bindata.gemspec CHANGED
@@ -8,17 +8,15 @@ Gem::Specification.new do |s|
8
8
  s.summary = 'A declarative way to read and write binary file formats'
9
9
  s.author = 'Dion Mendel'
10
10
  s.email = 'bindata@dm9.info'
11
- s.homepage = 'http://github.com/dmendel/bindata'
12
- s.rubyforge_project = 'bindata'
11
+ s.homepage = 'https://github.com/dmendel/bindata'
13
12
  s.require_path = 'lib'
14
- s.has_rdoc = true
15
13
  s.extra_rdoc_files = ['NEWS.rdoc']
16
14
  s.rdoc_options << '--main' << 'NEWS.rdoc'
17
15
  s.files = `git ls-files`.split("\n")
18
16
  s.license = 'Ruby'
19
17
 
20
18
  s.add_development_dependency('rake')
21
- s.add_development_dependency('minitest', "> 5.0.0")
19
+ s.add_development_dependency('minitest', "> 5.0.0", "< 5.12.0")
22
20
  s.add_development_dependency('coveralls')
23
21
  s.description = <<-END.gsub(/^ +/, "")
24
22
  BinData is a declarative way to read and write binary file formats.
data/examples/list.rb CHANGED
@@ -105,7 +105,7 @@ class Term < BinData::Record
105
105
  end
106
106
 
107
107
  def self.encode(val)
108
- if Fixnum === val
108
+ if Integer === val
109
109
  Term.new(tag: 'a', term: Atom.encode(val))
110
110
  else
111
111
  Term.new(tag: 'l', term: List.encode(val))
data/examples/tcp_ip.rb CHANGED
@@ -14,7 +14,7 @@ class MacAddr < BinData::Primitive
14
14
  array :octets, type: :uint8, initial_length: 6
15
15
 
16
16
  def set(val)
17
- self.octets = val.split(/\./).collect(&:to_i)
17
+ self.octets = val.split(/:/).collect(&:to_i)
18
18
  end
19
19
 
20
20
  def get
@@ -23,7 +23,7 @@ class MacAddr < BinData::Primitive
23
23
  end
24
24
 
25
25
  # Present IP addresses in a human readable way
26
- class IPAddr < BinData::Primitive
26
+ class IP_Addr < BinData::Primitive
27
27
  array :octets, type: :uint8, initial_length: 4
28
28
 
29
29
  def set(val)
@@ -47,7 +47,7 @@ module BinData
47
47
  end
48
48
  def readbytes(n)
49
49
  n.times.inject("") do |bytes, _|
50
- bytes << @io.readbits(8, :big).chr
50
+ bytes += @io.readbits(8, :big).chr
51
51
  end
52
52
  end
53
53
  end
data/lib/bindata/array.rb CHANGED
@@ -80,9 +80,11 @@ module BinData
80
80
  end
81
81
 
82
82
  def assign(array)
83
+ return if self.equal?(array) # prevent self assignment
83
84
  raise ArgumentError, "can't set a nil value for #{debug_name}" if array.nil?
84
85
 
85
- @element_list = to_storage_formats(array.to_ary)
86
+ @element_list = []
87
+ concat(array)
86
88
  end
87
89
 
88
90
  def snapshot
@@ -119,7 +121,17 @@ module BinData
119
121
 
120
122
  def insert(index, *objs)
121
123
  extend_array(index - 1)
122
- elements.insert(index, *to_storage_formats(objs))
124
+ abs_index = (index >= 0) ? index : index + 1 + length
125
+
126
+ # insert elements before...
127
+ new_elements = objs.map { new_element }
128
+ elements.insert(index, *new_elements)
129
+
130
+ # ...assigning values
131
+ objs.each_with_index do |obj, i|
132
+ self[abs_index + i] = obj
133
+ end
134
+
123
135
  self
124
136
  end
125
137
 
@@ -238,10 +250,6 @@ module BinData
238
250
  end
239
251
  end
240
252
 
241
- def to_storage_formats(els)
242
- els.collect { |el| new_element(el) }
243
- end
244
-
245
253
  def elements
246
254
  @element_list ||= []
247
255
  end
@@ -252,8 +260,8 @@ module BinData
252
260
  element
253
261
  end
254
262
 
255
- def new_element(value = nil)
256
- @element_prototype.instantiate(value, self)
263
+ def new_element
264
+ @element_prototype.instantiate(nil, self)
257
265
  end
258
266
 
259
267
  def sum_num_bytes_for_all_elements
data/lib/bindata/base.rb CHANGED
@@ -172,8 +172,7 @@ module BinData
172
172
  def to_binary_s(&block)
173
173
  io = BinData::IO.create_string_io
174
174
  write(io, &block)
175
- io.rewind
176
- io.read
175
+ io.string
177
176
  end
178
177
 
179
178
  # Returns the hexadecimal string representation of this data object.
data/lib/bindata/bits.rb CHANGED
@@ -115,14 +115,14 @@ module BinData
115
115
  end
116
116
 
117
117
  if signed == :signed
118
- max = (1 << (nbits - 1)) - 1
119
- min = -(max + 1)
118
+ max = "max = (1 << (#{nbits} - 1)) - 1"
119
+ min = "min = -(max + 1)"
120
120
  else
121
- min = 0
122
- max = (1 << nbits) - 1
121
+ min = "min = 0"
122
+ max = "max = (1 << #{nbits}) - 1"
123
123
  end
124
124
 
125
- clamp = "(val < #{min}) ? #{min} : (val > #{max}) ? #{max} : val"
125
+ clamp = "(#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val)"
126
126
 
127
127
  if nbits == 1
128
128
  # allow single bits to be used as booleans
data/lib/bindata/int.rb CHANGED
@@ -59,14 +59,16 @@ module BinData
59
59
 
60
60
  def create_clamp_code(nbits, signed)
61
61
  if signed == :signed
62
- max = (1 << (nbits - 1)) - 1
63
- min = -(max + 1)
62
+ max = "max = (1 << (#{nbits} - 1)) - 1"
63
+ min = "min = -(max + 1)"
64
64
  else
65
- max = (1 << nbits) - 1
66
- min = 0
65
+ max = "max = (1 << #{nbits}) - 1"
66
+ min = "min = 0"
67
67
  end
68
68
 
69
- "val = (val < #{min}) ? #{min} : (val > #{max}) ? #{max} : val"
69
+ clamp = "(#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val)"
70
+
71
+ "val = #{clamp}"
70
72
  end
71
73
 
72
74
  def create_read_code(nbits, endian, signed)
@@ -107,7 +109,7 @@ module BinData
107
109
  parts = (0...nwords).collect do |i|
108
110
  "(ints.at(#{idx[i]}) << #{bits_per_word(nbits) * i})"
109
111
  end
110
- parts[0].sub!(/ << 0\b/, "") # Remove " << 0" for optimisation
112
+ parts[0] = parts[0].sub(/ << 0\b/, "") # Remove " << 0" for optimisation
111
113
 
112
114
  parts.join(" + ")
113
115
  end
@@ -132,7 +134,7 @@ module BinData
132
134
  mask = (1 << bits_per_word(nbits)) - 1
133
135
 
134
136
  vals = (0...nwords).collect { |i| "val >> #{bits_per_word(nbits) * i}" }
135
- vals[0].sub!(/ >> 0\b/, "") # Remove " >> 0" for optimisation
137
+ vals[0] = vals[0].sub(/ >> 0\b/, "") # Remove " >> 0" for optimisation
136
138
  vals.reverse! if (endian == :big)
137
139
 
138
140
  vals = vals.collect { |val| "#{val} & #{mask}" } # TODO: "& mask" is needed to work around jruby bug. Remove this line when fixed.
@@ -160,7 +162,7 @@ module BinData
160
162
  directives = { 8 => "C", 16 => "S", 32 => "L", 64 => "Q" }
161
163
 
162
164
  d = directives[bits_per_word(nbits)]
163
- d << ((endian == :big) ? ">" : "<") unless d == "C"
165
+ d += ((endian == :big) ? ">" : "<") unless d == "C"
164
166
 
165
167
  if signed == :signed && directives.key?(nbits)
166
168
  (d * nwords).downcase
data/lib/bindata/io.rb CHANGED
@@ -169,7 +169,7 @@ module BinData
169
169
 
170
170
  unless @read_data.empty? || @in_readahead
171
171
  bytes_to_consume = [n, @read_data.length].min
172
- data << @read_data.slice!(0, bytes_to_consume)
172
+ data += @read_data.slice!(0, bytes_to_consume)
173
173
  n -= bytes_to_consume
174
174
 
175
175
  if @read_data.empty?
@@ -180,10 +180,10 @@ module BinData
180
180
  end
181
181
 
182
182
  raw_data = @raw_io.read(n)
183
- data << raw_data if raw_data
183
+ data += raw_data if raw_data
184
184
 
185
185
  if @in_readahead
186
- @read_data << data
186
+ @read_data += data
187
187
  end
188
188
 
189
189
  @offset += data.size
@@ -213,7 +213,9 @@ module BinData
213
213
 
214
214
  # Creates a StringIO around +str+.
215
215
  def self.create_string_io(str = "")
216
- StringIO.new(str.dup.force_encoding(Encoding::BINARY))
216
+ s = StringIO.new(str.dup.force_encoding(Encoding::BINARY))
217
+ s.binmode
218
+ s
217
219
  end
218
220
 
219
221
  # Create a new IO Read wrapper around +io+. +io+ must provide #read,
@@ -54,7 +54,7 @@ module BinData
54
54
  # read until zero byte or we have read in the max number of bytes
55
55
  while ch != "\0" && i != max_length
56
56
  ch = io.readbytes(1)
57
- str << ch
57
+ str += ch
58
58
  i += 1
59
59
  end
60
60
 
@@ -80,7 +80,7 @@ module BinData
80
80
  def trim_to!(str, max_length = nil)
81
81
  if max_length
82
82
  max_length = 1 if max_length < 1
83
- str.slice!(max_length)
83
+ str.slice!(max_length..-1)
84
84
  if str.length == max_length && str[-1, 1] != "\0"
85
85
  str[-1, 1] = "\0"
86
86
  end
@@ -1,3 +1,3 @@
1
1
  module BinData
2
- VERSION = "2.4.4"
2
+ VERSION = "2.4.10"
3
3
  end
data/test/array_test.rb CHANGED
@@ -266,6 +266,11 @@ describe BinData::Array, "when accessing elements" do
266
266
  lambda { obj["a"] }.must_raise TypeError
267
267
  lambda { obj[1, "a"] }.must_raise TypeError
268
268
  end
269
+
270
+ it "is unaffected by self assignment" do
271
+ obj.assign(obj)
272
+ obj.snapshot.must_equal [1, 2, 3, 4, 5]
273
+ end
269
274
  end
270
275
 
271
276
  describe BinData::Array, "with :read_until" do
data/test/bits_test.rb CHANGED
@@ -101,14 +101,14 @@ def generate_bit_classes_to_test(endian, signed)
101
101
 
102
102
  (start .. 64).each do |nbits|
103
103
  name = "#{base}#{nbits}"
104
- name << "le" if endian == :little
104
+ name += "le" if endian == :little
105
105
  obj = BinData.const_get(name).new
106
106
  bits << [obj, nbits]
107
107
  end
108
108
 
109
109
  (start .. 64).each do |nbits|
110
110
  name = "#{base}"
111
- name << "Le" if endian == :little
111
+ name += "Le" if endian == :little
112
112
  obj = BinData.const_get(name).new(nbits: nbits)
113
113
  bits << [obj, nbits]
114
114
  end
data/test/buffer_test.rb CHANGED
@@ -153,4 +153,3 @@ describe BinData::Buffer, "nested buffers" do
153
153
  obj.to_binary_s.must_equal_binary "abcdeABCDE12345"
154
154
  end
155
155
  end
156
-
@@ -99,7 +99,7 @@ describe BinData::DelayedIO, "subclassed with a single type" do
99
99
  end
100
100
 
101
101
  it "writes explicitly" do
102
- io = StringIO.new "\001\002\003\004\005\006\007\010\011"
102
+ io = StringIO.new "\001\002\003\004\005\006\007\010\011".dup
103
103
  obj = IntDelayedIO.new(3)
104
104
  obj.write(io)
105
105
  obj.write_now!
@@ -107,7 +107,7 @@ describe BinData::DelayedIO, "subclassed with a single type" do
107
107
  end
108
108
 
109
109
  it "writes explicitly after setting abs_offset" do
110
- io = StringIO.new "\001\002\003\004\005\006\007\010\011"
110
+ io = StringIO.new "\001\002\003\004\005\006\007\010\011".dup
111
111
  obj = IntDelayedIO.new(7)
112
112
  obj.write(io)
113
113
 
@@ -139,7 +139,7 @@ describe BinData::DelayedIO, "subclassed with multiple types" do
139
139
  end
140
140
 
141
141
  it "writes explicitly" do
142
- io = StringIO.new "\001\002\003\004\005\006\007\010\011\012\013\014\015"
142
+ io = StringIO.new "\001\002\003\004\005\006\007\010\011\012\013\014\015".dup
143
143
  obj = StringDelayedIO.new(str: "hello")
144
144
  obj.write(io)
145
145
  obj.write_now!
data/test/int_test.rb CHANGED
@@ -128,7 +128,7 @@ module AllIntegers
128
128
  end
129
129
 
130
130
  def int_to_binary_str(val)
131
- str = "".force_encoding(Encoding::BINARY)
131
+ str = "".dup.force_encoding(Encoding::BINARY)
132
132
  v = val & ((1 << (@nbytes * 8)) - 1)
133
133
  @nbytes.times do
134
134
  str.concat(v & 0xff)
data/test/offset_test.rb CHANGED
@@ -74,9 +74,15 @@ describe BinData::Base, "offsets" do
74
74
  end
75
75
 
76
76
  it "adjust offset when incorrect" do
77
- io.seek(2)
78
- obj = TenByteOffsetBase.create(adjust_offset: 13)
79
- obj.read(io).snapshot.must_equal data[2 + 13, 3]
77
+ w, $-w = $-w, false
78
+
79
+ begin
80
+ io.seek(2)
81
+ obj = TenByteOffsetBase.create(adjust_offset: 13)
82
+ obj.read(io).snapshot.must_equal data[2 + 13, 3]
83
+ ensure
84
+ $-w = w
85
+ end
80
86
  end
81
87
 
82
88
  it "succeeds when offset is correct" do
@@ -36,10 +36,16 @@ describe BinData::Registry do
36
36
  end
37
37
 
38
38
  it "allows overriding of registered classes" do
39
- r.register('A', A)
40
- r.register('A', B)
39
+ w, $-w = $-w, false
41
40
 
42
- r.lookup('a').must_equal B
41
+ begin
42
+ r.register('A', A)
43
+ r.register('A', B)
44
+
45
+ r.lookup('a').must_equal B
46
+ ensure
47
+ $-w = w
48
+ end
43
49
  end
44
50
 
45
51
  it "converts CamelCase to underscores" do
data/test/stringz_test.rb CHANGED
@@ -106,12 +106,12 @@ describe BinData::Stringz, "with max_length" do
106
106
  end
107
107
 
108
108
  it "trims values greater than max_length" do
109
- obj.assign("abcde")
109
+ obj.assign("abcdefg")
110
110
  obj.must_equal "abcd"
111
111
  end
112
112
 
113
113
  it "writes values greater than max_length" do
114
- obj.assign("abcde")
114
+ obj.assign("abcdefg")
115
115
  obj.to_binary_s.must_equal_binary "abcd\0"
116
116
  end
117
117
 
data/test/system_test.rb CHANGED
@@ -360,3 +360,37 @@ describe BinData::Record, "with boolean parameters" do
360
360
  obj.a.must_equal 2
361
361
  end
362
362
  end
363
+
364
+ describe BinData::Record, "encoding" do
365
+ class EncodingTestBufferRecord < BinData::Record
366
+ endian :big
367
+ default_parameter length: 5
368
+
369
+ uint16 :num
370
+ string :str, length: 10
371
+ end
372
+
373
+ it "returns binary encoded data" do
374
+ obj = EncodingTestBufferRecord.new(num: 3)
375
+ obj.to_binary_s.encoding.must_equal Encoding::ASCII_8BIT
376
+ end
377
+
378
+ it "returns binary encoded data with utf-8 string" do
379
+ obj = EncodingTestBufferRecord.new(num: 3, str: "日本語")
380
+ obj.to_binary_s.encoding.must_equal Encoding::ASCII_8BIT
381
+ end
382
+
383
+ it "returns binary encoded data despite Encoding.default_internal" do
384
+ w, $-w = $-w, false
385
+ before_enc = Encoding.default_internal
386
+
387
+ begin
388
+ Encoding.default_internal = Encoding::UTF_8
389
+ obj = EncodingTestBufferRecord.new(num: 3, str: "日本語")
390
+ obj.to_binary_s.encoding.must_equal Encoding::ASCII_8BIT
391
+ ensure
392
+ Encoding.default_internal = before_enc
393
+ $-w = w
394
+ end
395
+ end
396
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bindata
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.4
4
+ version: 2.4.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dion Mendel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-03 00:00:00.000000000 Z
11
+ date: 2021-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -31,6 +31,9 @@ dependencies:
31
31
  - - ">"
32
32
  - !ruby/object:Gem::Version
33
33
  version: 5.0.0
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: 5.12.0
34
37
  type: :development
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -38,6 +41,9 @@ dependencies:
38
41
  - - ">"
39
42
  - !ruby/object:Gem::Version
40
43
  version: 5.0.0
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: 5.12.0
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: coveralls
43
49
  requirement: !ruby/object:Gem::Requirement
@@ -143,7 +149,7 @@ files:
143
149
  - test/uint8_array_test.rb
144
150
  - test/virtual_test.rb
145
151
  - test/warnings_test.rb
146
- homepage: http://github.com/dmendel/bindata
152
+ homepage: https://github.com/dmendel/bindata
147
153
  licenses:
148
154
  - Ruby
149
155
  metadata: {}
@@ -164,8 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
170
  - !ruby/object:Gem::Version
165
171
  version: '0'
166
172
  requirements: []
167
- rubyforge_project: bindata
168
- rubygems_version: 2.7.6
173
+ rubygems_version: 3.1.4
169
174
  signing_key:
170
175
  specification_version: 4
171
176
  summary: A declarative way to read and write binary file formats