Esempio n. 1
0
 public TorrentReader(string archivo)
 {
     FileStream f = new FileStream(archivo, FileMode.Open, FileAccess.Read);
     BEncodeDecoder parser = new BEncodeDecoder();
     decodedTorrent = parser.parse(f);
     comment = ((BEncodeString)decodedTorrent.getValue(new BEncodeString("comment"))).ToString();
     announceURL = ((BEncodeString)decodedTorrent.getValue(new BEncodeString("announce"))).ToString();
     announceList = (BEncodeList)decodedTorrent.getValue(new BEncodeString("announce-list"));
     try
     {
         encoding = ((BEncodeString)decodedTorrent.getValue(new BEncodeString("encoding"))).ToString();
     }
     catch (Exception e)
     {
     }
     string test = ((BEncodeDictionary)decodedTorrent.getValue(new BEncodeString("info"))).ToBencodeDictionary();
     char[] tester = test.ToCharArray();
     byte[] total = new byte[tester.Length];
     int counter = 0;
     foreach(char chars in tester)
     {
         total[counter] = (byte)chars;
         counter += 1;
     }
     info_hash_byte = SHA1CryptoServiceProvider.Create().ComputeHash(total);
     info_hash = BitConverter.ToString(info_hash_byte).Replace("-", "");
     content_length = ((BEncodeInteger)((BEncodeDictionary)decodedTorrent.getValue(new BEncodeString("info"))).getValue(new BEncodeString("length"))).getValue();
     filename = ((BEncodeString)((BEncodeDictionary)decodedTorrent.getValue(new BEncodeString("info"))).getValue(new BEncodeString("name"))).ToString();
     created_by = ((BEncodeString)decodedTorrent.getValue(new BEncodeString("created by"))).ToString();
 }
        private BEncodeDictionary decodeDictionary()
        {
            BEncodeDictionary value = new BEncodeDictionary();
            char start = (char)reader.ReadByte();
            bool hasFinal = false;
            if (start == 'd')
            {
                while (true)
                {
                    char actual = (char)reader.PeekChar();

                    if (actual == 'e')
                    {
                        reader.ReadByte();
                        hasFinal = true;
                        break;
                    }
                    else if (actual == 'd')
                    {
                        if ((innerDictionaries == 0) && (keys.Count % 2 == 1))
                        {
                            innerDictionaries += 1;
                            BEncodeDictionary dic = decodeDictionary();
                            value.Add(keys.Pop(), dic);
                            innerDictionaries -= 1;
                        }
                        else if ((innerDictionaries == 0) && (keys.Count % 2 == 0))
                        {
                            throw new BEncodeException("key must be a string, not dictionary");
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 1) && (keys.Count % 2 == 0))
                        {
                            innerDictionaries += 1;
                            BEncodeDictionary dic = decodeDictionary();
                            value.Add(keys.Pop(), dic);
                            innerDictionaries -= 1;
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 1) && (keys.Count % 2 == 1))
                        {
                            throw new BEncodeException("key must be a string, not dictionary");
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 0) && (keys.Count % 2 == 1))
                        {
                            innerDictionaries += 1;
                            BEncodeDictionary dic = decodeDictionary();
                            value.Add(keys.Pop(), dic);
                            innerDictionaries -= 1;
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 0) && (keys.Count % 2 == 0))
                        {
                            throw new BEncodeException("key must be a string, not dictionary");
                        }
                    }
                    else if (actual == 'l')
                    {
                        if ((innerDictionaries == 0) && (keys.Count % 2 == 1))
                        {
                            value.Add(keys.Pop(), decodeList());
                        }
                        else if ((innerDictionaries == 0) && (keys.Count % 2 == 0))
                        {
                            throw new BEncodeException("key must be a string, not dictionary");
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 1) && (keys.Count % 2 == 0))
                        {
                            value.Add(keys.Pop(), decodeList());
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 1) && (keys.Count % 2 == 1))
                        {
                            throw new BEncodeException("key must be a string, not dictionary");
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 0) && (keys.Count % 2 == 1))
                        {
                            value.Add(keys.Pop(), decodeList());
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 0) && (keys.Count % 2 == 0))
                        {
                            throw new BEncodeException("key must be a string, not dictionary");
                        }
                    }
                    else if (actual == 'i')
                    {
                        if ((innerDictionaries == 0) && (keys.Count % 2 == 1))
                        {
                            value.Add(keys.Pop(), decodeInteger());
                        }
                        else if ((innerDictionaries == 0) && (keys.Count % 2 == 0))
                        {
                            throw new BEncodeException("key must be a string, not dictionary");
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 1) && (keys.Count % 2 == 0))
                        {
                            value.Add(keys.Pop(), decodeInteger());
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 1) && (keys.Count % 2 == 1))
                        {
                            throw new BEncodeException("key must be a string, not dictionary");
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 0) && (keys.Count % 2 == 1))
                        {
                            value.Add(keys.Pop(), decodeInteger());
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 0) && (keys.Count % 2 == 0))
                        {
                            throw new BEncodeException("key must be a string, not dictionary");
                        }
                    }
                    else if (char.IsDigit(actual))
                    {
                        if ((innerDictionaries == 0) && (keys.Count % 2 == 1))
                        {
                            value.Add(keys.Pop(), decodeString());
                        }
                        else if ((innerDictionaries == 0) && (keys.Count % 2 == 0))
                        {
                            keys.Push(decodeString());
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 1) && (keys.Count % 2 == 0))
                        {
                            value.Add(keys.Pop(), decodeString());
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 1) && (keys.Count % 2 == 1))
                        {
                            keys.Push(decodeString());
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 0) && (keys.Count % 2 == 1))
                        {
                            value.Add(keys.Pop(), decodeString());
                        }
                        else if ((innerDictionaries > 0) && (innerDictionaries % 2 == 0) && (keys.Count % 2 == 0))
                        {
                            keys.Push(decodeString());
                        }
                        else
                        {
                            throw new BEncodeException("cant recognize type in dictionary");
                        }
                    }
                }
            }
            else
            {
                throw new BEncodeException("dictionary must start with d");
            }

            if (!hasFinal)
            {
                throw new BEncodeException("dictionary must finish with e");
            }
            return value;
        }
 public void Add(BEncodeString key, BEncodeDictionary value)
 {
     data.Add(key, value);
 }
Esempio n. 4
0
 public void Add(BEncodeDictionary value)
 {
     data.Add(value);
 }