bindata-contrib 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: f24245c18ea4e764422c520309e2d492ce57f595
4
+ data.tar.gz: f36c9fbcebd5bce5e8414ff2eaf8f82880fe65bf
5
+ SHA512:
6
+ metadata.gz: 00bba4729d535d5f85dc7d19e9e3a3da06ac6fb5026a98431965b2f5f9b190ce716e2669e3b23c0ab14a6a9f5b806df3f9cf0671573cae62779a04f7df695c82
7
+ data.tar.gz: 2dc5d7831a7de89358007aafcc0c4fe5f889e2d040f44e0200ed0ed45df2ae03c2aa3bb7408ac796603c2954c455f77264697a3baa9542eb3987edd7507b4a4c
@@ -0,0 +1,3 @@
1
+ require_relative 'bool'
2
+ require_relative 'enum'
3
+ require_relative 'flags'
data/lib/bool.rb ADDED
@@ -0,0 +1,13 @@
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
data/lib/enum.rb ADDED
@@ -0,0 +1,45 @@
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
data/lib/flags.rb ADDED
@@ -0,0 +1,49 @@
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
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bindata-contrib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jérémy Pagé
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Contribution to BinData.
14
+ email:
15
+ - contact@jeremypage.me
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/bindata-contrib.rb
21
+ - lib/bool.rb
22
+ - lib/enum.rb
23
+ - lib/flags.rb
24
+ homepage: https://github.com/jejepage/bindata-contrib
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.4.8
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: BinData Contrub
48
+ test_files: []