Skip to content

Unpacking zlib compressed part #6

@hno

Description

@hno

The zlib compressed part is another compressed archive of files.

The following code is briefly copied from mikrotik-npk and adapted to python3, showing the archive parsing and dumping.

Missing zlib decompression (done using the zlib-flate tool when testing the code), and proper integration into the npkpy code base. Too rusty in Python object model to immediately figure out how this should be done proper.

import os
import argparse
import struct
from pathlib import Path

def parse_data(data):
        res = []
        while len(data)>30:
                header = data[:30]
                dsize = struct.unpack("I", header[24:28])[0]
                fsize = struct.unpack("H", header[28:30])[0]
                if len(data) - 30 - fsize - dsize < 0:
                        dsize = len(data) - 30 - fsize
                fstuff = data[30:30+fsize]
                dstuff = data[30+fsize:30+fsize+dsize]
                res.append({"header": header, "file": fstuff, "data": dstuff})
                data = data[30+fsize+dsize:]
        return res

def dump_data(data):
    print("Files in package:")
    for k in data:
        perm, type, z1, z2, tim = struct.unpack("BBHII", k["header"][:12])
        if type == 65:
            type = "dir"
            if not os.access(k["file"], os.F_OK):
                os.makedirs(k["file"])
        if type == 129:
            type = "fil"
            f = open(k["file"], "wb")
            f.write(k["data"])
            f.close()
            os.chmod(k["file"], int(perm))
        if type == 161:
            type = "sym"
            os.symlink(k["data"],k["file"])
            # os.chmod(k["file"], int(perm))
        if perm == 164:
            perm = "nx"
        if perm == 237:
            perm = "ex"
        print(type, perm, k["file"], tim)

def parse_args():
    parser = argparse.ArgumentParser()

    input_group = parser.add_argument_group("input")
    input_group.add_argument("--files",
                             action='append', type=Path, help="Select one or more files to process")

    return parser.parse_args()

if __name__ == "__main__":
    opts = parse_args()

    for file in opts.files:
        with open(file, "rb") as f:
            data = f.read()
            data = parse_data(data)
            dump_data(data)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions