package epson.maintain;



import android.support.annotation.VisibleForTesting;
import android.support.media.ExifInterface;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.CharEncoding;

public class EfuReader {
    /* JADX WARNING: Removed duplicated region for block: B:35:0x005d  */
    /* JADX WARNING: Removed duplicated region for block: B:37:0x0062 A[SYNTHETIC, Splitter:B:37:0x0062] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static void unzipOnlyOneEntry(@android.support.annotation.NonNull java.lang.String r3, @android.support.annotation.NonNull java.lang.String r4) throws java.io.IOException {
        /*
            java.util.zip.ZipFile r0 = new java.util.zip.ZipFile
            r0.<init>(r3)
            int r3 = r0.size()
            r1 = 1
            if (r3 != r1) goto L_0x007d
            java.util.Enumeration r3 = r0.entries()
            boolean r1 = r3.hasMoreElements()
            if (r1 == 0) goto L_0x0075
            java.lang.Object r3 = r3.nextElement()     // Catch:{ NoSuchElementException -> 0x006e }
            java.util.zip.ZipEntry r3 = (java.util.zip.ZipEntry) r3     // Catch:{ NoSuchElementException -> 0x006e }
            boolean r1 = r3.isDirectory()
            if (r1 != 0) goto L_0x0066
            r1 = 0
            java.io.BufferedInputStream r2 = new java.io.BufferedInputStream     // Catch:{ all -> 0x0059 }
            java.io.InputStream r3 = r0.getInputStream(r3)     // Catch:{ all -> 0x0059 }
            r2.<init>(r3)     // Catch:{ all -> 0x0059 }
            java.io.BufferedOutputStream r3 = new java.io.BufferedOutputStream     // Catch:{ all -> 0x0057 }
            java.io.FileOutputStream r0 = new java.io.FileOutputStream     // Catch:{ all -> 0x0057 }
            r0.<init>(r4)     // Catch:{ all -> 0x0057 }
            r3.<init>(r0)     // Catch:{ all -> 0x0057 }
            r4 = 4096(0x1000, float:5.74E-42)
            byte[] r4 = new byte[r4]     // Catch:{ all -> 0x0054 }
        L_0x003a:
            r0 = -1
            int r1 = r2.read(r4)     // Catch:{ IOException -> 0x004d }
            if (r0 == r1) goto L_0x0046
            r0 = 0
            r3.write(r4, r0, r1)     // Catch:{ IOException -> 0x004d }
            goto L_0x003a
        L_0x0046:
            r3.close()
            r2.close()     // Catch:{ IOException -> 0x004c }
        L_0x004c:
            return
        L_0x004d:
            r4 = move-exception
            epson.maintain.EfuReader$WriteException r0 = new epson.maintain.EfuReader$WriteException     // Catch:{ all -> 0x0054 }
            r0.<init>(r4)     // Catch:{ all -> 0x0054 }
            throw r0     // Catch:{ all -> 0x0054 }
        L_0x0054:
            r4 = move-exception
            r1 = r3
            goto L_0x005b
        L_0x0057:
            r4 = move-exception
            goto L_0x005b
        L_0x0059:
            r4 = move-exception
            r2 = r1
        L_0x005b:
            if (r1 == 0) goto L_0x0060
            r1.close()
        L_0x0060:
            if (r2 == 0) goto L_0x0065
            r2.close()     // Catch:{ IOException -> 0x0065 }
        L_0x0065:
            throw r4
        L_0x0066:
            java.io.IOException r3 = new java.io.IOException
            java.lang.String r4 = "zip no entry."
            r3.<init>(r4)
            throw r3
        L_0x006e:
            r3 = move-exception
            java.io.IOException r4 = new java.io.IOException
            r4.<init>(r3)
            throw r4
        L_0x0075:
            java.io.IOException r3 = new java.io.IOException
            java.lang.String r4 = "zip no entry."
            r3.<init>(r4)
            throw r3
        L_0x007d:
            java.io.IOException r3 = new java.io.IOException
            java.lang.StringBuilder r4 = new java.lang.StringBuilder
            r4.<init>()
            java.lang.String r1 = "entry != 1 <"
            r4.append(r1)
            int r0 = r0.size()
            r4.append(r0)
            java.lang.String r0 = ">"
            r4.append(r0)
            java.lang.String r4 = r4.toString()
            r3.<init>(r4)
            throw r3
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.maintain.EfuReader.unzipOnlyOneEntry(java.lang.String, java.lang.String):void");
    }

    /* JADX WARNING: Removed duplicated region for block: B:22:0x0031  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void writeOnlyOneFirmwareData(java.io.File r3, java.io.File r4) throws java.io.IOException {
        /*
            r2 = this;
            boolean r0 = epson.maintain.EfuReader.RcxFileReader.checkHeaderId(r3)
            if (r0 != 0) goto L_0x0035
            r0 = 0
            epson.maintain.EfuReader$RcxFileReader r1 = new epson.maintain.EfuReader$RcxFileReader     // Catch:{ all -> 0x002d }
            r1.<init>(r3)     // Catch:{ all -> 0x002d }
            boolean r3 = r1.readHeader()     // Catch:{ all -> 0x002b }
            if (r3 == 0) goto L_0x0025
            boolean r3 = r2.checkHeaderData(r1)     // Catch:{ all -> 0x002b }
            if (r3 == 0) goto L_0x001f
            r1.writeOnlyOneFirmwareData(r4)     // Catch:{ all -> 0x002b }
            r1.close()
            return
        L_0x001f:
            java.io.IOException r3 = new java.io.IOException     // Catch:{ all -> 0x002b }
            r3.<init>()     // Catch:{ all -> 0x002b }
            throw r3     // Catch:{ all -> 0x002b }
        L_0x0025:
            java.io.IOException r3 = new java.io.IOException     // Catch:{ all -> 0x002b }
            r3.<init>()     // Catch:{ all -> 0x002b }
            throw r3     // Catch:{ all -> 0x002b }
        L_0x002b:
            r3 = move-exception
            goto L_0x002f
        L_0x002d:
            r3 = move-exception
            r1 = r0
        L_0x002f:
            if (r1 == 0) goto L_0x0034
            r1.close()
        L_0x0034:
            throw r3
        L_0x0035:
            java.io.IOException r3 = new java.io.IOException
            r3.<init>()
            throw r3
        */
        throw new UnsupportedOperationException("Method not decompiled: epson.maintain.EfuReader.writeOnlyOneFirmwareData(java.io.File, java.io.File):void");
    }

    private boolean checkHeaderData(@NonNull RcxFileReader rcxFileReader) {
        String sectionAData = rcxFileReader.getSectionAData("1");
        if ((!ExifInterface.GPS_MEASUREMENT_2D.equals(sectionAData) && !ExifInterface.GPS_MEASUREMENT_3D.equals(sectionAData)) || !"11".equals(rcxFileReader.getSectionAData(ExifInterface.GPS_MEASUREMENT_2D))) {
            return false;
        }
        String sectionAData2 = rcxFileReader.getSectionAData("16");
        if (sectionAData2 == null || !RcxFileReader.PATTERN_KOMMA.matcher(sectionAData2).lookingAt()) {
            return true;
        }
        return false;
    }

    static class RcxFileReader implements Closeable {
        private static final Pattern COMMENT_PATTERN = Pattern.compile("^#.*");
        private static final Pattern PATTERN_KEY_AND_VALUE = Pattern.compile("(\\d+)=\"(.*)\"");
        static final Pattern PATTERN_KOMMA = Pattern.compile(PreferencesConstants.COOKIE_DELIMITER);
        private static final Pattern PATTERN_SECTION_SEPARATOR = Pattern.compile("\\[([A-Z]+)\\]", 2);
        private static final byte[] RCX_HEADER_ID = "RCX\r\nSEIKO EPSON EpsonNet Form\r\n".getBytes(Charset.forName(CharEncoding.US_ASCII));

        /* renamed from: is */
        private RandomAccessFile f333is = null;
        private long mHeaderEndPosition = -1;
        private final HashMap<String, String> mSectionAKeyAndValue = new HashMap<>();

        RcxFileReader(File file) throws FileNotFoundException {
            f333is = new RandomAccessFile(file, "r");
        }

        public static boolean checkHeaderId(File file) {
            return !Arrays.equals(RCX_HEADER_ID, getFileByte(file, RCX_HEADER_ID.length));
        }

        /* JADX WARNING: Removed duplicated region for block: B:20:0x0021 A[SYNTHETIC, Splitter:B:20:0x0021] */
        /* JADX WARNING: Removed duplicated region for block: B:27:0x0028 A[SYNTHETIC, Splitter:B:27:0x0028] */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        private static byte[] getFileByte(java.io.File r5, int r6) {
            /*
                byte[] r0 = new byte[r6]
                r1 = 0
                java.io.FileInputStream r2 = new java.io.FileInputStream     // Catch:{ IOException -> 0x0025, all -> 0x001e }
                r2.<init>(r5)     // Catch:{ IOException -> 0x0025, all -> 0x001e }
                r5 = 0
            L_0x0009:
                int r3 = r6 - r5
                int r3 = r2.read(r0, r5, r3)     // Catch:{ IOException -> 0x001c, all -> 0x0019 }
                r4 = -1
                if (r3 == r4) goto L_0x0015
                int r5 = r5 + r3
                if (r5 < r6) goto L_0x0009
            L_0x0015:
                r2.close()     // Catch:{ IOException -> 0x0018 }
            L_0x0018:
                return r0
            L_0x0019:
                r5 = move-exception
                r1 = r2
                goto L_0x001f
            L_0x001c:
                goto L_0x0026
            L_0x001e:
                r5 = move-exception
            L_0x001f:
                if (r1 == 0) goto L_0x0024
                r1.close()     // Catch:{ IOException -> 0x0024 }
            L_0x0024:
                throw r5
            L_0x0025:
                r2 = r1
            L_0x0026:
                if (r2 == 0) goto L_0x002b
                r2.close()     // Catch:{ IOException -> 0x002b }
            L_0x002b:
                return r1
            */
            throw new UnsupportedOperationException("Method not decompiled: epson.maintain.EfuReader.RcxFileReader.getFileByte(java.io.File, int):byte[]");
        }

        @Nullable
        public String getSectionAData(String str) {
            return mSectionAKeyAndValue.get(str);
        }

        /* access modifiers changed from: private */
        /* JADX WARNING: Code restructure failed: missing block: B:20:0x0048, code lost:
            return false;
         */
        /* JADX WARNING: Exception block dominator not found, dom blocks: [] */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        public boolean readHeader() {
            /*
                r6 = this;
                java.util.HashMap<java.lang.String, java.lang.String> r0 = r6.mSectionAKeyAndValue
                r0.clear()
                r0 = -1
                r6.mHeaderEndPosition = r0
                r0 = 0
                java.lang.String r1 = ""
            L_0x000c:
                java.io.RandomAccessFile r2 = r6.f333is     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                java.lang.String r2 = r6.getNextLine(r2)     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                r3 = 1
                if (r2 == 0) goto L_0x003d
                boolean r4 = isCommentLine(r2)     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                if (r4 == 0) goto L_0x001c
                goto L_0x000c
            L_0x001c:
                java.lang.String r4 = isSectionSeparator(r2)     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                if (r4 == 0) goto L_0x0024
                r1 = r4
                goto L_0x000c
            L_0x0024:
                java.lang.String[] r2 = isKeyAndValueData(r2)     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                if (r2 != 0) goto L_0x002b
                goto L_0x000c
            L_0x002b:
                java.lang.String r4 = "A"
                boolean r4 = r4.equals(r1)     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                if (r4 == 0) goto L_0x000c
                java.util.HashMap<java.lang.String, java.lang.String> r4 = r6.mSectionAKeyAndValue     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                r5 = r2[r0]     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                r2 = r2[r3]     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                r4.put(r5, r2)     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                goto L_0x000c
            L_0x003d:
                java.io.RandomAccessFile r1 = r6.f333is     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                long r1 = r1.getFilePointer()     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                r6.mHeaderEndPosition = r1     // Catch:{ IOException | OutOfMemoryError -> 0x0048, all -> 0x0046 }
                return r3
            L_0x0046:
                r0 = move-exception
                throw r0
            L_0x0048:
                return r0
            */
            throw new UnsupportedOperationException("Method not decompiled: epson.maintain.EfuReader.RcxFileReader.readHeader():boolean");
        }

        @VisibleForTesting
        static boolean isCommentLine(@NonNull String str) {
            return COMMENT_PATTERN.matcher(str).matches();
        }

        @Nullable
        @VisibleForTesting
        static String isSectionSeparator(@NonNull String str) {
            Matcher matcher = PATTERN_SECTION_SEPARATOR.matcher(str);
            if (!matcher.lookingAt()) {
                return null;
            }
            return matcher.group(1).toUpperCase();
        }

        @Nullable
        @VisibleForTesting
        static String[] isKeyAndValueData(@NonNull String str) {
            Matcher matcher = PATTERN_KEY_AND_VALUE.matcher(str);
            if (!matcher.lookingAt()) {
                return null;
            }
            return new String[]{matcher.group(1).toLowerCase(), matcher.group(2)};
        }

        @Nullable
        private String getNextLine(@NonNull RandomAccessFile randomAccessFile) throws IOException {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            boolean z = false;
            while (true) {
                int read = randomAccessFile.read();
                if (read != -1) {
                    switch (z) {
                        case false:
                            if (read == 13) {
                                z = true;
                                break;
                            } else if (read != 12 || byteArrayOutputStream.size() != 0) {
                                byteArrayOutputStream.write(read);
                                break;
                            } else {
                                return null;
                            }
                        case true:
                            if (read == 10) {
                                return byteArrayOutputStream.toString(CharEncoding.US_ASCII);
                            }
                            throw new IOException();
                    }
                } else {
                    throw new IOException("file data ends");
                }
            }
        }

        public void close() {
            RandomAccessFile randomAccessFile = f333is;
            if (randomAccessFile != null) {
                try {
                    randomAccessFile.close();
                    f333is = null;
                } catch (IOException unused) {
                }
            }
        }

        public void writeOnlyOneFirmwareData(File file) throws IOException {
            writeRemainingData(this.f333is, mHeaderEndPosition, file);
        }

        /* JADX WARNING: Removed duplicated region for block: B:23:0x0034 A[Catch:{ all -> 0x002b }] */
        /* JADX WARNING: Removed duplicated region for block: B:26:0x003a A[SYNTHETIC, Splitter:B:26:0x003a] */
        @android.support.annotation.VisibleForTesting
        /* Code decompiled incorrectly, please refer to instructions dump. */
        static void writeRemainingData(@android.support.annotation.NonNull java.io.RandomAccessFile r3, long r4, @android.support.annotation.NonNull java.io.File r6) throws java.io.IOException {
            /*
                r0 = 0
                java.io.BufferedOutputStream r1 = new java.io.BufferedOutputStream     // Catch:{ IOException -> 0x002d }
                java.io.FileOutputStream r2 = new java.io.FileOutputStream     // Catch:{ IOException -> 0x002d }
                r2.<init>(r6)     // Catch:{ IOException -> 0x002d }
                r1.<init>(r2)     // Catch:{ IOException -> 0x002d }
                r3.seek(r4)     // Catch:{ IOException -> 0x0028, all -> 0x0025 }
                writeRemainingData(r3, r1)     // Catch:{ IOException -> 0x0028, all -> 0x0025 }
                r1.close()     // Catch:{ IOException -> 0x0015 }
                return
            L_0x0015:
                r3 = move-exception
                boolean r4 = r6.exists()
                if (r4 == 0) goto L_0x001f
                r6.delete()
            L_0x001f:
                epson.maintain.EfuReader$WriteException r4 = new epson.maintain.EfuReader$WriteException
                r4.<init>(r3)
                throw r4
            L_0x0025:
                r3 = move-exception
                r0 = r1
                goto L_0x0038
            L_0x0028:
                r3 = move-exception
                r0 = r1
                goto L_0x002e
            L_0x002b:
                r3 = move-exception
                goto L_0x0038
            L_0x002d:
                r3 = move-exception
            L_0x002e:
                boolean r4 = r6.exists()     // Catch:{ all -> 0x002b }
                if (r4 == 0) goto L_0x0037
                r6.delete()     // Catch:{ all -> 0x002b }
            L_0x0037:
                throw r3     // Catch:{ all -> 0x002b }
            L_0x0038:
                if (r0 == 0) goto L_0x004e
                r0.close()     // Catch:{ IOException -> 0x003e }
                goto L_0x004e
            L_0x003e:
                r3 = move-exception
                boolean r4 = r6.exists()
                if (r4 == 0) goto L_0x0048
                r6.delete()
            L_0x0048:
                epson.maintain.EfuReader$WriteException r4 = new epson.maintain.EfuReader$WriteException
                r4.<init>(r3)
                throw r4
            L_0x004e:
                throw r3
            */
            throw new UnsupportedOperationException("Method not decompiled: epson.maintain.EfuReader.RcxFileReader.writeRemainingData(java.io.RandomAccessFile, long, java.io.File):void");
        }

        /* JADX WARNING: Code restructure failed: missing block: B:21:0x002f, code lost:
            r8.delete();
         */
        /* JADX WARNING: Code restructure failed: missing block: B:24:?, code lost:
            r1.close();
         */
        /* JADX WARNING: Code restructure failed: missing block: B:27:0x003c, code lost:
            if (r8.exists() == false) goto L_?;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:38:?, code lost:
            return false;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:39:?, code lost:
            return false;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:40:?, code lost:
            return false;
         */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:18:0x0029 */
        /* JADX WARNING: Removed duplicated region for block: B:21:0x002f A[Catch:{ all -> 0x0027 }] */
        /* JADX WARNING: Removed duplicated region for block: B:23:0x0034 A[SYNTHETIC, Splitter:B:23:0x0034] */
        /* JADX WARNING: Removed duplicated region for block: B:29:0x0042 A[SYNTHETIC, Splitter:B:29:0x0042] */
        /* JADX WARNING: Removed duplicated region for block: B:38:? A[RETURN, SYNTHETIC] */
        /* Code decompiled incorrectly, please refer to instructions dump. */
        static boolean searchDataAndWriteRemaining(@android.support.annotation.NonNull java.io.RandomAccessFile r4, long r5, @android.support.annotation.NonNull byte[] r7, @android.support.annotation.NonNull java.io.File r8) {
            /*
                r0 = 0
                r1 = 0
                java.io.BufferedOutputStream r2 = new java.io.BufferedOutputStream     // Catch:{ IOException -> 0x0029 }
                java.io.FileOutputStream r3 = new java.io.FileOutputStream     // Catch:{ IOException -> 0x0029 }
                r3.<init>(r8)     // Catch:{ IOException -> 0x0029 }
                r2.<init>(r3)     // Catch:{ IOException -> 0x0029 }
                seekDataPosition(r4, r5, r7)     // Catch:{ IOException -> 0x0025, all -> 0x0022 }
                writeRemainingData(r4, r2)     // Catch:{ IOException -> 0x0025, all -> 0x0022 }
                r4 = 1
                r2.close()     // Catch:{ IOException -> 0x0018 }
                r0 = 1
                goto L_0x003f
            L_0x0018:
                boolean r4 = r8.exists()
                if (r4 == 0) goto L_0x003f
            L_0x001e:
                r8.delete()
                goto L_0x003f
            L_0x0022:
                r4 = move-exception
                r1 = r2
                goto L_0x0040
            L_0x0025:
                r1 = r2
                goto L_0x0029
            L_0x0027:
                r4 = move-exception
                goto L_0x0040
            L_0x0029:
                boolean r4 = r8.exists()     // Catch:{ all -> 0x0027 }
                if (r4 == 0) goto L_0x0032
                r8.delete()     // Catch:{ all -> 0x0027 }
            L_0x0032:
                if (r1 == 0) goto L_0x003f
                r1.close()     // Catch:{ IOException -> 0x0038 }
                goto L_0x003f
            L_0x0038:
                boolean r4 = r8.exists()
                if (r4 == 0) goto L_0x003f
                goto L_0x001e
            L_0x003f:
                return r0
            L_0x0040:
                if (r1 == 0) goto L_0x004f
                r1.close()     // Catch:{ IOException -> 0x0046 }
                goto L_0x004f
            L_0x0046:
                boolean r5 = r8.exists()
                if (r5 == 0) goto L_0x004f
                r8.delete()
            L_0x004f:
                throw r4
            */
            throw new UnsupportedOperationException("Method not decompiled: epson.maintain.EfuReader.RcxFileReader.searchDataAndWriteRemaining(java.io.RandomAccessFile, long, byte[], java.io.File):boolean");
        }

        static void writeRemainingData(RandomAccessFile randomAccessFile, BufferedOutputStream bufferedOutputStream) throws IOException {
            byte[] bArr = new byte[4096];
            while (true) {
                int read = randomAccessFile.read(bArr);
                if (read != -1) {
                    try {
                        bufferedOutputStream.write(bArr, 0, read);
                    } catch (IOException e) {
                        throw new WriteException(e);
                    }
                } else {
                    return;
                }
            }
        }

        static void seekDataPosition(RandomAccessFile randomAccessFile, long j, byte[] bArr) throws IOException {
            randomAccessFile.seek(j);
            long j2 = 0;
            int i = 0;
            while (true) {
                int read = randomAccessFile.read();
                if (read == -1) {
                    throw new IOException("data not found");
                } else if (i == 0) {
                    if (read == bArr[0]) {
                        j2 = randomAccessFile.getFilePointer();
                        i++;
                    }
                } else if (read == bArr[i]) {
                    i++;
                    if (i >= bArr.length) {
                        randomAccessFile.seek(j2 - 1);
                        return;
                    }
                } else {
                    randomAccessFile.seek(j2);
                    i = 0;
                }
            }
        }
    }

    public static class WriteException extends IOException {
        public WriteException(Throwable th) {
            super(th);
        }

        public WriteException() {
        }
    }
}
