mscorlib(4.0.0.0) API with additions
UTF7Encoding.cs
3 using System.Security;
4 
5 namespace System.Text
6 {
9  [ComVisible(true)]
10  [__DynamicallyInvokable]
11  public class UTF7Encoding : Encoding
12  {
13  [Serializable]
14  private class Decoder : DecoderNLS, ISerializable
15  {
16  internal int bits;
17 
18  internal int bitCount;
19 
20  internal bool firstByte;
21 
22  internal override bool HasState => bitCount != -1;
23 
24  public Decoder(UTF7Encoding encoding)
25  : base(encoding)
26  {
27  }
28 
29  internal Decoder(SerializationInfo info, StreamingContext context)
30  {
31  if (info == null)
32  {
33  throw new ArgumentNullException("info");
34  }
35  bits = (int)info.GetValue("bits", typeof(int));
36  bitCount = (int)info.GetValue("bitCount", typeof(int));
37  firstByte = (bool)info.GetValue("firstByte", typeof(bool));
38  m_encoding = (Encoding)info.GetValue("encoding", typeof(Encoding));
39  }
40 
41  [SecurityCritical]
43  {
44  if (info == null)
45  {
46  throw new ArgumentNullException("info");
47  }
48  info.AddValue("encoding", m_encoding);
49  info.AddValue("bits", bits);
50  info.AddValue("bitCount", bitCount);
51  info.AddValue("firstByte", firstByte);
52  }
53 
54  public override void Reset()
55  {
56  bits = 0;
57  bitCount = -1;
58  firstByte = false;
59  if (m_fallbackBuffer != null)
60  {
61  m_fallbackBuffer.Reset();
62  }
63  }
64  }
65 
66  [Serializable]
67  private class Encoder : EncoderNLS, ISerializable
68  {
69  internal int bits;
70 
71  internal int bitCount;
72 
73  internal override bool HasState
74  {
75  get
76  {
77  if (bits == 0)
78  {
79  return bitCount != -1;
80  }
81  return true;
82  }
83  }
84 
85  public Encoder(UTF7Encoding encoding)
86  : base(encoding)
87  {
88  }
89 
90  internal Encoder(SerializationInfo info, StreamingContext context)
91  {
92  if (info == null)
93  {
94  throw new ArgumentNullException("info");
95  }
96  bits = (int)info.GetValue("bits", typeof(int));
97  bitCount = (int)info.GetValue("bitCount", typeof(int));
98  m_encoding = (Encoding)info.GetValue("encoding", typeof(Encoding));
99  }
100 
101  [SecurityCritical]
103  {
104  if (info == null)
105  {
106  throw new ArgumentNullException("info");
107  }
108  info.AddValue("encoding", m_encoding);
109  info.AddValue("bits", bits);
110  info.AddValue("bitCount", bitCount);
111  }
112 
113  public override void Reset()
114  {
115  bitCount = -1;
116  bits = 0;
117  if (m_fallbackBuffer != null)
118  {
119  m_fallbackBuffer.Reset();
120  }
121  }
122  }
123 
124  [Serializable]
125  internal sealed class DecoderUTF7Fallback : DecoderFallback
126  {
127  public override int MaxCharCount => 1;
128 
129  public override DecoderFallbackBuffer CreateFallbackBuffer()
130  {
131  return new DecoderUTF7FallbackBuffer(this);
132  }
133 
134  public override bool Equals(object value)
135  {
136  DecoderUTF7Fallback decoderUTF7Fallback = value as DecoderUTF7Fallback;
137  if (decoderUTF7Fallback != null)
138  {
139  return true;
140  }
141  return false;
142  }
143 
144  public override int GetHashCode()
145  {
146  return 984;
147  }
148  }
149 
150  internal sealed class DecoderUTF7FallbackBuffer : DecoderFallbackBuffer
151  {
152  private char cFallback;
153 
154  private int iCount = -1;
155 
156  private int iSize;
157 
158  public override int Remaining
159  {
160  get
161  {
162  if (iCount <= 0)
163  {
164  return 0;
165  }
166  return iCount;
167  }
168  }
169 
170  public DecoderUTF7FallbackBuffer(DecoderUTF7Fallback fallback)
171  {
172  }
173 
174  public override bool Fallback(byte[] bytesUnknown, int index)
175  {
176  cFallback = (char)bytesUnknown[0];
177  if (cFallback == '\0')
178  {
179  return false;
180  }
181  iCount = (iSize = 1);
182  return true;
183  }
184 
185  public override char GetNextChar()
186  {
187  if (iCount-- > 0)
188  {
189  return cFallback;
190  }
191  return '\0';
192  }
193 
194  public override bool MovePrevious()
195  {
196  if (iCount >= 0)
197  {
198  iCount++;
199  }
200  if (iCount >= 0)
201  {
202  return iCount <= iSize;
203  }
204  return false;
205  }
206 
207  [SecuritySafeCritical]
208  public unsafe override void Reset()
209  {
210  iCount = -1;
211  byteStart = null;
212  }
213 
214  [SecurityCritical]
215  internal unsafe override int InternalFallback(byte[] bytes, byte* pBytes)
216  {
217  if (bytes.Length != 1)
218  {
219  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidCharSequenceNoIndex"));
220  }
221  if (bytes[0] != 0)
222  {
223  return 1;
224  }
225  return 0;
226  }
227  }
228 
229  private const string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
230 
231  private const string directChars = "\t\n\r '(),-./0123456789:?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
232 
233  private const string optionalChars = "!\"#$%&*;<=>@[]^_`{|}";
234 
235  private byte[] base64Bytes;
236 
237  private sbyte[] base64Values;
238 
239  private bool[] directEncode;
240 
241  [OptionalField(VersionAdded = 2)]
242  private bool m_allowOptionals;
243 
244  private const int UTF7_CODEPAGE = 65000;
245 
247  [__DynamicallyInvokable]
248  public UTF7Encoding()
249  : this(allowOptionals: false)
250  {
251  }
252 
256  [__DynamicallyInvokable]
257  public UTF7Encoding(bool allowOptionals)
258  : base(65000)
259  {
260  m_allowOptionals = allowOptionals;
261  MakeTables();
262  }
263 
264  private void MakeTables()
265  {
266  base64Bytes = new byte[64];
267  for (int i = 0; i < 64; i++)
268  {
269  base64Bytes[i] = (byte)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i];
270  }
271  base64Values = new sbyte[128];
272  for (int j = 0; j < 128; j++)
273  {
274  base64Values[j] = -1;
275  }
276  for (int k = 0; k < 64; k++)
277  {
278  base64Values[base64Bytes[k]] = (sbyte)k;
279  }
280  directEncode = new bool[128];
281  int length = "\t\n\r '(),-./0123456789:?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".Length;
282  for (int l = 0; l < length; l++)
283  {
284  directEncode["\t\n\r '(),-./0123456789:?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"[l]] = true;
285  }
286  if (m_allowOptionals)
287  {
288  length = "!\"#$%&*;<=>@[]^_`{|}".Length;
289  for (int m = 0; m < length; m++)
290  {
291  directEncode["!\"#$%&*;<=>@[]^_`{|}"[m]] = true;
292  }
293  }
294  }
295 
296  internal override void SetDefaultFallbacks()
297  {
298  encoderFallback = new EncoderReplacementFallback(string.Empty);
299  decoderFallback = new DecoderUTF7Fallback();
300  }
301 
302  [OnDeserializing]
303  private void OnDeserializing(StreamingContext ctx)
304  {
305  OnDeserializing();
306  }
307 
308  [OnDeserialized]
309  private void OnDeserialized(StreamingContext ctx)
310  {
311  OnDeserialized();
312  if (m_deserializedFromEverett)
313  {
314  m_allowOptionals = directEncode["!\"#$%&*;<=>@[]^_`{|}"[0]];
315  }
316  MakeTables();
317  }
318 
323  [ComVisible(false)]
324  [__DynamicallyInvokable]
325  public override bool Equals(object value)
326  {
327  UTF7Encoding uTF7Encoding = value as UTF7Encoding;
328  if (uTF7Encoding != null)
329  {
330  if (m_allowOptionals == uTF7Encoding.m_allowOptionals && base.EncoderFallback.Equals(uTF7Encoding.EncoderFallback))
331  {
332  return base.DecoderFallback.Equals(uTF7Encoding.DecoderFallback);
333  }
334  return false;
335  }
336  return false;
337  }
338 
341  [ComVisible(false)]
342  [__DynamicallyInvokable]
343  public override int GetHashCode()
344  {
345  return CodePage + base.EncoderFallback.GetHashCode() + base.DecoderFallback.GetHashCode();
346  }
347 
360  [SecuritySafeCritical]
361  [__DynamicallyInvokable]
362  public unsafe override int GetByteCount(char[] chars, int index, int count)
363  {
364  if (chars == null)
365  {
366  throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
367  }
368  if (index < 0 || count < 0)
369  {
370  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
371  }
372  if (chars.Length - index < count)
373  {
374  throw new ArgumentOutOfRangeException("chars", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
375  }
376  if (chars.Length == 0)
377  {
378  return 0;
379  }
380  fixed (char* ptr = chars)
381  {
382  return GetByteCount(ptr + index, count, null);
383  }
384  }
385 
394  [SecuritySafeCritical]
395  [ComVisible(false)]
396  [__DynamicallyInvokable]
397  public unsafe override int GetByteCount(string s)
398  {
399  if (s == null)
400  {
401  throw new ArgumentNullException("s");
402  }
403  fixed (char* chars = s)
404  {
405  return GetByteCount(chars, s.Length, null);
406  }
407  }
408 
419  [SecurityCritical]
420  [CLSCompliant(false)]
421  [ComVisible(false)]
422  public unsafe override int GetByteCount(char* chars, int count)
423  {
424  if (chars == null)
425  {
426  throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
427  }
428  if (count < 0)
429  {
430  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
431  }
432  return GetByteCount(chars, count, null);
433  }
434 
453  [SecuritySafeCritical]
454  [ComVisible(false)]
455  [__DynamicallyInvokable]
456  public unsafe override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
457  {
458  if (s == null || bytes == null)
459  {
460  throw new ArgumentNullException((s == null) ? "s" : "bytes", Environment.GetResourceString("ArgumentNull_Array"));
461  }
462  if (charIndex < 0 || charCount < 0)
463  {
464  throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
465  }
466  if (s.Length - charIndex < charCount)
467  {
468  throw new ArgumentOutOfRangeException("s", Environment.GetResourceString("ArgumentOutOfRange_IndexCount"));
469  }
470  if (byteIndex < 0 || byteIndex > bytes.Length)
471  {
472  throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
473  }
474  int byteCount = bytes.Length - byteIndex;
475  if (bytes.Length == 0)
476  {
477  bytes = new byte[1];
478  }
479  fixed (char* ptr = s)
480  {
481  byte[] array = bytes;
482  fixed (byte* ptr2 = array)
483  {
484  return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, null);
485  }
486  }
487  }
488 
507  [SecuritySafeCritical]
508  [__DynamicallyInvokable]
509  public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
510  {
511  if (chars == null || bytes == null)
512  {
513  throw new ArgumentNullException((chars == null) ? "chars" : "bytes", Environment.GetResourceString("ArgumentNull_Array"));
514  }
515  if (charIndex < 0 || charCount < 0)
516  {
517  throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
518  }
519  if (chars.Length - charIndex < charCount)
520  {
521  throw new ArgumentOutOfRangeException("chars", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
522  }
523  if (byteIndex < 0 || byteIndex > bytes.Length)
524  {
525  throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
526  }
527  if (chars.Length == 0)
528  {
529  return 0;
530  }
531  int byteCount = bytes.Length - byteIndex;
532  if (bytes.Length == 0)
533  {
534  bytes = new byte[1];
535  }
536  fixed (char* ptr = chars)
537  {
538  byte[] array = bytes;
539  fixed (byte* ptr2 = array)
540  {
541  return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, null);
542  }
543  }
544  }
545 
561  [SecurityCritical]
562  [CLSCompliant(false)]
563  [ComVisible(false)]
564  public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount)
565  {
566  if (bytes == null || chars == null)
567  {
568  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
569  }
570  if (charCount < 0 || byteCount < 0)
571  {
572  throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
573  }
574  return GetBytes(chars, charCount, bytes, byteCount, null);
575  }
576 
589  [SecuritySafeCritical]
590  [__DynamicallyInvokable]
591  public unsafe override int GetCharCount(byte[] bytes, int index, int count)
592  {
593  if (bytes == null)
594  {
595  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
596  }
597  if (index < 0 || count < 0)
598  {
599  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
600  }
601  if (bytes.Length - index < count)
602  {
603  throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
604  }
605  if (bytes.Length == 0)
606  {
607  return 0;
608  }
609  fixed (byte* ptr = bytes)
610  {
611  return GetCharCount(ptr + index, count, null);
612  }
613  }
614 
625  [SecurityCritical]
626  [CLSCompliant(false)]
627  [ComVisible(false)]
628  public unsafe override int GetCharCount(byte* bytes, int count)
629  {
630  if (bytes == null)
631  {
632  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
633  }
634  if (count < 0)
635  {
636  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
637  }
638  return GetCharCount(bytes, count, null);
639  }
640 
659  [SecuritySafeCritical]
660  [__DynamicallyInvokable]
661  public unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
662  {
663  if (bytes == null || chars == null)
664  {
665  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
666  }
667  if (byteIndex < 0 || byteCount < 0)
668  {
669  throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
670  }
671  if (bytes.Length - byteIndex < byteCount)
672  {
673  throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
674  }
675  if (charIndex < 0 || charIndex > chars.Length)
676  {
677  throw new ArgumentOutOfRangeException("charIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
678  }
679  if (bytes.Length == 0)
680  {
681  return 0;
682  }
683  int charCount = chars.Length - charIndex;
684  if (chars.Length == 0)
685  {
686  chars = new char[1];
687  }
688  fixed (byte* ptr = bytes)
689  {
690  char[] array = chars;
691  fixed (char* ptr2 = array)
692  {
693  return GetChars(ptr + byteIndex, byteCount, ptr2 + charIndex, charCount, null);
694  }
695  }
696  }
697 
713  [SecurityCritical]
714  [CLSCompliant(false)]
715  [ComVisible(false)]
716  public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount)
717  {
718  if (bytes == null || chars == null)
719  {
720  throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array"));
721  }
722  if (charCount < 0 || byteCount < 0)
723  {
724  throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
725  }
726  return GetChars(bytes, byteCount, chars, charCount, null);
727  }
728 
741  [SecuritySafeCritical]
742  [ComVisible(false)]
743  [__DynamicallyInvokable]
744  public unsafe override string GetString(byte[] bytes, int index, int count)
745  {
746  if (bytes == null)
747  {
748  throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
749  }
750  if (index < 0 || count < 0)
751  {
752  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
753  }
754  if (bytes.Length - index < count)
755  {
756  throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
757  }
758  if (bytes.Length == 0)
759  {
760  return string.Empty;
761  }
762  fixed (byte* ptr = bytes)
763  {
764  return string.CreateStringFromEncoding(ptr + index, count, this);
765  }
766  }
767 
768  [SecurityCritical]
769  internal unsafe override int GetByteCount(char* chars, int count, EncoderNLS baseEncoder)
770  {
771  return GetBytes(chars, count, null, 0, baseEncoder);
772  }
773 
774  [SecurityCritical]
775  internal unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS baseEncoder)
776  {
777  Encoder encoder = (Encoder)baseEncoder;
778  int num = 0;
779  int num2 = -1;
780  EncodingByteBuffer encodingByteBuffer = new EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
781  if (encoder != null)
782  {
783  num = encoder.bits;
784  num2 = encoder.bitCount;
785  while (num2 >= 6)
786  {
787  num2 -= 6;
788  if (!encodingByteBuffer.AddByte(base64Bytes[(num >> num2) & 0x3F]))
789  {
790  ThrowBytesOverflow(encoder, encodingByteBuffer.Count == 0);
791  }
792  }
793  }
794  while (encodingByteBuffer.MoreData)
795  {
796  char nextChar = encodingByteBuffer.GetNextChar();
797  if (nextChar < '\u0080' && directEncode[nextChar])
798  {
799  if (num2 >= 0)
800  {
801  if (num2 > 0)
802  {
803  if (!encodingByteBuffer.AddByte(base64Bytes[(num << 6 - num2) & 0x3F]))
804  {
805  break;
806  }
807  num2 = 0;
808  }
809  if (!encodingByteBuffer.AddByte(45))
810  {
811  break;
812  }
813  num2 = -1;
814  }
815  if (!encodingByteBuffer.AddByte((byte)nextChar))
816  {
817  break;
818  }
819  continue;
820  }
821  if (num2 < 0 && nextChar == '+')
822  {
823  if (!encodingByteBuffer.AddByte((byte)43, (byte)45))
824  {
825  break;
826  }
827  continue;
828  }
829  if (num2 < 0)
830  {
831  if (!encodingByteBuffer.AddByte(43))
832  {
833  break;
834  }
835  num2 = 0;
836  }
837  num = ((num << 16) | nextChar);
838  num2 += 16;
839  while (num2 >= 6)
840  {
841  num2 -= 6;
842  if (!encodingByteBuffer.AddByte(base64Bytes[(num >> num2) & 0x3F]))
843  {
844  num2 += 6;
845  nextChar = encodingByteBuffer.GetNextChar();
846  break;
847  }
848  }
849  if (num2 >= 6)
850  {
851  break;
852  }
853  }
854  if (num2 >= 0 && (encoder == null || encoder.MustFlush))
855  {
856  if (num2 > 0 && encodingByteBuffer.AddByte(base64Bytes[(num << 6 - num2) & 0x3F]))
857  {
858  num2 = 0;
859  }
860  if (encodingByteBuffer.AddByte(45))
861  {
862  num = 0;
863  num2 = -1;
864  }
865  else
866  {
867  encodingByteBuffer.GetNextChar();
868  }
869  }
870  if (bytes != null && encoder != null)
871  {
872  encoder.bits = num;
873  encoder.bitCount = num2;
874  encoder.m_charsUsed = encodingByteBuffer.CharsUsed;
875  }
876  return encodingByteBuffer.Count;
877  }
878 
879  [SecurityCritical]
880  internal unsafe override int GetCharCount(byte* bytes, int count, DecoderNLS baseDecoder)
881  {
882  return GetChars(bytes, count, null, 0, baseDecoder);
883  }
884 
885  [SecurityCritical]
886  internal unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS baseDecoder)
887  {
888  Decoder decoder = (Decoder)baseDecoder;
889  EncodingCharBuffer encodingCharBuffer = new EncodingCharBuffer(this, decoder, chars, charCount, bytes, byteCount);
890  int num = 0;
891  int num2 = -1;
892  bool flag = false;
893  if (decoder != null)
894  {
895  num = decoder.bits;
896  num2 = decoder.bitCount;
897  flag = decoder.firstByte;
898  }
899  if (num2 >= 16)
900  {
901  if (!encodingCharBuffer.AddChar((char)((num >> num2 - 16) & 0xFFFF)))
902  {
903  ThrowCharsOverflow(decoder, nothingDecoded: true);
904  }
905  num2 -= 16;
906  }
907  while (encodingCharBuffer.MoreData)
908  {
909  byte nextByte = encodingCharBuffer.GetNextByte();
910  int num3;
911  if (num2 >= 0)
912  {
913  sbyte b;
914  if (nextByte < 128 && (b = base64Values[nextByte]) >= 0)
915  {
916  flag = false;
917  num = ((num << 6) | (byte)b);
918  num2 += 6;
919  if (num2 < 16)
920  {
921  continue;
922  }
923  num3 = ((num >> num2 - 16) & 0xFFFF);
924  num2 -= 16;
925  }
926  else
927  {
928  num2 = -1;
929  if (nextByte != 45)
930  {
931  if (!encodingCharBuffer.Fallback(nextByte))
932  {
933  break;
934  }
935  continue;
936  }
937  if (!flag)
938  {
939  continue;
940  }
941  num3 = 43;
942  }
943  }
944  else
945  {
946  if (nextByte == 43)
947  {
948  num2 = 0;
949  flag = true;
950  continue;
951  }
952  if (nextByte >= 128)
953  {
954  if (!encodingCharBuffer.Fallback(nextByte))
955  {
956  break;
957  }
958  continue;
959  }
960  num3 = nextByte;
961  }
962  if (num3 >= 0 && !encodingCharBuffer.AddChar((char)num3))
963  {
964  if (num2 >= 0)
965  {
966  encodingCharBuffer.AdjustBytes(1);
967  num2 += 16;
968  }
969  break;
970  }
971  }
972  if (chars != null && decoder != null)
973  {
974  if (decoder.MustFlush)
975  {
976  decoder.bits = 0;
977  decoder.bitCount = -1;
978  decoder.firstByte = false;
979  }
980  else
981  {
982  decoder.bits = num;
983  decoder.bitCount = num2;
984  decoder.firstByte = flag;
985  }
986  decoder.m_bytesUsed = encodingCharBuffer.BytesUsed;
987  }
988  return encodingCharBuffer.Count;
989  }
990 
993  [__DynamicallyInvokable]
994  public override System.Text.Decoder GetDecoder()
995  {
996  return new Decoder(this);
997  }
998 
1001  [__DynamicallyInvokable]
1002  public override System.Text.Encoder GetEncoder()
1003  {
1004  return new Encoder(this);
1005  }
1006 
1014  [__DynamicallyInvokable]
1015  public override int GetMaxByteCount(int charCount)
1016  {
1017  if (charCount < 0)
1018  {
1019  throw new ArgumentOutOfRangeException("charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1020  }
1021  long num = (long)charCount * 3L + 2;
1022  if (num > int.MaxValue)
1023  {
1024  throw new ArgumentOutOfRangeException("charCount", Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow"));
1025  }
1026  return (int)num;
1027  }
1028 
1036  [__DynamicallyInvokable]
1037  public override int GetMaxCharCount(int byteCount)
1038  {
1039  if (byteCount < 0)
1040  {
1041  throw new ArgumentOutOfRangeException("byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1042  }
1043  int num = byteCount;
1044  if (num == 0)
1045  {
1046  num = 1;
1047  }
1048  return num;
1049  }
1050  }
1051 }
Represents a character encoding.To browse the .NET Framework source code for this type,...
Definition: Encoding.cs:15
unsafe override int GetCharCount(byte[] bytes, int index, int count)
Calculates the number of characters produced by decoding a sequence of bytes from the specified byte ...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
unsafe override string GetString(byte[] bytes, int index, int count)
Decodes a range of bytes from a byte array into a string.
Converts a set of characters into a sequence of bytes.
Definition: Encoder.cs:11
override bool Equals(object value)
Gets a value indicating whether the specified object is equal to the current T:System....
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
unsafe override int GetByteCount(char *chars, int count)
Calculates the number of bytes produced by encoding a set of characters starting at the specified cha...
unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount)
Decodes a sequence of bytes starting at the specified byte pointer into a set of characters that are ...
DecoderFallback DecoderFallback
Gets or sets the T:System.Text.DecoderFallback object for the current T:System.Text....
Definition: Encoding.cs:884
override int GetMaxCharCount(int byteCount)
Calculates the maximum number of characters produced by decoding the specified number of bytes.
EncoderFallback EncoderFallback
Gets or sets the T:System.Text.EncoderFallback object for the current T:System.Text....
Definition: Encoding.cs:857
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
unsafe override int GetCharCount(byte *bytes, int count)
Calculates the number of characters produced by decoding a sequence of bytes starting at the specifie...
unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
Encodes a set of characters from the specified character array into the specified byte array.
Provides a buffer that allows a fallback handler to return an alternate string to a decoder when it c...
virtual int CodePage
When overridden in a derived class, gets the code page identifier of the current T:System....
Definition: Encoding.cs:948
override int GetMaxByteCount(int charCount)
Calculates the maximum number of bytes produced by encoding the specified number of characters.
Converts a sequence of encoded bytes into a set of characters.
Definition: Decoder.cs:11
Represents a UTF-7 encoding of Unicode characters.
Definition: UTF7Encoding.cs:11
Provides a failure-handling mechanism, called a fallback, for an encoded input byte sequence that can...
unsafe override int GetByteCount(char[] chars, int index, int count)
Calculates the number of bytes produced by encoding a set of characters from the specified character ...
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
UTF7Encoding()
Initializes a new instance of the T:System.Text.UTF7Encoding class.
unsafe override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
Encodes a set of characters from the specified T:System.String into the specified byte array.
The exception that is thrown when one of the arguments provided to a method is not valid.
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
Decodes a sequence of bytes from the specified byte array into the specified character array.
unsafe override int GetByteCount(string s)
Calculates the number of bytes produced by encoding the characters in the specified T:System....
override int GetHashCode()
Returns the hash code for the current T:System.Text.UTF7Encoding object.
Specifies that the class can be serialized.
UTF7Encoding(bool allowOptionals)
Initializes a new instance of the T:System.Text.UTF7Encoding class. A parameter specifies whether to ...
Encoding()
Initializes a new instance of the T:System.Text.Encoding class.
Definition: Encoding.cs:1053
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount)
Encodes a set of characters starting at the specified character pointer into a sequence of bytes that...
override System.Text.Encoder GetEncoder()
Obtains an encoder that converts a sequence of Unicode characters into a UTF-7 encoded sequence of by...
override System.Text.Decoder GetDecoder()
Obtains a decoder that converts a UTF-7 encoded sequence of bytes into a sequence of Unicode characte...