package org.fidonet.binkp.codec;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.write.WriteRequest;

/**
 * Created by IntelliJ IDEA.
 * Author: Vladimir Kravets
 * E-Mail: vova.kravets@gmail.com
 * Date: 4/14/14
 * Time: 9:56 AM
 */

/**
 * This class control decrypt process for received data,
 * crypt process during sending data controls by last encoder
 */
public class TrafficCrypterCodecFilter extends IoFilterAdapter {

    @Override
    public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
        IoBuffer in = (IoBuffer) message;
        TrafficCrypter trafficCrypter = (TrafficCrypter) session.getAttribute(TrafficCrypter.TRAFFIC_CRYPTER_KEY);
        byte[] array = new byte[in.array().length];
        IoBuffer buf = IoBuffer.allocate(array.length);
        in.get(array);
        trafficCrypter.decrypt(array, array.length);
        buf.put(array);
        buf.flip();
        super.messageReceived(nextFilter, session, buf);
    }

    @Override
    public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
        IoBuffer in = (IoBuffer) writeRequest.getMessage();
        if (in.array().length > 0) {
            TrafficCrypter trafficCrypter = (TrafficCrypter) session.getAttribute(TrafficCrypter.TRAFFIC_CRYPTER_KEY);
            byte[] array = new byte[in.array().length];
            IoBuffer buf = IoBuffer.allocate(array.length);
            in.get(array);
            trafficCrypter.encrypt(array, array.length);
            buf.put(array);
            buf.flip();
        }
        super.filterWrite(nextFilter, session, writeRequest);
    }
}

@@ -1,8 +1,2170 @@
+/******************************************************************************%0A * Copyright (c) 2012-2014, Vladimir Kravets                                  *%0A *  All rights reserved.                                                      *%0A *                                                                            *%0A *  Redistribution and use in source and binary forms, with or without        *%0A *  modification, are permitted provided that the following conditions are    *%0A *  met: Redistributions of source code must retain the above copyright notice,%0A *  this list of conditions and the following disclaimer.                     *%0A *  Redistributions in binary form must reproduce the above copyright notice, *%0A *  this list of conditions and the following disclaimer in the documentation *%0A *  and/or other materials provided with the distribution.                    *%0A *  Neither the name of the Fido4Java nor the names of its contributors       *%0A *  may be used to endorse or promote products derived from this software     *%0A *  without specific prior written permission.                                *%0A *                                                                            *%0A *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %22AS IS%22%0A *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,     *%0A *  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR    *%0A *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR         *%0A *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     *%0A *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *%0A *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;%0A *  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  *%0A *  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR   *%0A *  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,            *%0A *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                        *%0A ******************************************************************************/%0A%0A
 package 
@@ -3692,68 +3692,8 @@
 h%5D;%0A
-            IoBuffer buf = IoBuffer.allocate(array.length);%0A
     
@@ -3776,35 +3776,58 @@
 h);%0A            
-buf
+in.clear();%0A            in
 .put(array);%0A   
@@ -3827,35 +3827,34 @@
 y);%0A            
-buf
+in
 .flip();%0A       

Fix passing encryption data to next encoder