Пример #1
0
        internal static void MakeRomFSData(RomfsFile[] RomFiles, MemoryStream metadata, RichTextBox TB_Progress = null, ProgressBar PB_Show = null)
        {
            updateTB(TB_Progress, "Computing IVFC Header Data...");
            IVFCInfo ivfc = new IVFCInfo {
                Levels = new IVFCLevel[3]
            };

            for (int i = 0; i < ivfc.Levels.Length; i++)
            {
                ivfc.Levels[i] = new IVFCLevel {
                    BlockSize = 0x1000
                };
            }
            ivfc.Levels[2].DataLength = RomfsFile.GetDataBlockLength(RomFiles, (ulong)metadata.Length);
            ivfc.Levels[1].DataLength = Align(ivfc.Levels[2].DataLength, ivfc.Levels[2].BlockSize) / ivfc.Levels[2].BlockSize * 0x20; //0x20 per SHA256 hash
            ivfc.Levels[0].DataLength = Align(ivfc.Levels[1].DataLength, ivfc.Levels[1].BlockSize) / ivfc.Levels[1].BlockSize * 0x20; //0x20 per SHA256 hash
            ulong MasterHashLen = Align(ivfc.Levels[0].DataLength, ivfc.Levels[0].BlockSize) / ivfc.Levels[0].BlockSize * 0x20;
            ulong lofs          = 0;

            foreach (IVFCLevel t in ivfc.Levels)
            {
                t.HashOffset = lofs;
                lofs        += Align(t.DataLength, t.BlockSize);
            }
            const uint IVFC_MAGIC      = 0x43465649; //IVFC
            const uint RESERVED        = 0x0;
            const uint HeaderLen       = 0x5C;
            const uint MEDIA_UNIT_SIZE = 0x200;

            byte[]     SuperBlockHash = new byte[0x20];
            FileStream OutFileStream  = new FileStream(TempFile, FileMode.Create, FileAccess.ReadWrite);

            try
            {
                OutFileStream.Seek(0, SeekOrigin.Begin);
                OutFileStream.Write(BitConverter.GetBytes(IVFC_MAGIC), 0, 0x4);
                OutFileStream.Write(BitConverter.GetBytes(0x10000), 0, 0x4);
                OutFileStream.Write(BitConverter.GetBytes(MasterHashLen), 0, 0x4);
                foreach (IVFCLevel t in ivfc.Levels)
                {
                    OutFileStream.Write(BitConverter.GetBytes(t.HashOffset), 0, 0x8);
                    OutFileStream.Write(BitConverter.GetBytes(t.DataLength), 0, 0x8);
                    OutFileStream.Write(BitConverter.GetBytes((int)Math.Log(t.BlockSize, 2)), 0, 0x4);
                    OutFileStream.Write(BitConverter.GetBytes(RESERVED), 0, 0x4);
                }
                OutFileStream.Write(BitConverter.GetBytes(HeaderLen), 0, 0x4);
                //IVFC Header is Written.
                OutFileStream.Seek((long)Align(MasterHashLen + 0x60, ivfc.Levels[0].BlockSize), SeekOrigin.Begin);
                byte[] metadataArray = metadata.ToArray();
                OutFileStream.Write(metadataArray, 0, metadataArray.Length);
                long baseOfs = OutFileStream.Position;
                updateTB(TB_Progress, "Writing Level 2 Data...");
                if (PB_Show.InvokeRequired)
                {
                    PB_Show.Invoke((MethodInvoker) delegate { PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = RomFiles.Length; });
                }
                else
                {
                    PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = RomFiles.Length;
                }

                foreach (RomfsFile t in RomFiles)
                {
                    OutFileStream.Seek(baseOfs + (long)t.Offset, SeekOrigin.Begin);
                    using (FileStream inStream = new FileStream(t.FullName, FileMode.Open, FileAccess.Read))
                    {
                        while (inStream.Position < inStream.Length)
                        {
                            byte[] buffer = new byte[inStream.Length - inStream.Position > 0x100000 ? 0x100000 : inStream.Length - inStream.Position];
                            inStream.Read(buffer, 0, buffer.Length);
                            OutFileStream.Write(buffer, 0, buffer.Length);
                        }
                    }
                    if (PB_Show.InvokeRequired)
                    {
                        PB_Show.Invoke((MethodInvoker)PB_Show.PerformStep);
                    }
                    else
                    {
                        PB_Show.PerformStep();
                    }
                }
                long          hashBaseOfs = (long)Align((ulong)OutFileStream.Position, ivfc.Levels[2].BlockSize);
                long          hOfs        = (long)Align(MasterHashLen, ivfc.Levels[0].BlockSize);
                long          cOfs        = hashBaseOfs + (long)ivfc.Levels[1].HashOffset;
                SHA256Managed sha         = new SHA256Managed();
                for (int i = ivfc.Levels.Length - 1; i >= 0; i--)
                {
                    updateTB(TB_Progress, "Computing Level " + i + " Hashes...");
                    byte[] buffer = new byte[(int)ivfc.Levels[i].BlockSize];

                    var count = (int)(ivfc.Levels[i].DataLength / ivfc.Levels[i].BlockSize);
                    if (PB_Show.InvokeRequired)
                    {
                        PB_Show.Invoke((MethodInvoker) delegate { PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = count; });
                    }
                    else
                    {
                        PB_Show.Minimum = 0; PB_Show.Step = 1; PB_Show.Value = 0; PB_Show.Maximum = count;
                    }

                    for (long ofs = 0; ofs < (long)ivfc.Levels[i].DataLength; ofs += ivfc.Levels[i].BlockSize)
                    {
                        OutFileStream.Seek(hOfs, SeekOrigin.Begin);
                        OutFileStream.Read(buffer, 0, (int)ivfc.Levels[i].BlockSize);
                        hOfs = OutFileStream.Position;
                        byte[] hash = sha.ComputeHash(buffer);
                        OutFileStream.Seek(cOfs, SeekOrigin.Begin);
                        OutFileStream.Write(hash, 0, hash.Length);
                        cOfs = OutFileStream.Position;
                        if (PB_Show.InvokeRequired)
                        {
                            PB_Show.Invoke((MethodInvoker)PB_Show.PerformStep);
                        }
                        else
                        {
                            PB_Show.PerformStep();
                        }
                    }

                    if (i <= 0)
                    {
                        continue;
                    }

                    if (i == 2)
                    {
                        long len = OutFileStream.Position;
                        if (len % 0x1000 != 0)
                        {
                            len = (long)Align((ulong)len, 0x1000);
                            byte[] buf = new byte[len - OutFileStream.Position];
                            OutFileStream.Write(buf, 0, buf.Length);
                        }
                    }

                    hOfs = hashBaseOfs + (long)ivfc.Levels[i - 1].HashOffset;
                    if (i > 1)
                    {
                        cOfs = hashBaseOfs + (long)ivfc.Levels[i - 2].HashOffset;
                    }
                    else
                    {
                        cOfs = (long)Align(HeaderLen, PADDING_ALIGN);
                    }
                }
                OutFileStream.Seek(0, SeekOrigin.Begin);
                uint   SuperBlockLen = (uint)Align(MasterHashLen + 0x60, MEDIA_UNIT_SIZE);
                byte[] MasterHashes  = new byte[SuperBlockLen];
                OutFileStream.Read(MasterHashes, 0, (int)SuperBlockLen);
                SuperBlockHash = sha.ComputeHash(MasterHashes);
            }
            finally
            {
                OutFileStream.Dispose();
            }

            if (OutFile == TempFile)
            {
                return;
            }
            if (File.Exists(OutFile))
            {
                File.Delete(OutFile);
            }
            File.Move(TempFile, OutFile);
        }
