Ejemplo n.º 1
0
        internal void BeginDecompress(CompressionAlgorithm algorithm)
        {
            if (algorithm != CompressionAlgorithm.None)
            {
                if (_compressionDecoder != null && _compressionDecoder.Algorithm != algorithm)
                {
                    _compressionDecoder?.Dispose();
                    _compressionDecoder = null;
                }
                else
                {
                    _compressionDecoder?.Reset();
                }
            }

            switch (algorithm)
            {
            case CompressionAlgorithm.None:
                _currentCompression = algorithm;
                return;

            case CompressionAlgorithm.Lz4:
                if (_compressionDecoder == null)
                {
                    _compressionDecoder = new Lz4CompressionDecoder(_bufferSize);
                }

                _currentCompression = algorithm;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(algorithm), algorithm, null);
            }
        }
        public void CheckCompressDecompress(CompressionAlgorithm algorithm)
        {
            if (!TestRuntime.CheckXcodeVersion(7, 0))
            {
                Assert.Ignore("Requires iOS 9.0+ or macOS 10.11+");
            }
            byte [] data = new byte[100000];
            for (int i = 0; i < 100000; i++)
            {
                data[i] = (byte)i;
            }
            MemoryStream  dataStream  = new MemoryStream(data);
            MemoryStream  backing     = new MemoryStream();
            DeflateStream compressing = new DeflateStream(backing, CompressionMode.Compress, algorithm, true);

            CopyStream(dataStream, compressing);
            dataStream.Close();
            compressing.Close();
            backing.Seek(0, SeekOrigin.Begin);
            DeflateStream decompressing = new DeflateStream(backing, CompressionMode.Decompress, algorithm);
            MemoryStream  output        = new MemoryStream();

            CopyStream(decompressing, output);
            Assert.AreNotEqual(0, output.Length, "Length should be more than 0.");
            Assert.IsTrue(compare_buffers(data, output.GetBuffer(), (int)output.Length), "Buffers are not equal.");
            decompressing.Close();
            output.Close();
        }
        public void JunkAtTheEnd(CompressionAlgorithm algorithm)
        {
            if (!TestRuntime.CheckXcodeVersion(7, 0))
            {
                Assert.Ignore("Requires iOS 9.0+ or macOS 10.11+");
            }
            // Write a deflated stream, then some additional data...
            using (MemoryStream ms = new MemoryStream())
            {
                // The compressed stream
                using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, algorithm, true))
                {
                    stream.WriteByte(1);
                    stream.Flush();
                }
                // Junk
                ms.WriteByte(2);

                ms.Position = 0;
                // Reading: this should not hang
                using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Decompress, algorithm))
                {
                    byte[] buffer = new byte[512];
                    int    len    = stream.Read(buffer, 0, buffer.Length);
                    Console.WriteLine(len == 1);
                }
            }
        }
        private void AddCompressionCapabilities(DetectionInfo info)
        {
            var excludedCompressionAlogrithms = new CompressionAlgorithm[]
            {
                CompressionAlgorithm.NONE,
                CompressionAlgorithm.Unsupported,
            };

            var possibleCompressionAlogrithms = Enum.GetValues(typeof(CompressionAlgorithm)).Cast <CompressionAlgorithm>().Except(excludedCompressionAlogrithms);

            foreach (var compressionAlgorithm in possibleCompressionAlogrithms)
            {
                if (info.smb2Info.SupportedCompressionAlgorithms.Contains(compressionAlgorithm))
                {
                    AddResultItem(ref this.compressionItems, compressionAlgorithm.ToString(), DetectResult.Supported);
                }
                else
                {
                    AddResultItem(ref this.compressionItems, compressionAlgorithm.ToString(), DetectResult.UnSupported);
                }
            }

            var chainedCompressionResult = info.smb2Info.IsChainedCompressionSupported ? DetectResult.Supported : DetectResult.UnSupported;

            AddResultItem(ref this.compressionItems, "Chained compression", chainedCompressionResult);
        }
Ejemplo n.º 5
0
        public void Uncompress(CompressionAlgorithm algorithm)
        {
            Position = 0;
            var ms     = new MemoryStream();
            var buffer = new byte[1024];

            // The zlib format is specified by RFC 1950. Zlib also uses deflate, plus 2 or 6 header bytes, and a 4 byte checksum at the end.
            // The first 2 bytes indicate the compression method and flags. If the dictionary flag is set, then 4 additional bytes will follow.
            // Preset dictionaries aren't very common and we don't support them
            var deflateStream = algorithm == CompressionAlgorithm.Zlib
                ? new ZlibStream(memoryStream, CompressionMode.Decompress, false)
                : new DeflateStream(memoryStream, CompressionMode.Decompress, false);

            while (true)
            {
                var readCount = deflateStream.Read(buffer, 0, buffer.Length);
                if (readCount == 0)
                {
                    break;
                }
                ms.Write(buffer, 0, readCount);
            }

            memoryStream.Dispose();
            memoryStream          = ms;
            memoryStream.Position = 0;
            dataOutput            = new DataOutput(new AmfWriter(memoryStream, serializationContext));
            dataInput             = new DataInput(new AmfReader(memoryStream, serializationContext));
        }
