-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathVarEncodingHelper.cs
More file actions
461 lines (410 loc) · 19.8 KB
/
VarEncodingHelper.cs
File metadata and controls
461 lines (410 loc) · 19.8 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// <copyright file="VarEncodingHelper.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
#nullable enable
using System;
using System.IO;
namespace Datadog.Trace.DataStreamsMonitoring;
internal static class VarEncodingHelper
{
/// <summary>
/// The maximum number of bytes used to encode a ulong
/// </summary>
private const int MaxVarLen64 = 9;
/// <summary>
/// Stores the number of bytes a <c>long</c> with a
/// given number of leading zeros will be encoded as
/// </summary>
private static readonly int[] VarLongLengths;
static VarEncodingHelper()
{
VarLongLengths = new int[65];
VarLongLengths[0] = MaxVarLen64; // special case (extra bit for encoding)
VarLongLengths[64] = 1; // special case (always need at least 1 byte)
for (var i = 1; i < 64; i++)
{
var value = (70 - i) / 7;
VarLongLengths[i] = value;
}
}
private interface IByteWriter
{
void WriteByte(int offset, byte value);
}
/// <summary>
/// Serializes 64-bit unsigned integers 7 bits at a time,
/// starting with the least significant bits. The most significant bit in each
/// output byte is the continuation bit and indicates whether there are
/// additional non-zero bits encoded in following bytes. There are at most 9
/// output bytes and the last one does not have a continuation bit, allowing for
/// it to encode 8 bits (8*7+8 = 64).
/// </summary>
/// <param name="bytes">The byte array to write the buffer into</param>
/// <param name="offset">The offset within the array to start writing</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarLong(byte[] bytes, int offset, ulong value)
=> WriteVarLong(value, new ByteArrayByteWriter(bytes, offset));
/// <summary>
/// Serializes 64-bit unsigned integers 7 bits at a time,
/// starting with the least significant bits. The most significant bit in each
/// output byte is the continuation bit and indicates whether there are
/// additional non-zero bits encoded in following bytes. There are at most 9
/// output bytes and the last one does not have a continuation bit, allowing for
/// it to encode 8 bits (8*7+8 = 64).
/// </summary>
/// <param name="writer">The writer to use to write the bytes</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarLong(BinaryWriter writer, ulong value)
=> WriteVarLong(value, new BinaryWriterByteWriter(writer));
/// <summary>
/// Serializes 64-bit unsigned integers 7 bits at a time,
/// starting with the least significant bits. The most significant bit in each
/// output byte is the continuation bit and indicates whether there are
/// additional non-zero bits encoded in following bytes. There are at most 9
/// output bytes and the last one does not have a continuation bit, allowing for
/// it to encode 8 bits (8*7+8 = 64).
/// </summary>
/// <param name="bytes">The buffer to write the encoded value to</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarLong(Span<byte> bytes, ulong value)
{
// duplicate implementation as can't include Span<T> in IByteWriter implementations
var length = VarLongLength(value);
for (var i = 0; i < length - 1; i++)
{
bytes[i] = (byte)((value & 0x7F) | 0x80);
value >>= 7;
}
bytes[length - 1] = (byte)(value);
return length;
}
/// <summary>
/// Serializes 64-bit signed integers using zig-zag encoding,
/// which ensures small-scale integers are turned into unsigned integers
/// that have leading zeros, whether they are positive or negative,
/// hence allows for space-efficient encoding of those values.
/// </summary>
/// <param name="bytes">The byte array to write the buffer into</param>
/// <param name="offset">The offset within the array to start writing</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarLongZigZag(byte[] bytes, int offset, long value)
=> WriteVarLong(bytes, offset, ZigZag(value));
/// <summary>
/// Serializes 64-bit signed integers using zig-zag encoding,
/// which ensures small-scale integers are turned into unsigned integers
/// that have leading zeros, whether they are positive or negative,
/// hence allows for space-efficient encoding of those values.
/// </summary>
/// <param name="writer">The writer to use to write the bytes</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarLongZigZag(BinaryWriter writer, long value)
=> WriteVarLong(writer, ZigZag(value));
/// <summary>
/// Serializes 64-bit signed integers using zig-zag encoding,
/// which ensures small-scale integers are turned into unsigned integers
/// that have leading zeros, whether they are positive or negative,
/// hence allows for space-efficient encoding of those values.
/// </summary>
/// <param name="bytes">The span to write the buffer into</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarLongZigZag(Span<byte> bytes, long value)
=> WriteVarLong(bytes, ZigZag(value));
/// <summary>
/// Serializes 32-bit unsigned integers 7 bits at a time,
/// starting with the least significant bits. The most significant bit in each
/// output byte is the continuation bit and indicates whether there are
/// additional non-zero bits encoded in following bytes. There are at most 5
/// output bytes and the last one does not have a continuation bit, allowing for
/// it to encode 8 bits (5*7+8 = 64).
/// </summary>
/// <param name="bytes">The byte array to write the buffer into</param>
/// <param name="offset">The offset within the array to start writing</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarInt(byte[] bytes, int offset, uint value)
=> WriteVarLong(value, new ByteArrayByteWriter(bytes, offset));
/// <summary>
/// Serializes 32-bit unsigned integers 7 bits at a time,
/// starting with the least significant bits. The most significant bit in each
/// output byte is the continuation bit and indicates whether there are
/// additional non-zero bits encoded in following bytes. There are at most 5
/// output bytes and the last one does not have a continuation bit, allowing for
/// it to encode 8 bits (5*7+8 = 64).
/// </summary>
/// <param name="writer">The writer to use to write the bytes</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarInt(BinaryWriter writer, uint value)
=> WriteVarLong(value, new BinaryWriterByteWriter(writer));
/// <summary>
/// Serializes 32-bit unsigned integers 7 bits at a time,
/// starting with the least significant bits. The most significant bit in each
/// output byte is the continuation bit and indicates whether there are
/// additional non-zero bits encoded in following bytes. There are at most 5
/// output bytes and the last one does not have a continuation bit, allowing for
/// it to encode 8 bits (5*7+8 = 64).
/// </summary>
/// <param name="bytes">The buffer to write the encoded value to</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarInt(Span<byte> bytes, uint value)
=> WriteVarLong(bytes, value);
/// <summary>
/// Serializes 32-bit signed integers using zig-zag encoding,
/// which ensures small-scale integers are turned into unsigned integers
/// that have leading zeros, whether they are positive or negative,
/// hence allows for space-efficient encoding of those values.
/// </summary>
/// <param name="bytes">The byte array to write the buffer into</param>
/// <param name="offset">The offset within the array to start writing</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarIntZigZag(byte[] bytes, int offset, int value)
=> WriteVarLongZigZag(bytes, offset, value);
/// <summary>
/// Serializes 32-bit signed integers using zig-zag encoding,
/// which ensures small-scale integers are turned into unsigned integers
/// that have leading zeros, whether they are positive or negative,
/// hence allows for space-efficient encoding of those values.
/// </summary>
/// <param name="writer">The writer to use to write the bytes</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarIntZigZag(BinaryWriter writer, int value)
=> WriteVarLongZigZag(writer, value);
/// <summary>
/// Serializes 32-bit signed integers using zig-zag encoding,
/// which ensures small-scale integers are turned into unsigned integers
/// that have leading zeros, whether they are positive or negative,
/// hence allows for space-efficient encoding of those values.
/// </summary>
/// <param name="bytes">The span to write the buffer into</param>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes written</returns>
public static int WriteVarIntZigZag(Span<byte> bytes, int value)
=> WriteVarLongZigZag(bytes, value);
/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
/// <see cref="WriteVarLong(byte[],int,ulong)"/>
/// </summary>
/// <param name="bytes">The byte array to write the buffer into</param>
/// <param name="offset">The offset within the array to start writing</param>
/// <param name="bytesRead">The number of bytes read when successfully decoded</param>
/// <returns>The decoded value, or null if decoding failed</returns>
public static ulong? ReadVarLong(byte[] bytes, int offset, out int bytesRead)
{
ulong value = 0;
var i = 0;
var shift = 0;
while ((i + offset) < bytes.Length)
{
var next = bytes[i + offset];
if (next < 0x80 || i == MaxVarLen64 - 1)
{
bytesRead = i + 1;
return value | ((ulong)next << shift);
}
value |= ((ulong)next & 0x7FL) << shift;
i++;
shift += 7;
}
// something went wrong, invalid encoding
bytesRead = default;
return null;
}
/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
/// <see cref="WriteVarLong(byte[],int,ulong)"/>
/// </summary>
/// <param name="bytes">The buffer to read the encoded value from</param>
/// <param name="bytesRead">The number of bytes read when successfully decoded</param>
/// <returns>The decoded value, or null if decoding failed</returns>
public static ulong? ReadVarLong(Span<byte> bytes, out int bytesRead)
{
// duplicate implementation
ulong value = 0;
var i = 0;
var shift = 0;
while (i < bytes.Length)
{
var next = bytes[i];
if (next < 0x80 || i == MaxVarLen64 - 1)
{
bytesRead = i + 1;
return value | ((ulong)next << shift);
}
value |= ((ulong)next & 0x7FL) << shift;
i++;
shift += 7;
}
// something went wrong, invalid encoding
bytesRead = default;
return null;
}
/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
/// <see cref="WriteVarLongZigZag(byte[],int,long)"/>
/// </summary>
/// <param name="bytes">The byte array to write the buffer into</param>
/// <param name="offset">The offset within the array to start writing</param>
/// <param name="bytesRead">The number of bytes read when successfully decoded</param>
/// <returns>The decoded value, or null if decoding failed</returns>
public static long? ReadVarLongZigZag(byte[] bytes, int offset, out int bytesRead)
=> ReadVarLong(bytes, offset, out bytesRead) is { } decoded ? UnZigZag(decoded) : null;
/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
/// <see cref="WriteVarLongZigZag(byte[],int,long)"/>
/// </summary>
/// <param name="bytes">The buffer to read the encoded value from</param>
/// <param name="bytesRead">The number of bytes read when successfully decoded</param>
/// <returns>The decoded value, or null if decoding failed</returns>
public static long? ReadVarLongZigZag(Span<byte> bytes, out int bytesRead)
=> ReadVarLong(bytes, out bytesRead) is { } decoded ? UnZigZag(decoded) : null;
/// <summary>
/// Deserializes 32-bit unsigned integers that have been encoded using
/// <see cref="WriteVarInt(byte[],int,uint)" />
/// </summary>
/// <param name="bytes">The byte array to write the buffer into</param>
/// <param name="offset">The offset within the array to start writing</param>
/// <param name="bytesRead">The number of bytes read when successfully decoded</param>
/// <returns>The decoded value, or null if decoding failed</returns>
public static uint? ReadVarInt(byte[] bytes, int offset, out int bytesRead)
=> ReadVarLong(bytes, offset, out bytesRead) is { } decoded and <= int.MaxValue
? (uint)decoded
: null;
/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
/// <see cref="WriteVarInt(byte[],int,uint)" />
/// </summary>
/// <param name="bytes">The buffer to read the encoded value from</param>
/// <param name="bytesRead">The number of bytes read when successfully decoded</param>
/// <returns>The decoded value, or null if decoding failed</returns>
public static uint? ReadVarInt(Span<byte> bytes, out int bytesRead)
=> ReadVarLong(bytes, out bytesRead) is { } decoded and <= int.MaxValue
? (uint)decoded
: null;
/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
/// <see cref="WriteVarIntZigZag(byte[],int,int)" />
/// </summary>
/// <param name="bytes">The byte array to write the buffer into</param>
/// <param name="offset">The offset within the array to start writing</param>
/// <param name="bytesRead">The number of bytes read when successfully decoded</param>
/// <returns>The decoded value, or null if decoding failed</returns>
public static int? ReadVarIntZigZag(byte[] bytes, int offset, out int bytesRead)
=> ReadVarLongZigZag(bytes, offset, out bytesRead) is { } decoded and >= int.MinValue and <= int.MaxValue
? (int)decoded
: null;
/// <summary>
/// Deserializes 64-bit unsigned integers that have been encoded using
/// <see cref="WriteVarIntZigZag(byte[],int,int)" />
/// </summary>
/// <param name="bytes">The buffer to read the encoded value from</param>
/// <param name="bytesRead">The number of bytes read when successfully decoded</param>
/// <returns>The decoded value, or null if decoding failed</returns>
public static int? ReadVarIntZigZag(Span<byte> bytes, out int bytesRead)
=> ReadVarLongZigZag(bytes, out bytesRead) is { } decoded and >= int.MinValue and <= int.MaxValue
? (int)decoded
: null;
/// <summary>
/// Returns the number of bytes that <see cref="WriteVarIntZigZag(byte[],int,int)"/> encodes a value into.
/// </summary>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes used to encode</returns>
public static int VarIntZigZagLength(int value) => VarLongLength(ZigZag(value));
/// <summary>
/// Returns the number of bytes that <see cref="WriteVarInt(byte[],int,uint)"/> encodes a value into.
/// </summary>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes used to encode</returns>
public static int VarIntLength(uint value) => VarLongLength(value);
/// <summary>
/// Returns the number of bytes that <see cref="WriteVarLongZigZag(byte[],int,long)"/> encodes a value into.
/// </summary>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes used to encode</returns>
public static int VarLongZigZagLength(long value) => VarLongLength(ZigZag(value));
/// <summary>
/// Returns the number of bytes that <see cref="WriteVarLong(byte[],int,ulong)"/> encodes a value into.
/// </summary>
/// <param name="value">The value to encode</param>
/// <returns>The number of bytes used to encode</returns>
public static int VarLongLength(ulong value) => VarLongLengths[NumberOfLeadingZerosLong(value)];
/// <summary>
/// C# implementation of Java Long.numberOfLeadingZeros
/// </summary>
private static uint NumberOfLeadingZerosLong(ulong x)
{
#if NETCOREAPP3_1_OR_GREATER
return (uint)System.Numerics.BitOperations.LeadingZeroCount(x);
#else
// Based on: https://stackoverflow.com/a/10439333/869621
// adjusted for long
const int numLongBits = sizeof(long) * 8; // compile time constant
// Do the smearing which turns (for example)
// this: 0000 0101 0011
// into: 0000 0111 1111
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
// Count the ones
x -= x >> 1 & 0x5555555555555555;
x = (x >> 2 & 0x3333333333333333) + (x & 0x3333333333333333);
x = (x >> 4) + x & 0x0f0f0f0f0f0f0f0f;
x += x >> 8;
x += x >> 16;
x += x >> 32;
return numLongBits - (uint)(x & 0x0000007f); // subtract # of 1s from 64
#endif
}
private static int WriteVarLong<T>(ulong value, in T writer)
where T : IByteWriter
{
var length = VarLongLength(value);
for (var i = 0; i < length - 1; i++)
{
writer.WriteByte(i, (byte)((value & 0x7F) | 0x80));
value >>= 7;
}
writer.WriteByte(length - 1, (byte)value);
return length;
}
private static ulong ZigZag(long signed) => unchecked((ulong)((signed << 1) ^ (signed >> 63)));
private static long UnZigZag(ulong unsigned) => unchecked((long)((unsigned >> 1) ^ (0 - (unsigned & 1))));
private readonly struct ByteArrayByteWriter : IByteWriter
{
private readonly byte[] _bytes;
private readonly int _offset;
public ByteArrayByteWriter(byte[] bytes, int offset)
{
_bytes = bytes;
_offset = offset;
}
public void WriteByte(int offset, byte value)
{
_bytes[offset + _offset] = value;
}
}
private readonly struct BinaryWriterByteWriter : IByteWriter
{
private readonly BinaryWriter _writer;
public BinaryWriterByteWriter(BinaryWriter writer)
{
_writer = writer;
}
public void WriteByte(int offset, byte value)
{
_writer.Write(value);
}
}
}