2006-03-30 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System.Text / UTF7Encoding.cs
1 /*
2  * UTF7Encoding.cs - Implementation of the
3  *              "System.Text.UTF7Encoding" class.
4  *
5  * Copyright (c) 2002  Southern Storm Software, Pty Ltd
6  * Copyright (c) 2003, 2004, Novell, Inc.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 namespace System.Text
28 {
29
30 using System;
31
32 [Serializable]
33 [MonoTODO ("Fix serialization compatibility with MS.NET")]
34 #if ECMA_COMPAT
35 internal
36 #else
37 public
38 #endif
39 class UTF7Encoding : Encoding
40 {
41         // Magic number used by Windows for UTF-7.
42         internal const int UTF7_CODE_PAGE = 65000;
43
44         // Internal state.
45         private bool allowOptionals;
46
47         // Encoding rule table for 0x00-0x7F.
48         // 0 - full encode, 1 - direct, 2 - optional, 3 - encode plus.
49         private static readonly byte[] encodingRules = {
50                 0, 0, 0, 0, 0, 0, 0, 0,   0, 1, 1, 0, 0, 1, 0, 0,       // 00
51                 0, 0, 0, 0, 0, 0, 0, 0,   0, 0, 0, 0, 0, 0, 0, 0,       // 10
52                 1, 2, 2, 2, 2, 2, 2, 1,   1, 1, 2, 3, 1, 1, 1, 1,       // 20
53                 1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 2, 2, 2, 2, 1,       // 30
54
55                 2, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,       // 40
56                 1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 2, 0, 2, 2, 2,       // 50
57                 2, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 1, 1, 1, 1, 1,       // 60
58                 1, 1, 1, 1, 1, 1, 1, 1,   1, 1, 1, 2, 2, 2, 0, 0,       // 70
59         };
60
61         // Characters to use to encode 6-bit values in base64.
62         private const String base64Chars =
63                 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
64
65         // Map bytes in base64 to 6-bit values.
66         private static readonly sbyte[] base64Values = {
67                 -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1, // 00
68                 -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1, // 10
69                 -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, 62, -1, -1, 63, // 20
70                 52, 53, 54, 55, 56, 57, 58, 59,   60, 61, -1, -1, -1, -1, -1, -1, // 30
71
72                 -1,  0,  1,  2,  3,  4,  5,  6,    7,  8,  9, 10, 11, 12, 13, 14, // 40
73                 15, 16, 17, 18, 19, 20, 21, 22,   23, 24, 25, -1, -1, -1, -1, -1, // 50
74                 -1, 26, 27, 28, 29, 30, 31, 32,   33, 34, 35, 36, 37, 38, 39, 40, // 60
75                 41, 42, 43, 44, 45, 46, 47, 48,   49, 50, 51, -1, -1, -1, -1, -1, // 70
76
77                 -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1, // 80
78                 -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1, // 90
79                 -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1, // A0
80                 -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1, // B0
81
82                 -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1, // C0
83                 -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1, // D0
84                 -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1, // E0
85                 -1, -1, -1, -1, -1, -1, -1, -1,   -1, -1, -1, -1, -1, -1, -1, -1, // F0
86         };
87
88         // Constructors.
89         public UTF7Encoding ()
90         : this (false)
91         {
92         }
93         
94         public UTF7Encoding (bool allowOptionals)
95         : base (UTF7_CODE_PAGE)
96         {
97                 this.allowOptionals = allowOptionals;
98                 
99                 body_name = "utf-7";
100                 encoding_name = "Unicode (UTF-7)";
101                 header_name = "utf-7";
102                 is_mail_news_display = true;
103                 is_mail_news_save = true;
104                 web_name = "utf-7";
105                 windows_code_page = UnicodeEncoding.UNICODE_CODE_PAGE;
106         }
107
108         // Internal version of "GetByteCount" that can handle
109         // a rolling state between calls.
110         private static int InternalGetByteCount
111                                 (char[] chars, int index, int count, bool flush,
112                                  int leftOver, bool isInShifted, bool allowOptionals)
113         {
114                 // Validate the parameters.
115                 if (chars == null) {
116                         throw new ArgumentNullException ("chars");
117                 }
118                 if (index < 0 || index > chars.Length) {
119                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
120                 }
121                 if (count < 0 || count > (chars.Length - index)) {
122                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
123                 }
124
125                 // Determine the length of the output.
126                 int length = 0;
127                 int leftOverSize = (leftOver >> 8);
128                 byte[] rules = encodingRules;
129                 int ch, rule;
130                 while (count > 0) {
131                         ch = (int)(chars[index++]);
132                         --count;
133                         if (ch < 0x0080) {
134                                 rule = rules[ch];
135                         } else {
136                                 rule = 0;
137                         }
138                         switch (rule) {
139                         case 0:
140                                 // Handle characters that must be fully encoded.
141                                 if ( !isInShifted ) {
142                                         ++length;
143                                     leftOverSize = 0;
144                                         isInShifted = true;
145                                 }
146                                 leftOverSize += 16;
147                                 while (leftOverSize >= 6) {
148                                         ++length;
149                                         leftOverSize -= 6;
150                                 }
151                                 break;
152                         case 1:
153                                 // The character is encoded as itself.
154                                 if (isInShifted) {
155                                         if (leftOverSize != 0) {
156                                                 // Flush the previous encoded sequence.
157                                                 ++length;
158                                                 leftOverSize = 0;
159                                         }
160                                         // Count the "-" (sequence terminator)
161                                         ++length;
162                                         isInShifted = false;
163                                 }
164                                 ++length;
165                                 break;
166                         case 2:
167                                 // The character may need to be encoded.
168                                 if (allowOptionals) {
169                                         goto case 1;
170                                 } else {
171                                         goto case 0;
172                                 }
173                         // Not reached.
174                         case 3:
175                                 // Encode the plus sign as "+-".
176                                 if (isInShifted) {
177                                         if (leftOverSize != 0) {
178                                                 // Flush the previous encoded sequence.
179                                                 ++length;
180                                                 leftOverSize = 0;
181                                         }
182                                         // Count the "-" (sequence terminator)
183                                         ++length;
184                                         isInShifted = false;
185                                 }
186                                 length += 2;
187                                 break;
188                         }
189                 }
190                 if (isInShifted && flush) {
191                         if (leftOverSize != 0) 
192                         {
193                                 // Flush the previous encoded sequence.
194                                 ++length;
195                         }
196                         // Count the "-" (sequence terminator)
197                         ++length;
198                 }
199
200                 // Return the length to the caller.
201                 return length;
202         }
203
204         // Get the number of bytes needed to encode a character buffer.
205         public override int GetByteCount (char[] chars, int index, int count)
206         {
207                 return InternalGetByteCount (chars, index, count, true, 0, false, allowOptionals);
208         }
209
210         // Internal version of "GetBytes" that can handle a
211         // rolling state between calls.
212         private static int InternalGetBytes
213                                 (char[] chars, int charIndex, int charCount,
214                                  byte[] bytes, int byteIndex, bool flush,
215                                  ref int leftOver, ref bool isInShifted, bool allowOptionals)
216         {
217                 // Validate the parameters.
218                 if (chars == null) {
219                         throw new ArgumentNullException ("chars");
220                 }
221                 if (bytes == null) {
222                         throw new ArgumentNullException ("bytes");
223                 }
224                 if (charIndex < 0 || charIndex > chars.Length) {
225                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
226                 }
227                 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
228                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
229                 }
230                 if (byteIndex < 0 || byteIndex > bytes.Length) {
231                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
232                 }
233
234                 // Convert the characters.
235                 int posn = byteIndex;
236                 int byteLength = bytes.Length;
237                 int leftOverSize = (leftOver >> 8);
238                 int leftOverBits = (leftOver & 0xFF);
239                 byte[] rules = encodingRules;
240                 String base64 = base64Chars;
241                 int ch, rule;
242                 while (charCount > 0) {
243                         ch = (int)(chars[charIndex++]);
244                         --charCount;
245                         if (ch < 0x0080) {
246                                 rule = rules[ch];
247                         } else {
248                                 rule = 0;
249                         }
250                         switch (rule) {
251                         case 0:
252                                 // Handle characters that must be fully encoded.
253                                 if (!isInShifted) {
254                                         if (posn >= byteLength) {
255                                                 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
256                                         }
257                                         // Start the sequence
258                                         bytes[posn++] = (byte)'+';
259                                         isInShifted = true;
260                                         leftOverSize = 0;
261                                 }
262                                 leftOverBits = ((leftOverBits << 16) | ch);
263                                 leftOverSize += 16;
264                                 while (leftOverSize >= 6) {
265                                         if (posn >= byteLength) {
266                                                 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
267                                         }
268                                         leftOverSize -= 6;
269                                         bytes[posn++] = (byte)(base64 [leftOverBits >> leftOverSize]);
270                                         leftOverBits &= ((1 << leftOverSize) - 1);
271                                 }
272                                 break;
273                         case 1:
274                                 // The character is encoded as itself.
275                                 if (isInShifted) {
276                                         if (leftOverSize != 0) {
277                                                 // Flush the previous encoded sequence.
278                                                 if ((posn + 1) > byteLength) {
279                                                         throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
280                                                 }
281                                                 bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
282                                         }
283                                         if ((posn + 1) > byteLength) {
284                                                 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
285                                         }
286                                         // Terminate the sequence
287                                         bytes[posn++] = (byte)'-';
288                                         isInShifted = false;
289                                         leftOverSize = 0;
290                                         leftOverBits = 0;
291                                 }
292                                 if (posn >= byteLength) {
293                                         throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
294                                 }
295                                 bytes[posn++] = (byte)ch;
296                                 break;
297                         case 2:
298                                 // The character may need to be encoded.
299                                 if (allowOptionals) {
300                                         goto case 1;
301                                 } else {
302                                         goto case 0;
303                                 }
304                                 // Not reached.
305                         case 3:
306                                 // Encode the plus sign as "+-".
307                                 if (isInShifted) {
308                                         if (leftOverSize != 0) {
309                                                 // Flush the previous encoded sequence.
310                                                 if ((posn + 1) > byteLength) {
311                                                         throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
312                                                 }
313                                                 bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
314                                         }
315                                         if ((posn + 1) > byteLength) {
316                                                 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
317                                         }
318                                         // Terminate the sequence
319                                         bytes[posn++] = (byte)'-';
320                                         isInShifted = false;
321                                         leftOverSize = 0;
322                                         leftOverBits = 0;
323                                 }
324                                 if ((posn + 2) > byteLength) {
325                                         throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
326                                 }
327                                 bytes[posn++] = (byte)'+';
328                                 bytes[posn++] = (byte)'-';
329                                 break;
330                         }
331                 }
332                 if (isInShifted && flush) {
333                         // Flush the previous encoded sequence.
334                         if (leftOverSize != 0) {
335                                 if ((posn + 1) > byteLength) {
336                                         throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
337                                 }
338                                 bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
339                         }
340                         // Terminate the sequence
341                         bytes[posn++] = (byte)'-';
342                         leftOverSize = 0;
343                         leftOverBits = 0;
344                         isInShifted = false;
345                 }
346                 leftOver = ((leftOverSize << 8) | leftOverBits);
347
348                 // Return the length to the caller.
349                 return posn - byteIndex;
350         }
351
352         // Get the bytes that result from encoding a character buffer.
353         public override int GetBytes (char[] chars, int charIndex, int charCount,
354                                                                  byte[] bytes, int byteIndex)
355         {
356                 int leftOver = 0;
357                 bool isInShifted = false;
358                 return InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, true,
359                                                                 ref leftOver, ref isInShifted, allowOptionals);
360         }
361
362         // Internal version of "GetCharCount" that can handle
363         // a rolling state between call.s
364         private static int InternalGetCharCount
365                                         (byte[] bytes, int index, int count, int leftOver)
366         {
367                 // Validate the parameters.
368                 if (bytes == null) {
369                         throw new ArgumentNullException ("bytes");
370                 }
371                 if (index < 0 || index > bytes.Length) {
372                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
373                 }
374                 if (count < 0 || count > (bytes.Length - index)) {
375                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
376                 }
377
378                 // Determine the length of the result.
379                 int length = 0;
380                 int byteval;
381                 bool normal = ((leftOver & 0x01000000) == 0);
382                 bool prevIsPlus = ((leftOver & 0x02000000) != 0);
383                 int leftOverSize = ((leftOver >> 16) & 0xFF);
384                 sbyte[] base64 = base64Values;
385                 while (count > 0) {
386                         byteval = (int)(bytes[index++]);
387                         --count;
388                         if (normal) {
389                                 if (byteval != '+') {
390                                         // Directly-encoded character.
391                                         ++length;
392                                 } else {
393                                         // Start of a base64-encoded character.
394                                         normal = false;
395                                         prevIsPlus = true;
396                                 }
397                         } else {
398                                 // Process the next byte in a base64 sequence.
399                                 if (byteval == (int)'-') {
400                                         // End of a base64 sequence.
401                                         if (prevIsPlus) {
402                                                 ++length;
403                                                 leftOverSize = 0;
404                                         }
405                                         normal = true;
406                                 } else if (base64 [byteval] != -1) {
407                                         // Extra character in a base64 sequence.
408                                         leftOverSize += 6;
409                                         if (leftOverSize >= 16) {
410                                                 ++length;
411                                                 leftOverSize -= 16;
412                                         }
413                                 } else {
414                                         ++length;
415                                         normal = true;
416                                         leftOverSize = 0;
417                                 }
418                                 prevIsPlus = false;
419                         }
420                 }
421
422                 // Return the final length to the caller.
423                 return length;
424         }
425
426         // Get the number of characters needed to decode a byte buffer.
427         public override int GetCharCount (byte[] bytes, int index, int count)
428         {
429                 return InternalGetCharCount (bytes, index, count, 0);
430         }
431
432         // Internal version of "GetChars" that can handle a
433         // rolling state between calls.
434         private static int InternalGetChars (byte[] bytes, int byteIndex, int byteCount,
435                                  char[] chars, int charIndex, ref int leftOver)
436         {
437                 // Validate the parameters.
438                 if (bytes == null) {
439                         throw new ArgumentNullException ("bytes");
440                 }
441                 if (chars == null) {
442                         throw new ArgumentNullException ("chars");
443                 }
444                 if (byteIndex < 0 || byteIndex > bytes.Length) {
445                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
446                 }
447                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
448                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
449                 }
450                 if (charIndex < 0 || charIndex > chars.Length) {
451                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
452                 }
453
454                 // Convert the bytes into characters.
455                 int posn = charIndex;
456                 int charLength = chars.Length;
457                 int byteval, b64value;
458                 bool normal = ((leftOver & 0x01000000) == 0);
459                 bool prevIsPlus = ((leftOver & 0x02000000) != 0);
460                 bool afterHighSurrogate = ((leftOver & 0x04000000) != 0);
461                 int leftOverSize = ((leftOver >> 16) & 0xFF);
462                 int leftOverBits = (leftOver & 0xFFFF);
463                 sbyte[] base64 = base64Values;
464                 while (byteCount > 0) {
465                         byteval = (int)(bytes[byteIndex++]);
466                         --byteCount;
467                         if (normal) {
468                                 if (byteval != '+') {
469                                         // Directly-encoded character.
470                                         if (posn >= charLength) {
471                                                 throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
472                                         }
473                                         if (afterHighSurrogate) {
474                                                 throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
475                                         }
476                                         chars[posn++] = (char)byteval;
477                                 } else {
478                                         // Start of a base64-encoded character.
479                                         normal = false;
480                                         prevIsPlus = true;
481                                 }
482                         } else {
483                                 // Process the next byte in a base64 sequence.
484                                 if (byteval == (int)'-') {
485                                         // End of a base64 sequence.
486                                         if (prevIsPlus) {
487                                                 if (posn >= charLength) {
488                                                         throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
489                                                 }
490                                                 if (afterHighSurrogate) {
491                                                         throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
492                                                 }
493                                                 chars[posn++] = '+';
494                                         }
495                                         // RFC1642 Rule #2
496                                         // When decoding, any bits at the end of the Modified Base64 sequence that 
497                                         // do not constitute a complete 16-bit Unicode character are discarded. 
498                                         // If such discarded bits are non-zero the sequence is ill-formed.
499                                         normal = true;
500                                         leftOverSize = 0;
501                                         leftOverBits = 0;
502                                 } 
503                                 else if ((b64value = base64[byteval]) != -1) 
504                                 {
505                                         // Extra character in a base64 sequence.
506                                         leftOverBits = (leftOverBits << 6) | b64value;
507                                         leftOverSize += 6;
508                                         if (leftOverSize >= 16) {
509                                                 if (posn >= charLength) {
510                                                         throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
511                                                 }
512                                                 leftOverSize -= 16;
513                                                 char nextChar = (char)(leftOverBits >> leftOverSize);
514                                                 if ((nextChar & 0xFC00) == 0xD800) {
515                                                   afterHighSurrogate = true;
516                                                 }
517                                                 else if ((nextChar & 0xFC00) == 0xDC00) {
518                                                         if (!afterHighSurrogate) {
519                                                                 throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
520                                                         }
521                                                         afterHighSurrogate = false;
522                                                 }
523                                                 chars[posn++] = nextChar;
524                                                 leftOverBits &= ((1 << leftOverSize) - 1);
525                                         }
526                                 } else {
527                                         if (posn >= charLength) {
528                                                 throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
529                                         }
530                                         if (afterHighSurrogate) {
531                                                 throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
532                                         }
533                                         chars[posn++] = (char)byteval;
534                                         normal = true;
535                                         leftOverSize = 0;
536                                         leftOverBits = 0;
537                                 }
538                                 prevIsPlus = false;
539                         }
540                 }
541                 leftOver = (leftOverBits | (leftOverSize << 16) |
542                                     (normal ? 0 : 0x01000000) |
543                                     (prevIsPlus ? 0x02000000 : 0) |
544                                     (afterHighSurrogate ? 0x04000000 : 0));
545
546                 // Return the final length to the caller.
547                 return posn - charIndex;
548         }
549
550         // Get the characters that result from decoding a byte buffer.
551         public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
552                                                                  char[] chars, int charIndex)
553         {
554                 int leftOver = 0;
555                 int amount = InternalGetChars (bytes, byteIndex, byteCount, chars, charIndex, ref leftOver);
556                 if ((leftOver & 0x04000000) != 0) {
557                         throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
558                 }
559                 return amount;
560         }
561
562         // Get the maximum number of bytes needed to encode a
563         // specified number of characters.
564         public override int GetMaxByteCount (int charCount)
565         {
566                 if (charCount < 0) {
567                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
568                 }
569                 if (charCount == 0)
570                         return 0;
571                 return 8 * (int) (charCount / 3) + (charCount % 3) * 3 + 2;
572         }
573
574         // Get the maximum number of characters needed to decode a
575         // specified number of bytes.
576         public override int GetMaxCharCount (int byteCount)
577         {
578                 if (byteCount < 0) {
579                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
580                 }
581                 return byteCount;
582         }
583
584         // Get a UTF7-specific decoder that is attached to this instance.
585         public override Decoder GetDecoder ()
586         {
587                 return new UTF7Decoder ();
588         }
589
590         // Get a UTF7-specific encoder that is attached to this instance.
591         public override Encoder GetEncoder ()
592         {
593                 return new UTF7Encoder (allowOptionals);
594         }
595
596         // UTF-7 decoder implementation.
597         private sealed class UTF7Decoder : Decoder
598         {
599                 // Internal state.
600                 private int leftOver;
601
602                 // Constructor.
603                 public UTF7Decoder ()
604                 {
605                         leftOver = 0;
606                 }
607
608                 // Override inherited methods.
609                 public override int GetCharCount (byte[] bytes, int index, int count)
610                 {
611                         return InternalGetCharCount (bytes, index, count, leftOver);
612                 }
613                 public override int GetChars (byte[] bytes, int byteIndex,
614                                                                          int byteCount, char[] chars,
615                                                                          int charIndex)
616                 {
617                         return InternalGetChars (bytes, byteIndex, byteCount, chars, charIndex, ref leftOver);
618                 }
619
620         } // class UTF7Decoder
621
622         // UTF-7 encoder implementation.
623         private sealed class UTF7Encoder : Encoder
624         {
625                 private bool allowOptionals;
626                 private int leftOver = 0;
627                 private bool isInShifted = false;
628
629                 // Constructor.
630                 public UTF7Encoder (bool allowOptionals)
631                 {
632                         this.allowOptionals = allowOptionals;
633                 }
634
635                 // Override inherited methods.
636                 public override int GetByteCount (char[] chars, int index,
637                                                                                  int count, bool flush)
638                 {
639                         return InternalGetByteCount
640                                 (chars, index, count, flush, leftOver, isInShifted, allowOptionals);
641                 }
642                 public override int GetBytes (char[] chars, int charIndex,
643                                                                          int charCount, byte[] bytes,
644                                                                          int byteIndex, bool flush)
645                 {
646                         return InternalGetBytes (chars, charIndex, charCount,
647                                                                         bytes, byteIndex, flush,
648                                                                         ref leftOver, ref isInShifted, allowOptionals);
649                 }
650
651         } // class UTF7Encoder
652
653 }; // class UTF7Encoding
654
655 }; // namespace System.Text