Ejemplo n.º 6
0
        private void BasicSMB2Compression(CompressionAlgorithm compressionAlgorithm)
        {
            CheckCompressionAndEncryptionApplicability(compressionAlgorithm);
            var compressionAlgorithms = new CompressionAlgorithm[] { compressionAlgorithm };

            SMB2CompressionTest(compressionAlgorithms, false, CompressionTestVariant.BasicReadWrite);
        }
        public void BeginCompress(CompressionAlgorithm algorithm, int compressionBlockSize)
        {
            if (_compressionEncoder != null)
            {
                if (_compressionEncoder.Algorithm != algorithm)
                {
                    _compressionEncoder.Dispose();
                    _compressionEncoder = null;
                }
                else
                {
                    _currentCompression = _compressionEncoder.Algorithm;
                    _compressionEncoder.Reset();
                    return;
                }
            }

            switch (algorithm)
            {
            case CompressionAlgorithm.None:
                break;

            case CompressionAlgorithm.Lz4:
                _currentCompression = algorithm;
                _compressionEncoder = new Lz4CompressionEncoder(_bufferSize, compressionBlockSize);
                break;

            default:
                throw new NotSupportedException($"Compression algorithm \"{algorithm}\" is not supported.");
            }
        }
Ejemplo n.º 8
0
        public static Byte[] Compress(this Byte[] data, CompressionAlgorithm algorithm)
        {
            //--- Define Location To Store Compressed Data ---//
                MemoryStream MS = new MemoryStream();

                //--- Create Compression Object ---//
                Stream zipper;
                if (algorithm == CompressionAlgorithm.GZipStream)
                    zipper = new GZipStream(MS, CompressionMode.Compress);
                else
                    zipper = new DeflateStream(MS, CompressionMode.Compress);

                //--- Compress ---//
                zipper.Write(data, 0, data.Length);
                zipper.Flush();
                zipper.Close();
                zipper.Dispose();

                //--- Get Compressed Data ---//
                Byte[] compressedData = MS.ToArray();
                MS.Close();
                MS.Dispose();

                //--- Return Compressed Data ---//
                return compressedData;
        }
Ejemplo n.º 9
0
        public virtual void CompressionAlgorithm_InputStreamThenOuputStreamProducesEquivalentData(Stream testData)
        {
            var originalDataStream = new MemoryStream();

            testData.CopyTo(originalDataStream);
            originalDataStream.Position = 0;

            CompressionAlgorithm algorithm = GetAlgorithm();
            CompressionOptions   options   = GenerateCompressionOptions(algorithm.CompressionType);

            var compressedMemoryStream = new MemoryStream();

            using (Stream compressorStream = algorithm.CreateCompressor(options).CreateOutputStream(compressedMemoryStream, true, 4096))
            {
                originalDataStream.CopyTo(compressorStream);
            }

            var decompressedData = new MemoryStream();

            compressedMemoryStream.Position = 0;
            using (Stream decopmressorStream = algorithm.CreateDecompressor().CreateInputStream(compressedMemoryStream, true))
            {
                decopmressorStream.CopyTo(decompressedData);
            }

            Assert.AreEqual(originalDataStream.Length, decompressedData.Length);
            CollectionAssert.AreEqual(originalDataStream.ToArray(), decompressedData.ToArray());
        }
Ejemplo n.º 10
0
        public virtual byte[] Decompress(byte[] compressedData, CompressionAlgorithm algorithm)
        {
            using (MemoryStream ms = new MemoryStream(compressedData))
            {
                if (algorithm == CompressionAlgorithm.GZip)
                {
                    using (GZipStream stream2 = new GZipStream(ms, CompressionMode.Decompress))
                    {
                        return(LoadToBuffer(stream2));
                    }
                }
                else if (this.Algorithm == CompressionAlgorithm.Deflate)
                {
                    using (DeflateStream stream3 = new DeflateStream(ms, CompressionMode.Decompress))
                    {
                        return(LoadToBuffer(stream3));
                    }
                }
                else
                {
                    using (var bzipStream = new BZip2InputStream(ms))
                    {
                        return(LoadToBuffer(bzipStream));
                        //这里要指明要读入的格式,要不就有乱码
                        //StreamReader reader = new StreamReader(bzipStream, Encoding.UTF8);
                        //reader.
                        //var commonString = reader.ReadToEnd();

                        //return commonString;
                    }
                }
            }
        }
Ejemplo n.º 11
0
        private int CompressBlock(ref byte[] input, int startPos, int blockSize, out CompressionAlgorithm compressionAlgorithm)
        {
            if (ZstdLevel < 1)
            {
                compressionAlgorithm = CompressionAlgorithm.None;
                return(blockSize);
            }

            // compress
            using (var memoryStream = new MemoryStream())
                using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Compress))
                {
                    compressionStream.CompressionLevel = ZstdLevel;
                    compressionStream.Write(input, startPos, blockSize);
                    compressionStream.Close();
                    var tmp = memoryStream.ToArray();
                    if (tmp.Length < blockSize)
                    {
                        compressionAlgorithm = CompressionAlgorithm.Zstandard;
                        Array.Copy(tmp, 0, input, startPos, tmp.Length);
                        return(tmp.Length);
                    }

                    compressionAlgorithm = CompressionAlgorithm.None;
                    return(blockSize);
                }
        }
