public override Stream OpenEntry(ArcFile arc, Entry entry) { var mca = arc as McaArchive; int method = arc.File.View.ReadInt32(entry.Offset); if (null == mca || method < 0 || method > 1) { return(base.OpenEntry(arc, entry)); } uint width = arc.File.View.ReadUInt32(entry.Offset + 0xC); uint height = arc.File.View.ReadUInt32(entry.Offset + 0x10); uint packed_size = arc.File.View.ReadUInt32(entry.Offset + 0x14); int unpacked_size = arc.File.View.ReadInt32(entry.Offset + 0x18); var data = arc.File.View.ReadBytes(entry.Offset + 0x20, packed_size); MrgOpener.Decrypt(data, 0, data.Length, mca.Key); if (method > 0) { using (var input = new MemoryStream(data)) using (var lzss = new MrgLzssReader(input, data.Length, unpacked_size)) { lzss.Unpack(); data = lzss.Data; } } int stride = ((int)width * 3 + 3) & ~3; var info = new ImageMetaData { Width = width, Height = height, BPP = 24 }; return(TgaStream.Create(info, stride, data)); }
public override ImageData Read(IBinaryStream stream, ImageMetaData info) { var meta = (ClmMetaData)info; stream.Position = meta.DataOffset; PixelFormat format; BitmapPalette palette = null; if (8 == meta.BPP) { format = PixelFormats.Indexed8; palette = ReadPalette(stream.AsStream); } else if (24 == meta.BPP) { format = PixelFormats.Bgr24; } else if (32 == meta.BPP) { format = PixelFormats.Bgr32; } else { throw new NotSupportedException("Not supported CLM color depth"); } int packed_size = (int)(stream.Length - stream.Position); using (var reader = new MrgLzssReader(stream, packed_size, meta.UnpackedSize)) { reader.Unpack(); return(ImageData.Create(info, format, palette, reader.Data)); } }
public override ImageData Read(IBinaryStream stream, ImageMetaData info) { var meta = (AcdMetaData)info; stream.Position = meta.DataOffset; using (var reader = new MrgLzssReader(stream, meta.PackedSize, meta.UnpackedSize)) { reader.Unpack(); var decoder = new AcdDecoder(reader.Data, meta); decoder.Unpack(); return(ImageData.Create(info, PixelFormats.Gray8, null, decoder.Data)); } throw new InvalidFormatException(); }
public override ImageData Read(Stream stream, ImageMetaData info) { var meta = info as AcdMetaData; if (null == meta) throw new ArgumentException ("AcdFormat.Read should be supplied with AcdMetaData", "info"); stream.Position = meta.DataOffset; using (var reader = new MrgLzssReader (stream, meta.PackedSize, meta.UnpackedSize)) { reader.Unpack(); var decoder = new AcdDecoder (reader.Data, meta); decoder.Unpack(); return ImageData.Create (info, PixelFormats.Gray8, null, decoder.Data); } throw new InvalidFormatException(); }
public override ImageData Read(Stream stream, ImageMetaData info) { var meta = info as AcdMetaData; if (null == meta) { throw new ArgumentException("AcdFormat.Read should be supplied with AcdMetaData", "info"); } stream.Position = meta.DataOffset; using (var reader = new MrgLzssReader(stream, meta.PackedSize, meta.UnpackedSize)) { reader.Unpack(); var decoder = new AcdDecoder(reader.Data, meta); decoder.Unpack(); return(ImageData.Create(info, PixelFormats.Gray8, null, decoder.Data)); } throw new InvalidFormatException(); }
protected override ImageData GetImageData() { m_input.Position = 0x20; var data = m_input.ReadBytes(m_packed_size); MrgOpener.Decrypt(data, 0, data.Length, m_key); if (m_compressed) { using (var input = new BinMemoryStream(data)) using (var lzss = new MrgLzssReader(input, data.Length, m_unpacked_size)) { lzss.Unpack(); data = lzss.Data; } } int stride = ((int)Info.Width * Info.BPP / 8 + 3) & ~3; var format = 8 == Info.BPP ? PixelFormats.Indexed8 : PixelFormats.Bgr24; return(ImageData.Create(Info, format, m_palette, data, stride)); }
void UnpackV101() { if (m_key != 0) { MrgOpener.Decrypt(m_input, 0, m_input.Length - 1, m_key); } #if DEBUG else // bruteforce key *in debug build only* { var copy = new byte[m_input.Length]; for (int key = 1; key < 256; ++key) { Buffer.BlockCopy(m_input, 0, copy, 0, m_input.Length); MrgOpener.Decrypt(copy, 0, copy.Length - 1, (byte)key); using (var input = new BinMemoryStream(copy)) using (var lzss = new MrgLzssReader(input, copy.Length, Stride * m_height)) { lzss.Unpack(); if (input.Length - input.Position <= 1) { Trace.WriteLine(string.Format("Found matching key {0:X2}", key), "[MCG]"); m_output = lzss.Data; m_key = (byte)key; return; } } } } #endif using (var input = new BinMemoryStream(m_input)) using (var lzss = new MrgLzssReader(input, m_input.Length, Stride * m_height)) { lzss.Unpack(); // data remaining within input stream indicates invalid encryption key if (input.Length - input.Position > 1) { m_key = 0; } m_output = lzss.Data; } }
void UnpackV101() { if (m_key != 0) { MrgOpener.Decrypt(m_input, 0, m_input.Length - 1, m_key); } #if DEBUG else // bruteforce key *in debug build only* { for (int key = 1; key < 256; ++key) { var copy = m_input.Clone() as byte[]; MrgOpener.Decrypt(copy, 0, copy.Length - 1, (byte)key); using (var input = new BinMemoryStream(copy)) using (var lzss = new MrgLzssReader(input, m_input.Length, Stride * m_height)) { lzss.Unpack(); if (input.Length - input.Position <= 1) { m_output = lzss.Data; m_key = (byte)key; return; } } } } #endif using (var input = new BinMemoryStream(m_input)) using (var lzss = new MrgLzssReader(input, m_input.Length, Stride * m_height)) { lzss.Unpack(); // data remaining within input stream indicates invalid encryption key if (input.Length - input.Position > 1) { m_key = 0; } m_output = lzss.Data; } }
public override Stream OpenEntry(ArcFile arc, Entry entry) { var mrg_entry = entry as Mrg2Entry; if (null == mrg_entry || mrg_entry.Method > 3) { return(arc.File.CreateStream(entry.Offset, entry.Size)); } IBinaryStream input; if (0 == mrg_entry.Method) { var data = arc.File.View.ReadBytes(entry.Offset, entry.Size); Decrypt(data, 0, data.Length, mrg_entry.Key, mrg_entry.ArcKey); input = new BinMemoryStream(data, entry.Name); } else if (mrg_entry.Method >= 2) { var data = arc.File.View.ReadBytes(entry.Offset, entry.Size); var reader = new MrgDecoder(data); reader.Unpack(); input = new BinMemoryStream(reader.Data, entry.Name); } else { input = arc.File.CreateStream(entry.Offset, entry.Size); } if (1 == mrg_entry.Method || 3 == mrg_entry.Method) { using (input) using (var reader = new MrgLzssReader(input, (int)input.Length, (int)mrg_entry.UnpackedSize)) { reader.Unpack(); return(new BinMemoryStream(reader.Data, entry.Name)); } } return(input.AsStream); }
public override Stream OpenEntry(ArcFile arc, Entry entry) { var packed_entry = entry as MrgEntry; if (null == packed_entry || !packed_entry.IsPacked || packed_entry.Method > 3) { return(arc.File.CreateStream(entry.Offset, entry.Size)); } Stream input; if (packed_entry.Method >= 2) { if (entry.Size < 0x108) { return(arc.File.CreateStream(entry.Offset, entry.Size)); } var data = new byte[entry.Size]; arc.File.View.Read(entry.Offset, data, 0, entry.Size); var reader = new MrgDecoder(data); reader.Unpack(); input = new MemoryStream(reader.Data); } else { input = arc.File.CreateStream(entry.Offset, entry.Size); } if (packed_entry.Method < 3) { using (input) using (var reader = new MrgLzssReader(input, (int)input.Length, (int)packed_entry.UnpackedSize)) { reader.Unpack(); return(new MemoryStream(reader.Data)); } } return(input); }
public override ImageData Read(Stream stream, ImageMetaData info) { var meta = (ClmMetaData)info; stream.Position = meta.DataOffset; PixelFormat format; BitmapPalette palette = null; if (8 == meta.BPP) { format = PixelFormats.Indexed8; palette = ReadPalette (stream); } else if (24 == meta.BPP) format = PixelFormats.Bgr24; else if (32 == meta.BPP) format = PixelFormats.Bgr32; else throw new NotSupportedException ("Not supported CLM color depth"); int packed_size = (int)(stream.Length - stream.Position); using (var reader = new MrgLzssReader (stream, packed_size, meta.UnpackedSize)) { reader.Unpack(); return ImageData.Create (info, format, palette, reader.Data); } }
public override Stream OpenEntry(ArcFile arc, Entry entry) { var packed_entry = entry as MrgEntry; if (null == packed_entry || !packed_entry.IsPacked || packed_entry.Method > 3) return arc.File.CreateStream (entry.Offset, entry.Size); Stream input; if (packed_entry.Method >= 2) { if (entry.Size < 0x108) return arc.File.CreateStream (entry.Offset, entry.Size); var data = new byte[entry.Size]; arc.File.View.Read (entry.Offset, data, 0, entry.Size); var reader = new MrgDecoder (data); reader.Unpack(); input = new MemoryStream (reader.Data); } else input = arc.File.CreateStream (entry.Offset, entry.Size); if (packed_entry.Method < 3) { using (input) using (var reader = new MrgLzssReader (input, (int)input.Length, (int)packed_entry.UnpackedSize)) { reader.Unpack(); return new MemoryStream (reader.Data); } } return input; }