| | | 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.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Globalization; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using System.Runtime.Serialization; |
| | | 8 | | |
| | | 9 | | namespace System.Text |
| | | 10 | | { |
| | | 11 | | public sealed class DecoderExceptionFallback : DecoderFallback |
| | | 12 | | { |
| | | 13 | | internal static readonly DecoderExceptionFallback s_default = new DecoderExceptionFallback(); |
| | | 14 | | |
| | | 15 | | public override DecoderFallbackBuffer CreateFallbackBuffer() => |
| | | 16 | | new DecoderExceptionFallbackBuffer(); |
| | | 17 | | |
| | | 18 | | // Maximum number of characters that this instance of this fallback could return |
| | | 19 | | public override int MaxCharCount => 0; |
| | | 20 | | |
| | | 21 | | public override bool Equals([NotNullWhen(true)] object? value) => |
| | | 22 | | value is DecoderExceptionFallback; |
| | | 23 | | |
| | | 24 | | public override int GetHashCode() => 879; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | |
| | | 28 | | public sealed class DecoderExceptionFallbackBuffer : DecoderFallbackBuffer |
| | | 29 | | { |
| | | 30 | | public override bool Fallback(byte[] bytesUnknown, int index) |
| | | 31 | | { |
| | 8713 | 32 | | Throw(bytesUnknown, index); |
| | | 33 | | return true; |
| | | 34 | | } |
| | | 35 | | |
| | 76 | 36 | | public override char GetNextChar() => (char)0; |
| | | 37 | | |
| | | 38 | | // Exception fallback doesn't have anywhere to back up to. |
| | 0 | 39 | | public override bool MovePrevious() => false; |
| | | 40 | | |
| | | 41 | | // Exceptions are always empty |
| | 3669 | 42 | | public override int Remaining => 0; |
| | | 43 | | |
| | | 44 | | [DoesNotReturn] |
| | | 45 | | private static void Throw(byte[] bytesUnknown, int index) |
| | | 46 | | { |
| | 8713 | 47 | | bytesUnknown ??= []; |
| | | 48 | | |
| | | 49 | | // Create a string representation of our bytes. |
| | 8713 | 50 | | StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 4); |
| | | 51 | | |
| | | 52 | | const int MaxLength = 20; |
| | 57456 | 53 | | for (int i = 0; i < bytesUnknown.Length && i < MaxLength; i++) |
| | | 54 | | { |
| | 20015 | 55 | | strBytes.Append($"[{bytesUnknown[i]:X2}]"); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // In case the string's really long |
| | 8713 | 59 | | if (bytesUnknown.Length > MaxLength) |
| | | 60 | | { |
| | 0 | 61 | | strBytes.Append(" ..."); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | // Known index |
| | 8713 | 65 | | throw new DecoderFallbackException( |
| | 8713 | 66 | | SR.Format(SR.Argument_InvalidCodePageBytesIndex, |
| | 8713 | 67 | | strBytes, index), bytesUnknown, index); |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // Exception for decoding unknown byte sequences. |
| | | 72 | | [Serializable] |
| | | 73 | | [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] |
| | | 74 | | public sealed class DecoderFallbackException : ArgumentException |
| | | 75 | | { |
| | | 76 | | private readonly byte[]? _bytesUnknown; |
| | | 77 | | private readonly int _index; |
| | | 78 | | |
| | | 79 | | public DecoderFallbackException() |
| | | 80 | | : base(SR.Arg_ArgumentException) |
| | | 81 | | { |
| | | 82 | | HResult = HResults.COR_E_ARGUMENT; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | public DecoderFallbackException(string? message) |
| | | 86 | | : base(message) |
| | | 87 | | { |
| | | 88 | | HResult = HResults.COR_E_ARGUMENT; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | public DecoderFallbackException(string? message, Exception? innerException) |
| | | 92 | | : base(message, innerException) |
| | | 93 | | { |
| | | 94 | | HResult = HResults.COR_E_ARGUMENT; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public DecoderFallbackException(string? message, byte[]? bytesUnknown, int index) |
| | | 98 | | : base(message) |
| | | 99 | | { |
| | | 100 | | _bytesUnknown = bytesUnknown; |
| | | 101 | | _index = index; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | [Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlForma |
| | | 105 | | private DecoderFallbackException(SerializationInfo serializationInfo, StreamingContext streamingContext) |
| | | 106 | | : base(serializationInfo, streamingContext) |
| | | 107 | | { |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public byte[]? BytesUnknown => _bytesUnknown; |
| | | 111 | | |
| | | 112 | | public int Index => _index; |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | |