Guest User

Untitled

a guest
May 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 65.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace BitIntegerTest
  6. {
  7.     public class Packet
  8.     {
  9.         private List<byte> buffer;
  10.         private int bitIndex = 0;
  11.         private int maxBitIndex = 0;
  12.         private int previousBitIndex = -1;
  13.  
  14.         /// <summary>
  15.         /// Expands the buffer size appending bytes so that the write functions don't overflow.
  16.         /// Records the furthest write in the maxBitIndex
  17.         /// </summary>
  18.         /// <param name="bits">The number of bits to allocate and record.</param>
  19.         private void ExpandBuffer(int bits)
  20.         {
  21.             if (bits < 1) throw new ArgumentOutOfRangeException("bits must be greater than 0");
  22.             while ((bitIndex + bits + 7) / 8 > buffer.Count) buffer.Add(new byte());
  23.             maxBitIndex = Math.Max(maxBitIndex, bitIndex + bits);
  24.         }
  25.  
  26.         /// <summary>
  27.         /// Constructor.
  28.         /// </summary>
  29.         public Packet()
  30.         {
  31.             buffer = new List<byte>();
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Fills the buffer with the requested bytes to work with.
  36.         /// </summary>
  37.         /// <param name="buffer">The bytes to load into the buffer</param>
  38.         public Packet(byte[] buffer)
  39.         {
  40.             this.buffer = new List<byte>(buffer);
  41.         }
  42.  
  43.         /// <summary>
  44.         /// Fills the buffer with the requested bytes to work with.
  45.         /// </summary>
  46.         /// <param name="buffer">The bytes to load into the buffer</param>
  47.         /// <param name="maxBitIndex">The maximum bit index</param>
  48.         public Packet(byte[] buffer, int maxBitIndex)
  49.         {
  50.             this.buffer = new List<byte>(buffer);
  51.             this.maxBitIndex = maxBitIndex;
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Gets or sets the bit index.
  56.         /// </summary>
  57.         public int BitIndex
  58.         {
  59.             get
  60.             {
  61.                 return bitIndex;
  62.             }
  63.             set
  64.             {
  65.                 if (value < 0 || (value + 7) / 8 > buffer.Count) throw new ArgumentOutOfRangeException("Unable to set the bit index outside of the buffer size.");
  66.                 bitIndex = value;
  67.             }
  68.         }
  69.  
  70.         /// <summary>
  71.         /// Sets the bit index to the start of the data section.
  72.         /// </summary>
  73.         public void ResetBitIndex()
  74.         {
  75.             if (maxBitIndex < 32) throw new InvalidOperationException("Unable to set the bit index over the max bit index.");
  76.             bitIndex = 32;
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Gets or Sets the max bit index.
  81.         /// </summary>
  82.         public int MaxBitIndex
  83.         {
  84.             set
  85.             {
  86.                 if (value < 0 || (value + 7) / 8 > buffer.Count) throw new ArgumentOutOfRangeException("Unable to set the max bit index outside of the buffer size.");
  87.                 maxBitIndex = value;
  88.             }
  89.         }
  90.  
  91.         public int BufferLength
  92.         {
  93.             get
  94.             {
  95.                 return buffer.Count;
  96.             }
  97.         }
  98.  
  99.         /// <summary>
  100.         /// Returns the buffer as an array of bytes.
  101.         /// </summary>
  102.         public byte[] Buffer
  103.         {
  104.             get
  105.             {
  106.                 return buffer.ToArray();
  107.             }
  108.         }
  109.  
  110.         public int HeaderSize
  111.         {
  112.             get
  113.             {
  114.                 return 4;
  115.             }
  116.         }
  117.  
  118.         /// <summary>
  119.         /// Returns a string of 0s and 1s representing the bits in the buffer. Good for debugging.
  120.         /// Places a space between nibbles and two spaces between bytes.
  121.         /// </summary>
  122.         /// <returns></returns>
  123.         public string Trace()
  124.         {
  125.             string s = string.Empty;
  126.             for (int copyBits = 0; copyBits < buffer.Count * 8; ++copyBits)
  127.             {
  128.                 s += ((buffer[copyBits / 8] >> (7 - copyBits % 8)) & 0x1) == 0 ? "0" : "1";
  129.                 if ((copyBits + 1) % 4 == 0 && copyBits != 0)
  130.                 {
  131.                     s += " ";
  132.                     if ((copyBits + 1) % 8 == 0)
  133.                     {
  134.                         s += " ";
  135.                     }
  136.                 }
  137.             }
  138.             return s;
  139.         }
  140.  
  141.         /// <summary>
  142.         /// Rounds the bitIndex up to a byte.
  143.         /// </summary>
  144.         public void RoundUpToByte()
  145.         {
  146.             bitIndex = (bitIndex + 7) / 8 * 8;
  147.         }
  148.  
  149.         /// <summary>
  150.         ///
  151.         /// </summary>
  152.         /// <returns>true if 32 bits exist in the buffer</returns>
  153.         public bool HasHeader()
  154.         {
  155.             return buffer.Count >= 4;
  156.         }
  157.  
  158.         /// <summary>
  159.         /// Reads the length in bits stored in the header.
  160.         /// </summary>
  161.         public void ReadHeader()
  162.         {
  163.             int oldBitIndex = bitIndex;
  164.             bitIndex = 0;
  165.             maxBitIndex = 32;
  166.             uint length;
  167.             if (ReadUInt32(out length))
  168.             {
  169.                 if (length > int.MaxValue)
  170.                 {
  171.                     maxBitIndex = int.MaxValue;
  172.                 }
  173.                 else
  174.                 {
  175.                     maxBitIndex = (int)length;
  176.                 }
  177.             }
  178.             bitIndex = oldBitIndex;
  179.         }
  180.  
  181.         /// <summary>
  182.         /// Writes a begin packet header of 32 bits.
  183.         /// </summary>
  184.         internal void BeginPacket()
  185.         {
  186.             WriteUInt32(0);
  187.         }
  188.  
  189.         /// <summary>
  190.         /// Writes maxBitIndex to the packet header created with BeginPacket.
  191.         /// </summary>
  192.         internal void EndPacket()
  193.         {
  194.             int oldBitIndex = bitIndex;
  195.             bitIndex = 0;
  196.             WriteUInt32((uint)maxBitIndex);
  197.             bitIndex = oldBitIndex;
  198.         }
  199.  
  200.         /// <summary>
  201.         /// Appends a byte directly to the end of the buffer.
  202.         /// </summary>
  203.         /// <param name="value"></param>
  204.         public void AppendByte(byte value)
  205.         {
  206.             buffer.Add(value);
  207.             bitIndex += 8;
  208.         }
  209.  
  210.         /// <summary>
  211.         /// Determines if the packet has read in all the data defined by the maxBitIndex.
  212.         /// </summary>
  213.         /// <returns></returns>
  214.         public bool HasData()
  215.         {
  216.             return bitIndex >= maxBitIndex;
  217.         }
  218.  
  219.         /// <summary>
  220.         /// Rollback as if the last read didn't occur.
  221.         /// </summary>
  222.         public void RollbackRead()
  223.         {
  224.             if (previousBitIndex == -1) throw new InvalidOperationException("A read method must be called successfully first");
  225.             bitIndex = previousBitIndex;
  226.             previousBitIndex = -1;
  227.         }
  228.  
  229.         public void ClearBits(int bits)
  230.         {
  231.             if (bitIndex % 8 + bits - 1 < 8)
  232.             {
  233.                 byte mask = 0xFF;
  234.                 for (int bitItr = bitIndex; bitItr < bitIndex + bits; ++bitItr)
  235.                 {
  236.                     mask ^= (byte)(1 << 7 - bitItr % 8);
  237.                 }
  238.                 buffer[bitIndex / 8] &= mask;
  239.             }
  240.             else if (bitIndex % 8 + bits - 1 < 16)
  241.             {
  242.                 int offset = 8 - bitIndex % 8;
  243.                 buffer[bitIndex / 8] >>= offset;
  244.                 buffer[bitIndex / 8] <<= offset;
  245.                 offset = bits - (8 - bitIndex % 8);
  246.                 buffer[bitIndex / 8 + 1] <<= offset;
  247.                 buffer[bitIndex / 8 + 1] >>= offset;
  248.             }
  249.             else
  250.             {
  251.                 int offset = 8 - bitIndex % 8;
  252.                 buffer[bitIndex / 8] >>= offset;
  253.                 buffer[bitIndex / 8] <<= offset;
  254.                 for (int bitItr = bitIndex + 8 - bitIndex % 8; bitItr < (bitIndex + bits - 1) / 8 * 8; bitItr += 8)
  255.                 {
  256.                     buffer[bitItr / 8] = 0;
  257.                 }
  258.                 offset = bitIndex + bits - (bitIndex + bits - 1) / 8 * 8;
  259.                 buffer[(bitIndex + bits - 1) / 8] <<= offset;
  260.                 buffer[(bitIndex + bits - 1) / 8] >>= offset;
  261.             }
  262.         }
  263.  
  264.         // Write Methods
  265.  
  266.         /// <summary>
  267.         /// Writes an Event ID.
  268.         /// </summary>
  269.         /// <param name="value">The event ID normally stored in an enumeration.</param>
  270.         public void WriteEventID(uint value)
  271.         {
  272.             WriteVariableWidthUInt(value, 6);
  273.         }
  274.  
  275.         /// <summary>
  276.         /// Writes a single bit either 0 or 1 into the buffer.
  277.         /// </summary>
  278.         /// <param name="value">The boolean value to write.</param>
  279.         public void WriteBool(bool value)
  280.         {
  281.             ExpandBuffer(1);
  282.             if (value) buffer[bitIndex / 8] |= (byte)(1 << (7 - bitIndex % 8));
  283.             ++bitIndex;
  284.         }
  285.  
  286.         /// <summary>
  287.         /// Writes an 8 bit unsigned byte into the buffer.
  288.         /// </summary>
  289.         /// <param name="value">The unsigned byte value to write.</param>
  290.         public void WriteByte(byte value)
  291.         {
  292.             ExpandBuffer(8);
  293.             int offset = bitIndex % 8;
  294.             buffer[bitIndex / 8] |= (byte)(value >> offset);
  295.             if (offset != 0)
  296.             {
  297.                 buffer[bitIndex / 8 + 1] |= (byte)(value << 8 - offset);
  298.             }
  299.             bitIndex += 8;
  300.         }
  301.  
  302.         /// <summary>
  303.         /// Writes an 8 bit signed byte into the buffer.
  304.         /// </summary>
  305.         /// <param name="value">The signed byte value to write.</param>
  306.         public void WriteSByte(sbyte value)
  307.         {
  308.             WriteByte((byte)value);
  309.         }
  310.  
  311.         /// <summary>
  312.         /// Writes a 16 bit unsigned short into the buffer.
  313.         /// </summary>
  314.         /// <param name="value">The unsigned short value to write.</param>
  315.         public void WriteUInt16(ushort value)
  316.         {
  317.             ExpandBuffer(16);
  318.             int offset = bitIndex % 8;
  319.             buffer[bitIndex / 8] |= (byte)(value >> 8 + offset);
  320.             buffer[bitIndex / 8 + 1] |= (byte)(value >> offset);
  321.             if (offset != 0)
  322.             {
  323.                 buffer[bitIndex / 8 + 2] |= (byte)(value << 8 - offset);
  324.             }
  325.             bitIndex += 16;
  326.         }
  327.  
  328.         /// <summary>
  329.         /// Writes a 16 bit signed short into the buffer.
  330.         /// </summary>
  331.         /// <param name="value">The signed short value to write.</param>
  332.         public void WriteInt16(short value)
  333.         {
  334.             WriteUInt16((ushort)value);
  335.         }
  336.  
  337.         /// <summary>
  338.         /// Writes a 32 bit unsigned integer into the buffer.
  339.         /// </summary>
  340.         /// <param name="value">The unsigned integer value to write.</param>
  341.         public void WriteUInt32(uint value)
  342.         {
  343.             ExpandBuffer(32);
  344.             int offset = bitIndex % 8;
  345.             buffer[bitIndex / 8] |= (byte)(value >> 24 + offset);
  346.             buffer[bitIndex / 8 + 1] |= (byte)(value >> 16 + offset);
  347.             buffer[bitIndex / 8 + 2] |= (byte)(value >> 8 + offset);
  348.             buffer[bitIndex / 8 + 3] |= (byte)(value >> offset);
  349.             if (offset != 0)
  350.             {
  351.                 buffer[bitIndex / 8 + 4] |= (byte)(value << 8 - offset);
  352.             }
  353.             bitIndex += 32;
  354.         }
  355.  
  356.         /// <summary>
  357.         /// Writes a 32 bit signed integer into the buffer.
  358.         /// </summary>
  359.         /// <param name="value">The signed integer value to write.</param>
  360.         public void WriteInt32(int value)
  361.         {
  362.             WriteUInt32((uint)value);
  363.         }
  364.  
  365.         /// <summary>
  366.         /// Writes a 64 bit unsigned integer into the buffer.
  367.         /// </summary>
  368.         /// <param name="value">The unsigned integer value to write.</param>
  369.         public void WriteUInt64(ulong value)
  370.         {
  371.             ExpandBuffer(64);
  372.             int offset = bitIndex % 8;
  373.             buffer[bitIndex / 8] |= (byte)(value >> 56 + offset);
  374.             buffer[bitIndex / 8 + 1] |= (byte)(value >> 48 + offset);
  375.             buffer[bitIndex / 8 + 2] |= (byte)(value >> 32 + offset);
  376.             buffer[bitIndex / 8 + 3] |= (byte)(value >> 24 + offset);
  377.             buffer[bitIndex / 8 + 4] |= (byte)(value >> 16 + offset);
  378.             buffer[bitIndex / 8 + 5] |= (byte)(value >> 8 + offset);
  379.             buffer[bitIndex / 8 + 6] |= (byte)(value >> offset);
  380.             if (offset != 0)
  381.             {
  382.                 buffer[bitIndex / 8 + 7] |= (byte)(value << 8 - offset);
  383.             }
  384.             bitIndex += 64;
  385.         }
  386.  
  387.         /// <summary>
  388.         /// Writes a 64 bit signed integer into the buffer.
  389.         /// </summary>
  390.         /// <param name="value">The signed integer value to write.</param>
  391.         public void WriteInt64(long value)
  392.         {
  393.             WriteUInt64((ulong)value);
  394.         }
  395.  
  396.         /// <summary>
  397.         /// Writes an n bit unsigned integer into the buffer.
  398.         /// </summary>
  399.         /// <param name="value">The unsigned integer value to write.</param>
  400.         /// <param name="bits">The number of bits to use.</param>
  401.         public void WriteUInt(uint value, int bits)
  402.         {
  403.             if (bits < 1 || bits > 32) throw new ArgumentOutOfRangeException("bits must be in the range (0, 32].");
  404.             if (bits != 32 && value > (0x1 << bits) - 1) throw new ArgumentOutOfRangeException("Value does not fit into " + bits.ToString() + " bits.");
  405.  
  406.             ExpandBuffer(bits);
  407.  
  408.             value <<= 32 - bits;
  409.  
  410.             int offset = bitIndex % 8;
  411.             buffer[bitIndex / 8] |= (byte)(value >> 24 + offset);
  412.             if (offset + bits > 8)
  413.             {
  414.                 buffer[bitIndex / 8 + 1] |= (byte)(value >> 16 + offset);
  415.                 if (offset + bits > 16)
  416.                 {
  417.                     buffer[bitIndex / 8 + 2] |= (byte)(value >> 8 + offset);
  418.                     if (offset + bits > 24)
  419.                     {
  420.                         buffer[bitIndex / 8 + 3] |= (byte)(value >> offset);
  421.                         if (offset + bits > 32)
  422.                         {
  423.                             buffer[bitIndex / 8 + 4] |= (byte)(value << 8 - offset);
  424.                         }
  425.                     }
  426.                 }
  427.             }
  428.             bitIndex += bits;
  429.         }
  430.  
  431.         /// <summary>
  432.         /// Writes an n bit unsigned integer into the buffer.
  433.         /// </summary>
  434.         /// <param name="value">The unsigned integer value to write.</param>
  435.         /// <param name="min">The minimum value in the range.</param>
  436.         /// <param name="max">The maximum value in the range.</param>
  437.         public void WriteUInt(uint value, uint min, uint max)
  438.         {
  439.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  440.             if (value < min || value > max) throw new ArgumentOutOfRangeException("The value must be on the interval [min, max]");
  441.  
  442.             uint valueBase = max - min + 1;
  443.             // Log2
  444.             var bits = 32;
  445.             if (valueBase != 0)
  446.             {
  447.                 int left = 0;
  448.                 int right = 32;
  449.                 for (int i = 0; i < 5; ++i)
  450.                 {
  451.                     if (valueBase >> (32 - (left + right) / 2) != 0)
  452.                     {
  453.                         // Left
  454.                         right = (left + right) / 2;
  455.                     }
  456.                     else
  457.                     {
  458.                         // Right
  459.                         left = (left + right) / 2;
  460.                     }
  461.                 }
  462.                 bits = 32 - left;
  463.             }
  464.             WriteUInt(value - min, bits);
  465.         }
  466.  
  467.         /// <summary>
  468.         /// Writes an n bit unsigned integer into the buffer.
  469.         /// </summary>
  470.         /// <param name="value">The unsigned integer value to write.</param>
  471.         /// <param name="bits">The number of bits to use.</param>
  472.         public void WriteUInt(ulong value, int bits)
  473.         {
  474.             if (bits < 1 || bits > 64) throw new ArgumentOutOfRangeException("bits must be in the range (0, 64].");
  475.             if (bits != 64 && value > ((ulong)0x1 << bits) - 1) throw new ArgumentOutOfRangeException("Value does not fit into " + bits.ToString() + " bits.");
  476.  
  477.             ExpandBuffer(bits);
  478.  
  479.             value <<= 64 - bits;
  480.  
  481.             int offset = bitIndex % 8;
  482.             buffer[bitIndex / 8] |= (byte)(value >> 56 + offset);
  483.             if (offset + bits > 8)
  484.             {
  485.                 buffer[bitIndex / 8 + 1] |= (byte)(value >> 48 + offset);
  486.                 if (offset + bits > 16)
  487.                 {
  488.                     buffer[bitIndex / 8 + 2] |= (byte)(value >> 40 + offset);
  489.                     if (offset + bits > 24)
  490.                     {
  491.                         buffer[bitIndex / 8 + 3] |= (byte)(value >> 32 + offset);
  492.                         if (offset + bits > 32)
  493.                         {
  494.                             buffer[bitIndex / 8 + 4] |= (byte)(value >> 24 + offset);
  495.                             if (offset + bits > 40)
  496.                             {
  497.                                 buffer[bitIndex / 8 + 5] |= (byte)(value >> 16 + offset);
  498.                                 if (offset + bits > 48)
  499.                                 {
  500.                                     buffer[bitIndex / 8 + 6] |= (byte)(value >> 8 + offset);
  501.                                     if (offset + bits > 56)
  502.                                     {
  503.                                         buffer[bitIndex / 8 + 7] |= (byte)(value >> offset);
  504.                                         if (offset + bits > 64)
  505.                                         {
  506.                                             buffer[bitIndex / 8 + 8] |= (byte)(value << 8 - offset);
  507.                                         }
  508.                                     }
  509.                                 }
  510.                             }
  511.                         }
  512.                     }
  513.                 }
  514.             }
  515.             bitIndex += bits;
  516.         }
  517.  
  518.         /// <summary>
  519.         /// Writes an n bit unsigned integer into the buffer.
  520.         /// </summary>
  521.         /// <param name="value">The unsigned integer value to write.</param>
  522.         /// <param name="min">The minimum value in the range.</param>
  523.         /// <param name="max">The maximum value in the range.</param>
  524.         public void WriteUInt(ulong value, ulong min, ulong max)
  525.         {
  526.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  527.             if (value < min || value > max) throw new ArgumentOutOfRangeException("The value must be on the interval [min, max]");
  528.  
  529.             ulong valueBase = max - min + 1;
  530.             // Log2
  531.             var bits = 64;
  532.             if (valueBase != 0)
  533.             {
  534.                 int left = 0;
  535.                 int right = 64;
  536.                 for (int i = 0; i < 6; ++i)
  537.                 {
  538.                     if (valueBase >> (64 - (left + right) / 2) != 0)
  539.                     {
  540.                         // Left
  541.                         right = (left + right) / 2;
  542.                     }
  543.                     else
  544.                     {
  545.                         // Right
  546.                         left = (left + right) / 2;
  547.                     }
  548.                 }
  549.                 bits = 64 - left;
  550.             }
  551.             WriteUInt(value - min, bits);
  552.         }
  553.  
  554.         /// <summary>
  555.         /// Writes an n bit signed integer into the buffer.
  556.         /// </summary>
  557.         /// <param name="value">The signed integer value to write.</param>
  558.         /// <param name="bits">The number of bits to use.</param>
  559.         public void WriteInt(int value, int bits)
  560.         {
  561.             if (bits < 1 || bits > 32) throw new ArgumentOutOfRangeException("bits must be in the range (0, 32].");
  562.             if (bits != 32 && (value < -(0x1 << (bits - 1)) || value >= 0x1 << (bits - 1))) throw new ArgumentOutOfRangeException("Value does not fit into " + bits.ToString() + " bits.");
  563.  
  564.             ExpandBuffer(bits);
  565.  
  566.             value <<= 32 - bits;
  567.             uint uvalue = (uint)value;
  568.  
  569.             int offset = bitIndex % 8;
  570.             buffer[bitIndex / 8] |= (byte)(uvalue >> 24 + offset);
  571.             if (offset + bits > 8)
  572.             {
  573.                 buffer[bitIndex / 8 + 1] |= (byte)(uvalue >> 16 + offset);
  574.                 if (offset + bits > 16)
  575.                 {
  576.                     buffer[bitIndex / 8 + 2] |= (byte)(uvalue >> 8 + offset);
  577.                     if (offset + bits > 24)
  578.                     {
  579.                         buffer[bitIndex / 8 + 3] |= (byte)(uvalue >> offset);
  580.                         if (offset + bits > 32)
  581.                         {
  582.                             buffer[bitIndex / 8 + 4] |= (byte)(uvalue << 8 - offset);
  583.                         }
  584.                     }
  585.                 }
  586.             }
  587.             bitIndex += bits;
  588.         }
  589.  
  590.         /// <summary>
  591.         /// Writes an n bit signed integer into the buffer.
  592.         /// </summary>
  593.         /// <param name="value">The signed integer value to write.</param>
  594.         /// <param name="min">The minimum value in the range.</param>
  595.         /// <param name="max">The maximum value in the range.</param>
  596.         public void WriteInt(int value, int min, int max)
  597.         {
  598.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  599.             if (value < min || value > max) throw new ArgumentOutOfRangeException("The value must be on the interval [min, max]");
  600.  
  601.             uint valueBase = (uint)(max - min + 1);
  602.             // Log2
  603.             var bits = 32;
  604.             if (valueBase != 0)
  605.             {
  606.                 int left = 0;
  607.                 int right = 32;
  608.                 for (int i = 0; i < 5; ++i)
  609.                 {
  610.                     if (valueBase >> (32 - (left + right) / 2) != 0)
  611.                     {
  612.                         // Left
  613.                         right = (left + right) / 2;
  614.                     }
  615.                     else
  616.                     {
  617.                         // Right
  618.                         left = (left + right) / 2;
  619.                     }
  620.                 }
  621.                 bits = 32 - left;
  622.             }
  623.             WriteInt(value - min, bits);
  624.         }
  625.  
  626.         /// <summary>
  627.         /// Writes an n bit signed integer into the buffer.
  628.         /// </summary>
  629.         /// <param name="value">The signed integer value to write.</param>
  630.         /// <param name="bits">The number of bits to use.</param>
  631.         public void WriteInt(long value, int bits)
  632.         {
  633.             if (bits < 1 || bits > 64) throw new ArgumentOutOfRangeException("bits must be in the range (0, 64].");
  634.             if (bits != 64 && (value < -((long)0x1 << (bits - 1)) || value >= (long)0x1 << (bits - 1))) throw new ArgumentOutOfRangeException("Value does not fit into " + bits.ToString() + " bits.");
  635.  
  636.             ExpandBuffer(bits);
  637.  
  638.             value <<= 64 - bits;
  639.             ulong uvalue = (ulong)value;
  640.  
  641.             int offset = bitIndex % 8;
  642.             buffer[bitIndex / 8] |= (byte)(uvalue >> 56 + offset);
  643.             if (offset + bits > 8)
  644.             {
  645.                 buffer[bitIndex / 8 + 1] |= (byte)(uvalue >> 48 + offset);
  646.                 if (offset + bits > 16)
  647.                 {
  648.                     buffer[bitIndex / 8 + 2] |= (byte)(uvalue >> 40 + offset);
  649.                     if (offset + bits > 24)
  650.                     {
  651.                         buffer[bitIndex / 8 + 3] |= (byte)(uvalue >> 32 + offset);
  652.                         if (offset + bits > 32)
  653.                         {
  654.                             buffer[bitIndex / 8 + 4] |= (byte)(uvalue >> 24 + offset);
  655.                             if (offset + bits > 40)
  656.                             {
  657.                                 buffer[bitIndex / 8 + 5] |= (byte)(uvalue >> 16 + offset);
  658.                                 if (offset + bits > 48)
  659.                                 {
  660.                                     buffer[bitIndex / 8 + 6] |= (byte)(uvalue >> 8 + offset);
  661.                                     if (offset + bits > 56)
  662.                                     {
  663.                                         buffer[bitIndex / 8 + 7] |= (byte)(uvalue >> offset);
  664.                                         if (offset + bits > 64)
  665.                                         {
  666.                                             buffer[bitIndex / 8 + 8] |= (byte)(uvalue << 8 - offset);
  667.                                         }
  668.                                     }
  669.                                 }
  670.                             }
  671.                         }
  672.                     }
  673.                 }
  674.             }
  675.             bitIndex += bits;
  676.         }
  677.  
  678.         /// <summary>
  679.         /// Writes an n bit signed integer into the buffer.
  680.         /// </summary>
  681.         /// <param name="value">The signed integer value to write.</param>
  682.         /// <param name="min">The minimum value in the range.</param>
  683.         /// <param name="max">The maximum value in the range.</param>
  684.         public void WriteInt(long value, long min, long max)
  685.         {
  686.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  687.             if (value < min || value > max) throw new ArgumentOutOfRangeException("The value must be on the interval [min, max]");
  688.  
  689.             ulong valueBase = (ulong)(max - min + 1);
  690.             // Log2
  691.             var bits = 64;
  692.             if (valueBase != 0)
  693.             {
  694.                 int left = 0;
  695.                 int right = 64;
  696.                 for (int i = 0; i < 6; ++i)
  697.                 {
  698.                     if (valueBase >> (64 - (left + right) / 2) != 0)
  699.                     {
  700.                         // Left
  701.                         right = (left + right) / 2;
  702.                     }
  703.                     else
  704.                     {
  705.                         // Right
  706.                         left = (left + right) / 2;
  707.                     }
  708.                 }
  709.                 bits = 64 - left;
  710.             }
  711.             WriteInt(value - min, bits);
  712.         }
  713.  
  714.         /// <summary>
  715.         /// Writes a 32 bit single into the buffer.
  716.         /// </summary>
  717.         /// <param name="value">The single value to write.</param>
  718.         public void WriteSingle(float value)
  719.         {
  720.             WriteUInt32(BitConverter.ToUInt32(BitConverter.GetBytes(value), 0));
  721.         }
  722.  
  723.         /// <summary>
  724.         /// Writes a 64 bit double into the buffer.
  725.         /// </summary>
  726.         /// <param name="value">The double value to write.</param>
  727.         public void WriteDouble(double value)
  728.         {
  729.             byte[] bytes = BitConverter.GetBytes(value);
  730.             WriteUInt32(BitConverter.ToUInt32(bytes, 0));
  731.             WriteUInt32(BitConverter.ToUInt32(bytes, 4));
  732.         }
  733.  
  734.         /// <summary>
  735.         /// Writes an integer using a variable width encoding of bits. Choose a bits value that represents the number of bits to hold the average value.
  736.         /// </summary>
  737.         /// <param name="value">The unsigned integer value to write.</param>
  738.         /// <param name="bits">The number of bits to use for the sequence.</param>
  739.         public void WriteVariableWidthUInt(uint value, int bits = 4)
  740.         {
  741.             if (bits < 1 || bits > 32) throw new ArgumentOutOfRangeException("bits must be in the range (0, 32].");
  742.             int shift = bits;
  743.             // Stop when our value can fit inside
  744.             for (; shift < 32 && value >= (0x1 << shift); shift += bits)
  745.             {
  746.                 WriteBool(true); // Write a 1 for a continuation bit signifying one more interval is needed
  747.             }
  748.             if (shift < 32)
  749.             {
  750.                 WriteBool(false);    // Write a 0 for a continuation bit signifying the end
  751.             }
  752.             WriteUInt(value, shift >= 32 ? 32 : shift);
  753.         }
  754.  
  755.         /// <summary>
  756.         /// Writes an integer using a variable length of bits. Choose a bits value that represents the number of bits to hold the average value.
  757.         /// </summary>
  758.         /// <param name="value">The signed integer value to write.</param>
  759.         /// <param name="bits">The number of bits to use for the sequence.</param>
  760.         public void WriteVariableWidthInt(int value, int bits = 4)
  761.         {
  762.             if (bits < 1 || bits > 32) throw new ArgumentOutOfRangeException("bits must be in the range (0, 32].");
  763.             int shift = bits;
  764.             // Stop when our value can fit inside
  765.             for (; shift < 32 && (value < -(0x1 << (shift - 1)) || value >= 0x1 << (shift - 1)); shift += bits)
  766.             {
  767.                 WriteBool(true); // Write a 1 for a continuation bit signifying one more interval is needed
  768.             }
  769.             if (shift < 32)
  770.             {
  771.                 WriteBool(false);    // Write a 0 for a continuation bit signifying the end
  772.             }
  773.             WriteInt(value, shift >= 32 ? 32 : shift);
  774.         }
  775.  
  776.         /// <summary>
  777.         /// Writes an array of unsigned integers in a given range defining a base.
  778.         /// </summary>
  779.         /// <param name="value">The array of unsigned integers to write.</param>
  780.         /// <param name="min">The minimum value in the range.</param>
  781.         /// <param name="max">The maximum value in the range.</param>
  782.         public void WriteArrayUInt32(uint[] value, int min, int max)
  783.         {
  784.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  785.             ulong valueBase = (ulong)(max - min + 1);
  786.             var sum = new List<ulong>();
  787.             var valueBasePow = new List<ulong>();
  788.             valueBasePow.Add(1);
  789.             for (var i = 0; i < value.Length; ++i)
  790.             {
  791.                 // sum += arrayBasePow * array[i];
  792.                 ulong multiplyCarry = 0;
  793.                 ulong addCarry = 0;
  794.                 for (var j = 0; j < valueBasePow.Count || multiplyCarry != 0 || j < sum.Count || addCarry != 0; ++j)
  795.                 {
  796.                     if (j >= sum.Count)
  797.                     {
  798.                         sum.Add(0);
  799.                     }
  800.  
  801.                     ulong temp = 0;
  802.                     ulong product = 0;
  803.                     if (j < valueBasePow.Count)
  804.                     {
  805.                         product = (ulong)(value[i] - min) * valueBasePow[j];
  806.                     }
  807.                     temp += product + multiplyCarry;
  808.                     multiplyCarry = temp >> 32;
  809.                     temp &= 0xFFFFFFFF;
  810.  
  811.                     sum[j] += temp + addCarry;
  812.                     addCarry = sum[j] >> 32;
  813.                     sum[j] &= 0xFFFFFFFF;
  814.                 }
  815.                 // arrayBasePow *= arrayBase;
  816.                 multiplyCarry = 0;
  817.                 for (var j = 0; j < valueBasePow.Count || multiplyCarry != 0; ++j)
  818.                 {
  819.                     if (j >= valueBasePow.Count)
  820.                     {
  821.                         valueBasePow.Add(0);
  822.                     }
  823.                     ulong product = 0;
  824.                     if (j < valueBasePow.Count)
  825.                     {
  826.                         product = valueBase * valueBasePow[j];
  827.                     }
  828.                     valueBasePow[j] = product + multiplyCarry;
  829.                     multiplyCarry = valueBasePow[j] >> 32;
  830.                     valueBasePow[j] &= 0xFFFFFFFF;
  831.                 }
  832.             }
  833.  
  834.             for (var i = sum.Count - 1; i > 0 && sum[i] == 0; --i)
  835.             {
  836.                 sum.RemoveAt(sum.Count - 1);
  837.             }
  838.  
  839.             // Log2
  840.             int numberOfBits = 0;
  841.             if (!(valueBasePow.Count == 1 && valueBasePow[0] == 0))
  842.             {
  843.                 int left = 0;
  844.                 int right = 32;
  845.                 for (int i = 0; i < 5; ++i)
  846.                 {
  847.                     if (valueBasePow[valueBasePow.Count - 1] >> (32 - (left + right) / 2) != 0)
  848.                     {
  849.                         // Left
  850.                         right = (left + right) / 2;
  851.                     }
  852.                     else
  853.                     {
  854.                         // Right
  855.                         left = (left + right) / 2;
  856.                     }
  857.                 }
  858.                 numberOfBits = (valueBasePow.Count - 1) * 32 + (32 - left);
  859.             }
  860.  
  861.             for (int copyBits = 0; copyBits < numberOfBits; copyBits += 32)
  862.             {
  863.                 WriteUInt((uint)sum[copyBits / 32], numberOfBits - copyBits > 32 ? 32 : numberOfBits - copyBits);
  864.             }
  865.         }
  866.  
  867.         /// <summary>
  868.         /// Writes an array of signed integers in a given range defining a base.
  869.         /// </summary>
  870.         /// <param name="value">The array of signed integers to write.</param>
  871.         /// <param name="min">The minimum value in the range.</param>
  872.         /// <param name="max">The maximum value in the range.</param>
  873.         public void WriteArrayInt32(int[] value, int min, int max)
  874.         {
  875.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  876.             var uValue = new uint[value.Length];
  877.             for (var i = 0; i < value.Length; ++i)
  878.             {
  879.                 uValue[i] = (uint)(value[i] - min);
  880.             }
  881.             WriteArrayUInt32(uValue, 0, max - min);
  882.         }
  883.  
  884.         /// <summary>
  885.         /// Writes an array of singles in a given range converted to a given integral base.
  886.         /// Creates a value for the ratio: value / (valueBase - 1) in relationship to value / (max - min).
  887.         /// </summary>
  888.         /// <param name="value">The array of singles to write.</param>
  889.         /// <param name="min">The minimum value in the range.</param>
  890.         /// <param name="max">The maximum value in the range.</param>
  891.         /// <param name="valueBase">The base to convert the singles to.</param>
  892.         public void WriteArraySingle(float[] value, float min, float max, int valueBase)
  893.         {
  894.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  895.             var uValue = new uint[value.Length];
  896.             if (min < 0 && max > 0)
  897.             {
  898.                 for (var i = 0; i < value.Length; ++i)
  899.                 {
  900.                     uValue[i] = value[i] == 0 ? 0 : (uint)Math.Round((value[i] - min) / (max - min) * (valueBase - 2)) + 1;
  901.                 }
  902.             }
  903.             else
  904.             {
  905.                 for (var i = 0; i < value.Length; ++i)
  906.                 {
  907.                     uValue[i] = (uint)Math.Round((value[i] - min) / (max - min) * (valueBase - 1));
  908.                 }
  909.             }
  910.             WriteArrayUInt32(uValue, 0, valueBase - 1);
  911.         }
  912.  
  913.         /// <summary>
  914.         /// Writes an array of doubles in a given range converted to a given integral base.
  915.         /// Creates a value for the ratio: value / (valueBase - 1) in relationship to value / (max - min).
  916.         /// </summary>
  917.         /// <param name="value">The array of doubles to write.</param>
  918.         /// <param name="min">The minimum value in the range.</param>
  919.         /// <param name="max">The maximum value in the range.</param>
  920.         /// <param name="valueBase">The base to convert the doubles to.</param>
  921.         public void WriteArrayDouble(double[] value, double min, double max, int valueBase)
  922.         {
  923.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  924.             var uValue = new uint[value.Length];
  925.             if (min < 0 && max > 0)
  926.             {
  927.                 for (var i = 0; i < value.Length; ++i)
  928.                 {
  929.                     uValue[i] = value[i] == 0 ? 0 : (uint)Math.Round((value[i] - min) / (max - min) * (valueBase - 2)) + 1;
  930.                 }
  931.             }
  932.             else
  933.             {
  934.                 for (var i = 0; i < value.Length; ++i)
  935.                 {
  936.                     uValue[i] = (uint)Math.Round((value[i] - min) / (max - min) * (valueBase - 1));
  937.                 }
  938.             }
  939.             WriteArrayUInt32(uValue, 0, valueBase - 1);
  940.         }
  941.  
  942.         /// <summary>
  943.         /// Creates a value for the ratio: value / (2 ^ bitResolution - 1) in relationship to value / (max - min).
  944.         /// This allows a floating point to have a resolution.
  945.         /// </summary>
  946.         /// <param name="value">The floating point value in the range.</param>
  947.         /// <param name="min">The minimum value in the range.</param>
  948.         /// <param name="max">The maximum value in the range.</param>
  949.         /// <param name="bitResolution">The number of bits to use for the ratio.</param>
  950.         public void WriteCustomResolutionSingle(float value, float min, float max, int bitResolution)
  951.         {
  952.             if (bitResolution < 1 || bitResolution > 31) throw new ArgumentOutOfRangeException("bitResolution must be in the range (0, 32).");
  953.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  954.             if (value < min || value > max) throw new ArgumentOutOfRangeException("The value must be on the interval [min, max]");
  955.             uint uValue;
  956.             if (min < 0 && max > 0)
  957.             {
  958.                 uValue = value == 0 ? 0 : (uint)Math.Round((value - min) / (max - min) * (float)((0x1 << bitResolution) - 2)) + 1;
  959.             }
  960.             else
  961.             {
  962.                 uValue = (uint)Math.Round((value - min) / (max - min) * (float)((0x1 << bitResolution) - 1));
  963.             }
  964.             WriteUInt(uValue, bitResolution);
  965.         }
  966.  
  967.         /// <summary>
  968.         /// Creates a value for the ratio: value / (2 ^ bitResolution - 1) in relationship to value / (max - min).
  969.         /// This allows a floating point to have a resolution.
  970.         /// </summary>
  971.         /// <param name="value">The double value in the range.</param>
  972.         /// <param name="min">The minimum value in the range.</param>
  973.         /// <param name="max">The maximum value in the range.</param>
  974.         /// <param name="bitResolution">The number of bits to use for the ratio.</param>
  975.         public void WriteCustomResolutionDouble(double value, double min, double max, int bitResolution)
  976.         {
  977.             if (bitResolution < 1 || bitResolution > 31) throw new ArgumentOutOfRangeException("bitResolution must be in the range (0, 32).");
  978.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  979.             if (value < min || value > max) throw new ArgumentOutOfRangeException("The value must be on the interval [min, max]");
  980.             uint uValue;
  981.             if (min < 0 && max > 0)
  982.             {
  983.                 uValue = value == 0 ? 0 : (uint)Math.Round((value - min) / (max - min) * (double)((0x1 << bitResolution) - 2)) + 1;
  984.             }
  985.             else
  986.             {
  987.                 uValue = (uint)Math.Round((value - min) / (max - min) * (double)((0x1 << bitResolution) - 1));
  988.             }
  989.             WriteUInt(uValue, bitResolution);
  990.         }
  991.  
  992.         /// <summary>
  993.         /// Writes the length of the string using WriteVariableWidthUInt and then writes a boolean, true for unicode, false for ascii.
  994.         /// ASCII uses a compression step using either 6 or 7 bits per character.
  995.         /// </summary>
  996.         /// <param name="value">The string value to write.</param>
  997.         /// <param name="bits">The number of bits for the length written using an unsigned variable-width integer.</param>
  998.         public void WriteString(string value, int bits = 4)
  999.         {
  1000.             if (bits < 1 || bits > 31) throw new ArgumentOutOfRangeException("bits must be in the range (0, 32).");
  1001.  
  1002.             var asciiBytes = Encoding.ASCII.GetBytes(value);
  1003.             uint size = (uint)asciiBytes.Length;
  1004.             WriteVariableWidthUInt(size, bits);
  1005.             foreach (byte asciiByte in asciiBytes)
  1006.             {
  1007.                 WriteByte(asciiByte);
  1008.             }
  1009.         }
  1010.  
  1011.         /// <summary>
  1012.         /// Appends a binary packet to the buffer.
  1013.         /// </summary>
  1014.         /// <param name="value">The binary packet to write.</param>
  1015.         public void WriteBinaryPacket(Packet value)
  1016.         {
  1017.             int oldBitIndex = value.bitIndex;
  1018.             value.bitIndex = 0;
  1019.             int valueMaxBitIndex = value.maxBitIndex;
  1020.             WriteVariableWidthUInt((uint)valueMaxBitIndex);
  1021.             for (int copyBits = 0; copyBits < valueMaxBitIndex; copyBits += 32)
  1022.             {
  1023.                 uint uValue;
  1024.                 value.ReadUInt(out uValue, valueMaxBitIndex - copyBits > 32 ? 32 : valueMaxBitIndex - copyBits);
  1025.                 WriteUInt(uValue, valueMaxBitIndex - copyBits > 32 ? 32 : valueMaxBitIndex - copyBits);
  1026.             }
  1027.             bitIndex += (int)valueMaxBitIndex;
  1028.             value.bitIndex = oldBitIndex;
  1029.         }
  1030.  
  1031.         //READ METHODS
  1032.  
  1033.         /// <summary>
  1034.         /// Reads an Event ID.
  1035.         /// </summary>
  1036.         /// <param name="value">The event ID normally stored in an enumeration.</param>
  1037.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1038.         /// <returns>false on error.</returns>
  1039.         public bool ReadEventID(out uint value, bool nestedRead = false)
  1040.         {
  1041.             return ReadVariableWidthUInt(out value, 6);
  1042.         }
  1043.  
  1044.         /// <summary>
  1045.         /// Reads one bit from the buffer.
  1046.         /// </summary>
  1047.         /// <param name="value">Boolean.</param>
  1048.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1049.         /// <returns>false on error.</returns>
  1050.         public bool ReadBool(out bool value, bool nestedRead = false)
  1051.         {
  1052.             if (!nestedRead) previousBitIndex = bitIndex;
  1053.             value = false;
  1054.             if ((bitIndex + 1 + 7) / 8 > buffer.Count)
  1055.             {
  1056.                 return false;
  1057.             }
  1058.             value = ((buffer[bitIndex / 8] >> (7 - bitIndex % 8)) & 0x1) == 1;
  1059.             ++bitIndex;
  1060.             return true;
  1061.         }
  1062.  
  1063.         /// <summary>
  1064.         /// Reads an 8 bits unsigned byte from the buffer.
  1065.         /// </summary>
  1066.         /// <param name="value">Unsigned Byte.</param>
  1067.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1068.         /// <returns>false on error.</returns>
  1069.         public bool ReadByte(out byte value, bool nestedRead = false)
  1070.         {
  1071.             if (!nestedRead) previousBitIndex = bitIndex;
  1072.             value = 0;
  1073.             if ((bitIndex + 8 + 7) / 8 > buffer.Count)
  1074.             {
  1075.                 return false;
  1076.             }
  1077.             int offset = bitIndex % 8;
  1078.             value |= (byte)(buffer[bitIndex / 8] << offset);
  1079.             if (offset != 0)
  1080.             {
  1081.                 value |= (byte)(buffer[bitIndex / 8 + 1] >> 8 - offset);
  1082.             }
  1083.             bitIndex += 8;
  1084.             return true;
  1085.         }
  1086.  
  1087.         /// <summary>
  1088.         /// Reads an 8 bits signed byte from the buffer.
  1089.         /// </summary>
  1090.         /// <param name="value">Signed Byte.</param>
  1091.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1092.         /// <returns>false on error.</returns>
  1093.         public bool ReadSByte(out sbyte value, bool nestedRead = false)
  1094.         {
  1095.             if (!nestedRead) previousBitIndex = bitIndex;
  1096.             value = 0;
  1097.             if ((bitIndex + 8 + 7) / 8 > buffer.Count)
  1098.             {
  1099.                 return false;
  1100.             }
  1101.             int offset = bitIndex % 8;
  1102.             value |= (sbyte)(buffer[bitIndex / 8] << offset);
  1103.             if (offset != 0)
  1104.             {
  1105.                 value |= (sbyte)(buffer[bitIndex / 8 + 1] >> 8 - offset);
  1106.             }
  1107.             bitIndex += 8;
  1108.             return true;
  1109.         }
  1110.  
  1111.         /// <summary>
  1112.         /// Reads a 16 bit unsigned short from the buffer.
  1113.         /// </summary>
  1114.         /// <param name="value">Unsigned Short.</param>
  1115.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1116.         /// <returns>false on error.</returns>
  1117.         public bool ReadUInt16(out ushort value, bool nestedRead = false)
  1118.         {
  1119.             if (!nestedRead) previousBitIndex = bitIndex;
  1120.             value = 0;
  1121.             if ((bitIndex + 16 + 7) / 8 > buffer.Count)
  1122.             {
  1123.                 return false;
  1124.             }
  1125.             int offset = bitIndex % 8;
  1126.             value |= (ushort)(buffer[bitIndex / 8] << 8 + offset);
  1127.             value |= (ushort)(buffer[bitIndex / 8 + 1] << offset);
  1128.             if (offset != 0)
  1129.             {
  1130.                 value |= (ushort)(buffer[bitIndex / 8 + 2] >> 8 - offset);
  1131.             }
  1132.             bitIndex += 16;
  1133.             return true;
  1134.         }
  1135.  
  1136.         /// <summary>
  1137.         /// Reads a 16 bit signed short from the buffer.
  1138.         /// </summary>
  1139.         /// <param name="value">Signed Short.</param>
  1140.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1141.         /// <returns>false on error.</returns>
  1142.         public bool ReadInt16(out short value, bool nestedRead = false)
  1143.         {
  1144.             if (!nestedRead) previousBitIndex = bitIndex;
  1145.             value = 0;
  1146.             if ((bitIndex + 16 + 7) / 8 > buffer.Count)
  1147.             {
  1148.                 return false;
  1149.             }
  1150.             int offset = bitIndex % 8;
  1151.             value |= (short)(buffer[bitIndex / 8] << 8 + offset);
  1152.             value |= (short)(buffer[bitIndex / 8 + 1] << offset);
  1153.             if (offset != 0)
  1154.             {
  1155.                 value |= (short)(buffer[bitIndex / 8 + 2] >> 8 - offset);
  1156.             }
  1157.             bitIndex += 16;
  1158.             return true;
  1159.         }
  1160.  
  1161.         /// <summary>
  1162.         /// Reads a 32 bit unsigned integer from the buffer.
  1163.         /// </summary>
  1164.         /// <param name="value">Unsigned Integer.</param>
  1165.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1166.         /// <returns>false on error.</returns>
  1167.         public bool ReadUInt32(out uint value, bool nestedRead = false)
  1168.         {
  1169.             if (!nestedRead) previousBitIndex = bitIndex;
  1170.             value = 0;
  1171.             if ((bitIndex + 32 + 7) / 8 > buffer.Count)
  1172.             {
  1173.                 return false;
  1174.             }
  1175.             int offset = bitIndex % 8;
  1176.             value |= (uint)(buffer[bitIndex / 8] << 24 + offset);
  1177.             value |= (uint)(buffer[bitIndex / 8 + 1] << 16 + offset);
  1178.             value |= (uint)(buffer[bitIndex / 8 + 2] << 8 + offset);
  1179.             value |= (uint)(buffer[bitIndex / 8 + 3] << offset);
  1180.             if (offset != 0)
  1181.             {
  1182.                 value |= (uint)(buffer[bitIndex / 8 + 4] >> 8 - offset);
  1183.             }
  1184.             bitIndex += 32;
  1185.             return true;
  1186.         }
  1187.  
  1188.         /// <summary>
  1189.         /// Reads a 32 bit signed integer from the buffer.
  1190.         /// </summary>
  1191.         /// <param name="value">Signed Integer.</param>
  1192.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1193.         /// <returns>false on error.</returns>
  1194.         public bool ReadInt32(out int value, bool nestedRead = false)
  1195.         {
  1196.             if (!nestedRead) previousBitIndex = bitIndex;
  1197.             value = 0;
  1198.             if ((bitIndex + 32 + 7) / 8 > buffer.Count)
  1199.             {
  1200.                 return false;
  1201.             }
  1202.             int offset = bitIndex % 8;
  1203.             value |= (int)(buffer[bitIndex / 8] << 24 + offset);
  1204.             value |= (int)(buffer[bitIndex / 8 + 1] << 16 + offset);
  1205.             value |= (int)(buffer[bitIndex / 8 + 2] << 8 + offset);
  1206.             value |= (int)(buffer[bitIndex / 8 + 3] << offset);
  1207.             if (offset != 0)
  1208.             {
  1209.                 value |= (int)(buffer[bitIndex / 8 + 4] >> 8 - offset);
  1210.             }
  1211.             bitIndex += 32;
  1212.             return true;
  1213.         }
  1214.  
  1215.         /// <summary>
  1216.         /// Reads a 64 bit unsigned integer from the buffer.
  1217.         /// </summary>
  1218.         /// <param name="value">Unsigned Long.</param>
  1219.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1220.         /// <returns>false on error.</returns>
  1221.         public bool ReadUInt64(out ulong value, bool nestedRead = false)
  1222.         {
  1223.             if (!nestedRead) previousBitIndex = bitIndex;
  1224.             value = 0;
  1225.             if ((bitIndex + 64 + 7) / 8 > buffer.Count)
  1226.             {
  1227.                 return false;
  1228.             }
  1229.             int offset = bitIndex % 8;
  1230.             value |= (ulong)(buffer[bitIndex / 8]) << 56 + offset;
  1231.             value |= (ulong)(buffer[bitIndex / 8 + 1]) << 48 + offset;
  1232.             value |= (ulong)(buffer[bitIndex / 8 + 2]) << 40 + offset;
  1233.             value |= (ulong)(buffer[bitIndex / 8 + 3]) << 32 + offset;
  1234.             value |= (ulong)(buffer[bitIndex / 8 + 4]) << 24 + offset;
  1235.             value |= (ulong)(buffer[bitIndex / 8 + 5]) << 16 + offset;
  1236.             value |= (ulong)(buffer[bitIndex / 8 + 6]) << 8 + offset;
  1237.             value |= (ulong)(buffer[bitIndex / 8 + 7]) << offset;
  1238.             if (offset != 0)
  1239.             {
  1240.                 value |= (ulong)(buffer[bitIndex / 8 + 8]) >> 8 - offset;
  1241.             }
  1242.             bitIndex += 64;
  1243.             return true;
  1244.         }
  1245.  
  1246.         /// <summary>
  1247.         /// Reads a 64 bit signed integer from the buffer.
  1248.         /// </summary>
  1249.         /// <param name="value">Signed Long.</param>
  1250.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1251.         /// <returns>false on error.</returns>
  1252.         public bool ReadInt64(out long value, bool nestedRead = false)
  1253.         {
  1254.             if (!nestedRead) previousBitIndex = bitIndex;
  1255.             value = 0;
  1256.             if ((bitIndex + 64 + 7) / 8 > buffer.Count)
  1257.             {
  1258.                 return false;
  1259.             }
  1260.             int offset = bitIndex % 8;
  1261.             value |= (long)(buffer[bitIndex / 8]) << 56 + offset;
  1262.             value |= (long)(buffer[bitIndex / 8 + 1]) << 48 + offset;
  1263.             value |= (long)(buffer[bitIndex / 8 + 2]) << 40 + offset;
  1264.             value |= (long)(buffer[bitIndex / 8 + 3]) << 32 + offset;
  1265.             value |= (long)(buffer[bitIndex / 8 + 4]) << 24 + offset;
  1266.             value |= (long)(buffer[bitIndex / 8 + 5]) << 16 + offset;
  1267.             value |= (long)(buffer[bitIndex / 8 + 6]) << 8 + offset;
  1268.             value |= (long)(buffer[bitIndex / 8 + 7]) << offset;
  1269.             if (offset != 0)
  1270.             {
  1271.                 value |= (long)(buffer[bitIndex / 8 + 8]) >> 8 - offset;
  1272.             }
  1273.             bitIndex += 64;
  1274.             return true;
  1275.         }
  1276.  
  1277.         /// <summary>
  1278.         /// Reads an n bit unsigned integer from the buffer.
  1279.         /// </summary>
  1280.         /// <param name="value">Unsigned Integer.</param>
  1281.         /// <param name="bits">The number of bits used to write.</param>
  1282.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1283.         /// <returns>false on error.</returns>
  1284.         public bool ReadUInt(out uint value, int bits, bool nestedRead = false)
  1285.         {
  1286.             if (bits < 1 || bits > 32) throw new ArgumentOutOfRangeException("bits must be in the range (0, 32].");
  1287.  
  1288.             if (!nestedRead) previousBitIndex = bitIndex;
  1289.             value = 0;
  1290.             if ((bitIndex + bits + 7) / 8 > buffer.Count)
  1291.             {
  1292.                 return false;
  1293.             }
  1294.  
  1295.             int offset = bitIndex % 8;
  1296.             value = (uint)buffer[bitIndex / 8] << 24 + offset;
  1297.             if (offset + bits > 8)
  1298.             {
  1299.                 value |= (uint)buffer[bitIndex / 8 + 1] << 16 + offset;
  1300.                 if (offset + bits > 16)
  1301.                 {
  1302.                     value |= (uint)buffer[bitIndex / 8 + 2] << 8 + offset;
  1303.                     if (offset + bits > 24)
  1304.                     {
  1305.                         value |= (uint)buffer[bitIndex / 8 + 3] << offset;
  1306.                         if (offset + bits > 32)
  1307.                         {
  1308.                             value |= (uint)buffer[bitIndex / 8 + 4] >> 8 - offset;
  1309.                         }
  1310.                     }
  1311.                 }
  1312.             }
  1313.  
  1314.             value >>= 32 - bits;
  1315.             bitIndex += bits;
  1316.             return true;
  1317.         }
  1318.  
  1319.         /// <summary>
  1320.         /// Reads an n bit unsigned integer from the buffer.
  1321.         /// </summary>
  1322.         /// <param name="value">Unsigned Integer.</param>
  1323.         /// <param name="min">The minimum value in the range.</param>
  1324.         /// <param name="max">The maximum value in the range.</param>
  1325.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1326.         /// <returns>false on error.</returns>
  1327.         public bool ReadUInt(out uint value, uint min, uint max, bool nestedRead = false)
  1328.         {
  1329.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  1330.  
  1331.             if (!nestedRead) previousBitIndex = bitIndex;
  1332.             uint valueBase = max - min + 1;
  1333.             // Log2
  1334.             var bits = 32;
  1335.             if (valueBase != 0)
  1336.             {
  1337.                 int left = 0;
  1338.                 int right = 32;
  1339.                 for (int i = 0; i < 5; ++i)
  1340.                 {
  1341.                     if (valueBase >> (32 - (left + right) / 2) != 0)
  1342.                     {
  1343.                         // Left
  1344.                         right = (left + right) / 2;
  1345.                     }
  1346.                     else
  1347.                     {
  1348.                         // Right
  1349.                         left = (left + right) / 2;
  1350.                     }
  1351.                 }
  1352.                 bits = 32 - left;
  1353.             }
  1354.             if (!ReadUInt(out value, bits, true)) return false;
  1355.             value += min;
  1356.             return true;
  1357.         }
  1358.  
  1359.         /// <summary>
  1360.         /// Reads an n bit unsigned integer from the buffer.
  1361.         /// </summary>
  1362.         /// <param name="value">Unsigned Integer.</param>
  1363.         /// <param name="bits">The number of bits used to write.</param>
  1364.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1365.         /// <returns>false on error.</returns>
  1366.         public bool ReadUInt(out ulong value, int bits, bool nestedRead = false)
  1367.         {
  1368.             if (bits < 1 || bits > 64) throw new ArgumentOutOfRangeException("bits must be in the range (0, 64].");
  1369.  
  1370.             if (!nestedRead) previousBitIndex = bitIndex;
  1371.             value = 0;
  1372.             if ((bitIndex + bits + 7) / 8 > buffer.Count)
  1373.             {
  1374.                 return false;
  1375.             }
  1376.  
  1377.             int offset = bitIndex % 8;
  1378.             value = (ulong)buffer[bitIndex / 8] << 56 + offset;
  1379.             if (offset + bits > 8)
  1380.             {
  1381.                 value |= (ulong)buffer[bitIndex / 8 + 1] << 48 + offset;
  1382.                 if (offset + bits > 16)
  1383.                 {
  1384.                     value |= (ulong)buffer[bitIndex / 8 + 2] << 40 + offset;
  1385.                     if (offset + bits > 24)
  1386.                     {
  1387.                         value |= (ulong)buffer[bitIndex / 8 + 3] << 32 + offset;
  1388.                         if (offset + bits > 32)
  1389.                         {
  1390.                             value |= (ulong)buffer[bitIndex / 8 + 4] << 24 + offset;
  1391.                             if (offset + bits > 40)
  1392.                             {
  1393.                                 value |= (ulong)buffer[bitIndex / 8 + 5] << 16 + offset;
  1394.                                 if (offset + bits > 48)
  1395.                                 {
  1396.                                     value |= (ulong)buffer[bitIndex / 8 + 6] << 8 + offset;
  1397.                                     if (offset + bits > 56)
  1398.                                     {
  1399.                                         value |= (ulong)buffer[bitIndex / 8 + 7] << offset;
  1400.                                         if (offset + bits > 64)
  1401.                                         {
  1402.                                             value |= (ulong)buffer[bitIndex / 8 + 8] >> 8 - offset;
  1403.                                         }
  1404.                                     }
  1405.                                 }
  1406.                             }
  1407.                         }
  1408.                     }
  1409.                 }
  1410.             }
  1411.  
  1412.             value >>= 64 - bits;
  1413.             bitIndex += bits;
  1414.             return true;
  1415.         }
  1416.  
  1417.         /// <summary>
  1418.         /// Reads an n bit unsigned integer from the buffer.
  1419.         /// </summary>
  1420.         /// <param name="value">Unsigned Integer.</param>
  1421.         /// <param name="min">The minimum value in the range.</param>
  1422.         /// <param name="max">The maximum value in the range.</param>
  1423.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1424.         /// <returns>false on error.</returns>
  1425.         public bool ReadUInt(out ulong value, ulong min, ulong max, bool nestedRead = false)
  1426.         {
  1427.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  1428.  
  1429.             if (!nestedRead) previousBitIndex = bitIndex;
  1430.             ulong valueBase = max - min + 1;
  1431.             // Log2
  1432.             var bits = 64;
  1433.             if (valueBase != 0)
  1434.             {
  1435.                 int left = 0;
  1436.                 int right = 64;
  1437.                 for (int i = 0; i < 6; ++i)
  1438.                 {
  1439.                     if (valueBase >> (64 - (left + right) / 2) != 0)
  1440.                     {
  1441.                         // Left
  1442.                         right = (left + right) / 2;
  1443.                     }
  1444.                     else
  1445.                     {
  1446.                         // Right
  1447.                         left = (left + right) / 2;
  1448.                     }
  1449.                 }
  1450.                 bits = 64 - left;
  1451.             }
  1452.             if (!ReadUInt(out value, bits, true)) return false;
  1453.             value += min;
  1454.             return true;
  1455.         }
  1456.  
  1457.         /// <summary>
  1458.         /// Reads an n bit custom signed integer from the buffer.
  1459.         /// </summary>
  1460.         /// <param name="value">Signed Integer.</param>
  1461.         /// <param name="bits">The number of bits used to write.</param>
  1462.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1463.         /// <returns>false on error.</returns>
  1464.         public bool ReadInt(out int value, int bits, bool nestedRead = false)
  1465.         {
  1466.             if (bits < 1 || bits > 32) throw new ArgumentOutOfRangeException("bits must be in the range (0, 32].");
  1467.  
  1468.             if (!nestedRead) previousBitIndex = bitIndex;
  1469.             value = 0;
  1470.             if ((bitIndex + bits + 7) / 8 > buffer.Count)
  1471.             {
  1472.                 return false;
  1473.             }
  1474.  
  1475.             int offset = bitIndex % 8;
  1476.             value = buffer[bitIndex / 8] << 24 + offset;
  1477.             if (offset + bits > 8)
  1478.             {
  1479.                 value |= buffer[bitIndex / 8 + 1] << 16 + offset;
  1480.                 if (offset + bits > 16)
  1481.                 {
  1482.                     value |= buffer[bitIndex / 8 + 2] << 8 + offset;
  1483.                     if (offset + bits > 24)
  1484.                     {
  1485.                         value |= buffer[bitIndex / 8 + 3] << offset;
  1486.                         if (offset + bits > 32)
  1487.                         {
  1488.                             value |= buffer[bitIndex / 8 + 4] >> 8 - offset;
  1489.                         }
  1490.                     }
  1491.                 }
  1492.             }
  1493.  
  1494.             value >>= 32 - bits;
  1495.             bitIndex += bits;
  1496.             return true;
  1497.         }
  1498.  
  1499.         /// <summary>
  1500.         /// Reads an n bit custom signed integer from the buffer.
  1501.         /// </summary>
  1502.         /// <param name="value">Signed Integer.</param>
  1503.         /// <param name="min">The minimum value in the range.</param>
  1504.         /// <param name="max">The maximum value in the range.</param>
  1505.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1506.         /// <returns>false on error.</returns>
  1507.         public bool ReadInt(out int value, int min, int max, bool nestedRead = false)
  1508.         {
  1509.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  1510.  
  1511.             if (!nestedRead) previousBitIndex = bitIndex;
  1512.             uint valueBase = (uint)(max - min + 1);
  1513.             // Log2
  1514.             var bits = 32;
  1515.             if (valueBase != 0)
  1516.             {
  1517.                 int left = 0;
  1518.                 int right = 32;
  1519.                 for (int i = 0; i < 5; ++i)
  1520.                 {
  1521.                     if (valueBase >> (32 - (left + right) / 2) != 0)
  1522.                     {
  1523.                         // Left
  1524.                         right = (left + right) / 2;
  1525.                     }
  1526.                     else
  1527.                     {
  1528.                         // Right
  1529.                         left = (left + right) / 2;
  1530.                     }
  1531.                 }
  1532.                 bits = 32 - left;
  1533.             }
  1534.             if (!ReadInt(out value, bits, true)) return false;
  1535.             value += min;
  1536.             return true;
  1537.         }
  1538.  
  1539.         /// <summary>
  1540.         /// Reads an n bit custom signed integer from the buffer.
  1541.         /// </summary>
  1542.         /// <param name="value">Signed Integer.</param>
  1543.         /// <param name="bits">The number of bits used to write.</param>
  1544.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1545.         /// <returns>false on error.</returns>
  1546.         public bool ReadInt(out long value, int bits, bool nestedRead = false)
  1547.         {
  1548.             if (bits < 1 || bits > 64) throw new ArgumentOutOfRangeException("bits must be in the range (0, 64].");
  1549.  
  1550.             if (!nestedRead) previousBitIndex = bitIndex;
  1551.             value = 0;
  1552.             if ((bitIndex + bits + 7) / 8 > buffer.Count)
  1553.             {
  1554.                 return false;
  1555.             }
  1556.  
  1557.             int offset = bitIndex % 8;
  1558.             value = buffer[bitIndex / 8] << 56 + offset;
  1559.             if (offset + bits > 8)
  1560.             {
  1561.                 value |= (long)buffer[bitIndex / 8 + 1] << 48 + offset;
  1562.                 if (offset + bits > 16)
  1563.                 {
  1564.                     value |= (long)buffer[bitIndex / 8 + 2] << 40 + offset;
  1565.                     if (offset + bits > 24)
  1566.                     {
  1567.                         value |= (long)buffer[bitIndex / 8 + 3] << 32 + offset;
  1568.                         if (offset + bits > 32)
  1569.                         {
  1570.                             value |= (long)buffer[bitIndex / 8 + 4] << 24 + offset;
  1571.                             if (offset + bits > 40)
  1572.                             {
  1573.                                 value |= (long)buffer[bitIndex / 8 + 5] << 16 + offset;
  1574.                                 if (offset + bits > 48)
  1575.                                 {
  1576.                                     value |= (long)buffer[bitIndex / 8 + 6] << 8 + offset;
  1577.                                     if (offset + bits > 56)
  1578.                                     {
  1579.                                         value |= (long)buffer[bitIndex / 8 + 7] << offset;
  1580.                                         if (offset + bits > 64)
  1581.                                         {
  1582.                                             value |= (long)buffer[bitIndex / 8 + 8] >> 8 - offset;
  1583.                                         }
  1584.                                     }
  1585.                                 }
  1586.                             }
  1587.                         }
  1588.                     }
  1589.                 }
  1590.             }
  1591.  
  1592.             value >>= 64 - bits;
  1593.             bitIndex += bits;
  1594.             return true;
  1595.         }
  1596.  
  1597.         /// <summary>
  1598.         /// Reads an n bit custom signed integer from the buffer.
  1599.         /// </summary>
  1600.         /// <param name="value">Signed Integer.</param>
  1601.         /// <param name="min">The minimum value in the range.</param>
  1602.         /// <param name="max">The maximum value in the range.</param>
  1603.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1604.         /// <returns>false on error.</returns>
  1605.         public bool ReadInt(out long value, long min, long max, bool nestedRead = false)
  1606.         {
  1607.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  1608.  
  1609.             if (!nestedRead) previousBitIndex = bitIndex;
  1610.             ulong valueBase = (ulong)(max - min + 1);
  1611.             // Log2
  1612.             var bits = 64;
  1613.             if (valueBase != 0)
  1614.             {
  1615.                 int left = 0;
  1616.                 int right = 64;
  1617.                 for (int i = 0; i < 6; ++i)
  1618.                 {
  1619.                     if (valueBase >> (64 - (left + right) / 2) != 0)
  1620.                     {
  1621.                         // Left
  1622.                         right = (left + right) / 2;
  1623.                     }
  1624.                     else
  1625.                     {
  1626.                         // Right
  1627.                         left = (left + right) / 2;
  1628.                     }
  1629.                 }
  1630.                 bits = 64 - left;
  1631.             }
  1632.             if (!ReadInt(out value, bits, true)) return false;
  1633.             value += min;
  1634.             return true;
  1635.         }
  1636.  
  1637.         /// <summary>
  1638.         /// Reads a 32 bit single from the buffer.
  1639.         /// </summary>
  1640.         /// <param name="value">Single.</param>
  1641.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1642.         /// <returns>false on error.</returns>
  1643.         public bool ReadSingle(out float value, bool nestedRead = false)
  1644.         {
  1645.             if (!nestedRead) previousBitIndex = bitIndex;
  1646.             value = 0;
  1647.             if ((bitIndex + 32 + 7) / 8 > buffer.Count)
  1648.             {
  1649.                 return false;
  1650.             }
  1651.             uint uValue;
  1652.             if (!ReadUInt32(out uValue, true)) return false;
  1653.             value = BitConverter.ToSingle(BitConverter.GetBytes(uValue), 0);
  1654.             return true;
  1655.         }
  1656.  
  1657.         /// <summary>
  1658.         /// Reads a 64 bit double from the buffer.
  1659.         /// </summary>
  1660.         /// <param name="value">Double.</param>
  1661.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1662.         /// <returns>false on error.</returns>
  1663.         public bool ReadDouble(out double value, bool nestedRead = false)
  1664.         {
  1665.             if (!nestedRead) previousBitIndex = bitIndex;
  1666.             value = 0;
  1667.             if ((bitIndex + 64 + 7) / 8 > buffer.Count)
  1668.             {
  1669.                 return false;
  1670.             }
  1671.             byte[] bytes = new byte[8];
  1672.             uint uValue1;
  1673.             if (!ReadUInt32(out uValue1, true)) return false;
  1674.             byte[] first4bytes = BitConverter.GetBytes(uValue1);
  1675.             uint uValue2;
  1676.             if (!ReadUInt32(out uValue2, true)) return false;
  1677.             byte[] last4bytes = BitConverter.GetBytes(uValue2);
  1678.  
  1679.             for (uint copyBytes = 0; copyBytes < 4; ++copyBytes)
  1680.             {
  1681.                 bytes[copyBytes] = first4bytes[copyBytes];
  1682.             }
  1683.             for (uint copyBytes = 4; copyBytes < 8; ++copyBytes)
  1684.             {
  1685.                 bytes[copyBytes] = last4bytes[copyBytes - 4];
  1686.             }
  1687.             value = BitConverter.ToDouble(bytes, 0);
  1688.             return true;
  1689.         }
  1690.  
  1691.         /// <summary>
  1692.         /// Reads the variable-width unsigned integer.
  1693.         /// </summary>
  1694.         /// <param name="value">Unsigned Integer.</param>
  1695.         /// <param name="bits">The bit size of the sequence used to write.</param>
  1696.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1697.         /// <returns>false on error.</returns>
  1698.         public bool ReadVariableWidthUInt(out uint value, int bits = 4, bool nestedRead = false)
  1699.         {
  1700.             if (bits < 1 || bits > 32) throw new ArgumentOutOfRangeException("bits must be in the range (0, 32].");
  1701.  
  1702.             if (!nestedRead) previousBitIndex = bitIndex;
  1703.             value = 0;
  1704.             int valueBitCount = bits;
  1705.             bool continuationBitValue = true;
  1706.             do
  1707.             {
  1708.                 if (!ReadBool(out continuationBitValue, true)) return false;
  1709.                 if (continuationBitValue) valueBitCount += bits;
  1710.             }
  1711.             while (continuationBitValue && valueBitCount < 32);
  1712.             return ReadUInt(out value, valueBitCount >= 32 ? 32 : valueBitCount, true);
  1713.         }
  1714.  
  1715.         /// <summary>
  1716.         /// Reads the variable-width signed integer.
  1717.         /// </summary>
  1718.         /// <param name="value">Signed Integer.</param>
  1719.         /// <param name="bits">The bit size of the sequence used to write.</param>
  1720.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1721.         /// <returns>false on error.</returns>
  1722.         public bool ReadVariableWidthInt(out int value, int bits = 4, bool nestedRead = false)
  1723.         {
  1724.             if (bits < 1 || bits > 32) throw new ArgumentOutOfRangeException("bits must be in the range (0, 32].");
  1725.  
  1726.             if (!nestedRead) previousBitIndex = bitIndex;
  1727.             value = 0;
  1728.             int valueBitCount = bits;
  1729.             bool continuationBitValue = true;
  1730.             do
  1731.             {
  1732.                 if (!ReadBool(out continuationBitValue, true)) return false;
  1733.                 if (continuationBitValue) valueBitCount += bits;
  1734.             }
  1735.             while (continuationBitValue && valueBitCount < 32);
  1736.             return ReadInt(out value, valueBitCount >= 32 ? 32 : valueBitCount, true);
  1737.         }
  1738.  
  1739.         /// <summary>
  1740.         /// Reads an array of unsigned integers in the given range defining a base.
  1741.         /// </summary>
  1742.         /// <param name="value">Array of Unsigned Integers.</param>
  1743.         /// <param name="min">The minimum value in the range.</param>
  1744.         /// <param name="max">The maximum value in the range.</param>
  1745.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1746.         /// <returns>false on error.</returns>
  1747.         public bool ReadArrayUInt32(uint[] value, int min, int max, bool nestedRead = false)
  1748.         {
  1749.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  1750.             if (!nestedRead) previousBitIndex = bitIndex;
  1751.             ulong valueBase = (ulong)(max - min + 1);
  1752.             var exponent = value.Length;
  1753.  
  1754.             var powResult = new List<ulong>();
  1755.             powResult.Add(1);
  1756.             var newPowResult = new List<ulong>();
  1757.  
  1758.             var valueBaseTemp = new List<ulong>();
  1759.             valueBaseTemp.Add(valueBase);
  1760.             var newValueBaseTemp = new List<ulong>();
  1761.  
  1762.             // Pow(value.size, arrayBase) using exponentiation by squaring
  1763.             while (exponent != 0)
  1764.             {
  1765.                 if (exponent % 2 != 0)
  1766.                 {
  1767.                     // powResult *= valueBaseTemp;
  1768.                     newPowResult.Clear();
  1769.                     for (int i = 0; i < valueBaseTemp.Count; ++i)
  1770.                     {
  1771.                         ulong carry = 0;
  1772.                         for (var j = 0; j < powResult.Count || carry != 0; ++j)
  1773.                         {
  1774.                             if (i + j >= newPowResult.Count)
  1775.                             {
  1776.                                 newPowResult.Add(0);
  1777.                             }
  1778.                             ulong product = 0;
  1779.                             if (j < powResult.Count)
  1780.                             {
  1781.                                 product = valueBaseTemp[i] * powResult[j];
  1782.                             }
  1783.                             newPowResult[j + i] += product + carry;
  1784.                             carry = newPowResult[j + i] >> 32;
  1785.                             newPowResult[j + i] &= 0xFFFFFFFF;
  1786.                         }
  1787.                     }
  1788.                     powResult.Clear();
  1789.                     powResult.InsertRange(0, newPowResult);
  1790.                     exponent--;
  1791.                 }
  1792.                 // valueBaseTemp *= valueBaseTemp;
  1793.                 newValueBaseTemp.Clear();
  1794.                 for (int i = 0; i < valueBaseTemp.Count; ++i)
  1795.                 {
  1796.                     ulong carry = 0;
  1797.                     for (var j = 0; j < valueBaseTemp.Count || carry != 0; ++j)
  1798.                     {
  1799.                         if (i + j >= newValueBaseTemp.Count)
  1800.                         {
  1801.                             newValueBaseTemp.Add(0);
  1802.                         }
  1803.                         ulong product = 0;
  1804.                         if (j < valueBaseTemp.Count)
  1805.                         {
  1806.                             product = valueBaseTemp[i] * valueBaseTemp[j];
  1807.                         }
  1808.                         newValueBaseTemp[j + i] += product + carry;
  1809.                         carry = newValueBaseTemp[j + i] >> 32;
  1810.                         newValueBaseTemp[j + i] &= 0xFFFFFFFF;
  1811.                     }
  1812.                 }
  1813.                 valueBaseTemp.Clear();
  1814.                 valueBaseTemp.InsertRange(0, newValueBaseTemp);
  1815.  
  1816.                 exponent /= 2;
  1817.             }
  1818.  
  1819.             for (var i = powResult.Count - 1; i > 0 && powResult[i] == 0; --i)
  1820.             {
  1821.                 powResult.RemoveAt(powResult.Count - 1);
  1822.             }
  1823.  
  1824.             // Log2
  1825.             int numberOfBits = 0;
  1826.             if (!(powResult.Count == 1 && powResult[0] == 0))
  1827.             {
  1828.                 int left = 0;
  1829.                 int right = 32;
  1830.                 for (int i = 0; i < 5; ++i)
  1831.                 {
  1832.                     if (powResult[powResult.Count - 1] >> (32 - (left + right) / 2) != 0)
  1833.                     {
  1834.                         // Left
  1835.                         right = (left + right) / 2;
  1836.                     }
  1837.                     else
  1838.                     {
  1839.                         // Right
  1840.                         left = (left + right) / 2;
  1841.                     }
  1842.                 }
  1843.                 numberOfBits = (powResult.Count - 1) * 32 + (32 - left);
  1844.             }
  1845.  
  1846.             var sum = new List<ulong>((numberOfBits + 31) / 32);
  1847.             for (int copyBits = 0; copyBits < numberOfBits; copyBits += 32)
  1848.             {
  1849.                 uint uValue;
  1850.                 if (!ReadUInt(out uValue, (int)numberOfBits - copyBits > 32 ? 32 : (int)numberOfBits - copyBits, true)) return false;
  1851.                 sum.Add(uValue);
  1852.             }
  1853.  
  1854.             for (var i = 0; i < value.Length; ++i)
  1855.             {
  1856.                 ulong remainder = 0;
  1857.                 for (var j = sum.Count - 1; j >= 0; --j)
  1858.                 {
  1859.                     sum[j] += remainder << 32;
  1860.                     remainder = sum[j] % valueBase;
  1861.                     sum[j] /= valueBase;
  1862.                 }
  1863.                 value[i] = (uint)remainder + (uint)min;
  1864.             }
  1865.             return true;
  1866.         }
  1867.  
  1868.         /// <summary>
  1869.         /// Reads an array of signed integers in the given range defining a base.
  1870.         /// </summary>
  1871.         /// <param name="value">Array of Signed Integers.</param>
  1872.         /// <param name="min">The minimum value in the range.</param>
  1873.         /// <param name="max">The maximum value in the range.</param>
  1874.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1875.         /// <returns>false on error.</returns>
  1876.         public bool ReadArrayInt32(int[] value, int min, int max, bool nestedRead = false)
  1877.         {
  1878.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  1879.             if (!nestedRead) previousBitIndex = bitIndex;
  1880.  
  1881.             var uValue = new uint[value.Length];
  1882.             if (!ReadArrayUInt32(uValue, min, max, true)) return false;
  1883.  
  1884.             for (var i = 0; i < value.Length; ++i)
  1885.             {
  1886.                 value[i] = (int)uValue[i] + min;
  1887.             }
  1888.             return true;
  1889.         }
  1890.  
  1891.         /// <summary>
  1892.         /// Reads an array of singles in the given range converted to the given base.
  1893.         /// </summary>
  1894.         /// <param name="value">Array of Singles.</param>
  1895.         /// <param name="min">The minimum value in the range.</param>
  1896.         /// <param name="max">The maximum value in the range.</param>
  1897.         /// <param name="valueBase">The base to convert the singles to.</param>
  1898.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1899.         /// <returns>false on error.</returns>
  1900.         public bool ReadArraySingle(float[] value, float min, float max, int valueBase, bool nestedRead = false)
  1901.         {
  1902.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  1903.             if (!nestedRead) previousBitIndex = bitIndex;
  1904.             var uValue = new uint[value.Length];
  1905.             if (!ReadArrayUInt32(uValue, 0, valueBase - 1, true)) return false;
  1906.             if (min < 0 && max > 0)
  1907.             {
  1908.                 for (var i = 0; i < value.Length; ++i)
  1909.                 {
  1910.                     value[i] = uValue[i] == 0 ? 0 : (uValue[i] - 1) / (float)(valueBase - 2) * (max - min) + min;
  1911.                 }
  1912.             }
  1913.             else
  1914.             {
  1915.                 for (var i = 0; i < value.Length; ++i)
  1916.                 {
  1917.                     value[i] = uValue[i] / (float)(valueBase - 1) * (max - min) + min;
  1918.                 }
  1919.             }
  1920.             return true;
  1921.         }
  1922.  
  1923.         /// <summary>
  1924.         /// Reads an array of doubles in the given range converted to the given base.
  1925.         /// </summary>
  1926.         /// <param name="value">Array of Doubles.</param>
  1927.         /// <param name="min">The minimum value in the range.</param>
  1928.         /// <param name="max">The maximum value in the range.</param>
  1929.         /// <param name="valueBase">The base to convert the doubles to.</param>
  1930.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1931.         /// <returns>false on error.</returns>
  1932.         public bool ReadArrayDouble(double[] value, double min, double max, int valueBase, bool nestedRead = false)
  1933.         {
  1934.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  1935.             if (!nestedRead) previousBitIndex = bitIndex;
  1936.             var uValue = new uint[value.Length];
  1937.             if (!ReadArrayUInt32(uValue, 0, valueBase - 1, true)) return false;
  1938.             if (min < 0 && max > 0)
  1939.             {
  1940.                 for (var i = 0; i < value.Length; ++i)
  1941.                 {
  1942.                     value[i] = uValue[i] == 0 ? 0 : (uValue[i] - 1) / (double)(valueBase - 2) * (max - min) + min;
  1943.                 }
  1944.             }
  1945.             else
  1946.             {
  1947.                 for (var i = 0; i < value.Length; ++i)
  1948.                 {
  1949.                     value[i] = uValue[i] / (double)(valueBase - 1) * (max - min) + min;
  1950.                 }
  1951.             }
  1952.             return true;
  1953.         }
  1954.  
  1955.         /// <summary>
  1956.         /// Reads a custom resolution single.
  1957.         /// </summary>
  1958.         /// <param name="value">Single.</param>
  1959.         /// <param name="min">The minimum value in the range.</param>
  1960.         /// <param name="max">The maximum value in the range.</param>
  1961.         /// <param name="bitResolution">The number of bits written for the ratio.</param>
  1962.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1963.         /// <returns>false on error.</returns>
  1964.         public bool ReadCustomResolutionSingle(out float value, float min, float max, int bitResolution, bool nestedRead = false)
  1965.         {
  1966.             if (bitResolution < 1 || bitResolution > 31) throw new ArgumentOutOfRangeException("bitResolution must be in the range (0, 32).");
  1967.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  1968.  
  1969.             if (!nestedRead) previousBitIndex = bitIndex;
  1970.             value = 0;
  1971.             uint uValue;
  1972.             if (!ReadUInt(out uValue, bitResolution, true)) return false;
  1973.             if (min < 0 && max > 0)
  1974.             {
  1975.                 value = uValue == 0 ? 0 : (uValue - 1) / (float)((0x1 << bitResolution) - 2) * (max - min) + min;
  1976.             }
  1977.             else
  1978.             {
  1979.                 value = uValue / (float)((0x1 << bitResolution) - 1) * (max - min) + min;
  1980.             }
  1981.             return true;
  1982.         }
  1983.  
  1984.         /// <summary>
  1985.         /// Reads a custom resolution double.
  1986.         /// </summary>
  1987.         /// <param name="value">Double.</param>
  1988.         /// <param name="min">The minimum value in the range.</param>
  1989.         /// <param name="max">The maximum value in the range.</param>
  1990.         /// <param name="bitResolution">The number of bits written for the ratio.</param>
  1991.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  1992.         /// <returns>false on error.</returns>
  1993.         public bool ReadCustomResolutionDouble(out double value, double min, double max, int bitResolution, bool nestedRead = false)
  1994.         {
  1995.             if (bitResolution < 1 || bitResolution > 31) throw new ArgumentOutOfRangeException("bitResolution must be in the range (0, 32).");
  1996.             if (max <= min) throw new ArgumentOutOfRangeException("max must be greater than min.");
  1997.  
  1998.             if (!nestedRead) previousBitIndex = bitIndex;
  1999.             value = 0;
  2000.             uint uValue;
  2001.             if (!ReadUInt(out uValue, bitResolution, true)) return false;
  2002.             if (min < 0 && max > 0)
  2003.             {
  2004.                 value = uValue == 0 ? 0 : (uValue - 1) / (double)((0x1 << bitResolution) - 2) * (max - min) + min;
  2005.             }
  2006.             else
  2007.             {
  2008.                 value = uValue / (double)((0x1 << bitResolution) - 1) * (max - min) + min;
  2009.             }
  2010.             return true;
  2011.         }
  2012.  
  2013.         /// <summary>
  2014.         /// Reads a string.
  2015.         /// </summary>
  2016.         /// <param name="value">String.</param>
  2017.         /// <param name="bits">The number of bits for the length written using an unsigned variable-width integer.</param>
  2018.         /// <param name="limit">The maximum number of characters to read.</param>
  2019.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  2020.         /// <returns>false on error.</returns>
  2021.         public bool ReadString(out string value, int bits = 4, int limit = 255, bool nestedRead = false)
  2022.         {
  2023.             if (bits < 1 || bits > 31) throw new ArgumentOutOfRangeException("bits must be in the range (0, 32).");
  2024.  
  2025.             if (!nestedRead) previousBitIndex = bitIndex;
  2026.             value = string.Empty;
  2027.             uint size;
  2028.             if (!ReadVariableWidthUInt(out size, bits, true)) return false;
  2029.             for (var asciiByteItr = 0; asciiByteItr < size; ++asciiByteItr)
  2030.             {
  2031.                 byte asciiByte;
  2032.                 if (!ReadByte(out asciiByte, true)) return false;
  2033.                 value += asciiByte;
  2034.                 if (value.Length > limit) return false;
  2035.             }
  2036.             return true;
  2037.         }
  2038.  
  2039.         /// <summary>
  2040.         /// Reads a binary packet that has been written to the buffer.
  2041.         /// </summary>
  2042.         /// <param name="value">A binary packet.</param>
  2043.         /// <param name="nestedRead">If false then the previous bit index is stored so the operation can be rolled back.</param>
  2044.         /// <returns>false on error.</returns>
  2045.         public bool ReadBinaryPacket(out Packet value, bool nestedRead = false)
  2046.         {
  2047.             value = new Packet();
  2048.             if (!nestedRead) previousBitIndex = bitIndex;
  2049.             uint valueMaxBitIndex;
  2050.             if (!ReadVariableWidthUInt(out valueMaxBitIndex, nestedRead: true)) return false;
  2051.             for (int copyBits = 0; copyBits < valueMaxBitIndex; copyBits += 32)
  2052.             {
  2053.                 uint uValue;
  2054.                 if (!ReadUInt(out uValue, (int)valueMaxBitIndex - copyBits > 32 ? 32 : (int)valueMaxBitIndex - copyBits, true)) return false;
  2055.                 value.WriteUInt(uValue, (int)valueMaxBitIndex - copyBits > 32 ? 32 : (int)valueMaxBitIndex - copyBits);
  2056.             }
  2057.             bitIndex += (int)valueMaxBitIndex;
  2058.             value.bitIndex = 0;
  2059.             return true;
  2060.         }
  2061.  
  2062.     }
  2063. }
Add Comment
Please, Sign In to add comment