mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-11-01 21:17:13 +00:00
Simplify VarInt implementation.
This commit is contained in:
committed by
Mike Hearn
parent
dc94018992
commit
282db823fc
@@ -16,99 +16,97 @@
|
|||||||
|
|
||||||
package org.bitcoinj.core;
|
package org.bitcoinj.core;
|
||||||
|
|
||||||
import static org.bitcoinj.core.Utils.isLessThanUnsigned;
|
|
||||||
import static org.bitcoinj.core.Utils.isLessThanOrEqualToUnsigned;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A variable-length encoded integer using Satoshis encoding.
|
* A variable-length encoded unsigned integer using Satoshi's encoding (a.k.a. "CompactSize").
|
||||||
*/
|
*/
|
||||||
public class VarInt {
|
public class VarInt {
|
||||||
public final long value;
|
public final long value;
|
||||||
private final int originallyEncodedSize;
|
private final int originallyEncodedSize;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new VarInt with the given unsigned long value.
|
||||||
|
*
|
||||||
|
* @param value the unsigned long value (beware widening conversion of negatives!)
|
||||||
|
*/
|
||||||
public VarInt(long value) {
|
public VarInt(long value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
originallyEncodedSize = getSizeInBytes();
|
originallyEncodedSize = getSizeInBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bitcoin has its own varint format, known in the C++ source as "compact size".
|
/**
|
||||||
|
* Constructs a new VarInt with the value parsed from the specified offset of the given buffer.
|
||||||
|
*
|
||||||
|
* @param buf the buffer containing the value
|
||||||
|
* @param offset the offset of the value
|
||||||
|
*/
|
||||||
public VarInt(byte[] buf, int offset) {
|
public VarInt(byte[] buf, int offset) {
|
||||||
int first = 0xFF & buf[offset];
|
int first = 0xFF & buf[offset];
|
||||||
if (first < 253) {
|
if (first < 253) {
|
||||||
// 8 bits.
|
value = first;
|
||||||
this.value = first;
|
originallyEncodedSize = 1; // 1 data byte (8 bits)
|
||||||
originallyEncodedSize = 1;
|
|
||||||
} else if (first == 253) {
|
} else if (first == 253) {
|
||||||
// 16 bits.
|
value = (0xFF & buf[offset + 1]) | ((0xFF & buf[offset + 2]) << 8);
|
||||||
this.value = (0xFF & buf[offset + 1]) | ((0xFF & buf[offset + 2]) << 8);
|
originallyEncodedSize = 3; // 1 marker + 2 data bytes (16 bits)
|
||||||
originallyEncodedSize = 3;
|
|
||||||
} else if (first == 254) {
|
} else if (first == 254) {
|
||||||
// 32 bits.
|
value = Utils.readUint32(buf, offset + 1);
|
||||||
this.value = Utils.readUint32(buf, offset + 1);
|
originallyEncodedSize = 5; // 1 marker + 4 data bytes (32 bits)
|
||||||
originallyEncodedSize = 5;
|
|
||||||
} else {
|
} else {
|
||||||
// 64 bits.
|
value = Utils.readInt64(buf, offset + 1);
|
||||||
this.value = Utils.readUint32(buf, offset + 1) | (Utils.readUint32(buf, offset + 5) << 32);
|
originallyEncodedSize = 9; // 1 marker + 8 data bytes (64 bits)
|
||||||
originallyEncodedSize = 9;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the number of bytes used to encode this originally if deserialized from a byte array.
|
* Returns the original number of bytes used to encode the value if it was
|
||||||
* Otherwise returns the minimum encoded size
|
* deserialized from a byte array, or the minimum encoded size if it was not.
|
||||||
*/
|
*/
|
||||||
public int getOriginalSizeInBytes() {
|
public int getOriginalSizeInBytes() {
|
||||||
return originallyEncodedSize;
|
return originallyEncodedSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the minimum encoded size of the value stored in this VarInt
|
* Returns the minimum encoded size of the value.
|
||||||
*/
|
*/
|
||||||
public int getSizeInBytes() {
|
public int getSizeInBytes() {
|
||||||
return sizeOf(value);
|
return sizeOf(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the minimum encoded size of the given value.
|
* Returns the minimum encoded size of the given unsigned long value.
|
||||||
|
*
|
||||||
|
* @param value the unsigned long value (beware widening conversion of negatives!)
|
||||||
*/
|
*/
|
||||||
public static int sizeOf(int value) {
|
public static int sizeOf(long value) {
|
||||||
if (value < 253)
|
// if negative, it's actually a very large unsigned long value
|
||||||
return 1;
|
if (value < 0) return 9; // 1 marker + 8 data bytes
|
||||||
else if (value < 65536)
|
if (value < 253) return 1; // 1 data byte
|
||||||
return 3; // 1 marker + 2 data bytes
|
if (value <= 0xFFFFL) return 3; // 1 marker + 2 data bytes
|
||||||
return 5; // 1 marker + 4 data bytes
|
if (value <= 0xFFFFFFFFL) return 5; // 1 marker + 4 data bytes
|
||||||
|
return 9; // 1 marker + 8 data bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the minimum encoded size of the given value.
|
* Encodes the value into its minimal representation.
|
||||||
|
*
|
||||||
|
* @return the minimal encoded bytes of the value
|
||||||
*/
|
*/
|
||||||
public static int sizeOf(long value) {
|
|
||||||
if (isLessThanUnsigned(value, 253))
|
|
||||||
return 1;
|
|
||||||
else if (isLessThanOrEqualToUnsigned(value, 0xFFFFL))
|
|
||||||
return 3; // 1 marker + 2 data bytes
|
|
||||||
else if (isLessThanOrEqualToUnsigned(value, 0xFFFFFFFFL))
|
|
||||||
return 5; // 1 marker + 4 data bytes
|
|
||||||
else
|
|
||||||
return 9; // 1 marker + 8 data bytes
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] encode() {
|
public byte[] encode() {
|
||||||
if (isLessThanUnsigned(value, 253)) {
|
byte[] bytes;
|
||||||
return new byte[]{(byte) value};
|
switch (sizeOf(value)) {
|
||||||
} else if (isLessThanOrEqualToUnsigned(value, 0xFFFFL)) {
|
case 1:
|
||||||
return new byte[]{(byte) 253, (byte) (value), (byte) (value >> 8)};
|
return new byte[]{(byte) value};
|
||||||
} else if (isLessThanOrEqualToUnsigned(value, 0xFFFFFFFFL)) {
|
case 3:
|
||||||
byte[] bytes = new byte[5];
|
return new byte[]{(byte) 253, (byte) (value), (byte) (value >> 8)};
|
||||||
bytes[0] = (byte) 254;
|
case 5:
|
||||||
Utils.uint32ToByteArrayLE(value, bytes, 1);
|
bytes = new byte[5];
|
||||||
return bytes;
|
bytes[0] = (byte) 254;
|
||||||
} else {
|
Utils.uint32ToByteArrayLE(value, bytes, 1);
|
||||||
byte[] bytes = new byte[9];
|
return bytes;
|
||||||
bytes[0] = (byte) 255;
|
default:
|
||||||
Utils.uint32ToByteArrayLE(value, bytes, 1);
|
bytes = new byte[9];
|
||||||
Utils.uint32ToByteArrayLE(value >>> 32, bytes, 5);
|
bytes[0] = (byte) 255;
|
||||||
return bytes;
|
Utils.uint64ToByteArrayLE(value, bytes, 1);
|
||||||
|
return bytes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,14 +20,14 @@ import junit.framework.TestCase;
|
|||||||
|
|
||||||
public class VarIntTest extends TestCase {
|
public class VarIntTest extends TestCase {
|
||||||
public void testBytes() throws Exception {
|
public void testBytes() throws Exception {
|
||||||
VarInt a = new VarInt(10);
|
VarInt a = new VarInt(10); // with widening conversion
|
||||||
assertEquals(1, a.getSizeInBytes());
|
assertEquals(1, a.getSizeInBytes());
|
||||||
assertEquals(1, a.encode().length);
|
assertEquals(1, a.encode().length);
|
||||||
assertEquals(10, new VarInt(a.encode(), 0).value);
|
assertEquals(10, new VarInt(a.encode(), 0).value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testShorts() throws Exception {
|
public void testShorts() throws Exception {
|
||||||
VarInt a = new VarInt(64000);
|
VarInt a = new VarInt(64000); // with widening conversion
|
||||||
assertEquals(3, a.getSizeInBytes());
|
assertEquals(3, a.getSizeInBytes());
|
||||||
assertEquals(3, a.encode().length);
|
assertEquals(3, a.encode().length);
|
||||||
assertEquals(64000, new VarInt(a.encode(), 0).value);
|
assertEquals(64000, new VarInt(a.encode(), 0).value);
|
||||||
@@ -63,4 +63,9 @@ public class VarIntTest extends TestCase {
|
|||||||
byte[] bytes = a.encode();
|
byte[] bytes = a.encode();
|
||||||
assertEquals(0xCAFEBABEDEADBEEFL, new VarInt(bytes, 0).value);
|
assertEquals(0xCAFEBABEDEADBEEFL, new VarInt(bytes, 0).value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testSizeOfNegativeInt() throws Exception {
|
||||||
|
// shouldn't normally be passed, but at least stay consistent (bug regression test)
|
||||||
|
assertEquals(VarInt.sizeOf(-1), new VarInt(-1).encode().length);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user