1 /* 2 * Created on 17-Jan-2006 3 * Created by Paul Gardner 4 * Copyright (C) Azureus Software, Inc, All Rights Reserved. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 * 18 */ 19 20 package com.aelitis.azureus.core.networkmanager.impl; 21 22 import java.io.IOException; 23 import java.nio.ByteBuffer; 24 25 26 public class 27 TransportHelperFilterStreamXOR 28 extends TransportHelperFilterStream 29 { 30 private byte[] mask; 31 private int read_position; 32 private int write_position; 33 34 protected TransportHelperFilterStreamXOR( TransportHelper _transport, byte[] _mask )35 TransportHelperFilterStreamXOR( 36 TransportHelper _transport, 37 byte[] _mask ) 38 { 39 super( _transport ); 40 41 mask = _mask; 42 } 43 44 protected void cryptoOut( ByteBuffer source_buffer, ByteBuffer target_buffer )45 cryptoOut( 46 ByteBuffer source_buffer, 47 ByteBuffer target_buffer ) 48 49 throws IOException 50 { 51 int rem = source_buffer.remaining(); 52 53 for (int i=0;i<rem;i++){ 54 55 byte b = source_buffer.get(); 56 57 b = (byte)( b ^ mask[ write_position++ ]); 58 59 target_buffer.put( b ); 60 61 if ( write_position == mask.length ){ 62 63 write_position = 0; 64 } 65 } 66 } 67 68 protected void cryptoIn( ByteBuffer source_buffer, ByteBuffer target_buffer )69 cryptoIn( 70 ByteBuffer source_buffer, 71 ByteBuffer target_buffer ) 72 73 throws IOException 74 { 75 int rem = source_buffer.remaining(); 76 77 for (int i=0;i<rem;i++){ 78 79 byte b = source_buffer.get(); 80 81 b = (byte)( b ^ mask[ read_position++ ]); 82 83 target_buffer.put( b ); 84 85 if ( read_position == mask.length ){ 86 87 read_position = 0; 88 } 89 } 90 } 91 92 public boolean isEncrypted()93 isEncrypted() 94 { 95 return( true ); 96 } 97 98 public String getName(boolean verbose)99 getName(boolean verbose) 100 { 101 String proto_str = getHelper().getName(verbose); 102 103 if ( proto_str.length() > 0 ){ 104 105 proto_str = " (" + proto_str + ")"; 106 } 107 108 return( "XOR-" + mask.length*8 + proto_str ); 109 } 110 } 111