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

package org.bouncycastle.crypto.util;

import java.math.BigInteger;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Strings;

class SSHBuffer
{
    private final byte[] buffer;
    private int pos;
    
    public SSHBuffer(final byte[] array, final byte[] buffer) {
        this.pos = 0;
        this.buffer = buffer;
        for (int i = 0; i != array.length; ++i) {
            if (array[i] != buffer[i]) {
                throw new IllegalArgumentException("magic-number incorrect");
            }
        }
        this.pos += array.length;
    }
    
    public SSHBuffer(final byte[] buffer) {
        this.pos = 0;
        this.buffer = buffer;
    }
    
    public int readU32() {
        if (this.pos > this.buffer.length - 4) {
            throw new IllegalArgumentException("4 bytes for U32 exceeds buffer.");
        }
        return (this.buffer[this.pos++] & 0xFF) << 24 | (this.buffer[this.pos++] & 0xFF) << 16 | (this.buffer[this.pos++] & 0xFF) << 8 | (this.buffer[this.pos++] & 0xFF);
    }
    
    public String readString() {
        return Strings.fromByteArray(this.readBlock());
    }
    
    public byte[] readBlock() {
        final int u32 = this.readU32();
        if (u32 == 0) {
            return new byte[0];
        }
        if (this.pos > this.buffer.length - u32) {
            throw new IllegalArgumentException("not enough data for block");
        }
        final int pos = this.pos;
        this.pos += u32;
        return Arrays.copyOfRange(this.buffer, pos, this.pos);
    }
    
    public void skipBlock() {
        final int u32 = this.readU32();
        if (this.pos > this.buffer.length - u32) {
            throw new IllegalArgumentException("not enough data for block");
        }
        this.pos += u32;
    }
    
    public byte[] readPaddedBlock() {
        return this.readPaddedBlock(8);
    }
    
    public byte[] readPaddedBlock(final int n) {
        final int u32 = this.readU32();
        if (u32 == 0) {
            return new byte[0];
        }
        if (this.pos > this.buffer.length - u32) {
            throw new IllegalArgumentException("not enough data for block");
        }
        if (0 != u32 % n) {
            throw new IllegalArgumentException("missing padding");
        }
        final int pos = this.pos;
        this.pos += u32;
        int pos2 = this.pos;
        if (u32 > 0) {
            final int n2 = this.buffer[this.pos - 1] & 0xFF;
            if (0 < n2 && n2 < n) {
                final int n3 = n2;
                pos2 -= n3;
                for (int i = 1, n4 = pos2; i <= n3; ++i, ++n4) {
                    if (i != (this.buffer[n4] & 0xFF)) {
                        throw new IllegalArgumentException("incorrect padding");
                    }
                }
            }
        }
        return Arrays.copyOfRange(this.buffer, pos, pos2);
    }
    
    public BigInteger readBigNumPositive() {
        final int u32 = this.readU32();
        if (this.pos + u32 > this.buffer.length) {
            throw new IllegalArgumentException("not enough data for big num");
        }
        final int pos = this.pos;
        this.pos += u32;
        return new BigInteger(1, Arrays.copyOfRange(this.buffer, pos, this.pos));
    }
    
    public byte[] getBuffer() {
        return Arrays.clone(this.buffer);
    }
    
    public boolean hasRemaining() {
        return this.pos < this.buffer.length;
    }
}
