Skip to content

Commit

Permalink
Python3 cross-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Sky-xgit authored Jun 25, 2018
1 parent b1a61d3 commit 953d6e5
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 189 deletions.
185 changes: 96 additions & 89 deletions depack.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/usr/bin/python

# -*- coding: utf_8 -*-

#'"When you dig a well, there's no sign of water until you reach it, only rocks and dirt to move out of the way. You have removed enough; soon the pure water will flow," said Buddha.' - Deepak Chopra

#Chopra 1.0 - port of ShuffleUnpack to Python, more or less

from sys import argv, exit
from sys import argv, exit, version_info
import os, os.path
import io
from binascii import hexlify
Expand All @@ -23,8 +25,7 @@ def read_short(file,start=None):
start = file_pointer
file_pointer += 2
return unpack("<H",file[start:start+2])[0]



def read_signed_short(file,start=None):
global file_pointer
if start is None:
Expand All @@ -38,7 +39,7 @@ def read_int(file,start=None):
start = file_pointer
file_pointer += 4
return unpack("<I",file[start:start+4])[0]

def as_signed_int(file,start=None):
global file_pointer
if start is None:
Expand All @@ -51,8 +52,11 @@ def read_hexname(file,start=None):
if start is None:
start = file_pointer
file_pointer += 4
return string.upper(hexlify(file[start:start+4][::-1])) #the [::-1] swaps the bytes - endianness!

if (version_info > (3, 0)):
return str(hexlify(file[start:start+4][::-1]),"utf-8").upper()
else:
return string.upper(hexlify(file[start:start+4][::-1])) #the [::-1] swaps the bytes - endianness!

def read_data(file,lenR,start=None):
global file_pointer
if start is None:
Expand All @@ -68,16 +72,15 @@ def write_data(file,data,start=None):
start = file_pointer
file_pointer += lenW
if start+lenW > len(file):
print "Warning: A write extends the length of this data."
print("Warning: A write extends the length of this data.")
return file[:start]+data+file[start+lenW:]


if len(argv) < 2:
print "Sorry, I need a file or folder path to unpack."
print("Sorry, I need a file or folder path to unpack.")
exit(1)

if not os.access(argv[1], os.R_OK):
print "I can't read the file path you've given me (or it isn't a path at all)."
print("I can't read the file path you've given me (or it isn't a path at all).")
exit(1)

isdir = os.path.isdir(argv[1])
Expand All @@ -98,48 +101,48 @@ def write_data(file,data,start=None):
for line in filenames_file:
subline = line.strip().split("\t")
filenames[subline[0]] = subline[1]

with open("Message Regions.txt") as filenames_file:
for line in filenames_file:
subline = line.strip().split("\t")
regionnames[subline[0]] = subline[1]

with open("Archive Names.txt") as filenames_file:
for line in filenames_file:
subline = line.strip().split("\t")
arcnames[subline[0]] = subline[1]
except IOError:
print "I need my data files - File Names.txt, Message Regions.txt, and Archive Names.txt - in the current directory for the files to be properly named. Stopping now."
print("I need my data files - File Names.txt, Message Regions.txt, and Archive Names.txt - in the current directory for the files to be properly named. Stopping now.")
exit(1)

#set up output dir
#set up output dir
try:
os.mkdir("output") #make output dir, fail silently if it's already here.
os.mkdir("output") #make output dir, fail silently if it's already here.
except OSError:
pass
#check file / files in folder


#check file / files in folder
for file in files_to_check:
origfilename = file
if isdir:
file = argv[1]+"/"+file #add directory before path
file = argv[1]+os.sep+file #add directory before path

if os.path.isdir(file):
print "File '"+file+"' is a directory. Skipping."
print("File '"+file+"' is a directory. Skipping.")
continue

if not os.access(file, os.R_OK):
print "I can't read file '"+file+"'. Skipping."
print("I can't read file '"+file+"'. Skipping.")
continue

shuffle_file = b""
shuffle_file = b""

with io.open(file, mode='rb') as shuffle_file_ptr:
shuffle_file = shuffle_file_ptr.read() #read whole file

file_pointer = 0 #global - acts as 'file pointer' when reading

#validate the contents
#We're looking for the magic number to see if this is a REAL archive file.
extra_offset = 0 #set to 256 if this is an ExtData archive.
Expand All @@ -149,139 +152,143 @@ def write_data(file,data,start=None):
if magic != 11:
extra_offset = 256 #skip RSA signature from hereon
file_pointer = 256

magic = read_int(shuffle_file)
if magic != 11: #0x000B
print magic
print "File '"+file+"' doesn't look like a valid archive file. Skipping."
print(magic)
print("File '"+file+"' doesn't look like a valid archive file. Skipping.")
continue
#Continuing the magic number check, the next bytes spell out the name of the file!
#Continuing the magic number check, the next bytes spell out the name of the file!

magic_name = read_hexname(shuffle_file)


if origfilename != magic_name:
print "File '"+file+"' internal name doesn't match filename. This probably isn't a valid archive file. Skipping."
print magic_name
print("File '"+file+"' internal name doesn't match filename. This probably isn't a valid archive file. Skipping.")
print(magic_name)
continue
print "Unpacking file '"+file+"'."
os.chdir("output/")

print("Unpacking file '"+file+"'.")

os.chdir("output"+os.sep)

try:
this_file_dir_name = arcnames[origfilename]
os.mkdir(arcnames[origfilename]) #make output dir, fail silently if it's already here.
os.mkdir(arcnames[origfilename]) #make output dir, fail silently if it's already here.
except OSError:
pass
except KeyError:
print "Couldn't find a folder name for file '"+origfilename+"'."
print("Couldn't find a folder name for file '"+origfilename+"'.")
try:
this_file_dir_name = origfilename
os.mkdir(origfilename)
except OSError:
pass



