コード例 #1
0
def gen_multi_file_header(prlist, filelist):
    oflist = []
    osizelist = []
    ototlist = []
    files = []
    totSize = 0
    for i in range(len(prlist)):
        for j in prlist[i][4]:
            el = j[0]
            if el.endswith('.nca'):
                oflist.append(j[0])
                #print(j[0])
                totSize = totSize + j[1]
                #print(j[1])
            ototlist.append(j[0])
    sec_hashlist = list()
    GClist = list()
    # print(filelist)
    for file in oflist:
        for filepath in filelist:
            if filepath.endswith('.nsp') or filepath.endswith('.nsz'):
                try:
                    f = squirrelNSP(filepath)
                    sha, size, gamecard = f.file_hash(file)
                    if sha != False:
                        sec_hashlist.append(sha)
                        osizelist.append(size)
                        GClist.append([file, gamecard])
                    f.flush()
                    f.close()
                except BaseException as e:
                    Print.error('Exception: ' + str(e))
            if filepath.endswith('.xci') or filepath.endswith('.xcz'):
                try:
                    f = squirrelXCI(filepath)
                    sha, size, gamecard = f.file_hash(file)
                    if sha != False:
                        sec_hashlist.append(sha)
                        osizelist.append(size)
                        GClist.append([file, gamecard])
                    f.flush()
                    f.close()
                except BaseException as e:
                    Print.error('Exception: ' + str(e))
    xci_header, game_info, sig_padding, xci_certificate, root_header, upd_header, norm_header, sec_header, rootSize, upd_multiplier, norm_multiplier, sec_multiplier = sq_tools.get_xciheader(
        oflist, osizelist, sec_hashlist)
    totSize = len(xci_header) + len(game_info) + len(sig_padding) + len(
        xci_certificate) + rootSize
    outheader = xci_header
    outheader += game_info
    outheader += sig_padding
    outheader += xci_certificate
    outheader += root_header
    outheader += upd_header
    outheader += norm_header
    outheader += sec_header
    properheadsize = len(outheader)
    return outheader, properheadsize, totSize, oflist
コード例 #2
0
ファイル: listmanager.py プロジェクト: wyyprince/NSC_BUILDER
def calculate_name(filelist, romanize=True, ext='.xci'):
    from Fs import Nsp as squirrelNSP
    from Fs import Xci as squirrelXCI
    import re
    prlist = list()
    for filepath in filelist:
        if filepath.endswith('.nsp'):
            try:
                c = list()
                f = squirrelNSP(filepath)
                contentlist = f.get_content(False, False, True)
                f.flush()
                f.close()
                if len(prlist) == 0:
                    for i in contentlist:
                        prlist.append(i)
                else:
                    for j in range(len(contentlist)):
                        notinlist = False
                        for i in range(len(prlist)):
                            if contentlist[j][1] == prlist[i][1]:
                                if contentlist[j][6] > prlist[i][6]:
                                    del prlist[i]
                                    prlist.append(contentlist[j])
                                    notinlist = False
                                elif contentlist[j][6] == prlist[i][6]:
                                    notinlist = False
                            else:
                                notinlist = True
                        if notinlist == True:
                            prlist.append(contentlist[j])
            except BaseException as e:
                nutPrint.error('Exception: ' + str(e))
        if filepath.endswith('.xci'):
            try:
                c = list()
                f = squirrelXCI(filepath)
                contentlist = f.get_content(False, False, True)
                f.flush()
                f.close()
                if len(prlist) == 0:
                    for i in contentlist:
                        prlist.append(i)
                else:
                    for j in range(len(contentlist)):
                        notinlist = False
                        for i in range(len(prlist)):
                            if contentlist[j][1] == prlist[i][1]:
                                if contentlist[j][6] > prlist[i][6]:
                                    del prlist[i]
                                    prlist.append(contentlist[j])
                                    notinlist = False
                                elif contentlist[j][6] == prlist[i][6]:
                                    notinlist = False
                            else:
                                notinlist = True
                        if notinlist == True:
                            prlist.append(contentlist[j])
            except BaseException as e:
                nutPrint.error('Exception: ' + str(e))
    basecount = 0
    basename = ''
    basever = ''
    baseid = ''
    basefile = ''
    updcount = 0
    updname = ''
    updver = ''
    updid = ''
    updfile = ''
    dlccount = 0
    dlcname = ''
    dlcver = ''
    dlcid = ''
    dlcfile = ''
    ccount = ''
    bctag = ''
    updtag = ''
    dctag = ''
    for i in range(len(prlist)):
        if prlist[i][5] == 'BASE':
            basecount += 1
            if baseid == "":
                basefile = str(prlist[i][0])
                baseid = str(prlist[i][1])
                basever = '[v' + str(prlist[i][6]) + ']'
        if prlist[i][5] == 'UPDATE':
            updcount += 1
            endver = str(prlist[i][6])
            if updid == "":
                updfile = str(prlist[i][0])
                updid = str(prlist[i][1])
                updver = '[v' + str(prlist[i][6]) + ']'
        if prlist[i][5] == 'DLC':
            dlccount += 1
            if dlcid == "":
                dlcfile = str(prlist[i][0])
                dlcid = str(prlist[i][1])
                dlcver = '[v' + str(prlist[i][6]) + ']'
        if basecount != 0:
            bctag = str(basecount) + 'G'
        else:
            bctag = ''
        if updcount != 0:
            if bctag != '':
                updtag = '+' + str(updcount) + 'U'
            else:
                updtag = str(updcount) + 'U'
        else:
            updtag = ''
        if dlccount != 0:
            if bctag != '' or updtag != '':
                dctag = '+' + str(dlccount) + 'D'
            else:
                dctag = str(dlccount) + 'D'
        else:
            dctag = ''
        ccount = '(' + bctag + updtag + dctag + ')'
    if baseid != "":
        if basefile.endswith('.xci'):
            f = squirrelXCI(basefile)
        elif basefile.endswith('.nsp'):
            f = squirrelNSP(basefile)
        ctitl = f.get_title(baseid)
        f.flush()
        f.close()
        if ctitl == 'DLC' or ctitl == '-':
            ctitl = ''
    elif updid != "":
        if updfile.endswith('.xci'):
            f = squirrelXCI(updfile)
        elif updfile.endswith('.nsp'):
            f = squirrelNSP(updfile)
        ctitl = f.get_title(updid)
        f.flush()
        f.close()
        if ctitl == 'DLC' or ctitl == '-':
            ctitl = ''
    elif dlcid != "":
        ctitl = get_title
        if dlcfile.endswith('.xci'):
            f = squirrelXCI(dlcfile)
        elif dlcfile.endswith('.nsp'):
            f = squirrelNSP(dlcfile)
        ctitl = f.get_title(dlcid)
        f.flush()
        f.close()
    else:
        ctitl = 'UNKNOWN'
    baseid = '[' + baseid.upper() + ']'
    updid = '[' + updid.upper() + ']'
    dlcid = '[' + dlcid.upper() + ']'
    if ccount == '(1G)' or ccount == '(1U)' or ccount == '(1D)':
        ccount = ''
    if baseid != "[]":
        if updver != "":
            endname = ctitl + ' ' + baseid + ' ' + updver + ' ' + ccount
        else:
            endname = ctitl + ' ' + baseid + ' ' + basever + ' ' + ccount
    elif updid != "[]":
        endname = ctitl + ' ' + updid + ' ' + updver + ' ' + ccount
    else:
        endname = ctitl + ' ' + dlcid + ' ' + dlcver + ' ' + ccount
    if romanize == True:
        import pykakasi
        kakasi = pykakasi.kakasi()
        kakasi.setMode("H", "a")
        kakasi.setMode("K", "a")
        kakasi.setMode("J", "a")
        kakasi.setMode("s", True)
        kakasi.setMode("E", "a")
        kakasi.setMode("a", None)
        kakasi.setMode("C", False)
        converter = kakasi.getConverter()
        endname = converter.do(endname)
        endname = endname[0].upper() + endname[1:]
    endname = (re.sub(r'[\/\\\:\*\?]+', '', endname))
    endname = re.sub(r'[™©®`~^´ªº¢#£€¥$ƒ±¬½¼♡«»±•²‰œæƳ☆<<>>|]', '', endname)
    endname = re.sub(r'[Ⅰ]', 'I', endname)
    endname = re.sub(r'[Ⅱ]', 'II', endname)
    endname = re.sub(r'[Ⅲ]', 'III', endname)
    endname = re.sub(r'[Ⅳ]', 'IV', endname)
    endname = re.sub(r'[Ⅴ]', 'V', endname)
    endname = re.sub(r'[Ⅵ]', 'VI', endname)
    endname = re.sub(r'[Ⅶ]', 'VII', endname)
    endname = re.sub(r'[Ⅷ]', 'VIII', endname)
    endname = re.sub(r'[Ⅸ]', 'IX', endname)
    endname = re.sub(r'[Ⅹ]', 'X', endname)
    endname = re.sub(r'[Ⅺ]', 'XI', endname)
    endname = re.sub(r'[Ⅻ]', 'XII', endname)
    endname = re.sub(r'[Ⅼ]', 'L', endname)
    endname = re.sub(r'[Ⅽ]', 'C', endname)
    endname = re.sub(r'[Ⅾ]', 'D', endname)
    endname = re.sub(r'[Ⅿ]', 'M', endname)
    endname = re.sub(r'[—]', '-', endname)
    endname = re.sub(r'[√]', 'Root', endname)
    endname = re.sub(r'[àâá@äå]', 'a', endname)
    endname = re.sub(r'[ÀÂÁÄÅ]', 'A', endname)
    endname = re.sub(r'[èêéë]', 'e', endname)
    endname = re.sub(r'[ÈÊÉË]', 'E', endname)
    endname = re.sub(r'[ìîíï]', 'i', endname)
    endname = re.sub(r'[ÌÎÍÏ]', 'I', endname)
    endname = re.sub(r'[òôóöø]', 'o', endname)
    endname = re.sub(r'[ÒÔÓÖØ]', 'O', endname)
    endname = re.sub(r'[ùûúü]', 'u', endname)
    endname = re.sub(r'[ÙÛÚÜ]', 'U', endname)
    endname = re.sub(r'[’]', "'", endname)
    endname = re.sub(r'[“”]', '"', endname)
    endname = re.sub(' {3,}', ' ', endname)
    re.sub(' {2,}', ' ', endname)
    try:
        endname = endname.replace("( ", "(")
        endname = endname.replace(" )", ")")
        endname = endname.replace("[ ", "[")
        endname = endname.replace(" ]", "]")
        endname = endname.replace("[ (", "[(")
        endname = endname.replace(") ]", ")]")
        endname = endname.replace("[]", "")
        endname = endname.replace("()", "")
        endname = endname.replace('" ', '"')
        endname = endname.replace(' "', '"')
        endname = endname.replace(" !", "!")
        endname = endname.replace(" ?", "?")
        endname = endname.replace("  ", " ")
        endname = endname.replace("  ", " ")
        endname = endname.replace('"', '')
        endname = endname.replace(')', ') ')
        endname = endname.replace(']', '] ')
        endname = endname.replace("[ (", "[(")
        endname = endname.replace(") ]", ")]")
        endname = endname.replace("  ", " ")
    except:
        pass
    if endname[-1] == ' ':
        endname = endname[:-1]
    endname = endname + ext
    return endname, prlist
コード例 #3
0
ファイル: compressor.py プロジェクト: Kolduncheg/NSC_BUILDER
def xci_to_nsz(filepath,
               buffer=65536,
               outfile=None,
               keepupd=False,
               level=17,
               threads=0,
               pos=False,
               nthreads=False):
    isthreaded = False
    if pos != False:
        isthreaded = True
    elif str(pos) == '0':
        isthreaded = True
    else:
        pos = 0
    pos = int(pos)
    try:
        exchangefile.deletefile()
    except:
        pass
    f = squirrelXCI(filepath)
    for nspF in f.hfs0:
        if str(nspF._path) == "secure":
            for ticket in nspF:
                if str(ticket._path).endswith('.tik'):
                    if isthreaded == False:
                        print('- Titlerights: ' + ticket.rightsId)
                    tk = (str(hex(ticket.getTitleKeyBlock()))[2:]).upper()
                    if isthreaded == False:
                        print('- Titlekey: ' + tk)
                    exchangefile.add(ticket.rightsId, tk)
    f.flush()
    f.close()
    files_list = sq_tools.ret_xci_offsets(filepath)
    files = list()
    filesizes = list()
    if isthreaded == True and nthreads != False:
        tqlist = list()
        for i in range(nthreads):
            tq = tqdm(total=0,
                      unit='B',
                      unit_scale=True,
                      leave=False,
                      position=i)
            tqlist.append(tq)
    fplist = list()
    for k in range(len(files_list)):
        entry = files_list[k]
        fplist.append(entry[0])
    for i in range(len(files_list)):
        entry = files_list[i]
        cnmtfile = entry[0]
        if cnmtfile.endswith('.cnmt.nca'):
            f = squirrelXCI(filepath)
            titleid, titleversion, base_ID, keygeneration, rightsId, RSV, RGV, ctype, metasdkversion, exesdkversion, hasHtmlManual, Installedsize, DeltaSize, ncadata = f.get_data_from_cnmt(
                cnmtfile)
            f.flush()
            f.close()
            for j in range(len(ncadata)):
                row = ncadata[j]
                # print(row)
                if row['NCAtype'] != 'Meta':
                    test1 = str(row['NcaId']) + '.nca'
                    test2 = str(row['NcaId']) + '.ncz'
                    if test1 in fplist or test2 in fplist:
                        # print(str(row['NcaId'])+'.nca')
                        files.append(str(row['NcaId']) + '.nca')
                        filesizes.append(int(row['Size']))
                else:
                    # print(str(row['NcaId'])+'.cnmt.nca')
                    files.append(str(row['NcaId']) + '.cnmt.nca')
                    filesizes.append(int(row['Size']))
    for k in range(len(files_list)):
        entry = files_list[k]
        fp = entry[0]
        sz = int(entry[3])
        if fp.endswith('xml'):
            files.append(fp)
            filesizes.append(sz)
    for k in range(len(files_list)):
        entry = files_list[k]
        fp = entry[0]
        sz = int(entry[3])
        if fp.endswith('.tik'):
            files.append(fp)
            filesizes.append(sz)
    for k in range(len(files_list)):
        entry = files_list[k]
        fp = entry[0]
        sz = int(entry[3])
        if fp.endswith('.cert'):
            files.append(fp)
            filesizes.append(sz)
    nspheader = sq_tools.gen_nsp_header(files, filesizes)
    properheadsize = len(nspheader)

    compressionLevel = int(level)
    CHUNK_SZ = buffer
    if outfile == None:
        nszPath = filepath[0:-1] + 'z'
    else:
        nszPath = outfile
    nszPath = os.path.abspath(nszPath)

    tsize = properheadsize
    for sz in filesizes:
        tsize += sz
    if isthreaded == True:
        from colorama import Fore
        colors = Fore.__dict__
        k = 0
        l = pos
        for col in colors:
            if l > len(colors):
                l = l - len(colors)
            color = colors[col]
            if k == (l + 1):
                break
            else:
                k += 1
        t = tqdm(total=tsize,
                 unit='B',
                 unit_scale=True,
                 leave=False,
                 position=pos,
                 bar_format="{l_bar}%s{bar}%s{r_bar}" % (color, Fore.RESET))
    else:
        t = tqdm(total=tsize,
                 unit='B',
                 unit_scale=True,
                 leave=False,
                 position=0)

    if isthreaded == False:
        t.write('Compressing with level %d and %d threads' %
                (compressionLevel, threads))
        t.write('\n  %s -> %s \n' % (filepath, nszPath))

    newNsp = nutFs.Pfs0.Pfs0Stream(nszPath,
                                   headsize=properheadsize,
                                   mode='wb+')

    xcicontainer = Xci(filepath)
    # f.compressed_supertrim(buffer,outfile,keepupd,level,threads)
    for nspF in xcicontainer.hfs0:
        if str(nspF._path) == "secure":
            for nca in nspF:
                if isinstance(nca, nutFs.Nca.Nca) and (
                        nca.header.contentType == nutFs.Type.Content.PROGRAM
                        or nca.header.contentType
                        == nutFs.Type.Content.PUBLIC_DATA):
                    if isNcaPacked(nca):
                        cctx = zstandard.ZstdCompressor(level=compressionLevel,
                                                        threads=threads)

                        newFileName = nca._path[0:-1] + 'z'

                        f = newNsp.add(newFileName, nca.size, t, isthreaded)

                        start = f.tell()

                        nca.seek(0)
                        data = nca.read(ncaHeaderSize)
                        f.write(data)
                        nca.seek(ncaHeaderSize)

                        written = ncaHeaderSize

                        compressor = cctx.stream_writer(f)

                        sections = get_sections(nca)

                        header = b'NCZSECTN'
                        header += len(sections).to_bytes(8, 'little')

                        i = 0
                        for fs in sections:
                            i += 1
                            header += fs.offset.to_bytes(8, 'little')
                            header += fs.size.to_bytes(8, 'little')
                            header += fs.cryptoType.to_bytes(8, 'little')
                            header += b'\x00' * 8
                            header += fs.cryptoKey
                            header += fs.cryptoCounter

                        f.write(header)
                        written += len(header)
                        timestamp = time.time()
                        decompressedBytes = ncaHeaderSize
                        totsize = 0
                        for fs in sections:
                            totsize += fs.size

                        for section in sections:
                            #print('offset: %x\t\tsize: %x\t\ttype: %d\t\tiv%s' % (section.offset, section.size, section.cryptoType, str(hx(section.cryptoCounter))))
                            o = nca.partition(offset=section.offset,
                                              size=section.size,
                                              n=None,
                                              cryptoType=section.cryptoType,
                                              cryptoKey=section.cryptoKey,
                                              cryptoCounter=bytearray(
                                                  section.cryptoCounter),
                                              autoOpen=True)

                            while not o.eof():
                                buffer = o.read(CHUNK_SZ)
                                t.update(len(buffer))
                                if len(buffer) == 0:
                                    raise IOError('read failed')

                                written += compressor.write(buffer)

                                decompressedBytes += len(buffer)

                        compressor.flush(zstandard.FLUSH_FRAME)

                        elapsed = time.time() - timestamp
                        minutes = elapsed / 60
                        seconds = elapsed % 60

                        speed = 0 if elapsed == 0 else (nca.size / elapsed)

                        written = f.tell() - start
                        if isthreaded == False:
                            t.write(
                                '\n  * Compressed at %d%% from %s to %s  - %s'
                                % (int(written * 100 / nca.size),
                                   str(sq_tools.getSize(decompressedBytes)),
                                   str(sq_tools.getSize(written)), nca._path))
                            t.write(
                                '  * Compressed in %02d:%02d at speed: %.1f MB/s\n'
                                % (minutes, seconds, speed / 1000000.0))
                        newNsp.resize(newFileName, written)
                        continue
                    else:
                        if isthreaded == False:
                            t.write('not packed!')

                f = newNsp.add(nca._path, nca.size, t, isthreaded)

                nca.seek(0)
                while not nca.eof():
                    buffer = nca.read(CHUNK_SZ)
                    t.update(len(buffer))
                    f.write(buffer)
    t.close()
    newNsp.close()
    try:
        exchangefile.deletefile()
    except:
        pass
    if isthreaded == True and nthreads != False:
        for i in range(nthreads):
            tqlist[i].close()
    return nszPath
コード例 #4
0
def gen_xci_parts(filepath,cachefolder=None,keepupd=False,id_target=False,keypatch=False,export_type='csv'):
	if not os.path.exists(cachefolder):
		os.makedirs(cachefolder)
	else:
		for f in os.listdir(cachefolder):
			fp = os.path.join(cachefolder, f)
			try:
				shutil.rmtree(fp)
			except OSError:
				os.remove(fp)
	files_list=sq_tools.ret_xci_offsets(filepath)
	files=list();filesizes=list()
	fplist=list()
	for k in range(len(files_list)):
		entry=files_list[k]
		fplist.append(entry[0])
	for i in range(len(files_list)):
		entry=files_list[i]
		cnmtfile=entry[0]
		if cnmtfile.endswith('.cnmt.nca'):
			f=squirrelXCI(filepath)
			titleid,titleversion,base_ID,keygeneration,rightsId,RSV,RGV,ctype,metasdkversion,exesdkversion,hasHtmlManual,Installedsize,DeltaSize,ncadata=f.get_data_from_cnmt(cnmtfile)
			f.flush()
			f.close()
			for j in range(len(ncadata)):
				row=ncadata[j]
				# print(row)
				if row['NCAtype']!='Meta' and row['NCAtype']!='Program':
					test1=str(row['NcaId'])+'.nca';test2=str(row['NcaId'])+'.ncz'
					if test1 in fplist or test2 in fplist:
						# print(str(row['NcaId'])+'.nca')
						files.append(str(row['NcaId'])+'.nca')
						filesizes.append(int(row['Size']))					
				elif row['NCAtype']=='Meta':
					# print(str(row['NcaId'])+'.cnmt.nca')
					files.append(str(row['NcaId'])+'.cnmt.nca')
					filesizes.append(int(row['Size']))	
			for j in range(len(ncadata)):
				row=ncadata[j]
				# print(row)
				if row['NCAtype']=='Program':
					test1=str(row['NcaId'])+'.nca';test2=str(row['NcaId'])+'.ncz'
					if test1 in fplist or test2 in fplist:
						# print(str(row['NcaId'])+'.nca')
						files.append(str(row['NcaId'])+'.nca')
						filesizes.append(int(row['Size']))				
	f.flush()
	f.close()						
	outheader = sq_tools.gen_nsp_header(files,filesizes)	
	properheadsize=len(outheader)
	# print(properheadsize)
	# print(bucketsize)
	i=0;sum=properheadsize;
	for file in files:
		# print(file)
		# print(filesizes[i])
		if i<(len(files)-1):
			sum+=filesizes[i]
		i+=1
	# print(sum)
	# print(sum/bucketsize)
	multiplier=math.ceil(sum/bucketsize)
	# print(multiplier)
	remainder = bucketsize*multiplier - sum
	# print(bucketsize*multiplier)
	xci=squirrelXCI(filepath)
	outfile=os.path.join(cachefolder, "0")
	written=0;
	outf = open(outfile, 'w+b')		
	outf.write(outheader)	
	written+=len(outheader)
	for fi in files:
		for nspF in xci.hfs0:
			if str(nspF._path)=="secure":
				for nca in nspF:	
					if nca._path==fi:
						crypto1=nca.header.getCryptoType()
						crypto2=nca.header.getCryptoType2()	
						if crypto2>crypto1:
							masterKeyRev=crypto2
						if crypto2<=crypto1:	
							masterKeyRev=crypto1						
						crypto = aes128.AESECB(Keys.keyAreaKey(Keys.getMasterKeyIndex(masterKeyRev), nca.header.keyIndex))
						hcrypto = aes128.AESXTS(uhx(Keys.get('header_key')))	
						gc_flag='00'*0x01					
						crypto1=nca.header.getCryptoType()
						crypto2=nca.header.getCryptoType2()					
						if nca.header.getRightsId() != 0:				
							nca.rewind()	
							encKeyBlock = crypto.encrypt(titleKeyDec * 4)
							if str(keypatch) != "False":
								if keypatch < nca.header.getCryptoType2():
									encKeyBlock,crypto1,crypto2=squirrelXCI.get_new_cryptoblock(nca, keypatch,encKeyBlock,t)	
						if nca.header.getRightsId() == 0:
							nca.rewind()				
							encKeyBlock = nca.header.getKeyBlock()	
							if str(keypatch) != "False":
								if keypatch < nca.header.getCryptoType2():
									encKeyBlock,crypto1,crypto2=squirrelXCI.get_new_cryptoblock(nca, keypatch,encKeyBlock,t)					
						nca.rewind()					
						i=0				
						newheader=xci.get_newheader(nca,encKeyBlock,crypto1,crypto2,hcrypto,gc_flag)		
						outf.write(newheader)
						written+=len(newheader)
						nca.seek(0xC00)	
						if (str(nca.header.contentType) != 'Content.PROGRAM'):
							data=nca.read()	
							nca.close()
							outf.write(data)
							written+=len(data)
						else:
							remainder = bucketsize*multiplier - written
							data=nca.read(remainder)
							nca.close()
							outf.write(data)
							written+=len(data)						
						break					
					else:pass
	outf.close()		
	if export_type=='json':
		files_json=os.path.join(cachefolder, "files.json")	
		d={}
		d['0']={"step": 0,
		  "filepath":outfile ,
		  "size":( bucketsize*multiplier) ,
		  "targetsize":( bucketsize*multiplier) ,
		  "off1":0,
		  "off2":( bucketsize*multiplier)
		}
		for j in files_list:
			if j[0]==files[-1]:
				off1=j[1]
				off2=j[2]
				targetsize=j[3]
		d['1']={"step": 1,
		  "filepath":filepath ,
		  "size":( os.path.getsize(filepath)) ,
		  "targetsize":targetsize,
		  "off1":off1,
		  "off2":off2
		}	
		app_json = json.dumps(d, indent=1)
		with open(files_json, 'w') as json_file:
		  json_file.write(app_json)			
		# print(os.path.getsize(filepath))	
	else:
		for j in files_list:
			if j[0]==files[-1]:
				off1=j[1]
				off2=j[2]
				targetsize=j[3]	
		tfile=os.path.join(cachefolder, "files.csv")
		i=0;
		with open(tfile,'w') as csvfile:
			if i==0:
				csvfile.write("{}|{}|{}|{}|{}|{}\n".format("step","filepath","size","targetsize","off1","off2"))
				i+=1
			if i==1:	
				csvfile.write("{}|{}|{}|{}|{}|{}\n".format(0,outfile,( bucketsize*multiplier),( bucketsize*multiplier),0,( bucketsize*multiplier)))		
				i+=1				
			if i==2:	
				csvfile.write("{}|{}|{}|{}|{}|{}".format(1,filepath,( os.path.getsize(filepath)),targetsize,off1,off2))		
				i+=1			
