Example #1
0
        private void extractToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode      node = this.resourceView.SelectedNode;
            SCI32Resource res  = null;

            try
            {
                if (this.ResourceDirectories.TryGetResourceByFilename(node.Name, out res))
                {
                    string ext = SCI32Resource.ExtentionFromResourceType(res.ResourceType);
                    saveFileDialog1.Filter   = string.Format("SCI32 {0}|{1}", res.ResourceType, ext);
                    saveFileDialog1.FileName = res.FileName;
                    DialogResult result = saveFileDialog1.ShowDialog();
                    if (result == System.Windows.Forms.DialogResult.OK)
                    {
                        byte[] file = new byte[res.FileData.Length - 8];
                        Array.Copy(res.FileData, 8, file, 0, file.Length);
                        string _file = Encoding.Default.GetString(file);
                        try
                        {
                            bool compresed = res.CompressedLength != res.UnCompressedLength;
                            if (compresed)
                            {
                                LZWDecoder decoder = new LZWDecoder();
                                string     sfile   = decoder.Decode(_file);
                                //file = Encoding.ASCII.GetBytes(sfile);
                            }
                            File.WriteAllBytes(saveFileDialog1.FileName, file);
                        }
                        catch (Exception ex) { MessageBox.Show(string.Format("Unable to save file {0}, Error was {1}", res.FileName, ex.Message)); }
                    }
                }
            }
            catch { }
        }
        /// <summary>
        /// Reads from the specified stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="descriptor">The descriptor.</param>
        /// <returns>
        /// The resulting FrameIndices object
        /// </returns>
        public static FrameIndices Read(Stream stream, ImageDescriptor descriptor)
        {
            var DataSize = stream.ReadByte();
            var Decoder  = new LZWDecoder(stream);

            return(new FrameIndices(Decoder.Decode(descriptor.Width, descriptor.Height, DataSize), 0));
        }
Example #3
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);
        }
Example #4
0
        public static byte[] decompress(byte[] compressed, CompressionMethod compId, int expectedByteCount)
        {
            switch (compId)
            {
            case CompressionMethod.UNCOMPRESSED:
                return(compressed);

            case CompressionMethod.LZW:
                var    lzwDecoder   = new LZWDecoder();
                string decompressed = lzwDecoder.Decode(compressed);
                byte[] bytes        = new byte[expectedByteCount];
                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = (byte)decompressed[i];
                }
                return(bytes);

            case CompressionMethod.DEFLATE:
            case CompressionMethod.ADOBE_DEFLATE:
                return(deflateDecompress(compressed, expectedByteCount));

            case CompressionMethod.PACKBITS:
                return(packbitsDecompress(compressed, expectedByteCount));

            default:
                throw new ReadFileException("Unsupported comnpression");
            }
        }
Example #5
0
        public static void Decode(string arquivo)
        {
            //Carrega arquivo comprimido
            byte[] dados = Arquivo.AbrirEmBytes(arquivo);

            BitArray bits = new BitArray(dados);

            Console.WriteLine("Iniciada a decompressão");
            DateTime dataInicial = DateTime.Now;

            HuffmanTree huffman = new HuffmanTree();

            //Decodificando a árvore huffman
            string huffmanDecoded = huffman.Decode(bits);

            LZWDecoder lZWDecoder = new LZWDecoder();

            //Decodificando LZW
            string conteudo = lZWDecoder.Decode(huffmanDecoded);

            DateTime dataFinal = DateTime.Now;

            string tempoDecorrido = (dataFinal - dataInicial).TotalSeconds.ToString("N2");

            //Contabiliza apenas os tempos para decompressão. Igora tempo de IO
            Console.WriteLine($"Arquivo descomprimido em {tempoDecorrido} segundos");

            Arquivo.Gravar(conteudo, $"{arquivo.Split('.')[0]} - Descomprimido.txt");
        }
Example #6
0
            private byte[] ReadFrameIndices(GifImageDescriptor imageDescriptor)
            {
                int dataSize = _stream.ReadByte();

                LZWDecoder lzwDecoder = new LZWDecoder(_stream);

                byte[] indices = lzwDecoder.DecodePixels(imageDescriptor.Width, imageDescriptor.Height, dataSize);
                return(indices);
            }
Example #7
0
        public static void decode(string filename)
        {
            var fileContent  = FileWrapper.openInBytes(filename);
            var deltaDecoded = Delta.decode(fileContent);
            var decoder      = new LZWDecoder();
            var lzwDecoded   = decoder.Decode(deltaDecoded);

            FileWrapper.write(lzwDecoded, getDecodedFilename(filename));
        }
Example #8
0
        public override byte[] Decompress(Stream s, int dataLen, uint key)
        {
            var sb      = new List <byte>();
            var decoder = new LZWDecoder();

            while (sb.Count < dataLen)
            {
                sb.AddRange(decoder.Decode(s.ReadBytes(s.ReadInt32())));
            }

            return(sb.ToArray());
        }
        public override byte[] Decompress(Stream s, int dataLen, uint key)
        {
            var sb = new List<byte>();
            var decoder = new LZWDecoder();

            while (sb.Count < dataLen)
            {
                sb.AddRange(decoder.Decode(s.ReadBytes(s.ReadInt32())));
            }

            return sb.ToArray();
        }
Example #10
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.");
            }
        }
Example #11
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 = "Файл не был распокван!";
            }
        }
Example #12
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);
        }
Example #13
0
 public static byte[] LzwDecode(byte[] data)
 {
     try
     {
         LZWDecoder lzw = new LZWDecoder();
         MemoryStream stream = new MemoryStream();
         lzw.Decode(data, stream);
         return stream.GetBuffer();
     }
     catch (Exception e)
     {
         MessageBox.Show(e.ToString() );
     }
     return new byte[0];
 }
Example #14
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);
            }
        }
Example #15
0
 static void ReadImage(StreamHelper streamHelper, Stream fs, GifImage gifImage, List<GraphicEx> graphics, int frameCount)
 {
     ImageDescriptor imgDes = streamHelper.GetImageDescriptor(fs);
     GifFrame frame = new GifFrame();
     frame.ImageDescriptor = imgDes;
     frame.LocalColorTable = gifImage.GlobalColorTable;
     if (imgDes.LctFlag)
     {
         frame.LocalColorTable = streamHelper.ReadByte(imgDes.LctSize*3);
     }
     LZWDecoder lzwDecoder = new LZWDecoder(fs);
     int dataSize = streamHelper.Read();
     frame.ColorDepth = dataSize;
     byte[] piexel = lzwDecoder.DecodeImageData(imgDes.Width, imgDes.Height, dataSize);
     frame.IndexedPixel = piexel;
     int blockSize = streamHelper.Read();
     DataStruct data = new DataStruct(blockSize, fs);
     GraphicEx graphicEx = graphics[frameCount];
     frame.GraphicExtension = graphicEx;
     Bitmap img = GetImageFromPixel(piexel, frame.Palette, imgDes.InterlaceFlag, imgDes.Width, imgDes.Height);
     frame.Image = img;
     gifImage.Frames.Add(frame);
 }
Example #16
0
            private byte[] ReadFrameIndices(GifImageDescriptor imageDescriptor)
            {
                int dataSize = _stream.ReadByte();

                LZWDecoder lzwDecoder = new LZWDecoder(_stream);

                byte[] indices = lzwDecoder.DecodePixels(imageDescriptor.Width, imageDescriptor.Height, dataSize);
                return indices;
            }