public void TestBIntegerNotEqualToObject() { BInteger bint = 10; object obj = new object(); bool result = bint.Equals(obj); Assert.IsFalse(result); }
public override LLocal Parse(BinaryReaderEx reader, BHeader header) { LString name = header.String.Parse(reader, header); BInteger start = header.Integer.Parse(reader, header), end = header.Integer.Parse(reader, header); return(new LLocal(name, start, end)); }
public void Encode(BInteger input, Stream outputStream) { outputStream.WriteByte(Definitions.ASCII_i); byte[] numberStringBytes = Encoding.ASCII.GetBytes(input.Value.ToString()); outputStream.Write(numberStringBytes, 0, numberStringBytes.Length); outputStream.WriteByte(Definitions.ASCII_e); }
public void TestDecodeBInteger() { IBElement[] list = BencodeDecoder.Decode("i45e"); Assert.IsNotNull(list); Assert.AreEqual(1, list.Length); Assert.IsNotNull(list[0]); Assert.IsInstanceOfType(list[0], typeof(BInteger)); BInteger integer = (BInteger)list[0]; Assert.AreEqual(45L, integer.Value); }
public BList <T> ParseList(BinaryReaderEx reader, BHeader header) { BInteger length = header.Integer.Parse(reader, header); List <T> values = new(); length.Iterate(Run); void Run() => values.Add(Parse(reader, header)); return(new BList <T>(length, values)); }
private IEnumerable <byte[]> SplitPieces(BDictionary info, BInteger pieceLength) { /***********************************************************************************/ /* Performs the actual splitting of sha1 data. */ /***********************************************************************************/ var bString = info[TorrentKeys.InfoPieces] as BString; if (bString != null) { byte[] rawChecksums = Encoding.ASCII.GetBytes(bString.InnerValue); return(rawChecksums.Split(20)); } throw new NullBencodedEntryException($"{TorrentKeys.InfoPieces} cannot be null"); }
public void TestBIntegerEquality() { BInteger bint1 = new BInteger(25); BInteger bint2 = new BInteger(25); BInteger bint3 = new BInteger(10); Assert.IsTrue(bint1 == bint2); Assert.IsFalse(bint1 == bint3); Assert.IsFalse(null == bint2); Assert.IsFalse(bint1 == null); Assert.IsFalse(bint1 != bint2); Assert.IsTrue(bint1 != bint3); Assert.IsTrue(null != bint2); Assert.IsTrue(bint1 != null); }
public void TestBInteger_String() { BInteger bint = 123; var tb = bint.ToBencodedString(null); Assert.IsNotNull(tb); Assert.AreEqual("i123e", tb.ToString()); Assert.AreEqual("123", bint.ToString()); Assert.AreEqual("i123e", bint.ToBencodedString()); BInteger bint2 = 123; BInteger bint3 = 12; Assert.AreEqual(bint.GetHashCode(), bint2.GetHashCode()); Assert.AreNotEqual(bint.GetHashCode(), bint3.GetHashCode()); }
public static TorrentFileInfo Parse(BDictionary dictionary) { if (dictionary == null) { throw new ArgumentNullException("dictionary"); } var file = new TorrentFileInfo(); foreach (var item in dictionary) { if (item.Key == null) { continue; } switch (item.Key.Value) { case "length": BInteger integer = item.Value as BInteger; if (integer != null) { file.Length = integer.Value; } break; case "path": BList listItems = item.Value as BList; if (listItems != null) { foreach (var listItem in listItems) { if (listItem != null) { file.Path.Add(listItem.ToString()); } } } break; } } return(file); }
private void TestBInteger(long testInteger) { BInteger bInteger = new BInteger(testInteger); byte[] encoded = bInteger.Encode(); Assert.True(BObject.TryParse(encoded, out BInteger bObject)); Assert.Equal(testInteger, bObject.Value); using (MemoryStream ms = new MemoryStream()) { bInteger.Encode(ms); encoded = ms.ToArray(); } Assert.True(BObject.TryParse(encoded, out bObject)); Assert.Equal(testInteger, bObject.Value); Assert.Equal(encoded.Length, bInteger.EncodedSize); }
private List <byte[]> GetRawChecksums(BDictionary info, BInteger pieceLength) { /************************************************************************************/ /* Splits pieces string into blocks of 20 bytes (the size of the SHA1 of each file.)*/ /************************************************************************************/ if (info["pieces"] == null) { throw new NullReferenceException(); } var infoPieces = info["pieces"] as BString; Debug.Assert(infoPieces != null, "infoPieces != null"); byte[] rawChecksum = Encoding.ASCII.GetBytes(infoPieces.InnerValue); if (pieceLength == null || rawChecksum == null || rawChecksum.Length % CheckSumSize != 0) { throw new Exception(); } return(SplitPieces(info, pieceLength).ToList()); }
public void BIntegerTest() { int unencodedInt = 10; int unencodedNegativeInt = -10; long unencodedLong = long.MaxValue; string expectedInt = $"i{unencodedInt}e"; string expectedLong = $"i{long.MaxValue}e"; string expectedNegative = $"i{unencodedNegativeInt}e"; var bIntSmall = new BInteger(unencodedInt); var bIntLarge = new BInteger(unencodedLong); var bIntNegative = new BInteger(unencodedNegativeInt); Console.WriteLine(expectedInt); Console.WriteLine(expectedLong); Console.WriteLine(expectedNegative); Assert.AreEqual(expectedInt, bIntSmall.BencodedString); Assert.AreEqual(expectedLong, bIntLarge.BencodedString); Assert.AreEqual(expectedNegative, bIntNegative.BencodedString); }
public void TestBIntegerGreaterLowerOperator() { BInteger bint1 = new BInteger(1); BInteger bint2 = new BInteger(2); BInteger bint3 = new BInteger(2); BInteger bint4 = new BInteger(3); Assert.IsTrue(bint1 < bint2); Assert.IsTrue(bint2 < bint4); Assert.IsFalse(bint4 < bint1); Assert.IsFalse(bint2 < bint3); Assert.IsFalse(bint1 > bint2); Assert.IsFalse(bint2 > bint4); Assert.IsTrue(bint4 > bint1); Assert.IsFalse(bint2 > bint3); Assert.AreEqual(1, bint1.CompareTo(null)); Assert.AreEqual(0, BInteger.Compare(null, null)); Assert.AreEqual(1, BInteger.Compare(bint1, null)); Assert.AreEqual(-1, BInteger.Compare(null, bint1)); }
public void TestDecodeBDictionary() { IBElement[] result = BencodeDecoder.Decode("d5:itemsli45e28:aBCdefghijklmnopqrstuvwxyz12ee"); Assert.IsNotNull(result); Assert.AreEqual(1, result.Length); Assert.IsNotNull(result[0]); Assert.IsInstanceOfType(result[0], typeof(BDictionary)); BDictionary dict = (BDictionary)result[0]; Assert.AreEqual(1, dict.Count); var item = dict.First(); Assert.IsNotNull(item); BString key = item.Key; Assert.IsNotNull(key); Assert.AreEqual("items", key.Value); Assert.IsNotNull(item.Value); Assert.IsInstanceOfType(item.Value, typeof(BList)); BList list = (BList)item.Value; Assert.AreEqual(2, list.Count); Assert.IsNotNull(list[0]); Assert.IsInstanceOfType(list[0], typeof(BInteger)); BInteger integer = (BInteger)list[0]; Assert.AreEqual(45L, integer.Value); Assert.IsNotNull(list[1]); Assert.IsInstanceOfType(list[1], typeof(BString)); BString str = (BString)list[1]; Assert.AreEqual("aBCdefghijklmnopqrstuvwxyz12", str.Value); }
public void TestBEncodeComplexString2() { const string initialString = "i456e2:bei67e"; IBElement[] result = BinaryBencoding.Decode(initialString); Assert.IsNotNull(result); Assert.AreEqual(3, result.Length); BInteger integer = result[0] as BInteger; Assert.IsNotNull(integer); Assert.AreEqual(456, integer.Value); BString str = result[1] as BString; Assert.IsNotNull(str); Assert.AreEqual("be", str.Value); BInteger integer2 = result[2] as BInteger; Assert.IsNotNull(integer2); Assert.AreEqual(67, integer2.Value); }
public void TestDecodeBList() { IBElement[] result = BencodeDecoder.Decode("li45e28:aBCdefghijklmnopqrstuvwxyz12e"); Assert.IsNotNull(result); Assert.AreEqual(1, result.Length); Assert.IsNotNull(result[0]); Assert.IsInstanceOfType(result[0], typeof(BList)); BList list = (BList)result[0]; Assert.AreEqual(2, list.Count); Assert.IsNotNull(list[0]); Assert.IsInstanceOfType(list[0], typeof(BInteger)); BInteger integer = (BInteger)list[0]; Assert.AreEqual(45L, integer.Value); Assert.IsNotNull(list[1]); Assert.IsInstanceOfType(list[1], typeof(BString)); BString str = (BString)list[1]; Assert.AreEqual("aBCdefghijklmnopqrstuvwxyz12", str.Value); }
public BInteger(BInteger b) { _big = b._big; _n = b._n; }
public BList(BInteger length, List <T> values) { Length = length; _values = values; }
public static TorrentInfo Parse(BDictionary dictionary) { Contract.Requires(dictionary != null); if (dictionary == null) { throw new ArgumentNullException("dictionary"); } var torrent = new TorrentInfo(); TorrentFileInfo singleFile = new TorrentFileInfo(); bool isSingleFile = false; foreach (var item in dictionary) { if (item.Key == null) { continue; } if (item.Key.Value == "announce") { if (item.Value != null) { torrent.Announce = item.Value.ToString(); } } else if (item.Key.Value == "created by") { if (item.Value != null) { torrent.CreatedBy = item.Value.ToString(); } } else if (item.Key.Value == "creation date") { BInteger integer = item.Value as BInteger; if (integer != null) { torrent.CreationDate = new DateTime(1970, 1, 1).AddSeconds(integer.Value); } } else if (item.Key.Value == "encoding") { if (item.Value != null) { torrent.Encoding = item.Value.ToString(); } } else if (item.Key.Value == "info") { BDictionary dict = item.Value as BDictionary; if (dict != null) { ParseInfo(torrent, singleFile, ref isSingleFile, item.Value as BDictionary); } } } if (isSingleFile) { if (singleFile.Path != null) { singleFile.Path.Add(torrent.Name); } torrent.Files.Add(singleFile); } return(torrent); }
private static void ParseInfo(TorrentInfo torrent, TorrentFileInfo singleFile, ref bool isSingleFile, BDictionary dictionary) { Contract.Requires(torrent != null); Contract.Requires(singleFile != null); Contract.Requires(dictionary != null); foreach (var info in dictionary) { if (info.Key == null) { continue; } if (info.Key.Value == "name") { if (info.Value != null) { torrent.Name = info.Value.ToString(); } } else if (info.Key.Value == "piece length") { BInteger integer = info.Value as BInteger; if (integer != null) { torrent.PieceLength = integer.Value; } } else if (info.Key.Value == "pieces") { if (info.Value != null) { torrent.Pieces = info.Value.ToString(); } } else if (info.Key.Value == "private") { BInteger integer = info.Value as BInteger; if (integer != null) { torrent.Private = integer.Value != 0; } } else if (info.Key.Value == "files") { BList files = info.Value as BList; if (files != null) { foreach (var file in files) { BDictionary dict = file as BDictionary; if (dict != null) { torrent.Files.Add(TorrentFileInfo.Parse(dict)); } } } } else if (info.Key.Value == "file-duration") { isSingleFile = true; BList items = info.Value as BList; if (items != null) { foreach (var item in items) { BInteger integer = item as BInteger; if (integer != null) { singleFile.Duration.Add(integer.Value); } } } } else if (info.Key.Value == "file-media") { isSingleFile = true; BList items = info.Value as BList; if (items != null) { foreach (var item in items) { BInteger integer = item as BInteger; if (integer != null) { singleFile.Media.Add(integer.Value); } } } } else if (info.Key.Value == "profiles") { isSingleFile = true; BList items = info.Value as BList; if (items != null) { foreach (var item in items) { BDictionary dictItems = item as BDictionary; if (dictItems != null) { TorrentFileProfileCollection profiles = new TorrentFileProfileCollection(); profiles.AddRange(dictItems.Select(dictItem => new TorrentFileProfile { Name = dictItem.Key.ToString(), Value = dictItem.Value.ToString() })); singleFile.Profiles.Add(profiles); } } } } } }
public LLocal(LString name, BInteger start, BInteger end) { this.name = name; this.Start = start.asInt(); this.end = end.asInt(); }
void createTorrent(TorrentCreationViewModel vm) { /* info: a dictionary that describes the file(s) of the torrent. There are two possible forms: one for the case of a 'single-file' torrent with no directory structure, and one for the case of a 'multi-file' torrent (see below for details) announce: The announce URL of the tracker (string) announce-list: (optional) this is an extention to the official specification, offering backwards-compatibility. (list of lists of strings). The official request for a specification change is here. creation date: (optional) the creation time of the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC) comment: (optional) free-form textual comments of the author (string) created by: (optional) name and version of the program used to create the .torrent (string) encoding: (optional) the string encoding format used to generate the pieces part of the info dictionary in the .torrent metafile (string) */ BDictionary info = new BDictionary(); info["pieces"] = new BString(buildPiecesHash(vm.Media)); info["piece length"] = new BInteger(pieceLength); info["private"] = new BInteger(vm.IsPrivate ? 1 : 0); if (vm.Media.Count == 1) { singleFileTorrent(info, new FileInfo(vm.Media.ElementAt(0).Location)); } else { List<FileInfo> fileInfo = new List<FileInfo>(); foreach (MediaFileItem item in vm.Media) { fileInfo.Add(new FileInfo(item.Location)); } multiFileTorrent(info, vm, fileInfo); } BDictionary metaInfo = new BDictionary(); metaInfo["info"] = info; metaInfo["announce"] = new BString(vm.AnnounceURL); metaInfo["creation date"] = new BInteger((long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalSeconds); metaInfo["created by"] = new BString(createdBy); if (!String.IsNullOrEmpty(vm.Comment) && !String.IsNullOrWhiteSpace(vm.Comment) && vm.IsCommentEnabled) { metaInfo["comment"] = new BString(vm.Comment); } if (!String.IsNullOrEmpty(encoding) && !String.IsNullOrWhiteSpace(encoding) && vm.IsCommentEnabled) { metaInfo["encoding"] = new BString(encoding); } String torrentName = String.IsNullOrEmpty(vm.TorrentName) ? Path.GetFileNameWithoutExtension(vm.Media.ElementAt(0).Location) : vm.TorrentName; String torrentFullName = vm.OutputPath + "\\" + torrentName + ".torrent"; if (CancellationToken.IsCancellationRequested) return; FileStream outputTorrent = null; try { outputTorrent = new FileStream(torrentFullName, FileMode.CreateNew); BinaryWriter bw = new BinaryWriter(outputTorrent); foreach (char c in metaInfo.ToBencodedString()) { bw.Write((byte)c); } InfoMessages.Add("Created torrent file: " + torrentFullName); } finally { if (outputTorrent != null) { outputTorrent.Close(); } } }
public LLocal(LString name, BInteger start, BInteger end) { this.Name = name; this.Start = start.AsInteger(); this.End = end.AsInteger(); }
void singleFileTorrent(BDictionary info, FileInfo file) { info["name"] = new BString(file.Name); info["length"] = new BInteger(file.Length); }
private void WriteInteger(int indentLevel, BInteger integer) { Console.WriteLine("{0}{1}", GetIndentSpaces(indentLevel), integer.Value.ToString()); }
public BSizeT(BInteger b) : base(b) { }
public LLocal(LString name, BInteger start, BInteger end) { Name = name; Start = start.AsInt(); End = end.AsInt(); }
private void EncodeIntegerTest(BInteger input) { string expected = string.Format("i{0}e", input.Value); MemoryStream outputBuffer = new MemoryStream(); var transform = new IntegerTransform(); transform.Encode(input, outputBuffer); outputBuffer.Position = 0; StreamReader sr = new StreamReader(outputBuffer); string actual = sr.ReadToEnd(); Assert.AreEqual<string>(expected, actual); }
void multiFileTorrent(BDictionary info, TorrentCreationViewModel vm, List<FileInfo> files) { BList filesList = new BList(); foreach (FileInfo file in files) { BDictionary fileDictionary = new BDictionary(); fileDictionary["length"] = new BInteger(file.Length); BList pathList = new BList(); String relativePath = file.FullName.Remove(0, vm.PathRoot.Length); foreach (String elem in relativePath.Split(new char[] { '\\' })) { if (!String.IsNullOrEmpty(elem)) { pathList.Add(elem); } } fileDictionary["path"] = pathList; filesList.Add(fileDictionary); } info["name"] = new BString(vm.TorrentName); info["files"] = filesList; }
public void TestBIntegerImplicitOperator() { BInteger bint = 10; Assert.AreEqual(10, bint.Value); }