// Copyright 1997 The Open Group Research Institute.  All rights reserved.

package krb5.lib;

import krb5.lib.asn1.*;

/**
 * PA data
 */
public class PAData {

	/**
	 * PA data type
	 */
	public int pADataType;

	/**
	 * PA data value
	 */
	public byte[] pADataValue;

	/**
	 * Class constructor
	 */
	private PAData() {
	}

	/**
	 * Class constructor
	 *
	 * @param new_pADataType is of type int
	 * @param new_pADataValue is of type byte[]
	 */
	public PAData(int new_pADataType, byte[] new_pADataValue) {
		pADataType = new_pADataType;
		pADataValue = new_pADataValue;
	}

	/**
	 * Clones a copy of the object
	 *
	 * @return Object is a result
	 */
	public Object clone() {
		PAData new_pAData = new PAData();
		new_pAData.pADataType = pADataType;
		if (pADataValue != null) {
			new_pAData.pADataValue = new byte[pADataValue.length];
			System.arraycopy(pADataValue, 0, new_pAData.pADataValue,
				0, pADataValue.length);
		}
		return new_pAData;
	}

	/**
	 * Class constructor
	 *
	 * @param ref is of type EncodeRef
	 * @exception Asn1Exception an exception
	 * @see krb5.lib.asn1.EncodeRef
	 * @see krb5.lib.Asn1Exception
	 */
	public PAData(EncodeRef ref) throws Asn1Exception {
		EncodeRef subRef = decode.Sequence(ref);
		if (decode.Tag(subRef) == 1) {
			pADataType = decode.Integer(subRef.startOfData());
			//XXX need to validate that the type is recognized
			subRef.next();
		}
		else
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
		if (decode.Tag(subRef) == 2) {
			pADataValue = decode.OctetString(subRef.startOfData());
			//XXX need to validate pADataValue is right for pADataType
			subRef.next();
		}
		else
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
		if (subRef.isMoreInSequence())
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
	}

	/**
	 * Encodes the object in asn1
	 *
	 * @return byte[] is a result
	 * @exception Asn1Exception an exception
	 * @see krb5.lib.Asn1Exception
	 */
	public byte[] asn1Encode() throws Asn1Exception {
		byte[][] pa_data = {
			encode.prependExplicitTag(1, encode.Integer(pADataType)),
			encode.prependExplicitTag(2, encode.OctetString(pADataValue))
		};
		return encode.Sequence(pa_data);
	}

}