/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccsid_ASNgetP_header[] = "@(#)ASNgetP_hdr.c	1.22 12/3/92 10:00:54 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	6/28/90

	ROUTINE:	 getP_header
	Version:	__ROr_ASNgetP_header

PURPOSE:
		This routine obtains the TYPE and LENGTH of the ASN.1 TLV-
	encoding, and returns them to the caller.  It also manages indefinite
	length encoding emulation if definite length SET or SEQUENCE data values
	are encountered.  First, this routine checks to see if an EOC should be
	returned based of the fact that a definite length encoding has expired.
	Next, the Type and Length are obtained.  If a definite encoding for
	SET or SEQUENCE is found, then that item is added to the stack, and
	returned as indefinite.


IN:
	instance - passed to get_data(). Definite encodings checked.

OUT:
	instance - Receive state is updated to reflect the data consumed
		from the input stream, and any indefinite encoding emulations.
		Exception state is updated.
	encodedtype - contains the TYPE of the TLV-encoding.  Predefined
		types are defined in <elros/typetags.h>  This should be a
		4-byte integer value.
	encodedlength - The length in bytes of the value encoding for the
		 forthcoming ASN.1 value.


CALLED ROUTINES:
	dcdP_tag
	dcdP_length

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>
#include <elros/typetags.h>
#include <elros/enc_dcd.h>
#include <elros/getP_element.h>

static int __ROr_extend_definite_stack();

int __ROr_ASNgetP_header(instance,encodedtype,encodedlength)

   __RO_Instance_Record *	instance;
   __RO_int32 *			encodedtype;
   unsigned long *		encodedlength;

#define STACK		(instance->get_state.definite)
#define BYTECOUNTER	(instance->get_state.bytecounter)
{
   int class,form;
   __RO_int32 tag;

	/* If we've finished with a definite length encoding then
	   signal EOC and pop the encoding number */

   if (STACK.top && BYTECOUNTER == STACK.stack[STACK.top])
   {
      *encodedtype = TAG_EOC;
      *encodedlength = 0;
      STACK.top--;
      return(SUCCESS);
   }

	/* Decode the length and tag with optimization. */
   {
      unsigned char data;


	/* Decode the type tag and put it in '*encodedtype' */

      __ROr_lookP_1element(instance,&data);
      if ((data & ASN_ID_XTND) == ASN_ID_XTND)
      {
         if(__ROr_dcdP_tag(instance,&class,&form,&tag))
            return(EXCEPTION);
         *encodedtype = (class << 30) | (tag);
      }
      else
      {
         __ROr_eatP_1element(instance);
         *encodedtype = ((data & ASN_CLASS_MASK) << (30-ASN_CLASS_SHIFT))
			| (data & ASN_CODE_MASK);
         form = (data & ASN_FORM_MASK) >> ASN_FORM_SHIFT;
      }

	/* Decode the length and put it in '*encodedlength' */

      __ROr_lookP_1element(instance,&data);
      if (((*encodedlength = data) & ASN_LEN_XTND) == ASN_LEN_XTND)
      {
         if (data == ASN_LEN_XTND)
         {
            *encodedlength = ASN_LEN_INDF;
            __ROr_eatP_1element(instance);
         }
         else
            if(__ROr_dcdP_length(instance,encodedlength))
               return(EXCEPTION);
      }
      else
      {
         __ROr_eatP_1element(instance);
      }

      if ((*encodedtype == TAG_EOC) && *encodedlength != 0)
         __RO_RETURN_RUNTIME(instance,__ROx_ASN_EOCNOT0);

      if (form /* == 1 (CONSTRUCTED) */ && *encodedlength != ASN_LEN_INDF)
         if (STACK.top < STACK.maximum)
         {
            STACK.stack[++STACK.top] = BYTECOUNTER + *encodedlength;
            *encodedlength = ASN_LEN_INDF;
         }
         else
         {
            STACK.stack[0] = BYTECOUNTER + *encodedlength;
            *encodedlength = ASN_LEN_INDF;
/*         __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_DEFSTKOVERFLOW);*/
            if (__ROr_extend_definite_stack(instance))
               return(EXCEPTION);
         }
   }

   return(SUCCESS);

}

#define STACK_EXTEND_SIZE	16

static int __ROr_extend_definite_stack(instance)

   __RO_Instance_Record *instance;

{
   struct __RO_Definite_stack_struct NEWSTACK;
   int i;

   if (STACK.top != STACK.maximum)
      __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_BADSTATEEXTEND);
   NEWSTACK.stack = (unsigned long *)
                    malloc(sizeof(unsigned long) *
                           (STACK_EXTEND_SIZE + STACK.maximum));
   if (!NEWSTACK.stack)
      __RO_RETURN_RUNTIME(instance,__ROx_GENERAL_MALLOCFAILED);
   NEWSTACK.maximum = STACK.maximum + STACK_EXTEND_SIZE;
   NEWSTACK.top = STACK.top + 1;
   for (i=0; i<=STACK.maximum; i++)
      NEWSTACK.stack[i] = STACK.stack[i];
   NEWSTACK.stack[STACK.top+1] = STACK.stack[0];

   free(STACK.stack);
   STACK = NEWSTACK;

   return(SUCCESS);
}

