News:

A forum for users of LackeyCCG

Main Menu
Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - lionelpx

#1
Plugins & Plugin Creation Forum / Re: Checksums unveiled?
November 15, 2022, 11:15:00 AM
I am pleased to let you know I have cracked this  8).

This is what you need to understand from Trevor's code:
  • Yes, it does consume the EOF -1 (\xFF) value, so you need to add it yourself
  • It does read each char as a signed char because in his code, Sum is a (signed) int so, even though he forces temp_char to be cast to unisgned char, it gets cast back to a signed char immediately because of the compound assignment to a signed int. That's how assignment works in C. The cast has no impact, you can remove/ignore it.
  • And the final very sneaky thing: it uses the C modulo operator %. The modulo operator in C does not work as it does in Python (see Guido's explanation). The C modulo is truncated while the Python modulo is floored. While the Python behavior makes sense (in numbers theory, you would go that way), it yields different values for negative integers, which is what makes us fail here. So you have to use a proper C-style truncated modulo implementation. Luckily, Python offers math.fmod.

So this is what you get:
import math

def checksum(path):
    value = 0
    with open(path, "rb") as fp:
        char = fp.peek(1)
        while char:
            char = fp.read(1)
            if char in [b"\n", b"\r"]:
                continue
            if char:
                value += int.from_bytes(char, byteorder="big", signed=True)
            else:
                value -= 1
            value = int(math.fmod(value, 100000000))
    return value

On my end, it works for all files, text or binary, images, and sounds alike. Have fun!