BitStructEx 0.0.65 → 0.0.74
Sign up to get free protection for your applications and to get access to all the features.
- data/src/{ruby/bit_struct → bit_struct}/bit_slicer.rb +8 -25
- data/src/bit_struct/byte_data.rb +56 -0
- data/src/{ruby/bit_struct → bit_struct}/nested.rb +3 -1
- data/src/{ruby/bit_struct → bit_struct}/struct_base.rb +35 -21
- metadata +13 -10
- /data/src/{ruby/bit_struct → bit_struct}/field.rb +0 -0
- /data/src/{ruby/bit_struct → bit_struct}/text.rb +0 -0
- /data/src/{ruby/bit_struct → bit_struct}/unsigned.rb +0 -0
@@ -2,36 +2,21 @@
|
|
2
2
|
module BitStruct
|
3
3
|
|
4
4
|
# Implements access to a 'segment' of a byte string, specified by
|
5
|
-
# offset (from bit 0) and length (in bits).
|
6
|
-
# endian data input and output.
|
5
|
+
# offset (from bit 0) and length (in bits).
|
7
6
|
#
|
8
7
|
# NOTE: This is highly inefficient. If a segment is byte-aligned,
|
9
8
|
# please use the optimized ByteSlicer class.
|
9
|
+
#
|
10
|
+
# NOTE: Oops! Haven't implemented the ByteSlicer, yet! :)
|
11
|
+
#
|
10
12
|
class BitSlicer
|
11
13
|
|
12
|
-
|
13
|
-
LITTLE_ENDIAN = LITTLE = INTEL = 0
|
14
|
-
BIG_ENDIAN = BIG = MOTOROLA = 1
|
15
|
-
NETWORK_BYTE_ORDER = BIG_ENDIAN
|
16
|
-
end
|
14
|
+
attr_accessor :data
|
17
15
|
|
18
|
-
|
19
|
-
|
20
|
-
def initialize( bit_offset, bit_length, endianess = MOTOROLA )
|
16
|
+
def initialize( bit_offset, bit_length )
|
21
17
|
raise ArgumentError if bit_offset < 0 || bit_length < 0
|
22
|
-
|
23
18
|
@bit_offset = bit_offset
|
24
19
|
@bit_length = bit_length
|
25
|
-
@endianess = endianess
|
26
|
-
end
|
27
|
-
|
28
|
-
def data=( new_data )
|
29
|
-
raise ArgumentError if ( @bit_offset + @bit_length ) > new_data.length * 8
|
30
|
-
@data = new_data
|
31
|
-
end
|
32
|
-
|
33
|
-
def data
|
34
|
-
@data
|
35
20
|
end
|
36
21
|
|
37
22
|
def get_bytes
|
@@ -88,13 +73,11 @@ module BitStruct
|
|
88
73
|
#private
|
89
74
|
|
90
75
|
def get_data( idx )
|
91
|
-
|
92
|
-
return @data[ -idx ] if @endianess == INTEL
|
76
|
+
@data[ idx ]
|
93
77
|
end
|
94
78
|
|
95
79
|
def set_data( idx, val )
|
96
|
-
@data[ idx ] = val
|
97
|
-
@data[ -idx ] = val if @endianess == INTEL
|
80
|
+
@data[ idx ] = val
|
98
81
|
end
|
99
82
|
|
100
83
|
def first_byte
|
@@ -0,0 +1,56 @@
|
|
1
|
+
|
2
|
+
module BitStruct
|
3
|
+
|
4
|
+
class ByteData
|
5
|
+
|
6
|
+
LITTLE_ENDIAN = LITTLE = INTEL = 0
|
7
|
+
BIG_ENDIAN = BIG = MOTOROLA = 1
|
8
|
+
NETWORK_BYTE_ORDER = BIG_ENDIAN
|
9
|
+
|
10
|
+
def initialize( data, endianess = nil )
|
11
|
+
raise "Bad input" unless data.respond_to? "[]".to_sym
|
12
|
+
raise "Unknown endianess" unless valid endianess
|
13
|
+
@data = data
|
14
|
+
@endianess = endianess || BIG_ENDIAN
|
15
|
+
end
|
16
|
+
|
17
|
+
def size
|
18
|
+
@data.size
|
19
|
+
end
|
20
|
+
|
21
|
+
def length
|
22
|
+
@data.length
|
23
|
+
end
|
24
|
+
|
25
|
+
def each
|
26
|
+
0.upto( size - 1 ) { |idx| yield self[ idx ] }
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
str = ''
|
31
|
+
each { |b| str << b }
|
32
|
+
str
|
33
|
+
end
|
34
|
+
|
35
|
+
def []( idx )
|
36
|
+
@data[ endianized( idx ) ]
|
37
|
+
end
|
38
|
+
|
39
|
+
def []=( idx, val )
|
40
|
+
@data[ endianized( idx ) ] = val
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def valid( endianess )
|
46
|
+
endianess == nil || endianess == LITTLE_ENDIAN || endianess == BIG_ENDIAN
|
47
|
+
end
|
48
|
+
|
49
|
+
def endianized( idx )
|
50
|
+
return idx if @endianess == BIG_ENDIAN
|
51
|
+
return @data.length - idx - 1 if @endianess == LITTLE_ENDIAN
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -8,6 +8,7 @@ module BitStruct
|
|
8
8
|
def initialize( name, nested_class, description = nil )
|
9
9
|
super name, nil, description
|
10
10
|
@nested_class = nested_class
|
11
|
+
@nested = nested_class.new
|
11
12
|
end
|
12
13
|
|
13
14
|
def length
|
@@ -16,7 +17,8 @@ module BitStruct
|
|
16
17
|
|
17
18
|
def read( slicer )
|
18
19
|
bytes = slicer.get_bytes
|
19
|
-
@
|
20
|
+
@nested.set_data bytes
|
21
|
+
@nested
|
20
22
|
end
|
21
23
|
|
22
24
|
def write( slicer, new_value )
|
@@ -1,5 +1,6 @@
|
|
1
1
|
|
2
2
|
require 'bit_struct/bit_slicer'
|
3
|
+
require 'bit_struct/byte_data'
|
3
4
|
require 'bit_struct/field'
|
4
5
|
|
5
6
|
# The BitStruct module primarily provides the StructBase class, which in turn
|
@@ -8,27 +9,28 @@ require 'bit_struct/field'
|
|
8
9
|
#
|
9
10
|
# Example:
|
10
11
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
12
|
+
# class Flags < StructBase
|
13
|
+
# unsigned :direction, 4
|
14
|
+
# unsigned :multiplier, 2
|
15
|
+
# unsigned :offset, 2
|
16
|
+
# end
|
16
17
|
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
18
|
+
# class Entry < StructBase
|
19
|
+
# unsigned :offset, 4
|
20
|
+
# nested :flags, Flags
|
21
|
+
# unsigned :address, 24
|
22
|
+
# unsigned :cache_id, 16
|
23
|
+
# end
|
23
24
|
#
|
24
|
-
#
|
25
|
+
# Then use it like this:
|
26
|
+
#
|
27
|
+
# entry = Entry.new some_data
|
28
|
+
# entry.offset
|
29
|
+
#
|
30
|
+
# NOTE: In contrast to the already available
|
25
31
|
# http://raa.ruby-lang.org/project/bit-struct/ implementation, BitStructEx
|
26
32
|
# allows nested structures which are not aligned on byte boundaries.
|
27
33
|
#
|
28
|
-
# Author:: Daniel Lukic (mailto:bitstructex@berlinfactor.com)
|
29
|
-
# Copyright:: Copyright (c) 2005 The.Berlin.Factor
|
30
|
-
# License:: Distributes under the GPL
|
31
|
-
#
|
32
34
|
module BitStruct
|
33
35
|
|
34
36
|
# Define a subclass of StructBase and use the available field types (at
|
@@ -41,14 +43,25 @@ module BitStruct
|
|
41
43
|
# Please see the provided examples in the samples folder for.. well..
|
42
44
|
# examples.
|
43
45
|
#
|
44
|
-
|
46
|
+
# NOTE: You may ask 'What examples?'. And right you are, I haven't written
|
47
|
+
# any examples, yet.. Sorry.. :)
|
48
|
+
#
|
49
|
+
class StructBase
|
45
50
|
|
46
|
-
|
47
|
-
|
51
|
+
attr_reader :data
|
52
|
+
|
53
|
+
def initialize( initial_data = nil, endianess = nil )
|
54
|
+
set_data initial_data, endianess if initial_data
|
48
55
|
end
|
49
56
|
|
50
|
-
def
|
51
|
-
|
57
|
+
def set_data( new_data, endianess = nil )
|
58
|
+
raise "No data?" unless new_data
|
59
|
+
if new_data.is_a? ByteData
|
60
|
+
raise "Cannot change endianess of ByteData object" if endianess
|
61
|
+
@data = new_Data
|
62
|
+
else new_data
|
63
|
+
@data = ByteData.new new_data, endianess
|
64
|
+
end
|
52
65
|
end
|
53
66
|
|
54
67
|
class << self
|
@@ -71,6 +84,7 @@ module BitStruct
|
|
71
84
|
slicer.data = @data
|
72
85
|
field.send :read, slicer
|
73
86
|
end
|
87
|
+
|
74
88
|
define_method "#{field.name}=" do |val|
|
75
89
|
slicer = context.send :get_slicer_for, field
|
76
90
|
slicer.data = @data
|
metadata
CHANGED
@@ -3,16 +3,16 @@ 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.
|
7
|
-
date: 2006-
|
6
|
+
version: 0.0.74
|
7
|
+
date: 2006-05-06 00:00:00 +02:00
|
8
8
|
summary: Simple DSL for defining bit-based structures on byte data.
|
9
9
|
require_paths:
|
10
|
-
- src
|
10
|
+
- src
|
11
11
|
email: tfdj {AT} berlinfactor {DOT} com
|
12
12
|
homepage: bit-struct-ex.rubyforge.org.
|
13
13
|
rubyforge_project: bit-struct-ex
|
14
14
|
description: "Allows the specification of bit-based structures and provides an intuitive way of access data. Example: class Flags < StructBase unsigned :direction, 4 unsigned :multiplier, 2 unsigned :offset, 2 end class Entry < StructBase unsigned :offset, 4 nested :flags, Flags unsigned :address, 24 unsigned :cache_id, 16 end In contrast to the already available http://raa.ruby-lang.org/project/bit-struct/ implementation, BitStructEx allows nested structures which are not aligned on byte boundaries."
|
15
|
-
autorequire:
|
15
|
+
autorequire:
|
16
16
|
default_executable:
|
17
17
|
bindir: bin
|
18
18
|
has_rdoc: true
|
@@ -28,12 +28,15 @@ cert_chain:
|
|
28
28
|
authors:
|
29
29
|
- The.French.DJ
|
30
30
|
files:
|
31
|
-
- src/
|
32
|
-
- src/
|
33
|
-
- src/
|
34
|
-
- src/
|
35
|
-
- src/
|
36
|
-
- src/
|
31
|
+
- src/bit_struct
|
32
|
+
- src/rake
|
33
|
+
- src/bit_struct/bit_slicer.rb
|
34
|
+
- src/bit_struct/byte_data.rb
|
35
|
+
- src/bit_struct/field.rb
|
36
|
+
- src/bit_struct/nested.rb
|
37
|
+
- src/bit_struct/struct_base.rb
|
38
|
+
- src/bit_struct/text.rb
|
39
|
+
- src/bit_struct/unsigned.rb
|
37
40
|
test_files: []
|
38
41
|
|
39
42
|
rdoc_options: []
|
File without changes
|
File without changes
|
File without changes
|