package rj;

import com.google.firebase.perf.util.Constants;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import net.sqlcipher.database.SQLiteDatabase;

/* loaded from: classes3.dex */
public abstract class k {
    public static long a(l lVar, InputStream inputStream, OutputStream outputStream, long j10) {
        byte[] bArr = new byte[16384];
        DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(inputStream, 4096));
        int readInt = dataInputStream.readInt();
        if (readInt != -771763713) {
            throw new j("Unexpected magic=".concat(String.valueOf(String.format("%x", Integer.valueOf(readInt)))));
        }
        int read = dataInputStream.read();
        if (read != 4) {
            throw new j("Unexpected version=" + read);
        }
        long j11 = 0;
        while (true) {
            long j12 = j10 - j11;
            try {
                int read2 = dataInputStream.read();
                if (read2 == -1) {
                    throw new IOException("Patch file overrun");
                }
                if (read2 == 0) {
                    return j11;
                }
                switch (read2) {
                    case 247:
                        read2 = dataInputStream.readUnsignedShort();
                        c(bArr, dataInputStream, outputStream, read2, j12);
                        break;
                    case 248:
                        read2 = dataInputStream.readInt();
                        c(bArr, dataInputStream, outputStream, read2, j12);
                        break;
                    case 249:
                        long readUnsignedShort = dataInputStream.readUnsignedShort();
                        read2 = dataInputStream.read();
                        if (read2 == -1) {
                            throw new IOException("Unexpected end of patch");
                        }
                        b(bArr, lVar, outputStream, readUnsignedShort, read2, j12);
                        break;
                    case SQLiteDatabase.MAX_SQL_CACHE_SIZE /* 250 */:
                        long readUnsignedShort2 = dataInputStream.readUnsignedShort();
                        read2 = dataInputStream.readUnsignedShort();
                        b(bArr, lVar, outputStream, readUnsignedShort2, read2, j12);
                        break;
                    case 251:
                        long readUnsignedShort3 = dataInputStream.readUnsignedShort();
                        read2 = dataInputStream.readInt();
                        b(bArr, lVar, outputStream, readUnsignedShort3, read2, j12);
                        break;
                    case 252:
                        long readInt2 = dataInputStream.readInt();
                        read2 = dataInputStream.read();
                        if (read2 == -1) {
                            throw new IOException("Unexpected end of patch");
                        }
                        b(bArr, lVar, outputStream, readInt2, read2, j12);
                        break;
                    case 253:
                        long readInt3 = dataInputStream.readInt();
                        read2 = dataInputStream.readUnsignedShort();
                        b(bArr, lVar, outputStream, readInt3, read2, j12);
                        break;
                    case 254:
                        long readInt4 = dataInputStream.readInt();
                        read2 = dataInputStream.readInt();
                        b(bArr, lVar, outputStream, readInt4, read2, j12);
                        break;
                    case Constants.MAX_HOST_LENGTH /* 255 */:
                        long readLong = dataInputStream.readLong();
                        read2 = dataInputStream.readInt();
                        b(bArr, lVar, outputStream, readLong, read2, j12);
                        break;
                    default:
                        c(bArr, dataInputStream, outputStream, read2, j12);
                        break;
                }
                j11 += read2;
            } finally {
                outputStream.flush();
            }
        }
    }

    private static void b(byte[] bArr, l lVar, OutputStream outputStream, long j10, int i10, long j11) {
        if (i10 < 0) {
            throw new IOException("copyLength negative");
        }
        if (j10 < 0) {
            throw new IOException("inputOffset negative");
        }
        long j12 = i10;
        if (j12 > j11) {
            throw new IOException("Output length overrun");
        }
        try {
            InputStream c10 = new m(lVar, j10, j12, false).c();
            while (i10 > 0) {
                try {
                    int min = Math.min(i10, 16384);
                    int i11 = 0;
                    while (i11 < min) {
                        int read = c10.read(bArr, i11, min - i11);
                        if (read == -1) {
                            throw new IOException("truncated input stream");
                        }
                        i11 += read;
                    }
                    outputStream.write(bArr, 0, min);
                    i10 -= min;
                } finally {
                }
            }
            c10.close();
        } catch (EOFException e10) {
            throw new IOException("patch underrun", e10);
        }
    }

    private static void c(byte[] bArr, DataInputStream dataInputStream, OutputStream outputStream, int i10, long j10) {
        if (i10 < 0) {
            throw new IOException("copyLength negative");
        }
        if (i10 > j10) {
            throw new IOException("Output length overrun");
        }
        while (i10 > 0) {
            try {
                int min = Math.min(i10, 16384);
                dataInputStream.readFully(bArr, 0, min);
                outputStream.write(bArr, 0, min);
                i10 -= min;
            } catch (EOFException unused) {
                throw new IOException("patch underrun");
            }
        }
    }
}