コード例 #5
0
def get_header_size(filepath):
	properheadsize=0;sz=0
	if filepath.endswith('xci') or filepath.endswith('xcz'):
		files_list=sq_tools.ret_xci_offsets(filepath)
		files=list();filesizes=list()
		fplist=list()
		for k in range(len(files_list)):
			entry=files_list[k]
			fplist.append(entry[0])
		for i in range(len(files_list)):
			entry=files_list[i]
			cnmtfile=entry[0]
			if cnmtfile.endswith('.cnmt.nca'):
				f=squirrelXCI(filepath)
				titleid,titleversion,base_ID,keygeneration,rightsId,RSV,RGV,ctype,metasdkversion,exesdkversion,hasHtmlManual,Installedsize,DeltaSize,ncadata=f.get_data_from_cnmt(cnmtfile)
				for j in range(len(ncadata)):
					row=ncadata[j]
					# print(row)
					if row['NCAtype']!='Meta':
						test1=str(row['NcaId'])+'.nca';test2=str(row['NcaId'])+'.ncz'
						if test1 in fplist or test2 in fplist:
							# print(str(row['NcaId'])+'.nca')
							files.append(str(row['NcaId'])+'.nca')
							filesizes.append(int(row['Size']))	
							sz+=int(row['Size'])		
					elif row['NCAtype']=='Meta':
						# print(str(row['NcaId'])+'.cnmt.nca')
						files.append(str(row['NcaId'])+'.cnmt.nca')
						filesizes.append(int(row['Size']))	
						sz+=int(row['Size'])
		sec_hashlist=list()
		try:
			for file in files:
				sha,size,gamecard=f.file_hash(file)
				# print(sha)
				if sha != False:
					sec_hashlist.append(sha)	
		except BaseException as e:
			Print.error('Exception: ' + str(e))									
		f.flush()
		f.close()	
		xci_header,game_info,sig_padding,xci_certificate,root_header,upd_header,norm_header,sec_header,rootSize,upd_multiplier,norm_multiplier,sec_multiplier=sq_tools.get_xciheader(files,filesizes,sec_hashlist)			
		outheader=xci_header
		outheader+=game_info
		outheader+=sig_padding
		outheader+=xci_certificate
		outheader+=root_header
		outheader+=upd_header
		outheader+=norm_header
		outheader+=sec_header		
	elif filepath.endswith('nsp') or filepath.endswith('nsz'):
		files_list=sq_tools.ret_nsp_offsets(filepath)
		files=list();filesizes=list()
		fplist=list()
		for k in range(len(files_list)):
			entry=files_list[k]
			fplist.append(entry[0])
		for i in range(len(files_list)):
			entry=files_list[i]
			cnmtfile=entry[0]
			if cnmtfile.endswith('.cnmt.nca'):
				f=squirrelNSP(filepath)
				titleid,titleversion,base_ID,keygeneration,rightsId,RSV,RGV,ctype,metasdkversion,exesdkversion,hasHtmlManual,Installedsize,DeltaSize,ncadata=f.get_data_from_cnmt(cnmtfile)
				f.flush()
				f.close()
				for j in range(len(ncadata)):
					row=ncadata[j]
					# print(row)
					if row['NCAtype']!='Meta':
						test1=str(row['NcaId'])+'.nca';test2=str(row['NcaId'])+'.ncz'
						if test1 in fplist or test2 in fplist:
							# print(str(row['NcaId'])+'.nca')
							files.append(str(row['NcaId'])+'.nca')
							filesizes.append(int(row['Size']))	
							sz+=int(row['Size'])								
					elif row['NCAtype']=='Meta':
						# print(str(row['NcaId'])+'.cnmt.nca')
						files.append(str(row['NcaId'])+'.cnmt.nca')
						filesizes.append(int(row['Size']))
						sz+=int(row['Size'])												
		f.flush()
		f.close()	
		outheader = sq_tools.gen_nsp_header(files,filesizes)			
	properheadsize=len(outheader)
	return properheadsize,keygeneration,sz
コード例 #6
0
def gen_xci_parts_spec1(filepath,
                        target_cnmt=None,
                        cachefolder=None,
                        keypatch=False):
    if keypatch != False:
        try:
            keypatch = int(keypatch)
        except:
            keypatch = False
    if cachefolder == None:
        cachefolder = os.path.join(ztools_dir, '_mtp_cache_')
    if not os.path.exists(cachefolder):
        os.makedirs(cachefolder)
    else:
        for f in os.listdir(cachefolder):
            fp = os.path.join(cachefolder, f)
            try:
                shutil.rmtree(fp)
            except OSError:
                os.remove(fp)
    files_list = sq_tools.ret_xci_offsets(filepath)
    files = list()
    filesizes = list()
    fplist = list()
    for k in range(len(files_list)):
        entry = files_list[k]
        fplist.append(entry[0])
    if target_cnmt == None:
        for i in range(len(files_list)):
            entry = files_list[i]
            cnmtfile = entry[0]
            if cnmtfile.endswith('.cnmt.nca'):
                target_cnmt = cnmtfile
                break
    for i in range(len(files_list)):
        entry = files_list[i]
        cnmtfile = entry[0]
        if cnmtfile.endswith('.cnmt.nca') and target_cnmt == cnmtfile:
            f = squirrelXCI(filepath)
            titleid, titleversion, base_ID, keygeneration, rightsId, RSV, RGV, ctype, metasdkversion, exesdkversion, hasHtmlManual, Installedsize, DeltaSize, ncadata = f.get_data_from_cnmt(
                cnmtfile)
            f.flush()
            f.close()
            for j in range(len(ncadata)):
                row = ncadata[j]
                # print(row)
                if row['NCAtype'] != 'Meta' and row['NCAtype'] != 'Program':
                    test1 = str(row['NcaId']) + '.nca'
                    test2 = str(row['NcaId']) + '.ncz'
                    if test1 in fplist:
                        files.append(str(row['NcaId']) + '.nca')
                        filesizes.append(int(row['Size']))
                    elif test2 in fplist:
                        files.append(str(row['NcaId']) + '.ncz')
                        for k in range(len(files_list)):
                            entry = files_list[k]
                            if entry[0] == test2:
                                filesizes.append(int(entry[3]))
                                break
            for j in range(len(ncadata)):
                row = ncadata[j]
                if row['NCAtype'] == 'Meta':
                    # print(str(row['NcaId'])+'.cnmt.nca')
                    files.append(str(row['NcaId']) + '.cnmt.nca')
                    filesizes.append(int(row['Size']))
            for j in range(len(ncadata)):
                row = ncadata[j]
                # print(row)
                if row['NCAtype'] == 'Program':
                    test1 = str(row['NcaId']) + '.nca'
                    test2 = str(row['NcaId']) + '.ncz'
                    if test1 in fplist:
                        files.append(str(row['NcaId']) + '.nca')
                        filesizes.append(int(row['Size']))
                    elif test2 in fplist:
                        files.append(str(row['NcaId']) + '.ncz')
                        for k in range(len(files_list)):
                            entry = files_list[k]
                            if entry[0] == test2:
                                filesizes.append(int(entry[3]))
                                break
            break
    f.flush()
    f.close()
    outheader = sq_tools.gen_nsp_header(files, filesizes)
    properheadsize = len(outheader)
    # print(properheadsize)
    # print(bucketsize)
    i = 0
    sum = properheadsize
    xci = squirrelXCI(filepath)
    outfile = os.path.join(cachefolder, "0")
    outf = open(outfile, 'w+b')
    outf.write(outheader)
    written = 0
    for fi in files:
        for nspF in xci.hfs0:
            if str(nspF._path) == "secure":
                for nca in nspF:
                    if nca._path == fi:
                        nca = Nca(nca)
                        crypto1 = nca.header.getCryptoType()
                        crypto2 = nca.header.getCryptoType2()
                        if crypto2 > crypto1:
                            masterKeyRev = crypto2
                        if crypto2 <= crypto1:
                            masterKeyRev = crypto1
                        crypto = aes128.AESECB(
                            Keys.keyAreaKey(
                                Keys.getMasterKeyIndex(masterKeyRev),
                                nca.header.keyIndex))
                        hcrypto = aes128.AESXTS(uhx(Keys.get('header_key')))
                        gc_flag = '00' * 0x01
                        crypto1 = nca.header.getCryptoType()
                        crypto2 = nca.header.getCryptoType2()
                        if nca.header.getRightsId() != 0:
                            nca.rewind()
                            if crypto2 > crypto1:
                                masterKeyRev = crypto2
                            if crypto2 <= crypto1:
                                masterKeyRev = crypto1
                            from mtp_tools import get_nca_ticket
                            check, titleKey = get_nca_ticket(filepath, fi)
                            if check == False:
                                sys.exit("Can't verify titleckey")
                            titleKeyDec = Keys.decryptTitleKey(
                                titleKey,
                                Keys.getMasterKeyIndex(int(masterKeyRev)))
                            encKeyBlock = crypto.encrypt(titleKeyDec * 4)
                            if str(keypatch) != "False":
                                t = tqdm(total=False,
                                         unit='B',
                                         unit_scale=False,
                                         leave=False)
                                if keypatch < nca.header.getCryptoType2():
                                    encKeyBlock, crypto1, crypto2 = squirrelXCI.get_new_cryptoblock(
                                        squirrelXCI, nca, keypatch,
                                        encKeyBlock, t)
                                t.close()
                        if nca.header.getRightsId() == 0:
                            nca.rewind()
                            encKeyBlock = nca.header.getKeyBlock()
                            if str(keypatch) != "False":
                                t = tqdm(total=False,
                                         unit='B',
                                         unit_scale=False,
                                         leave=False)
                                if keypatch < nca.header.getCryptoType2():
                                    encKeyBlock, crypto1, crypto2 = squirrelXCI.get_new_cryptoblock(
                                        squirrelXCI, nca, keypatch,
                                        encKeyBlock, t)
                                t.close()
                        nca.rewind()
                        i = 0
                        newheader = xci.get_newheader(nca, encKeyBlock,
                                                      crypto1, crypto2,
                                                      hcrypto, gc_flag)
                        outf.write(newheader)
                        written += len(newheader)
                        nca.seek(0xC00)
                        break
                    else:
                        pass
    xci.flush()
    xci.close()
    outf.flush()
    outf.close()
    tfile = os.path.join(cachefolder, "files.csv")
    with open(tfile, 'w') as csvfile:
        csvfile.write("{}|{}|{}|{}|{}|{}\n".format("step", "filepath", "size",
                                                   "targetsize", "off1",
                                                   "off2"))
        csvfile.write("{}|{}|{}|{}|{}|{}\n".format(0, outfile,
                                                   properheadsize + written,
                                                   properheadsize, 0,
                                                   properheadsize))
        k = 0
        l = 0
        for fi in files:
            for j in files_list:
                if j[0] == fi:
                    csvfile.write("{}|{}|{}|{}|{}|{}\n".format(
                        k + 1, outfile, properheadsize + written, 0xC00,
                        (properheadsize + l * 0xC00),
                        (properheadsize + (l * 0xC00) + 0xC00)))
                    off1 = j[1] + 0xC00
                    off2 = j[2]
                    targetsize = j[3] - 0xC00
                    csvfile.write("{}|{}|{}|{}|{}|{}\n".format(
                        k + 2, filepath, (os.path.getsize(filepath)),
                        targetsize, off1, off2))
                    break
            k += 2
            l += 1
    nspname = "test.nsp"
    try:
        g = os.path.basename(filepath)
        g0 = [pos for pos, char in enumerate(g) if char == '[']
        g0 = (g[0:g0[0]]).strip()
        nspname = f"{g0} [{titleid}] [v{titleversion}] [{ctype}].nsp"
    except:
        pass
    return nspname
