From b9f99f99410562516ae17dbe6e58a0b94127a904 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Mon, 2 Apr 2018 10:03:26 -0700 Subject: [PATCH] Manually calculate length of attachments if they're super old Fixes #7560 --- .../securesms/backup/FullBackupExporter.java | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/org/thoughtcrime/securesms/backup/FullBackupExporter.java b/src/org/thoughtcrime/securesms/backup/FullBackupExporter.java index af8d1ef57b..d3aa10b07b 100644 --- a/src/org/thoughtcrime/securesms/backup/FullBackupExporter.java +++ b/src/org/thoughtcrime/securesms/backup/FullBackupExporter.java @@ -20,7 +20,6 @@ import org.thoughtcrime.securesms.crypto.AttachmentSecret; import org.thoughtcrime.securesms.crypto.ClassicDecryptingPartInputStream; import org.thoughtcrime.securesms.crypto.IdentityKeyUtil; import org.thoughtcrime.securesms.crypto.ModernDecryptingPartInputStream; -import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.database.AttachmentDatabase; import org.thoughtcrime.securesms.database.MmsDatabase; import org.thoughtcrime.securesms.database.MmsSmsColumns; @@ -190,7 +189,11 @@ public class FullBackupExporter extends FullBackupBase { String data = cursor.getString(cursor.getColumnIndexOrThrow(AttachmentDatabase.DATA)); byte[] random = cursor.getBlob(cursor.getColumnIndexOrThrow(AttachmentDatabase.DATA_RANDOM)); - if (!TextUtils.isEmpty(data)) { + if (!TextUtils.isEmpty(data) && size <= 0) { + size = calculateVeryOldStreamLength(attachmentSecret, random, data); + } + + if (!TextUtils.isEmpty(data) && size > 0) { InputStream inputStream; if (random != null && random.length == 32) inputStream = ModernDecryptingPartInputStream.createFor(attachmentSecret, random, new File(data), 0); @@ -203,6 +206,23 @@ public class FullBackupExporter extends FullBackupBase { } } + private static long calculateVeryOldStreamLength(@NonNull AttachmentSecret attachmentSecret, @Nullable byte[] random, @NonNull String data) throws IOException { + long result = 0; + InputStream inputStream; + + if (random != null && random.length == 32) inputStream = ModernDecryptingPartInputStream.createFor(attachmentSecret, random, new File(data), 0); + else inputStream = ClassicDecryptingPartInputStream.createFor(attachmentSecret, new File(data)); + + int read; + byte[] buffer = new byte[8192]; + + while ((read = inputStream.read(buffer, 0, buffer.length)) != -1) { + result += read; + } + + return result; + } + private static class BackupFrameOutputStream extends BackupStream { private final OutputStream outputStream;