IList <TorrentFile> LoadTorrentFiles(BEncodedList list) { var sb = new StringBuilder(32); var files = new List <(string path, long length, byte[] md5sum, byte[] ed2k, byte[] sha1)> (); foreach (BEncodedDictionary dict in list) { long length = 0; string path = null; byte[] md5sum = null; byte[] ed2k = null; byte[] sha1 = null; foreach (KeyValuePair <BEncodedString, BEncodedValue> keypair in dict) { switch (keypair.Key.Text) { case ("sha1"): sha1 = ((BEncodedString)keypair.Value).TextBytes; break; case ("ed2k"): ed2k = ((BEncodedString)keypair.Value).TextBytes; break; case ("length"): length = long.Parse(keypair.Value.ToString()); break; case ("path.utf-8"): foreach (BEncodedString str in ((BEncodedList)keypair.Value)) { sb.Append(str.Text); sb.Append(Path.DirectorySeparatorChar); } path = sb.ToString(0, sb.Length - 1); sb.Remove(0, sb.Length); break; case ("path"): if (string.IsNullOrEmpty(path)) { foreach (BEncodedString str in ((BEncodedList)keypair.Value)) { sb.Append(str.Text); sb.Append(Path.DirectorySeparatorChar); } path = sb.ToString(0, sb.Length - 1); sb.Remove(0, sb.Length); } break; case ("md5sum"): md5sum = ((BEncodedString)keypair.Value).TextBytes; break; default: break; //FIXME: Log unknown values } } PathValidator.Validate(path); files.Add((path, length, md5sum, ed2k, sha1)); Size += length; } return(Array.AsReadOnly(TorrentFile.Create(PieceLength, files.ToArray()))); }
IList <TorrentFile> LoadTorrentFiles(BEncodedList list) { int endIndex; int startIndex; var sb = new StringBuilder(32); var files = new List <TorrentFile> (); foreach (BEncodedDictionary dict in list) { long length = 0; string path = null; byte[] md5sum = null; byte[] ed2k = null; byte[] sha1 = null; foreach (KeyValuePair <BEncodedString, BEncodedValue> keypair in dict) { switch (keypair.Key.Text) { case ("sha1"): sha1 = ((BEncodedString)keypair.Value).TextBytes; break; case ("ed2k"): ed2k = ((BEncodedString)keypair.Value).TextBytes; break; case ("length"): length = long.Parse(keypair.Value.ToString()); break; case ("path.utf-8"): foreach (BEncodedString str in ((BEncodedList)keypair.Value)) { sb.Append(str.Text); sb.Append(Path.DirectorySeparatorChar); } path = sb.ToString(0, sb.Length - 1); sb.Remove(0, sb.Length); break; case ("path"): if (string.IsNullOrEmpty(path)) { foreach (BEncodedString str in ((BEncodedList)keypair.Value)) { sb.Append(str.Text); sb.Append(Path.DirectorySeparatorChar); } path = sb.ToString(0, sb.Length - 1); sb.Remove(0, sb.Length); } break; case ("md5sum"): md5sum = ((BEncodedString)keypair.Value).TextBytes; break; default: break; //FIXME: Log unknown values } } // A zero length file always belongs to the same piece as the previous file if (length == 0) { if (files.Count > 0) { startIndex = files[files.Count - 1].EndPieceIndex; endIndex = files[files.Count - 1].EndPieceIndex; } else { startIndex = 0; endIndex = 0; } } else { startIndex = (int)(Size / PieceLength); endIndex = (int)((Size + length) / PieceLength); if ((Size + length) % PieceLength == 0) { endIndex--; } } PathValidator.Validate(path); files.Add(new TorrentFile(path, length, startIndex, endIndex, (int)(Size % PieceLength), md5sum, ed2k, sha1)); Size += length; } return(files.AsReadOnly()); }