@@ -79,6 +79,16 @@ ConstantArray.SettingsDescriptor = {
7979 type = " number" ,
8080 min = 0 ,
8181 default = 65535 ,
82+ };
83+ Encoding = {
84+ name = " Encoding" ,
85+ description = " The Encoding to use for the Strings" ,
86+ type = " enum" ,
87+ default = " base64" ,
88+ values = {
89+ " none" ,
90+ " base64" ,
91+ },
8292 }
8393}
8494
96106function ConstantArray :createArray ()
97107 local entries = {};
98108 for i , v in ipairs (self .constants ) do
109+ if type (v ) == " string" then
110+ v = self :encode (v );
111+ end
99112 entries [i ] = Ast .TableEntry (Ast .ConstantNode (v ));
100113 end
101114 return Ast .TableConstructorExpression (entries );
@@ -176,26 +189,139 @@ function ConstantArray:addRotateCode(ast, shift)
176189 LuaVersion = LuaVersion .Lua51 ;
177190 });
178191
179- local doStat = parser :parse (string.gsub (string.gsub (rotateCode , " SHIFT" , tostring (shift )), " LEN" , tostring (# self .constants ))).body .statements [1 ];
180- doStat .body .scope :setParent (ast .body .scope );
181- visitast (doStat , nil , function (node , data )
192+ local newAst = parser :parse (string.gsub (string.gsub (rotateCode , " SHIFT" , tostring (shift )), " LEN" , tostring (# self .constants )));
193+ local forStat = newAst .body .statements [1 ];
194+ forStat .body .scope :setParent (ast .body .scope );
195+ visitast (newAst , nil , function (node , data )
182196 if (node .kind == AstKind .VariableExpression ) then
183197 if (node .scope :getVariableName (node .id ) == " ARR" ) then
184- doStat . body .scope :removeReferenceToHigherScope (node .scope , node .id );
198+ data .scope :removeReferenceToHigherScope (node .scope , node .id );
185199 data .scope :addReferenceToHigherScope (self .rootScope , self .arrId );
186200 node .scope = self .rootScope ;
187201 node .id = self .arrId ;
188202 end
189203 end
190204 end )
191205
192- table.insert (ast .body .statements , 1 , doStat );
206+ table.insert (ast .body .statements , 1 , forStat );
207+ end
208+
209+ function ConstantArray :addDecodeCode (ast )
210+ if self .Encoding == " base64" then
211+ local base64DecodeCode = [[
212+ do ]] .. table.concat (util .shuffle {
213+ " local lookup = LOOKUP_TABLE;" ,
214+ " local len = string.len;" ,
215+ " local sub = string.sub;" ,
216+ " local floor = math.floor;" ,
217+ " local strchar = string.char;" ,
218+ " local insert = table.insert;" ,
219+ " local concat = table.concat;" ,
220+ " local type = type;" ,
221+ " local arr = ARR;" ,
222+ }) .. [[
223+ for i = 1, #arr do
224+ local data = arr[i];
225+ if type(data) == "string" then
226+ local length = len(data)
227+ local parts = {}
228+ local index = 1
229+ local value = 0
230+ local count = 0
231+ while index <= length do
232+ local char = sub(data, index, index)
233+ local code = lookup[char]
234+ if code then
235+ value = value + code * (64 ^ (3 - count))
236+ count = count + 1
237+ if count == 4 then
238+ count = 0
239+ local c1 = floor(value / 65536)
240+ local c2 = floor(value % 65536 / 256)
241+ local c3 = value % 256
242+ insert(parts, strchar(c1, c2, c3))
243+ value = 0
244+ end
245+ elseif char == "=" then
246+ insert(parts, strchar(floor(value / 65536)));
247+ if index >= length or sub(data, index + 1, index + 1) ~= "=" then
248+ insert(parts, strchar(floor(value % 65536 / 256)));
249+ end
250+ break
251+ end
252+ index = index + 1
253+ end
254+ arr[i] = concat(parts)
255+ end
256+ end
257+ end
258+ ]] ;
259+
260+ local parser = Parser :new ({
261+ LuaVersion = LuaVersion .Lua51 ;
262+ });
263+
264+ local newAst = parser :parse (base64DecodeCode );
265+ local forStat = newAst .body .statements [1 ];
266+ forStat .body .scope :setParent (ast .body .scope );
267+
268+ visitast (newAst , nil , function (node , data )
269+ if (node .kind == AstKind .VariableExpression ) then
270+ if (node .scope :getVariableName (node .id ) == " ARR" ) then
271+ data .scope :removeReferenceToHigherScope (node .scope , node .id );
272+ data .scope :addReferenceToHigherScope (self .rootScope , self .arrId );
273+ node .scope = self .rootScope ;
274+ node .id = self .arrId ;
275+ end
276+
277+ if (node .scope :getVariableName (node .id ) == " LOOKUP_TABLE" ) then
278+ data .scope :removeReferenceToHigherScope (node .scope , node .id );
279+ return self :createBase64Lookup ();
280+ end
281+ end
282+ end )
283+
284+ table.insert (ast .body .statements , 1 , forStat );
285+ end
286+ end
287+
288+ function ConstantArray :createBase64Lookup ()
289+ local entries = {};
290+ local i = 0 ;
291+ for char in string.gmatch (self .base64chars , " ." ) do
292+ table.insert (entries , Ast .KeyedTableEntry (Ast .StringExpression (char ), Ast .NumberExpression (i )));
293+ i = i + 1 ;
294+ end
295+ util .shuffle (entries );
296+ return Ast .TableConstructorExpression (entries );
297+ end
298+
299+ function ConstantArray :encode (str )
300+ if self .Encoding == " base64" then
301+ return ((str :gsub (' .' , function (x )
302+ local r ,b = ' ' ,x :byte ()
303+ for i = 8 ,1 ,- 1 do r = r .. (b % 2 ^ i - b % 2 ^ (i - 1 )> 0 and ' 1' or ' 0' ) end
304+ return r ;
305+ end ).. ' 0000' ):gsub (' %d%d%d?%d?%d?%d?' , function (x )
306+ if (# x < 6 ) then return ' ' end
307+ local c = 0
308+ for i = 1 ,6 do c = c + (x :sub (i ,i )== ' 1' and 2 ^ (6 - i ) or 0 ) end
309+ return self .base64chars :sub (c + 1 ,c + 1 )
310+ end ).. ({ ' ' , ' ==' , ' =' })[# str % 3 + 1 ]);
311+ end
193312end
194313
195314function ConstantArray :apply (ast , pipeline )
196315 self .rootScope = ast .body .scope ;
197316 self .arrId = self .rootScope :addVariable ();
198317
318+ self .base64chars = table.concat (util .shuffle {
319+ " A" , " B" , " C" , " D" , " E" , " F" , " G" , " H" , " I" , " J" , " K" , " L" , " M" , " N" , " O" , " P" , " Q" , " R" , " S" , " T" , " U" , " V" , " W" , " X" , " Y" , " Z" ,
320+ " a" , " b" , " c" , " d" , " e" , " f" , " g" , " h" , " i" , " j" , " k" , " l" , " m" , " n" , " o" , " p" , " q" , " r" , " s" , " t" , " u" , " v" , " w" , " x" , " y" , " z" ,
321+ " 0" , " 1" , " 2" , " 3" , " 4" , " 5" , " 6" , " 7" , " 8" , " 9" ,
322+ " +" , " /" ,
323+ });
324+
199325 self .constants = {};
200326 self .lookup = {};
201327
@@ -331,6 +457,8 @@ function ConstantArray:apply(ast, pipeline)
331457 end
332458 end );
333459
460+ self :addDecodeCode (ast );
461+
334462 local steps = util .shuffle ({
335463 -- Add Wrapper Function Code
336464 function ()
0 commit comments