コード例 #7
0
def gen_xci_parts_spec0(filepath,
                        target_cnmt=None,
                        cachefolder=None,
                        keypatch=False,
                        export_type='csv'):
    if cachefolder == None:
        cachefolder = os.path.join(ztools_dir, '_mtp_cache_')
    if not os.path.exists(cachefolder):
        os.makedirs(cachefolder)
    else:
        for f in os.listdir(cachefolder):
            fp = os.path.join(cachefolder, f)
            try:
                shutil.rmtree(fp)
            except OSError:
                os.remove(fp)
    program_name = None
    files_list = sq_tools.ret_xci_offsets(filepath)
    files = list()
    filesizes = list()
    fplist = list()
    for k in range(len(files_list)):
        entry = files_list[k]
        fplist.append(entry[0])
    if target_cnmt == None:
        for i in range(len(files_list)):
            entry = files_list[i]
            cnmtfile = entry[0]
            if cnmtfile.endswith('.cnmt.nca'):
                target_cnmt = cnmtfile
                break
    for i in range(len(files_list)):
        entry = files_list[i]
        cnmtfile = entry[0]
        if cnmtfile.endswith('.cnmt.nca') and target_cnmt == cnmtfile:
            f = squirrelXCI(filepath)
            titleid, titleversion, base_ID, keygeneration, rightsId, RSV, RGV, ctype, metasdkversion, exesdkversion, hasHtmlManual, Installedsize, DeltaSize, ncadata = f.get_data_from_cnmt(
                cnmtfile)
            f.flush()
            f.close()
            for j in range(len(ncadata)):
                row = ncadata[j]
                # print(row)
                if row['NCAtype'] != 'Meta' and row['NCAtype'] != 'Program':
                    test1 = str(row['NcaId']) + '.nca'
                    test2 = str(row['NcaId']) + '.ncz'
                    if test1 in fplist or test2 in fplist:
                        # print(str(row['NcaId'])+'.nca')
                        if test1 in fplist:
                            files.append(str(row['NcaId']) + '.nca')
                            filesizes.append(int(row['Size']))
                        elif test2 in fplist:
                            files.append(str(row['NcaId']) + '.ncz')
                            for k in range(len(files_list)):
                                entry = files_list[k]
                                if entry[0] == test2:
                                    filesizes.append(int(entry[3]))
                                    break
            for j in range(len(ncadata)):
                row = ncadata[j]
                if row['NCAtype'] == 'Meta':
                    # print(str(row['NcaId'])+'.cnmt.nca')
                    files.append(str(row['NcaId']) + '.cnmt.nca')
                    filesizes.append(int(row['Size']))
            for j in range(len(ncadata)):
                row = ncadata[j]
                # print(row)
                if row['NCAtype'] == 'Program':
                    test1 = str(row['NcaId']) + '.nca'
                    test2 = str(row['NcaId']) + '.ncz'
                    if test1 in fplist or test2 in fplist:
                        # print(str(row['NcaId'])+'.nca')
                        if test1 in fplist:
                            files.append(str(row['NcaId']) + '.nca')
                            filesizes.append(int(row['Size']))
                        elif test2 in fplist:
                            files.append(str(row['NcaId']) + '.ncz')
                            for k in range(len(files_list)):
                                entry = files_list[k]
                                if entry[0] == test2:
                                    filesizes.append(int(entry[3]))
                                    break
            break
    f.flush()
    f.close()
    outheader = sq_tools.gen_nsp_header(files, filesizes)
    properheadsize = len(outheader)
    # print(properheadsize)
    # print(bucketsize)
    i = 0
    sum = properheadsize
    for file in files:
        # print(file)
        # print(filesizes[i])
        if i < (len(files) - 1):
            sum += filesizes[i]
        i += 1
    # print(sum)
    # print(sum/bucketsize)
    multiplier = math.ceil(sum / bucketsize)
    # print(multiplier)
    remainder = bucketsize * multiplier - sum
    # print(bucketsize*multiplier)
    xci = squirrelXCI(filepath)
    outfile = os.path.join(cachefolder, "0")
    written = 0
    outf = open(outfile, 'w+b')
    outf.write(outheader)
    written += len(outheader)
    movoffset = 0
    for fi in files:
        for nspF in xci.hfs0:
            if str(nspF._path) == "secure":
                for nca in nspF:
                    if nca._path == fi:
                        nca = Nca(nca)
                        crypto1 = nca.header.getCryptoType()
                        crypto2 = nca.header.getCryptoType2()
                        if crypto2 > crypto1:
                            masterKeyRev = crypto2
                        if crypto2 <= crypto1:
                            masterKeyRev = crypto1
                        crypto = aes128.AESECB(
                            Keys.keyAreaKey(
                                Keys.getMasterKeyIndex(masterKeyRev),
                                nca.header.keyIndex))
                        hcrypto = aes128.AESXTS(uhx(Keys.get('header_key')))
                        gc_flag = '00' * 0x01
                        crypto1 = nca.header.getCryptoType()
                        crypto2 = nca.header.getCryptoType2()
                        if nca.header.getRightsId() != 0:
                            nca.rewind()
                            if crypto2 > crypto1:
                                masterKeyRev = crypto2
                            if crypto2 <= crypto1:
                                masterKeyRev = crypto1
                            from mtp_tools import get_nca_ticket
                            check, titleKey = get_nca_ticket(filepath, fi)
                            if check == False:
                                sys.exit("Can't verify titleckey")
                            titleKeyDec = Keys.decryptTitleKey(
                                titleKey,
                                Keys.getMasterKeyIndex(int(masterKeyRev)))
                            encKeyBlock = crypto.encrypt(titleKeyDec * 4)
                            if str(keypatch) != "False":
                                t = tqdm(total=False,
                                         unit='B',
                                         unit_scale=False,
                                         leave=False)
                                if keypatch < nca.header.getCryptoType2():
                                    encKeyBlock, crypto1, crypto2 = squirrelXCI.get_new_cryptoblock(
                                        squirrelXCI, nca, keypatch,
                                        encKeyBlock, t)
                                t.close()
                        if nca.header.getRightsId() == 0:
                            nca.rewind()
                            encKeyBlock = nca.header.getKeyBlock()
                            if str(keypatch) != "False":
                                t = tqdm(total=False,
                                         unit='B',
                                         unit_scale=False,
                                         leave=False)
                                if keypatch < nca.header.getCryptoType2():
                                    encKeyBlock, crypto1, crypto2 = squirrelXCI.get_new_cryptoblock(
                                        squirrelXCI, nca, keypatch,
                                        encKeyBlock, t)
                                t.close()
                        nca.rewind()
                        i = 0
                        newheader = xci.get_newheader(nca, encKeyBlock,
                                                      crypto1, crypto2,
                                                      hcrypto, gc_flag)
                        outf.write(newheader)
                        written += len(newheader)
                        nca.seek(0xC00)
                        movoffset += 0xC00
                        if (str(nca.header.contentType) != 'Content.PROGRAM'):
                            data = nca.read()
                            nca.close()
                            outf.write(data)
                            written += len(data)
                        break
                    else:
                        pass
    xci.flush()
    xci.close()
    outf.close()
    if export_type == 'json':
        files_json = os.path.join(cachefolder, "files.json")
        d = {}
        d['0'] = {
            "step": 0,
            "filepath": outfile,
            "size": (written),
            "targetsize": (written),
            "off1": 0,
            "off2": (written)
        }
        for j in files_list:
            if j[0] == files[-1]:
                off1 = j[1] + 0xC00
                off2 = j[2]
                targetsize = j[3] - 0xC00
        d['1'] = {
            "step": 1,
            "filepath": filepath,
            "size": (os.path.getsize(filepath)),
            "targetsize": targetsize,
            "off1": off1,
            "off2": off2
        }
        app_json = json.dumps(d, indent=1)
        with open(files_json, 'w') as json_file:
            json_file.write(app_json)
        # print(os.path.getsize(filepath))
    else:
        for j in files_list:
            if j[0] == files[-1]:
                off1 = j[1] + 0xC00
                off2 = j[2]
                targetsize = j[3] - 0xC00
        tfile = os.path.join(cachefolder, "files.csv")
        i = 0
        while True:
            with open(tfile, 'w') as csvfile:
                if i == 0:
                    csvfile.write("{}|{}|{}|{}|{}|{}\n".format(
                        "step", "filepath", "size", "targetsize", "off1",
                        "off2"))
                    i += 1
                if i == 1:
                    csvfile.write("{}|{}|{}|{}|{}|{}\n".format(
                        0, outfile, os.path.getsize(outfile),
                        os.path.getsize(outfile), 0, os.path.getsize(outfile)))
                    i += 1
                if i == 2:
                    csvfile.write("{}|{}|{}|{}|{}|{}".format(
                        1, filepath, (os.path.getsize(filepath)), targetsize,
                        off1, off2))
                    i += 1
                    break
    nspname = "test.nsp"
    try:
        g = os.path.basename(filepath)
        g0 = [pos for pos, char in enumerate(g) if char == '[']
        g0 = (g[0:g0[0]]).strip()
        nspname = f"{g0} [{titleid}] [v{titleversion}] [{ctype}].nsp"
    except:
        pass
    return nspname
