Fix bug in BytesArrayTools reading
This commit is contained in:
parent
859c9146d9
commit
460964bd6e
@ -243,28 +243,32 @@ public class ProtocolP2PDatagram {
|
|||||||
this.hostR = hostR;
|
this.hostR = hostR;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int computeCheckSum(byte [] datagram){
|
private int computeCheckSum(byte [] datagram) throws SizeError {
|
||||||
int checksum = 0;
|
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 += BytesArrayTools.readInt16Bits(datagram,i);
|
||||||
checksum &= 0xffff;
|
checksum &= 0xffff;
|
||||||
}
|
}
|
||||||
return checksum ^ 0xffff;
|
return checksum ^ 0xffff;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setCheckSum(byte [] datagram) throws InternalError{
|
private void setCheckSum(byte [] datagram) throws InternalError {
|
||||||
int checksum = computeCheckSum(datagram);
|
try {
|
||||||
try{
|
int checksum = computeCheckSum(datagram);
|
||||||
BytesArrayTools.write16Bits(datagram,CHECKSUM_POSITION,checksum);
|
BytesArrayTools.write16Bits(datagram,CHECKSUM_POSITION,checksum);
|
||||||
} catch (SizeError e) {
|
} catch (SizeError e) {
|
||||||
throw new InternalError();
|
throw new InternalError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkCheckSum(byte [] datagram) throws TransmissionError{
|
private void checkCheckSum(byte [] datagram) throws TransmissionError {
|
||||||
int checksum = BytesArrayTools.readInt16Bits(datagram,CHECKSUM_POSITION);
|
try {
|
||||||
int computedCheckSum = computeCheckSum(datagram);
|
int checksum = BytesArrayTools.readInt16Bits(datagram,CHECKSUM_POSITION);
|
||||||
if (computedCheckSum != checksum){
|
int computedCheckSum = computeCheckSum(datagram);
|
||||||
|
if (computedCheckSum != checksum){
|
||||||
|
throw new TransmissionError();
|
||||||
|
}
|
||||||
|
} catch(SizeError e) {
|
||||||
throw new TransmissionError();
|
throw new TransmissionError();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ public class BytesArrayTools {
|
|||||||
public static int readInt(byte[] array, int start) throws SizeError {
|
public static int readInt(byte[] array, int start) throws SizeError {
|
||||||
int size = 0;
|
int size = 0;
|
||||||
for(int i=0;i<4;i++) {
|
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) {
|
if (size < 0) {
|
||||||
// Size in array is probably correct
|
// Size in array is probably correct
|
||||||
@ -72,11 +72,12 @@ public class BytesArrayTools {
|
|||||||
int size = 0;
|
int size = 0;
|
||||||
for(int i=0;i<2;i++) {
|
for(int i=0;i<2;i++) {
|
||||||
try {
|
try {
|
||||||
size |= ((int)array[start + i]) << (8* (1 -i));
|
size |= (0xFF & (int)array[start + i]) << (8* (1 -i));
|
||||||
} catch (ArrayIndexOutOfBoundsException e){
|
} catch (ArrayIndexOutOfBoundsException e){
|
||||||
// 0 padding
|
// 0 padding
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assert (size & 0xFFFF0000) == 0 : "Read on more than 16 bits";
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -89,7 +90,7 @@ public class BytesArrayTools {
|
|||||||
public static long readLong(byte[] array, int start) throws SizeError {
|
public static long readLong(byte[] array, int start) throws SizeError {
|
||||||
long size = 0;
|
long size = 0;
|
||||||
for(int i=0;i<8;i++) {
|
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) {
|
if (size < 0) {
|
||||||
// Size in array is probably correct
|
// Size in array is probably correct
|
||||||
|
Loading…
Reference in New Issue
Block a user