コード例 #1
0
        /// <summary>
        /// Decodes a BEncodedList from the given StreamReader
        /// </summary>
        /// <param name="reader"></param>
        internal override void DecodeInternal(RawReader reader)
        {
            try
            {
                if (reader.ReadByte() != 'l')                            // Remove the leading 'l'
                {
                    throw new BEncodingException("Invalid data found. Aborting");
                }

                while (reader.PeekChar() != -1 && (char)reader.PeekChar() != 'e')
                {
                    list.Add(Decode(reader));
                }

                if (reader.ReadByte() != 'e')                            // Remove the trailing 'e'
                {
                    throw new BEncodingException("Invalid data found. Aborting");
                }
            }
            catch (BEncodingException ex)
            {
                throw new BEncodingException("Couldn't decode list", ex);
            }
            catch
            {
                throw new BEncodingException("Couldn't decode list");
            }
        }
コード例 #2
0
        private void DecodeInternal(RawReader reader, bool strictDecoding)
        {
            BEncodedString key    = null;
            BEncodedValue  value  = null;
            BEncodedString oldkey = null;

            try
            {
                if (reader.ReadByte() != 'd')
                {
                    throw new BEncodingException("Invalid data found. Aborting"); // Remove the leading 'd'
                }
                while (reader.PeekChar() != -1 && (char)reader.PeekChar() != 'e')
                {
                    key = (BEncodedString)Decode(reader);         // keys have to be BEncoded strings

                    if (oldkey != null && oldkey.CompareTo(key) > 0)
                    {
                        if (strictDecoding)
                        {
                            throw new BEncodingException(string.Format(
                                                             "Illegal BEncodedDictionary. The attributes are not ordered correctly. Old key: {0}, New key: {1}",
                                                             oldkey, key));
                        }
                    }

                    oldkey = key;
                    value  = Decode(reader);                    // the value is a BEncoded value
                    _dictionary.Add(key, value);
                }

                if (reader.ReadByte() != 'e')                                    // remove the trailing 'e'
                {
                    throw new BEncodingException("Invalid data found. Aborting");
                }
            }
            catch (Exception ex)
            {
                throw new BEncodingException("Couldn't decode dictionary", ex);
            }
        }
コード例 #3
0
        /// <summary>
        /// Special decoding method for torrent files - allows dictionary attributes to be out of order for the
        /// overall torrent file, but imposes strict rules on the info dictionary.
        /// </summary>
        /// <returns></returns>
        public static BEncodedDictionary DecodeTorrent(RawReader reader)
        {
            BEncodedString     key     = null;
            BEncodedValue      value   = null;
            BEncodedDictionary torrent = new BEncodedDictionary();

            try
            {
                if (reader.ReadByte() != 'd')
                {
                    throw new BEncodingException("Invalid data found. Aborting"); // Remove the leading 'd'
                }
                while (reader.PeekChar() != -1 && (char)reader.PeekChar() != 'e')
                {
                    key = (BEncodedString)Decode(reader);         // keys have to be BEncoded strings

                    if (reader.PeekChar() == 'd')
                    {
                        value = new BEncodedDictionary();
                        bool strictDecoding = key.Text.ToLower().Equals("info");
                        ((BEncodedDictionary)value).DecodeInternal(reader, strictDecoding);
                    }
                    else
                    {
                        value = Decode(reader);                     // the value is a BEncoded value
                    }
                    torrent._dictionary.Add(key, value);
                }

                if (reader.ReadByte() != 'e')                                    // remove the trailing 'e'
                {
                    throw new BEncodingException("Invalid data found. Aborting");
                }

                return(torrent);
            }
            catch (Exception ex)
            {
                throw new BEncodingException("Couldn't decode dictionary", ex);
            }
        }
コード例 #4
0
        /// <summary>
        /// Decodes a BEncoded number from the supplied RawReader
        /// </summary>
        /// <param name="reader">RawReader containing a BEncoded Number</param>
        internal override void DecodeInternal(RawReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            try
            {
                if (reader.ReadByte() != 'i')              // remove the leading 'i'
                {
                    throw new BEncodingException("Invalid data found. Aborting.");
                }

                int letter;
                while ((letter = reader.PeekChar()) != -1 && letter != 'e')
                {
                    // TODO: Maybe should completely use a nuget package for BEncode.
                    //if (letter < '0' || letter > '9')
                    //throw new BEncodingException("Invalid number found.");
                    Number = Number * 10 + (letter - '0');
                    reader.ReadChar();
                }
                if (reader.ReadByte() != 'e')        //remove the trailing 'e'
                {
                    throw new BEncodingException("Invalid data found. Aborting.");
                }
            }
            catch (BEncodingException ex)
            {
                throw new BEncodingException("Couldn't decode number", ex);
            }
            catch
            {
                throw new BEncodingException("Couldn't decode number");
            }
        }