bitstream 0.1.0 → 0.1.1

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.
data/README.en CHANGED
@@ -72,4 +72,4 @@ In preparation.
72
72
  == License
73
73
 
74
74
  This library is distributed under the dual license of the Ruby license (in the narrow sense) and the 2-clause BSD license. Please see http://www.ruby-lang.org and BSDL.
75
- Copyright (c) 2011, Natsuki Kawai.
75
+ Copyright (c) 2011, 2012 Natsuki Kawai.
data/lib/bitstream.rb CHANGED
@@ -11,6 +11,7 @@ require 'types/cstring'
11
11
  require 'types/character'
12
12
 
13
13
  require 'bitstream/utils'
14
+ require 'bitstream/field-info'
14
15
 
15
16
  module BitStream
16
17
 
@@ -104,7 +105,7 @@ module BitStream
104
105
 
105
106
  def read(s, offset)
106
107
  instance = @type.create_with_offset(s, offset, @props)
107
- [instance, instance.length]
108
+ return FieldInfo.new(instance, instance.length)
108
109
  end
109
110
 
110
111
  #def write(s, offset, data)
@@ -166,8 +167,6 @@ module BitStream
166
167
  def initialize(type, instance)
167
168
  @type = type
168
169
  @instance = instance
169
- @length = @type.length if @type.respond_to? :length
170
- @has_read = false
171
170
  end
172
171
 
173
172
  def props
@@ -175,34 +174,32 @@ module BitStream
175
174
  end
176
175
  private :props
177
176
 
178
- def has_read?
179
- @has_read
180
- end
181
-
182
177
  def read
183
- unless @has_read
178
+ if @info.nil?
184
179
  if @offset.nil?
185
180
  index
186
181
  end
187
- @value, @length = @type.read(props.raw_data, @offset)
188
- @has_read = true
182
+ @info = @type.read(props.raw_data, @offset)
189
183
  end
190
- return @value
184
+ return @info[:value]
191
185
  end
192
186
 
193
187
  alias :value :read
194
188
 
195
189
  def length
190
+ length = @type.length
196
191
  # @length must not be nil if @has_read.
197
- if @length.nil?
198
- if @offset.nil?
199
- index
200
- else
201
- @value, @length = @type.read(props.raw_data, @offset)
202
- @has_read = true
192
+ if length.nil?
193
+ if @info.nil?
194
+ if @offset.nil?
195
+ index
196
+ end
197
+ @info = @type.read(props.raw_data, @offset)
203
198
  end
199
+ return @info[:length]
200
+ else
201
+ return length
204
202
  end
205
- return @length
206
203
  end
207
204
 
208
205
  def index
@@ -233,27 +230,6 @@ module BitStream
233
230
  @bitstream_mutex = Mutex.new
234
231
  end
235
232
 
236
- # Currently BitStream does not support inheritance.
237
- if false
238
- def on_inherit(types, chain, fields, mutex)
239
- @field_defs = []
240
- @fields = fields
241
- @types = types
242
- @index = 0
243
- @class_props = {}
244
- @class_props_chain = [@class_props]
245
- @class_props_chain.concat(chain)
246
- @bitstream_mutex = mutex
247
- end
248
-
249
- def inherited(subclass)
250
- subclass.on_inherit(@types, @class_props_chain, @fields, @bitstream_mutex)
251
- def subclass.fields
252
- raise NameError, "Cannot define fields on a subclass of a class includes BitStream."
253
- end
254
- end
255
- end
256
-
257
233
  def fields(&field_def)
258
234
  @field_defs << field_def
259
235
  end
@@ -324,9 +300,9 @@ module BitStream
324
300
  @singleton_props[:substream_types] = types
325
301
  end
326
302
 
327
- def self.add_type(type, name = nil, bs = self)
303
+ def self.add_type(type, method_name = nil, bs = self)
328
304
  bs.instance_eval do
329
- define_method(name) do |*args|
305
+ define_method(method_name) do |*args|
330
306
  name = args.shift.intern
331
307
  #if respond_to? name
332
308
  # raise "#{name} has already defined."
@@ -339,11 +315,7 @@ module BitStream
339
315
 
340
316
  case props.mode
341
317
  when :field_def
