Example #1
0
        ///<summary>
        ///To deal with the structure of the INFO
        ///</ Summary>
        ///<param Name="TorrentBytes"> </ param>
        ///<param Name="StarIndex"> </ param>
        ///<param Name="ListCount"> </ param>
        private void GetFileInfo(byte[] TorrentBytes, ref int StarIndex, ref int ListCount)
        {
            if ((char)TorrentBytes[StarIndex] != 'd')
            {
                return;
            }
            StarIndex++;
            if (GetKeyText(TorrentBytes, ref StarIndex).ToString().ToUpper() == "FILES")
            {
                TorrentFileInfoClass Info = new TorrentFileInfoClass();
                while (true)
                {
                    string TextStar = Encoding.UTF8.GetString(TorrentBytes, StarIndex, 1);

                    switch (TextStar)
                    {
                    case "l":
                        StarIndex++;
                        ListCount++;
                        break;

                    case "e":
                        ListCount--;
                        StarIndex++;
                        if (ListCount == 1)
                        {
                            TorrentFileInfo.Add(Info);
                        }
                        if (ListCount == 0)
                        {
                            return;
                        }
                        break;

                    case "d":
                        Info = new TorrentFileInfoClass();
                        ListCount++;
                        StarIndex++;
                        break;

                    default:
                        object ListText = GetKeyText(TorrentBytes, ref StarIndex);
                        if (ListText == null)
                        {
                            return;
                        }
                        switch (ListText.ToString().ToUpper())     // convert to uppercase
                        {
                        case "ED2K":
                            Info.De2K = GetKeyText(TorrentBytes, ref StarIndex).ToString();
                            break;

                        case "THE FILEHASH":
                            Info.FileHash = GetKeyText(TorrentBytes, ref StarIndex).ToString();
                            break;

                        case "LENGTH":
                            Info.Length = Convert.ToInt64(GetKeyNumb(TorrentBytes, ref StarIndex));
                            break;

                        case "PATH":
                            int       pathcount = 0;
                            ArrayList PathList  = GetKeyData(TorrentBytes, ref StarIndex, ref pathcount);
                            string    Temp      = "";
                            for (int i = 0; i != PathList.Count; i++)
                            {
                                Temp += PathList[i].ToString();
                            }
                            Info.Path = Temp;
                            break;

                        case "PATH.UTF-8":
                            int       PathUtf8Count = 0;
                            ArrayList Pathutf8List  = GetKeyData(TorrentBytes, ref StarIndex, ref PathUtf8Count);
                            string    UtfTemp       = "";
                            for (int i = 0; i != Pathutf8List.Count; i++)
                            {
                                UtfTemp += Pathutf8List[i].ToString();
                            }
                            Info.PathUTF8 = UtfTemp;
                            break;
                        }

                        break;
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// 对付INFO的结构
        /// </summary>
        /// <param name="TorrentBytes"></param>
        /// <param name="StarIndex"></param>
        /// <param name="ListCount"></param>
        private void GetFileInfo(byte[] TorrentBytes, ref int StarIndex, ref int ListCount)
        {
            if ((char)TorrentBytes[StarIndex] != 'd')
            {
                return;
            }
            StarIndex++;

            String getkey = GetKeyText(TorrentBytes, ref StarIndex).ToString().ToUpper();

            while (getkey != "FILES" && getkey != "LENGTH")
            {
                object PieceLengthNumb = GetKeyNumb(TorrentBytes, ref StarIndex);
                char   temp1           = (char)TorrentBytes[StarIndex];
                while (temp1 == 'i')
                {
                    object temp2 = GetKeyNumb(TorrentBytes, ref StarIndex);
                    temp1 = (char)TorrentBytes[StarIndex];
                }

                getkey = GetKeyText(TorrentBytes, ref StarIndex).ToString().ToUpper();
            }
            if (getkey == "FILES")
            {
                TorrentFileInfoClass Info = new TorrentFileInfoClass();
                while (true)
                {
                    string TextStar = Encoding.UTF8.GetString(TorrentBytes, StarIndex, 1);
                    switch (TextStar)
                    {
                    case "l":
                        StarIndex++;
                        ListCount++;
                        break;

                    case "e":
                        ListCount--;
                        StarIndex++;
                        if (ListCount == 1)
                        {
                            TorrentFileInfo.Add(Info);
                        }
                        if (ListCount == 0)
                        {
                            return;
                        }
                        break;

                    case "d":
                        Info = new TorrentFileInfoClass();
                        ListCount++;
                        StarIndex++;
                        break;

                    default:
                        object ListText = GetKeyText(TorrentBytes, ref StarIndex);
                        if (ListText == null)
                        {
                            return;
                        }
                        switch (ListText.ToString().ToUpper())       //转换为大写
                        {
                        case "ED2K":
                            Info.De2K = GetKeyText(TorrentBytes, ref StarIndex).ToString();
                            break;

                        case "FILEHASH":
                            Info.FileHash = GetKeyText(TorrentBytes, ref StarIndex).ToString();
                            break;

                        case "LENGTH":
                            Info.Length = Convert.ToInt64(GetKeyNumb(TorrentBytes, ref StarIndex));
                            break;

                        case "PATH":
                            int       PathCount = 0;
                            ArrayList PathList  = GetKeyData(TorrentBytes, ref StarIndex, ref PathCount);
                            string    Temp      = "";
                            for (int i = 0; i != PathList.Count; i++)
                            {
                                Temp += PathList[i].ToString();
                            }
                            Info.Path = Temp;
                            break;

                        case "PATH.UTF-8":
                            int       PathUtf8Count = 0;
                            ArrayList Pathutf8List  = GetKeyData(TorrentBytes, ref StarIndex, ref PathUtf8Count);
                            string    UtfTemp       = "";
                            for (int i = 0; i != Pathutf8List.Count; i++)
                            {
                                UtfTemp += Pathutf8List[i].ToString();
                            }
                            Info.PathUTF8 = UtfTemp;
                            break;
                        }
                        break;
                    }
                }
            }
            else if (getkey == "LENGTH")
            {
                //单一文件结构时
                TorrentFileInfoClass Info = new TorrentFileInfoClass();
                Info.Length = Convert.ToInt64(GetKeyNumb(TorrentBytes, ref StarIndex));
                string test = Encoding.UTF8.GetString(TorrentBytes, StarIndex, TorrentBytes.Length - StarIndex > 500 ? 500 : TorrentBytes.Length - StarIndex);

                string TextStar = Encoding.UTF8.GetString(TorrentBytes, StarIndex, 1);

                object ListText = GetKeyText(TorrentBytes, ref StarIndex);
                if (ListText.ToString().ToUpper() == "NAME")
                {
                    Info.Path = GetKeyText(TorrentBytes, ref StarIndex).ToString();
                    TorrentFileInfo.Add(Info);
                    TorrentName = Info.Path;
                }
            }
        }