예제 #1
0
        static Base64Encoding()
        {
            Lex = new Base64Encoding(lexAlphabet);

            Mime = new Base64Encoding(mimeAlphabet, mimePaddingSymbol);

            Url = new Base64Encoding(urlAlphabet);
        }
예제 #2
0
        public static TryResult <String> TryToBase64String(Value128 data, Base64Encoding base64Encoding)
        {
            if (base64Encoding == null)
            {
                return(encodeFailResult);
            }

            Char[] result;

            // Get padding symbol
            var paddingSymbol = base64Encoding.PaddingSymbol;

            if (paddingSymbol.HasValue)
            {
                result = new Char[24];

                result[22] = result[23] = paddingSymbol.Value;
            }
            else
            {
                result = new Char[22];
            }

            var higherHalf = data.HigherHalf;

            var lowerHalf = data.LowerHalf;

            // Get alphabet
            var alphabet = base64Encoding.Alphabet;

            for (Int32 indexH = 0, indexL = 11, shiftH = 58, shiftL = 56; indexH < 10; indexH++, indexL++, shiftH -= 6, shiftL -= 6)
            {
                result[indexH] = alphabet[(Int32)(higherHalf >> shiftH) & 0x3F];

                result[indexL] = alphabet[(Int32)(lowerHalf >> shiftL) & 0x3F];
            }

            result[10] = alphabet[(Int32)(((higherHalf << 2) & 0x3C) | ((lowerHalf >> 62) & 0x03))];

            result[21] = alphabet[(Int32)((lowerHalf << 4) & 0x30)];

            return(TryResult <String> .CreateSuccess(new String(result)));
        }
예제 #3
0
        public void TestCtor()
        {
            try             // null alphabet
            {
                var x = new Base64Encoding(null, Base64Encoding.Mime.LookupTable, '=');

                Assert.Fail(typeof(ArgumentNullException).ToString());
            }
            catch (ArgumentNullException)
            {
            }

            try             // bad length alphabet
            {
                var x = new Base64Encoding("1245645", Base64Encoding.Mime.LookupTable, null);

                Assert.Fail(typeof(ArgumentException).ToString());
            }
            catch (ArgumentException)
            {
            }

            try             // look-up table null
            {
                var x = new Base64Encoding(Base64Encoding.Mime.Alphabet, null, '=');

                Assert.Fail(typeof(ArgumentNullException).ToString());
            }
            catch (ArgumentNullException)
            {
            }

            try             // look-up table bad length
            {
                var x = new Base64Encoding(Base64Encoding.Mime.Alphabet, new Byte[20], null);

                Assert.Fail(typeof(ArgumentNullException).ToString());
            }
            catch (ArgumentException)
            {
            }
        }
예제 #4
0
        public static TryResult <Value128> TryFromBase64String(String data, Int32 offset, Base64Encoding base64Encoding)
        {
            // Check input values
            if ((data == null) || (offset > data.Length - base64EncodedSymbolsCount) || (base64Encoding == null))
            {
                return(convertFailResult);
            }

            // Get look-up table
            var lookupTable = base64Encoding.LookupTable;

            var lastIndex = offset + 10;

            var symbol10 = (UInt64)lookupTable[data[lastIndex] & 0x7F];

            var symbol21 = (UInt64)lookupTable[data[offset + 21] & 0x7F];

            // Check symbol
            if ((symbol10 | symbol21) == 0xFF)
            {
                return(convertFailResult);
            }

            // Calculate higher half
            var higherHalf = symbol10 >> 2;

            // Calculate lower half
            var lowerHalf = symbol10 << 62 | symbol21 >> 4;

            // Decode symbols
            for (Int32 indexH = offset, indexL = offset + 11, shiftH = 58, shiftL = 56; indexH < lastIndex; indexH++, indexL++, shiftH -= 6, shiftL -= 6)
            {
                // Get symbols
                var symbolH = data[indexH] & 0x7F;

                var symbolL = data[indexL] & 0x7F;

                // Get symbols values
                var symbolHValue = (UInt64)lookupTable[symbolH];

                var symbolLValue = (UInt64)lookupTable[symbolL];

                // Check symbol
                if ((symbolHValue | symbolLValue) == 0xFF)
                {
                    return(convertFailResult);
                }

                higherHalf |= symbolHValue << shiftH;

                lowerHalf |= symbolLValue << shiftL;
            }

            // Initialize a new instance
            var result = new Value128(higherHalf, lowerHalf);

            return(TryResult <Value128> .CreateSuccess(result));
        }