ffi-struct_ex 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b5f6419c35f3fafa8e32d334891f9ff94c8de5d8
4
+ data.tar.gz: 44f8717d43e0bd7704fbafd3a5b86f884bafe3c5
5
+ SHA512:
6
+ metadata.gz: 1f3ed97c400af758499755b85d04f06886a595bd5fd6a1cc729ba0dceaaa2df964368c45c26429cf9393a031b43da2473c35206f63e5f69efd629643f7a8760b
7
+ data.tar.gz: 0d3840f2325ca19f4178121ad9bd450fb906c80f85ce4bb1be390e36a89490bff6b2f4e8db7ada1c6390add4b4b11a1d7351cbca82030d67fb1b10f5c9afcf0d
data/.gitattributes ADDED
@@ -0,0 +1,22 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
3
+
4
+ # Custom for Visual Studio
5
+ *.cs diff=csharp
6
+ *.sln merge=union
7
+ *.csproj merge=union
8
+ *.vbproj merge=union
9
+ *.fsproj merge=union
10
+ *.dbproj merge=union
11
+
12
+ # Standard to msysgit
13
+ *.doc diff=astextplain
14
+ *.DOC diff=astextplain
15
+ *.docx diff=astextplain
16
+ *.DOCX diff=astextplain
17
+ *.dot diff=astextplain
18
+ *.DOT diff=astextplain
19
+ *.pdf diff=astextplain
20
+ *.PDF diff=astextplain
21
+ *.rtf diff=astextplain
22
+ *.RTF diff=astextplain
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.project ADDED
@@ -0,0 +1,18 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projectDescription>
3
+ <name>ffi-struct_ex</name>
4
+ <comment></comment>
5
+ <projects>
6
+ </projects>
7
+ <buildSpec>
8
+ <buildCommand>
9
+ <name>com.aptana.ide.core.unifiedBuilder</name>
10
+ <arguments>
11
+ </arguments>
12
+ </buildCommand>
13
+ </buildSpec>
14
+ <natures>
15
+ <nature>com.aptana.ruby.core.rubynature</nature>
16
+ <nature>com.aptana.projects.webnature</nature>
17
+ </natures>
18
+ </projectDescription>
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ffi-struct_ex.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 RUIJIA LI
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # FFI::StructEx
2
+
3
+ The gem adds bit field for C struct by FFI::StructEx inherited from FFI::Struct. The functionality is limited now, refer to usage section for supported forms.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ffi-struct_ex'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ffi-struct_ex
18
+
19
+ ## Usage
20
+
21
+ * Struct (only support "unsigned" field)
22
+
23
+ ```ruby
24
+ require 'ffi/struct_ex'
25
+
26
+ class Subject < FFI::StructEx
27
+ layout :bits_0_2, 3,
28
+ :bit_3, 1,
29
+ :bit_4, 1,
30
+ :bits_5_7, 3
31
+ end
32
+
33
+ subject = Subject.new(bits_0_2: 0b001, bit_3: 0b1, bit_4: 0b0, bits_5_7: 0b011)
34
+
35
+ subject[:bits_0_2] #=> 0b101
36
+ subject.read #=> 0b0110_1001
37
+ ```
38
+
39
+ * Struct (embedded bit fields)
40
+
41
+ ```ruby
42
+ require 'ffi/struct_ex'
43
+
44
+ class Subject < FFI::StructEx
45
+ layout :field_0, bit_fields(:bits_0_2, 3,
46
+ :bit_3, 1,
47
+ :bit_4, 1,
48
+ :bits_5_7, 3),
49
+ :field_1, :uint8,
50
+ :field_2, :uint8,
51
+ :field_3, [:uint8, 2]
52
+ end
53
+
54
+ subject[:field_0].class.superclass #=> FFI::StructEx
55
+
56
+ subject[:field_0] = 0b0110_1001
57
+ subject[:field_0].read #=> 0b0110_1001
58
+
59
+ subject[:field_0][:bits_0_2] = 0b101
60
+ subject[:field_0][:bits_0_2] #=> 0b101
61
+ subject[:field_0].read #=> 0b0110_1101
62
+
63
+ subject[:field_0] = {bits_0_2: 0b001, bit_3: 0b1, bit_4: 0b0, bits_5_7: 0b011}
64
+ subject[:field_0].read #=> 0b0110_1001
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require "bundler/gem_tasks"
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'lib' << 'test'
8
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ffi/struct_ex/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ffi-struct_ex"
8
+ spec.version = Ffi::StructEx::VERSION
9
+ spec.authors = ["Ruijia Li"]
10
+ spec.email = ["ruijia.li@gmail.com"]
11
+ spec.description = %q{A module to add extra functionalities to FFI::Struct}
12
+ spec.summary = %q{A module to add extra functionalities to FFI::Struct}
13
+ spec.homepage = "https://github.com/rli9/ffi-struct_ex"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "ffi", "~> 1.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,160 @@
1
+ require 'ffi'
2
+
3
+ class Integer
4
+ def to_bytes(size)
5
+ bytes = [0] * size
6
+ bytes.each_index do |i|
7
+ bytes[i] = (self >> (i * 8)) & 0xff
8
+ end
9
+
10
+ bytes
11
+ end
12
+ end
13
+
14
+ module FFI
15
+ class StructEx < FFI::Struct
16
+ BitLayout = ::Struct.new(:name, :bits, :offset, :texts)
17
+
18
+ class << self
19
+ def bit_fields(*descs)
20
+ struct_class = Class.new(StructEx) do
21
+ layout(*descs)
22
+ end
23
+
24
+ bit_fields_class = Class.new(FFI::StructLayout::Field) do
25
+ def initialize(name, offset, type)
26
+ #TODO use a different native_type to avoid dummy field for struct
27
+ super(name, offset, FFI::Type::Struct.new(self.class.struct_class))
28
+ end
29
+
30
+ def get(ptr)
31
+ #self.class.struct_class == type.struct_class
32
+ self.class.struct_class.new(ptr.slice(self.offset, self.size))
33
+ end
34
+
35
+ def put(ptr, value)
36
+ self.class.struct_class.new(ptr.slice(self.offset, self.size)).write(value)
37
+ end
38
+
39
+ class << self
40
+ attr_accessor :struct_class
41
+
42
+ def alignment
43
+ struct_class.alignment
44
+ end
45
+ end
46
+
47
+ self.struct_class = struct_class
48
+ end
49
+ end
50
+
51
+ attr_reader :bits_size, :bit_layouts
52
+
53
+ def layout(*descs)
54
+ if descs.size == 0 || !descs[1].is_a?(Integer)
55
+ super(*descs)
56
+ else
57
+ @bit_layouts = {}
58
+
59
+ index = @bits_size = 0
60
+
61
+ while index < descs.size
62
+ bit_field_name, bits, texts = descs[index, 3]
63
+
64
+ if texts.kind_of?(Hash)
65
+ @bit_layouts[bit_field_name] = BitLayout.new(bit_field_name, bits, @bits_size, texts)
66
+ index += 3
67
+ else
68
+ @bit_layouts[bit_field_name] = BitLayout.new(bit_field_name, bits, @bits_size)
69
+ index += 2
70
+ end
71
+
72
+ @bits_size += bits
73
+ end
74
+
75
+ #FIXME consider 24 bits situation or larger than 32 bits
76
+ #FIXME remove dummy field or have a better name for this field
77
+ super(:dummy, "uint#{bytes_size * 8}".to_sym)
78
+ end
79
+ end
80
+
81
+ def bytes_size
82
+ (bits_size + 7) >> 3
83
+ end
84
+
85
+ def alignment
86
+ #FIXME consider 24 bits situation
87
+ FFI.find_type("uint#{bytes_size * 8}".to_sym).alignment
88
+ end
89
+ end
90
+
91
+ def initialize(options = {})
92
+ if options.is_a?(FFI::Pointer)
93
+ super(options)
94
+ else
95
+ super()
96
+ write(options)
97
+ end
98
+ end
99
+
100
+ def [](bit_field_name)
101
+ return super unless self.class.bit_layouts && self.class.bit_layouts.keys.include?(bit_field_name)
102
+
103
+ bit_layout = self.class.bit_layouts[bit_field_name]
104
+ mask = ((1 << bit_layout.bits) - 1) << bit_layout.offset
105
+
106
+ (self.read & mask) >> bit_layout.offset
107
+ end
108
+
109
+ def []=(bit_field_name, value)
110
+ return super unless self.class.bit_layouts && self.class.bit_layouts.keys.include?(bit_field_name)
111
+ value = look_for_value(bit_field_name, value)
112
+
113
+ bit_layout = self.class.bit_layouts[bit_field_name]
114
+ mask = ((1 << bit_layout.bits) - 1) << bit_layout.offset
115
+
116
+ self.write((self.read & (-1 - mask)) | ((value << bit_layout.offset) & mask))
117
+ end
118
+
119
+ def write(value)
120
+ if value.is_a?(Integer)
121
+ to_ptr.write_array_of_uint8(value.to_bytes(self.class.bytes_size))
122
+ elsif value.is_a?(Hash)
123
+ value.each do |bit_field_name, v|
124
+ self[bit_field_name] = v if self.class.bit_layouts.keys.include? bit_field_name
125
+ end
126
+ end
127
+ end
128
+
129
+ def read
130
+ bytes = to_ptr.read_array_of_uint8(self.class.bytes_size)
131
+ bytes.reverse.inject(0) {|value, n| (value << 8) | n}
132
+ end
133
+
134
+ def size
135
+ self.class.bytes_size
136
+ end
137
+
138
+ def look_for_value(bit_field_name, value)
139
+ #FIXME add error handling
140
+ if value.kind_of?(Integer)
141
+ value
142
+ elsif value.kind_of?(String)
143
+ #FIXME this requires texts hash to have downcase key
144
+ value = value.downcase
145
+ if self.class.bit_layouts[bit_field_name].texts && self.class.bit_layouts[bit_field_name].texts[value]
146
+ self.class.bit_layouts[bit_field_name].texts[value]
147
+ else
148
+ case value
149
+ when /^\d+$/
150
+ value.to_i
151
+ when /^0x[\da-fA-F]+$/
152
+ value.to_i(16)
153
+ when /^0b[01]+$/
154
+ value.to_i(2)
155
+ end
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,5 @@
1
+ module Ffi
2
+ module StructEx
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ require "ffi/struct_ex/version"
2
+ require "ffi/struct_ex/struct_ex"
3
+
4
+ module FFI
5
+ end
@@ -0,0 +1 @@
1
+
@@ -0,0 +1,89 @@
1
+ require 'test_helper'
2
+ require 'test/unit'
3
+ require 'ffi/struct_ex'
4
+
5
+ class TestStructEx < Test::Unit::TestCase
6
+ def test_bit_fields
7
+ subject = Class.new(FFI::StructEx) do
8
+ layout :field_0, bit_fields(:bits_0_2, 3,
9
+ :bit_3, 1,
10
+ :bit_4, 1,
11
+ :bits_5_7, 3),
12
+ :field_1, :uint8,
13
+ :field_2, :uint8,
14
+ :field_3, [:uint8, 2]
15
+ end.new
16
+
17
+ assert_equal(FFI::StructEx, subject[:field_0].class.superclass)
18
+
19
+ subject[:field_0] = 0b0110_1001
20
+ assert_equal(0b0110_1001, subject[:field_0].read)
21
+ assert_equal(0b001, subject[:field_0][:bits_0_2])
22
+
23
+ subject[:field_0][:bits_0_2] = 0b101
24
+ assert_equal(0b101, subject[:field_0][:bits_0_2])
25
+ assert_equal(0b0110_1101, subject[:field_0].read)
26
+
27
+ subject[:field_0] = {bits_0_2: 0b001, bit_3: 0b1, bit_4: 0b0, bits_5_7: 0b011}
28
+ assert_equal(0b0110_1001, subject[:field_0].read)
29
+ assert_equal(0b001, subject[:field_0][:bits_0_2])
30
+ end
31
+
32
+ def test_pure_bit_fields
33
+ subject_class = Class.new(FFI::StructEx) do
34
+ layout :bits_0_2, 3,
35
+ :bit_3, 1,
36
+ :bit_4, 1,
37
+ :bits_5_7, 3
38
+ end
39
+
40
+ subject = subject_class.new
41
+
42
+ subject[:bits_0_2] = 0b101
43
+ assert_equal(0b101, subject[:bits_0_2])
44
+
45
+ subject[:bit_3] = 0b1
46
+ assert_equal(0b1, subject[:bit_3])
47
+ assert_equal(0b101, subject[:bits_0_2])
48
+
49
+ subject = subject_class.new(0b0110_1001)
50
+ assert_equal(0b001, subject[:bits_0_2])
51
+ assert_equal(0b1, subject[:bit_3])
52
+ assert_equal(0b0, subject[:bit_4])
53
+ assert_equal(0b011, subject[:bits_5_7])
54
+
55
+ subject = subject_class.new(bits_0_2: 0b001, bit_3: 0b1, bit_4: 0b0, bits_5_7: 0b011)
56
+ assert_equal(0b001, subject[:bits_0_2])
57
+ assert_equal(0b1, subject[:bit_3])
58
+ assert_equal(0b0, subject[:bit_4])
59
+ assert_equal(0b011, subject[:bits_5_7])
60
+ assert_equal(0b0110_1001, subject.read)
61
+ end
62
+
63
+ def test_interpreted_bit_fields
64
+ subject_class = Class.new(FFI::StructEx) do
65
+ layout :bits_0_2, 3, {'all_1' => 0b111, 'all_0' => 0b000},
66
+ :bit_3, 1, {'yes' => 0b1, 'no' => 0b0},
67
+ :bit_4, 1,
68
+ :bits_5_7, 3
69
+ end
70
+
71
+ subject = subject_class.new(bits_0_2: 'all_1', bit_3: 'yes')
72
+ assert_equal(0b111, subject[:bits_0_2])
73
+ assert_equal(0b1, subject[:bit_3])
74
+ assert_equal(0b0, subject[:bit_4])
75
+ assert_equal(0b0, subject[:bits_5_7])
76
+
77
+ subject[:bits_0_2] = 'all_0'
78
+ assert_equal(0b000, subject[:bits_0_2])
79
+
80
+ subject[:bits_0_2] = 0b010
81
+ assert_equal(0b010, subject[:bits_0_2])
82
+
83
+ subject[:bit_3] = 'no'
84
+ assert_equal(0b0, subject[:bit_3])
85
+
86
+ subject[:bit_3] = 1
87
+ assert_equal(0b1, subject[:bit_3])
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ffi-struct_ex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ruijia Li
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A module to add extra functionalities to FFI::Struct
56
+ email:
57
+ - ruijia.li@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitattributes
63
+ - .gitignore
64
+ - .project
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - ffi-struct_ex.gemspec
70
+ - lib/ffi/struct_ex.rb
71
+ - lib/ffi/struct_ex/struct_ex.rb
72
+ - lib/ffi/struct_ex/version.rb
73
+ - test/test_helper.rb
74
+ - test/test_struct_ex.rb
75
+ homepage: https://github.com/rli9/ffi-struct_ex
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: A module to add extra functionalities to FFI::Struct
99
+ test_files:
100
+ - test/test_helper.rb
101
+ - test/test_struct_ex.rb