libbin 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 230be2238c11ec4505d14cea8a43181b6b006cd3db0eccc4d40d1fb117912681
4
- data.tar.gz: c21f1a9ab288be527d14eeea80f248f9332d98d3dc8351dd811536aaa24850f0
3
+ metadata.gz: e87d96cfea4bd0ff2ca321e9e318eb2afcdddee0764811db35e2a92835396db8
4
+ data.tar.gz: e75d3aa6a8374e04db4e84db9eb88bc768a99e3ceff932d0ce26d4f6501ae5e6
5
5
  SHA512:
6
- metadata.gz: a8fbca741eb24035f80751e3eaf49a3b1b89ac1dbb8f4ae9cec3b58c77ddaf2e8b93c74af781cde5756e07d4a4fd115d07e703a8b5f8ba537da104c82f4a845d
7
- data.tar.gz: 307b2a33d3d6d3357bc8c59079dfa0bbdb62a9e189894abde7d9b32bcf180c42b5614dcdb4ccdcebdc593c40e32afe59c3ad018db50a3ecff8568c833034778e
6
+ metadata.gz: f9d940fd59641087b72fb734ba2db6833aa0c7b4620205e8e568580ccc64d4e1f49a433ca206ebe474b8e3946417d1a96491bc79ff977435ef442ad35d9590f7
7
+ data.tar.gz: 3b04707ce8a3d434596e641dabf1a4a58d95403207d492089029c37f68e4bdf49e568420f7736f426cd95377e7adf1a95ff306d3bcd3c9b0d71544fc541ef156
@@ -11,7 +11,7 @@ static VALUE pghalf_from_string_p(VALUE self, VALUE str, VALUE pack_str) {
11
11
  Check_Type(str, T_STRING);
12
12
  Check_Type(pack_str, T_STRING);
13
13
  VALUE arr = rb_funcall(str, rb_intern("unpack"), 1, pack_str);
14
- uint16_t val = NUM2USHORT(rb_funcall(arr, rb_intern("first"), 0));
14
+ uint16_t val = NUM2USHORT(rb_funcall(arr, rb_intern("first"), 0));
15
15
  union float_u res;
16
16
 
17
17
  res.i = pghalf_to_float(val);
@@ -22,7 +22,7 @@ static VALUE half_from_string_p(VALUE self, VALUE str, VALUE pack_str) {
22
22
  Check_Type(str, T_STRING);
23
23
  Check_Type(pack_str, T_STRING);
24
24
  VALUE arr = rb_funcall(str, rb_intern("unpack"), 1, pack_str);
25
- uint16_t val = NUM2USHORT(rb_funcall(arr, rb_intern("first"), 0));
25
+ uint16_t val = NUM2USHORT(rb_funcall(arr, rb_intern("first"), 0));
26
26
  union float_u res;
27
27
 
28
28
  res.i = half_to_float(val);
@@ -32,25 +32,25 @@ static VALUE half_from_string_p(VALUE self, VALUE str, VALUE pack_str) {
32
32
  static VALUE pghalf_to_string_p(VALUE self, VALUE number, VALUE pack_str) {
33
33
  Check_Type(number, T_FLOAT);
34
34
  union float_u val;
35
- uint16_t res;
35
+ uint16_t res;
36
36
 
37
37
  val.f = NUM2DBL(number);
38
38
  res = pghalf_from_float(val.i);
39
- VALUE arr = rb_ary_new3(1, UINT2NUM(res) );
39
+ VALUE arr = rb_ary_new3(1, UINT2NUM(res) );
40
40
 
41
- return rb_funcall(arr, rb_intern("pack"), 1, pack_str);
41
+ return rb_funcall(arr, rb_intern("pack"), 1, pack_str);
42
42
  }
43
43
 
44
44
  static VALUE half_to_string_p(VALUE self, VALUE number, VALUE pack_str) {
45
45
  Check_Type(number, T_FLOAT);
46
46
  union float_u val;
47
- uint16_t res;
47
+ uint16_t res;
48
48
 
49
49
  val.f = NUM2DBL(number);
50
50
  res = half_from_float(val.i);
51
- VALUE arr = rb_ary_new3(1, UINT2NUM(res) );
51
+ VALUE arr = rb_ary_new3(1, UINT2NUM(res) );
52
52
 
53
- return rb_funcall(arr, rb_intern("pack"), 1, pack_str);
53
+ return rb_funcall(arr, rb_intern("pack"), 1, pack_str);
54
54
  }
55
55
 
56
56
  void Init_libbin_c() {
@@ -142,10 +142,15 @@ module LibBin
142
142
  return __decode_expression(type)
143
143
  end
144
144
 
145
+ def __decode_length(length)
146
+ __decode_expression(length)
147
+ end
148
+
145
149
  def __decode_static_conditions(field)
146
150
  @__offset = nil
147
151
  @__condition = nil
148
152
  @__type = nil
153
+ @__length = nil
149
154
  @__count = nil
150
155
  unless field.sequence?
151
156
  @__offset = __decode_seek_offset(field.offset, field.relative_offset?)
@@ -153,6 +158,7 @@ module LibBin
153
158
  @__condition = __decode_condition(field.condition)
154
159
  throw :ignored, nil unless @__condition
155
160
  @__type = __decode_type(field.type)
161
+ @__length = __decode_length(field.length)
156
162
  end
157
163
  @__count = __decode_count(field.count)
158
164
  end
@@ -162,17 +168,20 @@ module LibBin
162
168
  @__offset = nil
163
169
  @__condition = nil
164
170
  @__type = nil
171
+ @__length = nil
165
172
  @__offset = __decode_seek_offset(field.offset, field.relative_offset?)
166
173
  return false if @__offset == false
167
174
  @__condition = __decode_condition(field.condition)
168
175
  return false unless @__condition
169
176
  @__type = __decode_type(field.type)
177
+ @__length = __decode_length(field.length)
170
178
  return true
171
179
  end
172
180
 
173
181
  def __restore_context
174
182
  @__iterator = nil
175
183
  @__type = nil
184
+ @__length = nil
176
185
  @__count = nil
177
186
  @__offset = nil
178
187
  @__condition = nil
@@ -183,7 +192,7 @@ module LibBin
183
192
  vs = @__count.times.collect do |it|
184
193
  @__iterator = it
185
194
  if __decode_dynamic_conditions(field)
186
- @__type::convert(@__input, @__output, @__input_big, @__output_big, self, it)
195
+ @__type::convert(@__input, @__output, @__input_big, @__output_big, self, it, @__length)
187
196
  else
188
197
  nil
189
198
  end
@@ -198,7 +207,7 @@ module LibBin
198
207
  vs = @__count.times.collect do |it|
199
208
  @__iterator = it
200
209
  if __decode_dynamic_conditions(field)
201
- @__type::load(@__input, @__input_big, self, it)
210
+ @__type::load(@__input, @__input_big, self, it, @__length)
202
211
  else
203
212
  nil
204
213
  end
@@ -214,7 +223,7 @@ module LibBin
214
223
  vs.each_with_index do |v, it|
215
224
  @__iterator = it
216
225
  if __decode_dynamic_conditions(field)
217
- @__type::dump(v, @__output, @__output_big, self, it)
226
+ @__type::dump(v, @__output, @__output_big, self, it, @__length)
218
227
  end
219
228
  end
220
229
  __restore_context
@@ -226,7 +235,7 @@ module LibBin
226
235
  vs = vs.each_with_index.collect do |v, it|
227
236
  @__iterator = it
228
237
  if __decode_dynamic_conditions(field)
229
- sh = @__type::shape(v, @__cur_position, self, it, kind)
238
+ sh = @__type::shape(v, @__cur_position, self, it, kind, @__length)
230
239
  @__cur_position = sh.last + 1 if sh.last && sh.last >= 0
231
240
  sh
232
241
  end
@@ -326,28 +335,57 @@ module LibBin
326
335
  self
327
336
  end
328
337
 
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
338
+ def self.convert(input, output, input_big = LibBin::default_big?, output_big = !LibBin::default_big?, parent = nil, index = nil, length = nil)
339
+ if length
340
+ length.times.collect {
341
+ h = self::new
342
+ h.__load(input, input_big, parent, index)
343
+ }
344
+ else
345
+ h = self::new
346
+ h.__convert(input, output, input_big, output_big, parent, index)
347
+ end
333
348
  end
334
349
 
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
350
+ def self.load(input, input_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
351
+ if length
352
+ length.times.collect {
353
+ h = self::new
354
+ h.__load(input, input_big, parent, index)
355
+ }
356
+ else
357
+ h = self::new
358
+ h.__load(input, input_big, parent, index)
359
+ end
339
360
  end
340
361
 
341
- def self.dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil)
342
- value.__dump(output, output_big, parent, index)
362
+ def self.dump(value, output, output_big = LibBin::default_big?, parent = nil, index = nil, length = nil)
363
+ if length
364
+ length.times.collect { |i|
365
+ value[i].__dump(output, output_big, parent, index)
366
+ }
367
+ value
368
+ else
369
+ value.__dump(output, output_big, parent, index)
370
+ end
343
371
  end
344
372
 
345
- def self.size(value, previous_offset = 0, parent = nil, index = nil)
346
- value.__shape(previous_offset, parent, index).size
373
+ def self.size(value, previous_offset = 0, parent = nil, index = nil, length = nil)
374
+ if length
375
+ shape(value, previous_offset, parent, index, length).size
376
+ else
377
+ value.__shape(previous_offset, parent, index).size
378
+ end
347
379
  end
348
380
 
349
- def self.shape(value, previous_offset = 0, parent = nil, index = nil, kind = DataShape)
350
- value.__shape(previous_offset, parent, index, kind)
381
+ def self.shape(value, previous_offset = 0, parent = nil, index = nil, kind = DataShape, length = nil)
382
+ if length
383
+ kind::new(length.times.collect { |i|
384
+ value[i].__shape(previous_offset, parent, index, kind)
385
+ })
386
+ else
387
+ value.__shape(previous_offset, parent, index, kind)
388
+ end
351
389
  end
352
390
 
353
391
  end
@@ -73,6 +73,7 @@ module LibBin
73
73
  class Field
74
74
  attr_reader :name,
75
75
  :type,
76
+ :length,
76
77
  :count,
77
78
  :offset,
78
79
  :sequence,
@@ -86,9 +87,10 @@ module LibBin
86
87
  @relative_offset
87
88
  end
88
89
 
89
- def initialize(name, type, count, offset, sequence, condition, relative_offset)
90
+ def initialize(name, type, length, count, offset, sequence, condition, relative_offset)
90
91
  @name = name
91
92
  @type = type
93
+ @length = length
92
94
  @count = count
93
95
  @offset = offset
94
96
  @sequence = sequence
@@ -100,12 +102,20 @@ module LibBin
100
102
 
101
103
  class DataConverter
102
104
 
103
- rl = lambda { |type, str|
104
- str.unpack(type.to_s).first
105
+ rl = lambda { |type, str, number = nil|
106
+ if number
107
+ str.unpack(type.to_s+number.to_s)
108
+ else
109
+ str.unpack(type.to_s).first
110
+ end
105
111
  }
106
112
 
107
- sl = lambda { |type, value|
108
- [value].pack(type.to_s)
113
+ sl = lambda { |type, value, number = nil|
114
+ if number
115
+ value.pack(type.to_s+number.to_s)
116
+ else
117
+ [value].pack(type.to_s)
118
+ end
109
119
  }
110
120
 
111
121
  l = lambda { |type|
@@ -189,6 +199,7 @@ module LibBin
189
199
  :D => 8,
190
200
  :E => 8,
191
201
  :G => 8,
202
+ :a => 1,
192
203
  :"a*" => -1,
193
204
  :half => 2,
194
205
  :half_le => 2,
@@ -214,6 +225,50 @@ module LibBin
214
225
  }
215
226
  }
216
227
 
228
+ rhl = lambda { |type, str, number = nil|
229
+ if number
230
+ number.times.collect { |i| LibBin::half_from_string(str[i*2,2], type) }
231
+ else
232
+ LibBin::half_from_string(str, type)
233
+ end
234
+ }
235
+
236
+ shl = lambda { |type, value, number = nil|
237
+ if number
238
+ str = ""
239
+ number.times { |i| str << LibBin::half_to_string(value[i], type) }
240
+ str
241
+ else
242
+ LibBin::half_to_string(value, type)
243
+ end
244
+ }
245
+
246
+ hl = lambda { |type|
247
+ [rhl.curry[type], shl.curry[type]]
248
+ }
249
+
250
+ rpghl = lambda { |type, str, number = nil|
251
+ if number
252
+ number.times.collect { |i| LibBin::pghalf_from_string(str[i*2,2], type) }
253
+ else
254
+ LibBin::pghalf_from_string(str, type)
255
+ end
256
+ }
257
+
258
+ spghl = lambda { |type, value, number = nil|
259
+ if number
260
+ str = ""
261
+ number.times { |i| str << LibBin::pghalf_to_string(value[i], type) }
262
+ str
263
+ else
264
+ LibBin::pghalf_to_string(value, type)
265
+ end
266
+ }
267
+
268
+ pghl = lambda { |type|
269
+ [rpghl.curry[type], spghl.curry[type]]
270
+ }
271
+
217
272
  DATA_ENDIAN[true].merge!( {
218
273
  :c => l["c"],
219
274
  :C => l["C"],
@@ -245,19 +300,12 @@ module LibBin
245
300
  :D => l["G"],
246
301
  :E => l["E"],
247
302
  :G => l["G"],
248
- :"a*" => l["a*"],
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>") } ]
303
+ :half => hl["S>"],
304
+ :half_le => hl["S<"],
305
+ :half_be => hl["S>"],
306
+ :pghalf => pghl["S>"],
307
+ :pghalf_le => pghl["S<"],
308
+ :pghalf_be => pghl["S>"]
261
309
  } )
262
310
  DATA_ENDIAN[false].merge!( {
263
311
  :c => l["c"],
@@ -290,19 +338,12 @@ module LibBin
290
338
  :D => l["E"],
291
339
  :E => l["E"],
292
340
  :G => l["G"],
293
- :"a*" => l["a*"],
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>") } ]
341
+ :half => hl["S<"],
342
+ :half_le => hl["S<"],
343
+ :half_be => hl["S>"],
344
+ :pghalf => pghl["S<"],
345
+ :pghalf_le => pghl["S<"],
346
+ :pghalf_be => pghl["S>"]
306
347
  } )
307
348
 
308
349
 
@@ -312,8 +353,9 @@ module LibBin
312
353
  @size
313
354
  end
314
355
 
315
- def self.shape(value, previous_offset = 0, _ = nil, _ = nil, kind = DataShape)
316
- kind::new(previous_offset, previous_offset + @size - 1)
356
+ def self.shape(value, previous_offset = 0, _ = nil, _ = nil, kind = DataShape, length = nil)
357
+ length = 1 unless length
358
+ kind::new(previous_offset, previous_offset - 1 + length * @size)
317
359
  end
318
360
 
319
361
  def self.init(symbol)
@@ -323,20 +365,22 @@ module LibBin
323
365
  @rl_le, @sl_le = DATA_ENDIAN[false][symbol]
324
366
  end
325
367
 
326
- def self.load(input, input_big = LibBin::default_big?, _ = nil, _ = nil)
327
- str = input.read(@size)
328
- input_big ? @rl_be[str] : @rl_le[str]
368
+ def self.load(input, input_big = LibBin::default_big?, _ = nil, _ = nil, length = nil)
369
+ l = (length ? length : 1)
370
+ str = input.read(@size*l)
371
+ input_big ? @rl_be[str, length] : @rl_le[str, length]
329
372
  end
330
373
 
331
- def self.dump(value, output, output_big = LibBin::default_big?, _ = nil, _ = nil)
332
- str = (output_big ? @sl_be[value] : @sl_le[value])
374
+ def self.dump(value, output, output_big = LibBin::default_big?, _ = nil, _ = nil, length = nil)
375
+ str = (output_big ? @sl_be[value, length] : @sl_le[value, length])
333
376
  output.write(str)
334
377
  end
335
378
 
336
- def self.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, _ = nil, _ = nil)
337
- str = input.read(@size)
338
- value = (input_big ? @rl_be[str] : @rl_le[str])
339
- str = (output_big ? @sl_be[value] : @sl_le[value])
379
+ def self.convert(input, output, input_big = LibBin::default_big?, output_big = !input_big, _ = nil, _ = nil, length = nil)
380
+ l = (length ? length : 1)
381
+ str = input.read(@size*l)
382
+ value = (input_big ? @rl_be[str, length] : @rl_le[str, length])
383
+ str = (output_big ? @sl_be[value, length] : @sl_le[value, length])
340
384
  output.write(str)
341
385
  value
342
386
  end
@@ -345,30 +389,34 @@ module LibBin
345
389
 
346
390
  class Str < Scalar
347
391
 
348
- def self.load(input, input_big = LibBin::default_big?, _ = nil, _ = nil)
349
- str = (@size < 0 ? input.readline("\x00") : input.read(@size))
350
- input_big ? @rl_be[str] : @rl_le[str]
392
+ def self.size(value, previous_offset = 0, parent = nil, index = nil, length = nil)
393
+ length ? length : value.size
351
394
  end
352
395
 
353
- def self.convert(input, output, input_big = LibBin::default_big?, output_big = !LibBin::default_big, _ = nil, _ = nil)
354
- str = (@size < 0 ? input.readline("\x00") : input.read(@size))
355
- value = (input_big ? @rl_be[str] : @rl_le[str])
356
- str = (output_big ? @sl_be[value] : @sl_le[value])
396
+ def self.load(input, input_big = LibBin::default_big?, _ = nil, _ = nil, length = nil)
397
+ str = (length ? input.read(length) : input.readline("\x00"))
398
+ end
399
+
400
+ def self.convert(input, output, input_big = LibBin::default_big?, output_big = !LibBin::default_big, _ = nil, _ = nil, length = nil)
401
+ str = (length ? input.read(length) : input.readline("\x00"))
357
402
  output.write(str)
358
- value
403
+ str
359
404
  end
360
405
 
361
- def self.shape(value, previous_offset = 0, _ = nil, _ = nil, kind = DataShape)
362
- if @size < 0
363
- kind::new(previous_offset, previous_offset + value.size - 1)
406
+ def self.shape(value, previous_offset = 0, _ = nil, _ = nil, kind = DataShape, length = nil)
407
+ if length
408
+ kind::new(previous_offset, previous_offset + length - 1)
364
409
  else
365
- kind::new(previous_offset, previous_offset + @size - 1)
410
+ kind::new(previous_offset, previous_offset + value.size - 1)
366
411
  end
367
412
  end
368
413
 
414
+ def self.dump(value, output, output_big = LibBin::default_big?, _ = nil, _ = nil, length = nil)
415
+ output.write(value)
416
+ end
369
417
  end
370
418
 
371
- def self.register_field(field, type, count: nil, offset: nil, sequence: false, condition: nil, relative_offset: false)
419
+ def self.register_field(field, type, length: nil, count: nil, offset: nil, sequence: false, condition: nil, relative_offset: false)
372
420
  if type.kind_of?(Symbol)
373
421
  if type[0] == 'a'
374
422
  real_type = Class::new(Str) do init(sym) end
@@ -378,7 +426,7 @@ module LibBin
378
426
  else
379
427
  real_type = type
380
428
  end
381
- @fields.push(Field::new(field, real_type, count, offset, sequence, condition, relative_offset))
429
+ @fields.push(Field::new(field, real_type, length, count, offset, sequence, condition, relative_offset))
382
430
  attr_accessor field
383
431
  end
384
432
 
@@ -389,8 +437,8 @@ module LibBin
389
437
  init(#{symbol.inspect})
390
438
  end
391
439
 
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))
440
+ def self.#{name}(field, length: nil, count: nil, offset: nil, sequence: false, condition: nil, relative_offset: false)
441
+ @fields.push(Field::new(field, #{klassname}, length, count, offset, sequence, condition, relative_offset))
394
442
  attr_accessor field
395
443
  end
396
444
  EOF
@@ -430,15 +478,14 @@ EOF
430
478
  create_scalar_type(:pghalf_be)
431
479
 
432
480
  def self.string( field, length = nil, count: nil, offset: nil, sequence: false, condition: nil, relative_offset: false)
433
- sym = (length ? :"a#{length}" : :"a*")
481
+ sym = (length ? :"a" : :"a*")
434
482
  c = Class::new(Str) do
435
483
  init(sym)
436
484
  end
437
- @fields.push(Field::new(field, c, count, offset, sequence, condition, relative_offset))
485
+ @fields.push(Field::new(field, c, length, count, offset, sequence, condition, relative_offset))
438
486
  attr_accessor field
439
487
  end
440
488
 
441
-
442
489
  end
443
490
 
444
491
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'libbin'
3
- s.version = "1.0.4"
3
+ s.version = "1.0.5"
4
4
  s.author = "Brice Videau"
5
5
  s.email = "brice.videau@imag.fr"
6
6
  s.homepage = "https://github.com/kerilk/libbin"
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.5
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: 2019-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: float-formats