Пример #2
0
        internal static void MakeRomFSData(RomfsFile[] romFiles, MemoryStream metadata)
        {
            IvfcInfo ivfc = new IvfcInfo {
                Levels = new IvfcLevel[3]
            };

            for (int i = 0; i < ivfc.Levels.Length; i++)
            {
                ivfc.Levels[i] = new IvfcLevel {
                    BlockSize = 0x1000
                };
            }
            ivfc.Levels[2].DataLength = RomfsFile.GetDataBlockLength(romFiles, (ulong)metadata.Length);
            ivfc.Levels[1].DataLength = RomFS.Align(ivfc.Levels[2].DataLength, ivfc.Levels[2].BlockSize) / ivfc.Levels[2].BlockSize * 0x20;                       //0x20 per SHA256 hash
            ivfc.Levels[0].DataLength = RomFS.Align(ivfc.Levels[1].DataLength, ivfc.Levels[1].BlockSize) / ivfc.Levels[1].BlockSize * 0x20;                       //0x20 per SHA256 hash
            ulong masterHashLen = RomFS.Align(ivfc.Levels[0].DataLength, ivfc.Levels[0].BlockSize) / ivfc.Levels[0].BlockSize * 0x20;
            ulong lofs          = 0;

            foreach (IvfcLevel t in ivfc.Levels)
            {
                t.HashOffset = lofs;
                lofs        += RomFS.Align(t.DataLength, t.BlockSize);
            }
            const uint ivfcMagic     = 0x43465649;         //IVFC
            const uint reserved      = 0x0;
            const uint headerLen     = 0x5C;
            const uint mediaUnitSize = 0x200;
            FileStream outFileStream = new FileStream(TempFile, FileMode.Create, FileAccess.ReadWrite);

            try
            {
                outFileStream.Seek(0, SeekOrigin.Begin);
                outFileStream.Write(BitConverter.GetBytes(ivfcMagic), 0, 0x4);
                outFileStream.Write(BitConverter.GetBytes(0x10000), 0, 0x4);
                outFileStream.Write(BitConverter.GetBytes(masterHashLen), 0, 0x4);
                foreach (IvfcLevel t in ivfc.Levels)
                {
                    outFileStream.Write(BitConverter.GetBytes(t.HashOffset), 0, 0x8);
                    outFileStream.Write(BitConverter.GetBytes(t.DataLength), 0, 0x8);
                    outFileStream.Write(BitConverter.GetBytes((int)Math.Log(t.BlockSize, 2)), 0, 0x4);
                    outFileStream.Write(BitConverter.GetBytes(reserved), 0, 0x4);
                }
                outFileStream.Write(BitConverter.GetBytes(headerLen), 0, 0x4);
                //IVFC Header is Written.
                outFileStream.Seek((long)RomFS.Align(masterHashLen + 0x60, ivfc.Levels[0].BlockSize), SeekOrigin.Begin);
                byte[] metadataArray = metadata.ToArray();
                outFileStream.Write(metadataArray, 0, metadataArray.Length);
                long baseOfs = outFileStream.Position;

                foreach (RomfsFile t in romFiles)
                {
                    outFileStream.Seek(baseOfs + (long)t.Offset, SeekOrigin.Begin);
                    using (FileStream inStream = new FileStream(t.FullName, FileMode.Open, FileAccess.Read))
                    {
                        while (inStream.Position < inStream.Length)
                        {
                            byte[] buffer = new byte[inStream.Length - inStream.Position > 0x100000
                                                                                                                  ? 0x100000
                                                                                                                  : inStream.Length - inStream.Position];
                            inStream.Read(buffer, 0, buffer.Length);
                            outFileStream.Write(buffer, 0, buffer.Length);
                        }
                    }
                }
                long          hashBaseOfs = (long)RomFS.Align((ulong)outFileStream.Position, ivfc.Levels[2].BlockSize);
                long          hOfs        = (long)RomFS.Align(masterHashLen, ivfc.Levels[0].BlockSize);
                long          cOfs        = hashBaseOfs + (long)ivfc.Levels[1].HashOffset;
                SHA256Managed sha         = new SHA256Managed();
                for (int i = ivfc.Levels.Length - 1; i >= 0; i--)
                {
                    byte[] buffer = new byte[(int)ivfc.Levels[i].BlockSize];

                    for (long ofs = 0; ofs < (long)ivfc.Levels[i].DataLength; ofs += ivfc.Levels[i].BlockSize)
                    {
                        outFileStream.Seek(hOfs, SeekOrigin.Begin);
                        outFileStream.Read(buffer, 0, (int)ivfc.Levels[i].BlockSize);
                        hOfs = outFileStream.Position;
                        byte[] hash = sha.ComputeHash(buffer);
                        outFileStream.Seek(cOfs, SeekOrigin.Begin);
                        outFileStream.Write(hash, 0, hash.Length);
                        cOfs = outFileStream.Position;
                    }

                    if (i <= 0)
                    {
                        continue;
                    }

                    if (i == 2)
                    {
                        long len = outFileStream.Position;
                        if (len % 0x1000 != 0)
                        {
                            len = (long)RomFS.Align((ulong)len, 0x1000);
                            byte[] buf = new byte[len - outFileStream.Position];
                            outFileStream.Write(buf, 0, buf.Length);
                        }
                    }

                    hOfs = hashBaseOfs + (long)ivfc.Levels[i - 1].HashOffset;
                    if (i > 1)
                    {
                        cOfs = hashBaseOfs + (long)ivfc.Levels[i - 2].HashOffset;
                    }
                    else
                    {
                        cOfs = (long)RomFS.Align(headerLen, PaddingAlign);
                    }
                }
                outFileStream.Seek(0, SeekOrigin.Begin);
                uint   superBlockLen = (uint)RomFS.Align(masterHashLen + 0x60, mediaUnitSize);
                byte[] masterHashes  = new byte[superBlockLen];
                outFileStream.Read(masterHashes, 0, (int)superBlockLen);
                sha.ComputeHash(masterHashes);
            }
            finally
            {
                outFileStream.Dispose();
            }

            if (OutFile == TempFile)
            {
                return;
            }
            if (File.Exists(OutFile))
            {
                File.Delete(OutFile);
            }
            File.Move(TempFile, OutFile);
        }
