Exemplo n.º 1
0
        static void Main()
        {
            string compress    = "lz77";
            string eamuse_info = "1-5cf9445d-0dfe";

            byte[] data = HexToBytes("93b01743b29ca06e7500db42d83c70843dc776d0617ac96ba0768dd6b457554591d4b8b5f963e12d5fbb5075684c2a9acbfc462aa52686a720c57b3e44373008178684f9fd7ddad3c3a1e9fe1422ae08b9b872520a64cc195a9c04585149ec8de30220345c023663ae916068117ab7d5619362019d18a6f789bbd27e4ee027ce236d2b8d6c0f0917c8990083b741b3958cdf770f970df13088f931da949d1c9f685ba7848a15c3b77083357a6fb430b8a914bf55249f092c2baf14adfa8a7ab6bd430cc6ca5b4a35ea8c893aaa0c88ae6305240d5ae479976caf35e29d943ec628752c191ae40d0998c28e3280b6a55f8198ae");

            compress = compress.ToLower();

            IEnumerable <byte> decryptedData = data;

            if (eamuse_info != null)
            {
                decryptedData = RC4.ApplyEAmuseInfo(eamuse_info, data);
            }

            var rawData = decryptedData;

            if (compress == "lz77")
            {
                rawData = LZ77.Decompress(decryptedData);
            }
            else if (compress != "none")
            {
                throw new ArgumentException("Unsupported compression algorithm");
            }

            KBinXML kbinxml = new KBinXML(rawData.ToArray());

            Console.WriteLine(kbinxml);

            //GenerateEchidnaSQL(kbinxml);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Extracts the files from the file list
        ///
        /// Also performs decompression or creation of folders where necessary
        /// </summary>
        public void ExtractFiles()
        {
            byte[] data = null;

            Entries.ForEach(fileEntry =>
            {
                Reader.BaseStream.Seek(fileEntry.Offset, SeekOrigin.Begin);
                data = Reader.ReadBytes((int)fileEntry.FileSize);

                switch (fileEntry.Compression)
                {
                case 1:
                case 3:
                    data = LZ77.Decompress(data, fileEntry.FileSize, fileEntry.RealFileSize, fileEntry.Compression);
                    break;

                case 2:
                    Directory.CreateDirectory(fileEntry.FileName);
                    break;
                }

                if (fileEntry.FileSize != 0)
                {
                    File.WriteAllBytes(fileEntry.FileName, data);
                }
            });
        }
Exemplo n.º 3
0
        static ClanMusicInfo()
        {
            Instance = Task.Run(() =>
            {
                foreach (string name in Assembly.GetExecutingAssembly().GetManifestResourceNames())
                {
                    if (name.EndsWith("music_info_l44_8.kbin", StringComparison.InvariantCultureIgnoreCase))
                    {
                        using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
                            using (MemoryStream ms = new MemoryStream())
                            {
                                stream.CopyTo(ms);
                                return(new ClanMusicInfo(
                                           new KBinXML(
                                               LZ77.Decompress(
                                                   ms.ToArray()
                                                   ).ToArray()
                                               ).Document
                                           ));
                            }
                    }
                }

                return(null);
            });
        }
Exemplo n.º 4
0
            public static byte[] GetMap(int room, int layer)
            {
                byte[] output;
                int    result = LZ77.Decompress(Rom, Rom.ReadInt(Address + 4 + (((room * 3) + layer) << 2)) + Address, out output);

                return(output);
            }
Exemplo n.º 5
0
        /// <summary>
        /// Extracts the files from the file list
        ///
        /// Also performs decompression or creation of folders where necessary
        /// </summary>
        public void ExtractFiles()
        {
            using (BinaryReader reader = new BinaryReader(new MemoryStream(File.ReadAllBytes(FilePath))))
            {
                byte[] data = null;

                Entries.ForEach(fileEntry =>
                {
                    reader.BaseStream.Seek(fileEntry.Offset, SeekOrigin.Begin);
                    data = reader.ReadBytes((int)fileEntry.FileSize);

                    switch (fileEntry.Compression)
                    {
                    case 1:
                    case 3:
                        data = LZ77.Decompress(data, fileEntry.FileSize, fileEntry.RealFileSize,
                                               fileEntry.Compression);
                        break;

                    case 2:
                        Directory.CreateDirectory(fileEntry.FileName);
                        break;

                    default:
                        Debug.WriteLine($"Unknown compression value '{fileEntry.Compression.ToString()}'");
                        break;
                    }

                    if (fileEntry.FileSize != 0)
                    {
                        File.WriteAllBytes(fileEntry.FileName, data);
                    }
                });
            }
        }
Exemplo n.º 6
0
            public static byte[] GetTiles(int room)
            {
                byte[] output;
                int    result = LZ77.Decompress(Rom, Rom.ReadInt(Address + 4 + (room << 2)) + Address, out output);

                return(output);
            }
Exemplo n.º 7
0
            public static byte[] GetGfx(ushort index)
            {
                byte[] output;
                int    result = LZ77.Decompress(Rom, GetGfxAddress(index), out output);

                return(output);
            }
Exemplo n.º 8
0
        private void btnDecomp_Click(object sender, EventArgs e)
        {
            int address = GetInt(txtAddress);

            if (address == -1)
            {
                txtAddress.SelectAll();
                return;
            }

            // Try to decomp
            byte[] output;
            int    res = LZ77.Decompress(M3Rom.Rom, address, out output);

            if (res == -1)
            {
                MessageBox.Show("There was an error decompressing the data!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show("Decompressed " + output.Length + " bytes from " + res + " bytes.", "Success");
                if (dlgSave.ShowDialog() == DialogResult.OK)
                {
                    File.WriteAllBytes(dlgSave.FileName, output);
                }
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Reads 'length' of bytes from the currently loaded ROM (does LZ77 decompress if length == 0)
        /// </summary>
        public static Byte[] ReadData(Pointer address, int length)
        {
            if (address == 0)
            {
                return(new byte[0]);
            }

            //Program.Core.FEH.Space.MarkSpace("DATA", address, address + length);

            try
            {
                if (length == 0)
                {
                    return(LZ77.Decompress(address));
                }
                else
                {
                    return(Program.Core.ROM.Read(address, length));
                }
            }
            catch (Exception ex)
            {
                Program.ShowError("Data could not be read from ROM at address " + address, ex);
                return(new Byte[length]);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Reads TSA data at the given address, handling the byte array as necessary
        /// </summary>
        /// <param name="address">The address to the read the data at</param>
        /// <param name="width">The width of the TSA, in tiles</param>
        /// <param name="height">The height of the TSA, in tiles</param>
        /// <param name="compressed">Whether or not the TSA data is LZ77 compressed</param>
        /// <param name="flipRows">Whether or not to flip the rows of the TSA (usually for uncompressed TSA)</param>
        /// <returns></returns>
        public static TSA_Array ReadTSA(Pointer address, int width, int height, bool compressed, bool flipRows)
        {
            if (address == 0)
            {
                return(null);
            }

            //Program.Core.FEH.Space.MarkSpace("TSA ", address, address + length);

            byte[] data;
            if (compressed)
            {
                try
                {
                    data = LZ77.Decompress(address);

                    if (flipRows)
                    {
                        width  = data[0] + 1;
                        height = data[1] + 1;
                        data   = data.GetBytes(2);
                    }
                }
                catch (Exception ex)
                {
                    Program.ShowError("There has been an error while trying to decompress the TSA.", ex);
                    return(null);
                }
            }
            else if (flipRows)
            {
                width  = Core.ReadByte(address) + 1;
                height = Core.ReadByte(address + 1) + 1;
                data   = Program.Core.ROM.Read(address + 2, width * height * 2);
            }
            else
            {
                data = Program.Core.ROM.Read(address, width * height * 2);
            }

            if (flipRows)
            {
                byte[] result = new byte[width * height * 2];
                int    row    = width * 2;
                for (int i = 0; i < height; i++)
                {
                    Array.Copy(data, (height - 1 - i) * row, result, i * row, row);
                }
                return(new TSA_Array(width, height, result));
            }
            else
            {
                return(new TSA_Array(width, height, data));
            }
        }
Exemplo n.º 11
0
        public void Decompress2_Decompresses_Decompressed()
        {
            var lz77   = new LZ77();
            var input  = new MemoryStream(new byte[] { 16, 9, 0, 0, 0, 2, 128, 0, 0, 0, 128, 0, 128, 33, 0, 0, 0, 0, 16, 0, 0, 0, 80, 0, 40, 4, 16, 0, 0, 0, 12, 0, 0, 0, 9, 0, 9, 0, 129, 128, 5, 0, 192, 64, 3, 192, 32, 32, 0, 32, 48, 128, 0, 0, 0, 0, 0, 152, 4, 48 });
            var output = new MemoryStream();

            byte[] excpected = { 5, 2, 5, 8, 1, 10, 2, 4, 6, 9, 2, 3, 5, 8, 1, 1, 2, 1, 1, 1, 1, 8, 0, 1, 12 };
            lz77.Decompress(input, output);
            byte[] result = output.ToArray();
            CollectionAssert.AreEqual(excpected, result);
        }
Exemplo n.º 12
0
        public void Decompress_Decompresses_Decompressed()
        {
            var lz77   = new LZ77();
            var input  = new MemoryStream(new byte[] { 16, 9, 0, 0, 0, 1, 0, 0, 0, 1, 64, 0, 0, 0, 224, 0, 0, 0, 16, 0, 0, 0, 64, 0, 40, 8, 8, 0, 0, 0, 0, 0, 18, 2, 1, 0, 0, 0, 3, 0, 4, 128, 66, 64, 3, 192, 32, 32 });
            var output = new MemoryStream();

            byte[] excpected = { 2, 5, 7, 1, 8, 2, 5, 2, 0, 2, 5, 1, 6, 8, 9, 2, 1 };
            lz77.Decompress(input, output);
            byte[] result = output.ToArray();
            CollectionAssert.AreEqual(excpected, result);
        }
Exemplo n.º 13
0
        public void Decompress3_Decompresses_Decompressed()
        {
            var lz77   = new LZ77();
            var input  = new MemoryStream(new byte[] { 16, 9, 0, 0, 0, 0, 128, 0, 0, 0, 128, 0, 0, 0, 96 });
            var output = new MemoryStream();

            byte[] excpected = { 1, 2, 3 };
            lz77.Decompress(input, output);
            byte[] result = output.ToArray();
            CollectionAssert.AreEqual(excpected, result);
        }
Exemplo n.º 14
0
        private async Task <InputFormatterResult> ProcessInputData(byte[] data, string eAmuseInfo, string compAlgo)
        {
            data = await Task.Run(() =>
            {
                IEnumerable <byte> rawData = data;
                if (eAmuseInfo != null)
                {
                    rawData = RC4.ApplyEAmuseInfo(eAmuseInfo, data);
                }

                switch (compAlgo.ToLower())
                {
                case "lz77":
                    return(LZ77.Decompress(rawData).ToArray());

                case "none":
                    return(rawData.ToArray());

                default:
                    return(null);
                }
            });

            if (data == null)
            {
                return(await InputFormatterResult.FailureAsync());
            }

            KBinXML result = await Task.Run(() =>
            {
                try
                {
                    return(new KBinXML(data));
                }
                catch (Exception)
                {
                    Console.WriteLine("Got invalid binary XML input!");
                    return(null);
                }
            });

            if (result == null)
            {
                return(await InputFormatterResult.FailureAsync());
            }

            return(await InputFormatterResult.SuccessAsync(new EamuseXrpcData()
            {
                Document = result.Document,
                Encoding = result.BinEncoding,
                EamuseInfo = eAmuseInfo
            }));
        }
Exemplo n.º 15
0
        static void Test(byte[] bytes, Method method)
        {
            byte[] bytes2 = new byte[bytes.Length];

            switch (method)
            {
            case Method.LZ10:
                bytes2 = LZ10.Decompress(new MemoryStream(LZ10.Compress(new MemoryStream(bytes))), bytes.Length);
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                break;

            case Method.LZ11:
                bytes2 = LZ11.Decompress(new MemoryStream(LZ11.Compress(new MemoryStream(bytes))), bytes.Length);
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                break;

            case Method.LZ40:
                bytes2 = LZ40.Decompress(new MemoryStream(LZ40.Compress(new MemoryStream(bytes))), bytes.Length);
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                break;

            case Method.LZ77:
                bytes2 = LZ77.Decompress(new MemoryStream(LZ77.Compress(new MemoryStream(bytes))));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                break;

            case Method.RevLZ77:
                bytes2 = RevLZ77.Decompress(new MemoryStream(RevLZ77.Compress(new MemoryStream(bytes))));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                break;

            case Method.LZ4:
                bytes2 = LZ4.Decompress(new MemoryStream(LZ4.Compress(new MemoryStream(bytes))));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                break;

            case Method.LZECD:
                bytes2 = LZECD.Decompress(new MemoryStream(LZECD.Compress(new MemoryStream(bytes))));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                break;

            case Method.LZOvl:
                bytes2 = LZOvl.Decompress(new MemoryStream(LZOvl.Compress(new MemoryStream(bytes))));
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                break;

            case Method.MIO0:
                bytes2 = MIO0.Decompress(new MemoryStream(MIO0.Compress(new MemoryStream(bytes), ByteOrder.LittleEndian)), ByteOrder.LittleEndian);
                Assert.IsTrue(bytes.SequenceEqual(bytes2));
                break;
            }
        }
        unsafe public static Bitmap[] GetEnemySprite(int index)
        {
            Bitmap[] ret = new Bitmap[2];

            // Get gfx stuff
            int gfxPointer = GetPointer(index + 8) + 12;

            byte[] gfxData;
            if (LZ77.Decompress(Rom, gfxPointer, out gfxData) == -1)
            {
                return(null);
            }

            // Bug with MOTHER 3: for the second Mole Cricket (index 0x3D),
            // it uses tiles that are out of bounds in the tile data!
            // So let's resize the tile buffer to 32KB (max addressable size)
            Array.Resize(ref gfxData, 0x8000);

            // Get pal stuff
            var palette = GetPals(index);

            // Get OAM stuff
            int oamPointer = GetPointer(index + 0x2A4);
            var oam        = new OamSprite[2][];

            for (int i = 0; i < 2; i++)
            {
                // Get the front/back sprite pointer
                int subPointer = Rom.ReadUShort(oamPointer + 8 + (i << 1)) + oamPointer;

                // Get the number of sub-sprites
                ushort numSubSprites = Rom.ReadUShort(subPointer + 2);
                oam[i] = new OamSprite[numSubSprites];

                // Get the OAM data
                for (int j = 0; j < numSubSprites; j++)
                {
                    oam[i][j] = Rom.ReadOam(subPointer + 4 + (j << 3));

                    // Filter out the palette -- each enemy only has one palette, regardless of what the OAM entry says
                    // This is only an issue with enemy #0 anyway
                    oam[i][j].Palette = 0;
                }

                // Render the sprites
                Bitmap bmp = GfxProvider.RenderSprites(oam[i], gfxData, palette);
                ret[i] = bmp;
            }

            return(ret);
        }
        public static ArrEntry[] GetArr(int index)
        {
            int[]  entry = GetEntry(index);
            byte[] bytes;
            int    res = LZ77.Decompress(Rom, entry[0] + 12, out bytes);

            if (res == -1)
            {
                return(null);
            }
            if (bytes.Length != 2048)
            {
                return(null);
            }

            ArrEntry[] ret = new ArrEntry[1024];
            for (int i = 0; i < 1024; i++)
            {
                ret[i] = bytes.ReadArrEntry(i << 1);
            }

            return(ret);
        }
Exemplo n.º 18
0
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            List <ImageWrapper> images = new List <ImageWrapper>();
            int i = 0;

            for (int offset = 0; offset < (file.Length - 1); offset++)
            {
                if (file[offset] == 'L' && file[offset + 1] == 'e')
                {
                    int size = Utils.GetIntAtPosition(file, offset + 2);
                    if (size > 0 && size < 0x8000)
                    {
                        //ArrayPtr arrayPtr = new ArrayPtr(file, offset);
                        byte[]       data   = Malias2.Decompress(file, offset);
                        ImageWrapper result = new ImageWrapper(i, offset, data, "Malias2");
                        images.Add(result);
                        i++;
                    }
                }
                else if (file[offset] == 0x10)
                {
                    int size = file[offset + 1] | file[offset + 2] << 8 | file[offset + 3] << 16;
                    if (size > 0 && size < 0x8000 && size % 0x20 == 0)
                    {
                        byte[] data = LZ77.Decompress(file, offset);
                        if (data != null)
                        {
                            ImageWrapper result = new ImageWrapper(i, offset, data, "LZ77");
                            images.Add(result);
                            i++;
                        }
                    }
                }
            }
            imagesList.ItemsSource = images;
        }
Exemplo n.º 19
0
    public static string DecompressStr(byte[] data)
    {
        LZ77 lz = new LZ77();

        return(lz.Decompress(data));
    }
Exemplo n.º 20
0
        public static void Decompress(object sender, EventArgs e)
        {
            var tsi = sender as ToolStripMenuItem;

            if (!Shared.PrepareFiles("Open a " + tsi?.Tag + " compressed file...", "Save your decompressed file...", ".decomp", out var openFile, out var saveFile))
            {
                return;
            }

            try
            {
                using (openFile)
                    using (var outFs = new BinaryWriterX(saveFile))
                        switch (tsi?.Tag)
                        {
                        case Compression.Level5:
                            outFs.Write(Level5.Decompress(openFile));
                            break;

                        case Compression.Nintendo:
                            outFs.Write(Nintendo.Decompress(openFile));
                            break;

                        case Compression.LZ77:
                            outFs.Write(LZ77.Decompress(openFile));
                            break;

                        case Compression.RevLZ77:
                            outFs.Write(RevLZ77.Decompress(openFile));
                            break;

                        case Compression.LZOvl:
                            outFs.Write(LZOvl.Decompress(openFile));
                            break;

                        case Compression.LZ4:
                            outFs.Write(Kontract.Compression.LZ4.Decompress(openFile));
                            break;

                        case Compression.MIO0LE:
                            outFs.Write(MIO0.Decompress(openFile, ByteOrder.LittleEndian));
                            break;

                        case Compression.MIO0BE:
                            outFs.Write(MIO0.Decompress(openFile, ByteOrder.BigEndian));
                            break;

                        case Compression.Yay0LE:
                            outFs.Write(Yay0.Decompress(openFile, ByteOrder.LittleEndian));
                            break;

                        case Compression.Yay0BE:
                            outFs.Write(Yay0.Decompress(openFile, ByteOrder.BigEndian));
                            break;

                        case Compression.Yaz0LE:
                            outFs.Write(Yaz0.Decompress(openFile, ByteOrder.LittleEndian));
                            break;

                        case Compression.Yaz0BE:
                            outFs.Write(Yaz0.Decompress(openFile, ByteOrder.BigEndian));
                            break;

                        case Compression.LZECD:
                            outFs.Write(LZECD.Decompress(openFile));
                            break;

                        case Compression.LZ10VLE:
                            outFs.Write(LZSSVLE.Decompress(openFile));
                            break;

                        case Compression.GZip:
                            outFs.Write(GZip.Decompress(openFile));
                            break;

                        case Compression.ZLib:
                            outFs.Write(ZLib.Decompress(openFile));
                            break;

                        case Compression.PSVSpikeChun:
                            outFs.Write(PSVSpikeChun.Decompress(openFile));
                            break;
                        }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            MessageBox.Show($"Successfully decompressed {Path.GetFileName(openFile.Name)}.", tsi.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Exemplo n.º 21
0
 public byte[] DecompressLZ77CompressedData(int offset)
 {
     return(LZ77.Decompress(ROMdata, offset));
 }
 public static byte[] GetTitleTileset()
 {
     byte[] ret;
     LZ77.Decompress(Rom, GetPointer(6), out ret);
     return(ret);
 }
Exemplo n.º 23
0
        public static void Decompress(object sender, EventArgs e)
        {
            var tsi = sender as ToolStripMenuItem;

            if (!PrepareFiles("Open a " + tsi.Tag.ToString() + " compressed file...", "Save your decompressed file...", ".decomp", out FileStream openFile, out FileStream saveFile))
            {
                return;
            }

            try
            {
                using (openFile)
                    using (var outFs = new BinaryWriterX(saveFile))
                        switch (tsi.Tag)
                        {
                        case Compression.Level5:
                            outFs.Write(Level5.Decompress(openFile));
                            break;

                        case Compression.GZip:
                            outFs.Write(GZip.Decompress(openFile));
                            break;

                        case Compression.Huff4:
                            outFs.Write(Huffman.Decompress(openFile, 4));
                            break;

                        case Compression.Huff8:
                            outFs.Write(Huffman.Decompress(openFile, 8));
                            break;

                        case Compression.LZ10:
                            outFs.Write(LZ10.Decompress(openFile));
                            break;

                        case Compression.LZ11:
                            outFs.Write(LZ11.Decompress(openFile));
                            break;

                        case Compression.LZ77:
                            outFs.Write(LZ77.Decompress(openFile));
                            break;

                        /*case Compression.LZSS:
                         *  outFs.Write(LZSS.Decompress(openFile, LZSS.GetDecompressedSize(openFile)));
                         *  break;*/
                        case Compression.RevLZ77:
                            outFs.Write(RevLZ77.Decompress(openFile));
                            break;

                        case Compression.RLE:
                            outFs.Write(RLE.Decompress(openFile));
                            break;

                        case Compression.ZLib:
                            outFs.Write(ZLib.Decompress(openFile));
                            break;
                        }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            MessageBox.Show($"Successfully decompressed {Path.GetFileName(openFile.Name)}.", tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
Exemplo n.º 24
-4
 public static string DecompressStr(byte[] data)
 {
     LZ77 lz = new LZ77();
     return lz.Decompress(data);
 }