342
- if type.respond_to? :read
343
- type_instance = type
344
- else
345
- type_instance = type.instance(user_props, *args)
346
- end
318
+ type_instance = get_type(method_name, user_props, *args)
347
319
  field = FieldReader.new(type_instance, @instance)
348
320
  queue.enq(field)
349
321
 
@@ -368,24 +340,11 @@ module BitStream
368
340
 
369
341
  def array(name, size, type_name, *type_args)
370
342
  name = name.intern
371
- type_name = type_name.intern
372
- type = @types[type_name]
373
343
  props = @instance.bitstream_properties
374
344
  queue = props.eval_queue
375
345
  user_props = props.user_props
376
346
 
377
- if type.nil?
378
- raise BitStreamError, "There is no type named \"#{type_name}\""
379
- end
380
-
381
- if type.respond_to? :read
382
- unless type_args.empty?
383
- raise BitStreamError, "#{type} does not accept any arguments."
384
- end
385
- type_instance = type
386
- else
387
- type_instance = type.instance(user_props, *type_args)
388
- end
347
+ type_instance = get_type(type_name, user_props, *type_args)
389
348
 
390
349
  case props.mode
391
350
  when :field_def
@@ -425,25 +384,12 @@ module BitStream
425
384
 
426
385
  def dyn_array(name, type_name, *type_args)
427
386
  name = name.intern
428
- type_name = type_name.intern
429
- type = @types[type_name]
430
387
  props = @instance.bitstream_properties
431
388
  fields = props.fields
432
389
  queue = props.eval_queue
433
390
  user_props = props.user_props
434
391
 
435
- if type.nil?
436
- raise BitStreamError, "There is no type named \"#{type_name}\""
437
- end
438
-
439
- if type.respond_to? :read
440
- unless type_args.empty?
441
- raise BitStreamError, "#{type} does not accept any arguments."
442
- end
443
- type_instance = type
444
- else
445
- type_instance = type.instance(user_props, *type_args)
446
- end
392
+ type_instance = get_type(type_name, user_props, *type_args)
447
393
 
448
394
  case props.mode
449
395
  when :field_def
@@ -527,16 +473,13 @@ module BitStream
527
473
  end
528
474
 
529
475
  def method_missing(name, *args)
530
- name_s = name.to_s
531
476
  field_name = args.shift
532
- if name_s =~ /^uint(\d+)$/
533
- bit_width = Regexp.last_match[1].to_i
534
- unsigned field_name, bit_width, *args
535
- elsif name_s =~ /^int(\d+)$/
536
- bit_width = Regexp.last_match[1].to_i
537
- signed field_name, bit_width, *args
477
+ aliasee = resolve_alias(name, args)
478
+ args.unshift(field_name)
479
+ if aliasee.nil?
480
+ super name, *args
538
481
  else
539
- super name, args
482
+ return send(aliasee, *args)
540
483
  end
541
484
  end
542
485
 
@@ -544,6 +487,37 @@ module BitStream
544
487
  NestWrapper.new(self, inherited_props, user_props)
545
488
  end
546
489
 
490
+ private
491
+ def resolve_alias(type_name, args)
492
+ type_s = type_name.to_s
493
+ if type_s =~ /^uint(\d+)$/
494
+ bit_width = Regexp.last_match[1].to_i
495
+ args.unshift(bit_width)
496
+ return :unsigned
497
+ elsif type_s =~ /^int(\d+)$/
498
+ bit_width = Regexp.last_match[1].to_i
499
+ args.unshift(bit_width)
500
+ return :signed
501
+ else
502
+ return nil
503
+ end
504
+ end
505
+
506
+ def get_type(type_name, props, *args)
507
+ type = @types[type_name.intern]
508
+
509
+ if type.nil?
510
+ aliasee = resolve_alias(type_name, args)
511
+ if aliasee.nil?
512
+ raise BitStreamError, "There is no type named \"#{type_name}\""
513
+ else
514
+ type = @types[aliasee]
515
+ end
516
+ end
517
+
518
+ return type.instance(props, *args)
519
+ end
520
+
547
521
  end
548
522
 
549
523
  def self.included(obj)
@@ -579,6 +553,10 @@ module BitStream
579
553
  def substreams
580
554
  @bitstream_properties.substreams.values
581
555
  end