Ejemplo n.º 12
0
        private void BasicSMB2Compression_Encrypted(CompressionAlgorithm compressionAlgorithm)
        {
            CheckCompressionAndEncryptionApplicability(compressionAlgorithm, true);
            var compressionAlgorithms = new CompressionAlgorithm[] { compressionAlgorithm };

            SMB2CompressionTest(compressionAlgorithms, CompressionTestVariant.BasicReadWrite, enableEncryption: true);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Get compressor instance.
 /// </summary>
 /// <param name="compressionAlgorithm">The compression algorithm to use.</param>
 /// <returns>The compressor.</returns>
 public static XcaCompressor GetCompressor(CompressionAlgorithm compressionAlgorithm)
 {
     if (!compressorInstances.ContainsKey(compressionAlgorithm))
     {
         throw new InvalidOperationException("Invalid compressor algorithm!");
     }
     return compressorInstances[compressionAlgorithm];
 }
 public CompressionMessageFormatter(CompressionAlgorithm algorithm, OperationDescription description, DataContractFormatAttribute dataContractFormatAttribute, DataContractSerializerOperationBehavior serializerFactory)
 {
     this.MessageCompressor = new MessageCompressor(algorithm);
     Type innerFormatterType = Type.GetType(DataContractSerializerOperationFormatterTypeName);
     var innerFormatter = Activator.CreateInstance(innerFormatterType, description, dataContractFormatAttribute, serializerFactory);
     this.InnerClientMessageFormatter = innerFormatter as IClientMessageFormatter;
     this.InnerDispatchMessageFormatter = innerFormatter as IDispatchMessageFormatter;
 }
Ejemplo n.º 15
0
 public Tid(Bitmap bitmap, MixedString filename, CompressionAlgorithm compression = CompressionAlgorithm.None, byte version = 0x90)
 {
     Bitmap      = bitmap;
     Filename    = filename.GetCustomLength(0x20);
     Compression = compression;
     Version     = version;
     Loaded      = true;
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Creates the Compression stream that will handle inflation.
        /// </summary>
        private void InflateInit(CompressionAlgorithm algorithm)
        {
            _compression_struct = new CompressionStreamStruct();

            var status = CompressionStreamStruct.compression_stream_init(ref _compression_struct, StreamOperation.Decode, algorithm);

            if (status != CompressionStatus.Ok)
                throw new InvalidOperationException(status.ToString()); }
Ejemplo n.º 17
0
 /// <summary>
 /// Get decompressor instance.
 /// </summary>
 /// <param name="compressionAlgorithm">The compression algorithm to use.</param>
 /// <returns>The decompressor.</returns>
 public static XcaDecompressor GetDecompressor(CompressionAlgorithm compressionAlgorithm)
 {
     if (!compressorInstances.ContainsKey(compressionAlgorithm))
     {
         throw new InvalidOperationException();
     }
     return(decompressorInstances[compressionAlgorithm]);
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Sets the parser back to the beginning of the file.
 /// </summary>
 public void Reset()
 {
     m_compressionAlgorithm = CompressionAlgorithm.None;
     m_compressionStyle     = CompressionStyle.None;
     m_fileReader.BaseStream.Seek(0, SeekOrigin.Begin);
     m_hasNextRecord = true;
     m_headerAddresses.Clear();
     m_exceptionList.Clear();
 }
Ejemplo n.º 19
0
        /// <summary>
        /// Indicates that all data that will be written into the stream should be compressed.
        /// </summary>
        /// <param name="algorithm">Compression algorithm that should be used</param>
        public void BeginCompression(CompressionAlgorithm algorithm)
        {
            Write(ref algorithm);

            if (algorithm == CompressionAlgorithm.Deflate)
            {
                _activeStream = new DeflateStream(_stream, CompressionLevel.SmallestSize, true);
            }
        }
Ejemplo n.º 20
0
        public CompressionMessageFormatter(CompressionAlgorithm algorithm, OperationDescription description, DataContractFormatAttribute dataContractFormatAttribute, DataContractSerializerOperationBehavior serializerFactory)
        {
            this.MessageCompressor = new MessageCompressor(algorithm);
            Type innerFormatterType = Type.GetType(DataContractSerializerOperationFormatterTypeName);
            var  innerFormatter     = Activator.CreateInstance(innerFormatterType, description, dataContractFormatAttribute, serializerFactory);

            this.InnerClientMessageFormatter   = innerFormatter as IClientMessageFormatter;
            this.InnerDispatchMessageFormatter = innerFormatter as IDispatchMessageFormatter;
        }
        public void TestDecodeRealFile(CompressionAlgorithm algorithm, string compressedFile)
        {
#if MONOMAC
            string compressedFilePath = Path.Combine(NSBundle.MainBundle.BundlePath, $"Contents/Resources/{compressedFile}");
#else
            string compressedFilePath = Path.Combine(NSBundle.MainBundle.BundlePath, compressedFile);
#endif
            DecodeRealFile(algorithm, compressedFilePath, uncompressedFilePath);
        }
 //The GZip encoder wraps an inner encoder
 //We require a factory to be passed in that will create this inner encoder
 public GZipMessageEncoderFactory(MessageEncoderFactory messageEncoderFactory, CompressionAlgorithm compressionAlgorithm)
 {
     if (messageEncoderFactory == null)
     {
         throw new ArgumentNullException("messageEncoderFactory", "A valid message encoder factory must be passed to the CompressionEncoder");
     }
     encoder = new GZipMessageEncoder(messageEncoderFactory.Encoder, compressionAlgorithm);
     this.compressionAlgorithm = compressionAlgorithm;
     this.innerFactory         = messageEncoderFactory;
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Initializes a new KdbxWriter with the given options.
 /// </summary>
 /// <param name="cipher">The algorithm to use for encrypting the database.</param>
 /// <param name="rngAlgorithm">The random number generator used for String protection.</param>
 /// <param name="compression">The document compression algorithm.</param>
 /// <param name="kdfParams">Recipe for transforming the raw key. This will be reseeded.</param>
 private KdbxWriter(
     EncryptionAlgorithm cipher,
     RngAlgorithm rngAlgorithm,
     CompressionAlgorithm compression,
     KdfParameters kdfParams
     )
     : base()
 {
     SeedHeaderData(cipher, rngAlgorithm, compression, kdfParams);
 }
		public static MethodInfo GetDecompress(ModuleBuilder modBldr, GeneratorOptions config, CompressionAlgorithm alg)
		{
			switch (alg)
			{
				case CompressionAlgorithm.LZF:
					return GetDecompressLZF(modBldr, config);
				default:
					throw new Exception("Unknown CompressionAlgorithm!");
			}
		}
        public void TestDecodeRealFile(CompressionAlgorithm algorithm, string compressedFile)
        {
            if (!TestRuntime.CheckXcodeVersion(7, 0))
            {
                Assert.Ignore("Requires iOS 9.0+ or macOS 10.11+");
            }
            string compressedFilePath = Path.Combine(NSBundle.MainBundle.ResourcePath, compressedFile);

            DecodeRealFile(algorithm, compressedFilePath, uncompressedFilePath);
        }
Ejemplo n.º 26
0
        public Smb2CompressionInfo()
        {
            CompressAllPackets            = false;
            CompressionIds                = new CompressionAlgorithm[0];
            PreferredCompressionAlgorithm = CompressionAlgorithm.NONE;

            CompressBufferOnly = false;

            SupportChainedCompression = false;
        }
 //We require an inner encoder to be supplied (see comment above)
 internal GZipMessageEncoder(MessageEncoder messageEncoder, CompressionAlgorithm compressionAlgorithm)
     : base()
 {
     if (messageEncoder == null)
     {
         throw new ArgumentNullException("messageEncoder", "A valid message encoder must be passed to the CompressionEncoder");
     }
     innerEncoder = messageEncoder;
     this.compressionAlgorithm = compressionAlgorithm;
 }
Ejemplo n.º 28
0
        private void FetchSmb2CompressionInfo(Smb2Info smb2Info)
        {
            if (smb2Info.MaxSupportedDialectRevision < DialectRevision.Smb311)
            {
                logWriter.AddLog(LogLevel.Information, "SMB dialect less than 3.1.1 does not support compression.");
                smb2Info.SupportedCompressionAlgorithms = new CompressionAlgorithm[0];
                return;
            }

            var possibleCompressionAlogrithms = new CompressionAlgorithm[] { CompressionAlgorithm.LZ77, CompressionAlgorithm.LZ77Huffman, CompressionAlgorithm.LZNT1 };

            // Iterate all possible compression algorithm for Windows will only return only one supported compression algorithm in response.
            var result = possibleCompressionAlogrithms.Where(compressionAlgorithm =>
            {
                using (var client = new Smb2Client(new TimeSpan(0, 0, defaultTimeoutInSeconds)))
                {
                    client.ConnectOverTCP(SUTIpAddress);

                    DialectRevision selectedDialect;
                    byte[] gssToken;
                    Packet_Header responseHeader;
                    NEGOTIATE_Response responsePayload;

                    uint status = client.Negotiate(
                        0,
                        1,
                        Packet_Header_Flags_Values.NONE,
                        0,
                        new DialectRevision[] { DialectRevision.Smb311 },
                        SecurityMode_Values.NEGOTIATE_SIGNING_ENABLED,
                        Capabilities_Values.NONE,
                        Guid.NewGuid(),
                        out selectedDialect,
                        out gssToken,
                        out responseHeader,
                        out responsePayload,
                        preauthHashAlgs: new PreauthIntegrityHashID[] { PreauthIntegrityHashID.SHA_512 },
                        compressionAlgorithms: new CompressionAlgorithm[] { compressionAlgorithm }
                        );

                    if (status == Smb2Status.STATUS_SUCCESS && client.CompressionInfo.CompressionIds.Length == 1 && client.CompressionInfo.CompressionIds[0] == compressionAlgorithm)
                    {
                        logWriter.AddLog(LogLevel.Information, $"Compression algorithm: {compressionAlgorithm} is supported by SUT.");
                        return(true);
                    }
                    else
                    {
                        logWriter.AddLog(LogLevel.Information, $"Compression algorithm: {compressionAlgorithm} is not supported by SUT.");
                        return(false);
                    }
                }
            });

            smb2Info.SupportedCompressionAlgorithms = result.ToArray();
        }
Ejemplo n.º 29
0
 /// <summary>
 /// Initializes a new KdbxWriter with the given options.
 /// </summary>
 /// <param name="tokenList">An enumeration of security tokens used for encryption.</param>
 /// <param name="cipher">The algorithm to use for encrypting the database.</param>
 /// <param name="rngAlgorithm">The random number generator used for String protection.</param>
 /// <param name="compression">The document compression algorithm.</param>
 /// <param name="kdfParams">Recipe for transforming the raw key.</param>
 public KdbxWriter(
     IEnumerable <ISecurityToken> securityTokens,
     EncryptionAlgorithm cipher,
     RngAlgorithm rngAlgorithm,
     CompressionAlgorithm compression,
     KdfParameters kdfParams
     )
     : this(cipher, rngAlgorithm, compression, kdfParams)
 {
     this.securityTokens = securityTokens ?? throw new ArgumentNullException(nameof(securityTokens));
 }
Ejemplo n.º 30
0
 public override void Deserialize(Stream input)
 {
     this.Name       = input.ReadStringAlignedU8();
     this.Version    = input.ReadValueU32();
     this.Width      = input.ReadValueU32();
     this.Height     = input.ReadValueU32();
     this.Unknown4   = input.ReadValueU32();
     this.Unknown5   = input.ReadValueU32();
     this.NumMipMaps = input.ReadValueU32();
     this.Algorithm  = (CompressionAlgorithm)input.ReadValueU32();
 }
Ejemplo n.º 31
0
        /// <summary>
        /// Indicates that all data that will be read from the stream has been compressed.
        /// </summary>
        public void BeginCompression()
        {
            CompressionAlgorithm algorithm = CompressionAlgorithm.None;

            Read(ref algorithm);

            if (algorithm == CompressionAlgorithm.Deflate)
            {
                _activeStream = new DeflateStream(_stream, CompressionMode.Decompress, true);
            }
        }
Ejemplo n.º 32
0
 // 解压缩
 public static void DecompressData(byte[] inBytes, uint startPos, uint inLen, ref byte[] outBytes, ref uint outLen, CompressionAlgorithm algorithm = CompressionAlgorithm.ZLIB)
 {
     if (CompressionAlgorithm.ZLIB == algorithm)
     {
         DecompressByteZipNet(inBytes, startPos, inLen, ref outBytes, ref outLen);
     }
     else
     {
         DecompressStrLZMA(inBytes, startPos, inLen, ref outBytes, ref outLen);
     }
 }
        //The GZip encoder wraps an inner encoder
        //We require a factory to be passed in that will create this inner encoder
        public CompressMessageEncoderFactory(MessageEncoderFactory messageEncoderFactory,
            CompressionAlgorithm compressionAlgorithm)
        {
            if (messageEncoderFactory == null)
                throw new ArgumentNullException("messageEncoderFactory",
                                                "A valid message encoder factory must be passed to the CompressionEncoder");

            _encoder = new MyCompressionMessageEncoder(messageEncoderFactory.Encoder, compressionAlgorithm);
            _compressionAlgorithm = compressionAlgorithm;
            _innerFactory = messageEncoderFactory;
        }
Ejemplo n.º 34
0
        internal void InitializeInflater(Stream stream, CompressionAlgorithm algorithm, bool leaveOpen)
        {
            if (!stream.CanRead)
            {
                throw new ArgumentException("Stream does not support reading.", nameof(stream));
            }

            _inflater  = new Inflater(algorithm);
            _stream    = stream;
            _mode      = CompressionMode.Decompress;
            _leaveOpen = leaveOpen;
        }
Ejemplo n.º 35
0
 public static string ConvertToString(CompressionAlgorithm algorithm)
 {
     switch (algorithm)
     {
         case CompressionAlgorithm.GZip:
             return gzip;
         case CompressionAlgorithm.Deflate:
             return deflate;
         default:
             throw new NotSupportedException("Compression mode " + algorithm + " is not supported");
     }
 }
 public static SquishFlags ToSquishFlags(this CompressionAlgorithm compression)
 {
     if (compression == CompressionAlgorithm.Dxt1)
     {
         return(SquishFlags.Dxt1);
     }
     if (compression == CompressionAlgorithm.Dxt5)
     {
         return(SquishFlags.Dxt5);
     }
     return(0);
 }
Ejemplo n.º 37
0
 public static byte[] Compress(byte[] decompressedData, CompressionAlgorithm algorithm)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         if (algorithm == CompressionAlgorithm.Deflate)
         {
             GZipStream stream2 = new GZipStream(stream, CompressionMode.Compress, true);
             stream2.Write(decompressedData, 0, decompressedData.Length);
             stream2.Close();
         }
         else
         {
             DeflateStream stream3 = new DeflateStream(stream, CompressionMode.Compress, true);
             stream3.Write(decompressedData, 0, decompressedData.Length);
             stream3.Close();
         }
         return stream.ToArray();
     }
 }
Ejemplo n.º 38
0
 public virtual byte[] Decompress(byte[] compressedData, CompressionAlgorithm algorithm)
 {
     using (MemoryStream stream = new MemoryStream(compressedData))
     {
         if (algorithm == CompressionAlgorithm.Deflate)
         {
             using (GZipStream stream2 = new GZipStream(stream, CompressionMode.Decompress))
             {
                 return LoadToBuffer(stream2);
             }
         }
         else
         {
             using (DeflateStream stream3 = new DeflateStream(stream, CompressionMode.Decompress))
             {
                 return LoadToBuffer(stream3);
             }
         }
     }
 }
Ejemplo n.º 39
0
        public static byte[] Compress(byte[] decompressedData, CompressionAlgorithm algorithm, CompressionLevel Level)
        {
            var memoryStream = new MemoryStream();

            switch (algorithm)
            {
                case CompressionAlgorithm.GZip:
                    var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true);
                    gZipStream.Write(decompressedData, 0, decompressedData.Length);
                    gZipStream.Close();
                    break;
                case CompressionAlgorithm.Deflate:
                    var compressedDeflateStream = new DeflateStream(memoryStream, CompressionMode.Compress, true);
                    compressedDeflateStream.Write(decompressedData, 0, decompressedData.Length);
                    compressedDeflateStream.Close();
                    break;
                default:
                    throw new NotSupportedException(
                        String.Format("Unknown compression algorithm parameter must be deflate or gzip: {0}", algorithm));
            }

            return memoryStream.ToArray();
        }
Ejemplo n.º 40
0
 public static byte[] Decompress(byte[] compressedData, CompressionAlgorithm algorithm, CompressionLevel Level)
 {
     var memoryStream = new MemoryStream(compressedData);
     byte[] buffer;
     switch (algorithm)
     {
         case CompressionAlgorithm.GZip:
             using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
             {
                 buffer = LoadToBuffer(gZipStream);
             }
             break;
         case CompressionAlgorithm.Deflate:
             using (var deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress))
             {
                 buffer = LoadToBuffer(deflateStream);
             }
             break;
         default:
             throw new NotSupportedException(
                 String.Format("Unknown compression algorithm parameter must be deflate or gzip: {0}", algorithm));
     }
     return buffer;
 }
