BitStructEx 0.0.54 → 0.0.64

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -33,8 +33,10 @@ end
33
33
  desc "Increment the build number and upate the Version file"
34
34
  task :increment_build_number => [ :run_tests ] do
35
35
  src = Dir.glob "#{SRC_DIR}/**/*"
36
+ test = Dir.glob "#{TEST_DIR}/**/*"
37
+ files = src + test
36
38
  target = 'Version'
37
- unless uptodate?( target, src )
39
+ unless uptodate?( target, files )
38
40
  new_version = PKG_VERSION.increment( :build )
39
41
  puts "Version: #{new_version}"
40
42
  end
data/Version CHANGED
@@ -1 +1 @@
1
- 0.0.54
1
+ 0.0.64
@@ -99,7 +99,7 @@ module BitStruct
99
99
  require "bit_struct/#{symbol.id2name}"
100
100
 
101
101
  class_name = make_class_name symbol
102
- clazz = const_get class_name.to_sym
102
+ clazz = eval "BitStruct::#{class_name.to_sym}"
103
103
  self.class.send :define_method, symbol do |*args|
104
104
  add_field clazz.new( *args )
105
105
  end
@@ -0,0 +1,28 @@
1
+
2
+ require 'bit_struct/field'
3
+
4
+ module BitStruct
5
+
6
+ class TextField < Field
7
+
8
+ def initialize( name, length, description = nil )
9
+ raise "Length must be multiple of 8" unless ( length % 8 ) == 0
10
+ super name, length, description
11
+ @length_in_bytes = length / 8
12
+ end
13
+
14
+ def read( slicer )
15
+ res = ''
16
+ slicer.get_bytes.reverse.each { |b| res << b }
17
+ res
18
+ end
19
+
20
+ def write( slicer, new_value )
21
+ bytes = []
22
+ new_value.each_byte { |b| bytes << b }
23
+ slicer.set_bytes bytes.reverse
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -20,6 +20,8 @@ class Entry < StructBase
20
20
  unsigned :cache_id, 16
21
21
  end
22
22
 
23
+
24
+
23
25
  class TestBitStruct < Test::Unit::TestCase
24
26
 
25
27
  def test_flags_class
@@ -0,0 +1,54 @@
1
+
2
+ require 'test/unit'
3
+
4
+ require 'bit_struct/text'
5
+
6
+ include BitStruct
7
+
8
+
9
+
10
+ class TestTextField < Test::Unit::TestCase
11
+
12
+ # Note: The TextField doesn't check the length. It is part of the
13
+ # contract between the field and the slicer that only the expected
14
+ # number of bytes (or less) are passed to the field :)
15
+
16
+ def test_initialize
17
+ TextField.new 'dummy', 8
18
+ TextField.new 'dummy', 16
19
+ TextField.new 'dummy', 24
20
+ assert_raise( RuntimeError ) { TextField.new 'dummy', 5 }
21
+ end
22
+
23
+ def test_read
24
+ slicer = FakeSlicer.new 'toast'.bytes
25
+ field = TextField.new 'dummy', 24
26
+ assert_equal 'tsaot', field.read( slicer )
27
+ end
28
+
29
+ def test_write
30
+ slicer = FakeSlicer.new
31
+ field = TextField.new 'dummy', 24
32
+ field.write slicer, 'toast'
33
+ assert_equal 'tsaot', slicer.get_bytes.stringify
34
+ end
35
+
36
+ end
37
+
38
+
39
+
40
+ class FakeSlicer
41
+
42
+ def initialize( data = '' )
43
+ @data = data
44
+ end
45
+
46
+ def get_bytes
47
+ @data
48
+ end
49
+
50
+ def set_bytes( data )
51
+ @data = data
52
+ end
53
+
54
+ end
@@ -0,0 +1,58 @@
1
+
2
+ require 'test/unit'
3
+
4
+ require 'bit_struct/struct_base'
5
+
6
+ include BitStruct
7
+
8
+
9
+
10
+ class TestTextStruct < Test::Unit::TestCase
11
+
12
+ class AlignedText < StructBase
13
+ unsigned :before, 8
14
+ text :aligned, 32
15
+ unsigned :after, 8
16
+ end
17
+
18
+ def test_aligned_text
19
+ data = [ 0x12, ?t, ?s, ?e, ?t, 0x34 ].pack( 'C*' )
20
+ struct = AlignedText.new data
21
+ assert_equal 0x34, struct.before
22
+ assert_equal 'test', struct.aligned
23
+ assert_equal 0x12, struct.after
24
+
25
+ struct.before = ?A
26
+ struct.aligned = 'goal'
27
+ struct.after = ?B
28
+ assert_equal ?A, struct.before
29
+ assert_equal 'goal', struct.aligned
30
+ assert_equal ?B, struct.after
31
+
32
+ assert_equal 'BlaogA', struct.data
33
+ end
34
+
35
+ class UnalignedText < StructBase
36
+ unsigned :before, 4
37
+ text :unaligned, 24
38
+ unsigned :after, 4
39
+ end
40
+
41
+ def test_unaligned_text
42
+ a_hi, a_lo = get_hi_lo ?a
43
+ b_hi, b_lo = get_hi_lo ?b
44
+ c_hi, c_lo = get_hi_lo ?c
45
+
46
+ data = [ 12 << 4 | c_hi, c_lo << 4 | b_hi, b_lo << 4 | a_hi, a_lo << 4 | 3 ].pack( 'C*' )
47
+
48
+ struct = UnalignedText.new data
49
+ assert_equal 3, struct.before
50
+ assert_equal 'abc', struct.unaligned
51
+ assert_equal 12, struct.after
52
+ end
53
+
54
+ def get_hi_lo( char )
55
+ return char >> 4, char & 0x0F
56
+ end
57
+
58
+ end
data/test/ruby/runner.rb CHANGED
@@ -5,6 +5,12 @@ require 'test/unit/autorunner'
5
5
 
6
6
  class String
7
7
 
8
+ def bytes
9
+ bytes = []
10
+ self.each_byte { |b| bytes << b }
11
+ bytes
12
+ end
13
+
8
14
  def hexify
9
15
  result = ''
10
16
  each_byte do |b|
@@ -17,6 +23,12 @@ end
17
23
 
18
24
  class Array
19
25
 
26
+ def stringify
27
+ res = ''
28
+ self.each { |b| res << b }
29
+ res
30
+ end
31
+
20
32
  def hexify
21
33
  map { |b| "0x#{b && b.hexify}" }.join( ' ' )
22
34
  end
metadata CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: BitStructEx
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.54
6
+ version: 0.0.64
7
7
  date: 2006-04-13 00:00:00 +02:00
8
8
  summary: Simple DSL for defining bit-based structures on byte data.
9
9
  require_paths:
@@ -33,10 +33,13 @@ files:
33
33
  - src/ruby/bit_struct/field.rb
34
34
  - src/ruby/bit_struct/nested.rb
35
35
  - src/ruby/bit_struct/struct_base.rb
36
+ - src/ruby/bit_struct/text.rb
36
37
  - src/ruby/bit_struct/unsigned.rb
37
38
  - test/ruby/runner.rb
38
39
  - test/ruby/bit_struct/test_bit_slicer.rb
39
40
  - test/ruby/bit_struct/test_struct_base.rb
41
+ - test/ruby/bit_struct/test_text.rb
42
+ - test/ruby/bit_struct/test_text_struct.rb
40
43
  - Rakefile
41
44
  - Version
42
45
  test_files: []