summaryrefslogtreecommitdiff
path: root/update/adler32
blob: 187e34d3d4093f5606d65dcac1b51e8fb355375e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3

"""
    Project: TheManaWorld
    Author: Bjorn Lindeijer, jak1
    License: GPLv3
    Description: Calculates adler32 checksums for all files passed as argument.
    Credits: adler32.c 2006 Bjorn Lindeijer
"""

from zlib import adler32
import sys
import os

def adler32sum(filepath):
    BLOCKSIZE = 256*1024*1024
    asum = 1
    with open(filepath, 'rb') as f:
        while (data := f.read(BLOCKSIZE)):
            asum = adler32(data, asum)
    return hex(asum)

def cmd_help():
        print(f"Usage: adler32.py <mode> <file>")
        print(f" mode: 0 prints file & checksum")
        print(f" mode: 1 prints only checksum")

if __name__ == "__main__":
    if len(sys.argv) >= 3:
        try:
            mode = int(sys.argv[1])
            zfiles = sys.argv[2:]
            for zfile in zfiles:
                file_exists = os.path.exists(zfile)
                if file_exists:
                    if mode == 0:
                        print(zfile + " " + adler32sum(zfile)[2:])
                    elif mode == 1:
                        print(adler32sum(zfile)[2:])
                    else:
                        cmd_help()
                else:
                    print("file not found!")
                    exit(-1)
        except ValueError:
            print("mode needs to be an integer value!")
            exit(-1)
    else:
        cmd_help()