예제 #1
0
 public OpenPgpMarker(string marker, OpenPgpState initial, OpenPgpState detected, bool isEnd)
 {
     Marker        = CharsetUtils.UTF8.GetBytes(marker);
     InitialState  = initial;
     DetectedState = detected;
     IsEnd         = isEnd;
 }
예제 #2
0
        /// <summary>
        /// Resets the filter.
        /// </summary>
        /// <remarks>
        /// Resets the filter.
        /// </remarks>
        public override void Reset()
        {
            state         = OpenPgpState.None;
            seenEndMarker = false;
            BeginOffset   = null;
            EndOffset     = null;
            midline       = false;
            position      = 0;
            next          = 0;

            base.Reset();
        }
예제 #3
0
        /// <summary>
        /// Filter the specified input.
        /// </summary>
        /// <remarks>
        /// Filters the specified input buffer starting at the given index,
        /// spanning across the specified number of bytes.
        /// </remarks>
        /// <returns>The filtered output.</returns>
        /// <param name="input">The input buffer.</param>
        /// <param name="startIndex">The starting index of the input buffer.</param>
        /// <param name="length">The length of the input buffer, starting at <paramref name="startIndex"/>.</param>
        /// <param name="outputIndex">The output index.</param>
        /// <param name="outputLength">The output length.</param>
        /// <param name="flush">If set to <c>true</c>, all internally buffered data should be flushed to the output buffer.</param>
        protected override byte[] Filter(byte[] input, int startIndex, int length, out int outputIndex, out int outputLength, bool flush)
        {
            int  endIndex = startIndex + length;
            int  index    = startIndex;
            bool cr;

            outputIndex  = startIndex;
            outputLength = 0;

            if (seenEndMarker || length == 0)
            {
                return(input);
            }

            if (midline)
            {
                while (index < endIndex && input[index] != (byte)'\n')
                {
                    index++;
                }

                if (index == endIndex)
                {
                    if (state != OpenPgpState.None)
                    {
                        outputLength = index - startIndex;
                    }

                    position += index - startIndex;

                    return(input);
                }

                midline = false;
            }

            if (state == OpenPgpState.None)
            {
                do
                {
                    int lineIndex = index;

                    while (index < endIndex && input[index] != (byte)'\n')
                    {
                        index++;
                    }

                    if (index == endIndex)
                    {
                        bool isPartialMatch = false;

                        for (int i = 0; i < OpenPgpMarkers.Length; i++)
                        {
                            if (OpenPgpMarkers[i].InitialState == state && IsPartialMatch(input, lineIndex, index, OpenPgpMarkers[i].Marker))
                            {
                                isPartialMatch = true;
                                break;
                            }
                        }

                        if (isPartialMatch)
                        {
                            SaveRemainingInput(input, lineIndex, index - lineIndex);
                            position += lineIndex - startIndex;
                        }
                        else
                        {
                            position += index - lineIndex;
                            midline   = true;
                        }

                        return(input);
                    }

                    index++;

                    for (int i = 0; i < OpenPgpMarkers.Length; i++)
                    {
                        if (OpenPgpMarkers[i].InitialState == state && IsMarker(input, lineIndex, endIndex, OpenPgpMarkers[i].Marker, out cr))
                        {
                            state = OpenPgpMarkers[i].DetectedState;
                            SetPosition(lineIndex - startIndex, i, cr);
                            outputLength = index - lineIndex;
                            outputIndex  = lineIndex;
                            next         = i + 1;
                            break;
                        }
                    }
                } while (index < endIndex && state == OpenPgpState.None);

                if (index == endIndex)
                {
                    position += index - startIndex;
                    return(input);
                }
            }

            do
            {
                int lineIndex = index;

                while (index < endIndex && input[index] != (byte)'\n')
                {
                    index++;
                }

                if (index == endIndex)
                {
                    if (!flush)
                    {
                        if (IsPartialMatch(input, lineIndex, index, OpenPgpMarkers[next].Marker))
                        {
                            SaveRemainingInput(input, lineIndex, index - lineIndex);
                            outputLength = lineIndex - outputIndex;
                            position    += lineIndex - startIndex;
                        }
                        else
                        {
                            outputLength = index - outputIndex;
                            position    += index - startIndex;
                            midline      = true;
                        }

                        return(input);
                    }

                    outputLength = index - outputIndex;
                    position    += index - startIndex;

                    return(input);
                }

                index++;

                if (IsMarker(input, lineIndex, endIndex, OpenPgpMarkers[next].Marker, out cr))
                {
                    seenEndMarker = OpenPgpMarkers[next].IsEnd;
                    state         = OpenPgpMarkers[next].DetectedState;
                    SetPosition(lineIndex - startIndex, next, cr);
                    next++;

                    if (seenEndMarker)
                    {
                        break;
                    }
                }
            } while (index < endIndex);

            outputLength = index - outputIndex;
            position    += index - startIndex;

            return(input);
        }