Correction bugs compil checksum

This commit is contained in:
js 2020-02-03 14:06:21 +01:00
parent 9530ad1004
commit 859c9146d9
2 changed files with 11 additions and 6 deletions

View File

@ -10,6 +10,7 @@ import remoteException.NotFound;
import remoteException.ProtocolRemoteError;
import remoteException.VersionRemoteError;
import remoteException.EmptyFile;
import tools.BytesArrayTools;
import protocolP2P.Payload;
import protocolP2P.RequestResponseCode;
import protocolP2P.LoadRequest;
@ -243,7 +244,7 @@ public class ProtocolP2PDatagram {
}
private int computeCheckSum(byte [] datagram){
int checksum;
int checksum = 0;
for (int i=CHECKSUM_POSITION+2; i<datagram.length; ++i){
checksum += BytesArrayTools.readInt16Bits(datagram,i);
checksum &= 0xffff;
@ -251,16 +252,20 @@ public class ProtocolP2PDatagram {
return checksum ^ 0xffff;
}
private void setCheckSum(byte [] datagram){
private void setCheckSum(byte [] datagram) throws InternalError{
int checksum = computeCheckSum(datagram);
BytesArrayTools.write16Bits(datagram,CHECKSUM_POSITION,checksum);
try{
BytesArrayTools.write16Bits(datagram,CHECKSUM_POSITION,checksum);
} catch (SizeError e) {
throw new InternalError();
}
}
private void checkCheckSum(byte [] datagram) throws TransmissionError{
int checksum = BytesArrayTools.readInt16Bits(datagram,CHECKSUM_POSITION);
int computedCheckSum = computeCheckSum(datagram);
if (computedCheckSum != checksum){
throws new TransmissionError();
throw new TransmissionError();
}
}
}

View File

@ -24,8 +24,8 @@ public class BytesArrayTools {
* @throws SizeError
*/
public static void write16Bits(byte[] array, int start, int value) throws SizeError {
assert value & 0xFFFF0000 == 0x00000000 : "Wrong size for value, value should be only 16 bits";
if (value & 0xFFFF0000 != 0x00000000){
assert (value & 0xFFFF0000) == 0x00000000 : "Wrong size for value, value should be only 16 bits";
if ((value & 0xFFFF0000) != 0x00000000){
throw new SizeError();
}
for(int i=0;i<2;i++) {