Example #1
0
File: Utils.cs Project: biuken/eAnt
        private void DecompresNowZip()
        {
            ICSharpCode.SharpZipLib.Zip.Compression.Inflater def =
                new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(true);

            byte[] data = new byte[50000];
            int    size = 0;

            try
            {
                def.SetInput(this.m_byteinput);
                size = def.Inflate(data);
                if (size == 0)
                {
                    while (def.IsFinished == false)
                    {
                        if (def.IsNeedingInput)
                        {
                            Exception e = new Exception("Se necesitan mas datos, se han perdido?");
                            break;
                        }
                        else
                        {
                            if (def.IsNeedingDictionary)
                            {
                                Exception e = new Exception("Falta el diccionario");
                                break;
                            }
                            else
                            {
                                System.Threading.Thread.Sleep(2000);
                                size = def.Inflate(data);
                            }
                        }
                    }
                }
            }
            catch (ZipException e)
            {
                this.m_byteoutput   = null;
                this.m_streamoutput = null;
                Debug.WriteLine(e.Message);
            }
            finally
            {
                this.m_byteoutput   = null;
                this.m_byteoutput   = new byte[size];
                this.m_streamoutput = null;
                this.m_streamoutput = new MemoryStream(size);
                this.m_streamoutput.Write(data, 0, size);
                this.m_streamoutput.Seek(0, SeekOrigin.Begin);
                this.m_streamoutput.Read(this.m_byteoutput, 0, size);
                this.m_streamoutput.Seek(0, SeekOrigin.Begin);
            }
        }
Example #2
0
        public override Stream Open()
        {
            lock (Archive) {
                BinaryReader reader = Archive.Reader;
                reader.BaseStream.Position = Offset;

                byte[] data = new byte[Size];

                switch (Mode)
                {
                case ArchiveRecordMode.Deflate:
                    var inflater = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                    inflater.SetInput(reader.ReadBytes(SizeCompressed), 0, SizeCompressed);
                    if (inflater.Inflate(data, 0, Size) != Size)
                    {
                        throw new InvalidDataException();
                    }
                    break;

                case ArchiveRecordMode.Uncompressed:
                    if (reader.Read(data, 0, Size) != Size)
                    {
                        throw new InvalidDataException();
                    }
                    break;

                default:
                    throw new NotSupportedException();
                }

                return(new MemoryStream(data, false));
            }
        }
Example #3
0
        /// <summary>
        /// Tries to identify the file extension
        /// Updates the file descriptor in response
        /// </summary>
        /// <param name="archFile">file to consider</param>
        private void TreatHeader(FileInArchive archFile)
        {
            MemoryStream outputMS = new MemoryStream();

            byte[] output_buffer = new byte[4096];

            if (archFile.descriptor.compressionMethod == 1)
            {
                try
                {
                    ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                    inf.SetInput(archFile.data_start_200);
                    inf.Inflate(output_buffer);
                }
                catch (Exception)
                {
                    Error_FileTableEntry(archFile);
                }

                archFile.descriptor.extension = GetExtension(output_buffer);
                //In order to sort the filenames in folders in the treeView. Yeah I cheat!
                //archFile.descriptor.filename = archFile.descriptor.extension + "/" + archFile.descriptor.filename;
            }
            else if (archFile.descriptor.compressionMethod == 0)
            {
                archFile.descriptor.extension = GetExtension(archFile.data_start_200);
            }
            else
            {
            }
        }
Example #4
0
 /// <summary>
 /// Inflate a region of the pack starting at
 /// <code>position</code>
 /// .
 /// </summary>
 /// <param name="pack">the file the desired window is stored within.</param>
 /// <param name="position">position within the file to read from.</param>
 /// <param name="dstbuf">
 /// destination buffer the inflater should output decompressed
 /// data to.
 /// </param>
 /// <param name="dstoff">current offset within <code>dstbuf</code> to inflate into.</param>
 /// <returns>
 /// updated <code>dstoff</code> based on the number of bytes
 /// successfully inflated into <code>dstbuf</code>.
 /// </returns>
 /// <exception cref="System.IO.IOException">
 /// this cursor does not match the provider or id and the proper
 /// window could not be acquired through the provider's cache.
 /// </exception>
 /// <exception cref="ICSharpCode.SharpZipLib.SharpZipBaseException">
 /// the inflater encountered an invalid chunk of data. Data
 /// stream corruption is likely.
 /// </exception>
 internal int Inflate(PackFile pack, long position, byte[] dstbuf, int dstoff)
 {
     PrepareInflater();
     Pin(pack, position);
     position += window.SetInput(position, inf);
     do
     {
         int n = inf.Inflate(dstbuf, dstoff, dstbuf.Length - dstoff);
         if (n == 0)
         {
             if (inf.IsNeedingInput)
             {
                 Pin(pack, position);
                 position += window.SetInput(position, inf);
             }
             else
             {
                 if (inf.IsFinished || (dstbuf.Length - dstoff) == 0)
                 {
                     return(dstoff);
                 }
                 else
                 {
                     throw new SharpZipBaseException();
                 }
             }
         }
         dstoff += n;
     }while (dstoff < dstbuf.Length);
     return(dstoff);
 }
