ptfile._java


// Read files, decrypt and maintain cache

Hashtable 	file_Cache = null;
boolean 	file_cachefiles = true;



void file_init(){
	file_cachefiles = true;
	file_Cache = new Hashtable();
}

void file_dispose(){
	if( file_Cache != null ){
		file_Cache.clear();
		file_Cache = null;
	} 
}		

// Read file into memory
byte[] file_read( String name, int[] progress ){
	byte[] readBuffer = (byte[]) file_Cache.get( name );
	
	if( readBuffer != null ){
		if( progress != null ){
			progress[0] = 80;
			repaint();
		}
		return readBuffer;
	}
		
	int psize = 0;
	InputStream is = null;
	
	try{ 
		URL url = new URL(getDocumentBase(), name);	 
 		URLConnection uc = url.openConnection();
    	uc.setUseCaches( true );
    	try{
    		psize 	= uc.getContentLength();
    	}catch(Exception e1){
    		psize = 0;
    	}
		is = uc.getInputStream(); 
		readBuffer = file_read( is, psize, progress );
		is.close();
		if( readBuffer != null ){
			ptd( readBuffer, name );
			if( file_cachefiles ){
				synchronized( file_Cache ){
  							file_Cache.put( name, readBuffer );
  				}
  			}
			return readBuffer;
		}
 	}catch(Exception e){}
		
	try{
 		Class c = Class.forName("ptviewer");
		is = null;
		is = c.getResourceAsStream( name );
		if( is != null ){
			readBuffer = file_read( is, 0, null );
			is.close();
		}
		if( readBuffer != null ){
			ptd( readBuffer, name );
			if( file_cachefiles ){
				synchronized( file_Cache ){
  							file_Cache.put( name, readBuffer );
  				}
  			}
			return readBuffer;
		}
	}catch(Exception e2){}
	return null;
}



byte[] file_read( InputStream is, int fsize, int[] progress ){
	int np = 0, nb = 0, n = 0;
	int bufsize = (fsize > 0 ? fsize/10 + 1 : 50000);
	byte[] buf = new byte[fsize > 0 ? fsize : 50000];
		
	try{
		while( n != -1 ){
			nb = 0;
			if( buf.length < np + bufsize ){
				byte[] temp = new byte[np + bufsize];
				System.arraycopy(buf,0,temp,0,np);
				buf = temp;
			}
					
			while( nb < bufsize &&  (n = is.read( buf, np, bufsize-nb )) != -1 ){
				nb += n; np += n;
				if( fsize > 0 && progress != null){
					progress[0] = (800 * np / fsize + 5) / 10;  // rounding; show 80% when file loaded
					if( progress[0] > 100 ) progress[0] = 100;
					repaint();
				}
			}
		}
		if( buf.length > np ){ 
			byte[] temp = new byte[np];
			System.arraycopy(buf,0,temp,0,np);
			buf = temp;
		}
	}catch(Exception e){ return null; }
		
	return buf;
}
	

	private void ptd( byte[] buf, byte[] key ){
		int i,k;
		
		// XOR with key
		
		for(i=0,k=0; i < buf.length; i++,k++){
			if( k >= key.length ) k=0;
			buf[i] ^= key[k];
		}
		
		// Simple byte shifter and scrambler
		
		int[] sp = {1,20,3,18,0,17,14,11,22,19,2,5,7,6,13,4,21,8,10,9,12,15,16};
		
		int length = buf.length - sp.length;
		byte b[] = new byte[ sp.length ];

		for(i=0; i <length; i += sp.length){
			System.arraycopy(buf,i,b,0,sp.length);
			
			for(k=0; k<sp.length; k++){
				buf[k+i] = b[sp[k]];
			}		
		}
	}
			
	private void ptd( byte[] buf, String fname ){
		if( buf == null || fname == null ) return;
		
		int i = fname.lastIndexOf('.');
		if( i<0 || i+1 >= fname.length() ) return;

		byte[] dk = {	(byte)122, (byte)1, (byte)12, (byte)-78, (byte)-99, (byte)-33, 
						(byte)-50, (byte)17, (byte)88, (byte)90, (byte)-117, (byte)119, 
						(byte)30, (byte)20, (byte)10, (byte)33, (byte)27, (byte)114, 
						(byte)121, (byte)3, (byte)-11, (byte)51, (byte)97, (byte)-59, 
						(byte)-32, (byte)-28, (byte)0, (byte)83, (byte)37, (byte)43, 
						(byte)-67, (byte)17, (byte)32, (byte)31, (byte)70, (byte)-70, 
						(byte)-10, (byte)-39, (byte)-33, (byte)2, (byte)55, (byte)59, 
						(byte)-88 };
				
		if( fname.substring( i+1 ).equalsIgnoreCase( "jpa" ) ){
			ptd( buf, dk );
		}else if( fname.substring( i+1 ).equalsIgnoreCase( "jpb" ) ){
			byte[] pb = ptgetPath().getBytes();
			byte[] ku = new byte[ dk.length + pb.length ];
			System.arraycopy(dk,0,ku,0,dk.length);
			System.arraycopy(pb,0,ku,dk.length,pb.length);
			ptd( buf, ku );
		}else if( fname.substring( i+1 ).equalsIgnoreCase( "jpc" ) ){
			byte[] pb = getDocumentBase().toString().getBytes();
			byte[] ku = new byte[ dk.length + pb.length ];
			System.arraycopy(dk,0,ku,0,dk.length);
			System.arraycopy(pb,0,ku,dk.length,pb.length);
			ptd( buf, ku );
		}
	}
			
	

Back