Re: [Jnetstream-users] Error in TCP decode
Brought to you by:
voytechs
|
From: Frédéric D. <fre...@dr...> - 2008-08-06 14:31:30
|
Good morning Mark,
I will be unreachable until next monday so I already send you what I found.
- my implementation of readBitsOneAtATime(). Perhaps it is not so good
but it just does the trick until the original one will be fixed.
- something in FastScanner didn't work after I changed
readBitsOneAtATime(). 32 kept to be added to the ip header length.
Regards,
Frédéric
Fast Scanner ---------------------------------
/**
*
*/
public final boolean scanIp4() {
addToCache(IP4);
r1 = readUByte(Ip4.StaticField.PROTOCOL);
if (DEBUG) {
logger.debug(String.format("Ip4 p=%d, r1=%d\n", offset, r1));
}
//offset += bits.getBits(offset + 4, 4) * 4 * 8 + 32; <<== why +32 ?
offset += bits.getBits(offset + 4, 4) * 32;
switch (r1) {
case 1:
return scanIcmp();
case 6:
return scanTcp();
case 17:
return scanUdp();
}
return true;
}
BitBuffer -------------------------------------
private static final int[] RIGHT_BIT_MASK = new int[33];
static {
// initialize mask used by readBitsOneAtATime()
for (int i = 1; i < RIGHT_BIT_MASK.length; i++) {
RIGHT_BIT_MASK[i] = (RIGHT_BIT_MASK[i - 1] << 1) | 1;
}
}
private int readBitsOneAtATime(int length) {
// basic checks to avoid unnecessary computation
if (length == 0) {
return 0;
}
if (length > 32) {
throw new IndexOutOfBoundsException("0 >= range <= 32bits");
}
// value is stored in long since length+posBits
// could be greater than 32.
long value = current;
int acc = 8 - posBits;
// concatenate buffer byte per byte
for (; acc < length; acc += 8) {
// fetch next byte
posBits = 8;
fetch();
// append new current byte
value = (value << 8) | (current & 0xFF);
}
// right-align the bits and mask high bits
value = (value >> (acc - length)) & RIGHT_BIT_MASK[length];
// update posBits value
posBits = (posBits + length) % 8;
// return
return (int) value;
}
|