package com.google.ads.interactivemedia.v3.internal;

import com.google.android.gms.measurement.internal.u1;
import java.io.IOException;
import java.math.RoundingMode;
import java.util.Objects;
import javax.annotation.CheckForNull;

/* JADX INFO: loaded from: classes2.dex */
class zzsy extends zzsz {

    @CheckForNull
    private volatile zzsz zza;
    final zzsu zzb;

    @CheckForNull
    final Character zzc;

    public zzsy(zzsu zzsuVar, @CheckForNull Character ch) {
        this.zzb = zzsuVar;
        boolean z10 = true;
        if (ch != null) {
            ch.charValue();
            if (zzsuVar.zze('=')) {
                z10 = false;
            }
        }
        zzqh.zzg(z10, "Padding character %s was already in alphabet", ch);
        this.zzc = ch;
    }

    public final boolean equals(@CheckForNull Object obj) {
        if (obj instanceof zzsy) {
            zzsy zzsyVar = (zzsy) obj;
            if (this.zzb.equals(zzsyVar.zzb) && Objects.equals(this.zzc, zzsyVar.zzc)) {
                return true;
            }
        }
        return false;
    }

    public final int hashCode() {
        Character ch = this.zzc;
        return Objects.hashCode(ch) ^ this.zzb.hashCode();
    }

    public final String toString() {
        StringBuilder sb = new StringBuilder("BaseEncoding.");
        sb.append(this.zzb);
        if (8 % this.zzb.zzb != 0) {
            if (this.zzc == null) {
                sb.append(".omitPadding()");
            } else {
                sb.append(".withPadChar('");
                sb.append(this.zzc);
                sb.append("')");
            }
        }
        return sb.toString();
    }

    @Override // com.google.ads.interactivemedia.v3.internal.zzsz
    public int zza(byte[] bArr, CharSequence charSequence) throws zzsx {
        zzsu zzsuVar;
        CharSequence charSequenceZzg = zzg(charSequence);
        if (!this.zzb.zzd(charSequenceZzg.length())) {
            throw new zzsx(u1.g("Invalid input length ", charSequenceZzg.length()));
        }
        int i10 = 0;
        int i11 = 0;
        while (i10 < charSequenceZzg.length()) {
            long jZzb = 0;
            int i12 = 0;
            int i13 = 0;
            while (true) {
                zzsuVar = this.zzb;
                if (i12 >= zzsuVar.zzc) {
                    break;
                }
                jZzb <<= zzsuVar.zzb;
                if (i10 + i12 < charSequenceZzg.length()) {
                    jZzb |= (long) this.zzb.zzb(charSequenceZzg.charAt(i13 + i10));
                    i13++;
                }
                i12++;
            }
            int i14 = zzsuVar.zzd;
            int i15 = i13 * zzsuVar.zzb;
            int i16 = (i14 - 1) * 8;
            while (i16 >= (i14 * 8) - i15) {
                bArr[i11] = (byte) ((jZzb >>> i16) & 255);
                i16 -= 8;
                i11++;
            }
            i10 += this.zzb.zzc;
        }
        return i11;
    }

    public zzsz zzb(zzsu zzsuVar, @CheckForNull Character ch) {
        return new zzsy(zzsuVar, ch);
    }

    @Override // com.google.ads.interactivemedia.v3.internal.zzsz
    public void zzc(Appendable appendable, byte[] bArr, int i10, int i11) throws IOException {
        int i12 = 0;
        zzqh.zzh(0, i11, bArr.length);
        while (i12 < i11) {
            zzh(appendable, bArr, i12, Math.min(this.zzb.zzd, i11 - i12));
            i12 += this.zzb.zzd;
        }
    }

    @Override // com.google.ads.interactivemedia.v3.internal.zzsz
    public final int zzd(int i10) {
        return (int) (((((long) this.zzb.zzb) * ((long) i10)) + 7) / 8);
    }

    @Override // com.google.ads.interactivemedia.v3.internal.zzsz
    public final int zze(int i10) {
        zzsu zzsuVar = this.zzb;
        return zzsuVar.zzc * zztd.zza(i10, zzsuVar.zzd, RoundingMode.CEILING);
    }

    @Override // com.google.ads.interactivemedia.v3.internal.zzsz
    public final zzsz zzf() {
        zzsz zzszVarZzb = this.zza;
        if (zzszVarZzb == null) {
            zzsu zzsuVar = this.zzb;
            zzsu zzsuVarZzc = zzsuVar.zzc();
            zzszVarZzb = zzsuVarZzc == zzsuVar ? this : zzb(zzsuVarZzc, this.zzc);
            this.zza = zzszVarZzb;
        }
        return zzszVarZzb;
    }

    @Override // com.google.ads.interactivemedia.v3.internal.zzsz
    public final CharSequence zzg(CharSequence charSequence) {
        charSequence.getClass();
        Character ch = this.zzc;
        if (ch == null) {
            return charSequence;
        }
        ch.charValue();
        int length = charSequence.length();
        do {
            length--;
            if (length < 0) {
                break;
            }
        } while (charSequence.charAt(length) == '=');
        return charSequence.subSequence(0, length + 1);
    }

    public final void zzh(Appendable appendable, byte[] bArr, int i10, int i11) throws IOException {
        zzqh.zzh(i10, i10 + i11, bArr.length);
        int i12 = 0;
        zzqh.zzd(i11 <= this.zzb.zzd);
        long j10 = 0;
        for (int i13 = 0; i13 < i11; i13++) {
            j10 = (j10 | ((long) (bArr[i10 + i13] & 255))) << 8;
        }
        int i14 = (i11 + 1) * 8;
        zzsu zzsuVar = this.zzb;
        while (i12 < i11 * 8) {
            long j11 = j10 >>> ((i14 - zzsuVar.zzb) - i12);
            zzsu zzsuVar2 = this.zzb;
            appendable.append(zzsuVar2.zza(((int) j11) & zzsuVar2.zza));
            i12 += this.zzb.zzb;
        }
        if (this.zzc != null) {
            while (i12 < this.zzb.zzd * 8) {
                this.zzc.charValue();
                appendable.append('=');
                i12 += this.zzb.zzb;
            }
        }
    }

    public zzsy(String str, String str2, @CheckForNull Character ch) {
        this(new zzsu(str, str2.toCharArray()), ch);
    }
}
