This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
JsonWriterHelper.Escaping.cs
362 lines (317 loc) · 14.7 KB
/
JsonWriterHelper.Escaping.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Buffers;
using System.Buffers.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Encodings.Web;
namespace System.Text.Json
{
// TODO: Replace the escaping logic with publicly shipping APIs from https://github.com/dotnet/corefx/issues/33509
internal static partial class JsonWriterHelper
{
// Only allow ASCII characters between ' ' (0x20) and '~' (0x7E), inclusively,
// but exclude characters that need to be escaped as hex: '"', '\'', '&', '+', '<', '>', '`'
// and exclude characters that need to be escaped by adding a backslash: '\n', '\r', '\t', '\\', '\b', '\f'
//
// non-zero = allowed, 0 = disallowed
public const int LastAsciiCharacter = 0x7F;
private static ReadOnlySpan<byte> AllowList => new byte[byte.MaxValue + 1]
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // U+0000..U+000F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // U+0010..U+001F
1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, // U+0020..U+002F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, // U+0030..U+003F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // U+0040..U+004F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // U+0050..U+005F
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // U+0060..U+006F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // U+0070..U+007F
// Also include the ranges from U+0080 to U+00FF for performance to avoid UTF8 code from checking boundary.
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // U+00F0..U+00FF
};
#if BUILDING_INBOX_LIBRARY
private const string HexFormatString = "X4";
#endif
private static readonly StandardFormat s_hexStandardFormat = new StandardFormat('X', 4);
private static bool NeedsEscaping(byte value) => AllowList[value] == 0;
private static bool NeedsEscapingNoBoundsCheck(char value) => AllowList[value] == 0;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool NeedsEscaping(char value) => value > LastAsciiCharacter || AllowList[value] == 0;
public static int NeedsEscaping(ReadOnlySpan<byte> value, JavaScriptEncoder encoder)
{
int idx;
if (encoder != null)
{
idx = encoder.FindFirstCharacterToEncodeUtf8(value);
goto Return;
}
for (idx = 0; idx < value.Length; idx++)
{
if (NeedsEscaping(value[idx]))
{
goto Return;
}
}
idx = -1; // all characters allowed
Return:
return idx;
}
public static unsafe int NeedsEscaping(ReadOnlySpan<char> value, JavaScriptEncoder encoder)
{
int idx;
// Some implementations of JavascriptEncoder.FindFirstCharacterToEncode may not accept
// null pointers and gaurd against that. Hence, check up-front and fall down to return -1.
if (encoder != null && !value.IsEmpty)
{
fixed (char* ptr = value)
{
idx = encoder.FindFirstCharacterToEncode(ptr, value.Length);
}
goto Return;
}
for (idx = 0; idx < value.Length; idx++)
{
if (NeedsEscaping(value[idx]))
{
goto Return;
}
}
idx = -1; // all characters allowed
Return:
return idx;
}
public static int GetMaxEscapedLength(int textLength, int firstIndexToEscape)
{
Debug.Assert(textLength > 0);
Debug.Assert(firstIndexToEscape >= 0 && firstIndexToEscape < textLength);
return firstIndexToEscape + JsonConstants.MaxExpansionFactorWhileEscaping * (textLength - firstIndexToEscape);
}
private static void EscapeString(ReadOnlySpan<byte> value, Span<byte> destination, JavaScriptEncoder encoder, ref int written)
{
Debug.Assert(encoder != null);
OperationStatus result = encoder.EncodeUtf8(value, destination, out int encoderBytesConsumed, out int encoderBytesWritten);
Debug.Assert(result != OperationStatus.DestinationTooSmall);
Debug.Assert(result != OperationStatus.NeedMoreData);
if (result != OperationStatus.Done)
{
ThrowHelper.ThrowArgumentException_InvalidUTF8(value.Slice(encoderBytesWritten));
}
Debug.Assert(encoderBytesConsumed == value.Length);
written += encoderBytesWritten;
}
public static void EscapeString(ReadOnlySpan<byte> value, Span<byte> destination, int indexOfFirstByteToEscape, JavaScriptEncoder encoder, out int written)
{
Debug.Assert(indexOfFirstByteToEscape >= 0 && indexOfFirstByteToEscape < value.Length);
value.Slice(0, indexOfFirstByteToEscape).CopyTo(destination);
written = indexOfFirstByteToEscape;
if (encoder != null)
{
destination = destination.Slice(indexOfFirstByteToEscape);
value = value.Slice(indexOfFirstByteToEscape);
EscapeString(value, destination, encoder, ref written);
}
else
{
// For performance when no encoder is specified, perform escaping here for Ascii and on the
// first occurrence of a non-Ascii character, then call into the default encoder.
while (indexOfFirstByteToEscape < value.Length)
{
byte val = value[indexOfFirstByteToEscape];
if (IsAsciiValue(val))
{
if (NeedsEscaping(val))
{
EscapeNextBytes(val, destination, ref written);
indexOfFirstByteToEscape++;
}
else
{
destination[written] = val;
written++;
indexOfFirstByteToEscape++;
}
}
else
{
// Fall back to default encoder.
destination = destination.Slice(written);
value = value.Slice(indexOfFirstByteToEscape);
EscapeString(value, destination, JavaScriptEncoder.Default, ref written);
break;
}
}
}
}
private static void EscapeNextBytes(byte value, Span<byte> destination, ref int written)
{
destination[written++] = (byte)'\\';
switch (value)
{
case JsonConstants.Quote:
// Optimize for the common quote case.
destination[written++] = (byte)'u';
destination[written++] = (byte)'0';
destination[written++] = (byte)'0';
destination[written++] = (byte)'2';
destination[written++] = (byte)'2';
break;
case JsonConstants.LineFeed:
destination[written++] = (byte)'n';
break;
case JsonConstants.CarriageReturn:
destination[written++] = (byte)'r';
break;
case JsonConstants.Tab:
destination[written++] = (byte)'t';
break;
case JsonConstants.BackSlash:
destination[written++] = (byte)'\\';
break;
case JsonConstants.BackSpace:
destination[written++] = (byte)'b';
break;
case JsonConstants.FormFeed:
destination[written++] = (byte)'f';
break;
default:
destination[written++] = (byte)'u';
bool result = Utf8Formatter.TryFormat(value, destination.Slice(written), out int bytesWritten, format: s_hexStandardFormat);
Debug.Assert(result);
Debug.Assert(bytesWritten == 4);
written += bytesWritten;
break;
}
}
private static bool IsAsciiValue(byte value) => value <= LastAsciiCharacter;
private static bool IsAsciiValue(char value) => value <= LastAsciiCharacter;
private static void EscapeString(ReadOnlySpan<char> value, Span<char> destination, JavaScriptEncoder encoder, ref int written)
{
Debug.Assert(encoder != null);
OperationStatus result = encoder.Encode(value, destination, out int encoderBytesConsumed, out int encoderCharsWritten);
Debug.Assert(result != OperationStatus.DestinationTooSmall);
Debug.Assert(result != OperationStatus.NeedMoreData);
if (result != OperationStatus.Done)
{
ThrowHelper.ThrowArgumentException_InvalidUTF16(value[encoderCharsWritten]);
}
Debug.Assert(encoderBytesConsumed == value.Length);
written += encoderCharsWritten;
}
public static void EscapeString(ReadOnlySpan<char> value, Span<char> destination, int indexOfFirstByteToEscape, JavaScriptEncoder encoder, out int written)
{
Debug.Assert(indexOfFirstByteToEscape >= 0 && indexOfFirstByteToEscape < value.Length);
value.Slice(0, indexOfFirstByteToEscape).CopyTo(destination);
written = indexOfFirstByteToEscape;
if (encoder != null)
{
destination = destination.Slice(indexOfFirstByteToEscape);
value = value.Slice(indexOfFirstByteToEscape);
EscapeString(value, destination, encoder, ref written);
}
else
{
// For performance when no encoder is specified, perform escaping here for Ascii and on the
// first occurrence of a non-Ascii character, then call into the default encoder.
while (indexOfFirstByteToEscape < value.Length)
{
char val = value[indexOfFirstByteToEscape];
if (IsAsciiValue(val))
{
if (NeedsEscapingNoBoundsCheck(val))
{
EscapeNextChars(val, destination, ref written);
indexOfFirstByteToEscape++;
}
else
{
destination[written] = val;
written++;
indexOfFirstByteToEscape++;
}
}
else
{
// Fall back to default encoder.
destination = destination.Slice(written);
value = value.Slice(indexOfFirstByteToEscape);
EscapeString(value, destination, JavaScriptEncoder.Default, ref written);
break;
}
}
}
}
private static void EscapeNextChars(char value, Span<char> destination, ref int written)
{
Debug.Assert(IsAsciiValue(value));
destination[written++] = '\\';
switch ((byte)value)
{
case JsonConstants.Quote:
// Optimize for the common quote case.
destination[written++] = 'u';
destination[written++] = '0';
destination[written++] = '0';
destination[written++] = '2';
destination[written++] = '2';
break;
case JsonConstants.LineFeed:
destination[written++] = 'n';
break;
case JsonConstants.CarriageReturn:
destination[written++] = 'r';
break;
case JsonConstants.Tab:
destination[written++] = 't';
break;
case JsonConstants.BackSlash:
destination[written++] = '\\';
break;
case JsonConstants.BackSpace:
destination[written++] = 'b';
break;
case JsonConstants.FormFeed:
destination[written++] = 'f';
break;
default:
destination[written++] = 'u';
#if BUILDING_INBOX_LIBRARY
int intChar = value;
intChar.TryFormat(destination.Slice(written), out int charsWritten, HexFormatString);
Debug.Assert(charsWritten == 4);
written += charsWritten;
#else
written = WriteHex(value, destination, written);
#endif
break;
}
}
#if !BUILDING_INBOX_LIBRARY
private static int WriteHex(int value, Span<char> destination, int written)
{
destination[written++] = (char)Int32LsbToHexDigit(value >> 12);
destination[written++] = (char)Int32LsbToHexDigit((int)((value >> 8) & 0xFU));
destination[written++] = (char)Int32LsbToHexDigit((int)((value >> 4) & 0xFU));
destination[written++] = (char)Int32LsbToHexDigit((int)(value & 0xFU));
return written;
}
/// <summary>
/// Converts a number 0 - 15 to its associated hex character '0' - 'F' as byte.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte Int32LsbToHexDigit(int value)
{
Debug.Assert(value < 16);
return (byte)((value < 10) ? ('0' + value) : ('A' + (value - 10)));
}
#endif
}
}