forked from jleclanche/python-ptch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptch.py
176 lines (143 loc) · 5.04 KB
/
ptch.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
PTCH files are a container format for Blizzard patch files.
They begin with a 72 byte header containing some metadata, immediately
followed by a RLE-packed BSDIFF40.
The original BSDIFF40 format is compressed with bzip2 instead of RLE.
"""
from binascii import hexlify
from hashlib import md5
try:
from io import StringIO
except ImportError:
from cStringIO import StringIO
from struct import unpack
__version__ = "1.0"
def xsign(i):
"""
Helper to decode an int packed by BSDIFF.
Instead of true signedness, like 0xFFFFFFFFC,
BSDIFF uses a top-level switch, eg 0x80000004
"""
if i & 0x80000000:
return 0x80000000 - i
return i
class PatchFile(object):
def __init__(self, file):
# Parse the header
if isinstance(file, str):
file = open(file, "rb")
assert file.read(4) == "PTCH" # Magic
##
# Sizes
# - patchSize: Size of the entire patch data, not including the 'PTCH' signature itself
# - sizeBefore: Size of the file before patching
# - sizeAfter: Size of the file after patching
self.patchSize, self.sizeBefore, self.sizeAfter = unpack("iii", file.read(12))
##
# MD5 block
# - md5BlockSize: Size of the MD5 block, including the signature and size
# - md5Before: MD5 digest of the original (unpatched) file
# - md5After: MD5 digest of the patched file
assert file.read(4) == "MD5_"
self.md5BlockSize, = unpack("i", file.read(4))
self.md5Before, self.md5After = unpack("16s16s", file.read(32))
self.md5Before, self.md5After = hexlify(self.md5Before), hexlify(self.md5After)
##
# XFRM block
# - xfrmBlockSize: Size of the XFRM block + the packed data
# - unpackedSize: Unpacked size of the patch data
assert file.read(4) == "XFRM"
self.xfrmBlockSize, = unpack("i", file.read(4))
assert file.read(4) in ("BSD0", "COPY") # patch type?
self.unpackedSize, = unpack("i", file.read(4))
self.compressedDiff = file.read()
file.close()
def __repr__(self):
header = ("sizeBefore", "sizeAfter", "md5Before", "md5After", "fileSize")
return "%s(%s)" % (self.__class__.__name__, ", ".join("%s=%r" % (k, getattr(self, k)) for k in header))
def __bsdiffParseHeader(self, diff):
"""
The BSDIFF header is as follows:
- 8 bytes magic "BSDIFF40"
- 8 bytes control block size
- 8 bytes diff block size
- 8 bytes new file size
We read all this and make sure it's all valid.
"""
assert diff.read(8) == "BSDIFF40"
ctrlBlockSize, diffBlockSize, sizeAfter = unpack("QQQ", diff.read(24))
assert ctrlBlockSize > 0 and diffBlockSize > 0
assert sizeAfter == self.sizeAfter
return ctrlBlockSize, diffBlockSize, sizeAfter
def bsdiffParse(self):
diff = StringIO(self.rleUnpack())
ctrlBlockSize, diffBlockSize, sizeAfter = self.__bsdiffParseHeader(diff)
##
# The data part of the file is divded into three parts
# * The control block, which contains the control part every chunk
# * The diff block, which contains the diff chunks
# * And the extra block, which contains the extra chunks
ctrlBlock = StringIO(diff.read(ctrlBlockSize))
diffBlock = StringIO(diff.read(diffBlockSize))
extraBlock = StringIO(diff.read())
diff.close()
return ctrlBlock, diffBlock, extraBlock
def rleUnpack(self):
"""
Read the RLE-packed data and
return the unpacked output.
"""
data = StringIO(self.compressedDiff)
ret = []
byte = data.read(1)
while byte:
byte = ord(byte)
# Is it a repeat control?
if byte & 0x80:
count = (byte & 0x7F) + 1
ret.append(data.read(count))
else:
ret.append("\0" * (byte+1))
byte = data.read(1)
return "".join(ret)
def apply(self, old, validate=True):
"""
Apply the patch to the old argument. Returns the output.
If validate == True, will raise ValueError if the md5 of
input or output is wrong.
"""
if validate:
hash = md5(old).hexdigest()
if hash != self.md5Before:
raise ValueError("Input MD5 fail. Expected %s, got %s." % (self.md5Before, hash))
ctrlBlock, diffBlock, extraBlock = self.bsdiffParse()
sizeBefore = len(old)
new = ["\0" for i in range(self.sizeAfter)]
cursor, oldCursor = 0, 0
while cursor < self.sizeAfter:
# Read control chunk
diffChunkSize, extraChunkSize, extraOffset = unpack("III", ctrlBlock.read(12))
assert cursor + diffChunkSize <= self.sizeAfter
# Read diff block
new[cursor:cursor + diffChunkSize] = diffBlock.read(diffChunkSize)
# Add old data to diff string
for i in range(diffChunkSize):
if (oldCursor + i >= 0) and (oldCursor + i < sizeBefore):
nb, ob = ord(new[cursor + i]), ord(old[oldCursor + i])
new[cursor + i] = chr((nb + ob) % 256)
# Update cursors
cursor += diffChunkSize
oldCursor += diffChunkSize
assert cursor + extraChunkSize <= self.sizeAfter
# Read extra chunk
new[cursor:cursor + extraChunkSize] = extraBlock.read(extraChunkSize)
# Update cursors
cursor += extraChunkSize
oldCursor += xsign(extraOffset)
ret = "".join(new)
if validate:
hash = md5(ret).hexdigest()
if hash != self.md5After:
# This likely means a parsing bug
raise ValueError("Output MD5 fail. Expected %s, got %s" % (self.md5Before, hash))
return ret