z85rb 0.0.0

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,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in z85rb.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,4 @@
1
+ Permission to use, copy, modify, and/or distribute this software
2
+ for any purpose with or without fee is hereby granted.
3
+
4
+ This software is provided "as is" without express or implied warranty.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jakukyo Friel
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,68 @@
1
+ z85rb - pure Ruby implementation of Z85 encoding
2
+ ================================================
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'z85rb'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install z85rb
17
+
18
+ ## Usage
19
+
20
+ ```rb
21
+ encode("\x86\x4f\xd2\x6f\xb5\x59\xf7\x5b") # => 'HelloWorld'
22
+ decode('nm=QNzY&b1A+]nf') # => 'Hello World!'
23
+ ```
24
+
25
+ Encoding data not padded to a multiple of 4 will raise `ArgumentError`.
26
+
27
+ ## Difference to reference implementation in C:
28
+
29
+ When input binary form in encoding is not bounded to 4 bytes, or input string
30
+ frame in decoding is not bounded to 5 bytes, the [reference implementation][c]
31
+ returns `NULL`.
32
+
33
+ Instead of returning `nil`, we raise an `ArgumentError`.
34
+
35
+ Raising an exception conforms the [specification][rfc]. We guess that the reference
36
+ implementation returns `NULL` is because C does not have native support for
37
+ exceptions.
38
+
39
+ ## Difference to z85 C extension for ruby
40
+
41
+ This gem is a drop-in replace for [z85 C extension][z85].
42
+ API is the same.
43
+
44
+ There is only one difference:
45
+
46
+ When input is invalid (not bounded to 4 or 5 bytes), this gem raises an
47
+ `ArgumentError` while z85 C extension raises `RuntimeError`.
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
56
+
57
+ ## References
58
+
59
+ - [Z85 - ZeroMQ Base-85 Encoding Algorithm][rfc]
60
+ - [A reference implementation in C][c]
61
+
62
+ [rfc]: http://rfc.zeromq.org/spec:32
63
+ [c]: https://github.com/zeromq/rfc/blob/master/src/spec_32.c
64
+ [z85]: https://github.com/fpesce/z85/
65
+
66
+ ## License
67
+
68
+ 0BSD
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ require 'lib/z85rb'
2
+
3
+ open('lib/z85rb.rb') do |f|
4
+ module_lines = f.find_all { |line| line[0..5] == 'module' }
5
+ modules = module_lines.map { |s| s.strip.tr(' ', '')[6..-1] }
6
+ modules.each do |mod|
7
+ mod = Object.const_get(mod)
8
+ include mod
9
+ YARD::Doctest.configure do |doctest|
10
+ mod.private_instance_methods.each do |m|
11
+ doctest.skip "#{mod}.#{m}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,107 @@
1
+ require_relative 'z85rb/version'
2
+
3
+ module Z85rb
4
+ module_function
5
+
6
+ # Encode binary data into Z85.
7
+ #
8
+ # @param data [String] binary
9
+ # @return [String] Z85 encoded
10
+ #
11
+ # @example HelloWorld
12
+ # # Encode to 'HelloWorld' is an example from Z85 RFC.
13
+ # encode("\x86\x4f\xd2\x6f\xb5\x59\xf7\x5b") #=> 'HelloWorld'
14
+ # encode('Hello World!') #=> 'nm=QNzY&b1A+]nf'
15
+ #
16
+ # @example Not padded to a multiple of 4
17
+ # assert_raise(ArgumentError) { encode('HelloWorld') } #=> true
18
+ #
19
+ # @example longer data
20
+ # # Borrowed from Z85 reference implementation in C.
21
+ # test_data_2 = "\x8E\v\xDDiv(\xB9\x1D\x8F$U\x87\xEE\x95\xC5\xB0MH\x96?y%\x98w\xB4\x9C\xD9\x06:\xEA\xD3\xB7"
22
+ # encode(test_data_2) #=> 'JTKVSB%%)wK0E.X)V>+}o?pNmC{O&4W4b!Ni{Lh6'
23
+ def encode(data)
24
+ if data.length % 4 != 0
25
+ raise ArgumentError, 'The input binary frame should have a length that is divisible by 4 with no remainder.'
26
+ end
27
+
28
+ encoder = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#'
29
+ str = ''
30
+ byte_nbr = 0
31
+ size = data.length
32
+ value = 0
33
+ while byte_nbr < size
34
+ value = (value * 256) + data[byte_nbr].ord
35
+ byte_nbr += 1
36
+ if byte_nbr % 4 == 0
37
+ divisor = 85 * 85 * 85 * 85
38
+ while divisor >= 1
39
+ idx = (value / divisor).floor % 85
40
+ str += encoder[idx]
41
+ divisor /= 85
42
+ end
43
+ value = 0
44
+ end
45
+ end
46
+ str
47
+ end
48
+
49
+ # Decode Z85 to binary string.
50
+ #
51
+ # @param string [String] Z85 encoded
52
+ # @return [String] binary
53
+ #
54
+ # @example HelloWorld
55
+ # decode('HelloWorld') #=> "\x86\x4f\xd2\x6f\xb5\x59\xf7\x5b"
56
+ # decode('nm=QNzY&b1A+]nf') #=> 'Hello World!'
57
+ #
58
+ # @example Not padded to a multiple of 5
59
+ # assert_raise(ArgumentError) { decode('hello_world') } #=> true
60
+ #
61
+ # @example longer data
62
+ # decode('JTKVSB%%)wK0E.X)V>+}o?pNmC{O&4W4b!Ni{Lh6')
63
+ # #=> "\x8E\v\xDDiv(\xB9\x1D\x8F$U\x87\xEE\x95\xC5\xB0MH\x96?y%\x98w\xB4\x9C\xD9\x06:\xEA\xD3\xB7"
64
+ def decode(string)
65
+ if string.length % 5 != 0
66
+ raise ArgumentError, 'The input string frame should have a length that is divisible by 5 with no remainder.'
67
+ end
68
+
69
+ decoder = [
70
+ 0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00,
71
+ 0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45,
72
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
73
+ 0x08, 0x09, 0x40, 0x00, 0x49, 0x42, 0x4A, 0x47,
74
+ 0x51, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
75
+ 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
76
+ 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
77
+ 0x3B, 0x3C, 0x3D, 0x4D, 0x00, 0x4E, 0x43, 0x00,
78
+ 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
79
+ 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
80
+ 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
81
+ 0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00,
82
+ ]
83
+ dest = []
84
+ byte_nbr = 0
85
+ char_nbr = 0
86
+ string_len = string.length
87
+ value = 0
88
+ while char_nbr < string_len
89
+ idx = string[char_nbr].ord - 32
90
+ value = (value * 85) + decoder[idx]
91
+ char_nbr += 1
92
+ if char_nbr % 5 == 0
93
+ divisor = 256 * 256 * 256
94
+ while divisor >= 1
95
+ dest[byte_nbr] = (value / divisor) % 256
96
+ byte_nbr += 1
97
+ divisor /= 256
98
+ end
99
+ value = 0
100
+ end
101
+ end
102
+ # We think `C*` and `c*` is equivalent in this case.
103
+ # We use `C*` merely because reference implement in C uses `unsigned char`.
104
+ # https://github.com/zeromq/rfc/blob/master/src/spec_32.c#
105
+ dest.pack 'C*'
106
+ end
107
+ end
@@ -0,0 +1,3 @@
1
+ module Z85rb
2
+ VERSION = "0.0.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'z85rb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "z85rb"
8
+ spec.version = Z85rb::VERSION
9
+ spec.authors = ["Jakukyo Friel"]
10
+ spec.email = ["weakish@gmail.com"]
11
+ spec.description = %q{Pure Ruby implementiation of Z85 encoding.
12
+ A drop-in replace for Z85 C extension for Ruby.}
13
+ spec.summary = %q{Pure Ruby implementiation of Z85 encoding.}
14
+ spec.homepage = "https://github.com/weakish/z85rb"
15
+ spec.license = "0BSD"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.3'
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'yard-doctest'
25
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: z85rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jakukyo Friel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-12-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: yard-doctest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ! "Pure Ruby implementiation of Z85 encoding.\n A
63
+ drop-in replace for Z85 C extension for Ruby."
64
+ email:
65
+ - weakish@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - LICENSE
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - doctest_helper.rb
77
+ - lib/z85rb.rb
78
+ - lib/z85rb/version.rb
79
+ - z85rb.gemspec
80
+ homepage: https://github.com/weakish/z85rb
81
+ licenses:
82
+ - 0BSD
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.23
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Pure Ruby implementiation of Z85 encoding.
105
+ test_files: []
106
+ has_rdoc: