コード例 #1
0
        /// <summary>
        /// Applies the given filename patch to the in-memory RomData
        /// </summary>
        /// <param name="filename"></param>
        /// <returns>SHA256 hash of the patch.</returns>
        public static byte[] ApplyPatch(string filename)
        {
            var hashAlg = new SHA256Managed();

            using (var filestream = File.OpenRead(filename))
                using (var cryptoStream = new CryptoStream(filestream, hashAlg, CryptoStreamMode.Read))
                    using (var decompressStream = new GZipStream(cryptoStream, CompressionMode.Decompress))
                        using (var memoryStream = new MemoryStream())
                        {
                            decompressStream.CopyTo(memoryStream);
                            memoryStream.Seek(0, SeekOrigin.Begin);
                            using (var reader = new BinaryReader(memoryStream))
                            {
                                var magic   = ReadWriteUtils.ReadU32(reader);
                                var version = ReadWriteUtils.ReadU32(reader);

                                // Validate patch magic and version values
                                PatchUtils.Validate(magic, version);

                                while (reader.BaseStream.Position != reader.BaseStream.Length)
                                {
                                    var fileIndex = ReadWriteUtils.ReadS32(reader);
                                    var fileAddr  = ReadWriteUtils.ReadS32(reader);
                                    var index     = ReadWriteUtils.ReadS32(reader);
                                    var isStatic  = ReadWriteUtils.ReadS32(reader) != 0 ? true : false;
                                    var length    = ReadWriteUtils.ReadS32(reader);
                                    var data      = reader.ReadBytes(length);
                                    if (fileIndex >= RomData.MMFileList.Count)
                                    {
                                        var newFile = new MMFile
                                        {
                                            Addr         = fileAddr,
                                            IsCompressed = false,
                                            Data         = data,
                                            End          = fileAddr + data.Length,
                                            IsStatic     = isStatic,
                                        };
                                        RomUtils.AppendFile(newFile);
                                    }
                                    if (index == -1)
                                    {
                                        RomData.MMFileList[fileIndex].Data = data;
                                        if (data.Length == 0)
                                        {
                                            RomData.MMFileList[fileIndex].Cmp_Addr = -1;
                                            RomData.MMFileList[fileIndex].Cmp_End  = -1;
                                        }
                                    }
                                    else
                                    {
                                        CheckCompressed(fileIndex);
                                        ReadWriteUtils.Arr_Insert(data, 0, data.Length, RomData.MMFileList[fileIndex].Data, index);
                                    }
                                }
                            }

                            return(hashAlg.Hash);
                        }
        }
コード例 #2
0
        public static bool CheckOldCRC(BinaryReader ROM)
        {
            ROM.BaseStream.Seek(16, 0);
            uint CRC1 = ReadWriteUtils.ReadU32(ROM);
            uint CRC2 = ReadWriteUtils.ReadU32(ROM);

            return((CRC1 == 0x5354631C) && (CRC2 == 0x03A2DEF0));
        }
コード例 #3
0
        public static int ByteswapROM(string filename)
        {
            using (BinaryReader ROM = new BinaryReader(File.OpenRead(filename)))
            {
                if (ROM.BaseStream.Length % 4 != 0)
                {
                    return(-1);
                }

                byte[] buffer = new byte[4];
                ROM.Read(buffer, 0, 4);
                // very hacky
                ROM.BaseStream.Seek(0, 0);
                if (buffer[0] == 0x80)
                {
                    return(1);
                }
                else if (buffer[1] == 0x80)
                {
                    using (BinaryWriter newROM = new BinaryWriter(File.Open(filename + ".z64", FileMode.Create)))
                    {
                        while (ROM.BaseStream.Position < ROM.BaseStream.Length)
                        {
                            newROM.Write(ReadWriteUtils.Byteswap16(ReadWriteUtils.ReadU16(ROM)));
                        }
                    }
                    return(0);
                }
                else if (buffer[3] == 0x80)
                {
                    using (BinaryWriter newROM = new BinaryWriter(File.Open(filename + ".z64", FileMode.Create)))
                    {
                        while (ROM.BaseStream.Position < ROM.BaseStream.Length)
                        {
                            newROM.Write(ReadWriteUtils.Byteswap32(ReadWriteUtils.ReadU32(ROM)));
                        }
                    }
                    return(0);
                }
            }
            return(-1);
        }