//******************************************************************************************************// Encoding7Bit.cs - Gbtc//// Copyright © 2014, Grid Protection Alliance. All Rights Reserved.//// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See// the NOTICE file distributed with this work for additional information regarding copyright ownership.// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may// not use this file except in compliance with the License. You may obtain a copy of the License at://// http://www.opensource.org/licenses/MIT//// Unless agreed to in writing, the subject software distributed under the License is distributed on an// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the// License for the specific language governing permissions and limitations.//// Code Modification History:// ----------------------------------------------------------------------------------------------------// 03/16/2012 - Steven E. Chisholm// Generated original version of source code. ////****************************************************************************************************** using System;using System.IO;using System.Runtime.CompilerServices;using GSF.IO; namespace GSF{ /// <summary> /// Contains 7 bit encoding functions /// </summary> public static class Encoding7Bit { /// <summary> /// Gets the number of bytes required to write the provided value. /// </summary> /// <param name="value1">the value to measure</param> /// <returns></returns> public static int GetSizeInt15(short value1) { if (value1 < 0) throw new ArgumentOutOfRangeException("value1", "Cannot be negative"); if (value1 < 128) return 1; return 2; } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <returns></returns> public unsafe static int MeasureInt15(byte* stream) { if (stream[0] < 128) return 1; return 2; } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <param name="position"></param> /// <returns></returns> public unsafe static int Measure15(byte* stream, int position) { return MeasureInt15(stream + position); } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <param name="position"></param> /// <returns></returns> public static int Measure15(byte[] stream, int position) { if (stream[position] < 128) return 1; return 2; } #region [ Write ] /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="value1">the value to write. Cannot be negative</param> /// <returns>The number of bytes required to store the value</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe int WriteInt15(byte* stream, short value1) { if (value1 < 0) throw new ArgumentOutOfRangeException("value1", "Cannot be negative"); if (value1 < 128) { stream[0] = (byte)value1; return 1; } stream[0] = (byte)(value1 | 128); stream[1] = (byte)(value1 >> 7); return 2; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">a reference parameter to the starting position. /// This field will be updated. when the function returns</param> /// <param name="value1">the value to write</param> public static unsafe void WriteInt15(byte* stream, ref int position, short value1) { if (value1 < 0) throw new ArgumentOutOfRangeException("value1", "Cannot be negative"); if (value1 < 128) { stream[position] = (byte)value1; position += 1; return; } stream[position] = (byte)(value1 | 128); stream[position + 1] = (byte)(value1 >> 7); position += 2; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">a reference parameter to the starting position. /// This field will be updated. when the function returns</param> /// <param name="value1">the value to write</param> public static void WriteInt15(byte[] stream, ref int position, short value1) { if (value1 < 0) throw new ArgumentOutOfRangeException("value1", "Cannot be negative"); if (value1 < 128) { stream[position] = (byte)value1; position += 1; return; } stream[position] = (byte)(value1 | 128); stream[position + 1] = (byte)(value1 >> 7); position += 2; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">a delegate to a write byte method</param> /// <param name="value1">the value to write</param> public static void WriteInt15(Action<byte> stream, short value1) { if (value1 < 0) throw new ArgumentOutOfRangeException("value1", "Cannot be negative"); if (value1 < 128) { stream((byte)value1); return; } stream((byte)(value1 | 128)); stream((byte)(value1 >> 7)); } #endregion #region [ Read ] /// <summary> /// Reads a 7-bit encoded uint. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">the position in the stream. Position will be updated after reading</param> /// <returns>The value</returns> public unsafe static short ReadInt15(byte* stream, ref int position) { stream += position; short value11; value11 = stream[0]; if (value11 < 128) { position += 1; return value11; } value11 ^= (short)(stream[1] << 7); position += 2; return (short)(value11 ^ 0x80); } /// <summary> /// Reads a 7-bit encoded uint. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">the position in the stream. Position will be updated after reading</param> /// <returns>The value</returns> public static short ReadInt15(byte[] stream, ref int position) { short value11; value11 = stream[position]; if (value11 < 128) { position++; return value11; } value11 ^= (short)(stream[position + 1] << 7); position += 2; return (short)(value11 ^ 0x80); } /// <summary> /// Reads a 7-bit encoded uint. /// </summary> /// <param name="stream">A stream to read from.</param> /// <returns>the value</returns> /// <remarks> /// This method will check for the end of the stream /// </remarks> /// <exception cref="EndOfStreamException">Occurs if the end of the stream was reached.</exception> public static short ReadInt15(Stream stream) { short value11; value11 = stream.ReadNextByte(); if (value11 < 128) { return value11; } value11 ^= (short)(stream.ReadNextByte() << 7); return (short)(value11 ^ 0x80); } /// <summary> /// Reads a 7-bit encoded uint. /// </summary> /// <param name="stream">A delegate where to read the next byte</param> /// <returns>the value</returns> public static short ReadInt15(Func<byte> stream) { short value11; value11 = stream(); if (value11 < 128) { return value11; } value11 ^= (short)(stream() << 7); return (short)(value11 ^ 0x80); } #endregion #region [ 32 bit ] /// <summary> /// Gets the number of bytes required to write the provided value. /// </summary> /// <param name="value1">the value to measure</param> /// <returns></returns> public static int GetSize(uint value1) { if (value1 < 128) return 1; if (value1 < 128 * 128) return 2; if (value1 < 128 * 128 * 128) return 3; if (value1 < 128 * 128 * 128 * 128) return 4; return 5; } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <returns></returns> public unsafe static int MeasureUInt32(byte* stream) { if (stream[0] < 128) return 1; if (stream[1] < 128) return 2; if (stream[2] < 128) return 3; if (stream[3] < 128) return 4; return 5; } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <param name="position"></param> /// <returns></returns> public unsafe static int MeasureUInt32(byte* stream, int position) { return MeasureUInt32(stream + position); } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <param name="position"></param> /// <returns></returns> public static int MeasureUInt32(byte[] stream, int position) { if (stream[position + 0] < 128) return 1; if (stream[position + 1] < 128) return 2; if (stream[position + 2] < 128) return 3; if (stream[position + 3] < 128) return 4; return 5; } #region [ Write ] /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="value1">the value to write</param> /// <returns>The number of bytes required to store the value</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe int Write(byte* stream, uint value1) { if (value1 < 128) { stream[0] = (byte)value1; return 1; } stream[0] = (byte)(value1 | 128); if (value1 < 128 * 128) { stream[1] = (byte)(value1 >> 7); return 2; } stream[1] = (byte)((value1 >> 7) | 128); if (value1 < 128 * 128 * 128) { stream[2] = (byte)(value1 >> 14); return 3; } stream[2] = (byte)((value1 >> 14) | 128); if (value1 < 128 * 128 * 128 * 128) { stream[3] = (byte)(value1 >> 21); return 4; } stream[3] = (byte)((value1 >> 21) | 128); stream[4] = (byte)(value1 >> 28); return 5; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">a reference parameter to the starting position. /// This field will be updated. when the function returns</param> /// <param name="value1">the value to write</param> public static unsafe void Write(byte* stream, ref int position, uint value1) { if (value1 < 128) { stream[position] = (byte)value1; position += 1; return; } stream[position] = (byte)(value1 | 128); if (value1 < 128 * 128) { stream[position + 1] = (byte)(value1 >> 7); position += 2; return; } stream[position + 1] = (byte)((value1 >> 7) | 128); if (value1 < 128 * 128 * 128) { stream[position + 2] = (byte)(value1 >> 14); position += 3; return; } stream[position + 2] = (byte)((value1 >> 14) | 128); if (value1 < 128 * 128 * 128 * 128) { stream[position + 3] = (byte)(value1 >> 21); position += 4; return; } stream[position + 3] = (byte)((value1 >> 21) | 128); stream[position + 4] = (byte)(value1 >> 28); position += 5; return; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">a reference parameter to the starting position. /// This field will be updated. when the function returns</param> /// <param name="value1">the value to write</param> public static void Write(byte[] stream, ref int position, uint value1) { if (value1 < 128) { stream[position] = (byte)value1; position += 1; return; } stream[position] = (byte)(value1 | 128); if (value1 < 128 * 128) { stream[position + 1] = (byte)(value1 >> 7); position += 2; return; } stream[position + 1] = (byte)((value1 >> 7) | 128); if (value1 < 128 * 128 * 128) { stream[position + 2] = (byte)(value1 >> 14); position += 3; return; } stream[position + 2] = (byte)((value1 >> 14) | 128); if (value1 < 128 * 128 * 128 * 128) { stream[position + 3] = (byte)(value1 >> 21); position += 4; return; } stream[position + 3] = (byte)((value1 >> 21) | 128); stream[position + 4] = (byte)(value1 >> 28); position += 5; return; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">a delegate to a write byte method</param> /// <param name="value1">the value to write</param> public static void Write(Action<byte> stream, uint value1) { if (value1 < 128) { stream((byte)value1); return; } stream((byte)(value1 | 128)); if (value1 < 128 * 128) { stream((byte)(value1 >> 7)); return; } stream((byte)((value1 >> 7) | 128)); if (value1 < 128 * 128 * 128) { stream((byte)(value1 >> 14)); return; } stream((byte)((value1 >> 14) | 128)); if (value1 < 128 * 128 * 128 * 128) { stream((byte)(value1 >> 21)); return; } stream((byte)((value1 >> 21) | 128)); stream((byte)(value1 >> 28)); } #endregion #region [ Read ] /// <summary> /// Reads a 7-bit encoded uint. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">the position in the stream. Position will be updated after reading</param> /// <returns>The value</returns> public unsafe static uint ReadUInt32(byte* stream, ref int position) { stream += position; uint value11; value11 = stream[0]; if (value11 < 128) {Lines 1–500 of 1234. Load more
//****************************************************************************************************** // Encoding7Bit.cs - Gbtc // // Copyright © 2014, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 03/16/2012 - Steven E. Chisholm // Generated original version of source code. // //****************************************************************************************************** using System; using System.IO; using System.Runtime.CompilerServices; using GSF.IO; namespace GSF { /// <summary> /// Contains 7 bit encoding functions /// </summary> public static class Encoding7Bit { /// <summary> /// Gets the number of bytes required to write the provided value. /// </summary> /// <param name="value1">the value to measure</param> /// <returns></returns> public static int GetSizeInt15(short value1) { if (value1 < 0) throw new ArgumentOutOfRangeException("value1", "Cannot be negative"); if (value1 < 128) return 1; return 2; } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <returns></returns> public unsafe static int MeasureInt15(byte* stream) { if (stream[0] < 128) return 1; return 2; } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <param name="position"></param> /// <returns></returns> public unsafe static int Measure15(byte* stream, int position) { return MeasureInt15(stream + position); } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <param name="position"></param> /// <returns></returns> public static int Measure15(byte[] stream, int position) { if (stream[position] < 128) return 1; return 2; } #region [ Write ] /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="value1">the value to write. Cannot be negative</param> /// <returns>The number of bytes required to store the value</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe int WriteInt15(byte* stream, short value1) { if (value1 < 0) throw new ArgumentOutOfRangeException("value1", "Cannot be negative"); if (value1 < 128) { stream[0] = (byte)value1; return 1; } stream[0] = (byte)(value1 | 128); stream[1] = (byte)(value1 >> 7); return 2; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">a reference parameter to the starting position. /// This field will be updated. when the function returns</param> /// <param name="value1">the value to write</param> public static unsafe void WriteInt15(byte* stream, ref int position, short value1) { if (value1 < 0) throw new ArgumentOutOfRangeException("value1", "Cannot be negative"); if (value1 < 128) { stream[position] = (byte)value1; position += 1; return; } stream[position] = (byte)(value1 | 128); stream[position + 1] = (byte)(value1 >> 7); position += 2; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">a reference parameter to the starting position. /// This field will be updated. when the function returns</param> /// <param name="value1">the value to write</param> public static void WriteInt15(byte[] stream, ref int position, short value1) { if (value1 < 0) throw new ArgumentOutOfRangeException("value1", "Cannot be negative"); if (value1 < 128) { stream[position] = (byte)value1; position += 1; return; } stream[position] = (byte)(value1 | 128); stream[position + 1] = (byte)(value1 >> 7); position += 2; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">a delegate to a write byte method</param> /// <param name="value1">the value to write</param> public static void WriteInt15(Action<byte> stream, short value1) { if (value1 < 0) throw new ArgumentOutOfRangeException("value1", "Cannot be negative"); if (value1 < 128) { stream((byte)value1); return; } stream((byte)(value1 | 128)); stream((byte)(value1 >> 7)); } #endregion #region [ Read ] /// <summary> /// Reads a 7-bit encoded uint. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">the position in the stream. Position will be updated after reading</param> /// <returns>The value</returns> public unsafe static short ReadInt15(byte* stream, ref int position) { stream += position; short value11; value11 = stream[0]; if (value11 < 128) { position += 1; return value11; } value11 ^= (short)(stream[1] << 7); position += 2; return (short)(value11 ^ 0x80); } /// <summary> /// Reads a 7-bit encoded uint. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">the position in the stream. Position will be updated after reading</param> /// <returns>The value</returns> public static short ReadInt15(byte[] stream, ref int position) { short value11; value11 = stream[position]; if (value11 < 128) { position++; return value11; } value11 ^= (short)(stream[position + 1] << 7); position += 2; return (short)(value11 ^ 0x80); } /// <summary> /// Reads a 7-bit encoded uint. /// </summary> /// <param name="stream">A stream to read from.</param> /// <returns>the value</returns> /// <remarks> /// This method will check for the end of the stream /// </remarks> /// <exception cref="EndOfStreamException">Occurs if the end of the stream was reached.</exception> public static short ReadInt15(Stream stream) { short value11; value11 = stream.ReadNextByte(); if (value11 < 128) { return value11; } value11 ^= (short)(stream.ReadNextByte() << 7); return (short)(value11 ^ 0x80); } /// <summary> /// Reads a 7-bit encoded uint. /// </summary> /// <param name="stream">A delegate where to read the next byte</param> /// <returns>the value</returns> public static short ReadInt15(Func<byte> stream) { short value11; value11 = stream(); if (value11 < 128) { return value11; } value11 ^= (short)(stream() << 7); return (short)(value11 ^ 0x80); } #endregion #region [ 32 bit ] /// <summary> /// Gets the number of bytes required to write the provided value. /// </summary> /// <param name="value1">the value to measure</param> /// <returns></returns> public static int GetSize(uint value1) { if (value1 < 128) return 1; if (value1 < 128 * 128) return 2; if (value1 < 128 * 128 * 128) return 3; if (value1 < 128 * 128 * 128 * 128) return 4; return 5; } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <returns></returns> public unsafe static int MeasureUInt32(byte* stream) { if (stream[0] < 128) return 1; if (stream[1] < 128) return 2; if (stream[2] < 128) return 3; if (stream[3] < 128) return 4; return 5; } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <param name="position"></param> /// <returns></returns> public unsafe static int MeasureUInt32(byte* stream, int position) { return MeasureUInt32(stream + position); } /// <summary> /// Gets the number of bytes for the supplied value in the stream. /// </summary> /// <param name="stream"></param> /// <param name="position"></param> /// <returns></returns> public static int MeasureUInt32(byte[] stream, int position) { if (stream[position + 0] < 128) return 1; if (stream[position + 1] < 128) return 2; if (stream[position + 2] < 128) return 3; if (stream[position + 3] < 128) return 4; return 5; } #region [ Write ] /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="value1">the value to write</param> /// <returns>The number of bytes required to store the value</returns> [MethodImpl(MethodImplOptions.AggressiveInlining)] public static unsafe int Write(byte* stream, uint value1) { if (value1 < 128) { stream[0] = (byte)value1; return 1; } stream[0] = (byte)(value1 | 128); if (value1 < 128 * 128) { stream[1] = (byte)(value1 >> 7); return 2; } stream[1] = (byte)((value1 >> 7) | 128); if (value1 < 128 * 128 * 128) { stream[2] = (byte)(value1 >> 14); return 3; } stream[2] = (byte)((value1 >> 14) | 128); if (value1 < 128 * 128 * 128 * 128) { stream[3] = (byte)(value1 >> 21); return 4; } stream[3] = (byte)((value1 >> 21) | 128); stream[4] = (byte)(value1 >> 28); return 5; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">a reference parameter to the starting position. /// This field will be updated. when the function returns</param> /// <param name="value1">the value to write</param> public static unsafe void Write(byte* stream, ref int position, uint value1) { if (value1 < 128) { stream[position] = (byte)value1; position += 1; return; } stream[position] = (byte)(value1 | 128); if (value1 < 128 * 128) { stream[position + 1] = (byte)(value1 >> 7); position += 2; return; } stream[position + 1] = (byte)((value1 >> 7) | 128); if (value1 < 128 * 128 * 128) { stream[position + 2] = (byte)(value1 >> 14); position += 3; return; } stream[position + 2] = (byte)((value1 >> 14) | 128); if (value1 < 128 * 128 * 128 * 128) { stream[position + 3] = (byte)(value1 >> 21); position += 4; return; } stream[position + 3] = (byte)((value1 >> 21) | 128); stream[position + 4] = (byte)(value1 >> 28); position += 5; return; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">a reference parameter to the starting position. /// This field will be updated. when the function returns</param> /// <param name="value1">the value to write</param> public static void Write(byte[] stream, ref int position, uint value1) { if (value1 < 128) { stream[position] = (byte)value1; position += 1; return; } stream[position] = (byte)(value1 | 128); if (value1 < 128 * 128) { stream[position + 1] = (byte)(value1 >> 7); position += 2; return; } stream[position + 1] = (byte)((value1 >> 7) | 128); if (value1 < 128 * 128 * 128) { stream[position + 2] = (byte)(value1 >> 14); position += 3; return; } stream[position + 2] = (byte)((value1 >> 14) | 128); if (value1 < 128 * 128 * 128 * 128) { stream[position + 3] = (byte)(value1 >> 21); position += 4; return; } stream[position + 3] = (byte)((value1 >> 21) | 128); stream[position + 4] = (byte)(value1 >> 28); position += 5; return; } /// <summary> /// Writes the 7-bit encoded value to the provided stream. /// </summary> /// <param name="stream">a delegate to a write byte method</param> /// <param name="value1">the value to write</param> public static void Write(Action<byte> stream, uint value1) { if (value1 < 128) { stream((byte)value1); return; } stream((byte)(value1 | 128)); if (value1 < 128 * 128) { stream((byte)(value1 >> 7)); return; } stream((byte)((value1 >> 7) | 128)); if (value1 < 128 * 128 * 128) { stream((byte)(value1 >> 14)); return; } stream((byte)((value1 >> 14) | 128)); if (value1 < 128 * 128 * 128 * 128) { stream((byte)(value1 >> 21)); return; } stream((byte)((value1 >> 21) | 128)); stream((byte)(value1 >> 28)); } #endregion #region [ Read ] /// <summary> /// Reads a 7-bit encoded uint. /// </summary> /// <param name="stream">the stream</param> /// <param name="position">the position in the stream. Position will be updated after reading</param> /// <returns>The value</returns> public unsafe static uint ReadUInt32(byte* stream, ref int position) { stream += position; uint value11; value11 = stream[0]; if (value11 < 128) {