Пример #1
0
        private static List <string> AllKeys(List <string> output, BencodedValue value)
        {
            AllKeys(output, value.Dictionary);
            AllKeys(output, value.Array);

            return(output);
        }
Пример #2
0
 public byte[] Encode(BencodedValue data)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         this.Encode(stream, data);
         return(stream.ToArray());
     }
 }
Пример #3
0
        private static List <string> AllTexts(List <string> output, BencodedValue value, Encoding encoding)
        {
            if (value != null)
            {
                AllTexts(output, value.Text, encoding);
                AllTexts(output, value.Dictionary, encoding);
                AllTexts(output, value.Array, encoding);
            }

            return(output);
        }
Пример #4
0
        public static T Find <T>(this BencodedValue value, string name, Func <BencodedValue, T> selector)
        {
            if (value != null && value.Dictionary != null)
            {
                foreach (BencodedEntry entry in value.Dictionary)
                {
                    if (entry.Key.Text != null && entry.Key.Text.GetString() == name)
                    {
                        return(selector.Invoke(entry.Value));
                    }
                }
            }

            return(selector.Invoke(null));
        }
Пример #5
0
        private void Encode(MemoryStream stream, BencodedValue data)
        {
            if (data.Dictionary != null)
            {
                this.Write(stream, "d");

                foreach (BencodedEntry entry in data.Dictionary)
                {
                    this.Encode(stream, entry.Key);
                    this.Encode(stream, entry.Value);
                }

                this.Write(stream, "e");
            }
            else if (data.Array != null)
            {
                this.Write(stream, "l");

                foreach (BencodedValue item in data.Array)
                {
                    this.Encode(stream, item);
                }

                this.Write(stream, "e");
            }
            else if (data.Text != null)
            {
                this.Write(stream, data.Text.Length.ToString());
                this.Write(stream, ":");
                this.Write(stream, data.Text.GetBytes());
            }
            else if (data.Number != null)
            {
                this.Write(stream, "i");
                this.Write(stream, data.Number.ToString());
                this.Write(stream, "e");
            }
            else
            {
                this.Write(stream, data.Data.Length.ToString());
                this.Write(stream, ":");
                this.Write(stream, data.Data.GetBytes());
            }
        }
Пример #6
0
        private BencodedValue DecodeDictionary(byte[] data, ref int position)
        {
            int offset = position - 1;
            List <BencodedEntry> entries = new List <BencodedEntry>();

            while (data[position] != 0x65)
            {
                BencodedValue key   = DecodeCore(data, ref position);
                BencodedValue value = DecodeCore(data, ref position);

                entries.Add(new BencodedEntry {
                    Key = key, Value = value
                });
            }

            position++;
            return(new BencodedValue
            {
                Data = new BencodedData(data, offset, position - offset),
                Dictionary = entries.ToArray()
            });
        }
Пример #7
0
 public static string[] AllTexts(this BencodedValue value, Encoding encoding)
 {
     return(AllTexts(new List <string>(), value, encoding).ToArray());
 }
Пример #8
0
 public static long ToInt64(this BencodedValue value)
 {
     return(value.Number.ToInt64());
 }
Пример #9
0
 public static int ToInt32(this BencodedValue value)
 {
     return(value.Number.ToInt32());
 }
Пример #10
0
 public static string ToText(this BencodedValue value, Encoding encoding)
 {
     return(AllTexts(value, encoding).FirstOrDefault());
 }
Пример #11
0
 public static string ToText(this BencodedValue value)
 {
     return(ToText(value, Encoding.ASCII));
 }