fixed encrypted ibb file transfer which was broken with ART. fixes #1172

This commit is contained in:
Daniel Gultsch 2015-05-07 14:19:51 +02:00
parent 05f0aa614f
commit e0653c0371
2 changed files with 34 additions and 27 deletions

View File

@ -99,7 +99,7 @@ public class JingleConnection implements Downloadable {
file.delete(); file.delete();
} }
} }
Log.d(Config.LOGTAG,"sucessfully transmitted file:" + file.getAbsolutePath()); Log.d(Config.LOGTAG,"successfully transmitted file:" + file.getAbsolutePath()+" ("+file.getSha1Sum()+")");
if (message.getEncryption() != Message.ENCRYPTION_PGP) { if (message.getEncryption() != Message.ENCRYPTION_PGP) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file)); intent.setData(Uri.fromFile(file));

View File

@ -8,7 +8,9 @@ import java.security.NoSuchAlgorithmException;
import java.util.Arrays; import java.util.Arrays;
import android.util.Base64; import android.util.Base64;
import android.util.Log;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.DownloadableFile;
import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.persistance.FileBackend;
@ -110,7 +112,11 @@ public class JingleInbandTransport extends JingleTransport {
this.onFileTransmissionStatusChanged = callback; this.onFileTransmissionStatusChanged = callback;
this.file = file; this.file = file;
try { try {
this.remainingSize = this.file.getSize(); if (this.file.getKey() != null) {
this.remainingSize = (this.file.getSize() / 16 + 1) * 16;
} else {
this.remainingSize = this.file.getSize();
}
this.fileSize = this.remainingSize; this.fileSize = this.remainingSize;
this.digest = MessageDigest.getInstance("SHA-1"); this.digest = MessageDigest.getInstance("SHA-1");
this.digest.reset(); this.digest.reset();
@ -150,29 +156,33 @@ public class JingleInbandTransport extends JingleTransport {
byte[] buffer = new byte[this.bufferSize]; byte[] buffer = new byte[this.bufferSize];
try { try {
int count = fileInputStream.read(buffer); int count = fileInputStream.read(buffer);
if (count == -1) { this.remainingSize -= count;
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest())); if (count != buffer.length && count != -1) {
fileInputStream.close(); int rem = fileInputStream.read(buffer,count,buffer.length-count);
this.onFileTransmissionStatusChanged.onFileTransmitted(file); if (rem > 0) {
} else { count += rem;
this.remainingSize -= count; }
this.digest.update(buffer); }
String base64 = Base64.encodeToString(buffer, Base64.NO_WRAP); this.digest.update(buffer,0,count);
IqPacket iq = new IqPacket(IqPacket.TYPE.SET); String base64 = Base64.encodeToString(buffer,0,count, Base64.NO_WRAP);
iq.setTo(this.counterpart); IqPacket iq = new IqPacket(IqPacket.TYPE.SET);
Element data = iq.addChild("data", iq.setTo(this.counterpart);
"http://jabber.org/protocol/ibb"); Element data = iq.addChild("data", "http://jabber.org/protocol/ibb");
data.setAttribute("seq", Integer.toString(this.seq)); data.setAttribute("seq", Integer.toString(this.seq));
data.setAttribute("block-size", data.setAttribute("block-size", Integer.toString(this.blockSize));
Integer.toString(this.blockSize)); data.setAttribute("sid", this.sessionId);
data.setAttribute("sid", this.sessionId); data.setContent(base64);
data.setContent(base64); this.account.getXmppConnection().sendIqPacket(iq, this.onAckReceived);
this.account.getXmppConnection().sendIqPacket(iq, this.seq++;
this.onAckReceived); if (this.remainingSize > 0) {
this.seq++;
connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100)); connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100));
} else {
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
this.onFileTransmissionStatusChanged.onFileTransmitted(file);
fileInputStream.close();
} }
} catch (IOException e) { } catch (IOException e) {
Log.d(Config.LOGTAG,e.getMessage());
FileBackend.close(fileInputStream); FileBackend.close(fileInputStream);
this.onFileTransmissionStatusChanged.onFileTransferAborted(); this.onFileTransmissionStatusChanged.onFileTransferAborted();
} }
@ -182,14 +192,10 @@ public class JingleInbandTransport extends JingleTransport {
try { try {
byte[] buffer = Base64.decode(data, Base64.NO_WRAP); byte[] buffer = Base64.decode(data, Base64.NO_WRAP);
if (this.remainingSize < buffer.length) { if (this.remainingSize < buffer.length) {
buffer = Arrays buffer = Arrays.copyOfRange(buffer, 0, (int) this.remainingSize);
.copyOfRange(buffer, 0, (int) this.remainingSize);
} }
this.remainingSize -= buffer.length; this.remainingSize -= buffer.length;
this.fileOutputStream.write(buffer); this.fileOutputStream.write(buffer);
this.digest.update(buffer); this.digest.update(buffer);
if (this.remainingSize <= 0) { if (this.remainingSize <= 0) {
file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest())); file.setSha1Sum(CryptoHelper.bytesToHex(digest.digest()));
@ -200,6 +206,7 @@ public class JingleInbandTransport extends JingleTransport {
connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100)); connection.updateProgress((int) ((((double) (this.fileSize - this.remainingSize)) / this.fileSize) * 100));
} }
} catch (IOException e) { } catch (IOException e) {
Log.d(Config.LOGTAG,e.getMessage());
FileBackend.close(fileOutputStream); FileBackend.close(fileOutputStream);
this.onFileTransmissionStatusChanged.onFileTransferAborted(); this.onFileTransmissionStatusChanged.onFileTransferAborted();
} }