TrueZIP 6.8.3

de.schlichtherle.crypto.io
Class CipherReadOnlyFile

java.lang.Object
  extended by de.schlichtherle.io.rof.AbstractReadOnlyFile
      extended by de.schlichtherle.io.rof.FilterReadOnlyFile
          extended by de.schlichtherle.crypto.io.CipherReadOnlyFile
All Implemented Interfaces:
ReadOnlyFile
Direct Known Subclasses:
RaesReadOnlyFile

public abstract class CipherReadOnlyFile
extends FilterReadOnlyFile

A read only file for transparent random read access to an encrypted file.

The client must call init(SeekableBlockCipher, long, long) before it can actually read anything!

Note that this class implements its own virtual file pointer. Thus, if you would like to access the underlying ReadOnlyFile again after you have finished working with an instance of this class, you should synchronize their file pointers using the pattern as described in the base class FilterReadOnlyFile.

Since:
TrueZIP 6.0
Author:
Christian Schlichtherle

Field Summary
private  byte[] block
          The block buffer to use for decryption of partial blocks.
private  long blockOff
          The current offset in the encrypted file where the data starts that has been decrypted to the block.
private  SeekableBlockCipher cipher
          The seekable block cipher which allows random access.
private  boolean closed
          Whether this read only file has been closed or not.
private  long fp
          The virtual file pointer in the encrypted data.
private  long length
          The length of the encrypted data.
private static int MAX_WINDOW_LEN
          The maximum buffer length of the window to the encrypted file.
private  long start
          Start offset of the encrypted data.
private  byte[] window
          The buffer window to the encrypted file.
private  long windowOff
          The current offset in the encrypted file where the buffer window starts.
 
Fields inherited from class de.schlichtherle.io.rof.FilterReadOnlyFile
rof
 
Constructor Summary
CipherReadOnlyFile(ReadOnlyFile rof)
          Creates a read only file for transparent random read access to an encrypted file.
 
Method Summary
 void close()
          Closes this read only file and releases any resources associated with it.
protected  byte[] computeMac(Mac mac)
          Returns the authentication code of the encrypted data in this cipher read only file using the given Message Authentication Code (MAC) object.
private  void ensureInit()
          Ensures that this file is open and has been initialized.
 long getFilePointer()
           
 void init(SeekableBlockCipher cipher, long start, long length)
          Initializes this cipher read only file - must be called before first read access!
 long length()
           
private static long min(long a, long b)
          Returns the smaller parameter.
private  void positionBlock()
          Ensures that the block with the decrypted data for partial reading is positioned so that it contains the current virtual file pointer in the encrypted file.
private  void positionWindow()
          Ensures that the window is positioned so that the block containing the current virtual file pointer in the encrypted file is entirely contained in it.
 int read()
           
 int read(byte[] buf, int off, int len)
          Reads up to len bytes of data from this read only file into the given array.
 void seek(long fp)
          Sets the file pointer offset, measured from the beginning of this file, at which the next read occurs.
 
Methods inherited from class de.schlichtherle.io.rof.AbstractReadOnlyFile
read, readFully, readFully, skipBytes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_WINDOW_LEN

private static final int MAX_WINDOW_LEN
The maximum buffer length of the window to the encrypted file. This value has been adjusted to provide optimum performance at minimal size on a Windows XP computer - results may vary. Note that the actual size of the window is a multiple of the cipher's block size and may be smaller than the maximum window size.

See Also:
Constant Field Values

start

private long start
Start offset of the encrypted data.


length

private long length
The length of the encrypted data.


fp

private long fp
The virtual file pointer in the encrypted data. This is relative to the start.


windowOff

private long windowOff
The current offset in the encrypted file where the buffer window starts. This is always a multiple of the block size.


window

private byte[] window
The buffer window to the encrypted file. Note that this buffer contains encrypted data only. The actual size of the window is a multiple of the cipher's block size and may be slightly smaller than MAX_WINDOW_LEN.


cipher

private SeekableBlockCipher cipher
The seekable block cipher which allows random access.


blockOff

private long blockOff
The current offset in the encrypted file where the data starts that has been decrypted to the block. This is always a multiple of the block size.


block

private byte[] block
The block buffer to use for decryption of partial blocks. Note that this buffer contains decrypted data only.


closed

private boolean closed
Whether this read only file has been closed or not.

Constructor Detail

CipherReadOnlyFile

public CipherReadOnlyFile(ReadOnlyFile rof)
Creates a read only file for transparent random read access to an encrypted file. The client must call init(SeekableBlockCipher, long, long) before it can actually read anything!

Parameters:
rof - A read-only file. This may be null, but must be properly init before the call to init().
Method Detail

min

private static final long min(long a,
                              long b)
Returns the smaller parameter.


init

public void init(SeekableBlockCipher cipher,
                 long start,
                 long length)
          throws IOException
Initializes this cipher read only file - must be called before first read access!

Parameters:
start - The start offset of the encrypted data in this file.
length - The length of the encrypted data in this file.
Throws:
IOException - If this read only file has already been closed. This exception is not recoverable.
IllegalStateException - If this object has already been initialized. This exception is not recoverable.
NullPointerException - If FilterReadOnlyFile.rof is null or cipher is null. This exception is recoverable.

computeMac

protected byte[] computeMac(Mac mac)
                     throws IOException
Returns the authentication code of the encrypted data in this cipher read only file using the given Message Authentication Code (MAC) object. It is safe to call this method multiple times to detect if the file has been tampered with meanwhile.

Parameters:
mac - A properly initialized MAC object.
Throws:
IOException - On any I/O related issue.

length

public long length()
            throws IOException
Specified by:
length in interface ReadOnlyFile
Overrides:
length in class FilterReadOnlyFile
Throws:
IOException

getFilePointer

public long getFilePointer()
                    throws IOException
Specified by:
getFilePointer in interface ReadOnlyFile
Overrides:
getFilePointer in class FilterReadOnlyFile
Throws:
IOException

seek

public void seek(long fp)
          throws IOException
Description copied from interface: ReadOnlyFile
Sets the file pointer offset, measured from the beginning of this file, at which the next read occurs. Whether the offset may be set beyond the end of the file is up to the implementor. For example, the SimpleReadOnlyFile subclasses RandomAccessFile and passes "r" as a parameter to the superclass constructor. On the Windows platform, this implementation allows to seek past the end of file, but on the Linux platform it doesn't.

Specified by:
seek in interface ReadOnlyFile
Overrides:
seek in class FilterReadOnlyFile
Parameters:
fp - The offset position, measured in bytes from the beginning of the file, at which to set the file pointer.
Throws:
IOException - If pos is less than 0 or if an I/O error occurs.

read

public int read()
         throws IOException
Specified by:
read in interface ReadOnlyFile
Overrides:
read in class FilterReadOnlyFile
Throws:
IOException

read

public int read(byte[] buf,
                int off,
                int len)
         throws IOException
Description copied from interface: ReadOnlyFile
Reads up to len bytes of data from this read only file into the given array. This method blocks until at least one byte of input is available.

Specified by:
read in interface ReadOnlyFile
Overrides:
read in class FilterReadOnlyFile
Parameters:
buf - The buffer to fill with data.
off - The start offset of the data.
len - The maximum number of bytes to read.
Returns:
The total number of bytes read, or -1 if there is no more data because the end of the file has been reached.
Throws:
IOException - On any I/O related issue.

ensureInit

private final void ensureInit()
                       throws IOException
Ensures that this file is open and has been initialized.

Throws:
IOException - If the preconditions do not hold.

close

public void close()
           throws IOException
Closes this read only file and releases any resources associated with it. This method invalidates the state of this object, causing any subsequent calls to a public method to fail with an IOException.

Specified by:
close in interface ReadOnlyFile
Overrides:
close in class FilterReadOnlyFile
Throws:
IOException - If an I/O error occurs.

positionBlock

private void positionBlock()
                    throws IOException
Ensures that the block with the decrypted data for partial reading is positioned so that it contains the current virtual file pointer in the encrypted file.

Throws:
IOException - On any I/O related issue. The block is not moved in this case.

positionWindow

private void positionWindow()
                     throws IOException
Ensures that the window is positioned so that the block containing the current virtual file pointer in the encrypted file is entirely contained in it.

Throws:
IOException - On any I/O related issue. The window is invalidated in this case.

TrueZIP 6.8.3

Copyright © 2005-2011 Schlichtherle IT Services. All Rights Reserved.