Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
QPackEncoder.cs
Go to the documentation of this file.
2using System.Text;
3
5
6internal static class QPackEncoder
7{
8 public static bool EncodeStaticIndexedHeaderField(int index, Span<byte> destination, out int bytesWritten)
9 {
10 if (!destination.IsEmpty)
11 {
12 destination[0] = 192;
13 return IntegerEncoder.Encode(index, 6, destination, out bytesWritten);
14 }
15 bytesWritten = 0;
16 return false;
17 }
18
20 {
21 Span<byte> destination = stackalloc byte[6];
22 int bytesWritten;
23 bool flag = EncodeStaticIndexedHeaderField(index, destination, out bytesWritten);
24 return destination.Slice(0, bytesWritten).ToArray();
25 }
26
27 public static bool EncodeLiteralHeaderFieldWithStaticNameReference(int index, string value, Span<byte> destination, out int bytesWritten)
28 {
30 }
31
32 public static bool EncodeLiteralHeaderFieldWithStaticNameReference(int index, string value, Encoding valueEncoding, Span<byte> destination, out int bytesWritten)
33 {
34 if (destination.Length >= 2)
35 {
36 destination[0] = 80;
37 if (IntegerEncoder.Encode(index, 4, destination, out var bytesWritten2))
38 {
39 destination = destination.Slice(bytesWritten2);
40 if (EncodeValueString(value, valueEncoding, destination, out var length))
41 {
42 bytesWritten = bytesWritten2 + length;
43 return true;
44 }
45 }
46 }
47 bytesWritten = 0;
48 return false;
49 }
50
52 {
53 Span<byte> destination = stackalloc byte[6];
54 destination[0] = 112;
55 int bytesWritten;
56 bool flag = IntegerEncoder.Encode(index, 4, destination, out bytesWritten);
57 return destination.Slice(0, bytesWritten).ToArray();
58 }
59
61 {
62 Span<byte> span = ((value.Length >= 256) ? ((Span<byte>)new byte[value.Length + 12]) : stackalloc byte[268]);
64 int bytesWritten;
66 return destination.Slice(0, bytesWritten).ToArray();
67 }
68
69 public static bool EncodeLiteralHeaderFieldWithoutNameReference(string name, string value, Span<byte> destination, out int bytesWritten)
70 {
71 return EncodeLiteralHeaderFieldWithoutNameReference(name, value, null, destination, out bytesWritten);
72 }
73
74 public static bool EncodeLiteralHeaderFieldWithoutNameReference(string name, string value, Encoding valueEncoding, Span<byte> destination, out int bytesWritten)
75 {
76 if (EncodeNameString(name, destination, out var length) && EncodeValueString(value, valueEncoding, destination.Slice(length), out var length2))
77 {
78 bytesWritten = length + length2;
79 return true;
80 }
81 bytesWritten = 0;
82 return false;
83 }
84
85 public static bool EncodeLiteralHeaderFieldWithoutNameReference(string name, ReadOnlySpan<string> values, string valueSeparator, Encoding valueEncoding, Span<byte> destination, out int bytesWritten)
86 {
87 if (EncodeNameString(name, destination, out var length) && EncodeValueString(values, valueSeparator, valueEncoding, destination.Slice(length), out var length2))
88 {
89 bytesWritten = length + length2;
90 return true;
91 }
92 bytesWritten = 0;
93 return false;
94 }
95
97 {
98 Span<byte> span = ((name.Length >= 256) ? ((Span<byte>)new byte[name.Length + 6]) : stackalloc byte[262]);
99 Span<byte> buffer = span;
100 int length;
101 bool flag = EncodeNameString(name, buffer, out length);
102 return buffer.Slice(0, length).ToArray();
103 }
104
105 public static byte[] EncodeLiteralHeaderFieldWithoutNameReferenceToArray(string name, string value)
106 {
107 Span<byte> span = ((name.Length + value.Length >= 256) ? ((Span<byte>)new byte[name.Length + value.Length + 12]) : stackalloc byte[268]);
108 Span<byte> destination = span;
109 int bytesWritten;
110 bool flag = EncodeLiteralHeaderFieldWithoutNameReference(name, value, destination, out bytesWritten);
111 return destination.Slice(0, bytesWritten).ToArray();
112 }
113
114 private static bool EncodeValueString(string s, Encoding valueEncoding, Span<byte> buffer, out int length)
115 {
116 if (buffer.Length != 0)
117 {
118 buffer[0] = 0;
119 int num = ((valueEncoding == null || valueEncoding == Encoding.Latin1) ? s.Length : valueEncoding.GetByteCount(s));
120 if (IntegerEncoder.Encode(num, 7, buffer, out var bytesWritten))
121 {
122 buffer = buffer.Slice(bytesWritten);
123 if (buffer.Length >= num)
124 {
125 if (valueEncoding == null)
126 {
128 }
129 else
130 {
131 int bytes = valueEncoding.GetBytes(s, buffer);
132 }
133 length = bytesWritten + num;
134 return true;
135 }
136 }
137 }
138 length = 0;
139 return false;
140 }
141
142 public static bool EncodeValueString(ReadOnlySpan<string> values, string separator, Encoding valueEncoding, Span<byte> buffer, out int length)
143 {
144 if (values.Length == 1)
145 {
146 return EncodeValueString(values[0], valueEncoding, buffer, out length);
147 }
148 if (values.Length == 0)
149 {
150 return EncodeValueString(string.Empty, null, buffer, out length);
151 }
152 if (buffer.Length > 0)
153 {
154 int num;
155 if (valueEncoding == null || valueEncoding == Encoding.Latin1)
156 {
157 num = separator.Length * (values.Length - 1);
158 ReadOnlySpan<string> readOnlySpan = values;
159 for (int i = 0; i < readOnlySpan.Length; i++)
160 {
161 string text = readOnlySpan[i];
162 num += text.Length;
163 }
164 }
165 else
166 {
167 num = valueEncoding.GetByteCount(separator) * (values.Length - 1);
168 ReadOnlySpan<string> readOnlySpan2 = values;
169 for (int j = 0; j < readOnlySpan2.Length; j++)
170 {
171 string s = readOnlySpan2[j];
172 num += valueEncoding.GetByteCount(s);
173 }
174 }
175 buffer[0] = 0;
176 if (IntegerEncoder.Encode(num, 7, buffer, out var bytesWritten))
177 {
178 buffer = buffer.Slice(bytesWritten);
179 if (buffer.Length >= num)
180 {
181 if (valueEncoding == null)
182 {
183 string text2 = values[0];
185 buffer = buffer.Slice(text2.Length);
186 for (int k = 1; k < values.Length; k++)
187 {
188 EncodeValueStringPart(separator, buffer);
189 buffer = buffer.Slice(separator.Length);
190 text2 = values[k];
192 buffer = buffer.Slice(text2.Length);
193 }
194 }
195 else
196 {
197 int bytes = valueEncoding.GetBytes(values[0], buffer);
198 buffer = buffer.Slice(bytes);
199 for (int l = 1; l < values.Length; l++)
200 {
201 bytes = valueEncoding.GetBytes(separator, buffer);
202 buffer = buffer.Slice(bytes);
203 bytes = valueEncoding.GetBytes(values[l], buffer);
204 buffer = buffer.Slice(bytes);
205 }
206 }
207 length = bytesWritten + num;
208 return true;
209 }
210 }
211 }
212 length = 0;
213 return false;
214 }
215
216 private static void EncodeValueStringPart(string s, Span<byte> buffer)
217 {
218 for (int i = 0; i < s.Length; i++)
219 {
220 char c = s[i];
221 if (c > '\u007f')
222 {
224 }
225 buffer[i] = (byte)c;
226 }
227 }
228
229 private static bool EncodeNameString(string s, Span<byte> buffer, out int length)
230 {
231 if (buffer.Length != 0)
232 {
233 buffer[0] = 48;
234 if (IntegerEncoder.Encode(s.Length, 3, buffer, out var bytesWritten))
235 {
236 buffer = buffer.Slice(bytesWritten);
237 if (buffer.Length >= s.Length)
238 {
239 for (int i = 0; i < s.Length; i++)
240 {
241 int num = s[i];
242 if ((uint)(num - 65) <= 25u)
243 {
244 num |= 0x20;
245 }
246 buffer[i] = (byte)num;
247 }
248 length = bytesWritten + s.Length;
249 return true;
250 }
251 }
252 }
253 length = 0;
254 return false;
255 }
256}
static bool Encode(int value, int numBits, Span< byte > destination, out int bytesWritten)
static bool EncodeStaticIndexedHeaderField(int index, Span< byte > destination, out int bytesWritten)
static bool EncodeValueString(string s, Encoding valueEncoding, Span< byte > buffer, out int length)
static bool EncodeValueString(ReadOnlySpan< string > values, string separator, Encoding valueEncoding, Span< byte > buffer, out int length)
static byte[] EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(int index, string value)
static bool EncodeLiteralHeaderFieldWithStaticNameReference(int index, string value, Span< byte > destination, out int bytesWritten)
static bool EncodeLiteralHeaderFieldWithoutNameReference(string name, string value, Encoding valueEncoding, Span< byte > destination, out int bytesWritten)
static bool EncodeNameString(string s, Span< byte > buffer, out int length)
static bool EncodeLiteralHeaderFieldWithoutNameReference(string name, string value, Span< byte > destination, out int bytesWritten)
static void EncodeValueStringPart(string s, Span< byte > buffer)
static byte[] EncodeLiteralHeaderFieldWithoutNameReferenceToArray(string name, string value)
static byte[] EncodeLiteralHeaderFieldWithoutNameReferenceToArray(string name)
static byte[] EncodeStaticIndexedHeaderFieldToArray(int index)
static bool EncodeLiteralHeaderFieldWithStaticNameReference(int index, string value, Encoding valueEncoding, Span< byte > destination, out int bytesWritten)
static bool EncodeLiteralHeaderFieldWithoutNameReference(string name, ReadOnlySpan< string > values, string valueSeparator, Encoding valueEncoding, Span< byte > destination, out int bytesWritten)
static byte[] EncodeLiteralHeaderFieldWithStaticNameReferenceToArray(int index)
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