Exemplo n.º 1
0
        public void DecodeFlag()
        {
            var bytes = BitConverter.GetBytes(0b10011_10); // val = 0x33
            var bs    = new BitReadStream(bytes);

            var(rlen, rmap) = TransformTable.DecodeFlag(bs);
            Assert.AreEqual <int>(5, rlen);

            var rval = (int)bs.Read(rlen);

            Assert.AreEqual <byte>(0x33, rmap[rval]);
        }
Exemplo n.º 2
0
        public byte[] Decode(byte[] flag, byte[] residual)
        {
            var flagIn = new BitReadStream(flag);
            var resOut = new ByteBuffer();

            // Actual residual length
            int resLength = residual.Length;

            try {
                while (!flagIn.IsEOF)
                {
                    // Parse residual header
                    int kset  = residual[0] >> 3;
                    int fbits = residual[0] & 7;

                    // Initialize residual reader and discard header
                    var resIn = new BitReadStream(residual, resLength * 8 - fbits);
                    resIn.Read(8);

                    // Perform inverse MV2 substitutions
                    for (int i = 0; !resIn.IsEOF; i++)
                    {
                        var(rlen, rmap) = TransformTable.DecodeFlag(flagIn);
                        byte val = rmap[resIn.Read(rlen)];

                        // Unscramble byte values after MV2 decoding
                        val = key.XorByte(key.Decode(val, kset), i);
                        resOut.Write(val);
                    }

                    // Skip flag padding
                    flagIn.FinishByte();

                    // Forward the residual to the next round
                    residual  = resOut.GetBytesInternal();
                    resLength = resOut.Position;
                    resOut.Reset();
                }
            } catch (ArgumentException ex) {
                throw new DecoderException(ex);
            } catch (IndexOutOfRangeException ex) {
                throw new DecoderException(ex);
            }

            // Trim final output
            var output = new byte[resLength];

            Array.Copy(residual, output, resLength);

            return(output);
        }