Zlib.InflateManager.Inflate C# (CSharp) Метод

Inflate() приватный Метод

private Inflate ( ) : int
Результат int
        internal int Inflate()
        {
            if (this.codec.InputBuffer == null)
            {
                throw new ZlibException("InputBuffer is null. ");
            }

            // int f = (flush == FlushType.Finish) ? ZlibConstants.Z_BUF_ERROR : ZlibConstants.Z_OK;

            // workitem 8870
            const int f = ZlibConstants.Zok;
            int r = ZlibConstants.ZBufError;

            while (true)
            {
                switch (this.mode)
                {
                    case InflateManagerMode.Method:
                        if (this.codec.AvailableBytesIn == 0)
                        {
                            return r;
                        }

                        r = f;
                        this.codec.AvailableBytesIn--;
                        this.codec.TotalBytesIn++;
                        if (((this.method = this.codec.InputBuffer[this.codec.NextIn++]) & 0xf) != ZDeflated)
                        {
                            this.mode = InflateManagerMode.Bad;
                            this.codec.Message = string.Format(
                                CultureInfo.CurrentCulture, "unknown compression method (0x{0:X2})", this.method);
                            break;
                        }

                        if ((this.method >> 4) + 8 > this.wbits)
                        {
                            this.mode = InflateManagerMode.Bad;
                            this.codec.Message = string.Format(
                                CultureInfo.CurrentCulture, "invalid window size ({0})", (this.method >> 4) + 8);
                            break;
                        }

                        this.mode = InflateManagerMode.Flag;
                        break;

                    case InflateManagerMode.Flag:
                        if (this.codec.AvailableBytesIn == 0)
                        {
                            return r;
                        }

                        r = f;
                        this.codec.AvailableBytesIn--;
                        this.codec.TotalBytesIn++;
                        int b = this.codec.InputBuffer[this.codec.NextIn++] & 0xff;

                        if ((((this.method << 8) + b) % 31) != 0)
                        {
                            this.mode = InflateManagerMode.Bad;
                            this.codec.Message = "incorrect header check";
                            break;
                        }

                        this.mode = ((b & PresetDict) == 0) ? InflateManagerMode.Blocks : InflateManagerMode.DicT4;
                        break;

                    case InflateManagerMode.DicT4:
                        if (this.codec.AvailableBytesIn == 0)
                        {
                            return r;
                        }

                        r = f;
                        this.codec.AvailableBytesIn--;
                        this.codec.TotalBytesIn++;
                        this.expectedCheck = (uint)((this.codec.InputBuffer[this.codec.NextIn++] << 24) & 0xff000000);
                        this.mode = InflateManagerMode.DicT3;
                        break;

                    case InflateManagerMode.DicT3:
                        if (this.codec.AvailableBytesIn == 0)
                        {
                            return r;
                        }

                        r = f;
                        this.codec.AvailableBytesIn--;
                        this.codec.TotalBytesIn++;
                        this.expectedCheck += (uint)((this.codec.InputBuffer[this.codec.NextIn++] << 16) & 0x00ff0000);
                        this.mode = InflateManagerMode.DicT2;
                        break;

                    case InflateManagerMode.DicT2:

                        if (this.codec.AvailableBytesIn == 0)
                        {
                            return r;
                        }

                        r = f;
                        this.codec.AvailableBytesIn--;
                        this.codec.TotalBytesIn++;
                        this.expectedCheck += (uint)((this.codec.InputBuffer[this.codec.NextIn++] << 8) & 0x0000ff00);
                        this.mode = InflateManagerMode.DicT1;
                        break;

                    case InflateManagerMode.DicT1:
                        if (this.codec.AvailableBytesIn == 0)
                        {
                            return r;
                        }

                        this.codec.AvailableBytesIn--;
                        this.codec.TotalBytesIn++;
                        this.expectedCheck += (uint)(this.codec.InputBuffer[this.codec.NextIn++] & 0x000000ff);
                        this.codec.Adler32 = this.expectedCheck;
                        this.mode = InflateManagerMode.DicT0;
                        return ZlibConstants.ZNeedDict;

                    case InflateManagerMode.DicT0:
                        this.mode = InflateManagerMode.Bad;
                        this.codec.Message = "need dictionary";
                        return ZlibConstants.ZStreamError;

                    case InflateManagerMode.Blocks:
                        r = this.blocks.Process(r);
                        if (r == ZlibConstants.ZDataError)
                        {
                            this.mode = InflateManagerMode.Bad;
                            break;
                        }

                        if (r == ZlibConstants.Zok)
                        {
                            r = f;
                        }

                        if (r != ZlibConstants.ZStreamEnd)
                        {
                            return r;
                        }

                        r = f;
                        this.computedCheck = this.blocks.Reset();
                        if (!this.HandleRfc1950HeaderBytes)
                        {
                            this.mode = InflateManagerMode.Done;
                            return ZlibConstants.ZStreamEnd;
                        }

                        this.mode = InflateManagerMode.ChecK4;
                        break;

                    case InflateManagerMode.ChecK4:
                        if (this.codec.AvailableBytesIn == 0)
                        {
                            return r;
                        }

                        r = f;
                        this.codec.AvailableBytesIn--;
                        this.codec.TotalBytesIn++;
                        this.expectedCheck = (uint)((this.codec.InputBuffer[this.codec.NextIn++] << 24) & 0xff000000);
                        this.mode = InflateManagerMode.ChecK3;
                        break;

                    case InflateManagerMode.ChecK3:
                        if (this.codec.AvailableBytesIn == 0)
                        {
                            return r;
                        }

                        r = f;
                        this.codec.AvailableBytesIn--;
                        this.codec.TotalBytesIn++;
                        this.expectedCheck += (uint)((this.codec.InputBuffer[this.codec.NextIn++] << 16) & 0x00ff0000);
                        this.mode = InflateManagerMode.ChecK2;
                        break;

                    case InflateManagerMode.ChecK2:
                        if (this.codec.AvailableBytesIn == 0)
                        {
                            return r;
                        }

                        r = f;
                        this.codec.AvailableBytesIn--;
                        this.codec.TotalBytesIn++;
                        this.expectedCheck += (uint)((this.codec.InputBuffer[this.codec.NextIn++] << 8) & 0x0000ff00);
                        this.mode = InflateManagerMode.ChecK1;
                        break;

                    case InflateManagerMode.ChecK1:
                        if (this.codec.AvailableBytesIn == 0)
                        {
                            return r;
                        }

                        r = f;
                        this.codec.AvailableBytesIn--;
                        this.codec.TotalBytesIn++;
                        this.expectedCheck += (uint)(this.codec.InputBuffer[this.codec.NextIn++] & 0x000000ff);
                        if (this.computedCheck != this.expectedCheck)
                        {
                            this.mode = InflateManagerMode.Bad;
                            this.codec.Message = "incorrect data check";
                            break;
                        }

                        this.mode = InflateManagerMode.Done;
                        return ZlibConstants.ZStreamEnd;

                    case InflateManagerMode.Done:
                        return ZlibConstants.ZStreamEnd;

                    case InflateManagerMode.Bad:
                        throw new ZlibException(
                            string.Format(CultureInfo.CurrentCulture, "Bad state ({0})", this.codec.Message));

                    default:
                        throw new ZlibException("Stream error.");
                }
            }
        }

