-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlz77.py
104 lines (82 loc) · 3.26 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
99
100
101
102
103
104
#!/usr/bin/python
# -*- coding: latin-1 -*-
# Reggie Next - New Super Mario Bros. Wii / New Super Mario Bros 2 Level Editor
# Milestone 2 Alpha 4
# Copyright (C) 2009-2014 Treeki, Tempus, angelsl, JasonP27, Kamek64,
# MalStar1000, RoadrunnerWMC
# 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
# API for decompressing LZ77-compressed data.
################################################################
################################################################
import struct
class LZS11(object):
def __init__(self):
self.magic = 0x11
self.decomp_size = 0
self.curr_size = 0
self.compressed = True
self.outdata = []
def Decompress11LZS( self , filein ):
offset = 0
# check that file is < 2GB
#print "length of file: 0x%x" % len(filein)
assert len(filein) < ( 0x4000 * 0x4000 * 2 )
self.magic = struct.unpack('<B', filein[0:1])[0]
#print "magic = 0x%x" % self.magic
assert self.magic == 0x11
self.decomp_size = struct.unpack('<I', filein[offset:offset+4])[0] >> 8
offset += 4
assert self.decomp_size <= 0x200000
if ( self.decomp_size == 0 ):
self.decomp_size = struct.unpack('<I', filein[offset:offset+4])[0]
offset += 4
assert self.decomp_size <= 0x200000 << 8
#print "Decompressing 0x%x. (outsize: 0x%x)" % (len(filein), self.decomp_size)
self.outdata = [0 for x in range(self.decomp_size)]
while self.curr_size < self.decomp_size and offset < len(filein):
flags = struct.unpack('<B', filein[offset:offset+1])[0]
offset += 1
for i in range(8):
x = 7 - i
if self.curr_size >= self.decomp_size:
break
if (flags & (1 << x)) > 0:
first = struct.unpack('<B', filein[offset:offset+1])[0]
offset += 1
second = struct.unpack('<B', filein[offset:offset+1])[0]
offset += 1
if first < 0x20:
third = struct.unpack('<B', filein[offset:offset+1])[0]
offset += 1
if first >= 0x10:
fourth = struct.unpack('<B', filein[offset:offset+1])[0]
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):
self.outdata[self.curr_size + y] = self.outdata[self.curr_size - pos + y]
self.curr_size += copylen
else:
self.outdata[self.curr_size] = struct.unpack('<B', filein[offset:offset+1])[0]
offset += 1
self.curr_size += 1
if offset >= len(filein) or self.curr_size >= self.decomp_size:
break
return self.outdata