-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathutil.lua
More file actions
294 lines (258 loc) · 5.77 KB
/
util.lua
File metadata and controls
294 lines (258 loc) · 5.77 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
-- This Script is Part of the Prometheus Obfuscator by Levno_710
--
-- util.lua
-- This file Provides some utility functions
local logger = require("logger");
local bit32 = require("prometheus.bit").bit32;
local MAX_UNPACK_COUNT = 4096;
local function lookupify(tb)
local tb2 = {};
for _, v in ipairs(tb) do
tb2[v] = true
end
return tb2
end
local function unlookupify(tb)
local tb2 = {};
for v, _ in pairs(tb) do
table.insert(tb2, v);
end
return tb2;
end
local function escape(str)
return str:gsub(".", function(char)
if char:match("[^ %-~\n\t\a\b\v\r\"\']") then -- Check if non Printable ASCII Character
return string.format("\\%03d", string.byte(char))
end
if(char == "\\") then
return "\\\\";
end
if(char == "\n") then
return "\\n";
end
if(char == "\r") then
return "\\r";
end
if(char == "\t") then
return "\\t";
end
if(char == "\a") then
return "\\a";
end
if(char == "\b") then
return "\\b";
end
if(char == "\v") then
return "\\v";
end
if(char == "\"") then
return "\\\"";
end
if(char == "\'") then
return "\\\'";
end
return char;
end)
end
local function chararray(str)
local tb = {};
for i = 1, str:len(), 1 do
table.insert(tb, str:sub(i, i));
end
return tb;
end
local function keys(tb)
local keyset={}
local n=0
for k,v in pairs(tb) do
n=n+1
keyset[n]=k
end
return keyset
end
local utf8char;
do
local string_char = string.char
function utf8char(cp)
if cp < 128 then
return string_char(cp)
end
local suffix = cp % 64
local c4 = 128 + suffix
cp = (cp - suffix) / 64
if cp < 32 then
return string_char(192 + cp, c4)
end
suffix = cp % 64
local c3 = 128 + suffix
cp = (cp - suffix) / 64
if cp < 16 then
return string_char(224 + cp, c3, c4)
end
suffix = cp % 64
cp = (cp - suffix) / 64
return string_char(240 + cp, 128 + suffix, c3, c4)
end
end
local function shuffle(tb)
for i = #tb, 2, -1 do
local j = math.random(i)
tb[i], tb[j] = tb[j], tb[i]
end
return tb
end
local function shuffle_string(str)
local len = #str
local t = {}
for i = 1, len do
t[i] = string.sub(str, i, i)
end
for i = 1, len do
local j = math.random(i, len)
t[i], t[j] = t[j], t[i]
end
return table.concat(t)
end
local function readDouble(bytes)
local sign = 1
local mantissa = bytes[2] % 2^4
for i = 3, 8 do
mantissa = mantissa * 256 + bytes[i]
end
if bytes[1] > 127 then sign = -1 end
local exponent = (bytes[1] % 128) * 2^4 + math.floor(bytes[2] / 2^4)
if exponent == 0 then
return 0
end
mantissa = (math.ldexp(mantissa, -52) + 1) * sign
return math.ldexp(mantissa, exponent - 1023)
end
local function writeDouble(num)
local bytes = {0,0,0,0, 0,0,0,0}
if num == 0 then
return bytes
end
local anum = math.abs(num)
local mantissa, exponent = math.frexp(anum)
exponent = exponent - 1
mantissa = mantissa * 2 - 1
local sign = num ~= anum and 128 or 0
exponent = exponent + 1023
bytes[1] = sign + math.floor(exponent / 2^4)
mantissa = mantissa * 2^4
local currentmantissa = math.floor(mantissa)
mantissa = mantissa - currentmantissa
bytes[2] = (exponent % 2^4) * 2^4 + currentmantissa
for i= 3, 8 do
mantissa = mantissa * 2^8
currentmantissa = math.floor(mantissa)
mantissa = mantissa - currentmantissa
bytes[i] = currentmantissa
end
return bytes
end
local function writeU16(u16)
if (u16 < 0 or u16 > 65535) then
logger:error(string.format("u16 out of bounds: %d", u16));
end
local lower = bit32.band(u16, 255);
local upper = bit32.rshift(u16, 8);
return {lower, upper}
end
local function readU16(arr)
return bit32.bor(arr[1], bit32.lshift(arr[2], 8));
end
local function writeU24(u24)
if(u24 < 0 or u24 > 16777215) then
logger:error(string.format("u24 out of bounds: %d", u24));
end
local arr = {};
for i = 0, 2 do
arr[i + 1] = bit32.band(bit32.rshift(u24, 8 * i), 255);
end
return arr;
end
local function readU24(arr)
local val = 0;
for i = 0, 2 do
val = bit32.bor(val, bit32.lshift(arr[i + 1], 8 * i));
end
return val;
end
local function writeU32(u32)
if(u32 < 0 or u32 > 4294967295) then
logger:error(string.format("u32 out of bounds: %d", u32));
end
local arr = {};
for i = 0, 3 do
arr[i + 1] = bit32.band(bit32.rshift(u32, 8 * i), 255);
end
return arr;
end
local function readU32(arr)
local val = 0;
for i = 0, 3 do
val = bit32.bor(val, bit32.lshift(arr[i + 1], 8 * i));
end
return val;
end
-- Adapted from https://github.com/Reselim/Base64/blob/master/Base64.lua#L16
local function bytesToString(values)
local chunks = {}
local lenght = #values
for i = 1, lenght, MAX_UNPACK_COUNT do
chunks[#chunks + 1] = string.char(
unpack(values, i, math.min(i + MAX_UNPACK_COUNT - 1, lenght))
)
end
return table.concat(chunks, "")
end
local function isNaN(n)
return type(n) == "number" and n ~= n;
end
local function isInt(n)
return math.floor(n) == n;
end
local function isU32(n)
return n >= 0 and n <= 4294967295 and isInt(n);
end
local function toBits(num)
-- returns a table of bits, least significant first.
local t={} -- will contain the bits
local rest;
while num>0 do
rest=math.fmod(num,2)
t[#t+1]=rest
num=(num-rest)/2
end
return t
end
local function readonly(obj)
local r = newproxy(true);
getmetatable(r).__index = obj;
return r;
end
return {
lookupify = lookupify,
unlookupify = unlookupify,
escape = escape,
chararray = chararray,
keys = keys,
shuffle = shuffle,
shuffle_string = shuffle_string,
readDouble = readDouble,
writeDouble = writeDouble,
readU16 = readU16,
writeU16 = writeU16,
readU32 = readU32,
writeU32 = writeU32,
readU24 = readU24,
writeU24 = writeU24,
isNaN = isNaN,
isU32 = isU32,
isInt = isInt,
utf8char = utf8char,
toBits = toBits,
bytesToString = bytesToString,
readonly = readonly,
}