rasn1 0.6.5 → 0.6.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +29 -0
- data/lib/rasn1/types/enumerated.rb +2 -84
- data/lib/rasn1/types/integer.rb +100 -5
- data/lib/rasn1/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4acb42db867027d85bfd208aaaee5906af7fe4fdcd92327055f66187415e959f
|
4
|
+
data.tar.gz: 52cbb1f7306006fb0c03e3e00911d52a474a78c994e2a8e5618046356de43117
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce57ddfbbed465bce5fefb9c7c8b91593e09e4a9cc3797e467e5eb0f482964f1f2bbed3a9053da1f590fddb18e846a1a608351594d667459f59a0f5218294480
|
7
|
+
data.tar.gz: 93943756f8b526d6467d834976eda477bacd777565d4d46723c050aab5e909b78c5e3f74539999d0cf3ab3c60f316582fc8060814003936df02c09e78b4d7308
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
TargetRubyVersion: 2.3
|
2
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
3
|
+
EnforcedStyle: no_space
|
4
|
+
Lint/EmptyWhen:
|
5
|
+
Enabled: false
|
6
|
+
Lint/Void:
|
7
|
+
Enabled: false
|
8
|
+
Metrics:
|
9
|
+
Enabled: false
|
10
|
+
Style/AsciiComments:
|
11
|
+
Enabled: false
|
12
|
+
Style/Encoding:
|
13
|
+
Enabled: false
|
14
|
+
Style/EvalWithLocation:
|
15
|
+
Enabled: false
|
16
|
+
Style/FormatString:
|
17
|
+
EnforcedStyle: percent
|
18
|
+
Style/FormatStringToken:
|
19
|
+
EnforcedStyle: unannotated
|
20
|
+
Style/PerlBackrefs:
|
21
|
+
Enabled: false
|
22
|
+
Style/RedundantSelf:
|
23
|
+
Enabled: false
|
24
|
+
Style/StructInheritance:
|
25
|
+
Enabled: false
|
26
|
+
Style/TrailingCommaInArrayLiteral:
|
27
|
+
Enabled: false
|
28
|
+
Style/TrailingCommaInHashLiteral:
|
29
|
+
Enabled: false
|
@@ -18,8 +18,8 @@ module RASN1
|
|
18
18
|
# A {EnumeratedError} is raised when set value is not in enumeration.
|
19
19
|
# @author Sylvain Daubert
|
20
20
|
class Enumerated < Integer
|
21
|
+
# @!attribute enum
|
21
22
|
# @return [Hash]
|
22
|
-
attr_reader :enum
|
23
23
|
|
24
24
|
TAG = 0x0a
|
25
25
|
|
@@ -37,95 +37,13 @@ module RASN1
|
|
37
37
|
# @see Base#initialize common options to all ASN.1 types
|
38
38
|
def initialize(value_or_options={}, options={})
|
39
39
|
super
|
40
|
-
|
41
|
-
|
42
|
-
raise EnumeratedError, 'no enumeration given' unless opts.has_key? :enum
|
43
|
-
@enum = opts[:enum]
|
44
|
-
|
45
|
-
case @default
|
46
|
-
when String,Symbol
|
47
|
-
unless @enum.has_key? @default
|
48
|
-
raise EnumeratedError, "TAG #@name: unknwon enumerated default value #@default"
|
49
|
-
end
|
50
|
-
when ::Integer
|
51
|
-
if @enum.has_value? @default
|
52
|
-
@default = @enum.key(@default)
|
53
|
-
else
|
54
|
-
raise EnumeratedError, "TAG #@name: default value #@defalt not in enumeration"
|
55
|
-
end
|
56
|
-
when nil
|
57
|
-
else
|
58
|
-
raise TypeError, "TAG #@name: #{@value.class} not handled as default value"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
# @param [Integer,String,Symbol,nil] v
|
63
|
-
# @return [String,Symbol,nil]
|
64
|
-
def value=(v)
|
65
|
-
case v
|
66
|
-
when String,Symbol
|
67
|
-
unless @enum.has_key? v
|
68
|
-
raise EnumeratedError, "TAG #@name: unknwon enumerated value #{v}"
|
69
|
-
end
|
70
|
-
@value = v
|
71
|
-
when ::Integer
|
72
|
-
unless @enum.has_value? v
|
73
|
-
raise EnumeratedError, "TAG #@name: #{v} not in enumeration"
|
74
|
-
end
|
75
|
-
@value = @enum.key(v)
|
76
|
-
when nil
|
77
|
-
@value = nil
|
78
|
-
else
|
79
|
-
raise EnumeratedError, "TAG #@name: not in enumeration"
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
# @return [Integer]
|
84
|
-
def to_i
|
85
|
-
case @value
|
86
|
-
when String, Symbol
|
87
|
-
@enum[@value]
|
88
|
-
when ::Integer
|
89
|
-
super
|
90
|
-
when nil
|
91
|
-
if @default
|
92
|
-
@enum[@default]
|
93
|
-
else
|
94
|
-
0
|
95
|
-
end
|
96
|
-
else
|
97
|
-
raise TypeError, "TAG #@name: #{@value.class} not handled"
|
98
|
-
end
|
40
|
+
raise EnumeratedError, 'no enumeration given' if @enum.nil?
|
99
41
|
end
|
100
42
|
|
101
43
|
# @return [Hash]
|
102
44
|
def to_h
|
103
45
|
@enum
|
104
46
|
end
|
105
|
-
|
106
|
-
private
|
107
|
-
|
108
|
-
def value_to_der
|
109
|
-
case @value
|
110
|
-
when String, Symbol
|
111
|
-
super @enum[@value]
|
112
|
-
when ::Integer
|
113
|
-
super
|
114
|
-
else
|
115
|
-
raise TypeError, "TAG #@name: #{@value.class} not handled"
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
def der_to_value(der, ber:false)
|
120
|
-
super
|
121
|
-
v = @value
|
122
|
-
@value = @enum.key(v)
|
123
|
-
raise EnumeratedError, "TAG #@name: value #{v} not in enumeration" if @value.nil?
|
124
|
-
end
|
125
|
-
|
126
|
-
def explicit_type
|
127
|
-
self.class.new(@name, enum: self.enum)
|
128
|
-
end
|
129
47
|
end
|
130
48
|
end
|
131
49
|
end
|
data/lib/rasn1/types/integer.rb
CHANGED
@@ -4,16 +4,87 @@ module RASN1
|
|
4
4
|
# ASN.1 Integer
|
5
5
|
# @author Sylvain Daubert
|
6
6
|
class Integer < Primitive
|
7
|
+
# @return [Hash,nil]
|
8
|
+
attr_reader :enum
|
9
|
+
|
7
10
|
TAG = 0x02
|
8
11
|
|
12
|
+
# @overload initialize(options={})
|
13
|
+
# @option options [Hash] :enum enumeration hash. Keys are names, and values
|
14
|
+
# are integers.
|
15
|
+
# @raise [EnumeratedError] +:default+ value is unknown when +:enum+ key is present
|
16
|
+
# @overload initialize(value, options={})
|
17
|
+
# @param [Object] value value to set for this ASN.1 object
|
18
|
+
# @option options [Hash] :enum enumeration hash. Keys are names, and values
|
19
|
+
# are integers. This key is mandatory.
|
20
|
+
# @raise [EnumeratedError] +:default+ value is unknown when +:enum+ key is present
|
21
|
+
# @see Base#initialize common options to all ASN.1 types
|
22
|
+
def initialize(value_or_options={}, options={})
|
23
|
+
super
|
24
|
+
opts = value_or_options.is_a?(Hash) ? value_or_options : options
|
25
|
+
@enum = opts[:enum]
|
26
|
+
|
27
|
+
return if @enum.nil?
|
28
|
+
|
29
|
+
# To ensure @value has the correct type
|
30
|
+
self.value = @value
|
31
|
+
|
32
|
+
case @default
|
33
|
+
when String,Symbol
|
34
|
+
unless @enum.has_key? @default
|
35
|
+
raise EnumeratedError, "TAG #@name: unknwon enumerated default value #@default"
|
36
|
+
end
|
37
|
+
when ::Integer
|
38
|
+
if @enum.has_value? @default
|
39
|
+
@default = @enum.key(@default)
|
40
|
+
else
|
41
|
+
raise EnumeratedError, "TAG #@name: default value #@default not in enumeration"
|
42
|
+
end
|
43
|
+
when nil
|
44
|
+
else
|
45
|
+
raise TypeError, "TAG #@name: #{@default.class} not handled as default value"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# @param [Integer,String,Symbol,nil] v
|
50
|
+
# @return [String,Symbol,nil]
|
51
|
+
def value=(v)
|
52
|
+
case v
|
53
|
+
when String,Symbol
|
54
|
+
raise EnumeratedError, 'TAG #@name has no :enum' if @enum.nil?
|
55
|
+
|
56
|
+
unless @enum.has_key? v
|
57
|
+
raise EnumeratedError, "TAG #@name: unknwon enumerated value #{v}"
|
58
|
+
end
|
59
|
+
@value = v
|
60
|
+
when ::Integer
|
61
|
+
if @enum.nil?
|
62
|
+
@value = v
|
63
|
+
elsif @enum.has_value? v
|
64
|
+
@value = @enum.key(v)
|
65
|
+
else
|
66
|
+
raise EnumeratedError, "TAG #@name: #{v} not in enumeration"
|
67
|
+
end
|
68
|
+
when nil
|
69
|
+
@value = nil
|
70
|
+
else
|
71
|
+
raise EnumeratedError, "TAG #@name: not in enumeration"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Integer value
|
9
76
|
# @return [Integer]
|
10
77
|
def to_i
|
11
|
-
|
78
|
+
if @enum.nil?
|
79
|
+
@value || @default || 0
|
80
|
+
else
|
81
|
+
@enum[@value || @default] || 0
|
82
|
+
end
|
12
83
|
end
|
13
84
|
|
14
85
|
private
|
15
86
|
|
16
|
-
def
|
87
|
+
def int_value_to_der(value=nil)
|
17
88
|
v = value || @value
|
18
89
|
size = v.bit_length / 8 + (v.bit_length % 8 > 0 ? 1 : 0)
|
19
90
|
size = 1 if size == 0
|
@@ -30,12 +101,36 @@ module RASN1
|
|
30
101
|
ary.reverse.pack('C*')
|
31
102
|
end
|
32
103
|
|
33
|
-
def
|
104
|
+
def value_to_der
|
105
|
+
case @value
|
106
|
+
when String, Symbol
|
107
|
+
int_value_to_der @enum[@value]
|
108
|
+
when ::Integer
|
109
|
+
int_value_to_der
|
110
|
+
else
|
111
|
+
raise TypeError, "TAG #@name: #{@value.class} not handled"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def der_to_int_value(der, ber: false)
|
34
116
|
ary = der.unpack('C*')
|
35
|
-
|
117
|
+
v = ary.reduce(0) { |len, b| (len << 8) | b }
|
36
118
|
if ary[0] & 0x80 == 0x80
|
37
|
-
|
119
|
+
v = -((~v & ((1 << v.bit_length) - 1)) + 1)
|
38
120
|
end
|
121
|
+
v
|
122
|
+
end
|
123
|
+
|
124
|
+
def der_to_value(der, ber: false)
|
125
|
+
@value = der_to_int_value(der, ber: ber)
|
126
|
+
return if @enum.nil?
|
127
|
+
|
128
|
+
@value = @enum.key(@value)
|
129
|
+
raise EnumeratedError, "TAG #@name: value #{v} not in enumeration" if @value.nil?
|
130
|
+
end
|
131
|
+
|
132
|
+
def explicit_type
|
133
|
+
@enum.nil? ? self.class.new(@name) : self.class.new(@name, enum: @enum)
|
39
134
|
end
|
40
135
|
end
|
41
136
|
end
|
data/lib/rasn1/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rasn1
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sylvain Daubert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard
|
@@ -91,6 +91,7 @@ extensions: []
|
|
91
91
|
extra_rdoc_files: []
|
92
92
|
files:
|
93
93
|
- ".gitignore"
|
94
|
+
- ".rubocop.yml"
|
94
95
|
- ".travis.yml"
|
95
96
|
- Gemfile
|
96
97
|
- LICENSE
|