Example #5
0
        private static byte[] UnscrambleAndUncompress(byte[] data, StreamFormatFlags eInputFormat, ObjectType eObjectType)
        {
            byte[] scdata = (byte[])data.Clone();

            if ((eInputFormat & StreamFormatFlags.Scrambled) == StreamFormatFlags.Scrambled)
            {
                // Unscramble the data
                XorData(scdata, eObjectType);
            }

            if ((eInputFormat & StreamFormatFlags.Compressed) == StreamFormatFlags.Compressed)
            {
                int len = (scdata[0]) + (scdata[1] << 8) +
                          (scdata[2] << 16) + (scdata[3] << 24);

                ICSharpCode.SharpZipLib.Zip.Compression.Inflater decompressor =
                    new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();

                decompressor.SetInput(scdata, sizeof(uint),
                                      scdata.Length - sizeof(uint));

                byte[] byInflated = new byte[len];

                int nBytesDecompressed = decompressor.Inflate(byInflated);
                if (nBytesDecompressed != len)
                {
                    throw new ApplicationException("Didn't decompress all of the text stream");
                }

                scdata = byInflated;
            }

            return(scdata);
        }
Example #6
0
        /// <summary>
        /// Tries to identify the file extension
        /// Updates the file descriptor in response
        /// </summary>
        /// <param name="archFile">file to consider</param>
        private void TreatHeader(FileInArchive archFile)
        {
            MemoryStream outputMS = new MemoryStream();

            byte[] output_buffer = new byte[4096];

            if (archFile.descriptor.compressionMethod == 1)
            {
                try
                {
                    ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                    inf.SetInput(archFile.data_start_200);
                    inf.Inflate(output_buffer);
                }
                catch
                {
                    Error_FileTableEntry(archFile);
                }

                archFile.descriptor.extension = GetExtension(output_buffer);
            }
            else if (archFile.descriptor.compressionMethod == 0)
            {
                archFile.descriptor.extension = GetExtension(archFile.data_start_200);
            }
            else
            {
            }
        }
Example #7
0
            public byte[] GetData()
            {
                FileInfo     file   = new FileInfo(bsa.name);
                BinaryReader binary = new BinaryReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite), System.Text.Encoding.Default);

                binary.BaseStream.Seek(offset, SeekOrigin.Begin);
                byte start = 0;

                if (bsa.defaultFlag9)
                {
                    start = binary.ReadByte();
                    string str = new string(binary.ReadChars(start));
                    //Console.WriteLine(bsa.name + " name " + start + " " + str);
                }
                if (compressed)
                {
                    //Console.WriteLine(bsa.name + " compressed " + size);
                    byte[] b      = new byte[size - 4 - start];
                    byte[] output = new byte[binary.ReadUInt32()];
                    binary.Read(b, 0, size - 4 - start);
                    ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                    inf.SetInput(b, 0, b.Length);
                    inf.Inflate(output);
                    binary.Close();
                    return(output);
                }
                else
                {
                    //Console.WriteLine(bsa.name + " not compressed "+ size);
                    byte[] output = binary.ReadBytes(size);
                    binary.Close();
                    return(output);
                }
            }
        /// <summary>
        /// Treats the data extracted from the myp archive
        /// Uncompress the data if said data was compressed
        /// </summary>
        /// <param name="archFile"></param>
        private void TreatExtractedFile(FileInArchive archFile)
        {
            try
            {
                if (archFile.descriptor.compressionMethod == 1) //ZLib compression
                {
                    try
                    {
                        //Create the output_buffer, useless to create it if no compression
                        byte[] output_buffer = new byte[archFile.descriptor.uncompressedSize];

                        ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                        inf.SetInput(archFile.data);
                        inf.Inflate(output_buffer);

                        if (!MypHandlerConfig.MultithreadedExtraction)
                        {
                            //Treat directly the write
                            SaveBufferToFile(output_buffer, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension);
                        }
                        else
                        {
                            boList.AddBufferItemToQueue(output_buffer, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension);
                        }
                        //Clear the buffer (useless in CSharp)
                        output_buffer = null;
                    }
                    catch (Exception)
                    {
                        TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.FileExtractionError, error_ExtractionNumber++));
                    }
                }
                else if (archFile.descriptor.compressionMethod == 0) //No compression
                {
                    if (!MypHandlerConfig.MultithreadedExtraction)
                    {
                        //Treat directly the write
                        SaveBufferToFile(archFile.data, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension);
                    }
                    else
                    {
                        boList.AddBufferItemToQueue(archFile.data, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension);
                    }
                }
                else
                {
                    TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownCompressionMethod, error_ExtractionNumber++));
                }
            }
            catch (Exception)
            {
                TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownError, error_ExtractionNumber++));
            }
        }
Example #9
0
        private void _initiator_OnDataReceived(object sender, MessageEventArgs args)
        {
            try
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug("_initiator_OnDataReceived: [" + System.Text.Encoding.ASCII.GetString(args.Message) + "]");
                }

                //compressedStream.Write(args.Message, 0, Convert.ToInt32(args.MessageLen));

                _pendingBytes = appendBytes(args.Message, 0, Convert.ToInt32(args.MessageLen));

                byte [] buff = new byte[BUFFER_SIZE];

                if (zlib.IsNeedingInput)
                {
                    zlib.SetInput(_inputBuffer, 0, _pendingBytes);
                    _pendingBytes = 0;
                }

                int blidos = zlib.Inflate(buff, 0, BUFFER_SIZE);

                if (blidos > 0)
                {
                    UncompressedPacket pkt = new UncompressedPacket();
                    pkt.Buffer    = new byte[blidos];
                    pkt.BufferLen = blidos;

                    System.Array.Copy(buff, pkt.Buffer, blidos);

                    //compressedStream.Flush();
                    //compressedStream.Position = 0;

                    qUnpack.Enqueue(pkt);

                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug("leu : [" + System.Text.Encoding.ASCII.GetString(buff, 0, blidos) + "]");
                    }

                    if (DateTime.UtcNow.Ticks - lastLogSEC > TimeSpan.TicksPerSecond)
                    {
                        lastLogSEC = DateTime.UtcNow.Ticks;
                        logger.Info("Pacotes descompactados na fila: " + qUnpack.Count);
                    }
                }
                //zlib.BeginRead(unzipped, 0, unzipped.Length, new AsyncCallback(OnReceiveDecompressed), null);
            }
            catch (Exception ex)
            {
                logger.Error("_initiator_OnDataReceived: " + ex.Message, ex);
            }
        }
