hash-ber-tlv 1.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.
- data/lib/hash-ber-tlv.rb +66 -0
- metadata +64 -0
data/lib/hash-ber-tlv.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
class Hash
|
2
|
+
def from_ber_tlv(data)
|
3
|
+
i = 0
|
4
|
+
tag = data[i]
|
5
|
+
if tag.unpack('C').pop & 0x1F == 0x1F # matches 00011111 mask?
|
6
|
+
begin
|
7
|
+
i += 1
|
8
|
+
tag[i] = data[i]
|
9
|
+
end while tag[i].unpack('C').pop & 0x80 == 0x80
|
10
|
+
end
|
11
|
+
|
12
|
+
length = data[tag.length].unpack('C').pop
|
13
|
+
|
14
|
+
tag_i = tag.unpack('H*').pop.to_i(16)
|
15
|
+
|
16
|
+
if length < 0x80
|
17
|
+
self[tag_i] = data[tag.length + 1, length]
|
18
|
+
remainder = data[tag.length + 1 + length..-1]
|
19
|
+
else
|
20
|
+
num_octets = length & 0x7f
|
21
|
+
octets = data[tag.length + 1, num_octets].unpack('C*')
|
22
|
+
length = 0
|
23
|
+
octets.each_with_index do |octet, idx|
|
24
|
+
length += octet * 255 ** (num_octets - idx - 1)
|
25
|
+
end
|
26
|
+
|
27
|
+
self[tag_i] = data[tag.length + 1 + num_octets, length]
|
28
|
+
remainder = data[tag.length + 1 + num_octets + length..-1]
|
29
|
+
end
|
30
|
+
|
31
|
+
if remainder.length > 2
|
32
|
+
self.from_ber_tlv(remainder)
|
33
|
+
end
|
34
|
+
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_ber_tlv
|
39
|
+
result = ''
|
40
|
+
self.each do |tag, value|
|
41
|
+
result << int_to_binstr(tag) # adding tag to result string
|
42
|
+
if value.length < 0x80
|
43
|
+
result << int_to_binstr(value.length)
|
44
|
+
else
|
45
|
+
#calculate length of length :)
|
46
|
+
len_len = int_to_binstr(value.length)
|
47
|
+
result << int_to_binstr(0x80 | len_len.length)
|
48
|
+
result << len_len
|
49
|
+
end
|
50
|
+
|
51
|
+
result << value
|
52
|
+
end
|
53
|
+
|
54
|
+
result
|
55
|
+
end
|
56
|
+
|
57
|
+
# converts integer to binary string representation
|
58
|
+
def int_to_binstr(i)
|
59
|
+
binstr = i.to_s(16)
|
60
|
+
if binstr.length % 2 == 1
|
61
|
+
binstr = '0' << binstr
|
62
|
+
end
|
63
|
+
|
64
|
+
[binstr].pack('H*')
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hash-ber-tlv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 1.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Maxim Chechel
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-10-22 00:00:00 +04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Extend Hash class with ability to parse and convert BER TLV format
|
22
|
+
email: maximchick@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/hash-ber-tlv.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://rubygems.org/gems/hash-ber-tlv
|
33
|
+
licenses: []
|
34
|
+
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.3.7
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: ""
|
63
|
+
test_files: []
|
64
|
+
|