Advertisement
nicopow

Untitled

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