TrueZIP Driver ZIP.RAES (TZP) 7.1

de.schlichtherle.truezip.crypto
Class CipherReadOnlyFile

java.lang.Object
  extended by de.schlichtherle.truezip.rof.AbstractReadOnlyFile
      extended by de.schlichtherle.truezip.rof.DecoratingReadOnlyFile
          extended by de.schlichtherle.truezip.crypto.CipherReadOnlyFile
All Implemented Interfaces:
ReadOnlyFile, Closeable
Direct Known Subclasses:
RaesReadOnlyFile

@NotThreadSafe
@DefaultAnnotation(value=edu.umd.cs.findbugs.annotations.NonNull.class)
public abstract class CipherReadOnlyFile
extends DecoratingReadOnlyFile

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 DecoratingReadOnlyFile.

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.truezip.rof.DecoratingReadOnlyFile
delegate
 
Constructor Summary
CipherReadOnlyFile(ReadOnlyFile rof)
          Creates a read only file for transparent random read access to an encrypted file.
 
Method Summary
private  void assertOpen()
          Asserts that this cipher output stream is in open state, which requires that cipher is not null.
 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.
 long getFilePointer()
           
protected  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()
          Positions the block with the decrypted data for partial reading so that it contains the current virtual file pointer in the encrypted file.
private  void positionWindow()
          Positions the window 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)
           
 void seek(long fp)
           
 
Methods inherited from class de.schlichtherle.truezip.rof.DecoratingReadOnlyFile
toString
 
Methods inherited from class de.schlichtherle.truezip.rof.AbstractReadOnlyFile
read, readFully, readFully
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, 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

@Nullable
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(@CheckForNull
                          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 initialized before a call to init(de.schlichtherle.truezip.crypto.SeekableBlockCipher, long, long).
Method Detail

min

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


assertOpen

private void assertOpen()
                 throws IOException
Asserts that this cipher output stream is in open state, which requires that cipher is not null.

Throws:
IOException - If the preconditions do not hold.

init

protected final 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 DecoratingReadOnlyFile.delegate 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 DecoratingReadOnlyFile
Throws:
IOException

getFilePointer

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

seek

public void seek(long fp)
          throws IOException
Specified by:
seek in interface ReadOnlyFile
Overrides:
seek in class DecoratingReadOnlyFile
Throws:
IOException

read

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

read

public int read(byte[] buf,
                int off,
                int len)
         throws IOException
Specified by:
read in interface ReadOnlyFile
Overrides:
read in class DecoratingReadOnlyFile
Throws:
IOException

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
Specified by:
close in interface Closeable
Overrides:
close in class DecoratingReadOnlyFile
Throws:
IOException - If an I/O error occurs.

positionBlock

private void positionBlock()
                    throws IOException
Positions the block with the decrypted data for partial reading 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
Positions the window 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 Driver ZIP.RAES (TZP) 7.1

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