Skip to content

Instantly share code, notes, and snippets.

@ExtReMLapin
Created December 9, 2020 11:52

Revisions

  1. ExtReMLapin created this gist Dec 9, 2020.
    44 changes: 44 additions & 0 deletions binaryluadiff.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    assert(#arg == 2)
    local source
    local dest
    local f1 = assert(io.open(arg[1], "rb"), "cannot open " .. arg[1])
    source = f1:read("*all")
    f1:close()
    local f2 = assert(io.open(arg[2], "rb"), "cannot open " .. arg[2])
    dest = f2:read("*all")
    f2:close()
    assert(#source == #dest)
    print("ok")
    print[[
    | Position | OriginalBytes | PatchedBytes |
    |----------|:-------------:|--------------:|]]


    local i = 1
    local max = #source

    while (i <= max) do
    if source:sub(i, i) ~= dest:sub(i, i) then
    local i2 = 0
    local tmpTbl = {}

    while (source:sub(i + i2, i + i2) ~= dest:sub(i + i2, i + i2)) do
    table.insert(tmpTbl, { string.format('%02X',(source:sub(i + i2, i + i2):byte())), string.format('%02X',(dest:sub(i + i2, i + i2):byte()))})

    i2 = i2 + 1
    end

    local originalBytes = ""
    local patchedBytes = ""

    for k, v in ipairs(tmpTbl) do
    originalBytes = originalBytes .. v[1] .. " "
    patchedBytes = patchedBytes .. v[2] .. " "
    end

    print(string.format("| 0x%s | %s | %s |", string.format('%08X', i), originalBytes, patchedBytes))
    i = i + i2
    end

    i = i + 1
    end