Example #10
0
        public static uint LowLevelDecompress(byte[] compressedBytes, byte[] uncompressedBytes,
                                              bool noHeader = true)
        {
            Contract.Requires <ArgumentNullException>(compressedBytes != null);
            Contract.Requires <ArgumentNullException>(uncompressedBytes != null);

            var zip = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater(noHeader);

            {
                zip.SetInput(compressedBytes);
                zip.Inflate(uncompressedBytes);
            }
            return((uint)zip.Adler);
        }
Example #11
0
        public static byte[] LowLevelDecompress(byte[] bytes, int uncompressedSize,
                                                int skipHeaderLength = sizeof(uint))
        {
            Contract.Requires <ArgumentNullException>(bytes != null);
            Contract.Requires <ArgumentOutOfRangeException>(uncompressedSize >= 0);
            Contract.Requires <ArgumentOutOfRangeException>(skipHeaderLength >= 0);
            Contract.Ensures(Contract.Result <byte[]>() != null);

            byte[] result = new byte[uncompressedSize];
            var    zip    = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();

            {
                zip.SetInput(bytes, skipHeaderLength, bytes.Length - skipHeaderLength);                 // skip the decompressed size header
                zip.Inflate(result);
            }
            return(result);
        }
Example #12
0
        /// <summary>
        /// Decompress data.
        /// </summary>
        /// <param name="byCompresedData">The data to decompress</param>
        /// <param name="dwDecompressedLen">The decompressed size that the compressed data will
        /// expand into.</param>
        /// <returns>The decompressed data</returns>
        public static byte[] Decompress(byte[] byCompresedData, uint dwDecompressedLen)
        {
            byte[] byUncompressedData = new byte[dwDecompressedLen];

            ICSharpCode.SharpZipLib.Zip.Compression.Inflater decompressor =
                new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();

            decompressor.SetInput(byCompresedData);

            int nBytesDecompressed = decompressor.Inflate(byUncompressedData);

            if (nBytesDecompressed != dwDecompressedLen)
            {
                throw new ApplicationException("Didn't decompress all of the metadata");
            }

            return(byUncompressedData);
        }
Example #13
0
            internal byte[] GetRawData()
            {
                BSA._br.BaseStream.Seek(Offset, SeekOrigin.Begin);
                if (!Compressed)
                {
                    return(BSA._br.ReadBytes(Size));
                }

                byte[] b      = new byte[Size - 4];
                byte[] output = new byte[BSA._br.ReadUInt32()];
                BSA._br.Read(b, 0, Size - 4);

                var inflater = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();

                inflater.SetInput(b, 0, b.Length);
                inflater.Inflate(output);

                return(output);
            }
Example #14
0
        //解压
        public byte[] Decompress(byte[] bytes)
        {
            MemoryStream memory = new MemoryStream();

            ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
            inf.SetInput(bytes);
            while (!inf.IsFinished)
            {
                int extracted = inf.Inflate(temp);
                if (extracted > 0)
                {
                    memory.Write(temp, 0, extracted);
                }
                else
                {
                    break;
                }
            }
            return(memory.ToArray());
        }
Example #15
0
            public MemoryStream Data()
            {
                br.BaseStream.Seek(offset, SeekOrigin.Begin);
                if (compressed)
                {
                    byte[] b      = new byte[size - 4];
                    byte[] output = new byte[br.ReadUInt32()];
                    br.Read(b, 0, size - 4);

                    ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                    inf.SetInput(b, 0, b.Length);
                    inf.Inflate(output);

                    return(new MemoryStream(output));
                }
                else
                {
                    return(new MemoryStream(br.ReadBytes(size)));
                }
            }
			internal byte[] GetRawData()
			{
				bsa.br.BaseStream.Seek(offset, SeekOrigin.Begin);
				if (bsa.SkipNames) bsa.br.BaseStream.Position += bsa.br.ReadByte() + 1;
				if (compressed)
				{
					byte[] b = new byte[size - 4];
					byte[] output = new byte[bsa.br.ReadUInt32()];
					bsa.br.Read(b, 0, size - 4);

					ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
					inf.SetInput(b, 0, b.Length);
					inf.Inflate(output);

					return output;
				}
				else
				{
					return bsa.br.ReadBytes(size);
				}
			}
Example #17
0
        /// <summary>
        /// Inflater解压缩
        /// </summary>
        /// <param name="inputByte"></param>
        /// <returns></returns>
        public static byte[] inflater(byte[] inputByte)
        {
            byte[]       temp   = new byte[1024];
            MemoryStream memory = new MemoryStream();

            ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
            inf.SetInput(inputByte);
            while (!inf.IsFinished)
            {
                int extracted = inf.Inflate(temp);
                if (extracted > 0)
                {
                    memory.Write(temp, 0, extracted);
                }
                else
                {
                    break;
                }
            }
            return(memory.ToArray());
        }
