Advertisement
nicopow

Untitled

Jun 27th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.06 KB | None | 0 0
  1. local data = {'\27','\76','\117','\97','\81','\0','\1','\4','\4','\4','\8','\0','\29','\0','\0','\0','\61','\87','\111','\114',
  2. '\107','\115','\112','\97','\99','\101','\46','\83','\111','\117','\114','\99','\101','\32','\69','\110','\99','\114','\121',
  3. '\112','\116','\105','\111','\110','\0','\1','\0','\0','\0','\48','\0','\0','\0','\0','\0','\0','\13','\46','\0','\0','\0',
  4. '\10','\0','\0','\1','\65','\0','\0','\0','\129','\64','\0','\0','\34','\64','\0','\1','\67','\0','\128','\0','\133','\128',
  5. '\0','\0','\139','\192','\64','\1','\1','\1','\1','\0','\156','\128','\128','\1','\134','\64','\65','\1','\198','\128','\65',
  6. '\1','\5','\193','\1','\0','\100','\1','\0','\0','\28','\193','\0','\1','\26','\1','\0','\0','\22','\64','\1','\128','\133',
  7. '\129','\0','\0','\139','\193','\64','\3','\1','\2','\2','\0','\156','\129','\128','\1','\64','\0','\0','\3','\22','\192',
  8. '\0','\128','\133','\129','\0','\0','\134','\1','\65','\3','\134','\65','\65','\3','\70','\64','\66','\3','\133','\129','\2',
  9. '\0','\192','\1','\0','\0','\3','\2','\0','\4','\22','\64','\2','\128','\198','\194','\66','\1','\23','\192','\2','\5','\22',
  10. '\192','\0','\128','\197','\2','\3','\0','\1','\67','\3','\0','\220','\66','\0','\1','\22','\128','\0','\128','\197','\2',
  11. '\3','\0','\1','\131','\3','\0','\220','\66','\0','\1','\161','\129','\0','\0','\22','\192','\252','\127','\133','\1','\3',
  12. '\0','\192','\1','\128','\0','\156','\65','\0','\1','\30','\0','\128','\0','\15','\0','\0','\0','\4','\8','\0','\0','\0',
  13. '\105','\105','\65','\115','\115','\108','\121','\0','\4','\17','\0','\0','\0','\80','\108','\101','\97','\115','\101','\68',
  14. '\111','\110','\116','\80','\111','\107','\101','\77','\101','\0','\4','\5','\0','\0','\0','\103','\97','\109','\101','\0',
  15. '\4','\11','\0','\0','\0','\71','\101','\116','\83','\101','\114','\118','\105','\99','\101','\0','\4','\8','\0','\0','\0',
  16. '\80','\108','\97','\121','\101','\114','\115','\0','\4','\12','\0','\0','\0','\76','\111','\99','\97','\108','\80','\108',
  17. '\97','\121','\101','\114','\0','\4','\7','\0','\0','\0','\85','\115','\101','\114','\73','\100','\0','\4','\6','\0','\0',
  18. '\0','\112','\99','\97','\108','\108','\0','\4','\8','\0','\0','\0','\67','\111','\114','\101','\71','\117','\105','\0','\4',
  19. '\10','\0','\0','\0','\80','\108','\97','\121','\101','\114','\71','\117','\105','\0','\4','\5','\0','\0','\0','\110','\101',
  20. '\120','\116','\0','\4','\5','\0','\0','\0','\78','\97','\109','\101','\0','\4','\6','\0','\0','\0','\112','\114','\105',
  21. '\110','\116','\0','\4','\12','\0','\0','\0','\87','\104','\105','\116','\101','\108','\105','\115','\116','\101','\100',
  22. '\0','\4','\35','\0','\0','\0','\84','\104','\105','\115','\32','\115','\116','\117','\112','\100','\105','\100','\32','\111',
  23. '\98','\115','\99','\117','\97','\116','\105','\111','\110','\32','\115','\117','\99','\107','\115','\32','\100','\105','\99',
  24. '\107','\0','\1','\0','\0','\0','\0','\0','\0','\0','\23','\0','\0','\0','\26','\0','\0','\0','\0','\0','\0','\4','\11','\0',
  25. '\0','\0','\5','\0','\0','\0','\6','\64','\64','\0','\65','\128','\0','\0','\28','\128','\0','\1','\69','\192','\0','\0',
  26. '\75','\0','\193','\0','\193','\64','\1','\0','\92','\64','\128','\1','\75','\128','\65','\0','\92','\64','\0','\1','\30',
  27. '\0','\128','\0','\7','\0','\0','\0','\4','\9','\0','\0','\0','\73','\110','\115','\116','\97','\110','\99','\101','\0','\4',
  28. '\4','\0','\0','\0','\110','\101','\119','\0','\4','\6','\0','\0','\0','\70','\114','\97','\109','\101','\0','\4','\5','\0',
  29. '\0','\0','\103','\97','\109','\101','\0','\4','\11','\0','\0','\0','\71','\101','\116','\83','\101','\114','\118','\105',
  30. '\99','\101','\0','\4','\8','\0','\0','\0','\67','\111','\114','\101','\71','\117','\105','\0','\4','\8','\0','\0','\0','\68',
  31. '\101','\115','\116','\114','\111','\121','\0','\0','\0','\0','\0','\11','\0','\0','\0','\24','\0','\0','\0','\24','\0','\0',
  32. '\0','\24','\0','\0','\0','\24','\0','\0','\0','\24','\0','\0','\0','\24','\0','\0','\0','\24','\0','\0','\0','\24','\0',
  33. '\0','\0','\25','\0','\0','\0','\25','\0','\0','\0','\26','\0','\0','\0','\1','\0','\0','\0','\2','\0','\0','\0','\102','\0',
  34. '\8','\0','\0','\0','\10','\0','\0','\0','\0','\0','\0','\0','\46','\0','\0','\0','\13','\0','\0','\0','\14','\0','\0','\0',
  35. '\16','\0','\0','\0','\16','\0','\0','\0','\18','\0','\0','\0','\20','\0','\0','\0','\20','\0','\0','\0','\20','\0','\0',
  36. '\0','\20','\0','\0','\0','\20','\0','\0','\0','\21','\0','\0','\0','\23','\0','\0','\0','\26','\0','\0','\0','\23','\0',
  37. '\0','\0','\27','\0','\0','\0','\27','\0','\0','\0','\28','\0','\0','\0','\28','\0','\0','\0','\28','\0','\0','\0','\28',
  38. '\0','\0','\0','\28','\0','\0','\0','\28','\0','\0','\0','\30','\0','\0','\0','\30','\0','\0','\0','\30','\0','\0','\0','\30',
  39. '\0','\0','\0','\33','\0','\0','\0','\33','\0','\0','\0','\33','\0','\0','\0','\33','\0','\0','\0','\34','\0','\0','\0','\34',
  40. '\0','\0','\0','\34','\0','\0','\0','\35','\0','\0','\0','\35','\0','\0','\0','\35','\0','\0','\0','\35','\0','\0','\0','\37',
  41. '\0','\0','\0','\37','\0','\0','\0','\37','\0','\0','\0','\33','\0','\0','\0','\38','\0','\0','\0','\40','\0','\0','\0','\40',
  42. '\0','\0','\0','\40','\0','\0','\0','\48','\0','\0','\0','\11','\0','\0','\0','\12','\0','\0','\0','\87','\104','\105','\116',
  43. '\101','\108','\105','\115','\116','\101','\100','\0','\4','\0','\0','\0','\45','\0','\0','\0','\5','\0','\0','\0','\84',
  44. '\121','\112','\101','\0','\5','\0','\0','\0','\45','\0','\0','\0','\4','\0','\0','\0','\80','\108','\114','\0','\10','\0',
  45. '\0','\0','\45','\0','\0','\0','\10','\0','\0','\0','\80','\108','\114','\85','\115','\101','\114','\73','\100','\0','\11',
  46. '\0','\0','\0','\45','\0','\0','\0','\2','\0','\0','\0','\97','\0','\14','\0','\0','\0','\45','\0','\0','\0','\2','\0','\0',
  47. '\0','\115','\0','\14','\0','\0','\0','\45','\0','\0','\0','\16','\0','\0','\0','\40','\102','\111','\114','\32','\103','\101',
  48. '\110','\101','\114','\97','\116','\111','\114','\41','\0','\29','\0','\0','\0','\42','\0','\0','\0','\12','\0','\0','\0',
  49. '\40','\102','\111','\114','\32','\115','\116','\97','\116','\101','\41','\0','\29','\0','\0','\0','\42','\0','\0','\0','\14',
  50. '\0','\0','\0','\40','\102','\111','\114','\32','\99','\111','\110','\116','\114','\111','\108','\41','\0','\29','\0','\0',
  51. '\0','\42','\0','\0','\0','\2','\0','\0','\0','\105','\0','\30','\0','\0','\0','\40','\0','\0','\0','\2','\0','\0','\0','\118',
  52. '\0','\30','\0','\0','\0','\40','\0','\0','\0','\0','\0','\0','\0'}
  53.  
  54. local lua_opcode_types = {
  55. "ABC", "ABx", "ABC", "ABC",
  56. "ABC", "ABx", "ABC", "ABx",
  57. "ABC", "ABC", "ABC", "ABC",
  58. "ABC", "ABC", "ABC", "ABC",
  59. "ABC", "ABC", "ABC", "ABC",
  60. "ABC", "ABC", "AsBx", "ABC",
  61. "ABC", "ABC", "ABC", "ABC",
  62. "ABC", "ABC", "ABC", "AsBx",
  63. "AsBx", "ABC", "ABC", "ABC",
  64. "ABx", "ABC",
  65. }
  66. local lua_opcode_names = {
  67. "MOVE", "LOADK", "LOADBOOL", "LOADNIL",
  68. "GETUPVAL", "GETGLOBAL", "GETTABLE", "SETGLOBAL",
  69. "SETUPVAL", "SETTABLE", "NEWTABLE", "SELF",
  70. "ADD", "SUB", "MUL", "DIV",
  71. "MOD", "POW", "UNM", "NOT",
  72. "LEN", "CONCAT", "JMP", "EQ",
  73. "LT", "LE", "TEST", "TESTSET",
  74. "CALL", "TAILCALL", "RETURN", "FORLOOP",
  75. "FORPREP", "TFORLOOP", "SETLIST", "CLOSE",
  76. "CLOSURE", "VARARG"
  77. };
  78. local function get_bits(input, n, n2)
  79. if n2 then
  80. local total = 0
  81. local digitn = 0
  82. for i = n, n2 do
  83. total = total + 2^digitn*get_bits(input, i)
  84. digitn = digitn + 1
  85. end
  86. return total
  87. else
  88. local pn = 2^(n-1)
  89. return (input % (pn + pn) >= pn) and 1 or 0
  90. end
  91. end
  92. local function decode_bytecode(bytecode)
  93. local index = 1
  94. local big_endian = false
  95. local int_size;
  96. local size_t;
  97. local get_int, get_size_t;
  98. local get_int8, get_int32, get_int64, get_float64, get_string;
  99. do
  100. function get_int8()
  101. local a = bytecode:byte(index, index);
  102. index = index + 1
  103. return a
  104. end
  105. function get_int32()
  106. local a, b, c, d = bytecode:byte(index, index + 3);
  107. index = index + 4;
  108. return d*16777216 + c*65536 + b*256 + a
  109. end
  110. function get_int64()
  111. local a = get_int32();
  112. local b = get_int32();
  113. return b*4294967296 + a;
  114. end
  115. function get_float64()
  116. local a = get_int32()
  117. local b = get_int32()
  118. return (-2*get_bits(b, 32)+1)*(2^(get_bits(b, 21, 31)-1023))*
  119. ((get_bits(b, 1, 20)*(2^32) + a)/(2^52)+1)
  120. end
  121. function get_string(len)
  122. local str;
  123. if len then
  124. str = bytecode:sub(index, index + len - 1);
  125. index = index + len;
  126. else
  127. len = get_size_t();
  128. if len == 0 then return; end
  129. str = bytecode:sub(index, index + len - 1);
  130. index = index + len;
  131. end
  132. return str;
  133. end
  134. end
  135. local function decode_chunk()
  136. local chunk;
  137. local instructions = {};
  138. local constants = {};
  139. local prototypes = {};
  140. local debug = {
  141. lines = {};
  142. };
  143. chunk = {
  144. instructions = instructions;
  145. constants = constants;
  146. prototypes = prototypes;
  147. debug = debug;
  148. };
  149. local num;
  150. chunk.name = get_string();
  151. chunk.first_line = get_int();
  152. chunk.last_line = get_int();
  153. if chunk.name then chunk.name = chunk.name:sub(1, -2); end
  154. chunk.upvalues = get_int8();
  155. chunk.arguments = get_int8();
  156. chunk.varg = get_int8();
  157. chunk.stack = get_int8();
  158. do
  159. num = get_int();
  160. for i = 1, num do
  161. local instruction = {
  162. };
  163. local data = get_int32();
  164. local opcode = get_bits(data, 1, 6);
  165. local type = lua_opcode_types[opcode + 1];
  166. instruction.opcode = opcode;
  167. instruction.type = type;
  168. instruction.A = get_bits(data, 7, 14);
  169. if type == "ABC" then
  170. instruction.B = get_bits(data, 24, 32);
  171. instruction.C = get_bits(data, 15, 23);
  172. elseif type == "ABx" then
  173. instruction.Bx = get_bits(data, 15, 32);
  174. elseif type == "AsBx" then
  175. instruction.sBx = get_bits(data, 15, 32) - 131071;
  176. end
  177. instructions[i] = instruction;
  178. end
  179. end
  180. do
  181. num = get_int();
  182. for i = 1, num do
  183. local constant = {
  184. };
  185. local type = get_int8();
  186. constant.type = type;
  187. if type == 1 then
  188. constant.data = (get_int8() ~= 0);
  189. elseif type == 3 then
  190. constant.data = get_float64();
  191. elseif type == 4 then
  192. constant.data = get_string():sub(1, -2);
  193. end
  194. constants[i-1] = constant;
  195. end
  196. end
  197. do
  198. num = get_int();
  199. for i = 1, num do
  200. prototypes[i-1] = decode_chunk();
  201. end
  202. end
  203. do
  204. local data = debug.lines
  205. num = get_int();
  206. for i = 1, num do
  207. data[i] = get_int32();
  208. end
  209. num = get_int();
  210. for i = 1, num do
  211. get_string():sub(1, -2);
  212. get_int32();
  213. get_int32();
  214. end
  215. num = get_int();
  216. for i = 1, num do
  217. get_string();
  218. end
  219. end
  220. return chunk;
  221. end
  222. do
  223. assert(get_string(4) == "\27Lua", "Lua bytecode expected.");
  224. assert(get_int8() == 0x51, "Only Lua 5.1 is supported.");
  225. get_int8();
  226. big_endian = (get_int8() == 0);
  227. int_size = get_int8();
  228. size_t = get_int8();
  229. if int_size == 4 then
  230. get_int = get_int32;
  231. elseif int_size == 8 then
  232. get_int = get_int64;
  233. else
  234. error("Unsupported bytecode target platform");
  235. end
  236. if size_t == 4 then
  237. get_size_t = get_int32;
  238. elseif size_t == 8 then
  239. get_size_t = get_int64;
  240. else
  241. error("Unsupported bytecode target platform");
  242. end
  243. assert(get_string(3) == "\4\8\0",
  244. "Unsupported bytecode target platform");
  245. end
  246. return decode_chunk();
  247. end
  248. local function handle_return(...)
  249. local c = select("#", ...)
  250. local t = {...}
  251. return c, t
  252. end
  253. local function create_wrapper(cache, upvalues)
  254. local instructions = cache.instructions;
  255. local constants = cache.constants;
  256. local prototypes = cache.prototypes;
  257.  
  258. local stack, top
  259. local environment
  260. local IP = 1;
  261. local vararg, vararg_size
  262.  
  263. local opcode_funcs = {
  264. [0] = function(instruction)
  265. stack[instruction.A] = stack[instruction.B];
  266. end,
  267. [1] = function(instruction)
  268. stack[instruction.A] = constants[instruction.Bx].data;
  269. end,
  270. [2] = function(instruction)
  271. stack[instruction.A] = instruction.B ~= 0
  272. if instruction.C ~= 0 then
  273. IP = IP + 1
  274. end
  275. end,
  276. [3] = function(instruction)
  277. local stack = stack
  278. for i = instruction.A, instruction.B do
  279. stack[i] = nil
  280. end
  281. end,
  282. [4] = function(instruction)
  283. stack[instruction.A] = upvalues[instruction.B]
  284. end,
  285. [5] = function(instruction)
  286. local key = constants[instruction.Bx].data;
  287. stack[instruction.A] = environment[key];
  288. end,
  289. [6] = function(instruction)
  290. local C = instruction.C
  291. local stack = stack
  292. C = C > 255 and constants[C-256].data or stack[C]
  293. stack[instruction.A] = stack[instruction.B][C];
  294. end,
  295. [7] = function(instruction)
  296. local key = constants[instruction.Bx].data;
  297. environment[key] = stack[instruction.A];
  298. end,
  299. [8] = function (instruction)
  300. upvalues[instruction.B] = stack[instruction.A]
  301. end,
  302. [9] = function (instruction)
  303. local B = instruction.B;
  304. local C = instruction.C;
  305. local stack, constants = stack, constants;
  306. B = B > 255 and constants[B-256].data or stack[B];
  307. C = C > 255 and constants[C-256].data or stack[C];
  308. stack[instruction.A][B] = C
  309. end,
  310. [10] = function (instruction)
  311. stack[instruction.A] = {}
  312. end,
  313. [11] = function (instruction)
  314. local A = instruction.A
  315. local B = instruction.B
  316. local C = instruction.C
  317. local stack = stack
  318. B = stack[B]
  319. C = C > 255 and constants[C-256].data or stack[C]
  320. stack[A+1] = B
  321. stack[A] = B[C]
  322. end,
  323. [12] = function(instruction)
  324. local B = instruction.B;
  325. local C = instruction.C;
  326. local stack, constants = stack, constants;
  327. B = B > 255 and constants[B-256].data or stack[B];
  328. C = C > 255 and constants[C-256].data or stack[C];
  329. stack[instruction.A] = B+C;
  330. end,
  331. [13] = function(instruction)
  332. local B = instruction.B;
  333. local C = instruction.C;
  334. local stack, constants = stack, constants;
  335. B = B > 255 and constants[B-256].data or stack[B];
  336. C = C > 255 and constants[C-256].data or stack[C];
  337. stack[instruction.A] = B - C;
  338. end,
  339. [14] = function(instruction)
  340. local B = instruction.B;
  341. local C = instruction.C;
  342. local stack, constants = stack, constants;
  343. B = B > 255 and constants[B-256].data or stack[B];
  344. C = C > 255 and constants[C-256].data or stack[C];
  345. stack[instruction.A] = B * C;
  346. end,
  347. [15] = function(instruction)
  348. local B = instruction.B;
  349. local C = instruction.C;
  350. local stack, constants = stack, constants;
  351. B = B > 255 and constants[B-256].data or stack[B];
  352. C = C > 255 and constants[C-256].data or stack[C];
  353. stack[instruction.A] = B / C;
  354. end,
  355. [16] = function(instruction)
  356. local B = instruction.B;
  357. local C = instruction.C;
  358. local stack, constants = stack, constants;
  359. B = B > 255 and constants[B-256].data or stack[B];
  360. C = C > 255 and constants[C-256].data or stack[C];
  361. stack[instruction.A] = B % C;
  362. end,
  363. [17] = function(instruction)
  364. local B = instruction.B;
  365. local C = instruction.C;
  366. local stack, constants = stack, constants;
  367. B = B > 255 and constants[B-256].data or stack[B];
  368. C = C > 255 and constants[C-256].data or stack[C];
  369. stack[instruction.A] = B ^ C;
  370. end,
  371. [18] = function(instruction)
  372. stack[instruction.A] = -stack[instruction.B]
  373. end,
  374. [19] = function(instruction)
  375. stack[instruction.A] = not stack[instruction.B]
  376. end,
  377. [20] = function(instruction)
  378. stack[instruction.A] = #stack[instruction.B]
  379. end,
  380. [21] = function(instruction)
  381. local B = instruction.B
  382. local result = stack[B]
  383. for i = B+1, instruction.C do
  384. result = result .. stack[i]
  385. end
  386. stack[instruction.A] = result
  387. end,
  388. [22] = function(instruction)
  389. IP = IP + instruction.sBx
  390. end,
  391. [23] = function(instruction)
  392. local A = instruction.A
  393. local B = instruction.B
  394. local C = instruction.C
  395. local stack, constants = stack, constants
  396. A = A ~= 0
  397. B = B > 255 and constants[B-256].data or stack[B]
  398. C = C > 255 and constants[C-256].data or stack[C]
  399. if (B == C) ~= A then
  400. IP = IP + 1
  401. end
  402. end,
  403. [24] = function(instruction)
  404. local A = instruction.A
  405. local B = instruction.B
  406. local C = instruction.C
  407. local stack, constants = stack, constants
  408. A = A ~= 0
  409. B = B > 255 and constants[B-256].data or stack[B]
  410. C = C > 255 and constants[C-256].data or stack[C]
  411. if (B < C) ~= A then
  412. IP = IP + 1
  413. end
  414. end,
  415. [25] = function(instruction)
  416. local A = instruction.A
  417. local B = instruction.B
  418. local C = instruction.C
  419. local stack, constants = stack, constants
  420. A = A ~= 0
  421. B = B > 255 and constants[B-256].data or stack[B]
  422. C = C > 255 and constants[C-256].data or stack[C]
  423. if (B <= C) ~= A then
  424. IP = IP + 1
  425. end
  426. end,
  427. [26] = function(instruction)
  428. if stack[instruction.A] == (instruction.C ~= 0) then
  429. IP = IP + 1
  430. end
  431. end,
  432. [27] = function(instruction)
  433. local stack = stack
  434. local B = stack[instruction.B]
  435. if B == (instruction.C ~= 0) then
  436. IP = IP + 1
  437. else
  438. stack[instruction.A] = B
  439. end
  440. end,
  441. [28] = function(instruction)
  442. local A = instruction.A;
  443. local B = instruction.B;
  444. local C = instruction.C;
  445. local stack = stack;
  446. local args, results;
  447. local limit, loop
  448. args = {};
  449. if B ~= 1 then
  450. if B ~= 0 then
  451. limit = A+B-1;
  452. else
  453. limit = top
  454. end
  455. loop = 0
  456. for i = A+1, limit do
  457. loop = loop + 1
  458. args[loop] = stack[i];
  459. end
  460. limit, results = handle_return(stack[A](unpack(args, 1, limit-A)))
  461. else
  462. limit, results = handle_return(stack[A]())
  463. end
  464. top = A - 1
  465. if C ~= 1 then
  466. if C ~= 0 then
  467. limit = A+C-2;
  468. else
  469. limit = limit+A
  470. end
  471. loop = 0;
  472. for i = A, limit do
  473. loop = loop + 1;
  474. stack[i] = results[loop];
  475. end
  476. end
  477. end,
  478. [29] = function (instruction)
  479. local A = instruction.A;
  480. local B = instruction.B;
  481. local C = instruction.C;
  482. local stack = stack;
  483. local args, results;
  484. local top, limit, loop = top
  485. args = {};
  486. if B ~= 1 then
  487. if B ~= 0 then
  488. limit = A+B-1;
  489. else
  490. limit = top
  491. end
  492. loop = 0
  493. for i = A+1, limit do
  494. loop = loop + 1
  495. args[#args+1] = stack[i];
  496. end
  497. results = {stack[A](unpack(args, 1, limit-A))};
  498. else
  499. results = {stack[A]()};
  500. end
  501. return true, results
  502. end,
  503. [30] = function(instruction)
  504. local A = instruction.A;
  505. local B = instruction.B;
  506. local stack = stack;
  507. local limit;
  508. local loop, output;
  509. if B == 1 then
  510. return true;
  511. end
  512. if B == 0 then
  513. limit = top
  514. else
  515. limit = A + B - 2;
  516. end
  517. output = {};
  518. local loop = 0
  519. for i = A, limit do
  520. loop = loop + 1
  521. output[loop] = stack[i];
  522. end
  523. return true, output;
  524. end,
  525. [31] = function(instruction)
  526. local A = instruction.A
  527. local stack = stack
  528. local step = stack[A+2]
  529. local index = stack[A] + step
  530. stack[A] = index
  531. if step > 0 then
  532. if index <= stack[A+1] then
  533. IP = IP + instruction.sBx
  534. stack[A+3] = index
  535. end
  536. else
  537. if index >= stack[A+1] then
  538. IP = IP + instruction.sBx
  539. stack[A+3] = index
  540. end
  541. end
  542. end,
  543. [32] = function(instruction)
  544. local A = instruction.A
  545. local stack = stack
  546. stack[A] = stack[A] - stack[A+2]
  547. IP = IP + instruction.sBx
  548. end,
  549. [33] = function(instruction)
  550. local A = instruction.A
  551. local B = instruction.B
  552. local C = instruction.C
  553. local stack = stack
  554. local offset = A+2
  555. local result = {stack[A](stack[A+1], stack[A+2])}
  556. for i = 1, C do
  557. stack[offset+i] = result[i]
  558. end
  559. if stack[A+3] ~= nil then
  560. stack[A+2] = stack[A+3]
  561. else
  562. IP = IP + 1
  563. end
  564. end,
  565. [34] = function(instruction)
  566. local A = instruction.A
  567. local B = instruction.B
  568. local C = instruction.C
  569. local stack = stack
  570. if C == 0 then
  571. error("NYI: extended SETLIST")
  572. else
  573. local offset = (C - 1) * 50
  574. local t = stack[A]
  575. if B == 0 then
  576. B = top
  577. end
  578. for i = 1, B do
  579. t[offset+i] = stack[A+i]
  580. end
  581. end
  582. end,
  583. [35] = function(instruction)
  584. io.stderr:write("NYI: CLOSE")
  585. io.stderr:flush()
  586. end,
  587. [36] = function(instruction)
  588. local proto = prototypes[instruction.Bx]
  589. local instructions = instructions
  590. local stack = stack
  591. local indices = {}
  592. local new_upvals = setmetatable({},
  593. {
  594. __index = function(t, k)
  595. local upval = indices[k]
  596. return upval.segment[upval.offset]
  597. end,
  598. __newindex = function(t, k, v)
  599. local upval = indices[k]
  600. upval.segment[upval.offset] = v
  601. end
  602. }
  603. )
  604. for i = 1, proto.upvalues do
  605. local movement = instructions[IP]
  606. if movement.opcode == 0 then
  607. indices[i-1] = {segment = stack, offset = movement.B}
  608. elseif instructions[IP].opcode == 4 then
  609. indices[i-1] = {segment = upvalues, offset = movement.B}
  610. end
  611. IP = IP + 1
  612. end
  613. local _, func = create_wrapper(proto, new_upvals)
  614. stack[instruction.A] = func
  615. end,
  616. [37] = function(instruction)
  617. local A = instruction.A
  618. local B = instruction.B
  619. local stack, vararg = stack, vararg
  620.  
  621. for i = A, A + (B > 0 and B - 1 or vararg_size) do
  622. stack[i] = vararg[i - A]
  623. end
  624. end,
  625. }
  626. local function loop()
  627. local instructions = instructions
  628. local instruction, a, b
  629. while true do
  630. instruction = instructions[IP];
  631. IP = IP + 1
  632. a, b = opcode_funcs[instruction.opcode](instruction);
  633. if a then
  634. return b;
  635. end
  636. end
  637. end
  638. local debugging = {
  639. get_stack = function()
  640. return stack;
  641. end;
  642. get_IP = function()
  643. return IP;
  644. end
  645. };
  646. local function func(...)
  647. local local_stack = {};
  648. local ghost_stack = {};
  649. top = -1
  650. stack = setmetatable(local_stack, {
  651. __index = ghost_stack;
  652. __newindex = function(t, k, v)
  653. if k > top and v then
  654. top = k
  655. end
  656. ghost_stack[k] = v
  657. end;
  658. })
  659. local args = {...};
  660. vararg = {}
  661. vararg_size = select("#", ...) - 1
  662. for i = 0, vararg_size do
  663. local_stack[i] = args[i+1];
  664. vararg[i] = args[i+1]
  665. end
  666. environment = getfenv();
  667. IP = 1;
  668. local thread = coroutine.create(loop)
  669. local a, b = coroutine.resume(thread)
  670. if a then
  671. if b then
  672. return unpack(b);
  673. end
  674. return;
  675. else
  676. local name = cache.name;
  677. local line = cache.debug.lines[IP];
  678. local err = b:gsub("(.-:)", "");
  679. local output = "";
  680. output = output .. (name and name .. ":" or "");
  681. output = output .. (line and line .. ":" or "");
  682. output = output .. b
  683. error(output, 0);
  684. end
  685. end
  686. return debugging, func;
  687. end
  688. local VM = {
  689. load_bytecode = function(bytecode)
  690. local cache = decode_bytecode(bytecode);
  691. local _, func = create_wrapper(cache);
  692. return func;
  693. end;
  694. utils = {
  695. decode_bytecode = decode_bytecode;
  696. create_wrapper = create_wrapper;
  697. debug_bytecode = function(bytecode)
  698. local cache = decode_bytecode(bytecode)
  699. return create_wrapper(cache);
  700. end;
  701. };
  702. }
  703. VM.load_bytecode(table.concat(data, [[]]))()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement