/// <summary> /// Writes a ROM to the pack. /// </summary> /// <param name="rom">The rom file to write.</param> /// <param name="flip">Whether to flip words. <c>true</c> by default.</param> public void SetImage(byte[] rom, bool flip = true) { ensureUndisposed(); if (rom == null || rom.Length == 0) { throw new ArgumentNullException("rom"); } var myRom = (byte[])rom.Clone(); if (flip) { FlipWords(myRom); } WordFlipped = true; ImageSize = (uint)myRom.Length; ImageChecksum = CalcChecksum(myRom, true); var compRom = new byte[(myRom.Length + 7) / 8 + myRom.Length]; // Account for worst-case scenario (completely random data) ImageCompressedSize = (uint)Lzss.Encode(myRom, compRom); Array.Resize <byte>(ref compRom, (int)ImageCompressedSize); CryptBuffer(compRom, ImageChecksum ^ KeyConversionFactor, keyBytes); pakStream.Seek(0x60, SeekOrigin.Begin); bw.Write(compRom); loaded = true; }
/// <summary> /// Gets the stored ROM image, unflipped. /// </summary> /// <returns>A stream with the ROM image.</returns> public MemoryStream GetImage() { ensureLoaded(); pakStream.Seek(0x60, SeekOrigin.Begin); byte[] rom = br.ReadBytes((int)ImageCompressedSize); CryptBuffer(rom, ImageChecksum ^ KeyConversionFactor, keyBytes); var decompRom = new byte[ImageSize]; Lzss.Decode(rom, decompRom); if (CalcChecksum(decompRom, true) != ImageChecksum) { throw new InvalidDataException("Image checksum failed."); } if (WordFlipped) { FlipWords(decompRom); } return(new MemoryStream(decompRom)); }