bindata-contrib 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ffa7d572716ebd2fdb76833b6c35a338da122e7
4
- data.tar.gz: cccd0027c6c9edc49244bd34337619542e9e4c35
3
+ metadata.gz: 242796ae676a122c4e006baded203c96bf97b9c5
4
+ data.tar.gz: f99cc0e78fd9ac9ea04e7c353deb5d879e636b8b
5
5
  SHA512:
6
- metadata.gz: 490f62be5c3d49db8a77475bf81d9a6bcc0cd8435a3033af68e2e97ab3606dcb6c695d7c60961def6c2e66df0b81efd1af468bfe433d5251e794f585e786b397
7
- data.tar.gz: a34ab5f2a69f2494a31c0c5011a1a86951503ddbdeed63426791d80eba08a7ab250ed24b1b705b052d4a5ae63a364143b7f83d46c9178fc3b35355bac663641e
6
+ metadata.gz: 7fc673bf311e29c79333b7227855e4575d088e3c4422ea55128c7a336f9bb41a8281e47836b038469f7740e13791b237c175c7c0d8d5c90445a788cf653725b4
7
+ data.tar.gz: 5f3a8724905618f4d8a6dfe053ccfed48f6b362756c09378dea5af1d680b44db3101f6cf161fc452e8148b68e8bdad408705e2c5d330b420418a50925fa5ff36
@@ -1,6 +1,7 @@
1
- require_relative 'bool'
2
- require_relative 'enum'
3
- require_relative 'flags'
4
- require_relative 'mac_address'
5
- require_relative 'ipv4_address'
6
- require_relative 'ipv6_address'
1
+ require 'bindata-contrib/version'
2
+ require 'bindata-contrib/bool'
3
+ require 'bindata-contrib/enum'
4
+ require 'bindata-contrib/flags'
5
+ require 'bindata-contrib/mac_address'
6
+ require 'bindata-contrib/ipv4_address'
7
+ require 'bindata-contrib/ipv6_address'
@@ -0,0 +1,15 @@
1
+ require 'bindata'
2
+
3
+ module BinData
4
+ class Bool < Primitive
5
+ bit1 :bool, initial_value: 0
6
+
7
+ def get
8
+ bool == 1
9
+ end
10
+
11
+ def set(value)
12
+ self.bool = value ? 1 : 0
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,47 @@
1
+ require 'bindata'
2
+
3
+ def _def_enum(size)
4
+ eval %(
5
+ module BinData
6
+ class Enum#{size} < Primitive
7
+ mandatory_parameter :list
8
+
9
+ endian :big
10
+ uint#{size} :enum, initial_value: 0
11
+
12
+ def get
13
+ list.invert.fetch(enum)
14
+ rescue KeyError
15
+ enum
16
+ end
17
+
18
+ def set(value)
19
+ self.enum = list.fetch(value)
20
+ rescue KeyError
21
+ self.enum = value
22
+ end
23
+
24
+ private
25
+
26
+ def list
27
+ list = eval_parameter(:list)
28
+ case list.class.to_s
29
+ when 'Array'
30
+ shift = 0
31
+ list.each_with_object({}) do |each, result|
32
+ result[each] = shift
33
+ shift += 1
34
+ result
35
+ end
36
+ when 'Hash'
37
+ list
38
+ end
39
+ end
40
+ end
41
+ end
42
+ )
43
+ end
44
+
45
+ _def_enum 8
46
+ _def_enum 16
47
+ _def_enum 32
@@ -0,0 +1,51 @@
1
+ require 'bindata'
2
+
3
+ def _def_flags(size)
4
+ eval %(
5
+ module BinData
6
+ class Flags#{size} < Primitive
7
+ mandatory_parameter :list
8
+
9
+ endian :big
10
+ uint#{size} :flags, initial_value: 0
11
+
12
+ def get
13
+ list.each_with_object([]) do |(key, value), result|
14
+ result << key if (flags & value != 0) || (flags == value)
15
+ result
16
+ end
17
+ end
18
+
19
+ def set(value)
20
+ value.each do |each|
21
+ fail "Invalid flag: \#{value}" unless list.keys.include?(each)
22
+ end
23
+ self.flags = value.empty? ?
24
+ 0 :
25
+ value.map { |each| list[each] }.inject(:|)
26
+ end
27
+
28
+ private
29
+
30
+ def list
31
+ list = eval_parameter(:list)
32
+ case list.class.to_s
33
+ when 'Array'
34
+ shift = 0
35
+ list.each_with_object({}) do |each, result|
36
+ result[each] = 1 << shift
37
+ shift += 1
38
+ result
39
+ end
40
+ when 'Hash'
41
+ list
42
+ end
43
+ end
44
+ end
45
+ end
46
+ )
47
+ end
48
+
49
+ _def_flags 8
50
+ _def_flags 16
51
+ _def_flags 32
@@ -0,0 +1,16 @@
1
+ require 'bindata'
2
+
3
+ module BinData
4
+ class Ipv4Address < Primitive
5
+ endian :big
6
+ array :octets, type: :uint8, initial_length: 4
7
+
8
+ def get
9
+ octets.map(&:to_s).join('.')
10
+ end
11
+
12
+ def set(value)
13
+ self.octets = value.split('.').map(&:to_i)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'bindata'
2
+
3
+ module BinData
4
+ class Ipv6Address < Primitive
5
+ endian :big
6
+ array :octets, type: :uint16, initial_length: 8
7
+
8
+ def get
9
+ octets.map { |o| o.to_i.to_s(16) }.join(':')
10
+ end
11
+
12
+ def set(value)
13
+ self.octets = value.split(':').map { |o| o.to_i(16) }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'bindata'
2
+
3
+ module BinData
4
+ class MacAddress < Primitive
5
+ endian :big
6
+ array :octets, type: :uint8, initial_length: 6
7
+
8
+ def get
9
+ octets.map { |octet| format('%02x', octet) }.join(':')
10
+ end
11
+
12
+ def set(value)
13
+ self.octets = value.split(':').map(&:hex)
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  module BinData
2
2
  module Contrib
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
@@ -1,19 +1,17 @@
1
- require 'spec_helper'
2
-
3
- describe Bool do
1
+ describe BinData::Bool do
4
2
  it 'should read binary' do
