Ejemplo n.º 1
0
        /// <summary>
        /// Decodes the text of a URI by unescaping any percent-encoded character sequences and
        /// then evaluating the result using the specified encoding.
        /// </summary>
        /// <param name="text">The encoded URI.</param>
        /// <param name="encoding">The encoding to use for Unicode characters in the URI. If this value is <see langword="null"/>, the <see cref="Encoding.UTF8"/> encoding will be used.</param>
        /// <returns>The decoded URI text.</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="text"/> is <see langword="null"/>.</exception>
        public static string UriDecode(string text, Encoding encoding)
        {
            if (text == null)
                throw new ArgumentNullException("text");

            encoding = encoding ?? Encoding.UTF8;
            MatchEvaluator matchEvaluator =
                match =>
                {
                    string hexValue = match.Groups[1].Value;
                    return ((char)byte.Parse(hexValue, NumberStyles.HexNumber)).ToString();
                };
            string decodedText = _percentEncodedPattern.Replace(text, matchEvaluator);
            byte[] data = Array.ConvertAll(decodedText.ToCharArray(), c => (byte)c);
            return encoding.GetString(data);
        }
Ejemplo n.º 2
0
        //private object _thisLock = new object();

        /// <summary>
        ///     Put bytes into the parser.
        /// </summary>
        /// <param name="buf">The bytes to put into the parse stream</param>
        /// <param name="offset">Offset into buf to start at</param>
        /// <param name="length">Number of bytes to write</param>
        public void Push(byte[] buf, int offset, int length)
        {
            // or assert, really, but this is a little nicer.
            if (length == 0)
            {
                return;
            }

            // No locking is required.  Read() won't get called again
            // until this method returns.

            // TODO: only do this copy if we have a partial token at the
            // end of parsing.
            var copy = new byte[length];

            Buffer.BlockCopy(buf, offset, copy, 0, length);
            _mBuf.Write(copy);

            var b   = _mBuf.GetBuffer();
            var off = 0;
            var ct  = new ContentToken();

            try
            {
                while (off < b.Length)
                {
                    var tok = _mCdata
                        ? _mEnc.tokenizeCdataSection(b, off, b.Length, ct)
                        : _mEnc.tokenizeContent(b, off, b.Length, ct);

                    // ReSharper disable once SwitchStatementMissingSomeCases
                    switch (tok)
                    {
                    case TOK.EMPTY_ELEMENT_NO_ATTS:
                    case TOK.EMPTY_ELEMENT_WITH_ATTS:
                        StartTag(b, off, ct, tok);
                        EndTag();
                        break;

                    case TOK.START_TAG_NO_ATTS:
                    case TOK.START_TAG_WITH_ATTS:
                        StartTag(b, off, ct, tok);
                        break;

                    case TOK.END_TAG:
                        EndTag();
                        break;

                    case TOK.DATA_CHARS:
                    case TOK.DATA_NEWLINE:
                        AddText(Utf.GetString(b, off, ct.TokenEnd - off));
                        break;

                    case TOK.CHAR_REF:
                    case TOK.MAGIC_ENTITY_REF:
                        AddText(new string(new[] { ct.RefChar1 }));
                        break;

                    case TOK.CHAR_PAIR_REF:
                        AddText(new string(new[]
                        {
                            ct.RefChar1,
                            ct.RefChar2
                        }));
                        break;

                    case TOK.COMMENT:
                        if (_current != null)
                        {
                            // <!-- 4
                            //  --> 3
                            var start = off + 4 * _mEnc.MinBytesPerChar;
                            var end   = ct.TokenEnd - off -
                                        7 * _mEnc.MinBytesPerChar;
                            var text = Utf.GetString(b, start, end);
                            _current.AddChild(new Comment(text));
                        }
                        break;

                    case TOK.CDATA_SECT_OPEN:
                        _mCdata = true;
                        break;

                    case TOK.CDATA_SECT_CLOSE:
                        _mCdata = false;
                        break;

                    case TOK.XML_DECL:
                        // thou shalt use UTF8, and XML version 1.
                        // i shall ignore evidence to the contrary...

                        // TODO: Throw an exception if these assumptions are
                        // wrong
                        break;

                    case TOK.ENTITY_REF:
                    case TOK.PI:
#if CF
                        throw new util.NotImplementedException("Token type not implemented: " + tok);
#else
                        throw new NotImplementedException("Token type not implemented: " + tok);
#endif
                    }
                    off = ct.TokenEnd;
                }
            }
            catch (PartialTokenException)
            {
                // ignored;
            }
            catch (ExtensibleTokenException)
            {
                // ignored;
            }
            catch (Exception ex)
            {
                OnStreamError?.Invoke(this, ex);
            }
            finally
            {
                _mBuf.Clear(off);
            }
        }
Ejemplo n.º 3
0
        public async Task <string> GetStringFromUrlAsync(string Url, System.Text.Encoding Encoding)
        {
            var Data = await GetByteArrayFromUrlAsync(Url);

            return(Encoding.GetString(Data, 0, Data.Length));
        }
Ejemplo n.º 4
0
        internal static string StringFromBuffer(byte[] buf, System.Text.Encoding encoding)
        {
            string s = encoding.GetString(buf);

            return(s);
        }
Ejemplo n.º 5
0
        private LocalizedStringDict LoadPluginStrings(System.Text.Encoding encoding, LocalizedStringFormat format,
                                                      BinaryReader reader)
        {
            if (encoding == null)
            {
                encoding = Encoding.CP1252;
            }
            var dict   = new LocalizedStringDict();
            int length = reader.ReadInt32();
            int size   = reader.ReadInt32(); // size of data section
            var list   = new List <Pair <uint, uint> >();

            for (uint i = 0; i < length; ++i)
            {
                uint id  = reader.ReadUInt32();
                uint off = reader.ReadUInt32();
                list.Add(new Pair <uint, uint>(id, off));
            }
            long offset = reader.BaseStream.Position;
            var  data   = new byte[size];

            using (var stream = new MemoryStream(data, 0, size, true, false))
            {
                var buffer = new byte[65536];
                int left   = size;
                while (left > 0)
                {
                    int read  = Math.Min(left, buffer.Length);
                    int nread = reader.BaseStream.Read(buffer, 0, read);
                    if (nread == 0)
                    {
                        break;
                    }
                    stream.Write(buffer, 0, nread);
                    left -= nread;
                }
            }
            foreach (var kvp in list)
            {
                var start = (int)kvp.Value;
                int len   = 0;
                switch (format)
                {
                case LocalizedStringFormat.Base:
                    while (data[start + len] != 0)
                    {
                        ++len;
                    }
                    break;

                case LocalizedStringFormat.DL:
                case LocalizedStringFormat.IL:
                    len   = BitConverter.ToInt32(data, start) - 1;
                    start = start + sizeof(int);
                    if (start + len > data.Length)
                    {
                        len = data.Length - start;
                    }
                    if (len < 0)
                    {
                        len = 0;
                    }
                    break;
                }
                string str = encoding.GetString(data, start, len);
                dict.Add(kvp.Key, str);
            }
            return(dict);
        }