コード例 #8
0
ファイル: mtpxci.py プロジェクト: tiliarou/NSC_BUILDER
def gen_xci_parts_spec0(filepath,target_cnmt=None,cachefolder=None,keypatch=False,export_type='csv'):
	if cachefolder==None:
		cachefolder=os.path.join(ztools_dir, '_mtp_cache_')	
	if not os.path.exists(cachefolder):
		os.makedirs(cachefolder)
	else:
		for f in os.listdir(cachefolder):
			fp = os.path.join(cachefolder, f)
			try:
				shutil.rmtree(fp)
			except OSError:
				os.remove(fp)
	program_name=None			
	files_list=sq_tools.ret_xci_offsets(filepath)
	files=list();filesizes=list()
	fplist=list()
	for k in range(len(files_list)):
		entry=files_list[k]
		fplist.append(entry[0])
	if target_cnmt==None:	
		for i in range(len(files_list)):			
			entry=files_list[i]
			cnmtfile=entry[0]	
			if cnmtfile.endswith('.cnmt.nca'):
				target_cnmt=cnmtfile
				break
	for i in range(len(files_list)):
		entry=files_list[i]
		cnmtfile=entry[0]
		if cnmtfile.endswith('.cnmt.nca') and target_cnmt==cnmtfile:
			f=squirrelXCI(filepath)
			titleid,titleversion,base_ID,keygeneration,rightsId,RSV,RGV,ctype,metasdkversion,exesdkversion,hasHtmlManual,Installedsize,DeltaSize,ncadata=f.get_data_from_cnmt(cnmtfile)
			f.flush()
			f.close()
			for j in range(len(ncadata)):
				row=ncadata[j]
				# print(row)
				if row['NCAtype']!='Meta' and row['NCAtype']!='Program':
					test1=str(row['NcaId'])+'.nca';test2=str(row['NcaId'])+'.ncz'
					if test1 in fplist or test2 in fplist:
						# print(str(row['NcaId'])+'.nca')
						if test1 in fplist:
							files.append(str(row['NcaId'])+'.nca')
							filesizes.append(int(row['Size']))
						elif test2 in fplist:	
							files.append(str(row['NcaId'])+'.ncz')
							for k in range(len(files_list)):
								entry=files_list[k]
								if entry[0]==test2:				
									filesizes.append(int(entry[3]))	
									break
			for j in range(len(ncadata)):
				row=ncadata[j]						
				if row['NCAtype']=='Meta':
					# print(str(row['NcaId'])+'.cnmt.nca')
					files.append(str(row['NcaId'])+'.cnmt.nca')
					filesizes.append(int(row['Size']))	
			for j in range(len(ncadata)):
				row=ncadata[j]
				# print(row)
				if row['NCAtype']=='Program':
					test1=str(row['NcaId'])+'.nca';test2=str(row['NcaId'])+'.ncz'
					if test1 in fplist or test2 in fplist:
						# print(str(row['NcaId'])+'.nca')
						if test1 in fplist:
							files.append(str(row['NcaId'])+'.nca')
							filesizes.append(int(row['Size']))
						elif test2 in fplist:	
							files.append(str(row['NcaId'])+'.ncz')
							for k in range(len(files_list)):
								entry=files_list[k]
								if entry[0]==test2:				
									filesizes.append(int(entry[3]))	
									break
			break			
	f.flush()
	f.close()						
	outheader = sq_tools.gen_nsp_header(files,filesizes)	
	properheadsize=len(outheader)
	# print(properheadsize)
	# print(bucketsize)
	i=0;sum=properheadsize;
	for file in files:
		# print(file)
		# print(filesizes[i])
		if i<(len(files)-1):
			sum+=filesizes[i]
		i+=1
	# print(sum)
	# print(sum/bucketsize)
	multiplier=math.ceil(sum/bucketsize)
	# print(multiplier)
	remainder = bucketsize*multiplier - sum
	# print(bucketsize*multiplier)
	xci=squirrelXCI(filepath)
	outfile=os.path.join(cachefolder, "0")
	written=0;
	outf = open(outfile, 'w+b')		
	outf.write(outheader)	
	written+=len(outheader)
	movoffset=0
	for fi in files:
		for nspF in xci.hfs0:
			if str(nspF._path)=="secure":
				for nca in nspF:	
					if nca._path==fi:
						nca=Nca(nca)
						crypto1=nca.header.getCryptoType()
						crypto2=nca.header.getCryptoType2()	
						if crypto2>crypto1:
							masterKeyRev=crypto2
						if crypto2<=crypto1:	
							masterKeyRev=crypto1						
						crypto = aes128.AESECB(Keys.keyAreaKey(Keys.getMasterKeyIndex(masterKeyRev), nca.header.keyIndex))
						hcrypto = aes128.AESXTS(uhx(Keys.get('header_key')))	
						gc_flag='00'*0x01					
						crypto1=nca.header.getCryptoType()
						crypto2=nca.header.getCryptoType2()					
						if nca.header.getRightsId() != 0:				
							nca.rewind()	
							if crypto2>crypto1:
								masterKeyRev=crypto2
							if crypto2<=crypto1:	
								masterKeyRev=crypto1	
							from mtp_tools import get_nca_ticket
								check,titleKey=get_nca_ticket(filepath,fi)
								if check==False:
									sys.exit("Can't verify titleckey")						
							titleKeyDec = Keys.decryptTitleKey(titleKey, Keys.getMasterKeyIndex(int(masterKeyRev)))							
							encKeyBlock = crypto.encrypt(titleKeyDec * 4)
							if str(keypatch) != "False":
								t = tqdm(total=False, unit='B', unit_scale=False, leave=False)								
								if keypatch < nca.header.getCryptoType2():
									encKeyBlock,crypto1,crypto2=squirrelXCI.get_new_cryptoblock(squirrelXCI,nca, keypatch,encKeyBlock,t)	
								t.close()
						if nca.header.getRightsId() == 0:
							nca.rewind()				
							encKeyBlock = nca.header.getKeyBlock()	
							if str(keypatch) != "False":
								t = tqdm(total=False, unit='B', unit_scale=False, leave=False)									
								if keypatch < nca.header.getCryptoType2():
									encKeyBlock,crypto1,crypto2=squirrelXCI.get_new_cryptoblock(squirrelXCI,nca,keypatch,encKeyBlock,t)					
								t.close()									
						nca.rewind()					
						i=0				
						newheader=xci.get_newheader(nca,encKeyBlock,crypto1,crypto2,hcrypto,gc_flag)		
						outf.write(newheader)
						written+=len(newheader)
						nca.seek(0xC00)	
						movoffset+=0xC00
						if (str(nca.header.contentType) != 'Content.PROGRAM'):
							data=nca.read()	
							nca.close()
							outf.write(data)
							written+=len(data)
						break					
					else:pass
