Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
VariableLengthIntegerHelper.cs
Go to the documentation of this file.
2
3namespace System.Net.Http;
4
5internal static class VariableLengthIntegerHelper
6{
7 public static bool TryRead(ReadOnlySpan<byte> buffer, out long value, out int bytesRead)
8 {
9 if (buffer.Length != 0)
10 {
11 byte b = buffer[0];
12 switch (b & 0xC0)
13 {
14 case 0:
15 value = b;
16 bytesRead = 1;
17 return true;
18 case 64:
19 {
21 {
22 value = (uint)(value3 - 16384);
23 bytesRead = 2;
24 return true;
25 }
26 break;
27 }
28 case 128:
29 {
31 {
32 value = (uint)((int)value4 - int.MinValue);
33 bytesRead = 4;
34 return true;
35 }
36 break;
37 }
38 default:
39 {
41 {
42 value = (long)value2 - -4611686018427387904L;
43 bytesRead = 8;
44 return true;
45 }
46 break;
47 }
48 }
49 }
50 value = 0L;
51 bytesRead = 0;
52 return false;
53 }
54
55 public static bool TryWrite(Span<byte> buffer, long longToEncode, out int bytesWritten)
56 {
57 if (longToEncode < 63)
58 {
59 if (buffer.Length != 0)
60 {
61 buffer[0] = (byte)longToEncode;
62 bytesWritten = 1;
63 return true;
64 }
65 }
66 else if (longToEncode < 16383)
67 {
68 if (BinaryPrimitives.TryWriteUInt16BigEndian(buffer, (ushort)((uint)(int)longToEncode | 0x4000u)))
69 {
70 bytesWritten = 2;
71 return true;
72 }
73 }
74 else if (longToEncode < 1073741823)
75 {
76 if (BinaryPrimitives.TryWriteUInt32BigEndian(buffer, (uint)(int)longToEncode | 0x80000000u))
77 {
78 bytesWritten = 4;
79 return true;
80 }
81 }
82 else if (BinaryPrimitives.TryWriteUInt64BigEndian(buffer, (ulong)longToEncode | 0xC000000000000000uL))
83 {
84 bytesWritten = 8;
85 return true;
86 }
87 bytesWritten = 0;
88 return false;
89 }
90
91 public static int WriteInteger(Span<byte> buffer, long longToEncode)
92 {
93 int bytesWritten;
94 bool flag = TryWrite(buffer, longToEncode, out bytesWritten);
95 return bytesWritten;
96 }
97
98 public static int GetByteCount(long value)
99 {
100 if (value >= 63)
101 {
102 if (value >= 16383)
103 {
104 if (value >= 1073741823)
105 {
106 return 8;
107 }
108 return 4;
109 }
110 return 2;
111 }
112 return 1;
113 }
114}
static bool TryReadUInt32BigEndian(ReadOnlySpan< byte > source, out uint value)
static bool TryWriteUInt32BigEndian(Span< byte > destination, uint value)
static bool TryWriteUInt64BigEndian(Span< byte > destination, ulong value)
static bool TryReadUInt64BigEndian(ReadOnlySpan< byte > source, out ulong value)
static bool TryReadUInt16BigEndian(ReadOnlySpan< byte > source, out ushort value)
static bool TryWriteUInt16BigEndian(Span< byte > destination, ushort value)
static bool TryRead(ReadOnlySpan< byte > buffer, out long value, out int bytesRead)
static int WriteInteger(Span< byte > buffer, long longToEncode)
static bool TryWrite(Span< byte > buffer, long longToEncode, out int bytesWritten)