Example #18
0
        /// <summary>
        /// Treats the data extracted from the myp archive
        /// Uncompress the data if said data was compressed
        /// </summary>
        /// <param name="archFile"></param>
        private void TreatExtractedFile(FileInArchive archFile)
        {
            MemoryStream inputMS  = new MemoryStream(archFile.data, 0, archFile.data.Length);
            MemoryStream outputMS = new MemoryStream();

            byte[] output_buffer = new byte[archFile.descriptor.uncompressedSize];

            try
            {
                if (archFile.descriptor.compressionMethod == 1) //ZLib compression
                {
                    try
                    {
                        ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                        inf.SetInput(archFile.data);
                        inf.Inflate(output_buffer);

                        SaveBufferToFile(output_buffer, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension);
                        TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.FileExtracted, numExtractedFiles++));
                    }
                    catch
                    {
                        TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.FileExtractionError, error_ExtractionNumber++));
                    }
                }
                else if (archFile.descriptor.compressionMethod == 0) //No compression
                {
                    SaveBufferToFile(archFile.data, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension);
                    TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.FileExtracted, numExtractedFiles++));
                }
                else
                {
                    TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownCompressionMethod, error_ExtractionNumber++));
                }
            }
            catch
            {
                TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownError, error_ExtractionNumber++));
            }
        }
Example #19
0
        private static ByteVector Inflate(ByteVector data)
        {
#if HAVE_SHARPZIPLIB
            using (System.IO.MemoryStream out_stream = new System.IO.MemoryStream()) {
                ICSharpCode.SharpZipLib.Zip.Compression.Inflater inflater =
                    new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();

                inflater.SetInput(data.Data);

                byte [] buffer = new byte [1024];
                int     written_bytes;

                while ((written_bytes = inflater.Inflate(buffer)) > 0)
                {
                    out_stream.Write(buffer, 0, written_bytes);
                }

                return(new ByteVector(out_stream.ToArray()));
            }
#else
            return(null);
#endif
        }
Example #20
0
        /// <summary>
        /// 字节数组解压缩
        /// 返回:已解压缩的字节数组
        /// </summary>
        /// <param name="data">待解压缩的字节数组</param>
        /// <returns></returns>
        public static byte[] DecompressBytes(byte[] data)
        {
            var f = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();

            f.SetInput(data);

            var o = new MemoryStream(data.Length);

            try
            {
                var buf = new byte[1024];
                while (!f.IsFinished)
                {
                    var got = f.Inflate(buf);
                    o.Write(buf, 0, got);
                }
            }
            finally
            {
                o.Close();
            }

            return(o.ToArray());
        }
        public byte[] ReadFile(Stream outstream, FileInArchive file)
        {
            if (file.Descriptor.CompressionMethod == 1) //ZLib compression
            {
                outstream.Position = file.Descriptor.StartingPosition + file.Descriptor.FileHeaderSize;
                byte[] compressedData = new byte[file.Descriptor.CompressedSize];
                outstream.Read(compressedData, 0, (int)file.Descriptor.CompressedSize);

                byte[] output_buffer = new byte[file.Descriptor.UncompressedSize];

                ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                inf.SetInput(compressedData);
                inf.Inflate(output_buffer);
                return(output_buffer);
            }
            else
            {
                outstream.Position = file.Descriptor.StartingPosition + file.Descriptor.FileHeaderSize;
                byte[] compressedData = new byte[file.Descriptor.CompressedSize];
                outstream.Read(compressedData, 0, (int)file.Descriptor.CompressedSize);

                return(compressedData);
            }
        }
Example #22
0
            internal byte[] GetRawData()
            {
                bsa.br.BaseStream.Seek(offset, SeekOrigin.Begin);
                if (bsa.SkipNames)
                {
                    bsa.br.BaseStream.Position += bsa.br.ReadByte() + 1;
                }
                if (compressed)
                {
                    byte[] b      = new byte[size - 4];
                    byte[] output = new byte[bsa.br.ReadUInt32()];
                    bsa.br.Read(b, 0, size - 4);

                    ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                    inf.SetInput(b, 0, b.Length);
                    inf.Inflate(output);

                    return(output);
                }
                else
                {
                    return(bsa.br.ReadBytes(size));
                }
            }