556
+
557
+ #def field_info(name)
558
+ # @bitstream_properties.field_info[]
559
+ #end
582
560
 
583
561
  #def properties=(props)
584
562
  # Method to override.
@@ -1,11 +1,14 @@
1
1
  # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
2
+ # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
3
  # License:: 2-clause BSDL or Ruby's
4
4
 
5
+ require 'bitstream/field-info'
5
6
 
6
7
  module BitStream
7
8
 
8
9
  class Char
10
+
11
+ LENGTH = 8
9
12
 
10
13
  @instance = new
11
14
 
@@ -14,7 +17,7 @@ module BitStream
14
17
  end
15
18
 
16
19
  def length
17
- 8
20
+ LENGTH
18
21
  end
19
22
 
20
23
  def read(s, offset)
@@ -23,7 +26,7 @@ module BitStream
23
26
 
24
27
  value = s[byteoffset]
25
28
 
26
- return [value, 8]
29
+ return FieldInfo.new(value, LENGTH)
27
30
  end
28
31
 
29
32
  def write(s, offset, value)
data/lib/types/cstring.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
2
+ # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
3
  # License:: 2-clause BSDL or Ruby's
4
4
 
5
+ require 'bitstream/field-info'
5
6
 
6
7
  module BitStream
7
8
 
@@ -29,7 +30,7 @@ module BitStream
29
30
 
30
31
  bytelen = val.size
31
32
  val.slice!(val.size - 1)
32
- return [val, 8 * bytelen]
33
+ return FieldInfo.new(val, 8 * bytelen)
33
34
  end
34
35
 
35
36
  def write(s, offset, data)
data/lib/types/integer.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
2
+ # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
3
  # License:: 2-clause BSDL or Ruby's
4
4
 
5
+ require 'bitstream/field-info'
5
6
 
6
7
  module BitStream
7
8
 
@@ -59,7 +60,7 @@ module BitStream
59
60
  value &= ~(-1 << (bytelength * 8 - bitoffset))
60
61
  value >>= (8 - (@bit_width + bitoffset) % 8) % 8
61
62
 
62
- return [value, @bit_width]
63
+ return FieldInfo.new(value, @bit_width)
63
64
  end
64
65
 
65
66
  def write(s, offset, value)
@@ -112,12 +113,14 @@ module BitStream
112
113
  end
113
114
 
114
115
  def read(s, offset)
115
- val, len = @unsigned.read(s, offset)
116
+ info = @unsigned.read(s, offset)
117
+ val = info[:value]
118
+ len = info[:length]
116
119
  mask = -1 << (len - 1)
117
120
  if (val & mask) != 0
118
121
  val |= mask
119
122
  end
120
- return [val, len]
123
+ return FieldInfo.new(val, len)
121
124
  end
122
125
 
123
126
  def write(s, offset, value)
data/lib/types/string.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
2
+ # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
3
  # License:: 2-clause BSDL or Ruby's
4
4
 
5
5
 
6
+ require 'bitstream/field-info'
6
7
  require 'types/string-utils'
7
8
 
8
9
  module BitStream
@@ -29,7 +30,7 @@ module BitStream
29
30
  Utils.bit_lshift(val, bitoffset)
30
31
  val.slice!(val.size - 1) if bitoffset != 0
31
32
 
32
- [val, @byte_len * 8]
33
+ return FieldInfo.new(val, @byte_len * 8)
33
34
  end
34
35
 
35
36
  def write(s, offset, data)
data/test/test-array.rb CHANGED
@@ -21,7 +21,7 @@ class InfiniteArraySample
21
21
  include BitStream
22
22
 
23
23
  fields do
24
- array :a1, nil, :unsigned_int, 16
24
+ array :a1, nil, :uint16
25
25
  end
26
26
 
27
27
  end
@@ -1,5 +1,5 @@
1
1
  # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
2
+ # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
3
  # License:: 2-clause BSDL or Ruby's
4
4
 
5
5
 
@@ -10,9 +10,9 @@ class TestUint < Test::Unit::TestCase
10
10
 
11
11
  def test_char_nooffset_read
12
12
  type = BitStream::Char.instance({})
13
- val, len = type.read("abcd", 8)
14
- assert_equal("b", val)
15
- assert_equal(8, len)
13
+ info = type.read("abcd", 8)
14
+ assert_equal("b", info[:value])
15
+ assert_equal(8, info[:length])
16
16
  end
17
17
 
18
18
  end
@@ -1,5 +1,5 @@
1
1
  # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
2
+ # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
3
  # License:: 2-clause BSDL or Ruby's
4
4
 
5
5
 
@@ -10,9 +10,9 @@ class TestCstring < Test::Unit::TestCase
10
10
 
11
11
  def test_aligned_read
12
12
  type = BitStream::Cstring.instance({})
13
- val, len = type.read("foobar\0baz", 16)
14
- assert_equal("obar", val)
15
- assert_equal(8 * "obar\0".size, len)
13
+ info = type.read("foobar\0baz", 16)
14
+ assert_equal("obar", info[:value])
15
+ assert_equal(8 * "obar\0".size, info[:length])
16
16
  end
17
17
 
18
18
  end
@@ -1,5 +1,5 @@
1
1
  # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
2
+ # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
3
  # License:: 2-clause BSDL or Ruby's
4
4
 
5
5
 
@@ -13,30 +13,30 @@ class TestUint < Test::Unit::TestCase
13
13
 
14
14
  def test_uint32be_nooffset_read
15
15
  type = BitStream::Unsigned.instance(BE_PROP,32)
16
- val, len = type.read("\x01\x02\x03\x04", 0)
17
- assert_equal(0x01020304, val)
18
- assert_equal(32, len)
16
+ info = type.read("\x01\x02\x03\x04", 0)
17
+ assert_equal(0x01020304, info[:value])
18
+ assert_equal(32, info[:length])
19
19
  end
20
20
 
21
21
  def test_uint32le_nooffset_read
22
22
  type = BitStream::Unsigned.instance(LE_PROP,32)
23
- val, len = type.read("\x01\x02\x03\x04", 0)
24
- assert_equal(0x04030201, val)
25
- assert_equal(32, len)
23
+ info = type.read("\x01\x02\x03\x04", 0)
24
+ assert_equal(0x04030201, info[:value])
25
+ assert_equal(32, info[:length])
26
26
  end
27
27
 
28
28
  def test_sint32be_nooffset_read
29
29
  type = BitStream::Signed.instance(BE_PROP,32)
30
- val, len = type.read("\xfe\xfd\xfc\xfb", 0)
31
- assert_equal(-0x01020304 - 1, val)
32
- assert_equal(32, len)
30
+ info = type.read("\xfe\xfd\xfc\xfb", 0)
31
+ assert_equal(-0x01020304 - 1, info[:value])
32
+ assert_equal(32, info[:length])
33
33
  end
34
34
 
35
35
  def test_sint32le_nooffset_read
36
36
  type = BitStream::Signed.instance(LE_PROP,32)
37
- val, len = type.read("\xfe\xfd\xfc\xfb", 0)
38
- assert_equal(-0x04030201 - 1, val)
39
- assert_equal(32, len)
37
+ info = type.read("\xfe\xfd\xfc\xfb", 0)
38
+ assert_equal(-0x04030201 - 1, info[:value])
39
+ assert_equal(32, info[:length])
40
40
  end
41
41
 
42
42
  def test_uint32_nooffset_write
@@ -48,51 +48,51 @@ class TestUint < Test::Unit::TestCase
48
48
 
49
49
  def test_uint32be_offset4_read
50
50
  type = BitStream::Unsigned.instance(BE_PROP, 32)
51
- val, len = type.read("\xf1\x02\x03\x04\x05", 4)
52
- assert_equal(0x10203040.to_s(16), val.to_s(16))
53
- assert_equal(32, len)
51
+ info = type.read("\xf1\x02\x03\x04\x05", 4)
52
+ assert_equal(0x10203040.to_s(16), info[:value].to_s(16))
53
+ assert_equal(32, info[:length])
54
54
  end
55
55
 
56
56
  def test_uint32be_offset1_read
57
57
  type = BitStream::Unsigned.instance(BE_PROP, 32)
58
- val, len = type.read("\x12\x23\x34\x45\x56", 1)
59
- assert_equal(0x2446688a.to_s(16), val.to_s(16))
60
- assert_equal(32, len)
58
+ info = type.read("\x12\x23\x34\x45\x56", 1)
59
+ assert_equal(0x2446688a.to_s(16), info[:value].to_s(16))
60
+ assert_equal(32, info[:length])
61
61
  end
