libbin 1.0.4 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/libbin.rb CHANGED
@@ -1,5 +1,3 @@
1
- module LibBin
2
- end
3
1
  require "libbin_c.so"
4
2
 
5
3
  require_relative 'libbin/alignment'
@@ -7,12 +5,17 @@ require_relative 'libbin/data_types'
7
5
 
8
6
  module LibBin
9
7
 
10
- @__big_architecture = [0x12345678].pack("i") == "\x12\x34\x56\x78"
8
+ BIG_ARCHITECTURE = [0x12345678].pack("i") == "\x12\x34\x56\x78"
11
9
  @__big = nil
10
+ @__output = $stderr
11
+
12
+ class << self
13
+ attr_accessor :__output
14
+ end
12
15
 
13
16
  def self.default_big?
14
17
  if @__big.nil?
15
- @__big_architecture
18
+ BIG_ARCHITECTURE
16
19
  else
17
20
  @__big
18
21
  end
@@ -25,329 +28,20 @@ module LibBin
25
28
  to_s
26
29
  end
27
30
 
28
- attr_reader :__parent
29
- attr_reader :__index
30
- attr_reader :__iterator
31
- attr_reader :__position
32
- def __set_convert_type(input, output, input_big, output_big, parent, index)
33
- @__input_big = input_big
34
- @__output_big = output_big
35
- @__input = input
36
- @__output = output
37
- @__parent = parent
38
- @__index = index
39
- @__position = input.tell
40
- @__cur_position = @__position
41
- end
42
-
43
- def __set_size_type(position, parent, index)
44
- @__parent = parent
45
- @__index = index
46
- @__position = position
47
- @__cur_position = @__position
48
- end
49
-
50
- def __set_load_type(input, input_big, parent, index)
51
- @__input_big = input_big
52
- @__input = input
53
- @__parent = parent
54
- @__index = index
55
- @__position = input.tell
56
- @__cur_position = @__position
57
- end
58
-
59
- def __set_dump_type(output, output_big, parent, index)
60
- @__output_big = output_big
61
- @__output = output
62
- @__parent = parent
63
- @__index = index
64
- @__position = output.tell
65
- @__cur_position = @__position
66
- end
67
-
68
- def __unset_convert_type
69
- @__input_big = nil
70
- @__output_big = nil
71
- @__input = nil
72
- @__output = nil
73
- @__parent = nil
74
- @__index = nil
75
- @__position = nil
76
- @__cur_position = nil
77
- end
78
-
79
- def __unset_size_type
80
- @__parent = nil
81
- @__index = nil
82
- @__position = nil
83
- @__cur_position = nil
84
- end
85
-
86
- def __unset_load_type
87
- @__input_big = nil
88
- @__input = nil
89
- @__parent = nil
90
- @__index = nil
91
- @__position = nil
92
- @__cur_position = nil
93
- end
94
-
95
- def __unset_dump_type
96
- @__output_big = nil
97
- @__output = nil
98
- @__parent = nil
99
- @__index = nil
100
- @__position = nil
101
- @__cur_position = nil
102
- end
103
-
104
31
  def self.inherited(subclass)
105
32
  subclass.instance_variable_set(:@fields, [])
106
33
  end
107
34
 