Example #23
0
 /// <summary>
 /// Inflater解压缩
 /// </summary>
 /// <param name="inputByte"></param>
 /// <returns></returns>
 public static byte[] inflater(byte[] inputByte)
 {
     byte[] temp = new byte[1024];
     MemoryStream memory = new MemoryStream();
     ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
     inf.SetInput(inputByte);
     while (!inf.IsFinished)
     {
         int extracted = inf.Inflate(temp);
         if (extracted > 0)
         {
             memory.Write(temp, 0, extracted);
         }
         else
         {
             break;
         }
     }
     return memory.ToArray();
 }
        public void ExtractFiles(object oinfo)
        {
            long succeeded, failed, current;
            ExtractThreadInfo info = (ExtractThreadInfo)oinfo;

            FileStream inputfs = new FileStream(info.Path, FileMode.Open, FileAccess.Read, FileShare.Read);
            Stream     input   = inputfs as Stream;

            succeeded = failed = current = 0;

            this.Log(String.Format("{0} files in package.", info.Package.Entries.Count));

            foreach (PackageEntry entry in info.Package.Entries)
            {
                this.SetProgress(++current);

                input.Seek(entry.Offset, SeekOrigin.Begin);

                string outputName = entry.Name + "." + entry.Extension;
                this.Log(outputName);

                FileStream outputfs = new FileStream(Path.Combine(info.Save, outputName), FileMode.Create, FileAccess.Write, FileShare.None);
                Stream     output   = outputfs as Stream;

                if (entry.CompressedSize == -1)
                {
                    long   left = entry.UncompressedSize;
                    byte[] data = new byte[4096];
                    while (left > 0)
                    {
                        int block = (int)(Math.Min(left, 4096));
                        input.Read(data, 0, block);
                        output.Write(data, 0, block);
                        left -= block;
                    }
                }
                else
                {
                    ICSharpCode.SharpZipLib.Zip.Compression.Inflater inflater =
                        new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();

                    // compressed using zlib
                    long   left           = entry.CompressedSize;
                    byte[] compressedData = new byte[4096];
                    byte[] data           = new byte[4096];

                    while (inflater.TotalOut < entry.UncompressedSize)
                    {
                        if (inflater.IsNeedingInput == true)
                        {
                            if (left == 0)
                            {
                                throw new Exception();
                            }

                            int block = (int)(Math.Min(left, 4096));
                            input.Read(compressedData, 0, block);
                            inflater.SetInput(compressedData, 0, block);
                            left -= block;
                        }

                        int inflated = inflater.Inflate(data);
                        output.Write(data, 0, inflated);
                    }
                }

                output.Close();
                succeeded++;
            }

            input.Close();

            this.Log(String.Format("Done, {0} succeeded, {1} failed, {2} total.", succeeded, failed, info.Package.Entries.Count));
            this.EnableButtons(true);
        }
Example #25
0
        /// <summary>
        /// Tries to identify the file extension
        /// Updates the file descriptor in response
        /// </summary>
        /// <param name="archFile">file to consider</param>
        private void TreatHeader(FileInArchive archFile)
        {
            MemoryStream outputMS = new MemoryStream();

            byte[] output_buffer = new byte[4096];

            if (archFile.descriptor.compressionMethod == 1)
            {
                try
                {
                    ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                    inf.SetInput(archFile.data_start_200);
                    inf.Inflate(output_buffer);
                }
                catch (Exception)
                {
                    Error_FileTableEntry(archFile);
                }

                archFile.descriptor.extension = GetExtension(output_buffer);
                //In order to sort the filenames in folders in the treeView. Yeah I cheat!
                //archFile.descriptor.filename = archFile.descriptor.extension + "/" + archFile.descriptor.filename;
            }
            else if (archFile.descriptor.compressionMethod == 0)
            {
                archFile.descriptor.extension = GetExtension(archFile.data_start_200);
            }
            else
            {
            }
        }
        public void Send(OP code, byte[] data)
        {
            if (sock == null)
            {
                return;
            }

            #region debug
#if DEBUG
            //    StreamWriter w=new StreamWriter("d:\\msend.txt", true);
            StreamWriter  w   = new StreamWriter("c:\\msend.txt", true);
            StringBuilder str = new StringBuilder();

            byte[] cdata = data;
            if (code == OP.SMSG_COMPRESSED_UPDATE_OBJECT)
            {
                ICSharpCode.SharpZipLib.Zip.Compression.Inflater inflater = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                inflater.SetInput(data, 4, data.Length - 4);

                cdata = new byte[data[0] + data[1] * 256 + data[2] * 256 * 256];
                inflater.Inflate(cdata);
                //  ZLib.Decompress(cdata);
            }

            str.AppendFormat("[{0} {1}] {2} length={3} ", Const.Tick, sock.RemoteEndPoint.ToString(), code, cdata.Length);
            if (code == OP.SMSG_UPDATE_OBJECT || code == OP.SMSG_COMPRESSED_UPDATE_OBJECT)
            {
                int pos = 5;
                str.AppendFormat("count={0}\r\n", cdata[0]);
                for (int i = 0; i < cdata[0]; i++)
                {
                    if (pos >= cdata.Length)
                    {
                        break;
                    }

                    byte type = cdata[pos];
                    str.AppendFormat(" {0} TYPE={1} GUID={2:X} ", i + 1, type, BitConverter.ToUInt32(cdata, pos + 1));
                    pos += 9;

                    if (type > 0)
                    {
                        byte obtype = cdata[pos]; pos += 9;
                        str.AppendFormat("OBTYPE={0} MOVE [", obtype);
                        for (int j = 0; j < 10; j++)
                        {
                            str.AppendFormat("{0} ", BitConverter.ToSingle(cdata, pos));
                            pos += 4;
                        }
                        pos += 20;
                        str.AppendFormat("] ");
                    }
                    str.AppendFormat("UPDATE [");
                    byte cnt  = cdata[pos]; pos++;
                    int  cpos = pos; pos += cnt * 4;
                    for (int j = 0; j < cnt * 32; j++)
                    {
                        if ((cdata[cpos + j / 8] & (1 << (j % 8))) != 0)
                        {
                            if (pos >= cdata.Length)
                            {
                                break;
                            }

                            str.AppendFormat("{0}={1:X} ", j, BitConverter.ToUInt32(cdata, pos));
                            pos += 4;
                        }
                    }
                    str.AppendFormat("]\r\n");
                }
                str.Length = str.Length - 2;
            }             //
            else
            {
                str.Append("{ ");
                for (int i = 0; i < data.Length; i++)
                {
                    string s = data[i].ToString("X");
                    if (s.Length == 2)
                    {
                        str.Append(s + " ");
                    }
                    else
                    {
                        str.Append("0" + s + " ");
                    }
                }
                str.Append("}");
            }

            w.WriteLine(str.ToString());
            w.Close();
#endif
            #endregion
            ByteArrayBuilder pack = new ByteArrayBuilder();
            pack.Add((ushort)(data.Length + 2));
            pack.Add((byte)code, (byte)((ushort)code >> 8));
            pack.Add(data);
            if (SS_Hash != null)
            {
                Encode(pack);
            }
            Send(pack);
        }