コード例 #9
0
def gen_mxci_parts(input_files, cachefolder=None, keypatch=False):
    from listmanager import calculate_name
    if keypatch != False:
        try:
            keypatch = int(keypatch)
        except:
            keypatch = False
    if cachefolder == None:
        cachefolder = os.path.join(ztools_dir, '_mtp_cache_')
    if not os.path.exists(cachefolder):
        os.makedirs(cachefolder)
    else:
        for f in os.listdir(cachefolder):
            fp = os.path.join(cachefolder, f)
            try:
                shutil.rmtree(fp)
            except OSError:
                os.remove(fp)
    end_name, prlist = calculate_name(input_files, romanize=True, ext='.xci')
    print(f"Calculated name {end_name}")
    outheader, properheadsize, sz, files = gen_multi_file_header(
        prlist, input_files)
    properheadsize = len(outheader)
    outfile = os.path.join(cachefolder, "0")
    outf = open(outfile, 'w+b')
    outf.write(outheader)
    # print(properheadsize)
    # print(bucketsize)
    i = 0
    sum = properheadsize
    for fi in files:
        for filepath in input_files:
            if filepath.endswith('xci'):
                xci = squirrelXCI(filepath)
                written = 0
                for nspF in xci.hfs0:
                    if str(nspF._path) == "secure":
                        for nca in nspF:
                            if nca._path == fi:
                                nca = Nca(nca)
                                crypto1 = nca.header.getCryptoType()
                                crypto2 = nca.header.getCryptoType2()
                                if crypto2 > crypto1:
                                    masterKeyRev = crypto2
                                if crypto2 <= crypto1:
                                    masterKeyRev = crypto1
                                crypto = aes128.AESECB(
                                    Keys.keyAreaKey(
                                        Keys.getMasterKeyIndex(masterKeyRev),
                                        nca.header.keyIndex))
                                hcrypto = aes128.AESXTS(
                                    uhx(Keys.get('header_key')))
                                gc_flag = '00' * 0x01
                                crypto1 = nca.header.getCryptoType()
                                crypto2 = nca.header.getCryptoType2()
                                if nca.header.getRightsId() != 0:
                                    nca.rewind()
                                    if crypto2 > crypto1:
                                        masterKeyRev = crypto2
                                    if crypto2 <= crypto1:
                                        masterKeyRev = crypto1
                                    from mtp_tools import get_nca_ticket
                                    check, titleKey = get_nca_ticket(
                                        filepath, fi)
                                    if check == False:
                                        sys.exit("Can't verify titleckey")
                                    titleKeyDec = Keys.decryptTitleKey(
                                        titleKey,
                                        Keys.getMasterKeyIndex(
                                            int(masterKeyRev)))
                                    encKeyBlock = crypto.encrypt(titleKeyDec *
                                                                 4)
                                    if str(keypatch) != "False":
                                        t = tqdm(total=False,
                                                 unit='B',
                                                 unit_scale=False,
                                                 leave=False)
                                        if keypatch < nca.header.getCryptoType2(
                                        ):
                                            encKeyBlock, crypto1, crypto2 = squirrelXCI.get_new_cryptoblock(
                                                squirrelXCI, nca, keypatch,
                                                encKeyBlock, t)
                                        t.close()
                                if nca.header.getRightsId() == 0:
                                    nca.rewind()
                                    encKeyBlock = nca.header.getKeyBlock()
                                    if str(keypatch) != "False":
                                        t = tqdm(total=False,
                                                 unit='B',
                                                 unit_scale=False,
                                                 leave=False)
                                        if keypatch < nca.header.getCryptoType2(
                                        ):
                                            encKeyBlock, crypto1, crypto2 = squirrelXCI.get_new_cryptoblock(
                                                squirrelXCI, nca, keypatch,
                                                encKeyBlock, t)
                                        t.close()
                                nca.rewind()
                                i = 0
                                newheader = xci.get_newheader(
                                    nca, encKeyBlock, crypto1, crypto2,
                                    hcrypto, gc_flag)
                                outf.write(newheader)
                                written += len(newheader)
                                nca.seek(0xC00)
                                break
                            else:
                                pass
                xci.flush()
                xci.close()
            elif filepath.endswith('nsp'):
                nsp = squirrelNSP(filepath)
                written = 0
                for nca in nsp:
                    if nca._path == fi:
                        nca = Nca(nca)
                        crypto1 = nca.header.getCryptoType()
                        crypto2 = nca.header.getCryptoType2()
                        if crypto2 > crypto1:
                            masterKeyRev = crypto2
                        if crypto2 <= crypto1:
                            masterKeyRev = crypto1
                        crypto = aes128.AESECB(
                            Keys.keyAreaKey(
                                Keys.getMasterKeyIndex(masterKeyRev),
                                nca.header.keyIndex))
                        hcrypto = aes128.AESXTS(uhx(Keys.get('header_key')))
                        gc_flag = '00' * 0x01
                        crypto1 = nca.header.getCryptoType()
                        crypto2 = nca.header.getCryptoType2()
                        if nca.header.getRightsId() != 0:
                            nca.rewind()
                            if crypto2 > crypto1:
                                masterKeyRev = crypto2
                            if crypto2 <= crypto1:
                                masterKeyRev = crypto1
                            from mtp_tools import get_nca_ticket
                            check, titleKey = get_nca_ticket(filepath, fi)
                            if check == False:
                                sys.exit("Can't verify titleckey")
                            titleKeyDec = Keys.decryptTitleKey(
                                titleKey,
                                Keys.getMasterKeyIndex(int(masterKeyRev)))
                            encKeyBlock = crypto.encrypt(titleKeyDec * 4)
                            if str(keypatch) != "False":
                                t = tqdm(total=False,
                                         unit='B',
                                         unit_scale=False,
                                         leave=False)
                                if keypatch < nca.header.getCryptoType2():
                                    encKeyBlock, crypto1, crypto2 = squirrelNSP.get_new_cryptoblock(
                                        squirrelNSP, nca, keypatch,
                                        encKeyBlock, t)
                                t.close()
                        if nca.header.getRightsId() == 0:
                            nca.rewind()
                            encKeyBlock = nca.header.getKeyBlock()
                            if str(keypatch) != "False":
                                t = tqdm(total=False,
                                         unit='B',
                                         unit_scale=False,
                                         leave=False)
                                if keypatch < nca.header.getCryptoType2():
                                    encKeyBlock, crypto1, crypto2 = squirrelNSP.get_new_cryptoblock(
                                        squirrelNSP, nca, keypatch,
                                        encKeyBlock, t)
                                t.close()
                        nca.rewind()
                        i = 0
                        newheader = nsp.get_newheader(nca, encKeyBlock,
                                                      crypto1, crypto2,
                                                      hcrypto, gc_flag)
                        outf.write(newheader)
                        written += len(newheader)
                        nca.seek(0xC00)
                        break
                    else:
                        pass
                nsp.flush()
                nsp.close()
    outf.flush()
    outf.close()
    tfile = os.path.join(cachefolder, "files.csv")
    with open(tfile, 'w') as csvfile:
        csvfile.write("{}|{}|{}|{}|{}|{}\n".format("step", "filepath", "size",
                                                   "targetsize", "off1",
                                                   "off2"))
        csvfile.write("{}|{}|{}|{}|{}|{}\n".format(0, outfile,
                                                   properheadsize + written,
                                                   properheadsize, 0,
                                                   properheadsize))
        k = 0
        l = 0
        for fi in files:
            for filepath in input_files:
                if filepath.endswith('xci'):
                    files_list = sq_tools.ret_xci_offsets(filepath)
                elif filepath.endswith('nsp'):
                    files_list = sq_tools.ret_nsp_offsets(filepath)
                for j in files_list:
                    if j[0] == fi:
                        csvfile.write("{}|{}|{}|{}|{}|{}\n".format(
                            k + 1, outfile, properheadsize + written, 0xC00,
                            (properheadsize + l * 0xC00),
                            (properheadsize + (l * 0xC00) + 0xC00)))
                        off1 = j[1] + 0xC00
                        off2 = j[2]
                        targetsize = j[3] - 0xC00
                        csvfile.write("{}|{}|{}|{}|{}|{}\n".format(
                            k + 2, filepath, (os.path.getsize(filepath)),
                            targetsize, off1, off2))
                        break
            k += 2
            l += 1
    return end_name
