-
Notifications
You must be signed in to change notification settings - Fork 0
/
graveduck.py
194 lines (160 loc) · 4.97 KB
/
graveduck.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python2.7
'''
GraveyardDuck
A tool decompress and recompress graphic files in the old Famicom Disk System
game Dracula II: Noroi no Fuuin, better known as Simon's Quest in the US.
It also works on Ai Senshi Nicol, and possibly other Konami games.
Version: 1.3
Author: Derrick Sobodash <[email protected]>
Copyright: (c) 2012 Derrick Sobodash
Web site: https://github.com/sobodash/graveyardduck/
License: BSD License <http://opensource.org/licenses/bsd-license.php>
'''
'''
Change Log
1.3 - Cleaned up a couple lines and added decompression support for the
special 0x80 code, which is used to include a 256-byte block in which
no compression is possible.
1.2 - Fixed the int() command used to sanitize input. You will now be able to
specify an offset using octal (0712) or hexadecimal (0xbeef).
1.1 - Fixed a potential (highly unlikely) overflow if more than 126 bytes pass
before any RLE can be achieved.
Fixed output message when no input is given. Old line was necro-ripped
from another project of mine. Oops!
'''
# Initialize the program
try:
import sys
import glob
import os
import re
from struct import *
import time
from stat import *
except ImportError, err:
print "Could not load %s module." % (err)
raise SystemExit
print "GraveyardDuck 1.3 (cli)\nCopyright (c) 2012, 2013 Derrick Sobodash\n"
'''
konamidec():
This function decompresses a block of the game that is stored in the Konami
compression format. The format is a simple RLE scheme:
* value < 0x80 = repeat the next byte n times
* value > 0x80 = write the following n bytes
* value = 0x80 = write the following 256 bytes [TODO!]
Compression is terminated when a value of 0xff is read into the file.
'''
def konamidec(file, offset):
print "Opening " + file + " ..."
f = open(file, "rb")
print "[Seeked to " + str(offset) + "]"
f.seek(offset)
decomp = ""
# This loop will run forever till an 0xff breaks it.
print "Decompressing data ..."
while file:
control = ord(f.read(1))
if control == 0xff:
break
# If byte is bigger than 0x80, grab bytes
if control > 0x80:
decomp += f.read(control - 0x80)
# If byte is 0x80, write the next 256 bytes
elif control == 0x80:
decomp += f.read(0x100)
# Otherwise, write the next byte n times
else:
temp = f.read(1)
for i in range(0, control):
decomp += temp
f.close()
return decomp
'''
konamicod():
UP, UP, DOWN, DOWN, LEFT, RIGHT, LEFT, RIGHT, B, A, START
'''
def konamicod(block):
print "Spooling " + block + " ..."
f = open(block, "rb")
size = os.stat(block)[ST_SIZE]
stream = f.read(size)
f.close()
# Start the compressed stream
comp = ""
i = 0
last = None
running = None
print "Compressing block data ..."
while i < len(stream):
char = stream[i]
last = i
count = 0
while i < len(stream) and stream[i] == char:
count = count + 1
i = i + 1
if count > 2:
if running != None:
comp = comp + chr(0x80 + len(running)) + running
if count > 0x7f:
while count > 0x7f:
comp = comp + chr(0x7f) + char
count = count - 0x7f
comp = comp + chr(count) + char
running = None
else:
if running == None:
running = stream[last:i]
else:
# A "gotcha" for 1.1. There was no check in place in case
# there were more than 0x7c bytes between RLEs. Need for this
# fix was highly unlikely, but better safe than sorry.
# Why 0x7c? because 0x7c + 2 from a possible failed RLE gives
# us 0xfe, which is still safe.
if len(running) > 0xfc - 0x80:
comp = comp + chr(0x80 + len(running)) + running
running = None
else:
running = running + stream[last:i]
if running != None:
comp = comp + chr(0x80 + len(running)) + running
return comp + "\xff"
# No input, prompt user
if len(sys.argv) < 2:
print "No input action given. Run with -h for a list of options."
raise SystemExit
# Help message
elif sys.argv[1] == "-h" or sys.argv[1] == "-help":
print "Decompress Usage:"
print " graveduck.py -d [FILENAME] [POSITION] [BLOCK]"
print "Compress Usage:"
print " graveduck.py -c [FILENAME] [POSITION] [BLOCK]"
print "Where:"
print " FILENAME = Source file containing compressed data"
print " POSITION = Offset in source file where compressed block begins"
print " BLOCK = Decompressed data"
raise SystemExit
# Super happy fun time!
if len(sys.argv) == 5:
file = sys.argv[2]
offset = int(sys.argv[3], 0)
block = sys.argv[4]
if sys.argv[1] == "-d":
decomp = konamidec(file, offset)
print "[Expanded " + str(len(decomp)) + "b from " + file + "]"
o = open(block, "wb")
print "Writing to " + block + " ..."
o.write(decomp)
o.close()
elif sys.argv[1] == "-c":
comp = konamicod(block)
print "[Compressed " + str(os.stat(block)[ST_SIZE]) + "b to " + str(len(comp)) + "b]"
print "Opening " + file + " ..."
o = open(file, "r+b")
o.seek(offset)
print "[Seeked to " + str(offset) + "]"
print "Inserting data ..."
o.write(comp)
o.close()
else:
print "DIVISION BY 0 OH SH--"
print "~Fin"