rbstruct 0.0.1

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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZDFiYzI0YjkyMDg3MzFjZmI4ZGI5ZTEyOGI4OThkMTA4MWRmM2VjNQ==
5
+ data.tar.gz: !binary |-
6
+ NjNkYmUxYjVkNGEwN2QxNGNmOWJmZWZkZjlmNGI4YzJjOGYzYzg5MQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ YTU3NmUwNzZlZjM5NTM0MTVlMjRlYmRlZTZjNTYyNmI3MTM1N2VmYWU2YjUx
10
+ ZDQ3NzZiYzRmNjhiMWZlMGE4NjI5YmQxY2QxODYxZGVlZjVhYmY1MTJiOGE5
11
+ MWJhZWM4YzRiMTJkMDU0ZTgxNjNkMjBhNWI2Njg3ZTY0NzJjYmM=
12
+ data.tar.gz: !binary |-
13
+ YzAxMWQ0MzIzZmU2Y2E5Mzg3NmNkMDY2NzI5ZGQ2OTA0ODg1MWJkOGU2Zjg3
14
+ OTEzODI5NmFhOWRkNzJmMWZmMTAyYjY1NjU0MTljZTBlZmIwNmRlNzFiMjNi
15
+ NjAxMjY0ZWQ4MmUyYTczYWVkNDc3ZGU2NWE4YzkxZDMxYzJlYjc=
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,22 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ script: bundle exec rake spec
5
+ before_deploy:
6
+ - bundle exec rake build
7
+ - export GEM_FILE="$(ls -1 pkg/*.gem | tail -n1)"
8
+ deploy:
9
+ - provider: rubygems
10
+ api_key: $RUBYGEMS_KEY
11
+ gem: rbstruct
12
+ on:
13
+ tags: true
14
+ repo: kaffepanna/rbstruct
15
+ - provider: releases
16
+ api_key:
17
+ secure: $GITHUB_TOKEN
18
+ file: ${GEM_FILE}
19
+ file_glob: true
20
+ on:
21
+ tags: true
22
+ repo: kaffepanna/rbstruct
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rbstruct.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Patrik Pettersson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Rbstruct
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rbstruct`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rbstruct'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rbstruct
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rbstruct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rbstruct"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/examples/gpt.rb ADDED
@@ -0,0 +1,46 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),'../lib')
2
+ require 'rbstruct'
3
+ dev = ARGV[0]
4
+
5
+ raise Exception.new('No dev') unless dev
6
+
7
+ GptHeader = RbStruct do
8
+ unsigned_char :signature, 8
9
+ unsigned_int :revision
10
+ unsigned_int :header_size
11
+ unsigned_int :crc32
12
+ unsigned_int :revserved
13
+ unsigned_long :current_lba
14
+ unsigned_long :backup_lba
15
+ unsigned_long :first_lba
16
+ unsigned_long :last_lba
17
+ unsigned_char :guid, 16
18
+ unsigned_long :partition_array_lba
19
+ unsigned_int :n_partition_array
20
+ unsigned_int :partition_entry_size
21
+ unsigned_int :partition_array_crc32
22
+ end
23
+
24
+ GptPartEntry = RbStruct do
25
+ unsigned_char :part_type_guid, 16
26
+ unsigned_char :part_guid, 16
27
+ unsigned_long :last_lba
28
+ unsigned_long :attributes
29
+ unsigned_char :name, 72
30
+ end
31
+
32
+
33
+
34
+ file = open(dev, 'r+')
35
+
36
+ file.seek(512)
37
+ header = RbStruct::read_struct(file, GptHeader).first
38
+
39
+ file.seek(512*header.backup_lba)
40
+ backup = RbStruct::read_struct(file, GptHeader).first
41
+
42
+ puts "Primary header"
43
+ puts header.inspect
44
+ puts "----------------------------------------"
45
+ puts "Backup header"
46
+ puts backup.inspect
@@ -0,0 +1,153 @@
1
+ module RbStruct
2
+
3
+ TYPES = {
4
+ :float => {:size => 4, :str => 'f'},
5
+ :int => {:size => 4, :str => 'i'},
6
+ :short => {:size => 2, :str => 's'},
7
+ :char => {:size => 1, :str => 'c'},
8
+ :unsigned_char => {:size => 1, :str => "C"},
9
+ :unsigned_short => {:size => 2, :str => "S"},
10
+ :unsigned_int => {:size => 4, :str => "I"},
11
+ :unsigned_long => {:size => 8, :str => "L_"}
12
+ }
13
+
14
+
15
+
16
+ class StructBase < Array
17
+ class << self; attr_reader :format_string, :fields, :bsize, :size end
18
+
19
+ protected
20
+
21
+ TYPES.each_key do |type|
22
+ class_eval %{
23
+ def self.#{type}(name, size=1)
24
+ field_add(:#{type}, name, size)
25
+ end
26
+ }
27
+ end
28
+
29
+ # def initialize(*args)
30
+ # unless args.size > 0
31
+ # super self.class.size,0
32
+ # else
33
+ # super *args
34
+ # end
35
+ # end
36
+
37
+ def self.field_add(type, name, length)
38
+ @fields ||= Array.new
39
+ @bsize ||= 0
40
+ @size ||= 0
41
+ @format_string ||= String.new
42
+
43
+ #calculate offset in array
44
+ f = @fields.last
45
+ if f == nil
46
+ offset = 0
47
+ elsif f[:type].class == Symbol
48
+ offset = f[:offset] + f[:length]
49
+ else
50
+ offset = f[:offset] + f[:type].size * f[:length]
51
+ end
52
+
53
+
54
+ @fields << { :name => name, :type => type,
55
+ :length => length, :offset => offset }
56
+ if type.class == Symbol
57
+ @size += length
58
+ @bsize += TYPES[type][:size]*length
59
+ @format_string += "#{TYPES[type][:str]}#{length}"
60
+ else
61
+ @size += length * type.size
62
+ @bsize += type.bsize*length
63
+ @format_string += "#{type.format_string}"*length
64
+ end
65
+
66
+ #create getter/setters
67
+ class_eval %{
68
+ def #{name}()
69
+ get_field(#{@fields.size-1})
70
+ end
71
+
72
+ def #{name}=(val)
73
+ set_field(#{@fields.size-1}, val)
74
+ end
75
+ }
76
+ end
77
+
78
+ def self.struct(type, name, size=1)
79
+ self.field_add(type, name, size)
80
+ end
81
+
82
+ def get_field(index)
83
+ type = self.class.fields[index][:type]
84
+ length = self.class.fields[index][:length]
85
+ offset = self.class.fields[index][:offset]
86
+
87
+ if type.class == Symbol
88
+ return self[offset] if length == 1
89
+ return self[offset...offset+length].to_a
90
+ else
91
+ return type.new(self[offset...offset+type.size]) if length == 1
92
+ return (0...length).collect {|i|
93
+ type.new(self[offset+i*type.size...offset+(1+i)*type.size])
94
+ }
95
+ end
96
+ end
97
+
98
+ def set_field(index, val)
99
+ type = self.class.fields[index][:type]
100
+ length = self.class.fields[index][:length]
101
+ offset = self.class.fields[index][:offset]
102
+ if type.class == Symbol
103
+ self[offset...offset+length] = val
104
+ else
105
+ self[offset...offset+length*type.size] = val
106
+ end
107
+ end
108
+ public
109
+ def to_s
110
+ pack(self.class.format_string)
111
+ end
112
+ def inspect(d=0)
113
+ self.class.to_s + " {\n" +
114
+ self.class.fields.each_with_index.map {|f,n|
115
+ field = self.get_field(n)
116
+ "\t"*(d+1) + "#{f[:type]} #{f[:name]} = " +
117
+ case field
118
+ when StructBase
119
+ "#{field.inspect(d+1)}"
120
+ else
121
+ field.inspect
122
+ end
123
+
124
+ }.join("\n") +
125
+ "\n" + "\t"*d + "}"
126
+ end
127
+
128
+ def write
129
+ end
130
+ end
131
+
132
+ def self.Struct(&block)
133
+ c = Class.new(StructBase)
134
+ c.class_eval &block
135
+ return c
136
+ end
137
+
138
+ def self.write_struct(f, *structs)
139
+ structs.each do |struct|
140
+ f.write(struct.pack(struct.class.format_string))
141
+ end
142
+ end
143
+
144
+ def self.read_struct(f, type, n=1)
145
+ format = type.format_string*n
146
+ len = type.bsize*n
147
+ array = f.read(len).unpack(format)
148
+ return Array.new(n) {
149
+ type.new(array.slice!(0, type.size))
150
+ }
151
+ end
152
+
153
+ end
data/lib/rbstruct.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "rbstruct/rbstruct"
2
+ def RbStruct &block
3
+ RbStruct.Struct &block
4
+ end
data/rbstruct.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rbstruct"
7
+ spec.version = `git describe --abbrev=0 --tags`
8
+ spec.authors = ["Patrik Pettersson"]
9
+ spec.email = ["pettersson.pa@gmail.com"]
10
+
11
+ spec.summary = %q{Create C like structs in ruby}
12
+ spec.description = %q{C like structs for ruby allowing read/write from binary files"}
13
+ spec.homepage = "https://github.com/kaffepanna/rbstruct"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbstruct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Patrik Pettersson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: C like structs for ruby allowing read/write from binary files"
56
+ email:
57
+ - pettersson.pa@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - .travis.yml
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - examples/gpt.rb
72
+ - lib/rbstruct.rb
73
+ - lib/rbstruct/rbstruct.rb
74
+ - rbstruct.gemspec
75
+ homepage: https://github.com/kaffepanna/rbstruct
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.4.5
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Create C like structs in ruby
99
+ test_files: []