/*****************************************************************/
/**                                                             **/
/**             (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_dcd_length[] = "@(#)dcd_length.c	1.16 10/20/92 05:40:25 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Robyne M. Sumpter
			Network Systems Division

	DATE WRITTEN:	7/9/90

	ROUTINE:	__ROr_dcdP_length

PURPOSE:
	Decode the asn.1 representation of a length and return it in value.
	This is the same as decoding an integer.


IN:
	instance-
	   Receive and error state are updated. 	
	

OUT:
	length - Decoded length value.


CALLED ROUTINES:
	getP_1element

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>
#include <elros/enc_dcd.h>
#include <elros/getP_element.h>
int __ROr_dcdP_length( instance, length )
    __RO_Instance_Record	*instance;
    unsigned long		*length;

{
    int	i;
    unsigned char data;


    __ROr_getP_1element( instance, &data );

                              /* if the length is extended, build the
                                 value from succeeding octets */
 
    if(( (i = data) & ASN_LEN_XTND) == ASN_LEN_XTND)
      {
                              /* if the length of the value exceeds the
                                 number of octets in an unsigned int,
                                 return a value error */
 
      if( (i = i & ASN_LEN_MASK) > sizeof(unsigned long))
        {
	__RO_RETURN_RUNTIME( instance, __ROx_DCD_LENGTHTOOBIG );
        }
 
                              /* if the length is definite, accumulate
                                 its value */
 
      if( i != 0)
        {
        for (*length = 0; (i--) > 0;)
          {
          __ROr_getP_1element( instance, &data );
 
          *length = (*length << 8) | (data & 0xff);
          }
        }
      else
        {
                              /* if the length is indefinite, set the
                                 length accordingly */
 
        *length = ASN_LEN_INDF;
        }
      }
    else
                                /* if the length is not extended, set it
                                   to the value in the first octet */
      {
      *length = i;
      }
    return(SUCCESS);

} 
