Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BlobUtilities.cs
Go to the documentation of this file.
5
6namespace System.Reflection;
7
8internal static class BlobUtilities
9{
10 public const int SizeOfSerializedDecimal = 13;
11
12 public const int SizeOfGuid = 16;
13
14 public unsafe static byte[] ReadBytes(byte* buffer, int byteCount)
15 {
16 if (byteCount == 0)
17 {
19 }
20 byte[] array = new byte[byteCount];
22 return array;
23 }
24
30
31 public unsafe static void WriteBytes(this byte[] buffer, int start, byte value, int byteCount)
32 {
33 fixed (byte* ptr = &buffer[0])
34 {
35 byte* ptr2 = ptr + start;
36 for (int i = 0; i < byteCount; i++)
37 {
38 ptr2[i] = value;
39 }
40 }
41 }
42
43 public unsafe static void WriteDouble(this byte[] buffer, int start, double value)
44 {
45 buffer.WriteUInt64(start, *(ulong*)(&value));
46 }
47
48 public unsafe static void WriteSingle(this byte[] buffer, int start, float value)
49 {
50 buffer.WriteUInt32(start, *(uint*)(&value));
51 }
52
53 public static void WriteByte(this byte[] buffer, int start, byte value)
54 {
56 }
57
58 public unsafe static void WriteUInt16(this byte[] buffer, int start, ushort value)
59 {
60 fixed (byte* ptr = &buffer[start])
61 {
62 *ptr = (byte)value;
63 ptr[1] = (byte)(value >> 8);
64 }
65 }
66
67 public unsafe static void WriteUInt16BE(this byte[] buffer, int start, ushort value)
68 {
69 fixed (byte* ptr = &buffer[start])
70 {
71 *ptr = (byte)(value >> 8);
72 ptr[1] = (byte)value;
73 }
74 }
75
76 public unsafe static void WriteUInt32BE(this byte[] buffer, int start, uint value)
77 {
78 fixed (byte* ptr = &buffer[start])
79 {
80 *ptr = (byte)(value >> 24);
81 ptr[1] = (byte)(value >> 16);
82 ptr[2] = (byte)(value >> 8);
83 ptr[3] = (byte)value;
84 }
85 }
86
87 public unsafe static void WriteUInt32(this byte[] buffer, int start, uint value)
88 {
89 fixed (byte* ptr = &buffer[start])
90 {
91 *ptr = (byte)value;
92 ptr[1] = (byte)(value >> 8);
93 ptr[2] = (byte)(value >> 16);
94 ptr[3] = (byte)(value >> 24);
95 }
96 }
97
98 public static void WriteUInt64(this byte[] buffer, int start, ulong value)
99 {
100 buffer.WriteUInt32(start, (uint)value);
101 buffer.WriteUInt32(start + 4, (uint)(value >> 32));
102 }
103
104 public static void WriteDecimal(this byte[] buffer, int start, decimal value)
105 {
106 value.GetBits(out var isNegative, out var scale, out var low, out var mid, out var high);
107 buffer.WriteByte(start, (byte)(scale | (isNegative ? 128u : 0u)));
108 buffer.WriteUInt32(start + 1, low);
109 buffer.WriteUInt32(start + 5, mid);
110 buffer.WriteUInt32(start + 9, high);
111 }
112
113 public unsafe static void WriteGuid(this byte[] buffer, int start, Guid value)
114 {
115 fixed (byte* ptr2 = &buffer[start])
116 {
117 byte* ptr = (byte*)(&value);
118 uint num = *(uint*)ptr;
119 *ptr2 = (byte)num;
120 ptr2[1] = (byte)(num >> 8);
121 ptr2[2] = (byte)(num >> 16);
122 ptr2[3] = (byte)(num >> 24);
123 ushort num2 = *(ushort*)(ptr + 4);
124 ptr2[4] = (byte)num2;
125 ptr2[5] = (byte)(num2 >> 8);
126 ushort num3 = *(ushort*)(ptr + 6);
127 ptr2[6] = (byte)num3;
128 ptr2[7] = (byte)(num3 >> 8);
129 ptr2[8] = ptr[8];
130 ptr2[9] = ptr[9];
131 ptr2[10] = ptr[10];
132 ptr2[11] = ptr[11];
133 ptr2[12] = ptr[12];
134 ptr2[13] = ptr[13];
135 ptr2[14] = ptr[14];
136 ptr2[15] = ptr[15];
137 }
138 }
139
140 public unsafe static void WriteUTF8(this byte[] buffer, int start, char* charPtr, int charCount, int byteCount, bool allowUnpairedSurrogates)
141 {
142 char* ptr = charPtr + charCount;
143 fixed (byte* ptr2 = &buffer[0])
144 {
145 byte* ptr3 = ptr2 + start;
146 if (byteCount == charCount)
147 {
148 while (charPtr < ptr)
149 {
150 *(ptr3++) = (byte)(*(charPtr++));
151 }
152 return;
153 }
154 while (charPtr < ptr)
155 {
156 char c = *(charPtr++);
157 if (c < '\u0080')
158 {
159 *(ptr3++) = (byte)c;
160 continue;
161 }
162 if (c < 'ࠀ')
163 {
164 *ptr3 = (byte)(((uint)((int)c >> 6) & 0x1Fu) | 0xC0u);
165 ptr3[1] = (byte)((c & 0x3Fu) | 0x80u);
166 ptr3 += 2;
167 continue;
168 }
169 if (IsSurrogateChar(c))
170 {
171 if (IsHighSurrogateChar(c) && charPtr < ptr && IsLowSurrogateChar(*charPtr))
172 {
173 int num = c;
174 int num2 = *(charPtr++);
175 int num3 = (num - 55296 << 10) + num2 - 56320 + 65536;
176 *ptr3 = (byte)(((uint)(num3 >> 18) & 7u) | 0xF0u);
177 ptr3[1] = (byte)(((uint)(num3 >> 12) & 0x3Fu) | 0x80u);
178 ptr3[2] = (byte)(((uint)(num3 >> 6) & 0x3Fu) | 0x80u);
179 ptr3[3] = (byte)(((uint)num3 & 0x3Fu) | 0x80u);
180 ptr3 += 4;
181 continue;
182 }
183 if (!allowUnpairedSurrogates)
184 {
185 c = '\ufffd';
186 }
187 }
188 *ptr3 = (byte)(((uint)((int)c >> 12) & 0xFu) | 0xE0u);
189 ptr3[1] = (byte)(((uint)((int)c >> 6) & 0x3Fu) | 0x80u);
190 ptr3[2] = (byte)((c & 0x3Fu) | 0x80u);
191 ptr3 += 3;
192 }
193 }
194 }
195
196 internal unsafe static int GetUTF8ByteCount(string str)
197 {
198 fixed (char* str2 = str)
199 {
200 return GetUTF8ByteCount(str2, str.Length);
201 }
202 }
203
204 internal unsafe static int GetUTF8ByteCount(char* str, int charCount)
205 {
206 char* remainder;
207 return GetUTF8ByteCount(str, charCount, int.MaxValue, out remainder);
208 }
209
210 internal unsafe static int GetUTF8ByteCount(char* str, int charCount, int byteLimit, out char* remainder)
211 {
212 char* ptr = str + charCount;
213 char* ptr2 = str;
214 int num = 0;
215 while (ptr2 < ptr)
216 {
217 char c = *(ptr2++);
218 int num2;
219 if (c < '\u0080')
220 {
221 num2 = 1;
222 }
223 else if (c < 'ࠀ')
224 {
225 num2 = 2;
226 }
227 else if (IsHighSurrogateChar(c) && ptr2 < ptr && IsLowSurrogateChar(*ptr2))
228 {
229 num2 = 4;
230 ptr2++;
231 }
232 else
233 {
234 num2 = 3;
235 }
236 if (num + num2 > byteLimit)
237 {
238 ptr2 -= ((num2 < 4) ? 1 : 2);
239 break;
240 }
241 num += num2;
242 }
243 remainder = ptr2;
244 return num;
245 }
246
247 internal static bool IsSurrogateChar(int c)
248 {
249 return (uint)(c - 55296) <= 2047u;
250 }
251
252 internal static bool IsHighSurrogateChar(int c)
253 {
254 return (uint)(c - 55296) <= 1023u;
255 }
256
257 internal static bool IsLowSurrogateChar(int c)
258 {
259 return (uint)(c - 56320) <= 1023u;
260 }
261
262 [MethodImpl(MethodImplOptions.AggressiveInlining)]
263 internal static void ValidateRange(int bufferLength, int start, int byteCount, string byteCountParameterName)
264 {
265 if (start < 0 || start > bufferLength)
266 {
267 Throw.ArgumentOutOfRange("start");
268 }
269 if (byteCount < 0 || byteCount > bufferLength - start)
270 {
271 Throw.ArgumentOutOfRange(byteCountParameterName);
272 }
273 }
274
275 internal static int GetUserStringByteLength(int characterCount)
276 {
277 return characterCount * 2 + 1;
278 }
279
280 internal static byte GetUserStringTrailingByte(string str)
281 {
282 foreach (char c in str)
283 {
284 if (c >= '\u007f')
285 {
286 return 1;
287 }
288 switch (c)
289 {
290 case '\u0001':
291 case '\u0002':
292 case '\u0003':
293 case '\u0004':
294 case '\u0005':
295 case '\u0006':
296 case '\a':
297 case '\b':
298 case '\u000e':
299 case '\u000f':
300 case '\u0010':
301 case '\u0011':
302 case '\u0012':
303 case '\u0013':
304 case '\u0014':
305 case '\u0015':
306 case '\u0016':
307 case '\u0017':
308 case '\u0018':
309 case '\u0019':
310 case '\u001a':
311 case '\u001b':
312 case '\u001c':
313 case '\u001d':
314 case '\u001e':
315 case '\u001f':
316 case '\'':
317 case '-':
318 return 1;
319 }
320 }
321 return 0;
322 }
323}
static bool IsLowSurrogateChar(int c)
static unsafe int GetUTF8ByteCount(char *str, int charCount, int byteLimit, out char *remainder)
static unsafe int GetUTF8ByteCount(char *str, int charCount)
static unsafe void WriteUInt32(this byte[] buffer, int start, uint value)
static unsafe void WriteDouble(this byte[] buffer, int start, double value)
static unsafe void WriteGuid(this byte[] buffer, int start, Guid value)
static void ValidateRange(int bufferLength, int start, int byteCount, string byteCountParameterName)
static unsafe void WriteUInt16(this byte[] buffer, int start, ushort value)
static unsafe void WriteUInt32BE(this byte[] buffer, int start, uint value)
static unsafe void WriteUInt16BE(this byte[] buffer, int start, ushort value)
static unsafe ImmutableArray< byte > ReadImmutableBytes(byte *buffer, int byteCount)
static unsafe void WriteBytes(this byte[] buffer, int start, byte value, int byteCount)
static unsafe void WriteSingle(this byte[] buffer, int start, float value)
static bool IsHighSurrogateChar(int c)
static unsafe void WriteUTF8(this byte[] buffer, int start, char *charPtr, int charCount, int byteCount, bool allowUnpairedSurrogates)
static bool IsSurrogateChar(int c)
static void WriteDecimal(this byte[] buffer, int start, decimal value)
static byte GetUserStringTrailingByte(string str)
static void WriteUInt64(this byte[] buffer, int start, ulong value)
static void WriteByte(this byte[] buffer, int start, byte value)
static unsafe int GetUTF8ByteCount(string str)
static int GetUserStringByteLength(int characterCount)
static unsafe byte[] ReadBytes(byte *buffer, int byteCount)
static ImmutableArray< byte > DangerousCreateFromUnderlyingArray(ref byte[]? array)
static void ArgumentOutOfRange(string parameterName)
Definition Throw.cs:145
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Definition Marshal.cs:800