Beispiel #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Generate ANSI table ...");

            ANSI ascii = new ANSI();

            ascii.WriteToFile();

            Console.WriteLine("ANSI table generated.");

            Console.WriteLine("Start encoding " + fileToCompress + " ...");

            string     text    = File.ReadAllText(fileToCompress, System.Text.ASCIIEncoding.Default);
            LZWEncoder encoder = new LZWEncoder();

            byte[] b = encoder.EncodeToByteList(text);
            File.WriteAllBytes(encodedFile, b);

            Console.WriteLine("File " + fileToCompress + " encoded to " + encodedFile);

            Console.WriteLine("Start decoding " + encodedFile);

            LZWDecoder decoder = new LZWDecoder();

            byte[] bo            = File.ReadAllBytes(encodedFile);
            string decodedOutput = decoder.DecodeFromCodes(bo);

            File.WriteAllText(decodedFile, decodedOutput, System.Text.Encoding.Default);

            Console.WriteLine("File " + encodedFile + " decoded to " + decodedFile);
        }
Beispiel #2
0
        private void btnUnpack_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();

            opf.Filter = "*.lzw|*.lzw";

            if (opf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtPath.Text = opf.FileName;

                var        buffer  = File.ReadAllBytes(opf.FileName);
                LZWDecoder decoder = new LZWDecoder();

                var text = decoder.DecodeFromCodes(buffer);
                File.WriteAllText(opf.FileName.Replace(".lzw", ""), text, System.Text.Encoding.Default);

                MessageBox.Show("Decoding complete.");
            }
        }
Beispiel #3
0
        private void Decompress(object sender, RoutedEventArgs e)
        {
            if (!filenameCom.fileIsEmpty())
            {
                string noteCompressed   = @"D:\RealEncAndCom\noteCompressed.txt";
                string noteDecompressed = @"D:\RealEncAndCom\noteDecompressed" + fileExtension;

                File.Delete(noteDecompressed);
                LZWDecoder decoder = new LZWDecoder();

                byte[] bo            = File.ReadAllBytes(noteCompressed);
                string decodedOutput = decoder.DecodeFromCodes(bo);
                File.WriteAllText(noteDecompressed, decodedOutput, System.Text.Encoding.Default);
                decomLabel1.Content = "Файл был распакован!";
            }
            else
            {
                decomLabel1.Content = "Файл не был распокван!";
            }
        }
Beispiel #4
0
        public static void Do()
        {
            Console.WriteLine("Generate ANSI table ...");
            var ascii = new Ansi();

            ascii.WriteToFile();
            Console.WriteLine("ANSI table generated.");
            Console.WriteLine("Start encoding " + _fileToCompress + " ...");
            var text    = File.ReadAllText(_fileToCompress);
            var encoder = new LZWEncoder();
            var b       = encoder.EncodeToByteList(text);

            File.WriteAllBytes(_encodedFile, b);
            Console.WriteLine("File " + _fileToCompress + " encoded to " + _encodedFile);
            Console.WriteLine("Start decoding " + _encodedFile);
            var decoder       = new LZWDecoder();
            var bo            = File.ReadAllBytes(_encodedFile);
            var decodedOutput = decoder.DecodeFromCodes(bo);

            File.WriteAllText(_decodedFile, decodedOutput, Encoding.Default);
            Console.WriteLine("File " + _encodedFile + " decoded to " + _decodedFile);
        }
