| | | 1 | | using System; |
| | | 2 | | using System.Text; |
| | | 3 | | using Itinero.Network.Storage; |
| | | 4 | | |
| | | 5 | | namespace Itinero.IO; |
| | | 6 | | |
| | | 7 | | internal static class BitCoderBuffer |
| | | 8 | | { |
| | | 9 | | public static uint GetVarUInt32(byte[] data, ref int offset) |
| | 0 | 10 | | { |
| | 0 | 11 | | var size = data.GetDynamicUInt32(offset, out var value); |
| | 0 | 12 | | offset += size; |
| | 0 | 13 | | return value; |
| | 0 | 14 | | } |
| | | 15 | | |
| | | 16 | | public static uint? GetVarUInt32Nullable(byte[] data, ref int offset) |
| | 0 | 17 | | { |
| | 0 | 18 | | var size = data.GetDynamicUInt32Nullable(offset, out var value); |
| | 0 | 19 | | offset += size; |
| | 0 | 20 | | return value; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public static int GetVarInt32(byte[] data, ref int offset) |
| | 0 | 24 | | { |
| | 0 | 25 | | return FromUnsigned(GetVarUInt32(data, ref offset)); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public static int? GetVarInt32Nullable(byte[] data, ref int offset) |
| | 0 | 29 | | { |
| | 0 | 30 | | return FromUnsigned(GetVarUInt32Nullable(data, ref offset)); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public static Guid GetGuid(byte[] data, ref int offset) |
| | 0 | 34 | | { |
| | 0 | 35 | | data.GetGuid(offset, out var value); |
| | 0 | 36 | | offset += 16; |
| | 0 | 37 | | return value; |
| | 0 | 38 | | } |
| | | 39 | | |
| | | 40 | | public static long GetInt64(byte[] data, ref int offset) |
| | 0 | 41 | | { |
| | 0 | 42 | | long value = 0; |
| | 0 | 43 | | for (var b = 0; b < 8; b++) |
| | 0 | 44 | | { |
| | 0 | 45 | | value += (long)data[offset + b] << (b * 8); |
| | 0 | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | offset += 8; |
| | 0 | 49 | | return value; |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | public static string GetWithSizeString(byte[] data, ref int offset) |
| | 0 | 53 | | { |
| | 0 | 54 | | var size = GetInt64(data, ref offset); |
| | 0 | 55 | | var str = Encoding.Unicode.GetString(data, offset, (int)size); |
| | 0 | 56 | | offset += (int)size; |
| | 0 | 57 | | return str; |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | public static void SetVarUInt32(byte[] data, ref int offset, uint value) |
| | 0 | 61 | | { |
| | 0 | 62 | | offset += data.SetDynamicUInt32(offset, value); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | public static void SetVarUInt32Nullable(byte[] data, ref int offset, uint? value) |
| | 0 | 66 | | { |
| | 0 | 67 | | offset += data.SetDynamicUInt32Nullable(offset, value); |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | public static void SetVarInt32(byte[] data, ref int offset, int value) |
| | 0 | 71 | | { |
| | 0 | 72 | | SetVarUInt32(data, ref offset, ToUnsigned(value)); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | public static void SetVarInt32Nullable(byte[] data, ref int offset, int? value) |
| | 0 | 76 | | { |
| | 0 | 77 | | SetVarUInt32Nullable(data, ref offset, ToUnsigned(value)); |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | public static void SetGuid(byte[] data, ref int offset, Guid value) |
| | 0 | 81 | | { |
| | 0 | 82 | | data.SetGuid(offset, value); |
| | 0 | 83 | | offset += 16; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | public static void SetInt64(byte[] data, ref int offset, long value) |
| | 0 | 87 | | { |
| | 0 | 88 | | for (var b = 0; b < 8; b++) |
| | 0 | 89 | | { |
| | 0 | 90 | | data[offset + b] = (byte)(value & byte.MaxValue); |
| | 0 | 91 | | value >>= 8; |
| | 0 | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | offset += 8; |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | public static void SetWithSizeString(byte[] data, ref int offset, string value) |
| | 0 | 98 | | { |
| | 0 | 99 | | var bytes = Encoding.Unicode.GetBytes(value); |
| | 0 | 100 | | SetInt64(data, ref offset, bytes.Length); |
| | 0 | 101 | | Buffer.BlockCopy(bytes, 0, data, offset, bytes.Length); |
| | 0 | 102 | | offset += bytes.Length; |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | private static uint ToUnsigned(int value) |
| | 0 | 106 | | { |
| | 0 | 107 | | var unsigned = (uint)value; |
| | 0 | 108 | | if (value < 0) |
| | 0 | 109 | | { |
| | 0 | 110 | | unsigned = (uint)-value; |
| | 0 | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | unsigned <<= 1; |
| | 0 | 114 | | if (value < 0) |
| | 0 | 115 | | { |
| | 0 | 116 | | unsigned += 1; |
| | 0 | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | return unsigned; |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | private static uint? ToUnsigned(int? value) |
| | 0 | 123 | | { |
| | 0 | 124 | | if (value == null) return null; |
| | 0 | 125 | | return ToUnsigned(value.Value); |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | private static int FromUnsigned(uint unsigned) |
| | 0 | 129 | | { |
| | 0 | 130 | | var sign = unsigned & 1; |
| | 0 | 131 | | var value = (int)(unsigned >> 1); |
| | 0 | 132 | | if (sign == 1) |
| | 0 | 133 | | { |
| | 0 | 134 | | value = -value; |
| | 0 | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | return value; |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | private static int? FromUnsigned(uint? unsigned) |
| | 0 | 141 | | { |
| | 0 | 142 | | if (unsigned == null) return null; |
| | 0 | 143 | | return FromUnsigned(unsigned.Value); |
| | 0 | 144 | | } |
| | | 145 | | } |