Skip to content

Commit

Permalink
Fixed PFS0 header padding so it follows the PFS0 specification.
Browse files Browse the repository at this point in the history
  • Loading branch information
seiya-git committed Jan 1, 2024
1 parent d5dbed1 commit 275c0e2
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions py/nstools/Fs/Pfs0.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,34 @@ def close(self):
self.seek(0)
self.write(self.getHeader())
super(Pfs0Stream, self).close()

def getHeaderSize(self):
stringTable = '\x00'.join(file['name'] for file in self.files)+'\x00'
headerSize = 0x10 + len(self.files) * 0x18 + self.stringTableSize
return headerSize

#0xff => 0x1, 0x100 => 0x20, 0x1ff => 0x1, 0x120 => 0x20
def allign0x20(self, n):
return 0x20-n%0x20

def getStringTableSize(self):
stringTable = '\x00'.join(file['name'] for file in self.files)+'\x00'
stringTableLen = len(stringTable)
stringTableNonPadded = '\x00'.join(file['name'] for file in self.files)+'\x00'
headerSizeNonPadded = 0x10 + len(self.files) * 0x18 + len(stringTableNonPadded)
stringTableSizePadded = len(stringTableNonPadded) + self.allign0x20(headerSizeNonPadded)
if self._stringTableSize == None:
self._stringTableSize = stringTableLen
if stringTableLen > self._stringTableSize:
self._stringTableSize = stringTableLen
self._stringTableSize = stringTableSizePadded
if stringTableSizePadded > self._stringTableSize:
self._stringTableSize = stringTableSizePadded
return self._stringTableSize

def getFirstFileOffset(self):
return self.files[0].offset

def getHeader(self):
stringTable = '\x00'.join(file['name'] for file in self.files)
headerSize = 0x10 + len(self.files) * 0x18 + self.getStringTableSize()
stringTableNonPadded = '\x00'.join(file['name'] for file in self.files)+'\x00'
stringTableSizePadded = self.getStringTableSize()
stringTable = stringTableNonPadded + ('\x00'*(stringTableSizePadded-len(stringTableNonPadded)))
headerSize = 0x10 + len(self.files) * 0x18 + stringTableSizePadded

h = b''
h += b'PFS0'
h += len(self.files).to_bytes(4, byteorder='little')
h += (self.getStringTableSize()).to_bytes(4, byteorder='little')
h += (stringTableSizePadded).to_bytes(4, byteorder='little')
h += b'\x00\x00\x00\x00'

stringOffset = 0
Expand Down Expand Up @@ -145,21 +147,24 @@ def close(self):
pass

def getStringTableSize(self):
stringTable = '\x00'.join(file['name'] for file in self.files)
stringTableLen = len(stringTable)
stringTableNonPadded = '\x00'.join(file['name'] for file in self.files)+'\x00'
headerSizeNonPadded = 0x10 + len(self.files) * 0x18 + len(stringTableNonPadded)
stringTableSizePadded = len(stringTableNonPadded) + self.allign0x20(headerSizeNonPadded)
if self._stringTableSize == None:
self._stringTableSize = stringTableLen
if stringTableLen > self._stringTableSize:
self._stringTableSize = stringTableLen
self._stringTableSize = stringTableSizePadded
if stringTableSizePadded > self._stringTableSize:
self._stringTableSize = stringTableSizePadded
return self._stringTableSize

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

def getHeaderHash(self):
stringTable = '\x00'.join(file['name'] for file in self.files)
headerSize = 0x10 + len(self.files) * 0x18 + self.getStringTableSize()
stringTableNonPadded = '\x00'.join(file['name'] for file in self.files)+'\x00'
stringTableSizePadded = self.getStringTableSize()
stringTable = stringTableNonPadded + ('\x00'*(stringTableSizePadded-len(stringTableNonPadded)))
headerSize = 0x10 + len(self.files) * 0x18 + stringTableSizePadded

h = b''
h += b'PFS0'
Expand Down

0 comments on commit 275c0e2

Please sign in to comment.