Beispiel #5
0
        static List <HashTableElement> LoadFile(string path, int fileId)
        {
            using (var br = new BinaryReader(File.OpenRead(path)))
            {
                if ("ARCH000" != Encoding.ASCII.GetString(br.ReadBytes(7)))
                {
                    Console.WriteLine("Not a TBAR file");
                    Environment.Exit(1);
                }

                bool  compressed = br.ReadByte() == 1;
                int   offset     = br.ReadInt32();
                short amount     = br.ReadInt16();

                br.BaseStream.Position = offset;

                List <HashTableElement> elements = new List <HashTableElement>(amount);

                for (var i = 0; i < amount; i++)
                {
                    var element = new HashTableElement();
                    element.fileId       = fileId;
                    element.elementId    = br.ReadInt32();
                    element.specialHash  = br.ReadUInt32();
                    element.offsetInFile = br.ReadInt32();
                    element.sizeInFile   = br.ReadInt32();
                    element.checksum     = br.ReadInt32();

                    elements.Add(element);
                }

                for (var i = 0; i < amount; i++)
                {
                    var element = elements[i];
                    br.BaseStream.Position = element.offsetInFile;


                    element.contents = br.ReadBytes(element.sizeInFile);
                    elements[i]      = element;

                    byte[] cleanData;

                    if (compressed)
                    {
                        uint hur = (uint)(element.elementId);
                        //if (hur != (uint)402372392)
                        //    continue;
                        File.WriteAllBytes(Path.Combine("output", hur + "_RAW.bin"), element.contents);

                        cleanData = new byte[4];
                        int        cleanDataOffset = 0;
                        LZWDecoder decoder         = new LZWDecoder();

                        bool   nosave    = false;
                        string buffertje = "";

                        for (var j = 0; j < element.sizeInFile;)
                        {
                            try
                            {
                                int len = BitConverter.ToInt32(element.contents, j);
                                j += 4;
                                byte[] blah = new byte[len];
                                Console.WriteLine("Reading blob {0} - {1}", j, j + len);

                                Buffer.BlockCopy(element.contents, j, blah, 0, len);
                                j += len;

                                var output = decoder.DecodeFromCodes(blah);
                                buffertje += output;

                                if (cleanData.Length < (cleanDataOffset + output.Length))
                                {
                                    Array.Resize <byte>(ref cleanData, cleanDataOffset + output.Length);
                                }

                                for (var k = 0; k < output.Length; k++)
                                {
                                    cleanData[cleanDataOffset++] = (byte)output[k];
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.ToString());
                                nosave = true;
                                break;
                            }
                            //Console.WriteLine(output);
                            //File.AppendAllText(filename, output);
                            //break;
                        }

                        //if (nosave)
                        //    continue;


                        File.WriteAllText(Path.Combine("output", hur + "_text.txt"), buffertje);
                    }
                    else
                    {
                        cleanData = new byte[element.sizeInFile];
                        uint derp = (uint)(element.specialHash ^ 0xF9524287 | 1);

                        for (var j = 0; j < element.sizeInFile; j += 1024)
                        {
                            var blobsize = Math.Min(element.sizeInFile - j, 1024);

                            byte[] blah = new byte[1024];
                            Buffer.BlockCopy(element.contents, j, blah, 0, blobsize);

                            derp = Crypto(derp, blah);
                            Buffer.BlockCopy(blah, 0, cleanData, j, blobsize);
                        }
                    }

                    string ext = ".bin";

                    if (cleanData[0] == 0x89 && cleanData[1] == 0x50 && cleanData[2] == 0x4E && cleanData[3] == 0x47)
                    {
                        ext = ".png";
                    }
                    else if (cleanData[0] == 0x4F && cleanData[1] == 0x67 && cleanData[2] == 0x67)
                    {
                        ext = ".ogg";
                    }
                    else if (cleanData[0] == 0x42 && cleanData[1] == 0x4D && cleanData[2] == 0x46)
                    {
                        ext = ".bmf";
                    }
                    else if (cleanData[0] == 0x52 && cleanData[1] == 0x49 && cleanData[2] == 0x46 && cleanData[3] == 0x46)
                    {
                        ext = ".wav";
                    }

                    if (compressed)
                    {
                        ext = "_compressed" + ext;
                    }

                    var filename = Path.Combine("output", (uint)(element.elementId) + ext);
                    File.WriteAllBytes(filename, cleanData);
                }

                return(elements);
            }
        }