5
- bool = Bool.read [0x80].pack('C*')
3
+ bool = BinData::Bool.read [0x80].pack('C*')
6
4
  expect(bool).to eq(true)
7
5
 
8
- bool = Bool.read [0].pack('C*')
6
+ bool = BinData::Bool.read [0].pack('C*')
9
7
  expect(bool).to eq(false)
10
8
  end
11
9
  it 'should initialize with default values' do
12
- bool = Bool.new
10
+ bool = BinData::Bool.new
13
11
  expect(bool).to eq(false)
14
12
  end
15
13
  it 'should initialize with some values' do
16
- bool = Bool.new(true)
14
+ bool = BinData::Bool.new(true)
17
15
  expect(bool).to eq(true)
18
16
  end
19
17
  end
@@ -1,6 +1,4 @@
1
- require 'spec_helper'
2
-
3
- describe 'Enum8, Enum16, Enum32' do
1
+ describe 'BinData::Enum8, BinData::Enum16, BinData::Enum32' do
4
2
  before(:all) do
5
3
  class MyClass < BinData::Record
6
4
  endian :big
@@ -1,8 +1,6 @@
1
- require 'spec_helper'
2
-
3
- describe 'Flags8, Flags16, Flags32' do
1
+ describe 'BinData::Flags8, BinData::Flags16, BinData::Flags32' do
4
2
  before(:all) do
5
- class MyClass < BinData::Record
3
+ class MyClass2 < BinData::Record
6
4
  endian :big
7
5
  flags8 :flags8, list: [:a, :b, :c]
8
6
  flags16 :flags16, list: [:d, :e, :f]
@@ -11,7 +9,7 @@ describe 'Flags8, Flags16, Flags32' do
11
9
  end
12
10
 
13
11
  it 'should read binary' do
