BufferHelper.cs 5.1 KB

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