package com.bumptech.glide.c.d.a;

import android.util.Log;
import com.bumptech.glide.c.b.a.b;
import com.bumptech.glide.c.f$a;
import com.bumptech.glide.h.i;
import com.tencent.matrix.trace.core.AppMethodBeat;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;

public final class f implements com.bumptech.glide.c.f {
    static final byte[] aEW = "Exif\u0000\u0000".getBytes(Charset.forName("UTF-8"));
    private static final int[] aEX = new int[]{0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8};

    static {
        AppMethodBeat.i(92214);
        AppMethodBeat.o(92214);
    }

    public final f$a c(InputStream inputStream) {
        AppMethodBeat.i(92208);
        f$a a = a(new d((InputStream) i.d(inputStream, "Argument must not be null")));
        AppMethodBeat.o(92208);
        return a;
    }

    public final f$a b(ByteBuffer byteBuffer) {
        AppMethodBeat.i(92209);
        f$a a = a(new a((ByteBuffer) i.d(byteBuffer, "Argument must not be null")));
        AppMethodBeat.o(92209);
        return a;
    }

    public final int a(InputStream inputStream, b bVar) {
        AppMethodBeat.i(92210);
        int a = a(new d((InputStream) i.d(inputStream, "Argument must not be null")), (b) i.d(bVar, "Argument must not be null"));
        AppMethodBeat.o(92210);
        return a;
    }

    private static f$a a(c cVar) {
        AppMethodBeat.i(92211);
        int nn = cVar.nn();
        f$a f_a;
        if (nn == 65496) {
            f_a = f$a.JPEG;
            AppMethodBeat.o(92211);
            return f_a;
        }
        nn = ((nn << 16) & -65536) | (cVar.nn() & 65535);
        if (nn == -1991225785) {
            cVar.skip(21);
            if (cVar.np() >= 3) {
                f_a = f$a.PNG_A;
                AppMethodBeat.o(92211);
                return f_a;
            }
            f_a = f$a.PNG;
            AppMethodBeat.o(92211);
            return f_a;
        } else if ((nn >> 8) == 4671814) {
            f_a = f$a.GIF;
            AppMethodBeat.o(92211);
            return f_a;
        } else if (nn != 1380533830) {
            f_a = f$a.UNKNOWN;
            AppMethodBeat.o(92211);
            return f_a;
        } else {
            cVar.skip(4);
            if ((((cVar.nn() << 16) & -65536) | (cVar.nn() & 65535)) != 1464156752) {
                f_a = f$a.UNKNOWN;
                AppMethodBeat.o(92211);
                return f_a;
            }
            nn = ((cVar.nn() << 16) & -65536) | (cVar.nn() & 65535);
            if ((nn & -256) != 1448097792) {
                f_a = f$a.UNKNOWN;
                AppMethodBeat.o(92211);
                return f_a;
            } else if ((nn & 255) == 88) {
                cVar.skip(4);
                if ((cVar.np() & 16) != 0) {
                    f_a = f$a.WEBP_A;
                    AppMethodBeat.o(92211);
                    return f_a;
                }
                f_a = f$a.WEBP;
                AppMethodBeat.o(92211);
                return f_a;
            } else if ((nn & 255) == 76) {
                cVar.skip(4);
                if ((cVar.np() & 8) != 0) {
                    f_a = f$a.WEBP_A;
                    AppMethodBeat.o(92211);
                    return f_a;
                }
                f_a = f$a.WEBP;
                AppMethodBeat.o(92211);
                return f_a;
            } else {
                f_a = f$a.WEBP;
                AppMethodBeat.o(92211);
                return f_a;
            }
        }
    }

