package androidx.media3.datasource;

import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util;
import com.google.firebase.sessions.settings.RemoteSettings;
import java.nio.ByteBuffer;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/* JADX INFO: loaded from: classes.dex */
@UnstableApi
public final class AesFlushingCipher {
    private final int blockSize;
    private final Cipher cipher;
    private final byte[] flushedBlock;
    private int pendingXorBytes;
    private final byte[] zerosBlock;

    public AesFlushingCipher(int i6, byte[] bArr, String str, long j4) {
        this(i6, bArr, getFNV64Hash(str), j4);
    }

    private static long getFNV64Hash(String str) {
        long j4 = 0;
        if (str == null) {
            return 0L;
        }
        for (int i6 = 0; i6 < str.length(); i6++) {
            long jCharAt = j4 ^ ((long) str.charAt(i6));
            j4 = jCharAt + (jCharAt << 1) + (jCharAt << 4) + (jCharAt << 5) + (jCharAt << 7) + (jCharAt << 8) + (jCharAt << 40);
        }
        return j4;
    }

    private byte[] getInitializationVector(long j4, long j6) {
        return ByteBuffer.allocate(16).putLong(j4).putLong(j6).array();
    }

    private int nonFlushingUpdate(byte[] bArr, int i6, int i7, byte[] bArr2, int i8) {
        try {
            return this.cipher.update(bArr, i6, i7, bArr2, i8);
        } catch (ShortBufferException e6) {
            throw new RuntimeException(e6);
        }
    }

    public void update(byte[] bArr, int i6, int i7, byte[] bArr2, int i8) {
        int i9 = i6;
        int i10 = i7;
        int i11 = i8;
        do {
            int i12 = this.pendingXorBytes;
            if (i12 <= 0) {
                int iNonFlushingUpdate = nonFlushingUpdate(bArr, i9, i10, bArr2, i11);
                if (i10 == iNonFlushingUpdate) {
                    return;
                }
                int i13 = i10 - iNonFlushingUpdate;
                int i14 = 0;
                Assertions.checkState(i13 < this.blockSize);
                int i15 = i11 + iNonFlushingUpdate;
                int i16 = this.blockSize - i13;
                this.pendingXorBytes = i16;
                Assertions.checkState(nonFlushingUpdate(this.zerosBlock, 0, i16, this.flushedBlock, 0) == this.blockSize);
                while (i14 < i13) {
                    bArr2[i15] = this.flushedBlock[i14];
                    i14++;
                    i15++;
                }
                return;
            }
            bArr2[i11] = (byte) (bArr[i9] ^ this.flushedBlock[this.blockSize - i12]);
            i11++;
            i9++;
            this.pendingXorBytes = i12 - 1;
            i10--;
        } while (i10 != 0);
    }

    public void updateInPlace(byte[] bArr, int i6, int i7) {
        update(bArr, i6, i7, bArr, i6);
    }

    public AesFlushingCipher(int i6, byte[] bArr, long j4, long j6) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
            this.cipher = cipher;
            int blockSize = cipher.getBlockSize();
            this.blockSize = blockSize;
            this.zerosBlock = new byte[blockSize];
            this.flushedBlock = new byte[blockSize];
            long j7 = j6 / ((long) blockSize);
            int i7 = (int) (j6 % ((long) blockSize));
            cipher.init(i6, new SecretKeySpec(bArr, Util.splitAtFirst(cipher.getAlgorithm(), RemoteSettings.FORWARD_SLASH_STRING)[0]), new IvParameterSpec(getInitializationVector(j4, j7)));
            if (i7 != 0) {
                updateInPlace(new byte[i7], 0, i7);
            }
        } catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e6) {
            throw new RuntimeException(e6);
        }
    }
}
