Ejemplo n.º 1
0
        private Byte[] DecodeFromUnNormalizedInput(Byte[] input)
        {
            BufferByteArray decoded      = new BufferByteArray((input.Length / 4 * 3));
            var             buffer       = this.GetBuffer();
            Int32           bufferIndex  = 0;
            UInt32          bbbb         = 0;
            Int32           bIndex       = 0;
            Byte            c            = 0;
            Int32           paddingCount = 0;
            var             decodeTable  = this.Base64CharTable.DecodeTable; //For performance improvement
            Int32           length       = input.Length;                     //For performance improvement

            for (int i = 0; i < length; i++)
            {
                c = decodeTable[input[i]];
                if (c == 255)
                {
                    continue;
                }

                if (bufferIndex + 3 >= buffer.Length)
                {
                    decoded.Add(buffer, bufferIndex);
                    bufferIndex = 0;
                }

                bbbb    = bbbb << 6 | c;
                bIndex += 1;
                if (input[i] == 61)// =
                {
                    paddingCount++;
                }
                if (bIndex == 4)
                {
                    buffer[bufferIndex++] = (byte)((bbbb >> 16) & 0xFF);
                    buffer[bufferIndex++] = (byte)((bbbb >> 8) & 0xFF);
                    buffer[bufferIndex++] = (byte)(bbbb & 0xFF);
                    bbbb   = 0;
                    bIndex = 0;
                }
            }
            if (bufferIndex > 0)
            {
                decoded.Add(buffer, bufferIndex - paddingCount);
            }
            return(decoded.ToArray());
        }
Ejemplo n.º 2
0
        private Byte[] EncodeWithInsertingNewline(Byte[] input)
        {
            BufferByteArray encoded = new BufferByteArray((input.Length / 3) * 4);
            var             buffer = this.GetBuffer();
            var             bufferIndex = 0;
            Int32           loopCount = input.Length / 3;
            Byte            c0, c1, c2;
            Int32           startIndex       = 0;
            Int32           charCountPerLine = 0;
            Int32           paddingCount     = 0;
            var             encodeTable      = this.Base64CharTable.EncodeTable;

            for (int i = 0; i < loopCount; i++)
            {
                startIndex = i * 3;
                c0         = input[startIndex];
                c1         = input[startIndex + 1];
                c2         = input[startIndex + 2];

                if (bufferIndex + 6 >= buffer.Length)
                {
                    encoded.Add(buffer, bufferIndex);
                    bufferIndex = 0;
                }
                buffer[bufferIndex++] = encodeTable[c0 >> 2];
                buffer[bufferIndex++] = encodeTable[(c1 >> 4) | ((c0 & 0x3) << 4)];
                buffer[bufferIndex++] = encodeTable[((c1 & 0x0f) << 2) | (c2 >> 6)];
                buffer[bufferIndex++] = encodeTable[c2 & 63];

                charCountPerLine += 4;

                if (charCountPerLine >= this.CharCountPerLine)
                {
                    buffer[bufferIndex++] = 13; // \r
                    buffer[bufferIndex++] = 10; // \n
                    charCountPerLine      = 0;
                }
            }
            if (loopCount > 0)
            {
                startIndex += 3;
            }

            if (bufferIndex + 4 > buffer.Length)
            {
                encoded.Add(buffer, bufferIndex);
            }
            if (input.Length - startIndex == 2)
            {
                c0 = input[startIndex];
                c1 = input[startIndex + 1];
                c2 = 0;
                buffer[bufferIndex++] = encodeTable[c0 >> 2];
                buffer[bufferIndex++] = encodeTable[(c1 >> 4) | ((c0 & 0x3) << 4)];
                buffer[bufferIndex++] = encodeTable[((c1 & 0x0f) << 2) | (c2 >> 6)];
                buffer[bufferIndex++] = 61;
                paddingCount          = 1;
            }
            else if (input.Length - startIndex == 1)
            {
                c0 = input[startIndex];
                c1 = 0;
                c2 = 0;
                buffer[bufferIndex++] = encodeTable[c0 >> 2];
                buffer[bufferIndex++] = encodeTable[(c1 >> 4) | ((c0 & 0x3) << 4)];
                buffer[bufferIndex++] = 61;
                buffer[bufferIndex++] = 61;
                paddingCount          = 2;
            }
            if (bufferIndex > 0)
            {
                if (this.Padding == true)
                {
                    encoded.Add(buffer, bufferIndex);
                }
                else
                {
                    encoded.Add(buffer, bufferIndex - paddingCount);
                }
            }
            return(encoded.ToArray());
        }
Ejemplo n.º 3
0
        public override Byte[] Decode(Byte[] input)
        {
            BufferByteArray decoded = new BufferByteArray(input.Length);
            var buffer = this.GetBuffer();
            Int32 bufferIndex = 0;
            var bbb = new byte[3];
            var bbbIndex = 0;
            Byte b = 0;
            Byte c = 0;

            var length = input.Length;
            for (int i = 0; i < length; i++)
            {
                c = input[i];
                if (bufferIndex + 3 > buffer.Length)
                {
                    decoded.Add(buffer, bufferIndex);
                    bufferIndex = 0;
                }

                if (bbbIndex == 0)
                {
                    if (c == 61) // =
                    {
                        bbb[bbbIndex++] = c;
                    }
                    else if (this.Mode == QuotedPrintableConvertMode.Header && c == 0x5f) // _
                    {
                        buffer[bufferIndex++] = 32; // white space
                    }
                    else
                    {
                        // NonQuotedChar
                        buffer[bufferIndex++] = c;
                    }
                }
                else
                {
                    // QuotedChar
                    bbb[bbbIndex++] = c;
                }

                if (bbbIndex == 3)
                {
                    if (bbb[1] == 13 && bbb[2] == 10)
                    {
                        // New line
                        bbbIndex = 0;
                    }
                    else if (bbb[1] == 13 || bbb[1] == 10)
                    {
                        if (bbb[2] == 61)
                        {
                            bbbIndex = 1;
                        }
                        else
                        {
                            buffer[bufferIndex++] = bbb[2];
                            bbbIndex = 0;
                        }
                    }
                    else
                    {
                        byte d = 0;

                        for (var bIndex = 1; bIndex < 3; bIndex++)
                        {
                            d <<= 4;
                            b = bbb[bIndex];

                            if (0x30 <= b && b <= 0x39)
                            {
                                d |= (byte)(b - 0x30);// 0-9
                            }
                            else if (0x41 <= b && b <= 0x46)
                            {
                                d |= (byte)(b - 0x37);// A-F
                            }
                            else if (0x61 <= b && b <= 0x66)
                            {
                                d |= (byte)(b - 0x57);// a-f
                            }
                            else
                            {
                                throw new FormatException();
                            }
                        }
                        buffer[bufferIndex++] = d;
                        bbbIndex = 0;
                    }
                }
            }
            if (bufferIndex > 0)
            {
                decoded.Add(buffer, bufferIndex);
            }
            return decoded.ToArray();
        }
Ejemplo n.º 4
0
        public override Byte[] Encode(Byte[] input)
        {
            BufferByteArray decoded = new BufferByteArray(input.Length * 3);
            var buffer = this.GetBuffer();
            Int32 bufferIndex = 0;
            Byte c = 0;
            Int32 charCount = 0;

            Int32 length = input.Length;
            for (int i = 0; i < length; i++)
            {
                c = input[i];
                if (bufferIndex + 6 > buffer.Length)
                {
                    decoded.Add(buffer, bufferIndex);
                    bufferIndex = 0;
                }
                if (InsertNewline == true && this.CharCountPerLine - 3 < charCount)//Insert new line
                {
                    var escaped = false;

                    if (c == 0x09 || c == 0x20)
                    {
                        // \t or white space
                        buffer[bufferIndex++] = 61; // '='
                        buffer[bufferIndex++] = HexAlphabet[(c & 0xf0) >> 4];
                        buffer[bufferIndex++] = HexAlphabet[c & 0x0f];

                        escaped = true;
                    }

                    buffer[bufferIndex++] = 61; // '='
                    buffer[bufferIndex++] = 13; // \r
                    buffer[bufferIndex++] = 10; // \n

                    charCount = 0;

                    if (escaped) { continue; }
                }

                var quote = false;

                switch (c)
                {
                    case 9: // \t
                    case 32: // ' ' WhiteSpace
                    case 63: // ?
                    case 95: // _
                        quote = this.InsertNewline == false;
                        break;
                    case 0x3d: // '='
                        quote = true;
                        break;

                    default:
                        // NonPrintableChar
                        quote = (c < 33 || 127 < c);
                        break;
                }

                if (quote == true)
                {
                    // = or NonPrintableChar
                    buffer[bufferIndex++] = 61; // '='
                    buffer[bufferIndex++] = HexAlphabet[(c & 0xf0) >> 4];
                    buffer[bufferIndex++] = HexAlphabet[c & 0x0f];

                    charCount += 3;
                }
                else
                {
                    // = or PrintableChar
                    buffer[bufferIndex++] = c;

                    charCount++;
                }
            }
            if (bufferIndex > 0)
            {
                decoded.Add(buffer, bufferIndex);
            }
            return decoded.ToArray();
        }
