| | | 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; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | |
| | | 8 | | namespace System.Text |
| | | 9 | | { |
| | | 10 | | // An Encoder is used to encode a sequence of blocks of characters into |
| | | 11 | | // a sequence of blocks of bytes. Following instantiation of an encoder, |
| | | 12 | | // sequential blocks of characters are converted into blocks of bytes through |
| | | 13 | | // calls to the GetBytes method. The encoder maintains state between the |
| | | 14 | | // conversions, allowing it to correctly encode character sequences that span |
| | | 15 | | // adjacent blocks. |
| | | 16 | | // |
| | | 17 | | // Instances of specific implementations of the Encoder abstract base |
| | | 18 | | // class are typically obtained through calls to the GetEncoder method |
| | | 19 | | // of Encoding objects. |
| | | 20 | | // |
| | | 21 | | public abstract class Encoder |
| | | 22 | | { |
| | | 23 | | internal EncoderFallback? _fallback; |
| | | 24 | | |
| | | 25 | | internal EncoderFallbackBuffer? _fallbackBuffer; |
| | | 26 | | |
| | 1 | 27 | | protected Encoder() |
| | | 28 | | { |
| | | 29 | | // We don't call default reset because default reset probably isn't good if we aren't initialized. |
| | 1 | 30 | | } |
| | | 31 | | |
| | | 32 | | public EncoderFallback? Fallback |
| | | 33 | | { |
| | 0 | 34 | | get => _fallback; |
| | | 35 | | set |
| | | 36 | | { |
| | 0 | 37 | | ArgumentNullException.ThrowIfNull(value); |
| | | 38 | | |
| | | 39 | | // Can't change fallback if buffer is wrong |
| | 0 | 40 | | if (_fallbackBuffer is not null && _fallbackBuffer.Remaining > 0) |
| | 0 | 41 | | throw new ArgumentException( |
| | 0 | 42 | | SR.Argument_FallbackBufferNotEmpty, nameof(value)); |
| | | 43 | | |
| | 0 | 44 | | _fallback = value; |
| | 0 | 45 | | _fallbackBuffer = null; |
| | 0 | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | // Note: we don't test for threading here because async access to Encoders and Decoders |
| | | 50 | | // doesn't work anyway. |
| | | 51 | | public EncoderFallbackBuffer FallbackBuffer |
| | | 52 | | { |
| | | 53 | | get |
| | | 54 | | { |
| | 0 | 55 | | _fallbackBuffer ??= _fallback is not null ? |
| | 0 | 56 | | _fallback.CreateFallbackBuffer() : |
| | 0 | 57 | | EncoderFallback.ReplacementFallback.CreateFallbackBuffer(); |
| | | 58 | | |
| | 0 | 59 | | return _fallbackBuffer; |
| | | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | internal bool InternalHasFallbackBuffer => _fallbackBuffer is not null; |
| | | 64 | | |
| | | 65 | | // Reset the Encoder |
| | | 66 | | // |
| | | 67 | | // Normally if we call GetBytes() and an error is thrown we don't change the state of the encoder. This |
| | | 68 | | // would allow the caller to correct the error condition and try again (such as if they need a bigger buffer.) |
| | | 69 | | // |
| | | 70 | | // If the caller doesn't want to try again after GetBytes() throws an error, then they need to call Reset(). |
| | | 71 | | // |
| | | 72 | | // Virtual implementation has to call GetBytes with flush and a big enough buffer to clear a 0 char string |
| | | 73 | | // We avoid GetMaxByteCount() because a) we can't call the base encoder and b) it might be really big. |
| | | 74 | | public virtual void Reset() |
| | | 75 | | { |
| | 0 | 76 | | char[] charTemp = []; |
| | 0 | 77 | | byte[] byteTemp = new byte[GetByteCount(charTemp, 0, 0, true)]; |
| | 0 | 78 | | GetBytes(charTemp, 0, 0, byteTemp, 0, true); |
| | 0 | 79 | | _fallbackBuffer?.Reset(); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | // Returns the number of bytes the next call to GetBytes will |
| | | 83 | | // produce if presented with the given range of characters and the given |
| | | 84 | | // value of the flush parameter. The returned value takes into |
| | | 85 | | // account the state in which the encoder was left following the last call |
| | | 86 | | // to GetBytes. The state of the encoder is not affected by a call |
| | | 87 | | // to this method. |
| | | 88 | | // |
| | | 89 | | public abstract int GetByteCount(char[] chars, int index, int count, bool flush); |
| | | 90 | | |
| | | 91 | | // We expect this to be the workhorse for NLS encodings |
| | | 92 | | // unfortunately for existing overrides, it has to call the [] version, |
| | | 93 | | // which is really slow, so avoid this method if you might be calling external encodings. |
| | | 94 | | [CLSCompliant(false)] |
| | | 95 | | public virtual unsafe int GetByteCount(char* chars, int count, bool flush) |
| | | 96 | | { |
| | 0 | 97 | | ArgumentNullException.ThrowIfNull(chars); |
| | | 98 | | |
| | 0 | 99 | | ArgumentOutOfRangeException.ThrowIfNegative(count); |
| | | 100 | | |
| | 0 | 101 | | char[] arrChar = new char[count]; |
| | | 102 | | |
| | 0 | 103 | | for (int index = 0; index < count; index++) |
| | 0 | 104 | | arrChar[index] = chars[index]; |
| | | 105 | | |
| | 0 | 106 | | return GetByteCount(arrChar, 0, count, flush); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | public virtual unsafe int GetByteCount(ReadOnlySpan<char> chars, bool flush) |
| | 0 | 110 | | { |
| | 0 | 111 | | fixed (char* charsPtr = &MemoryMarshal.GetNonNullPinnableReference(chars)) |
| | | 112 | | { |
| | 0 | 113 | | return GetByteCount(charsPtr, chars.Length, flush); |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | // Encodes a range of characters in a character array into a range of bytes |
| | | 118 | | // in a byte array. The method encodes charCount characters from |
| | | 119 | | // chars starting at index charIndex, storing the resulting |
| | | 120 | | // bytes in bytes starting at index byteIndex. The encoding |
| | | 121 | | // takes into account the state in which the encoder was left following the |
| | | 122 | | // last call to this method. The flush parameter indicates whether |
| | | 123 | | // the encoder should flush any shift-states and partial characters at the |
| | | 124 | | // end of the conversion. To ensure correct termination of a sequence of |
| | | 125 | | // blocks of encoded bytes, the last call to GetBytes should specify |
| | | 126 | | // a value of true for the flush parameter. |
| | | 127 | | // |
| | | 128 | | // An exception occurs if the byte array is not large enough to hold the |
| | | 129 | | // complete encoding of the characters. The GetByteCount method can |
| | | 130 | | // be used to determine the exact number of bytes that will be produced for |
| | | 131 | | // a given range of characters. Alternatively, the GetMaxByteCount |
| | | 132 | | // method of the Encoding that produced this encoder can be used to |
| | | 133 | | // determine the maximum number of bytes that will be produced for a given |
| | | 134 | | // number of characters, regardless of the actual character values. |
| | | 135 | | // |
| | | 136 | | public abstract int GetBytes(char[] chars, int charIndex, int charCount, |
| | | 137 | | byte[] bytes, int byteIndex, bool flush); |
| | | 138 | | |
| | | 139 | | // We expect this to be the workhorse for NLS Encodings, but for existing |
| | | 140 | | // ones we need a working (if slow) default implementation) |
| | | 141 | | // |
| | | 142 | | // WARNING WARNING WARNING |
| | | 143 | | // |
| | | 144 | | // WARNING: If this breaks it could be a security threat. Obviously we |
| | | 145 | | // call this internally, so you need to make sure that your pointers, counts |
| | | 146 | | // and indexes are correct when you call this method. |
| | | 147 | | // |
| | | 148 | | // In addition, we have internal code, which will be marked as "safe" calling |
| | | 149 | | // this code. However this code is dependent upon the implementation of an |
| | | 150 | | // external GetBytes() method, which could be overridden by a third party and |
| | | 151 | | // the results of which cannot be guaranteed. We use that result to copy |
| | | 152 | | // the byte[] to our byte* output buffer. If the result count was wrong, we |
| | | 153 | | // could easily overflow our output buffer. Therefore we do an extra test |
| | | 154 | | // when we copy the buffer so that we don't overflow byteCount either. |
| | | 155 | | [CLSCompliant(false)] |
| | | 156 | | public virtual unsafe int GetBytes(char* chars, int charCount, |
| | | 157 | | byte* bytes, int byteCount, bool flush) |
| | | 158 | | { |
| | 0 | 159 | | ArgumentNullException.ThrowIfNull(chars); |
| | 0 | 160 | | ArgumentNullException.ThrowIfNull(bytes); |
| | | 161 | | |
| | 0 | 162 | | ArgumentOutOfRangeException.ThrowIfNegative(charCount); |
| | 0 | 163 | | ArgumentOutOfRangeException.ThrowIfNegative(byteCount); |
| | | 164 | | |
| | | 165 | | // Get the char array to convert |
| | 0 | 166 | | char[] arrChar = new char[charCount]; |
| | | 167 | | |
| | 0 | 168 | | for (int index = 0; index < charCount; index++) |
| | 0 | 169 | | arrChar[index] = chars[index]; |
| | | 170 | | |
| | | 171 | | // Get the byte array to fill |
| | 0 | 172 | | byte[] arrByte = new byte[byteCount]; |
| | | 173 | | |
| | | 174 | | // Do the work |
| | 0 | 175 | | int result = GetBytes(arrChar, 0, charCount, arrByte, 0, flush); |
| | | 176 | | |
| | 0 | 177 | | Debug.Assert(result <= byteCount, "Returned more bytes than we have space for"); |
| | | 178 | | |
| | | 179 | | // Copy the byte array |
| | | 180 | | // WARNING: We MUST make sure that we don't copy too many bytes. We can't |
| | | 181 | | // rely on result because it could be a 3rd party implementation. We need |
| | | 182 | | // to make sure we never copy more than byteCount bytes no matter the value |
| | | 183 | | // of result |
| | 0 | 184 | | if (result < byteCount) |
| | 0 | 185 | | byteCount = result; |
| | | 186 | | |
| | | 187 | | // Don't copy too many bytes! |
| | 0 | 188 | | for (int index = 0; index < byteCount; index++) |
| | 0 | 189 | | bytes[index] = arrByte[index]; |
| | | 190 | | |
| | 0 | 191 | | return byteCount; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | public virtual unsafe int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes, bool flush) |
| | 3 | 195 | | { |
| | 3 | 196 | | fixed (char* charsPtr = &MemoryMarshal.GetNonNullPinnableReference(chars)) |
| | 3 | 197 | | fixed (byte* bytesPtr = &MemoryMarshal.GetNonNullPinnableReference(bytes)) |
| | | 198 | | { |
| | 3 | 199 | | return GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length, flush); |
| | | 200 | | } |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | // This method is used to avoid running out of output buffer space. |
| | | 204 | | // It will encode until it runs out of chars, and then it will return |
| | | 205 | | // true if it the entire input was converted. In either case it |
| | | 206 | | // will also return the number of converted chars and output bytes used. |
| | | 207 | | // It will only throw a buffer overflow exception if the entire length of bytes[] is |
| | | 208 | | // too small to store the next byte. (like 0 or maybe 1 or 4 for some encodings) |
| | | 209 | | // We're done processing this buffer only if completed returns true. |
| | | 210 | | // |
| | | 211 | | // Might consider checking Max...Count to avoid the extra counting step. |
| | | 212 | | // |
| | | 213 | | // Note that if all of the input chars are not consumed, then we'll do a /2, which means |
| | | 214 | | // that its likely that we didn't consume as many chars as we could have. For some |
| | | 215 | | // applications this could be slow. (Like trying to exactly fill an output buffer from a bigger stream) |
| | | 216 | | public virtual void Convert(char[] chars, int charIndex, int charCount, |
| | | 217 | | byte[] bytes, int byteIndex, int byteCount, bool flush, |
| | | 218 | | out int charsUsed, out int bytesUsed, out bool completed) |
| | | 219 | | { |
| | 0 | 220 | | ArgumentNullException.ThrowIfNull(chars); |
| | 0 | 221 | | ArgumentNullException.ThrowIfNull(bytes); |
| | | 222 | | |
| | 0 | 223 | | ArgumentOutOfRangeException.ThrowIfNegative(charIndex); |
| | 0 | 224 | | ArgumentOutOfRangeException.ThrowIfNegative(charCount); |
| | | 225 | | |
| | 0 | 226 | | ArgumentOutOfRangeException.ThrowIfNegative(byteIndex); |
| | 0 | 227 | | ArgumentOutOfRangeException.ThrowIfNegative(byteCount); |
| | | 228 | | |
| | 0 | 229 | | if (chars.Length - charIndex < charCount) |
| | 0 | 230 | | throw new ArgumentOutOfRangeException(nameof(chars), |
| | 0 | 231 | | SR.ArgumentOutOfRange_IndexCountBuffer); |
| | | 232 | | |
| | 0 | 233 | | if (bytes.Length - byteIndex < byteCount) |
| | 0 | 234 | | throw new ArgumentOutOfRangeException(nameof(bytes), |
| | 0 | 235 | | SR.ArgumentOutOfRange_IndexCountBuffer); |
| | | 236 | | |
| | 0 | 237 | | charsUsed = charCount; |
| | | 238 | | |
| | | 239 | | // Its easy to do if it won't overrun our buffer. |
| | | 240 | | // Note: We don't want to call unsafe version because that might be an untrusted version |
| | | 241 | | // which could be really unsafe and we don't want to mix it up. |
| | 0 | 242 | | while (charsUsed > 0) |
| | | 243 | | { |
| | 0 | 244 | | if (GetByteCount(chars, charIndex, charsUsed, flush) <= byteCount) |
| | | 245 | | { |
| | 0 | 246 | | bytesUsed = GetBytes(chars, charIndex, charsUsed, bytes, byteIndex, flush); |
| | 0 | 247 | | completed = (charsUsed == charCount && |
| | 0 | 248 | | (_fallbackBuffer == null || _fallbackBuffer.Remaining == 0)); |
| | 0 | 249 | | return; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | // Try again with 1/2 the count, won't flush then 'cause won't read it all |
| | 0 | 253 | | flush = false; |
| | 0 | 254 | | charsUsed /= 2; |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | // Oops, we didn't have anything, we'll have to throw an overflow |
| | 0 | 258 | | throw new ArgumentException(SR.Argument_ConversionOverflow); |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | // Same thing, but using pointers |
| | | 262 | | // |
| | | 263 | | // Might consider checking Max...Count to avoid the extra counting step. |
| | | 264 | | // |
| | | 265 | | // Note that if all of the input chars are not consumed, then we'll do a /2, which means |
| | | 266 | | // that its likely that we didn't consume as many chars as we could have. For some |
| | | 267 | | // applications this could be slow. (Like trying to exactly fill an output buffer from a bigger stream) |
| | | 268 | | [CLSCompliant(false)] |
| | | 269 | | public virtual unsafe void Convert(char* chars, int charCount, |
| | | 270 | | byte* bytes, int byteCount, bool flush, |
| | | 271 | | out int charsUsed, out int bytesUsed, out bool completed) |
| | | 272 | | { |
| | 0 | 273 | | ArgumentNullException.ThrowIfNull(chars); |
| | 0 | 274 | | ArgumentNullException.ThrowIfNull(bytes); |
| | | 275 | | |
| | 0 | 276 | | ArgumentOutOfRangeException.ThrowIfNegative(charCount); |
| | 0 | 277 | | ArgumentOutOfRangeException.ThrowIfNegative(byteCount); |
| | | 278 | | |
| | | 279 | | // Get ready to do it |
| | 0 | 280 | | charsUsed = charCount; |
| | | 281 | | |
| | | 282 | | // Its easy to do if it won't overrun our buffer. |
| | 0 | 283 | | while (charsUsed > 0) |
| | | 284 | | { |
| | 0 | 285 | | if (GetByteCount(chars, charsUsed, flush) <= byteCount) |
| | | 286 | | { |
| | 0 | 287 | | bytesUsed = GetBytes(chars, charsUsed, bytes, byteCount, flush); |
| | 0 | 288 | | completed = (charsUsed == charCount && |
| | 0 | 289 | | (_fallbackBuffer == null || _fallbackBuffer.Remaining == 0)); |
| | 0 | 290 | | return; |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | // Try again with 1/2 the count, won't flush then 'cause won't read it all |
| | 0 | 294 | | flush = false; |
| | 0 | 295 | | charsUsed /= 2; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | // Oops, we didn't have anything, we'll have to throw an overflow |
| | 0 | 299 | | throw new ArgumentException(SR.Argument_ConversionOverflow); |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | public virtual unsafe void Convert(ReadOnlySpan<char> chars, Span<byte> bytes, bool flush, out int charsUsed, ou |
| | 0 | 303 | | { |
| | 0 | 304 | | fixed (char* charsPtr = &MemoryMarshal.GetNonNullPinnableReference(chars)) |
| | 0 | 305 | | fixed (byte* bytesPtr = &MemoryMarshal.GetNonNullPinnableReference(bytes)) |
| | | 306 | | { |
| | 0 | 307 | | Convert(charsPtr, chars.Length, bytesPtr, bytes.Length, flush, out charsUsed, out bytesUsed, out complet |
| | | 308 | | } |
| | 0 | 309 | | } |
| | | 310 | | } |
| | | 311 | | } |
| | | 312 | | |