.NET 的 IMAP 文件夹路径编码 (IMAP UTF-7)?

spl*_*tne 5 .net c# encoding imap utf-7

IMAP 规范(RFC 2060,5.1.3。邮箱国际命名约定)描述了如何处理文件夹名称中的非 ASCII 字符。它定义了经过修改的UTF-7 编码:

按照惯例,国际邮箱名称使用 [UTF-7] 中描述的 UTF-7 编码的修改版本指定。这些修改的目的是纠正 UTF-7 的以下问题:

  1. UTF-7 使用“+”字符进行移位;这与邮箱名称中常用的“+”冲突,尤其是 USENET 新闻组名称。

  2. UTF-7 的编码是 BASE64,它使用“/”字符;这与使用“/”作为流行的层次结构分隔符相冲突。

  3. UTF-7 禁止“\”的未编码使用;这与使用“\”作为流行的层次结构分隔符相冲突。

  4. UTF-7 禁止“~”的未编码使用;这与在某些服务器中使用“~”作为主目录指示符相冲突。

  5. UTF-7 允许多种替代形式来表示同一个字符串;特别是,可打印的 US-ASCII 字符可以以编码形式表示。

在修改后的 UTF-7 中,除“&”之外的可打印 US-ASCII 字符代表它们自己;也就是说,八位字节值为 0x20-0x25 和 0x27-0x7e 的字符。字符“&”(0x26)由两个八位字节序列“&-”表示。

所有其他字符(八位字节值 0x00-0x1f、0x7f-0xff 和所有 Unicode 16 位八位字节)都以修改后的 BASE64 表示,[UTF-7] 进一步修改为使用“,”而不是“/”。
修改后的 BASE64 不得用于表示任何可以表示其自身的打印 US-ASCII 字符。

"&" 用于转换到修改后的 BASE64 和 "-" 用于转换回 US-ASCII。所有名称都以 US-ASCII 开头,并且必须以 US-ASCII 结尾(即以 Unicode 16 位八位字节结尾的名称必须以“-”结尾)。

在我开始实现它之前,我的问题是:是否有一些.NET 代码/库(甚至在框架中)可以完成这项工作?我找不到 .NET 资源(仅适用于其他语言/框架的实现)。

谢谢!

jst*_*ast 3

//
// ImapEncoding.cs
//
// Author: Jeffrey Stedfast <jestedfa@microsoft.com>
//
// Copyright (c) 2013-2019 Microsoft Corp. (www.microsoft.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

using System.Text;

namespace MailKit.Net.Imap {
    static class ImapEncoding
    {
        const string utf7_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";

        static readonly byte[] utf7_rank = {
            255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
            255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
            255,255,255,255,255,255,255,255,255,255,255, 62, 63,255,255,255,
             52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255,255,255,255,
            255,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
             15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
            255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
             41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
        };

        public static string Decode (string text)
        {
            var decoded = new StringBuilder ();
            bool shifted = false;
            int bits = 0, v = 0;
            int index = 0;
            char c;

            while (index < text.Length) {
                c = text[index++];

                if (shifted) {
                    if (c == '-') {
                        // shifted back out of modified UTF-7
                        shifted = false;
                        bits = v = 0;
                    } else if (c > 127) {
                        // invalid UTF-7
                        return text;
                    } else {
                        byte rank = utf7_rank[(byte) c];

                        if (rank == 0xff) {
                            // invalid UTF-7
                            return text;
                        }

                        v = (v << 6) | rank;
                        bits += 6;

                        if (bits >= 16) {
                            char u = (char) ((v >> (bits - 16)) & 0xffff);
                            decoded.Append (u);
                            bits -= 16;
                        }
                    }
                } else if (c == '&' && index < text.Length) {
                    if (text[index] == '-') {
                        decoded.Append ('&');
                        index++;
                    } else {
                        // shifted into modified UTF-7
                        shifted = true;
                    }
                } else {
                    decoded.Append (c);
                }
            }

            return decoded.ToString ();
        }

        static void Utf7ShiftOut (StringBuilder output, int u, int bits)
        {
            if (bits > 0) {
                int x = (u << (6 - bits)) & 0x3f;
                output.Append (utf7_alphabet[x]);
            }

            output.Append ('-');
        }

        public static string Encode (string text)
        {
            var encoded = new StringBuilder ();
            bool shifted = false;
            int bits = 0, u = 0;

            for (int index = 0; index < text.Length; index++) {
                char c = text[index];

                if (c >= 0x20 && c < 0x7f) {
                    // characters with octet values 0x20-0x25 and 0x27-0x7e
                    // represent themselves while 0x26 ("&") is represented
                    // by the two-octet sequence "&-"

                    if (shifted) {
                        Utf7ShiftOut (encoded, u, bits);
                        shifted = false;
                        bits = 0;
                    }

                    if (c == 0x26)
                        encoded.Append ("&-");
                    else
                        encoded.Append (c);
                } else {
                    // base64 encode
                    if (!shifted) {
                        encoded.Append ('&');
                        shifted = true;
                    }

                    u = (u << 16) | (c & 0xffff);
                    bits += 16;

                    while (bits >= 6) {
                        int x = (u >> (bits - 6)) & 0x3f;
                        encoded.Append (utf7_alphabet[x]);
                        bits -= 6;
                    }
                }
            }

            if (shifted)
                Utf7ShiftOut (encoded, u, bits);

            return encoded.ToString ();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)