NetWrok.HTTP.Zlib.InflateManager.Sync C# (CSharp) Method

Sync() private method

private Sync ( ZlibCodec z ) : int
z ZlibCodec
return int
        internal int Sync(ZlibCodec z)
        {
            int n; // number of bytes to look at
            int p; // pointer to bytes
            int m; // number of marker bytes found in a row
            long r, w; // temporaries to save total_in and total_out

            // set up
            if (z == null || z.istate == null)
                return ZlibConstants.Z_STREAM_ERROR;
            if (z.istate.mode != BAD) {
                z.istate.mode = BAD;
                z.istate.marker = 0;
            }
            if ((n = z.AvailableBytesIn) == 0)
                return ZlibConstants.Z_BUF_ERROR;
            p = z.NextIn;
            m = z.istate.marker;

            // search
            while (n != 0 && m < 4) {
                if (z.InputBuffer [p] == mark [m]) {
                    m++;
                } else if (z.InputBuffer [p] != 0) {
                    m = 0;
                } else {
                    m = 4 - m;
                }
                p++;
                n--;
            }

            // restore
            z.TotalBytesIn += p - z.NextIn;
            z.NextIn = p;
            z.AvailableBytesIn = n;
            z.istate.marker = m;

            // return no joy or set up to restart on a new block
            if (m != 4) {
                return ZlibConstants.Z_DATA_ERROR;
            }
            r = z.TotalBytesIn;
            w = z.TotalBytesOut;
            Reset (z);
            z.TotalBytesIn = r;
            z.TotalBytesOut = w;
            z.istate.mode = BLOCKS;
            return ZlibConstants.Z_OK;
        }