private long CalculatePartialDataCrc(long offset, int count) { long num; long num1; long num2; byte[] buffer = this.memoryStream.GetBuffer(); if (offset == (long)0) { int length = (int)buffer.Length; long?dataCrc = this.DataCrc; CrcUtils.SplitCalculateDataCrc(count - 1, length, buffer, dataCrc.Value, out num, out num1); return(num); } if ((long)count + offset >= this.Length) { int length1 = (int)buffer.Length; long?nullable = this.DataCrc; CrcUtils.SplitCalculateDataCrc((int)offset - 1, length1, buffer, nullable.Value, out num, out num1); return(num1); } int length2 = (int)buffer.Length; long?dataCrc1 = this.DataCrc; CrcUtils.SplitCalculateDataCrc((int)offset - 1, count + (int)offset - 1, length2, buffer, dataCrc1.Value, out num, out num1, out num2); return(num1); }
private void m_buttonCrcBig_Click(object sender, EventArgs e) { var strReq = this.m_textBoxCrcSourceReq.Text; var arrReq = ByteUtilsEx.hexPadStr2HexPadArr(strReq); var arrCrcRes = CrcUtils.UDPModbusRtuCRC(arrReq.ToArray(), false); var strCrcRes = String.Join("", arrCrcRes); this.m_textBoxCrcRes.Text = strCrcRes; }
private void VerifyCrc(byte[] buffer, int offset, int count, long?crc) { if (crc.HasValue) { long num = CrcUtils.ComputeCrc(buffer, offset, count); if (num != crc.Value) { throw new CrcMismatchException("Calculated CRC does not match Read CRC while streaming data to client", crc.Value, num, true); } IStringDataEventStream verbose = Logger <IRestProtocolHeadLogger> .Instance.Verbose; object[] objArray = new object[] { count, num }; verbose.Log("Data CRC while streaming to client. Length:{0} CRC:0x{1:x}", objArray); } }
public int TryParseNextIncomingPacket(byte[] outBuffer, out byte channelId) { ushort dataLength; byte crc, headerLength, headerCrc; if (_bufferLength == 0) { channelId = 0; return(0); } if (!ParseHeader(_buffer, 0, out crc, out headerCrc, out channelId, out dataLength, out headerLength)) { Debug.LogError("invalid message header!!!"); AdvanceToNextViablePacket(); return(0); } var dataLengthBytes = BitConverter.GetBytes(dataLength); var actualHeaderCrc = CrcUtils.Checksum(new byte[] { channelId, dataLengthBytes[0], dataLengthBytes[1] }, 0, 3, HEADER_CRC_SIZE); if (actualHeaderCrc != headerCrc) { Debug.LogError("header crc check failed"); AdvanceToNextViablePacket(); return(0); } if (_bufferLength == 0) { return(0); } if (_bufferLength < (headerLength + dataLength)) { return(0); //not enough data } // copy the data to outBuffer Array.Copy(_buffer, headerLength, outBuffer, 0, dataLength); // Trim the packet from the buffer _bufferLength -= (dataLength + headerLength); Array.Copy(_buffer, headerLength + dataLength, _buffer, 0, _bufferLength); if (!ValidateCrc(crc, outBuffer, 0, dataLength)) { Debug.LogError("data crc check failed"); AdvanceToNextViablePacket(); return(0); } return(dataLength); }
/// <summary> /// Header structure: /// ======================================== /// 1. Fixed packet prefix (2 bits) /// 2. Channel Id (2 bits) /// 3. Data CRC (4 bits) /// 4. Header CRC (4 bits) /// 5. Data Length (12 bits, trimmed ushort, max=4096) /// /// Illustrated header structure /// ======================================== /// BYTE 1 +-+[] FIXED PACKET PREFIX (2 bits) /// | [] /// | /// | [] CHANNEL ID (2 bits) /// | [] /// | /// | [] DATA CRC (4 bits) /// | [] /// | [] /// +-+[] /// /// BYTE 2 +-+[] HEADER CRC (4 bits) /// | [] /// | [] /// | [] /// | /// | [] DATA LENGTH (12 bits) /// | [] /// | [] /// +-+[] /// BYTE 3 +-+[] /// | [] /// | [] /// | [] /// | [] /// | [] /// | [] /// +-+[] /// /// </summary> /// <param name="outHeader">byte[] buffer to write the header into</param> /// <param name="channelId">Channel ID to embed into the header</param> /// <param name="data">Data of the packet, given for calculating the data CRC</param> /// <param name="startPosition">Start position of the data in the given Data buffer</param> /// <param name="length">Length of the data in the given data buffer</param> /// <returns>The length of the header in bytes</returns> private byte CreateHeader(byte[] outHeader, byte channelId, byte[] data, int startPosition, ushort length) { if (length > 4096) { throw new Exception("Maximum data length is 4096 bytes! Got " + length); } var random = new System.Random(); var lengthBytes = BitConverter.GetBytes(length); var crc = CrcUtils.Checksum(data, startPosition, length, DATA_CRC_SIZE); var headerCrc = CrcUtils.Checksum(new byte[] { channelId, lengthBytes[0], lengthBytes[1] }, 0, 3, HEADER_CRC_SIZE); outHeader[0] = crc; outHeader[0] |= (byte)(channelId << 4); outHeader[0] |= (byte)(FIXED_PACKET_PREFIX << 6); outHeader[1] = (byte)(length >> 8 & FOUR_BITS_MASK); outHeader[2] = (byte)(length & FULL_BYTE_MASK); outHeader[1] |= (byte)(headerCrc << 4); return(HEADER_LENGTH); }
private void UpdateDataCrc(byte[] buffer, int offset, int count, long?crc) { if (this.invalidCrc || count == 0) { return; } if (!crc.HasValue || this.Position != (long)0 && !this.DataCrc.HasValue) { this.invalidCrc = true; this.DataCrc = null; return; } if (this.Position == (long)0) { this.DataCrc = crc; return; } long?dataCrc = this.DataCrc; this.DataCrc = new long?(CrcUtils.ConcatenateCrc(dataCrc.Value, this.Position, crc.Value, (long)count)); }
private void SendDatagram(Packet packet) { lastAckSent = receivedTotal; packet.Stream.Position = PACKET_RELIABLE_STATE_OFFSET * 8; packet.Stream.WriteByte(reliableStateIn); packet.Stream.Position = PACKET_RELIABLE_STATE_OFFSET * 8; ushort crc = CrcUtils.Compute16(packet.Stream); packet.Stream.Position = 0; packet.Stream.WriteUInt32(packet.Seq); packet.Stream.WriteUInt32(packet.Ack); packet.Stream.WriteByte(packet.Flags); packet.Stream.WriteUInt16(crc); byte[] bytes = new byte[packet.Stream.Length]; packet.Stream.Position = 0; packet.Stream.Read(bytes, 0, bytes.Length); socket.Send(bytes); }
public void Auth() { var pb = new CMsgAuthTicket(); pb.gameid = App; pb.h_steam_pipe = 327684; using (var stream = Bitstream.CreateWith(PendingTicketForAuth.ToArray())) { pb.ticket_crc = CrcUtils.Compute32(stream); } pb.ticket = PendingTicketForAuth.ToArray(); var msg = new ClientMsgProtobuf <CMsgClientAuthList>(EMsg.ClientAuthList); msg.Body.tokens_left = Bot.TokenCount; msg.Body.app_ids.Add(App); msg.Body.tickets.Add(pb); msg.Body.message_sequence = AuthSequence++; Bot.Client.Send(msg); log.Debug("Sent auth list with crc " + msg.Body.tickets[0].ticket_crc + "/" + msg.Body.message_sequence); }
private void ProcessPacket(byte[] bytes, int length) { using (var stream = Bitstream.CreateWith(bytes, length)) { var seq = stream.ReadUInt32(); var ack = stream.ReadUInt32(); var flags = stream.ReadByte(); var checksum = stream.ReadUInt16(); var at = stream.Position; var computed = CrcUtils.Compute16(stream); stream.Position = at; if (checksum != computed) { return; } var reliableState = stream.ReadByte(); if (seq < sequenceIn) { // We no longer care. return; } for (byte i = 0; i < subchannels.Length; ++i) { var channel = subchannels[i]; var mask = 1 << i; if ((reliableStateOut & mask) == (reliableState & mask)) { if (channel.Blocked) { channel.Clear(); } } else { if (channel.Blocked && channel.SentIn < ack) { reliableStateOut = Flip(reliableStateOut, i); channel.Requeue(); } } } if ((flags & (uint)PacketFlags.IsReliable) != 0) { var bit = stream.ReadBits(3); reliableStateIn = Flip(reliableStateIn, bit); for (var i = 0; i < streams.Length; ++i) { var message = streams[i].Receive(stream); if (message.HasValue) { ProcessMessage(message.Value); } } } while (stream.HasByte()) { HandleMessage(stream); } if (!stream.Eof) { var remain = (byte)stream.Remain; var expect = (1 << remain) - 1; var expectedTru = stream.ReadBits(remain) == expect; // if false then probably something wrong } lastAckRecv = ack; sequenceIn = seq; } }
/// <summary> /// Import from csv file, in the format (Category,Key,Content) /// </summary> /// <param name="path">The path to the csv file to import from</param> /// <param name="locTable">The localisation table to import to</param> /// <param name="currentSchema">The current schema to import against</param> /// <param name="emptyValuesOnly"> If set to true, the import will only import values that are currently empty, /// and will not override existing values, defaults to false. /// </param> /// <param name="saveAssets">If set to true the LocalisationTable object will be saved to disk. /// Otherwise it will import and the changes will be in memory (and therefore require saving). defaults to true /// </param> /// <exception cref="ArgumentNullException"></exception> public static void ImportFromCsv( string path, LocalisationTable locTable, LocalisationKeySchema currentSchema, bool emptyValuesOnly = false, bool saveAssets = true) { if (locTable == null) { throw new ArgumentNullException(nameof(locTable)); } if (currentSchema == null) { throw new ArgumentNullException(nameof(currentSchema)); } if (path == null) { throw new ArgumentNullException(nameof(path)); } try { const char separator = ','; var newData = new Dictionary <int, string>(); Func <string, string> cleanInput = input => input.Replace("\t", string.Empty); var lines = CsvParser.Parse(File.ReadAllText(path)); foreach (var line in lines) { if (line.Count < 3) { continue; } var category = cleanInput(line[0]); var key = cleanInput(line[1]); var value = cleanInput(line[2]); var lookup = currentSchema.FindKey(category, key); if (!lookup.IsValid) { continue; } var keyCRC = CrcUtils.GetCrc(category, key); if (newData.ContainsKey(keyCRC)) { continue; } newData.Add(keyCRC, value); } locTable.SetData(newData, emptyValuesOnly); EditorUtility.SetDirty(locTable); AssetDatabase.SaveAssets(); Debug.Log($"Data populated from file: {path} {(emptyValuesOnly ? "(empty only)" : string.Empty)}"); } catch (Exception e) { Debug.LogError($"Could not load from CSV format: {e.Message}"); } }
private void ProcessPacket(byte[] bytes, int length) { using (var stream = Bitstream.CreateWith(bytes, length)) { uint seq = stream.ReadUInt32(); uint ack = stream.ReadUInt32(); byte flags = stream.ReadByte(); ushort checksum = stream.ReadUInt16(); long at = stream.Position; ushort computed = CrcUtils.Compute16(stream); stream.Position = at; if (checksum != computed) { log.WarnFormat( "failed checksum:" + "recv seq {0} ack {1} flags {2:x} checksum {3:x} computed {4:x}", seq, ack, flags, checksum, computed); return; } byte reliableState = stream.ReadByte(); if ((flags & 0x10) == 0x10) { log.WarnFormat( "choke {0}: recv seq {1} ack {2} flags {3:x}", stream.ReadByte(), seq, ack, flags); } if (seq < sequenceIn) { // We no longer care. log.WarnFormat("dropped: recv seq {0} ack {1}", seq, ack); return; } for (byte i = 0; i < subchannels.Length; ++i) { Subchannel channel = subchannels[i]; int mask = 1 << i; if ((reliableStateOut & mask) == (reliableState & mask)) { if (channel.Blocked) { Preconditions.CheckArgument(ack >= channel.SentIn); channel.Clear(); } } else { if (channel.Blocked && channel.SentIn < ack) { reliableStateOut = Flip(reliableStateOut, i); channel.Requeue(); } } } if ((flags & (uint)PacketFlags.IsReliable) != 0) { uint bit = stream.ReadBits(3); //Debug.WriteLine(" reliable, flip {0}. {1} => {2}", bit, reliableStateIn, Flip(reliableStateIn, bit)); reliableStateIn = Flip(reliableStateIn, bit); for (int i = 0; i < streams.Length; ++i) { Nullable <Stream.Message> message = streams[i].Receive(stream); if (message.HasValue) { ProcessMessage(message.Value); } } } while (stream.HasByte()) { HandleMessage(stream); } if (!stream.Eof) { byte remain = (byte)stream.Remain; int expect = (1 << remain) - 1; Preconditions.CheckArgument(stream.ReadBits(remain) == expect); } lastAckRecv = ack; sequenceIn = seq; } }
/// <summary> /// Exports table contents to a csv file in the format (Category,Key,Content) /// </summary> /// <param name="path">Path where the csv file will be written</param> /// <param name="locTable">The localisation table to export</param> /// <param name="currentSchema">The current schema</param> /// <param name="emptyValuesOnly">If set to true only the missing values will be exported</param> /// <exception cref="ArgumentNullException"></exception> public static void ExportTableToCsv( string path, LocalisationTable locTable, LocalisationKeySchema currentSchema, bool emptyValuesOnly = false) { if (path == null) { throw new ArgumentNullException(nameof(path)); } if (locTable == null) { throw new ArgumentNullException(nameof(locTable)); } if (currentSchema == null) { throw new ArgumentNullException(nameof(currentSchema)); } Func <string, string, string, string> cleanOutput = (csvKey1, csvKey2, csvValue) => $"{csvKey1},{csvKey2},\"{csvValue}\""; if (locTable.CRCEncodingVersion != CrcUtils.KEY_CRC_ENCODING_VERSION) { Debug.LogError( $"Table encoding version ({locTable.CRCEncodingVersion}) does not match current version (1) - please update the table before trying to export anything."); return; } try { if (File.Exists(path)) { File.Delete(path); } var streamWriter = new StreamWriter(File.OpenWrite(path)); foreach (var category in currentSchema.categories) { foreach (var key in category.keys) { var locKeyCRC = CrcUtils.GetCrc(category.name, key); var locValue = string.Empty; try { locValue = locTable.GetString(locKeyCRC); } catch { // ignored } if (string.IsNullOrEmpty(locValue) || !emptyValuesOnly) { streamWriter.WriteLine(cleanOutput(category.name, key, locValue)); } } } streamWriter.Close(); Debug.Log( $"Contents of table '{locTable.name}' written to: {path} {(emptyValuesOnly ? "(empty only)" : string.Empty)}"); } catch (Exception e) { Debug.LogError($"Could not save to CSV: {e.Message}"); } }
private static void ProcessPacket(byte[] bytes) { var length = bytes.Length; uint seq, ack; byte flags; using (var stream = BitStreamUtil.Create(bytes)) { seq = stream.ReadInt(32); ack = stream.ReadInt(32); flags = stream.ReadByte(); ushort checksum = (ushort)stream.ReadInt(16); stream.BeginChunk((length - 11) * 8); ushort computed = CrcUtils.Compute16(stream); stream.EndChunk(); if (checksum != computed) { //Console.WriteLine( // "failed checksum:" // + "recv seq {0} ack {1} flags {2:x} checksum {3:x} computed {4:x}", // seq, ack, flags, checksum, computed); return; } } var remaining = new byte[length - 11]; Array.Copy(bytes, 11, remaining, 0, length - 11); using (var stream = BitStreamUtil.Create(remaining)) { byte reliableState = stream.ReadByte(); if ((flags & 0x10) == 0x10) { //Console.WriteLine( // "choke {0}: recv seq {1} ack {2} flags {3:x}", // stream.ReadByte(), seq, ack, flags); return; } if (seq < sequenceIn) { // We no longer care. //Console.WriteLine("dropped: recv seq {0} ack {1}", seq, ack); return; } if ((flags & (uint)PacketFlags.IsReliable) != 0) { return; } //filter2++; //Console.WriteLine($"filter2: {filter2}"); try { stream.BeginChunk((remaining.Length - 1) * 8); DemoPacketParser.ParsePacket(stream, demoParser); stream.EndChunk(); demoParser.ForceTick(true); } catch { } lastAckRecv = ack; sequenceIn = seq; } }
/// <summary> /// Connect to the game server. Will use existing lobby on default. /// </summary> /// <param name="lobb"></param> public void Connect(CSODOTALobby lobb = null) { if (_connectDetails != null) { Disconnect(); } lobb = lobb ?? DotaGc.Lobby; if (lobb == null) { Log("No lobby so not connecting."); return; } _connectLobby = lobb; if (_appOwnershipTicket == null) { Log("Waiting for ownership ticket..."); _waitingForAuthTicket = true; FetchAppTicket(); return; } _authTicket = AuthTicket.CreateAuthTicket(_gameConnectTokens.Dequeue(), publicIP); var ver = new CMsgAuthTicket { gameid = (uint)DotaGc.GameID, h_steam_pipe = 327684, ticket = _authTicket }; using (var stream = Bitstream.CreateWith(_authTicket)) ver.ticket_crc = CrcUtils.Compute32(stream); _connectDetails = new DOTAConnectDetails { AuthTicket = _authTicket, ServerAuthTicket = AuthTicket.CreateServerTicket(DotaGc.SteamClient.SteamID, _authTicket, _appOwnershipTicket), ConnectInfo = lobb.connect, ConnectID = _connectAttempt++, AuthTicketCRC = ver.ticket_crc, Name = DotaGc.SteamClient.GetHandler <SteamFriends>().GetPersonaName(), PassKey = lobb.pass_key, SteamId = DotaGc.SteamClient.SteamID.ConvertToUInt64() }; var msg = new ClientMsgProtobuf <CMsgClientAuthList>(EMsg.ClientAuthList) { Body = { tokens_left = (uint)_gameConnectTokens.Count, app_ids = { (uint)DotaGc.GameID }, tickets = { ver }, message_sequence = 2 // Second in sequence. } }; DotaGc.SteamClient.Send(msg); Log("Sent crc ticket auth list, hash: " + ver.ticket_crc + "."); }
private bool ValidateCrc(byte crc, byte[] data, byte position, ushort length) { var dataCrc = CrcUtils.Checksum(data, position, length, DATA_CRC_SIZE); return(dataCrc == crc); }
private IEnumerator <IAsyncResult> ReadImpl(byte[] buffer, int offset, int count, AsyncIteratorContext <int> context) { bool flag; long num; this.CheckDisposed(); IAsyncResult asyncResult = this.innerStream.BeginRead(buffer, offset, count, context.GetResumeCallback(), context.GetResumeState("CrcReaderStream.ReadImpl")); yield return(asyncResult); int num1 = this.innerStream.EndRead(asyncResult); this.streamPosition += (long)num1; this.crcCalcuationTimer.Start(); if (num1 > 0) { long num2 = (long)0; if (this.maxCrcRangeSize != 0) { if (this.crcArray == null) { this.crcArray = new List <long>(16); } long num3 = this.currentCrcDataLength; int num4 = num1; while (num4 > 0) { int num5 = this.maxCrcRangeSize; if (this.crcArray.Count <= 1 && num3 < (long)(this.maxCrcRangeSize - this.pageOffset)) { num5 -= this.pageOffset; } int num6 = num5 - this.currentCrcRangeLength; if (num6 > num4) { num6 = num4; } long num7 = CrcUtils.ComputeCrc(buffer, offset, num6); num2 = (num3 <= (long)0 ? num7 : CrcUtils.ConcatenateCrc(num2, num3, num7, (long)num6)); if (this.currentCrcRangeLength <= 0) { this.crcArray.Add(num7); } else { long item = this.crcArray[this.crcArray.Count - 1]; item = CrcUtils.ConcatenateCrc(item, (long)this.currentCrcRangeLength, num7, (long)num6); this.crcArray[this.crcArray.Count - 1] = item; } num4 -= num6; offset += num6; num3 += (long)num6; this.currentCrcRangeLength += num6; if (this.currentCrcRangeLength != num5) { continue; } this.currentCrcRangeLength = 0; } } else { num2 = CrcUtils.ComputeCrc(buffer, offset, num1); } if (this.currentCrcDataLength <= (long)0) { this.currentCrc = num2; } else { this.currentCrc = CrcUtils.ConcatenateCrc(this.currentCrc, this.currentCrcDataLength, num2, (long)num1); } this.currentCrcDataLength += (long)num1; } this.crcCalcuationTimer.Stop(); if (this.streamPosition == this.streamLength) { string base64String = Convert.ToBase64String(BitConverter.GetBytes(this.currentCrc)); string str = this.Crc64ToVerifyAgainst.ToBase64String(); if (this.requestContext != null) { this.requestContext.RequestContentCrc64 = str; this.requestContext.ServerContentCrc64 = base64String; } IStringDataEventStream verbose = Logger <IRestProtocolHeadLogger> .Instance.Verbose; object[] objArray = new object[] { base64String, null, null }; object[] objArray1 = objArray; string str1 = str; if (str1 == null) { str1 = "NULL"; } objArray1[1] = str1; objArray[2] = this.currentCrcDataLength; verbose.Log("CalculatedCRC64 = {0} ReceivedCRC64 = {1} DataLength = {2}", objArray); if (this.Crc64ToVerifyAgainst.HasValue) { long?crc64ToVerifyAgainst = this.Crc64ToVerifyAgainst; long num8 = this.currentCrc; flag = (crc64ToVerifyAgainst.GetValueOrDefault() != num8 ? true : !crc64ToVerifyAgainst.HasValue); if (flag) { long?nullable = this.Crc64ToVerifyAgainst; num = (nullable.HasValue ? nullable.GetValueOrDefault() : (long)0); throw new CrcMismatchException("CrcReaderStream: Computed CRC64 does not match supplied CRC64", num, this.currentCrc, false); } } } this.calculatedCrc = this.currentCrc; context.ResultData = num1; }