private static void GetCommonContentMetaInfo(ref NintendoSubmissionPackageFileSystemInfo.EntryInfo entry, string contentMetaFileName, ContentMetaModel model, NintendoSubmissionPackageReader nspReader, KeyConfiguration keyConfig)
        {
            IFileSystemArchiveReader systemArchiveReader = nspReader.OpenNintendoContentArchiveReader(contentMetaFileName, keyConfig.GetKeyAreaEncryptionKeys()).OpenFileSystemArchiveReader(0);
            Tuple <string, long>     tuple = systemArchiveReader.ListFileInfo().Single <Tuple <string, long> >();

            entry.ContentMetaInfo = new NintendoSubmissionPackageFileSystemInfo.ContentMetaInfo(systemArchiveReader.ReadFile(tuple.Item1, 0L, tuple.Item2), model);
            NintendoContentMetaReader contentMetaReader = new NintendoContentMetaReader(entry.ContentMetaInfo.Data);

            entry.MetaType = contentMetaReader.GetType();
        }
        private static void CalculatePartitionSize(out long partitionHeaderSize, out long partitionSize, NintendoSubmissionPackageFileSystemInfo nspInfo, NintendoSubmissionPackageFileSystemInfo patchInfo, bool isNormal, KeyConfiguration keyConfig)
        {
            long          num           = 0;
            List <string> entryNameList = new List <string>();

            for (int index1 = 0; index1 < nspInfo.Entries.Count; ++index1)
            {
                List <Tuple <ISource, NintendoContentInfo> >      contentSourceList = new List <Tuple <ISource, NintendoContentInfo> >();
                NintendoSubmissionPackageFileSystemInfo.EntryInfo entry             = nspInfo.Entries[index1];
                for (int index2 = 0; index2 < entry.Contents.Count; ++index2)
                {
                    NintendoSubmissionPackageFileSystemInfo.ContentInfo content = entry.Contents[index2];
                    if ((!isNormal || !(content.ContentType != "Control")) && !(content.ContentType == "Meta"))
                    {
                        ISource source;
                        if (content.FsInfo != null)
                        {
                            source = (ISource) new NintendoContentArchiveSource(content.FsInfo as NintendoContentFileSystemInfo, keyConfig, false);
                        }
                        else
                        {
                            if (content.Source == null)
                            {
                                throw new NotImplementedException();
                            }
                            source = content.Source;
                        }
                        contentSourceList.Add(Tuple.Create <ISource, NintendoContentInfo>(source, new NintendoContentInfo(content.ContentType, source.Size)));
                        entryNameList.Add("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.nca");
                        num += ProdEncryptedXciArchive.RoundupPageSize(source.Size);
                    }
                }
                entryNameList.Add("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cnmt.nca");
                NintendoContentMetaArchiveSource metaArchiveSource = new NintendoContentMetaArchiveSource(entry.ContentMetaInfo == null ? new NintendoContentMetaBase(contentSourceList, entry.MetaType, entry.MetaFilePath) : new NintendoContentMetaBase(contentSourceList, entry.ContentMetaInfo.Data, entry.ContentMetaInfo.Model, true), entry.KeyIndex, keyConfig, nspInfo.IsProdEncryption, true);
                num += ProdEncryptedXciArchive.RoundupPageSize(metaArchiveSource.Size);
            }
            partitionHeaderSize = HashNameEntryPartitionFsHeaderSource <Sha256PartitionFileSystemMeta> .GetDummySize(entryNameList);

            ProdEncryptedXciArchive.CheckAlignmentPageSize(partitionHeaderSize);
            partitionSize = num;
        }
        public NintendoSubmissionPackageFileSystemInfo GetFileSystemInfo()
        {
            NintendoSubmissionPackageFileSystemInfo packageFileSystemInfo = new NintendoSubmissionPackageFileSystemInfo();

            using (StreamReader streamReader = new StreamReader(this.m_adfPath, Encoding.UTF8))
            {
                YamlStream yamlStream = new YamlStream();
                yamlStream.Load((TextReader)streamReader);
                YamlSequenceNode child1;
                try
                {
                    YamlMappingNode rootNode = (YamlMappingNode)yamlStream.Documents[0].RootNode;
                    if (((YamlScalarNode)rootNode.Children[(YamlNode) new YamlScalarNode("formatType")]).Value != "NintendoSubmissionPackage")
                    {
                        throw new ArgumentException();
                    }
                    YamlScalarNode child2 = (YamlScalarNode)rootNode.Children[(YamlNode) new YamlScalarNode("version")];
                    child1 = (YamlSequenceNode)rootNode.Children[(YamlNode) new YamlScalarNode("entries")];
                    packageFileSystemInfo.Version = Convert.ToByte(child2.Value);
                }
                catch
                {
                    throw new ArgumentException("invalid format .adf file.");
                }
                foreach (YamlMappingNode yamlMappingNode1 in child1)
                {
                    NintendoSubmissionPackageFileSystemInfo.EntryInfo entryInfo = new NintendoSubmissionPackageFileSystemInfo.EntryInfo();
                    entryInfo.Contents = new List <NintendoSubmissionPackageFileSystemInfo.ContentInfo>();
                    YamlNode child2 = yamlMappingNode1.Children[(YamlNode) new YamlScalarNode("contents")];
                    if (child2 is YamlSequenceNode)
                    {
                        foreach (YamlMappingNode yamlMappingNode2 in (YamlSequenceNode)child2)
                        {
                            NintendoSubmissionPackageFileSystemInfo.ContentInfo contentInfo = new NintendoSubmissionPackageFileSystemInfo.ContentInfo();
                            string empty1 = string.Empty;
                            string empty2 = string.Empty;
                            foreach (KeyValuePair <YamlNode, YamlNode> keyValuePair in yamlMappingNode2)
                            {
                                string str = ((YamlScalarNode)keyValuePair.Key).Value;
                                if (!(str == "type"))
                                {
                                    if (!(str == "contentType"))
                                    {
                                        if (!(str == "path"))
                                        {
                                            throw new ArgumentException("invalid format .adf file. invalid key is specified\n" + yamlMappingNode2.ToString());
                                        }
                                        empty2 = ((YamlScalarNode)keyValuePair.Value).Value;
                                    }
                                    else
                                    {
                                        contentInfo.ContentType = ((YamlScalarNode)keyValuePair.Value).Value;
                                    }
                                }
                                else
                                {
                                    empty1 = ((YamlScalarNode)keyValuePair.Value).Value;
                                }
                            }
                            if (empty2 == null)
                            {
                                throw new ArgumentException("invalid format .adf file. \"path\" is not specified\n" + yamlMappingNode2.ToString());
                            }
                            if (empty1 == "format")
                            {
                                NintendoContentAdfReader contentAdfReader = new NintendoContentAdfReader(empty2);
                                contentInfo.FsInfo = (Nintendo.Authoring.FileSystemMetaLibrary.FileSystemInfo)contentAdfReader.GetFileSystemInfo();
                            }
                            else
                            {
                                if (!(empty1 == "file"))
                                {
                                    throw new ArgumentException("invalid format .adf file. unknown \"type\" is specified\n" + yamlMappingNode2.ToString());
                                }
                                FileInfo fileInfo = new FileInfo(empty2);
                                contentInfo.Source = (ISource) new FileSource(empty2, 0L, fileInfo.Length);
                            }
                            entryInfo.Contents.Add(contentInfo);
                        }
                    }
                    List <Tuple <string, string> > iconList   = new List <Tuple <string, string> >();
                    List <Tuple <string, string> > nxIconList = new List <Tuple <string, string> >();
                    uint maxNxIconSize = 102400;
                    foreach (KeyValuePair <YamlNode, YamlNode> keyValuePair in yamlMappingNode1)
                    {
                        switch (((YamlScalarNode)keyValuePair.Key).Value)
                        {
                        case "contents":
                            continue;

                        case "icon":
                            using (IEnumerator <YamlNode> enumerator = ((YamlSequenceNode)keyValuePair.Value).GetEnumerator())
                            {
                                while (enumerator.MoveNext())
                                {
                                    YamlMappingNode current = (YamlMappingNode)enumerator.Current;
                                    string          str1    = ((YamlScalarNode)current.Children[(YamlNode) new YamlScalarNode("language")]).Value;
                                    string          str2    = ((YamlScalarNode)current.Children[(YamlNode) new YamlScalarNode("path")]).Value;
                                    iconList.Add(new Tuple <string, string>(str1, str2));
                                }
                                continue;
                            }

                        case "keyIndex":
                            entryInfo.KeyIndex = int.Parse(((YamlScalarNode)keyValuePair.Value).Value);
                            continue;

                        case "metaFilePath":
                            entryInfo.MetaFilePath = ((YamlScalarNode)keyValuePair.Value).Value;
                            continue;

                        case "metaType":
                            entryInfo.MetaType = ((YamlScalarNode)keyValuePair.Value).Value;
                            continue;

                        case "nxIcon":
                            using (IEnumerator <YamlNode> enumerator = ((YamlSequenceNode)keyValuePair.Value).GetEnumerator())
                            {
                                while (enumerator.MoveNext())
                                {
                                    YamlMappingNode current = (YamlMappingNode)enumerator.Current;
                                    string          str1    = ((YamlScalarNode)current.Children[(YamlNode) new YamlScalarNode("language")]).Value;
                                    string          str2    = ((YamlScalarNode)current.Children[(YamlNode) new YamlScalarNode("path")]).Value;
                                    nxIconList.Add(new Tuple <string, string>(str1, str2));
                                }
                                continue;
                            }

                        case "nxIconMaxSize":
                            maxNxIconSize = uint.Parse(((YamlScalarNode)keyValuePair.Value).Value);
                            continue;

                        default:
                            throw new ArgumentException("invalid format .adf file. invalid key is specified\n" + yamlMappingNode1.ToString());
                        }
                    }
                    if (entryInfo.MetaFilePath == null)
                    {
                        throw new ArgumentException();
                    }
                    Tuple <int, int> cardSpec = new DotMetaReader(entryInfo.MetaFilePath).GetCardSpec();
                    if (cardSpec != null)
                    {
                        packageFileSystemInfo.CardSize      = cardSpec.Item1;
                        packageFileSystemInfo.CardClockRate = cardSpec.Item2;
                    }
                    else
                    {
                        Log.Info("\"CardSpec\" is not specified by .meta file. Size and ClockRate will be calculated automatically.");
                    }
                    entryInfo.ExtraData = this.CreateExtraSource(iconList, nxIconList, maxNxIconSize);
                    packageFileSystemInfo.Entries.Add(entryInfo);
                }
            }
            return(packageFileSystemInfo);
        }
