-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgobuf.cs
executable file
·177 lines (151 loc) · 4.26 KB
/
gobuf.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Runtime.InteropServices;
class Gobuf
{
public static ushort ReadUint16(byte[] b, ref int offset) {
return (ushort)(
(ushort)(b[offset++]) |
(ushort)(b[offset++]) << 8
);
}
public static uint ReadUint32(byte[] b, ref int offset) {
return (uint)(b[offset++]) |
(uint)(b[offset++]) << 8 |
(uint)(b[offset++]) << 16 |
(uint)(b[offset++]) << 24;
}
public static ulong ReadUint64(byte[] b, ref int offset) {
return (ulong)(b[offset++]) |
(ulong)(b[offset++]) << 8 |
(ulong)(b[offset++]) << 16 |
(ulong)(b[offset++]) << 24 |
(ulong)(b[offset++]) << 32 |
(ulong)(b[offset++]) << 40 |
(ulong)(b[offset++]) << 48 |
(ulong)(b[offset++]) << 56;
}
public static float ReadFloat32(byte[] b, ref int offset) {
return Int32BitsToFloat(ReadUint32(b, ref offset));
}
public static double ReadFloat64(byte[] b, ref int offset) {
return BitConverter.Int64BitsToDouble((long)ReadUint64(b, ref offset));
}
public static byte[] ReadBytes(byte[] b, ref int offset) {
var length = (int)ReadUvarint(b, ref offset);
var data = new byte[length];
Buffer.BlockCopy(b, offset, data, 0, length);
offset += length;
return data;
}
public static string ReadString(byte[] b, ref int offset) {
return System.Text.Encoding.UTF8.GetString(ReadBytes(b, ref offset));
}
public static void WriteUint16(ushort v, byte[] b, ref int offset) {
b[offset++] = (byte)(v);
b[offset++] = (byte)(v >> 8);
}
public static void WriteUint32(uint v, byte[] b, ref int offset) {
b[offset++] = (byte)(v);
b[offset++] = (byte)(v >> 8);
b[offset++] = (byte)(v >> 16);
b[offset++] = (byte)(v >> 24);
}
public static void WriteUint64(ulong v, byte[] b, ref int offset) {
b[offset++] = (byte)(v);
b[offset++] = (byte)(v >> 8);
b[offset++] = (byte)(v >> 16);
b[offset++] = (byte)(v >> 24);
b[offset++] = (byte)(v >> 32);
b[offset++] = (byte)(v >> 40);
b[offset++] = (byte)(v >> 48);
b[offset++] = (byte)(v >> 56);
}
public static void WriteFloat32(float v, byte[] b, ref int offset) {
WriteUint32((uint)FloatToInt32Bits(v), b, ref offset);
}
public static void WriteFloat64(double v, byte[] b, ref int offset) {
WriteUint64((ulong)BitConverter.DoubleToInt64Bits(v), b, ref offset);
}
public static void WriteBytes(byte[] data, byte[] b, ref int offset) {
WriteUvarint((ulong)data.Length, b, ref offset);
Buffer.BlockCopy(data, 0, b, offset, data.Length);
offset += data.Length;
}
public static void WriteString(string str, byte[] b, ref int offset) {
WriteBytes(System.Text.Encoding.UTF8.GetBytes(str), b, ref offset);
}
private static uint FloatToInt32Bits(float f)
{
var bits = default(FloatUnion);
bits.FloatData = f;
return bits.IntData;
}
private static float Int32BitsToFloat(uint i)
{
var bits = default(FloatUnion);
bits.IntData = i;
return bits.FloatData;
}
[StructLayout(LayoutKind.Explicit)]
private struct FloatUnion
{
[FieldOffset(0)]
public uint IntData;
[FieldOffset(0)]
public float FloatData;
}
public static ulong ReadUvarint(byte[] b, ref int offset) {
ulong value = 0;
for (int i = 0, s = 0; ;i++, s += 7) {
var x = b[offset++];
if (x < 0x80) {
if (i > 9 || i == 9 && x > 1) {
value = 0;
throw new Exception("uvarint overflow");
}
value |= (ulong)x << s;
break;
}
value |= (ulong)(x & 0x7f) << s;
}
return value;
}
public static void WriteUvarint(ulong v, byte[] b, ref int offset) {
while (v >= 0x80) {
b[offset ++] = (byte)(v | 0x80);
v >>= 7;
}
b[offset ++] = (byte)v;
}
public static int UvarintSize(ulong v) {
int i = 0;
while (v >= 0x80) {
i ++;
v >>= 7;
}
return i + 1;
}
public static long ReadVarint(byte[] b, ref int offset) {
ulong ux = ReadUvarint(b, ref offset);
long value = (long)(ux >> 1);
if ((ux & 1) != 0)
value ^= value;
return value;
}
public static void WriteVarint(long v, byte[] b, ref int offset) {
var ux = (ulong)v << 1;
if (v < 0)
ux ^= ux;
WriteUvarint(ux, b, ref offset);
}
public static int VarintSize(long v) {
var ux = (ulong)v << 1;
if (v < 0)
ux ^= ux;
return UvarintSize(ux);
}
public static int StringSize(string str) {
var count = System.Text.Encoding.UTF8.GetByteCount(str);
return UvarintSize((ulong)count) + count;
}
}