Skip to content

Instantly share code, notes, and snippets.

@Toliak
Last active February 22, 2020 15:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Toliak/8d1d2004d59802a4584c8e665d4f40d9 to your computer and use it in GitHub Desktop.
Save Toliak/8d1d2004d59802a4584c8e665d4f40d9 to your computer and use it in GitHub Desktop.
MTA encodeString filesize fix
local KEY = "sampletext"
addEventHandler("onResourceStart", resourceRoot, function()
-- ENCODE
local file = File.open("test.txt")
local raw = file:read(file.size)
file:close()
local len = #raw
local lent = {}
-- file length to 256-digit notation
local len0 = len
while len0 > 0 do
table.insert(lent, 1, len0 % 256)
len0 = math.floor(len0 / 256)
end
table.insert(lent, #lent) -- #lent < 255
-- put \0 bytes. Expected length %4 == 0 , where length = file length + length of file length
local deltafile = (4 - len % 4)
local deltatable = #lent % 4
local lendelta = (deltafile < deltatable) and (4 - deltatable + deltafile) or deltafile - deltatable
for i = 1, lendelta do
table.insert(lent, 1, 0)
end
raw = raw .. string.char(unpack(lent))
local encoded = encodeString(
"tea",
raw,
{ key = KEY }
)
local file = File.new("encoded")
file:write(encoded)
file:close()
-- DECODE
local decoded = decodeString(
"tea",
encoded,
{ key = KEY }
)
local declen = #decoded
local lentlen = decoded:sub(declen, declen):byte(1)
local lent = 0
for i = 1, lentlen do
lent = lent + decoded:sub(declen - i, declen - i):byte(1) * 256 ^ (i - 1)
end
local decodedcut = decoded:sub(1, lent)
local file = File.new("decoded.txt")
file:write(decodedcut)
file:close()
outputDebugString("Ready!")
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment