Compressor and decompressor for the LZ-0x11 format used in many of the games for the newer Nintendo consoles and handhelds.
Exemplo n.º 1
0
        private void saveAsButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "Binary files (*.bin)|*.bin";
            if (saveDialog.ShowDialog() != DialogResult.OK) return;

            // For testing purpose
            if (Header == null)
            {
                File.WriteAllBytes(saveDialog.FileName, SaveData);
                return;
            }

            LZ11 lz11 = new LZ11();
            byte[] compressed;
            using (MemoryStream inStream = new MemoryStream(SaveData))
            {
                using (MemoryStream outStream = new MemoryStream())
                {
                    lz11.Compress(inStream, SaveData.Length, outStream);
                    compressed = outStream.ToArray();
                }
            }
            using (FileStream fs = new FileStream(saveDialog.FileName, FileMode.Create, FileAccess.Write))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    bw.Write(Header);
                    bw.Write(compressed);
                }
            }
        }
Exemplo n.º 2
0
        public void openButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Binary files (*.bin)|*.bin|All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() != DialogResult.OK) return;

            // Open a decompressed save file, for testing purpose
            if (new FileInfo(openFileDialog.FileName).Length == 0x51FB8)
            {
                Header = null;
                SaveData = File.ReadAllBytes(openFileDialog.FileName);
                enableButtons();
                return;
            }

            byte[] rawSave = File.ReadAllBytes(openFileDialog.FileName);
            if (rawSave[0x58] != 0x11)
            {
                MessageBox.Show("Invalid save file", "Error");
                return;
            }
            byte[] compressed = new byte[rawSave.Length - 0x58];
            Array.Copy(rawSave, 0x58, compressed, 0, rawSave.Length - 0x58);

            // Decompress
            byte[] decompressed = null;
            LZ11 lz11 = new LZ11();
            using (MemoryStream inStream = new MemoryStream(compressed))
            {
                using (MemoryStream outStream = new MemoryStream())
                {
                    lz11.Decompress(inStream, compressed.Length, outStream);
                    if (outStream != null)
                        decompressed = outStream.ToArray();
                         
                    else
                    {
                        MessageBox.Show("Invalid save file", "Decompress Error");
                        return;
                    }
                }
                //only for testing
                /*
                using (FileStream fs = new FileStream("dec2.bin", FileMode.Create, FileAccess.Write))
                {
                    using (BinaryWriter bw = new BinaryWriter(fs))
                    {
                        
                        bw.Write(decompressed);
                    }
                }
                 */
                
            }

            // Check file validity and save loading
            if (decompressed.Length != saveLength_EUR)
            {
                MessageBox.Show("Invalid save file", "Error");
                return;
            }

            // Check decompressed save file size
            /*
            string filesize = Convert.ToString(decompressed.Length);
            MessageBox.Show(filesize, "bytesize");
             */
             
            Header = new byte[0x58];
            Array.Copy(rawSave, 0, Header, 0, 0x58);
            SaveData = decompressed;
            enableButtons();
        }        
Exemplo n.º 3
0
        private void openButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Binary files (*.bin)|*.bin|All files (*.*)|*.*";
            if (openFileDialog.ShowDialog() != DialogResult.OK) return;

            long length = (new FileInfo(openFileDialog.FileName)).Length;
            if (length > saveLength_EUR)
            {
                MessageBox.Show("Invalid save file", "Error");
                return;
            }

            // Open a decompressed save file, for testing purpose
            if (length == saveLength_EUR)
            {
                Header = null;
                SaveData = File.ReadAllBytes(openFileDialog.FileName);
                enableButtons();
                return;
            }

            Header = new byte[headerLength_EUR];
            byte[] compressed = new byte[length - headerLength_EUR];
            using (FileStream fs = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    br.BaseStream.Seek(headerLength_EUR, SeekOrigin.Begin);
                    byte isLZ11 = br.ReadByte();
                    if (isLZ11 != 0x11)
                    {
                        MessageBox.Show("Invalid save file", "Error");
                        return;
                    }
                    br.BaseStream.Seek(0x00, SeekOrigin.Begin);
                    br.Read(Header, 0x0, headerLength_EUR);
                    br.Read(compressed, 0x0, (int)length - headerLength_EUR);
                }
            }

            // Decompress
            byte[] decompressed = null;
            LZ11 lz11 = new LZ11();
            using (MemoryStream inStream = new MemoryStream(compressed))
            {
                using (MemoryStream outStream = new MemoryStream())
                {
                    lz11.Decompress(inStream, compressed.Length, outStream);
                    if (outStream != null)
                        decompressed = outStream.ToArray();
                    else
                    {
                        MessageBox.Show("Invalid save file", "Error");
                        return;
                    }

                }
            }

            // Check file validity and save loading
            if (decompressed.Length != saveLength_EUR)
            {
                labelRegion.Text = "USA";
                MessageBox.Show("Invalid save file", "Error");
                return;
            }
            SaveData = decompressed;
            labelRegion.Text = "EUR";
            enableButtons();
        }
