Beispiel #1
0
        /// <summary>
        /// Decodes a BEncodedString from the supplied StreamReader
        /// </summary>
        /// <param name="reader">The StreamReader containing the BEncodedString</param>
        internal override void DecodeInternal(RawReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            int    letterCount;
            string length = string.Empty;

            while ((reader.PeekByte() != -1) && (reader.PeekByte() != ':'))        // read in how many characters
            {
                length += (char)reader.ReadByte();                                 // the string is
            }
            if (reader.ReadByte() != ':')                                          // remove the ':'
            {
                throw new BEncodingException("Invalid data found. Aborting");
            }

            if (!int.TryParse(length, out letterCount))
            {
                throw new BEncodingException(string.Format("Invalid BEncodedString. Length was '{0}' instead of a number", length));
            }

            this.textBytes = new byte[letterCount];
            if (reader.Read(textBytes, 0, letterCount) != letterCount)
            {
                throw new BEncodingException("Couldn't decode string");
            }
        }
Beispiel #2
0
        static BEncodedString DecodeString(RawReader reader, int length)
        {
            int read;

            while ((read = reader.ReadByte()) != -1 && read != ':')
            {
                if (read < '0' || read > '9')
                {
                    throw new BEncodingException($"Invalid BEncodedString. Length was '{length}' instead of a number");
                }
                length = length * 10 + (read - '0');
            }

            if (read != ':')
            {
                throw new BEncodingException("Invalid data found. Aborting");
            }

            var bytes = new byte[length];

            if (reader.Read(bytes, 0, length) != length)
            {
                throw new BEncodingException("Couldn't decode string");
            }
            return(new BEncodedString(bytes));
        }
Beispiel #3
0
        public override void Decode(byte[] buffer, int offset, int length)
        {
            BEncodedValue val;
            using (RawReader reader = new RawReader(new MemoryStream(buffer, offset, length, false), false))
            {
                BEncodedDictionary d = BEncodedDictionary.Decode<BEncodedDictionary>(reader);
                int totalSize = 0;

                if (d.TryGetValue(MessageTypeKey, out val))
                    messageType = (eMessageType)((BEncodedNumber)val).Number;
                if (d.TryGetValue(PieceKey, out val))
                    piece = (int)((BEncodedNumber)val).Number;
                if (d.TryGetValue(TotalSizeKey, out val))
                {
                    totalSize = (int)((BEncodedNumber)val).Number;
                    metadata = new byte[Math.Min(totalSize - piece * BlockSize, BlockSize)];
                    reader.Read(metadata, 0, metadata.Length);
                }
            }
        }