Ejemplo n.º 41
0
 private static byte[] Decompress(byte[] responseBytes, CompressionAlgorithm compressionAlgorithm)
 {
     Stream decompressingStream;
     switch (compressionAlgorithm)
     {
         case CompressionAlgorithm.GZip:
             decompressingStream = new GZipStream(new MemoryStream(responseBytes), CompressionMode.Decompress);
             break;
         case CompressionAlgorithm.Deflate:
             decompressingStream = new DeflateStream(new MemoryStream(responseBytes), CompressionMode.Decompress);
             break;
         default:
             throw new NotSupportedException("Unsupported compression algorithm: " + compressionAlgorithm + ".");
     }
     try
     {
         var buffer = new byte[BufferSize];
         using (var outputMemoryStream = new MemoryStream())
         {
             while (true)
             {
                 var bytesRead = decompressingStream.Read(buffer, 0, buffer.Length);
                 if (bytesRead <= 0)
                 {
                     break;
                 }
                 outputMemoryStream.Write(buffer, 0, bytesRead);
             }
             return outputMemoryStream.ToArray();
         }
     }
     finally
     {
         decompressingStream.Close();
     }
 }
Ejemplo n.º 42
0
 public MessageCompressor(CompressionAlgorithm algorithm, CompressionLevel level)
 {
     _algorithm = algorithm;
     _level = level;
 }
