bitstream 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bitstream.rb CHANGED
@@ -280,12 +280,14 @@ module BitStream
280
280
 
281
281
  name_in_method = name
282
282
 
283
- define_method name do
284
- field = bitstream_properties.fields[name_in_method]
285
- if field.value.nil?
286
- BitStream.read_one_field(field, self)
283
+ @instance.singleton_class.instance_eval do
284
+ define_method name do
285
+ field = bitstream_properties.fields[name_in_method]
286
+ if field.value.nil?
287
+ BitStream.read_one_field(field, self)
288
+ end
289
+ field.value
287
290
  end
288
- field.value
289
291
  end
290
292
 
291
293
  instance = @instance
@@ -329,8 +331,10 @@ module BitStream
329
331
  queue.enq(field_element)
330
332
  end
331
333
 
332
- define_method name do
333
- field
334
+ @instance.singleton_class.instance_eval do
335
+ define_method name do
336
+ field
337
+ end
334
338
  end
335
339
 
336
340
  name_in_method = name
@@ -376,8 +380,10 @@ module BitStream
376
380
 
377
381
  name_in_method = name
378
382
 
379
- define_method name do
380
- return fields[name_in_method]
383
+ @instance.singleton_class.instance_eval do
384
+ define_method name do
385
+ return fields[name_in_method]
386
+ end
381
387
  end
382
388
 
383
389
  instance = @instance
@@ -0,0 +1,9 @@
1
+ module BitStream
2
+
3
+ module SingleType
4
+ def instance
5
+
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,26 @@
1
+ module BitStream
2
+
3
+ module Utils
4
+
5
+ def self.align(s, offset, length)
6
+ index = offset
7
+ bitoffset = offset % 8
8
+ aligned = nil
9
+ if bitoffset != 0
10
+ aligned = ""
11
+ while index < offset + length
12
+ i = index / 8
13
+ c = s[i..(i + 1)].unpack('n')[0] >> (8 - bitoffset)
14
+ aligned << [c].pack('C')
15
+ index += 8
16
+ end
17
+ else
18
+ aligned = s[(offset / 8)...((offset + length) / 8)]
19
+ end
20
+ return aligned
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
data/sample/flac.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'bitstream'
2
+
3
+ class FLAC
4
+
5
+ include BitStream
6
+
7
+ class MetadataBlock
8
+
9
+ include BitStream
10
+
11
+ #add_type MetadataBlockData
12
+
13
+ fields do
14
+ unsigned_int :last_metadata_block, 1
15
+ unsigned_int :block_type, 7
16
+ unsigned_int :body_length, 24
17
+ string :data, body_length
18
+ end
19
+
20
+ def length
21
+ 32 + body_length
22
+ end
23
+
24
+ end
25
+
26
+ add_type MetadataBlock
27
+
28
+ fields do
29
+ string :magic, 4#, "The FLAC stream marker"
30
+ dyn_array :metadata_blocks, :metadata_block
31
+ unless metadata_blocks.last.last_metadata_block
32
+ dyn_array :metadata_blocks, :metadata_block
33
+ #, "The basic property of the stream."
34
+ end
35
+ end
36
+
37
+ end
data/sample/front.rb ADDED
@@ -0,0 +1,8 @@
1
+ require './flac'
2
+
3
+ if ARGV.size != 1
4
+ STDERR.puts "Usage: #{__FILE__} flac_file"
5
+ exit 1
6
+ end
7
+
8
+ flac = FLAC.create(ARGV[0])
@@ -0,0 +1,36 @@
1
+ require 'test/unit'
2
+ require 'types/utils'
3
+
4
+ class TestUtils < Test::Unit::TestCase
5
+
6
+ def test_align_aligned_bytelen
7
+ source = "abcde"
8
+ aligned = BitStream::Utils.align(source, 8, 8 * 3)
9
+ assert_equal("bcd", aligned)
10
+ end
11
+
12
+ def test_align_aligned_bytelen_lastbyte
13
+ source = "abcde"
14
+ aligned = BitStream::Utils.align(source, 8 * 2, 8 * 3)
15
+ assert_equal("cde", aligned)
16
+ end
17
+
18
+ def test_align_aligned
19
+ source = "defghi"
20
+ aligned = BitStream::Utils.align(source, 8 * 2, 8 * 2 + 6)
21
+ assert_equal(3, aligned.size)
22
+ aligned = aligned.unpack("a2C")
23
+ assert_equal("fg", aligned[0])
24
+ assert_equal("h".ord & 0b11111100, aligned[1] & 0b11111100)
25
+ end
26
+
27
+ def test_align_aligned_lastbyte
28
+ source = "defghi"
29
+ aligned = BitStream::Utils.align(source, 8 * 3, 8 * 2 + 6)
30
+ assert_equal(3, aligned.size)
31
+ aligned = aligned.unpack("a2C")
32
+ assert_equal("gh", aligned[0])
33
+ assert_equal("i".ord & 0b11111100, aligned[1] & 0b11111100)
34
+ end
35
+
36
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitstream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-28 00:00:00.000000000 Z
12
+ date: 2012-01-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: random-accessible
16
- requirement: &80676780 !ruby/object:Gem::Requirement
16
+ requirement: &71448960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 0.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *80676780
24
+ version_requirements: *71448960
25
25
  description: ! 'BitStream is a mixin to write data structures of bit streams such