Usage Example

Пример #1
0
 /// <summary>
 /// Inflate the data in the InputBuffer, placing the result in the OutputBuffer.
 /// </summary>
 /// <remarks>
 /// You must have set InputBuffer and OutputBuffer, NextIn and NextOut, and AvailableBytesIn and
 /// AvailableBytesOut  before calling this method.
 /// </remarks>
 /// <example>
 /// <code>
 /// private void InflateBuffer()
 /// {
 ///     int bufferSize = 1024;
 ///     byte[] buffer = new byte[bufferSize];
 ///     ZlibCodec decompressor = new ZlibCodec();
 ///
 ///     Console.WriteLine("\n============================================");
 ///     Console.WriteLine("Size of Buffer to Inflate: {0} bytes.", CompressedBytes.Length);
 ///     MemoryStream ms = new MemoryStream(DecompressedBytes);
 ///
 ///     int rc = decompressor.InitializeInflate();
 ///
 ///     decompressor.InputBuffer = CompressedBytes;
 ///     decompressor.NextIn = 0;
 ///     decompressor.AvailableBytesIn = CompressedBytes.Length;
 ///
 ///     decompressor.OutputBuffer = buffer;
 ///
 ///     // pass 1: inflate
 ///     do
 ///     {
 ///         decompressor.NextOut = 0;
 ///         decompressor.AvailableBytesOut = buffer.Length;
 ///         rc = decompressor.Inflate(FlushType.None);
 ///
 ///         if (rc != ZlibConstants.Z_OK &amp;&amp; rc != ZlibConstants.Z_STREAM_END)
 ///             throw new Exception("inflating: " + decompressor.Message);
 ///
 ///         ms.Write(decompressor.OutputBuffer, 0, buffer.Length - decompressor.AvailableBytesOut);
 ///     }
 ///     while (decompressor.AvailableBytesIn &gt; 0 || decompressor.AvailableBytesOut == 0);
 ///
 ///     // pass 2: finish and flush
 ///     do
 ///     {
 ///         decompressor.NextOut = 0;
 ///         decompressor.AvailableBytesOut = buffer.Length;
 ///         rc = decompressor.Inflate(FlushType.Finish);
 ///
 ///         if (rc != ZlibConstants.Z_STREAM_END &amp;&amp; rc != ZlibConstants.Z_OK)
 ///             throw new Exception("inflating: " + decompressor.Message);
 ///
 ///         if (buffer.Length - decompressor.AvailableBytesOut &gt; 0)
 ///             ms.Write(buffer, 0, buffer.Length - decompressor.AvailableBytesOut);
 ///     }
 ///     while (decompressor.AvailableBytesIn &gt; 0 || decompressor.AvailableBytesOut == 0);
 ///
 ///     decompressor.EndInflate();
 /// }
 ///
 /// </code>
 /// </example>
 /// <param name="flush">The flush to use when inflating.</param>
 /// <returns>Z_OK if everything goes well.</returns>
 internal int Inflate(FlushType flush)
 {
     if (istate == null)
     {
         throw new ZlibException("No Inflate State!");
     }
     return(istate.Inflate(flush));
 }