Correction bugs compil checksum

pull/10/head
js 5 years ago
parent 9530ad1004
commit 859c9146d9

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

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

Loading…
Cancel
Save