BufferHelper.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_ALTERNATE_SSL && !BESTHTTP_DISABLE_HTTP2
  2. using System;
  3. namespace BestHTTP.Connections.HTTP2
  4. {
  5. public static class BufferHelper
  6. {
  7. public static void SetUInt16(byte[] buffer, int offset, UInt16 value)
  8. {
  9. buffer[offset + 1] = (byte)(value);
  10. buffer[offset + 0] = (byte)(value >> 8);
  11. }
  12. public static void SetUInt24(byte[] buffer, int offset, UInt32 value)
  13. {
  14. buffer[offset + 2] = (byte)(value);
  15. buffer[offset + 1] = (byte)(value >> 8);
  16. buffer[offset + 0] = (byte)(value >> 16);
  17. }
  18. public static void SetUInt31(byte[] buffer, int offset, UInt32 value)
  19. {
  20. buffer[offset + 3] = (byte)(value);
  21. buffer[offset + 2] = (byte)(value >> 8);
  22. buffer[offset + 1] = (byte)(value >> 16);
  23. buffer[offset + 0] = (byte)((value >> 24) & 0x7F);
  24. }
  25. public static void SetUInt32(byte[] buffer, int offset, UInt32 value)
  26. {
  27. buffer[offset + 3] = (byte)(value);
  28. buffer[offset + 2] = (byte)(value >> 8);
  29. buffer[offset + 1] = (byte)(value >> 16);
  30. buffer[offset + 0] = (byte)(value >> 24);
  31. }
  32. public static void SetLong(byte[] buffer, int offset, long value)
  33. {
  34. buffer[offset + 7] = (byte)(value);
  35. buffer[offset + 6] = (byte)(value >> 8);
  36. buffer[offset + 5] = (byte)(value >> 16);
  37. buffer[offset + 4] = (byte)(value >> 24);
  38. buffer[offset + 3] = (byte)(value >> 32);
  39. buffer[offset + 2] = (byte)(value >> 40);
  40. buffer[offset + 1] = (byte)(value >> 48);
  41. buffer[offset + 0] = (byte)(value >> 56);
  42. }
  43. /// <summary>
  44. /// bitIdx: 01234567
  45. /// </summary>
  46. public static byte SetBit(byte value, byte bitIdx, bool bitValue)
  47. {
  48. return SetBit(value, bitIdx, Convert.ToByte(bitValue));
  49. }
  50. /// <summary>
  51. /// bitIdx: 01234567
  52. /// </summary>
  53. public static byte SetBit(byte value, byte bitIdx, byte bitValue)
  54. {
  55. //byte mask = (byte)(0x80 >> bitIdx);
  56. return (byte)((value ^ (value & (0x80 >> bitIdx))) | bitValue << (7 - bitIdx));
  57. }
  58. /// <summary>
  59. /// bitIdx: 01234567
  60. /// </summary>
  61. public static byte ReadBit(byte value, byte bitIdx)
  62. {
  63. byte mask = (byte)(0x80 >> bitIdx);
  64. return (byte)((value & mask) >> (7 - bitIdx));
  65. }
  66. /// <summary>
  67. /// bitIdx: 01234567
  68. /// </summary>
  69. public static byte ReadValue(byte value, byte fromBit, byte toBit)
  70. {
  71. byte result = 0;
  72. short idx = toBit;
  73. while (idx >= fromBit)
  74. {
  75. result += (byte)(ReadBit(value, (byte)idx) << (toBit - idx));
  76. idx--;
  77. }
  78. return result;
  79. }
  80. public static UInt16 ReadUInt16(byte[] buffer, int offset)
  81. {
  82. return (UInt16)(buffer[offset + 1] | buffer[offset] << 8);
  83. }
  84. public static UInt32 ReadUInt24(byte[] buffer, int offset)
  85. {
  86. return (UInt32)(buffer[offset + 2] |
  87. buffer[offset + 1] << 8 |
  88. buffer[offset + 0] << 16
  89. );
  90. }
  91. public static UInt32 ReadUInt31(byte[] buffer, int offset)
  92. {
  93. return (UInt32)(buffer[offset + 3] |
  94. buffer[offset + 2] << 8 |
  95. buffer[offset + 1] << 16 |
  96. (buffer[offset] & 0x7F) << 24
  97. );
  98. }
  99. public static UInt32 ReadUInt32(byte[] buffer, int offset)
  100. {
  101. return (UInt32)(buffer[offset + 3] |
  102. buffer[offset + 2] << 8 |
  103. buffer[offset + 1] << 16 |
  104. buffer[offset + 0] << 24
  105. );
  106. }
  107. public static long ReadLong(byte[] buffer, int offset)
  108. {
  109. return (long)buffer[offset + 7] |
  110. (long)buffer[offset + 6] << 8 |
  111. (long)buffer[offset + 5] << 16 |
  112. (long)buffer[offset + 4] << 24 |
  113. (long)buffer[offset + 3] << 32 |
  114. (long)buffer[offset + 2] << 40 |
  115. (long)buffer[offset + 1] << 48 |
  116. (long)buffer[offset + 0] << 56;
  117. }
  118. }
  119. }
  120. #endif