Example #27
0
        public override Stream Open()
        {
            lock (Archive) {
                BinaryReader reader = Archive.Reader;
                reader.BaseStream.Position = Offset;

                byte[] data = new byte[Size];

                switch (Mode) {
                    case ArchiveRecordMode.Deflate:
                        var inflater = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                        inflater.SetInput(reader.ReadBytes(SizeCompressed), 0, SizeCompressed);
                        if (inflater.Inflate(data, 0, Size) != Size)
                            throw new InvalidDataException();
                        break;

                    case ArchiveRecordMode.Uncompressed:
                        if (reader.Read(data, 0, Size) != Size)
                            throw new InvalidDataException();
                        break;

                    default:
                        throw new NotSupportedException();
                }

                return new MemoryStream(data, false);
            }
        }
Example #28
0
            public byte[] GetData()
            {
                FileInfo     file   = new FileInfo(bsa.name);
                BinaryReader binary = new BinaryReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite), System.Text.Encoding.Default);

                binary.BaseStream.Seek((Int64)offset, SeekOrigin.Begin);
                byte start = 0;

                if (bsa.defaultFlag9)
                {
                    start = binary.ReadByte();
                    string str = new string(binary.ReadChars(start));
                    //Console.WriteLine("filename found");
                }
                if (compressed)
                {
                    // https://github.com/IonKiwi/lz4.net for interop C++ version
                    // https://github.com/Fody/Costura to merge dlls into exe
                    if (bsa.compressionType)
                    {
                        if (zsize != 0)
                        {
                            byte[] b      = new byte[size - start];
                            byte[] output = new byte[zsize];
                            binary.Read(b, 0, (int)size - start);
                            output = lz4.LZ4Helper.Decompress(b);
                            return(output);
                        }
                        else
                        {
                            byte[] b      = new byte[size - 4 - start];
                            uint   l      = binary.ReadUInt32();
                            byte[] output = new byte[l];
                            binary.Read(b, 0, (int)size - 4 - start);
                            output = lz4.LZ4Helper.Decompress(b);
                            return(output);
                        }
                    }
                    else
                    {
                        if (zsize != 0)
                        {
                            byte[] b      = new byte[size - start];
                            byte[] output = new byte[zsize];
                            binary.Read(b, 0, (int)size - start);
                            ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                            inf.SetInput(b, 0, b.Length);
                            inf.Inflate(output);
                            binary.Close();
                            return(output);
                        }
                        else
                        {
                            byte[] b      = new byte[size - 4 - start];
                            byte[] output = new byte[binary.ReadUInt32()];
                            binary.Read(b, 0, (int)size - 4 - start);
                            ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                            inf.SetInput(b, 0, b.Length);
                            inf.Inflate(output);
                            binary.Close();
                            return(output);
                        }
                    }
                }
                else
                {
                    byte[] output = binary.ReadBytes((int)size);
                    binary.Close();
                    return(output);
                }
            }
        private static ByteVector Inflate(ByteVector data)
        {
            #if HAVE_SHARPZIPLIB
            using (System.IO.MemoryStream out_stream = new System.IO.MemoryStream ()) {

                ICSharpCode.SharpZipLib.Zip.Compression.Inflater inflater =
                    new ICSharpCode.SharpZipLib.Zip.Compression.Inflater ();

                inflater.SetInput (data.Data);

                byte [] buffer = new byte [1024];
                int written_bytes;

                while ((written_bytes = inflater.Inflate (buffer)) > 0)
                    out_stream.Write (buffer, 0, written_bytes);

                return new ByteVector (out_stream.ToArray ());
            }
            #else
            return null;
            #endif
        }
Example #30
0
        static void SetupWoffDecompressFunctions()
        {
            //
            //Woff
            WoffDefaultZlibDecompressFunc.DecompressHandler = (byte[] compressedBytes, byte[] decompressedResult) =>
            {
                //ZLIB
                //****
                //YOU can change to  your prefer decode libs***
                //****

                bool result = false;
                try
                {
                    var inflater = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                    inflater.SetInput(compressedBytes);
                    inflater.Inflate(decompressedResult);
#if DEBUG
                    long outputLen = inflater.TotalOut;
                    if (outputLen != decompressedResult.Length)
                    {
                    }
#endif

                    result = true;
                }
                catch (Exception ex)
                {
                }
                return(result);
            };
            //Woff2

            Woff2DefaultBrotliDecompressFunc.DecompressHandler = (byte[] compressedBytes, Stream output) =>
            {
                //BROTLI
                //****
                //YOU can change to  your prefer decode libs***
                //****

                bool result = false;
                try
                {
                    using (MemoryStream ms = new MemoryStream(compressedBytes))
                    {
                        ms.Position = 0;//set to start pos
                        DecompressAndCalculateCrc1(ms, output);
                        //
                        //

                        //Decompress(ms, output);
                    }
                    //DecompressBrotli(compressedBytes, output);
                    result = true;
                }
                catch (Exception ex)
                {
                }
                return(result);
            };
        }
