CStructParser 0.1.0
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/README.md +44 -0
- data/Rakefile +15 -0
- data/UNLICENSE +24 -0
- data/lib/cstruct_parser/bindata.rb +102 -0
- data/lib/cstruct_parser/parser.rb +70 -0
- data/lib/cstruct_parser/version.rb +5 -0
- data/lib/cstruct_parser.rb +38 -0
- data/sig/CStructParser.rbs +4 -0
- metadata +119 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 3e07a5df41c9f0657cea7c81adc45ab49353e3f92c8e688dc22880e7dc89ebcd
|
|
4
|
+
data.tar.gz: 3473f7f32f3cdf1116f1fe0d02e550c899acfd9d6ae8eb22bb21a28944d050e9
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a4160449ba037b6c1ea9ee734074118a99310f25c85771c94ff4f4a903ec8016c513345ad849f77165039cbbc9c146f1f584dd72ab0b13086e36274aa42fb39b
|
|
7
|
+
data.tar.gz: fde1f3e2de0ce8314d6fda2ca3cad45a37586f67e1b693fcbef30a21c478d545ce4a73fcd26b40357e9372e5d9c0d91d9712079ea35c36cfe8bcf5bc62fc00dd
|
data/CHANGELOG.md
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# CStructParser
|
|
2
|
+
|
|
3
|
+
Parse C structs into usable Ruby objects like BinData records.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bundle add CStructParser
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
gem install CStructParser
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### For usage with [FFI](https://github.com/ffi/ffi)
|
|
22
|
+
```
|
|
23
|
+
sourceCode = 'struct s { int a; float b; };' # Can be path to file aswell
|
|
24
|
+
CStructParser.to_ffi(sourceCode)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### For usage with [BinData](https://github.com/dmendel/bindata)
|
|
28
|
+
```
|
|
29
|
+
CStructParser.to_bindata('stuff.h')
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Other use cases
|
|
33
|
+
|
|
34
|
+
Not implemented, but should be quite simple to add support for other uses like [BinaryStruct](https://github.com/ManageIQ/binary_struct), `String#unpack` format etc.
|
|
35
|
+
|
|
36
|
+
## Development
|
|
37
|
+
|
|
38
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
39
|
+
|
|
40
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
41
|
+
|
|
42
|
+
## Contributing
|
|
43
|
+
|
|
44
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/davispuh/CStructParser.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'bundler/gem_tasks'
|
|
4
|
+
require 'rspec/core/rake_task'
|
|
5
|
+
require 'yard'
|
|
6
|
+
|
|
7
|
+
desc 'Run specs'
|
|
8
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
YARD::Rake::YardocTask.new(:doc) do |t|
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
task default: :spec
|
data/UNLICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
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 NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
require 'stringio'
|
|
2
|
+
|
|
3
|
+
module CStructParser::BinData
|
|
4
|
+
|
|
5
|
+
TYPES = {
|
|
6
|
+
:bool => :bit1,
|
|
7
|
+
:char => :int8,
|
|
8
|
+
:s_char => :int8,
|
|
9
|
+
:char_s => :int8,
|
|
10
|
+
:u_char => :uint8,
|
|
11
|
+
:short => :int16,
|
|
12
|
+
:u_short => :uint16,
|
|
13
|
+
:int => :int32,
|
|
14
|
+
:u_int => :uint32,
|
|
15
|
+
:long_long => :int64,
|
|
16
|
+
:u_long_long => :uint64,
|
|
17
|
+
:float => :float,
|
|
18
|
+
:double => :double,
|
|
19
|
+
:long_double => :long_double
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
TYPES_32 = {
|
|
23
|
+
:long => :int32,
|
|
24
|
+
:u_long => :uint32
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
TYPES_64 = {
|
|
28
|
+
:long => :int64,
|
|
29
|
+
:u_long => :uint64
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
def self.toBinDataType(type, options)
|
|
33
|
+
if type.is_a?(FFIGenerate::Generator::PrimitiveType)
|
|
34
|
+
types = TYPES.merge(options[:long32] ? TYPES_32 : TYPES_64)
|
|
35
|
+
raise "Unknown primitive type #{type.clang_type}" unless types.include?(type.clang_type)
|
|
36
|
+
types[type.clang_type]
|
|
37
|
+
elsif type.is_a?(FFIGenerate::Generator::StringType)
|
|
38
|
+
:string
|
|
39
|
+
elsif type.is_a?(FFIGenerate::Generator::ArrayType)
|
|
40
|
+
:array
|
|
41
|
+
elsif type.is_a?(FFIGenerate::Generator::ByValueType)
|
|
42
|
+
type.name.to_ruby_downcase
|
|
43
|
+
elsif type.is_a?(FFIGenerate::Generator::StructOrUnion)
|
|
44
|
+
('p' + type.ruby_name).to_sym
|
|
45
|
+
elsif type.is_a?(FFIGenerate::Generator::PointerType)
|
|
46
|
+
types = TYPES.merge(options[:long_32] ? TYPES_32 : TYPES_64)
|
|
47
|
+
typeName = type.ruby_name
|
|
48
|
+
typeName = 's_char' if typeName == 'schar'
|
|
49
|
+
typeName = 'u_char' if typeName == 'uchar'
|
|
50
|
+
typeName = 'u_short' if typeName == 'ushort'
|
|
51
|
+
typeName = 'u_int' if typeName == 'uint'
|
|
52
|
+
typeName = 'u_long' if typeName == 'ulong'
|
|
53
|
+
typeName = 'long_long' if typeName == 'longlong'
|
|
54
|
+
typeName = 'u_long_long' if typeName == 'ulonglong'
|
|
55
|
+
typeName = 'long_double' if typeName == 'longdouble'
|
|
56
|
+
typeName = types[typeName.to_sym].to_s if types.include?(typeName.to_sym)
|
|
57
|
+
('p' * type.depth + typeName).to_sym
|
|
58
|
+
else
|
|
59
|
+
raise "Unexpected type #{type}"
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def self.buildField(field, attributes, i, options, indent = 2)
|
|
64
|
+
params = []
|
|
65
|
+
if field[:type].is_a?(FFIGenerate::Generator::ArrayType)
|
|
66
|
+
typeName = 'array'
|
|
67
|
+
elementType = toBinDataType(field[:type].element_type, options)
|
|
68
|
+
params << ['type', ':' + elementType.to_s].join(': ')
|
|
69
|
+
params << ['initial_length', field[:type].constant_size].join(': ')
|
|
70
|
+
else
|
|
71
|
+
typeName = toBinDataType(field[:type], options)
|
|
72
|
+
end
|
|
73
|
+
if attributes[:aligned]
|
|
74
|
+
params << ['byte_align', attributes[:aligned]].join(': ')
|
|
75
|
+
end
|
|
76
|
+
fieldName = field[:name] ? field[:name].to_ruby_downcase : 'field' + (i + 1).to_s
|
|
77
|
+
comment = ''
|
|
78
|
+
comment = " # #{field[:comment].join(' ')}" unless field[:comment].empty?
|
|
79
|
+
' ' * indent + "#{typeName} :#{fieldName}#{params.empty? ? '' : ', ' + params.join(', ')}#{comment}\n"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def self.generate_rb(declarations, options = {})
|
|
83
|
+
result = StringIO.new
|
|
84
|
+
indent = options[:indent].to_i
|
|
85
|
+
declarations.each do |declaration|
|
|
86
|
+
if declaration.is_a?(FFIGenerate::Generator::StructOrUnion)
|
|
87
|
+
name = declaration.name.format(:downcase, :camelcase, FFIGenerate::Generator::Name::RUBY_KEYWORDS)
|
|
88
|
+
classLine = "class #{name} < BinData::Record\n"
|
|
89
|
+
classLine += ' ' * indent + "endian :#{options[:endian]}\n"
|
|
90
|
+
classLine += ' ' * indent + "# TODO Choice/Union\n" if declaration.union?
|
|
91
|
+
result << classLine
|
|
92
|
+
declaration.fields.each_with_index do |field, i|
|
|
93
|
+
fieldLine = buildField(field, declaration.attributes, i, options, declaration.union? && i > 0 ? 0 : indent)
|
|
94
|
+
fieldLine = ' ' * indent + '# ' + fieldLine if declaration.union? && i > 0
|
|
95
|
+
result << fieldLine
|
|
96
|
+
end
|
|
97
|
+
result << "end\n\n"
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
result.string
|
|
101
|
+
end
|
|
102
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'ffi_generator'
|
|
4
|
+
require 'stringio'
|
|
5
|
+
require 'tempfile'
|
|
6
|
+
require_relative 'bindata'
|
|
7
|
+
|
|
8
|
+
module CStructParser
|
|
9
|
+
class Parser
|
|
10
|
+
|
|
11
|
+
attr_reader :generator
|
|
12
|
+
|
|
13
|
+
def initialize(options = {})
|
|
14
|
+
@options = { module_name: 'CStructModule', endian: :little, indent: 2 }.merge(options)
|
|
15
|
+
@generator = nil
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def load!(headers)
|
|
19
|
+
@options[:headers] = headers
|
|
20
|
+
@options[:headers] = [@options[:headers]] unless @options[:headers].is_a?(Array)
|
|
21
|
+
@generator = FFIGenerate::Generator.new(@options)
|
|
22
|
+
@generator.declarations
|
|
23
|
+
@generator
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def parse!(data)
|
|
27
|
+
if isFile(data)
|
|
28
|
+
load!(data)
|
|
29
|
+
else
|
|
30
|
+
Tempfile.create('struct.h') do |file|
|
|
31
|
+
file.write(data)
|
|
32
|
+
file.flush
|
|
33
|
+
load!(file.path)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
@generator
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def declarations
|
|
40
|
+
return nil unless @generator
|
|
41
|
+
@generator.declarations
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def to_ffi
|
|
45
|
+
@generator.generate_rb
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def to_bindata
|
|
49
|
+
BinData.generate_rb(@generator.declarations, @options)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def to_pack
|
|
53
|
+
# TODO
|
|
54
|
+
# Ruby String#unpack / Array#pack format
|
|
55
|
+
raise 'Not Implemented!'
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def to_binarystruct
|
|
59
|
+
# TODO
|
|
60
|
+
# https://github.com/ManageIQ/binary_struct
|
|
61
|
+
raise 'Not Implemented!'
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
protected
|
|
65
|
+
|
|
66
|
+
def isFile(data)
|
|
67
|
+
data.to_s.lines.count == 1 && File.exist?(data.to_s)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'cstruct_parser/version'
|
|
4
|
+
require_relative 'cstruct_parser/parser'
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
module CStructParser
|
|
8
|
+
class Error < StandardError; end
|
|
9
|
+
|
|
10
|
+
def self.load(headers, options = {})
|
|
11
|
+
parser = Parser.new(options)
|
|
12
|
+
parser.load!(headers)
|
|
13
|
+
parser
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.parse(data, options = {})
|
|
17
|
+
parser = Parser.new(options)
|
|
18
|
+
parser.parse!(data)
|
|
19
|
+
parser
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.to_ffi(data, options = {})
|
|
23
|
+
self.parse(data, options).to_ffi
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.to_bindata(data, options = {})
|
|
27
|
+
self.parse(data, options).to_bindata
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.to_pack(data, options = {})
|
|
31
|
+
self.parse(data, options).to_pack
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.to_binarystruct(data, options = {})
|
|
35
|
+
self.parse(data, options).to_binarystruct
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: CStructParser
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Dāvis Mosāns
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: ffi_generator
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rake
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: rspec
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: simplecov
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: yard
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
type: :development
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
executables: []
|
|
83
|
+
extensions: []
|
|
84
|
+
extra_rdoc_files: []
|
|
85
|
+
files:
|
|
86
|
+
- CHANGELOG.md
|
|
87
|
+
- README.md
|
|
88
|
+
- Rakefile
|
|
89
|
+
- UNLICENSE
|
|
90
|
+
- lib/cstruct_parser.rb
|
|
91
|
+
- lib/cstruct_parser/bindata.rb
|
|
92
|
+
- lib/cstruct_parser/parser.rb
|
|
93
|
+
- lib/cstruct_parser/version.rb
|
|
94
|
+
- sig/CStructParser.rbs
|
|
95
|
+
homepage: https://github.com/davispuh/CStructParser
|
|
96
|
+
licenses:
|
|
97
|
+
- UNLICENSE
|
|
98
|
+
metadata:
|
|
99
|
+
homepage_uri: https://github.com/davispuh/CStructParser
|
|
100
|
+
source_code_uri: https://github.com/davispuh/CStructParser
|
|
101
|
+
changelog_uri: https://github.com/davispuh/CStructParser/CHANGELOG.md
|
|
102
|
+
rdoc_options: []
|
|
103
|
+
require_paths:
|
|
104
|
+
- lib
|
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - ">="
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: 3.2.0
|
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
|
+
requirements:
|
|
112
|
+
- - ">="
|
|
113
|
+
- !ruby/object:Gem::Version
|
|
114
|
+
version: '0'
|
|
115
|
+
requirements: []
|
|
116
|
+
rubygems_version: 4.0.5
|
|
117
|
+
specification_version: 4
|
|
118
|
+
summary: Parse C structs into usable Ruby structs such as BinData
|
|
119
|
+
test_files: []
|