Example #1
0
        private static void ExtractFiles(FileList fileList, IFileWriter writer)
        {
            Console.WriteLine($"Extracting {fileList.Name}");

            writer.CreateDirectory(fileList.Name);

            foreach (FileData file in fileList.GetFiles())
            {
                if (file.Compressed)
                {
                    using (var compressed = new MemoryStream(file.Contents))
                    {
                        using (var decompressed = new MemoryStream())
                        {
                            LZSS.Decompress(compressed, decompressed);
                            file.Contents = decompressed.ToArray();
                        }
                    }
                }

                if (file.IsArchive())
                {
                    ExtractFiles(new ArchiveFileList(Path.Combine(fileList.Name, file.Name), file.Contents), writer);
                }
                else
                {
                    writer.Write(Path.Combine(fileList.Name, $"{file.Name}.{file.GetExtension()}"), file.Contents);
                }
            }
        }
Example #2
0
 private void tabMain_DragDrop(object sender, string path)
 {
     if (sender == PB_Unpack)
     {
         openARC(path, pBar1);
     }
     else if (sender == PB_BCLIM)
     {
         openIMG(path);
     }
     else if (sender == PB_Repack)
     {
         saveARC(path);
     }
     else
     {
         try {
             LZSS.Decompress(path, Path.Combine(Path.GetDirectoryName(path), "dec_" + Path.GetFileName(path)));
             File.Delete(path);
             System.Media.SystemSounds.Asterisk.Play();
         } catch { try { if (threads < 1)
                         {
                             new Thread(() => { threads++; new BLZCoder(new[] { "-d", path }, pBar1); threads--; WinFormsUtil.Alert("Decompressed!"); }).Start();
                         }
                   } catch { WinFormsUtil.Error("Unable to process file."); threads = 0; } }
     }
 }
Example #3
0
            public void DecompressSimpleText()
            {
                var inputPath    = TestContext.CurrentContext.TestDirectory + "../../../res/compressedTestFile";
                var expectedPath = TestContext.CurrentContext.TestDirectory + "../../../res/testfile2";
                var expectedFile = new DataFile(expectedPath);
                var inputFile    = new DataFile(inputPath);

                var comp       = new LZSS();
                var actualFile = comp.Decompress(inputFile);

                Assert.AreEqual(expectedFile.GetBytes(0, expectedFile.Length),
                                actualFile.GetBytes(0, actualFile.Length));
                ;
            }
Example #4
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                return;
            }

            using (FileStream input = new FileStream(args[0], FileMode.Open))
            {
                using (FileStream output = new FileStream(args[0] + "_decompressed", FileMode.Create))
                {
                    LZSS.Decompress(input, output);
                }
            }
        }
Example #5
0
        private void button_decompDbg_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "binary dump|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                byte[] dump             = File.ReadAllBytes(openFileDialog.FileName);
                byte[] decompDump       = LZSS.Decompress(dump);
                var    outputFolderPath = Path.GetDirectoryName(openFileDialog.FileName);

                //Decompressed file
                using (FileStream fs = File.Create(outputFolderPath + "//" + "decompressed_blob.bin"))
                {
                    fs.Write(decompDump, 0, decompDump.Length);
                }
            }
        }
Example #6
0
 private void DecompressLZSS_BLZ(string path)
 {
     try
     {
         LZSS.Decompress(path, Path.Combine(Path.GetDirectoryName(path), "dec_" + Path.GetFileName(path)));
         File.Delete(path);
     }
     catch
     {
         try
         {
             if (threads < 1)
             {
                 new Thread(() => { Interlocked.Increment(ref threads); new BLZCoder(new[] { "-d", path }, pBar1); Interlocked.Decrement(ref threads); WinFormsUtil.Alert("Decompressed!"); }).Start();
             }
         }
         catch { WinFormsUtil.Error("Unable to process file."); threads = 0; }
     }
 }
Example #7
0
        private static void Decompress(string filename, Stream file)
        {
            if (file.ReadUInt() != Version)
            {
                Console.WriteLine("Unknown GTZ version");
                return;
            }

            uint compressedSize = file.ReadUInt();

            if (file.Length != compressedSize + 16)
            {
                Console.WriteLine("Incorrect file size");
                return;
            }

            uint uncompressedSize = file.ReadUInt();

            using (var compressed = new MemoryStream())
            {
                file.CopyTo(compressed);
                compressed.Position = 0;
                using (var decompressed = new MemoryStream())
                {
                    LZSS.Decompress(compressed, decompressed);

                    if (decompressed.Length < uncompressedSize)
                    {
                        Console.WriteLine("Decompressed data too short");
                        return;
                    }

                    decompressed.Position = 0;
                    decompressed.SetLength(uncompressedSize);
                    filename = filename.EndsWith(Extension) ? filename.Replace(Extension, "") : $"decompressed_{filename}";
                    using (var output = new FileStream(filename, FileMode.Create, FileAccess.Write))
                    {
                        decompressed.CopyTo(output);
                    }
                }
            }
        }
