private static NintendoContentFileSystemInfo.EntryInfo GetFsEntry(NintendoContentArchiveReader ncaReader, int fsIndex, EntryReplaceRule replaceRule)
        {
            NintendoContentArchiveFsHeaderInfo fsInfo = (NintendoContentArchiveFsHeaderInfo)null;
            IFileSystemArchiveReader           systemArchiveReader = ncaReader.OpenFileSystemArchiveReader(fsIndex, ref fsInfo);

            NintendoContentFileSystemInfo.EntryInfo commonFsEntry = ArchiveReconstructionUtils.GetCommonFsEntry(fsInfo, fsIndex);
            Tuple <long, long> tuple = systemArchiveReader.GetFileFragmentList(replaceRule.Path).First <Tuple <long, long> >();
            long offset = tuple.Item1;

            if (tuple.Item2 == replaceRule.Source.Size)
            {
                AdaptedSource adaptedSource = new AdaptedSource((ISource) new FileSystemArchvieBaseSource(systemArchiveReader), replaceRule.Source, offset, replaceRule.Source.Size);
                commonFsEntry.sourceInterface = (SourceInterface) new CliCompatibleSource((ISource)adaptedSource);
            }
            else if (commonFsEntry.formatType == "RomFs")
            {
                RomFsArchiveSource romFsArchiveSource = new RomFsArchiveSource(ArchiveReconstructionUtils.GetRomFsInfo(systemArchiveReader, replaceRule));
                commonFsEntry.sourceInterface = (SourceInterface) new CliCompatibleSource((ISource)romFsArchiveSource);
            }
            else if (commonFsEntry.formatType == "PartitionFs")
            {
                PartitionFsArchiveSource partitionFsArchiveSource = new PartitionFsArchiveSource(ArchiveReconstructionUtils.GetPartitionFsInfo(systemArchiveReader, replaceRule));
                commonFsEntry.sourceInterface = (SourceInterface) new CliCompatibleSource((ISource)partitionFsArchiveSource);
            }
            return(commonFsEntry);
        }
        private static RomFsFileSystemInfo GetRomFsInfo(IFileSystemArchiveReader fsReader, EntryReplaceRule replaceRule)
        {
            RomFsFileSystemInfo fsInfo      = new RomFsFileSystemInfo();
            DirectoryList       directories = new DirectoryList();

            ArchiveReconstructionUtils.AddEntryToFsInfo addEntryDelegate = (ArchiveReconstructionUtils.AddEntryToFsInfo)(fileInfoList =>
            {
                foreach (ArchiveReconstructionUtils.BasicFileInfo fileInfo in fileInfoList)
                {
                    RomFsFileSystemInfo.EntryInfo entryInfo = new RomFsFileSystemInfo.EntryInfo();
                    entryInfo.type            = "source";
                    entryInfo.name            = "/" + fileInfo.Name;
                    entryInfo.size            = (ulong)fileInfo.Size;
                    entryInfo.offset          = (ulong)fileInfo.Offset;
                    entryInfo.path            = (string)null;
                    entryInfo.sourceInterface = fileInfo.Source;
                    fsInfo.entries.Add(entryInfo);
                    ++fsInfo.fileEntryCount;
                    directories.AddAncestors(Path.GetDirectoryName(entryInfo.name));
                }
            });
            ArchiveReconstructionUtils.GetFsInfo(fsReader, replaceRule, addEntryDelegate);
            fsInfo.directoryEntryCount = directories.Count;
            return(fsInfo);
        }
 public BasicFileInfo(Tuple <string, long> fileInfo, IFileSystemArchiveReader fsReader)
 {
     this.Name   = fileInfo.Item1;
     this.Size   = fileInfo.Item2;
     this.Offset = fsReader.GetFileFragmentList(this.Name).First <Tuple <long, long> >().Item1;
     this.Source = (SourceInterface)null;
 }
        private static void GetFsInfo(IFileSystemArchiveReader fsReader, EntryReplaceRule replaceRule, ArchiveReconstructionUtils.AddEntryToFsInfo addEntryDelegate)
        {
            ArchiveReconstructionUtils.BasicFileInfo basicFileInfo1 = new ArchiveReconstructionUtils.BasicFileInfo(fsReader.ListFileInfo().Find((Predicate <Tuple <string, long> >)(x => x.Item1 == replaceRule.Path)), fsReader);
            long num = replaceRule.Source.Size - basicFileInfo1.Size;
            List <ArchiveReconstructionUtils.BasicFileInfo> basicFileInfoList = new List <ArchiveReconstructionUtils.BasicFileInfo>();

            foreach (Tuple <string, long> fileInfo in fsReader.ListFileInfo())
            {
                ArchiveReconstructionUtils.BasicFileInfo basicFileInfo2 = new ArchiveReconstructionUtils.BasicFileInfo(fileInfo, fsReader);
                if (basicFileInfo2.Offset > basicFileInfo1.Offset)
                {
                    basicFileInfo2.Offset += num;
                }
                if (basicFileInfo2.Name == replaceRule.Path)
                {
                    basicFileInfo2.Size   = replaceRule.Source.Size;
                    basicFileInfo2.Source = (SourceInterface) new CliCompatibleSource(replaceRule.Source);
                }
                else
                {
                    basicFileInfo2.Source = (SourceInterface) new CliCompatibleSource((ISource) new FileSystemArchvieFileSource(fsReader, basicFileInfo2.Name));
                }
                basicFileInfoList.Add(basicFileInfo2);
            }
            addEntryDelegate((IEnumerable <ArchiveReconstructionUtils.BasicFileInfo>)basicFileInfoList);
        }
