1package org.bouncycastle.crypto;
2
3/**
4 * A parent class for block cipher modes that do not require block aligned data to be processed, but can function in
5 * a streaming mode.
6 */
7public abstract class StreamBlockCipher
8    implements BlockCipher, StreamCipher
9{
10    private final BlockCipher cipher;
11
12    protected StreamBlockCipher(BlockCipher cipher)
13    {
14        this.cipher = cipher;
15    }
16
17    /**
18     * return the underlying block cipher that we are wrapping.
19     *
20     * @return the underlying block cipher that we are wrapping.
21     */
22    public BlockCipher getUnderlyingCipher()
23    {
24        return cipher;
25    }
26
27    public final byte returnByte(byte in)
28    {
29        return calculateByte(in);
30    }
31
32    public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff)
33        throws DataLengthException
34    {
35        if (outOff + len > out.length)
36        {
37            throw new DataLengthException("output buffer too short");
38        }
39
40        if (inOff + len > in.length)
41        {
42            throw new DataLengthException("input buffer too small");
43        }
44
45        int inStart = inOff;
46        int inEnd = inOff + len;
47        int outStart = outOff;
48
49        while (inStart < inEnd)
50        {
51             out[outStart++] = calculateByte(in[inStart++]);
52        }
53
54        return len;
55    }
56
57    protected abstract byte calculateByte(byte b);
58}