Refactoring

pull/26/head
Louis Royer 5 years ago
parent e621ab159c
commit 44b96d9f2c

@ -229,6 +229,17 @@ public class ClientManagementTCP implements Runnable {
} while(!fileFullyWritten);
if (!Arrays.equals(hash512, computeHashsum(filename, HashAlgorithm.SHA512))) {
System.err.println("Error: Hashsum does not match");
System.err.println("Computed checksum:");
byte[] c = computeHashsum(filename, HashAlgorithm.SHA512);
for (byte b: c) {
System.err.print(String.format("%02X", b));
}
System.err.println("");
System.err.println("Received checksum:");
for (byte b: hash512) {
System.err.print(String.format("%02X", b));
}
System.err.println("");
throw new InternalError();
}
socket.close();

@ -216,6 +216,17 @@ public class ClientManagementUDP implements Runnable {
} while(!fileFullyWritten);
if (!Arrays.equals(hash512, computeHashsum(filename, HashAlgorithm.SHA512))) {
System.err.println("Error: Hashsum does not match");
System.err.println("Computed checksum:");
byte[] c = computeHashsum(filename, HashAlgorithm.SHA512);
for (byte b: c) {
System.err.print(String.format("%02X", b));
}
System.err.println("");
System.err.println("Received checksum:");
for (byte b: hash512) {
System.err.print(String.format("%02X", b));
}
System.err.println("");
throw new InternalError();
}
}

