private int getDataLengthFromMessageHeader(List <byte> header) { int data_length = -1; // we should have the full header, save the data length using (MemoryStream m = new MemoryStream(header.ToArray())) { using (BinaryReader reader = new BinaryReader(m)) { reader.ReadByte(); // skip start byte int code = reader.ReadInt32(); // skip message code data_length = reader.ReadInt32(); // finally read data length byte[] data_checksum = reader.ReadBytes(32); // skip checksum sha512qu/sha512sq, 32 bytes byte checksum = reader.ReadByte(); // header checksum byte byte endByte = reader.ReadByte(); // end byte if (endByte != 'I') { Logging.warn("Header end byte was not 'I'"); return(-1); } if (CoreProtocolMessage.getHeaderChecksum(header.Take(41).ToArray()) != checksum) { Logging.warn(String.Format("Header checksum mismatch")); return(-1); } if (data_length <= 0) { Logging.warn(String.Format("Data length was {0}, code {1}", data_length, code)); return(-1); } if (data_length > CoreConfig.maxMessageSize) { Logging.warn(String.Format("Received data length was bigger than max allowed message size - {0}, code {1}.", data_length, code)); return(-1); } } } return(data_length); }