| | | 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 | | // |
| | | 5 | | // Don't override IsAlwaysNormalized because it is just a Unicode Transformation and could be confused. |
| | | 6 | | // |
| | | 7 | | |
| | | 8 | | using System.Diagnostics; |
| | | 9 | | using System.Diagnostics.CodeAnalysis; |
| | | 10 | | using System.Runtime.InteropServices; |
| | | 11 | | |
| | | 12 | | namespace System.Text |
| | | 13 | | { |
| | | 14 | | public class UTF7Encoding : Encoding |
| | | 15 | | { |
| | | 16 | | #pragma warning disable SYSLIB0001 |
| | | 17 | | // Used by Encoding.UTF7 for lazy initialization |
| | | 18 | | // The initialization code will not be run until a static member of the class is referenced |
| | 0 | 19 | | internal static readonly UTF7Encoding s_default = new UTF7Encoding(); |
| | | 20 | | #pragma warning restore SYSLIB0001 |
| | | 21 | | |
| | | 22 | | // The set of base 64 characters. |
| | | 23 | | private byte[] _base64Bytes; |
| | | 24 | | // The decoded bits for every base64 values. This array has a size of 128 elements. |
| | | 25 | | // The index is the code point value of the base 64 characters. The value is -1 if |
| | | 26 | | // the code point is not a valid base 64 character. Otherwise, the value is a value |
| | | 27 | | // from 0 ~ 63. |
| | | 28 | | private sbyte[] _base64Values; |
| | | 29 | | // The array to decide if a Unicode code point below 0x80 can be directly encoded in UTF7. |
| | | 30 | | // This array has a size of 128. |
| | | 31 | | private bool[] _directEncode; |
| | | 32 | | |
| | | 33 | | private readonly bool _allowOptionals; |
| | | 34 | | |
| | | 35 | | private const int UTF7_CODEPAGE = 65000; |
| | | 36 | | |
| | | 37 | | [Obsolete(Obsoletions.SystemTextEncodingUTF7Message, DiagnosticId = Obsoletions.SystemTextEncodingUTF7DiagId, Ur |
| | | 38 | | public UTF7Encoding() |
| | 3403 | 39 | | : this(false) |
| | | 40 | | { |
| | 3403 | 41 | | } |
| | | 42 | | |
| | | 43 | | [Obsolete(Obsoletions.SystemTextEncodingUTF7Message, DiagnosticId = Obsoletions.SystemTextEncodingUTF7DiagId, Ur |
| | | 44 | | public UTF7Encoding(bool allowOptionals) |
| | 3403 | 45 | | : base(UTF7_CODEPAGE) // Set the data item. |
| | | 46 | | { |
| | | 47 | | // Allowing optionals? |
| | 3403 | 48 | | _allowOptionals = allowOptionals; |
| | | 49 | | |
| | | 50 | | // Make our tables |
| | 3403 | 51 | | MakeTables(); |
| | 3403 | 52 | | } |
| | | 53 | | |
| | | 54 | | [MemberNotNull(nameof(_base64Bytes))] |
| | | 55 | | [MemberNotNull(nameof(_base64Values))] |
| | | 56 | | [MemberNotNull(nameof(_directEncode))] |
| | | 57 | | private void MakeTables() |
| | | 58 | | { |
| | | 59 | | // Build our tables |
| | | 60 | | |
| | 3403 | 61 | | _base64Bytes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"u8.ToArray(); |
| | 3403 | 62 | | Debug.Assert(_base64Bytes.Length == 64); |
| | | 63 | | |
| | 3403 | 64 | | _base64Values = new sbyte[128]; |
| | 1313558 | 65 | | for (int i = 0; i < 128; i++) _base64Values[i] = -1; |
| | 660182 | 66 | | for (int i = 0; i < 64; i++) _base64Values[_base64Bytes[i]] = (sbyte)i; |
| | | 67 | | |
| | | 68 | | // These are the characters that can be directly encoded in UTF7. |
| | 3403 | 69 | | ReadOnlySpan<byte> directChars = "\t\n\r '(),-./0123456789:?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv |
| | 3403 | 70 | | _directEncode = new bool[128]; |
| | 517256 | 71 | | foreach (byte c in directChars) |
| | | 72 | | { |
| | 255225 | 73 | | _directEncode[c] = true; |
| | | 74 | | } |
| | | 75 | | |
| | 3403 | 76 | | if (_allowOptionals) |
| | | 77 | | { |
| | | 78 | | // These are the characters that can be optionally directly encoded in UTF7. |
| | 0 | 79 | | ReadOnlySpan<byte> optionalChars = "!\"#$%&*;<=>@[]^_`{|}"u8; |
| | | 80 | | |
| | 0 | 81 | | foreach (byte c in optionalChars) |
| | | 82 | | { |
| | 0 | 83 | | _directEncode[c] = true; |
| | | 84 | | } |
| | | 85 | | } |
| | 3403 | 86 | | } |
| | | 87 | | |
| | | 88 | | // We go ahead and set this because Encoding expects it, however nothing can fall back in UTF7. |
| | | 89 | | internal sealed override void SetDefaultFallbacks() |
| | | 90 | | { |
| | | 91 | | // UTF7 had an odd decoderFallback behavior, and the Encoder fallback |
| | | 92 | | // is irrelevant because we encode surrogates individually and never check for unmatched ones |
| | | 93 | | // (so nothing can fallback during encoding) |
| | 3403 | 94 | | this.encoderFallback = new EncoderReplacementFallback(string.Empty); |
| | 3403 | 95 | | this.decoderFallback = new DecoderUTF7Fallback(); |
| | 3403 | 96 | | } |
| | | 97 | | |
| | | 98 | | public override bool Equals([NotNullWhen(true)] object? value) |
| | | 99 | | { |
| | 0 | 100 | | if (value is UTF7Encoding that) |
| | | 101 | | { |
| | 0 | 102 | | return (_allowOptionals == that._allowOptionals) && |
| | 0 | 103 | | (EncoderFallback.Equals(that.EncoderFallback)) && |
| | 0 | 104 | | (DecoderFallback.Equals(that.DecoderFallback)); |
| | | 105 | | } |
| | 0 | 106 | | return false; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | // Compared to all the other encodings, variations of UTF7 are unlikely |
| | | 110 | | |
| | | 111 | | public override int GetHashCode() |
| | | 112 | | { |
| | 0 | 113 | | return this.CodePage + this.EncoderFallback.GetHashCode() + this.DecoderFallback.GetHashCode(); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | // The following methods are copied from EncodingNLS.cs. |
| | | 117 | | // Unfortunately EncodingNLS.cs is internal and we're public, so we have to re-implement them here. |
| | | 118 | | // These should be kept in sync for the following classes: |
| | | 119 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 120 | | |
| | | 121 | | // Returns the number of bytes required to encode a range of characters in |
| | | 122 | | // a character array. |
| | | 123 | | // |
| | | 124 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 125 | | // So if you fix this, fix the others. Currently those include: |
| | | 126 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 127 | | // parent method is safe |
| | | 128 | | |
| | | 129 | | public override unsafe int GetByteCount(char[] chars, int index, int count) |
| | | 130 | | { |
| | 0 | 131 | | ArgumentNullException.ThrowIfNull(chars); |
| | | 132 | | |
| | 0 | 133 | | ArgumentOutOfRangeException.ThrowIfNegative(index); |
| | 0 | 134 | | ArgumentOutOfRangeException.ThrowIfNegative(count); |
| | | 135 | | |
| | 0 | 136 | | if (chars.Length - index < count) |
| | 0 | 137 | | throw new ArgumentOutOfRangeException(nameof(chars), SR.ArgumentOutOfRange_IndexCountBuffer); |
| | | 138 | | |
| | | 139 | | // If no input, return 0, avoid fixed empty array problem |
| | 0 | 140 | | if (count == 0) |
| | 0 | 141 | | return 0; |
| | | 142 | | |
| | | 143 | | // Just call the pointer version |
| | 0 | 144 | | fixed (char* pChars = chars) |
| | 0 | 145 | | return GetByteCount(pChars + index, count, null); |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 149 | | // So if you fix this, fix the others. Currently those include: |
| | | 150 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 151 | | // parent method is safe |
| | | 152 | | |
| | | 153 | | public override unsafe int GetByteCount(string s) |
| | | 154 | | { |
| | 0 | 155 | | if (s is null) |
| | | 156 | | { |
| | 0 | 157 | | ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | fixed (char* pChars = s) |
| | 0 | 161 | | return GetByteCount(pChars, s.Length, null); |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 165 | | // So if you fix this, fix the others. Currently those include: |
| | | 166 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 167 | | |
| | | 168 | | [CLSCompliant(false)] |
| | | 169 | | public override unsafe int GetByteCount(char* chars, int count) |
| | | 170 | | { |
| | 0 | 171 | | ArgumentNullException.ThrowIfNull(chars); |
| | | 172 | | |
| | 0 | 173 | | ArgumentOutOfRangeException.ThrowIfNegative(count); |
| | | 174 | | |
| | | 175 | | // Call it with empty encoder |
| | 0 | 176 | | return GetByteCount(chars, count, null); |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | // Parent method is safe. |
| | | 180 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 181 | | // So if you fix this, fix the others. Currently those include: |
| | | 182 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 183 | | |
| | | 184 | | public override unsafe int GetBytes(string s, int charIndex, int charCount, |
| | | 185 | | byte[] bytes, int byteIndex) |
| | | 186 | | { |
| | 0 | 187 | | ArgumentNullException.ThrowIfNull(s); |
| | 0 | 188 | | ArgumentNullException.ThrowIfNull(bytes); |
| | | 189 | | |
| | 0 | 190 | | ArgumentOutOfRangeException.ThrowIfNegative(charIndex); |
| | 0 | 191 | | ArgumentOutOfRangeException.ThrowIfNegative(charCount); |
| | | 192 | | |
| | 0 | 193 | | if (s.Length - charIndex < charCount) |
| | 0 | 194 | | throw new ArgumentOutOfRangeException(nameof(s), SR.ArgumentOutOfRange_IndexCount); |
| | | 195 | | |
| | 0 | 196 | | if (byteIndex < 0 || byteIndex > bytes.Length) |
| | 0 | 197 | | throw new ArgumentOutOfRangeException(nameof(byteIndex), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual); |
| | | 198 | | |
| | 0 | 199 | | int byteCount = bytes.Length - byteIndex; |
| | | 200 | | |
| | 0 | 201 | | fixed (char* pChars = s) |
| | 0 | 202 | | fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) |
| | | 203 | | { |
| | 0 | 204 | | return GetBytes(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, null); |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | // Encodes a range of characters in a character array into a range of bytes |
| | | 209 | | // in a byte array. An exception occurs if the byte array is not large |
| | | 210 | | // enough to hold the complete encoding of the characters. The |
| | | 211 | | // GetByteCount method can be used to determine the exact number of |
| | | 212 | | // bytes that will be produced for a given range of characters. |
| | | 213 | | // Alternatively, the GetMaxByteCount method can be used to |
| | | 214 | | // determine the maximum number of bytes that will be produced for a given |
| | | 215 | | // number of characters, regardless of the actual character values. |
| | | 216 | | // |
| | | 217 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 218 | | // So if you fix this, fix the others. Currently those include: |
| | | 219 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 220 | | // parent method is safe |
| | | 221 | | |
| | | 222 | | public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, |
| | | 223 | | byte[] bytes, int byteIndex) |
| | | 224 | | { |
| | 0 | 225 | | ArgumentNullException.ThrowIfNull(chars); |
| | 0 | 226 | | ArgumentNullException.ThrowIfNull(bytes); |
| | | 227 | | |
| | 0 | 228 | | ArgumentOutOfRangeException.ThrowIfNegative(charIndex); |
| | 0 | 229 | | ArgumentOutOfRangeException.ThrowIfNegative(charCount); |
| | | 230 | | |
| | 0 | 231 | | if (chars.Length - charIndex < charCount) |
| | 0 | 232 | | throw new ArgumentOutOfRangeException(nameof(chars), SR.ArgumentOutOfRange_IndexCountBuffer); |
| | | 233 | | |
| | 0 | 234 | | if (byteIndex < 0 || byteIndex > bytes.Length) |
| | 0 | 235 | | throw new ArgumentOutOfRangeException(nameof(byteIndex), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual); |
| | | 236 | | |
| | | 237 | | // If nothing to encode return 0, avoid fixed problem |
| | 0 | 238 | | if (charCount == 0) |
| | 0 | 239 | | return 0; |
| | | 240 | | |
| | | 241 | | // Just call pointer version |
| | 0 | 242 | | int byteCount = bytes.Length - byteIndex; |
| | | 243 | | |
| | 0 | 244 | | fixed (char* pChars = chars) |
| | 0 | 245 | | fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) |
| | | 246 | | { |
| | | 247 | | // Remember that byteCount is # to decode, not size of array. |
| | 0 | 248 | | return GetBytes(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, null); |
| | | 249 | | } |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 253 | | // So if you fix this, fix the others. Currently those include: |
| | | 254 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 255 | | |
| | | 256 | | [CLSCompliant(false)] |
| | | 257 | | public override unsafe int GetBytes(char* chars, int charCount, byte* bytes, int byteCount) |
| | | 258 | | { |
| | 0 | 259 | | ArgumentNullException.ThrowIfNull(chars); |
| | 0 | 260 | | ArgumentNullException.ThrowIfNull(bytes); |
| | | 261 | | |
| | 0 | 262 | | ArgumentOutOfRangeException.ThrowIfNegative(charCount); |
| | 0 | 263 | | ArgumentOutOfRangeException.ThrowIfNegative(byteCount); |
| | | 264 | | |
| | 0 | 265 | | return GetBytes(chars, charCount, bytes, byteCount, null); |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | // Returns the number of characters produced by decoding a range of bytes |
| | | 269 | | // in a byte array. |
| | | 270 | | // |
| | | 271 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 272 | | // So if you fix this, fix the others. Currently those include: |
| | | 273 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 274 | | // parent method is safe |
| | | 275 | | |
| | | 276 | | public override unsafe int GetCharCount(byte[] bytes, int index, int count) |
| | | 277 | | { |
| | 0 | 278 | | ArgumentNullException.ThrowIfNull(bytes); |
| | | 279 | | |
| | 0 | 280 | | ArgumentOutOfRangeException.ThrowIfNegative(index); |
| | 0 | 281 | | ArgumentOutOfRangeException.ThrowIfNegative(count); |
| | | 282 | | |
| | 0 | 283 | | if (bytes.Length - index < count) |
| | 0 | 284 | | throw new ArgumentOutOfRangeException(nameof(bytes), SR.ArgumentOutOfRange_IndexCountBuffer); |
| | | 285 | | |
| | | 286 | | // If no input just return 0, fixed doesn't like 0 length arrays. |
| | 0 | 287 | | if (count == 0) |
| | 0 | 288 | | return 0; |
| | | 289 | | |
| | | 290 | | // Just call pointer version |
| | 0 | 291 | | fixed (byte* pBytes = bytes) |
| | 0 | 292 | | return GetCharCount(pBytes + index, count, null); |
| | | 293 | | } |
| | | 294 | | |
| | | 295 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 296 | | // So if you fix this, fix the others. Currently those include: |
| | | 297 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 298 | | |
| | | 299 | | [CLSCompliant(false)] |
| | | 300 | | public override unsafe int GetCharCount(byte* bytes, int count) |
| | | 301 | | { |
| | 0 | 302 | | ArgumentNullException.ThrowIfNull(bytes); |
| | | 303 | | |
| | 0 | 304 | | ArgumentOutOfRangeException.ThrowIfNegative(count); |
| | | 305 | | |
| | 0 | 306 | | return GetCharCount(bytes, count, null); |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 310 | | // So if you fix this, fix the others. Currently those include: |
| | | 311 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 312 | | // parent method is safe |
| | | 313 | | |
| | | 314 | | public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, |
| | | 315 | | char[] chars, int charIndex) |
| | | 316 | | { |
| | 0 | 317 | | ArgumentNullException.ThrowIfNull(bytes); |
| | 0 | 318 | | ArgumentNullException.ThrowIfNull(chars); |
| | | 319 | | |
| | 0 | 320 | | ArgumentOutOfRangeException.ThrowIfNegative(byteIndex); |
| | 0 | 321 | | ArgumentOutOfRangeException.ThrowIfNegative(byteCount); |
| | | 322 | | |
| | 0 | 323 | | if (bytes.Length - byteIndex < byteCount) |
| | 0 | 324 | | throw new ArgumentOutOfRangeException(nameof(bytes), SR.ArgumentOutOfRange_IndexCountBuffer); |
| | | 325 | | |
| | 0 | 326 | | if (charIndex < 0 || charIndex > chars.Length) |
| | 0 | 327 | | throw new ArgumentOutOfRangeException(nameof(charIndex), SR.ArgumentOutOfRange_IndexMustBeLessOrEqual); |
| | | 328 | | |
| | | 329 | | // If no input, return 0 & avoid fixed problem |
| | 0 | 330 | | if (byteCount == 0) |
| | 0 | 331 | | return 0; |
| | | 332 | | |
| | | 333 | | // Just call pointer version |
| | 0 | 334 | | int charCount = chars.Length - charIndex; |
| | | 335 | | |
| | 0 | 336 | | fixed (byte* pBytes = bytes) |
| | 0 | 337 | | fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) |
| | | 338 | | { |
| | | 339 | | // Remember that charCount is # to decode, not size of array |
| | 0 | 340 | | return GetChars(pBytes + byteIndex, byteCount, pChars + charIndex, charCount, null); |
| | | 341 | | } |
| | | 342 | | } |
| | | 343 | | |
| | | 344 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 345 | | // So if you fix this, fix the others. Currently those include: |
| | | 346 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 347 | | |
| | | 348 | | [CLSCompliant(false)] |
| | | 349 | | public override unsafe int GetChars(byte* bytes, int byteCount, char* chars, int charCount) |
| | | 350 | | { |
| | 0 | 351 | | ArgumentNullException.ThrowIfNull(bytes); |
| | 0 | 352 | | ArgumentNullException.ThrowIfNull(chars); |
| | | 353 | | |
| | 0 | 354 | | ArgumentOutOfRangeException.ThrowIfNegative(charCount); |
| | 0 | 355 | | ArgumentOutOfRangeException.ThrowIfNegative(byteCount); |
| | | 356 | | |
| | 0 | 357 | | return GetChars(bytes, byteCount, chars, charCount, null); |
| | | 358 | | } |
| | | 359 | | |
| | | 360 | | // Returns a string containing the decoded representation of a range of |
| | | 361 | | // bytes in a byte array. |
| | | 362 | | // |
| | | 363 | | // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) |
| | | 364 | | // So if you fix this, fix the others. Currently those include: |
| | | 365 | | // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding |
| | | 366 | | // parent method is safe |
| | | 367 | | |
| | | 368 | | public override unsafe string GetString(byte[] bytes, int index, int count) |
| | | 369 | | { |
| | 0 | 370 | | ArgumentNullException.ThrowIfNull(bytes); |
| | | 371 | | |
| | 0 | 372 | | ArgumentOutOfRangeException.ThrowIfNegative(index); |
| | 0 | 373 | | ArgumentOutOfRangeException.ThrowIfNegative(count); |
| | | 374 | | |
| | 0 | 375 | | if (bytes.Length - index < count) |
| | 0 | 376 | | throw new ArgumentOutOfRangeException(nameof(bytes), SR.ArgumentOutOfRange_IndexCountBuffer); |
| | | 377 | | |
| | | 378 | | // Avoid problems with empty input buffer |
| | 0 | 379 | | if (count == 0) return string.Empty; |
| | | 380 | | |
| | 0 | 381 | | fixed (byte* pBytes = bytes) |
| | 0 | 382 | | return string.CreateStringFromEncoding( |
| | 0 | 383 | | pBytes + index, count, this); |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | // |
| | | 387 | | // End of standard methods copied from EncodingNLS.cs |
| | | 388 | | // |
| | | 389 | | internal sealed override unsafe int GetByteCount(char* chars, int count, EncoderNLS? baseEncoder) |
| | | 390 | | { |
| | 0 | 391 | | Debug.Assert(chars is not null, "[UTF7Encoding.GetByteCount]chars!=null"); |
| | 0 | 392 | | Debug.Assert(count >= 0, "[UTF7Encoding.GetByteCount]count >=0"); |
| | | 393 | | |
| | | 394 | | // Just call GetBytes with bytes is null |
| | 0 | 395 | | return GetBytes(chars, count, null, 0, baseEncoder); |
| | | 396 | | } |
| | | 397 | | |
| | | 398 | | internal sealed override unsafe int GetBytes( |
| | | 399 | | char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS? baseEncoder) |
| | | 400 | | { |
| | 3403 | 401 | | Debug.Assert(byteCount >= 0, "[UTF7Encoding.GetBytes]byteCount >=0"); |
| | 3403 | 402 | | Debug.Assert(chars is not null, "[UTF7Encoding.GetBytes]chars!=null"); |
| | 3403 | 403 | | Debug.Assert(charCount >= 0, "[UTF7Encoding.GetBytes]charCount >=0"); |
| | | 404 | | |
| | | 405 | | // Get encoder info |
| | 3403 | 406 | | Encoder? encoder = (Encoder?)baseEncoder; |
| | | 407 | | |
| | | 408 | | // Default bits & count |
| | 3403 | 409 | | int bits = 0; |
| | 3403 | 410 | | int bitCount = -1; |
| | | 411 | | |
| | | 412 | | // prepare our helpers |
| | 3403 | 413 | | EncodingByteBuffer buffer = new EncodingByteBuffer( |
| | 3403 | 414 | | this, encoder, bytes, byteCount, chars, charCount); |
| | | 415 | | |
| | 3403 | 416 | | if (encoder is not null) |
| | | 417 | | { |
| | 3403 | 418 | | bits = encoder.bits; |
| | 3403 | 419 | | bitCount = encoder.bitCount; |
| | | 420 | | |
| | | 421 | | // May have had too many left over |
| | 3403 | 422 | | while (bitCount >= 6) |
| | | 423 | | { |
| | 0 | 424 | | bitCount -= 6; |
| | | 425 | | // If we fail we'll never really have enough room |
| | 0 | 426 | | if (!buffer.AddByte(_base64Bytes[(bits >> bitCount) & 0x3F])) |
| | 0 | 427 | | ThrowBytesOverflow(encoder, buffer.Count == 0); |
| | | 428 | | } |
| | | 429 | | } |
| | | 430 | | |
| | 289396 | 431 | | while (buffer.MoreData) |
| | | 432 | | { |
| | 285993 | 433 | | char currentChar = buffer.GetNextChar(); |
| | | 434 | | |
| | 285993 | 435 | | if (currentChar < 0x80 && _directEncode[currentChar]) |
| | | 436 | | { |
| | 33347 | 437 | | if (bitCount >= 0) |
| | | 438 | | { |
| | 17888 | 439 | | if (bitCount > 0) |
| | | 440 | | { |
| | | 441 | | // Try to add the next byte |
| | 11989 | 442 | | if (!buffer.AddByte(_base64Bytes[bits << 6 - bitCount & 0x3F])) |
| | | 443 | | break; // Stop here, didn't throw |
| | | 444 | | |
| | 11989 | 445 | | bitCount = 0; |
| | | 446 | | } |
| | | 447 | | |
| | | 448 | | // Need to get emit '-' and our char, 2 bytes total |
| | 17888 | 449 | | if (!buffer.AddByte((byte)'-')) |
| | | 450 | | break; // Stop here, didn't throw |
| | | 451 | | |
| | 17888 | 452 | | bitCount = -1; |
| | | 453 | | } |
| | | 454 | | |
| | | 455 | | // Need to emit our char |
| | 33347 | 456 | | if (!buffer.AddByte((byte)currentChar)) |
| | 0 | 457 | | break; // Stop here, didn't throw |
| | | 458 | | } |
| | 252646 | 459 | | else if (bitCount < 0 && currentChar == '+') |
| | | 460 | | { |
| | 756 | 461 | | if (!buffer.AddByte((byte)'+', (byte)'-')) |
| | 0 | 462 | | break; // Stop here, didn't throw |
| | | 463 | | } |
| | | 464 | | else |
| | | 465 | | { |
| | 251890 | 466 | | if (bitCount < 0) |
| | | 467 | | { |
| | | 468 | | // Need to emit a + and 12 bits (3 bytes) |
| | | 469 | | // Only 12 of the 16 bits will be emitted this time, the other 4 wait 'til next time |
| | 20776 | 470 | | if (!buffer.AddByte((byte)'+')) |
| | | 471 | | break; // Stop here, didn't throw |
| | | 472 | | |
| | | 473 | | // We're now in bit mode, but haven't stored data yet |
| | 20776 | 474 | | bitCount = 0; |
| | | 475 | | } |
| | | 476 | | |
| | | 477 | | // Add our bits |
| | 251890 | 478 | | bits = bits << 16 | currentChar; |
| | 251890 | 479 | | bitCount += 16; |
| | | 480 | | |
| | 916503 | 481 | | while (bitCount >= 6) |
| | | 482 | | { |
| | 664613 | 483 | | bitCount -= 6; |
| | 664613 | 484 | | if (!buffer.AddByte(_base64Bytes[(bits >> bitCount) & 0x3F])) |
| | | 485 | | { |
| | 0 | 486 | | bitCount += 6; // We didn't use these bits |
| | 0 | 487 | | buffer.GetNextChar(); // We're processing this char still, but AddByte |
| | | 488 | | // --'d it when we ran out of space |
| | 0 | 489 | | break; // Stop here, not enough room for bytes |
| | | 490 | | } |
| | | 491 | | } |
| | | 492 | | |
| | 251890 | 493 | | if (bitCount >= 6) |
| | | 494 | | break; // Didn't have room to encode enough bits |
| | | 495 | | } |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | // Now if we have bits left over we have to encode them. |
| | | 499 | | // MustFlush may have been cleared by encoding.ThrowBytesOverflow earlier if converting |
| | 3403 | 500 | | if (bitCount >= 0 && (encoder is null || encoder.MustFlush)) |
| | | 501 | | { |
| | | 502 | | // Do we have bits we have to stick in? |
| | 2888 | 503 | | if (bitCount > 0) |
| | | 504 | | { |
| | 2063 | 505 | | if (buffer.AddByte(_base64Bytes[(bits << (6 - bitCount)) & 0x3F])) |
| | | 506 | | { |
| | | 507 | | // Emitted spare bits, 0 bits left |
| | 2063 | 508 | | bitCount = 0; |
| | | 509 | | } |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | // If converting and failed bitCount above, then we'll fail this too |
| | 2888 | 513 | | if (buffer.AddByte((byte)'-')) |
| | | 514 | | { |
| | | 515 | | // turned off bit mode'; |
| | 2888 | 516 | | bits = 0; |
| | 2888 | 517 | | bitCount = -1; |
| | | 518 | | } |
| | | 519 | | else |
| | | 520 | | // If not successful, convert will maintain state for next time, also |
| | | 521 | | // AddByte will have decremented our char count, however we need it to remain the same |
| | 0 | 522 | | buffer.GetNextChar(); |
| | | 523 | | } |
| | | 524 | | |
| | | 525 | | // Do we have an encoder we're allowed to use? |
| | | 526 | | // bytes is null if counting, so don't use encoder then |
| | 3403 | 527 | | if (bytes is not null && encoder is not null) |
| | | 528 | | { |
| | | 529 | | // We already cleared bits & bitcount for mustflush case |
| | 3403 | 530 | | encoder.bits = bits; |
| | 3403 | 531 | | encoder.bitCount = bitCount; |
| | 3403 | 532 | | encoder._charsUsed = buffer.CharsUsed; |
| | | 533 | | } |
| | | 534 | | |
| | 3403 | 535 | | return buffer.Count; |
| | | 536 | | } |
| | | 537 | | |
| | | 538 | | internal sealed override unsafe int GetCharCount(byte* bytes, int count, DecoderNLS? baseDecoder) |
| | | 539 | | { |
| | 3403 | 540 | | Debug.Assert(count >= 0, "[UTF7Encoding.GetCharCount]count >=0"); |
| | 3403 | 541 | | Debug.Assert(bytes is not null, "[UTF7Encoding.GetCharCount]bytes!=null"); |
| | | 542 | | |
| | | 543 | | // Just call GetChars with null char* to do counting |
| | 3403 | 544 | | return GetChars(bytes, count, null, 0, baseDecoder); |
| | | 545 | | } |
| | | 546 | | |
| | | 547 | | internal sealed override unsafe int GetChars( |
| | | 548 | | byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS? baseDecoder) |
| | | 549 | | { |
| | 10209 | 550 | | Debug.Assert(byteCount >= 0, "[UTF7Encoding.GetChars]byteCount >=0"); |
| | 10209 | 551 | | Debug.Assert(bytes is not null, "[UTF7Encoding.GetChars]bytes!=null"); |
| | 10209 | 552 | | Debug.Assert(charCount >= 0, "[UTF7Encoding.GetChars]charCount >=0"); |
| | | 553 | | |
| | | 554 | | // Might use a decoder |
| | 10209 | 555 | | Decoder? decoder = (Decoder?)baseDecoder; |
| | | 556 | | |
| | | 557 | | // Get our output buffer info. |
| | 10209 | 558 | | EncodingCharBuffer buffer = new EncodingCharBuffer( |
| | 10209 | 559 | | this, decoder, chars, charCount, bytes, byteCount); |
| | | 560 | | |
| | | 561 | | // Get decoder info |
| | 10209 | 562 | | int bits = 0; |
| | 10209 | 563 | | int bitCount = -1; |
| | 10209 | 564 | | bool firstByte = false; |
| | 10209 | 565 | | if (decoder is not null) |
| | | 566 | | { |
| | 10209 | 567 | | bits = decoder.bits; |
| | 10209 | 568 | | bitCount = decoder.bitCount; |
| | 10209 | 569 | | firstByte = decoder.firstByte; |
| | | 570 | | |
| | 10209 | 571 | | Debug.Assert(!firstByte || decoder.bitCount <= 0, |
| | 10209 | 572 | | "[UTF7Encoding.GetChars]If remembered bits, then first byte flag shouldn't be set"); |
| | | 573 | | } |
| | | 574 | | |
| | | 575 | | // We may have had bits in the decoder that we couldn't output last time, so do so now |
| | 10209 | 576 | | if (bitCount >= 16) |
| | | 577 | | { |
| | | 578 | | // Check our decoder buffer |
| | 0 | 579 | | if (!buffer.AddChar((char)((bits >> (bitCount - 16)) & 0xFFFF))) |
| | 0 | 580 | | ThrowCharsOverflow(decoder, true); // Always throw, they need at least 1 char even in Convert |
| | | 581 | | |
| | | 582 | | // Used this one, clean up extra bits |
| | 0 | 583 | | bitCount -= 16; |
| | | 584 | | } |
| | | 585 | | |
| | | 586 | | // Loop through the input |
| | 1343637 | 587 | | while (buffer.MoreData) |
| | | 588 | | { |
| | 1333428 | 589 | | byte currentByte = buffer.GetNextByte(); |
| | | 590 | | int c; |
| | | 591 | | |
| | 1333428 | 592 | | if (bitCount >= 0) |
| | | 593 | | { |
| | | 594 | | // |
| | | 595 | | // Modified base 64 encoding. |
| | | 596 | | // |
| | | 597 | | sbyte v; |
| | 705359 | 598 | | if (currentByte < 0x80 && ((v = _base64Values[currentByte]) >= 0)) |
| | | 599 | | { |
| | 679219 | 600 | | firstByte = false; |
| | 679219 | 601 | | bits = (bits << 6) | ((byte)v); |
| | 679219 | 602 | | bitCount += 6; |
| | 679219 | 603 | | if (bitCount >= 16) |
| | | 604 | | { |
| | 251994 | 605 | | c = (bits >> (bitCount - 16)) & 0xFFFF; |
| | 251994 | 606 | | bitCount -= 16; |
| | | 607 | | } |
| | | 608 | | // If not enough bits just continue |
| | | 609 | | else continue; |
| | | 610 | | } |
| | | 611 | | else |
| | | 612 | | { |
| | | 613 | | // If it wasn't a base 64 byte, everything's going to turn off base 64 mode |
| | 26140 | 614 | | bitCount = -1; |
| | | 615 | | |
| | 26140 | 616 | | if (currentByte != '-') |
| | | 617 | | { |
| | | 618 | | // >= 0x80 (because of 1st if statemtn) |
| | | 619 | | // We need this check since the _base64Values[b] check below need b <= 0x7f. |
| | | 620 | | // This is not a valid base 64 byte. Terminate the shifted-sequence and |
| | | 621 | | // emit this byte. |
| | | 622 | | |
| | | 623 | | // not in base 64 table |
| | | 624 | | // According to the RFC 1642 and the example code of UTF-7 |
| | | 625 | | // in Unicode 2.0, we should just zero-extend the invalid UTF7 byte |
| | | 626 | | |
| | | 627 | | // Chars won't be updated unless this works, try to fallback |
| | 2620 | 628 | | if (!buffer.Fallback(currentByte)) |
| | 0 | 629 | | break; // Stop here, didn't throw |
| | | 630 | | |
| | | 631 | | // Used that byte, we're done with it |
| | | 632 | | continue; |
| | | 633 | | } |
| | | 634 | | |
| | | 635 | | // |
| | | 636 | | // The encoding for '+' is "+-". |
| | | 637 | | // |
| | 26058 | 638 | | if (firstByte) c = '+'; |
| | | 639 | | // We just turn it off if not emitting a +, so we're done. |
| | | 640 | | else continue; |
| | | 641 | | } |
| | | 642 | | // |
| | | 643 | | // End of modified base 64 encoding block. |
| | | 644 | | // |
| | | 645 | | } |
| | 628069 | 646 | | else if (currentByte == '+') |
| | | 647 | | { |
| | | 648 | | // |
| | | 649 | | // Found the start of a modified base 64 encoding block or a plus sign. |
| | | 650 | | // |
| | 26174 | 651 | | bitCount = 0; |
| | 26174 | 652 | | firstByte = true; |
| | 26174 | 653 | | continue; |
| | | 654 | | } |
| | | 655 | | else |
| | | 656 | | { |
| | | 657 | | // Normal character |
| | 601895 | 658 | | if (currentByte >= 0x80) |
| | | 659 | | { |
| | | 660 | | // Try to fallback |
| | 317002 | 661 | | if (!buffer.Fallback(currentByte)) |
| | 0 | 662 | | break; // Stop here, didn't throw |
| | | 663 | | |
| | | 664 | | // Done falling back |
| | | 665 | | continue; |
| | | 666 | | } |
| | | 667 | | |
| | | 668 | | // Use the normal character |
| | 284893 | 669 | | c = currentByte; |
| | | 670 | | } |
| | | 671 | | |
| | 539425 | 672 | | if (c >= 0) |
| | | 673 | | { |
| | | 674 | | // Check our buffer |
| | 539425 | 675 | | if (!buffer.AddChar((char)c)) |
| | | 676 | | { |
| | | 677 | | // No room. If it was a plain char we'll try again later. |
| | | 678 | | // Note, we'll consume this byte and stick it in decoder, even if we can't output it |
| | 0 | 679 | | if (bitCount >= 0) // Can we rememmber this byte (char) |
| | | 680 | | { |
| | 0 | 681 | | buffer.AdjustBytes(+1); // Need to readd the byte that AddChar subtr |
| | 0 | 682 | | bitCount += 16; // We'll still need that char we have in our |
| | | 683 | | } |
| | 0 | 684 | | break; // didn't throw, stop |
| | | 685 | | } |
| | | 686 | | } |
| | | 687 | | } |
| | | 688 | | |
| | | 689 | | // Stick stuff in the decoder if we can (chars is null if counting, so don't store decoder) |
| | 10209 | 690 | | if (chars is not null && decoder is not null) |
| | | 691 | | { |
| | | 692 | | // MustFlush? (Could've been cleared by ThrowCharsOverflow if Convert & didn't reach end of buffer) |
| | 6806 | 693 | | if (decoder.MustFlush) |
| | | 694 | | { |
| | | 695 | | // RFC doesn't specify what would happen if we have non-0 leftover bits, we just drop them |
| | 6806 | 696 | | decoder.bits = 0; |
| | 6806 | 697 | | decoder.bitCount = -1; |
| | 6806 | 698 | | decoder.firstByte = false; |
| | | 699 | | } |
| | | 700 | | else |
| | | 701 | | { |
| | 0 | 702 | | decoder.bits = bits; |
| | 0 | 703 | | decoder.bitCount = bitCount; |
| | 0 | 704 | | decoder.firstByte = firstByte; |
| | | 705 | | } |
| | 6806 | 706 | | decoder._bytesUsed = buffer.BytesUsed; |
| | | 707 | | } |
| | | 708 | | // else ignore any hanging bits. |
| | | 709 | | |
| | | 710 | | // Return our count |
| | 10209 | 711 | | return buffer.Count; |
| | | 712 | | } |
| | | 713 | | |
| | | 714 | | public override Text.Decoder GetDecoder() |
| | | 715 | | { |
| | 3403 | 716 | | return new Decoder(this); |
| | | 717 | | } |
| | | 718 | | |
| | | 719 | | public override Text.Encoder GetEncoder() |
| | | 720 | | { |
| | 3403 | 721 | | return new Encoder(this); |
| | | 722 | | } |
| | | 723 | | |
| | | 724 | | public override int GetMaxByteCount(int charCount) |
| | | 725 | | { |
| | 0 | 726 | | ArgumentOutOfRangeException.ThrowIfNegative(charCount); |
| | | 727 | | |
| | | 728 | | // Suppose that every char can not be direct-encoded, we know that |
| | | 729 | | // a byte can encode 6 bits of the Unicode character. And we will |
| | | 730 | | // also need two extra bytes for the shift-in ('+') and shift-out ('-') mark. |
| | | 731 | | // Therefore, the max byte should be: |
| | | 732 | | // byteCount = 2 + Math.Ceiling((double)charCount * 16 / 6); |
| | | 733 | | // That is always <= 2 + 3 * charCount; |
| | | 734 | | // Longest case is alternating encoded, direct, encoded data for 5 + 1 + 5... bytes per char. |
| | | 735 | | // UTF7 doesn't have left over surrogates, but if no input we may need an output - to turn off |
| | | 736 | | // encoding if MustFlush is true. |
| | | 737 | | |
| | | 738 | | // Its easiest to think of this as 2 bytes to turn on/off the base64 mode, then 3 bytes per char. |
| | | 739 | | // 3 bytes is 18 bits of encoding, which is more than we need, but if its direct encoded then 3 |
| | | 740 | | // bytes allows us to turn off and then back on base64 mode if necessary. |
| | | 741 | | |
| | | 742 | | // Note that UTF7 encoded surrogates individually and isn't worried about mismatches, so all |
| | | 743 | | // code points are encodable int UTF7. |
| | 0 | 744 | | long byteCount = (long)charCount * 3 + 2; |
| | | 745 | | |
| | | 746 | | // check for overflow |
| | 0 | 747 | | if (byteCount > 0x7fffffff) |
| | 0 | 748 | | throw new ArgumentOutOfRangeException(nameof(charCount), SR.ArgumentOutOfRange_GetByteCountOverflow); |
| | | 749 | | |
| | 0 | 750 | | return (int)byteCount; |
| | | 751 | | } |
| | | 752 | | |
| | | 753 | | public override int GetMaxCharCount(int byteCount) |
| | | 754 | | { |
| | 0 | 755 | | ArgumentOutOfRangeException.ThrowIfNegative(byteCount); |
| | | 756 | | |
| | | 757 | | // Worst case is 1 char per byte. Minimum 1 for left over bits in case decoder is being flushed |
| | | 758 | | // Also note that we ignore extra bits (per spec), so UTF7 doesn't have unknown in this direction. |
| | 0 | 759 | | int charCount = byteCount; |
| | 0 | 760 | | if (charCount == 0) charCount = 1; |
| | | 761 | | |
| | 0 | 762 | | return charCount; |
| | | 763 | | } |
| | | 764 | | |
| | | 765 | | // Of all the amazing things... This MUST be Decoder so that our com name |
| | | 766 | | // for System.Text.Decoder doesn't change |
| | | 767 | | private sealed class Decoder : DecoderNLS |
| | | 768 | | { |
| | | 769 | | /*private*/ |
| | | 770 | | internal int bits; |
| | | 771 | | /*private*/ |
| | | 772 | | internal int bitCount; |
| | | 773 | | /*private*/ |
| | | 774 | | internal bool firstByte; |
| | | 775 | | |
| | 3403 | 776 | | public Decoder(UTF7Encoding encoding) : base(encoding) |
| | | 777 | | { |
| | | 778 | | // base calls reset |
| | 3403 | 779 | | } |
| | | 780 | | |
| | | 781 | | public override void Reset() |
| | | 782 | | { |
| | 10209 | 783 | | this.bits = 0; |
| | 10209 | 784 | | this.bitCount = -1; |
| | 10209 | 785 | | this.firstByte = false; |
| | 10209 | 786 | | _fallbackBuffer?.Reset(); |
| | 6806 | 787 | | } |
| | | 788 | | |
| | | 789 | | // Anything left in our encoder? |
| | | 790 | | internal override bool HasState => |
| | | 791 | | // NOTE: This forces the last -, which some encoder might not encode. If we |
| | | 792 | | // don't see it we don't think we're done reading. |
| | 0 | 793 | | this.bitCount != -1; |
| | | 794 | | } |
| | | 795 | | |
| | | 796 | | // Of all the amazing things... This MUST be Encoder so that our com name |
| | | 797 | | // for System.Text.Encoder doesn't change |
| | | 798 | | private sealed class Encoder : EncoderNLS |
| | | 799 | | { |
| | | 800 | | /*private*/ |
| | | 801 | | internal int bits; |
| | | 802 | | /*private*/ |
| | | 803 | | internal int bitCount; |
| | | 804 | | |
| | 3403 | 805 | | public Encoder(UTF7Encoding encoding) : base(encoding) |
| | | 806 | | { |
| | | 807 | | // base calls reset |
| | 3403 | 808 | | } |
| | | 809 | | |
| | | 810 | | public override void Reset() |
| | | 811 | | { |
| | 3403 | 812 | | this.bitCount = -1; |
| | 3403 | 813 | | this.bits = 0; |
| | 3403 | 814 | | _fallbackBuffer?.Reset(); |
| | 0 | 815 | | } |
| | | 816 | | |
| | | 817 | | // Anything left in our encoder? |
| | 0 | 818 | | internal override bool HasState => this.bits != 0 || this.bitCount != -1; |
| | | 819 | | } |
| | | 820 | | |
| | | 821 | | // Preexisting UTF7 behavior for bad bytes was just to spit out the byte as the next char |
| | | 822 | | // and turn off base64 mode if it was in that mode. We still exit the mode, but now we fallback. |
| | | 823 | | private sealed class DecoderUTF7Fallback : DecoderFallback |
| | | 824 | | { |
| | | 825 | | // Default replacement fallback uses no best fit and ? replacement string |
| | | 826 | | |
| | | 827 | | public override DecoderFallbackBuffer CreateFallbackBuffer() => |
| | 3403 | 828 | | new DecoderUTF7FallbackBuffer(); |
| | | 829 | | |
| | | 830 | | // Maximum number of characters that this instance of this fallback could return |
| | 0 | 831 | | public override int MaxCharCount => 1; // returns 1 char per bad byte |
| | | 832 | | |
| | 0 | 833 | | public override bool Equals([NotNullWhen(true)] object? value) => value is DecoderUTF7Fallback; |
| | | 834 | | |
| | 0 | 835 | | public override int GetHashCode() => 984; |
| | | 836 | | } |
| | | 837 | | |
| | | 838 | | private sealed class DecoderUTF7FallbackBuffer : DecoderFallbackBuffer |
| | | 839 | | { |
| | | 840 | | // Store our default string |
| | | 841 | | private char cFallback; |
| | 3403 | 842 | | private int iCount = -1; |
| | | 843 | | private int iSize; |
| | | 844 | | |
| | | 845 | | // Fallback Methods |
| | | 846 | | public override bool Fallback(byte[] bytesUnknown, int index) |
| | | 847 | | { |
| | | 848 | | // We expect no previous fallback in our buffer |
| | 159811 | 849 | | Debug.Assert(iCount < 0, "[DecoderUTF7FallbackBuffer.Fallback] Can't have recursive fallbacks"); |
| | 159811 | 850 | | Debug.Assert(bytesUnknown.Length == 1, "[DecoderUTF7FallbackBuffer.Fallback] Only possible fallback case |
| | | 851 | | |
| | | 852 | | // Go ahead and get our fallback |
| | 159811 | 853 | | cFallback = (char)bytesUnknown[0]; |
| | | 854 | | |
| | | 855 | | // Any of the fallback characters can be handled except for 0 |
| | 159811 | 856 | | if (cFallback == 0) |
| | | 857 | | { |
| | 534 | 858 | | return false; |
| | | 859 | | } |
| | | 860 | | |
| | 159277 | 861 | | iCount = iSize = 1; |
| | | 862 | | |
| | 159277 | 863 | | return true; |
| | | 864 | | } |
| | | 865 | | |
| | | 866 | | public override char GetNextChar() |
| | | 867 | | { |
| | 318554 | 868 | | if (iCount-- > 0) |
| | 159277 | 869 | | return cFallback; |
| | | 870 | | |
| | | 871 | | // Note: this means that 0 in UTF7 stream will never be emitted. |
| | 159277 | 872 | | return (char)0; |
| | | 873 | | } |
| | | 874 | | |
| | | 875 | | public override bool MovePrevious() |
| | | 876 | | { |
| | 0 | 877 | | if (iCount >= 0) |
| | | 878 | | { |
| | 0 | 879 | | iCount++; |
| | | 880 | | } |
| | | 881 | | |
| | | 882 | | // return true if we were allowed to do this |
| | 0 | 883 | | return iCount >= 0 && iCount <= iSize; |
| | | 884 | | } |
| | | 885 | | |
| | | 886 | | // Return # of chars left in this fallback |
| | 10209 | 887 | | public override int Remaining => (iCount > 0) ? iCount : 0; |
| | | 888 | | |
| | | 889 | | // Clear the buffer |
| | | 890 | | public override unsafe void Reset() |
| | | 891 | | { |
| | 6806 | 892 | | iCount = -1; |
| | 6806 | 893 | | byteStart = null; |
| | 6806 | 894 | | } |
| | | 895 | | |
| | | 896 | | // This version just counts the fallback and doesn't actually copy anything. |
| | | 897 | | internal override unsafe int InternalFallback(byte[] bytes, byte* pBytes) |
| | | 898 | | // Right now this has both bytes and bytes[], since we might have extra bytes, hence the |
| | | 899 | | // array, and we might need the index, hence the byte* |
| | | 900 | | { |
| | | 901 | | // We expect no previous fallback in our buffer |
| | 159811 | 902 | | Debug.Assert(iCount < 0, "[DecoderUTF7FallbackBuffer.InternalFallback] Can't have recursive fallbacks"); |
| | 159811 | 903 | | if (bytes.Length != 1) |
| | | 904 | | { |
| | 0 | 905 | | throw new ArgumentException(SR.Argument_InvalidCharSequenceNoIndex); |
| | | 906 | | } |
| | | 907 | | |
| | | 908 | | // Can't fallback a byte 0, so return for that case, 1 otherwise. |
| | 159811 | 909 | | return bytes[0] == 0 ? 0 : 1; |
| | | 910 | | } |
| | | 911 | | } |
| | | 912 | | } |
| | | 913 | | } |
| | | 914 | | |