Ejemplo n.º 5
0
        private Byte[] EncodeWithInsertingNewline(Byte[] input)
        {
            BufferByteArray encoded = new BufferByteArray((input.Length / 3) * 4);
            var buffer = this.GetBuffer();
            var bufferIndex = 0;
            Int32 loopCount = input.Length / 3;
            Byte c0, c1, c2;
            Int32 startIndex = 0;
            Int32 charCountPerLine = 0;
            Int32 paddingCount = 0;
            var encodeTable = this.Base64CharTable.EncodeTable;

            for (int i = 0; i < loopCount; i++)
            {
                startIndex = i * 3;
                c0 = input[startIndex];
                c1 = input[startIndex + 1];
                c2 = input[startIndex + 2];

                if (bufferIndex + 6 >= buffer.Length)
                {
                    encoded.Add(buffer, bufferIndex);
                    bufferIndex = 0;
                }
                buffer[bufferIndex++] = encodeTable[c0 >> 2];
                buffer[bufferIndex++] = encodeTable[(c1 >> 4) | ((c0 & 0x3) << 4)];
                buffer[bufferIndex++] = encodeTable[((c1 & 0x0f) << 2) | (c2 >> 6)];
                buffer[bufferIndex++] = encodeTable[c2 & 63];

                charCountPerLine += 4;

                if (charCountPerLine >= this.CharCountPerLine)
                {
                    buffer[bufferIndex++] = 13;// \r
                    buffer[bufferIndex++] = 10;// \n
                    charCountPerLine = 0;
                }
            }
            if (loopCount > 0)
            {
                startIndex += 3;
            }

            if (bufferIndex + 4 > buffer.Length)
            {
                encoded.Add(buffer, bufferIndex);
            }
            if (input.Length - startIndex == 2)
            {
                c0 = input[startIndex];
                c1 = input[startIndex + 1];
                c2 = 0;
                buffer[bufferIndex++] = encodeTable[c0 >> 2];
                buffer[bufferIndex++] = encodeTable[(c1 >> 4) | ((c0 & 0x3) << 4)];
                buffer[bufferIndex++] = encodeTable[((c1 & 0x0f) << 2) | (c2 >> 6)];
                buffer[bufferIndex++] = 61;
                paddingCount = 1;
            }
            else if (input.Length - startIndex == 1)
            {
                c0 = input[startIndex];
                c1 = 0;
                c2 = 0;
                buffer[bufferIndex++] = encodeTable[c0 >> 2];
                buffer[bufferIndex++] = encodeTable[(c1 >> 4) | ((c0 & 0x3) << 4)];
                buffer[bufferIndex++] = 61;
                buffer[bufferIndex++] = 61;
                paddingCount = 2;
            }
            if (bufferIndex > 0)
            {
                if (this.Padding == true)
                {
                    encoded.Add(buffer, bufferIndex);
                }
                else
                {
                    encoded.Add(buffer, bufferIndex - paddingCount);
                }
            }
            return encoded.ToArray();
        }
