package o6; import android.util.Log; import e6.f; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.Charset; /* loaded from: classes2.dex */ public final class k implements e6.f { /* renamed from: a, reason: collision with root package name */ static final byte[] f12734a = "Exif\u0000\u0000".getBytes(Charset.forName("UTF-8")); /* renamed from: b, reason: collision with root package name */ private static final int[] f12735b = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8}; private interface a { /* renamed from: o6.k$a$a, reason: collision with other inner class name */ public static final class C0203a extends IOException { C0203a() { super("Unexpectedly reached end of a file"); } } int a(byte[] bArr, int i10); short a(); int b(); long skip(long j10); } private static final class b implements a { /* renamed from: a, reason: collision with root package name */ private final ByteBuffer f12736a; b(ByteBuffer byteBuffer) { this.f12736a = byteBuffer; byteBuffer.order(ByteOrder.BIG_ENDIAN); } @Override // o6.k.a public int a(byte[] bArr, int i10) { int min = Math.min(i10, this.f12736a.remaining()); if (min == 0) { return -1; } this.f12736a.get(bArr, 0, min); return min; } @Override // o6.k.a public int b() { return (a() << 8) | a(); } @Override // o6.k.a public long skip(long j10) { int min = (int) Math.min(this.f12736a.remaining(), j10); ByteBuffer byteBuffer = this.f12736a; byteBuffer.position(byteBuffer.position() + min); return min; } @Override // o6.k.a public short a() { if (this.f12736a.remaining() >= 1) { return (short) (this.f12736a.get() & 255); } throw new a.C0203a(); } } private static final class c { /* renamed from: a, reason: collision with root package name */ private final ByteBuffer f12737a; c(byte[] bArr, int i10) { this.f12737a = (ByteBuffer) ByteBuffer.wrap(bArr).order(ByteOrder.BIG_ENDIAN).limit(i10); } private boolean d(int i10, int i11) { return this.f12737a.remaining() - i10 >= i11; } int a() { return this.f12737a.remaining(); } short b(int i10) { if (d(i10, 2)) { return this.f12737a.getShort(i10); } return (short) -1; } void c(ByteOrder byteOrder) { this.f12737a.order(byteOrder); } int e(int i10) { if (d(i10, 4)) { return this.f12737a.getInt(i10); } return -1; } } private static final class d implements a { /* renamed from: a, reason: collision with root package name */ private final InputStream f12738a; d(InputStream inputStream) { this.f12738a = inputStream; } @Override // o6.k.a public int a(byte[] bArr, int i10) { int i11 = 0; int i12 = 0; while (i11 < i10 && (i12 = this.f12738a.read(bArr, i11, i10 - i11)) != -1) { i11 += i12; } if (i11 == 0 && i12 == -1) { throw new a.C0203a(); } return i11; } @Override // o6.k.a public int b() { return (a() << 8) | a(); } @Override // o6.k.a public long skip(long j10) { if (j10 < 0) { return 0L; } long j11 = j10; while (j11 > 0) { long skip = this.f12738a.skip(j11); if (skip <= 0) { if (this.f12738a.read() == -1) { break; } skip = 1; } j11 -= skip; } return j10 - j11; } @Override // o6.k.a public short a() { int read = this.f12738a.read(); if (read != -1) { return (short) read; } throw new a.C0203a(); } } private static int e(int i10, int i11) { return i10 + 2 + (i11 * 12); } private int f(a aVar, i6.a aVar2) { try { int b10 = aVar.b(); if (!j(b10)) { if (Log.isLoggable("DfltImageHeaderParser", 3)) { Log.d("DfltImageHeaderParser", "Parser doesn't handle magic number: " + b10); } return -1; } int l10 = l(aVar); if (l10 == -1) { if (Log.isLoggable("DfltImageHeaderParser", 3)) { Log.d("DfltImageHeaderParser", "Failed to parse exif segment length, or exif segment not found"); } return -1; } byte[] bArr = (byte[]) aVar2.e(l10, byte[].class); try { return g(aVar, bArr, l10); } finally { aVar2.d(bArr); } } catch (a.C0203a unused) { return -1; } } private int g(a aVar, byte[] bArr, int i10) { int a10 = aVar.a(bArr, i10); if (a10 == i10) { if (k(bArr, i10)) { return h(new c(bArr, i10)); } if (Log.isLoggable("DfltImageHeaderParser", 3)) { Log.d("DfltImageHeaderParser", "Missing jpeg exif preamble"); } return -1; } if (Log.isLoggable("DfltImageHeaderParser", 3)) { Log.d("DfltImageHeaderParser", "Unable to read exif segment data, length: " + i10 + ", actually read: " + a10); } return -1; } private static int h(c cVar) { ByteOrder byteOrder; StringBuilder sb; String str; String sb2; short b10 = cVar.b(6); if (b10 != 18761) { if (b10 != 19789 && Log.isLoggable("DfltImageHeaderParser", 3)) { Log.d("DfltImageHeaderParser", "Unknown endianness = " + ((int) b10)); } byteOrder = ByteOrder.BIG_ENDIAN; } else { byteOrder = ByteOrder.LITTLE_ENDIAN; } cVar.c(byteOrder); int e10 = cVar.e(10) + 6; short b11 = cVar.b(e10); for (int i10 = 0; i10 < b11; i10++) { int e11 = e(e10, i10); short b12 = cVar.b(e11); if (b12 == 274) { short b13 = cVar.b(e11 + 2); if (b13 >= 1 && b13 <= 12) { int e12 = cVar.e(e11 + 4); if (e12 >= 0) { if (Log.isLoggable("DfltImageHeaderParser", 3)) { Log.d("DfltImageHeaderParser", "Got tagIndex=" + i10 + " tagType=" + ((int) b12) + " formatCode=" + ((int) b13) + " componentCount=" + e12); } int i11 = e12 + f12735b[b13]; if (i11 <= 4) { int i12 = e11 + 8; if (i12 >= 0 && i12 <= cVar.a()) { if (i11 >= 0 && i11 + i12 <= cVar.a()) { return cVar.b(i12); } if (Log.isLoggable("DfltImageHeaderParser", 3)) { sb = new StringBuilder(); sb.append("Illegal number of bytes for TI tag data tagType="); sb.append((int) b12); } } else if (Log.isLoggable("DfltImageHeaderParser", 3)) { sb2 = "Illegal tagValueOffset=" + i12 + " tagType=" + ((int) b12); Log.d("DfltImageHeaderParser", sb2); } } else if (Log.isLoggable("DfltImageHeaderParser", 3)) { sb = new StringBuilder(); str = "Got byte count > 4, not orientation, continuing, formatCode="; sb.append(str); sb.append((int) b13); } } else if (Log.isLoggable("DfltImageHeaderParser", 3)) { sb2 = "Negative tiff component count"; Log.d("DfltImageHeaderParser", sb2); } } else if (Log.isLoggable("DfltImageHeaderParser", 3)) { sb = new StringBuilder(); str = "Got invalid format code = "; sb.append(str); sb.append((int) b13); } sb2 = sb.toString(); Log.d("DfltImageHeaderParser", sb2); } } return -1; } private f.a i(a aVar) { try { int b10 = aVar.b(); if (b10 == 65496) { return f.a.JPEG; } int a10 = (b10 << 8) | aVar.a(); if (a10 == 4671814) { return f.a.GIF; } int a11 = (a10 << 8) | aVar.a(); if (a11 == -1991225785) { aVar.skip(21L); try { return aVar.a() >= 3 ? f.a.PNG_A : f.a.PNG; } catch (a.C0203a unused) { return f.a.PNG; } } if (a11 != 1380533830) { return f.a.UNKNOWN; } aVar.skip(4L); if (((aVar.b() << 16) | aVar.b()) != 1464156752) { return f.a.UNKNOWN; } int b11 = (aVar.b() << 16) | aVar.b(); if ((b11 & (-256)) != 1448097792) { return f.a.UNKNOWN; } int i10 = b11 & 255; if (i10 == 88) { aVar.skip(4L); return (aVar.a() & 16) != 0 ? f.a.WEBP_A : f.a.WEBP; } if (i10 != 76) { return f.a.WEBP; } aVar.skip(4L); return (aVar.a() & 8) != 0 ? f.a.WEBP_A : f.a.WEBP; } catch (a.C0203a unused2) { return f.a.UNKNOWN; } } private static boolean j(int i10) { return (i10 & 65496) == 65496 || i10 == 19789 || i10 == 18761; } private boolean k(byte[] bArr, int i10) { boolean z10 = bArr != null && i10 > f12734a.length; if (z10) { int i11 = 0; while (true) { byte[] bArr2 = f12734a; if (i11 >= bArr2.length) { break; } if (bArr[i11] != bArr2[i11]) { return false; } i11++; } } return z10; } private int l(a aVar) { short a10; int b10; long j10; long skip; do { short a11 = aVar.a(); if (a11 != 255) { if (Log.isLoggable("DfltImageHeaderParser", 3)) { Log.d("DfltImageHeaderParser", "Unknown segmentId=" + ((int) a11)); } return -1; } a10 = aVar.a(); if (a10 == 218) { return -1; } if (a10 == 217) { if (Log.isLoggable("DfltImageHeaderParser", 3)) { Log.d("DfltImageHeaderParser", "Found MARKER_EOI in exif segment"); } return -1; } b10 = aVar.b() - 2; if (a10 == 225) { return b10; } j10 = b10; skip = aVar.skip(j10); } while (skip == j10); if (Log.isLoggable("DfltImageHeaderParser", 3)) { Log.d("DfltImageHeaderParser", "Unable to skip enough data, type: " + ((int) a10) + ", wanted to skip: " + b10 + ", but actually skipped: " + skip); } return -1; } @Override // e6.f public f.a a(ByteBuffer byteBuffer) { return i(new b((ByteBuffer) b7.j.d(byteBuffer))); } @Override // e6.f public f.a b(InputStream inputStream) { return i(new d((InputStream) b7.j.d(inputStream))); } @Override // e6.f public int c(ByteBuffer byteBuffer, i6.a aVar) { return f(new b((ByteBuffer) b7.j.d(byteBuffer)), (i6.a) b7.j.d(aVar)); } @Override // e6.f public int d(InputStream inputStream, i6.a aVar) { return f(new d((InputStream) b7.j.d(inputStream)), (i6.a) b7.j.d(aVar)); } }