Skip to content

Instantly share code, notes, and snippets.

@Toliak
Last active February 22, 2020 15:29
Show Gist options
  • Select an option

  • Save Toliak/8d1d2004d59802a4584c8e665d4f40d9 to your computer and use it in GitHub Desktop.

Select an option

Save Toliak/8d1d2004d59802a4584c8e665d4f40d9 to your computer and use it in GitHub Desktop.

Revisions

  1. Toliak revised this gist Jul 8, 2018. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions encodeString-filesize-fix.lua
    Original file line number Diff line number Diff line change
    @@ -20,12 +20,10 @@ addEventHandler("onResourceStart", resourceRoot, function()
    -- put \0 bytes. Expected length %4 == 0 , where length = file length + length of file length
    local deltafile = (4 - len % 4)
    local deltatable = #lent % 4
    iprint(deltatable, deltafile)
    local lendelta = (deltafile < deltatable) and (4 - deltatable + deltafile) or deltafile - deltatable
    for i = 1, lendelta do
    table.insert(lent, 1, 0)
    end
    iprint(lent)

    raw = raw .. string.char(unpack(lent))

    @@ -50,7 +48,6 @@ addEventHandler("onResourceStart", resourceRoot, function()
    local declen = #decoded
    local lentlen = decoded:sub(declen, declen):byte(1)
    local lent = 0
    iprint(lentlen)
    for i = 1, lentlen do
    lent = lent + decoded:sub(declen - i, declen - i):byte(1) * 256 ^ (i - 1)
    end
  2. Toliak created this gist Jul 8, 2018.
    65 changes: 65 additions & 0 deletions encodeString-filesize-fix.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    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
    iprint(deltatable, deltafile)
    local lendelta = (deltafile < deltatable) and (4 - deltatable + deltafile) or deltafile - deltatable
    for i = 1, lendelta do
    table.insert(lent, 1, 0)
    end
    iprint(lent)

    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
    iprint(lentlen)
    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)