Ejemplo n.º 1
0
        private void DecodeInternal(RawReader reader, bool strictDecoding)
        {
            BEncodedString key   = null;
            BEncodedValue  value = null;

            //BEncodedString oldkey = null;

            if (reader.ReadByte() != 'd')
            {
                throw new BEncodingException("Invalid data found. Aborting"); // Remove the leading 'd'
            }
            while ((reader.PeekByte() != -1) && (reader.PeekByte() != 'e'))
            {
                key = (BEncodedString)BEncodedValue.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 = BEncodedValue.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");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Decodes a BEncodedList from the given StreamReader
        /// </summary>
        /// <param name="reader"></param>
        internal override void DecodeInternal(RawReader reader)
        {
            if (reader.ReadByte() != 'l')                            // Remove the leading 'l'
            {
                throw new BEncodingException("Invalid data found. Aborting");
            }

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

            if (reader.ReadByte() != 'e')                            // Remove the trailing 'e'
            {
                throw new BEncodingException("Invalid data found. Aborting");
            }
        }
Ejemplo n.º 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();

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

                if (reader.PeekByte() == 'd')
                {
                    value = new BEncodedDictionary();
                    if (key.Text.ToLower().Equals("info"))
                    {
                        ((BEncodedDictionary)value).DecodeInternal(reader, true);
                    }
                    else
                    {
                        ((BEncodedDictionary)value).DecodeInternal(reader, false);
                    }
                }
                else
                {
                    value = BEncodedValue.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);
        }