Пример #3
0
        internal static Stream MakeRomFSData(RomfsFile[] RomFiles, MemoryStream metadata, string _writeDest)
        {
            updateTB("Computing IVFC Header Data...");
            IVFCInfo ivfc = new IVFCInfo {
                Levels = new IVFCLevel[3]
            };

            for (int i = 0; i < ivfc.Levels.Length; i++)
            {
                ivfc.Levels[i] = new IVFCLevel {
                    BlockSize = 0x1000
                };
            }
            ivfc.Levels[2].DataLength = RomfsFile.GetDataBlockLength(RomFiles, (ulong)metadata.Length);
            ivfc.Levels[1].DataLength = (Align(ivfc.Levels[2].DataLength, ivfc.Levels[2].BlockSize) / ivfc.Levels[2].BlockSize) * 0x20; //0x20 per SHA256 hash
            ivfc.Levels[0].DataLength = (Align(ivfc.Levels[1].DataLength, ivfc.Levels[1].BlockSize) / ivfc.Levels[1].BlockSize) * 0x20; //0x20 per SHA256 hash
            ulong MasterHashLen = (Align(ivfc.Levels[0].DataLength, ivfc.Levels[0].BlockSize) / ivfc.Levels[0].BlockSize) * 0x20;
            ulong lofs          = 0;

            foreach (IVFCLevel t in ivfc.Levels)
            {
                t.HashOffset = lofs;
                lofs        += Align(t.DataLength, t.BlockSize);
            }
            const uint IVFC_MAGIC = 0x43465649; //IVFC
            const uint RESERVED   = 0x0;
            const uint HeaderLen  = 0x5C;
            Stream     OutFileStream;

            if (_writeDest != null)
            {
                OutFileStream = new FileStream(TempFile, FileMode.Create, FileAccess.ReadWrite);
            }
            else
            {
                OutFileStream = new MemoryStream();
            }
            try
            {
                OutFileStream.Seek(0, SeekOrigin.Begin);
                OutFileStream.Write(BitConverter.GetBytes(IVFC_MAGIC), 0, 0x4);
                OutFileStream.Write(BitConverter.GetBytes(0x10000), 0, 0x4);
                OutFileStream.Write(BitConverter.GetBytes(MasterHashLen), 0, 0x4);
                foreach (IVFCLevel t in ivfc.Levels)
                {
                    OutFileStream.Write(BitConverter.GetBytes(t.HashOffset), 0, 0x8);
                    OutFileStream.Write(BitConverter.GetBytes(t.DataLength), 0, 0x8);
                    OutFileStream.Write(BitConverter.GetBytes((int)(Math.Log(t.BlockSize, 2))), 0, 0x4);
                    OutFileStream.Write(BitConverter.GetBytes(RESERVED), 0, 0x4);
                }
                OutFileStream.Write(BitConverter.GetBytes(HeaderLen), 0, 0x4);
                //IVFC Header is Written.
                OutFileStream.Seek((long)Align(MasterHashLen + 0x60, ivfc.Levels[0].BlockSize), SeekOrigin.Begin);
                byte[] metadataArray = metadata.ToArray();
                OutFileStream.Write(metadataArray, 0, metadataArray.Length);
                long baseOfs = OutFileStream.Position;
                updateTB("Writing Level 2 Data...");

                foreach (RomfsFile t in RomFiles)
                {
                    OutFileStream.Seek(baseOfs + (long)t.Offset, SeekOrigin.Begin);
                    using (FileStream inStream = new FileStream(t.realFilePath, FileMode.Open, FileAccess.Read))
                    {
                        while (inStream.Position < inStream.Length)
                        {
                            byte[] buffer = new byte[inStream.Length - inStream.Position > 0x100000 ? 0x100000 : inStream.Length - inStream.Position];
                            inStream.Read(buffer, 0, buffer.Length);
                            OutFileStream.Write(buffer, 0, buffer.Length);
                        }
                    }
                }
                long          hashBaseOfs = (long)Align((ulong)OutFileStream.Position, ivfc.Levels[2].BlockSize);
                long          hOfs        = (long)Align(MasterHashLen, ivfc.Levels[0].BlockSize);
                long          cOfs        = hashBaseOfs + (long)ivfc.Levels[1].HashOffset;
                SHA256Managed sha         = new SHA256Managed();
                for (int i = ivfc.Levels.Length - 1; i >= 0; i--)
                {
                    updateTB("Computing Level " + i + " Hashes...");
                    byte[] buffer = new byte[(int)ivfc.Levels[i].BlockSize];

                    for (long ofs = 0; ofs < (long)ivfc.Levels[i].DataLength; ofs += ivfc.Levels[i].BlockSize)
                    {
                        OutFileStream.Seek(hOfs, SeekOrigin.Begin);
                        OutFileStream.Read(buffer, 0, (int)ivfc.Levels[i].BlockSize);
                        hOfs = OutFileStream.Position;
                        byte[] hash = sha.ComputeHash(buffer);
                        OutFileStream.Seek(cOfs, SeekOrigin.Begin);
                        OutFileStream.Write(hash, 0, hash.Length);
                        cOfs = OutFileStream.Position;
                    }
                    if (i == 2)
                    {
                        long len = OutFileStream.Position;
                        if (len % 0x1000 != 0)
                        {
                            len = (long)Align((ulong)len, 0x1000);
                            byte[] buf = new byte[len - OutFileStream.Position];
                            OutFileStream.Write(buf, 0, buf.Length);
                        }
                    }
                    if (i <= 0)
                    {
                        continue;
                    }
                    hOfs = hashBaseOfs + (long)ivfc.Levels[i - 1].HashOffset;
                    if (i > 1)
                    {
                        cOfs = hashBaseOfs + (long)ivfc.Levels[i - 2].HashOffset;
                    }
                    else
                    {
                        cOfs = (long)Align(HeaderLen, PADDING_ALIGN);
                    }
                }
            }
            finally
            {
                if (OutFileStream != null && _writeDest != null)
                {
                    OutFileStream.Dispose();
                }
            }
            if (_writeDest != null)
            {
                OutFileStream.Dispose();
                if (_writeDest != TempFile)
                {
                    if (File.Exists(OutFile))
                    {
                        File.Delete(OutFile);
                    }
                    File.Move(TempFile, OutFile);
                }
                return(null);
            }
            else
            {
                return(OutFileStream);
            }
        }