Example #31
0
        unsafe void AcquireScreen()
        {
            Message msg = new Message();
            msg.type = MessageType.Message_DisplayCapture;
            msg.Send(ns);

            msg.Recv(ns);
            int zipped_size = msg.dispcap_size;
            BinaryReader br = new BinaryReader(ns);
            byte[] zipbuf = new byte[zipped_size];
            for (int i = 0; i < zipped_size; i++)
                zipbuf[i] = br.ReadByte();

            var inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
            inf.SetInput(zipbuf);
            byte[] bscreen = new byte[256 * 192 * 2];
            inf.Inflate(bscreen);

            var bmp = new Bitmap(256, 192, PixelFormat.Format32bppArgb);
            BitmapData bmpdata = bmp.LockBits(new System.Drawing.Rectangle(0, 0, 256, 192), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
            byte* bp = (byte*)bmpdata.Scan0.ToPointer();
            for (int i = 0; i < 256 * 192; i++)
            {
                int r = bscreen[i * 2] & 0x1F;
                int g = ((bscreen[i * 2] & 0xE0)>>5) | ((bscreen[i*2+1] & 3)<<3);
                int b = (bscreen[i * 2 + 1] >> 2) & 0x1F;
                int a = bscreen[i * 2 + 1] >> 7;

                //todo - use same color conversion as desmume (whatever that is)
                r <<= 3;
                g <<= 3;
                b <<= 3;

                bp[i * 4 + 0] = (byte)b;
                bp[i * 4 + 1] = (byte)g;
                bp[i * 4 + 2] = (byte)r;
                bp[i * 4 + 3] = 255;
            }
            bmp.UnlockBits(bmpdata);

            if (lastScreen != null) lastScreen.Dispose();
            lastScreen = bmp;
            SetViewport(lastScreen);
        }
Example #32
0
        /// <summary>
        /// Treats the data extracted from the myp archive
        /// Uncompress the data if said data was compressed
        /// </summary>
        /// <param name="archFile"></param>
        private void TreatExtractedFile(FileInArchive archFile)
        {
            try
            {
                if (archFile.descriptor.compressionMethod == 1) //ZLib compression
                {
                    try
                    {
                        //Create the output_buffer, useless to create it if no compression
                        byte[] output_buffer = new byte[archFile.descriptor.uncompressedSize];

                        ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                        inf.SetInput(archFile.data);
                        inf.Inflate(output_buffer);

                        if (!MypHandlerConfig.MultithreadedExtraction)
                        {
                            //Treat directly the write
                            SaveBufferToFile(output_buffer, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension);
                        }
                        else
                        {
                            boList.AddBufferItemToQueue(output_buffer, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension);
                        }
                        //Clear the buffer (useless in CSharp)
                        output_buffer = null;
                    }
                    catch (Exception)
                    {
                        TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.FileExtractionError, error_ExtractionNumber++));
                    }
                }
                else if (archFile.descriptor.compressionMethod == 0) //No compression
                {
                    if (!MypHandlerConfig.MultithreadedExtraction)
                    {
                        //Treat directly the write
                        SaveBufferToFile(archFile.data, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension);
                    }
                    else
                    {
                        boList.AddBufferItemToQueue(archFile.data, archFile.descriptor.foundFileName, archFile.descriptor.filename, archFile.descriptor.extension);
                    }
                }
                else
                {
                    TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownCompressionMethod, error_ExtractionNumber++));
                }
            }
            catch (Exception)
            {
                TriggerExtractionEvent(new MYPFileEventArgs(Event_ExtractionType.UnknownError, error_ExtractionNumber++));
            }
        }
 public byte[] GetData()
 {
     FileInfo file = new FileInfo(bsa.name);
     BinaryReader binary = new BinaryReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite), System.Text.Encoding.Default);
     binary.BaseStream.Seek(offset, SeekOrigin.Begin);
     byte start = 0;
     if (bsa.defaultFlag9)
     {
         start = binary.ReadByte();
         string str = new string(binary.ReadChars(start));
         //Console.WriteLine(bsa.name + " name " + start + " " + str);
     }
     if (compressed)
     {
         //Console.WriteLine(bsa.name + " compressed " + size);
         byte[] b = new byte[size - 4 - start];
         byte[] output = new byte[binary.ReadUInt32()];
         binary.Read(b, 0, size - 4 - start);
         ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
         inf.SetInput(b, 0, b.Length);
         inf.Inflate(output);
         binary.Close();
         return output;
     }
     else
     {
         //Console.WriteLine(bsa.name + " not compressed "+ size);
         byte[] output = binary.ReadBytes(size);
         binary.Close();
         return output;
     }
 }
