Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Helpers.cs
Go to the documentation of this file.
1using System;
4using System.Text;
5
7
8internal static class Helpers
9{
10 public unsafe delegate void DecodedObjectReceiver(void* pvDecodedObject, int cbDecodedObject);
11
12 public unsafe delegate TResult DecodedObjectReceiver<TResult>(void* pvDecodedObject, int cbDecodedObject);
13
14 public unsafe static SafeHandle ToLpstrArray(this OidCollection oids, out int numOids)
15 {
16 if (oids == null || oids.Count == 0)
17 {
18 numOids = 0;
20 }
21 string[] array = new string[oids.Count];
22 for (int i = 0; i < array.Length; i++)
23 {
24 array[i] = oids[i].Value;
25 }
26 SafeLocalAllocHandle safeLocalAllocHandle;
27 checked
28 {
29 int num = array.Length * sizeof(void*);
30 string[] array2 = array;
31 foreach (string text in array2)
32 {
33 num += text.Length + 1;
34 }
35 safeLocalAllocHandle = SafeLocalAllocHandle.Create(num);
36 }
37 byte** ptr = (byte**)(void*)safeLocalAllocHandle.DangerousGetHandle();
38 byte* ptr2 = (byte*)(ptr + array.Length);
39 for (int k = 0; k < array.Length; k++)
40 {
41 string text2 = array[k];
42 ptr[k] = ptr2;
43 int bytes = Encoding.ASCII.GetBytes(text2, new Span<byte>(ptr2, text2.Length));
44 ptr2[text2.Length] = 0;
45 ptr2 += text2.Length + 1;
46 }
47 numOids = array.Length;
48 return safeLocalAllocHandle;
49 }
50
51 public static byte[] ValueAsAscii(this Oid oid)
52 {
53 return Encoding.ASCII.GetBytes(oid.Value);
54 }
55
56 public unsafe static TResult DecodeObject<TResult>(this byte[] encoded, CryptDecodeObjectStructType lpszStructType, DecodedObjectReceiver<TResult> receiver)
57 {
58 int pcbStructInfo = 0;
59 if (!global::Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, null, ref pcbStructInfo))
60 {
61 throw Marshal.GetLastWin32Error().ToCryptographicException();
62 }
63 byte* ptr = stackalloc byte[(int)(uint)pcbStructInfo];
64 if (!global::Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, ptr, ref pcbStructInfo))
65 {
66 throw Marshal.GetLastWin32Error().ToCryptographicException();
67 }
68 return receiver(ptr, pcbStructInfo);
69 }
70
71 public unsafe static TResult DecodeObject<TResult>(this byte[] encoded, string lpszStructType, DecodedObjectReceiver<TResult> receiver)
72 {
73 int pcbStructInfo = 0;
74 if (!global::Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, null, ref pcbStructInfo))
75 {
76 throw Marshal.GetLastWin32Error().ToCryptographicException();
77 }
78 byte* ptr = stackalloc byte[(int)(uint)pcbStructInfo];
79 if (!global::Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, ptr, ref pcbStructInfo))
80 {
81 throw Marshal.GetLastWin32Error().ToCryptographicException();
82 }
83 return receiver(ptr, pcbStructInfo);
84 }
85
86 public unsafe static bool DecodeObjectNoThrow(this byte[] encoded, CryptDecodeObjectStructType lpszStructType, DecodedObjectReceiver receiver)
87 {
88 int pcbStructInfo = 0;
89 if (!global::Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, null, ref pcbStructInfo))
90 {
91 return false;
92 }
93 byte* ptr = stackalloc byte[(int)(uint)pcbStructInfo];
94 if (!global::Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, ptr, ref pcbStructInfo))
95 {
96 return false;
97 }
98 receiver(ptr, pcbStructInfo);
99 return true;
100 }
101}
static unsafe bool DecodeObjectNoThrow(this byte[] encoded, CryptDecodeObjectStructType lpszStructType, DecodedObjectReceiver receiver)
Definition Helpers.cs:86
unsafe delegate TResult DecodedObjectReceiver< TResult >(void *pvDecodedObject, int cbDecodedObject)
static byte[] ValueAsAscii(this Oid oid)
Definition Helpers.cs:51
static unsafe SafeHandle ToLpstrArray(this OidCollection oids, out int numOids)
Definition Helpers.cs:14
static unsafe TResult DecodeObject< TResult >(this byte[] encoded, CryptDecodeObjectStructType lpszStructType, DecodedObjectReceiver< TResult > receiver)
Definition Helpers.cs:56
unsafe delegate void DecodedObjectReceiver(void *pvDecodedObject, int cbDecodedObject)
static Encoding ASCII
Definition Encoding.cs:511