using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace ProtocolParser
{
    public static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        public static void Main()
        {
			byte[] validateNew;

			validateNew = new byte[]
			{
				0xBA, 0x67, 0x1C, 0x34, 0xF2, 0x43, 0x2D, 0x13, 0x37, 0x50, 0x1E, 0x64, 0xCA, 0x25, 0x52, 0x0D,
				0x06, 0x23, 0x33, 0x47, 0x3F, 0x0C, 0x12, 0x44, 0xA6, 0x74, 0xF3, 0x53, 0x5E, 0x37, 0xD9, 0x4B,
				0x23, 0x4A, 0x4C, 0x74, 0x2B, 0x3E, 0x2D, 0x21, 0x37, 0x29, 0x19, 0x65, 0xE3, 0x3F, 0xE4, 0x24,
				0xB5, 0x73, 0xF6, 0x33, 0x84, 0x3A, 0x94, 0x28, 0x27, 0x12, 0x53, 0x4F, 0x8F, 0x2A, 0xE2, 0x3B
			};

			/*
			validateNew = new byte[]
			{
				0x89, 0x58, 0x56, 0x63, 0x94, 0x5B, 0x9A, 0x5D, 0x38, 0x39, 0x7C, 0x5B, 0xC9, 0x06, 0x4C, 0x7A,
				0x72, 0x7C, 0x1E, 0x69, 0x2F, 0x6E, 0x6E, 0x50, 0x4E, 0x1D, 0x09, 0x44, 0x36, 0x2F, 0x42, 0x79,
				0x53, 0x3B, 0xB3, 0x0B, 0x49, 0x31, 0x19, 0x2D, 0xE0, 0x21, 0x13, 0x41, 0x50, 0x08, 0xC7, 0x2B,
				0xCB, 0x5E, 0xB6, 0x12, 0x7A, 0x60, 0x54, 0x09, 0x49, 0x2C, 0xEF, 0x1D, 0x02, 0x05, 0x08, 0x02
			};

			validateNew = new byte[]
			{
				0xBA, 0x67, 0x1C, 0x34, 0xF2, 0x43, 0x2D, 0x13, 0x37, 0x50, 0x1E, 0x64, 0xCA, 0x25, 0x52, 0x0D,
				0x06, 0x23, 0x33, 0x47, 0x3F, 0x0C, 0x12, 0x44, 0xA6, 0x74, 0xF3, 0x53, 0x5E, 0x37, 0xD9, 0x4B,
				0x23, 0x4A, 0x4C, 0x74, 0x2B, 0x3E, 0x2D, 0x21, 0x37, 0x29, 0x19, 0x65, 0xE3, 0x3F, 0xE4, 0x24,
				0xB5, 0x73, 0xF6, 0x33, 0x84, 0x3A, 0x94, 0x28, 0x27, 0x12, 0x53, 0x4F, 0x8F, 0x2A, 0xE2, 0x3B
			};

			validateNew = new byte[]
			{
				0x62, 0x19, 0x82, 0x22, 0xCB, 0x49, 0x22, 0x7D, 0xFC, 0x53, 0x81, 0x77, 0xA8, 0x64, 0x3F, 0x66,
				0xC2, 0x55, 0x10, 0x7C, 0x96, 0x75, 0xE9, 0x22, 0xAD, 0x6F, 0xD5, 0x63, 0xEF, 0x2D, 0x1C, 0x7A,
				0x95, 0x7B, 0xDB, 0x54, 0xE9, 0x40, 0x4B, 0x22, 0xF3, 0x25, 0x41, 0x1A, 0x76, 0x1B, 0xAF, 0x29,
				0xEB, 0x04, 0x2F, 0x6F, 0x68, 0x53, 0xDE, 0x38, 0x8A, 0x20, 0x0E, 0x76, 0xD9, 0x11, 0x69, 0x0B
			};
			*/

			/*
			for(UInt16 i = 0; i < 64; i += 8)
			{
				UInt32 value = (UInt16)validateNew[i];

				value ^= (UInt16)validateNew[i + 1];
				value ^= (UInt16)validateNew[i + 3];
				value ^= previousValue;
				value ^= (UInt16)validateNew[i + 2];

				previousValue = value;
			}
			*/

			/*
			using (MemoryStream memoryStream = new MemoryStream())
			using(BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
			{
				UInt16 value = 1;
				UInt16 randomValue;

				Random random = new Random();

				for (UInt16 i = 0; i < 32; i++)
				{
					randomValue = Convert.ToUInt16((UInt16)random.Next());

					value ^= randomValue;

					binaryWriter.Write(randomValue);
				}

				validateNew = memoryStream.ToArray();
			}
			*/

			validateNew = new byte[]
			{
				0x8F, 0x08, 0x81, 0x7B, 0x59, 0x63, 0x7C, 0x7A, 0x02, 0x57, 0xD0, 0x71, 0xB3, 0x0D, 0xD1, 0x3C,
				0x17, 0x10, 0x95, 0x79, 0x98, 0x43, 0xCC, 0x00, 0x6B, 0x2B, 0x0A, 0x29, 0x23, 0x0B, 0xA8, 0x19,
				0x9B, 0x3E, 0x69, 0x51, 0x7C, 0x7A, 0x80, 0x67, 0xAA, 0x01, 0xB9, 0x01, 0x59, 0x7F, 0xC0, 0x10,
				0xA3, 0x72, 0xE7, 0x2B, 0xC5, 0x3F, 0xDA, 0x6F, 0x7F, 0x5C, 0x18, 0x20, 0x21, 0x78, 0x3F, 0x57
			};

			validateNew = new byte[]
			{
				0x85, 0x39, 0xA1, 0x71, 0x5B, 0x12, 0x32, 0x34, 0x0B, 0x65, 0x16, 0x68, 0x26, 0x54, 0x90, 0x4E,
				0xF3, 0x08, 0x99, 0x6C, 0x28, 0x1A, 0x27, 0x15, 0x98, 0x31, 0xAA, 0x2A, 0x32, 0x30, 0x91, 0x79,
				0xFD, 0x4A, 0xD2, 0x5B, 0xF9, 0x30, 0xC4, 0x15, 0x0E, 0x09, 0xD8, 0x1E, 0x73, 0x50, 0xE8, 0x5C,
				0x29, 0x25, 0x38, 0x4F, 0x84, 0x25, 0x66, 0x4E, 0xDD, 0x14, 0xD1, 0x70, 0xDA, 0x36, 0x69, 0x3C
			};

			if ((validateNew.Length % 8) == 0)
			{
				UInt32 previousValue = 0;

				using (MemoryStream memoryStream = new MemoryStream(validateNew))
				using (BinaryReader binaryReader = new BinaryReader(memoryStream))
				{
					UInt16 count = 0;

					while (memoryStream.Position != memoryStream.Length)
					{
						UInt16 value1 = binaryReader.ReadUInt16();
						UInt16 value2 = binaryReader.ReadUInt16();
						UInt16 value3 = binaryReader.ReadUInt16();
						UInt16 value4 = binaryReader.ReadUInt16();

						UInt32 value = value1;

						value ^= value2;
						value ^= value4;
						value ^= previousValue;
						value ^= value3;

						previousValue = value;

						count++;
					}
				}
			}

			// SHOULD ALWAYS BE TRUE WHEN READING FROM WIRESHARK SINCE ALL PACKETS ARE POST BIT SHIFTING
			bool isRecievePacket = true;

			UInt16 PacketType = 0x0721;

			if (isRecievePacket)
			{
				PacketType = (UInt16)(((PacketType >> 0xA) << 0x8) + (PacketType & 0x3FF));
			}
			else
			{
				PacketType = (UInt16)(((PacketType >> 0x8) << 0xA) + (PacketType & 0xFF));
			}

			Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FrmMain());
        }
    }
}