Пример #1
0
        public override void DecodeBytes(string s, int startIndex, int length, Stream outputStream)
        {
            var abc      = Alphabet;
            var temp     = new byte[8];
            var bitGroup = new BitGroup(_bitCount);

            while (length > _bitGroupCharCount)
            {
                // this is the faster code path
                // it does not need to check for padding
                // or rather it does not expect to encounter any padding
                bitGroup.Reset();
                for (int i = 0; i < _bitGroupCharCount; i++)
                {
                    var c = s[startIndex + i];
                    var x = (uint)abc.Decode(c);
                    bitGroup.Write(x);
                }
                bitGroup.Read(temp, 0, _bitGroupByteCount);
                outputStream.Write(temp, 0, _bitGroupByteCount);
                startIndex += _bitGroupCharCount;
                length     -= _bitGroupCharCount;
            }
            // padding?
            if (length > 0)
            {
                bitGroup.Reset();
                int i, padding = 0;
                for (i = 0; i < _bitGroupCharCount; i++)
                {
                    var c = s[startIndex + i];
                    if (c == _paddingChar)
                    {
                        bitGroup.Write(0);
                        padding++;
                        continue;
                    }
                    var x = (uint)abc.Decode(c);
                    bitGroup.Write(x);
                }
                bitGroup.Read(temp, 0, _bitGroupByteCount);
                var byteCount = _byteCountFromCharCount[i - padding];
                if (byteCount == -1)
                {
                    throw new InvalidOperationException("Padding is invalid.");
                }
                outputStream.Write(temp, 0, byteCount);
            }
        }
Пример #2
0
        public void BitGroup_LoadFromTest()
        {
            var xs = new[] {
                Tuple.Create(new byte[] {  }, 0x00UL),
                Tuple.Create(new byte[] { 0x01 }, 0x01UL),
                Tuple.Create(new byte[] { 0x01, 0x02 }, 0x0102UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03 }, 0x010203UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03, 0x04 }, 0x01020304UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }, 0x0102030405UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }, 0x010203040506UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, 0x01020304050607UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }, 0x0102030405060708UL),
            };

            foreach (var x in xs)
            {
                var bitGroup = new BitGroup();
                bitGroup.LoadFrom(x.Item1, 0, x.Item1.Length);
                Assert.AreEqual(x.Item2, bitGroup.Value);
            }
        }
Пример #3
0
        public void BitGroup_ReadByteArrayTest()
        {
            var xs = new[] {
                Tuple.Create(new byte[] {  }, 0x00UL),
                Tuple.Create(new byte[] { 0x01 }, 0x01UL),
                Tuple.Create(new byte[] { 0x01, 0x02 }, 0x0102UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03 }, 0x010203UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03, 0x04 }, 0x01020304UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 }, 0x0102030405UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }, 0x010203040506UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, 0x01020304050607UL),
                Tuple.Create(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }, 0x0102030405060708UL),
            };

            foreach (var x in xs)
            {
                var bitGroup = new BitGroup();
                bitGroup.LoadFrom(x.Item1, 0, x.Item1.Length);
                var temp = new byte[x.Item1.Length];
                bitGroup.Read(temp, 0, temp.Length);
                CollectionAssert.AreEqual(x.Item1, temp);
            }
        }