Joakim Hjalmarsson
Joakim Hjalmarsson

Reputation: 29

How to run lua bytecode from variable?

So I am working on a custom lua bytecode thing and when i run this code nothing happens but if i print the dec variable and then copy it and do loadstring("long bytecode thing")() then it works but not with my thing, could someone pelase point out what is wrong?

local OVal = 999
local SVal = 754

local function customEnc(STR)
    local currentVar = ""
    local strings = {}
    STR:gsub(".", function(c)
        if c == [[\]] then
            local newVar = tonumber(currentVar)
            if newVar == nil then
            else
                newVar = newVar * OVal /SVal
                table.insert(strings, tostring(newVar))
                newVar = ""
            end
            currentVar = ""
        else
            currentVar = currentVar .. c
        end
    end)
    local newString = ""
    for i,v in pairs(strings) do
        newString = newString .. [[\]] .. v
    end
    newString = newString .. [[\]]
    return newString
    end
    
    local function customDec(STR)
        local currentVar = ""
        local strings = {}
        STR:gsub(".", function(c)
            if c == [[\]] then
                local newVar = tonumber(currentVar)
                if newVar == nil then
                else
                    newVar = newVar / OVal *SVal
                    table.insert(strings, tostring(newVar))
                    newVar = ""
                end
                currentVar = ""
            else
                currentVar = currentVar .. c
            end
        end)
        local newString = ""
        for i,v in pairs(strings) do
            newString = newString .. [[\]] .. string.format("%.0f",v)
        end
        return newString
    end
local enc = customEnc([[\112\114\105\110\116\40\34\72\101\108\108\111\32\87\111\114\108\100\34\41\10]])
local dec = customDec(enc) .. [[\10]]
loadstring(dec)()

Upvotes: 1

Views: 426

Answers (1)

Nifim
Nifim

Reputation: 5021

"\100" is not the same as [[\100]] the first results in a single char and the second in 4 chars.

You also did not reverse your math in the customDec function

local function customEnc(STR)
  local currentVar = ""
  local strings = {}
  STR:gsub(".", function(c)
      local newVar = string.byte(c) * OVal / SVal
      table.insert(strings, string.char(newVar))
    end)
  local newString = ""
  for i,v in ipairs(strings) do
    newString = newString .. v
  end
  newString = newString
  return newString
end

local function customDec(STR)
  local currentVar = ""
  local strings = {}
  STR:gsub(".", function(c)
      local newVar = string.byte(c) / OVal * SVal
      table.insert(strings, string.char(newVar))
    end)
  local newString = ""
  for i,v in ipairs(strings) do
    newString = newString .. v
  end
  return newString
end

local enc = customEnc("\112\114\105\110\116\40\34\72\101\108\108\111\32\87\111\114\108\100\34\41\10")
local dec = customDec(enc)
loadstring(dec)()

Upvotes: 1

Related Questions