| | | 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.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | |
| | | 8 | | namespace System.Text |
| | | 9 | | { |
| | | 10 | | public abstract class EncoderFallback |
| | | 11 | | { |
| | | 12 | | // Default fallback, uses no best fit & "?" |
| | | 13 | | public static EncoderFallback ReplacementFallback => EncoderReplacementFallback.s_default; |
| | | 14 | | |
| | | 15 | | public static EncoderFallback ExceptionFallback => EncoderExceptionFallback.s_default; |
| | | 16 | | |
| | | 17 | | // Fallback |
| | | 18 | | // |
| | | 19 | | // Return the appropriate unicode string alternative to the character that need to fall back. |
| | | 20 | | // Most implementations will be: |
| | | 21 | | // return new MyCustomEncoderFallbackBuffer(this); |
| | | 22 | | |
| | | 23 | | public abstract EncoderFallbackBuffer CreateFallbackBuffer(); |
| | | 24 | | |
| | | 25 | | // Maximum number of characters that this instance of this fallback could return |
| | | 26 | | |
| | | 27 | | public abstract int MaxCharCount { get; } |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | |
| | | 31 | | public abstract class EncoderFallbackBuffer |
| | | 32 | | { |
| | | 33 | | // Most implementations will probably need an implementation-specific constructor |
| | | 34 | | |
| | | 35 | | // Public methods that cannot be overridden that let us do our fallback thing |
| | | 36 | | // These wrap the internal methods so that we can check for people doing stuff that is incorrect |
| | | 37 | | |
| | | 38 | | public abstract bool Fallback(char charUnknown, int index); |
| | | 39 | | |
| | | 40 | | public abstract bool Fallback(char charUnknownHigh, char charUnknownLow, int index); |
| | | 41 | | |
| | | 42 | | // Get next character |
| | | 43 | | |
| | | 44 | | public abstract char GetNextChar(); |
| | | 45 | | |
| | | 46 | | // Back up a character |
| | | 47 | | |
| | | 48 | | public abstract bool MovePrevious(); |
| | | 49 | | |
| | | 50 | | // How many chars left in this fallback? |
| | | 51 | | |
| | | 52 | | public abstract int Remaining { get; } |
| | | 53 | | |
| | | 54 | | // Not sure if this should be public or not. |
| | | 55 | | // Clear the buffer |
| | | 56 | | |
| | | 57 | | public virtual void Reset() |
| | | 58 | | { |
| | 0 | 59 | | while (GetNextChar() != (char)0) ; |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | // Internal items to help us figure out what we're doing as far as error messages, etc. |
| | | 63 | | // These help us with our performance and messages internally |
| | | 64 | | internal unsafe char* charStart; |
| | | 65 | | internal unsafe char* charEnd; |
| | | 66 | | internal EncoderNLS? encoder; // TODO: MAKE ME PRIVATE |
| | | 67 | | internal bool setEncoder; |
| | | 68 | | internal bool bUsedEncoder; |
| | | 69 | | internal bool bFallingBack; |
| | | 70 | | internal int iRecursionCount; |
| | | 71 | | private const int iMaxRecursion = 250; |
| | | 72 | | private Encoding? encoding; |
| | | 73 | | private int originalCharCount; |
| | | 74 | | |
| | | 75 | | // Internal Reset |
| | | 76 | | // For example, what if someone fails a conversion and wants to reset one of our fallback buffers? |
| | | 77 | | internal unsafe void InternalReset() |
| | | 78 | | { |
| | 0 | 79 | | charStart = null; |
| | 0 | 80 | | bFallingBack = false; |
| | 0 | 81 | | iRecursionCount = 0; |
| | 0 | 82 | | Reset(); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | // Set the above values |
| | | 86 | | // This can't be part of the constructor because EncoderFallbacks would have to know how to implement these. |
| | | 87 | | internal unsafe void InternalInitialize(char* charStart, char* charEnd, EncoderNLS? encoder, bool setEncoder) |
| | | 88 | | { |
| | 0 | 89 | | this.charStart = charStart; |
| | 0 | 90 | | this.charEnd = charEnd; |
| | 0 | 91 | | this.encoder = encoder; |
| | 0 | 92 | | this.setEncoder = setEncoder; |
| | 0 | 93 | | this.bUsedEncoder = false; |
| | 0 | 94 | | this.bFallingBack = false; |
| | 0 | 95 | | this.iRecursionCount = 0; |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | internal static EncoderFallbackBuffer CreateAndInitialize(Encoding encoding, EncoderNLS? encoder, int originalCh |
| | | 99 | | { |
| | | 100 | | // The original char count is only used for keeping track of what 'index' value needs |
| | | 101 | | // to be passed to the abstract Fallback method. The index value is calculated by subtracting |
| | | 102 | | // 'chars.Length' (where chars is expected to be the entire remaining input buffer) |
| | | 103 | | // from the 'originalCharCount' value specified here. |
| | | 104 | | |
| | 0 | 105 | | EncoderFallbackBuffer fallbackBuffer = (encoder is null) ? encoding.EncoderFallback.CreateFallbackBuffer() : |
| | | 106 | | |
| | 0 | 107 | | fallbackBuffer.encoding = encoding; |
| | 0 | 108 | | fallbackBuffer.encoder = encoder; |
| | 0 | 109 | | fallbackBuffer.originalCharCount = originalCharCount; |
| | | 110 | | |
| | 0 | 111 | | return fallbackBuffer; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | internal char InternalGetNextChar() |
| | | 115 | | { |
| | 0 | 116 | | char ch = GetNextChar(); |
| | 0 | 117 | | bFallingBack = (ch != 0); |
| | 0 | 118 | | if (ch == 0) iRecursionCount = 0; |
| | 0 | 119 | | return ch; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | private bool InternalFallback(ReadOnlySpan<char> chars, out int charsConsumed) |
| | | 123 | | { |
| | 0 | 124 | | Debug.Assert(!chars.IsEmpty, "Caller shouldn't invoke this if there's no data to fall back."); |
| | | 125 | | |
| | | 126 | | // First, try falling back a single BMP character or a standalone low surrogate. |
| | | 127 | | // If the first char is a high surrogate, we'll try to combine it with the next |
| | | 128 | | // char in the input sequence. |
| | | 129 | | |
| | 0 | 130 | | char firstChar = chars[0]; |
| | 0 | 131 | | char secondChar = default; |
| | | 132 | | |
| | 0 | 133 | | if (!chars.IsEmpty) |
| | | 134 | | { |
| | 0 | 135 | | firstChar = chars[0]; |
| | | 136 | | |
| | 0 | 137 | | if (chars.Length > 1) |
| | | 138 | | { |
| | 0 | 139 | | secondChar = chars[1]; |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | // Ask the subclassed type to initiate fallback logic. |
| | | 144 | | |
| | 0 | 145 | | int index = originalCharCount - chars.Length; |
| | | 146 | | |
| | 0 | 147 | | if (!char.IsSurrogatePair(firstChar, secondChar)) |
| | | 148 | | { |
| | | 149 | | // This code path is also used when 'firstChar' is a standalone surrogate or |
| | | 150 | | // if it's a high surrogate at the end of the input buffer. |
| | | 151 | | |
| | 0 | 152 | | charsConsumed = 1; |
| | 0 | 153 | | return Fallback(firstChar, index); |
| | | 154 | | } |
| | | 155 | | else |
| | | 156 | | { |
| | 0 | 157 | | charsConsumed = 2; |
| | 0 | 158 | | return Fallback(firstChar, secondChar, index); |
| | | 159 | | } |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | internal int InternalFallbackGetByteCount(ReadOnlySpan<char> chars, out int charsConsumed) |
| | | 163 | | { |
| | 0 | 164 | | int bytesWritten = 0; |
| | | 165 | | |
| | 0 | 166 | | if (InternalFallback(chars, out charsConsumed)) |
| | | 167 | | { |
| | | 168 | | // There's data in the fallback buffer - pull it out now. |
| | | 169 | | |
| | 0 | 170 | | bytesWritten = DrainRemainingDataForGetByteCount(); |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | return bytesWritten; |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | internal bool TryInternalFallbackGetBytes(ReadOnlySpan<char> chars, Span<byte> bytes, out int charsConsumed, out |
| | | 177 | | { |
| | 0 | 178 | | if (InternalFallback(chars, out charsConsumed)) |
| | | 179 | | { |
| | | 180 | | // There's data in the fallback buffer - pull it out now. |
| | | 181 | | |
| | 0 | 182 | | return TryDrainRemainingDataForGetBytes(bytes, out bytesWritten); |
| | | 183 | | } |
| | | 184 | | else |
| | | 185 | | { |
| | | 186 | | // There's no data in the fallback buffer. |
| | | 187 | | |
| | 0 | 188 | | bytesWritten = 0; |
| | 0 | 189 | | return true; // true = didn't run out of space in destination buffer |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | internal bool TryDrainRemainingDataForGetBytes(Span<byte> bytes, out int bytesWritten) |
| | | 194 | | { |
| | 0 | 195 | | int originalBytesLength = bytes.Length; |
| | | 196 | | |
| | 0 | 197 | | Debug.Assert(encoding != null); |
| | | 198 | | Rune thisRune; |
| | 0 | 199 | | while ((thisRune = GetNextRune()).Value != 0) |
| | | 200 | | { |
| | 0 | 201 | | switch (encoding.EncodeRune(thisRune, bytes, out int bytesWrittenJustNow)) |
| | | 202 | | { |
| | | 203 | | case OperationStatus.Done: |
| | | 204 | | |
| | 0 | 205 | | bytes = bytes.Slice(bytesWrittenJustNow); |
| | 0 | 206 | | continue; |
| | | 207 | | |
| | | 208 | | case OperationStatus.DestinationTooSmall: |
| | | 209 | | |
| | | 210 | | // Since we're not consuming the Rune we just read, back up as many chars as necessary |
| | | 211 | | // to undo the read we just performed, then report to our caller that we ran out of space. |
| | | 212 | | |
| | 0 | 213 | | for (int i = 0; i < thisRune.Utf16SequenceLength; i++) |
| | | 214 | | { |
| | 0 | 215 | | MovePrevious(); |
| | | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | bytesWritten = originalBytesLength - bytes.Length; |
| | 0 | 219 | | return false; // ran out of destination buffer |
| | | 220 | | |
| | | 221 | | case OperationStatus.InvalidData: |
| | | 222 | | |
| | | 223 | | // We can't fallback the fallback. We can't make forward progress, so report to our caller |
| | | 224 | | // that something went terribly wrong. The error message contains the fallback char that |
| | | 225 | | // couldn't be converted. (Ideally we'd provide the first char that originally triggered |
| | | 226 | | // the fallback, but it's complicated to keep this state around, and a fallback producing |
| | | 227 | | // invalid data should be a very rare occurrence.) |
| | | 228 | | |
| | 0 | 229 | | ThrowLastCharRecursive(thisRune.Value); |
| | | 230 | | break; // will never be hit; call above throws |
| | | 231 | | |
| | | 232 | | default: |
| | | 233 | | |
| | 0 | 234 | | Debug.Fail("Unexpected return value."); |
| | | 235 | | break; |
| | | 236 | | } |
| | | 237 | | } |
| | | 238 | | |
| | 0 | 239 | | bytesWritten = originalBytesLength - bytes.Length; |
| | 0 | 240 | | return true; // finished successfully |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | internal int DrainRemainingDataForGetByteCount() |
| | | 244 | | { |
| | 0 | 245 | | int totalByteCount = 0; |
| | | 246 | | |
| | 0 | 247 | | Debug.Assert(encoding != null); |
| | | 248 | | Rune thisRune; |
| | 0 | 249 | | while ((thisRune = GetNextRune()).Value != 0) |
| | | 250 | | { |
| | 0 | 251 | | if (!encoding.TryGetByteCount(thisRune, out int byteCountThisIteration)) |
| | | 252 | | { |
| | | 253 | | // We can't fallback the fallback. We can't make forward progress, so report to our caller |
| | | 254 | | // that something went terribly wrong. The error message contains the fallback char that |
| | | 255 | | // couldn't be converted. (Ideally we'd provide the first char that originally triggered |
| | | 256 | | // the fallback, but it's complicated to keep this state around, and a fallback producing |
| | | 257 | | // invalid data should be a very rare occurrence.) |
| | | 258 | | |
| | 0 | 259 | | ThrowLastCharRecursive(thisRune.Value); |
| | | 260 | | } |
| | | 261 | | |
| | 0 | 262 | | Debug.Assert(byteCountThisIteration >= 0, "Encoding shouldn't have returned a negative byte count."); |
| | | 263 | | |
| | | 264 | | // We need to check for overflow while tallying the fallback byte count. |
| | | 265 | | |
| | 0 | 266 | | totalByteCount += byteCountThisIteration; |
| | 0 | 267 | | if (totalByteCount < 0) |
| | | 268 | | { |
| | 0 | 269 | | InternalReset(); |
| | 0 | 270 | | Encoding.ThrowConversionOverflow(); |
| | | 271 | | } |
| | | 272 | | } |
| | | 273 | | |
| | 0 | 274 | | return totalByteCount; |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | private Rune GetNextRune() |
| | | 278 | | { |
| | 0 | 279 | | char firstChar = GetNextChar(); |
| | 0 | 280 | | if (Rune.TryCreate(firstChar, out Rune value) || Rune.TryCreate(firstChar, GetNextChar(), out value)) |
| | | 281 | | { |
| | 0 | 282 | | return value; |
| | | 283 | | } |
| | | 284 | | |
| | 0 | 285 | | throw new ArgumentException(SR.Argument_InvalidCharSequenceNoIndex); |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | // Fallback the current character using the remaining buffer and encoder if necessary |
| | | 289 | | // This can only be called by our encodings (other have to use the public fallback methods), so |
| | | 290 | | // we can use our EncoderNLS here too. |
| | | 291 | | // setEncoder is true if we're calling from a GetBytes method, false if we're calling from a GetByteCount |
| | | 292 | | // |
| | | 293 | | // Note that this could also change the contents of this.encoder, which is the same |
| | | 294 | | // object that the caller is using, so the caller could mess up the encoder for us |
| | | 295 | | // if they aren't careful. |
| | | 296 | | internal unsafe bool InternalFallback(char ch, ref char* chars) |
| | | 297 | | { |
| | | 298 | | // Shouldn't have null charStart |
| | 0 | 299 | | Debug.Assert(charStart != null, |
| | 0 | 300 | | "[EncoderFallback.InternalFallbackBuffer]Fallback buffer is not initialized"); |
| | | 301 | | |
| | | 302 | | // Get our index, remember chars was preincremented to point at next char, so have to -1 |
| | 0 | 303 | | int index = (int)(chars - charStart) - 1; |
| | | 304 | | |
| | | 305 | | // See if it was a high surrogate |
| | 0 | 306 | | if (char.IsHighSurrogate(ch)) |
| | | 307 | | { |
| | | 308 | | // See if there's a low surrogate to go with it |
| | 0 | 309 | | if (chars >= this.charEnd) |
| | | 310 | | { |
| | | 311 | | // Nothing left in input buffer |
| | | 312 | | // No input, return 0 if mustflush is false |
| | 0 | 313 | | if (this.encoder != null && !this.encoder.MustFlush) |
| | | 314 | | { |
| | | 315 | | // Done, nothing to fallback |
| | 0 | 316 | | if (this.setEncoder) |
| | | 317 | | { |
| | 0 | 318 | | bUsedEncoder = true; |
| | 0 | 319 | | this.encoder._charLeftOver = ch; |
| | | 320 | | } |
| | 0 | 321 | | bFallingBack = false; |
| | 0 | 322 | | return false; |
| | | 323 | | } |
| | | 324 | | } |
| | | 325 | | else |
| | | 326 | | { |
| | | 327 | | // Might have a low surrogate |
| | 0 | 328 | | char cNext = *chars; |
| | 0 | 329 | | if (char.IsLowSurrogate(cNext)) |
| | | 330 | | { |
| | | 331 | | // If already falling back then fail |
| | 0 | 332 | | if (bFallingBack && iRecursionCount++ > iMaxRecursion) |
| | 0 | 333 | | ThrowLastCharRecursive(char.ConvertToUtf32(ch, cNext)); |
| | | 334 | | |
| | | 335 | | // Next is a surrogate, add it as surrogate pair, and increment chars |
| | 0 | 336 | | chars++; |
| | 0 | 337 | | bFallingBack = Fallback(ch, cNext, index); |
| | 0 | 338 | | return bFallingBack; |
| | | 339 | | } |
| | | 340 | | // Next isn't a low surrogate, just fallback the high surrogate |
| | | 341 | | } |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | // If already falling back then fail |
| | 0 | 345 | | if (bFallingBack && iRecursionCount++ > iMaxRecursion) |
| | 0 | 346 | | ThrowLastCharRecursive((int)ch); |
| | | 347 | | |
| | | 348 | | // Fall back our char |
| | 0 | 349 | | bFallingBack = Fallback(ch, index); |
| | | 350 | | |
| | 0 | 351 | | return bFallingBack; |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | [DoesNotReturn] |
| | | 355 | | internal static void ThrowLastCharRecursive(int charRecursive) => |
| | | 356 | | // Throw it, using our complete character |
| | 0 | 357 | | throw new ArgumentException(SR.Format(SR.Argument_RecursiveFallback, charRecursive), "chars"); |
| | | 358 | | } |
| | | 359 | | } |
| | | 360 | | |