62
62
 
63
63
  def test_uint32le_offset4_read
64
64
  type = BitStream::Unsigned.instance(LE_PROP, 32)
65
- val, len = type.read("\xf1\x02\x03\x04\x05", 4)
66
- assert_equal(0x5040302f.to_s(16), val.to_s(16))
67
- assert_equal(32, len)
65
+ info = type.read("\xf1\x02\x03\x04\x05", 4)
66
+ assert_equal(0x5040302f.to_s(16), info[:value].to_s(16))
67
+ assert_equal(32, info[:length])
68
68
  end
69
69
 
70
70
  def test_uint32le_offset1_read
71
71
  type = BitStream::Unsigned.instance(LE_PROP, 32)
72
- val, len = type.read("\x80\x23\x34\x45\x56", 1)
73
- assert_equal(0xac8a6847.to_s(16), val.to_s(16))
74
- assert_equal(32, len)
72
+ info = type.read("\x80\x23\x34\x45\x56", 1)
73
+ assert_equal(0xac8a6847.to_s(16), info[:value].to_s(16))
74
+ assert_equal(32, info[:length])
75
75
  end
76
76
 
77
77
  def test_uint1be_read
78
78
  type = BitStream::Unsigned.instance(BE_PROP, 1)
79
- val, len = type.read("\x40", 1)
80
- assert_equal(1, val)
81
- assert_equal(1, len)
79
+ info = type.read("\x40", 1)
80
+ assert_equal(1, info[:value])
81
+ assert_equal(1, info[:length])
82
82
 
83
- val, len = type.read("\xfd", 6)
84
- assert_equal(0, val)
85
- assert_equal(1, len)
83
+ info = type.read("\xfd", 6)
84
+ assert_equal(0, info[:value])
85
+ assert_equal(1, info[:length])
86
86
  end
87
87
 
88
88
  def test_uint1le_read
89
89
  type = BitStream::Unsigned.instance(LE_PROP, 1)
90
- val, len = type.read("\x40", 1)
91
- assert_equal(1, val)
92
- assert_equal(1, len)
90
+ info = type.read("\x40", 1)
91
+ assert_equal(1, info[:value])
92
+ assert_equal(1, info[:length])
93
93
 
94
- val, len = type.read("\xfd", 6)
95
- assert_equal(0, val)
96
- assert_equal(1, len)
94
+ info = type.read("\xfd", 6)
95
+ assert_equal(0, info[:value])
96
+ assert_equal(1, info[:length])
97
97
  end
98
98
  end
@@ -1,5 +1,5 @@
1
1
  # Author:: Natsuki Kawai (natsuki.kawai@gmail.com)
2
- # Copyright:: Copyright 2011 Natsuki Kawai
2
+ # Copyright:: Copyright 2011, 2012 Natsuki Kawai
3
3
  # License:: 2-clause BSDL or Ruby's
4
4
 
5
5
 
@@ -10,9 +10,9 @@ class TestString < Test::Unit::TestCase
10
10
 
11
11
  def test_aligned_read
12
12
  type = BitStream::String.instance({}, 3)
13
- val, len = type.read("foobarbaz", 16)
14
- assert_equal("oba", val)
15
- assert_equal(3 * 8, len)
13
+ info = type.read("foobarbaz", 16)
14
+ assert_equal("oba", info[:value])
15
+ assert_equal(3 * 8, info[:length])
16
16
  end
17
17
 
18
18
  def test_aligned_write
@@ -24,9 +24,9 @@ class TestString < Test::Unit::TestCase
24
24
 
25
25
  def test_unaligned_read
26
26
  type = BitStream::String.instance({}, 2)
27
- val, len = type.read([0x12345678].pack('N'), 1)
28
- assert_equal("\x24\x68", val)
29
- assert_equal(2 * 8, len)
27
+ info = type.read([0x12345678].pack('N'), 1)
28
+ assert_equal("\x24\x68", info[:value])
29
+ assert_equal(2 * 8, info[:length])
30
30
  end
31
31
 
32
32
  def test_unaligned_write
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Natsuki Kawai
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-02-12 00:00:00 +09:00
17
+ date: 2012-02-14 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency