FyreVM.Quetzal.DecompressMemory C# (CSharp) Method

DecompressMemory() public static method

Reconstitutes a changed block of memory by applying a compressed set of differences to the original block from the game file.
public static DecompressMemory ( byte original, byte delta ) : byte[]
original byte The original block of memory.
delta byte The RLE-compressed set of differences, /// prefixed with a 4-byte length. This length may be larger than /// the original block, but not smaller.
return byte[]
        public static byte[] DecompressMemory(byte[] original, byte[] delta)
        {
            MemoryStream mstr = new MemoryStream(delta);
            uint length = BigEndian.ReadInt32(mstr);
            if (length < original.Length)
                throw new ArgumentException("Compressed block's length tag must be no less than original block's size");

            byte[] result = new byte[length];
            int rp = 0;

            for (int i = 4; i < delta.Length; i++)
            {
                byte b = delta[i];
                if (b == 0)
                {
                    int repeats = delta[++i] + 1;
                    Array.Copy(original, rp, result, rp, repeats);
                    rp += repeats;
                }
                else
                {
                    result[rp] = (byte)(original[rp] ^ b);
                    rp++;
                }
            }

            while (rp < original.Length)
            {
                result[rp] = original[rp];
                rp++;
            }

            return result;
        }