package com.epson.memcardacc;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.common.primitives.UnsignedBytes;

import epson.print.IprintApplication;
import epson.print.Util.EPLog;

import java.io.UnsupportedEncodingException;
import java.util.LinkedList;
import java.util.TimeZone;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.CharEncoding;
import org.apache.commons.lang.time.DateUtils;

public class CifsAccess {
    public static final int CHECK_STORAGE_ERROR = 0;
    public static final int CIFS_ERROR_LOGIN_FAILURE = -1205;
    public static final int CIFS_ERROR_PATH_EXISTED = -1203;
    public static final String KEY_SMB_PASSWORD = "password";
    public static final String KEY_SMB_USER_NAME = "smb_user_name";
    public static final String SMB_AUTHINFO_SHARED_PREFS_KEY = "smb_authinfo";
    public static final int STORAGE_SET_TYPE_GENERIC_MEMCARD_COPY = 1;
    public static final int STORAGE_SET_TYPE_PHOTO_COPY = 2;
    private static CifsLib sCifsLib;
    private static CifsLibFactory sCifsLibFactory;
    protected static final Semaphore sCifsLibSemaphore = new Semaphore(1);
    private boolean mGotCifsLibSemaphore;
    private String[] mStorageSet;

    protected static void setCifsFactory(CifsLibFactory cifsLibFactory) {
    }

    public int initDefault(Context context, int i) {
        if (i != 2) {
            mStorageSet = MemcardConfig.GENERIC_STORAGE_SET;
        } else {
            mStorageSet = MemcardConfig.PHOTO_COPY_STORAGE_SET;
        }
        try {
            return init(MemcardUtil.getPrinterIpAddress(context), TimeZone.getDefault().getRawOffset() / DateUtils.MILLIS_IN_HOUR);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return 0;
        }
    }