Ejemplo n.º 6
0
        private Byte[] DecodeFromUnNormalizedInput(Byte[] input)
        {
            BufferByteArray decoded = new BufferByteArray((input.Length / 4 * 3));
            var buffer = this.GetBuffer();
            Int32 bufferIndex = 0;
            UInt32 bbbb = 0;
            Int32 bIndex = 0;
            Byte c = 0;
            Int32 paddingCount = 0;
            var decodeTable = this.Base64CharTable.DecodeTable;//For performance improvement
            Int32 length = input.Length;//For performance improvement
            for (int i = 0; i < length; i++)
            {
                c = decodeTable[input[i]];
                if (c == 255) { continue; }

                if (bufferIndex + 3 >= buffer.Length)
                {
                    decoded.Add(buffer, bufferIndex);
                    bufferIndex = 0;
                }

                bbbb = bbbb << 6 | c;
                bIndex += 1;
                if (input[i] == 61)// =
                {
                    paddingCount++;
                }
                if (bIndex == 4)
                {
                    buffer[bufferIndex++] = (byte)((bbbb >> 16) & 0xFF);
                    buffer[bufferIndex++] = (byte)((bbbb >> 8) & 0xFF);
                    buffer[bufferIndex++] = (byte)(bbbb & 0xFF);
                    bbbb = 0;
                    bIndex = 0;
                }
            }
            if (bufferIndex > 0)
            {
                decoded.Add(buffer, bufferIndex - paddingCount);
            }
            return decoded.ToArray();
        }
        public override Byte[] Encode(Byte[] input)
        {
            BufferByteArray decoded     = new BufferByteArray(input.Length * 3);
            var             buffer      = this.GetBuffer();
            Int32           bufferIndex = 0;
            Byte            c           = 0;
            Int32           charCount   = 0;

            Int32 length = input.Length;

            for (int i = 0; i < length; i++)
            {
                c = input[i];
                if (bufferIndex + 6 > buffer.Length)
                {
                    decoded.Add(buffer, bufferIndex);
                    bufferIndex = 0;
                }
                if (InsertNewline == true && this.CharCountPerLine - 3 < charCount)//Insert new line
                {
                    var escaped = false;

                    if (c == 0x09 || c == 0x20)
                    {
                        // \t or white space
                        buffer[bufferIndex++] = 61; // '='
                        buffer[bufferIndex++] = HexAlphabet[(c & 0xf0) >> 4];
                        buffer[bufferIndex++] = HexAlphabet[c & 0x0f];

                        escaped = true;
                    }

                    buffer[bufferIndex++] = 61; // '='
                    buffer[bufferIndex++] = 13; // \r
                    buffer[bufferIndex++] = 10; // \n

                    charCount = 0;

                    if (escaped)
                    {
                        continue;
                    }
                }

                var quote = false;

                switch (c)
                {
                case 9:     // \t
                case 32:    // ' ' WhiteSpace
                case 63:    // ?
                case 95:    // _
                    quote = this.InsertNewline == false;
                    break;

                case 0x3d:     // '='
                    quote = true;
                    break;

                default:
                    // NonPrintableChar
                    quote = (c < 33 || 127 < c);
                    break;
                }

                if (quote == true)
                {
                    // = or NonPrintableChar
                    buffer[bufferIndex++] = 61; // '='
                    buffer[bufferIndex++] = HexAlphabet[(c & 0xf0) >> 4];
                    buffer[bufferIndex++] = HexAlphabet[c & 0x0f];

                    charCount += 3;
                }
                else
                {
                    // = or PrintableChar
                    buffer[bufferIndex++] = c;

                    charCount++;
                }
            }
            if (bufferIndex > 0)
            {
                decoded.Add(buffer, bufferIndex);
            }
            return(decoded.ToArray());
        }
        public override Byte[] Decode(Byte[] input)
        {
            BufferByteArray decoded     = new BufferByteArray(input.Length);
            var             buffer      = this.GetBuffer();
            Int32           bufferIndex = 0;
            var             bbb         = new byte[3];
            var             bbbIndex    = 0;
            Byte            b           = 0;
            Byte            c           = 0;

            var length = input.Length;

            for (int i = 0; i < length; i++)
            {
                c = input[i];
                if (bufferIndex + 3 > buffer.Length)
                {
                    decoded.Add(buffer, bufferIndex);
                    bufferIndex = 0;
                }

                if (bbbIndex == 0)
                {
                    if (c == 61) // =
                    {
                        bbb[bbbIndex++] = c;
                    }
                    else if (this.Mode == QuotedPrintableConvertMode.Header && c == 0x5f) // _
                    {
                        buffer[bufferIndex++] = 32;                                       // white space
                    }
                    else
                    {
                        // NonQuotedChar
                        buffer[bufferIndex++] = c;
                    }
                }
                else
                {
                    // QuotedChar
                    bbb[bbbIndex++] = c;
                }

                if (bbbIndex == 3)
                {
                    if (bbb[1] == 13 && bbb[2] == 10)
                    {
                        // New line
                        bbbIndex = 0;
                    }
                    else if (bbb[1] == 13 || bbb[1] == 10)
                    {
                        if (bbb[2] == 61)
                        {
                            bbbIndex = 1;
                        }
                        else
                        {
                            buffer[bufferIndex++] = bbb[2];
                            bbbIndex = 0;
                        }
                    }
                    else
                    {
                        byte d = 0;

                        for (var bIndex = 1; bIndex < 3; bIndex++)
                        {
                            d <<= 4;
                            b   = bbb[bIndex];

                            if (0x30 <= b && b <= 0x39)
                            {
                                d |= (byte)(b - 0x30);// 0-9
                            }
                            else if (0x41 <= b && b <= 0x46)
                            {
                                d |= (byte)(b - 0x37);// A-F
                            }
                            else if (0x61 <= b && b <= 0x66)
                            {
                                d |= (byte)(b - 0x57);// a-f
                            }
                            else
                            {
                                //Not encoded text
                            }
                        }
                        if (d > 0)
                        {
                            buffer[bufferIndex++] = d;
                        }
                        else
                        {
                            buffer[bufferIndex++] = bbb[0];
                            buffer[bufferIndex++] = bbb[1];
                            buffer[bufferIndex++] = bbb[2];
                        }
                        bbbIndex = 0;
                    }
                }
            }
            if (bufferIndex > 0)
            {
                decoded.Add(buffer, bufferIndex);
            }
            return(decoded.ToArray());
        }