Ejemplo n.º 43
0
 public static void AddCompressionHeader(this Message message, CompressionAlgorithm algorithm)
 {
     message.Headers.Add(MessageHeader.CreateHeader(Constants.CompressionMessageHeader, Constants.Namespace, string.Format("algorithm = \"{0}\"", algorithm)));
 }
Ejemplo n.º 44
0
        public void Compress(CompressionAlgorithm algorithm)
        {
            var buffer = memoryStream.ToArray();
            memoryStream.Close();
            var ms = new MemoryStream();

            var stream = algorithm == CompressionAlgorithm.Zlib
                ? new ZlibStream(ms, CompressionMode.Compress, true)
                : new DeflateStream(ms, CompressionMode.Compress, true);

            using (stream)
                stream.Write(buffer, 0, buffer.Length);

            memoryStream = ms;
            dataOutput = new DataOutput(new AmfWriter(memoryStream, serializationContext));
            dataInput = new DataInput(new AmfReader(memoryStream, serializationContext));
        }
Ejemplo n.º 45
0
        public void Uncompress(CompressionAlgorithm algorithm)
        {
            Position = 0;
            var ms = new MemoryStream();
            var buffer = new byte[1024];

            // The zlib format is specified by RFC 1950. Zlib also uses deflate, plus 2 or 6 header bytes, and a 4 byte checksum at the end. 
            // The first 2 bytes indicate the compression method and flags. If the dictionary flag is set, then 4 additional bytes will follow.
            // Preset dictionaries aren't very common and we don't support them
            var deflateStream = algorithm == CompressionAlgorithm.Zlib
                ? new ZlibStream(memoryStream, CompressionMode.Decompress, false)
                : new DeflateStream(memoryStream, CompressionMode.Decompress, false);

            while (true)
            {
                var readCount = deflateStream.Read(buffer, 0, buffer.Length);
                if (readCount == 0)
                    break;
                ms.Write(buffer, 0, readCount);
            }

            memoryStream.Dispose();
            memoryStream = ms;
            memoryStream.Position = 0;
            dataOutput = new DataOutput(new AmfWriter(memoryStream, serializationContext));
            dataInput = new DataInput(new AmfReader(memoryStream, serializationContext));
        }
