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