Example #34
0
        public static void Trim(IntPtr hwnd, string In, string Out, ReportProgressDelegate del)
        {
            NativeMethods.ddsInit(hwnd);
            BinaryReader br=new BinaryReader(File.OpenRead(In), System.Text.Encoding.Default);
            BinaryWriter bw=new BinaryWriter(File.Create(Out), System.Text.Encoding.Default);
            System.Text.StringBuilder sb=new System.Text.StringBuilder(64);
            ICSharpCode.SharpZipLib.Zip.Compression.Inflater inf=new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
            bool Compressed, SkipName;

            if(br.ReadInt32()!=0x00415342) throw new Exception("Invalid bsa");
            uint version=br.ReadUInt32();
            bw.Write((int)0x00415342);
            bw.Write(version);
            bw.Write(br.ReadInt32());
            uint flags=br.ReadUInt32();
            if((flags&0x004)>0) { Compressed=true; flags^=0x4; } else Compressed=false;
            if((flags&0x100)>0&&version==0x68) SkipName=true; else SkipName=false;
            flags^=0x2;
            int FolderCount=br.ReadInt32();
            int FileCount=br.ReadInt32();
            bw.Write(flags);

            bw.Write(FolderCount);
            bw.Write(FileCount);
            bw.Write(br.ReadInt32());
            bw.Write(br.ReadInt32());
            bw.Write(br.ReadInt32());

            int[] folderFileCount=new int[FolderCount];
            for(int i=0;i<FolderCount;i++) {
                bw.Write(br.ReadInt64());
                folderFileCount[i]=br.ReadInt32();
                bw.Write(folderFileCount[i]);
                bw.Write(br.ReadInt32());
            }
            int[] fileLengths=new int[FileCount];
            long[] offsetOffsets=new long[FileCount];
            uint[] fileOffsets=new uint[FileCount];
            bool[] parsefiles=new bool[FileCount];
            int file=0;
            for(int i=0;i<FolderCount;i++) {
                byte len=br.ReadByte();
                bw.Write(len);
                sb.Length=0;
                while(--len>0) {
                    char c=br.ReadChar();
                    sb.Append(c);
                    bw.Write(c);
                }
                br.ReadByte();
                bw.Write((byte)0);
                bool parse=true;
                if(sb.ToString().StartsWith("textures\\interface\\")) parse=false;

                for(int j=0;j<folderFileCount[i];j++) {
                    bw.Write(br.ReadUInt64());
                    offsetOffsets[file]=br.BaseStream.Position;
                    fileLengths[file]=br.ReadInt32();
                    bw.Write(fileLengths[file]);
                    fileOffsets[file]=br.ReadUInt32();
                    bw.Write(fileOffsets[file]);
                    parsefiles[file]=parse;
                    file++;
                }
            }

            for(int i=0;i<FileCount;i++) {
                sb.Length=0;
                while(true) {
                    char c=(char)br.ReadByte();
                    //bw.Write(c);
                    if(c=='\0') break;
                    sb.Append(c);
                }
                if(!sb.ToString().EndsWith(".dds", StringComparison.OrdinalIgnoreCase)) parsefiles[i]=false;
            }

            int count=0;
            for(int i=0;i<FileCount;i++) {
                if((i%100)==0) del("Processing file "+i+" of "+FileCount);
                br.BaseStream.Position=fileOffsets[i];
                long offset=bw.BaseStream.Position;
                int add=0;
                if(SkipName) {
                    byte len=br.ReadByte();
                    bw.Write(len);
                    bw.Write(br.ReadBytes(len+1));
                    add=len+2;
                }
                bool compressed2=Compressed;
                if((fileLengths[i]&(1<<30))!=0) {
                    compressed2=!compressed2;
                    fileLengths[i]^=(1<<30);
                }
                if(!compressed2) {
                    byte[] bytes=new byte[fileLengths[i]];
                    br.Read(bytes, 0, fileLengths[i]);
                    Commit(bw, offsetOffsets[i], bytes, offset, add, parsefiles[i]);
                } else {
                    count++;
                    byte[] uncompressed=new byte[br.ReadUInt32()];
                    byte[] compressed=new byte[fileLengths[i]-4];
                    br.Read(compressed, 0, fileLengths[i]-4);
                    inf.Reset();
                    inf.SetInput(compressed);
                    inf.Inflate(uncompressed);
                    Commit(bw, offsetOffsets[i], uncompressed, offset, add, parsefiles[i]);
                }
            }

            br.Close();
            bw.Close();
            NativeMethods.ddsClose();
        }
Example #35
0
        public Form1()
        {
            InitializeComponent();

            var dicProvider = new Typography.TextBreak.IcuSimpleTextFileDictionaryProvider()
            {
                DataDir = "../../../../../Typography.TextBreak/icu62/brkitr"
            };

            Typography.TextBreak.CustomBreakerBuilder.Setup(dicProvider);

            this.Load += new System.EventHandler(this.Form1_Load);


            //
            //Woff
            WoffDefaultZlibDecompressFunc.DecompressHandler = (byte[] compressedBytes, byte[] decompressedResult) =>
            {
                //ZLIB
                //****
                //YOU can change to  your prefer decode libs***
                //****

                bool result = false;
                try
                {
                    var inflater = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
                    inflater.SetInput(compressedBytes);
                    inflater.Inflate(decompressedResult);
#if DEBUG
                    long outputLen = inflater.TotalOut;
                    if (outputLen != decompressedResult.Length)
                    {
                    }
#endif

                    result = true;
                }
                catch (Exception ex)
                {
                }
                return(result);
            };
            //Woff2

            Woff2DefaultBrotliDecompressFunc.DecompressHandler = (byte[] compressedBytes, Stream output) =>
            {
                //BROTLI
                //****
                //YOU can change to  your prefer decode libs***
                //****

                bool result = false;
                try
                {
                    using (MemoryStream ms = new MemoryStream(compressedBytes))
                    {
                        ms.Position = 0;//set to start pos
                        DecompressAndCalculateCrc1(ms, output);
                        //
                        //

                        //Decompress(ms, output);
                    }
                    //DecompressBrotli(compressedBytes, output);
                    result = true;
                }
                catch (Exception ex)
                {
                }
                return(result);
            };
        }