/*****************************************************************/
/**                                                             **/
/**             (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_bool[] = "@(#)getP_bool.c	1.24 10/20/92 05:51:27 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Robyne M. Sumpter
			Network Systems Division

	DATE WRITTEN:	7/18/90

	ROUTINE:	__ROr_getP_Boolean

PURPOSE:
        Receive and decode an ASN.1 representation of a boolean.  If the ASN.1
        represented value is 0xff, the least significant bit of the value
        returned from this routine will be set.


OUT:
        value - The decoded ASN.1 value.
        instance -
           Receive state and error state are updated.
 


CALLED ROUTINES:
        getP_1element
	

	
MODIFICATION HISTORY:


*/

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

#include <memory.h>


#define CURRENT_TYPE_LENGTHS_FIELD	Boolean_lengths

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

    {

    int numbytes = 1;
    int little_endian_flag;
    int index;
    unsigned char buffer;
    unsigned char *valueptr = (unsigned char *)value;


    if( *encodedlength > 1 )
	__RO_RETURN_RUNTIME( instance, __ROx_DCD_EXCEEDEDMAXLEN ); 

    __ROr_getP_1element(instance,&buffer);

			/* If value pointer is null just consume the bytes
			   in the buffer and return. */
    if( value != NULL )
    {
        int NonZeroValue;

	
	memset( valueptr, 0, *valuelength );


			/* Is the value received TRUE or FALSE */
 	if( (buffer & 0xff) != 0 )
	    NonZeroValue = 1;
	else
    	    NonZeroValue = 0;


			/* Determine the type of variable to recieve 
			   the data and cast the value into it */
	if( ISCHAR( instance, *maxlength, *valuelength )	|| 
	    ISSHORT( instance, *maxlength, *valuelength )	||
	    ISINT( instance, *maxlength, *valuelength )		||	 
	    ISLONG( instance, *maxlength, *valuelength )	|| 
	    ISLONGLONG( instance, *maxlength, *valuelength ) ) {
          little_endian_flag = LITTLE_ENDIAN;
          index = little_endian_flag ? 0 : *valuelength - 1;
 	} else {
	  little_endian_flag = 
		instance->type_information->non_native_endianness == __ROm_LITTLE_ENDIAN;
          index = little_endian_flag ? 0 : *maxlength - 1;
        }


	if( NonZeroValue )
	   valueptr[index] = 0x01;
	   
	    
    }

    return( SUCCESS );
}