Ejemplo n.º 46
0
        // 解压
        public uint uncompress(uint len_ = 0, CompressionAlgorithm algorithm = CompressionAlgorithm.ZLIB)
        {
            len_ = (len_ == 0 ? length : len_);

            byte[] retByte = null;
            uint retSize = 0;
            Compress.DecompressData(m_dynBuff.buff, position, len_, ref retByte, ref retSize, algorithm);

            replace(retByte, 0, retSize, position, len_);

            //check();

            return retSize;
        }
Ejemplo n.º 47
0
 public static String Decompress(this String data, CompressionAlgorithm algorithm)
 {
     return Decompress(data, Encoding.UTF8, algorithm);
 }
Ejemplo n.º 48
0
        /// <summary>
        /// 压缩数据流。
        /// </summary>
        /// <param name="algorithm">要使用的压缩算法。CompressionAlgorithms 类中提供了常用的压缩算法。</param>
        /// <param name="inputBuffer">指定输入缓冲区。</param>
        /// <param name="inputOffset">指定输入缓冲区的开始位置。</param>
        /// <param name="inputCount">指定输入缓冲区的长度。</param>
        /// <param name="outputBuffer">指定输出缓冲区。</param>
        /// <param name="outputOffset">指定输出缓冲区的开始位置。</param>
        /// <returns>返回压缩后的字节数。</returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static int Compression(CompressionAlgorithm algorithm, CompressionMode mode, byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
        {
            if (algorithm == null || inputBuffer == null || outputBuffer == null) throw new ArgumentNullException();

            if (mode == CompressionMode.Compress)
                return algorithm.CompressBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
            else
                return algorithm.DecompressBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
        }
