Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
seiya-git committed Jan 1, 2024
1 parent ee87713 commit d616919
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 34 deletions.
16 changes: 8 additions & 8 deletions py/nstools/Fs/BaseFs.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,17 @@ def getCnmt(self):

def printInfo(self, maxDepth = 3, indent = 0):
tabs = '\t' * indent
Print.info(tabs + 'magic = ' + str(self.magic))
Print.info(tabs + 'fsType = ' + str(self.fsType))
Print.info(tabs + 'cryptoType = ' + str(self.cryptoType))
Print.info(tabs + 'size = ' + str(self.size))
Print.info(tabs + 'headerSize = %s' % (str(self._headerSize)))
Print.info(tabs + 'offset = %s - (%s)' % (str(self.offset), str(self.sectionStart)))
Print.info(f'{tabs}magic = {self.magic}')
Print.info(f'{tabs}fsType = {self.fsType}')
Print.info(f'{tabs}cryptoType = {self.cryptoType}')
Print.info(f'{tabs}size = {hex(self.size)}')
Print.info(f'{tabs}headerSize = {self._headerSize}')
Print.info(f'{tabs}offset = {hex(self.offset)} - ({hex(self.sectionStart)})')
if self.cryptoCounter:
Print.info(tabs + 'cryptoCounter = ' + str(hx(self.cryptoCounter)))
Print.info(f'{tabs}cryptoCounter = {hx(self.cryptoCounter)}')

if self.cryptoKey:
Print.info(tabs + 'cryptoKey = ' + str(hx(self.cryptoKey)))
Print.info(f'{tabs}cryptoKey = {hx(self.cryptoKey)}')

Print.info('\n%s\t%s\n' % (tabs, '*' * 64))
Print.info('\n%s\tFiles:\n' % (tabs))
Expand Down
31 changes: 21 additions & 10 deletions py/nstools/Fs/Hfs0.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def __init__(self, f, mode = 'wb'):
super(Hfs0Stream, self).__init__(f, mode)
self.headerSize = 0x8000
self.files = []

self.actualSize = 0

self.seek(self.headerSize)
self.addpos = self.headerSize
self.written = False

def __enter__(self):
return self
Expand All @@ -38,18 +38,25 @@ def __exit__(self, type, value, traceback):

