/*****************************************************************/
/**                                                             **/
/**             (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_int[] = "@(#)getP_int.c	1.26 10/20/92 05:51:32 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Robyne M. Sumpter
			Network Systems Division

	DATE WRITTEN:	7/18/90

	ROUTINE:	__ROr_getP_Integer

PURPOSE:
	To receive "encodedlength" number of bytes and decode them from their
	ASN.1 form to short, int, long.


IN:
	instance-
	   Receive state and error state are updated
	encodedlength - Number of encoded bytes to receive.

OUT:
	value - Decode integer value.


CALLED ROUTINES:
	instance->get_data	

	
MODIFICATION HISTORY:


*/

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

#include <memory.h>
#include <malloc.h>

#define CURRENT_TYPE_LENGTHS_FIELD	Integer_lengths


int __ROr_BERgetP_Integer( 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 char data;
    register unsigned long length = *encodedlength;


    if( !value )			/* Consumed data and return */
       {
	   if( __ROr_STDconsume_data( instance, encodedlength ) )
		return( EXCEPTION );

	   return( SUCCESS );
       }




    if( *valuelength < *maxlength )
	__RO_RETURN_RUNTIME( instance, __ROx_DCD_EXCEEDEDVALLEN );
    if( *encodedlength > *maxlength )
	__RO_RETURN_RUNTIME( instance, __ROx_DCD_EXCEEDEDMAXLEN );
    if (*encodedlength < 1)
        __RO_RETURN_RUNTIME( instance, __ROx_DCD_INTEGERLENGTH1 );



   __ROr_lookP_1element(instance,&data);
				/* Determine the type of the variable
				   to receive the value, short, int or long */
   if( ISCHAR(instance,*maxlength,*valuelength) )
   {
      char *newvalue = (char *)value;

      *newvalue = (data & 0x80) ? (-1) : 0;

      while( length > 0 )
      {
         __ROr_getP_1element(instance,&data);
         *newvalue = (*newvalue << 8) | (data & 0xff);
         length--;
      }
   }
   else if( ISSHORT(instance,*maxlength,*valuelength) )
   {
      short *newvalue = (short *)value;

      *newvalue = (data & 0x80) ? (-1) : 0;

      while( length > 0 )
      {
         __ROr_getP_1element(instance,&data);
         *newvalue = (*newvalue << 8) | (data & 0xff);
         length--;
      }
   }
   else if( ISINT(instance,*maxlength,*valuelength) )
   {
      int *newvalue = (int *)value;

      *newvalue = (data & 0x80) ? (-1) : 0;

      while( length > 0 )
      {
         __ROr_getP_1element(instance,&data);
         *newvalue = (*newvalue << 8) | (data & 0xff);
         length--;
      }
   }
   else if( ISLONG(instance,*maxlength,*valuelength) )
   {
      long *newvalue = (long *)value;

      *newvalue = (data & 0x80) ? (-1) : 0;

      while( length > 0 )
      {
         __ROr_getP_1element(instance,&data);
         *newvalue = (*newvalue << 8) | (data & 0xff);
         length--;
      }
   }
   else if( ISLONGLONG(instance,*maxlength,*valuelength) )
   {
      LONGEST_INTEGER *newvalue = (LONGEST_INTEGER *)value;

      *newvalue = (data & 0x80) ? (-1) : 0;

      while( length > 0 )
      {
         __ROr_getP_1element(instance,&data);
         *newvalue = (*newvalue << 8) | (data & 0xff);
         length--;
      }
   }
   else
   {
      unsigned char *newvalue = (unsigned char *)value;
      int i,sign;

      sign = data & 0x80 ? -1 : 0;

      if (instance->type_information->non_native_endianness == __ROm_BIG_ENDIAN)
      {
         __ROr_getP_Nelement(instance,length,newvalue+(*maxlength-*encodedlength));
         memset(newvalue,sign,(*maxlength - *encodedlength));
      }
      else if (instance->type_information->non_native_endianness == __ROm_LITTLE_ENDIAN)
      {
         unsigned char *newptr = newvalue+length-1;
         for (; length>0; length--,newptr--)
         {
            __ROr_getP_1element(instance,newptr);
         }
         memset(newvalue+*encodedlength,sign,(*valuelength - *encodedlength));
      }
      else
         __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_MUTUAL);
   }


   return( SUCCESS );
}
