/*****************************************************************/
/**                                                             **/
/**             (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_getP_oid[] = "@(#)getP_oid.c	1.21 10/20/92 05:51:43 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Robyne M. Sumpter
			Network Systems Division

	DATE WRITTEN:	7/18/90

	ROUTINE:	__ROr_BERgetP_ObjectIdentifier

PURPOSE:
	Receive and decode an object identifer encoded in ASN.1.  The object
	id is represented as a struct containing a size(number of elements) 
	and an array of longs, representing the oid's components.


IN:
	valuelength - number of bytes to receive the value.
	encodedlength - number of bytes in the encoded representation to be
			received.	
	maxlength - maximum number of oid elements to receive.
	maxlength2 - maximum number of bytes in each oid element.

OUT:
	value - decoded representation of the oid.
	instance -
	  Receive state and error state are updated.


CALLED ROUTINES:
	getP_Nelement
	

	
MODIFICATION HISTORY:


*/


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

#define TRUE	1

int __ROr_BERgetP_ObjectIdentifier( instance, value, valuelength, encodedlength,
		 			 maxlength, maxlength2 )
    __RO_Instance_Record	*instance;
    struct __RO_oid_skeleton	*value;
    unsigned long 		*valuelength;
    unsigned long		*encodedlength;
    unsigned long		*maxlength;
    unsigned long		*maxlength2;

{
    unsigned char buffer[128];
    unsigned char *dataptr, *endptr, *bufferptr;
    unsigned long *component, *firstcomp;
    int  freebuffer = 0;
    int  i;

    /* Consume data and throw it away */
    if( !value )	
      {
	  if( __ROr_STDconsume_data( instance, encodedlength ) )
	      return( EXCEPTION );
	  return( SUCCESS );
      }


    /* Check if there is enough room in the variable to receive the data */
    if( *maxlength && 
        *valuelength < ( sizeof(int) + (*maxlength * sizeof(long))) )
  	__RO_RETURN_RUNTIME( instance, __ROx_DCD_EXCEEDEDVALLEN );


    if( *valuelength > sizeof( buffer ) )
    {
	if( (bufferptr = (unsigned char *)malloc( *valuelength ) ) == NULL )
	    __RO_RETURN_RUNTIME( instance, __ROx_DCD_OUTOFMEM );
	freebuffer = TRUE;
    }
    else
        bufferptr = (unsigned char *)buffer;

    __ROr_getP_Nelement( instance, *encodedlength, bufferptr );

    dataptr = bufferptr;
    endptr = dataptr + *encodedlength;
 
    for (i = 1; dataptr < endptr; i++) { /* another whacko ISO encoding... */
        if (*dataptr == 0x80)
	    __RO_RETURN_RUNTIME( instance, __ROx_DCD_ERROID );
 
        while (*dataptr++ & 0x80)
            if (dataptr > endptr)
	    	__RO_RETURN_RUNTIME( instance, __ROx_DCD_ERROID );
    }
 
    /* if the number of components exceed the maximum, raise an
       exception */
    if (*maxlength) {
       if ( i > *maxlength )
	  __RO_RETURN_RUNTIME( instance, __ROx_DCD_EXCEEDEDMAXLEN );
       component = value->comp;
    }
    else {
       component = (unsigned long *)malloc(i * sizeof(long));
       if (component == NULL)
          __RO_RETURN_RUNTIME(instance,__ROx_GENERAL_MALLOCFAILED);
       ((struct __RO_oid_indef *)value)->comp = component;
    }

    /* set the size of this OID */
    value->size = i;

    firstcomp = component;

    for (dataptr = bufferptr; dataptr < endptr; ) {
        i = 0;
        do {
            i <<= 7;
            i |= *dataptr & 0x7f;
        } while (*dataptr++ & 0x80);
        if (component == firstcomp )
	{
	   if (i<40)
           {
              *component++ = 0;	/* iso(0) */
              *component++ = i;
           }
           else if (i<80)
           {
              *component++ = 1;	/* ccitt(1) */
              *component++ = i - 40;
           }
           else
           {
              *component++ = 2;	/* joint-iso-ccitt(2) */
              *component++ = i - 80;
           }
	}
        else
            *component++ = i;
    }
       
    if( freebuffer )
	free( bufferptr );

    return( SUCCESS );


}


int __ROr_BERskip_oid( instance, value, valuelength, encodedlength,
                    maxlength, maxlength2 )
    __RO_Instance_Record	*instance;
    void			*value;
    unsigned long		*valuelength;
    unsigned long		*encodedlength;
    unsigned long		*maxlength;
    unsigned long		*maxlength2;
 
{
   __RO_int32 encodedtype;

   if (*encodedlength == ASN_LEN_INDF)
   {
      if ((instance->getP_header)(instance,&encodedtype,encodedlength))
         return(EXCEPTION);
      while (encodedtype != TAG_EOC)
      {
         if(__ROr_BERskip_oid(instance,NULL,NULL,encodedlength,NULL,NULL))
            return(EXCEPTION);
         if ((instance->getP_header)(instance,&encodedtype,encodedlength))
            return(EXCEPTION);
      }
   }
   else
      if (__ROr_STDconsume_data(instance,encodedlength))
            return(EXCEPTION);
   return(SUCCESS);
}

