Ejemplo n.º 1
0
        public void LoadFile(string path,long ncchoffset)
        {
            FileStream fs = File.OpenRead(path);
            fs.Position = ncchoffset;

            if (fs.Position >= fs.Length - Marshal.SizeOf(typeof(NCCHHeader)) + 0x100)
            {
                PrintErrorMessage("Invalid file");
                return;
            }
            CXI = new NCCH(fs);

            long ExefsSize = CaculateMediaUnite(CXI.Header.ExeFSLength);
            byte[] ExheaderRaw = new byte[0x800];
            byte[] ExeFSRaw = new byte[ExefsSize];
            fs.Read(ExheaderRaw, 0, ExheaderRaw.Length);
            fs.Position = NcchOffset + CaculateMediaUnite(CXI.Header.ExeFSOffset);
            fs.Read(ExeFSRaw, 0, ExeFSRaw.Length);

            using (MemoryStream exhstream = new MemoryStream(ExheaderRaw))
            {
                switch (CXI.Header.Flags[7])
                {
                    case 0:
                    case 0x20:
                        {
                            if (!CB_Advance.Checked)
                                throw new Exception("Use Advance Options to decrypt this file.");
                            if (string.IsNullOrEmpty(TB_Xor.Text))
                                return;
                            string exhxor = GetXorFilePath("exheader");
                            using (FileStream xorfs = File.OpenRead(exhxor))
                            {
                                using (MemoryStream decstream = new MemoryStream())
                                {
                                    Xorer.TramsfoemStream(decstream, exhstream, xorfs, 0, exhstream.Length);
                                    decstream.Position = 0;
                                    CXI.ExHeader = StructReader.ReadStruct<NCCHExtendedHeader>(decstream);
                                    CXI.AccessControlExtend = StructReader.ReadStruct<NCCHAccessControlExtend>(decstream);
                                }
                            }

                            string exefsnorm = GetXorFilePath("exefs_norm");
                            using (FileStream xorfs = File.OpenRead(exefsnorm))
                            {
                                using (MemoryStream decstream = new MemoryStream())
                                {
                                    Xorer.TramsfoemStream(decstream, new MemoryStream(ExeFSRaw), xorfs, 0, ExefsSize);
                                    decstream.Position = 0;
                                    ExeFs = new ExeFS(decstream);
                                    if (CXI.Header.Flags[3] == 1)
                                    {
                                        decstream.Position = 0;
                                        xorfs.Position = 0;
                                        Xorer.TramsfoemStream(decstream, new MemoryStream(ExeFSRaw), xorfs, 0, Marshal.SizeOf(typeof(ExeFSHeader)));
                                        using (FileStream xor7xfs = File.OpenRead(GetXorFilePath("exefs_7x")))
                                        {
                                            Xorer.TramsfoemStream(decstream, new MemoryStream(ExeFSRaw), xor7xfs, Marshal.SizeOf(typeof(ExeFSHeader)), ExeFs.Header.HeaderEntries[0].size);
                                        }
                                        xorfs.Position = 0;
                                        long TopLength = Marshal.SizeOf(typeof(ExeFSHeader)) + ExeFs.Header.HeaderEntries[0].size;
                                        Xorer.TramsfoemStream(decstream, new MemoryStream(ExeFSRaw), xorfs, TopLength, ExeFSRaw.Length - TopLength);
                                        decstream.Position = 0;
                                        ExeFs = new ExeFS(decstream);
                                    }
                                }
                            }
                            break;
                        }
                    case 1:
                    case 0x21:
                        {
                            byte[] aesctr = new byte[16];
                            NCCHInfo.GetNcchAesCounter(ref aesctr, CXI, NcchSection.exheader);
                            using (MemoryStream decstream = new MemoryStream())
                            {
                                Aes128Ctr.TransformFile(new byte[16], aesctr, decstream, exhstream, 0, exhstream.Length);
                                decstream.Position = 0;
                                CXI.ExHeader = StructReader.ReadStruct<NCCHExtendedHeader>(decstream);
                                CXI.AccessControlExtend = StructReader.ReadStruct<NCCHAccessControlExtend>(decstream);
                            }

                            NCCHInfo.GetNcchAesCounter(ref aesctr, CXI, NcchSection.exefs);
                            using (MemoryStream decstream = new MemoryStream())
                            {
                                Aes128Ctr.TransformFile(new byte[16], aesctr, decstream, new MemoryStream(ExeFSRaw), 0, ExeFSRaw.Length);
                                decstream.Position = 0;
                                ExeFs = new ExeFS(decstream);
                            }
                            break;
                        }
                    case 4:
                    case 5:
                        {
                            CXI.ExHeader = StructReader.ReadStruct<NCCHExtendedHeader>(exhstream);
                            CXI.AccessControlExtend = StructReader.ReadStruct<NCCHAccessControlExtend>(exhstream);
                            ExeFs = new ExeFS(new MemoryStream(ExeFSRaw));
                            break;
                        }
                }
            }
            fs.Close();
            ExeFs.SavedCode += ExeFs_SavedCode;
            ExeFs.SavingCode += ExeFs_SavingCode;
            ExeFs.DecompressingCode += ExeFs_DecompressingCode;
            ExeFs.DecompressedCode += ExeFs_DecompressedCode;
        }
Ejemplo n.º 2
0
        public void Initial()
        {
            CXI = null;
            ExeFs = null;
            NcchOffset = 0;

            ClearErrorMessage();
            PB_1.Visible = false;
            PB_1.Maximum = 0;
            PB_1.Value = 0;
        }