Search code examples
autohotkey

Check that two buffers are equal


I would like to check if two binary files have the same content. They are loaded in Buffers and I haven't found a method to check that two Buffers are equal.

I came with this solution using NumGet

BufferEqual(Buf1, Buf2) {
    if Buf1.Size != Buf2.Size {
        return False
    }

    Int64Chunks := Buf1.Size // 8
    Int8Chunks := Mod(Buf1.Size, 8)

    Loop Int64Chunks {
        Chunk1 := NumGet(Buf1, (A_Index - 1) * 8, "Int64")
        Chunk2 := NumGet(Buf2, (A_Index - 1) * 8, "Int64")
        if Chunk1 != Chunk2 {
            return False
        }
    }

    Loop Int8Chunks {
        Chunk1 := NumGet(Buf1, Int64Chunks * 8 + A_Index - 1, "Char")
        Chunk2 := NumGet(Buf2, Int64Chunks * 8 + A_Index - 1, "Char")
        if Chunk1 != Chunk2 {
            return False
        }
    }

   return True
}

Am I missing an obvious way to do that?


Solution

  • memcmp can be used

    BufferEqual(Buf1, Buf2) {
        return Buf1.Size == Buf2.Size && DllCall("msvcrt\memcmp", "Ptr", Buf1, "Ptr", Buf2, "Ptr", Buf1.Size) == 0
    }