Beispiel #5
0
        private static void ExtractNca(NintendoContentArchiveReader reader, string outputDirectoryPath, string targetEntryPath)
        {
            bool flag1 = false;
            bool flag2 = !string.IsNullOrEmpty(targetEntryPath);

            Directory.CreateDirectory(outputDirectoryPath);
            foreach (Tuple <int, long> tuple1 in reader.ListFsInfo())
            {
                int index = tuple1.Item1;
                IFileSystemArchiveReader fsReader = reader.OpenFileSystemArchiveReader(index);
                foreach (Tuple <string, long> tuple2 in fsReader.ListFileInfo())
                {
                    string str1     = tuple2.Item1;
                    long   fileSize = tuple2.Item2;
                    string str2     = string.Format("fs{0}/{1}", (object)index, (object)str1);
                    if (!Program.FsUtil.CheckShouldSkipComparePath(targetEntryPath, str2))
                    {
                        string str3 = Path.Combine(outputDirectoryPath, Path.GetFileName(str1));
                        if (!flag2)
                        {
                            str3 = Path.Combine(outputDirectoryPath, str2);
                            Directory.CreateDirectory(Path.GetDirectoryName(str3));
                        }
                        Console.WriteLine("  Extracting {0}\t({1} byte)", (object)str2, (object)fileSize);
                        Program.FsUtil.WriteFile(fsReader, str3, str1, fileSize);
                        flag1 = true;
                    }
                }
            }
            if (!flag1)
            {
                throw new ExtractEntryNotFoundException();
            }
        }
Beispiel #6
0
 public FileSystemArchvieBaseSource(IFileSystemArchiveReader reader)
 {
     this.m_Reader = reader;
     this.Size     = this.m_Reader.GetBaseSize();
     this.m_status = new SourceStatus();
     this.m_status.AvailableRangeList.MergingAdd(new Range(0L, this.Size));
 }
Beispiel #7
0
 public FileSystemArchvieFileSource(IFileSystemArchiveReader reader, string fileName)
 {
     this.m_Reader   = reader;
     this.m_FileName = fileName;
     this.Size       = this.m_Reader.GetFileSize(this.m_FileName);
     this.m_status   = new SourceStatus();
     this.m_status.AvailableRangeList.MergingAdd(new Range(0L, this.Size));
 }
        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 NintendoContentFileSystemInfo.EntryInfo GetFsEntry(NintendoContentArchiveReader ncaReader, int fsIndex)
        {
            NintendoContentArchiveFsHeaderInfo fsInfo = (NintendoContentArchiveFsHeaderInfo)null;
            IFileSystemArchiveReader           reader = ncaReader.OpenFileSystemArchiveReader(fsIndex, ref fsInfo);

            NintendoContentFileSystemInfo.EntryInfo commonFsEntry     = ArchiveReconstructionUtils.GetCommonFsEntry(fsInfo, fsIndex);
            FileSystemArchvieBaseSource             archvieBaseSource = new FileSystemArchvieBaseSource(reader);

            commonFsEntry.sourceInterface = (SourceInterface) new CliCompatibleSource((ISource)archvieBaseSource);
            return(commonFsEntry);
        }
