package com.pgl.ssdk;

import android.content.Context;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;

/* JADX INFO: loaded from: classes.dex */
public class af {
    /* JADX WARN: Removed duplicated region for block: B:31:0x00df  */
    /* JADX WARN: Removed duplicated region for block: B:36:0x00d9 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static synchronized String a(Context context) {
        String strA;
        String str;
        InputStream inputStreamOpen;
        ByteArrayOutputStream byteArrayOutputStream;
        byte[] bArr;
        try {
            str = context.getFilesDir().getAbsolutePath() + "/dic";
            if (new File(str).exists()) {
                a("chmod 777 ".concat(String.valueOf(str)));
                String strA2 = a(str);
                a("chmod 600 ".concat(String.valueOf(str)));
                if (strA2 != null && strA2.length() > 0) {
                    return strA2;
                }
            }
            inputStreamOpen = context.getResources().getAssets().open("dic");
            byteArrayOutputStream = new ByteArrayOutputStream();
            bArr = new byte[4096];
        } catch (Throwable unused) {
            strA = "0[<!>]EXCEPTION[<!>]";
        }
        while (true) {
            int i10 = inputStreamOpen.read(bArr, 0, 4096);
            if (i10 == -1) {
                break;
            }
            a(bArr, i10, "dic".getBytes(Charset.forName("UTF-8")));
            byteArrayOutputStream.write(bArr, 0, i10);
            if (strA == null) {
                try {
                    if (strA.length() == 0) {
                        strA = "0[<!>]ERROR[<!>]";
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            return strA;
        }
        FileOutputStream fileOutputStream = new FileOutputStream(str);
        fileOutputStream.write(byteArrayOutputStream.toByteArray());
        fileOutputStream.close();
        a("chmod 777 ".concat(String.valueOf(str)));
        strA = a(str);
        if (strA == null || strA.length() == 0) {
            RandomAccessFile randomAccessFile = new RandomAccessFile(str, "rw");
            randomAccessFile.seek(16L);
            randomAccessFile.write(new byte[]{2});
            randomAccessFile.close();
            strA = a(str);
        }
        a("chmod 600 ".concat(String.valueOf(str)));
        if (strA == null) {
        }
        return strA;
    }

    private static String a(BufferedInputStream bufferedInputStream) {
        int i10;
        if (bufferedInputStream == null) {
            return "";
        }
        byte[] bArr = new byte[4096];
        StringBuilder sb = new StringBuilder();
        do {
            try {
                i10 = bufferedInputStream.read(bArr);
                if (i10 > 0) {
                    sb.append(new String(bArr, 0, i10));
                }
            } catch (Exception unused) {
            }
        } while (i10 >= 4096);
        return sb.toString();
    }

    /* JADX WARN: Removed duplicated region for block: B:25:0x0056  */
    /* JADX WARN: Removed duplicated region for block: B:47:0x0051 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:49:0x004c A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:53:0x0064 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:55:0x005f A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:65:? A[SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private static String a(String str) throws Throwable {
        Process processExec;
        BufferedInputStream bufferedInputStream;
        BufferedOutputStream bufferedOutputStream;
        BufferedOutputStream bufferedOutputStream2 = null;
        strA = null;
        String strA = null;
        try {
            processExec = Runtime.getRuntime().exec("sh");
            try {
                bufferedOutputStream = new BufferedOutputStream(processExec.getOutputStream());
                try {
                    bufferedInputStream = new BufferedInputStream(processExec.getInputStream());
                } catch (Exception unused) {
                    bufferedInputStream = null;
                } catch (Throwable th) {
                    th = th;
                    bufferedInputStream = null;
                }
            } catch (Exception unused2) {
                bufferedOutputStream = null;
                bufferedInputStream = null;
                if (bufferedOutputStream != null) {
                    try {
                        bufferedOutputStream.close();
                    } catch (IOException unused3) {
                    }
                }
                if (bufferedInputStream != null) {
                    try {
                        bufferedInputStream.close();
                    } catch (IOException unused4) {
                    }
                }
                if (processExec != null) {
                    processExec.destroy();
                }
                return strA;
            } catch (Throwable th2) {
                th = th2;
                bufferedInputStream = null;
                if (bufferedOutputStream2 != null) {
                    try {
                        bufferedOutputStream2.close();
                    } catch (IOException unused5) {
                    }
                }
                if (bufferedInputStream != null) {
                    try {
                        bufferedInputStream.close();
                    } catch (IOException unused6) {
                    }
                }
                if (processExec != null) {
                    throw th;
                }
                processExec.destroy();
                throw th;
            }
        } catch (Exception unused7) {
            processExec = null;
        } catch (Throwable th3) {
            th = th3;
            processExec = null;
        }
        try {
            bufferedOutputStream.write(str.getBytes());
            bufferedOutputStream.write(10);
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
            processExec.waitFor();
            strA = a(bufferedInputStream);
            try {
                bufferedOutputStream.close();
            } catch (IOException unused8) {
            }
            try {
                bufferedInputStream.close();
            } catch (IOException unused9) {
            }
        } catch (Exception unused10) {
            if (bufferedOutputStream != null) {
            }
            if (bufferedInputStream != null) {
            }
            if (processExec != null) {
            }
            return strA;
        } catch (Throwable th4) {
            th = th4;
            bufferedOutputStream2 = bufferedOutputStream;
            if (bufferedOutputStream2 != null) {
            }
            if (bufferedInputStream != null) {
            }
            if (processExec != null) {
            }
        }
        processExec.destroy();
        return strA;
    }

    private static void a(byte[] bArr, int i10, byte[] bArr2) {
        for (int i11 = 0; i11 < i10; i11++) {
            bArr[i11] = (byte) (bArr[i11] ^ bArr2[i11 % bArr2.length]);
        }
    }
}
