z85 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/z85/extconf.rb +2 -0
- data/ext/z85/z85.c +129 -0
- data/lib/z85.rb +17 -0
- data/lib/z85/version.rb +3 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f0bd341e9c8a0230ff57e97dc939fd3195a849775bdfc1b702e65b06c1eec30e
|
4
|
+
data.tar.gz: 9cafa70181d291c96f397f19af8dd333aecbb1acdce30d7aa3f188e0ed45d88f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 799cad30de56e47b2ea6cbf481bc3b55a2319c43f2a33568c1e9af15ef4778ca5cb8145d13e1e4bcdf0dceb3cbc21a30bd0538c97a910a03ef51bc490a53cbc2
|
7
|
+
data.tar.gz: 9b1a86f71561024b2432d0fa23c306dd0e454ae34eb837741244e90456231e799ac7887eba95d70951fd4fdf55fb3dc2aa3b4d2f79c1ad8d317a9ab80c44a3c0
|
data/ext/z85/extconf.rb
ADDED
data/ext/z85/z85.c
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
// Ruby C extension of Z85 based on the reference C implementation found in
|
2
|
+
//
|
3
|
+
// https://github.com/zeromq/rfc/blob/master/src/spec_32.c
|
4
|
+
//
|
5
|
+
|
6
|
+
// Original header:
|
7
|
+
//
|
8
|
+
// --------------------------------------------------------------------------
|
9
|
+
// Copyright (c) 2010-2013 iMatix Corporation and Contributors
|
10
|
+
//
|
11
|
+
// Permission is hereby granted, free of charge, to any person obtaining a
|
12
|
+
// copy of this software and associated documentation files (the "Software"),
|
13
|
+
// to deal in the Software without restriction, including without limitation
|
14
|
+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
15
|
+
// and/or sell copies of the Software, and to permit persons to whom the
|
16
|
+
// Software is furnished to do so, subject to the following conditions:
|
17
|
+
//
|
18
|
+
// The above copyright notice and this permission notice shall be included in
|
19
|
+
// all copies or substantial portions of the Software.
|
20
|
+
//
|
21
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
22
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
24
|
+
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
26
|
+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
27
|
+
// DEALINGS IN THE SOFTWARE.
|
28
|
+
// --------------------------------------------------------------------------
|
29
|
+
|
30
|
+
#include <stdlib.h>
|
31
|
+
#include <stdint.h>
|
32
|
+
#include <ruby/ruby.h>
|
33
|
+
|
34
|
+
typedef unsigned char byte;
|
35
|
+
|
36
|
+
static char encoder[85 + 1] = {
|
37
|
+
"0123456789"
|
38
|
+
"abcdefghij"
|
39
|
+
"klmnopqrst"
|
40
|
+
"uvwxyzABCD"
|
41
|
+
"EFGHIJKLMN"
|
42
|
+
"OPQRSTUVWX"
|
43
|
+
"YZ.-:+=^!/"
|
44
|
+
"*?&<>()[]{"
|
45
|
+
"}@%$#"
|
46
|
+
};
|
47
|
+
|
48
|
+
static byte decoder[96] = {
|
49
|
+
0x00, 0x44, 0x00, 0x54, 0x53, 0x52, 0x48, 0x00,
|
50
|
+
0x4B, 0x4C, 0x46, 0x41, 0x00, 0x3F, 0x3E, 0x45,
|
51
|
+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
52
|
+
0x08, 0x09, 0x40, 0x00, 0x49, 0x42, 0x4A, 0x47,
|
53
|
+
0x51, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
|
54
|
+
0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
|
55
|
+
0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
|
56
|
+
0x3B, 0x3C, 0x3D, 0x4D, 0x00, 0x4E, 0x43, 0x00,
|
57
|
+
0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
|
58
|
+
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
|
59
|
+
0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
|
60
|
+
0x21, 0x22, 0x23, 0x4F, 0x00, 0x50, 0x00, 0x00
|
61
|
+
};
|
62
|
+
|
63
|
+
static VALUE z85_encode(VALUE _mod, VALUE string)
|
64
|
+
{
|
65
|
+
byte* data = (byte*) StringValuePtr(string);
|
66
|
+
long size = RSTRING_LEN(string);
|
67
|
+
|
68
|
+
if (size % 4)
|
69
|
+
rb_raise(rb_eRuntimeError, "Invalid string, should have 0 mod 4 bytes");
|
70
|
+
|
71
|
+
size_t encoded_size = size * 5 / 4;
|
72
|
+
char *encoded = malloc(encoded_size + 1);
|
73
|
+
uint char_nbr = 0;
|
74
|
+
uint byte_nbr = 0;
|
75
|
+
uint32_t value = 0;
|
76
|
+
|
77
|
+
while (byte_nbr < size) {
|
78
|
+
value = value * 256 + data[byte_nbr++];
|
79
|
+
if (byte_nbr % 4 == 0) {
|
80
|
+
uint divisor = 85 * 85 * 85 * 85;
|
81
|
+
while (divisor) {
|
82
|
+
encoded[char_nbr++] = encoder[value / divisor % 85];
|
83
|
+
divisor /= 85;
|
84
|
+
}
|
85
|
+
value = 0;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
encoded[char_nbr] = 0;
|
89
|
+
|
90
|
+
return rb_str_export_locale(rb_str_new_cstr(encoded));
|
91
|
+
}
|
92
|
+
|
93
|
+
static VALUE z85_decode(VALUE _mod, VALUE string)
|
94
|
+
{
|
95
|
+
char* data = StringValuePtr(string);
|
96
|
+
long size = RSTRING_LEN(string);
|
97
|
+
|
98
|
+
if (size % 5)
|
99
|
+
rb_raise(rb_eRuntimeError, "Invalid string, should have 0 mod 5 bytes");
|
100
|
+
|
101
|
+
size_t decoded_size = size * 4 / 5;
|
102
|
+
byte* decoded = malloc(decoded_size);
|
103
|
+
|
104
|
+
uint byte_nbr = 0;
|
105
|
+
uint char_nbr = 0;
|
106
|
+
uint32_t value = 0;
|
107
|
+
while (char_nbr < size) {
|
108
|
+
value = value * 85 + decoder[(byte) data[char_nbr++] - 32];
|
109
|
+
if (char_nbr % 5 == 0) {
|
110
|
+
uint divisor = 256 * 256 * 256;
|
111
|
+
while (divisor) {
|
112
|
+
decoded[byte_nbr++] = value / divisor % 256;
|
113
|
+
divisor /= 256;
|
114
|
+
}
|
115
|
+
value = 0;
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
return rb_str_new((const char*) decoded, decoded_size);
|
120
|
+
}
|
121
|
+
|
122
|
+
/* This function has a special name and it is invoked by Ruby to initialize the extension. */
|
123
|
+
void Init_z85()
|
124
|
+
{
|
125
|
+
VALUE z85 = rb_define_module("Z85");
|
126
|
+
|
127
|
+
rb_define_singleton_method(z85, "encode", z85_encode, 1);
|
128
|
+
rb_define_singleton_method(z85, "decode", z85_decode, 1);
|
129
|
+
}
|
data/lib/z85.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require "z85/z85"
|
2
|
+
require "z85/version"
|
3
|
+
|
4
|
+
module Z85
|
5
|
+
def self.encode_with_padding(string)
|
6
|
+
rem = string.bytesize % 4
|
7
|
+
string += "\0" * (4 - rem) unless rem == 0
|
8
|
+
encode(string) + rem.to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.decode_with_padding(encoded)
|
12
|
+
rem = encoded[-1].to_i
|
13
|
+
decoded = decode(encoded.chop!)
|
14
|
+
(4 - rem).times { decoded.chop! } unless rem == 0
|
15
|
+
decoded
|
16
|
+
end
|
17
|
+
end
|
data/lib/z85/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: z85
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Xavier Noria
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-07-31 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: fxn@hashref.com
|
15
|
+
executables: []
|
16
|
+
extensions:
|
17
|
+
- ext/z85/extconf.rb
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ext/z85/extconf.rb
|
21
|
+
- ext/z85/z85.c
|
22
|
+
- lib/z85.rb
|
23
|
+
- lib/z85/version.rb
|
24
|
+
homepage: https://github.com/fxn/z85
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubygems_version: 3.0.3
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Z85 encoding
|
47
|
+
test_files: []
|