package com.bumptech.glide.load.resource.bitmap;

import android.util.Log;
import com.bumptech.glide.load.ImageHeaderParser;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import p000.p001.p002.p003.p004.C0567;
import p138.p139.p143.p144.C2803;
import p138.p145.p146.p151.p154.p155.InterfaceC2940;

/* JADX INFO: loaded from: classes.dex */
public final class DefaultImageHeaderParser implements ImageHeaderParser {

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static final byte[] f1526 = "Exif\u0000\u0000".getBytes(Charset.forName("UTF-8"));

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public static final int[] f1527 = {0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8};

    public interface Reader {

        public static final class EndOfFileException extends IOException {
            public EndOfFileException() {
                super("Unexpectedly reached end of a file");
            }
        }

        long skip(long j);

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        int mo1047(byte[] bArr, int i);

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        short mo1048();

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        int mo1049();
    }

    /* JADX INFO: renamed from: com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser$֏, reason: contains not printable characters */
    public static final class C0265 implements Reader {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final ByteBuffer f1528;

        public C0265(ByteBuffer byteBuffer) {
            this.f1528 = byteBuffer;
            byteBuffer.order(ByteOrder.BIG_ENDIAN);
        }

        @Override // com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser.Reader
        public long skip(long j) {
            int iMin = (int) Math.min(this.f1528.remaining(), j);
            ByteBuffer byteBuffer = this.f1528;
            byteBuffer.position(byteBuffer.position() + iMin);
            return iMin;
        }

        @Override // com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser.Reader
        /* JADX INFO: renamed from: ֏ */
        public int mo1047(byte[] bArr, int i) {
            int iMin = Math.min(i, this.f1528.remaining());
            if (iMin == 0) {
                return -1;
            }
            this.f1528.get(bArr, 0, iMin);
            return iMin;
        }

        @Override // com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser.Reader
        /* JADX INFO: renamed from: ֏ */
        public short mo1048() throws Reader.EndOfFileException {
            if (this.f1528.remaining() >= 1) {
                return (short) (this.f1528.get() & 255);
            }
            throw new Reader.EndOfFileException();
        }

        @Override // com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser.Reader
        /* JADX INFO: renamed from: ؠ */
        public int mo1049() {
            return (mo1048() << 8) | mo1048();
        }
    }

    /* JADX INFO: renamed from: com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser$ؠ, reason: contains not printable characters */
    public static final class C0266 {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final ByteBuffer f1529;

        public C0266(byte[] bArr, int i) {
            this.f1529 = (ByteBuffer) ByteBuffer.wrap(bArr).order(ByteOrder.BIG_ENDIAN).limit(i);
        }

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public short m1050(int i) {
            if (this.f1529.remaining() - i >= 2) {
                return this.f1529.getShort(i);
            }
            return (short) -1;
        }

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public int m1051(int i) {
            if (this.f1529.remaining() - i >= 4) {
                return this.f1529.getInt(i);
            }
            return -1;
        }
    }

    /* JADX INFO: renamed from: com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser$ހ, reason: contains not printable characters */
    public static final class C0267 implements Reader {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final InputStream f1530;

        public C0267(InputStream inputStream) {
            this.f1530 = inputStream;
        }

        @Override // com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser.Reader
        public long skip(long j) throws IOException {
            if (j < 0) {
                return 0L;
            }
            long j2 = j;
            while (j2 > 0) {
                long jSkip = this.f1530.skip(j2);
                if (jSkip <= 0) {
                    if (this.f1530.read() == -1) {
                        break;
                    }
                    jSkip = 1;
                }
                j2 -= jSkip;
            }
            return j - j2;
        }

        @Override // com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser.Reader
        /* JADX INFO: renamed from: ֏ */
        public int mo1047(byte[] bArr, int i) throws Reader.EndOfFileException {
            int i2 = 0;
            int i3 = 0;
            while (i2 < i && (i3 = this.f1530.read(bArr, i2, i - i2)) != -1) {
                i2 += i3;
            }
            if (i2 == 0 && i3 == -1) {
                throw new Reader.EndOfFileException();
            }
            return i2;
        }

        @Override // com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser.Reader
        /* JADX INFO: renamed from: ֏ */
        public short mo1048() throws IOException {
            int i = this.f1530.read();
            if (i != -1) {
                return (short) i;
            }
            throw new Reader.EndOfFileException();
        }

        @Override // com.bumptech.glide.load.resource.bitmap.DefaultImageHeaderParser.Reader
        /* JADX INFO: renamed from: ؠ */
        public int mo1049() {
            return (mo1048() << 8) | mo1048();
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final int m1044(Reader reader, byte[] bArr, int i) {
        ByteOrder byteOrder;
        int iMo1047 = reader.mo1047(bArr, i);
        if (iMo1047 != i) {
            if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                Log.d("DfltImageHeaderParser", "Unable to read exif segment data, length: " + i + ", actually read: " + iMo1047);
            }
            return -1;
        }
        boolean z = bArr != null && i > f1526.length;
        if (z) {
            int i2 = 0;
            while (true) {
                byte[] bArr2 = f1526;
                if (i2 >= bArr2.length) {
                    break;
                }
                if (bArr[i2] != bArr2[i2]) {
                    z = false;
                    break;
                }
                i2++;
            }
        }
        if (!z) {
            if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                Log.d("DfltImageHeaderParser", "Missing jpeg exif preamble");
            }
            return -1;
        }
        C0266 c0266 = new C0266(bArr, i);
        short sM1050 = c0266.m1050(6);
        if (sM1050 != 18761) {
            if (sM1050 != 19789 && Log.isLoggable("DfltImageHeaderParser", 3)) {
                Log.d("DfltImageHeaderParser", "Unknown endianness = " + ((int) sM1050));
            }
            byteOrder = ByteOrder.BIG_ENDIAN;
        } else {
            byteOrder = ByteOrder.LITTLE_ENDIAN;
        }
        c0266.f1529.order(byteOrder);
        int iM1051 = c0266.m1051(10) + 6;
        short sM10502 = c0266.m1050(iM1051);
        for (int i3 = 0; i3 < sM10502; i3++) {
            int i4 = (i3 * 12) + iM1051 + 2;
            short sM10503 = c0266.m1050(i4);
            if (sM10503 == 274) {
                short sM10504 = c0266.m1050(i4 + 2);
                if (sM10504 >= 1 && sM10504 <= 12) {
                    int iM10512 = c0266.m1051(i4 + 4);
                    if (iM10512 >= 0) {
                        if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                            StringBuilder sbM4890 = C2803.m4890("Got tagIndex=", i3, " tagType=", sM10503, " formatCode=");
                            sbM4890.append((int) sM10504);
                            sbM4890.append(" componentCount=");
                            sbM4890.append(iM10512);
                            Log.d("DfltImageHeaderParser", sbM4890.toString());
                        }
                        int i5 = iM10512 + f1527[sM10504];
                        if (i5 <= 4) {
                            int i6 = i4 + 8;
                            if (i6 >= 0 && i6 <= c0266.f1529.remaining()) {
                                if (i5 >= 0 && i5 + i6 <= c0266.f1529.remaining()) {
                                    return c0266.m1050(i6);
                                }
                                if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                                    Log.d("DfltImageHeaderParser", "Illegal number of bytes for TI tag data tagType=" + ((int) sM10503));
                                }
                            } else if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                                Log.d("DfltImageHeaderParser", "Illegal tagValueOffset=" + i6 + " tagType=" + ((int) sM10503));
                            }
                        } else if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                            Log.d("DfltImageHeaderParser", "Got byte count > 4, not orientation, continuing, formatCode=" + ((int) sM10504));
                        }
                    } else if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                        Log.d("DfltImageHeaderParser", "Negative tiff component count");
                    }
                } else if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                    Log.d("DfltImageHeaderParser", "Got invalid format code = " + ((int) sM10504));
                }
            }
        }
        return -1;
    }

    @Override // com.bumptech.glide.load.ImageHeaderParser
    /* JADX INFO: renamed from: ֏ */
    public int mo1036(InputStream inputStream, InterfaceC2940 interfaceC2940) {
        C0567.m1858(inputStream, "Argument must not be null");
        C0267 c0267 = new C0267(inputStream);
        C0567.m1858(interfaceC2940, "Argument must not be null");
        try {
            int iMo1049 = c0267.mo1049();
            if (!((iMo1049 & 65496) == 65496 || iMo1049 == 19789 || iMo1049 == 18761)) {
                if (!Log.isLoggable("DfltImageHeaderParser", 3)) {
                    return -1;
                }
                Log.d("DfltImageHeaderParser", "Parser doesn't handle magic number: " + iMo1049);
                return -1;
            }
            int iM1046 = m1046(c0267);
            if (iM1046 == -1) {
                if (!Log.isLoggable("DfltImageHeaderParser", 3)) {
                    return -1;
                }
                Log.d("DfltImageHeaderParser", "Failed to parse exif segment length, or exif segment not found");
                return -1;
            }
            byte[] bArr = (byte[]) interfaceC2940.mo5081(iM1046, byte[].class);
            try {
                int iM1044 = m1044(c0267, bArr, iM1046);
                interfaceC2940.mo5080(bArr);
                return iM1044;
            } catch (Throwable th) {
                interfaceC2940.mo5080(bArr);
                throw th;
            }
        } catch (Reader.EndOfFileException unused) {
            return -1;
        }
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final ImageHeaderParser.ImageType m1045(Reader reader) {
        try {
            int iMo1049 = reader.mo1049();
            if (iMo1049 == 65496) {
                return ImageHeaderParser.ImageType.JPEG;
            }
            int iMo1048 = (iMo1049 << 8) | reader.mo1048();
            if (iMo1048 == 4671814) {
                return ImageHeaderParser.ImageType.GIF;
            }
            int iMo10482 = (iMo1048 << 8) | reader.mo1048();
            if (iMo10482 == -1991225785) {
                reader.skip(21L);
                try {
                    return reader.mo1048() >= 3 ? ImageHeaderParser.ImageType.PNG_A : ImageHeaderParser.ImageType.PNG;
                } catch (Reader.EndOfFileException unused) {
                    return ImageHeaderParser.ImageType.PNG;
                }
            }
            if (iMo10482 != 1380533830) {
                return ImageHeaderParser.ImageType.UNKNOWN;
            }
            reader.skip(4L);
            if (((reader.mo1049() << 16) | reader.mo1049()) != 1464156752) {
                return ImageHeaderParser.ImageType.UNKNOWN;
            }
            int iMo10492 = (reader.mo1049() << 16) | reader.mo1049();
            if ((iMo10492 & (-256)) != 1448097792) {
                return ImageHeaderParser.ImageType.UNKNOWN;
            }
            int i = iMo10492 & 255;
            if (i == 88) {
                reader.skip(4L);
                return (reader.mo1048() & 16) != 0 ? ImageHeaderParser.ImageType.WEBP_A : ImageHeaderParser.ImageType.WEBP;
            }
            if (i != 76) {
                return ImageHeaderParser.ImageType.WEBP;
            }
            reader.skip(4L);
            return (reader.mo1048() & 8) != 0 ? ImageHeaderParser.ImageType.WEBP_A : ImageHeaderParser.ImageType.WEBP;
        } catch (Reader.EndOfFileException unused2) {
            return ImageHeaderParser.ImageType.UNKNOWN;
        }
    }

    @Override // com.bumptech.glide.load.ImageHeaderParser
    /* JADX INFO: renamed from: ֏ */
    public ImageHeaderParser.ImageType mo1037(InputStream inputStream) {
        C0567.m1858(inputStream, "Argument must not be null");
        return m1045(new C0267(inputStream));
    }

    @Override // com.bumptech.glide.load.ImageHeaderParser
    /* JADX INFO: renamed from: ֏ */
    public ImageHeaderParser.ImageType mo1038(ByteBuffer byteBuffer) {
        C0567.m1858(byteBuffer, "Argument must not be null");
        return m1045(new C0265(byteBuffer));
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final int m1046(Reader reader) {
        short sMo1048;
        int iMo1049;
        long j;
        long jSkip;
        do {
            short sMo10482 = reader.mo1048();
            if (sMo10482 != 255) {
                if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                    Log.d("DfltImageHeaderParser", "Unknown segmentId=" + ((int) sMo10482));
                }
                return -1;
            }
            sMo1048 = reader.mo1048();
            if (sMo1048 == 218) {
                return -1;
            }
            if (sMo1048 == 217) {
                if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                    Log.d("DfltImageHeaderParser", "Found MARKER_EOI in exif segment");
                }
                return -1;
            }
            iMo1049 = reader.mo1049() - 2;
            if (sMo1048 == 225) {
                return iMo1049;
            }
            j = iMo1049;
            jSkip = reader.skip(j);
        } while (jSkip == j);
        if (Log.isLoggable("DfltImageHeaderParser", 3)) {
            StringBuilder sbM4890 = C2803.m4890("Unable to skip enough data, type: ", sMo1048, ", wanted to skip: ", iMo1049, ", but actually skipped: ");
            sbM4890.append(jSkip);
            Log.d("DfltImageHeaderParser", sbM4890.toString());
        }
        return -1;
    }
}
