CrystalMpq.MpqFileStream.UnpackRle C# (CSharp) Method

UnpackRle() private method

private UnpackRle ( uint compressedLength ) : byte[]
compressedLength uint
return byte[]
        private unsafe byte[] UnpackRle(uint compressedLength)
        {
            uint length = this.ReadUInt32();
            compressedLength -= 4;
            var decompressed = new byte[length];

            var compressed = new byte[compressedLength];
            if (Read(compressed, 0, compressed.Length) != compressed.Length) throw new EndOfStreamException();

            int i = 0;
            int j = 0;
            while (i < compressed.Length && j < decompressed.Length)
            {
                int b = compressed[i++];

                var isRepeatCount = (b & 0x80) != 0;
                if (isRepeatCount)
                {
                    var repeatCount = (b & 0x7F) + 1;
                    for (var k = 0; k < repeatCount; ++k)
                    {
                        // I don't think this check is necessary... probably leaving it out some day in the future
                        if (i >= compressed.Length && j >= decompressed.Length)
                            break;
                        decompressed[j++] = compressed[i++];
                    }
                }
                else j += b + 1;
            }
            return decompressed;
        }