Example #8
0
        public static LPack FromData(byte[] Data)
        {
            if (Data.Length < 0x10)
            {
                return(null);
            }

            LPack Output = new LPack();

            using (MemoryStream Input = new MemoryStream(Data))
            {
                BinaryReader Reader = new BinaryReader(Input);

                uint FilesCount    = Reader.ReadUInt32();
                uint FileDecLength = Reader.ReadUInt32();

                //Simple validity check
                uint FirstAddr = 8 + FilesCount * 8;
                Input.Seek(4, SeekOrigin.Current);
                if (Reader.ReadUInt32() != FirstAddr)
                {
                    return(null);
                }

                Output.Files = new PackFile[FilesCount];

                for (int i = 0; i < FilesCount; i++)
                {
                    Input.Seek(8 + i * 8, SeekOrigin.Begin);

                    uint DecLength = Utils.ReadUInt24(Reader);
                    byte FileId    = Reader.ReadByte();
                    uint Address   = Reader.ReadUInt32();

                    Output.Files[i].FileId = FileId;
                    Output.Files[i].Data   = LZSS.Decompress(Data, Address, DecLength);
                }
            }

            return(Output);
        }
Example #9
0
        public TitleScreenEditor6()
        {
            InitializeComponent();
            AllowDrop           = true;
            DragEnter          += TC_Main_DragEnter;
            DragDrop           += TC_Main_DragDrop;
            PB_Image.AllowDrop  = true;
            PB_Image.DragEnter += TC_Main_DragEnter;
            PB_Image.DragDrop  += TC_Main_DragDrop;

            // Add tooltip to image
            new ToolTip().SetToolTip(PB_Image, "Click to toggle Green Screen\nRightClick for I/O\nCTRL+Click for Copy->Clipboard.");

            // Add context menus
            ContextMenuStrip  mnu  = new ContextMenuStrip();
            ToolStripMenuItem mnuR = new ToolStripMenuItem("Replace with...");
            ToolStripMenuItem mnuS = new ToolStripMenuItem("Save as...");

            // Assign event handlers
            mnuR.Click += ClickOpen;
            mnuS.Click += ClickSave;
            // Add to main context menu
            mnu.Items.AddRange(new ToolStripItem[] { mnuR, mnuS, });

            // Assign
            PB_Image.ContextMenuStrip = mnu;

            // Set up languages
            string[] languages = (Main.Config.ORAS ? new[] { "JP1" } : Array.Empty <string>()).Concat(new[] { "DE", "ES", "FR", "IT", "JP", "KO", "EN" }).ToArray();
            string[] games     = Main.Config.ORAS ? new[] { "OR", "AS" } : new[] { "X", "Y" };
            for (int i = 0; i < darcs.Length / 2; i++)
            {
                CB_DARC.Items.Add($"{games[0]} - {languages[i]}");
            }
            for (int i = darcs.Length / 2; i < darcs.Length; i++)
            {
                CB_DARC.Items.Add($"{games[1]} - {languages[i - (darcs.Length/2)]}");
            }

            // Load darcs
            for (int i = 0; i < darcs.Length; i++)
            {
                // Get DARC name and assign the decompressed name
                usedFiles[i] = "titlescreen\\" + (compressed ? "dec_" : "") + Path.GetFileName(files[darcFiles[i]]);
                if (compressed) // Decompress file (XY does not compress)
                {
                    LZSS.Decompress(files[darcFiles[i]], usedFiles[i]);
                }
                // Read decompressed file
                var data = File.ReadAllBytes(usedFiles[i]);

                // Find darc data offset (ignore header)
                int pos = 0;
                while (BitConverter.ToUInt32(data, pos) != 0x63726164)
                {
                    pos += 4;
                    if (pos >= data.Length)
                    {
                        throw new Exception("Invalid DARC?\n\n" + usedFiles[i]);
                    }
                }
                var darcData = data.Skip(pos).ToArray();
                darcs[i] = new DARC(darcData);
            }

            CB_DARC.SelectedIndex = CB_DARC.Items.Count - 1; // last (english game2)
        }