108
- def __decode_expression(sym)
109
- case sym
110
- when Proc
111
- return sym.call
112
- when String
113
- exp = sym.gsub("..","__parent").gsub("\\",".")
114
- return eval(exp)
115
- else
116
- return sym
117
- end
118
- end
119
-
120
- def __decode_seek_offset(offset, relative_offset)
121
- return nil unless offset
122
- offset = __decode_expression(offset)
123
- return false if offset == 0x0
124
- offset += @__position if relative_offset
125
- @__cur_position = offset
126
- @__input.seek(offset) if @__input
127
- @__output.seek(offset) if @__output
128
- offset
129
- end
130
-
131
- def __decode_condition(condition)
132
- return true unless condition
133
- __decode_expression(condition)
134
- end
135
-
136
- def __decode_count(count)
137
- return 1 unless count
138
- __decode_expression(count)
139
- end
140
-
141
- def __decode_type(type)
142
- return __decode_expression(type)
143
- end
144
-
145
- def __decode_static_conditions(field)
146
- @__offset = nil
147
- @__condition = nil
148
- @__type = nil
149
- @__count = nil
150
- unless field.sequence?
151
- @__offset = __decode_seek_offset(field.offset, field.relative_offset?)
152
- throw :ignored, nil if @__offset == false
153
- @__condition = __decode_condition(field.condition)
154
- throw :ignored, nil unless @__condition
155
- @__type = __decode_type(field.type)
156
- end
157
- @__count = __decode_count(field.count)
158
- end
159
-
160
- def __decode_dynamic_conditions(field)
161
- return true unless field.sequence?
162
- @__offset = nil
163
- @__condition = nil
164
- @__type = nil
165
- @__offset = __decode_seek_offset(field.offset, field.relative_offset?)
166
- return false if @__offset == false
167
- @__condition = __decode_condition(field.condition)
168
- return false unless @__condition
169
- @__type = __decode_type(field.type)
170
- return true
171
- end
172
-
173
- def __restore_context
174
- @__iterator = nil
175
- @__type = nil
176
- @__count = nil
177
- @__offset = nil
178
- @__condition = nil
179
- end
180
-
181
- def __convert_field(field)
182
- __decode_static_conditions(field)
183
- vs = @__count.times.collect do |it|
184
- @__iterator = it
185
- if __decode_dynamic_conditions(field)
186
- @__type::convert(@__input, @__output, @__input_big, @__output_big, self, it)
187
- else
188
- nil
189
- end
190
- end
191
- __restore_context
192
- vs = vs.first unless field.count
193
- vs
194
- end
195
-
196
- def __load_field(field)
197
- __decode_static_conditions(field)
198
- vs = @__count.times.collect do |it|
199
- @__iterator = it
200
- if __decode_dynamic_conditions(field)
201
- @__type::load(@__input, @__input_big, self, it)
202
- else
203
- nil
204
- end
205
- end
206
- __restore_context
207
- vs = vs.first unless field.count
208
- vs
209
- end
210
-
211
- def __dump_field(vs, field)
212
- __decode_static_conditions(field)
213
- vs = [vs] unless field.count
214
- vs.each_with_index do |v, it|
215
- @__iterator = it
216
- if __decode_dynamic_conditions(field)
217
- @__type::dump(v, @__output, @__output_big, self, it)
218
- end
219
- end
220
- __restore_context
221
- end
222
-
223
- def __shape_field(vs, previous_offset, kind, field)
224
- __decode_static_conditions(field)
225
- vs = [vs] unless field.count
226
- vs = vs.each_with_index.collect do |v, it|
227
- @__iterator = it
228
- if __decode_dynamic_conditions(field)
229
- sh = @__type::shape(v, @__cur_position, self, it, kind)
230
- @__cur_position = sh.last + 1 if sh.last && sh.last >= 0
231
- sh
232
- end
233
- end
234
- __restore_context
235
- vs = vs.first unless field.count
236
- vs
237
- end
238
-
239
35
  def __size(previous_offset = 0, parent = nil, index = nil)
240
36
  __shape(previous_offset, parent, index, DataRange).size
241
37
  end
242
38
 
