mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-27 04:04:43 +01:00
Replace other limiting streams with TruncatingInputStream.
This commit is contained in:
committed by
Cody Henthorne
parent
b00855b097
commit
a6767e4f8a
@@ -6,12 +6,12 @@
|
||||
|
||||
package org.whispersystems.signalservice.api.crypto;
|
||||
|
||||
import org.signal.core.util.stream.TruncatingInputStream;
|
||||
import org.signal.libsignal.protocol.InvalidMessageException;
|
||||
import org.signal.libsignal.protocol.incrementalmac.ChunkSizeChoice;
|
||||
import org.signal.libsignal.protocol.incrementalmac.IncrementalMacInputStream;
|
||||
import org.signal.libsignal.protocol.kdf.HKDF;
|
||||
import org.whispersystems.signalservice.api.backup.BackupKey;
|
||||
import org.whispersystems.signalservice.internal.util.ContentLengthInputStream;
|
||||
import org.whispersystems.signalservice.internal.util.Util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -25,7 +25,6 @@ import java.security.InvalidKeyException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
@@ -118,7 +117,7 @@ public class AttachmentCipherInputStream extends FilterInputStream {
|
||||
InputStream inputStream = new AttachmentCipherInputStream(wrappedStream, parts[0], streamLength - BLOCK_SIZE - mac.getMacLength());
|
||||
|
||||
if (plaintextLength != 0) {
|
||||
inputStream = new ContentLengthInputStream(inputStream, plaintextLength);
|
||||
inputStream = new TruncatingInputStream(inputStream, plaintextLength);
|
||||
}
|
||||
|
||||
return inputStream;
|
||||
@@ -143,7 +142,7 @@ public class AttachmentCipherInputStream extends FilterInputStream {
|
||||
InputStream inputStream = new AttachmentCipherInputStream(new FileInputStream(file), archivedMediaKeyMaterial.getCipherKey(), file.length() - BLOCK_SIZE - mac.getMacLength());
|
||||
|
||||
if (originalCipherTextLength != 0) {
|
||||
inputStream = new ContentLengthInputStream(inputStream, originalCipherTextLength);
|
||||
inputStream = new TruncatingInputStream(inputStream, originalCipherTextLength);
|
||||
}
|
||||
|
||||
return inputStream;
|
||||
@@ -180,7 +179,7 @@ public class AttachmentCipherInputStream extends FilterInputStream {
|
||||
InputStream inputStream = new AttachmentCipherInputStream(wrappedStream, parts[0], file.length() - BLOCK_SIZE - mac.getMacLength());
|
||||
|
||||
if (plaintextLength != 0) {
|
||||
inputStream = new ContentLengthInputStream(inputStream, plaintextLength);
|
||||
inputStream = new TruncatingInputStream(inputStream, plaintextLength);
|
||||
}
|
||||
|
||||
return inputStream;
|
||||
|
||||
@@ -26,83 +26,4 @@ public class ChunkedInputStream {
|
||||
}
|
||||
throw new IOException("Malformed varint!");
|
||||
}
|
||||
|
||||
protected static final class LimitedInputStream extends InputStream {
|
||||
|
||||
private final InputStream in;
|
||||
|
||||
private long left;
|
||||
private long mark = -1;
|
||||
|
||||
LimitedInputStream(InputStream in, long limit) {
|
||||
this.in = in;
|
||||
this.left = limit;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int available() throws IOException {
|
||||
return (int) Math.min(in.available(), left);
|
||||
}
|
||||
|
||||
// it's okay to mark even if mark isn't supported, as reset won't work
|
||||
@Override
|
||||
public synchronized void mark(int readLimit) {
|
||||
in.mark(readLimit);
|
||||
mark = left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
if (left == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int result = in.read();
|
||||
if (result != -1) {
|
||||
--left;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] b, int off, int len) throws IOException {
|
||||
if (left == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
len = (int) Math.min(len, left);
|
||||
int result = in.read(b, off, len);
|
||||
if (result != -1) {
|
||||
left -= result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void reset() throws IOException {
|
||||
if (!in.markSupported()) {
|
||||
throw new IOException("Mark not supported");
|
||||
}
|
||||
if (mark == -1) {
|
||||
throw new IOException("Mark not set");
|
||||
}
|
||||
|
||||
in.reset();
|
||||
left = mark;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long skip(long n) throws IOException {
|
||||
n = Math.min(n, left);
|
||||
long skipped = in.skip(n);
|
||||
left -= skipped;
|
||||
return skipped;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
package org.whispersystems.signalservice.api.messages.multidevice;
|
||||
|
||||
import org.signal.core.util.stream.TruncatingInputStream;
|
||||
import org.signal.libsignal.protocol.IdentityKey;
|
||||
import org.signal.libsignal.protocol.InvalidKeyException;
|
||||
import org.signal.libsignal.protocol.InvalidMessageException;
|
||||
@@ -61,7 +62,7 @@ public class DeviceContactsInputStream extends ChunkedInputStream {
|
||||
|
||||
if (details.avatar != null && details.avatar.length != null) {
|
||||
long avatarLength = details.avatar.length;
|
||||
InputStream avatarStream = new LimitedInputStream(in, avatarLength);
|
||||
InputStream avatarStream = new TruncatingInputStream(in, avatarLength);
|
||||
String avatarContentType = details.avatar.contentType != null ? details.avatar.contentType : "image/*";
|
||||
|
||||
avatar = Optional.of(new DeviceContactAvatar(avatarStream, avatarLength, avatarContentType));
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
package org.whispersystems.signalservice.internal.util;
|
||||
|
||||
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class ContentLengthInputStream extends FilterInputStream {
|
||||
|
||||
private long bytesRemaining;
|
||||
|
||||
public ContentLengthInputStream(InputStream inputStream, long contentLength) {
|
||||
super(inputStream);
|
||||
this.bytesRemaining = contentLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
if (bytesRemaining == 0) return -1;
|
||||
int result = super.read();
|
||||
bytesRemaining--;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer) throws IOException {
|
||||
return read(buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||
if (bytesRemaining == 0) return -1;
|
||||
|
||||
int result = super.read(buffer, offset, Math.min(length, Util.toIntExact(bytesRemaining)));
|
||||
|
||||
bytesRemaining -= result;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user