/*****************************************************************/
/**                                                             **/
/**             (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_bitstr[] = "@(#)getP_bitstr.c	1.21 12/3/92 10:00:57  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Robyne M. Sumpter
			Network Systems Division

	DATE WRITTEN:	08/02/90

	ROUTINE:	__ROr_BERgetP_BitString

PURPOSE:


IN:
	instance - instance record
	value - pointer to ELROS BitString struct
	valuelength - size of value's val field in bytes
	encodedlength - length in bytes of received value, from getP_header()
	maxlength - max length in bits of this BitString type
	maxlength2 - unused

OUT:
	encodedlength - actual bit length of value received


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:
	Modified for changes to BitString types, 920306 (ls)


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

unsigned char usedmask[9] = { 0x00, 0x80, 0xc0, 0xe0, 
                              0xf0, 0xf8, 0xfc, 0xfe, 0xff };

static int __ROr_dcdP_BitString();

int __ROr_BERgetP_BitString( 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 int lastbits = 0; /* number of unused bits in last byte of 
                                 received BitString value */
   unsigned long length = 0;  /* byte length of received value */
   unsigned char usedbits; /* #bits used in last received value byte */
   int newmaxlength; /* byte size of actual value object, if any */
   int maxbytelength = *maxlength/8 + (*maxlength%8 > 0);

   /* If value object exists but too small for type, report error */
   if ( value && *maxlength && *valuelength < maxbytelength )
      __RO_RETURN_RUNTIME(instance,__ROx_DCD_EXCEEDEDVALLEN);

   /* If value is null we want to consume all of the data, so set 
      allowedvaluelength to max length (in bytes) to allow dcdP_BitString() 
      to consume all of it. */

   if( !value )
     newmaxlength = *encodedlength;
   else
     newmaxlength = *maxlength ? maxbytelength : *encodedlength;

   if (value && !*maxlength) {
      __RO_ByteString_indef *valtmp;
      valtmp  = (__RO_ByteString_indef *)value;
      valtmp->val = (unsigned char *)malloc(newmaxlength);
      if (valtmp == NULL)
         __RO_RETURN_RUNTIME(instance,__ROx_GENERAL_MALLOCFAILED);
      if (__ROr_dcdP_BitString(instance,valtmp->val,&newmaxlength,
                               encodedlength, &length,&lastbits))
         return(EXCEPTION);
   }
   else {
      if (__ROr_dcdP_BitString(instance,value ? value->val : NULL,
                               &newmaxlength, encodedlength, &length,
                               &lastbits))
         return(EXCEPTION);
   }

   if (value)
   {
      usedbits = 8 - lastbits;
      value->len = (length-1)*8 + usedbits; /* received # of bits */

      if (*maxlength && value->len > *maxlength)
         __RO_RETURN_RUNTIME(instance,__ROx_DCD_EXCEEDEDMAXLEN);

      if (length > 0) { /* fix last byte received */
         if (*maxlength)
            value->val[length-1] &= usedmask[usedbits]; 
         else
            ((__RO_ByteString_indef *)value)->val[length-1] &= 
                                                      usedmask[usedbits];
      }
      else
         if (lastbits != 0) /* should never happen here */
            __RO_RETURN_RUNTIME(instance,__ROx_DCD_BITSTRINGNOT0BITS);

      if (*maxlength)
         for ( ; length < *valuelength; length++) /* fill out value bytes */
            value->val[length] = 0;
   }

   return(SUCCESS);

}



static int __ROr_dcdP_BitString(instance,value,maxlength,encodedlength,
				length,lastbits)

    __RO_Instance_Record	*instance;
    unsigned char 		*value;
    unsigned long		*maxlength; /* in bytes */
    unsigned long		*encodedlength; /* in bytes */
    unsigned long		*length; /* in bytes */
    unsigned int		*lastbits;

{
   __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 3] */

         if (type != TAG_BitString)
            __RO_RETURN_RUNTIME(instance,__ROx_DCD_BITSTRINGNOTU3);

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

   }
   else
   {
	/* Since this is a primitive element, we know the "last" one was not
	   the final one, so we verify that the initial octet (unused bits)
	   was 0. */

      if (*lastbits != 0)
         __RO_RETURN_RUNTIME(instance,__ROx_DCD_BITSTRINGNOT8BITS);

      /* Get the initial octet (unused bits), and keep for posterity */
      {
         unsigned char Octet;

         __ROr_getP_1element(instance,&Octet);
         *lastbits = Octet;
      }

      /* Discount the initial octet, and make sure the length will fit */

      if (*encodedlength > 0)
      {
         (*encodedlength)--;
         if (*encodedlength == 0 && *lastbits != 0)
           __RO_RETURN_RUNTIME(instance,__ROx_DCD_BITSTRINGNOT0BITS);
      }
      else
         __RO_RETURN_RUNTIME(instance,__ROx_DCD_BITSTRINGLENGTH0);

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

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

      if( !value )			/* Consume the data */
	{
	if(  __ROr_STDconsume_data( instance, encodedlength )  )
	    return( EXCEPTION );
	}
      else
	{
	   __ROr_getP_Nelement(instance,*encodedlength,value+*length);
	}
      *length += *encodedlength;

   }

   return(SUCCESS);
}