    private static int a(c cVar, b bVar) {
        Object obj;
        int i = -1;
        AppMethodBeat.i(92212);
        int nn = cVar.nn();
        if ((nn & 65496) == 65496 || nn == 19789 || nn == 18761) {
            obj = 1;
        } else {
            obj = null;
        }
        if (obj == null) {
            Log.isLoggable("DfltImageHeaderParser", 3);
            AppMethodBeat.o(92212);
        } else {
            int i2;
            while (cVar.no() == (short) 255) {
                short no = cVar.no();
                if (no != (short) 218) {
                    if (no != (short) 217) {
                        nn = cVar.nn() - 2;
                        if (no == (short) 225) {
                            i2 = nn;
                            break;
                        }
                        long skip = cVar.skip((long) nn);
                        if (skip != ((long) nn)) {
                            if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                                new StringBuilder("Unable to skip enough data, type: ").append(no).append(", wanted to skip: ").append(nn).append(", but actually skipped: ").append(skip);
                            }
                            i2 = -1;
                        }
                    } else {
                        Log.isLoggable("DfltImageHeaderParser", 3);
                        i2 = -1;
                        break;
                    }
                }
                i2 = -1;
                break;
            }
            Log.isLoggable("DfltImageHeaderParser", 3);
            i2 = -1;
            if (i2 == -1) {
                Log.isLoggable("DfltImageHeaderParser", 3);
                AppMethodBeat.o(92212);
            } else {
                byte[] bArr = (byte[]) bVar.a(i2, byte[].class);
                try {
                    int read = cVar.read(bArr, i2);
                    if (read != i2) {
                        if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                            new StringBuilder("Unable to read exif segment data, length: ").append(i2).append(", actually read: ").append(read);
                        }
                    } else if (c(bArr, i2)) {
                        i = a(new b(bArr, i2));
                    } else {
                        Log.isLoggable("DfltImageHeaderParser", 3);
                    }
                    bVar.put(bArr);
                    AppMethodBeat.o(92212);
                } catch (Throwable th) {
                    bVar.put(bArr);
                    AppMethodBeat.o(92212);
                }
            }
        }
        return i;
    }

    private static boolean c(byte[] bArr, int i) {
        boolean z = bArr != null && i > aEW.length;
        if (z) {
            for (int i2 = 0; i2 < aEW.length; i2++) {
                if (bArr[i2] != aEW[i2]) {
                    return false;
                }
            }
        }
        return z;
    }

    private static int a(b bVar) {
        ByteOrder byteOrder;
        AppMethodBeat.i(92213);
        switch (bVar.dh(6)) {
            case (short) 18761:
                byteOrder = ByteOrder.LITTLE_ENDIAN;
                break;
            case (short) 19789:
                byteOrder = ByteOrder.BIG_ENDIAN;
                break;
            default:
                Log.isLoggable("DfltImageHeaderParser", 3);
                byteOrder = ByteOrder.BIG_ENDIAN;
                break;
        }
        bVar.aEY.order(byteOrder);
        int dg = bVar.dg(10) + 6;
        short dh = bVar.dh(dg);
        for (short s = (short) 0; s < dh; s++) {
            int i = (dg + 2) + (s * 12);
            short dh2 = bVar.dh(i);
            if (dh2 == (short) 274) {
                short dh3 = bVar.dh(i + 2);
                if (dh3 > (short) 0 && dh3 <= (short) 12) {
                    int dg2 = bVar.dg(i + 4);
                    if (dg2 >= 0) {
                        if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                            new StringBuilder("Got tagIndex=").append(s).append(" tagType=").append(dh2).append(" formatCode=").append(dh3).append(" componentCount=").append(dg2);
                        }
                        int i2 = aEX[dh3] + dg2;
                        if (i2 <= 4) {
                            i += 8;
                            if (i < 0 || i > bVar.aEY.remaining()) {
                                if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                                    new StringBuilder("Illegal tagValueOffset=").append(i).append(" tagType=").append(dh2);
                                }
                            } else if (i2 >= 0 && i + i2 <= bVar.aEY.remaining()) {
                                s = bVar.dh(i);
                                AppMethodBeat.o(92213);
                                return s;
                            } else if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                            }
                        } else if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                        }
                    } else if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                    }
                } else if (Log.isLoggable("DfltImageHeaderParser", 3)) {
                }
            }
        }
        AppMethodBeat.o(92213);
        return -1;
    }
}
