Patch Delta


from ctypes import (windll, wintypes, c\_uint64, cast, POINTER, Union, c\_ubyte,
                    LittleEndianStructure, byref, c\_size\_t)
import zlib

# types and flags
DELTA\_FLAG\_TYPE             = c\_uint64
DELTA\_FLAG\_NONE             = 0x00000000
DELTA\_APPLY\_FLAG\_ALLOW\_PA19 = 0x00000001

# structures
class DELTA\_INPUT(LittleEndianStructure):
    class U1(Union):
        \_fields\_ = \[('lpcStart', wintypes.LPVOID),
                    ('lpStart', wintypes.LPVOID)\]
    \_anonymous\_ = ('u1',)
    \_fields\_ = \[('u1', U1),
                ('uSize', c\_size\_t),
                ('Editable', wintypes.BOOL)\]

class DELTA\_OUTPUT(LittleEndianStructure):
    \_fields\_ = \[('lpStart', wintypes.LPVOID),
                ('uSize', c\_size\_t)\]

# functions
ApplyDeltaB = windll.msdelta.ApplyDeltaB
ApplyDeltaB.rettype = wintypes.BOOL
DeltaFree = windll.msdelta.DeltaFree
DeltaFree.argtypes = \[wintypes.LPVOID\]
DeltaFree.rettype = wintypes.BOOL
gle = windll.kernel32.GetLastError

def apply\_patchfile\_to\_buffer(buf, buflen, patchpath, legacy):
    with open(patchpath, 'rb') as patch:
        patch\_contents =

    # some patches (Windows Update MSU) come with a CRC32 prepended to the file
    # if the file doesn't start with the signature (PA) then check it
    if patch\_contents\[:2\] != b"PA":
        paoff = patch\_contents.find(b"PA")
        if paoff != 4:
            raise Exception("Patch is invalid")
        crc = int.from\_bytes(patch\_contents\[:4\], 'little')
        patch\_contents = patch\_contents\[4:\]
        if zlib.crc32(patch\_contents) != crc:
            raise Exception("CRC32 check failed. Patch corrupted or invalid")

    applyflags = DELTA\_APPLY\_FLAG\_ALLOW\_PA19 if legacy else DELTA\_FLAG\_NONE

    dd = DELTA\_INPUT()
    ds = DELTA\_INPUT()
    dout = DELTA\_OUTPUT()

    ds.lpcStart = buf
    ds.uSize = buflen
    ds.Editable = False

    dd.lpcStart = cast(patch\_contents, wintypes.LPVOID)
    dd.uSize = len(patch\_contents)
    dd.Editable = False

    status = ApplyDeltaB(applyflags, ds, dd, byref(dout))
    if status == 0:
        raise Exception("Patch {} failed with error {}".format(patchpath, gle()))

    return (dout.lpStart, dout.uSize)

if \_\_name\_\_ == '\_\_main\_\_':
    import sys
    import base64
    import hashlib
    import argparse

    ap = argparse.ArgumentParser()
    mode = ap.add\_mutually\_exclusive\_group(required=True)
    output = ap.add\_mutually\_exclusive\_group(required=True)
    mode.add\_argument("-i", "--input-file",
                      help="File to patch (forward or reverse)")
    mode.add\_argument("-n", "--null", action="store\_true", default=False,
                      help="Create the output file from a null diff "
                           "(null diff must be the first one specified)")
    output.add\_argument("-o", "--output-file",
                        help="Destination to write patched file to")
    output.add\_argument("-d", "--dry-run", action="store\_true",
                        help="Don't write patch, just see if it would patch"
                             "correctly and get the resulting hash")
    ap.add\_argument("-l", "--legacy", action='store\_true', default=False,
                    help="Let the API use the PA19 legacy API (if required)")
    ap.add\_argument("patches", nargs='+', help="Patches to apply")
    args = ap.parse\_args()

    if not args.dry\_run and not args.output\_file:
        print("Either specify -d or -o", file=sys.stderr)

    if args.null:
        inbuf = b""
        with open(args.input\_file, 'rb') as r:
            inbuf =

    buf = cast(inbuf, wintypes.LPVOID)
    n = len(inbuf)
    to\_free = \[\]
        for patch in args.patches:
            buf, n = apply\_patchfile\_to\_buffer(buf, n, patch, args.legacy)

        outbuf = bytes((c\_ubyte\*n).from\_address(buf))
        if not args.dry\_run:
            with open(args.output\_file, 'wb') as w:
        for buf in to\_free:

    finalhash = hashlib.sha256(outbuf)
    print("Applied {} patch{} successfully"
          .format(len(args.patches), "es" if len(args.patches) > 1 else ""))
    print("Final hash: {}"