/*****************************************************************/
/**                                                             **/
/**             (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_octstr[] = "@(#)getP_octstr.c	1.25 11/13/92 10:45:30 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Robyne M. Sumpter
			Network Systems Division

	DATE WRITTEN:	7/9/90

	ROUTINE:	__ROr_BERgetP_OctetString	

PURPOSE:
	


IN:

OUT:


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:


*/

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


int __ROr_BERgetP_OctetString( instance, value, valuelength, encodedlength,
			      maxlength, maxlength2)
    __RO_Instance_Record	*instance;
    __RO_ByteString		*value;
    unsigned long		*valuelength;
    unsigned long		*encodedlength;
    unsigned long		*maxlength;
    unsigned long		*maxlength2;

{
   unsigned long length = 0;
   int code;
   int newmaxlength;
   
   if ( value && *maxlength && *valuelength < *maxlength)
      __RO_RETURN_RUNTIME(instance,__ROx_DCD_EXCEEDEDVALLEN);

   /* If value is null we want to consume all of the data, so set maxlength
      to encodedlength to allow dcdP_OctetString to consume all of it. */
   if( !value )
     newmaxlength = *encodedlength;
   else
     newmaxlength = *maxlength ? *valuelength : *encodedlength;

   if (!*maxlength && value) {
      unsigned char *tmpval;
      tmpval = (unsigned char *)malloc(newmaxlength);
      if (tmpval == NULL && newmaxlength)
        __RO_RETURN_RUNTIME(instance,__ROx_GENERAL_MALLOCFAILED);
      ((__RO_ByteString_indef *)value)->val = tmpval;
      code = __ROr_dcdP_OctetString(instance,tmpval,&newmaxlength,encodedlength,
   				    &length);
   }
   else
      code = __ROr_dcdP_OctetString(instance,value?value->val:NULL,&newmaxlength,encodedlength,
				    &length);

   if (value) {
      value->len = length;
      if (*maxlength)
         for ( ; length < *valuelength; length++)
            value->val[length] = 0;
   }

   return(code);
}


int __ROr_dcdP_OctetString(instance,value,maxlength,encodedlength,
				length)

    __RO_Instance_Record	*instance;
    unsigned char 		*value;
    unsigned long		*maxlength;
    unsigned long		*encodedlength;
    unsigned long		*length;

{
   __RO_int32 type;

   if (*encodedlength == ASN_LEN_INDF)	/* CONSTRUCTED */
   {
      if ((instance->getP_header)(instance,&type,encodedlength))
         return(EXCEPTION);
      while (type != TAG_EOC)
      {
		/* Members must be [UNIVERSAL 4] */

         if (type != TAG_OctetString)
            __RO_RETURN_RUNTIME(instance,__ROx_DCD_OCTETSTRINGNOTU4);

         if (__ROr_dcdP_OctetString(instance,value,maxlength,encodedlength,
				length))
            return(EXCEPTION);
         if ((instance->getP_header)(instance,&type,encodedlength))
            return(EXCEPTION);
      }

   }
   else
   {
      if (*length + *encodedlength > *maxlength)
         __RO_RETURN_RUNTIME(instance,__ROx_DCD_EXCEEDEDMAXLEN);

	/* Suck in the bytes and bump up the "current length". */

      if (value)
      {
         __ROr_getP_Nelement(instance,*encodedlength,value + *length);
      }
      else
         if (__ROr_STDconsume_data(instance,encodedlength))
            return(EXCEPTION);
      *length += *encodedlength;

   }

   return(SUCCESS);
}

