com.kirkwarez.kirkpack.io.uru.wdys
Class WDYSInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byjava.io.FilterInputStream
          extended bycom.kirkwarez.kirkpack.io.uru.wdys.WDYSInputStream

public class WDYSInputStream
extends FilterInputStream

This stream streams in Uru WDYS encrypted data and streams out the decrypted data. Since the WDYS header has the length tag set, one can retrieve the eventual length of the data at any time.

Since:
3.00
Version:
3.00
Author:
Berend "Kirk" Wouda

Field Summary
protected  byte[] block
          The xTEA dual block buffer.
private static int[] KEY
          The key.
protected  int length
          The length read from the header.
protected  int total
          The total read from the stream.
protected  int unread
          How much there's left to be read from the buffers.
static String WDYS_HEADER
          The WDYS header.
 
Fields inherited from class java.io.FilterInputStream
in
 
Fields inherited from class java.io.InputStream
 
Constructor Summary
WDYSInputStream(InputStream input)
          Creates a stream over the given stream.
 
Method Summary
 int available()
          Returns the number of bytes that can be read without blockage.
protected  int byteArrayToInt(byte[] bytes)
          Makes a byte array of an int.
protected  byte[][] decrypt(byte[][] v)
          The xTEA decryption algorithm.
 int getLength()
          Returns the length of the unencrypted data as indicated by the header of the file.
protected  byte[] intToByteArray(int value)
          Makes an int out of a byte array.
 int read()
          Reads an unsigned byte and returns it as an int.
 int read(byte[] buffer, int offset, int length)
          Fills the array with bytes from the stream.
protected  int readFromBuffer(byte[] buffer, int offset, int length)
          Reads length bytes from the buffers and puts them in the given array from offset on.
protected  void setLength(int length)
          Sets the length of the unencrypted data from the indication by the header of the file.
 long skip(long amount)
          Skips the given amount of bytes, or at least tries to.
 
Methods inherited from class java.io.FilterInputStream
close, mark, markSupported, read, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

block

protected byte[] block
The xTEA dual block buffer.


unread

protected int unread
How much there's left to be read from the buffers. A value between 0 and 8.


length

protected int length
The length read from the header.


total

protected int total
The total read from the stream.


KEY

private static int[] KEY
The key. Ssh!


WDYS_HEADER

public static final String WDYS_HEADER
The WDYS header.

See Also:
Constant Field Values
Constructor Detail

WDYSInputStream

public WDYSInputStream(InputStream input)
                throws IOException
Creates a stream over the given stream.

Parameters:
input - The stream to read from.
Throws:
IOException - When the given stream does not produce Uru data.
Method Detail

read

public int read()
         throws IOException
Reads an unsigned byte and returns it as an int. When the length indicated by the file is reached, no more bytes are read in.

Returns:
The byte as an int.
Throws:
IOException - When boo boo goes doo doo.
See Also:
InputStream.read()

read

public int read(byte[] buffer,
                int offset,
                int length)
         throws IOException
Fills the array with bytes from the stream. Not necessarily completely though, but with at least one. When the length indicated by the file is reached, no more bytes are read in.

Parameters:
buffer - The buffer to read the data into.
offset - The starting offset.
length - The amount of bytes to read.
Returns:
The amount of bytes actually read.
Throws:
IOException - When boo boo goes doo doo.
See Also:
InputStream.read(byte[], int, int)

skip

public long skip(long amount)
          throws IOException
Skips the given amount of bytes, or at least tries to.

Returns:
The actual amount of bytes skipped.
Throws:
IOException
See Also:
InputStream.read(byte[], int, int), InputStream.skip(long)

available

public int available()
              throws IOException
Returns the number of bytes that can be read without blockage. This version keeps in mind that there is a preset length in the header, and that there is at most 8 bytes that can be read without blocking (decrypting block size).

Returns:
The number of bytes that can be read without blockage.
Throws:
IOException
See Also:
InputStream.available()

readFromBuffer

protected int readFromBuffer(byte[] buffer,
                             int offset,
                             int length)
                      throws IOException
Reads length bytes from the buffers and puts them in the given array from offset on.

Parameters:
buffer - The buffer to read the data into.
offset - The starting offset.
length - The amount of bytes to read.
Returns:
The amount of bytes actually read. This method will only read until the buffers have been depleted.
Throws:
IOException - When your HDD explodes.

decrypt

protected byte[][] decrypt(byte[][] v)
The xTEA decryption algorithm.

Parameters:
v - The encrypted input blocks.
Returns:
The decrypted input blocks. These are not the same byte arrays, however the container byte array is.

intToByteArray

protected byte[] intToByteArray(int value)
Makes an int out of a byte array. It'll be using Little Endian, for the sake of the decryption algorithm implemented by this class.

Parameters:
value - The int to turn into a byte array.
Returns:
The byte array that used to be the int.

byteArrayToInt

protected int byteArrayToInt(byte[] bytes)
Makes a byte array of an int. It'll be using Little Endian, for the sake of the decryption algorithm implemented by this class.

Parameters:
bytes - The byte array to turn into an int.
Returns:
The int that used to be a byte array.

getLength

public int getLength()
Returns the length of the unencrypted data as indicated by the header of the file.

Returns:
The length of the unencrypted data.

setLength

protected void setLength(int length)
Sets the length of the unencrypted data from the indication by the header of the file.

Parameters:
length - The length of the unencrypted data.