From 74b2613306152db4f7e5bb7f3dbeb43d0047d7d1 Mon Sep 17 00:00:00 2001 From: seiya-git Date: Mon, 1 Jan 2024 12:16:55 +0300 Subject: [PATCH] PFS0 fixes --- py/nstools/Fs/Pfs0.py | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/py/nstools/Fs/Pfs0.py b/py/nstools/Fs/Pfs0.py index 7b80f0a..5ca027d 100644 --- a/py/nstools/Fs/Pfs0.py +++ b/py/nstools/Fs/Pfs0.py @@ -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 @@ -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): @@ -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): @@ -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): @@ -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))) @@ -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):