Skip to content

Commit

Permalink
- fixed param.sfo parsing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ghty committed Mar 10, 2018
1 parent ed6f486 commit 4a7f920
Showing 1 changed file with 14 additions and 74 deletions.
88 changes: 14 additions & 74 deletions pkg_list.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
## pkg_list v1.01 by n1ghty
## pkg_list by n1ghty
##
## This file is based on
## UnPKG rev 0x00000008 (public edition), (c) flatz
## and
## Python SFO Parser by: Chris Kreager a.k.a LanThief
REL_VERSION = 'v1.02'

import sys, os, struct, traceback, csv
import xlsxwriter
import sys, os, struct, traceback, xlsxwriter

## parse arguments

if len(sys.argv) < 2:
print 'pkg_list ' + REL_VERSION + ' by n1ghty'
script_file_name = os.path.split(sys.argv[0])[1]
print 'usage: {0} <pkg paths>'.format(script_file_name)
sys.exit()
Expand All @@ -26,9 +28,7 @@
## utility functions

def convert_bytes(num):
"""
this function will convert bytes to MB.... GB... etc
"""
"this function will convert bytes to MB.... GB... etc"
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
if num < 1024.0:
return "%3.1f %s" % (num, x)
Expand Down Expand Up @@ -61,70 +61,28 @@ def str2hex(s, size=8):
h = (h << size) | ord(c)
return h


def hex2hexList(h, size=8, reverse=True):
"hex converter to hex list"
return hex2hexList_charList(h, size, reverse, False)


def hex2hexList_charList(h, size=8, reverse=True, ischr=True):
"hex converter to either chr list or hex list"
l = []
if h == 0x0:
if ischr:
l.append(chr(h))
else:
l.append(h)
return l
while h:
_h = (h & mask_bit(size))
if ischr:
horc = chr(_h)
else:
horc = _h
l.append(horc)
h = (h >> size)
if reverse: l.reverse()
return l


def str2hexList(s, size=8, reverse=True):
"String converter to hex list"
return hex2hexList(str2hex(s), size, reverse)


def mask_bit(size=8):
if size > 32:
return (0x1L << size) - (0x1)
else:
return (0x1 << size) - (0x1)

def le32(bits):
bytes = str2hexList(bits)
result = 0x0
offset = 0
for byte in bytes:
for i in xrange(4):
byte = ord(bits[i])
result |= byte << offset
offset += 8
return result



def le16(bits):
bytes = str2hexList(bits)
if len(bytes) > 1:
return (bytes[0] | bytes[1] << 8)
return (bytes[0] | 0x0 << 8)
return (ord(bits[0]) | ord(bits[1]) << 8)

## classes

class PsfHdr:
size = 20

def __init__(self, bits):
self.size = 20
self.data = bits[:self.size]
self.magic = str2hexList(bits[:4])
self.rfu000 = str2hexList(bits[4:8])
self.magic = le32(bits[:4])
self.rfu000 = le32(bits[4:8])
self.label_ptr = bits[8:12]
self.data_ptr = bits[12:16]
self.nsects = bits[16:20]
Expand All @@ -149,7 +107,7 @@ def __init__(self, bits):
def __len__(self):
return self.size

# main code
## main code
PsfMagic = "\0PSF"
PKG_MAGIC = '\x7FCNT'
CONTENT_ID_SIZE = 0x24
Expand Down Expand Up @@ -188,12 +146,6 @@ def getPkgInfo(pkg_file_path):
pkg_file.seek(0x18)
file_table_offset = read_uint32_be(pkg_file)

#pkg content id may be used for extended formatting
#pkg_file.seek(0x40)
#content_id = read_cstring(pkg_file)
#if len(content_id) != CONTENT_ID_SIZE:
# raise MyError('invalid content id')

table_entries = []
table_entries_map = {}
pkg_file.seek(file_table_offset)
Expand Down Expand Up @@ -224,6 +176,7 @@ def getPkgInfo(pkg_file_path):
le32(sect.datafield_size),
le32(sect.datafield_used), sect.data_type,
str2hex(sect.rfu001),

if psflabels[le16(sect.label_off):].split('\x00')[0] == "TITLE":
TITLE = psfdata[le32(sect.data_off):].split('\x00\x00')[0]
if psflabels[le16(sect.label_off):].split('\x00')[0] == "CONTENT_ID":
Expand All @@ -243,19 +196,6 @@ def getPkgInfo(pkg_file_path):
index += PsfSec.size
sect = PsfSec(data[index:])

if CONTENT_ID and VERSION and APP_VER:
NEW_FILENAME = "{0}-A{1}-V{2}.pkg".format(CONTENT_ID, APP_VER.replace(".",""), VERSION.replace(".",""))
else:
raise MyError('parsing of param.sfo failed')
break

## may be used for extended formatting
#is_digests_valid = computed_main_entries1_digest == main_entries1_digest
#is_digests_valid = is_digests_valid and computed_main_entries2_digest == main_entries2_digest
#is_digests_valid = is_digests_valid and computed_digest_table_digest == digest_table_digest
#is_digests_valid = is_digests_valid and computed_body_digest == body_digest
#is_digests_valid = is_digests_valid and computed_entry_digests == entry_digests

# get filesize
pkg_file.seek(0, os.SEEK_END)
size = convert_bytes(pkg_file.tell())
Expand Down

0 comments on commit 4a7f920

Please sign in to comment.