def write(self, value, size = None):
super(Hfs0Stream, self).write(value, len(value))
if self.tell() > self.actualSize:
self.actualSize = self.tell()
self.written = True
pos = self.tell()
if pos > self.actualSize:
self.actualSize = pos

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())
if self.written:
self.addpos = self.tell()
self.written = False
Print.info(f'[ADDING] {name} {hex(size)} bytes to HFS0 at {hex(self.addpos)}', pleaseNoPrint)
partition = self.partition(self.addpos, size, n = BaseFile())
self.files.append({'name': name, 'size': size, 'offset': self.addpos, '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 All @@ -68,11 +75,14 @@ def close(self):
self.write(self.getHeader())
super(Hfs0Stream, self).close()

def updateHashHeader(self):
pass

def getHeader(self):
stringTable = '\x00'.join(file['name'] for file in self.files)+'\x00'

headerSize = 0x10 + len(self.files) * 0x40 + len(stringTable)

h = b''
h += b'HFS0'
h += len(self.files).to_bytes(4, byteorder='little')
Expand All @@ -82,7 +92,7 @@ def getHeader(self):
stringOffset = 0

for f in self.files:
sizeOfHashedRegion = 0x200 if 0x200 < f['size'] else f['size']
sizeOfHashedRegion = 0 #0x200 if 0x200 < f['size'] else f['size']

h += (f['offset'] - headerSize).to_bytes(8, byteorder='little')
h += f['size'].to_bytes(8, byteorder='little')
Expand Down Expand Up @@ -130,6 +140,7 @@ def open(self, path = None, mode = 'rb', cryptoType = -1, cryptoKey = -1, crypto
nameOffset = self.readInt32() # just the offset
name = stringTable[nameOffset:stringEndOffset].decode('utf-8').rstrip(' \t\r\n\0')
stringEndOffset = nameOffset
Print.info(f'[OPEN ] {name} {hex(size)} bytes at {hex(offset)}')

self.readInt32() # junk data

Expand Down
3 changes: 1 addition & 2 deletions py/nstools/Fs/Nsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def setPath(self, path):
if self.hasValidTicket is None:
self.setHasValidTicket(False)
else:
# print('unknown extension ' + str(path))
print('unknown extension ' + str(path))
return

def getPath(self):
Expand Down Expand Up @@ -450,4 +450,3 @@ def verify(self):
success = False

return success

19 changes: 13 additions & 6 deletions py/nstools/Fs/Pfs0.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(self, headerSize, stringTableSize, path, mode = 'wb'):
self.actualSize = 0
self.f.seek(self.headerSize)
self.addpos = self.headerSize
self.written = False

def __enter__(self):
return self
Expand All @@ -39,13 +40,18 @@ def __exit__(self, type, value, traceback):

def write(self, value, size = None):
super(Pfs0Stream, self).write(value, len(value))
if self.tell() > self.actualSize:
self.actualSize = self.tell()
self.written = True
pos = self.tell()
if pos > self.actualSize:
self.actualSize = pos

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

Expand Down Expand Up @@ -137,7 +143,7 @@ def tell(self):
return self.pos

def add(self, name, size, pleaseNoPrint = None):
Print.info('[ADDING] {0} {1} bytes to NSP'.format(name, size), pleaseNoPrint)
Print.info(f'[ADDING] {name} {hex(size)} bytes to PFS0 at {hex(self.addpos)}', pleaseNoPrint)
self.files.append({'name': name, 'size': size, 'offset': self.addpos})
self.addpos += size
return self
Expand Down Expand Up @@ -255,6 +261,7 @@ def open(self, path = None, mode = 'rb', cryptoType = -1, cryptoKey = -1, crypto
nameOffset = self.readInt32() # just the offset
name = stringTable[nameOffset:stringEndOffset].decode('utf-8').rstrip(' \t\r\n\0')
stringEndOffset = nameOffset
Print.info(f'[OPEN ] {name} {hex(size)} bytes at {hex(offset)}')

self.readInt32() # junk data

Expand Down
11 changes: 6 additions & 5 deletions py/nstools/Fs/Xci.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,19 @@ def __exit__(self, type, value, traceback):
self.close()

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()})
t = {'name': name, 'size': size, 'offset': self.f.tell()}
return self.f
Print.info(f'[ADDING] {name} {hex(size)} bytes to XCI at {hex(self.f.tell())}', pleaseNoPrint)
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 currentFileSize(self):
return self.f.tell() - self.files[-1]['offset']

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
4 changes: 2 additions & 2 deletions py/nstools/lib/BlockDecompressorReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, nspf, BlockHeader):
self.BlockSize = 2**BlockHeader.blockSizeExponent
self.CompressedBlockOffsetList = [initialOffset]

for compressedBlockSize in BlockHeader.compressedBlockSizeList:
for compressedBlockSize in BlockHeader.compressedBlockSizeList[:-1]:
self.CompressedBlockOffsetList.append(self.CompressedBlockOffsetList[-1] + compressedBlockSize)

self.CompressedBlockSizeList = BlockHeader.compressedBlockSizeList
Expand All @@ -28,7 +28,7 @@ def __decompressBlock(self, blockID):
if blockID >= len(self.CompressedBlockOffsetList) - 1:
if blockID >= len(self.CompressedBlockOffsetList):
raise EOFError("BlockID exceeds the amounts of compressed blocks in that file!")
decompressedBlockSize = self.BlockHeader.decompressedSize % BlockSize
decompressedBlockSize = self.BlockHeader.decompressedSize % self.BlockSize
self.nspf.seek(self.CompressedBlockOffsetList[blockID])
if self.CompressedBlockSizeList[blockID] < decompressedBlockSize:
self.CurrentBlock = ZstdDecompressor().decompress(self.nspf.read(decompressedBlockSize))
Expand Down
2 changes: 1 addition & 1 deletion py/nstools/lib/Header.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ def __init__(self, f):
self.blockSizeExponent = f.readInt8()
self.numberOfBlocks = f.readInt32()
self.decompressedSize = f.readInt64()
self.compressedBlockSizeList = [f.readInt32() for _ in range(self.numberOfBlocks)]
self.compressedBlockSizeList = [f.readInt32() for _ in range(self.numberOfBlocks)]

0 comments on commit d616919

Please sign in to comment.