public void BasicTest() { YEncEncoder encoder = new YEncEncoder(); string toEncode = "encode me"; byte[] source = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode); byte[] dest = new byte[4096]; int numBytes = encoder.GetBytes(source, 0, source.Length, dest, 0, true); Assert.IsTrue(numBytes != 0, "Should convert some bytes"); Assert.AreEqual(source.Length, numBytes, "No escaped chars, so should be the same length"); Assert.AreEqual(numBytes, encoder.GetByteCount(source, 0, source.Length, true), "GetByteCount should return the same count as the actual encoding"); YEncDecoder decoder = new YEncDecoder(); Assert.AreEqual(source.Length, decoder.GetByteCount(dest, 0, numBytes, true), "Should return same number of chars as original"); byte[] source2 = new byte[4096]; int numBytes2 = decoder.GetBytes(dest, 0, numBytes, source2, 0, true); Assert.AreEqual(numBytes, numBytes2, "Should decode to the same number of chars"); string finalText = System.Text.ASCIIEncoding.ASCII.GetString(source2, 0, numBytes2); Assert.AreEqual(toEncode, finalText, "Decoded back to original text"); // string result = System.Text.ASCIIEncoding.ASCII.GetString(bytes, 0, numBytes); // System.Diagnostics.Debug.WriteLine(result); }
public void FullByteRangeTest() { //writing MemoryStream ms = new MemoryStream(); //this could be any stream we want to write to YEncEncoder encoder = new YEncEncoder(); CryptoStream cs = new CryptoStream(ms, encoder, CryptoStreamMode.Write); byte[] bytes = new byte[256]; for(int i=byte.MinValue; i<=byte.MaxValue; i++) { bytes[i] = (byte)i; } BinaryWriter w = new BinaryWriter(cs); w.Write(bytes, 0, 256); w.Flush(); cs.Flush(); //reading back from the memorystream ms.Position = 0; YEncDecoder decoder = new YEncDecoder(); CryptoStream cs2 = new CryptoStream(ms, decoder, CryptoStreamMode.Read); BinaryReader r = new BinaryReader(cs2); byte[] newBytes = r.ReadBytes(256); Assert.AreEqual(BitConverter.ToString(bytes, 0, 256), BitConverter.ToString(newBytes, 0, 256)); }
private byte[] Download(IEnumerable <string> body) { byte[] buffer = new byte[Utilities.ARTICLE_SIZE]; using (MemoryStream tmpStream = new MemoryStream()) { tmpStream.Position = 0; StreamWriter sw = new StreamWriter(tmpStream, System.Text.Encoding.GetEncoding("ISO-8859-1")); sw.NewLine = Rfc977NntpClient.NEWLINE; #if YENC_HEADER_FOOTER List <string> lines = new List <string>(body); if (lines.Count < 2) { throw new InvalidDataException("Not enough data in article"); } if (!lines[0].StartsWith("=ybegin")) { throw new InvalidDataException("Must start with =ybegin"); } if (!lines[lines.Count - 1].StartsWith("=yend")) { throw new InvalidDataException("Must end with =yend"); } lines.RemoveAt(0); lines.RemoveAt(lines.Count - 1); foreach (var line in lines) { sw.WriteLine(line); } #else foreach (var line in body) { sw.WriteLine(line); } #endif sw.Flush(); tmpStream.Position = 0; YEncDecoder yencDecoder = new YEncDecoder(); using (CryptoStream yencStream = new CryptoStream(tmpStream, yencDecoder, CryptoStreamMode.Read)) { List <byte> b = new List <byte>(Utilities.ARTICLE_SIZE); int read = 0; while ((read = yencStream.Read(buffer, 0, buffer.Length)) > 0) { b.AddRange(buffer.Take(read)); } return(b.ToArray()); } } }
public void Download(IEnumerable <string> body, Stream outputStream) { byte[] buffer = new byte[4096]; using (MemoryStream tmpStream = new MemoryStream()) { tmpStream.Position = 0; StreamWriter sw = new StreamWriter(tmpStream, System.Text.Encoding.GetEncoding("ISO-8859-1")); #if YENC_HEADER_FOOTER List <string> lines = new List <string>(body); if (lines.Count < 2) { throw new InvalidDataException("Not enough data in article"); } if (!lines[0].StartsWith("=ybegin")) { throw new InvalidDataException("Must start with =ybegin"); } if (!lines[lines.Count - 1].StartsWith("=yend")) { throw new InvalidDataException("Must end with =yend"); } lines.RemoveAt(0); lines.RemoveAt(lines.Count - 1); foreach (var line in lines) { sw.WriteLine(line); } #else foreach (var line in body) { sw.WriteLine(line); } #endif sw.Flush(); tmpStream.Position = 0; YEncDecoder yencDecoder = new YEncDecoder(); using (CryptoStream yencStream = new CryptoStream(tmpStream, yencDecoder, CryptoStreamMode.Read)) { int read = 0; while ((read = yencStream.Read(buffer, 0, buffer.Length)) > 0) { outputStream.Write(buffer, 0, read); } } } }
/// <summary> /// Fetches overview database information from a group within a given range of article ids. /// A call to SetGroup must be made before calling this method or it will fail. /// </summary> /// <param name="articleStart">The start of the article range to pull overview information for.</param> /// <param name="articleEnd">The end of the article range to pull overview information for.</param> /// <returns></returns> public IEnumerable <Overview> GetXOverview(ulong articleStart, ulong articleEnd) { var result = conn.WriteLine("XZVER {0}-{1}", articleStart, articleEnd); if (result.IsGood) { var decoder = new YEncDecoder(conn); decoder.Decode(d => { }); var stream = new DeflateStream(decoder.Result, CompressionMode.Decompress); var headers = new StreamReader(stream).ReadToEnd(); return(headers.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Select(h => new Overview(h))); } return(null); }
public void BasicStringTest() { //writing MemoryStream ms = new MemoryStream(); //this could be any stream we want to write to YEncEncoder encoder = new YEncEncoder(); CryptoStream cs = new CryptoStream(ms, encoder, CryptoStreamMode.Write); StreamWriter w = new StreamWriter(cs); w.Write("Test string"); w.Flush(); cs.Flush(); //reading back from the memorystream ms.Position = 0; YEncDecoder decoder = new YEncDecoder(); CryptoStream cs2 = new CryptoStream(ms, decoder, CryptoStreamMode.Read); StreamReader r = new StreamReader(cs2); string finalText = r.ReadToEnd(); Assert.AreEqual("Test string", finalText); }
public void BinaryTest() { //create a binary const int sampleSize = 2000; byte[] original = new byte[sampleSize]; new System.Random().NextBytes(original); YEncEncoder encoder = new YEncEncoder(); byte[] encoded = new byte[4096]; int encodedBytes = encoder.GetBytes(original, 0, sampleSize, encoded, 0, true); Assert.IsTrue(encodedBytes > sampleSize, "Should always be larger than original"); YEncDecoder decoder = new YEncDecoder(); byte[] decoded = new byte[4096]; int decodedBytes = decoder.GetBytes(encoded, 0, encodedBytes, decoded, 0, true); Assert.AreEqual(sampleSize, decodedBytes, "Should decode back to the orginal length"); for(int i=0; i<sampleSize; i++) { Assert.AreEqual(original[i], decoded[i], "Each byte should match, failed on " + i.ToString()); } }
/// <summary> /// Fetches overview database information from a group within a given range of article ids. /// A call to SetGroup must be made before calling this method or it will fail. /// </summary> /// <param name="articleStart">The start of the article range to pull overview information for.</param> /// <param name="articleEnd">The end of the article range to pull overview information for.</param> /// <returns></returns> public IEnumerable<Overview> GetXOverview(ulong articleStart, ulong articleEnd) { var result = conn.WriteLine("XZVER {0}-{1}", articleStart, articleEnd); if(result.IsGood) { var decoder = new YEncDecoder(conn); decoder.Decode(d => { }); var stream = new DeflateStream(decoder.Result, CompressionMode.Decompress); var headers = new StreamReader(stream).ReadToEnd(); return headers.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Select(h => new Overview(h)); } return null; }
public void NormalUsageTest() { //Test normal usage - encode to a file, then open the file and decode it. const int sampleSize = 100000; const int batchSize = 500; const string sampleFileName = "encodedSample.BIN"; byte[] original = new byte[sampleSize]; new System.Random().NextBytes(original); YEncEncoder encoder = new YEncEncoder(); FileStream fs = new FileStream (sampleFileName, FileMode.Create, FileAccess.Write, FileShare.Write); try { for (int i=0; i<sampleSize/batchSize; i++) { bool flush = false; int startByte = i * batchSize; if (startByte + batchSize == sampleSize) flush = true; int destSize = encoder.GetByteCount(original, startByte, batchSize, flush); byte[] destBatch = new byte[destSize]; int bytesWritten = encoder.GetBytes(original, startByte, batchSize, destBatch, 0, flush); Assert.AreEqual(destSize, bytesWritten, "GetGyteCount must return the same as GetBytes"); //here is where we would write off the batch to a file. Lets do that. fs.Write(destBatch, 0, bytesWritten); } } finally { fs.Close(); } YEncDecoder decoder = new YEncDecoder(); FileStream fs2 = new FileStream (sampleFileName, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] decoded = new byte[sampleSize]; //I have faith, it will be right :) int destIndex = 0; try { byte[] buffer = new byte[batchSize]; int bytesRead = fs2.Read(buffer, 0, batchSize); while (bytesRead > 0) { int expectedBytes = decoder.GetByteCount(buffer, 0, bytesRead, false); int decodedBytes = decoder.GetBytes(buffer, 0, bytesRead, decoded, destIndex, false); Assert.AreEqual(expectedBytes, decodedBytes, "GetByteCount should return the same as GetBytes"); destIndex += decodedBytes; bytesRead = fs2.Read(buffer, 0, batchSize); } decoder.GetBytes(new byte[0], 0, 0, decoded, destIndex, true); //force a flush } finally { fs2.Close(); File.Delete(sampleFileName); } //final checks System.Diagnostics.Debug.WriteLine("original: " + BitConverter.ToString(new CRC32().ComputeHash(original, 0, sampleSize))); System.Diagnostics.Debug.WriteLine("encoder: " + BitConverter.ToString(encoder.CRCHash)); System.Diagnostics.Debug.WriteLine("decoder: " + BitConverter.ToString(decoder.CRCHash)); Assert.AreEqual(BitConverter.ToString(new CRC32().ComputeHash(original)), BitConverter.ToString(encoder.CRCHash), "Encoder should match original hash"); Assert.AreEqual(BitConverter.ToString(new CRC32().ComputeHash(original)), BitConverter.ToString(decoder.CRCHash), "Decoder should match original hash"); Assert.AreEqual(BitConverter.ToString(encoder.CRCHash), BitConverter.ToString(decoder.CRCHash), "The CRC32 hashes should match"); for(int i=0; i<sampleSize; i++) { Assert.AreEqual(original[i], decoded[i], "Final check failed at byte " + i.ToString()); } }
public void SampleDecode() { FileStream fs = File.OpenRead(@"..\..\Samples\Encoded.bin"); try { byte[] encoded = new byte[4096]; int encodedBytes = fs.Read(encoded, 0, 4096); YEncDecoder decoder = new YEncDecoder(); byte[] decoded = new byte[4096]; int decodedBytes = decoder.GetBytes(encoded, 0, encodedBytes, decoded, 0, true); Assert.AreEqual(584, decodedBytes, "Test file supplied was 584 before encoding"); byte counter = 255; for(int i=0x24; i<0x124; i++) { Assert.AreEqual(counter, decoded[i], "Checking against 255..0 failed on byte " + i.ToString()); counter--; } } finally { fs.Close(); } }