14
- cls = MyClass.read [
12
+ cls = MyClass2.read [
15
13
  3,
16
14
  0, 4,
17
15
  0, 0, 0, 7
@@ -21,13 +19,13 @@ describe 'Flags8, Flags16, Flags32' do
21
19
  expect(cls.flags32).to eq([:g, :h, :i])
22
20
  end
23
21
  it 'should initialize with default values' do
24
- cls = MyClass.new
22
+ cls = MyClass2.new
25
23
  expect(cls.flags8).to be_empty
26
24
  expect(cls.flags16).to be_empty
27
25
  expect(cls.flags32).to be_empty
28
26
  end
29
27
  it 'should initialize with some values' do
30
- cls = MyClass.new(
28
+ cls = MyClass2.new(
31
29
  flags8: [:c],
32
30
  flags16: [:d, :e]
33
31
  )
@@ -1,16 +1,14 @@
1
- require 'spec_helper'
2
-
3
- describe Ipv4Address do
1
+ describe BinData::Ipv4Address do
4
2
  it 'should read binary' do
5
- addr = Ipv4Address.read [127, 0, 0, 1].pack('C*')
3
+ addr = BinData::Ipv4Address.read [127, 0, 0, 1].pack('C*')
6
4
  expect(addr).to eq('127.0.0.1')
7
5
  end
8
6
  it 'should initialize with default values' do
9
- addr = Ipv4Address.new
7
+ addr = BinData::Ipv4Address.new
10
8
  expect(addr).to eq('0.0.0.0')
11
9
  end
12
10
  it 'should initialize with some values' do
13
- addr = Ipv4Address.new('192.168.1.2')
11
+ addr = BinData::Ipv4Address.new('192.168.1.2')
14
12
  expect(addr).to eq('192.168.1.2')
15
13
  end
16
14
  end
@@ -1,8 +1,6 @@
1
- require 'spec_helper'
2
-
3
- describe Ipv6Address do
1
+ describe BinData::Ipv6Address do
4
2
  it 'should read binary' do
5
- addr = Ipv6Address.read [
3
+ addr = BinData::Ipv6Address.read [
6
4
  0x20, 0x01,
7
5
  0x0d, 0xb8,
8
6
  0x85, 0xa3,
@@ -15,11 +13,11 @@ describe Ipv6Address do
15
13
  expect(addr).to eq('2001:db8:85a3:0:0:8a2e:370:7334')
16
14
  end
17
15
  it 'should initialize with default values' do
18
- addr = Ipv6Address.new
16
+ addr = BinData::Ipv6Address.new
19
17
  expect(addr).to eq('0:0:0:0:0:0:0:0')
20
18
  end
21
19
  it 'should initialize with some values' do
22
- addr = Ipv6Address.new('2001:db8:85a3:0:0:8a2e:370:7334')
20
+ addr = BinData::Ipv6Address.new('2001:db8:85a3:0:0:8a2e:370:7334')
23
21
  expect(addr).to eq('2001:db8:85a3:0:0:8a2e:370:7334')
24
22
  end
25
23
  end
@@ -1,16 +1,14 @@
1
- require 'spec_helper'
2
-
3
- describe MacAddress do
1
+ describe BinData::MacAddress do
4
2
  it 'should read binary' do
5
- addr = MacAddress.read [0x01, 0x23, 0x45, 0x67, 0x89, 0xab].pack('C*')
3
+ addr = BinData::MacAddress.read [0x01, 0x23, 0x45, 0x67, 0x89, 0xab].pack('C*')
6
4
  expect(addr).to eq('01:23:45:67:89:ab')
7
5
  end
8
6
  it 'should initialize with default values' do
9
- addr = MacAddress.new
7
+ addr = BinData::MacAddress.new
10
8
  expect(addr).to eq('00:00:00:00:00:00')
11
9
  end
12
10
  it 'should initialize with some values' do
13
- addr = MacAddress.new('01:23:45:67:89:ab')
11
+ addr = BinData::MacAddress.new('01:23:45:67:89:ab')
14
12
  expect(addr).to eq('01:23:45:67:89:ab')
15
13
  end
16
14
  end
@@ -1 +1 @@
1
- require_relative '../lib/bindata-contrib'
1
+ require 'bindata-contrib'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bindata-contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jérémy Pagé
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-11 00:00:00.000000000 Z
11
+ date: 2016-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bindata
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.1'
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.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.4'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -46,13 +60,13 @@ extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
62
  - lib/bindata-contrib.rb
63
+ - lib/bindata-contrib/bool.rb
64
+ - lib/bindata-contrib/enum.rb
65
+ - lib/bindata-contrib/flags.rb
66
+ - lib/bindata-contrib/ipv4_address.rb
67
+ - lib/bindata-contrib/ipv6_address.rb
68
+ - lib/bindata-contrib/mac_address.rb
49
69
  - lib/bindata-contrib/version.rb
50
- - lib/bool.rb
51
- - lib/enum.rb
52
- - lib/flags.rb
53
- - lib/ipv4_address.rb
54
- - lib/ipv6_address.rb
55
- - lib/mac_address.rb
56
70
  - spec/bool_spec.rb
57
71
  - spec/enum_spec.rb
58
72
  - spec/flags_spec.rb
@@ -1,13 +0,0 @@
1
- require 'bindata'
2
-
3
- class Bool < BinData::Primitive
4
- bit1 :bool, initial_value: 0
5
-
6
- def get
7
- bool == 1
8
- end
9
-
10
- def set(value)
11
- self.bool = value ? 1 : 0
12
- end
13
- end
@@ -1,45 +0,0 @@
1
- require 'bindata'
2
-
3
- def _def_enum(size)
4
- eval %(
5
- class Enum#{size} < BinData::Primitive
6
- mandatory_parameter :list
7
-
8
- endian :big
9
- uint#{size} :enum, initial_value: 0
10
-
11
- def get
12
- list.invert.fetch(enum)
13
- rescue KeyError
14
- enum
15
- end
16
-
17
- def set(value)
18
- self.enum = list.fetch(value)
19
- rescue KeyError
20
- self.enum = value
21
- end
22
-
23
- private
24
-
25
- def list
26
- list = eval_parameter(:list)
27
- case list
28
- when Array
29
- shift = 0
30
- list.each_with_object({}) do |each, result|
31
- result[each] = shift
32
- shift += 1
33
- result
34
- end
35
- when Hash
36
- list
37
- end
38
- end
39
- end
40
- )
41
- end
42
-
43
- _def_enum 8
44
- _def_enum 16
45
- _def_enum 32
@@ -1,49 +0,0 @@
1
- require 'bindata'
2
-
3
- def _def_flags(size)
4
- eval %(
5
- class Flags#{size} < BinData::Primitive
6
- mandatory_parameter :list
7
-
8
- endian :big
9
- uint#{size} :flags, initial_value: 0
10
-
11
- def get
12
- list.each_with_object([]) do |(key, value), result|
13
- result << key if (flags & value != 0) || (flags == value)
14
- result
15
- end
16
- end
17
-
18
- def set(value)
19
- value.each do |each|
20
- fail "Invalid flag: \#{value}" unless list.keys.include?(each)
21
- end
22
- self.flags = value.empty? ?
23
- 0 :
24
- value.map { |each| list[each] }.inject(:|)
25
- end
26
-
27
- private
28
-
29
- def list
30
- list = eval_parameter(:list)
31
- case list
32
- when Array
33
- shift = 0
34
- list.each_with_object({}) do |each, result|
35
- result[each] = 1 << shift
36
- shift += 1
37
- result
38
- end
39
- when Hash
40
- list
41
- end
42
- end
43
- end
44
- )
45
- end
46
-
47
- _def_flags 8
48
- _def_flags 16
49
- _def_flags 32
@@ -1,14 +0,0 @@
1
- require 'bindata'
2
-
3
- class Ipv4Address < BinData::Primitive
4
- endian :big
5
- array :octets, type: :uint8, initial_length: 4
6
-
7
- def get
8
- octets.map(&:to_s).join('.')
9
- end
10
-
11
- def set(value)
12
- self.octets = value.split('.').map(&:to_i)
13
- end
14
- end
@@ -1,14 +0,0 @@
1
- require 'bindata'
2
-
3
- class Ipv6Address < BinData::Primitive
4
- endian :big
5
- array :octets, type: :uint16, initial_length: 8
6
-
7
- def get
8
- octets.map { |o| o.to_i.to_s(16) }.join(':')
9
- end
10
-
11
- def set(value)
12
- self.octets = value.split(':').map { |o| o.to_i(16) }
13
- end
14
- end
@@ -1,14 +0,0 @@
1
- require 'bindata'
2
-
3
- class MacAddress < BinData::Primitive
4
- endian :big
5
- array :octets, type: :uint8, initial_length: 6
6
-
7
- def get
8
- octets.map { |octet| format('%02x', octet) }.join(':')
9
- end
10
-
11
- def set(value)
12
- self.octets = value.split(':').map(&:hex)
13
- end
14
- end