Beispiel #1
0
        public TryResult <String> TryEncode(Byte[] data, Int32 offset, Int32 count)
        {
            // Check data
            if (data == null)
            {
                return(encodeFailResult);
            }

            // Check count argument
            if (count < 0)
            {
                return(encodeFailResult);
            }

            // Check offset argument
            if (offset < 0)
            {
                return(encodeFailResult);
            }

            // Check offset and count
            if (offset > (data.Length - count))
            {
                return(encodeFailResult);
            }

            // Encode
            var result = EncodeInternal(data, offset, count);

            return(TryResult <String> .CreateSuccess(result));
        }
Beispiel #2
0
        public TryResult <Byte[]> TryDecode(String data, Int32 offset, Int32 count)
        {
            // Check data argument
            if (data == null)
            {
                return(decodeFailResult);
            }

            // Check count argument
            if (count < 0)
            {
                return(decodeFailResult);
            }

            // Check offset argument
            if (offset < 0)
            {
                return(decodeFailResult);
            }

            // Check offset and count
            if (offset > (data.Length - count))
            {
                return(decodeFailResult);
            }

            // Decode
            var result = DecodeInternal(data, offset, count);

            return(result == null ? decodeFailResult : TryResult <Byte[]> .CreateSuccess(result));
        }
        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));
        }
Beispiel #4
0
        public TryResult <String> TryEncode(Byte[] data)
        {
            // Check data argument
            if (data == null)
            {
                return(encodeFailResult);
            }

            // Encode
            var result = EncodeInternal(data, 0, data.Length);

            return(TryResult <String> .CreateSuccess(result));
        }
Beispiel #5
0
        public TryResult <Byte[]> TryDecode(String data)
        {
            // Check data argument
            if (data == null)
            {
                return(decodeFailResult);
            }

            // Decode
            var result = DecodeInternal(data, 0, data.Length);

            return(result == null ? decodeFailResult : TryResult <Byte[]> .CreateSuccess(result));
        }
        public static TryResult <Value128> TryFromByteArray(Byte[] data, Int32 offset)
        {
            if ((data == null) || (data.Length - offset < 16))
            {
                return(convertFailResult);
            }

            // Convert higher half
            var higherHalf = Convert(data, offset);

            // Convert lower half
            var lowerHalf = Convert(data, offset + 8);

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

            return(TryResult <Value128> .CreateSuccess(result));
        }
        public static TryResult <String> TryToBase32String(Value128 data, Base32Encoding base32Encoding)
        {
            if (base32Encoding == null)
            {
                return(encodeFailResult);
            }

            Char[] result;

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

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

                // Set padding
                result[26] = result[27] = result[28] = result[29] = result[30] = result[31] = paddingSymbol.Value;
            }
            else
            {
                result = new Char[26];
            }

            var higherHalf = data.HigherHalf;

            var lowerHalf = data.LowerHalf;

            // Get alphabet
            var alphabet = base32Encoding.Alphabet;

            for (Int32 indexH = 0, indexL = 13, shiftH = 59, shiftL = 58; indexH < 12; indexH++, indexL++, shiftH -= 5, shiftL -= 5)
            {
                result[indexH] = alphabet[(Int32)(higherHalf >> shiftH) & 0x1F];

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

            result[12] = alphabet[(Int32)(((higherHalf << 1) & 0x1E) | ((lowerHalf >> 63) & 0x01))];

            result[25] = alphabet[(Int32)((lowerHalf << 2) & 0x1C)];

            return(TryResult <String> .CreateSuccess(new String(result)));
        }
        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)));
        }
Beispiel #9
0
 public Boolean Equals(TryResult <T> other)
 {
     return(Success.Equals(other.Success) && Result.Equals(other.Result));
 }