예제 #1
0
        /// <summary>
        /// Decodes the specified input into the output buffer.
        /// </summary>
        /// <remarks>
        /// <para>Decodes the specified input into the output buffer.</para>
        /// <para>The output buffer should be large enough to hold all of the
        /// decoded input. For estimating the size needed for the output buffer,
        /// see <see cref="EstimateOutputLength"/>.</para>
        /// </remarks>
        /// <returns>The number of bytes written to the output buffer.</returns>
        /// <param name="input">A pointer to the beginning of the input buffer.</param>
        /// <param name="length">The length of the input buffer.</param>
        /// <param name="output">A pointer to the beginning of the output buffer.</param>
        public unsafe int Decode(byte *input, int length, byte *output)
        {
            byte *inend  = input + length;
            byte *outptr = output;
            byte *inptr  = input;
            byte  c;

            while (inptr < inend)
            {
                switch (state)
                {
                case HexDecoderState.PassThrough:
                    while (inptr < inend)
                    {
                        c = *inptr++;

                        if (c == '%')
                        {
                            state = HexDecoderState.Percent;
                            break;
                        }

                        *outptr++ = c;
                    }
                    break;

                case HexDecoderState.Percent:
                    c     = *inptr++;
                    state = HexDecoderState.DecodeByte;
                    saved = c;
                    break;

                case HexDecoderState.DecodeByte:
                    c = *inptr++;
                    if (c.IsXDigit() && saved.IsXDigit())
                    {
                        saved = saved.ToXDigit();
                        c     = c.ToXDigit();

                        *outptr++ = (byte)((saved << 4) | c);
                    }
                    else
                    {
                        // invalid encoded sequence - pass it through undecoded
                        *outptr++ = (byte)'%';
                        *outptr++ = saved;
                        *outptr++ = c;
                    }

                    state = HexDecoderState.PassThrough;
                    break;
                }
            }

            return((int)(outptr - output));
        }
예제 #2
0
 /// <summary>
 /// Resets the decoder.
 /// </summary>
 /// <remarks>
 /// Resets the state of the decoder.
 /// </remarks>
 public void Reset()
 {
     state = HexDecoderState.PassThrough;
     saved = 0;
 }
예제 #3
0
		/// <summary>
		/// Resets the decoder.
		/// </summary>
		/// <remarks>
		/// Resets the state of the decoder.
		/// </remarks>
		public void Reset ()
		{
			state = HexDecoderState.PassThrough;
			saved = 0;
		}
예제 #4
0
		/// <summary>
		/// Decodes the specified input into the output buffer.
		/// </summary>
		/// <remarks>
		/// <para>Decodes the specified input into the output buffer.</para>
		/// <para>The output buffer should be large enough to hold all of the
		/// decoded input. For estimating the size needed for the output buffer,
		/// see <see cref="EstimateOutputLength"/>.</para>
		/// </remarks>
		/// <returns>The number of bytes written to the output buffer.</returns>
		/// <param name="input">A pointer to the beginning of the input buffer.</param>
		/// <param name="length">The length of the input buffer.</param>
		/// <param name="output">A pointer to the beginning of the output buffer.</param>
		public unsafe int Decode (byte* input, int length, byte* output)
		{
			byte* inend = input + length;
			byte* outptr = output;
			byte* inptr = input;
			byte c;

			while (inptr < inend) {
				switch (state) {
				case HexDecoderState.PassThrough:
					while (inptr < inend) {
						c = *inptr++;

						if (c == '%') {
							state = HexDecoderState.Percent;
							break;
						}

						*outptr++ = c;
					}
					break;
				case HexDecoderState.Percent:
					c = *inptr++;
					state = HexDecoderState.DecodeByte;
					saved = c;
					break;
				case HexDecoderState.DecodeByte:
					c = *inptr++;
					if (c.IsXDigit () && saved.IsXDigit ()) {
						saved = saved.ToXDigit ();
						c = c.ToXDigit ();

						*outptr++ = (byte) ((saved << 4) | c);
					} else {
						// invalid encoded sequence - pass it through undecoded
						*outptr++ = (byte) '%';
						*outptr++ = saved;
						*outptr++ = c;
					}

					state = HexDecoderState.PassThrough;
					break;
				}
			}

			return (int) (outptr - output);
		}