コード例 #10
0
def supertrim_xci(filepath,
                  buffer=65536,
                  outfile=None,
                  keepupd=False,
                  level=17,
                  threads=0):
    try:
        exchangefile.deletefile()
    except:
        pass
    f = squirrelXCI(filepath)
    for nspF in f.hfs0:
        if str(nspF._path) == "secure":
            for ticket in nspF:
                if str(ticket._path).endswith('.tik'):
                    print('- Titlerights: ' + ticket.rightsId)
                    tk = (str(hex(ticket.getTitleKeyBlock()))[2:]).upper()
                    print('- Titlekey: ' + tk)
                    exchangefile.add(ticket.rightsId, tk)
    f.flush()
    f.close()
    files_list = sq_tools.ret_xci_offsets(filepath)
    files = list()
    filesizes = list()
    fplist = list()
    for k in range(len(files_list)):
        entry = files_list[k]
        fplist.append(entry[0])
    for i in range(len(files_list)):
        entry = files_list[i]
        cnmtfile = entry[0]
        if cnmtfile.endswith('.cnmt.nca'):
            f = squirrelXCI(filepath)
            titleid, titleversion, base_ID, keygeneration, rightsId, RSV, RGV, ctype, metasdkversion, exesdkversion, hasHtmlManual, Installedsize, DeltaSize, ncadata = f.get_data_from_cnmt(
                cnmtfile)
            f.flush()
            f.close()
            for j in range(len(ncadata)):
                row = ncadata[j]
                # print(row)
                if row['NCAtype'] != 'Meta':
                    test1 = str(row['NcaId']) + '.nca'
                    test2 = str(row['NcaId']) + '.ncz'
                    if test1 in fplist or test2 in fplist:
                        # print(str(row['NcaId'])+'.nca')
                        files.append(str(row['NcaId']) + '.nca')
                        filesizes.append(int(row['Size']))
                else:
                    # print(str(row['NcaId'])+'.cnmt.nca')
                    files.append(str(row['NcaId']) + '.cnmt.nca')
                    filesizes.append(int(row['Size']))
            for k in range(len(files_list)):
                entry = files_list[k]
                fp = entry[0]
                sz = int(entry[3])
                if fp.endswith('xml'):
                    files.append(fp)
                    filesizes.append(sz)
            for k in range(len(files_list)):
                entry = files_list[k]
                fp = entry[0]
                sz = int(entry[3])
                if fp.endswith('.tik'):
                    files.append(fp)
                    filesizes.append(sz)
            for k in range(len(files_list)):
                entry = files_list[k]
                fp = entry[0]
                sz = int(entry[3])
                if fp.endswith('.cert'):
                    files.append(fp)
                    filesizes.append(sz)
    sec_hashlist = list()
    f = squirrelXCI(filepath)
    try:
        for file in files:
            sha, size, gamecard = f.file_hash(file)
            # print(sha)
            if sha != False:
                sec_hashlist.append(sha)
    except BaseException as e:
        Print.error('Exception: ' + str(e))
    f.flush()
    f.close()
    xci_header, game_info, sig_padding, xci_certificate, root_header, upd_header, norm_header, sec_header, rootSize, upd_multiplier, norm_multiplier, sec_multiplier = sq_tools.get_xciheader(
        files, filesizes, sec_hashlist)
    compressionLevel = int(level)
    CHUNK_SZ = buffer
    if outfile == None:
        ofile = filepath[0:-1] + 'z'
    else:
        ofile = outfile
    ofile = os.path.abspath(ofile)
    outheader = xci_header
    outheader += game_info
    outheader += sig_padding
    outheader += xci_certificate
    outheader += root_header
    outheader += upd_header
    outheader += norm_header
    outheader += sec_header
    properheadsize = len(outheader)

    compressionLevel = int(level)
    CHUNK_SZ = buffer
    if outfile == None:
        nszPath = filepath[0:-1] + 'z'
    else:
        nszPath = outfile
    nszPath = os.path.abspath(nszPath)

    Print.info('Compressing with level %d and %d threads' %
               (compressionLevel, threads))
    Print.info('\n  %s -> %s \n' % (filepath, nszPath))

    newNsp = nutFs.Pfs0.Pfs0Stream(nszPath,
                                   headsize=properheadsize,
                                   mode='wb+')

    xcicontainer = Xci(filepath)
    # f.compressed_supertrim(buffer,outfile,keepupd,level,threads)
    files2 = list()
    filesizes2 = list()
    for file in files:
        for nspF in xcicontainer.hfs0:
            if str(nspF._path) == "secure":
                for nca in nspF:
                    if nca._path == file:
                        if isinstance(nca, nutFs.Nca.Nca) and (
                                nca.header.contentType
                                == nutFs.Type.Content.PROGRAM
                                or nca.header.contentType
                                == nutFs.Type.Content.PUBLIC_DATA):
                            if isNcaPacked(nca):
                                cctx = zstandard.ZstdCompressor(
                                    level=compressionLevel, threads=threads)

                                newFileName = nca._path[0:-1] + 'z'

                                f = newNsp.add(newFileName, nca.size)

                                start = f.tell()

                                nca.seek(0)
                                data = nca.read(ncaHeaderSize)
                                f.write(data)
                                nca.seek(ncaHeaderSize)

                                written = ncaHeaderSize

                                compressor = cctx.stream_writer(f)

                                sections = []
                                for fs in sortedFs(nca):
                                    sections += fs.getEncryptionSections()

                                header = b'NCZSECTN'
                                header += len(sections).to_bytes(8, 'little')

                                i = 0
                                for fs in sections:
                                    i += 1
                                    header += fs.offset.to_bytes(8, 'little')
                                    header += fs.size.to_bytes(8, 'little')
                                    header += fs.cryptoType.to_bytes(
                                        8, 'little')
                                    header += b'\x00' * 8
                                    header += fs.cryptoKey
                                    header += fs.cryptoCounter

                                f.write(header)
                                written += len(header)
                                timestamp = time.time()
                                decompressedBytes = ncaHeaderSize
                                totsize = 0
                                for fs in sortedFs(nca):
                                    totsize += fs.size
                                t = tqdm(total=totsize,
                                         unit='B',
                                         unit_scale=True,
                                         leave=False)

                                for section in sections:
                                    #print('offset: %x\t\tsize: %x\t\ttype: %d\t\tiv%s' % (section.offset, section.size, section.cryptoType, str(hx(section.cryptoCounter))))
                                    o = nca.partition(
                                        offset=section.offset,
                                        size=section.size,
                                        n=None,
                                        cryptoType=section.cryptoType,
                                        cryptoKey=section.cryptoKey,
                                        cryptoCounter=bytearray(
                                            section.cryptoCounter),
                                        autoOpen=True)

                                    while not o.eof():
                                        buffer = o.read(CHUNK_SZ)
                                        t.update(len(buffer))
                                        if len(buffer) == 0:
                                            raise IOError('read failed')

                                        written += compressor.write(buffer)

                                        decompressedBytes += len(buffer)
                                t.close()
                                compressor.flush(zstandard.FLUSH_FRAME)

                                elapsed = time.time() - timestamp
                                minutes = elapsed / 60
                                seconds = elapsed % 60

                                speed = 0 if elapsed == 0 else (nca.size /
                                                                elapsed)

                                written = f.tell() - start
                                print(
                                    '\n  * Compressed at %d%% from %s to %s  - %s'
                                    %
                                    (int(written * 100 / nca.size),
                                     str(sq_tools.getSize(decompressedBytes)),
                                     str(sq_tools.getSize(written)),
                                     nca._path))
                                print(
                                    '  * Compressed in %02d:%02d at speed: %.1f MB/s\n'
                                    % (minutes, seconds, speed / 1000000.0))
                                newNsp.resize(newFileName, written)
                                files2.append(newFileName)
                                filesizes2.append(written)
                                continue
                            else:
                                print('not packed!')

                        f = newNsp.add(nca._path, nca.size)
                        files2.append(nca._path)
                        filesizes2.append(nca.size)
                        nca.seek(0)
                        t = tqdm(total=nca.size,
                                 unit='B',
                                 unit_scale=True,
                                 leave=False)
                        while not nca.eof():
                            buffer = nca.read(CHUNK_SZ)
                            t.update(len(buffer))
                            f.write(buffer)
                        t.close()
    newNsp.close()

    xci_header, game_info, sig_padding, xci_certificate, root_header, upd_header, norm_header, sec_header, rootSize, upd_multiplier, norm_multiplier, sec_multiplier = sq_tools.get_xciheader(
        files2, filesizes2, sec_hashlist)
    outheader = xci_header
    outheader += game_info
    outheader += sig_padding
    outheader += xci_certificate
    outheader += root_header
    outheader += upd_header
    outheader += norm_header
    outheader += sec_header
    with open(nszPath, 'rb+') as o:
        o.seek(0)
        o.write(outheader)
    try:
        exchangefile.deletefile()
    except:
        pass
    return nszPath