Fix bug in BytesArrayTools reading

pull/10/head
Louis Royer 5 years ago
parent 859c9146d9
commit 460964bd6e

@ -243,9 +243,9 @@ public class ProtocolP2PDatagram {
this.hostR = hostR;
}
private int computeCheckSum(byte [] datagram){
private int computeCheckSum(byte [] datagram) throws SizeError {
int checksum = 0;
for (int i=CHECKSUM_POSITION+2; i<datagram.length; ++i){
for (int i=CHECKSUM_POSITION+2; i<CHECKSUM_POSITION+2+4+Payload.getPayloadSize(datagram); ++i){
checksum += BytesArrayTools.readInt16Bits(datagram,i);
checksum &= 0xffff;
}
@ -253,8 +253,8 @@ public class ProtocolP2PDatagram {
}
private void setCheckSum(byte [] datagram) throws InternalError {
int checksum = computeCheckSum(datagram);
try {
int checksum = computeCheckSum(datagram);
BytesArrayTools.write16Bits(datagram,CHECKSUM_POSITION,checksum);
} catch (SizeError e) {
throw new InternalError();
@ -262,10 +262,14 @@ public class ProtocolP2PDatagram {
}
private void checkCheckSum(byte [] datagram) throws TransmissionError {
try {
int checksum = BytesArrayTools.readInt16Bits(datagram,CHECKSUM_POSITION);
int computedCheckSum = computeCheckSum(datagram);
if (computedCheckSum != checksum){
throw new TransmissionError();
}
} catch(SizeError e) {
throw new TransmissionError();
}
}
}

@ -53,7 +53,7 @@ public class BytesArrayTools {
public static int readInt(byte[] array, int start) throws SizeError {
int size = 0;
for(int i=0;i<4;i++) {
size |= ((int)array[start + i]) << (8* (3 -i));
size |= (0xFF & (int)array[start + i]) << (8* (3 -i));
}
if (size < 0) {
// Size in array is probably correct
@ -72,11 +72,12 @@ public class BytesArrayTools {
int size = 0;
for(int i=0;i<2;i++) {
try {
size |= ((int)array[start + i]) << (8* (1 -i));
size |= (0xFF & (int)array[start + i]) << (8* (1 -i));
} catch (ArrayIndexOutOfBoundsException e){
// 0 padding
}
}
assert (size & 0xFFFF0000) == 0 : "Read on more than 16 bits";
return size;
}
@ -89,7 +90,7 @@ public class BytesArrayTools {
public static long readLong(byte[] array, int start) throws SizeError {
long size = 0;
for(int i=0;i<8;i++) {
size |= ((int)array[start + i]) << (8* (7 - i));
size |= (0xFF & (int)array[start + i]) << (8* (7 - i));
}
if (size < 0) {
// Size in array is probably correct

Loading…
Cancel
Save