Skip to content

Commit

Permalink
enabled aggressive formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
blawar committed Nov 15, 2020
1 parent 64d858a commit 7b2bd9b
Show file tree
Hide file tree
Showing 26 changed files with 2,092 additions and 287 deletions.
10 changes: 5 additions & 5 deletions Fs/BaseFs.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ def __init__(self, buffer, path=None, mode=None, cryptoType=-1, cryptoKey=-1, cr
self.buffer = buffer
try:
self.fsType = Fs.Type.Fs(buffer[0x3])
except:
except BaseException:
self.fsType = buffer[0x3]

try:
self.cryptoType = Fs.Type.Crypto(buffer[0x4])
except:
except BaseException:
self.cryptoType = buffer[0x4]

self.cryptoCounter = bytearray((b"\x00"*8) + buffer[0x140:0x148])
Expand Down Expand Up @@ -105,7 +105,7 @@ def bktrRead(self, size=None, direct=False):
'''
if self.bktrRelocation:
entry = self.bktrRelocation.getRelocationEntry(self.tell())
if entry:
self.ctr_val = entry.ctr
#self.cryptoOffset = entry.virtualOffset + entry.physicalOffset
Expand All @@ -125,9 +125,9 @@ def bktrRead(self, size=None, direct=False):
def read(self, size=None, direct=False):
'''
if self.cryptoType == Type.Crypto.BKTR or self.bktrSubsection is not None:
return self.bktrRead(size, True)
return self.bktrRead(size, True)
else:
return super(BaseFs, self).read(size, direct)
return super(BaseFs, self).read(size, direct)
'''
return super(BaseFs, self).read(size, direct)

Expand Down
24 changes: 13 additions & 11 deletions Fs/File.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(self, path=None, mode=None, cryptoType=-1, cryptoKey=-1, cryptoCoun
self._bufferAlign = 0x1000
self._bufferDirty = False

if path and mode != None:
if path and mode is not None:
self.open(path, mode, cryptoType, cryptoKey, cryptoCounter)

self.setupCrypto(cryptoType, cryptoKey, cryptoCounter)
Expand Down Expand Up @@ -66,7 +66,7 @@ def partition(self, offset=0x0, size=None, n=None, cryptoType=-1, cryptoKey=-1,
self._children.append(n)

#Print.info('created partition for %s %x, size = %d' % (n.__class__.__name__, offset, size))
if autoOpen == True:
if autoOpen:
n.open(None, None, cryptoType, cryptoKey, cryptoCounter)

return n
Expand All @@ -87,7 +87,7 @@ def read(self, size=None, direct=0):
print('tell = %d' % self.tell())
try:
return self.f.read(size, direct=direct)
except:
except BaseException:
return self.f.read(size)

def readInt8(self, byteorder='little', signed=False):
Expand All @@ -112,7 +112,7 @@ def readInt(self, size, byteorder='little', signed=False):
return int.from_bytes(self.read(size), byteorder=byteorder, signed=signed)

def write(self, value, size=None):
if size != None:
if size is not None:
value = value + '\0x00' * (size - len(value))
#Print.info('writing to ' + hex(self.f.tell()) + ' ' + self.f.__class__.__name__)
# Hex.dump(value)
Expand Down Expand Up @@ -208,7 +208,7 @@ def setupCrypto(self, cryptoType=-1, cryptoKey=-1, cryptoCounter=-1):
self.cryptoType = Fs.Type.Crypto.NONE

def open(self, path, mode='rb', cryptoType=-1, cryptoKey=-1, cryptoCounter=-1):
if path != None:
if path is not None:
if self.isOpen():
self.close()

Expand Down Expand Up @@ -251,7 +251,7 @@ def eof(self):
return self.tell() >= self.size

def isOpen(self):
return self.f != None
return self.f is not None

def setCounter(self, ofs):
ctr = self.cryptoCounter.copy()
Expand Down Expand Up @@ -305,7 +305,7 @@ def pageClear(self):
try:
if self.f:
self.f.pageClear()
except:
except BaseException:
pass

class BufferedFile(BaseFile):
Expand All @@ -330,7 +330,8 @@ def read(self, size=None, direct=0):
# return buffer
pass

if self._bufferOffset == None or self._buffer == None or self._relativePos < self._bufferOffset or (self._relativePos + size) > self._bufferOffset + len(self._buffer):
if self._bufferOffset is None or self._buffer is None or self._relativePos < self._bufferOffset or (
self._relativePos + size) > self._bufferOffset + len(self._buffer):
self.flushBuffer()
#self._bufferOffset = self._relativePos & ~(self._bufferAlign-1)
self._bufferOffset = (int(self._relativePos / self._bufferAlign) * self._bufferAlign)
Expand Down Expand Up @@ -360,7 +361,8 @@ def write(self, value, size=None):
# size = len(value)
size = len(value)

if self._bufferOffset == None or self._buffer == None or self._relativePos < self._bufferOffset or (self._relativePos + size) > self._bufferOffset + len(self._buffer):
if self._bufferOffset is None or self._buffer is None or self._relativePos < self._bufferOffset or (
self._relativePos + size) > self._bufferOffset + len(self._buffer):
self.flushBuffer()

# read page into memory
Expand All @@ -376,7 +378,7 @@ def write(self, value, size=None):
return

def flushBuffer(self):
if self.f != None and self._buffer != None and self._bufferDirty == True:
if self.f is not None and self._buffer is not None and self._bufferDirty:
#Print.info('writing dirty page')
# Hex.dump(self._buffer)
super(BufferedFile, self).seek(self._bufferOffset)
Expand Down Expand Up @@ -472,7 +474,7 @@ def __init__(self, buffer, cryptoType=-1, cryptoKey=-1, cryptoCounter=-1, offset
self.buffer = self.crypto.decrypt(self.buffer)

def read(self, size=None, direct=0):
if size == None:
if size is None:
size = self.size - self._relativePos

return self.buffer[self._relativePos:self._relativePos+size]
Expand Down
22 changes: 15 additions & 7 deletions Fs/IndexedFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __iter__(self):
def isUpdateAvailable(self):
title = self.title()

if self.titleId and str(title.version) != None and str(self.version) < str(title.version) and str(title.version) != '0':
if self.titleId and str(title.version) is not None and str(self.version) < str(title.version) and str(title.version) != '0':
return {'id': title.id, 'baseId': title.baseId, 'currentVersion': str(self.version), 'newVersion': str(title.version)}

if not title.isUpdate and not title.isDLC and Titles.contains(title.updateId):
Expand All @@ -52,7 +52,7 @@ def getId(self):
def setTimestamp(self, timestamp):
try:
self.timestamp = int(str(timestamp), 10)
except:
except BaseException:
pass

def getTimestamp(self):
Expand Down Expand Up @@ -90,15 +90,16 @@ def setHasValidTicket(self, value):

try:
self.hasValidTicket = (True if value and int(value) != 0 else False) or self.title().isUpdate
except:
except BaseException:
pass

def move(self, forceNsp=False):
if not self.path:
Print.error('no path set')
return False

if os.path.abspath(self.path).startswith(os.path.abspath(Config.paths.nspOut)) and not self.path.endswith('.nsz') and not self.path.endswith('.xcz') and Config.compression.auto:
if os.path.abspath(self.path).startswith(os.path.abspath(Config.paths.nspOut)) and not self.path.endswith(
'.nsz') and not self.path.endswith('.xcz') and Config.compression.auto:
nszFile = nut.compress(self.path, Config.compression.level, os.path.abspath(Config.paths.nspOut))

if nszFile:
Expand Down Expand Up @@ -183,7 +184,14 @@ def cleanFilename(self, s):
return s.strip()

def dict(self):
return {"titleId": self.titleId, "hasValidTicket": self.hasValidTicket, 'extractedNcaMeta': self.getExtractedNcaMeta(), 'version': self.version, 'timestamp': self.timestamp, 'path': self.path, 'fileSize': self.fileSize}
return {
"titleId": self.titleId,
"hasValidTicket": self.hasValidTicket,
'extractedNcaMeta': self.getExtractedNcaMeta(),
'version': self.version,
'timestamp': self.timestamp,
'path': self.path,
'fileSize': self.fileSize}

def getCr(self, inverted=False):
if not hasattr(self, 'cr') or not self.cr:
Expand Down Expand Up @@ -222,7 +230,7 @@ def getCr(self, inverted=False):
def fileName(self, forceNsp=False):
bt = None

if not self.titleId in Titles.keys():
if self.titleId not in Titles.keys():
if not Title.getBaseId(self.titleId) in Titles.keys():
Print.error('could not find base title for ' + str(self.titleId) + ' or ' + str(Title.getBaseId(self.titleId)))
return None
Expand All @@ -240,7 +248,7 @@ def fileName(self, forceNsp=False):
return None

try:
if not t.baseId in Titles.keys():
if t.baseId not in Titles.keys():
Print.info('could not find baseId for ' + self.path)
return None
except BaseException as e:
Expand Down
6 changes: 3 additions & 3 deletions Fs/Nca.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def open(self, file=None, mode='rb', cryptoType=-1, cryptoKey=-1, cryptoCounter=

try:
self.contentType = Fs.Type.Content(self.contentType)
except:
except BaseException:
pass

self.cryptoType = self.readInt8()
Expand Down Expand Up @@ -388,7 +388,7 @@ def buildId(self):
return hx(f.read(0x20)).decode('utf8').upper()
except IOError as e:
pass
except:
except BaseException:
raise
return None

Expand All @@ -405,7 +405,7 @@ def verifyKey(self, userkey):

'''
print('\nTesting {} with:'.format(self))
print('- Keygeneration {}'.format(self.masterKey()))
print('- Keygeneration {}'.format(self.masterKey()))
print('- Encrypted key {}'.format(str(hx(encKey))[2:-1]))
print('- Decrypted key {}'.format(str(hx(titleKeyDec))[2:-1]))
'''
Expand Down
30 changes: 15 additions & 15 deletions Fs/Nsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def __init__(self, path=None, mode='rb'):
# self.unlock()

def getFileSize(self):
if self.fileSize == None:
if self.fileSize is None:
try:
self.fileSize = os.path.getsize(self.path)
except:
except BaseException:
pass
return self.fileSize

def getFileModified(self):
if self.fileModified == None:
if self.fileModified is None:
self.fileModified = os.path.getmtime(self.path)
return self.fileModified

Expand Down Expand Up @@ -100,7 +100,7 @@ def getUpdateFile(self):
def isUpdateAvailable(self):
title = self.title()

if self.titleId and str(title.version) != None and str(self.version) < str(title.version) and str(title.version) != '0':
if self.titleId and str(title.version) is not None and str(self.version) < str(title.version) and str(title.version) != '0':
return {'id': title.id, 'baseId': title.baseId, 'currentVersion': str(self.version), 'newVersion': str(title.version)}

if not title.isUpdate and not title.isDLC and Titles.contains(title.updateId):
Expand Down Expand Up @@ -164,20 +164,20 @@ def unpack(self, path):
# extractedNcaMeta

def getExtractedNcaMeta(self):
if hasattr(self, 'extractedNcaMeta') and self.extractedNcaMeta == True:
if hasattr(self, 'extractedNcaMeta') and self.extractedNcaMeta:
return 1
return 0

def setExtractedNcaMeta(self, val):
if val and (val != 0 or val == True):
if val and (val != 0 or val):
self.extractedNcaMeta = True
else:
self.extractedNcaMeta = False

def getHasValidTicket(self):
if self.title().isUpdate:
return 1
return (1 if self.hasValidTicket and self.hasValidTicket == True else 0)
return (1 if self.hasValidTicket and self.hasValidTicket else 0)

def open(self, path=None, mode='rb', cryptoType=-1, cryptoKey=-1, cryptoCounter=-1):
super(Nsp, self).open(path or self.path, mode, cryptoType, cryptoKey, cryptoCounter)
Expand Down Expand Up @@ -216,7 +216,7 @@ def verifyKey(self, titleId, userkey=None):
ticket = self.ticket()
masterKeyRev = ticket.getMasterKeyRevision()
userkey = ticket.getTitleKeyBlock().to_bytes(16, byteorder='big')
except:
except BaseException:
userkey = None
else:
userkey = uhx(userkey)
Expand All @@ -227,7 +227,7 @@ def verifyKey(self, titleId, userkey=None):
try:
if f.verifyKey(userkey):
return True
except:
except BaseException:
pass
return False

Expand All @@ -253,7 +253,7 @@ def setMasterKeyRev(self, newMasterKeyRev):
if rightsId != 0:
raise IOError('please remove titlerights first')

if (newMasterKeyRev == None and rightsId == 0) or masterKeyRev == newMasterKeyRev:
if (newMasterKeyRev is None and rightsId == 0) or masterKeyRev == newMasterKeyRev:
Print.info('Nothing to do')
return

Expand All @@ -263,7 +263,7 @@ def setMasterKeyRev(self, newMasterKeyRev):
Print.info('masterKeyRev =\t' + hex(masterKeyRev))

for nca in self:
if type(nca) == Nca:
if isinstance(nca, Nca):
if nca.header.getCryptoType2() != masterKeyRev:
pass
raise IOError('Mismatched masterKeyRevs!')
Expand All @@ -273,7 +273,7 @@ def setMasterKeyRev(self, newMasterKeyRev):
ticket.setTitleKeyBlock(int.from_bytes(newTitleKey, 'big'))

for nca in self:
if type(nca) == Nca:
if isinstance(nca, Nca):
if nca.header.getCryptoType2() != newMasterKeyRev:
Print.info('writing masterKeyRev for %s, %d -> %s' % (str(nca._path), nca.header.getCryptoType2(), str(newMasterKeyRev)))

Expand Down Expand Up @@ -313,15 +313,15 @@ def removeTitleRights(self):
Print.info('masterKeyRev =\t' + hex(masterKeyRev))

for nca in self:
if type(nca) == Nca:
if isinstance(nca, Nca):
if nca.header.getCryptoType2() != masterKeyRev:
pass
raise IOError('Mismatched masterKeyRevs!')

ticket.setRightsId(0)

for nca in self:
if type(nca) == Nca:
if isinstance(nca, Nca):
if nca.header.getRightsId() == 0:
continue

Expand All @@ -342,7 +342,7 @@ def setGameCard(self, isGameCard=False):
targetValue = 0

for nca in self:
if type(nca) == Nca:
if isinstance(nca, Nca):
if nca.header.getIsGameCard() == targetValue:
continue

Expand Down
4 changes: 2 additions & 2 deletions Fs/Pfs0.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Pfs0Stream():
def __init__(self, path):
try:
os.makedirs(os.path.dirname(path), exist_ok=True)
except:
except BaseException:
pass
self.path = path
self.f = open(path, 'wb')
Expand Down Expand Up @@ -199,7 +199,7 @@ def open(self, path=None, mode='rb', cryptoType=-1, cryptoKey=-1, cryptoCounter=
'''
self.seek(0x10 + fileCount * 0x18)
stringTable = self.read(stringTableSize)
for i in range(fileCount):
if i == fileCount - 1:
self.files[i].name = stringTable[self.files[i].nameOffset:].decode('utf-8').rstrip(' \t\r\n\0')
Expand Down
2 changes: 1 addition & 1 deletion Fs/Ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def open(self, file=None, mode='rb', cryptoType=-1, cryptoKey=-1, cryptoCounter=
self.signatureType = self.readInt32()
try:
self.signatureType = Fs.Type.TicketSignature(self.signatureType)
except:
except BaseException:
raise IOError('Invalid ticket format')

self.signaturePadding = 0x40 - ((self.signatureSizes[self.signatureType] + 4) % 0x40)
Expand Down
Loading

0 comments on commit 7b2bd9b

Please sign in to comment.