- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEncoderLibrary.lua
More file actions
Latest commit
192 lines (185 loc) · 3.83 KB
/
Copy pathEncoderLibrary.lua
File metadata and controls
192 lines (185 loc) · 3.83 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
localerror=error
localipairs=ipairs
localstring=string
localtable=table
localunpack=unpack
localprint=print
localbit=bitorbit32
ifbit==nilthen
bit= {}
localMOD=2^32
localMODM=MOD-1
functionbit.bxor(a, b)
localp, c=1, 0
whilea>0andb>0do
localra, rb=a%2, b%2
ifra~=rbthen
c=c+p
end
a, b, p= (a-ra) /2, (b-rb) /2, p*2
end
ifa<bthen
a=b
end
whilea>0do
localra=a%2
ifra>0then
c=c+p
end
a, p= (a-ra) /2, p*2
end
returnc
end
functionbit.band(a, b)
return (a+b-bit.bxor(a, b)) /2
end
functionbit.bor(a, b)
returnMODM-bit.band((MODM-a), (MODM-b))
end
functionbit.lshift(a, disp)
ifdisp<0then
returnbit.rshift(a, -disp)
end
returna*2^disp%2^32
end
functionbit.rshift(a, disp)
ifdisp<0then
returnbit.lshift(a, -disp)
end
returnmath.floor(a%2^32/2^disp)
end
end
localutf8=utf8
ifutf8==nilthen
utf8= {}
utf8.char=function(...)
localbuf= {}
fork, vinipairs({
...,
}) do
ifv<0orv>1114111then
error("bad argument #" ..k.." to char (out of range)", 2)
end
localb1, b2, b3, b4=nil, nil, nil, nil
ifv<128then
table.insert(buf, string.char(v))
elseifv<2048then
b1=bit.bor(192, bit.band(bit.rshift(v, 6), 31))
b2=bit.bor(128, bit.band(v, 63))
table.insert(buf, string.char(b1, b2))
elseifv<65536then
b1=bit.bor(224, bit.band(bit.rshift(v, 12), 15))
b2=bit.bor(128, bit.band(bit.rshift(v, 6), 63))
b3=bit.bor(128, bit.band(v, 63))
table.insert(buf, string.char(b1, b2, b3))
else
b1=bit.bor(240, bit.band(bit.rshift(v, 18), 7))
b2=bit.bor(128, bit.band(bit.rshift(v, 12), 63))
b3=bit.bor(128, bit.band(bit.rshift(v, 6), 63))
b4=bit.bor(128, bit.band(v, 63))
table.insert(buf, string.char(b1, b2, b3, b4))
end
end
returntable.concat(buf, "")
end
end
localhelpers= {}
functionhelpers.StringToBytes(str)
localbytes= {
str:byte(1, -1),
}
fori=1, #bytesdo
bytes[i] =bytes[i] +12
end
returntable.concat(bytes, "'")
end
functionhelpers.StringSplit(str)
localt= {}
str:gsub(".", function(b)
t[#t+1] =b
end)
returnt
end
functionhelpers.SplitBytes(str)
localindex=0
localt= {}
str=str:gsub("255'172", "")
localassembled=""
forchunkinstring.gmatch(str, "([^']+)") do
ifindex==1then
index=0
assembled=assembled.."'" ..chunk
table.insert(t, assembled)
else
assembled=chunk
index=index+1
end
end
returnt
end
functionhelpers.ToBytes(str)
returnstr:gsub(".", function(b)
return"\\" ..b:byte()
end)
end
functionhelpers.ToString(bytes)
return (bytes
:gsub("\\(%d+)", function(b)
returnb:char()
end)
:gsub("\\", ""))
end
localascp_characters= {
"\\",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
}
localreplace_characters= {
917536,
917537,
917538,
917539,
917540,
917541,
917542,
917543,
917544,
917545,
917546,
}
localcharacters= {
characters= {},
reverse= {},
}
fori, vinipairs(ascp_characters) do
characters.characters[v] =replace_characters[i]
localcorresponding= (helpers.StringToBytes(utf8.char(replace_characters[i])):gsub("255'172'", ""))
characters.reverse[corresponding] =v
end
locallibrary= {}
functionlibrary.encode(str)
str=helpers.ToBytes(str)
localencoded= {}
fori, vinipairs(helpers.StringSplit(str)) do
encoded[i] =utf8.char(characters.characters[v])
end
returntable.concat(encoded, "")
end
functionlibrary.decode(str)
localdecoded= {}
localbytes=helpers.SplitBytes(helpers.StringToBytes(str))
fori, vinipairs(bytes) do
decoded[i] =characters.reverse[v]
end
returnhelpers.ToString(table.concat(decoded, ""))
end
library.helpers=helpers
returnlibrary