Exemplo n.º 4
0
        public NintendoSubmissionPackageArchive(IReadableSink outSink, NintendoSubmissionPackageFileSystemInfo fileSystemInfo, KeyConfiguration config)
        {
            this.ConnectionList = new List <Connection>();
            PartitionFileSystemMeta partitionFileSystemMeta = new PartitionFileSystemMeta();
            List <string>           entryNameList           = new List <string>();

            for (int index1 = 0; index1 < fileSystemInfo.Entries.Count; ++index1)
            {
                for (int index2 = 0; index2 < fileSystemInfo.Entries[index1].Contents.Count; ++index2)
                {
                    entryNameList.Add("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.nca");
                }
                entryNameList.Add("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cnmt.nca");
                entryNameList.Add("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cnmt.xml");
                if (NintendoSubmissionPackageArchiveUtils.HasProgramContent(fileSystemInfo.Entries[index1].Contents))
                {
                    entryNameList.Add("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.programinfo.xml");
                }
                if (this.CheckNeedTicket(fileSystemInfo.Entries[index1]))
                {
                    entryNameList.Add("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.tik");
                    entryNameList.Add("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cert");
                }
                if (fileSystemInfo.EnableContentMetaBinaryExport)
                {
                    entryNameList.Add("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cnmt");
                }
                foreach (NintendoSubmissionPackageExtraData packageExtraData in fileSystemInfo.Entries[index1].ExtraData)
                {
                    entryNameList.Add(packageExtraData.EntryName);
                }
                if (NintendoSubmissionPackageArchiveUtils.HasControlContent(fileSystemInfo.Entries[index1].Contents))
                {
                    entryNameList.Add("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.nacp.xml");
                }
                if (fileSystemInfo.Entries[index1].MetaType == "Application")
                {
                    entryNameList.Add("cardspec.xml");
                }
            }
            long dummySize = HashNameEntryPartitionFsHeaderSource <PartitionFileSystemMeta> .GetDummySize(entryNameList);

            long num1 = dummySize;
            List <ContentHashSource> hashSources        = new List <ContentHashSource>();
            List <ProgramInfoXml>    programInfoXmlList = new List <ProgramInfoXml>();
            PartitionFileSystemInfo  rootFsInfo         = new PartitionFileSystemInfo();

            rootFsInfo.version = (int)fileSystemInfo.Version;
            for (int index1 = 0; index1 < fileSystemInfo.Entries.Count; ++index1)
            {
                NintendoSubmissionPackageFileSystemInfo.EntryInfo entry             = fileSystemInfo.Entries[index1];
                List <Tuple <ISource, NintendoContentInfo> >      contentSourceList = new List <Tuple <ISource, NintendoContentInfo> >();
                for (int index2 = 0; index2 < entry.Contents.Count; ++index2)
                {
                    NintendoSubmissionPackageFileSystemInfo.ContentInfo content = entry.Contents[index2];
                    ISource source;
                    if (content.FsInfo != null)
                    {
                        source = (ISource) new NintendoContentArchiveSource(content.FsInfo as NintendoContentFileSystemInfo, config, this.CheckNeedLogoPartition(entry));
                    }
                    else
                    {
                        if (content.Source == null)
                        {
                            throw new NotImplementedException();
                        }
                        source = content.Source;
                    }
                    PartitionFileSystemInfo.EntryInfo entryInfo = PartitionFileSystemInfo.EntryInfo.Make("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.nca", (ulong)source.Size, (ulong)(num1 - dummySize));
                    rootFsInfo.entries.Add(entryInfo);
                    IReadableSink outReadableSubSink;
                    this.AddConnection(out outReadableSubSink, source, outSink, num1);
                    SinkLinkedSource sinkLinkedSource = new SinkLinkedSource((ISink)outReadableSubSink, (ISource) new Sha256StreamHashSource(outReadableSubSink.ToSource()));
                    hashSources.Add(new ContentHashSource((ISource)sinkLinkedSource, ".nca", content.ContentType));
                    num1 += source.Size;
                    contentSourceList.Add(Tuple.Create <ISource, NintendoContentInfo>(hashSources[hashSources.Count - 1].Source, new NintendoContentInfo(content.ContentType, source.Size)));
                }
                NintendoContentMetaBase           nintendoContentMetaBase = entry.ContentMetaInfo == null ? new NintendoContentMetaBase(contentSourceList, entry.MetaType, entry.MetaFilePath) : new NintendoContentMetaBase(contentSourceList, entry.ContentMetaInfo.Data, entry.ContentMetaInfo.Model, fileSystemInfo.IsProdEncryption);
                NintendoContentMetaArchiveSource  metaArchiveSource       = new NintendoContentMetaArchiveSource(nintendoContentMetaBase, entry.KeyIndex, config, fileSystemInfo.IsProdEncryption, false);
                PartitionFileSystemInfo.EntryInfo entryInfo1 = PartitionFileSystemInfo.EntryInfo.Make("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cnmt.nca", (ulong)metaArchiveSource.Size, (ulong)(num1 - dummySize));
                rootFsInfo.entries.Add(entryInfo1);
                IReadableSink outReadableSubSink1;
                this.AddConnection(out outReadableSubSink1, (ISource)metaArchiveSource, outSink, num1);
                ISource source1 = (ISource) new SinkLinkedSource((ISink)outReadableSubSink1, (ISource) new Sha256StreamHashSource(outReadableSubSink1.ToSource()));
                hashSources.Add(new ContentHashSource(source1, ".cnmt.nca"));
                long offset1 = num1 + metaArchiveSource.Size;
                NintendoContentMetaXmlSource contentMetaXmlSource = new NintendoContentMetaXmlSource(nintendoContentMetaBase, source1, metaArchiveSource.Size);
                num1 = this.RegisterRootEntry(outSink, ref rootFsInfo, dummySize, ref hashSources, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cnmt.xml", (ISource)contentMetaXmlSource, offset1, new ContentHashSource(source1, ".cnmt.xml"));
                if (NintendoSubmissionPackageArchiveUtils.HasProgramContent(fileSystemInfo.Entries[index1].Contents))
                {
                    ISource source2 = (ISource) new ProgramInfoXmlSource(NintendoSubmissionPackageArchiveUtils.GetProgramInfoXml(fileSystemInfo.Entries[index1].Contents, config));
                    ISource source3 = hashSources.Where <ContentHashSource>((Func <ContentHashSource, bool>)(x => x.ContentType == "Program")).Select <ContentHashSource, ISource>((Func <ContentHashSource, ISource>)(x => x.Source)).Single <ISource>();
                    num1 = this.RegisterRootEntry(outSink, ref rootFsInfo, dummySize, ref hashSources, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.programinfo.xml", source2, num1, new ContentHashSource(source3, ".programinfo.xml"));
                }
                if (fileSystemInfo.EnableContentMetaBinaryExport)
                {
                    ISource source2 = nintendoContentMetaBase.GetSource();
                    num1 = this.RegisterRootEntry(outSink, ref rootFsInfo, dummySize, ref hashSources, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cnmt", source2, num1, new ContentHashSource(source1, ".cnmt"));
                }
                foreach (NintendoSubmissionPackageExtraData packageExtraData in entry.ExtraData)
                {
                    num1 = this.RegisterRootEntry(outSink, ref rootFsInfo, dummySize, ref hashSources, packageExtraData.EntryName, (ISource)packageExtraData, num1, new ContentHashSource(source1, ".jpg"));
                }
                if (NintendoSubmissionPackageArchiveUtils.HasControlContent(fileSystemInfo.Entries[index1].Contents))
                {
                    ApplicationControlPropertyXmlSource propertyXmlSource = new ApplicationControlPropertyXmlSource(NintendoSubmissionPackageArchiveUtils.GetApplicationControlProperty(fileSystemInfo.Entries[index1].Contents, config));
                    ISource source2 = hashSources.Where <ContentHashSource>((Func <ContentHashSource, bool>)(x => x.ContentType == "Control")).Select <ContentHashSource, ISource>((Func <ContentHashSource, ISource>)(x => x.Source)).Single <ISource>();
                    num1 = this.RegisterRootEntry(outSink, ref rootFsInfo, dummySize, ref hashSources, "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.nacp.xml", (ISource)propertyXmlSource, num1, new ContentHashSource(source2, ".nacp.xml"));
                }
                if (this.CheckNeedTicket(fileSystemInfo.Entries[index1]))
                {
                    ulong num2 = fileSystemInfo.Entries[index1].ContentMetaInfo != null?this.GetApplicationIdFromContentMetaRawData(fileSystemInfo.Entries[index1].ContentMetaInfo.Data) : new DotMetaReader(fileSystemInfo.Entries[index1].MetaFilePath).GetContentMetaId();

                    string rightsIdText = TicketUtility.CreateRightsIdText(num2);
                    Ticket ticket       = new Ticket();
                    ticket.PublishTicket(num2, fileSystemInfo.IsProdEncryption, config);
                    TicketSource            ticketSource      = new TicketSource(ticket.Data, ticket.Length);
                    long                    offset2           = this.RegisterRootEntry(outSink, ref rootFsInfo, dummySize, ref hashSources, rightsIdText + ".tik", (ISource)ticketSource, num1, new ContentHashSource((ISource)null, ".tik"));
                    TicketCertificateSource certificateSource = new TicketCertificateSource(fileSystemInfo.IsProdEncryption, config);
                    num1 = this.RegisterRootEntry(outSink, ref rootFsInfo, dummySize, ref hashSources, rightsIdText + ".cert", (ISource)certificateSource, offset2, new ContentHashSource((ISource)null, ".cert"));
                }
                if (entry.MetaType == "Application")
                {
                    ISource source2 = (ISource) new CardSpecXmlSource(fileSystemInfo, config);
                    num1 = this.RegisterRootEntry(outSink, ref rootFsInfo, dummySize, ref hashSources, "cardspec.xml", source2, num1, new ContentHashSource((ISource)null, ".xml"));
                }
            }
            ISource source4 = (ISource) new HashNameEntryPartitionFsHeaderSource <PartitionFileSystemMeta>(hashSources, rootFsInfo, dummySize);
            ISink   sink    = (ISink) new SubSink((ISink)outSink, 0L, source4.Size);

            this.ConnectionList.Add(new Connection(source4, sink));
            outSink.SetSize(num1);
        }
Exemplo n.º 5
0
 private bool CheckNeedLogoPartition(NintendoSubmissionPackageFileSystemInfo.EntryInfo entry)
 {
     return(entry.MetaType == "Application");
 }
Exemplo n.º 6
0
 private bool CheckNeedTicket(NintendoSubmissionPackageFileSystemInfo.EntryInfo entry)
 {
     return(TicketUtility.NeedCreateTicket(entry.MetaType));
 }
        private static NintendoSubmissionPackageFileSystemInfo GetNspInfo(NintendoSubmissionPackageReader nspReader, KeyConfiguration keyConfig, ArchiveReconstructionUtils.GetContentInfoDelegate getContentInfoImpl, ArchiveReconstructionUtils.GetContentMetaInfoDelegate getContentMetaInfoImpl)
        {
            NintendoSubmissionPackageFileSystemInfo packageFileSystemInfo = new NintendoSubmissionPackageFileSystemInfo();

            packageFileSystemInfo.Version = (byte)0;
            foreach (Tuple <string, long> tuple in nspReader.ListFileInfo())
            {
                string fileName = tuple.Item1;
                long   fileSize = tuple.Item2;
                if (fileName.EndsWith(".cnmt.xml"))
                {
                    ContentMetaModel model = ArchiveReconstructionUtils.ReadXml <ContentMetaModel>(nspReader, fileName, fileSize);
                    string           type  = model.Type;
                    if (!(type == "Application"))
                    {
                        if (!(type == "Patch"))
                        {
                            if (type == "AddOnContent")
                            {
                                model = (ContentMetaModel)ArchiveReconstructionUtils.ReadXml <AddOnContentContentMetaModel>(nspReader, fileName, fileSize);
                            }
                        }
                        else
                        {
                            model = (ContentMetaModel)ArchiveReconstructionUtils.ReadXml <PatchContentMetaModel>(nspReader, fileName, fileSize);
                        }
                    }
                    else
                    {
                        model = (ContentMetaModel)ArchiveReconstructionUtils.ReadXml <ApplicationContentMetaModel>(nspReader, fileName, fileSize);
                    }
                    NintendoSubmissionPackageFileSystemInfo.EntryInfo entry = new NintendoSubmissionPackageFileSystemInfo.EntryInfo();
                    entry.Contents = new List <NintendoSubmissionPackageFileSystemInfo.ContentInfo>();
                    foreach (ContentModel content in model.ContentList)
                    {
                        if (!(content.Type == "Meta"))
                        {
                            NintendoSubmissionPackageFileSystemInfo.ContentInfo contentInfo = getContentInfoImpl(content);
                            entry.Contents.Add(contentInfo);
                        }
                    }
                    string contentMetaFileName = Path.GetFileNameWithoutExtension(fileName) + ".nca";
                    getContentMetaInfoImpl(ref entry, contentMetaFileName, model);
                    List <NintendoSubmissionPackageExtraData> list = nspReader.ListFileInfo().FindAll((Predicate <Tuple <string, long> >)(x =>
                    {
                        if (x.Item1.StartsWith(fileName.Replace(".cnmt.xml", "")))
                        {
                            return(x.Item1.EndsWith(".jpg"));
                        }
                        return(false);
                    })).Select <Tuple <string, long>, NintendoSubmissionPackageExtraData>((Func <Tuple <string, long>, NintendoSubmissionPackageExtraData>)(x =>
                    {
                        byte[] buffer = nspReader.ReadFile(x.Item1, 0L, x.Item2);
                        return(new NintendoSubmissionPackageExtraData(x.Item1, (ISource) new MemorySource(buffer, 0, buffer.Length)));
                    })).ToList <NintendoSubmissionPackageExtraData>();
                    entry.ExtraData = list;
                    packageFileSystemInfo.Entries.Add(entry);
                }
                if (fileName == "cardspec.xml")
                {
                    CardSpecModel cardSpecModel = ArchiveReconstructionUtils.ReadXml <CardSpecModel>(nspReader, fileName, fileSize);
                    packageFileSystemInfo.CardSize      = Convert.ToInt32(cardSpecModel.Size, 10);
                    packageFileSystemInfo.CardClockRate = Convert.ToInt32(cardSpecModel.ClockRate, 10);
                }
            }
            return(packageFileSystemInfo);
        }
        public ProdEncryptedXciArchive(IReadableSink outSink, NintendoSubmissionPackageReader nspReader, NintendoSubmissionPackageReader uppReader, NintendoSubmissionPackageReader patchReader, byte launchFlags, KeyConfiguration keyConfig)
        {
            this.ConnectionList = new List <Connection>();
            this.m_rng          = new RNGCryptoServiceProvider();
            this.m_keyConfig    = keyConfig;
            this.SetCryptor(this.m_keyConfig);
            uint  num1 = 0;
            uint  num2 = 0;
            ulong num3 = 0;

            byte[] numArray1 = (byte[])null;
            NintendoSubmissionPackageFileSystemInfo prodNspInfo = ArchiveReconstructionUtils.GetProdNspInfo(nspReader, this.m_keyConfig);

            XciUtils.CheckRomSizeAndClockRate(prodNspInfo.CardSize, prodNspInfo.CardClockRate);
            NintendoSubmissionPackageFileSystemInfo uppInfo = (NintendoSubmissionPackageFileSystemInfo)null;

            if (uppReader != null)
            {
                uppInfo = ArchiveReconstructionUtils.GetProdNspInfo(uppReader, this.m_keyConfig);
            }
            XciSizeInfo xciSize = ProdEncryptedXciArchive.CalculateXciSize(prodNspInfo, uppInfo, (NintendoSubmissionPackageFileSystemInfo)null, this.m_keyConfig);
            int         romSize = XciUtils.GetRomSize(xciSize.TotalSize);

            if (romSize == XciInfo.InvalidRomSize)
            {
                throw new ArgumentException(string.Format("Xci size exceed the maximum size of the game card.", Array.Empty <object>()));
            }
            if (prodNspInfo.CardSize < romSize)
            {
                throw new ArgumentException(string.Format("Xci requires CardSpec/Size = {0}, though it is set as {1}.", (object)romSize, (object)prodNspInfo.CardSize));
            }
            outSink.SetSize(xciSize.TotalSize);
            List <Sha256PartitionFsHashSource> hashSources1 = new List <Sha256PartitionFsHashSource>();
            PartitionFileSystemInfo            partFsInfo1  = new PartitionFileSystemInfo();
            List <ContentHashSource>           hashSources2 = new List <ContentHashSource>();
            List <Sha256PartitionFsHashSource> hashSources3 = new List <Sha256PartitionFsHashSource>();

            if (uppInfo != null && uppReader != null)
            {
                long partitionHeaderSize = xciSize.UpdatePartitionHeaderSize;
                foreach (NintendoSubmissionPackageFileSystemInfo.EntryInfo entry in uppInfo.Entries)
                {
                    if (entry.MetaType == "SystemUpdate")
                    {
                        NintendoContentMetaReader contentMetaReader = new NintendoContentMetaReader(entry.ContentMetaInfo.Data);
                        num2 = contentMetaReader.GetVersion();
                        num3 = contentMetaReader.GetId();
                    }
                }
                byte[] buffer = uppReader.ReadBase(0L, uppReader.GetBaseSize());
                numArray1 = new SHA256CryptoServiceProvider().ComputeHash(buffer, 0, buffer.Length);
                foreach (Tuple <string, long> tuple in uppReader.ListFileInfo().FindAll((Predicate <Tuple <string, long> >)(x => x.Item1.EndsWith(".nca"))))
                {
                    string str    = tuple.Item1;
                    long   num4   = tuple.Item2;
                    ulong  offset = (ulong)(partitionHeaderSize - xciSize.UpdatePartitionHeaderSize);
                    PartitionFileSystemInfo.EntryInfo entryInfo = PartitionFileSystemInfo.EntryInfo.Make(str, (ulong)num4, offset, 0UL, XciInfo.PageSize);
                    partFsInfo1.entries.Add(entryInfo);
                    FileSystemArchvieFileSource archvieFileSource = new FileSystemArchvieFileSource((IFileSystemArchiveReader)uppReader, str);
                    IReadableSink readableSink = (IReadableSink) new ReadableSubSink(outSink, xciSize.UpdatePartitionOffset + partitionHeaderSize, archvieFileSource.Size);
                    this.ConnectionList.Add(new Connection((ISource)archvieFileSource, (ISink)readableSink));
                    hashSources2.Add(new ContentHashSource((ISource)null, "no_change"));
                    SinkLinkedSource sinkLinkedSource = new SinkLinkedSource((ISink)readableSink, (ISource) new Sha256StreamHashSource((ISource) new SubSource(readableSink.ToSource(), 0L, (long)XciInfo.PageSize)));
                    hashSources3.Add(new Sha256PartitionFsHashSource((ISource)sinkLinkedSource, hashSources3.Count));
                    partitionHeaderSize += archvieFileSource.Size;
                }
            }
            HashAdaptedSha256PartitionFsHeaderSource partitionFsHeaderSource1 = new HashAdaptedSha256PartitionFsHeaderSource((ISource) new HashNameEntryPartitionFsHeaderSource <Sha256PartitionFileSystemMeta>(hashSources2, partFsInfo1, xciSize.UpdatePartitionHeaderSize), hashSources3);
            IReadableSink readableSink1 = (IReadableSink) new ReadableSubSink(outSink, xciSize.UpdatePartitionOffset, partitionFsHeaderSource1.Size);

            this.ConnectionList.Add(new Connection((ISource)partitionFsHeaderSource1, (ISink)readableSink1));
            SinkLinkedSource sinkLinkedSource1 = new SinkLinkedSource((ISink)readableSink1, (ISource) new Sha256StreamHashSource((ISource) new SubSource(readableSink1.ToSource(), 0L, xciSize.UpdatePartitionHeaderSize)));

            hashSources1.Add(new Sha256PartitionFsHashSource((ISource)sinkLinkedSource1, hashSources1.Count));
            PartitionFileSystemInfo            partFsInfo2  = new PartitionFileSystemInfo();
            PartitionFileSystemInfo            partFsInfo3  = new PartitionFileSystemInfo();
            List <ContentHashSource>           hashSources4 = new List <ContentHashSource>();
            List <ContentHashSource>           hashSources5 = new List <ContentHashSource>();
            List <Sha256PartitionFsHashSource> hashSources6 = new List <Sha256PartitionFsHashSource>();
            List <Sha256PartitionFsHashSource> hashSources7 = new List <Sha256PartitionFsHashSource>();
            long partitionHeaderSize1 = xciSize.SecurePartitionHeaderSize;
            long partitionHeaderSize2 = xciSize.NormalPartitionHeaderSize;

            for (int index1 = 0; index1 < prodNspInfo.Entries.Count; ++index1)
            {
                NintendoSubmissionPackageFileSystemInfo.EntryInfo entry             = prodNspInfo.Entries[index1];
                List <Tuple <ISource, NintendoContentInfo> >      contentSourceList = new List <Tuple <ISource, NintendoContentInfo> >();
                for (int index2 = 0; index2 < entry.Contents.Count; ++index2)
                {
                    NintendoSubmissionPackageFileSystemInfo.ContentInfo content = entry.Contents[index2];
                    if (!(content.ContentType == "Meta"))
                    {
                        if (content.ContentType == "Program")
                        {
                            num1 = (content.FsInfo as NintendoContentFileSystemInfo).sdkAddonVersion;
                        }
                        (content.FsInfo as NintendoContentFileSystemInfo).distributionType = (byte)1;
                        NintendoContentArchiveSource contentArchiveSource = new NintendoContentArchiveSource(content.FsInfo as NintendoContentFileSystemInfo, this.m_keyConfig, false);
                        ulong offset1 = (ulong)(partitionHeaderSize1 - xciSize.SecurePartitionHeaderSize);
                        PartitionFileSystemInfo.EntryInfo entryInfo1 = PartitionFileSystemInfo.EntryInfo.Make("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.nca", (ulong)contentArchiveSource.Size, offset1, 0UL, XciInfo.PageSize);
                        partFsInfo2.entries.Add(entryInfo1);
                        IReadableSink readableSink2 = (IReadableSink) new ReadableSubSink(outSink, xciSize.SecurePartitionOffset + partitionHeaderSize1, contentArchiveSource.Size);
                        this.ConnectionList.Add(new Connection((ISource)contentArchiveSource, (ISink)readableSink2));
                        SinkLinkedSource sinkLinkedSource2 = new SinkLinkedSource((ISink)readableSink2, (ISource) new Sha256StreamHashSource(readableSink2.ToSource()));
                        SinkLinkedSource sinkLinkedSource3 = new SinkLinkedSource((ISink)readableSink2, (ISource) new Sha256StreamHashSource((ISource) new SubSource(readableSink2.ToSource(), 0L, (long)XciInfo.PageSize)));
                        hashSources4.Add(new ContentHashSource((ISource)sinkLinkedSource2, ".nca"));
                        hashSources6.Add(new Sha256PartitionFsHashSource((ISource)sinkLinkedSource3, hashSources6.Count));
                        contentSourceList.Add(Tuple.Create <ISource, NintendoContentInfo>(hashSources4[hashSources4.Count - 1].Source, new NintendoContentInfo(content.ContentType, contentArchiveSource.Size)));
                        partitionHeaderSize1 += ProdEncryptedXciArchive.RoundupPageSize(contentArchiveSource.Size);
                        if (content.ContentType == "Control")
                        {
                            ulong offset2 = (ulong)(partitionHeaderSize2 - xciSize.NormalPartitionHeaderSize);
                            PartitionFileSystemInfo.EntryInfo entryInfo2 = PartitionFileSystemInfo.EntryInfo.Make("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.nca", (ulong)contentArchiveSource.Size, offset2, 0UL, XciInfo.PageSize);
                            partFsInfo3.entries.Add(entryInfo2);
                            SinkLinkedSource sinkLinkedSource4 = new SinkLinkedSource((ISink)readableSink2, readableSink2.ToSource());
                            IReadableSink    readableSink3     = (IReadableSink) new ReadableSubSink(outSink, xciSize.NormalPartitionOffset + partitionHeaderSize2, sinkLinkedSource4.Size);
                            this.ConnectionList.Add(new Connection((ISource)sinkLinkedSource4, (ISink)readableSink3));
                            hashSources5.Add(new ContentHashSource((ISource)sinkLinkedSource2, ".nca"));
                            hashSources7.Add(new Sha256PartitionFsHashSource((ISource)sinkLinkedSource3, hashSources7.Count));
                            partitionHeaderSize2 += ProdEncryptedXciArchive.RoundupPageSize(contentArchiveSource.Size);
                        }
                    }
                }
                NintendoContentMetaArchiveSource metaArchiveSource = new NintendoContentMetaArchiveSource(new NintendoContentMetaBase(contentSourceList, entry.ContentMetaInfo.Data, entry.ContentMetaInfo.Model, true), entry.KeyIndex, this.m_keyConfig, prodNspInfo.IsProdEncryption, true);
                ulong offset3 = (ulong)(partitionHeaderSize1 - xciSize.SecurePartitionHeaderSize);
                PartitionFileSystemInfo.EntryInfo entryInfo3 = PartitionFileSystemInfo.EntryInfo.Make("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cnmt.nca", (ulong)metaArchiveSource.Size, offset3, 0UL, XciInfo.PageSize);
                partFsInfo2.entries.Add(entryInfo3);
                IReadableSink readableSink4 = (IReadableSink) new ReadableSubSink(outSink, xciSize.SecurePartitionOffset + partitionHeaderSize1, metaArchiveSource.Size);
                this.ConnectionList.Add(new Connection((ISource)metaArchiveSource, (ISink)readableSink4));
                SinkLinkedSource sinkLinkedSource5 = new SinkLinkedSource((ISink)readableSink4, (ISource) new Sha256StreamHashSource(readableSink4.ToSource()));
                SinkLinkedSource sinkLinkedSource6 = new SinkLinkedSource((ISink)readableSink4, (ISource) new Sha256StreamHashSource((ISource) new SubSource(readableSink4.ToSource(), 0L, (long)XciInfo.PageSize)));
                hashSources4.Add(new ContentHashSource((ISource)sinkLinkedSource5, ".cnmt.nca"));
                hashSources6.Add(new Sha256PartitionFsHashSource((ISource)sinkLinkedSource6, hashSources6.Count));
                partitionHeaderSize1 += ProdEncryptedXciArchive.RoundupPageSize(metaArchiveSource.Size);
                ulong offset4 = (ulong)(partitionHeaderSize2 - xciSize.NormalPartitionHeaderSize);
                PartitionFileSystemInfo.EntryInfo entryInfo4 = PartitionFileSystemInfo.EntryInfo.Make("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.cnmt.nca", (ulong)metaArchiveSource.Size, offset4, 0UL, XciInfo.PageSize);
                partFsInfo3.entries.Add(entryInfo4);
                SinkLinkedSource sinkLinkedSource7 = new SinkLinkedSource((ISink)readableSink4, readableSink4.ToSource());
                IReadableSink    readableSink5     = (IReadableSink) new ReadableSubSink(outSink, xciSize.NormalPartitionOffset + partitionHeaderSize2, sinkLinkedSource7.Size);
                this.ConnectionList.Add(new Connection((ISource)sinkLinkedSource7, (ISink)readableSink5));
                hashSources5.Add(new ContentHashSource((ISource)sinkLinkedSource5, ".cnmt.nca"));
                hashSources7.Add(new Sha256PartitionFsHashSource((ISource)sinkLinkedSource6, hashSources7.Count));
                partitionHeaderSize2 += ProdEncryptedXciArchive.RoundupPageSize(metaArchiveSource.Size);
            }
            HashAdaptedSha256PartitionFsHeaderSource partitionFsHeaderSource2 = new HashAdaptedSha256PartitionFsHeaderSource((ISource) new HashNameEntryPartitionFsHeaderSource <Sha256PartitionFileSystemMeta>(hashSources5, partFsInfo3, xciSize.NormalPartitionHeaderSize), hashSources7);
            IReadableSink readableSink6 = (IReadableSink) new ReadableSubSink(outSink, xciSize.NormalPartitionOffset, partitionFsHeaderSource2.Size);

            this.ConnectionList.Add(new Connection((ISource)partitionFsHeaderSource2, (ISink)readableSink6));
            SinkLinkedSource sinkLinkedSource8 = new SinkLinkedSource((ISink)readableSink6, (ISource) new Sha256StreamHashSource((ISource) new SubSource(readableSink6.ToSource(), 0L, xciSize.NormalPartitionHeaderSize)));

            hashSources1.Add(new Sha256PartitionFsHashSource((ISource)sinkLinkedSource8, hashSources1.Count));
            HashAdaptedSha256PartitionFsHeaderSource partitionFsHeaderSource3 = new HashAdaptedSha256PartitionFsHeaderSource((ISource) new HashNameEntryPartitionFsHeaderSource <Sha256PartitionFileSystemMeta>(hashSources4, partFsInfo2, xciSize.SecurePartitionHeaderSize), hashSources6);
            IReadableSink readableSink7 = (IReadableSink) new ReadableSubSink(outSink, xciSize.SecurePartitionOffset, partitionFsHeaderSource3.Size);

            this.ConnectionList.Add(new Connection((ISource)partitionFsHeaderSource3, (ISink)readableSink7));
            SinkLinkedSource sinkLinkedSource9 = new SinkLinkedSource((ISink)readableSink7, (ISource) new Sha256StreamHashSource((ISource) new SubSource(readableSink7.ToSource(), 0L, xciSize.SecurePartitionHeaderSize)));

            hashSources1.Add(new Sha256PartitionFsHashSource((ISource)sinkLinkedSource9, hashSources1.Count));
            PartitionFileSystemInfo  partFsInfo4  = new PartitionFileSystemInfo();
            List <ContentHashSource> hashSources8 = new List <ContentHashSource>();
            ulong offset5 = (ulong)(xciSize.UpdatePartitionOffset - xciSize.UpdatePartitionOffset);

            PartitionFileSystemInfo.EntryInfo entryInfo5 = PartitionFileSystemInfo.EntryInfo.Make("update", (ulong)(xciSize.UpdatePartitionHeaderSize + xciSize.UpdatePartitionSize), offset5, 0UL, (uint)xciSize.UpdatePartitionHeaderSize);
            partFsInfo4.entries.Add(entryInfo5);
            hashSources8.Add(new ContentHashSource((ISource)null, "update"));
            ulong offset6 = (ulong)(xciSize.NormalPartitionOffset - xciSize.UpdatePartitionOffset);

            PartitionFileSystemInfo.EntryInfo entryInfo6 = PartitionFileSystemInfo.EntryInfo.Make("normal", (ulong)(xciSize.NormalPartitionHeaderSize + xciSize.NormalPartitionSize), offset6, 0UL, (uint)xciSize.NormalPartitionHeaderSize);
            partFsInfo4.entries.Add(entryInfo6);
            hashSources8.Add(new ContentHashSource((ISource)null, "normal"));
            ulong offset7 = (ulong)(xciSize.SecurePartitionOffset - xciSize.UpdatePartitionOffset);

            PartitionFileSystemInfo.EntryInfo entryInfo7 = PartitionFileSystemInfo.EntryInfo.Make("secure", (ulong)(xciSize.SecurePartitionHeaderSize + xciSize.SecurePartitionSize), offset7, 0UL, (uint)xciSize.SecurePartitionHeaderSize);
            partFsInfo4.entries.Add(entryInfo7);
            hashSources8.Add(new ContentHashSource((ISource)null, "secure"));
            HashAdaptedSha256PartitionFsHeaderSource partitionFsHeaderSource4 = new HashAdaptedSha256PartitionFsHeaderSource((ISource) new HashNameEntryPartitionFsHeaderSource <Sha256PartitionFileSystemMeta>(hashSources8, partFsInfo4, xciSize.RootPartitionHeaderSize), hashSources1);
            IReadableSink readableSink8 = (IReadableSink) new ReadableSubSink(outSink, xciSize.RootPartitionOffset, partitionFsHeaderSource4.Size);

            this.ConnectionList.Add(new Connection((ISource)partitionFsHeaderSource4, (ISink)readableSink8));
            ISource rootPartitionFsHeaderHashSource = (ISource) new SinkLinkedSource((ISink)readableSink8, (ISource) new Sha256StreamHashSource((ISource) new SubSource(readableSink8.ToSource(), 0L, xciSize.RootPartitionHeaderSize)));

            byte[] data1 = new byte[8];
            this.m_rng.GetBytes(data1);
            ulong uint64 = BitConverter.ToUInt64(data1, 0);

            byte[] numArray2 = new byte[XciMeta.TitleKey1Size];
            byte[] numArray3 = new byte[XciMeta.TitleKey2Size];
            this.m_rng.GetBytes(numArray2);
            this.m_rng.GetBytes(numArray3);
            byte[]       initialData  = this.CreateInitialData(uint64, new Pair <byte[], byte[]>(numArray2, numArray3));
            byte[]       keyArea      = this.CreateKeyArea(initialData, new Pair <byte[], byte[]>(numArray2, numArray3));
            MemorySource memorySource = new MemorySource(keyArea, 0, keyArea.Length);

            byte[] data2 = new byte[XciInfo.IvSize];
            this.m_rng.GetBytes(data2);
            XciInfo xciInfo = new XciInfo();

            xciInfo.romAreaStartPageAddress = (uint)((ulong)xciSize.SecurePartitionOffset / (ulong)XciInfo.PageSize) - XciInfo.CardKeyAreaPageCount;
            xciInfo.kekIndex            = XciInfo.KekIndexVersion0;
            xciInfo.romSize             = XciInfo.ConvertRomSizeToRomSizeByte(prodNspInfo.CardSize);
            xciInfo.flags               = launchFlags;
            xciInfo.packageId           = uint64;
            xciInfo.validDataEndAddress = (uint)((int)(uint)((ulong)ProdEncryptedXciArchive.RoundupPageSize(xciSize.TotalSize) / (ulong)XciInfo.PageSize) - (int)XciInfo.CardKeyAreaPageCount - 1);
            xciInfo.iv = data2;
            xciInfo.partitionFsHeaderAddress = (ulong)(XciInfo.NormalAreaStartPageAddress - XciInfo.CardKeyAreaPageCount) * (ulong)XciInfo.PageSize;
            xciInfo.partitionFsHeaderSize    = (ulong)xciSize.RootPartitionHeaderSize;
            xciInfo.partitionFsHeaderHash    = (byte[])null;
            xciInfo.selSec     = XciInfo.SelSecForT1;
            xciInfo.fwVersion  = XciInfo.FwVersion;
            xciInfo.accCtrl1   = XciInfo.ConvertClockRateToAccCtrl1(prodNspInfo.CardClockRate);
            xciInfo.fwMode     = num1;
            xciInfo.cupVersion = num2;
            xciInfo.cupId      = num3;
            xciInfo.uppHash    = numArray1 != null ? ((IEnumerable <byte>)numArray1).Take <byte>(XciInfo.UppHashSize).ToArray <byte>() : new byte[XciInfo.UppHashSize];
            XciMeta         xciMeta          = new XciMeta(initialData);
            XciHeaderSource xciHeaderSource  = new XciHeaderSource(ref xciInfo, xciMeta, rootPartitionFsHeaderHashSource, this.m_headerEncryptor, this.m_headerSigner);
            ReadableSubSink readableSubSink1 = new ReadableSubSink(outSink, 0L, memorySource.Size);

            this.ConnectionList.Add(new Connection((ISource)memorySource, (ISink)readableSubSink1));
            ReadableSubSink readableSubSink2 = new ReadableSubSink(outSink, memorySource.Size, xciHeaderSource.Size);

            this.ConnectionList.Add(new Connection((ISource)xciHeaderSource, (ISink)readableSubSink2));
        }