-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase2base.tl
175 lines (153 loc) · 4.99 KB
/
base2base.tl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
local fmt = string.format
local type Bigint = {integer}
local function next_power(p: Bigint, base_from: integer, base_to: integer): Bigint
local r: Bigint = {}
local j, t = 0, 0
for i = 1, #p do
j, t = i, p[i] * base_from
while true do
t = (r[j] or 0) + t
r[j] = t % base_to
t = t // base_to
if t == 0 then break end
j = j + 1
end
end
return r
end
-- a = a + n * b
local function add_times(a: Bigint, n: integer, b: Bigint, base: integer): nil
local j, t = 0, 0
for i = 1, #b do
j, t = i, n * (b[i] or 0)
while true do
t = (a[j] or 0) + t
a[j] = t % base
t = t // base
if t == 0 then break end
j = j + 1
end
end
end
local record Converter
alpha_to: string
r_alpha_from: {integer: integer}
base_from: integer
base_to: integer
power: {integer: Bigint}
metamethod __call: function(Converter, string): string
validate: function(Converter, string):(boolean)
end
local function s_to_t(self: Converter, s: string): Bigint
local r, l = {}, #s
for i = 1, l do r[i] = self.r_alpha_from[s:byte(l - i + 1)] end
return r
end
local function t_to_s(self: Converter, t: Bigint): string
local r, l = {}, #t
for i = l, 1, -1 do r[l - i + 1] = self.alpha_to:byte(t[i] + 1) end
return string.char(table.unpack(r))
end
local function get_power(self: Converter, n: integer): Bigint
return self.power[n] or next_power(
get_power(self, n-1), self.base_from, self.base_to
)
end
local function base_convert(self: Converter, t: Bigint): Bigint
local r = {}
for i = 1, #t do
add_times(r, t[i], get_power(self, i - 1), self.base_to)
end
return r
end
local function convert(self: Converter, s: string): string
return t_to_s(self, base_convert(self, s_to_t(self, s)))
end
local function validate(self: Converter, s: string): boolean
for i = 1, #s do
if not self.r_alpha_from[s:byte(i)] then
return false
end
end
return true
end
local converter_mt = {
__index = {convert = convert, validate = validate},
__call = convert,
}
local function new_converter(alpha_from: string, alpha_to: string): Converter
local self: Converter = {
alpha_to = alpha_to,
base_from = #alpha_from,
base_to = #alpha_to,
}
local v = {}
for i = 1, #alpha_from do v[alpha_from:byte(i)] = i - 1 end
self.r_alpha_from = v
self.power = { [0] = {1} }
return setmetatable(self, converter_mt)
end
local letters = "abcdefghijklmnopqrstuvwxyz"
local figures = "0123456789"
local ascii = {}; for i = 1, 256 do ascii[i] = i - 1 end
local ALPHABET_B64 = letters:upper() .. letters .. figures .. "+/"
local ALPHABET_B256 = string.char(table.unpack(ascii))
local ALPHABET_B64URL = ALPHABET_B64:sub(1, 62) .. "-_"
local function _byte_to_hex(x: string): string return fmt("%02x", x:byte()) end
local function to_hex(s: string): string return (s:gsub("(.)", _byte_to_hex)) end
local function _byte_from_hex(x: string): string return string.char(tonumber(x, 16)) end
local function from_hex(s: string): string return (s:gsub("(..)", _byte_from_hex)) end
local function is_hex(s: string): boolean return not not s:match("^[0-9a-f]*$") end
local function _from_b64(alphabet: string): function(string):(string)
local c = new_converter(alphabet, ALPHABET_B256)
return function(s: string): string
local n = 0
if s:sub(-2, -1) == "==" then
n = 2
s = s:sub(1, -3) .. "00"
elseif s:sub(-1) == "=" then
n = 1
s = s:sub(1, -2) .. "0"
end
return c(s):sub(1, -(n+1))
end
end
local function _to_b64(alphabet: string): function(string):(string)
local c = new_converter(ALPHABET_B256, alphabet)
return function(s: string): string
local l, n = #s, 0
if l % 3 == 1 then
n = 2
s = s .. "\0\0"
elseif l % 3 == 2 then
n = 1
s = s .. "\0"
end
local r = c(s)
if n == 2 then
return r:sub(1, -3) .. "=="
elseif n == 1 then
return r:sub(1, -2) .. "="
else return r end
end
end
local ALPHABET_B62 = figures .. letters .. letters:upper()
local ALPHABET_B36 = ALPHABET_B62:sub(1, 36)
local from_b36 = new_converter(ALPHABET_B36, ALPHABET_B256)
local M = {
converter = new_converter,
ALPHABET_B36 = ALPHABET_B36,
ALPHABET_B62 = ALPHABET_B62,
ALPHABET_B64 = ALPHABET_B64,
ALPHABET_B64URL = ALPHABET_B64URL,
ALPHABET_B256 = ALPHABET_B256,
to_hex = to_hex, from_hex = from_hex, is_hex = is_hex,
to_b64 = _to_b64(ALPHABET_B64),
from_b64 = _from_b64(ALPHABET_B64),
to_b64url = _to_b64(ALPHABET_B64URL),
from_b64url = _from_b64(ALPHABET_B64URL),
to_b36 = new_converter(ALPHABET_B256, ALPHABET_B36),
from_b36 = from_b36,
is_b36 = function(s: string): boolean return from_b36:validate(s) end
}
return M