package org.jmrtd; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.Serializable; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; import net.sf.scuba.smartcards.CardServiceException; import net.sf.scuba.smartcards.FileInfo; import net.sf.scuba.smartcards.FileSystemStructured; import net.sf.scuba.tlv.TLVInputStream; import org.jmrtd.io.FragmentBuffer; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public class MRTDFileSystem implements FileSystemStructured, Serializable { static final boolean $assertionsDisabled = false; private static final Logger LOGGER = Logger.getLogger("org.jmrtd"); private static final int READ_AHEAD_LENGTH = 8; private static final long serialVersionUID = -4357282016708205020L; private PassportService service; private Map fileInfos = new HashMap(); private short selectedFID = 0; private boolean isSelected = false; public MRTDFileSystem(PassportService passportService) { this.service = passportService; } @Override // net.sf.scuba.smartcards.FileSystemStructured public FileInfo[] getSelectedPath() throws CardServiceException { synchronized (this) { MRTDFileInfo fileInfo = getFileInfo(); if (fileInfo == null) { return null; } return new MRTDFileInfo[]{fileInfo}; } } @Override // net.sf.scuba.smartcards.FileSystemStructured public void selectFile(short s) throws CardServiceException { synchronized (this) { if (this.selectedFID == s) { return; } this.selectedFID = s; this.isSelected = false; } } @Override // net.sf.scuba.smartcards.FileSystemStructured public byte[] readBinary(int i, int i2) throws CardServiceException { byte[] bArr; synchronized (this) { try { try { short s = this.selectedFID; if (s <= 0) { throw new CardServiceException("No file selected"); } boolean z = i > 32767; if (!this.isSelected) { this.service.sendSelectFile(s); this.isSelected = true; } MRTDFileInfo fileInfo = getFileInfo(); FragmentBuffer.Fragment smallestUnbufferedFragment = fileInfo.getSmallestUnbufferedFragment(i, i2); if (smallestUnbufferedFragment.getLength() > 0) { byte[] sendReadBinary = this.service.sendReadBinary(smallestUnbufferedFragment.getOffset(), smallestUnbufferedFragment.getLength(), z); fileInfo.addFragment(smallestUnbufferedFragment.getOffset(), sendReadBinary); i2 = sendReadBinary.length; } bArr = new byte[i2]; System.arraycopy(fileInfo.getBuffer(), i, bArr, 0, i2); } catch (Exception unused) { StringBuilder sb = new StringBuilder("Read binary failed on file "); sb.append((Object) (0 == 0 ? Integer.toHexString(this.selectedFID) : null)); throw new CardServiceException(sb.toString()); } } catch (CardServiceException e) { StringBuilder sb2 = new StringBuilder("Read binary failed on file "); sb2.append((Object) (0 == 0 ? Integer.toHexString(this.selectedFID) : null)); sb2.append(": "); sb2.append(e.getMessage()); throw new CardServiceException(sb2.toString(), e.getSW()); } } return bArr; } private MRTDFileInfo getFileInfo() throws CardServiceException { int length; synchronized (this) { short s = this.selectedFID; if (s <= 0) { throw new CardServiceException("No file selected"); } MRTDFileInfo mRTDFileInfo = this.fileInfos.get(Short.valueOf(s)); if (mRTDFileInfo != null) { return mRTDFileInfo; } try { if (!this.isSelected) { this.service.sendSelectFile(this.selectedFID); this.isSelected = true; } byte[] sendReadBinary = this.service.sendReadBinary(0, 8, false); if (sendReadBinary != null && sendReadBinary.length == 8) { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(sendReadBinary); TLVInputStream tLVInputStream = new TLVInputStream(byteArrayInputStream); if (tLVInputStream.b() == 66) { length = 36; } else { length = (sendReadBinary.length - byteArrayInputStream.available()) + tLVInputStream.c(); } MRTDFileInfo mRTDFileInfo2 = new MRTDFileInfo(this.selectedFID, length); mRTDFileInfo2.addFragment(0, sendReadBinary); this.fileInfos.put(Short.valueOf(this.selectedFID), mRTDFileInfo2); return mRTDFileInfo2; } Logger logger = LOGGER; StringBuilder sb = new StringBuilder("Something is wrong with prefix, prefix = "); sb.append(Arrays.toString(sendReadBinary)); logger.severe(sb.toString()); return null; } catch (IOException e) { StringBuilder sb2 = new StringBuilder(); sb2.append(e.toString()); sb2.append(" getting file info for "); sb2.append(Integer.toHexString(this.selectedFID)); throw new CardServiceException(sb2.toString()); } } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public static class MRTDFileInfo extends FileInfo implements Serializable { private static final long serialVersionUID = 6727369753765119839L; private FragmentBuffer buffer; private short fid; public MRTDFileInfo(short s, int i) { this.fid = s; this.buffer = new FragmentBuffer(i); } public byte[] getBuffer() { return this.buffer.getBuffer(); } @Override // net.sf.scuba.smartcards.FileInfo public int getFileLength() { return this.buffer.getLength(); } public String toString() { return Integer.toHexString(this.fid); } public FragmentBuffer.Fragment getSmallestUnbufferedFragment(int i, int i2) { return this.buffer.getSmallestUnbufferedFragment(i, i2); } public void addFragment(int i, byte[] bArr) { this.buffer.addFragment(i, bArr); } @Override // net.sf.scuba.smartcards.FileInfo public short getFID() { return this.fid; } } }