Hi!

I'm implementing a simple XORFilter to XOR encrypt my messages. I
added the XORFilter on top of the ProtocolCodecFilter and it works ok,
but even if the messages go from and to the server ok (it's a
multiplayer game), I keep getting this error on background

org.apache.mina.common.BufferDataException: dataLength: 1694498816
        at 
org.apache.mina.common.ByteBuffer.prefixedDataAvailable(ByteBuffer.java:1631)
        at 
org.apache.mina.filter.codec.serialization.ObjectSerializationDecoder.doDecode(ObjectSerializationDecoder.java:88)
        at 
org.apache.mina.filter.codec.CumulativeProtocolDecoder.decode(CumulativeProtocolDecoder.java:133)
        at 
org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(ProtocolCodecFilter.java:158)

Some debugging showed that (quite possibly I'm wrong) this happens
when the XORFilter encrypts an empty ByteBuffer that MINA sends

Here is the source of the XORFilter class:

public class XORFilter extends IoFilterAdapter {
    @Override
    public void messageReceived(NextFilter nextFilter, IoSession session,
            Object message) throws Exception {

        nextFilter.messageReceived(session, xor((ByteBuffer) message));
    }

    @Override
    public void filterWrite(NextFilter nextFilter, IoSession session,
            WriteRequest writeRequest) throws Exception {

        ByteBuffer inBuffer = (ByteBuffer) writeRequest.getMessage();

        nextFilter.filterWrite(session, new WriteRequest(xor(inBuffer),
                writeRequest.getFuture()));
    }

    private ByteBuffer xor(ByteBuffer in) {
        ByteBuffer xor = ByteBuffer.allocate(in.capacity());

        // xor
        for (int i = 0; i < in.limit(); ++i) {
            byte b = in.get();
            byte xbx = (byte) (b ^ 101);

            xor = xor.put(xbx);
        }

        return xor.rewind();
    }
}


Any ideas of what might be the cause? Also I'm new to "low level"
byte-buffer handling, so any tips on improving the code will be most
appreciated

Thanks!

Andres

Reply via email to