Example #1
0
        /// <summary>
        /// Check if the stream is in ZLib format
        /// </summary>
        /// <param name="stream">Stream to check</param>
        /// <returns>Returns <see langword="true"/> in case the stream is in ZLib format,
        /// otherwise returns <see langword="true"/> or error.</returns>
        public static bool IsZLibStream(Stream stream)
        {
            bool       bResult = false;
            int        CMF     = 0;
            int        Flag    = 0;
            ZLibHeader header;

            // We check if the sequence is in position 0, if not, we throw an exception
            if (stream.Position != 0)
            {
                throw new ArgumentOutOfRangeException("Sequence must be at position 0");
            }

            // We check if we can read the two bytes that make up the header
            if (stream.CanRead == true)
            {
                CMF  = stream.ReadByte();
                Flag = stream.ReadByte();
                try
                {
                    header  = ZLibHeader.DecodeHeader(CMF, Flag);
                    bResult = header.IsZLibSupported;
                }
                catch
                {
                    //Nada
                }
            }

            return(bResult);
        }
Example #2
0
        public static ZLibHeader DecodeHeader(int pCMF, int pFlag)
        {
            ZLibHeader result = new ZLibHeader();

            //Ensure that parameters are bytes
            pCMF  = pCMF & 0x0FF;
            pFlag = pFlag & 0x0FF;

            //Decode bytes
            result.CompressionInfo   = Convert.ToByte((pCMF & 0xF0) >> 4);
            result.CompressionMethod = Convert.ToByte(pCMF & 0x0F);

            result.CheckFlag        = Convert.ToByte(pFlag & 0x1F);
            result.DictFlag         = Convert.ToBoolean(Convert.ToByte((pFlag & 0x20) >> 5));
            result.CompressionLevel = (ZLibCompressionLevel)Convert.ToByte((pFlag & 0xC0) >> 6);

            result.IsZLibSupported = (result.CompressionMethod == 8) &&
                                     (result.CompressionInfo == 7) && (((pCMF * 256 + pFlag) % 31 == 0)) && (result.DictFlag == false);

            return(result);
        }
Example #3
0
        /// <summary>
        /// Initialize the stream header in ZLib format
        /// </summary>
        private void InitializeZLibHeader()
        {
            byte[] bytesHeader;

            // We set the header configuration
            ZLibHeader header = new ZLibHeader();

            header.CompressionMethod = 8; //Deflate
            header.CompressionInfo   = 7;

            header.DictFlag         = false; // No dictionary
            header.CompressionLevel = _compressionLevel;
            //switch (_compressionLevel)
            //{
            //    case CompressionLevel.NoCompression:
            //        {
            //            header.FLevel = ZLibCompressionLevel.Faster;
            //            break;
            //        }
            //    case CompressionLevel.Fastest:
            //        {
            //            header.FLevel = ZLibCompressionLevel.Default;
            //            break;
            //        }
            //    case CompressionLevel.Optimal:
            //        {
            //            header.FLevel = ZLibCompressionLevel.Optimal;
            //            break;
            //        }
            //}

            bytesHeader = header.EncodeZlibHeader();

            _rawStream.WriteByte(bytesHeader[0]);
            _rawStream.WriteByte(bytesHeader[1]);
        }