private static byte[] ReadExactly(Stream stream, int count)
 {
     if (count < 0)
     {
         throw new ArgumentOutOfRangeException("count");
     }
     byte[] array = new byte[count];
     BinaryPatchUtility.ReadExactly(stream, array, 0, count);
     return(array);
 }
        public static void Apply(Stream input, Func <Stream> openPatchStream, Stream output)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            if (openPatchStream == null)
            {
                throw new ArgumentNullException("openPatchStream");
            }
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            long num2;
            long num3;
            long num4;

            using (Stream stream = openPatchStream.Invoke())
            {
                if (!stream.get_CanRead())
                {
                    throw new ArgumentException("Patch stream must be readable.", "openPatchStream");
                }
                if (!stream.get_CanSeek())
                {
                    throw new ArgumentException("Patch stream must be seekable.", "openPatchStream");
                }
                byte[] buf = BinaryPatchUtility.ReadExactly(stream, 32);
                long   num = BinaryPatchUtility.ReadInt64(buf, 0);
                if (num != 3473478480300364610L)
                {
                    throw new InvalidOperationException("Corrupt patch.");
                }
                num2 = BinaryPatchUtility.ReadInt64(buf, 8);
                num3 = BinaryPatchUtility.ReadInt64(buf, 16);
                num4 = BinaryPatchUtility.ReadInt64(buf, 24);
                if (num2 < 0L || num3 < 0L || num4 < 0L)
                {
                    throw new InvalidOperationException("Corrupt patch.");
                }
            }
            byte[] array  = new byte[1048576];
            byte[] array2 = new byte[1048576];
            using (Stream stream2 = openPatchStream.Invoke())
            {
                using (Stream stream3 = openPatchStream.Invoke())
                {
                    using (Stream stream4 = openPatchStream.Invoke())
                    {
                        stream2.Seek(32L, 1);
                        stream3.Seek(32L + num2, 1);
                        stream4.Seek(32L + num2 + num3, 1);
                        using (BZip2InputStream bZip2InputStream = new BZip2InputStream(stream2))
                        {
                            using (BZip2InputStream bZip2InputStream2 = new BZip2InputStream(stream3))
                            {
                                using (BZip2InputStream bZip2InputStream3 = new BZip2InputStream(stream4))
                                {
                                    long[] array3 = new long[3];
                                    byte[] array4 = new byte[8];
                                    int    num5   = 0;
                                    int    num6   = 0;
                                    while ((long)num6 < num4)
                                    {
                                        for (int i = 0; i < 3; i++)
                                        {
                                            BinaryPatchUtility.ReadExactly(bZip2InputStream, array4, 0, 8);
                                            array3[i] = BinaryPatchUtility.ReadInt64(array4, 0);
                                        }
                                        if ((long)num6 + array3[0] > num4)
                                        {
                                            throw new InvalidOperationException("Corrupt patch.");
                                        }
                                        input.set_Position((long)num5);
                                        int num7;
                                        for (int j = (int)array3[0]; j > 0; j -= num7)
                                        {
                                            num7 = Math.Min(j, 1048576);
                                            BinaryPatchUtility.ReadExactly(bZip2InputStream2, array, 0, num7);
                                            int num8 = Math.Min(num7, (int)(input.get_Length() - input.get_Position()));
                                            BinaryPatchUtility.ReadExactly(input, array2, 0, num8);
                                            for (int k = 0; k < num8; k++)
                                            {
                                                byte[] expr_22A_cp_0 = array;
                                                int    expr_22A_cp_1 = k;
                                                expr_22A_cp_0[expr_22A_cp_1] += array2[k];
                                            }
                                            output.Write(array, 0, num7);
                                            num6 += num7;
                                            num5 += num7;
                                        }
                                        if ((long)num6 + array3[1] > num4)
                                        {
                                            throw new InvalidOperationException("Corrupt patch.");
                                        }
                                        int num9;
                                        for (int j = (int)array3[1]; j > 0; j -= num9)
                                        {
                                            num9 = Math.Min(j, 1048576);
                                            BinaryPatchUtility.ReadExactly(bZip2InputStream3, array, 0, num9);
                                            output.Write(array, 0, num9);
                                            num6 += num9;
                                        }
                                        num5 = (int)((long)num5 + array3[2]);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }