ber 0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b529c78f22f9ef52acf218c052a848d9df4a9593
4
+ data.tar.gz: 9a9a07871c5ebe2a2cf22e1adb60c02f0d00c771
5
+ SHA512:
6
+ metadata.gz: 014c806ded70f21fc9d6330a76a8a803fc8f1ae710982b30d0dc62a404cf4e7ce9e894b8180e6c48cbe819da4bc6f36090c6ce6ec2bfb6a3b391faee3a17244a
7
+ data.tar.gz: 0d3d28f4ecd4cd0a0bfa1752a7828d246cd31a74bc4e60f9e4b111c0bfb2da008672c819eb9849d0b4ba5cb3c3e9443cbe7df8e54c87754946e6b97d835f9148
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_STORE
19
+ .ruby-version
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - rbx-19mode
6
+ - jruby-19mode
7
+ script: "bundle exec rspec"
8
+ notifications:
9
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ber.rb.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Arcath
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ # BER [![Build Status](https://travis-ci.org/Arcath/BER.png?branch=master)](https://travis-ci.org/Arcath/BER)
2
+
3
+ Implementation of Basic Encoding Rules (BER) in ruby
4
+
5
+ ## Installation
6
+
7
+ Install via Rubygems:
8
+
9
+ $ gem install ber
10
+
11
+ Or add as a dependency to your gem
12
+
13
+ ## Usage
14
+
15
+ Adds a `to_ber` to the supported classes e.g.
16
+
17
+ "Foo".to_ber
18
+ >> "\x04\x03foo"
19
+
20
+ Which in BER means that the supplied data is a string of length 3.
21
+
22
+ BER extends all the supported classes so all you need to do is require it somewhere in your gem.
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ber/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ber"
8
+ gem.version = Ber::VERSION
9
+ gem.authors = ["Arcath"]
10
+ gem.email = ["adam@arcath.net"]
11
+ gem.description = %q{An implementation of BER (Basic Encoding Rules) in Ruby}
12
+ gem.summary = %q{An implementation of BER (Basic Encoding Rules) in Ruby}
13
+ gem.homepage = "https://github.com/Arcath/BER"
14
+
15
+ gem.add_development_dependency "rspec"
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,61 @@
1
+ require "ber/version"
2
+
3
+ require "ber/array"
4
+ require "ber/bignum"
5
+ require "ber/boolean"
6
+ require "ber/fixnum"
7
+ require "ber/string"
8
+
9
+ module Ber
10
+ Zero = [0].pack('C')
11
+ Identifiers = {
12
+ end_of_content: 0x0,
13
+ boolean: 0x1,
14
+ fixnum: 0x2,
15
+ bit_string: 0x3,
16
+ ocet_string: 0x4,
17
+ null: 0x5,
18
+ array: 0x30,
19
+
20
+ # Quick shorthands
21
+ string: 4
22
+ }
23
+
24
+ def self.decode(input)
25
+ idents = Identifiers.select { |key, value| value == input.getbyte(0) }
26
+ identifier = idents.first.first
27
+ length = input.getbyte(1)
28
+ if length >= 127
29
+ length = input.length - (length - 126)
30
+ end
31
+ value = input[(input.length - length)..input.length]
32
+ if identifier == :fixnum
33
+ if length == 1
34
+ value = value.unpack('C').first
35
+ else
36
+ if value.length < 4
37
+ padded_value = "#{Zero * (4 - value.length)}#{value}"
38
+ value = padded_value.unpack('N').first
39
+ else
40
+ hex_total = ""
41
+ value.split("").each do |char|
42
+ hex = char.ord.to_s(16)
43
+ hex_total << "0" if hex.length == 1
44
+ hex_total << hex
45
+ end
46
+ value = hex_total.to_i(16)
47
+ end
48
+ end
49
+ elsif identifier == :boolean
50
+ value = (value == Zero ? false : true)
51
+ elsif identifier == :array
52
+ values = []
53
+ found = value.scan(/(..[A-z]*)/) # This WILL NOT work for Bignums or strings of over 127 in length TODO: Fix this
54
+ found.each do |v|
55
+ values << Ber.decode(v.first)[:value]
56
+ end
57
+ value = values
58
+ end
59
+ {identifier: identifier, length: length, value: value}
60
+ end
61
+ end
@@ -0,0 +1,34 @@
1
+ module Ber
2
+ module Array
3
+ def to_ber
4
+ array = (self.ber? ? self : self.berable).join
5
+ "#{[Ber::Identifiers[:array]].pack('C')}#{[array.length].pack('C')}#{array}"
6
+ end
7
+
8
+ def berable
9
+ self.map { |x| x.to_ber }
10
+ end
11
+
12
+ def berable!
13
+ self.map! { |x| x.to_ber }
14
+ end
15
+
16
+ def ber?
17
+ ber = true
18
+ self.each do |i|
19
+ if ber
20
+ if i.class == "".class
21
+ ber = i.ber?
22
+ else
23
+ ber = false
24
+ end
25
+ end
26
+ end
27
+ return ber
28
+ end
29
+ end
30
+ end
31
+
32
+ class Array
33
+ include Ber::Array
34
+ end
@@ -0,0 +1,26 @@
1
+ module Ber
2
+ module Bignum
3
+ ChunkSize = 0.size
4
+
5
+ def to_ber
6
+ "#{[Ber::Identifiers[:fixnum]].pack('C')}#{encoded_self}"
7
+ end
8
+
9
+ private
10
+
11
+ def encoded_self
12
+ ber_blocks = []
13
+ i = self
14
+ while i > 0
15
+ block = i & 0xff
16
+ ber_blocks.push block
17
+ i = i >> ChunkSize
18
+ end
19
+ ([ber_blocks.size] + ber_blocks.reverse).pack('C*')
20
+ end
21
+ end
22
+ end
23
+
24
+ class Bignum
25
+ include Ber::Bignum
26
+ end
@@ -0,0 +1,22 @@
1
+ module Ber
2
+ module Boolean
3
+ def to_ber
4
+ "#{[Ber::Identifiers[:boolean]].pack('C')}#{encoded_self}"
5
+ end
6
+
7
+ private
8
+
9
+ def encoded_self
10
+ encoded = (self ? [1].pack('C') : [0].pack('C'))
11
+ [1].pack('C') + encoded
12
+ end
13
+ end
14
+ end
15
+
16
+ class TrueClass
17
+ include Ber::Boolean
18
+ end
19
+
20
+ class FalseClass
21
+ include Ber::Boolean
22
+ end
@@ -0,0 +1,28 @@
1
+ module Ber
2
+ module Fixnum
3
+ ChunkSize = 0.size
4
+
5
+ def to_ber
6
+ "#{[Ber::Identifiers[:fixnum]].pack('C')}#{encoded_self}"
7
+ end
8
+
9
+ private
10
+
11
+ def encoded_self
12
+ if self < (2 ** ChunkSize)
13
+ code = [self].pack('C')
14
+ else
15
+ code32 = [self].pack('N')
16
+ code = ""
17
+ code32.split("").each do |char|
18
+ code += char if char != [0].pack('C')
19
+ end
20
+ end
21
+ [code.length].pack('C') + code
22
+ end
23
+ end
24
+ end
25
+
26
+ class Fixnum
27
+ include Ber::Fixnum
28
+ end
@@ -0,0 +1,26 @@
1
+ module Ber
2
+ module String
3
+ def to_ber(identifier = :string)
4
+ "#{[Ber::Identifiers[identifier]].pack('C')}#{encoded_length}#{self}"
5
+ end
6
+
7
+ def ber?
8
+ self[0].ord < 30
9
+ end
10
+
11
+ private
12
+
13
+ def encoded_length
14
+ if length <= 127
15
+ [length].pack('C')
16
+ else
17
+ i = [length].pack('N').sub(/^[\0]+/,"")
18
+ [0x80 + i.length].pack('C') + i
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ class String
25
+ include Ber::String
26
+ end
@@ -0,0 +1,3 @@
1
+ module Ber
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ber::Array do
4
+ it "should make everything in the array \"berable\"" do
5
+ a = ["foo", "bar"]
6
+ a.berable.should eq ["foo".to_ber, "bar".to_ber]
7
+ a.should eq ["foo", "bar"]
8
+ a.berable!
9
+ a.should eq ["foo".to_ber, "bar".to_ber]
10
+ end
11
+
12
+ it "should be able to check if it is ber" do
13
+ ["foo", "bar"].ber?.should be_false
14
+ ["foo".to_ber, "bar".to_ber].ber?.should be_true
15
+ ["foo".to_ber, "bar"].ber?.should be_false
16
+ end
17
+
18
+ it "should be able to convert into ber" do
19
+ ["foo", "bar"].to_ber.should eq "0\n\x04\x03foo\x04\x03bar"
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ber::Bignum do
4
+ it "should encode a bignum" do
5
+ (2 ** 64).class.should be Bignum
6
+ (2 ** 64).to_ber.should eq "\u0002\t\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000"
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ber::Boolean do
4
+ it "should encode a true value" do
5
+ true.to_ber.should eq "\001\001\001"
6
+ end
7
+
8
+ it "should encode a false value" do
9
+ false.to_ber.should eq "\001\001\000"
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ber::Fixnum do
4
+ it "should have a Chunk Size defined" do
5
+ Ber::Fixnum::ChunkSize.should be_a Fixnum
6
+ end
7
+
8
+ it "should extend fixnum with to_ber" do
9
+ 10.to_ber
10
+ end
11
+
12
+ it "should encode a small number (less than #{Ber::Fixnum::ChunkSize} bits long (less than #{(2 ** Ber::Fixnum::ChunkSize) - 1}))" do
13
+ 15.to_ber.should eq "\u0002\u0001\u000F"
14
+ end
15
+
16
+ it "should encode a large number (more than #{Ber::Fixnum::ChunkSize} bits long (more than #{(2 ** Ber::Fixnum::ChunkSize)}))" do
17
+ 257.to_ber.should eq "\u0002\u0002\u0001\u0001"
18
+ end
19
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ber do
4
+ it "should have a version" do
5
+ Ber::VERSION.should be_a String
6
+ end
7
+
8
+ it "should have a hash of identifiers" do
9
+ Ber::Identifiers.should be_a Hash
10
+ end
11
+
12
+ it "should be able to decode a string" do
13
+ string = "foo".to_ber
14
+ decoded = Ber.decode(string)
15
+ decoded.should be_a Hash
16
+ decoded[:identifier].should eq :ocet_string
17
+ decoded[:length].should eq 3
18
+ decoded[:value].should eq "foo"
19
+ end
20
+
21
+ it "should be able to decode a long_string" do
22
+ string = long_string.to_ber
23
+ decoded = Ber.decode(string)
24
+ decoded.should be_a Hash
25
+ decoded[:identifier].should eq :ocet_string
26
+ decoded[:length].should eq long_string.length
27
+ decoded[:value].should eq long_string
28
+
29
+ string = long_string(200).to_ber
30
+ decoded = Ber.decode(string)
31
+ decoded.should be_a Hash
32
+ decoded[:identifier].should eq :ocet_string
33
+ decoded[:length].should eq long_string(200).length
34
+ decoded[:value].should eq long_string(200)
35
+ end
36
+
37
+ it "should be able to decode a number" do
38
+ number = 15
39
+ decoded = Ber.decode(number.to_ber)
40
+ decoded[:identifier].should eq :fixnum
41
+ decoded[:length].should eq 1
42
+ decoded[:value].should eq number
43
+
44
+ number = 258
45
+ decoded = Ber.decode(number.to_ber)
46
+ decoded[:identifier].should eq :fixnum
47
+ decoded[:length].should eq 2
48
+ decoded[:value].should eq number
49
+ end
50
+
51
+ it "should be able to decode a big num" do
52
+ number = (2 ** 65)
53
+ decoded = Ber.decode(number.to_ber)
54
+ decoded[:identifier].should eq :fixnum
55
+ decoded[:length].should eq 9
56
+ decoded[:value].should eq number
57
+ end
58
+
59
+ it "should be able to decode a true value" do
60
+ bool = true
61
+ decoded = Ber.decode(bool.to_ber)
62
+ decoded[:identifier].should eq :boolean
63
+ decoded[:length].should eq 1
64
+ decoded[:value].should eq bool
65
+ end
66
+
67
+ it "should be able to decode a false value" do
68
+ bool = false
69
+ decoded = Ber.decode(bool.to_ber)
70
+ decoded[:identifier].should eq :boolean
71
+ decoded[:length].should eq 1
72
+ decoded[:value].should eq bool
73
+ end
74
+
75
+ it "should be able to decode an array" do
76
+ array = ["foo", "bar"]
77
+ decoded = Ber.decode(array.to_ber)
78
+ decoded[:identifier].should eq :array
79
+ decoded[:length].should eq 10
80
+ decoded[:value].should eq array
81
+ end
82
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ber::String do
4
+ it "should extend String" do
5
+ "foo".to_ber
6
+ end
7
+
8
+ it "should ber encode a string" do
9
+ "foo".to_ber.should eq "\x04\x03foo"
10
+ end
11
+
12
+ it "should be able to tell if a string is a ber string" do
13
+ "foo".ber?.should be_false
14
+ "foo".to_ber.ber?.should be_true
15
+ end
16
+
17
+ #it "should encode a longer string" do
18
+ # long_string.to_ber.should eq "\x04\x82\x02X#{long_string}"
19
+ #end
20
+ end
@@ -0,0 +1,5 @@
1
+ require 'ber'
2
+
3
+ def long_string(iterations = 100)
4
+ "foobar" * iterations
5
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Arcath
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: An implementation of BER (Basic Encoding Rules) in Ruby
28
+ email:
29
+ - adam@arcath.net
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - .travis.yml
36
+ - Gemfile
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - ber.gemspec
41
+ - lib/ber.rb
42
+ - lib/ber/array.rb
43
+ - lib/ber/bignum.rb
44
+ - lib/ber/boolean.rb
45
+ - lib/ber/fixnum.rb
46
+ - lib/ber/string.rb
47
+ - lib/ber/version.rb
48
+ - spec/ber_array_spec.rb
49
+ - spec/ber_bignum_spec.rb
50
+ - spec/ber_boolean_spec.rb
51
+ - spec/ber_fixnum_spec.rb
52
+ - spec/ber_spec.rb
53
+ - spec/ber_string_spec.rb
54
+ - spec/spec_helper.rb
55
+ homepage: https://github.com/Arcath/BER
56
+ licenses: []
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.0.0
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: An implementation of BER (Basic Encoding Rules) in Ruby
78
+ test_files:
79
+ - spec/ber_array_spec.rb
80
+ - spec/ber_bignum_spec.rb
81
+ - spec/ber_boolean_spec.rb
82
+ - spec/ber_fixnum_spec.rb
83
+ - spec/ber_spec.rb
84
+ - spec/ber_string_spec.rb
85
+ - spec/spec_helper.rb