Switch to varin32 for contact input/output stream headers.

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-06-22 12:09:47 -07:00
parent 4731a34252
commit d044a11bc0
2 changed files with 37 additions and 16 deletions

View File

@ -18,7 +18,7 @@ public class DeviceContactsInputStream {
}
public DeviceContact read() throws IOException {
long detailsLength = readRawVarint64();
long detailsLength = readRawVarint32();
byte[] detailsSerialized = new byte[(int)detailsLength];
Util.readFully(in, detailsSerialized);
@ -38,20 +38,41 @@ public class DeviceContactsInputStream {
return new DeviceContact(number, name, avatar);
}
private long readRawVarint64() throws IOException {
int shift = 0;
long result = 0;
while (shift < 64) {
final byte b = (byte)in.read();
result |= (long)(b & 0x7F) << shift;
if ((b & 0x80) == 0) {
public int readRawVarint32() throws IOException {
byte tmp = (byte)in.read();
if (tmp >= 0) {
return tmp;
}
int result = tmp & 0x7f;
if ((tmp = (byte)in.read()) >= 0) {
result |= tmp << 7;
} else {
result |= (tmp & 0x7f) << 7;
if ((tmp = (byte)in.read()) >= 0) {
result |= tmp << 14;
} else {
result |= (tmp & 0x7f) << 14;
if ((tmp = (byte)in.read()) >= 0) {
result |= tmp << 21;
} else {
result |= (tmp & 0x7f) << 21;
result |= (tmp = (byte)in.read()) << 28;
if (tmp < 0) {
// Discard upper 32 bits.
for (int i = 0; i < 5; i++) {
if ((byte)in.read() >= 0) {
return result;
}
shift += 7;
}
throw new IOException("Malformed varint!");
}
}
}
}
return result;
}
private static final class LimitedInputStream extends FilterInputStream {

View File

@ -56,17 +56,17 @@ public class DeviceContactsOutputStream {
byte[] serializedContactDetails = contactDetails.build().toByteArray();
writeVarint64(serializedContactDetails.length);
writeVarint32(serializedContactDetails.length);
out.write(serializedContactDetails);
}
public void writeVarint64(long value) throws IOException {
private void writeVarint32(int value) throws IOException {
while (true) {
if ((value & ~0x7FL) == 0) {
out.write((int) value);
if ((value & ~0x7F) == 0) {
out.write(value);
return;
} else {
out.write(((int) value & 0x7F) | 0x80);
out.write((value & 0x7F) | 0x80);
value >>>= 7;
}
}