@ -49,11 +49,7 @@ public class FileList extends Payload {
throw new InternalError();
}
int size = getPayloadSize(packet);
try {
fileList = (new String(packet, PAYLOAD_START_POSITION, size, "UTF-8")).split("\n");
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
fileList = BytesArrayTools.readStringArray(packet, PAYLOAD_START_POSITION, size, "\n");
}
/** Returns a byte[] containing Packet with padding.
@ -71,7 +67,7 @@ public class FileList extends Payload {
// set payload size
setPayloadSize(size - PAYLOAD_START_POSITION, packet);
// Write fileList
BytesArrayTools.writeStringArray(packet, fileList, PAYLOAD_START_POSITION, "\n");
BytesArrayTools.write(packet, fileList, PAYLOAD_START_POSITION, "\n");
return packet;
}

@ -90,22 +90,11 @@ public class FilePart extends Payload {
BytesArrayTools.write(packet, TOTAL_FILESIZE_POSITION, totalSize);
// write filenames size to Packet
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filename.length());
try {
// write filename to Packet
int i = FILENAME_POSITION;
for (byte b : filename.getBytes("UTF-8")) {
packet[i] = b;
i += 1;
}
// write partialContent to Packet
for (byte b: partialContent) {
packet[i] = b;
i += 1;
}
return packet;
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
// write filename to Packet
BytesArrayTools.write(packet, filename, FILENAME_POSITION);
// write partialContent to Packet
BytesArrayTools.write(packet, partialContent, FILENAME_POSITION + filename.length());
return packet;
}
/** Write from Packet into offset.
@ -150,11 +139,7 @@ public class FilePart extends Payload {
*/
private void setFilename(byte[] packet) throws ProtocolError, SizeError, InternalError {
int filenameSize = getFilenameSize(packet); // this can throw ProtocolError or SizeError
try {
filename = new String(packet, FILENAME_POSITION, filenameSize, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize);
}
/** Write from Packet into partialContent.
@ -165,11 +150,7 @@ public class FilePart extends Payload {
private void setPartialContent(byte[] packet) throws ProtocolError, SizeError {
int start = FILENAME_POSITION + getFilenameSize(packet); // this can throw SizeError or ProtocolError
int end = OFFSET_POSITION + getPayloadSize(packet); // this can throw SizeError
try {
partialContent = Arrays.copyOfRange(packet, start, end);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ProtocolError();
}
partialContent = BytesArrayTools.readByteArray(packet, start, end);
}
/** partialContent getter.

@ -60,23 +60,18 @@ public class HashRequest extends Payload {
}
/* Read filename */
int filenameSize = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION);
try {
filename = new String(packet, FILENAME_POSITION, filenameSize, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
/* Read filename */
filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize);
/* Read algo list */
int size = getPayloadSize(packet);
try {
String[] l = (new String(packet, FILENAME_POSITION + filenameSize, size, "UTF-8")).split("\n");
int i = 0;
algoList = new HashAlgorithm[l.length];
for(String algo : l) {
algoList[i] = HashAlgorithm.fromName(algo);
i++;
}
} catch (UnsupportedEncodingException e) {
throw new InternalError();
String[] algoListStr = BytesArrayTools.readStringArray(packet, FILENAME_POSITION + filenameSize, size, "\n");
int i = 0;
algoList = new HashAlgorithm[algoListStr.length];
for(String algo : algoListStr) {
algoList[i] = HashAlgorithm.fromName(algo);
i++;
}
}
@ -89,63 +84,24 @@ public class HashRequest extends Payload {
protected byte[] toPacket() throws InternalError {
// compute size
int filenameSize = filename.length();
int size = FILENAME_POSITION + filenameSize;
String[] algoListStr = new String[algoList.length];
int i = 0;
for (HashAlgorithm h : algoList) {
if (h == null) {
continue;
}
size += h.getName().length();
size += 1; // size for '\n'
algoListStr[i] = h.getName();
i++;
}
size -=1; // remove trailing '\n'
int size = FILENAME_POSITION + filenameSize + BytesArrayTools.computeStringArraySize(algoListStr, "\n");
byte[] packet = new byte[size + 1]; // java initialize all to zero
// set request/response code
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
// set Payload size
setPayloadSize(size - FILENAME_SIZE_POSITION, packet);
// write filename size
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filenameSize);
// Write filename
int bCount = FILENAME_POSITION;
try {
byte[] sb = filename.getBytes("UTF-8");
for(byte b : sb) {
packet[bCount] = b;
bCount += 1;
}
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
boolean firstIter = true;
for(HashAlgorithm h : algoList) {
if (h == null) {
continue;
}
String s = h.getName();
if (!firstIter) {
firstIter = false;
try {
packet[bCount] = "\n".getBytes("UTF-8")[0]; // separator
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
bCount += 1;
}
// Copy algoname
try {
byte[] sb = s.getBytes("UTF-8");
for(byte b : sb) {
packet[bCount] = b;
bCount += 1;
}
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
}
// write filename
BytesArrayTools.write(packet, filename, FILENAME_POSITION);
// write algo list
BytesArrayTools.write(packet, algoListStr, FILENAME_POSITION + filename.length(), "\n");
return packet;
}

@ -63,33 +63,22 @@ public class HashResponse extends Payload {
}
/* Read filename */
int filenameSize = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION);
try {
filename = new String(packet, FILENAME_POSITION, filenameSize, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
filename = BytesArrayTools.readString(packet, FILENAME_POSITION, filenameSize);
int size = getPayloadSize(packet);
int start = FILENAME_POSITION + filenameSize;
do {
int algoNameSize = BytesArrayTools.readInt(packet, start);
start +=4;
try {
String algoName = new String(packet, start, algoNameSize, "UTF-8");
start += algoNameSize;
int hashSize = BytesArrayTools.readInt(packet, start);
start += 4;
if (hashSize != 0) {
byte[] b = new byte[hashSize];
for (int i=0;i<hashSize;i++) {
b[i] = packet[start+i];
}
hashes.put(HashAlgorithm.fromName(algoName), b);
}
start += hashSize;
} catch (UnsupportedEncodingException e) {
throw new InternalError();
String algoName = BytesArrayTools.readString(packet, start, algoNameSize);
start += algoNameSize;
int hashSize = BytesArrayTools.readInt(packet, start);
start += 4;
if (hashSize != 0) {
byte[] b = BytesArrayTools.readByteArray(packet, start, start+hashSize);
hashes.put(HashAlgorithm.fromName(algoName), b);
}
start += hashSize;
} while (start < size);
}
@ -114,49 +103,28 @@ public class HashResponse extends Payload {
// set request/response code
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
// set Payload size
setPayloadSize(size - FILENAME_SIZE_POSITION, packet);
// write filename size
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, filenameSize);
// Write filename
int bCount = FILENAME_POSITION;
try {
byte[] sb = filename.getBytes("UTF-8");
for(byte b : sb) {
packet[bCount] = b;
bCount += 1;
}
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
// write filename
BytesArrayTools.write(packet, filename, FILENAME_POSITION);
int bCount = FILENAME_POSITION + filename.length();
for(HashAlgorithm h : hashes.keySet()) {
String s = h.getName();
BytesArrayTools.write(packet, bCount, (int)s.length());
bCount += 4;
// Copy algoname
try {
byte[] sb = s.getBytes("UTF-8");
for(byte b : sb) {
packet[bCount] = b;
bCount += 1;
}
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
// write algoname
BytesArrayTools.write(packet, s, bCount);
bCount += s.length();
// write hash size
byte[] hashb = hashes.get(HashAlgorithm.fromName(s));
if (hashb.length == 0) {
BytesArrayTools.write(packet, bCount, (int)0);
bCount += 4;
} else {
BytesArrayTools.write(packet, bCount, (int)hashb.length);
bCount +=4;
// copy hash
for(byte b : hashb) {
packet[bCount] = b;
bCount += 1;
}
BytesArrayTools.write(packet, bCount, (int)hashb.length);
bCount += 4;
if (hashb.length != 0) {
// write hash
BytesArrayTools.write(packet, hashb, bCount);
bCount += hashb.length;
}
}
return packet;

@ -64,11 +64,7 @@ public class LoadRequest extends Payload {
/* Read filename */
int size = BytesArrayTools.readInt(packet, FILENAME_SIZE_POSITION);
try {
filename = new String(packet, FILENAME_POSITION, size, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
filename = BytesArrayTools.readString(packet, FILENAME_POSITION, size);
}
/** Returns a byte[] containing Packet with padding.
@ -82,33 +78,18 @@ public class LoadRequest extends Payload {
int filenameSize = filename.length();
int size = FILENAME_POSITION + filenameSize;
byte[] packet = new byte[size + 1]; // java initialize all to zero
// set request/response code
packet[RequestResponseCode.RRCODE_POSITION] = requestResponseCode.codeValue;
// set Payload size
setPayloadSize(size - OFFSET_POSITION, packet);
// Write offset
BytesArrayTools.write(packet, OFFSET_POSITION, (long)offset);
// Write maxSizePartialContent
BytesArrayTools.write(packet, MAX_SIZE_PARTIAL_CONTENT_POSITION, (long)maxSizePartialContent);
// Write filenameSize
BytesArrayTools.write(packet, FILENAME_SIZE_POSITION, (int)filenameSize);
// Write filename
int bCount = FILENAME_POSITION;
try {
byte[] sb = filename.getBytes("UTF-8");
for(byte b : sb) {
packet[bCount] = b;
bCount += 1;
}
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
BytesArrayTools.write(packet, filename, FILENAME_POSITION);
return packet;
}

@ -1,6 +1,8 @@
package tools;
import exception.SizeError;
import exception.ProtocolError;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/** Helper to manipulate byte[].
@ -127,7 +129,7 @@ public class BytesArrayTools {
* @param separator separator to use
* @throws InternalError
*/
public static void writeStringArray(byte[] byteArray, String[] strArray, int start, String separator) throws InternalError {
public static void write(byte[] byteArray, String[] strArray, int start, String separator) throws InternalError {
int bCount = start;
try {
byte[] sepb = separator.getBytes("UTF-8");
@ -151,6 +153,27 @@ public class BytesArrayTools {
}
}
/** Read string array from byte array starting at start with size parameter.
* @param byteArray Array to read
* @param start Start position
* @param size Size to read
* @param separator Separator used
* @return String array output
* @throws InternalError
*/
public static String[] readStringArray(byte[] byteArray, int start, int size, String separator) throws InternalError {
try {
return (new String(byteArray, start, size, "UTF-8")).split(separator);
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
}
/** Compute size of string array once converted to byte array with separator
* @param strArray String Array
* @param separator Separator used
* @return size
*/
public static int computeStringArraySize(String[] strArray, String separator) {
int size = 0;
for (String s: strArray) {
@ -161,4 +184,66 @@ public class BytesArrayTools {
}
return size;
}
/** Write a string to a byte array.
* @param array Byte Array to write
* @param str String to read
* @param start Start position in byte array to write
* @throws InternalError
*/
public static void write(byte[] array, String str, int start) throws InternalError {
int bCount = start;
try {
byte[] sb = str.getBytes("UTF-8");
for(byte b : sb) {
array[bCount] = b;
bCount += 1;
}
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
}
/** Read string from byte array
* @param array Byte array to read
* @param start start position in byte array
* @param size size to read
* @return String read
* @throws InternalError
*/
public static String readString(byte[] array, int start, int size) throws InternalError {
try {
return new String(array, start, size, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new InternalError();
}
}
/** Write byte Array to byte Array.
* @param dst Destination byte Array
* @param src Source byte Array
* @param start Start in dest array
*/
public static void write(byte[] dst, byte[] src, int start) {
int i = start;
for (byte b: src) {
dst[i] = b;
i += 1;
}
}
/** Read byte Array to byte Array
* @param src Source byte Array
* @param start Start position in source
* @param end End position in source
* @return Resulting byte array
* @throws ProtocolError
*/
public static byte[] readByteArray(byte[] src, int start, int end) throws ProtocolError {
try {
return Arrays.copyOfRange(src, start, end);
} catch (ArrayIndexOutOfBoundsException e) {
throw new ProtocolError();
}
}
}

Loading…
Cancel
Save