Example #10
0
        private void button_Decompress_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "LoGH Containers|*.mvx;*.arc";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string      outputFolderPath = openFileDialog.FileName + "_";
                GenericInfo tmpGeneric       = new GenericInfo
                {
                    ContainerName = openFileDialog.FileName,
                    ContainerType = Path.GetExtension(openFileDialog.FileName)
                };

                ARCHeader     tmpHeader = new ARCHeader();
                List <ARCToc> tmpToc    = new List <ARCToc>();

                using (BinaryReader reader = new BinaryReader(new FileStream(openFileDialog.FileName, FileMode.Open)))
                {
                    //Parse Header
                    reader.BaseStream.Seek(0, SeekOrigin.Begin);
                    tmpHeader.HeaderIdentifier     = reader.ReadBytes(4);
                    tmpHeader.FileReaderBufferSize = reader.ReadByte();
                    tmpHeader.UnknownType          = reader.ReadByte();

                    reader.BaseStream.Seek(3, SeekOrigin.Current); //0x00 three times

                    tmpHeader.UnknownValue1   = reader.ReadByte();
                    tmpHeader.TocPointer2     = reader.ReadByte();
                    tmpHeader.UnknownValue2   = reader.ReadByte();
                    tmpHeader.UnknownType2    = reader.ReadByte();
                    tmpHeader.FileCount       = reader.ReadByte();
                    tmpHeader.UnknownValue3   = reader.ReadByte();
                    tmpHeader.CompressionType = reader.ReadByte();

                    tmpHeader.HeaderLeftover = reader.ReadBytes(112);

                    //Begin of TOC
                    reader.BaseStream.Seek(2, SeekOrigin.Current); //skip two 0x00
                    for (int i = 1; i <= tmpHeader.FileCount; i++)
                    {
                        long   entryOffset = reader.BaseStream.Position;
                        ARCToc localToc    = new ARCToc();

                        for (int j = 1; j <= 5; j++)
                        {
                            var VAL1 = reader.ReadByte();
                            reader.BaseStream.Seek(6, SeekOrigin.Current);
                            var VAL2 = reader.ReadByte();
                            reader.BaseStream.Seek(6, SeekOrigin.Current);
                            var VAL3 = reader.ReadByte();
                            reader.BaseStream.Seek(6, SeekOrigin.Current);
                            var VAL4 = reader.ReadByte();
                            reader.BaseStream.Seek(6, SeekOrigin.Current);

                            var tmpVal = (VAL1 | (VAL2 << 8) | (VAL3 << 16) | (VAL4 << 24));

                            switch (j)
                            {
                            case 1:
                                localToc.NameOffset = tmpVal;
                                break;

                            case 2:
                                localToc.NameLength = tmpVal;
                                break;

                            case 3:
                                localToc.Offset = tmpVal;
                                break;

                            case 4:
                                localToc.Zsize = tmpVal;
                                break;

                            case 5:
                                localToc.Size = tmpVal;
                                break;

                            default:
                                throw new Exception("NOPE!");
                            }

                            entryOffset += 1;
                            reader.BaseStream.Seek(entryOffset, SeekOrigin.Begin);
                        }

                        //Readout name string
                        long preOffset = reader.BaseStream.Position;
                        reader.BaseStream.Seek(localToc.NameOffset, SeekOrigin.Begin);
                        localToc.NameBytes = reader.ReadBytes(localToc.NameLength);
                        localToc.Name      = new ASCIIEncoding().GetString(localToc.NameBytes, 0, localToc.NameLength - 1); //-1 because we dont want the null terminator

                        //Read DATA (offset since file begin)
                        reader.BaseStream.Seek(localToc.Offset, SeekOrigin.Begin);
                        localToc.Data      = reader.ReadBytes(localToc.Zsize);
                        localToc.OffsetEnd = (int)reader.BaseStream.Position;

                        //Reset Seek for next header file
                        reader.BaseStream.Seek(preOffset, SeekOrigin.Begin);

                        //Add toc to list
                        tmpToc.Add(localToc);

                        //Everything is ready to readout compressed data
                        if (localToc.Zsize == localToc.Size)
                        {
                            //Content is unencrypted... passtrough directly to filewriter
                            localToc.DataDecompressed = localToc.Data;
                        }
                        else
                        {
                            //decompress with lzss
                            localToc.DataDecompressed = LZSS.Decompress(localToc.Data);
                        }

                        reader.BaseStream.Seek(27, SeekOrigin.Current);
                    }
                }

                //PHASE 2: Export data from memory to Filesystem
                DirectoryInfo di = Directory.CreateDirectory(outputFolderPath);
                foreach (var toc in tmpToc)
                {
                    //Check name for sub-folders - when there are some - create
                    Directory.CreateDirectory(Path.GetDirectoryName(outputFolderPath + "//" + toc.Name));

                    //Decompressed file
                    using (FileStream fs = File.Create(outputFolderPath + "//" + toc.Name))
                    {
                        fs.Write(toc.DataDecompressed, 0, toc.DataDecompressed.Length);
                    }

                    //Compressed file
                    using (FileStream fs = File.Create(outputFolderPath + "//" + toc.Name + "_COMP"))
                    {
                        fs.Write(toc.Data, 0, toc.Data.Length);
                    }
                }

                SerializeCollection tmpSer = new SerializeCollection
                {
                    generic   = tmpGeneric,
                    arcHeader = tmpHeader,
                    arcToc    = tmpToc
                };

                //Serialize header & toc
                IFormatter formatter = new BinaryFormatter();
                Stream     stream    = new FileStream(outputFolderPath + "//_header.bin", FileMode.Create, FileAccess.Write, FileShare.None);
                //GZipStream gzip = new GZipStream(stream, CompressionLevel.Fastest);
                formatter.Serialize(stream, tmpSer);
                stream.Close();
            }
        }
