Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HPackEncoder.cs
Go to the documentation of this file.
1using System.Text;
2
4
5internal static class HPackEncoder
6{
7 public static bool EncodeIndexedHeaderField(int index, Span<byte> destination, out int bytesWritten)
8 {
9 if (destination.Length != 0)
10 {
11 destination[0] = 128;
12 return IntegerEncoder.Encode(index, 7, destination, out bytesWritten);
13 }
14 bytesWritten = 0;
15 return false;
16 }
17
18 public static bool EncodeLiteralHeaderFieldWithoutIndexing(int index, string value, Encoding valueEncoding, Span<byte> destination, out int bytesWritten)
19 {
20 if ((uint)destination.Length >= 2u)
21 {
22 destination[0] = 0;
23 if (IntegerEncoder.Encode(index, 4, destination, out var bytesWritten2) && EncodeStringLiteral(value, valueEncoding, destination.Slice(bytesWritten2), out var bytesWritten3))
24 {
25 bytesWritten = bytesWritten2 + bytesWritten3;
26 return true;
27 }
28 }
29 bytesWritten = 0;
30 return false;
31 }
32
33 public static bool EncodeLiteralHeaderFieldWithoutIndexing(int index, Span<byte> destination, out int bytesWritten)
34 {
35 if (destination.Length != 0)
36 {
37 destination[0] = 0;
38 if (IntegerEncoder.Encode(index, 4, destination, out var bytesWritten2))
39 {
40 bytesWritten = bytesWritten2;
41 return true;
42 }
43 }
44 bytesWritten = 0;
45 return false;
46 }
47
48 public static bool EncodeLiteralHeaderFieldWithoutIndexingNewName(string name, ReadOnlySpan<string> values, string separator, Encoding valueEncoding, Span<byte> destination, out int bytesWritten)
49 {
50 if ((uint)destination.Length >= 3u)
51 {
52 destination[0] = 0;
53 if (EncodeLiteralHeaderName(name, destination.Slice(1), out var bytesWritten2) && EncodeStringLiterals(values, separator, valueEncoding, destination.Slice(1 + bytesWritten2), out var bytesWritten3))
54 {
55 bytesWritten = 1 + bytesWritten2 + bytesWritten3;
56 return true;
57 }
58 }
59 bytesWritten = 0;
60 return false;
61 }
62
63 public static bool EncodeLiteralHeaderFieldWithoutIndexingNewName(string name, Span<byte> destination, out int bytesWritten)
64 {
65 if ((uint)destination.Length >= 2u)
66 {
67 destination[0] = 0;
68 if (EncodeLiteralHeaderName(name, destination.Slice(1), out var bytesWritten2))
69 {
70 bytesWritten = 1 + bytesWritten2;
71 return true;
72 }
73 }
74 bytesWritten = 0;
75 return false;
76 }
77
78 private static bool EncodeLiteralHeaderName(string value, Span<byte> destination, out int bytesWritten)
79 {
80 if (destination.Length != 0)
81 {
82 destination[0] = 0;
83 if (IntegerEncoder.Encode(value.Length, 7, destination, out var bytesWritten2))
84 {
85 destination = destination.Slice(bytesWritten2);
86 if (value.Length <= destination.Length)
87 {
88 for (int i = 0; i < value.Length; i++)
89 {
90 char c = value[i];
91 destination[i] = (byte)(((uint)(c - 65) <= 25u) ? (c | 0x20u) : c);
92 }
93 bytesWritten = bytesWritten2 + value.Length;
94 return true;
95 }
96 }
97 }
98 bytesWritten = 0;
99 return false;
100 }
101
103 {
104 for (int i = 0; i < value.Length; i++)
105 {
106 char c = value[i];
107 if ((c & 0xFF80u) != 0)
108 {
110 }
111 destination[i] = (byte)c;
112 }
113 }
114
115 public static bool EncodeStringLiteral(string value, Encoding valueEncoding, Span<byte> destination, out int bytesWritten)
116 {
117 if (destination.Length != 0)
118 {
119 destination[0] = 0;
120 int num = ((valueEncoding == null || valueEncoding == Encoding.Latin1) ? value.Length : valueEncoding.GetByteCount(value));
121 if (IntegerEncoder.Encode(num, 7, destination, out var bytesWritten2))
122 {
123 destination = destination.Slice(bytesWritten2);
124 if (num <= destination.Length)
125 {
126 if (valueEncoding == null)
127 {
129 }
130 else
131 {
132 int bytes = valueEncoding.GetBytes(value, destination);
133 }
134 bytesWritten = bytesWritten2 + num;
135 return true;
136 }
137 }
138 }
139 bytesWritten = 0;
140 return false;
141 }
142
143 public static bool EncodeStringLiterals(ReadOnlySpan<string> values, string separator, Encoding valueEncoding, Span<byte> destination, out int bytesWritten)
144 {
145 bytesWritten = 0;
146 if (values.Length == 0)
147 {
148 return EncodeStringLiteral("", null, destination, out bytesWritten);
149 }
150 if (values.Length == 1)
151 {
152 return EncodeStringLiteral(values[0], valueEncoding, destination, out bytesWritten);
153 }
154 if (destination.Length != 0)
155 {
156 int num;
157 checked
158 {
159 if (valueEncoding == null || valueEncoding == Encoding.Latin1)
160 {
161 num = (values.Length - 1) * separator.Length;
162 ReadOnlySpan<string> readOnlySpan = values;
163 for (int i = 0; i < readOnlySpan.Length; i = unchecked(i + 1))
164 {
165 string text = readOnlySpan[i];
166 num += text.Length;
167 }
168 }
169 else
170 {
171 num = (values.Length - 1) * valueEncoding.GetByteCount(separator);
172 ReadOnlySpan<string> readOnlySpan2 = values;
173 for (int j = 0; j < readOnlySpan2.Length; j = unchecked(j + 1))
174 {
175 string s = readOnlySpan2[j];
176 num += valueEncoding.GetByteCount(s);
177 }
178 }
179 destination[0] = 0;
180 }
181 if (IntegerEncoder.Encode(num, 7, destination, out var bytesWritten2))
182 {
183 destination = destination.Slice(bytesWritten2);
184 if (destination.Length >= num)
185 {
186 if (valueEncoding == null)
187 {
188 string text2 = values[0];
190 destination = destination.Slice(text2.Length);
191 for (int k = 1; k < values.Length; k++)
192 {
194 destination = destination.Slice(separator.Length);
195 text2 = values[k];
197 destination = destination.Slice(text2.Length);
198 }
199 }
200 else
201 {
202 int bytes = valueEncoding.GetBytes(values[0], destination);
204 for (int l = 1; l < values.Length; l++)
205 {
206 bytes = valueEncoding.GetBytes(separator, destination);
208 bytes = valueEncoding.GetBytes(values[l], destination);
210 }
211 }
212 bytesWritten = bytesWritten2 + num;
213 return true;
214 }
215 }
216 }
217 return false;
218 }
219
221 {
222 Span<byte> destination = stackalloc byte[256];
223 int bytesWritten;
224 bool flag = EncodeLiteralHeaderFieldWithoutIndexing(index, destination, out bytesWritten);
225 return destination.Slice(0, bytesWritten).ToArray();
226 }
227
229 {
230 Span<byte> destination = stackalloc byte[256];
231 int bytesWritten;
232 bool flag = EncodeLiteralHeaderFieldWithoutIndexingNewName(name, destination, out bytesWritten);
233 return destination.Slice(0, bytesWritten).ToArray();
234 }
235
237 {
238 Span<byte> destination = stackalloc byte[512];
239 int bytesWritten;
240 while (!EncodeLiteralHeaderFieldWithoutIndexing(index, value, null, destination, out bytesWritten))
241 {
242 destination = new byte[destination.Length * 2];
243 }
244 return destination.Slice(0, bytesWritten).ToArray();
245 }
246}
static bool EncodeIndexedHeaderField(int index, Span< byte > destination, out int bytesWritten)
static byte[] EncodeLiteralHeaderFieldWithoutIndexingToAllocatedArray(int index, string value)
static bool EncodeLiteralHeaderName(string value, Span< byte > destination, out int bytesWritten)
static byte[] EncodeLiteralHeaderFieldWithoutIndexingToAllocatedArray(int index)
static byte[] EncodeLiteralHeaderFieldWithoutIndexingNewNameToAllocatedArray(string name)
static void EncodeValueStringPart(string value, Span< byte > destination)
static bool EncodeStringLiterals(ReadOnlySpan< string > values, string separator, Encoding valueEncoding, Span< byte > destination, out int bytesWritten)
static bool EncodeLiteralHeaderFieldWithoutIndexing(int index, string value, Encoding valueEncoding, Span< byte > destination, out int bytesWritten)
static bool EncodeStringLiteral(string value, Encoding valueEncoding, Span< byte > destination, out int bytesWritten)
static bool EncodeLiteralHeaderFieldWithoutIndexingNewName(string name, ReadOnlySpan< string > values, string separator, Encoding valueEncoding, Span< byte > destination, out int bytesWritten)
static bool EncodeLiteralHeaderFieldWithoutIndexing(int index, Span< byte > destination, out int bytesWritten)
static bool EncodeLiteralHeaderFieldWithoutIndexingNewName(string name, Span< byte > destination, out int bytesWritten)
static bool Encode(int value, int numBits, Span< byte > destination, out int bytesWritten)
static string net_http_request_invalid_char_encoding
Definition SR.cs:132
Definition SR.cs:7
static Encoding Latin1
Definition Encoding.cs:513
virtual byte[] GetBytes(char[] chars)
Definition Encoding.cs:781
virtual int GetByteCount(char[] chars)
Definition Encoding.cs:713
Span< T > Slice(int start)
Definition Span.cs:271