All pastes #809764 Raw Edit

Kendall

public text v1 · immutable
·published 2007-12-09 06:23 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace NzbLibrary.Decoding
{
	public class yEncDecoder
	{
		private static CRC32 CRC = new CRC32();
		private static bool m_CRCFailed = false;
		private static bool m_InyEnc = false;

		public static void Reset()
		{
			m_InyEnc = false;
		}

		public static bool CRCFailed { get { return m_CRCFailed; } }

		public static void DecodeLine(string Line, Stream Output)
		{
			if (Line == null) { return; }

			if (Line.StartsWith("=ybegin") && !m_InyEnc)
			{
				CRC.StartByteCRC();
				m_InyEnc = true;
			}
			else if (Line.StartsWith("=ypart") && m_InyEnc)
			{
				string[] ypart = Line.Split(" ".ToCharArray());
				foreach (string s in ypart)
				{
					if (s.StartsWith("begin"))
					{
						Output.Seek(long.Parse(s.Remove(0, 6)) - 1, SeekOrigin.Begin);
					}
				}
			}
			else if (Line.StartsWith("=yend") && m_InyEnc)
			{
				m_InyEnc = false;
				string[] yend = Line.Split(" ".ToCharArray());
				foreach (string s in yend)
				{
					if (s.StartsWith("pcrc32"))
					{
						long opcrc = Convert.ToInt64(s.Remove(0, 7), 16);
						long cpcrc = CRC.EndByteCRC();

						m_CRCFailed = (opcrc != cpcrc);
					}
				}
			}
			else if (m_InyEnc)
			{
				bool Escape = false;
				foreach (char c in Line.ToCharArray())
				{
					if (c == '=' && !Escape)
					{
						Escape = true;
					}
					else
					{
						byte nc = (byte)c;
						if (Escape)
						{
							nc = (byte)(nc - 64);
							Escape = false;
						}

						nc = (byte)(nc - 42);
						Output.WriteByte(nc);
						CRC.AddByteCRC(nc);
					}
				}
			}
		}
	}
}