123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #if (!UNITY_WEBGL || UNITY_EDITOR) && !BESTHTTP_DISABLE_ALTERNATE_SSL && !BESTHTTP_DISABLE_HTTP2
- using System;
- namespace BestHTTP.Connections.HTTP2
- {
- public static class BufferHelper
- {
- public static void SetUInt16(byte[] buffer, int offset, UInt16 value)
- {
- buffer[offset + 1] = (byte)(value);
- buffer[offset + 0] = (byte)(value >> 8);
- }
- public static void SetUInt24(byte[] buffer, int offset, UInt32 value)
- {
- buffer[offset + 2] = (byte)(value);
- buffer[offset + 1] = (byte)(value >> 8);
- buffer[offset + 0] = (byte)(value >> 16);
- }
- public static void SetUInt31(byte[] buffer, int offset, UInt32 value)
- {
- buffer[offset + 3] = (byte)(value);
- buffer[offset + 2] = (byte)(value >> 8);
- buffer[offset + 1] = (byte)(value >> 16);
- buffer[offset + 0] = (byte)((value >> 24) & 0x7F);
- }
- public static void SetUInt32(byte[] buffer, int offset, UInt32 value)
- {
- buffer[offset + 3] = (byte)(value);
- buffer[offset + 2] = (byte)(value >> 8);
- buffer[offset + 1] = (byte)(value >> 16);
- buffer[offset + 0] = (byte)(value >> 24);
- }
- public static void SetLong(byte[] buffer, int offset, long value)
- {
- buffer[offset + 7] = (byte)(value);
- buffer[offset + 6] = (byte)(value >> 8);
- buffer[offset + 5] = (byte)(value >> 16);
- buffer[offset + 4] = (byte)(value >> 24);
- buffer[offset + 3] = (byte)(value >> 32);
- buffer[offset + 2] = (byte)(value >> 40);
- buffer[offset + 1] = (byte)(value >> 48);
- buffer[offset + 0] = (byte)(value >> 56);
- }
- /// <summary>
- /// bitIdx: 01234567
- /// </summary>
- public static byte SetBit(byte value, byte bitIdx, bool bitValue)
- {
- return SetBit(value, bitIdx, Convert.ToByte(bitValue));
- }
- /// <summary>
- /// bitIdx: 01234567
- /// </summary>
- public static byte SetBit(byte value, byte bitIdx, byte bitValue)
- {
- //byte mask = (byte)(0x80 >> bitIdx);
- return (byte)((value ^ (value & (0x80 >> bitIdx))) | bitValue << (7 - bitIdx));
- }
- /// <summary>
- /// bitIdx: 01234567
- /// </summary>
- public static byte ReadBit(byte value, byte bitIdx)
- {
- byte mask = (byte)(0x80 >> bitIdx);
- return (byte)((value & mask) >> (7 - bitIdx));
- }
- /// <summary>
- /// bitIdx: 01234567
- /// </summary>
- public static byte ReadValue(byte value, byte fromBit, byte toBit)
- {
- byte result = 0;
- short idx = toBit;
- while (idx >= fromBit)
- {
- result += (byte)(ReadBit(value, (byte)idx) << (toBit - idx));
- idx--;
- }
- return result;
- }
- public static UInt16 ReadUInt16(byte[] buffer, int offset)
- {
- return (UInt16)(buffer[offset + 1] | buffer[offset] << 8);
- }
- public static UInt32 ReadUInt24(byte[] buffer, int offset)
- {
- return (UInt32)(buffer[offset + 2] |
- buffer[offset + 1] << 8 |
- buffer[offset + 0] << 16
- );
- }
- public static UInt32 ReadUInt31(byte[] buffer, int offset)
- {
- return (UInt32)(buffer[offset + 3] |
- buffer[offset + 2] << 8 |
- buffer[offset + 1] << 16 |
- (buffer[offset] & 0x7F) << 24
- );
- }
- public static UInt32 ReadUInt32(byte[] buffer, int offset)
- {
- return (UInt32)(buffer[offset + 3] |
- buffer[offset + 2] << 8 |
- buffer[offset + 1] << 16 |
- buffer[offset + 0] << 24
- );
- }
- public static long ReadLong(byte[] buffer, int offset)
- {
- return (long)buffer[offset + 7] |
- (long)buffer[offset + 6] << 8 |
- (long)buffer[offset + 5] << 16 |
- (long)buffer[offset + 4] << 24 |
- (long)buffer[offset + 3] << 32 |
- (long)buffer[offset + 2] << 40 |
- (long)buffer[offset + 1] << 48 |
- (long)buffer[offset + 0] << 56;
- }
- }
- }
- #endif
|