FyreVM.Quetzal.CompressMemory C# (CSharp) Method

CompressMemory() public static method

Compresses a block of memory by comparing it with the original version from the game file.
public static CompressMemory ( byte original, int origStart, int origLength, byte changed, int changedStart, int changedLength ) : byte[]
original byte An array containing the original block of memory.
origStart int The offset within the array where the original block /// starts.
origLength int The length of the original block, in bytes.
changed byte An array containing the changed block to be compressed.
changedStart int The offset within the array where the changed /// block starts.
changedLength int The length of the changed block. This may be /// greater than , but may not be less.
return byte[]
        public static byte[] CompressMemory(byte[] original, int origStart, int origLength,
            byte[] changed, int changedStart, int changedLength)
        {
            if (origStart + origLength > original.Length)
                throw new ArgumentException("Original array is too small");
            if (changedStart + changedLength > changed.Length)
                throw new ArgumentException("Changed array is too small");
            if (changedLength < origLength)
                throw new ArgumentException("New block must be no smaller than old block");

            MemoryStream mstr = new MemoryStream();
            BigEndian.WriteInt32(mstr, (uint)changedLength);

            for (int i = 0; i < origLength; i++)
            {
                byte b = (byte)(original[origStart+i] ^ changed[changedStart+i]);
                if (b == 0)
                {
                    int runLength;
                    for (runLength = 1; i + runLength < origLength; runLength++)
                    {
                        if (runLength == 256)
                            break;
                        if (original[origStart + i + runLength] != changed[changedStart + i + runLength])
                            break;
                    }
                    mstr.WriteByte(0);
                    mstr.WriteByte((byte)(runLength - 1));
                    i += runLength - 1;
                }
                else
                    mstr.WriteByte(b);
            }

            return mstr.ToArray();
        }