Exemplo n.º 4
0
Arquivo: Main.cs Projeto: MetLob/tinke
        private static int CompressBest(string infile, MemoryStream output, out FormatCompress actualFormat, params FormatCompress[] formats)
        {
            // only read the input data once from the file.
            byte[] inputData;
            using (FileStream inStream = File.OpenRead(infile))
            {
                inputData = new byte[inStream.Length];
                inStream.Read(inputData, 0, inputData.Length);
            }

            MemoryStream bestOutput = null;
            int minCompSize = int.MaxValue;
            actualFormat = FormatCompress.GBA;
            foreach (FormatCompress format in formats)
            {
                #region compress the file in each format, and save the best one

                MemoryStream currentOutput = new MemoryStream();
                CompressionFormat realFormat = null;
                switch (format)
                {
                    case FormatCompress.HUFF4: Huffman.CompressBlockSize = Huffman.BlockSize.FOURBIT; realFormat = new Huffman(); break;
                    case FormatCompress.HUFF8: Huffman.CompressBlockSize = Huffman.BlockSize.EIGHTBIT; realFormat = new Huffman(); break;
                    case FormatCompress.LZ10: realFormat = new LZ10(); break;
                    case FormatCompress.LZ11: realFormat = new LZ11(); break;
                    case FormatCompress.LZOVL: realFormat = new LZOvl(); break;
                    case FormatCompress.RLE: realFormat = new RLE(); break;
                }

                int currentOutSize;
                try
                {
                    using (MemoryStream inStream = new MemoryStream(inputData))
                    {
                        currentOutSize = realFormat.Compress(inStream, inStream.Length, currentOutput);
                    }
                }
                catch (InputTooLargeException i)
                {
                    Console.WriteLine(i.Message);
                    actualFormat = format;
                    return -1;
                }
                catch (Exception)
                {
                    continue;
                }
                if (currentOutSize < minCompSize)
                {
                    bestOutput = currentOutput;
                    minCompSize = currentOutSize;
                    actualFormat = format;
                }

                #endregion
            }

            if (bestOutput == null)
            {
                Console.WriteLine(Main.Get_Traduction("S1A"));
                return -1;
            }
            bestOutput.WriteTo(output);
            return minCompSize;
        }
Exemplo n.º 5
0
Arquivo: Main.cs Projeto: MetLob/tinke
 private static long Decompress(MemoryStream inputStream, MemoryStream output, FormatCompress format)
 {
     CompressionFormat realFormat = null;
     switch (format)
     {
         case FormatCompress.HUFF:
             realFormat = new Huffman(); break;
         case FormatCompress.LZ10:
             realFormat = new LZ10(); break;
         case FormatCompress.LZ11:
             realFormat = new LZ11(); break;
         case FormatCompress.LZOVL:
             realFormat = new LZOvl(); break;
         case FormatCompress.RLE:
             realFormat = new RLE(); break;
         default:
             return -1;
     }
     if (!realFormat.Supports(inputStream, inputStream.Length))
         return -1;
     try
     {
         return realFormat.Decompress(inputStream, inputStream.Length, output);
     }
     catch (TooMuchInputException e)
     {
         Console.WriteLine(e.Message);
         return output.Length;
     }
     catch (Exception e)
     {
         Console.WriteLine(String.Format(Main.Get_Traduction("S1D"), format.ToString(), e.Message));
         return -1;
     }
 }
Exemplo n.º 6
0
Arquivo: Main.cs Projeto: MetLob/tinke
        public static FormatCompress Get_Format(FileStream input, bool arm9)
        {
            CompressionFormat fmt = null;

            foreach (FormatCompress f in Enum.GetValues(typeof(FormatCompress)))
            {
                switch (f)
                {
                    //case FormatCompress.LZOVL: fmt = new LZOvl(); break;
                    case FormatCompress.LZ10: fmt = new LZ10(); break;
                    case FormatCompress.LZ11: fmt = new LZ11(); break;
                    case FormatCompress.RLE: fmt = new RLE(); break;
                    case FormatCompress.HUFF: fmt = new Huffman(); break;
                }

                if (fmt == null)
                    continue;

                long fLength = input.Length;
                if (arm9)
                    fLength -= 0xC;

                if (fmt.Supports(input, fLength))
                    return f;
            }

            return FormatCompress.Invalid;
        }
Exemplo n.º 7
0
Arquivo: Main.cs Projeto: MetLob/tinke
        public static FormatCompress Get_Format(string input)
        {
            CompressionFormat fmt = null;

            foreach (FormatCompress f in Enum.GetValues(typeof(FormatCompress)))
            {
                switch (f)
                {
                    //case FormatCompress.LZOVL: fmt = new LZOvl(); break;
                    case FormatCompress.LZ10: fmt = new LZ10(); break;
                    case FormatCompress.LZ11: fmt = new LZ11(); break;
                    case FormatCompress.RLE: fmt = new RLE(); break;
                    case FormatCompress.HUFF: fmt = new Huffman(); break;
                }

                if (fmt == null)
                    continue;

                if (fmt.Supports(input))
                    return f;
            }

            return FormatCompress.Invalid;
        }
