示例#1
0
        /// <summary>
        /// Decodes a base64 data array
        /// </summary>
        /// <param name="data">The base64 data to decode</param>
        public override byte[] Decode(byte[] data)
        {
            if (m_Padding != null)
            {
                int paddingChar = (char)m_Padding;
                if ((paddingChar < 0) || (paddingChar > 127))
                {
                    throw new ArgumentException(string.Format("Invalid padding character!"), nameof(m_Padding));
                }
            }
            //decode data
            List <byte> result = new List <byte>(data.Length);
            int         value  = 0;
            int         bits   = 0;

            foreach (byte b in data)
            {
                if (b == m_Padding)
                {
                    break;
                }

                value <<= BitCount;
                bits   += BitCount;
                value  |= CharacterDictionary.GetValue((char)b);
                if (bits >= 8)
                {
                    bits -= 8;
                    int l_Out = value >> bits;
                    value = value & ~(0xFFFF << bits);
                    result.Add((byte)l_Out);
                }
            }
            return(result.ToArray());
        }
示例#2
0
 /// <summary>Initializes a new instance of the <see cref="Base32"/> class.</summary>
 /// <param name="dictionary">The dictionary containing 64 ascii characters used for encoding.</param>
 /// <param name="padding">The padding (use null to skip padding).</param>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 /// <exception cref="ArgumentException"></exception>
 public Base32(CharacterDictionary dictionary, char?padding) : base(dictionary, BitCount)
 {
     m_Padding = padding;
     if (m_Padding != null)
     {
         int paddingChar = (char)m_Padding;
         if ((paddingChar < 0) || (paddingChar > 127))
         {
             throw new ArgumentException(string.Format("Invalid padding character!"), nameof(m_Padding));
         }
     }
 }
示例#3
0
        /// <summary>Encodes the specified data.</summary>
        /// <param name="data">The data to encode.</param>
        public override string Encode(byte[] data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var result = new List <char>(data.Length * 2);
            var value  = 0;
            var bits   = 0;

            foreach (var b in data)
            {
                value = (value << 8) | b;
                bits += 8;
                while (bits >= bitCount)
                {
                    bits -= bitCount;
                    var outValue = value >> bits;
                    value &= ~(0xFFFF << bits);
                    result.Add(CharacterDictionary.GetCharacter(outValue));
                }
            }

            if (bits > bitCount)
            {
                bits -= bitCount;
                var outValue = value >> bits;
                value &= ~(0xFFFF << bits);
                result.Add(CharacterDictionary.GetCharacter(outValue));
            }

            if (bits > 0)
            {
                var shift    = bitCount - bits;
                var outValue = value << shift;
                result.Add(CharacterDictionary.GetCharacter(outValue));
                bits -= bitCount;
            }

            if (Padding != null)
            {
                var padding = (char)Padding;
                while ((bits % 8) != 0)
                {
                    result.Add(padding);
                    bits -= bitCount;
                }
            }

            return(new string(result.ToArray()));
        }
示例#4
0
 /// <summary>Initializes a new instance of the <see cref="Base64" /> class.</summary>
 /// <param name="dict">The dictionary containing 64 ascii characters used for encoding.</param>
 /// <param name="padding">The padding (use null to skip padding).</param>
 /// <exception cref="ArgumentOutOfRangeException"></exception>
 /// <exception cref="ArgumentException">Invalid padding character.</exception>
 public Base64(CharacterDictionary dict, char?padding)
     : base(dict, bitCount)
 {
     Padding = padding;
     if (Padding != null)
     {
         int paddingChar = (char)Padding;
         if (paddingChar is < 0 or > 127)
         {
             throw new ArgumentOutOfRangeException(nameof(padding));
         }
     }
 }
