-
Notifications
You must be signed in to change notification settings - Fork 137
/
Deserialization.cs
273 lines (247 loc) · 14.2 KB
/
Deserialization.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
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
using Solnet.Wallet;
using System;
using System.Buffers.Binary;
using System.Diagnostics;
using System.Numerics;
using System.Text;
namespace Solnet.Programs.Utilities
{
/// <summary>
/// Extension methods for deserialization of program data using <see cref="ReadOnlySpan{T}"/>.
/// </summary>
public static class Deserialization
{
/// <summary>
/// Get a 8-bit unsigned integer from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the 8-bit unsigned integer begins.</param>
/// <returns>The 8-bit unsigned integer.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static byte GetU8(this ReadOnlySpan<byte> data, int offset)
{
if (offset > data.Length - sizeof(byte))
throw new ArgumentOutOfRangeException(nameof(offset));
return data[offset];
}
/// <summary>
/// Get a 16-bit unsigned integer from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the 16-bit unsigned integer begins.</param>
/// <returns>The 16-bit unsigned integer.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static ushort GetU16(this ReadOnlySpan<byte> data, int offset)
{
if (offset + sizeof(ushort) > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(offset, sizeof(ushort)));
}
/// <summary>
/// Get a 32-bit unsigned integer from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the 32-bit unsigned integer begins.</param>
/// <returns>The 32-bit unsigned integer.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static uint GetU32(this ReadOnlySpan<byte> data, int offset)
{
if (offset + sizeof(uint) > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(offset, sizeof(uint)));
}
/// <summary>
/// Get a 64-bit unsigned integer from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the 64-bit unsigned integer begins.</param>
/// <returns>The 64-bit unsigned integer.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static ulong GetU64(this ReadOnlySpan<byte> data, int offset)
{
if (offset + sizeof(ulong) > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return BinaryPrimitives.ReadUInt64LittleEndian(data.Slice(offset, sizeof(ulong)));
}
/// <summary>
/// Get a 8-bit signed integer from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the 8-bit signed integer begins.</param>
/// <returns>The 8-bit signed integer.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static sbyte GetS8(this ReadOnlySpan<byte> data, int offset)
{
if (offset > data.Length - sizeof(sbyte))
throw new ArgumentOutOfRangeException(nameof(offset));
return (sbyte)data[offset];
}
/// <summary>
/// Get a 16-bit signed integer from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the 16-bit signed integer begins.</param>
/// <returns>The 16-bit signed integer.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static short GetS16(this ReadOnlySpan<byte> data, int offset)
{
if (offset + sizeof(short) > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return BinaryPrimitives.ReadInt16LittleEndian(data.Slice(offset, sizeof(short)));
}
/// <summary>
/// Get a 32-bit signed integer from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the 32-bit signed integer begins.</param>
/// <returns>The 32-bit signed integer.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static int GetS32(this ReadOnlySpan<byte> data, int offset)
{
if (offset + sizeof(int) > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return BinaryPrimitives.ReadInt32LittleEndian(data.Slice(offset, sizeof(int)));
}
/// <summary>
/// Get a 64-bit signed integer from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the 64-bit signed integer begins.</param>
/// <returns>The 64-bit signed integer.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static long GetS64(this ReadOnlySpan<byte> data, int offset)
{
if (offset + sizeof(long) > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return BinaryPrimitives.ReadInt64LittleEndian(data.Slice(offset, sizeof(long)));
}
/// <summary>
/// Get a span from the read-only span at the given offset with the given length.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the desired span begins.</param>
/// <param name="length">The desired length for the new span.</param>
/// <returns>A <see cref="Span{T}"/> of bytes.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static ReadOnlySpan<byte> GetSpan(this ReadOnlySpan<byte> data, int offset, int length)
{
if (offset + length > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return data.Slice(offset, length);
}
/// <summary>
/// Get a <see cref="PublicKey"/> encoded as a 32 byte array from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the 32 byte array begins.</param>
/// <returns>The <see cref="PublicKey"/> instance.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static PublicKey GetPubKey(this ReadOnlySpan<byte> data, int offset)
{
if (offset + PublicKey.PublicKeyLength > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return new PublicKey(data.Slice(offset, PublicKey.PublicKeyLength).ToArray());
}
/// <summary>
/// Get an arbitrarily long number from the span at the given offset, specifying it's length in bytes.
/// Optionally specify if it's signed and the endianness.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the arbitrarily long number begins.</param>
/// <param name="length">The byte-length of the arbitrarily long number.</param>
/// <param name="isUnsigned">Whether the value does not use signed encoding.</param>
/// <param name="isBigEndian">Whether the value is in big-endian byte order.</param>
/// <returns>The <see cref="BigInteger"/> instance that represents the value.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static BigInteger GetBigInt(this ReadOnlySpan<byte> data, int offset, int length,
bool isUnsigned = false, bool isBigEndian = false)
{
if (offset + length > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return new BigInteger(data.Slice(offset, length), isUnsigned, isBigEndian);
}
/// <summary>
/// Get a double-precision floating-point number from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the double-precision floating-point number begins.</param>
/// <returns>The <see cref="double"/> instance that represents the value.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static double GetDouble(this ReadOnlySpan<byte> data, int offset)
{
if (offset + sizeof(double) > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return BinaryPrimitives.ReadDoubleLittleEndian(data.Slice(offset, sizeof(double)));
}
/// <summary>
/// Get a single-precision floating-point number from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the single-precision floating-point number begins.</param>
/// <returns>The <see cref="float"/> instance that represents the value.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static float GetSingle(this ReadOnlySpan<byte> data, int offset)
{
if (offset + sizeof(float) > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return BinaryPrimitives.ReadSingleLittleEndian(data.Slice(offset, sizeof(float)));
}
/// <summary>
/// Get a boolean value from the span at the given offset.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the boolean value is located.</param>
/// <returns>The <see cref="bool"/> instance that represents the value.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static bool GetBool(this ReadOnlySpan<byte> data, int offset)
{
return GetU8(data, offset) == 1;
}
/// <summary>
/// Decodes a string from a transaction instruction.
/// </summary>
/// <param name="data">The data to decode.</param>
/// <param name="offset">The offset at which the string begins.</param>
/// <returns>The decoded data.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static (string EncodedString, int Length) DecodeBincodeString(this ReadOnlySpan<byte> data, int offset)
{
if (offset + sizeof(ulong) > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
int stringLength = (int)data.GetU64(offset);
byte[] stringBytes = data.GetSpan(offset + sizeof(ulong), stringLength).ToArray();
return (EncodedString: Encoding.UTF8.GetString(stringBytes), Length: stringLength + sizeof(ulong));
}
/// <summary>
/// Decodes a string from a transaction instruction.
/// </summary>
/// <param name="data">The data to decode.</param>
/// <param name="offset">The offset at which the string begins.</param>
/// <param name="result">The decoded data./>.</param>
/// <returns>The length in bytes that was read from the original buffer, including the</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static int GetBorshString(this ReadOnlySpan<byte> data, int offset, out string result)
{
if (offset + sizeof(uint) > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
int stringLength = (int)data.GetU32(offset);
byte[] stringBytes = data.GetSpan(offset + sizeof(uint), stringLength).ToArray();
result = Encoding.UTF8.GetString(stringBytes);
return stringLength + sizeof(uint);
}
/// <summary>
/// Get a span from the read-only span at the given offset with the given length.
/// </summary>
/// <param name="data">The span to get data from.</param>
/// <param name="offset">The offset at which the desired <c>byte[]</c> begins.</param>
/// <param name="length">The desired length for the new <c>byte[]</c>.</param>
/// <returns>A <c>byte[]</c> of bytes.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
public static byte[] GetBytes(this ReadOnlySpan<byte> data, int offset, int length)
{
if (offset + length > data.Length)
throw new ArgumentOutOfRangeException(nameof(offset));
return data.Slice(offset, length).ToArray();
}
}
}