package epson.maintain;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.apache.commons.lang.CharEncoding;

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;

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 {

        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 {

        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) {

            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() {

            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 {

            throw new UnsupportedOperationException("Method not decompiled: epson.maintain.EfuReader.RcxFileReader.writeRemainingData(java.io.RandomAccessFile, long, java.io.File):void");
        }


        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) {

            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() {
        }
    }
}
