//****************************************************************************************************** // 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 { /// /// Contains 7 bit encoding functions /// public static class Encoding7Bit { /// /// Gets the number of bytes required to write the provided value. /// /// the value to measure /// public static int GetSizeInt15(short value1) { if (value1 < 0) throw new ArgumentOutOfRangeException("value1", "Cannot be negative"); if (value1 < 128) return 1; return 2; } /// /// Gets the number of bytes for the supplied value in the stream. /// /// /// public unsafe static int MeasureInt15(byte* stream) { if (stream[0] < 128) return 1; return 2; } /// /// Gets the number of bytes for the supplied value in the stream. /// /// /// /// public unsafe static int Measure15(byte* stream, int position) { return MeasureInt15(stream + position); } /// /// Gets the number of bytes for the supplied value in the stream. /// /// /// /// public static int Measure15(byte[] stream, int position) { if (stream[position] < 128) return 1; return 2; } #region [ Write ] /// /// Writes the 7-bit encoded value to the provided stream. /// /// the stream /// the value to write. Cannot be negative /// The number of bytes required to store the value [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; } /// /// Writes the 7-bit encoded value to the provided stream. /// /// the stream /// a reference parameter to the starting position. /// This field will be updated. when the function returns /// the value to write 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; } /// /// Writes the 7-bit encoded value to the provided stream. /// /// the stream /// a reference parameter to the starting position. /// This field will be updated. when the function returns /// the value to write 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; } /// /// Writes the 7-bit encoded value to the provided stream. /// /// a delegate to a write byte method /// the value to write public static void WriteInt15(Action 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 ] /// /// Reads a 7-bit encoded uint. /// /// the stream /// the position in the stream. Position will be updated after reading /// The value 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); } /// /// Reads a 7-bit encoded uint. /// /// the stream /// the position in the stream. Position will be updated after reading /// The value 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); } /// /// Reads a 7-bit encoded uint. /// /// A stream to read from. /// the value /// /// This method will check for the end of the stream /// /// Occurs if the end of the stream was reached. 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); } /// /// Reads a 7-bit encoded uint. /// /// A delegate where to read the next byte /// the value public static short ReadInt15(Func stream) { short value11; value11 = stream(); if (value11 < 128) { return value11; } value11 ^= (short)(stream() << 7); return (short)(value11 ^ 0x80); } #endregion #region [ 32 bit ] /// /// Gets the number of bytes required to write the provided value. /// /// the value to measure /// 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; } /// /// Gets the number of bytes for the supplied value in the stream. /// /// /// 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; } /// /// Gets the number of bytes for the supplied value in the stream. /// /// /// /// public unsafe static int MeasureUInt32(byte* stream, int position) { return MeasureUInt32(stream + position); } /// /// Gets the number of bytes for the supplied value in the stream. /// /// /// /// 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 ] /// /// Writes the 7-bit encoded value to the provided stream. /// /// the stream /// the value to write /// The number of bytes required to store the value [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; } /// /// Writes the 7-bit encoded value to the provided stream. /// /// the stream /// a reference parameter to the starting position. /// This field will be updated. when the function returns /// the value to write 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; } /// /// Writes the 7-bit encoded value to the provided stream. /// /// the stream /// a reference parameter to the starting position. /// This field will be updated. when the function returns /// the value to write 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; } /// /// Writes the 7-bit encoded value to the provided stream. /// /// a delegate to a write byte method /// the value to write public static void Write(Action 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 ] /// /// Reads a 7-bit encoded uint. /// /// the stream /// the position in the stream. Position will be updated after reading /// The value public unsafe static uint ReadUInt32(byte* stream, ref int position) { stream += position; uint value11; value11 = stream[0]; if (value11 < 128) { position += 1; return value11; } value11 ^= ((uint)stream[1] << 7); if (value11 < 128 * 128) { position += 2; return value11 ^ 0x80; } value11 ^= ((uint)stream[2] << 14); if (value11 < 128 * 128 * 128) { position += 3; return value11 ^ 0x4080; } value11 ^= ((uint)stream[3] << 21); if (value11 < 128 * 128 * 128 * 128) { position += 4; return value11 ^ 0x204080; } value11 ^= ((uint)stream[4] << 28) ^ 0x10204080; position += 5; return value11; } /// /// Reads a 7-bit encoded uint. /// /// the stream /// the position in the stream. Position will be updated after reading /// The value public static uint ReadUInt32(byte[] stream, ref int position) { int pos = position; uint value11; value11 = stream[pos]; if (value11 < 128) { position = pos + 1; return value11; } value11 ^= ((uint)stream[pos + 1] << 7); if (value11 < 128 * 128) { position = pos + 2; return value11 ^ 0x80; } value11 ^= ((uint)stream[pos + 2] << 14); if (value11 < 128 * 128 * 128) { position = pos + 3; return value11 ^ 0x4080; } value11 ^= ((uint)stream[pos + 3] << 21); if (value11 < 128 * 128 * 128 * 128) { position = pos + 4; return value11 ^ 0x204080; } value11 ^= ((uint)stream[pos + 4] << 28) ^ 0x10204080; position = pos + 5; return value11; } /// /// Reads a 7-bit encoded uint. /// /// A stream to read from. /// the value /// /// This method will check for the end of the stream /// /// Occurs if the end of the stream was reached. public static uint ReadUInt32(Stream stream) { uint value11; value11 = stream.ReadNextByte(); if (value11 < 128) { return value11; } value11 ^= ((uint)stream.ReadNextByte() << 7); if (value11 < 128 * 128) { return value11 ^ 0x80; } value11 ^= ((uint)stream.ReadNextByte() << 14); if (value11 < 128 * 128 * 128) { return value11 ^ 0x4080; } value11 ^= ((uint)stream.ReadNextByte() << 21); if (value11 < 128 * 128 * 128 * 128) { return value11 ^ 0x204080; } value11 ^= ((uint)stream.ReadNextByte() << 28) ^ 0x10204080; return value11; } /// /// Reads a 7-bit encoded uint. /// /// A delegate where to read the next byte /// the value public static uint ReadUInt32(Func stream) { uint value11; value11 = stream(); if (value11 < 128) { return value11; } value11 ^= ((uint)stream() << 7); if (value11 < 128 * 128) { return value11 ^ 0x80; } value11 ^= ((uint)stream() << 14); if (value11 < 128 * 128 * 128) { return value11 ^ 0x4080; } value11 ^= ((uint)stream() << 21); if (value11 < 128 * 128 * 128 * 128) { return value11 ^ 0x204080; } value11 ^= ((uint)stream() << 28) ^ 0x10204080; return value11; } #endregion #endregion #region [ 64 bit ] /// /// Gets the number of bytes required to write the provided value. /// /// the value to measure /// The number of bytes needed to store the provided value. public static int GetSize(ulong 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; if (value1 < 128L * 128 * 128 * 128 * 128) return 5; if (value1 < 128L * 128 * 128 * 128 * 128 * 128) return 6; if (value1 < 128L * 128 * 128 * 128 * 128 * 128 * 128) return 7; if (value1 < 128L * 128 * 128 * 128 * 128 * 128 * 128 * 128) return 8; return 9; } /// /// Gets the number of bytes for the supplied value in the stream. /// /// /// public unsafe static int MeasureUInt64(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; if (stream[4] < 128) return 5; if (stream[5] < 128) return 6; if (stream[6] < 128) return 7; if (stream[7] < 128) return 8; return 9; } /// /// Gets the number of bytes for the supplied value in the stream. /// /// /// /// public unsafe static int MeasureUInt64(byte* stream, int position) { return MeasureUInt64(stream + position); } /// /// Gets the number of bytes for the supplied value in the stream. /// /// /// /// public static int MeasureUInt64(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; if (stream[position + 4] < 128) return 5; if (stream[position + 5] < 128) return 6; if (stream[position + 6] < 128) return 7; if (stream[position + 7] < 128) return 8; return 9; } #region [ Write ] /// /// Writes the 7-bit encoded value to the provided stream. /// /// the stream /// the value to write /// The number of bytes required to store the value [MethodImpl(MethodImplOptions.AggressiveInlining)] unsafe public static int Write(byte* stream, ulong 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 >> (7 + 7)); return 3; } stream[2] = (byte)((value1 >> (7 + 7)) | 128); if (value1 < 128 * 128 * 128 * 128) { stream[3] = (byte)(value1 >> (7 + 7 + 7)); return 4; } stream[3] = (byte)((value1 >> (7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128) { stream[4] = (byte)(value1 >> (7 + 7 + 7 + 7)); return 5; } stream[4] = (byte)((value1 >> (7 + 7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128 * 128) { stream[5] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7)); return 6; } stream[5] = (byte)((value1 >> (7 + 7 + 7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128 * 128 * 128) { stream[6] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7)); return 7; } stream[6] = (byte)((value1 >> (7 + 7 + 7 + 7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128 * 128 * 128 * 128) { stream[7] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7)); return 8; } stream[7] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7) | 128); stream[8] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7)); return 9; } /// /// Writes the 7-bit encoded value to the provided stream. /// /// the stream /// a reference parameter to the starting position. /// This field will be updated. when the function returns /// the value to write unsafe public static void Write(byte* stream, ref int position, ulong 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 >> (7 + 7)); position += 3; return; } stream[position + 2] = (byte)((value1 >> (7 + 7)) | 128); if (value1 < 128 * 128 * 128 * 128) { stream[position + 3] = (byte)(value1 >> (7 + 7 + 7)); position += 4; return; } stream[position + 3] = (byte)((value1 >> (7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128) { stream[position + 4] = (byte)(value1 >> (7 + 7 + 7 + 7)); position += 5; return; } stream[position + 4] = (byte)((value1 >> (7 + 7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128 * 128) { stream[position + 5] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7)); position += 6; return; } stream[position + 5] = (byte)((value1 >> (7 + 7 + 7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128 * 128 * 128) { stream[position + 6] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7)); position += 7; return; } stream[position + 6] = (byte)((value1 >> (7 + 7 + 7 + 7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128 * 128 * 128 * 128) { stream[position + 7] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7)); position += 8; return; } stream[position + 7] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7) | 128); stream[position + 8] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7)); position += 9; return; } /// /// Writes the 7-bit encoded value to the provided stream. /// /// the stream /// a reference parameter to the starting position. /// This field will be updated. when the function returns /// the value to write public static void Write(byte[] stream, ref int position, ulong 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 >> (7 + 7)); position += 3; return; } stream[position + 2] = (byte)((value1 >> (7 + 7)) | 128); if (value1 < 128 * 128 * 128 * 128) { stream[position + 3] = (byte)(value1 >> (7 + 7 + 7)); position += 4; return; } stream[position + 3] = (byte)((value1 >> (7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128) { stream[position + 4] = (byte)(value1 >> (7 + 7 + 7 + 7)); position += 5; return; } stream[position + 4] = (byte)((value1 >> (7 + 7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128 * 128) { stream[position + 5] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7)); position += 6; return; } stream[position + 5] = (byte)((value1 >> (7 + 7 + 7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128 * 128 * 128) { stream[position + 6] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7)); position += 7; return; } stream[position + 6] = (byte)((value1 >> (7 + 7 + 7 + 7 + 7 + 7)) | 128); if (value1 < 128L * 128 * 128 * 128 * 128 * 128 * 128 * 128) { stream[position + 7] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7)); position += 8; return; } stream[position + 7] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7) | 128); stream[position + 8] = (byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7)); position += 9; return; } /// /// Writes the 7-bit encoded value to the provided stream. /// /// a delegate to a write byte method /// the value to write public static void Write(Action stream, ulong 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 >> (7 + 7))); return; } stream((byte)((value1 >> (7 + 7)) | 128)); if (value1 < 128 * 128 * 128 * 128) { stream((byte)(value1 >> (7 + 7 + 7))); return; } stream((byte)((value1 >> (7 + 7 + 7)) | 128)); if (value1 < 128L * 128 * 128 * 128 * 128) { stream((byte)(value1 >> (7 + 7 + 7 + 7))); return; } stream((byte)((value1 >> (7 + 7 + 7 + 7)) | 128)); if (value1 < 128L * 128 * 128 * 128 * 128 * 128) { stream((byte)(value1 >> (7 + 7 + 7 + 7 + 7))); return; } stream((byte)((value1 >> (7 + 7 + 7 + 7 + 7)) | 128)); if (value1 < 128L * 128 * 128 * 128 * 128 * 128 * 128) { stream((byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7))); return; } stream((byte)((value1 >> (7 + 7 + 7 + 7 + 7 + 7)) | 128)); if (value1 < 128L * 128 * 128 * 128 * 128 * 128 * 128 * 128) { stream((byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7))); return; } stream((byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7) | 128)); stream((byte)(value1 >> (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7))); return; } #endregion #region [ Read ] /// /// Reads a 7-bit encoded ulong. /// /// the stream /// the position in the stream. Position will be updated after reading /// The value public unsafe static ulong ReadUInt64(byte* stream, ref int position) { stream += position; ulong value11; value11 = stream[0]; if (value11 < 128) { position += 1; return value11; } value11 ^= ((ulong)stream[1] << (7)); if (value11 < 128 * 128) { position += 2; return value11 ^ 0x80; } value11 ^= ((ulong)stream[2] << (7 + 7)); if (value11 < 128 * 128 * 128) { position += 3; return value11 ^ 0x4080; } value11 ^= ((ulong)stream[3] << (7 + 7 + 7)); if (value11 < 128 * 128 * 128 * 128) { position += 4; return value11 ^ 0x204080; } value11 ^= ((ulong)stream[4] << (7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128) { position += 5; return value11 ^ 0x10204080L; } value11 ^= ((ulong)stream[5] << (7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128) { position += 6; return value11 ^ 0x810204080L; } value11 ^= ((ulong)stream[6] << (7 + 7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128 * 128) { position += 7; return value11 ^ 0x40810204080L; } value11 ^= ((ulong)stream[7] << (7 + 7 + 7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128 * 128 * 128) { position += 8; return value11 ^ 0x2040810204080L; } value11 ^= ((ulong)stream[8] << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7)); position += 9; return value11 ^ 0x102040810204080L; } /// /// Reads a 7-bit encoded ulong. /// /// the stream /// the position in the stream. Position will be updated after reading /// The value public static ulong ReadUInt64(byte[] stream, ref int position) { int pos = position; ulong value11; value11 = stream[pos]; if (value11 < 128) { position += 1; return value11; } value11 ^= ((ulong)stream[pos + 1] << (7)); if (value11 < 128 * 128) { position += 2; return value11 ^ 0x80; } value11 ^= ((ulong)stream[pos + 2] << (7 + 7)); if (value11 < 128 * 128 * 128) { position += 3; return value11 ^ 0x4080; } value11 ^= ((ulong)stream[pos + 3] << (7 + 7 + 7)); if (value11 < 128 * 128 * 128 * 128) { position += 4; return value11 ^ 0x204080; } value11 ^= ((ulong)stream[pos + 4] << (7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128) { position += 5; return value11 ^ 0x10204080L; } value11 ^= ((ulong)stream[pos + 5] << (7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128) { position += 6; return value11 ^ 0x810204080L; } value11 ^= ((ulong)stream[pos + 6] << (7 + 7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128 * 128) { position += 7; return value11 ^ 0x40810204080L; } value11 ^= ((ulong)stream[pos + 7] << (7 + 7 + 7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128 * 128 * 128) { position += 8; return value11 ^ 0x2040810204080L; } value11 ^= ((ulong)stream[pos + 8] << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7)); position += 9; return value11 ^ 0x102040810204080L; } /// /// Reads a 7-bit encoded ulong. /// /// A stream to read from. /// the value /// /// This method will check for the end of the stream /// /// Occurs if the end of the stream was reached. public static ulong ReadUInt64(Stream stream) { ulong value11; value11 = stream.ReadNextByte(); if (value11 < 128) { return value11; } value11 ^= ((ulong)stream.ReadNextByte() << (7)); if (value11 < 128 * 128) { return value11 ^ 0x80; } value11 ^= ((ulong)stream.ReadNextByte() << (7 + 7)); if (value11 < 128 * 128 * 128) { return value11 ^ 0x4080; } value11 ^= ((ulong)stream.ReadNextByte() << (7 + 7 + 7)); if (value11 < 128 * 128 * 128 * 128) { return value11 ^ 0x204080; } value11 ^= ((ulong)stream.ReadNextByte() << (7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128) { return value11 ^ 0x10204080L; } value11 ^= ((ulong)stream.ReadNextByte() << (7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128) { return value11 ^ 0x810204080L; } value11 ^= ((ulong)stream.ReadNextByte() << (7 + 7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128 * 128) { return value11 ^ 0x40810204080L; } value11 ^= ((ulong)stream.ReadNextByte() << (7 + 7 + 7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128 * 128 * 128) { return value11 ^ 0x2040810204080L; } value11 ^= ((ulong)stream.ReadNextByte() << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7)); return value11 ^ 0x102040810204080L; } /// /// Reads a 7-bit encoded uint. /// /// A delegate where to read the next byte /// the value public static ulong ReadUInt64(Func stream) { ulong value11; value11 = stream(); if (value11 < 128) { return value11; } value11 ^= ((ulong)stream() << (7)); if (value11 < 128 * 128) { return value11 ^ 0x80; } value11 ^= ((ulong)stream() << (7 + 7)); if (value11 < 128 * 128 * 128) { return value11 ^ 0x4080; } value11 ^= ((ulong)stream() << (7 + 7 + 7)); if (value11 < 128 * 128 * 128 * 128) { return value11 ^ 0x204080; } value11 ^= ((ulong)stream() << (7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128) { return value11 ^ 0x10204080L; } value11 ^= ((ulong)stream() << (7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128) { return value11 ^ 0x810204080L; } value11 ^= ((ulong)stream() << (7 + 7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128 * 128) { return value11 ^ 0x40810204080L; } value11 ^= ((ulong)stream() << (7 + 7 + 7 + 7 + 7 + 7 + 7)); if (value11 < 128L * 128 * 128 * 128 * 128 * 128 * 128 * 128) { return value11 ^ 0x2040810204080L; } value11 ^= ((ulong)stream() << (7 + 7 + 7 + 7 + 7 + 7 + 7 + 7)); return value11 ^ 0x102040810204080L; } #endregion #endregion } }