| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Buffers.Text; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Diagnostics.CodeAnalysis; |
| | | 8 | | using System.Text.Unicode; |
| | | 9 | | |
| | | 10 | | namespace System.Text.Json |
| | | 11 | | { |
| | | 12 | | internal static partial class JsonReaderHelper |
| | | 13 | | { |
| | | 14 | | public static bool TryGetUnescapedBase64Bytes(ReadOnlySpan<byte> utf8Source, [NotNullWhen(true)] out byte[]? byt |
| | 0 | 15 | | { |
| | 0 | 16 | | byte[]? unescapedArray = null; |
| | | 17 | | |
| | 0 | 18 | | Span<byte> utf8Unescaped = utf8Source.Length <= JsonConstants.StackallocByteThreshold ? |
| | 0 | 19 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 0 | 20 | | (unescapedArray = ArrayPool<byte>.Shared.Rent(utf8Source.Length)); |
| | | 21 | | |
| | 0 | 22 | | Unescape(utf8Source, utf8Unescaped, out int written); |
| | 0 | 23 | | Debug.Assert(written > 0); |
| | | 24 | | |
| | 0 | 25 | | utf8Unescaped = utf8Unescaped.Slice(0, written); |
| | 0 | 26 | | Debug.Assert(!utf8Unescaped.IsEmpty); |
| | | 27 | | |
| | 0 | 28 | | bool result = TryDecodeBase64InPlace(utf8Unescaped, out bytes!); |
| | | 29 | | |
| | 0 | 30 | | if (unescapedArray != null) |
| | 0 | 31 | | { |
| | 0 | 32 | | utf8Unescaped.Clear(); |
| | 0 | 33 | | ArrayPool<byte>.Shared.Return(unescapedArray); |
| | 0 | 34 | | } |
| | 0 | 35 | | return result; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | // Reject any invalid UTF-8 data rather than silently replacing. |
| | 1 | 39 | | public static readonly UTF8Encoding s_utf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, th |
| | | 40 | | |
| | | 41 | | // TODO: Similar to escaping, replace the unescaping logic with publicly shipping APIs from https://github.com/d |
| | | 42 | | public static string GetUnescapedString(ReadOnlySpan<byte> utf8Source) |
| | 0 | 43 | | { |
| | | 44 | | // The escaped name is always >= than the unescaped, so it is safe to use escaped name for the buffer length |
| | 0 | 45 | | int length = utf8Source.Length; |
| | 0 | 46 | | byte[]? pooledName = null; |
| | | 47 | | |
| | 0 | 48 | | Span<byte> utf8Unescaped = length <= JsonConstants.StackallocByteThreshold ? |
| | 0 | 49 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 0 | 50 | | (pooledName = ArrayPool<byte>.Shared.Rent(length)); |
| | | 51 | | |
| | 0 | 52 | | Unescape(utf8Source, utf8Unescaped, out int written); |
| | 0 | 53 | | Debug.Assert(written > 0); |
| | | 54 | | |
| | 0 | 55 | | utf8Unescaped = utf8Unescaped.Slice(0, written); |
| | 0 | 56 | | Debug.Assert(!utf8Unescaped.IsEmpty); |
| | | 57 | | |
| | 0 | 58 | | string utf8String = TranscodeHelper(utf8Unescaped); |
| | | 59 | | |
| | 0 | 60 | | if (pooledName != null) |
| | 0 | 61 | | { |
| | 0 | 62 | | utf8Unescaped.Clear(); |
| | 0 | 63 | | ArrayPool<byte>.Shared.Return(pooledName); |
| | 0 | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | return utf8String; |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | public static byte[] GetUnescaped(ReadOnlySpan<byte> utf8Source) |
| | 0 | 70 | | { |
| | | 71 | | // The escaped name is always >= than the unescaped, so it is safe to use escaped name for the buffer length |
| | 0 | 72 | | int length = utf8Source.Length; |
| | 0 | 73 | | byte[]? pooledName = null; |
| | | 74 | | |
| | 0 | 75 | | Span<byte> utf8Unescaped = length <= JsonConstants.StackallocByteThreshold ? |
| | 0 | 76 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 0 | 77 | | (pooledName = ArrayPool<byte>.Shared.Rent(length)); |
| | | 78 | | |
| | 0 | 79 | | Unescape(utf8Source, utf8Unescaped, out int written); |
| | 0 | 80 | | Debug.Assert(written > 0); |
| | | 81 | | |
| | 0 | 82 | | byte[] propertyName = utf8Unescaped.Slice(0, written).ToArray(); |
| | 0 | 83 | | Debug.Assert(propertyName.Length is not 0); |
| | | 84 | | |
| | 0 | 85 | | if (pooledName != null) |
| | 0 | 86 | | { |
| | 0 | 87 | | new Span<byte>(pooledName, 0, written).Clear(); |
| | 0 | 88 | | ArrayPool<byte>.Shared.Return(pooledName); |
| | 0 | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return propertyName; |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | public static bool UnescapeAndCompare(ReadOnlySpan<byte> utf8Source, ReadOnlySpan<byte> other) |
| | 0 | 95 | | { |
| | 0 | 96 | | Debug.Assert(utf8Source.Length >= other.Length && utf8Source.Length / JsonConstants.MaxExpansionFactorWhileE |
| | | 97 | | |
| | 0 | 98 | | byte[]? unescapedArray = null; |
| | | 99 | | |
| | 0 | 100 | | Span<byte> utf8Unescaped = utf8Source.Length <= JsonConstants.StackallocByteThreshold ? |
| | 0 | 101 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 0 | 102 | | (unescapedArray = ArrayPool<byte>.Shared.Rent(utf8Source.Length)); |
| | | 103 | | |
| | 0 | 104 | | Unescape(utf8Source, utf8Unescaped, 0, out int written); |
| | 0 | 105 | | Debug.Assert(written > 0); |
| | | 106 | | |
| | 0 | 107 | | utf8Unescaped = utf8Unescaped.Slice(0, written); |
| | 0 | 108 | | Debug.Assert(!utf8Unescaped.IsEmpty); |
| | | 109 | | |
| | 0 | 110 | | bool result = other.SequenceEqual(utf8Unescaped); |
| | | 111 | | |
| | 0 | 112 | | if (unescapedArray != null) |
| | 0 | 113 | | { |
| | 0 | 114 | | utf8Unescaped.Clear(); |
| | 0 | 115 | | ArrayPool<byte>.Shared.Return(unescapedArray); |
| | 0 | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | return result; |
| | 0 | 119 | | } |
| | | 120 | | |
| | | 121 | | public static bool UnescapeAndCompare(ReadOnlySequence<byte> utf8Source, ReadOnlySpan<byte> other) |
| | 0 | 122 | | { |
| | 0 | 123 | | Debug.Assert(!utf8Source.IsSingleSegment); |
| | 0 | 124 | | Debug.Assert(utf8Source.Length >= other.Length && utf8Source.Length / JsonConstants.MaxExpansionFactorWhileE |
| | | 125 | | |
| | 0 | 126 | | byte[]? escapedArray = null; |
| | 0 | 127 | | byte[]? unescapedArray = null; |
| | | 128 | | |
| | 0 | 129 | | int length = checked((int)utf8Source.Length); |
| | | 130 | | |
| | 0 | 131 | | Span<byte> utf8Unescaped = length <= JsonConstants.StackallocByteThreshold ? |
| | 0 | 132 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 0 | 133 | | (unescapedArray = ArrayPool<byte>.Shared.Rent(length)); |
| | | 134 | | |
| | 0 | 135 | | Span<byte> utf8Escaped = length <= JsonConstants.StackallocByteThreshold ? |
| | 0 | 136 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 0 | 137 | | (escapedArray = ArrayPool<byte>.Shared.Rent(length)); |
| | | 138 | | |
| | 0 | 139 | | utf8Source.CopyTo(utf8Escaped); |
| | 0 | 140 | | utf8Escaped = utf8Escaped.Slice(0, length); |
| | | 141 | | |
| | 0 | 142 | | Unescape(utf8Escaped, utf8Unescaped, 0, out int written); |
| | 0 | 143 | | Debug.Assert(written > 0); |
| | | 144 | | |
| | 0 | 145 | | utf8Unescaped = utf8Unescaped.Slice(0, written); |
| | 0 | 146 | | Debug.Assert(!utf8Unescaped.IsEmpty); |
| | | 147 | | |
| | 0 | 148 | | bool result = other.SequenceEqual(utf8Unescaped); |
| | | 149 | | |
| | 0 | 150 | | if (unescapedArray != null) |
| | 0 | 151 | | { |
| | 0 | 152 | | Debug.Assert(escapedArray != null); |
| | 0 | 153 | | utf8Unescaped.Clear(); |
| | 0 | 154 | | ArrayPool<byte>.Shared.Return(unescapedArray); |
| | 0 | 155 | | utf8Escaped.Clear(); |
| | 0 | 156 | | ArrayPool<byte>.Shared.Return(escapedArray); |
| | 0 | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | return result; |
| | 0 | 160 | | } |
| | | 161 | | |
| | | 162 | | public static bool UnescapeAndCompareBothInputs(ReadOnlySpan<byte> utf8Source1, ReadOnlySpan<byte> utf8Source2) |
| | 0 | 163 | | { |
| | 0 | 164 | | int index1 = utf8Source1.IndexOf(JsonConstants.BackSlash); |
| | 0 | 165 | | int index2 = utf8Source2.IndexOf(JsonConstants.BackSlash); |
| | | 166 | | |
| | 0 | 167 | | Debug.Assert(index1 >= 0, "the first parameter is not escaped"); |
| | 0 | 168 | | Debug.Assert(index2 >= 0, "the second parameter is not escaped"); |
| | | 169 | | |
| | 0 | 170 | | byte[]? unescapedArray1 = null; |
| | 0 | 171 | | byte[]? unescapedArray2 = null; |
| | | 172 | | |
| | 0 | 173 | | Span<byte> utf8Unescaped1 = utf8Source1.Length <= JsonConstants.StackallocByteThreshold ? |
| | 0 | 174 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 0 | 175 | | (unescapedArray1 = ArrayPool<byte>.Shared.Rent(utf8Source1.Length)); |
| | | 176 | | |
| | 0 | 177 | | Span<byte> utf8Unescaped2 = utf8Source2.Length <= JsonConstants.StackallocByteThreshold ? |
| | 0 | 178 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 0 | 179 | | (unescapedArray2 = ArrayPool<byte>.Shared.Rent(utf8Source2.Length)); |
| | | 180 | | |
| | 0 | 181 | | Unescape(utf8Source1, utf8Unescaped1, index1, out int written); |
| | 0 | 182 | | utf8Unescaped1 = utf8Unescaped1.Slice(0, written); |
| | 0 | 183 | | Debug.Assert(!utf8Unescaped1.IsEmpty); |
| | | 184 | | |
| | 0 | 185 | | Unescape(utf8Source2, utf8Unescaped2, index2, out written); |
| | 0 | 186 | | utf8Unescaped2 = utf8Unescaped2.Slice(0, written); |
| | 0 | 187 | | Debug.Assert(!utf8Unescaped2.IsEmpty); |
| | | 188 | | |
| | 0 | 189 | | bool result = utf8Unescaped1.SequenceEqual(utf8Unescaped2); |
| | | 190 | | |
| | 0 | 191 | | if (unescapedArray1 != null) |
| | 0 | 192 | | { |
| | 0 | 193 | | utf8Unescaped1.Clear(); |
| | 0 | 194 | | ArrayPool<byte>.Shared.Return(unescapedArray1); |
| | 0 | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | if (unescapedArray2 != null) |
| | 0 | 198 | | { |
| | 0 | 199 | | utf8Unescaped2.Clear(); |
| | 0 | 200 | | ArrayPool<byte>.Shared.Return(unescapedArray2); |
| | 0 | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | return result; |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | public static bool TryDecodeBase64InPlace(Span<byte> utf8Unescaped, [NotNullWhen(true)] out byte[]? bytes) |
| | 0 | 207 | | { |
| | 0 | 208 | | OperationStatus status = Base64.DecodeFromUtf8InPlace(utf8Unescaped, out int bytesWritten); |
| | 0 | 209 | | if (status != OperationStatus.Done) |
| | 0 | 210 | | { |
| | 0 | 211 | | bytes = null; |
| | 0 | 212 | | return false; |
| | | 213 | | } |
| | 0 | 214 | | bytes = utf8Unescaped.Slice(0, bytesWritten).ToArray(); |
| | 0 | 215 | | return true; |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | public static bool TryDecodeBase64(ReadOnlySpan<byte> utf8Unescaped, [NotNullWhen(true)] out byte[]? bytes) |
| | 834 | 219 | | { |
| | 834 | 220 | | byte[]? pooledArray = null; |
| | | 221 | | |
| | 834 | 222 | | Span<byte> byteSpan = utf8Unescaped.Length <= JsonConstants.StackallocByteThreshold ? |
| | 834 | 223 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 834 | 224 | | (pooledArray = ArrayPool<byte>.Shared.Rent(utf8Unescaped.Length)); |
| | | 225 | | |
| | 834 | 226 | | OperationStatus status = Base64.DecodeFromUtf8(utf8Unescaped, byteSpan, out int bytesConsumed, out int bytes |
| | | 227 | | |
| | 834 | 228 | | if (status != OperationStatus.Done) |
| | 474 | 229 | | { |
| | 474 | 230 | | bytes = null; |
| | | 231 | | |
| | 474 | 232 | | if (pooledArray != null) |
| | 0 | 233 | | { |
| | 0 | 234 | | byteSpan.Clear(); |
| | 0 | 235 | | ArrayPool<byte>.Shared.Return(pooledArray); |
| | 0 | 236 | | } |
| | | 237 | | |
| | 474 | 238 | | return false; |
| | | 239 | | } |
| | 360 | 240 | | Debug.Assert(bytesConsumed == utf8Unescaped.Length); |
| | | 241 | | |
| | 360 | 242 | | bytes = byteSpan.Slice(0, bytesWritten).ToArray(); |
| | | 243 | | |
| | 360 | 244 | | if (pooledArray != null) |
| | 0 | 245 | | { |
| | 0 | 246 | | byteSpan.Clear(); |
| | 0 | 247 | | ArrayPool<byte>.Shared.Return(pooledArray); |
| | 0 | 248 | | } |
| | | 249 | | |
| | 360 | 250 | | return true; |
| | 834 | 251 | | } |
| | | 252 | | |
| | | 253 | | public static string TranscodeHelper(ReadOnlySpan<byte> utf8Unescaped) |
| | 842 | 254 | | { |
| | | 255 | | try |
| | 842 | 256 | | { |
| | 842 | 257 | | return s_utf8Encoding.GetString(utf8Unescaped); |
| | | 258 | | } |
| | 310 | 259 | | catch (DecoderFallbackException ex) |
| | 310 | 260 | | { |
| | | 261 | | // We want to be consistent with the exception being thrown |
| | | 262 | | // so the user only has to catch a single exception. |
| | | 263 | | // Since we already throw InvalidOperationException for mismatch token type, |
| | | 264 | | // and while unescaping, using that exception for failure to decode invalid UTF-8 bytes as well. |
| | | 265 | | // Therefore, wrapping the DecoderFallbackException around an InvalidOperationException. |
| | 310 | 266 | | throw ThrowHelper.GetInvalidOperationException_ReadInvalidUTF8(ex); |
| | | 267 | | } |
| | 532 | 268 | | } |
| | | 269 | | |
| | | 270 | | public static int TranscodeHelper(ReadOnlySpan<byte> utf8Unescaped, Span<char> destination) |
| | 542 | 271 | | { |
| | | 272 | | try |
| | 542 | 273 | | { |
| | 542 | 274 | | return s_utf8Encoding.GetChars(utf8Unescaped, destination); |
| | | 275 | | } |
| | 246 | 276 | | catch (DecoderFallbackException dfe) |
| | 246 | 277 | | { |
| | | 278 | | // We want to be consistent with the exception being thrown |
| | | 279 | | // so the user only has to catch a single exception. |
| | | 280 | | // Since we already throw InvalidOperationException for mismatch token type, |
| | | 281 | | // and while unescaping, using that exception for failure to decode invalid UTF-8 bytes as well. |
| | | 282 | | // Therefore, wrapping the DecoderFallbackException around an InvalidOperationException. |
| | 246 | 283 | | throw ThrowHelper.GetInvalidOperationException_ReadInvalidUTF8(dfe); |
| | | 284 | | } |
| | 0 | 285 | | catch (ArgumentException) |
| | 0 | 286 | | { |
| | | 287 | | // Destination buffer was too small; clear it up since the encoder might have not. |
| | 0 | 288 | | destination.Clear(); |
| | 0 | 289 | | throw; |
| | | 290 | | } |
| | 296 | 291 | | } |
| | | 292 | | |
| | | 293 | | public static void ValidateUtf8(ReadOnlySpan<byte> utf8Buffer) |
| | 1547 | 294 | | { |
| | | 295 | | #if NET |
| | 1547 | 296 | | if (!Utf8.IsValid(utf8Buffer)) |
| | 416 | 297 | | { |
| | 416 | 298 | | throw ThrowHelper.GetInvalidOperationException_ReadInvalidUTF8(); |
| | | 299 | | } |
| | | 300 | | #else |
| | | 301 | | try |
| | | 302 | | { |
| | | 303 | | _ = s_utf8Encoding.GetCharCount(utf8Buffer); |
| | | 304 | | } |
| | | 305 | | catch (DecoderFallbackException ex) |
| | | 306 | | { |
| | | 307 | | // We want to be consistent with the exception being thrown |
| | | 308 | | // so the user only has to catch a single exception. |
| | | 309 | | // Since we already throw InvalidOperationException for mismatch token type, |
| | | 310 | | // and while unescaping, using that exception for failure to decode invalid UTF-8 bytes as well. |
| | | 311 | | // Therefore, wrapping the DecoderFallbackException around an InvalidOperationException. |
| | | 312 | | throw ThrowHelper.GetInvalidOperationException_ReadInvalidUTF8(ex); |
| | | 313 | | } |
| | | 314 | | #endif |
| | 1131 | 315 | | } |
| | | 316 | | |
| | | 317 | | internal static int GetUtf8ByteCount(ReadOnlySpan<char> text) |
| | 4 | 318 | | { |
| | | 319 | | try |
| | 4 | 320 | | { |
| | 4 | 321 | | return s_utf8Encoding.GetByteCount(text); |
| | | 322 | | } |
| | 0 | 323 | | catch (EncoderFallbackException ex) |
| | 0 | 324 | | { |
| | | 325 | | // We want to be consistent with the exception being thrown |
| | | 326 | | // so the user only has to catch a single exception. |
| | | 327 | | // Since we already throw ArgumentException when validating other arguments, |
| | | 328 | | // using that exception for failure to encode invalid UTF-16 chars as well. |
| | | 329 | | // Therefore, wrapping the EncoderFallbackException around an ArgumentException. |
| | 0 | 330 | | throw ThrowHelper.GetArgumentException_ReadInvalidUTF16(ex); |
| | | 331 | | } |
| | 4 | 332 | | } |
| | | 333 | | |
| | | 334 | | internal static int GetUtf8FromText(ReadOnlySpan<char> text, Span<byte> dest) |
| | 4 | 335 | | { |
| | | 336 | | try |
| | 4 | 337 | | { |
| | 4 | 338 | | return s_utf8Encoding.GetBytes(text, dest); |
| | | 339 | | } |
| | 0 | 340 | | catch (EncoderFallbackException ex) |
| | 0 | 341 | | { |
| | | 342 | | // We want to be consistent with the exception being thrown |
| | | 343 | | // so the user only has to catch a single exception. |
| | | 344 | | // Since we already throw ArgumentException when validating other arguments, |
| | | 345 | | // using that exception for failure to encode invalid UTF-16 chars as well. |
| | | 346 | | // Therefore, wrapping the EncoderFallbackException around an ArgumentException. |
| | 0 | 347 | | throw ThrowHelper.GetArgumentException_ReadInvalidUTF16(ex); |
| | | 348 | | } |
| | 4 | 349 | | } |
| | | 350 | | |
| | | 351 | | internal static string GetTextFromUtf8(ReadOnlySpan<byte> utf8Text) |
| | 4 | 352 | | { |
| | 4 | 353 | | return s_utf8Encoding.GetString(utf8Text); |
| | 4 | 354 | | } |
| | | 355 | | |
| | | 356 | | internal static void Unescape(ReadOnlySpan<byte> source, Span<byte> destination, out int written) |
| | 0 | 357 | | { |
| | 0 | 358 | | Debug.Assert(destination.Length >= source.Length); |
| | | 359 | | |
| | 0 | 360 | | int idx = source.IndexOf(JsonConstants.BackSlash); |
| | 0 | 361 | | Debug.Assert(idx >= 0); |
| | | 362 | | |
| | 0 | 363 | | bool result = TryUnescape(source, destination, idx, out written); |
| | 0 | 364 | | Debug.Assert(result); |
| | 0 | 365 | | } |
| | | 366 | | |
| | | 367 | | internal static void Unescape(ReadOnlySpan<byte> source, Span<byte> destination, int idx, out int written) |
| | 0 | 368 | | { |
| | 0 | 369 | | Debug.Assert(idx >= 0 && idx < source.Length); |
| | 0 | 370 | | Debug.Assert(source[idx] == JsonConstants.BackSlash); |
| | 0 | 371 | | Debug.Assert(destination.Length >= source.Length); |
| | | 372 | | |
| | 0 | 373 | | bool result = TryUnescape(source, destination, idx, out written); |
| | 0 | 374 | | Debug.Assert(result); |
| | 0 | 375 | | } |
| | | 376 | | |
| | | 377 | | /// <summary> |
| | | 378 | | /// Used when writing to buffers not guaranteed to fit the unescaped result. |
| | | 379 | | /// </summary> |
| | | 380 | | internal static bool TryUnescape(ReadOnlySpan<byte> source, Span<byte> destination, out int written) |
| | 0 | 381 | | { |
| | 0 | 382 | | int idx = source.IndexOf(JsonConstants.BackSlash); |
| | 0 | 383 | | Debug.Assert(idx >= 0); |
| | | 384 | | |
| | 0 | 385 | | return TryUnescape(source, destination, idx, out written); |
| | 0 | 386 | | } |
| | | 387 | | |
| | | 388 | | /// <summary> |
| | | 389 | | /// Used when writing to buffers not guaranteed to fit the unescaped result. |
| | | 390 | | /// </summary> |
| | | 391 | | private static bool TryUnescape(ReadOnlySpan<byte> source, Span<byte> destination, int idx, out int written) |
| | 0 | 392 | | { |
| | 0 | 393 | | Debug.Assert(idx >= 0 && idx < source.Length); |
| | 0 | 394 | | Debug.Assert(source[idx] == JsonConstants.BackSlash); |
| | | 395 | | |
| | 0 | 396 | | if (!source.Slice(0, idx).TryCopyTo(destination)) |
| | 0 | 397 | | { |
| | 0 | 398 | | written = 0; |
| | 0 | 399 | | goto DestinationTooShort; |
| | | 400 | | } |
| | | 401 | | |
| | 0 | 402 | | written = idx; |
| | | 403 | | |
| | 0 | 404 | | while (true) |
| | 0 | 405 | | { |
| | 0 | 406 | | Debug.Assert(source[idx] == JsonConstants.BackSlash); |
| | | 407 | | |
| | 0 | 408 | | if (written == destination.Length) |
| | 0 | 409 | | { |
| | 0 | 410 | | goto DestinationTooShort; |
| | | 411 | | } |
| | | 412 | | |
| | 0 | 413 | | switch (source[++idx]) |
| | | 414 | | { |
| | | 415 | | case JsonConstants.Quote: |
| | 0 | 416 | | destination[written++] = JsonConstants.Quote; |
| | 0 | 417 | | break; |
| | | 418 | | case (byte)'n': |
| | 0 | 419 | | destination[written++] = JsonConstants.LineFeed; |
| | 0 | 420 | | break; |
| | | 421 | | case (byte)'r': |
| | 0 | 422 | | destination[written++] = JsonConstants.CarriageReturn; |
| | 0 | 423 | | break; |
| | | 424 | | case JsonConstants.BackSlash: |
| | 0 | 425 | | destination[written++] = JsonConstants.BackSlash; |
| | 0 | 426 | | break; |
| | | 427 | | case JsonConstants.Slash: |
| | 0 | 428 | | destination[written++] = JsonConstants.Slash; |
| | 0 | 429 | | break; |
| | | 430 | | case (byte)'t': |
| | 0 | 431 | | destination[written++] = JsonConstants.Tab; |
| | 0 | 432 | | break; |
| | | 433 | | case (byte)'b': |
| | 0 | 434 | | destination[written++] = JsonConstants.BackSpace; |
| | 0 | 435 | | break; |
| | | 436 | | case (byte)'f': |
| | 0 | 437 | | destination[written++] = JsonConstants.FormFeed; |
| | 0 | 438 | | break; |
| | | 439 | | default: |
| | 0 | 440 | | Debug.Assert(source[idx] == 'u', "invalid escape sequences must have already been caught by Utf8 |
| | | 441 | | |
| | | 442 | | // The source is known to be valid JSON, and hence if we see a \u, it is guaranteed to have 4 he |
| | | 443 | | // Otherwise, the Utf8JsonReader would have already thrown an exception. |
| | 0 | 444 | | Debug.Assert(source.Length >= idx + 5); |
| | | 445 | | |
| | 0 | 446 | | bool result = Utf8Parser.TryParse(source.Slice(idx + 1, 4), out int scalar, out int bytesConsume |
| | 0 | 447 | | Debug.Assert(result); |
| | 0 | 448 | | Debug.Assert(bytesConsumed == 4); |
| | 0 | 449 | | idx += 4; |
| | | 450 | | |
| | 0 | 451 | | if (JsonHelpers.IsInRangeInclusive((uint)scalar, JsonConstants.HighSurrogateStartValue, JsonCons |
| | 0 | 452 | | { |
| | | 453 | | // The first hex value cannot be a low surrogate. |
| | 0 | 454 | | if (scalar >= JsonConstants.LowSurrogateStartValue) |
| | 0 | 455 | | { |
| | 0 | 456 | | ThrowHelper.ThrowInvalidOperationException_ReadInvalidUTF16(scalar); |
| | | 457 | | } |
| | | 458 | | |
| | 0 | 459 | | Debug.Assert(JsonHelpers.IsInRangeInclusive((uint)scalar, JsonConstants.HighSurrogateStartVa |
| | | 460 | | |
| | | 461 | | // We must have a low surrogate following a high surrogate. |
| | 0 | 462 | | if (source.Length < idx + 7 || source[idx + 1] != '\\' || source[idx + 2] != 'u') |
| | 0 | 463 | | { |
| | 0 | 464 | | ThrowHelper.ThrowInvalidOperationException_ReadIncompleteUTF16(); |
| | | 465 | | } |
| | | 466 | | |
| | | 467 | | // The source is known to be valid JSON, and hence if we see a \u, it is guaranteed to have |
| | | 468 | | // Otherwise, the Utf8JsonReader would have already thrown an exception. |
| | 0 | 469 | | result = Utf8Parser.TryParse(source.Slice(idx + 3, 4), out int lowSurrogate, out bytesConsum |
| | 0 | 470 | | Debug.Assert(result); |
| | 0 | 471 | | Debug.Assert(bytesConsumed == 4); |
| | 0 | 472 | | idx += 6; |
| | | 473 | | |
| | | 474 | | // If the first hex value is a high surrogate, the next one must be a low surrogate. |
| | 0 | 475 | | if (!JsonHelpers.IsInRangeInclusive((uint)lowSurrogate, JsonConstants.LowSurrogateStartValue |
| | 0 | 476 | | { |
| | 0 | 477 | | ThrowHelper.ThrowInvalidOperationException_ReadInvalidUTF16(lowSurrogate); |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | // To find the unicode scalar: |
| | | 481 | | // (0x400 * (High surrogate - 0xD800)) + Low surrogate - 0xDC00 + 0x10000 |
| | 0 | 482 | | scalar = (JsonConstants.BitShiftBy10 * (scalar - JsonConstants.HighSurrogateStartValue)) |
| | 0 | 483 | | + (lowSurrogate - JsonConstants.LowSurrogateStartValue) |
| | 0 | 484 | | + JsonConstants.UnicodePlane01StartValue; |
| | 0 | 485 | | } |
| | | 486 | | |
| | 0 | 487 | | var rune = new Rune(scalar); |
| | 0 | 488 | | bool success = rune.TryEncodeToUtf8(destination.Slice(written), out int bytesWritten); |
| | 0 | 489 | | if (!success) |
| | 0 | 490 | | { |
| | 0 | 491 | | goto DestinationTooShort; |
| | | 492 | | } |
| | | 493 | | |
| | 0 | 494 | | Debug.Assert(bytesWritten <= 4); |
| | 0 | 495 | | written += bytesWritten; |
| | 0 | 496 | | break; |
| | | 497 | | } |
| | | 498 | | |
| | 0 | 499 | | if (++idx == source.Length) |
| | 0 | 500 | | { |
| | 0 | 501 | | goto Success; |
| | | 502 | | } |
| | | 503 | | |
| | 0 | 504 | | if (source[idx] != JsonConstants.BackSlash) |
| | 0 | 505 | | { |
| | 0 | 506 | | ReadOnlySpan<byte> remaining = source.Slice(idx); |
| | 0 | 507 | | int nextUnescapedSegmentLength = remaining.IndexOf(JsonConstants.BackSlash); |
| | 0 | 508 | | if (nextUnescapedSegmentLength < 0) |
| | 0 | 509 | | { |
| | 0 | 510 | | nextUnescapedSegmentLength = remaining.Length; |
| | 0 | 511 | | } |
| | | 512 | | |
| | 0 | 513 | | if ((uint)(written + nextUnescapedSegmentLength) >= (uint)destination.Length) |
| | 0 | 514 | | { |
| | 0 | 515 | | goto DestinationTooShort; |
| | | 516 | | } |
| | | 517 | | |
| | 0 | 518 | | Debug.Assert(nextUnescapedSegmentLength > 0); |
| | 0 | 519 | | switch (nextUnescapedSegmentLength) |
| | | 520 | | { |
| | | 521 | | case 1: |
| | 0 | 522 | | destination[written++] = source[idx++]; |
| | 0 | 523 | | break; |
| | | 524 | | case 2: |
| | 0 | 525 | | destination[written++] = source[idx++]; |
| | 0 | 526 | | destination[written++] = source[idx++]; |
| | 0 | 527 | | break; |
| | | 528 | | case 3: |
| | 0 | 529 | | destination[written++] = source[idx++]; |
| | 0 | 530 | | destination[written++] = source[idx++]; |
| | 0 | 531 | | destination[written++] = source[idx++]; |
| | 0 | 532 | | break; |
| | | 533 | | default: |
| | 0 | 534 | | remaining.Slice(0, nextUnescapedSegmentLength).CopyTo(destination.Slice(written)); |
| | 0 | 535 | | written += nextUnescapedSegmentLength; |
| | 0 | 536 | | idx += nextUnescapedSegmentLength; |
| | 0 | 537 | | break; |
| | | 538 | | } |
| | | 539 | | |
| | 0 | 540 | | Debug.Assert(idx == source.Length || source[idx] == JsonConstants.BackSlash); |
| | | 541 | | |
| | 0 | 542 | | if (idx == source.Length) |
| | 0 | 543 | | { |
| | 0 | 544 | | goto Success; |
| | | 545 | | } |
| | 0 | 546 | | } |
| | 0 | 547 | | } |
| | | 548 | | |
| | 0 | 549 | | Success: |
| | 0 | 550 | | return true; |
| | | 551 | | |
| | 0 | 552 | | DestinationTooShort: |
| | 0 | 553 | | return false; |
| | 0 | 554 | | } |
| | | 555 | | } |
| | | 556 | | } |