Example #11
0
        static void Main(string[] args)
        {
            Console.Title = "MBuild - A Marvelous Translation and Hacking Tool";

            Console.WriteLine("MBuild - " + Application.ProductVersion.ToString().Split('.')[0] + "." + Application.ProductVersion.ToString().Split('.')[1]);
            Console.WriteLine("A Marvelous Translation and Hacking Tool");
            Console.WriteLine("Written by DackR aka Daniel Martin Burgess\r\n");

            if (args.Length == 0)
            {
                Console.WriteLine("No Arguments specified.");
                UseXML("MBuild.MBXML", !Settings.Default.AutoBuild);
            }
            else
            {
                string mArg = args[0].Trim().ToLower();
                switch (mArg)
                {
                case "build":
                    BuildXML(args);
                    break;

                case "dump-script":
                    DumpScript(args);
                    break;

                case "bin-script":
                    BuildScript(args);
                    break;

                case "comp":
                    CompressFile(args);
                    break;

                case "decomp":
                    DeCompressFile(args);
                    break;

                case "bpp-convert":
                    //BuildScript(args);
                    break;

                case "dmpptr":
                    DumpPointers();
                    break;

                case "fixsum":
                    FixCheckSum(args);
                    break;

                case "mbxml-shell":
                    string[] makeArg = new string[] { "-f", "-t", ".mbxml", "-si", Application.ExecutablePath, "-a", Application.ExecutablePath, "-sn", "MBXML File", "-sc", "MBuild_XML_File" };
                    //string[] assocArg = new string[] { "-f", "-t", ".txt", "-associate", Application.ExecutablePath };
                    CTypes.Main(makeArg);
                    //CTypes.Main(assocArg);
                    //FileAssociation.SetAssociation(".mbxml", "MBuild_XML_File", Application.ExecutablePath, "MBXML File", args);
                    break;

                case "ips":
                    MakeIPS(args);
                    break;

                case "xdelta":
                    MakexDelta(args);
                    break;

                case "extract":    //used to extract RAW binary data from a file
                    ExtractBin(args);
                    break;

                case "dmpdata":
                    //Dump data located at the various pointers
                    DumpData(args);
                    break;

                case "bm5dump":
                    SBM5.DumpDataFromPointerTab(@"F:\GoogleDrive\SBM5\Base\Base.sfc", @"F:\GoogleDrive\SBM5\Dump\");
                    break;

                case "test":
                    string       romfile = @"D:\GoogleDrive\SuperFamicomWars\base.sfc";
                    byte[]       data    = File.ReadAllBytes(romfile);
                    MemoryStream ms      = LZSS.Decompress(data);
                    byte[]       dcdata  = ms.ToArray();
                    break;

                default:
                    if (mArg.ToLower().Contains(".mbxml") && File.Exists(mArg))
                    {    //detected xml file specified... Try to build.
                        UseXML(mArg, false);
                        //Console.WriteLine(mArg);
                        //Console.ReadKey(true);
                    }
                    break;
                }
            }
            LCompress.LunarCompressCleanup();
            xDelta.xDeltaCleanup();
            bool wait = false;

            Console.Write("Press any key to exit (\"q\" to stop count-down)");
            DateTime beginWait = DateTime.Now;

            while (!Console.KeyAvailable && DateTime.Now.Subtract(beginWait).TotalSeconds < 3)
            {
                Console.Write(".");
                Thread.Sleep(250);
            }
            if (Console.KeyAvailable && (Console.ReadKey(true).Key == ConsoleKey.Q))
            {
                wait = true;
            }
            if (wait)
            {
                Console.CursorLeft = 0;
                Console.Write(("").PadLeft(79, ' '));
                Console.CursorLeft = 0;
                Console.Write("Press any key to exit...");
                Console.ReadKey(true);
            }
        }
Example #12
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.LZSSVLE:
                            outFs.Write(LZSSVLE.Decompress(openFile));
                            break;

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

                        case Compression.LZECD:
                            outFs.Write(LZECD.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);
        }