/*****************************************************************/
/**                                                             **/
/**             (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_charstr[] = "@(#)getP_charstr.c	1.4 12/3/92 10:01:00 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Robyne M. Sumpter
			Network Systems Division

	DATE WRITTEN:	7/9/90

	ROUTINE:	__ROr_BERgetP_CharString	

PURPOSE:
	


IN:

OUT:


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:
	920818  Split from getP_octstr.c, other <char> string changes.  (ls)


*/

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

static int __ROr_dcdP_CharString(); 

int __ROr_BERgetP_CharString( instance, value, valuelength, encodedlength,
			      maxlength, maxlength2)
    __RO_Instance_Record	*instance;
    void			*value;
    unsigned long		*valuelength;
    unsigned long		*encodedlength;
    unsigned long		*maxlength;
    unsigned long		*maxlength2;

{
   unsigned long newmaxlength;
   unsigned long length = 0;
   char *newvalue;
   int code;

   if (value && *maxlength && *valuelength-1 < *maxlength)
      __RO_RETURN_RUNTIME(instance,__ROx_DCD_EXCEEDEDVALLEN);

   /* If value is null we want to consume all of the data, so set maxlength
      to maxlength to allow dcdP_OctetString to consume all of it. */
   if( !value ) {
     newmaxlength = *encodedlength;
     newvalue = (char *)value;
   }
   else {
     if (*maxlength) {
        newvalue = (char *)value;
        newmaxlength = *valuelength - 1;
     }
     else {
        newmaxlength = *encodedlength + 1;
        newvalue = (char *)malloc(newmaxlength);
        if (newvalue == NULL)
           __RO_RETURN_RUNTIME(instance,__ROx_GENERAL_MALLOCFAILED);
        *(char **)value = newvalue;
     }
   }

   code = __ROr_dcdP_CharString(instance,newvalue,&newmaxlength,encodedlength,
				&length);

   if (newvalue)
      newvalue[length] = 0;
   return(code);

}



static int __ROr_dcdP_CharString(instance,value,maxlength,encodedlength,
				length)

    __RO_Instance_Record	*instance;
    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);
}


 
