/*****************************************************************/
/**                                                             **/
/**             (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_fenc_len_c[] = "@(#)fenc_len.c	1.4 10/20/92 08:41:52  LLNL ";


/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Robyne M. Sumpter	
			Network Systems Division

	DATE WRITTEN:	7/9/90

	ROUTINE:	fenc_len

PURPOSE:


IN:

OUT:


CALLED ROUTINES:
	fputhex	

	
MODIFICATION HISTORY:
8/10/90 - JAG
	send output to file


*/

#include <stdio.h>
#include <elros/enc_dcd.h>
fputhex();

static unsigned int high_byte = 0xff << ( (sizeof(unsigned int) - 1) * 8);

int fenc_len( file, length )
    FILE 	*file;
    int		*length;
    {

    unsigned int mask;
    int x,
	i;


					/* if the length is indefinite,
					   signify */

    if(*length == ASN_LEN_INDF)
      {
      fputhex(file,(ASN_LEN_XTND));
      return 0;
      }

					/* if the length is less than 128,
					   write the length byte and return */
    if(*length < ASN_LEN_SMAX)
      {
      fputhex(file,(*length & 0xff));
      return 0;
      }

					/* if the length is greater than 127,
					   write it out a byte at a time */

    mask = high_byte;

    for(i = sizeof(unsigned int) - 1; i >= 0; i--)
      {

      if((*length & mask) != 0)
	{
	break;
	}

      mask = mask >> 8;

      }


    fputhex(file,( (i + 1) | ASN_LEN_XTND));

    for(; i>= 0; i--)
      {
      fputhex(file,( (*length & mask) >> (i*8) ));
      mask = mask >> 8;
      }

    return 0;
  }
