golomb 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: 3006e47bfd7f8284ec9a97bbc04b3fb583208ad8
4
+ data.tar.gz: 0641044c55b5674cc32eefe4a5464e971a61b5a7
5
+ SHA512:
6
+ metadata.gz: e4de6b663ad2c59416846f9d1b97cdb3747a8a43bc6bf0ed332d33725792ef91a511374f61227184bc0a543b36d9b7e092dab9d03f0c5f7b0735999bb1a24f3c
7
+ data.tar.gz: 3a038c14452695393fbe31ea3e7ba88ad24baf0df5b2b77201ebd2a42179c89b4978a47341aeb7804dd637073667cc09b7652a87e29d8f0626b3fe049e8e2ea9
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ golomb (0.0.1)
5
+ gli (= 2.13.2)
6
+ terminal-table (= 1.5.2)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ aruba (0.9.0)
12
+ childprocess (~> 0.5.6)
13
+ contracts (~> 0.9)
14
+ cucumber (>= 1.3.19)
15
+ ffi (~> 1.9.10)
16
+ rspec-expectations (>= 2.99)
17
+ thor (~> 0.19)
18
+ builder (3.2.2)
19
+ childprocess (0.5.6)
20
+ ffi (~> 1.0, >= 1.0.11)
21
+ contracts (0.12.0)
22
+ cucumber (2.1.0)
23
+ builder (>= 2.1.2)
24
+ cucumber-core (~> 1.3.0)
25
+ diff-lcs (>= 1.1.3)
26
+ gherkin3 (~> 3.1.0)
27
+ multi_json (>= 1.7.5, < 2.0)
28
+ multi_test (>= 0.1.2)
29
+ cucumber-core (1.3.0)
30
+ gherkin3 (~> 3.1.0)
31
+ diff-lcs (1.2.5)
32
+ ffi (1.9.10)
33
+ gherkin3 (3.1.1)
34
+ gli (2.13.2)
35
+ multi_json (1.11.2)
36
+ multi_test (0.1.2)
37
+ rake (10.4.2)
38
+ rdoc (4.2.0)
39
+ rspec-expectations (3.3.1)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.3.0)
42
+ rspec-support (3.3.0)
43
+ terminal-table (1.5.2)
44
+ thor (0.19.1)
45
+
46
+ PLATFORMS
47
+ ruby
48
+
49
+ DEPENDENCIES
50
+ aruba
51
+ golomb!
52
+ rake
53
+ rdoc
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Golomb coding
2
+
3
+ Command-line tool that encodes and decodes content using the Golomb coding algorithm.
4
+
5
+ ## Installing
6
+
7
+ Make sure that you have the Ruby's environment installed.
8
+
9
+ #### Installing via Ruby Gems
10
+
11
+ $ gem install golomb-cli
12
+
13
+ After installing you have access to the command `golomb` on your terminal.
14
+
15
+ #### Manual installation
16
+
17
+ Download the project and execute the command below. Make sure that you are on the project directory.
18
+
19
+ $ bundle install
20
+
21
+ This command will install all dependencies. After installed you can access the binary located in `bin/golomb`. Execute the binary based on the examples below.
22
+
23
+ ## Usage
24
+
25
+ The tool has the commands `encode` and `decode`.
26
+
27
+ Use the help option to view all parameters allowed.
28
+
29
+ $ golomb --help
30
+
31
+ #### Encoding
32
+
33
+ The example below encodes a text file using the divisor 10 and saves the encoded data into a file named my_output. The verbose option prints the symbols table.
34
+
35
+ $ golomb --divisor 10 --output my_output encode --verbose my_file.txt
36
+
37
+ You can pass a text stream to encode instead of a file.
38
+
39
+ $ golomb --divisor 10 --output my_output encode --verbose "My text goes here"
40
+
41
+ Output generated by the encode command with the verbose option:
42
+
43
+ +---------+------------------+
44
+ | Symbol table |
45
+ +---------+------------------+
46
+ | Symbol | Encode |
47
+ +---------+------------------+
48
+ | M (77) | 111111101101 |
49
+ | y (121) | 1111111111110001 |
50
+ | (32) | 1110010 |
51
+ | t (116) | 1111111111101100 |
52
+ | e (101) | 11111111110001 |
53
+ | x (120) | 1111111111110000 |
54
+ | t (116) | 1111111111101100 |
55
+ | (32) | 1110010 |
56
+ | g (103) | 11111111110011 |
57
+ | o (111) | 111111111110001 |
58
+ | e (101) | 11111111110001 |
59
+ | s (115) | 111111111110101 |
60
+ | (32) | 1110010 |
61
+ | h (104) | 11111111110100 |
62
+ | e (101) | 11111111110001 |
63
+ | r (114) | 111111111110100 |
64
+ | e (101) | 11111111110001 |
65
+ +---------+------------------+
66
+
67
+ INPUT HEX:
68
+ 0x4d 0x79 0x20 0x74 0x65 0x78 0x74 0x20 0x67 0x6f 0x65 0x73 0x20 0x68 0x65 0x72 0x65
69
+
70
+ OUTPUT HEX:
71
+ 0xfed 0xfff1 0x72 0xffec 0x3ff1 0xfff0 0xffec 0x72 0x3ff3 0x7ff1 0x3ff1 0x7ff5 0x72 0x3ff4 0x3ff1 0x7ff4 0x3ff1
72
+
73
+ #### Decoding
74
+
75
+ The example below decodes the previously encoded file.
76
+
77
+ $ golomb --divisor 10 decode my_output
78
+
79
+
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rake/clean'
2
+ require 'rubygems'
3
+ require 'rubygems/package_task'
4
+ require 'rdoc/task'
5
+ require 'cucumber'
6
+ require 'cucumber/rake/task'
7
+ Rake::RDocTask.new do |rd|
8
+ rd.main = "README.rdoc"
9
+ rd.rdoc_files.include("README.rdoc","lib/**/*.rb","bin/**/*")
10
+ rd.title = 'Your application title'
11
+ end
12
+
13
+ spec = eval(File.read('golomb.gemspec'))
14
+
15
+ Gem::PackageTask.new(spec) do |pkg|
16
+ end
17
+ CUKE_RESULTS = 'results.html'
18
+ CLEAN << CUKE_RESULTS
19
+ desc 'Run features'
20
+ Cucumber::Rake::Task.new(:features) do |t|
21
+ opts = "features --format html -o #{CUKE_RESULTS} --format progress -x"
22
+ opts += " --tags #{ENV['TAGS']}" if ENV['TAGS']
23
+ t.cucumber_opts = opts
24
+ t.fork = false
25
+ end
26
+
27
+ desc 'Run features tagged as work-in-progress (@wip)'
28
+ Cucumber::Rake::Task.new('features:wip') do |t|
29
+ tag_opts = ' --tags ~@pending'
30
+ tag_opts = ' --tags @wip'
31
+ t.cucumber_opts = "features --format html -o #{CUKE_RESULTS} --format pretty -x -s#{tag_opts}"
32
+ t.fork = false
33
+ end
34
+
35
+ task :cucumber => :features
36
+ task 'cucumber:wip' => 'features:wip'
37
+ task :wip => 'features:wip'
38
+ require 'rake/testtask'
39
+ Rake::TestTask.new do |t|
40
+ t.libs << "test"
41
+ t.test_files = FileList['test/*_test.rb']
42
+ end
43
+
44
+ task :default => [:test,:features]
data/bin/golomb ADDED
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env ruby
2
+ require 'gli'
3
+
4
+ require 'golomb'
5
+ require 'terminal-table'
6
+
7
+ include GLI::App
8
+
9
+ program_desc 'Encoder and decoder'
10
+
11
+ version Golomb::VERSION
12
+
13
+ subcommand_option_handling :normal
14
+ arguments :strict
15
+
16
+ desc 'Divisor value'
17
+ default_value '16'
18
+ arg_name 'The divisor value for encode and decode'
19
+ flag [:d,:divisor]
20
+
21
+ desc 'Output file'
22
+ default_value 'output'
23
+ arg_name 'The output file name'
24
+ flag [:o,:output]
25
+
26
+ desc 'Encode a file or a text stream'
27
+ arg_name 'File path or text stream to be encoded'
28
+ command :encode do |c|
29
+
30
+ c.desc 'Show symbols table'
31
+ c.switch [:v,:verbose]
32
+
33
+
34
+ c.action do |global_options,options,args|
35
+ # executes the encoder process
36
+ @encoder = Golomb::Encoder.new(global_options[:d].to_i, @data)
37
+ @encoder.exec
38
+
39
+ end
40
+ end
41
+
42
+ desc 'Describe decode here'
43
+ arg_name 'File or text stream to be decoded'
44
+ command :decode do |c|
45
+ c.action do |global_options,options,args|
46
+ # executes the decoder process
47
+ @decoder = Golomb::Decoder.new(global_options[:d].to_i, @data)
48
+ @decoder.exec
49
+ end
50
+ end
51
+
52
+ pre do |global,command,options,args|
53
+ # checks if is a valid file or a text stream
54
+ if File.exists?(args.first)
55
+ @data = File.open(args.first).read
56
+ else
57
+ @data = args.first
58
+ end
59
+
60
+ true
61
+ end
62
+
63
+ post do |global,command,options,args|
64
+ case command.name
65
+ when :encode then
66
+ # if verbose flag is enable
67
+ if options[:v]
68
+ table = Terminal::Table.new :title => "Symbol table", :headings => ['Symbol', 'Encode'], :rows => @encoder.symbol_table
69
+ puts table
70
+
71
+ puts "\nINPUT HEX:"
72
+ puts @encoder.data_to_hex
73
+ puts "\nOUTPUT HEX:"
74
+ puts @encoder.result_to_hex
75
+ end
76
+
77
+ # generates the output file
78
+ output = File.new(global[:o], 'wb')
79
+ output.write(@encoder.result_to_hex)
80
+ output.close
81
+ when :decode then
82
+ # shows the decoded data
83
+ puts @decoder.decoded_data.join
84
+ end
85
+ end
86
+
87
+ on_error do |exception|
88
+ # Error logic here
89
+ # return false to skip default error handling
90
+ p exception.message
91
+ p exception.backtrace
92
+ true
93
+ end
94
+
95
+ exit run(ARGV)
data/golomb.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ require File.join([File.dirname(__FILE__),'lib','golomb','version.rb'])
2
+ require File.join([File.dirname(__FILE__),'lib','golomb','decoder.rb'])
3
+ require File.join([File.dirname(__FILE__),'lib','golomb','encoder.rb'])
4
+ spec = Gem::Specification.new do |s|
5
+ s.name = 'golomb'
6
+ s.version = Golomb::VERSION
7
+ s.author = 'Bruno Flach Bohn / Cássio Ghilardi'
8
+ s.platform = Gem::Platform::RUBY
9
+ s.summary = 'Golomb command-line tool'
10
+ s.files = `git ls-files`.split("
11
+ ")
12
+ s.require_paths << 'lib'
13
+ s.has_rdoc = true
14
+ s.extra_rdoc_files = ['README.md']
15
+ s.rdoc_options << '--title' << 'golomb' << '--main' << 'README.md' << '-ri'
16
+ s.bindir = 'bin'
17
+ s.executables << 'golomb'
18
+ s.add_development_dependency('rake')
19
+ s.add_development_dependency('rdoc')
20
+ s.add_development_dependency('aruba')
21
+ s.add_runtime_dependency('terminal-table','1.5.2')
22
+ s.add_runtime_dependency('gli','2.13.2')
23
+ end
data/golomb.rdoc ADDED
@@ -0,0 +1,5 @@
1
+ = golomb
2
+
3
+ Generate this with
4
+ golomb rdoc
5
+ After you have described your command line interface
@@ -0,0 +1,33 @@
1
+ module Golomb
2
+ class Decoder
3
+ def initialize(divisor, data)
4
+ @divisor = divisor
5
+ @data = data
6
+ @decoded_data = []
7
+ end
8
+
9
+ attr_accessor :divisor, :data, :decoded_data
10
+
11
+ def exec
12
+ @data.split(" ").each do |n|
13
+ decode(n)
14
+ end
15
+ end
16
+
17
+ def decode(n)
18
+ b = Math.log2(@divisor).ceil
19
+ binary = n.to_i(16).to_s(2)
20
+ quotient_code, remainder_code = binary.sub("0", "-").split("-")
21
+ quotient = quotient_code.size
22
+ remainder = (remainder_code == 3 ? "0#{remainder_code}" : remainder_code).to_i(2)
23
+
24
+ if remainder >= (2 ** b) - @divisor
25
+ remainder = -(2 ** b) + @divisor + remainder
26
+ end
27
+
28
+ symbol = (quotient * @divisor + remainder).chr
29
+ @decoded_data << symbol
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,52 @@
1
+ module Golomb
2
+ class Encoder
3
+
4
+ def initialize(divisor, data)
5
+ @divisor = divisor
6
+ @data = data
7
+ @encoded_data = []
8
+ end
9
+
10
+ attr_accessor :divisor, :data, :encoded_data
11
+
12
+ def exec
13
+ @data.each_byte do |byte|
14
+ encode(byte)
15
+ end
16
+ end
17
+
18
+ def encode(symbol)
19
+ quotient = (symbol / @divisor).to_i
20
+ remainder = symbol % @divisor
21
+ quotient_code = ("1" * quotient) << "0"
22
+
23
+ # checks if the divisor is power of 2 (Rice coding)
24
+ if @divisor.to_s(2).scan(/1/).size == 1
25
+ remainder_code = "0" << remainder.to_s(2)
26
+ else
27
+ b = Math.log2(@divisor).ceil
28
+
29
+ if remainder < (2 ** b) - @divisor
30
+ remainder_code = "0" * (b - 1 - remainder.to_s(2).size) << remainder.to_s(2)
31
+ else
32
+ remainder_code = (remainder + (2 ** b) - @divisor).to_i.to_s(2)
33
+ end
34
+ end
35
+
36
+ @encoded_data << (quotient_code << remainder_code)
37
+ end
38
+
39
+ def data_to_hex
40
+ @data.unpack('U'*@data.length).collect {|x| "0x#{x.to_s(16)}" }.join(" ")
41
+ end
42
+
43
+ def result_to_hex
44
+ @encoded_data.collect {|x| "0x%02x" % x.to_i(2) }.join(" ")
45
+ end
46
+
47
+ def symbol_table
48
+ @encoded_data.collect.with_index {|x, i| ["#{@data[i]} (#{@data[i].ord})", x]}
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module Golomb
2
+ VERSION = '0.0.1'
3
+ end
data/lib/golomb.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'golomb/version.rb'
2
+ require 'golomb/encoder.rb'
3
+ require 'golomb/decoder.rb'
data/my_output ADDED
@@ -0,0 +1 @@
1
+ 0xfed 0xfff1 0x72 0xffec 0x3ff1 0xfff0 0xffec 0x72 0x3ff3 0x7ff1 0x3ff1 0x7ff5 0x72 0x3ff4 0x3ff1 0x7ff4 0x3ff1 0x72 0x7fee 0x7ff1 0x7ff0 0x3ff3 0x72 0xffec 0x3ff1 0xfff0 0xffec 0x72 0x3ff5 0x7ff5 0x72 0x3fed 0x72 0x3ff3 0x7ff1 0x7ff1 0x3ff0 0x72 0x7ff1 0x7ff2 0xffec 0x3ff5 0x7ff1 0x7ff0 0x72 0xffec 0x7ff1 0x72 0xffec 0x3ff1 0x7ff5 0xffec 0x72 0xffec 0x3ff4 0x3ff5 0x7ff5 0x72 0x3fef 0x7ff1 0x7fef 0x7fef 0x3fed 0x7ff0 0x3ff0 0x72 0x7fee 0x3ff5 0x7ff0 0x3ff1 0x72 0xffec 0x7ff1 0x7ff1 0x7fee
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+
3
+ class DefaultTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def teardown
9
+ end
10
+
11
+ def test_the_truth
12
+ assert true
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ require 'test/unit'
2
+
3
+ # Add test libraries you want to use here, e.g. mocha
4
+
5
+ class Test::Unit::TestCase
6
+
7
+ # Add global extensions to the test case class here
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: golomb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Flach Bohn / Cássio Ghilardi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
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
+ - !ruby/object:Gem::Dependency
28
+ name: rdoc
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aruba
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: terminal-table
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.5.2
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 1.5.2
69
+ - !ruby/object:Gem::Dependency
70
+ name: gli
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 2.13.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 2.13.2
83
+ description:
84
+ email:
85
+ executables:
86
+ - golomb
87
+ extensions: []
88
+ extra_rdoc_files:
89
+ - README.md
90
+ files:
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - README.md
94
+ - Rakefile
95
+ - bin/golomb
96
+ - golomb.gemspec
97
+ - golomb.rdoc
98
+ - lib/golomb.rb
99
+ - lib/golomb/decoder.rb
100
+ - lib/golomb/encoder.rb
101
+ - lib/golomb/version.rb
102
+ - my_output
103
+ - test/default_test.rb
104
+ - test/test_helper.rb
105
+ homepage:
106
+ licenses: []
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options:
110
+ - "--title"
111
+ - golomb
112
+ - "--main"
113
+ - README.md
114
+ - "-ri"
115
+ require_paths:
116
+ - lib
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.4.3
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Golomb command-line tool
134
+ test_files: []