mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Tests and bugfixes for XOR encoder and decoder.
This commit is contained in:
@@ -4,10 +4,18 @@ import static net.sf.briar.api.transport.TransportConstants.MAX_FRAME_LENGTH;
|
||||
|
||||
class Frame {
|
||||
|
||||
private final byte[] buf = new byte[MAX_FRAME_LENGTH];
|
||||
private final byte[] buf;
|
||||
|
||||
private int length = -1;
|
||||
|
||||
Frame() {
|
||||
this(MAX_FRAME_LENGTH);
|
||||
}
|
||||
|
||||
Frame(int length) {
|
||||
buf = new byte[length];
|
||||
}
|
||||
|
||||
public byte[] getBuffer() {
|
||||
return buf;
|
||||
}
|
||||
|
||||
@@ -36,27 +36,31 @@ class XorErasureDecoder implements ErasureDecoder {
|
||||
// We don't need no stinkin' parity segment
|
||||
for(int i = 0; i < n - 1; i++) {
|
||||
byte[] src = set[i].getBuffer();
|
||||
System.arraycopy(src, 0, dest, offset, length);
|
||||
int copyLength = Math.min(length, dest.length - offset);
|
||||
System.arraycopy(src, 0, dest, offset, copyLength);
|
||||
offset += length;
|
||||
}
|
||||
} else {
|
||||
// Reconstruct the missing segment
|
||||
byte[] parity = new byte[length];
|
||||
int missingOffset = -1;
|
||||
for(int i = 0; i < n; i++) {
|
||||
for(int i = 0; i < n - 1; i++) {
|
||||
if(set[i] == null) {
|
||||
missingOffset = offset;
|
||||
} else {
|
||||
byte[] src = set[i].getBuffer();
|
||||
System.arraycopy(src, 0, dest, offset, length);
|
||||
for(int j = 0; j < length; j++) parity[j] ^= src[j];
|
||||
int copyLength = Math.min(length, dest.length - offset);
|
||||
System.arraycopy(src, 0, dest, offset, copyLength);
|
||||
}
|
||||
offset += length;
|
||||
}
|
||||
byte[] src = set[n - 1].getBuffer();
|
||||
for(int i = 0; i < length; i++) parity[i] ^= src[i];
|
||||
assert missingOffset != -1;
|
||||
System.arraycopy(parity, 0, dest, missingOffset, length);
|
||||
int copyLength = Math.min(length, dest.length - missingOffset);
|
||||
System.arraycopy(parity, 0, dest, missingOffset, copyLength);
|
||||
}
|
||||
assert offset == length * (n - 1);
|
||||
// The frame length may not be an exact multiple of the segment length
|
||||
int payload = HeaderEncoder.getPayloadLength(dest);
|
||||
int padding = HeaderEncoder.getPaddingLength(dest);
|
||||
|
||||
@@ -21,8 +21,9 @@ class XorErasureEncoder implements ErasureEncoder {
|
||||
byte[] src = f.getBuffer(), parity = set[n - 1].getBuffer();
|
||||
int offset = 0;
|
||||
for(int i = 0; i < n - 1; i++) {
|
||||
System.arraycopy(src, offset, set[i].getBuffer(), 0, length);
|
||||
for(int j = 0; j < length; j++) parity[j] ^= src[offset + j];
|
||||
int copyLength = Math.min(length, src.length - offset);
|
||||
System.arraycopy(src, offset, set[i].getBuffer(), 0, copyLength);
|
||||
for(int j = 0; j < copyLength; j++) parity[j] ^= src[offset + j];
|
||||
offset += length;
|
||||
}
|
||||
return set;
|
||||
|
||||
Reference in New Issue
Block a user