Skip to content

Commit

Permalink
PFS0 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
seiya-git committed Jan 1, 2024
1 parent 28866e5 commit 74b2613
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions py/nstools/Fs/Pfs0.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ def __init__(self, headerSize, stringTableSize, path, mode = 'wb'):
self.headerSize = headerSize
self._stringTableSize = stringTableSize
self.files = []

self.actualSize = 0

self.f.seek(self.headerSize)
self.addpos = self.headerSize

def __enter__(self):
return self
Expand All @@ -45,13 +44,15 @@ def write(self, value, size = None):

def add(self, name, size, pleaseNoPrint = None):
Print.info('[ADDING] {0} {1} bytes to NSP'.format(name, size), pleaseNoPrint)
self.files.append({'name': name, 'size': size, 'offset': self.f.tell()})
return self.partition(self.f.tell(), size, n = BaseFile())
partition = self.partition(self.f.tell(), size, n = BaseFile())
self.files.append({'name': name, 'size': size, 'offset': self.f.tell(), 'partition': partition})
self.addpos += size
return partition

def get(self, name):
for i in self.files:
if i['name'] == name:
return i
return i['partition']
return None

def resize(self, name, size):
Expand Down Expand Up @@ -116,6 +117,7 @@ def __init__(self, headerSize, stringTableSize, mode = 'wb'):
self.files = []
self.binhash = sha256()
self.pos = headerSize
self.addpos = headerSize
self._stringTableSize = stringTableSize

def __enter__(self):
Expand All @@ -133,7 +135,8 @@ def tell(self):

def add(self, name, size, pleaseNoPrint = None):
Print.info('[ADDING] {0} {1} bytes to NSP'.format(name, size), pleaseNoPrint)
self.files.append({'name': name, 'size': size, 'offset': self.pos})
self.files.append({'name': name, 'size': size, 'offset': self.addpos})
self.addpos += size
return self

def get(self, name):
Expand All @@ -154,13 +157,13 @@ def getStringTableSize(self):
self._stringTableSize = stringTableSizePadded
elif len(stringTableNonPadded) > self._stringTableSize:
self._stringTableSize = len(stringTableNonPadded)
return self._stringTableSize
return self._stringTableSize-1

def getHash(self):
hexHash = self.binhash.hexdigest()
return hexHash

def getHeaderHash(self):
def updateHashHeader(self):
stringTableNonPadded = '\x00'.join(file['name'] for file in self.files)+'\x00'
stringTableSizePadded = self.getStringTableSize()
stringTable = stringTableNonPadded + ('\x00'*(stringTableSizePadded-len(stringTableNonPadded)))
Expand All @@ -173,21 +176,23 @@ def getHeaderHash(self):
h += b'\x00\x00\x00\x00'

stringOffset = 0

for f in self.files:
h += (f['offset'] - headerSize).to_bytes(8, byteorder='little')
h += f['size'].to_bytes(8, byteorder='little')
h += stringOffset.to_bytes(4, byteorder='little')
h += b'\x00\x00\x00\x00'

h += b'\x00\x00\x00\x00'
stringOffset += len(f['name']) + 1


stringTable += '\x00' * 12
h += stringTable.encode()

headerBinhash = sha256()
headerBinhash.update(h)
headerHexHash = headerBinhash.hexdigest()
return headerHexHash
headerHex = h.hex()
#print(headerHex)
#print(len(h), self.files[0]['offset']-headerSize)
self.binhash.update(h)
#print(self.binhash.hexdigest())
#exit()



class Pfs0(BaseFs):
Expand Down

0 comments on commit 74b2613

Please sign in to comment.