Beispiel #10
0
 internal static void WriteFile(IFileSystemArchiveReader fsReader, string extractedPath, string fileName, long fileSize)
 {
     using (FileStream fileStream = new FileStream(extractedPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, FileOptions.RandomAccess))
     {
         long offset = 0;
         while (offset < fileSize)
         {
             uint   num    = (uint)Math.Min(1048576L, fileSize - offset);
             byte[] buffer = fsReader.ReadFile(fileName, offset, (long)num);
             fileStream.Write(buffer, 0, buffer.Length);
             offset += (long)buffer.Length;
         }
     }
 }
        public static ProgramInfoXml GetProgramInfoXml(List <NintendoSubmissionPackageFileSystemInfo.ContentInfo> contentInfos, KeyConfiguration config)
        {
            NintendoSubmissionPackageFileSystemInfo.ContentInfo contentInfo = contentInfos.Where <NintendoSubmissionPackageFileSystemInfo.ContentInfo>((Func <NintendoSubmissionPackageFileSystemInfo.ContentInfo, bool>)(x => x.ContentType == "Program")).ToList <NintendoSubmissionPackageFileSystemInfo.ContentInfo>().Single <NintendoSubmissionPackageFileSystemInfo.ContentInfo>();
            string fileName = "main";
            string filePath = "";

            byte[] data = (byte[])null;
            bool   flag = false;

            if (contentInfo.FsInfo != null)
            {
                NintendoContentFileSystemInfo.EntryInfo entryInfo = (contentInfo.FsInfo as NintendoContentFileSystemInfo).fsEntries.Where <NintendoContentFileSystemInfo.EntryInfo>((Func <NintendoContentFileSystemInfo.EntryInfo, bool>)(x => x.partitionIndex == 0)).Single <NintendoContentFileSystemInfo.EntryInfo>();
                PartitionFileSystemInfo fileSystemInfo            = (PartitionFileSystemInfo)entryInfo.fileSystemInfo;
                if (fileSystemInfo != null && fileSystemInfo.entries.Where <PartitionFileSystemInfo.EntryInfo>((Func <PartitionFileSystemInfo.EntryInfo, bool>)(x => x.name == fileName)).Count <PartitionFileSystemInfo.EntryInfo>() == 1)
                {
                    flag     = true;
                    filePath = fileSystemInfo.entries.Where <PartitionFileSystemInfo.EntryInfo>((Func <PartitionFileSystemInfo.EntryInfo, bool>)(x => x.name == fileName)).Single <PartitionFileSystemInfo.EntryInfo>().path;
                    using (SourceBasedStream sourceBasedStream = new SourceBasedStream(NintendoContentArchiveSource.GetDataSource(entryInfo)))
                    {
                        IFileSystemArchiveReader systemArchiveReader = NintendoSubmissionPackageArchiveUtils.OpenFsReader(entryInfo, (Stream)sourceBasedStream);
                        data = systemArchiveReader.ReadFile(fileName, 0L, systemArchiveReader.GetFileSize(fileName));
                    }
                }
            }
            else
            {
                if (contentInfo.Source == null)
                {
                    throw new NotImplementedException();
                }
                byte[][] areaEncryptionKeys = config.GetKeyAreaEncryptionKeys();
                using (SourceBasedStream sourceBasedStream = new SourceBasedStream(contentInfo.Source))
                {
                    IFileSystemArchiveReader systemArchiveReader = new NintendoContentArchiveReader((Stream)sourceBasedStream, areaEncryptionKeys).OpenFileSystemArchiveReader(0);
                    if (systemArchiveReader.ListFileInfo().Where <Tuple <string, long> >((Func <Tuple <string, long>, bool>)(x => x.Item1 == fileName)).Count <Tuple <string, long> >() == 1)
                    {
                        flag     = true;
                        filePath = systemArchiveReader.ListFileInfo().Where <Tuple <string, long> >((Func <Tuple <string, long>, bool>)(x => x.Item1 == fileName)).Single <Tuple <string, long> >().Item1;
                        data     = systemArchiveReader.ReadFile(fileName, 0L, systemArchiveReader.GetFileSize(fileName));
                    }
                }
            }
            if (!flag)
            {
                Log.Warning("\"main\" was not found in code region.");
                data = new byte[0];
            }
            return(new ProgramInfoXml(new SymbolExtract(data, filePath)));
        }
        private static PartitionFileSystemInfo GetPartitionFsInfo(IFileSystemArchiveReader fsReader, EntryReplaceRule replaceRule)
        {
            PartitionFileSystemInfo fsInfo = new PartitionFileSystemInfo();

            ArchiveReconstructionUtils.AddEntryToFsInfo addEntryDelegate = (ArchiveReconstructionUtils.AddEntryToFsInfo)(fileInfoList =>
            {
                foreach (ArchiveReconstructionUtils.BasicFileInfo fileInfo in fileInfoList)
                {
                    fsInfo.entries.Add(new PartitionFileSystemInfo.EntryInfo()
                    {
                        type            = "source",
                        name            = fileInfo.Name,
                        size            = (ulong)fileInfo.Size,
                        offset          = (ulong)fileInfo.Offset,
                        path            = (string)null,
                        sourceInterface = fileInfo.Source
                    });
                }
            });
            ArchiveReconstructionUtils.GetFsInfo(fsReader, replaceRule, addEntryDelegate);
            return(fsInfo);
        }
        public static ApplicationControlPropertyModel GetApplicationControlProperty(List <NintendoSubmissionPackageFileSystemInfo.ContentInfo> contentInfos, KeyConfiguration config)
        {
            List <NintendoSubmissionPackageFileSystemInfo.ContentInfo> list = contentInfos.Where <NintendoSubmissionPackageFileSystemInfo.ContentInfo>((Func <NintendoSubmissionPackageFileSystemInfo.ContentInfo, bool>)(x => x.ContentType == "Control")).ToList <NintendoSubmissionPackageFileSystemInfo.ContentInfo>();

            if (list.Count > 1)
            {
                throw new NotImplementedException("Multiple control contents in an Application are not supported.");
            }
            NintendoSubmissionPackageFileSystemInfo.ContentInfo contentInfo = list.Single <NintendoSubmissionPackageFileSystemInfo.ContentInfo>();
            string fileName = "control.nacp";

            byte[] bytes;
            if (contentInfo.FsInfo != null)
            {
                NintendoContentFileSystemInfo.EntryInfo entryInfo = (contentInfo.FsInfo as NintendoContentFileSystemInfo).fsEntries.Single <NintendoContentFileSystemInfo.EntryInfo>();
                using (SourceBasedStream sourceBasedStream = new SourceBasedStream(NintendoContentArchiveSource.GetDataSource(entryInfo)))
                {
                    IFileSystemArchiveReader systemArchiveReader = NintendoSubmissionPackageArchiveUtils.OpenFsReader(entryInfo, (Stream)sourceBasedStream);
                    bytes = systemArchiveReader.ReadFile(fileName, 0L, systemArchiveReader.GetFileSize(fileName));
                }
            }
            else
            {
                if (contentInfo.Source == null)
                {
                    throw new NotImplementedException();
                }
                byte[][] areaEncryptionKeys = config.GetKeyAreaEncryptionKeys();
                using (SourceBasedStream sourceBasedStream = new SourceBasedStream(contentInfo.Source))
                {
                    IFileSystemArchiveReader systemArchiveReader = new NintendoContentArchiveReader((Stream)sourceBasedStream, areaEncryptionKeys).OpenFileSystemArchiveReader(0);
                    bytes = systemArchiveReader.ReadFile(fileName, 0L, systemArchiveReader.GetFileSize(fileName));
                }
            }
            return(ApplicationControlPropertyModel.PropertyBytesToModel(bytes));
        }