243
- def __shape(previous_offset = 0, parent = nil, index = nil, kind = DataShape)
244
- __set_size_type(previous_offset, parent, index)
245
- members = {}
246
- self.class.instance_variable_get(:@fields).each { |field|
247
- begin
248
- vs = send(field.name)
249
- member = catch(:ignored) do
250
- __shape_field(vs, previous_offset, kind, field)
251
- end
252
- members[field.name] = member
253
- rescue
254
- STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
255
- raise
256
- end
257
- }
258
- __unset_size_type
259
- return nil if members.values.flatten.compact.size <= 0
260
- kind::new(members)
261
- end
262
-
263
- def __convert_fields
264
- self.class.instance_variable_get(:@fields).each { |field|
265
- begin
266
- vs = catch(:ignored) do
267
- __convert_field(field)
268
- end
269
- send("#{field.name}=", vs)
270
- rescue
271
- STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
272
- raise
273
- end
274
- }
275
- self
276
- end
277
-
278
- def __load_fields
279
- self.class.instance_variable_get(:@fields).each { |field|
280
- begin
281
- vs = catch(:ignored) do
282
- __load_field(field)
283
- end
284
- send("#{field.name}=", vs)
285
- rescue
286
- STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
287
- raise
288
- end
289
- }
290
- self
291
- end
292
-
293
- def __dump_fields
294
- self.class.instance_variable_get(:@fields).each { |field|
295
- begin
296
- vs = send(field.name)
297
- catch(:ignored) do
298
- __dump_field(vs, field)
299
- end
300
- rescue
301
- STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
302
- raise
303
- end
304
- }
305
- self
306
- end
307
-
308
- def __convert(input, output, input_big, output_big, parent = nil, index = nil)
309
- __set_convert_type(input, output, input_big, output_big, parent, index)
310
- __convert_fields
311
- __unset_convert_type
312
- self
313
- end
314
-
315
- def __load(input, input_big, parent = nil, index = nil)
316
- __set_load_type(input, input_big, parent, index)
317
- __load_fields
318
- __unset_load_type
319
- self
320
- end
321
-
322
- def __dump(output, output_big, parent = nil, index = nil)
323
- __set_dump_type(output, output_big, parent, index)
324
- __dump_fields
325
- __unset_dump_type
326
- self
327
- end
328
-
329
- def self.convert(input, output, input_big = LibBin::default_big?, output_big = !LibBin::default_big?, parent = nil, index = nil)
330
- h = self::new
331
- h.__convert(input, output, input_big, output_big, parent, index)
332
- h
333
- end
334
-
335
- def self.load(input, input_big = LibBin::default_big?, parent = nil, index = nil)
336
- h = self::new
337
- h.__load(input, input_big, parent, index)
338
- h
339
- end
340
-
341
- def self.dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil)
342
- value.__dump(output, output_big, parent, index)
343
- end
344
-
345
- def self.size(value, previous_offset = 0, parent = nil, index = nil)
346
- value.__shape(previous_offset, parent, index).size
347
- end
348
-
349
- def self.shape(value, previous_offset = 0, parent = nil, index = nil, kind = DataShape)
350
- value.__shape(previous_offset, parent, index, kind)
39
+ def self.size(value, previous_offset = 0, parent = nil, index = nil, length = nil)
40
+ if length
41
+ shape(value, previous_offset, parent, index, length).size
42
+ else
43
+ value.__shape(previous_offset, parent, index).size
44
+ end
351
45
  end
352
46
 
353
47
  end
data/libbin.gemspec CHANGED
@@ -1,14 +1,13 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'libbin'
3
- s.version = "1.0.4"
3
+ s.version = "1.0.8"
4
4
  s.author = "Brice Videau"
5
- s.email = "brice.videau@imag.fr"
5
+ s.email = "brice.videau@gmail.com"
6
6
  s.homepage = "https://github.com/kerilk/libbin"
7
7
  s.summary = "Library for loading and converting binary files"
8
8
  s.description = "Read, write and convert Binary data in Ruby."
9
9
  s.files = Dir[ 'libbin.gemspec', 'LICENSE', 'lib/**/*.rb', 'ext/libbin/extconf.rb', 'ext/libbin/*.c', 'ext/libbin/*.h' ]
10
10
  s.extensions << 'ext/libbin/extconf.rb'
11
- s.has_rdoc = false
12
11
  s.license = 'BSD-2-Clause'
13
12
  s.required_ruby_version = '>= 2.0.0'
14
13
  s.add_dependency 'float-formats', '~> 0.3', '>=0.3.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libbin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brice Videau
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-09-13 00:00:00.000000000 Z
11
+ date: 2021-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: float-formats
@@ -31,17 +31,21 @@ dependencies:
31
31
  - !ruby/object:Gem::Version
32
32
  version: 0.3.0
33
33
  description: Read, write and convert Binary data in Ruby.
34
- email: brice.videau@imag.fr
34
+ email: brice.videau@gmail.com
35
35
  executables: []
36
36
  extensions:
37
37
  - ext/libbin/extconf.rb
38
38
  extra_rdoc_files: []
39
39
  files:
40
40
  - LICENSE
41
+ - ext/libbin/data_types.c
42
+ - ext/libbin/data_types.h
41
43
  - ext/libbin/extconf.rb
42
44
  - ext/libbin/half.c
43
45
  - ext/libbin/half.h
44
46
  - ext/libbin/libbin_c.c
47
+ - ext/libbin/libbin_c.h
48
+ - ext/libbin/libbin_endian.h
45
49
  - ext/libbin/pghalf.c
46
50
  - ext/libbin/pghalf.h
47
51
  - lib/libbin.rb
@@ -67,8 +71,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
71
  - !ruby/object:Gem::Version
68
72
  version: '0'
69
73
  requirements: []
70
- rubyforge_project:
71
- rubygems_version: 2.7.6
74
+ rubygems_version: 3.1.2
72
75
  signing_key:
73
76
  specification_version: 4
74
77
  summary: Library for loading and converting binary files