// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.crypto.fpe;

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.util.Pack;
import org.bouncycastle.crypto.OutputLengthException;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.params.FPEParameters;
import org.bouncycastle.crypto.BlockCipher;

public abstract class FPEEngine
{
    protected final BlockCipher baseCipher;
    protected boolean forEncryption;
    protected FPEParameters fpeParameters;
    
    protected FPEEngine(final BlockCipher baseCipher) {
        this.baseCipher = baseCipher;
    }
    
    public int processBlock(final byte[] array, final int n, final int n2, final byte[] array2, final int n3) {
        if (this.fpeParameters == null) {
            throw new IllegalStateException("FPE engine not initialized");
        }
        if (n2 < 0) {
            throw new IllegalArgumentException("input length cannot be negative");
        }
        if (array == null || array2 == null) {
            throw new NullPointerException("buffer value is null");
        }
        if (array.length < n + n2) {
            throw new DataLengthException("input buffer too short");
        }
        if (array2.length < n3 + n2) {
            throw new OutputLengthException("output buffer too short");
        }
        if (this.forEncryption) {
            return this.encryptBlock(array, n, n2, array2, n3);
        }
        return this.decryptBlock(array, n, n2, array2, n3);
    }
    
    protected static short[] toShortArray(final byte[] array) {
        if ((array.length & 0x1) != 0x0) {
            throw new IllegalArgumentException("data must be an even number of bytes for a wide radix");
        }
        final short[] array2 = new short[array.length / 2];
        for (int i = 0; i != array2.length; ++i) {
            array2[i] = Pack.bigEndianToShort(array, i * 2);
        }
        return array2;
    }
    
    protected static byte[] toByteArray(final short[] array) {
        final byte[] array2 = new byte[array.length * 2];
        for (int i = 0; i != array.length; ++i) {
            Pack.shortToBigEndian(array[i], array2, i * 2);
        }
        return array2;
    }
    
    public abstract void init(final boolean p0, final CipherParameters p1);
    
    public abstract String getAlgorithmName();
    
    protected abstract int encryptBlock(final byte[] p0, final int p1, final int p2, final byte[] p3, final int p4);
    
    protected abstract int decryptBlock(final byte[] p0, final int p1, final int p2, final byte[] p3, final int p4);
}
