예제 #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));
 }