Exemplo n.º 8
0
Arquivo: Main.cs Projeto: MetLob/tinke
        public static int DoCompress(string infile, MemoryStream output, FormatCompress format, out FormatCompress actualFormat)
        {
            CompressionFormat fmt = null;
            switch (format)
            {
                case FormatCompress.LZ10: fmt = new LZ10(); break;
                case FormatCompress.LZ11: fmt = new LZ11(); break;
                case FormatCompress.LZOVL: fmt = new LZOvl(); break;
                case FormatCompress.RLE: fmt = new RLE(); break;
                case FormatCompress.HUFF4: Huffman.CompressBlockSize = Huffman.BlockSize.FOURBIT; fmt = new Huffman(); break;
                case FormatCompress.HUFF8: Huffman.CompressBlockSize = Huffman.BlockSize.EIGHTBIT; fmt = new Huffman(); break;
                case FormatCompress.HUFF:
                    return CompressHuff(infile, output, out actualFormat);
                case FormatCompress.GBA:
                    return CompressGBA(infile, output, out actualFormat);
                case FormatCompress.NDS:
                    return CompressNDS(infile, output, out actualFormat);
                default:
                    actualFormat = FormatCompress.Invalid;
                    return -1;
            }
            actualFormat = format;

            using (FileStream inStream = File.OpenRead(infile))
            {
                try
                {
                    return fmt.Compress(inStream, inStream.Length, output);
                }
                catch (Exception s)
                {
                    // any exception generated by compression is a fatal exception
                    Console.WriteLine(s.Message);
                    return -1;
                }
            }
        }
Exemplo n.º 9
0
        private static int DoCompress(string infile, MemoryStream output, Formats format, out Formats actualFormat)
        {
            CompressionFormat fmt = null;
            switch (format)
            {
                case Formats.LZ10: fmt = new LZ10(); break;
                case Formats.LZ11: fmt = new LZ11(); break;
                case Formats.LZOVL: fmt = new LZOvl(); break;
                case Formats.RLE: fmt = new RLE(); break;
                case Formats.HUFF4: fmt = new Huffman4(); break;
                case Formats.HUFF8: fmt = new Huffman8(); break;
                case Formats.HUFF:
                    return CompressHuff(infile, output, out actualFormat);
                case Formats.GBA:
                    return CompressGBA(infile, output, out actualFormat);
                case Formats.NDS:
                    return CompressNDS(infile, output, out actualFormat);
                default:
                    throw new Exception("Unhandled compression format " + format);
            }
            actualFormat = format;

            using (FileStream inStream = File.OpenRead(infile))
            {
                try
                {
                    return fmt.Compress(inStream, inStream.Length, output);
                }
                catch (Exception s)
                {
                    // any exception generated by compression is a fatal exception
                    Console.WriteLine(s.Message);
                    return -1;
                }
            }
        }
Exemplo n.º 10
0
        private static int CompressBest(string infile, MemoryStream output, out Formats actualFormat, params Formats[] formats)
        {
            // only read the input data once from the file.
            byte[] inputData;
            using (FileStream inStream = File.OpenRead(infile))
            {
                inputData = new byte[inStream.Length];
                inStream.Read(inputData, 0, inputData.Length);
            }

            MemoryStream bestOutput = null;
            int minCompSize = int.MaxValue;
            actualFormat = Formats.GBA;
            foreach (Formats format in formats)
            {
                #region compress the file in each format, and save the best one

                MemoryStream currentOutput = new MemoryStream();
                CompressionFormat realFormat = null;
                switch (format)
                {
                    case Formats.HUFF4: realFormat = new Huffman4(); break;
                    case Formats.HUFF8: realFormat = new Huffman8(); break;
                    case Formats.LZ10: realFormat = new LZ10(); break;
                    case Formats.LZ11: realFormat = new LZ11(); break;
                    case Formats.LZOVL: realFormat = new LZOvl(); break;
                    case Formats.RLE: realFormat = new RLE(); break;
                    default:
                        Console.WriteLine("Unsupported single format: "+format);
                        continue;
                }

                int currentOutSize;
                try
                {
                    using (MemoryStream inStream = new MemoryStream(inputData))
                    {
                        currentOutSize = realFormat.Compress(inStream, inStream.Length, currentOutput);
                    }
                }
                catch (InputTooLargeException i)
                {
                    Console.WriteLine(i.Message);
                    actualFormat = format;
                    return -1;
                }
                catch (Exception)
                {
                    continue;
                }
                if (currentOutSize < minCompSize)
                {
                    bestOutput = currentOutput;
                    minCompSize = currentOutSize;
                    actualFormat = format;
                }

                #endregion
            }

            if (bestOutput == null)
            {
                Console.WriteLine("The file could not be compressed in any format.");
                return -1;
            }
            bestOutput.WriteTo(output);
            return minCompSize;
        }