System.IO.Compression.FastEncoderWindow.GetNextSymbolOrMatch C# (CSharp) Method

GetNextSymbolOrMatch() private method

private GetNextSymbolOrMatch ( Match match ) : bool
match Match
return bool
        internal bool GetNextSymbolOrMatch(Match match) {
            int num2;
            uint hash = this.HashValue(0, this.window[this.bufPos]);
            hash = this.HashValue(hash, this.window[this.bufPos + 1]);
            int matchPos = 0;
            if ((this.bufEnd - this.bufPos) <= 3) {
                num2 = 0;
            }
            else {
                int search = (int)this.InsertString(ref hash);
                if (search != 0) {
                    num2 = this.FindMatch(search, out matchPos, 0x20, 0x20);
                    if ((this.bufPos + num2) > this.bufEnd) {
                        num2 = this.bufEnd - this.bufPos;
                    }
                }
                else {
                    num2 = 0;
                }
            }
            if (num2 < 3) {
                match.State = MatchState.HasSymbol;
                match.Symbol = this.window[this.bufPos];
                this.bufPos++;
            }
            else {
                this.bufPos++;
                if (num2 <= 6) {
                    int num5;
                    int num6 = 0;
                    int num7 = (int)this.InsertString(ref hash);
                    if (num7 != 0) {
                        num5 = this.FindMatch(num7, out num6, (num2 < 4) ? 0x20 : 8, 0x20);
                        if ((this.bufPos + num5) > this.bufEnd) {
                            num5 = this.bufEnd - this.bufPos;
                        }
                    }
                    else {
                        num5 = 0;
                    }
                    if (num5 > num2) {
                        match.State = MatchState.HasSymbolAndMatch;
                        match.Symbol = this.window[this.bufPos - 1];
                        match.Position = num6;
                        match.Length = num5;
                        this.bufPos++;
                        num2 = num5;
                        this.InsertStrings(ref hash, num2);
                    }
                    else {
                        match.State = MatchState.HasMatch;
                        match.Position = matchPos;
                        match.Length = num2;
                        num2--;
                        this.bufPos++;
                        this.InsertStrings(ref hash, num2);
                    }
                }
                else {
                    match.State = MatchState.HasMatch;
                    match.Position = matchPos;
                    match.Length = num2;
                    this.InsertStrings(ref hash, num2);
                }
            }
            if (this.bufPos == 0x4000) {
                this.MoveWindows();
            }
            return true;
        }

Usage Example

        // compress the bytes in input history window
        private void GetCompressedOutput(OutputBuffer output)
        {
            while (inputWindow.BytesAvailable > 0 && SafeToWriteTo(output))
            {
                // Find next match. A match can be a symbol,
                // a distance/length pair, a symbol followed by a distance/Length pair
                inputWindow.GetNextSymbolOrMatch(currentMatch);

                if (currentMatch.State == MatchState.HasSymbol)
                {
                    WriteChar(currentMatch.Symbol, output);
                }
                else if (currentMatch.State == MatchState.HasMatch)
                {
                    WriteMatch(currentMatch.Length, currentMatch.Position, output);
                }
                else
                {
                    WriteChar(currentMatch.Symbol, output);
                    WriteMatch(currentMatch.Length, currentMatch.Position, output);
                }
            }
        }