    /* access modifiers changed from: protected */
    /* JADX WARNING: Code restructure failed: missing block: B:31:0x0048, code lost:
        return 0;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public synchronized int init(java.lang.String r4, int r5) throws java.io.UnsupportedEncodingException {
        /*
            r3 = this;
            monitor-enter(r3)
            r0 = 0
            if (r4 == 0) goto L_0x0047
            int r1 = r4.length()     // Catch:{ all -> 0x0044 }
            if (r1 > 0) goto L_0x000b
            goto L_0x0047
        L_0x000b:
            boolean r1 = r3.mGotCifsLibSemaphore     // Catch:{ all -> 0x0044 }
            if (r1 == 0) goto L_0x0011
            monitor-exit(r3)
            return r0
        L_0x0011:
            boolean r1 = r3.acquireCifsLibSemaphore()     // Catch:{ all -> 0x0044 }
            if (r1 != 0) goto L_0x0019
            monitor-exit(r3)
            return r0
        L_0x0019:
            com.epson.memcardacc.CifsLib r1 = sCifsLib     // Catch:{ all -> 0x0044 }
            if (r1 == 0) goto L_0x0027
            java.lang.String r1 = "CifsAccess"
            java.lang.String r2 = "in init() sCifsLib != null"
            epson.print.Util.EPLog.w((java.lang.String) r1, (java.lang.String) r2)     // Catch:{ all -> 0x0044 }
            r3.freeCifsLib()     // Catch:{ all -> 0x0044 }
        L_0x0027:
            com.epson.memcardacc.CifsAccess$CifsLibFactory r1 = sCifsLibFactory     // Catch:{ all -> 0x0044 }
            com.epson.memcardacc.CifsLib r1 = r1.getCifsLib()     // Catch:{ all -> 0x0044 }
            sCifsLib = r1     // Catch:{ all -> 0x0044 }
            java.lang.String r1 = "US-ASCII"
            byte[] r4 = r4.getBytes(r1)     // Catch:{ all -> 0x0044 }
            com.epson.memcardacc.CifsLib r1 = sCifsLib     // Catch:{ all -> 0x0044 }
            int r4 = r1.init(r4, r5)     // Catch:{ all -> 0x0044 }
            if (r4 != 0) goto L_0x0042
            r3.free()     // Catch:{ all -> 0x0044 }
            monitor-exit(r3)
            return r0
        L_0x0042:
            monitor-exit(r3)
            return r4
        L_0x0044:
            r4 = move-exception
            monitor-exit(r3)
            throw r4
        L_0x0047:
            monitor-exit(r3)
            return r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.epson.memcardacc.CifsAccess.init(java.lang.String, int):int");
    }

    protected void freeCifsLib() {
        CifsLib cifsLib = sCifsLib;
        if (cifsLib != null) {
            cifsLib.free();
            sCifsLib = null;
        }
    }

    public synchronized void free() {
        freeCifsLib();
        releaseCifsLibSemaphore();
    }

    protected synchronized boolean acquireCifsLibSemaphore() {
        try {
            if (!getCifsLibSemaphore().tryAcquire(60, TimeUnit.SECONDS)) {
                EPLog.w("CifsAccess", "cifsSemaphore.tryAcquire() can not get semaphore.");
                return false;
            }
            mGotCifsLibSemaphore = true;
            return true;
        } catch (InterruptedException unused) {
            EPLog.i("CifsAccess", "cifsSemaphore.tryAcquire() interrupted.");
            return false;
        }
    }

    protected synchronized void releaseCifsLibSemaphore() {
        if (mGotCifsLibSemaphore) {
            getCifsLibSemaphore().release();
            mGotCifsLibSemaphore = false;
        }
    }

    public LinkedList<String> getStorageNames() {
        return getStringsFromUtf16LeByte(sCifsLib.getStrageNames());
    }

    public int checkStorage() {
        int i = 0;
        while (true) {
            String[] strArr = mStorageSet;
            if (i >= strArr.length) {
                return 0;
            }
            if (connectStorageWithApplicationUsernameAndPassword(strArr[i]) != 0) {
                long freeUnit = getFreeUnit();
                disconnectStorage();
                if (freeUnit >= 0) {
                    return i + 1;
                }
            }
            if (getErrorCode() == -1205) {
                return 0;
            }
            i++;
        }
    }

    public int connectStorage(String str, String str2, String str3) {
        if (!checkCifsLibStatus()) {
            return 0;
        }
        try {
            int connectStrageNative = sCifsLib.connectStrageNative(getZZTermUtf16Bin(str), str2, str3);
            EPLog.v("CifsAccess", "@@@return <" + connectStrageNative + ">");
            return connectStrageNative;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public int connectStorageWithApplicationUsernameAndPassword(String str) {
        String str2 = "";
        String str3 = "";
        SmbAuthInfo smbAuthInfo = getSmbAuthInfo();
        if (smbAuthInfo != null) {
            str2 = smbAuthInfo.mUserName;
            str3 = smbAuthInfo.mPassword;
        }
        return connectStorage(str, str2, str3);
    }

    public int connectDefaultStorage(int i, String str, String str2) {
        if (i <= 0) {
            return -1;
        }
        return connectStorage(mStorageSet[i - 1], str, str2);
    }

    public int connectDefaultStorageWidthDefaultAthInfo(int i) {
        SmbAuthInfo smbAuthInfo = getSmbAuthInfo();
        if (smbAuthInfo == null) {
            return connectDefaultStorage(i, "", "");
        }
        return connectDefaultStorage(i, smbAuthInfo.mUserName, smbAuthInfo.mPassword);
    }

    public void disconnectStorage() {
        CifsLib cifsLib = sCifsLib;
        if (cifsLib != null) {
            cifsLib.cancel();
            sCifsLib.disConnectStrage();
        }
    }

    public LinkedList<CifsFileInfo> getFileList(String str) {
        boolean equals = "\\".equals(str);
        try {
            byte[] fileList = sCifsLib.getFileList(getZZTermUtf16Bin(str));
            if (fileList == null) {
                return null;
            }
            try {
                return getFileInfoFromBin(fileList);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            }
        } catch (UnsupportedEncodingException e2) {
            e2.printStackTrace();
            return null;
        }
    }

    public long getFileSize(String str) {
        try {
            return sCifsLib.getFileSize(getZZTermUtf16Bin(str));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return -100;
        }
    }

    public long getFreeUnit() {
        return sCifsLib.getFreeUnit();
    }

    public int getUnitSize() {
        return sCifsLib.getUnitSize();
    }

    public long getFreeSize() {
        return ((long) getUnitSize()) * getFreeUnit();
    }

    public int readFromPrinterMemcard(String str, String str2) {
        if (!checkCifsLibStatus()) {
            return -1;
        }
        try {
            return sCifsLib.readFromPrinterMemcard(getZZTermUtf16Bin(str), str2);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return -100;
        }
    }

    public int writeToPrinterMemcard(String str, String str2, int i) throws UnsupportedEncodingException {
        if (!checkCifsLibStatus()) {
            return -1;
        }
        return sCifsLib.writeToPrinterMemcard(str, getZZTermUtf16Bin(str2), i);
    }

    public int createDirectory(String str) {
        if (!checkCifsLibStatus()) {
            return -1;
        }
        try {
            return sCifsLib.createDirectory(getZZTermUtf16Bin(str));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return -100;
        }
    }

    public int deleteDirectory(String str) {
        if (!checkCifsLibStatus()) {
            return -1;
        }
        try {
            return sCifsLib.deleteDirectory(getZZTermUtf16Bin(str));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return -100;
        }
    }

    public int deleteFile(String str) {
        if (!checkCifsLibStatus()) {
            return -1;
        }
        try {
            return sCifsLib.deleteFile(getZZTermUtf16Bin(str));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return -100;
        }
    }

    public synchronized void cancel() {
        if (checkCifsLibStatus()) {
            sCifsLib.cancel();
        }
    }

    public synchronized void cancelByFileSizeZero() {
        if (checkCifsLibStatus()) {
            sCifsLib.cancelByFileSizeZero();
        }
    }

    protected boolean checkCifsLibStatus() {
        return mGotCifsLibSemaphore && sCifsLib != null;
    }

    public int getErrorCode() {
        CifsLib cifsLib = sCifsLib;
        if (cifsLib == null) {
            return 0;
        }
        return cifsLib.getErrorCode();
    }

    public static LinkedList<String> getStringsFromUtf16LeByte(byte[] bArr) {
        String str;
        LinkedList<String> linkedList = new LinkedList<>();
        if (bArr == null) {
            return linkedList;
        }
        int i = 0;
        while (i < bArr.length) {
            int utf16ZZTermDataByteLength = getUtf16ZZTermDataByteLength(bArr, i);
            try {
                str = new String(bArr, i, utf16ZZTermDataByteLength, CharEncoding.UTF_16LE);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                str = "??can_not_decode??";
            }
            linkedList.add(str);
            i += utf16ZZTermDataByteLength + 2;
        }
        return linkedList;
    }

    public static int getUtf16ZZTermDataByteLength(byte[] bArr, int i) {
        if (bArr.length < i) {
            return 0;
        }
        int i2 = i;
        while (true) {
            int i3 = i2 + 2;
            if (bArr.length < i3) {
                return i2 - i;
            }
            if (bArr[i2] == 0 && bArr[i2 + 1] == 0) {
                return i2 - i;
            }
            i2 = i3;
        }
    }

    public static byte[] getZZTermUtf16Bin(String str) throws UnsupportedEncodingException {
        return (str + "\u0000").getBytes(CharEncoding.UTF_16LE);
    }

    static int readLeNByteInt(byte[] bArr, int i, int i2) {
        int i3 = 0;
        for (int i4 = 0; i4 < i2; i4++) {
            i3 |= (bArr[i + i4] & UnsignedBytes.MAX_VALUE) << (i4 * 8);
        }
        return i3;
    }

    public static LinkedList<CifsFileInfo> getFileInfoFromBin(byte[] bArr) throws UnsupportedEncodingException {
        LinkedList<CifsFileInfo> linkedList = new LinkedList<>();
        if (bArr == null) {
            return linkedList;
        }
        int i = 0;
        while (true) {
            int i2 = i + 4;
            int i3 = i2 + 2;
            if (i3 > bArr.length) {
                return linkedList;
            }
            int readLeNByteInt = readLeNByteInt(bArr, i, 4);
            int readLeNByteInt2 = readLeNByteInt(bArr, i2, 2);
            linkedList.add(new CifsFileInfo(new String(bArr, i3, readLeNByteInt2, CharEncoding.UTF_16LE), readLeNByteInt));
            i += readLeNByteInt2 + 6;
        }
    }

    public static String getParentDirectory(String str) {
        return (str == null || str.length() <= 1) ? "" : str.replaceFirst("\\\\[^\\\\]+\\\\?$", "");
    }

    public static String join(String str, String str2) {
        if (str.endsWith("\\")) {
            return str + str2;
        }
        return str + "\\" + str2;
    }

    protected CifsAccess() {
        if (sCifsLibFactory == null) {
            sCifsLibFactory = new CifsLibFactory();
        }
    }

    public static CifsAccess getInstance() {
        return new CifsAccess();
    }

    protected static SharedPreferences getCifsAccessSharedPreferences() {
        return IprintApplication.getInstance().getSharedPreferences(SMB_AUTHINFO_SHARED_PREFS_KEY, 0);
    }

    public static void clearSmbAuthInfo() {
        getCifsAccessSharedPreferences().edit().clear().commit();
    }

    public static SmbAuthInfo getSmbAuthInfo() {
        SharedPreferences cifsAccessSharedPreferences = getCifsAccessSharedPreferences();
        return new SmbAuthInfo(cifsAccessSharedPreferences.getString(KEY_SMB_USER_NAME, ""), cifsAccessSharedPreferences.getString("password", ""));
    }

    public static void setSmbAuthInfo(SmbAuthInfo smbAuthInfo) {
        SharedPreferences.Editor edit = getCifsAccessSharedPreferences().edit();
        edit.putString(KEY_SMB_USER_NAME, smbAuthInfo.mUserName);
        edit.putString("password", smbAuthInfo.mPassword);
        edit.commit();
    }

    public static Semaphore getCifsLibSemaphore() {
        return sCifsLibSemaphore;
    }

    protected static class CifsLibFactory {
        protected CifsLibFactory() {
        }

        public CifsLib getCifsLib() {
            return new CifsLib();
        }
    }
}
