libbin 0.9.0 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +0 -0
- data/ext/libbin/extconf.rb +3 -0
- data/ext/libbin/half.c +719 -0
- data/ext/libbin/half.h +18 -0
- data/ext/libbin/libbin_c.c +65 -0
- data/ext/libbin/pghalf.c +449 -0
- data/ext/libbin/pghalf.h +9 -0
- data/lib/libbin.rb +51 -54
- data/lib/libbin/alignment.rb +0 -0
- data/lib/libbin/data_types.rb +63 -35
- data/libbin.gemspec +3 -2
- metadata +10 -3
data/ext/libbin/pghalf.h
ADDED
data/lib/libbin.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
$VERBOSE = warn_level
|
5
|
-
|
6
|
-
Flt::IEEE.binary :IEEE_binary16_pg, significand: 9, exponent: 6, bias: 47
|
7
|
-
Flt::IEEE.binary :IEEE_binary16_pg_BE, significand: 9, exponent: 6, bias: 47, endianness: :big_endian
|
1
|
+
module LibBin
|
2
|
+
end
|
3
|
+
require "libbin_c.so"
|
8
4
|
|
9
5
|
require_relative 'libbin/alignment'
|
10
6
|
require_relative 'libbin/data_types'
|
@@ -121,10 +117,11 @@ module LibBin
|
|
121
117
|
end
|
122
118
|
end
|
123
119
|
|
124
|
-
def __decode_seek_offset(offset)
|
120
|
+
def __decode_seek_offset(offset, relative_offset)
|
125
121
|
return nil unless offset
|
126
122
|
offset = __decode_expression(offset)
|
127
123
|
return false if offset == 0x0
|
124
|
+
offset += @__position if relative_offset
|
128
125
|
@__cur_position = offset
|
129
126
|
@__input.seek(offset) if @__input
|
130
127
|
@__output.seek(offset) if @__output
|
@@ -145,31 +142,31 @@ module LibBin
|
|
145
142
|
return __decode_expression(type)
|
146
143
|
end
|
147
144
|
|
148
|
-
def __decode_static_conditions(
|
145
|
+
def __decode_static_conditions(field)
|
149
146
|
@__offset = nil
|
150
147
|
@__condition = nil
|
151
148
|
@__type = nil
|
152
149
|
@__count = nil
|
153
|
-
unless sequence
|
154
|
-
@__offset = __decode_seek_offset(offset)
|
150
|
+
unless field.sequence?
|
151
|
+
@__offset = __decode_seek_offset(field.offset, field.relative_offset?)
|
155
152
|
throw :ignored, nil if @__offset == false
|
156
|
-
@__condition = __decode_condition(condition)
|
153
|
+
@__condition = __decode_condition(field.condition)
|
157
154
|
throw :ignored, nil unless @__condition
|
158
|
-
@__type = __decode_type(type)
|
155
|
+
@__type = __decode_type(field.type)
|
159
156
|
end
|
160
|
-
@__count = __decode_count(count)
|
157
|
+
@__count = __decode_count(field.count)
|
161
158
|
end
|
162
159
|
|
163
|
-
def __decode_dynamic_conditions(
|
164
|
-
return true unless sequence
|
160
|
+
def __decode_dynamic_conditions(field)
|
161
|
+
return true unless field.sequence?
|
165
162
|
@__offset = nil
|
166
163
|
@__condition = nil
|
167
164
|
@__type = nil
|
168
|
-
@__offset = __decode_seek_offset(offset)
|
165
|
+
@__offset = __decode_seek_offset(field.offset, field.relative_offset?)
|
169
166
|
return false if @__offset == false
|
170
|
-
@__condition = __decode_condition(condition)
|
167
|
+
@__condition = __decode_condition(field.condition)
|
171
168
|
return false unless @__condition
|
172
|
-
@__type = __decode_type(type)
|
169
|
+
@__type = __decode_type(field.type)
|
173
170
|
return true
|
174
171
|
end
|
175
172
|
|
@@ -181,61 +178,61 @@ module LibBin
|
|
181
178
|
@__condition = nil
|
182
179
|
end
|
183
180
|
|
184
|
-
def __convert_field(field
|
185
|
-
__decode_static_conditions(
|
181
|
+
def __convert_field(field)
|
182
|
+
__decode_static_conditions(field)
|
186
183
|
vs = @__count.times.collect do |it|
|
187
184
|
@__iterator = it
|
188
|
-
if __decode_dynamic_conditions(
|
185
|
+
if __decode_dynamic_conditions(field)
|
189
186
|
@__type::convert(@__input, @__output, @__input_big, @__output_big, self, it)
|
190
187
|
else
|
191
188
|
nil
|
192
189
|
end
|
193
190
|
end
|
194
191
|
__restore_context
|
195
|
-
vs = vs.first unless count
|
192
|
+
vs = vs.first unless field.count
|
196
193
|
vs
|
197
194
|
end
|
198
195
|
|
199
|
-
def __load_field(field
|
200
|
-
__decode_static_conditions(
|
196
|
+
def __load_field(field)
|
197
|
+
__decode_static_conditions(field)
|
201
198
|
vs = @__count.times.collect do |it|
|
202
199
|
@__iterator = it
|
203
|
-
if __decode_dynamic_conditions(
|
200
|
+
if __decode_dynamic_conditions(field)
|
204
201
|
@__type::load(@__input, @__input_big, self, it)
|
205
202
|
else
|
206
203
|
nil
|
207
204
|
end
|
208
205
|
end
|
209
206
|
__restore_context
|
210
|
-
vs = vs.first unless count
|
207
|
+
vs = vs.first unless field.count
|
211
208
|
vs
|
212
209
|
end
|
213
210
|
|
214
|
-
def __dump_field(vs, field
|
215
|
-
__decode_static_conditions(
|
216
|
-
vs = [vs] unless count
|
211
|
+
def __dump_field(vs, field)
|
212
|
+
__decode_static_conditions(field)
|
213
|
+
vs = [vs] unless field.count
|
217
214
|
vs.each_with_index do |v, it|
|
218
215
|
@__iterator = it
|
219
|
-
if __decode_dynamic_conditions(
|
216
|
+
if __decode_dynamic_conditions(field)
|
220
217
|
@__type::dump(v, @__output, @__output_big, self, it)
|
221
218
|
end
|
222
219
|
end
|
223
220
|
__restore_context
|
224
221
|
end
|
225
222
|
|
226
|
-
def __shape_field(vs, previous_offset, kind, field
|
227
|
-
__decode_static_conditions(
|
228
|
-
vs = [vs] unless count
|
223
|
+
def __shape_field(vs, previous_offset, kind, field)
|
224
|
+
__decode_static_conditions(field)
|
225
|
+
vs = [vs] unless field.count
|
229
226
|
vs = vs.each_with_index.collect do |v, it|
|
230
227
|
@__iterator = it
|
231
|
-
if __decode_dynamic_conditions(
|
228
|
+
if __decode_dynamic_conditions(field)
|
232
229
|
sh = @__type::shape(v, @__cur_position, self, it, kind)
|
233
230
|
@__cur_position = sh.last + 1 if sh.last && sh.last >= 0
|
234
231
|
sh
|
235
232
|
end
|
236
233
|
end
|
237
234
|
__restore_context
|
238
|
-
vs = vs.first unless count
|
235
|
+
vs = vs.first unless field.count
|
239
236
|
vs
|
240
237
|
end
|
241
238
|
|
@@ -246,15 +243,15 @@ module LibBin
|
|
246
243
|
def __shape(previous_offset = 0, parent = nil, index = nil, kind = DataShape)
|
247
244
|
__set_size_type(previous_offset, parent, index)
|
248
245
|
members = {}
|
249
|
-
self.class.instance_variable_get(:@fields).each { |
|
246
|
+
self.class.instance_variable_get(:@fields).each { |field|
|
250
247
|
begin
|
251
|
-
vs = send(name)
|
248
|
+
vs = send(field.name)
|
252
249
|
member = catch(:ignored) do
|
253
|
-
__shape_field(vs, previous_offset, kind,
|
250
|
+
__shape_field(vs, previous_offset, kind, field)
|
254
251
|
end
|
255
|
-
members[name] = member
|
252
|
+
members[field.name] = member
|
256
253
|
rescue
|
257
|
-
STDERR.puts "#{self.class}: #{name}(#{type})"
|
254
|
+
STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
|
258
255
|
raise
|
259
256
|
end
|
260
257
|
}
|
@@ -264,14 +261,14 @@ module LibBin
|
|
264
261
|
end
|
265
262
|
|
266
263
|
def __convert_fields
|
267
|
-
self.class.instance_variable_get(:@fields).each { |
|
264
|
+
self.class.instance_variable_get(:@fields).each { |field|
|
268
265
|
begin
|
269
266
|
vs = catch(:ignored) do
|
270
|
-
__convert_field(
|
267
|
+
__convert_field(field)
|
271
268
|
end
|
272
|
-
send("#{name}=", vs)
|
269
|
+
send("#{field.name}=", vs)
|
273
270
|
rescue
|
274
|
-
STDERR.puts "#{self.class}: #{name}(#{type})"
|
271
|
+
STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
|
275
272
|
raise
|
276
273
|
end
|
277
274
|
}
|
@@ -279,14 +276,14 @@ module LibBin
|
|
279
276
|
end
|
280
277
|
|
281
278
|
def __load_fields
|
282
|
-
self.class.instance_variable_get(:@fields).each { |
|
279
|
+
self.class.instance_variable_get(:@fields).each { |field|
|
283
280
|
begin
|
284
281
|
vs = catch(:ignored) do
|
285
|
-
__load_field(
|
282
|
+
__load_field(field)
|
286
283
|
end
|
287
|
-
send("#{name}=", vs)
|
284
|
+
send("#{field.name}=", vs)
|
288
285
|
rescue
|
289
|
-
STDERR.puts "#{self.class}: #{name}(#{type})"
|
286
|
+
STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
|
290
287
|
raise
|
291
288
|
end
|
292
289
|
}
|
@@ -294,14 +291,14 @@ module LibBin
|
|
294
291
|
end
|
295
292
|
|
296
293
|
def __dump_fields
|
297
|
-
self.class.instance_variable_get(:@fields).each { |
|
294
|
+
self.class.instance_variable_get(:@fields).each { |field|
|
298
295
|
begin
|
299
|
-
vs = send(name)
|
296
|
+
vs = send(field.name)
|
300
297
|
catch(:ignored) do
|
301
|
-
__dump_field(vs,
|
298
|
+
__dump_field(vs, field)
|
302
299
|
end
|
303
300
|
rescue
|
304
|
-
STDERR.puts "#{self.class}: #{name}(#{type})"
|
301
|
+
STDERR.puts "#{self.class}: #{field.name}(#{field.type})"
|
305
302
|
raise
|
306
303
|
end
|
307
304
|
}
|
@@ -350,7 +347,7 @@ module LibBin
|
|
350
347
|
end
|
351
348
|
|
352
349
|
def self.shape(value, previous_offset = 0, parent = nil, index = nil, kind = DataShape)
|
353
|
-
value.__shape(previous_offset, parent, index, kind
|
350
|
+
value.__shape(previous_offset, parent, index, kind)
|
354
351
|
end
|
355
352
|
|
356
353
|
end
|
data/lib/libbin/alignment.rb
CHANGED
File without changes
|
data/lib/libbin/data_types.rb
CHANGED
@@ -70,6 +70,34 @@ module LibBin
|
|
70
70
|
|
71
71
|
end
|
72
72
|
|
73
|
+
class Field
|
74
|
+
attr_reader :name,
|
75
|
+
:type,
|
76
|
+
:count,
|
77
|
+
:offset,
|
78
|
+
:sequence,
|
79
|
+
:condition
|
80
|
+
|
81
|
+
def sequence?
|
82
|
+
@sequence
|
83
|
+
end
|
84
|
+
|
85
|
+
def relative_offset?
|
86
|
+
@relative_offset
|
87
|
+
end
|
88
|
+
|
89
|
+
def initialize(name, type, count, offset, sequence, condition, relative_offset)
|
90
|
+
@name = name
|
91
|
+
@type = type
|
92
|
+
@count = count
|
93
|
+
@offset = offset
|
94
|
+
@sequence = sequence
|
95
|
+
@condition = condition
|
96
|
+
@relative_offset = relative_offset
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
73
101
|
class DataConverter
|
74
102
|
|
75
103
|
rl = lambda { |type, str|
|
@@ -218,18 +246,18 @@ module LibBin
|
|
218
246
|
:E => l["E"],
|
219
247
|
:G => l["G"],
|
220
248
|
:"a*" => l["a*"],
|
221
|
-
:half => [ lambda { |str|
|
222
|
-
lambda { |v|
|
223
|
-
:half_le => [ lambda { |str|
|
224
|
-
lambda { |v|
|
225
|
-
:half_be => [ lambda { |str|
|
226
|
-
lambda { |v|
|
227
|
-
:pghalf => [ lambda { |str|
|
228
|
-
lambda { |v|
|
229
|
-
:pghalf_le => [ lambda { |str|
|
230
|
-
lambda { |v|
|
231
|
-
:pghalf_be => [ lambda { |str|
|
232
|
-
lambda { |v|
|
249
|
+
:half => [ lambda { |str| LibBin::half_from_string(str, "S>") },
|
250
|
+
lambda { |v| LibBin::half_to_string(v, "S>") } ],
|
251
|
+
:half_le => [ lambda { |str| LibBin::half_from_string(str, "S<") },
|
252
|
+
lambda { |v| LibBin::half_to_string(v, "S<") } ],
|
253
|
+
:half_be => [ lambda { |str| LibBin::half_from_string(str, "S>") },
|
254
|
+
lambda { |v| LibBin::half_to_string(v, "S>") } ],
|
255
|
+
:pghalf => [ lambda { |str| LibBin::pghalf_from_string(str, "S>") },
|
256
|
+
lambda { |v| LibBin::pghalf_to_string(v, "S>") } ],
|
257
|
+
:pghalf_le => [ lambda { |str| LibBin::pghalf_from_string(str, "S<") },
|
258
|
+
lambda { |v| LibBin::pghalf_to_string(v, "S<") } ],
|
259
|
+
:pghalf_be => [ lambda { |str| LibBin::pghalf_from_string(str, "S>") },
|
260
|
+
lambda { |v| LibBin::pghalf_to_string(v, "S>") } ]
|
233
261
|
} )
|
234
262
|
DATA_ENDIAN[false].merge!( {
|
235
263
|
:c => l["c"],
|
@@ -263,18 +291,18 @@ module LibBin
|
|
263
291
|
:E => l["E"],
|
264
292
|
:G => l["G"],
|
265
293
|
:"a*" => l["a*"],
|
266
|
-
:half => [ lambda { |str|
|
267
|
-
lambda { |v|
|
268
|
-
:half_le => [ lambda { |str|
|
269
|
-
lambda { |v|
|
270
|
-
:half_be => [ lambda { |str|
|
271
|
-
lambda { |v|
|
272
|
-
:pghalf => [ lambda { |str|
|
273
|
-
lambda { |v|
|
274
|
-
:pghalf_le => [ lambda { |str|
|
275
|
-
lambda { |v|
|
276
|
-
:pghalf_be => [ lambda { |str|
|
277
|
-
lambda { |v|
|
294
|
+
:half => [ lambda { |str| LibBin::half_from_string(str, "S<") },
|
295
|
+
lambda { |v| LibBin::half_to_string(v, "S<") } ],
|
296
|
+
:half_le => [ lambda { |str| LibBin::half_from_string(str, "S<") },
|
297
|
+
lambda { |v| LibBin::half_to_string(v, "S<") } ],
|
298
|
+
:half_be => [ lambda { |str| LibBin::half_from_string(str, "S>") },
|
299
|
+
lambda { |v| LibBin::half_to_string(v, "S>") } ],
|
300
|
+
:pghalf => [ lambda { |str| LibBin::pghalf_from_string(str, "S<") },
|
301
|
+
lambda { |v| LibBin::pghalf_to_string(v, "S<") } ],
|
302
|
+
:pghalf_le => [ lambda { |str| LibBin::pghalf_from_string(str, "S<") },
|
303
|
+
lambda { |v| LibBin::pghalf_to_string(v, "S<") } ],
|
304
|
+
:pghalf_be => [ lambda { |str| LibBin::pghalf_from_string(str, "S>") },
|
305
|
+
lambda { |v| LibBin::pghalf_to_string(v, "S>") } ]
|
278
306
|
} )
|
279
307
|
|
280
308
|
|
@@ -292,7 +320,7 @@ module LibBin
|
|
292
320
|
@symbol = symbol
|
293
321
|
@size = DATA_SIZES[symbol]
|
294
322
|
@rl_be, @sl_be = DATA_ENDIAN[true][symbol]
|
295
|
-
@rl_le, @
|
323
|
+
@rl_le, @sl_le = DATA_ENDIAN[false][symbol]
|
296
324
|
end
|
297
325
|
|
298
326
|
def self.load(input, input_big = LibBin::default_big?, _ = nil, _ = nil)
|
@@ -305,7 +333,7 @@ module LibBin
|
|
305
333
|
output.write(str)
|
306
334
|
end
|
307
335
|
|
308
|
-
def self.convert(input, output, input_big = LibBin::default_big?, output_big = !
|
336
|
+
def self.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, _ = nil, _ = nil)
|
309
337
|
str = input.read(@size)
|
310
338
|
value = (input_big ? @rl_be[str] : @rl_le[str])
|
311
339
|
str = (output_big ? @sl_be[value] : @sl_le[value])
|
@@ -340,17 +368,17 @@ module LibBin
|
|
340
368
|
|
341
369
|
end
|
342
370
|
|
343
|
-
def self.register_field(field, type, count: nil, offset: nil, sequence: false, condition: nil)
|
371
|
+
def self.register_field(field, type, count: nil, offset: nil, sequence: false, condition: nil, relative_offset: false)
|
344
372
|
if type.kind_of?(Symbol)
|
345
373
|
if type[0] == 'a'
|
346
|
-
|
347
|
-
@fields.push([field, c, count, offset, sequence, condition])
|
374
|
+
real_type = Class::new(Str) do init(sym) end
|
348
375
|
else
|
349
|
-
|
376
|
+
real_type = const_get(SCALAR_TYPES[type][0])
|
350
377
|
end
|
351
378
|
else
|
352
|
-
|
379
|
+
real_type = type
|
353
380
|
end
|
381
|
+
@fields.push(Field::new(field, real_type, count, offset, sequence, condition, relative_offset))
|
354
382
|
attr_accessor field
|
355
383
|
end
|
356
384
|
|
@@ -361,8 +389,8 @@ module LibBin
|
|
361
389
|
init(#{symbol.inspect})
|
362
390
|
end
|
363
391
|
|
364
|
-
def self.#{name}(field, count: nil, offset: nil, sequence: false, condition: nil)
|
365
|
-
@fields.push(
|
392
|
+
def self.#{name}(field, count: nil, offset: nil, sequence: false, condition: nil, relative_offset: false)
|
393
|
+
@fields.push(Field::new(field, #{klassname}, count, offset, sequence, condition, relative_offset))
|
366
394
|
attr_accessor field
|
367
395
|
end
|
368
396
|
EOF
|
@@ -401,12 +429,12 @@ EOF
|
|
401
429
|
create_scalar_type(:pghalf_le)
|
402
430
|
create_scalar_type(:pghalf_be)
|
403
431
|
|
404
|
-
def self.string( field, length = nil, count: nil, offset: nil, sequence: false, condition: nil)
|
432
|
+
def self.string( field, length = nil, count: nil, offset: nil, sequence: false, condition: nil, relative_offset: false)
|
405
433
|
sym = (length ? :"a#{length}" : :"a*")
|
406
434
|
c = Class::new(Str) do
|
407
435
|
init(sym)
|
408
436
|
end
|
409
|
-
@fields.push(
|
437
|
+
@fields.push(Field::new(field, c, count, offset, sequence, condition, relative_offset))
|
410
438
|
attr_accessor field
|
411
439
|
end
|
412
440
|
|
data/libbin.gemspec
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'libbin'
|
3
|
-
s.version = "0.
|
3
|
+
s.version = "1.0.4"
|
4
4
|
s.author = "Brice Videau"
|
5
5
|
s.email = "brice.videau@imag.fr"
|
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
|
-
s.files = Dir[ 'libbin.gemspec', 'LICENSE', 'lib/**/*.rb' ]
|
9
|
+
s.files = Dir[ 'libbin.gemspec', 'LICENSE', 'lib/**/*.rb', 'ext/libbin/extconf.rb', 'ext/libbin/*.c', 'ext/libbin/*.h' ]
|
10
|
+
s.extensions << 'ext/libbin/extconf.rb'
|
10
11
|
s.has_rdoc = false
|
11
12
|
s.license = 'BSD-2-Clause'
|
12
13
|
s.required_ruby_version = '>= 2.0.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: 0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brice Videau
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-09-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: float-formats
|
@@ -33,10 +33,17 @@ dependencies:
|
|
33
33
|
description: Read, write and convert Binary data in Ruby.
|
34
34
|
email: brice.videau@imag.fr
|
35
35
|
executables: []
|
36
|
-
extensions:
|
36
|
+
extensions:
|
37
|
+
- ext/libbin/extconf.rb
|
37
38
|
extra_rdoc_files: []
|
38
39
|
files:
|
39
40
|
- LICENSE
|
41
|
+
- ext/libbin/extconf.rb
|
42
|
+
- ext/libbin/half.c
|
43
|
+
- ext/libbin/half.h
|
44
|
+
- ext/libbin/libbin_c.c
|
45
|
+
- ext/libbin/pghalf.c
|
46
|
+
- ext/libbin/pghalf.h
|
40
47
|
- lib/libbin.rb
|
41
48
|
- lib/libbin/alignment.rb
|
42
49
|
- lib/libbin/data_types.rb
|