CifsAccess.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. package com.epson.memcardacc;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import com.google.common.primitives.UnsignedBytes;
  5. import epson.print.IprintApplication;
  6. import epson.print.Util.EPLog;
  7. import java.io.UnsupportedEncodingException;
  8. import java.util.LinkedList;
  9. import java.util.TimeZone;
  10. import java.util.concurrent.Semaphore;
  11. import java.util.concurrent.TimeUnit;
  12. import org.apache.commons.lang.CharEncoding;
  13. import org.apache.commons.lang.time.DateUtils;
  14. public class CifsAccess {
  15. public static final int CHECK_STORAGE_ERROR = 0;
  16. public static final int CIFS_ERROR_LOGIN_FAILURE = -1205;
  17. public static final int CIFS_ERROR_PATH_EXISTED = -1203;
  18. public static final String KEY_SMB_PASSWORD = "password";
  19. public static final String KEY_SMB_USER_NAME = "smb_user_name";
  20. public static final String SMB_AUTHINFO_SHARED_PREFS_KEY = "smb_authinfo";
  21. public static final int STORAGE_SET_TYPE_GENERIC_MEMCARD_COPY = 1;
  22. public static final int STORAGE_SET_TYPE_PHOTO_COPY = 2;
  23. private static CifsLib sCifsLib;
  24. private static CifsLibFactory sCifsLibFactory;
  25. protected static final Semaphore sCifsLibSemaphore = new Semaphore(1);
  26. private boolean mGotCifsLibSemaphore;
  27. private String[] mStorageSet;
  28. protected static void setCifsFactory(CifsLibFactory cifsLibFactory) {
  29. }
  30. public int initDefault(Context context, int i) {
  31. if (i != 2) {
  32. mStorageSet = MemcardConfig.GENERIC_STORAGE_SET;
  33. } else {
  34. mStorageSet = MemcardConfig.PHOTO_COPY_STORAGE_SET;
  35. }
  36. try {
  37. return init(MemcardUtil.getPrinterIpAddress(context), TimeZone.getDefault().getRawOffset() / DateUtils.MILLIS_IN_HOUR);
  38. } catch (UnsupportedEncodingException e) {
  39. e.printStackTrace();
  40. return 0;
  41. }
  42. }
  43. /* access modifiers changed from: protected */
  44. /* JADX WARNING: Code restructure failed: missing block: B:31:0x0048, code lost:
  45. return 0;
  46. */
  47. /* Code decompiled incorrectly, please refer to instructions dump. */
  48. public synchronized int init(java.lang.String r4, int r5) throws java.io.UnsupportedEncodingException {
  49. /*
  50. r3 = this;
  51. monitor-enter(r3)
  52. r0 = 0
  53. if (r4 == 0) goto L_0x0047
  54. int r1 = r4.length() // Catch:{ all -> 0x0044 }
  55. if (r1 > 0) goto L_0x000b
  56. goto L_0x0047
  57. L_0x000b:
  58. boolean r1 = r3.mGotCifsLibSemaphore // Catch:{ all -> 0x0044 }
  59. if (r1 == 0) goto L_0x0011
  60. monitor-exit(r3)
  61. return r0
  62. L_0x0011:
  63. boolean r1 = r3.acquireCifsLibSemaphore() // Catch:{ all -> 0x0044 }
  64. if (r1 != 0) goto L_0x0019
  65. monitor-exit(r3)
  66. return r0
  67. L_0x0019:
  68. com.epson.memcardacc.CifsLib r1 = sCifsLib // Catch:{ all -> 0x0044 }
  69. if (r1 == 0) goto L_0x0027
  70. java.lang.String r1 = "CifsAccess"
  71. java.lang.String r2 = "in init() sCifsLib != null"
  72. epson.print.Util.EPLog.w((java.lang.String) r1, (java.lang.String) r2) // Catch:{ all -> 0x0044 }
  73. r3.freeCifsLib() // Catch:{ all -> 0x0044 }
  74. L_0x0027:
  75. com.epson.memcardacc.CifsAccess$CifsLibFactory r1 = sCifsLibFactory // Catch:{ all -> 0x0044 }
  76. com.epson.memcardacc.CifsLib r1 = r1.getCifsLib() // Catch:{ all -> 0x0044 }
  77. sCifsLib = r1 // Catch:{ all -> 0x0044 }
  78. java.lang.String r1 = "US-ASCII"
  79. byte[] r4 = r4.getBytes(r1) // Catch:{ all -> 0x0044 }
  80. com.epson.memcardacc.CifsLib r1 = sCifsLib // Catch:{ all -> 0x0044 }
  81. int r4 = r1.init(r4, r5) // Catch:{ all -> 0x0044 }
  82. if (r4 != 0) goto L_0x0042
  83. r3.free() // Catch:{ all -> 0x0044 }
  84. monitor-exit(r3)
  85. return r0
  86. L_0x0042:
  87. monitor-exit(r3)
  88. return r4
  89. L_0x0044:
  90. r4 = move-exception
  91. monitor-exit(r3)
  92. throw r4
  93. L_0x0047:
  94. monitor-exit(r3)
  95. return r0
  96. */
  97. throw new UnsupportedOperationException("Method not decompiled: com.epson.memcardacc.CifsAccess.init(java.lang.String, int):int");
  98. }
  99. protected void freeCifsLib() {
  100. CifsLib cifsLib = sCifsLib;
  101. if (cifsLib != null) {
  102. cifsLib.free();
  103. sCifsLib = null;
  104. }
  105. }
  106. public synchronized void free() {
  107. freeCifsLib();
  108. releaseCifsLibSemaphore();
  109. }
  110. protected synchronized boolean acquireCifsLibSemaphore() {
  111. try {
  112. if (!getCifsLibSemaphore().tryAcquire(60, TimeUnit.SECONDS)) {
  113. EPLog.w("CifsAccess", "cifsSemaphore.tryAcquire() can not get semaphore.");
  114. return false;
  115. }
  116. mGotCifsLibSemaphore = true;
  117. return true;
  118. } catch (InterruptedException unused) {
  119. EPLog.i("CifsAccess", "cifsSemaphore.tryAcquire() interrupted.");
  120. return false;
  121. }
  122. }
  123. protected synchronized void releaseCifsLibSemaphore() {
  124. if (mGotCifsLibSemaphore) {
  125. getCifsLibSemaphore().release();
  126. mGotCifsLibSemaphore = false;
  127. }
  128. }
  129. public LinkedList<String> getStorageNames() {
  130. return getStringsFromUtf16LeByte(sCifsLib.getStrageNames());
  131. }
  132. public int checkStorage() {
  133. int i = 0;
  134. while (true) {
  135. String[] strArr = mStorageSet;
  136. if (i >= strArr.length) {
  137. return 0;
  138. }
  139. if (connectStorageWithApplicationUsernameAndPassword(strArr[i]) != 0) {
  140. long freeUnit = getFreeUnit();
  141. disconnectStorage();
  142. if (freeUnit >= 0) {
  143. return i + 1;
  144. }
  145. }
  146. if (getErrorCode() == -1205) {
  147. return 0;
  148. }
  149. i++;
  150. }
  151. }
  152. public int connectStorage(String str, String str2, String str3) {
  153. if (!checkCifsLibStatus()) {
  154. return 0;
  155. }
  156. try {
  157. int connectStrageNative = sCifsLib.connectStrageNative(getZZTermUtf16Bin(str), str2, str3);
  158. EPLog.m316v("CifsAccess", "@@@return <" + connectStrageNative + ">");
  159. return connectStrageNative;
  160. } catch (UnsupportedEncodingException e) {
  161. e.printStackTrace();
  162. return 0;
  163. }
  164. }
  165. public int connectStorageWithApplicationUsernameAndPassword(String str) {
  166. String str2 = "";
  167. String str3 = "";
  168. SmbAuthInfo smbAuthInfo = getSmbAuthInfo();
  169. if (smbAuthInfo != null) {
  170. str2 = smbAuthInfo.mUserName;
  171. str3 = smbAuthInfo.mPassword;
  172. }
  173. return connectStorage(str, str2, str3);
  174. }
  175. public int connectDefaultStorage(int i, String str, String str2) {
  176. if (i <= 0) {
  177. return -1;
  178. }
  179. return connectStorage(mStorageSet[i - 1], str, str2);
  180. }
  181. public int connectDefaultStorageWidthDefaultAthInfo(int i) {
  182. SmbAuthInfo smbAuthInfo = getSmbAuthInfo();
  183. if (smbAuthInfo == null) {
  184. return connectDefaultStorage(i, "", "");
  185. }
  186. return connectDefaultStorage(i, smbAuthInfo.mUserName, smbAuthInfo.mPassword);
  187. }
  188. public void disconnectStorage() {
  189. CifsLib cifsLib = sCifsLib;
  190. if (cifsLib != null) {
  191. cifsLib.cancel();
  192. sCifsLib.disConnectStrage();
  193. }
  194. }
  195. public LinkedList<CifsFileInfo> getFileList(String str) {
  196. boolean equals = "\\".equals(str);
  197. try {
  198. byte[] fileList = sCifsLib.getFileList(getZZTermUtf16Bin(str));
  199. if (fileList == null) {
  200. return null;
  201. }
  202. try {
  203. return getFileInfoFromBin(fileList);
  204. } catch (UnsupportedEncodingException e) {
  205. e.printStackTrace();
  206. return null;
  207. }
  208. } catch (UnsupportedEncodingException e2) {
  209. e2.printStackTrace();
  210. return null;
  211. }
  212. }
  213. public long getFileSize(String str) {
  214. try {
  215. return sCifsLib.getFileSize(getZZTermUtf16Bin(str));
  216. } catch (UnsupportedEncodingException e) {
  217. e.printStackTrace();
  218. return -100;
  219. }
  220. }
  221. public long getFreeUnit() {
  222. return sCifsLib.getFreeUnit();
  223. }
  224. public int getUnitSize() {
  225. return sCifsLib.getUnitSize();
  226. }
  227. public long getFreeSize() {
  228. return ((long) getUnitSize()) * getFreeUnit();
  229. }
  230. public int readFromPrinterMemcard(String str, String str2) {
  231. if (!checkCifsLibStatus()) {
  232. return -1;
  233. }
  234. try {
  235. return sCifsLib.readFromPrinterMemcard(getZZTermUtf16Bin(str), str2);
  236. } catch (UnsupportedEncodingException e) {
  237. e.printStackTrace();
  238. return -100;
  239. }
  240. }
  241. public int writeToPrinterMemcard(String str, String str2, int i) throws UnsupportedEncodingException {
  242. if (!checkCifsLibStatus()) {
  243. return -1;
  244. }
  245. return sCifsLib.writeToPrinterMemcard(str, getZZTermUtf16Bin(str2), i);
  246. }
  247. public int createDirectory(String str) {
  248. if (!checkCifsLibStatus()) {
  249. return -1;
  250. }
  251. try {
  252. return sCifsLib.createDirectory(getZZTermUtf16Bin(str));
  253. } catch (UnsupportedEncodingException e) {
  254. e.printStackTrace();
  255. return -100;
  256. }
  257. }
  258. public int deleteDirectory(String str) {
  259. if (!checkCifsLibStatus()) {
  260. return -1;
  261. }
  262. try {
  263. return sCifsLib.deleteDirectory(getZZTermUtf16Bin(str));
  264. } catch (UnsupportedEncodingException e) {
  265. e.printStackTrace();
  266. return -100;
  267. }
  268. }
  269. public int deleteFile(String str) {
  270. if (!checkCifsLibStatus()) {
  271. return -1;
  272. }
  273. try {
  274. return sCifsLib.deleteFile(getZZTermUtf16Bin(str));
  275. } catch (UnsupportedEncodingException e) {
  276. e.printStackTrace();
  277. return -100;
  278. }
  279. }
  280. public synchronized void cancel() {
  281. if (checkCifsLibStatus()) {
  282. sCifsLib.cancel();
  283. }
  284. }
  285. public synchronized void cancelByFileSizeZero() {
  286. if (checkCifsLibStatus()) {
  287. sCifsLib.cancelByFileSizeZero();
  288. }
  289. }
  290. protected boolean checkCifsLibStatus() {
  291. return mGotCifsLibSemaphore && sCifsLib != null;
  292. }
  293. public int getErrorCode() {
  294. CifsLib cifsLib = sCifsLib;
  295. if (cifsLib == null) {
  296. return 0;
  297. }
  298. return cifsLib.getErrorCode();
  299. }
  300. public static LinkedList<String> getStringsFromUtf16LeByte(byte[] bArr) {
  301. String str;
  302. LinkedList<String> linkedList = new LinkedList<>();
  303. if (bArr == null) {
  304. return linkedList;
  305. }
  306. int i = 0;
  307. while (i < bArr.length) {
  308. int utf16ZZTermDataByteLength = getUtf16ZZTermDataByteLength(bArr, i);
  309. try {
  310. str = new String(bArr, i, utf16ZZTermDataByteLength, CharEncoding.UTF_16LE);
  311. } catch (UnsupportedEncodingException e) {
  312. e.printStackTrace();
  313. str = "??can_not_decode??";
  314. }
  315. linkedList.add(str);
  316. i += utf16ZZTermDataByteLength + 2;
  317. }
  318. return linkedList;
  319. }
  320. public static int getUtf16ZZTermDataByteLength(byte[] bArr, int i) {
  321. if (bArr.length < i) {
  322. return 0;
  323. }
  324. int i2 = i;
  325. while (true) {
  326. int i3 = i2 + 2;
  327. if (bArr.length < i3) {
  328. return i2 - i;
  329. }
  330. if (bArr[i2] == 0 && bArr[i2 + 1] == 0) {
  331. return i2 - i;
  332. }
  333. i2 = i3;
  334. }
  335. }
  336. public static byte[] getZZTermUtf16Bin(String str) throws UnsupportedEncodingException {
  337. return (str + "\u0000").getBytes(CharEncoding.UTF_16LE);
  338. }
  339. static int readLeNByteInt(byte[] bArr, int i, int i2) {
  340. int i3 = 0;
  341. for (int i4 = 0; i4 < i2; i4++) {
  342. i3 |= (bArr[i + i4] & UnsignedBytes.MAX_VALUE) << (i4 * 8);
  343. }
  344. return i3;
  345. }
  346. public static LinkedList<CifsFileInfo> getFileInfoFromBin(byte[] bArr) throws UnsupportedEncodingException {
  347. LinkedList<CifsFileInfo> linkedList = new LinkedList<>();
  348. if (bArr == null) {
  349. return linkedList;
  350. }
  351. int i = 0;
  352. while (true) {
  353. int i2 = i + 4;
  354. int i3 = i2 + 2;
  355. if (i3 > bArr.length) {
  356. return linkedList;
  357. }
  358. int readLeNByteInt = readLeNByteInt(bArr, i, 4);
  359. int readLeNByteInt2 = readLeNByteInt(bArr, i2, 2);
  360. linkedList.add(new CifsFileInfo(new String(bArr, i3, readLeNByteInt2, CharEncoding.UTF_16LE), readLeNByteInt));
  361. i += readLeNByteInt2 + 6;
  362. }
  363. }
  364. public static String getParentDirectory(String str) {
  365. return (str == null || str.length() <= 1) ? "" : str.replaceFirst("\\\\[^\\\\]+\\\\?$", "");
  366. }
  367. public static String join(String str, String str2) {
  368. if (str.endsWith("\\")) {
  369. return str + str2;
  370. }
  371. return str + "\\" + str2;
  372. }
  373. protected CifsAccess() {
  374. if (sCifsLibFactory == null) {
  375. sCifsLibFactory = new CifsLibFactory();
  376. }
  377. }
  378. public static CifsAccess getInstance() {
  379. return new CifsAccess();
  380. }
  381. protected static SharedPreferences getCifsAccessSharedPreferences() {
  382. return IprintApplication.getInstance().getSharedPreferences(SMB_AUTHINFO_SHARED_PREFS_KEY, 0);
  383. }
  384. public static void clearSmbAuthInfo() {
  385. getCifsAccessSharedPreferences().edit().clear().commit();
  386. }
  387. public static SmbAuthInfo getSmbAuthInfo() {
  388. SharedPreferences cifsAccessSharedPreferences = getCifsAccessSharedPreferences();
  389. return new SmbAuthInfo(cifsAccessSharedPreferences.getString(KEY_SMB_USER_NAME, ""), cifsAccessSharedPreferences.getString("password", ""));
  390. }
  391. public static void setSmbAuthInfo(SmbAuthInfo smbAuthInfo) {
  392. SharedPreferences.Editor edit = getCifsAccessSharedPreferences().edit();
  393. edit.putString(KEY_SMB_USER_NAME, smbAuthInfo.mUserName);
  394. edit.putString("password", smbAuthInfo.mPassword);
  395. edit.commit();
  396. }
  397. public static Semaphore getCifsLibSemaphore() {
  398. return sCifsLibSemaphore;
  399. }
  400. protected static class CifsLibFactory {
  401. protected CifsLibFactory() {
  402. }
  403. public CifsLib getCifsLib() {
  404. return new CifsLib();
  405. }
  406. }
  407. }