package top.zibin.luban;

import android.graphics.BitmapFactory;
import android.util.Log;
import androidx.recyclerview.widget.RecyclerView;
import com.facebook.appevents.codeless.CodelessMatcher;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import ᕣ.K.K.AbstractC1459;

public enum Checker {
    SINGLE;
    

    /* renamed from: ῲ  reason: contains not printable characters */
    public static List<String> f3540 = new ArrayList();
    public final byte[] JPEG_SIGNATURE = {-1, -40, -1};

    /* access modifiers changed from: public */
    static {
        f3540.add(".jpg");
        f3540.add(".jpeg");
        f3540.add(".png");
        f3540.add(".webp");
        f3540.add(".gif");
    }

    /* access modifiers changed from: public */
    Checker() {
    }

    public String extSuffix(AbstractC1459 r3) {
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(r3.m3669(), null, options);
            return options.outMimeType.replace("image/", CodelessMatcher.CURRENT_CLASS_NAME);
        } catch (Exception unused) {
            return ".jpg";
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:31:0x006b, code lost:
        r1 = r2;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:32:0x006c, code lost:
        r2 = r1;
        r1 = 0;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:33:0x006e, code lost:
        if (r1 <= 8) goto L_0x00d9;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:34:0x0070, code lost:
        r3 = m2019(r12, r2, 4, false);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:35:0x0077, code lost:
        if (r3 == 1229531648) goto L_0x0084;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:37:0x007c, code lost:
        if (r3 == 1296891946) goto L_0x0084;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:38:0x007e, code lost:
        android.util.Log.e("Luban", "Invalid byte order");
     */
    /* JADX WARNING: Code restructure failed: missing block: B:39:0x0084, code lost:
        if (r3 != 1229531648) goto L_0x0088;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:40:0x0086, code lost:
        r3 = true;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:41:0x0088, code lost:
        r3 = false;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:42:0x0089, code lost:
        r7 = m2019(r12, r2 + 4, 4, r3) + 2;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:43:0x0092, code lost:
        if (r7 < 10) goto L_0x00d3;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:44:0x0094, code lost:
        if (r7 <= r1) goto L_0x0097;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:45:0x0097, code lost:
        r2 = r2 + r7;
        r1 = r1 - r7;
        r7 = m2019(r12, r2 - 2, 2, r3);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:46:0x009f, code lost:
        r9 = r7 - 1;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:47:0x00a1, code lost:
        if (r7 <= 0) goto L_0x00d9;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:49:0x00a5, code lost:
        if (r1 < 12) goto L_0x00d9;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:51:0x00ad, code lost:
        if (m2019(r12, r2, 2, r3) != 274) goto L_0x00cd;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:52:0x00af, code lost:
        r12 = m2019(r12, r2 + 8, 2, r3);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:53:0x00b4, code lost:
        if (r12 == 1) goto L_?;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:55:0x00b7, code lost:
        if (r12 == 3) goto L_0x00ca;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:57:0x00ba, code lost:
        if (r12 == 6) goto L_0x00c7;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:58:0x00bc, code lost:
        if (r12 == 8) goto L_0x00c4;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:59:0x00be, code lost:
        android.util.Log.e("Luban", "Unsupported orientation");
     */
    /* JADX WARNING: Code restructure failed: missing block: B:60:0x00c4, code lost:
        return com.google.android.material.bottomappbar.BottomAppBarTopEdgeTreatment.ANGLE_UP;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:61:0x00c7, code lost:
        return 90;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:62:0x00ca, code lost:
        return 180;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:63:0x00cd, code lost:
        r2 = r2 + 12;
        r1 = r1 - 12;
        r7 = r9;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:64:0x00d3, code lost:
        android.util.Log.e("Luban", "Invalid offset");
     */
    /* JADX WARNING: Code restructure failed: missing block: B:65:0x00d9, code lost:
        android.util.Log.e("Luban", "Orientation not found");
     */
    /* JADX WARNING: Code restructure failed: missing block: B:79:?, code lost:
        return 0;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:81:?, code lost:
        return 0;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:82:?, code lost:
        return 0;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:83:?, code lost:
        return 0;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:84:?, code lost:
        return 0;
     */
    public int getOrientation(InputStream inputStream) {
        int i;
        byte[] r12 = m2020(inputStream);
        if (r12 == null) {
            return 0;
        }
        int i2 = 0;
        while (true) {
            if (i2 + 3 >= r12.length) {
                break;
            }
            int i3 = i2 + 1;
            if ((r12[i2] & 255) != 255) {
                break;
            }
            int i4 = r12[i3] & 255;
            if (i4 != 255) {
                i3++;
                if (!(i4 == 216 || i4 == 1)) {
                    if (i4 != 217 && i4 != 218) {
                        int r3 = m2019(r12, i3, 2, false);
                        if (r3 >= 2 && (i = i3 + r3) <= r12.length) {
                            if (i4 == 225 && r3 >= 8 && m2019(r12, i3 + 2, 4, false) == 1165519206 && m2019(r12, i3 + 6, 2, false) == 0) {
                                int i5 = i3 + 8;
                                int i6 = r3 - 8;
                                break;
                            }
                            i2 = i;
                        } else {
                            Log.e("Luban", "Invalid length");
                        }
                    } else {
                        break;
                    }
                }
            }
            i2 = i3;
        }
        Log.e("Luban", "Invalid length");
        return 0;
    }

    public boolean isJPG(InputStream inputStream) {
        byte[] r4 = m2020(inputStream);
        if (r4 == null || r4.length < 3) {
            return false;
        }
        return Arrays.equals(this.JPEG_SIGNATURE, new byte[]{r4[0], r4[1], r4[2]});
    }

    public boolean needCompress(int i, String str) {
        if (i <= 0) {
            return true;
        }
        File file = new File(str);
        if (!file.exists() || file.length() <= ((long) (i << 10))) {
            return false;
        }
        return true;
    }

    /* renamed from: K  reason: contains not printable characters */
    public final int m2019(byte[] bArr, int i, int i2, boolean z) {
        int i3;
        if (z) {
            i += i2 - 1;
            i3 = -1;
        } else {
            i3 = 1;
        }
        int i4 = 0;
        while (true) {
            int i5 = i2 - 1;
            if (i2 <= 0) {
                return i4;
            }
            i4 = (bArr[i] & 255) | (i4 << 8);
            i += i3;
            i2 = i5;
        }
    }

    /* JADX WARNING: Can't wrap try/catch for region: R(5:15|16|17|18|19) */
    /* JADX WARNING: Code restructure failed: missing block: B:14:0x0023, code lost:
        r6 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:19:0x002a, code lost:
        return new byte[0];
     */
    /* JADX WARNING: Code restructure failed: missing block: B:22:?, code lost:
        r1.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:23:0x002e, code lost:
        throw r6;
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:15:0x0025 */
    /* renamed from: K  reason: contains not printable characters */
    public final byte[] m2020(InputStream inputStream) {
        if (inputStream == null) {
            return new byte[0];
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[RecyclerView.AbstractC0269.FLAG_APPEARED_IN_PRE_LAYOUT];
        while (true) {
            int read = inputStream.read(bArr, 0, bArr.length);
            if (read != -1) {
                byteArrayOutputStream.write(bArr, 0, read);
            } else {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException unused) {
                }
                return byteArrayOutputStream.toByteArray();
            }
        }
    }
}