#the next two ints are unknowns.
file_pointer += 8

#now we can start actually reading the file!
num_packed_files = read_int(shuffle_file)
padding_size = read_int(shuffle_file) #this is never USED in the unpacker, suggesting it's usually 0.

if padding_size > 0:
print "Note: Padding size of this archive is greater than 0. If you get gibberish out the other end or the extraction fails, please send the offending file to SoItBegins."
print("Note: Padding size of this archive is greater than 0. If you get gibberish out the other end or the extraction fails, please send the offending file to SoItBegins.")


#a note on how big we expect this header block to be:
#magic (4) + magicname (4) + two unknowns (8) + # packed files (4) + padding size (4) +, for EACH packed file:
# (name hash (4) + unknown (4) + file length (4) + file offset (4) + 16 more unknown bytes) = 32 bytes for EACH packed file.
#so that's 24 + 32 * packed files.

front_offset = 24 + 32*num_packed_files

packed_file_info = []

for fileno in range(num_packed_files):
name_hash = read_hexname(shuffle_file)
file_pointer += 4 #the original unpacker code is awfully vague on what the next int is supposed to be, too.
file_length = read_int(shuffle_file)
file_offset = read_int(shuffle_file)
packed_file_info.append([name_hash, file_length, file_offset])
file_pointer += 16 #skip the next 16 bytes.

#once we have the information, the next step is to unzip and decode everything inside the archive.


for packed_file in packed_file_info:
name_hash, file_length, file_offset = packed_file

if name_hash in filenames:
real_file_name = filenames[name_hash]
if "{0}" in real_file_name:
real_file_name = real_file_name[:-7]+regionnames[origfilename]+real_file_name[-4:]
print "Unpacking subfile '"+real_file_name+"' ("+name_hash+")."

print("Unpacking subfile '"+real_file_name+"' ("+name_hash+").")
else:
print "Couldn't find a name for packed subfile '"+name_hash+"'."
print("Couldn't find a name for packed subfile '"+name_hash+"'.")
if name_hash in mystery_filenames:
mystery_filenames[name_hash] += 1
else:
mystery_filenames[name_hash] = 1
real_file_name = name_hash

#We can use the starting point and the length to feed this whole business to ZipFile.

thisZip = tempfile.TemporaryFile()
file_pointer = file_offset+extra_offset
thisZipData = read_data(shuffle_file,file_length)

#before we write all this fine data, we need to do a little quick fixing. The files listed are perfectly normal ZIP files, but they may not have names.
#we need to find all the central directory and local file listings in the zipfile - we can do this quickly with re.finditer.
num_subfiles = read_short(thisZipData, start=re.search("PK\x05\x06",thisZipData).start()+10)
locals = re.finditer("PK\x03\x04",thisZipData)
centrals = re.finditer("PK\x01\x02",thisZipData)

num_subfiles = read_short(thisZipData, start=re.search(b"PK\x05\x06",thisZipData).start()+10)

locals = re.finditer(b"PK\x03\x04",thisZipData)
centrals = re.finditer(b"PK\x01\x02",thisZipData)


try:
for subfile_id in range(num_subfiles):
this_local_pos = locals.next().start() + 26
this_central_pos = centrals.next().start() + 28

#get lengths of file name space we have available - this keeps us from writing over something we shouldn't
local_file_name_len = read_short(thisZipData,start=this_local_pos)
central_file_name_len = read_short(thisZipData,start=this_central_pos)

if local_file_name_len != central_file_name_len:
file_name_len = min(local_file_name_len,central_file_name_len)
print "Note: Local file name length allotment and central file name length allotment do not match."
print("Note: Local file name length allotment and central file name length allotment do not match." )
else:
file_name_len = central_file_name_len
real_file_name = real_file_name[:file_name_len] #truncate length of name about to be written if need be

real_file_name = real_file_name[:file_name_len] #truncate length of name about to be written if need be
#now actually write the names
thisZipData = write_data(thisZipData,real_file_name, start=this_local_pos+4)
thisZipData = write_data(thisZipData,real_file_name, start=this_central_pos+18)

if (version_info > (3, 0)):
thisZipData = write_data(thisZipData,real_file_name.encode(), start=this_local_pos+4)
thisZipData = write_data(thisZipData,real_file_name.encode(), start=this_central_pos+18)
else:
thisZipData = write_data(thisZipData,real_file_name, start=this_local_pos+4)
thisZipData = write_data(thisZipData,real_file_name, start=this_central_pos+18)

except StopIteration:
#this would happen if there was a central directory entry not attached to an associated local... or vice versa.
print "This subfile contains malformed archive data. Please send the offending file to SoItBegins. Moving on..."
print("This subfile contains malformed archive data. Please send the offending file to SoItBegins. Moving on...")
continue


#actually extract the data now
thisZip.write(thisZipData)
thisZipFile = zipfile.ZipFile(thisZip, 'r')
thisZipFile.extractall(this_file_dir_name+"/")
os.chdir("..")
thisZipFile.extractall(this_file_dir_name+os.sep)
os.chdir("..")

if len(mystery_filenames.keys()) > 0:
print "Extraction complete. Mystery file names:"

print("Extraction complete. Mystery file names:")
mysterystring = ""
first = True
for key in sorted(mystery_filenames.keys()):
Expand All @@ -293,8 +300,8 @@ def write_data(file,data,start=None):
mysterystring += ( key +" ("+str(mystery_filenames[key])+" times)" )
else:
mysterystring += key
print mysterystring

print(mysterystring)

else:
print "Extraction complete."
print("Extraction complete.")
Loading

0 comments on commit 953d6e5

Please sign in to comment.