-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlz77.py
98 lines (68 loc) · 2.9 KB
/
lz77.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
#!/usr/bin/python
# -*- coding: latin-1 -*-
# Reggie Next - New Super Mario Bros. Wii Level Editor
# Milestone 3
# Copyright (C) 2009-2014 Treeki, Tempus, angelsl, JasonP27, Kamek64,
# MalStar1000, RoadrunnerWMC, 2017 Stella/AboodXD, John10v10
# This file is part of Reggie Next.
# Reggie Next is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Reggie Next is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Reggie Next. If not, see <http://www.gnu.org/licenses/>.
# lz77.py
# LZ77 decompressor in Python.
################################################################
################################################################
def GetUncompressedSize(inData):
offset = 4
outSize = inData[1] | (inData[2] << 8) | (inData[3] << 16)
if not outSize:
outSize = inData[4] | (inData[5] << 8) | (inData[6] << 16) | (inData[7] << 24)
offset += 4
return outSize, offset
def UncompressLZ77(inData):
if inData[0] != 0x11:
return inData
inLength = len(inData)
outLength, offset = GetUncompressedSize(inData)
outData = bytearray(outLength)
outIndex = 0
while outIndex < outLength and offset < inLength:
flags = inData[offset]
offset += 1
for x in reversed(range(8)):
if outIndex >= outLength or offset >= inLength:
break
if flags & (1 << x):
first = inData[offset]
offset += 1
second = inData[offset]
offset += 1
if first < 32:
third = inData[offset]
offset += 1
if first >= 16:
fourth = inData[offset]
offset += 1
pos = (((third & 0xF) << 8) | fourth) + 1
copylen = ((second << 4) | ((first & 0xF) << 12) | (third >> 4)) + 273
else:
pos = (((second & 0xF) << 8) | third) + 1
copylen = (((first & 0xF) << 4) | (second >> 4)) + 17
else:
pos = (((first & 0xF) << 8) | second) + 1
copylen = (first >> 4) + 1
for y in range(copylen):
outData[outIndex + y] = outData[outIndex - pos + y]
outIndex += copylen
else:
outData[outIndex] = inData[offset]
offset += 1
outIndex += 1
return bytes(outData)