示例#5
0
        /// <summary>Initializes a new instance of the <see cref="BaseX" /> class.</summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="bitCount">The bit count.</param>
        /// <exception cref="ArgumentOutOfRangeException">Invalid dictionary length or bit count.</exception>
        protected BaseX(CharacterDictionary dictionary, int bitCount)
        {
            if (bitCount is < 1 or > 32)
            {
                throw new ArgumentOutOfRangeException(nameof(bitCount), "BitCount in range 1..32 required!");
            }

            CharacterDictionary = dictionary ?? throw new ArgumentNullException(nameof(dictionary));
            BitsPerCharacter    = bitCount;
            if (dictionary.Length != (1 << bitCount))
            {
                throw new ArgumentOutOfRangeException(nameof(dictionary), "Invalid dictionary length!");
            }
        }
示例#6
0
        /// <summary>Initializes a new instance of the <see cref="BaseX" /> class.</summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="bitCount">The bit count.</param>
        /// <exception cref="Exception">Invalid dictionary / bits</exception>
        public BaseX(CharacterDictionary dictionary, int bitCount)
        {
            if (bitCount < 1 || bitCount > 32)
            {
                throw new ArgumentOutOfRangeException(nameof(BitsPerCharacter));
            }

            CharacterDictionary = dictionary;
            BitsPerCharacter    = bitCount;
            if (dictionary.Length != 1 << bitCount)
            {
                throw new Exception("Invalid dictionary / bits");
            }
        }
示例#7
0
        /// <summary>Decodes a Base32 data array.</summary>
        /// <param name="data">The Base32 data to decode.</param>
        public override byte[] Decode(byte[] data)
        {
            if (CharacterDictionary == null)
            {
                throw new InvalidOperationException($"Property {nameof(CharacterDictionary)} has to be set!");
            }

            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (Padding != null)
            {
                int paddingChar = (char)Padding;
                if (paddingChar is < 0 or > 127)
                {
                    throw new InvalidOperationException("Invalid padding character!");
                }
            }

            // decode data
            var result = new List <byte>(data.Length);
            var value  = 0;
            var bits   = 0;

            foreach (var b in data)
            {
                if (b == Padding)
                {
                    break;
                }

                value <<= bitCount;
                bits   += bitCount;
                value  |= CharacterDictionary.GetValue((char)b);
                if (bits >= 8)
                {
                    bits -= 8;
                    var outValue = value >> bits;
                    value &= ~(0xFFFF << bits);
                    result.Add((byte)outValue);
                }
            }

            return(result.ToArray());
        }
示例#8
0
        /// <summary>
        /// Encodes the specified data
        /// </summary>
        /// <param name="data">The data to encode</param>
        public override string Encode(byte[] data)
        {
            List <char> result = new List <char>(data.Length * 2);
            int         value  = 0;
            int         bits   = 0;

            foreach (byte b in data)
            {
                value = (value << 8) | b;
                bits += 8;
                while (bits >= BitCount)
                {
                    bits -= BitCount;
                    int outValue = value >> bits;
                    value = value & ~(0xFFFF << bits);
                    result.Add(CharacterDictionary.GetCharacter(outValue));
                }
            }
            if (bits >= BitCount)
            {
                bits -= BitCount;
                int outValue = value >> bits;
                value = value & ~(0xFFFF << bits);
                result.Add(CharacterDictionary.GetCharacter(outValue));
            }
            if (bits > 0)
            {
                int shift    = BitCount - bits;
                int outValue = value << shift;
                result.Add(CharacterDictionary.GetCharacter(outValue));
                bits -= BitCount;
            }
            if (m_Padding != null)
            {
                char padding = (char)m_Padding;
                while (bits % 8 != 0)
                {
                    result.Add(padding);
                    bits -= BitCount;
                }
            }
            return(new string(result.ToArray()));
        }
 private CharacterDictionary(CharacterDictionary cloneData)
 {
     m_Characters = (char[])cloneData.m_Characters.Clone();
     m_Values     = (int[])cloneData.m_Values.Clone();
 }