Ejemplo n.º 49
0
        /// <summary>
        /// 压缩数据流。
        /// </summary>
        /// <param name="algorithm">要使用的压缩算法。CompressionAlgorithms 类中提供了常用的压缩算法。</param>
        /// <param name="inputBuffer">指定输入缓冲区。</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static byte[] Compression(CompressionAlgorithm algorithm, CompressionMode mode, byte[] inputBuffer)
        {
            if (algorithm == null || inputBuffer == null) throw new ArgumentNullException();

            if (mode == CompressionMode.Compress)
                return algorithm.CompressFinalBlock(inputBuffer, 0, inputBuffer.Length);
            else
                return algorithm.DecompressFinalBlock(inputBuffer, 0, inputBuffer.Length);
        }
Ejemplo n.º 50
0
        public static Byte[] Decompress(this Byte[] data, CompressionAlgorithm algorithm)
        {
            if (data == null || data.Length <= 0)
                    return new Byte[0];

                //--- Define Location To Store Compressed n Decompressed Data ---//
                MemoryStream cprMS = new MemoryStream(data);
                MemoryStream dcprMS = new MemoryStream();
                Int32 size = 4096;
                cprMS.Position = 0;

                //--- Create Compression Object ---//
                Stream zipper;
                if (algorithm == CompressionAlgorithm.GZipStream)
                    zipper = new GZipStream(cprMS, CompressionMode.Decompress);
                else
                    zipper = new DeflateStream(cprMS, CompressionMode.Decompress);

                //--- Decompress ---//
                Byte[] dcprData = new Byte[size];
                Int32 Count = 0;
                while (true)
                {
                    Count = zipper.Read(dcprData, 0, dcprData.Length);
                    if (Count > 0)
                        dcprMS.Write(dcprData, 0, Count);
                    else
                        break;
                }

                return dcprMS.ToArray();
        }
Ejemplo n.º 51
0
 public static String Decompress(this String data, Encoding encoder, CompressionAlgorithm algorithm)
 {
     Byte[] dataBytes = Convert.FromBase64String(data);
         return encoder.GetString(Decompress(dataBytes, algorithm));
 }
            //Helper method to decompress an array of bytes
            private static ArraySegment<byte> DecompressBuffer(ArraySegment<byte> buffer, BufferManager bufferManager,
                CompressionAlgorithm compressionAlgorithm)
            {
                var memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count);
                var decompressedStream = new MemoryStream();
                //int totalRead = 0;
                const int blockSize = 1024;
                byte[] tempBuffer = bufferManager.TakeBuffer(blockSize);
                using (Stream compressedStream = compressionAlgorithm == CompressionAlgorithm.GZip
                                                     ? new GZipStream(memoryStream, CompressionMode.Decompress)
                                                     : (Stream)
                                                       new DeflateStream(memoryStream, CompressionMode.Decompress))
                {
                    while (true)
                    {
                        int bytesRead = compressedStream.Read(tempBuffer, 0, blockSize);
                        if (bytesRead == 0)
                            break;
                        decompressedStream.Write(tempBuffer, 0, bytesRead);
                        //totalRead += bytesRead;
                    }
                }
                bufferManager.ReturnBuffer(tempBuffer);

                byte[] decompressedBytes = decompressedStream.ToArray();
                byte[] bufferManagerBuffer = bufferManager.TakeBuffer(decompressedBytes.Length + buffer.Offset);
                Array.Copy(buffer.Array, 0, bufferManagerBuffer, 0, buffer.Offset);
                Array.Copy(decompressedBytes, 0, bufferManagerBuffer, buffer.Offset, decompressedBytes.Length);

                var byteArray = new ArraySegment<byte>(bufferManagerBuffer, buffer.Offset,
                                                       decompressedBytes.Length);
                bufferManager.ReturnBuffer(buffer.Array);

                return byteArray;
            }
            //Helper method to compress an array of bytes
            private static ArraySegment<byte> CompressBuffer(ArraySegment<byte> buffer, BufferManager bufferManager,
                int messageOffset,
                CompressionAlgorithm compressionAlgorithm)
            {
                var memoryStream = new MemoryStream();

                using (Stream compressedStream = compressionAlgorithm == CompressionAlgorithm.GZip
                                                     ? new GZipStream(memoryStream, CompressionMode.Compress, true)
                                                     : (Stream)
                                                       new DeflateStream(memoryStream, CompressionMode.Compress, true)
                    )
                {
                    compressedStream.Write(buffer.Array, buffer.Offset, buffer.Count);
                }

                byte[] compressedBytes = memoryStream.ToArray();
                int totalLength = messageOffset + compressedBytes.Length;
                byte[] bufferedBytes = bufferManager.TakeBuffer(totalLength);

                Array.Copy(compressedBytes, 0, bufferedBytes, messageOffset, compressedBytes.Length);

                bufferManager.ReturnBuffer(buffer.Array);
                var byteArray = new ArraySegment<byte>(bufferedBytes, messageOffset,
                                                       compressedBytes.Length);

                return byteArray;
            }
