private static void TestByteRead(uint delta) { int loop = 10000; byte[] buffer = new byte[65535]; int position = 0; for (int x = 0; x < 200; x++) { Encoding7Bit.Write(buffer, ref position, delta); Encoding7Bit.Write(buffer, ref position, delta); Encoding7Bit.Write(buffer, ref position, delta); Encoding7Bit.Write(buffer, ref position, delta); Encoding7Bit.Write(buffer, ref position, delta); } Stopwatch sw = new Stopwatch(); sw.Start(); for (int repeat = 0; repeat < loop; repeat++) { position = 0; for (int x = 0; x < 200; x++) { delta = Encoding7Bit.ReadUInt32(buffer, ref position); delta = Encoding7Bit.ReadUInt32(buffer, ref position); delta = Encoding7Bit.ReadUInt32(buffer, ref position); delta = Encoding7Bit.ReadUInt32(buffer, ref position); delta = Encoding7Bit.ReadUInt32(buffer, ref position); } } sw.Stop(); //MessageBox.Show(position.ToString() + "b " + (loop * 1000 / sw.Elapsed.TotalSeconds / 1000000).ToString()); }
private uint DecodeQuality(int code, TsscPointMetadata nextPoint) { uint quality = code == TsscCodeWords.Quality2 ? nextPoint.PrevQuality2 : Encoding7Bit.ReadUInt32(m_data, ref m_position); nextPoint.PrevQuality2 = nextPoint.PrevQuality1; nextPoint.PrevQuality1 = quality; return(quality); }
/// <summary> /// Reads the 7-bit encoded value from the stream. /// </summary> /// <param name="stream"></param> /// <returns></returns> public static uint Read7BitUInt32(this Stream stream) { return(Encoding7Bit.ReadUInt32(stream)); }
/// <summary> /// Reads from the underlying stream in little endian format. Advancing the position. /// </summary> /// <returns>The data read</returns> public virtual uint Read7BitUInt32() { return(Encoding7Bit.ReadUInt32(ReadUInt8)); }
private static unsafe void TestRandomGenerated() { Random rand = new Random(); int seed = rand.Next(); rand = new Random(seed); byte[] buffer = new byte[10]; byte[] data = new byte[4]; fixed(byte *lp = data) { for (int x = 0; x < 100000; x++) { uint value, result; int position = 1; rand.NextBytes(data); data[1] = 0; data[2] = 0; data[3] = 0; value = *(uint *)lp; Encoding7Bit.Write(buffer, ref position, value); if (position != 1 + Encoding7Bit.GetSize(value)) { throw new Exception(); } position = 1; result = Encoding7Bit.ReadUInt32(buffer, ref position); if (position != 1 + Encoding7Bit.GetSize(value)) { throw new Exception(); } if (result != value) { throw new Exception(); } position = 1; } for (int x = 0; x < 100000; x++) { uint value, result; int position = 1; rand.NextBytes(data); data[2] = 0; data[3] = 0; value = *(uint *)lp; Encoding7Bit.Write(buffer, ref position, value); if (position != 1 + Encoding7Bit.GetSize(value)) { throw new Exception(); } position = 1; result = Encoding7Bit.ReadUInt32(buffer, ref position); if (position != 1 + Encoding7Bit.GetSize(value)) { throw new Exception(); } if (result != value) { throw new Exception(); } position = 1; } for (int x = 0; x < 100000; x++) { uint value, result; int position = 1; rand.NextBytes(data); data[3] = 0; value = *(uint *)lp; Encoding7Bit.Write(buffer, ref position, value); if (position != 1 + Encoding7Bit.GetSize(value)) { throw new Exception(); } position = 1; result = Encoding7Bit.ReadUInt32(buffer, ref position); if (position != 1 + Encoding7Bit.GetSize(value)) { throw new Exception(); } if (result != value) { throw new Exception(); } position = 1; } for (int x = 0; x < 100000; x++) { uint value, result; int position = 1; rand.NextBytes(data); value = *(uint *)lp; Encoding7Bit.Write(buffer, ref position, value); if (position != 1 + Encoding7Bit.GetSize(value)) { throw new Exception(); } position = 1; result = Encoding7Bit.ReadUInt32(buffer, ref position); if (position != 1 + Encoding7Bit.GetSize(value)) { throw new Exception(); } if (result != value) { throw new Exception(); } position = 1; } } }
private static void TestMethod() { byte[] buffer = new byte[10]; uint value, result; int position = 1; value = 127; Encoding7Bit.Write(buffer, ref position, value); if (position != 2) { throw new Exception(); } position = 1; result = Encoding7Bit.ReadUInt32(buffer, ref position); if (position != 2) { throw new Exception(); } if (result != value) { throw new Exception(); } position = 1; value = 127 * 127; Encoding7Bit.Write(buffer, ref position, value); if (position != 3) { throw new Exception(); } position = 1; result = Encoding7Bit.ReadUInt32(buffer, ref position); if (position != 3) { throw new Exception(); } if (result != value) { throw new Exception(); } position = 1; value = 127 * 127 * 127; Encoding7Bit.Write(buffer, ref position, value); if (position != 4) { throw new Exception(); } position = 1; result = Encoding7Bit.ReadUInt32(buffer, ref position); if (position != 4) { throw new Exception(); } if (result != value) { throw new Exception(); } position = 1; value = 127 * 127 * 127 * 127; Encoding7Bit.Write(buffer, ref position, value); if (position != 5) { throw new Exception(); } position = 1; result = Encoding7Bit.ReadUInt32(buffer, ref position); if (position != 5) { throw new Exception(); } if (result != value) { throw new Exception(); } position = 1; value = uint.MaxValue; Encoding7Bit.Write(buffer, ref position, value); if (position != 6) { throw new Exception(); } position = 1; result = Encoding7Bit.ReadUInt32(buffer, ref position); if (position != 6) { throw new Exception(); } if (result != value) { throw new Exception(); } }
public unsafe DecompressionExitCode GetMeasurement(out ushort id, out long timestamp, out uint quality, out float value, out byte userCommand) { id = 0; timestamp = 0; quality = 0; value = 0; userCommand = 0; TryAgain: int index = m_startIndex; if (IsEndOfStream()) { return(DecompressionExitCode.EndOfStreamOccured); } byte code; if (m_nextRunLength > 0) { m_nextRunLength--; code = m_lastPoint.ExpectedNextCode; } else { code = m_buffer[index++]; m_lastPoint.ExpectedNextCode = code; m_nextRunLength = code >> 6; } PointMetaData point; if ((code & 7) == 5) { point = new PointMetaData(); fixed(byte *signalID = &m_buffer[index]) point.SignalID = *(ushort *)signalID; index += 2; point.PointID = m_points.Count; m_lastPoint.ExpectedNextPointID = m_points.Count; m_points.Add(point); m_startIndex = index; goto TryAgain; } if ((code & 7) == 6) { throw new NotSupportedException(); } if ((code & 7) == 7) { userCommand = m_buffer[index++]; m_startIndex = index; return(DecompressionExitCode.CommandRead); } if ((code & 32) == 0) { point = m_points[m_lastPoint.ExpectedNextPointID]; } else { point = m_points[(int)Encoding7Bit.ReadUInt32(m_buffer, ref index)]; m_lastPoint.ExpectedNextPointID = point.PointID; } if ((code & 16) != 0) { point.LastQuality = (uint)(m_buffer[index++] | m_buffer[index++] << 8 | m_buffer[index++] << 16 | m_buffer[index++] << 24); } if ((code & 7) == 1) { point.LastValue ^= m_buffer[index++]; } else if ((code & 7) == 2) { point.LastValue ^= (uint)(m_buffer[index++] | m_buffer[index++] << 8); } else if ((code & 7) == 3) { point.LastValue ^= (uint)(m_buffer[index++] | m_buffer[index++] << 8 | m_buffer[index++] << 16); } else if ((code & 7) == 4) { point.LastValue ^= (uint)(m_buffer[index++] | m_buffer[index++] << 8 | m_buffer[index++] << 16 | m_buffer[index++] << 24); } if ((code & 8) == 0) { timestamp = m_timeBucket; } else { timestamp = m_timeBucket ^ (long)Encoding7Bit.ReadUInt64(m_buffer, ref index); m_timeBucket = timestamp; } id = point.SignalID; quality = point.LastQuality; uint lastValue = point.LastValue; value = *(float *)&lastValue; m_lastPoint = point; m_startIndex = index; return(DecompressionExitCode.MeasurementRead); }
public DecompressionExitCode GetMeasurement() { TryAgain: PointMetaData nextPoint = null; if (m_buffer.Position == m_length && m_bitStream.IsEmpty) { return(DecompressionExitCode.EndOfStreamOccured); } int code = m_lastPoint.ReadCode(m_bitStream); if (code >= MeasurementStreamCodes.NewPointId && code <= MeasurementStreamCodes.FlushBits) { if (code == MeasurementStreamCodes.NewPointId) { MeasurementTypeCode type = (MeasurementTypeCode)m_buffer.Data[m_buffer.Position++]; PointMetaData point; switch (type) { case MeasurementTypeCode.Int32: case MeasurementTypeCode.UInt32: case MeasurementTypeCode.Single: point = new PointMetaDataInt32(m_buffer, m_bitStream, type, m_points.Count); break; case MeasurementTypeCode.UInt64: case MeasurementTypeCode.Int64: case MeasurementTypeCode.Double: point = new PointMetaDataInt64(m_buffer, m_bitStream, type, m_points.Count); break; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); } NewMeasurementRegisteredTypeCode = type; NewMeasurementRegisteredId = point.ReferenceId; m_lastPoint.PrevNextPointId1 = point.ReferenceId; m_points.Add(point); NewMeasurementRegisteredMetadataLength = (int)Encoding7Bit.ReadUInt32(m_buffer.Data, ref m_buffer.Position); while (NewMeasurementRegisteredMetadata.Length < NewMeasurementRegisteredMetadataLength) { NewMeasurementRegisteredMetadata = new byte[NewMeasurementRegisteredMetadata.Length * 2]; } Array.Copy(m_buffer.Data, m_buffer.Position, NewMeasurementRegisteredMetadata, 0, NewMeasurementRegisteredMetadataLength); m_buffer.Position += NewMeasurementRegisteredMetadataLength; return(DecompressionExitCode.NewMeasurementRegistered); } else if (code == MeasurementStreamCodes.UserCommand) { UserCommand = (long)Encoding7Bit.ReadUInt64(m_buffer.Data, ref m_buffer.Position); return(DecompressionExitCode.UserData); } else if (code == MeasurementStreamCodes.UserCommandWithData) { UserCommand = (long)Encoding7Bit.ReadUInt64(m_buffer.Data, ref m_buffer.Position); UserCommandDataLength = (int)Encoding7Bit.ReadUInt32(m_buffer.Data, ref m_buffer.Position); while (UserCommandData.Length < UserCommandDataLength) { UserCommandData = new byte[UserCommandData.Length * 2]; } Array.Copy(m_buffer.Data, m_buffer.Position, UserCommandData, 0, UserCommandDataLength); m_buffer.Position += UserCommandDataLength; return(DecompressionExitCode.UserDataWithValue); } else if (code == MeasurementStreamCodes.FlushBits) { m_bitStream.Clear(); goto TryAgain; } else { throw new Exception("Programming Error."); } } if (code < MeasurementStreamCodes.PointIDXOR4) { throw new Exception("Expecting higher code"); } if (code <= MeasurementStreamCodes.PointIDXOR32) { if (code == MeasurementStreamCodes.PointIDXOR4) { m_lastPoint.PrevNextPointId1 ^= m_bitStream.ReadBits4(); } else if (code == MeasurementStreamCodes.PointIDXOR8) { m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++]; } else if (code == MeasurementStreamCodes.PointIDXOR12) { m_lastPoint.PrevNextPointId1 ^= m_bitStream.ReadBits4(); m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++] << 4; } else if (code == MeasurementStreamCodes.PointIDXOR16) { m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++]; m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++] << 8; } else if (code == MeasurementStreamCodes.PointIDXOR20) { m_lastPoint.PrevNextPointId1 ^= m_bitStream.ReadBits4(); m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++] << 4; m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++] << 12; } else if (code == MeasurementStreamCodes.PointIDXOR24) { m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++]; m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++] << 8; m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++] << 16; } else if (code == MeasurementStreamCodes.PointIDXOR32) { m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++]; m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++] << 8; m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++] << 16; m_lastPoint.PrevNextPointId1 ^= m_buffer.Data[m_buffer.Position++] << 24; } else { throw new Exception("Programming Error."); } code = m_lastPoint.ReadCode(m_bitStream); } if (code < MeasurementStreamCodes.TimeDelta1Forward) { throw new Exception("Expecting higher code"); } ID = m_lastPoint.PrevNextPointId1; nextPoint = m_points[m_lastPoint.PrevNextPointId1]; Quality = nextPoint.PrevQuality1; if (code <= MeasurementStreamCodes.TimeXOR7Bit) { if (code == MeasurementStreamCodes.TimeDelta1Forward) { Timestamp = m_prevTimestamp1 + m_prevTimeDelta1; } else if (code == MeasurementStreamCodes.TimeDelta2Forward) { Timestamp = m_prevTimestamp1 + m_prevTimeDelta2; } else if (code == MeasurementStreamCodes.TimeDelta3Forward) { Timestamp = m_prevTimestamp1 + m_prevTimeDelta3; } else if (code == MeasurementStreamCodes.TimeDelta4Forward) { Timestamp = m_prevTimestamp1 + m_prevTimeDelta4; } else if (code == MeasurementStreamCodes.TimeDelta1Reverse) { Timestamp = m_prevTimestamp1 - m_prevTimeDelta1; } else if (code == MeasurementStreamCodes.TimeDelta2Reverse) { Timestamp = m_prevTimestamp1 - m_prevTimeDelta2; } else if (code == MeasurementStreamCodes.TimeDelta3Reverse) { Timestamp = m_prevTimestamp1 - m_prevTimeDelta3; } else if (code == MeasurementStreamCodes.TimeDelta4Reverse) { Timestamp = m_prevTimestamp1 - m_prevTimeDelta4; } else if (code == MeasurementStreamCodes.Timestamp2) { Timestamp = m_prevTimestamp2; } else if (code == MeasurementStreamCodes.TimeXOR7Bit) { Timestamp = m_prevTimestamp1 ^ (long)Encoding7Bit.ReadUInt64(m_buffer.Data, ref m_buffer.Position); } else { throw new Exception("Programming Error."); } //Save the smallest delta time long minDelta = Math.Abs(m_prevTimestamp1 - Timestamp); if (minDelta < m_prevTimeDelta4 && minDelta != m_prevTimeDelta1 && minDelta != m_prevTimeDelta2 && minDelta != m_prevTimeDelta3) { if (minDelta < m_prevTimeDelta1) { m_prevTimeDelta4 = m_prevTimeDelta3; m_prevTimeDelta3 = m_prevTimeDelta2; m_prevTimeDelta2 = m_prevTimeDelta1; m_prevTimeDelta1 = minDelta; } else if (minDelta < m_prevTimeDelta2) { m_prevTimeDelta4 = m_prevTimeDelta3; m_prevTimeDelta3 = m_prevTimeDelta2; m_prevTimeDelta2 = minDelta; } else if (minDelta < m_prevTimeDelta3) { m_prevTimeDelta4 = m_prevTimeDelta3; m_prevTimeDelta3 = minDelta; } else { m_prevTimeDelta4 = minDelta; } } m_prevTimestamp2 = m_prevTimestamp1; m_prevTimestamp1 = Timestamp; code = m_lastPoint.ReadCode(m_bitStream); } else { Timestamp = m_prevTimestamp1; } if (code < MeasurementStreamCodes.Quality2) { throw new Exception("Expecting higher code"); } if (code <= MeasurementStreamCodes.Quality7Bit32) { if (code == MeasurementStreamCodes.Quality2) { Quality = nextPoint.PrevQuality2; } else if (code == MeasurementStreamCodes.Quality7Bit32) { Quality = Encoding7Bit.ReadUInt32(m_buffer.Data, ref m_buffer.Position); } nextPoint.PrevQuality2 = nextPoint.PrevQuality1; nextPoint.PrevQuality1 = Quality; code = m_lastPoint.ReadCode(m_bitStream); } else { Quality = nextPoint.PrevQuality1; } if (code < 32) { throw new Exception("Programming Error. Expecting a value quality code."); } nextPoint.ReadValue(code, Value); Value.Code = nextPoint.Code; m_lastPoint = nextPoint; return(DecompressionExitCode.MeasurementRead); }