bool CheckCRC(HeaderType type, Byte[] args, Byte[] crc) { Crc16Ccitt crc16 = new Crc16Ccitt(ZModem_Protocol.Crc16Ccitt.InitialCrcValue.Zeros); Byte[] HeaderCrc = crc16.ComputeChecksumBytes(new Byte[5] { Convert.ToByte((int)type), args[0], args[1], args[2], args[3] }); if ((HeaderCrc[0] == crc[1]) && (HeaderCrc[1] == crc[0])) { return(true); } else { return(false); } }
public static byte[] BuildZFILEFrame(string FileName, int FileSize) { Queue <byte> bframe2send = new Queue <byte>(); string frame = BuildHexFrame(HeaderType.ZFILE, (int)ZFILE_Header_ZF0.NOTHING, (int)ZFILE_Header_ZF1.ZMCLOB, (int)ZFILE_Header_ZF2.NOTHING, (int)ZFILE_Header_ZF3.NOTHING); foreach (char c in frame) { bframe2send.Enqueue((byte)c); } Queue <byte> binframe = new Queue <byte>(); foreach (char c in FileName) { binframe.Enqueue((byte)c); } binframe.Enqueue((byte)'\0'); byte[] b4crc = binframe.Concat(new byte[] { (byte)ZDLE_Sequence.ZCRCW }).ToArray(); //string size = FileSize.ToString(); //foreach (char c in size) // binframe.Enqueue((byte)c); //binframe.Enqueue((byte)' '); //for (int i = 0; i < 7; i++) //{ // binframe.Enqueue((byte)'0'); // binframe.Enqueue((byte)''); //} binframe.Enqueue((byte)ControlBytes.ZDLE); binframe.Enqueue((byte)ZDLE_Sequence.ZCRCW); Crc16Ccitt crc = new Crc16Ccitt(Crc16Ccitt.InitialCrcValue.Zeros); byte[] crcdata = crc.ComputeChecksumBytes(b4crc); binframe.Enqueue(crcdata[0]); binframe.Enqueue(crcdata[1]); return(bframe2send.Concat(binframe).ToArray()); }
bool CheckCRC() { Crc16Ccitt crc16 = new Crc16Ccitt(ZModem_Protocol.Crc16Ccitt.InitialCrcValue.Zeros); if (data.Length > sizeOfFrame) { Array.Resize <byte>(ref data, sizeOfFrame); } Byte[] r = crc16.ComputeChecksumBytes(data); MemoryStream ms = new MemoryStream(); if ((r[0] == crc2) && (r[1] == crc1)) { return(true); } else { return(false); } }
/// <summary> /// Constructor. It decode a Byte array to a ZModem header. /// </summary> /// <param name="data">Byte array</param> /// <param name="startIndex">index of the byte in the array</param> public Header(Byte[] data, int startIndex) { if (startIndex < 0) { return; } int index = startIndex; if (data[index] == Convert.ToByte(ControlBytes.ZPAD)) { index++; if (data[index] == Convert.ToByte(ControlBytes.ZDLE)) { index++; if (data[index] == Convert.ToByte(ControlBytes.ZBIN)) { index++; type = (HeaderType)Enum.Parse(typeof(HeaderType), ((int)DecodeByte(data, ref index)).ToString()); index++; arg1 = DecodeByte(data, ref index); index++; arg2 = DecodeByte(data, ref index); index++; arg3 = DecodeByte(data, ref index); index++; arg4 = DecodeByte(data, ref index); index++; crc1 = DecodeByte(data, ref index); index++; crc2 = DecodeByte(data, ref index); Byte[] b = new Byte[5]; b[0] = Convert.ToByte((int)type); b[1] = arg1; b[2] = arg2; b[3] = arg3; b[4] = arg4; Crc16Ccitt crccomp = new Crc16Ccitt(0); Byte[] crcAttempted = crccomp.ComputeChecksumBytes(b); if ((crc1 != crcAttempted[1]) || (crc2 != crcAttempted[0])) { valid = false; return; //erreur crc } else { valid = true; } endofHeader = index; } } else if (data[index] == Convert.ToByte(ControlBytes.ZPAD)) //Header Hex { index++; if (data[index] == Convert.ToByte(ControlBytes.ZDLE)) { index++; if (data[index] == Convert.ToByte(ControlBytes.ZHEX)) { index++; string val = ((char)DecodeByte(data, ref index)).ToString(); index++; val += ((char)DecodeByte(data, ref index)).ToString(); type = (HeaderType)Enum.Parse(typeof(HeaderType), val); index++; val = ((char)DecodeByte(data, ref index)).ToString(); index++; val += ((char)DecodeByte(data, ref index)).ToString(); int a1 = Convert.ToInt32(val, 16); arg1 = Convert.ToByte(a1); index++; val = ((char)DecodeByte(data, ref index)).ToString(); index++; val += ((char)DecodeByte(data, ref index)).ToString(); int a2 = Convert.ToInt32(val, 16); arg2 = Convert.ToByte(a2); index++; val = ((char)DecodeByte(data, ref index)).ToString(); index++; val += ((char)DecodeByte(data, ref index)).ToString(); int a3 = Convert.ToInt32(val, 16); arg3 = Convert.ToByte(a3); index++; val = ((char)DecodeByte(data, ref index)).ToString(); index++; val += ((char)DecodeByte(data, ref index)).ToString(); int a4 = Convert.ToInt32(val, 16); arg4 = Convert.ToByte(a4); index++; val = ((char)DecodeByte(data, ref index)).ToString(); index++; val += ((char)DecodeByte(data, ref index)).ToString(); int c1 = Convert.ToInt32(val, 16); crc1 = Convert.ToByte(c1); index++; val = ((char)DecodeByte(data, ref index)).ToString(); index++; val += ((char)DecodeByte(data, ref index)).ToString(); int c2 = Convert.ToInt32(val, 16); crc2 = Convert.ToByte(c2); Byte[] b = new Byte[5]; b[0] = Convert.ToByte((int)type); b[1] = arg1; b[2] = arg2; b[3] = arg3; b[4] = arg4; Crc16Ccitt crccomp = new Crc16Ccitt(0); Byte[] crcAttempted = crccomp.ComputeChecksumBytes(b); if ((crc1 != crcAttempted[1]) || (crc2 != crcAttempted[0])) { valid = false; return; //erreur crc } else { valid = true; } endofHeader = index; } } } } }