26
26
  as picture, music, movie files, and e.t.c.. You can refer contents of bit streams
27
27
  even when you are defining the data structures. With the function, you can write
@@ -35,25 +35,29 @@ extra_rdoc_files:
35
35
  - README.en
36
36
  files:
37
37
  - lib/bitstream.rb
38
- - lib/types/integer.rb
39
- - lib/types/cstring.rb
40
38
  - lib/types/string.rb
41
- - lib/types/string-utils.rb
39
+ - lib/types/utils.rb
42
40
  - lib/types/character.rb
41
+ - lib/types/string-utils.rb
42
+ - lib/types/integer.rb
43
+ - lib/types/cstring.rb
44
+ - lib/types/basetype.rb
45
+ - test/test-nesting.rb
43
46
  - test/test-dynarray.rb
47
+ - test/test-condition.rb
44
48
  - test/test-simple-properties.rb
45
49
  - test/test-suite.rb
46
- - test/test-primitives.rb
47
- - test/test-condition.rb
48
50
  - test/test-array.rb
49
- - test/test-nesting.rb
51
+ - test/test-primitives.rb
52
+ - test/types/test-utils.rb
50
53
  - test/types/test-character.rb
54
+ - test/types/test-cstring.rb
51
55
  - test/types/test-string.rb
52
56
  - test/types/test-integer.rb
53
- - test/types/test-cstring.rb
54
- - sample/gzip-viewer.rb
55
- - sample/tar-viewer.rb
56
57
  - sample/gzip.rb
58
+ - sample/flac.rb
59
+ - sample/gzip-viewer.rb
60
+ - sample/front.rb
57
61
  - README.en
58
62
  homepage: https://github.com/natsuki14/bitstream
59
63
  licenses:
@@ -77,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
81
  version: '0'
78
82
  requirements: []
79
83
  rubyforge_project:
80
- rubygems_version: 1.8.12
84
+ rubygems_version: 1.8.11
81
85
  signing_key:
82
86
  specification_version: 3
83
87
  summary: A bitstream parser supports dynamic-defined fields
data/sample/tar-viewer.rb DELETED
@@ -1,15 +0,0 @@
1
- require 'bitstream'
2
-
3
- class Tar
4
-
5
- include BitStream
6
- byte_order :big_endian
7
-
8
- fields do
9
- cstring :name
10
- string :padding1, 99 - file_name.size
11
- string :mode, 8
12
- string :uid, 8
13
- string :gid, 8
14
- string
15
- end