Fix bugs in encodeBigInteger() and RLP.encodeLength()

This commit is contained in:
nicksavers 2014-05-15 14:03:13 +02:00
parent c1d619bc92
commit 41862c44a1
1 changed files with 13 additions and 3 deletions

View File

@ -744,7 +744,11 @@ public class RLP {
byte firstByte = (byte) (length + offset); byte firstByte = (byte) (length + offset);
return new byte[] { firstByte }; return new byte[] { firstByte };
} else if (length < MAX_ITEM_LENGTH) { } else if (length < MAX_ITEM_LENGTH) {
byte[] binaryLength = BigInteger.valueOf(length).toByteArray(); byte[] binaryLength;
if(length < 0xFF)
binaryLength = new byte[] { (byte) length };
else
binaryLength = BigInteger.valueOf(length).toByteArray();
byte firstByte = (byte) (binaryLength.length + offset + SIZE_THRESHOLD - 1 ); byte firstByte = (byte) (binaryLength.length + offset + SIZE_THRESHOLD - 1 );
return concatenate(new byte[] { firstByte }, binaryLength); return concatenate(new byte[] { firstByte }, binaryLength);
} else { } else {
@ -777,6 +781,9 @@ public class RLP {
} }
public static byte[] encodeBigInteger(BigInteger srcBigInteger) { public static byte[] encodeBigInteger(BigInteger srcBigInteger) {
if(srcBigInteger == BigInteger.ZERO)
return encodeByte((byte)0);
else
return encodeElement(srcBigInteger.toByteArray()); return encodeElement(srcBigInteger.toByteArray());
} }
@ -864,6 +871,9 @@ public class RLP {
} else if (input instanceof String) { } else if (input instanceof String) {
String inputString = (String) input; String inputString = (String) input;
return inputString.getBytes(); return inputString.getBytes();
} else if(input instanceof Long) {
Long inputLong = (Long) input;
return (inputLong == 0) ? new byte[0] : BigInteger.valueOf(inputLong).toByteArray();
} else if(input instanceof Integer) { } else if(input instanceof Integer) {
Integer inputInt = (Integer) input; Integer inputInt = (Integer) input;
return (inputInt == 0) ? new byte[0] : BigInteger.valueOf(inputInt.longValue()).toByteArray(); return (inputInt == 0) ? new byte[0] : BigInteger.valueOf(inputInt.longValue()).toByteArray();