package com.google.android.gms.internal.clearcut; import com.kofax.mobile.sdk._internal.impl.camera.h; import java.io.IOException; import java.nio.BufferOverflowException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.ReadOnlyBufferException; /* loaded from: classes2.dex */ public final class zzfs { private final ByteBuffer zzgd; private zzbn zzrh; private int zzri; public static long zzj(long j) { return (j << 1) ^ (j >> 63); } public static int zzo(long j) { if (((-128) & j) == 0) { return 1; } if (((-16384) & j) == 0) { return 2; } if (((-2097152) & j) == 0) { return 3; } if (((-268435456) & j) == 0) { return 4; } if (((-34359738368L) & j) == 0) { return 5; } if (((-4398046511104L) & j) == 0) { return 6; } if (((-562949953421312L) & j) == 0) { return 7; } if (((-72057594037927936L) & j) == 0) { return 8; } return (j & Long.MIN_VALUE) == 0 ? 9 : 10; } private static int zzz(int i) { if ((i & (-128)) == 0) { return 1; } if ((i & (-16384)) == 0) { return 2; } if (((-2097152) & i) == 0) { return 3; } return (i & (-268435456)) == 0 ? 4 : 5; } public final void zzn(long j) throws IOException { while (((-128) & j) != 0) { zzao((((int) j) & 127) | 128); j >>>= 7; } zzao((int) j); } public final void zzi(int i, long j) throws IOException { zzb(i, 0); zzn(j); } public final void zzem() { if (this.zzgd.remaining() != 0) { throw new IllegalStateException(String.format("Did not write as much data as expected, %s bytes remaining.", Integer.valueOf(this.zzgd.remaining()))); } } public final void zze(int i, zzdo zzdoVar) throws IOException { if (this.zzrh != null) { if (this.zzri != this.zzgd.position()) { this.zzrh.write(this.zzgd.array(), this.zzri, this.zzgd.position() - this.zzri); } zzbn zzbnVar = this.zzrh; zzbnVar.zza(i, zzdoVar); zzbnVar.flush(); this.zzri = this.zzgd.position(); } this.zzrh = zzbn.zza(this.zzgd); this.zzri = this.zzgd.position(); zzbn zzbnVar2 = this.zzrh; zzbnVar2.zza(i, zzdoVar); zzbnVar2.flush(); this.zzri = this.zzgd.position(); } public final void zzc(int i, int i2) throws IOException { zzb(i, 0); if (i2 >= 0) { zzap(i2); } else { zzn(i2); } } public final void zzb(int i, boolean z) throws IOException { zzb(25, 0); byte b = z ? (byte) 1 : (byte) 0; if (!this.zzgd.hasRemaining()) { throw new zzft(this.zzgd.position(), this.zzgd.limit()); } this.zzgd.put(b); } public final void zzb(int i, int i2) throws IOException { zzap((i << 3) | i2); } public final void zza(int i, byte[] bArr) throws IOException { zzb(i, 2); zzap(bArr.length); int length = bArr.length; if (this.zzgd.remaining() < length) { throw new zzft(this.zzgd.position(), this.zzgd.limit()); } this.zzgd.put(bArr, 0, length); } public final void zza(int i, String str) throws IOException { zzb(i, 2); try { int zzz = zzz(str.length()); if (zzz != zzz(str.length() * 3)) { zzap(zza(str)); zzd(str, this.zzgd); return; } int position = this.zzgd.position(); if (this.zzgd.remaining() < zzz) { throw new zzft(position + zzz, this.zzgd.limit()); } this.zzgd.position(position + zzz); zzd(str, this.zzgd); int position2 = this.zzgd.position(); this.zzgd.position(position); zzap((position2 - position) - zzz); this.zzgd.position(position2); } catch (BufferOverflowException e) { zzft zzftVar = new zzft(this.zzgd.position(), this.zzgd.limit()); zzftVar.initCause(e); throw zzftVar; } } public final void zza(int i, zzfz zzfzVar) throws IOException { zzb(i, 2); if (zzfzVar.zzrs < 0) { zzfzVar.zzas(); } zzap(zzfzVar.zzrs); zzfzVar.zza(this); } public static int zzs(int i) { if (i >= 0) { return zzz(i); } return 10; } public static int zzr(int i) { return zzz(i << 3); } public static zzfs zzh(byte[] bArr, int i, int i2) { return new zzfs(bArr, 0, i2); } public static int zzh(byte[] bArr) { return zzz(bArr.length) + bArr.length; } public static int zzh(String str) { int zza = zza(str); return zzz(zza) + zza; } public static zzfs zzg(byte[] bArr) { return zzh(bArr, 0, bArr.length); } /* JADX WARN: Multi-variable type inference failed */ /* JADX WARN: Type inference failed for: r8v29 */ private static void zzd(CharSequence charSequence, ByteBuffer byteBuffer) { int i; int i2; char charAt; int i3; if (byteBuffer.isReadOnly()) { throw new ReadOnlyBufferException(); } int i4 = 0; if (!byteBuffer.hasArray()) { int length = charSequence.length(); while (i4 < length) { char charAt2 = charSequence.charAt(i4); char c = charAt2; if (charAt2 >= 128) { if (charAt2 < 2048) { i3 = (charAt2 >>> 6) | 960; } else { if (charAt2 >= 55296 && 57343 >= charAt2) { int i5 = i4 + 1; if (i5 != charSequence.length()) { char charAt3 = charSequence.charAt(i5); if (Character.isSurrogatePair(charAt2, charAt3)) { int codePoint = Character.toCodePoint(charAt2, charAt3); byteBuffer.put((byte) ((codePoint >>> 18) | 240)); byteBuffer.put((byte) (((codePoint >>> 12) & 63) | 128)); byteBuffer.put((byte) (((codePoint >>> 6) & 63) | 128)); byteBuffer.put((byte) ((codePoint & 63) | 128)); i4 = i5; i4++; } else { i4 = i5; } } StringBuilder sb = new StringBuilder(39); sb.append("Unpaired surrogate at index "); sb.append(i4 - 1); throw new IllegalArgumentException(sb.toString()); } byteBuffer.put((byte) ((charAt2 >>> '\f') | h.Az)); i3 = ((charAt2 >>> 6) & 63) | 128; } byteBuffer.put((byte) i3); c = (charAt2 & '?') | 128; } byteBuffer.put((byte) c); i4++; } return; } try { byte[] array = byteBuffer.array(); int arrayOffset = byteBuffer.arrayOffset() + byteBuffer.position(); int remaining = byteBuffer.remaining(); int length2 = charSequence.length(); int i6 = remaining + arrayOffset; while (i4 < length2) { int i7 = i4 + arrayOffset; if (i7 >= i6 || (charAt = charSequence.charAt(i4)) >= 128) { break; } array[i7] = (byte) charAt; i4++; } if (i4 == length2) { i = arrayOffset + length2; } else { i = arrayOffset + i4; while (i4 < length2) { char charAt4 = charSequence.charAt(i4); if (charAt4 >= 128 || i >= i6) { if (charAt4 < 2048 && i <= i6 - 2) { array[i] = (byte) ((charAt4 >>> 6) | 960); i2 = i + 2; array[i + 1] = (byte) ((charAt4 & '?') | 128); } else { if ((charAt4 >= 55296 && 57343 >= charAt4) || i > i6 - 3) { if (i > i6 - 4) { StringBuilder sb2 = new StringBuilder(37); sb2.append("Failed writing "); sb2.append(charAt4); sb2.append(" at index "); sb2.append(i); throw new ArrayIndexOutOfBoundsException(sb2.toString()); } int i8 = i4 + 1; if (i8 != charSequence.length()) { char charAt5 = charSequence.charAt(i8); if (Character.isSurrogatePair(charAt4, charAt5)) { int codePoint2 = Character.toCodePoint(charAt4, charAt5); array[i] = (byte) ((codePoint2 >>> 18) | 240); array[i + 1] = (byte) (((codePoint2 >>> 12) & 63) | 128); array[i + 2] = (byte) (((codePoint2 >>> 6) & 63) | 128); array[i + 3] = (byte) ((codePoint2 & 63) | 128); i += 4; i4 = i8; } else { i4 = i8; } } StringBuilder sb3 = new StringBuilder(39); sb3.append("Unpaired surrogate at index "); sb3.append(i4 - 1); throw new IllegalArgumentException(sb3.toString()); } array[i] = (byte) ((charAt4 >>> '\f') | h.Az); array[i + 1] = (byte) (((charAt4 >>> 6) & 63) | 128); i2 = i + 3; array[i + 2] = (byte) ((charAt4 & '?') | 128); } i = i2; } else { array[i] = (byte) charAt4; i++; } i4++; } } byteBuffer.position(i - byteBuffer.arrayOffset()); } catch (ArrayIndexOutOfBoundsException e) { BufferOverflowException bufferOverflowException = new BufferOverflowException(); bufferOverflowException.initCause(e); throw bufferOverflowException; } } public static int zzd(int i, long j) { return zzr(i) + zzo(j); } public static int zzb(int i, byte[] bArr) { return zzr(i) + zzh(bArr); } public static int zzb(int i, String str) { return zzr(i) + zzh(str); } public static int zzb(int i, zzfz zzfzVar) { int zzr = zzr(i); int zzas = zzfzVar.zzas(); return zzr + zzz(zzas) + zzas; } private final void zzap(int i) throws IOException { while ((i & (-128)) != 0) { zzao((i & 127) | 128); i >>>= 7; } zzao(i); } private final void zzao(int i) throws IOException { byte b = (byte) i; if (!this.zzgd.hasRemaining()) { throw new zzft(this.zzgd.position(), this.zzgd.limit()); } this.zzgd.put(b); } private static int zza(CharSequence charSequence) { int length = charSequence.length(); int i = 0; int i2 = 0; while (i2 < length && charSequence.charAt(i2) < 128) { i2++; } int i3 = length; while (true) { if (i2 >= length) { break; } char charAt = charSequence.charAt(i2); if (charAt < 2048) { i3 += (127 - charAt) >>> 31; i2++; } else { int length2 = charSequence.length(); while (i2 < length2) { char charAt2 = charSequence.charAt(i2); if (charAt2 < 2048) { i += (127 - charAt2) >>> 31; } else { i += 2; if (55296 <= charAt2 && charAt2 <= 57343) { if (Character.codePointAt(charSequence, i2) < 65536) { StringBuilder sb = new StringBuilder(39); sb.append("Unpaired surrogate at index "); sb.append(i2); throw new IllegalArgumentException(sb.toString()); } i2++; } } i2++; } i3 += i; } } if (i3 >= length) { return i3; } StringBuilder sb2 = new StringBuilder(54); sb2.append("UTF-8 length does not fit in int: "); sb2.append(i3 + 4294967296L); throw new IllegalArgumentException(sb2.toString()); } private zzfs(byte[] bArr, int i, int i2) { this(ByteBuffer.wrap(bArr, i, i2)); } private zzfs(ByteBuffer byteBuffer) { this.zzgd = byteBuffer; byteBuffer.order(ByteOrder.LITTLE_ENDIAN); } }