Skip to content

Instantly share code, notes, and snippets.

@mfgmfg
Created February 10, 2016 15:14
Show Gist options
  • Save mfgmfg/b38addec59637be4fc6c to your computer and use it in GitHub Desktop.
Save mfgmfg/b38addec59637be4fc6c to your computer and use it in GitHub Desktop.
import struct
import sys
if (len(sys.argv) < 3):
exit("need input and output file names")
infile = open(sys.argv[1],"rb")
outfile = open(sys.argv[2],"wb")
indata = infile.read()
inbytes = len(indata)
outlen = struct.unpack('<I',indata[8:12])[0]
outdata = bytearray(outlen)
inptr=12
outptr=0
printout=0
while(outptr < outlen):
copybytes = (indata[inptr] & 0xf0) >> 4
matchbytes = (indata[inptr] & 0x0f)
inptr += 1
if (copybytes == 15):
while( True ):
copybytes += indata[inptr]
inptr += 1
if (indata[inptr-1] != 0xff):
break
#print("Copying %d bytes" % copybytes)
if (copybytes > 0):
outdata[outptr:outptr+copybytes] = indata[inptr:inptr+copybytes]
inptr += copybytes
outptr += copybytes
if (inptr >= inbytes):
break
offset = indata[inptr] + (indata[inptr+1] << 8)
inptr += 2
if (matchbytes == 15):
while( True ):
matchbytes += indata[inptr]
inptr += 1
if (indata[inptr-1] != 0xff):
break
matchbytes += 4
#print("Offset is %d, copying %d match bytes" % (offset, matchbytes))
if (matchbytes >= offset): #overlapping copy
for i in range(matchbytes):
outdata[outptr+i] = outdata[outptr-offset+i] # slow
else:
outdata[outptr:outptr+matchbytes] = outdata[outptr-offset:outptr-offset+matchbytes]
outptr += matchbytes
if (outptr > printout):
print("%d/%d" % (outptr,outlen))
printout += 256*1024
print("%d/%d" % (outptr,outlen))
outfile.write(outdata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment