utf7encoding.cs
来自「没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没」· CS 代码 · 共 799 行 · 第 1/2 页
CS
799 行
("index", _("ArgRange_Array")); } if(count < 0 || count > (bytes.Length - index)) { throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array")); } // Determine the length of the result. int length = 0; int byteval, b64value; bool normal = ((leftOver & 0x01000000) == 0); bool prevIsPlus = ((leftOver & 0x02000000) != 0); int leftOverSize = ((leftOver >> 16) & 0xFF); sbyte[] base64 = base64Values; while(count > 0) { byteval = (int)(bytes[index++]); --count; if(normal) { if(byteval != '+') { // Directly-encoded character. ++length; } else { // Start of a base64-encoded character. normal = false; prevIsPlus = true; } } else { // Process the next byte in a base64 sequence. if(byteval == (int)'-') { // End of a base64 sequence. if(prevIsPlus || leftOverSize > 0) { ++length; leftOverSize = 0; } normal = true; } else if((b64value = base64[byteval]) != -1) { // Extra character in a base64 sequence. leftOverSize += 6; if(leftOverSize >= 16) { ++length; leftOverSize -= 16; } } else { // Normal character terminating a base64 sequence. if(leftOverSize > 0) { ++length; leftOverSize = 0; } ++length; normal = true; } prevIsPlus = false; } } // Return the final length to the caller. return length; } // Get the number of characters needed to decode a byte buffer. public override int GetCharCount(byte[] bytes, int index, int count) { return InternalGetCharCount(bytes, index, count, 0); } // Internal version of "GetChars" that can handle a // rolling state between calls. private static int InternalGetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, ref int leftOver) { // Validate the parameters. if(bytes == null) { throw new ArgumentNullException("bytes"); } if(chars == null) { throw new ArgumentNullException("chars"); } if(byteIndex < 0 || byteIndex > bytes.Length) { throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array")); } if(byteCount < 0 || byteCount > (bytes.Length - byteIndex)) { throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array")); } if(charIndex < 0 || charIndex > chars.Length) { throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array")); } // Convert the bytes into characters. int posn = charIndex; int charLength = chars.Length; int byteval, b64value; bool normal = ((leftOver & 0x01000000) == 0); bool prevIsPlus = ((leftOver & 0x02000000) != 0); int leftOverSize = ((leftOver >> 16) & 0xFF); int leftOverBits = (leftOver & 0xFFFF); sbyte[] base64 = base64Values; while(byteCount > 0) { byteval = (int)(bytes[byteIndex++]); --byteCount; if(normal) { if(byteval != '+') { // Directly-encoded character. if(posn >= charLength) { throw new ArgumentException (_("Arg_InsufficientSpace"), "chars"); } chars[posn++] = (char)byteval; } else { // Start of a base64-encoded character. normal = false; prevIsPlus = true; } } else { // Process the next byte in a base64 sequence. if(byteval == (int)'-') { // End of a base64 sequence. if(prevIsPlus) { if(posn >= charLength) { throw new ArgumentException (_("Arg_InsufficientSpace"), "chars"); } chars[posn++] = '+'; } else if(leftOverSize > 0) { if(posn >= charLength) { throw new ArgumentException (_("Arg_InsufficientSpace"), "chars"); } chars[posn++] = (char)(leftOverBits << (16 - leftOverSize)); leftOverSize = 0; leftOverBits = 0; } normal = true; } else if((b64value = base64[byteval]) != -1) { // Extra character in a base64 sequence. leftOverBits = (leftOverBits << 6) | b64value; leftOverSize += 6; if(leftOverSize >= 16) { if(posn >= charLength) { throw new ArgumentException (_("Arg_InsufficientSpace"), "chars"); } leftOverSize -= 16; chars[posn++] = (char)(leftOverBits >> leftOverSize); leftOverBits &= ((1 << leftOverSize) - 1); } } else { // Normal character terminating a base64 sequence. if(leftOverSize > 0) { if(posn >= charLength) { throw new ArgumentException (_("Arg_InsufficientSpace"), "chars"); } chars[posn++] = (char)(leftOverBits << (16 - leftOverSize)); leftOverSize = 0; leftOverBits = 0; } if(posn >= charLength) { throw new ArgumentException (_("Arg_InsufficientSpace"), "chars"); } chars[posn++] = (char)byteval; normal = true; } prevIsPlus = false; } } leftOver = (leftOverBits | (leftOverSize << 16) | (normal ? 0 : 0x01000000) | (prevIsPlus ? 0x02000000 : 0)); // Return the final length to the caller. return posn - charIndex; } // Get the characters that result from decoding a byte buffer. public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { int leftOver = 0; return InternalGetChars(bytes, byteIndex, byteCount, chars, charIndex, ref leftOver); } // Get the maximum number of bytes needed to encode a // specified number of characters. public override int GetMaxByteCount(int charCount) { if(charCount < 0) { throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative")); } return charCount * 5; } // Get the maximum number of characters needed to decode a // specified number of bytes. public override int GetMaxCharCount(int byteCount) { if(byteCount < 0) { throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative")); } return byteCount; } // Get a UTF7-specific decoder that is attached to this instance. public override Decoder GetDecoder() { return new UTF7Decoder(); } // Get a UTF7-specific encoder that is attached to this instance. public override Encoder GetEncoder() { return new UTF7Encoder(allowOptionals); }#if !ECMA_COMPAT // Get the mail body name for this encoding. internal override String InternalBodyName { get { return "utf-7"; } } // Get the human-readable name for this encoding. internal override String InternalEncodingName { get { return "Unicode (UTF-7)"; } } // Get the mail agent header name for this encoding. internal override String InternalHeaderName { get { return "utf-7"; } } // Determine if this encoding can be displayed in a mail/news agent. internal override bool InternalIsMailNewsDisplay { get { return true; } } // Determine if this encoding can be saved from a mail/news agent. internal override bool InternalIsMailNewsSave { get { return true; } } // Get the IANA-preferred Web name for this encoding. internal override String InternalWebName { get { return "utf-7"; } } // Get the Windows code page represented by this object. internal override int InternalWindowsCodePage { get { return UnicodeEncoding.UNICODE_CODE_PAGE; } }#endif // !ECMA_COMPAT // UTF-7 decoder implementation. private sealed class UTF7Decoder : Decoder { // Internal state. private int leftOver; // Constructor. public UTF7Decoder() { leftOver = 0; } // Override inherited methods. public override int GetCharCount(byte[] bytes, int index, int count) { return InternalGetCharCount(bytes, index, count, leftOver); } public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { return InternalGetChars(bytes, byteIndex, byteCount, chars, charIndex, ref leftOver); } } // class UTF7Decoder // UTF-7 encoder implementation. private sealed class UTF7Encoder : Encoder { private bool allowOptionals; private int leftOver; // Constructor. public UTF7Encoder(bool allowOptionals) { this.allowOptionals = allowOptionals; this.leftOver = 0; } // Override inherited methods. public override int GetByteCount(char[] chars, int index, int count, bool flush) { return InternalGetByteCount (chars, index, count, flush, leftOver, allowOptionals); } public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex, bool flush) { return InternalGetBytes(chars, charIndex, charCount, bytes, byteIndex, flush, ref leftOver, allowOptionals); } } // class UTF7Encoder}; // class UTF7Encoding}; // namespace System.Text
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?