Ejemplo n.º 54
0
        /// <summary>
        /// 压缩数据流。
        /// </summary>
        /// <param name="algorithm">要使用的压缩算法。CompressionAlgorithms 类中提供了常用的压缩算法。</param>
        /// <param name="inputStream">指定输入流。</param>
        /// <param name="inputCount">输入流的长度,或 -1 以代表读至流的末尾。</param>
        /// <param name="outputStream">指定输出流。</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static void Compression(CompressionAlgorithm algorithm, CompressionMode mode, Stream inputStream, int inputCount, Stream outputStream)
        {
            if (algorithm == null || inputStream == null || outputStream == null) throw new ArgumentNullException();
            if (inputCount == 0)
                return;

            var countRead = 0;
            var lastTimeRead = 0;
            var inputBuffer = new byte[Compressor.ChunkSize];
            do {
                lastTimeRead = inputStream.Read(inputBuffer, 0, inputCount == -1 ? Compressor.ChunkSize : Math.Min(Compressor.ChunkSize, (inputCount - countRead)));
                if (lastTimeRead != 0) {
                    countRead += lastTimeRead;
                    if (mode == CompressionMode.Compress) {
                        var ret = algorithm.CompressFinalBlock(inputBuffer, 0, lastTimeRead);
                        outputStream.Write(ret, 0, ret.Length);
                    }
                    else {
                        var ret = algorithm.DecompressFinalBlock(inputBuffer, 0, lastTimeRead);
                        outputStream.Write(ret, 0, ret.Length);
                    }
                }
            }
            while ((countRead < inputCount && inputCount != -1) || (inputCount == -1 && lastTimeRead != 0));
        }
Ejemplo n.º 55
0
 /// <summary>
 /// 压缩数据流。
 /// </summary>
 /// <param name="algorithm">要使用的压缩算法。CompressionAlgorithms 类中提供了常用的压缩算法。</param>
 /// <param name="inputStream">指定输入流。</param>
 /// <param name="outputStream">指定输出流。</param>
 /// <returns></returns>
 /// <exception cref="System.ArgumentNullException"></exception>
 public static void Compression(CompressionAlgorithm algorithm, CompressionMode mode, Stream inputStream, Stream outputStream)
 {
     Compression(algorithm, mode, inputStream, -1, outputStream);
 }
Ejemplo n.º 56
0
        /// <summary>
        /// 压缩数据流。
        /// </summary>
        /// <param name="algorithm">要使用的压缩算法。CompressionAlgorithms 类中提供了常用的压缩算法。</param>
        /// <param name="inputStream">指定输入流。</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static byte[] Compression(CompressionAlgorithm algorithm, CompressionMode mode, Stream inputStream)
        {
            var ms = new MemoryStream();
            Compression(algorithm, mode, inputStream, -1, ms);

            return ms.ToArray();
        }
Ejemplo n.º 57
0
 public DataCompressor(CompressionAlgorithm algorithm)
 {
     this.Algorithm = algorithm;
 }
Ejemplo n.º 58
0
		public static byte[] CompressData(byte[] data, CompressionAlgorithm alg)
		{
			switch (alg)
			{
				case CompressionAlgorithm.None:
					return data;
				case CompressionAlgorithm.LZF:
					return LZF.Compress(data);
				default:
					throw new Exception("Unknown compression algorithm!");
			}
		}
 //We require an inner encoder to be supplied (see comment above)
 internal MyCompressionMessageEncoder(MessageEncoder messageEncoder,
     CompressionAlgorithm compressionAlgorithm)
 {
     if (messageEncoder == null)
         throw new ArgumentNullException("messageEncoder",
                                         "A valid message encoder must be passed to the CompressionEncoder");
     _innerEncoder = messageEncoder;
     _compressionAlgorithm = compressionAlgorithm;
 }
Ejemplo n.º 60
0
 public static String Compress(this String data, Encoding encoder, CompressionAlgorithm algorithm)
 {
     Byte[] dataBytes = data.ToByteArray(encoder);
         return Convert.ToBase64String(Compress(dataBytes, algorithm));
 }