public override byte[] PackRequest() { byte[] request = new byte[25]; DNP3WriteCommandParameters commandParam = (DNP3WriteCommandParameters)CommandParameters; CommandParameters.Length = 18; Buffer.BlockCopy(headerBuilder.Build(CommandParameters), 0, request, 0, 10); request[10] = commandParam.TransportControl; request[11] = commandParam.AplicationControl; request[12] = commandParam.FunctionCode; Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)commandParam.ObjectTypeField)), 0, request, 13, 2); request[15] = commandParam.Qualifier; Buffer.BlockCopy(BitConverter.GetBytes(Convert.ToUInt16(commandParam.RangeField)), 0, request, 16, 2); Buffer.BlockCopy(BitConverter.GetBytes(Convert.ToUInt16(commandParam.Prefix)), 0, request, 18, 2); Buffer.BlockCopy(BitConverter.GetBytes(Convert.ToUInt16(commandParam.Value)), 0, request, 20, 2); request[22] = 0x00; ushort crc1 = 0; for (int i = 10; i < 23; i++) { CrcCalculator.computeCRC(request[i], ref crc1); } crc1 = (ushort)(~crc1); Buffer.BlockCopy(BitConverter.GetBytes(crc1), 0, request, 23, 2); return(request); }
public void testGetProtocolModeCrc() { int expected_crc = 0x0414; byte[] sample = { (byte)0x83, 0x02, 0x74, 0x01 }; Assert.AreEqual(CrcCalculator.Crc(sample), (expected_crc)); }
public void testReadSiCard6B8() { int expected_crc = 0x4E0A; byte[] sample = { (byte)0xE1, 0x01, 0x08 }; Assert.AreEqual(CrcCalculator.Crc(sample), (expected_crc)); }
public void testAnswerStartupCrc() { int expected_crc = 0x8B12; byte[] sample = { (byte)0xF0, 0x03, 0x01, 0x01, 0x4D }; Assert.AreEqual(CrcCalculator.Crc(sample), (expected_crc)); }
public void testGetCardBlocksCrc() { int expected_crc = 0x1611; byte[] sample = { (byte)0x83, 0x02, 0x33, 0x01 }; Assert.AreEqual(CrcCalculator.Crc(sample), (expected_crc)); }
public override byte[] PackRequest() { byte[] request = new byte[20]; DNP3ReadCommandParameters commadnParam = (DNP3ReadCommandParameters)CommandParameters; CommandParameters.Length = 0x0d; //20 - 2*(2 CRC) - 2 Start - 1 len Buffer.BlockCopy(headerBuilder.Build(CommandParameters), 0, request, 0, 10); request[10] = commadnParam.TransportControl; request[11] = commadnParam.AplicationControl; request[12] = commadnParam.FunctionCode; Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)commadnParam.ObjectTypeField)), 0, request, 13, 2); request[15] = commadnParam.Qualifier; request[16] = (byte)commadnParam.RangeField; request[17] = (byte)commadnParam.RangeField; ushort crc = 0; for (int i = 10; i < 18; i++) { CrcCalculator.computeCRC(request[i], ref crc); } crc = (ushort)(~crc); Buffer.BlockCopy(BitConverter.GetBytes(crc), 0, request, 18, 2); return(request); }
public void testReadSiCard8B0() { int expected_crc = 0xE209; byte[] sample = { (byte)0xEF, 0x01, 0x00 }; Assert.AreEqual(CrcCalculator.Crc(sample), (expected_crc)); }
public static byte[] getSwappedCrc(byte[] chunk) { var crc32 = new CrcCalculator(); var crc = crc32.GetCRC32(chunk); var crcArray = BitConverter.GetBytes(crc); Array.Reverse(crcArray); return(crcArray); }
public static uint calc_crc(string filename) { var b = Encoding.ASCII.GetBytes(filename); for (var i = 0; i < 4; i++) { b[i] = (byte)(~filename[i] & 0xff); } return(CrcCalculator.CaclulateCRC32(b) & 0xFFFFFFFF); }
public void testReadSiCard8Bx() { int[] expected_crc = { 0xE609, 0xE709, 0xE409, 0xE509 }; byte[] sample_byte = { 0x04, 0x05, 0x06, 0x07 }; for (int i = 0; i < expected_crc.Length; i++) { byte[] sample = { (byte)0xEF, 0x01, sample_byte[i] }; Assert.AreEqual((object)CrcCalculator.Crc(sample), (expected_crc[i]), "CRC failed on Block " + sample_byte[i]); } }
public void testReadSiCard6Bx() { int[] expected_crc = { 0x460A, 0x440A, 0x450A, 0x420A, 0x430A, 0x400A, 0x410A }; byte[] sample_byte = { 0x00, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; for (int i = 0; i < expected_crc.Length; i++) { byte[] sample = { (byte)0xE1, 0x01, sample_byte[i] }; Assert.AreEqual(CrcCalculator.Crc(sample), (object)(expected_crc[i]), "CRC failed on Block " + sample_byte[i]); } }
public void TestSampleCRC() { byte[] sample = { 0x53, 0x00, 0x05, 0x01, 0x0F, 0xB5, 0x00, 0x00, 0x1E, 0x08 }; int expected_crc = 0x2C12; Assert.AreEqual(CrcCalculator.Crc(sample), expected_crc); }
public TelloCommand Read(byte[] data) { EndianBinaryReader reader = new EndianBinaryReader(data); Stream inStream = reader.BaseSteam; byte[] header = reader.ReadBytes(3); inStream.Position = 0; byte fid = reader.ReadByte(); ushort size = (ushort)(reader.ReadUInt16() >> 3); if (size < 11) { throw new TelloException( $"Packet too small, expected at least {size}."); } byte crc8 = reader.ReadByte(); byte calcCrc8 = CrcCalculator.Crc8(header); if (crc8 != calcCrc8) { throw new TelloException( $"CRC8 mismatch, {crc8:x} != {calcCrc8:x}."); } byte packetType = reader.ReadByte(); ushort commandId = reader.ReadUInt16(); ushort sequenceId = reader.ReadUInt16(); ushort dataSize = (ushort)(size - HeaderSize); byte[] messageData = reader.ReadBytes(dataSize); ushort crc16 = reader.ReadUInt16(); long pos = inStream.Position; inStream.Position = 0; byte[] crc16Data = reader.ReadBytes(size - 2); inStream.Position = pos; ushort calcCrc16 = CrcCalculator.Crc16(crc16Data); if (crc16 != calcCrc16) { throw new TelloException( $"CRC16 mismatch, {crc16:x} != {calcCrc16:x}."); } return(new TelloCommand(packetType, (TelloCommandId)commandId, sequenceId, messageData)); }
override public int Read(byte[] buffer, int offset, int count) { CheckDisposed(); int readCount = 0; // We should calculate CRC accumulatively for the following conditions // 1. Seek is not supported by the underlying stream: this will be the case for // writing stream in streaming mode // 2. This write request is consequtive to the highwater mark of the CRC calculation // 3. This write request is at 0 offset and the CRC hasn't been calculated yet if (!_underlyingStream.CanSeek) // Case #1 { readCount = _underlyingStream.Read(buffer, offset, count); CrcCalculator.Accumulate(buffer, offset, readCount); } else { long originalPosition = _underlyingStream.Position; readCount = _underlyingStream.Read(buffer, offset, count); // This operation needs to be done after Read since read can throw an exception; in that case // we want to preserve the original CRC if (originalPosition == 0 && _highWaterMark == -1) { _highWaterMark = 0; CrcCalculator.ClearCrc(); } if (originalPosition == _highWaterMark) { CrcCalculator.Accumulate(buffer, offset, readCount); _highWaterMark = _underlyingStream.Position; } if (_validateCrcWithExpectedCrc && CanValidateCrcWithoutRead()) { if (CrcCalculator.Crc != _expectedCrc) { throw new FileFormatException(SR.Get(SRID.CorruptedData)); } } } return(readCount); }
override public void Write(byte[] buffer, int offset, int count) { CheckDisposed(); // We should calculate CRC accumulatively for the following conditions // 1. Seek is not supported by the underlying stream: this will be the case for // writing stream in streaming mode // 2. This write request is consequtive to the highwater mark of the CRC calculation // 3. This write request is at 0 offset and the CRC hasn't been calculated yet if (!_underlyingStream.CanSeek) // Case #1 { _underlyingStream.Write(buffer, offset, count); CrcCalculator.Accumulate(buffer, offset, count); } else { long originalPosition = _underlyingStream.Position; // If we ever fail to Write below _highWaterMark, we want CRC to be recalculated in case // if a caller decides to recover from the error if (originalPosition < _highWaterMark) { _highWaterMark = -1; } _underlyingStream.Write(buffer, offset, count); if (originalPosition == 0) { _highWaterMark = 0; CrcCalculator.ClearCrc(); } if (originalPosition == _highWaterMark) { CrcCalculator.Accumulate(buffer, offset, count); _highWaterMark = _underlyingStream.Position; } } // CRC cannot be checked against the to-be-validated CRC anymore _validateCrcWithExpectedCrc = false; // mark the global dirty flag _blockManager.DirtyFlag = true; }
public void ProcessPhotonData(byte[] payload) { if (payload.Length < PhotonHeaderLength) { return; } TickCount = Environment.TickCount; //TickCount = 146722234; int offset = 0; NumberDeserializer.Deserialize(out short peerId, payload, ref offset); ReadByte(out byte flags, payload, ref offset); ReadByte(out byte commandCount, payload, ref offset); NumberDeserializer.Deserialize(out int timestamp, payload, ref offset); NumberDeserializer.Deserialize(out int challenge, payload, ref offset); bool isEncrypted = flags == 1; bool isCrcEnabled = flags == 0xCC; if (isEncrypted) { // Encrypted packages are not supported return; } if (isCrcEnabled) { int ignoredOffset = 0; NumberDeserializer.Deserialize(out int crc, payload, ref ignoredOffset); NumberSerializer.Serialize(0, payload, ref offset); if (crc != CrcCalculator.Calculate(payload, payload.Length)) { // Invalid crc return; } } for (int commandIdx = 0; commandIdx < commandCount; commandIdx++) { HandleCommand(payload, ref offset); } }
public byte[] Write(TelloCommand command, ushort seqId) { int size = command.Data.Length + HeaderSize; MemoryStream outStream = new MemoryStream(); EndianBinaryWriter writer = EndianBinaryWriter.FromStream(outStream, true); writer.Write(0xcc); writer.Write((ushort)size << 3); writer.Write(CrcCalculator.Crc8(outStream.ToArray())); writer.Write(command.Type); writer.Write((ushort)command.Id); writer.Write(seqId); if (command.Data != null) { writer.Write(command.Data); } writer.Write(CrcCalculator.Crc16(outStream.ToArray())); return(outStream.ToArray()); }
public byte[] Build(DNP3CommandParameters param) { byte[] header = new byte[10]; Buffer.BlockCopy(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)param.Start)), 0, header, 0, 2); header[2] = param.Length; header[3] = param.Control; Buffer.BlockCopy(BitConverter.GetBytes((short)param.Destination), 0, header, 4, 2); Buffer.BlockCopy(BitConverter.GetBytes((short)param.Source), 0, header, 6, 2); ushort crc = 0; for (int i = 0; i < 8; i++) { CrcCalculator.computeCRC(header[i], ref crc); } crc = (ushort)(~crc); Buffer.BlockCopy(BitConverter.GetBytes((short)crc), 0, header, 8, 2); return(header); }
public override Dictionary <Tuple <RegisterType, int>, BasePoint> PareseResponse(byte[] response) { if (!CrcCalculator.CheckCRC(response)) { return(null); } byte[] dataObjects = MessagesHelper.GetResponseDataObjects(response); var index = (ushort)BitConverter.ToUInt16(dataObjects, 5); var value = BitConverter.ToUInt16(dataObjects, 7); Dictionary <Tuple <RegisterType, int>, BasePoint> retVal = new Dictionary <Tuple <RegisterType, int>, BasePoint>(); AnalogPoint point = new AnalogPoint(); point.Index = index; point.Value = value; point.RegisterType = RegisterType.ANALOG_OUTPUT; retVal.Add(new Tuple <RegisterType, int>(RegisterType.ANALOG_OUTPUT, point.Index), point); return(retVal); }
public override Dictionary <Tuple <RegisterType, int>, BasePoint> PareseResponse(byte[] response) { if (!CrcCalculator.CheckCRC(response)) { return(null); } byte[] dataObjects = MessagesHelper.GetResponseDataObjects(response); Dictionary <Tuple <RegisterType, int>, BasePoint> retVal = new Dictionary <Tuple <RegisterType, int>, BasePoint>(); DiscretePoint point = new DiscretePoint(); point.Index = (ushort)BitConverter.ToUInt16(dataObjects, 5); byte controlCode = dataObjects[7]; point.Value = controlCode == 0x81 ? 0 : 1; retVal.Add(new Tuple <RegisterType, int>(RegisterType.BINARY_OUTPUT, point.Index), point); return(retVal); }
public override byte[] PackRequest() { byte[] message = new byte[15]; DNP3ConfirmCommandParamters commandParam = (DNP3ConfirmCommandParamters)CommandParameters; CommandParameters.Length = 0x08; Buffer.BlockCopy(headerBuilder.Build(CommandParameters), 0, message, 0, 10); message[10] = commandParam.TransportControl; message[11] = commandParam.AplicationControl; message[12] = commandParam.FunctionCode; ushort crc = 0; for (int i = 10; i < 13; i++) { CrcCalculator.computeCRC(message[i], ref crc); } crc = (ushort)(~crc); Buffer.BlockCopy(BitConverter.GetBytes(crc), 0, message, 13, 2); return(message); }
///////////////////////////// // Internal Methods ///////////////////////////// // !!!!!!!!!!!!!!!!IMPORTANT !!!!!!!!!!!!!!!! // This method doesn't preserve the seek position of the underlying stream. // It (non-preservation of the seek pointer) is mostly done for compression // scenarios in which seeking back in the compressed streams will result in // switching to the expensive simulation stream. This method is only called // from scenarios during Flush Close of the package where position of the // Compressed stream is insignificant internal UInt32 CalculateCrc() { CheckDisposed(); if (_underlyingStream.CanSeek) { long originalPosition = _underlyingStream.Position; if (_highWaterMark == -1) { CrcCalculator.ClearCrc(); _highWaterMark = 0; } if (_highWaterMark < _underlyingStream.Length) { _underlyingStream.Position = _highWaterMark; CrcCalculator.CalculateStreamCrc(_underlyingStream); _highWaterMark = _underlyingStream.Length; } } return(CrcCalculator.Crc); }
public override Dictionary <Tuple <RegisterType, int>, BasePoint> PareseResponse(byte[] response) { if (!CrcCalculator.CheckCRC(response)) { return(null); } byte[] dataObjects = MessagesHelper.GetResponseDataObjects(response); Dictionary <Tuple <RegisterType, int>, BasePoint> retVal = new Dictionary <Tuple <RegisterType, int>, BasePoint>(); try { int typeFieldPosition = 0; int startIndexPosition = 3; int stopIndexPosition = 4; int len = dataObjects.Length; int lastRange = 0; int pointTypes = 0; while (len > 0) { ushort typeField = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(dataObjects, typeFieldPosition)); byte startIndex = dataObjects[startIndexPosition]; byte stopIndex = dataObjects[stopIndexPosition]; int numberOfItems = stopIndex - startIndex + 1; int range = 5; //type(2) + qual + satrt + stop index switch (typeField) { case (ushort)TypeField.BINARY_INPUT_PACKED_FORMAT: case (ushort)TypeField.BINARY_OUTPUT_PACKED_FORMAT: { pointTypes += 1; range += numberOfItems % 8 == 0 ? numberOfItems / 8 : numberOfItems / 8 + 1; byte[] binaryObject = new byte[range]; Buffer.BlockCopy(dataObjects, lastRange, binaryObject, 0, range); ParseBinaryObject(binaryObject, typeField, ref retVal); lastRange += range; break; } case (ushort)TypeField.ANALOG_OUTPUT_STATUS_16BIT: { pointTypes += 1; range += numberOfItems * 3; byte[] analogObject = new byte[range]; Buffer.BlockCopy(dataObjects, lastRange, analogObject, 0, range); ParseAnalogOutputObject(analogObject, typeField, ref retVal); lastRange += range; break; } case (ushort)TypeField.ANALOG_INPUT_16BIT: { pointTypes += 1; range += numberOfItems * 2; byte[] analogObject = new byte[range]; Buffer.BlockCopy(dataObjects, lastRange, analogObject, 0, range); ParseAnalogInputObject(analogObject, typeField, ref retVal); lastRange += range; break; } } len -= range; typeFieldPosition += range; startIndexPosition += range; stopIndexPosition += range; if (pointTypes == 4) { break; } } } catch { } return(retVal); }
public SimpleCrcInputStream(IAsyncSimpleInputStream <byte> @base, CrcCalculator <T> crcCalculator, bool leaveBaseUndisposed = false) : base(@base, leaveBaseUndisposed) { CrcCalculator = crcCalculator; }
public CrcCalculator <T> CreateCalculator() { return(CrcCalculator.Create <T>(this)); }
void Start() { crcCalculator = new CrcCalculator(CrcPolynomialDivisor); UdpNetworkManager.Instance.OnReceiveData += ReceiveData; PacketReliabilityManager.Instance.SetUpReliabilitySystem(); }