iostruct 0.0.2 → 0.0.5

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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 78f534f107abf828f64d62134f93a553cb5af56af1860c338e0844b49cb482ba
4
+ data.tar.gz: cdd0dfc48cf8a171bfcfc25fbe630182424008376e245b6a4816b12ed183940e
5
+ SHA512:
6
+ metadata.gz: 534ccfd92eadd911de930d08cc787dab929d0a2af5210da3700b5f6ea98f444896d9cdc674b657216f03da753d902acf80a55ae21cb4943b34fd7e065ab8a0af
7
+ data.tar.gz: 660427b2d4652c94791a8552dd2542614fd6b7e04a895b0ab7d1ffeeabfc508b77c02d61788bd1a9a46f9c2880257cddf274297885e4b0c11d8e6f141632a518
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile.lock CHANGED
@@ -1,17 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- iostruct (0.0.2)
4
+ iostruct (0.0.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- rake (10.0.3)
9
+ rake (13.0.6)
10
10
 
11
11
  PLATFORMS
12
12
  ruby
13
13
 
14
14
  DEPENDENCIES
15
- bundler (~> 1.3)
15
+ bundler
16
16
  iostruct!
17
17
  rake
18
+
19
+ BUNDLED WITH
20
+ 2.1.4
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc "run specs"
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
data/iostruct.gemspec CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.require_paths = ["lib"]
18
18
  s.summary = "A Struct that can read/write itself from/to IO-like objects"
19
19
 
20
- s.add_development_dependency "bundler", "~> 1.3"
20
+ s.add_development_dependency "bundler"
21
21
  s.add_development_dependency "rake"
22
22
  end
23
23
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IOStruct
2
- VERSION = "0.0.2"
4
+ VERSION = "0.0.5"
3
5
  end
data/lib/iostruct.rb CHANGED
@@ -1,14 +1,45 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module IOStruct
4
+
5
+ # https://apidock.com/ruby/String/unpack
6
+ FIELD_SIZES = {
7
+ 'C' => 1, # Integer | 8-bit unsigned (unsigned char)
8
+ 'S' => 2, # Integer | 16-bit unsigned, native endian (uint16_t)
9
+ 'L' => 4, # Integer | 32-bit unsigned, native endian (uint32_t)
10
+ 'Q' => 8, # Integer | 64-bit unsigned, native endian (uint64_t)
11
+ 'c' => 1, # Integer | 8-bit signed (signed char)
12
+ 's' => 2, # Integer | 16-bit signed, native endian (int16_t)
13
+ 'l' => 4, # Integer | 32-bit signed, native endian (int32_t)
14
+ 'q' => 8, # Integer | 64-bit signed, native endian (int64_t)
15
+
16
+ 'n' => 2, # Integer | 16-bit unsigned, network (big-endian) byte order
17
+ 'N' => 4, # Integer | 32-bit unsigned, network (big-endian) byte order
18
+ 'v' => 2, # Integer | 16-bit unsigned, VAX (little-endian) byte order
19
+ 'V' => 4, # Integer | 32-bit unsigned, VAX (little-endian) byte order
20
+
21
+ 'A' => 1, # String | arbitrary binary string (remove trailing nulls and ASCII spaces)
22
+ 'a' => 1, # String | arbitrary binary string
23
+
24
+ 'D' => 8, # Float | double-precision, native format
25
+ 'd' => 8,
26
+ 'F' => 4, # Float | single-precision, native format
27
+ 'f' => 4,
28
+ 'E' => 8, # Float | double-precision, little-endian byte order
29
+ 'e' => 4, # Float | single-precision, little-endian byte order
30
+ 'G' => 8, # Float | double-precision, network (big-endian) byte order
31
+ 'g' => 4, # Float | single-precision, network (big-endian) byte order
32
+
33
+ 'x' => 1, # --- | skip forward one byte
34
+ }.freeze
35
+
2
36
  def self.new fmt, *args
3
37
  size = fmt.scan(/([a-z])(\d*)/i).map do |f,len|
4
- [len.to_i, 1].max *
5
- case f
6
- when /[AC]/i then 1
7
- when 'v' then 2
8
- when 'V','l','L' then 4
9
- when 'Q' then 8
10
- else raise "unknown fmt #{f.inspect}"
11
- end
38
+ if (field_size = FIELD_SIZES[f])
39
+ [len.to_i, 1].max * field_size
40
+ else
41
+ raise "Unknown fmt #{f.inspect}"
42
+ end
12
43
  end.inject(&:+)
13
44
 
14
45
  Struct.new( *args ).tap do |x|
@@ -48,5 +79,21 @@ module IOStruct
48
79
  def empty?
49
80
  to_a.all?{ |t| t == 0 || t.nil? || t.to_s.tr("\x00","").empty? }
50
81
  end
82
+
83
+ # allow initializing individual struct members by name, like:
84
+ # PEdump::IMAGE_SECTION_HEADER.new(
85
+ # :VirtualSize => 0x100,
86
+ # :VirtualAddress => 0x100000
87
+ # )
88
+ def initialize *args
89
+ if args.size == 1 && args.first.is_a?(Hash)
90
+ super()
91
+ args.first.each do |k,v|
92
+ send "#{k}=", v
93
+ end
94
+ else
95
+ super
96
+ end
97
+ end
51
98
  end # InstanceMethods
52
99
  end # IOStruct
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'stringio'
3
+
4
+ describe IOStruct do
5
+ describe "#read" do
6
+ it "reads from IO" do
7
+ a = [12345, 56789]
8
+ data = a.pack('L2')
9
+ x = IOStruct.new('LL', :x, :y).read(StringIO.new(data))
10
+ expect(x.x).to eq a[0]
11
+ expect(x.y).to eq a[1]
12
+ end
13
+
14
+ it "reads from String" do
15
+ a = [12345, 56789]
16
+ data = a.pack('L2')
17
+ x = IOStruct.new('LL', :x, :y).read(data)
18
+ expect(x.x).to eq a[0]
19
+ expect(x.y).to eq a[1]
20
+ end
21
+ end
22
+
23
+ it "skips on 'x'" do
24
+ a = [12345, 56789]
25
+ data = a.pack('L2')
26
+ x = IOStruct.new('x4L', :y).read(data)
27
+ expect(x.y).to eq a[1]
28
+ end
29
+
30
+ it "unpacks big-endian" do
31
+ a = [12345, 56789]
32
+ data = a.pack('nN')
33
+ x = IOStruct.new('nN', :x, :y).read(data)
34
+ expect(x.x).to eq a[0]
35
+ expect(x.y).to eq a[1]
36
+ end
37
+
38
+ it "unpacks little-endian" do
39
+ a = [12345, 56789]
40
+ data = a.pack('vV')
41
+ x = IOStruct.new('vV', :x, :y).read(data)
42
+ expect(x.x).to eq a[0]
43
+ expect(x.y).to eq a[1]
44
+ end
45
+
46
+ it "throws exception on unknown format" do
47
+ expect { IOStruct.new('K', :x) }.to raise_error('Unknown fmt "K"')
48
+ end
49
+ end
@@ -0,0 +1,6 @@
1
+ require File.join(File.dirname(__FILE__), '../lib/iostruct.rb')
2
+
3
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
4
+ RSpec.configure do |config|
5
+ config.run_all_when_everything_filtered = true
6
+ end
metadata CHANGED
@@ -1,12 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iostruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Andrey "Zed" Zaikin
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
11
  date: 2013-01-08 00:00:00.000000000 Z
@@ -14,42 +13,39 @@ dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
- version: '1.3'
19
+ version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: '1.3'
26
+ version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
- description:
41
+ description:
47
42
  email: zed.0xff@gmail.com
48
43
  executables: []
49
44
  extensions: []
50
45
  extra_rdoc_files: []
51
46
  files:
52
- - .gitignore
47
+ - ".gitignore"
48
+ - ".rspec"
53
49
  - Gemfile
54
50
  - Gemfile.lock
55
51
  - LICENSE.txt
@@ -58,29 +54,29 @@ files:
58
54
  - iostruct.gemspec
59
55
  - lib/iostruct.rb
60
56
  - lib/iostruct/version.rb
57
+ - spec/iostruct_spec.rb
58
+ - spec/spec_helper.rb
61
59
  homepage: http://github.com/zed-0xff/iostruct
62
60
  licenses:
63
61
  - MIT
64
- post_install_message:
62
+ metadata: {}
63
+ post_install_message:
65
64
  rdoc_options: []
66
65
  require_paths:
67
66
  - lib
68
67
  required_ruby_version: !ruby/object:Gem::Requirement
69
- none: false
70
68
  requirements:
71
- - - ! '>='
69
+ - - ">="
72
70
  - !ruby/object:Gem::Version
73
71
  version: '0'
74
72
  required_rubygems_version: !ruby/object:Gem::Requirement
75
- none: false
76
73
  requirements:
77
- - - ! '>='
74
+ - - ">="
78
75
  - !ruby/object:Gem::Version
79
76
  version: '0'
80
77
  requirements: []
81
- rubyforge_project:
82
- rubygems_version: 1.8.24
83
- signing_key:
84
- specification_version: 3
78
+ rubygems_version: 3.3.7
79
+ signing_key:
80
+ specification_version: 4
85
81
  summary: A Struct that can read/write itself from/to IO-like objects
86
82
  test_files: []