// Copyright 1997 The Open Group Research Institute.  All rights reserved.

package krb5.lib;

import krb5.lib.asn1.*;

/**
 * Transited encoding
 */
public class TransitedEncoding {

	/**
	 * Type
	 */
	public int trType;

	/**
	 * Contents
	 */
	public byte[] contents;

	/**
	 * Class constructor
	 *
	 * @param ref is of type EncodeRef
	 * @exception Asn1Exception an exception
	 * @see krb5.lib.asn1.EncodeRef
	 * @see krb5.lib.Asn1Exception
	 */
	public TransitedEncoding(EncodeRef ref) throws Asn1Exception {
		EncodeRef subRef = decode.Sequence(ref);
		if (decode.Tag(subRef) == 0) {
			trType = decode.Integer(subRef.startOfData());
			//XXX need to validate that the type is recognized
			//Only supported value is 1 == DOMAIN-COMPRESS
			subRef.next();
		}
		else
			throw new Asn1Exception(Krb5.ASN1_BAD_ID);
		if (decode.Tag(subRef) == 1) {
			contents = decode.OctetString(subRef.startOfData());
			//XXX Need to edit for correct DOMAIN-COMPRESS format
			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[][] transited_encoding = {
			encode.prependExplicitTag(0, encode.Integer(trType)),
			encode.prependExplicitTag(1, encode.OctetString(contents))
		};
		return encode.Sequence(transited_encoding);
	}

}