Ejemplo n.º 1
0
        /// <summary>
        /// Read the percent-encoding and try unescape it.
        ///
        /// The operation first peek at the character the <paramref name="scan"/>
        /// iterator points at. If it is % the <paramref name="scan"/> is then
        /// moved on to scan the following to characters. If the two following
        /// characters are hexadecimal literals they will be unescaped and the
        /// value will be returned.
        ///
        /// If the first character is not % the <paramref name="scan"/> iterator
        /// will be removed beyond the location of % and -1 will be returned.
        ///
        /// If the following two characters can't be successfully unescaped the
        /// <paramref name="scan"/> iterator will be move behind the % and -1
        /// will be returned.
        /// </summary>
        /// <param name="scan">The value to read</param>
        /// <param name="end">The end of the sequence</param>
        /// <returns>The unescaped byte if success. Otherwise return -1.</returns>
        private static int UnescapePercentEncoding(ref MemoryPoolIterator scan, MemoryPoolIterator end)
        {
            if (scan.Take() != '%')
            {
                return(-1);
            }

            var probe = scan;

            int value1 = ReadHex(ref probe, end);

            if (value1 == -1)
            {
                return(-1);
            }

            int value2 = ReadHex(ref probe, end);

            if (value2 == -1)
            {
                return(-1);
            }

            if (SkipUnescape(value1, value2))
            {
                return(-1);
            }

            scan = probe;
            return((value1 << 4) + value2);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read the next char and convert it into hexadecimal value.
        ///
        /// The <paramref name="scan"/> iterator will be moved to the next
        /// byte no matter no matter whether the operation successes.
        /// </summary>
        /// <param name="scan">The value to read</param>
        /// <param name="end">The end of the sequence</param>
        /// <returns>The hexadecimal value if successes, otherwise -1.</returns>
        private static int ReadHex(ref MemoryPoolIterator scan, MemoryPoolIterator end)
        {
            if (CompareIterators(ref scan, ref end))
            {
                return(-1);
            }

            var value  = scan.Take();
            var isHead = (((value >= '0') && (value <= '9')) ||
                          ((value >= 'A') && (value <= 'F')) ||
                          ((value >= 'a') && (value <= 'f')));

            if (!isHead)
            {
                return(-1);
            }

            if (value <= '9')
            {
                return(value - '0');
            }
            else if (value <= 'F')
            {
                return((value - 'A') + 10);
            }
            else // a - f
            {
                return((value - 'a') + 10);
            }
        }
Ejemplo n.º 3
0
 private static void Copy(MemoryPoolIterator head, MemoryPoolIterator tail, ref MemoryPoolIterator writer)
 {
     while (!CompareIterators(ref head, ref tail))
     {
         writer.Put((byte)head.Take());
     }
 }