2 * UTF7Encoding.cs - Implementation of the
3 * "System.Text.UTF7Encoding" class.
5 * Copyright (c) 2002 Southern Storm Software, Pty Ltd
6 * Copyright (c) 2003, 2004, Novell, Inc.
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
31 using System.Runtime.InteropServices;
37 [MonoTODO ("Fix serialization compatibility with MS.NET")]
43 class UTF7Encoding : Encoding
45 // Magic number used by Windows for UTF-7.
46 internal const int UTF7_CODE_PAGE = 65000;
49 private bool allowOptionals;
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
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
65 // Characters to use to encode 6-bit values in base64.
66 private const String base64Chars =
67 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
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
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
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
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
93 public UTF7Encoding ()
98 public UTF7Encoding (bool allowOptionals)
99 : base (UTF7_CODE_PAGE)
101 this.allowOptionals = allowOptionals;
104 encoding_name = "Unicode (UTF-7)";
105 header_name = "utf-7";
106 is_mail_news_display = true;
107 is_mail_news_save = true;
109 windows_code_page = UnicodeEncoding.UNICODE_CODE_PAGE;
113 public override int GetHashCode ()
115 int basis = base.GetHashCode ();
116 return allowOptionals ? -basis : basis;
119 public override bool Equals (object other)
121 UTF7Encoding e = other as UTF7Encoding;
124 return allowOptionals == e.allowOptionals &&
125 EncoderFallback.Equals (e.EncoderFallback) &&
126 DecoderFallback.Equals (e.DecoderFallback);
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)
136 // Validate the parameters.
138 throw new ArgumentNullException ("chars");
140 if (index < 0 || index > chars.Length) {
141 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
143 if (count < 0 || count > (chars.Length - index)) {
144 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
147 // Determine the length of the output.
149 int leftOverSize = (leftOver >> 8);
150 byte[] rules = encodingRules;
153 ch = (int)(chars[index++]);
162 // Handle characters that must be fully encoded.
163 if ( !isInShifted ) {
169 while (leftOverSize >= 6) {
175 // The character is encoded as itself.
177 if (leftOverSize != 0) {
178 // Flush the previous encoded sequence.
182 // Count the "-" (sequence terminator)
189 // The character may need to be encoded.
190 if (allowOptionals) {
197 // Encode the plus sign as "+-".
199 if (leftOverSize != 0) {
200 // Flush the previous encoded sequence.
204 // Count the "-" (sequence terminator)
212 if (isInShifted && flush) {
213 if (leftOverSize != 0)
215 // Flush the previous encoded sequence.
218 // Count the "-" (sequence terminator)
222 // Return the length to the caller.
226 // Get the number of bytes needed to encode a character buffer.
227 public override int GetByteCount (char[] chars, int index, int count)
229 return InternalGetByteCount (chars, index, count, true, 0, false, allowOptionals);
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)
239 // Validate the parameters.
241 throw new ArgumentNullException ("chars");
244 throw new ArgumentNullException ("bytes");
246 if (charIndex < 0 || charIndex > chars.Length) {
247 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
249 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
250 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
252 if (byteIndex < 0 || byteIndex > bytes.Length) {
253 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
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;
264 while (charCount > 0) {
265 ch = (int)(chars[charIndex++]);
274 // Handle characters that must be fully encoded.
276 if (posn >= byteLength) {
277 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
279 // Start the sequence
280 bytes[posn++] = (byte)'+';
284 leftOverBits = ((leftOverBits << 16) | ch);
286 while (leftOverSize >= 6) {
287 if (posn >= byteLength) {
288 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
291 bytes[posn++] = (byte)(base64 [leftOverBits >> leftOverSize]);
292 leftOverBits &= ((1 << leftOverSize) - 1);
296 // The character is encoded as itself.
298 if (leftOverSize != 0) {
299 // Flush the previous encoded sequence.
300 if ((posn + 1) > byteLength) {
301 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
303 bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
305 if ((posn + 1) > byteLength) {
306 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
308 // Terminate the sequence
309 bytes[posn++] = (byte)'-';
314 if (posn >= byteLength) {
315 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
317 bytes[posn++] = (byte)ch;
320 // The character may need to be encoded.
321 if (allowOptionals) {
328 // Encode the plus sign as "+-".
330 if (leftOverSize != 0) {
331 // Flush the previous encoded sequence.
332 if ((posn + 1) > byteLength) {
333 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
335 bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
337 if ((posn + 1) > byteLength) {
338 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
340 // Terminate the sequence
341 bytes[posn++] = (byte)'-';
346 if ((posn + 2) > byteLength) {
347 throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
349 bytes[posn++] = (byte)'+';
350 bytes[posn++] = (byte)'-';
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");
360 bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
362 // Terminate the sequence
363 bytes[posn++] = (byte)'-';
368 leftOver = ((leftOverSize << 8) | leftOverBits);
370 // Return the length to the caller.
371 return posn - byteIndex;
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)
379 bool isInShifted = false;
380 return InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, true,
381 ref leftOver, ref isInShifted, allowOptionals);
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)
389 // Validate the parameters.
391 throw new ArgumentNullException ("bytes");
393 if (index < 0 || index > bytes.Length) {
394 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
396 if (count < 0 || count > (bytes.Length - index)) {
397 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
400 // Determine the length of the result.
403 bool normal = ((leftOver & 0x01000000) == 0);
404 bool prevIsPlus = ((leftOver & 0x02000000) != 0);
405 int leftOverSize = ((leftOver >> 16) & 0xFF);
406 sbyte[] base64 = base64Values;
408 byteval = (int)(bytes[index++]);
411 if (byteval != '+') {
412 // Directly-encoded character.
415 // Start of a base64-encoded character.
420 // Process the next byte in a base64 sequence.
421 if (byteval == (int)'-') {
422 // End of a base64 sequence.
428 } else if (base64 [byteval] != -1) {
429 // Extra character in a base64 sequence.
431 if (leftOverSize >= 16) {
444 // Return the final length to the caller.
448 // Get the number of characters needed to decode a byte buffer.
449 public override int GetCharCount (byte[] bytes, int index, int count)
451 return InternalGetCharCount (bytes, index, count, 0);
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)
459 // Validate the parameters.
461 throw new ArgumentNullException ("bytes");
464 throw new ArgumentNullException ("chars");
466 if (byteIndex < 0 || byteIndex > bytes.Length) {
467 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
469 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
470 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
472 if (charIndex < 0 || charIndex > chars.Length) {
473 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
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++]);
490 if (byteval != '+') {
491 // Directly-encoded character.
492 if (posn >= charLength) {
493 throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
495 if (afterHighSurrogate) {
496 throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
498 chars[posn++] = (char)byteval;
500 // Start of a base64-encoded character.
505 // Process the next byte in a base64 sequence.
506 if (byteval == (int)'-') {
507 // End of a base64 sequence.
509 if (posn >= charLength) {
510 throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
512 if (afterHighSurrogate) {
513 throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
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.
525 else if ((b64value = base64[byteval]) != -1)
527 // Extra character in a base64 sequence.
528 leftOverBits = (leftOverBits << 6) | b64value;
530 if (leftOverSize >= 16) {
531 if (posn >= charLength) {
532 throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
535 char nextChar = (char)(leftOverBits >> leftOverSize);
536 if ((nextChar & 0xFC00) == 0xD800) {
537 afterHighSurrogate = true;
539 else if ((nextChar & 0xFC00) == 0xDC00) {
540 if (!afterHighSurrogate) {
541 throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
543 afterHighSurrogate = false;
545 chars[posn++] = nextChar;
546 leftOverBits &= ((1 << leftOverSize) - 1);
549 if (posn >= charLength) {
550 throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
552 if (afterHighSurrogate) {
553 throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
555 chars[posn++] = (char)byteval;
563 leftOver = (leftOverBits | (leftOverSize << 16) |
564 (normal ? 0 : 0x01000000) |
565 (prevIsPlus ? 0x02000000 : 0) |
566 (afterHighSurrogate ? 0x04000000 : 0));
568 // Return the final length to the caller.
569 return posn - charIndex;
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)
577 int amount = InternalGetChars (bytes, byteIndex, byteCount, chars, charIndex, ref leftOver);
578 if ((leftOver & 0x04000000) != 0) {
579 throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
584 // Get the maximum number of bytes needed to encode a
585 // specified number of characters.
586 public override int GetMaxByteCount (int charCount)
589 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
593 return 8 * (int) (charCount / 3) + (charCount % 3) * 3 + 2;
596 // Get the maximum number of characters needed to decode a
597 // specified number of bytes.
598 public override int GetMaxCharCount (int byteCount)
601 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
606 // Get a UTF7-specific decoder that is attached to this instance.
607 public override Decoder GetDecoder ()
609 return new UTF7Decoder ();
612 // Get a UTF7-specific encoder that is attached to this instance.
613 public override Encoder GetEncoder ()
615 return new UTF7Encoder (allowOptionals);
618 // UTF-7 decoder implementation.
619 private sealed class UTF7Decoder : Decoder
622 private int leftOver;
625 public UTF7Decoder ()
630 // Override inherited methods.
631 public override int GetCharCount (byte[] bytes, int index, int count)
633 return InternalGetCharCount (bytes, index, count, leftOver);
635 public override int GetChars (byte[] bytes, int byteIndex,
636 int byteCount, char[] chars,
639 return InternalGetChars (bytes, byteIndex, byteCount, chars, charIndex, ref leftOver);
642 } // class UTF7Decoder
644 // UTF-7 encoder implementation.
645 private sealed class UTF7Encoder : Encoder
647 private bool allowOptionals;
648 private int leftOver = 0;
649 private bool isInShifted = false;
652 public UTF7Encoder (bool allowOptionals)
654 this.allowOptionals = allowOptionals;
657 // Override inherited methods.
658 public override int GetByteCount (char[] chars, int index,
659 int count, bool flush)
661 return InternalGetByteCount
662 (chars, index, count, flush, leftOver, isInShifted, allowOptionals);
664 public override int GetBytes (char[] chars, int charIndex,
665 int charCount, byte[] bytes,
666 int byteIndex, bool flush)
668 return InternalGetBytes (chars, charIndex, charCount,
669 bytes, byteIndex, flush,
670 ref leftOver, ref isInShifted, allowOptionals);
673 } // class UTF7Encoder
676 // a bunch of practically missing implementations (but should just work)
678 [CLSCompliantAttribute (false)]
679 public override unsafe int GetByteCount (char *chars, int count)
681 return base.GetByteCount (chars, count);
684 public override int GetByteCount (string s)
686 return base.GetByteCount (s);
689 [CLSCompliantAttribute (false)]
690 public override unsafe int GetBytes (char *chars, int charCount, byte* bytes, int byteCount)
692 return base.GetBytes (chars, charCount, bytes, byteCount);
695 public override int GetBytes (string s, int charIndex, int charCount, byte [] bytes, int byteIndex)
697 return base.GetBytes (s, charIndex, charCount, bytes, byteIndex);
700 [CLSCompliantAttribute (false)]
701 public override unsafe int GetCharCount (byte *bytes, int count)
703 return base.GetCharCount (bytes, count);
706 [CLSCompliantAttribute (false)]
707 public override unsafe int GetChars (byte* bytes, int byteCount, char* chars, int charCount)
709 return base.GetChars (bytes, byteCount, chars, charCount);
712 public override string GetString (byte [] bytes, int index, int count)
714 return base.GetString (bytes, index, count);
719 }; // class UTF7Encoding
721 }; // namespace System.Text