/*****************************************************************/
/**                                                             **/
/**             (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_sendP_bitstr[] = "@(#)sendP_bitstr.c	1.20 11/13/92 10:44:16 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Robyne M. Sumpter
			Network Systems Division

	DATE WRITTEN:	08/02/90

	ROUTINE:	__ROr_BERsendP_BitString

PURPOSE:
	Encode in ASN.1 a bitstring.  


IN:
	value - The octets containing the bits to send.
	valuelength - actual number of bits in value
	maxlength - number of bits allowed for type
	maxlength2 - unused

OUT:
	instance -
	   Send state and error information are updated.


CALLED ROUTINES:
	__ROr_encP_length
	__ROr_sendP_1element
	__ROr_sendP_Nelement

	

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>
#include <elros/enc_dcd.h>
#include <elros/encP_length.h>
#include <elros/sendP_element.h>


int __ROr_BERsendP_BitString( instance, value, valuelength, maxlength, maxlength2)

   __RO_Instance_Record *instance;      /* pointer to instance record */
   unsigned char        *value;         /* location of unencoded value */
   unsigned long	*valuelength;   /* bit length of unencoded value */
   unsigned long	*maxlength;     /* max number of bits for type */
   unsigned long	*maxlength2;    /* unused */ 

{
   char unused_bits;
   int 	num_bytes;
   int 	used_bits;
   int  length;
   unsigned char mask = 0xff;
   unsigned char lastbyte;

   if( *maxlength && *valuelength > *maxlength)
	__RO_RETURN_RUNTIME( instance, __ROx_ENC_ERRVALUELEN );
   
   /* Calculate the number of bytes needed to store the bits. */
   num_bytes =  (*valuelength+7) / 8;

   /* Get the number of used bits in the last byte. */
   used_bits = *valuelength % 8;

   /* Send the length of the encoding.  We always use one byte more
      than the value bytes, to send the "unused bits" byte. */
   length = num_bytes+1;
   __ROr_encP_length(instance, &length);


   /* Send the lead (unused bits) byte */
   unused_bits = used_bits==0 ? 0 : ( 8 - used_bits ) & 0xff;
   __ROr_sendP_1element( instance, &unused_bits );
   

   /* Send the value */
   length--;

   if (length > 0) {
      __ROr_sendP_Nelement( instance, value, length-1);

      /* Zero out the unused bits in the last byte, and send it */

      lastbyte = value[num_bytes-1] & (mask << unused_bits);
      __ROr_sendP_1element( instance, &lastbyte );
   }

   return(SUCCESS);
}
