/*****************************************************************/
/**                                                             **/
/**             (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_oid[] = "@(#)sendP_oid.c	1.22 10/20/92 06:01:40 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		Robyne M. Sumpter
			Network Systems Division

	DATE WRITTEN:	7/18/90

	ROUTINE:	__ROr_BERsendP_ObjectIdentifier

PURPOSE:
	Encode in ASN.1 an object identifier.


IN:
	value - the data to be encoded.
	valuelength - the number of bytes in value.
	maxlength - maximum number of elements in the oid.
	maxlength2 - maximum number of bytes in each oid element.

OUT:
	instance-
	  buffer state is updated.
	  error information is updated.


CALLED ROUTINES:
	__ROr_encP_length()
	

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>
#include <elros/enc_dcd.h>
#include <elros/encP_length.h>
#include <elros/sendP_element.h>
 
int __ROr_BERsendP_ObjectIdentifier( instance, value, valuelength, 
				     maxlength, maxlength2 )
    __RO_Instance_Record	*instance;
    void			*value;
    unsigned long		*valuelength;
    unsigned long		*maxlength;
    unsigned long		*maxlength2;
 
    {

    long	i,
		m,
		n,
		*compptr,
		*componentlst;
    long 	j,
		*component;
    unsigned char 	*dataptr,
			*endptr,
			*oid_bytes;
    struct __RO_oid_indef val;
   
    val.size = ((struct __RO_oid_indef *)value)->size;
    val.comp = *maxlength ?
               ((struct __RO_oid_skeleton *)value)->comp :
               ((struct __RO_oid_indef *)value)->comp;

    if( *maxlength2 > sizeof(long) )
	__RO_RETURN_RUNTIME( instance, __ROx_ENC_OIDERRMAXLEN2 );

    if( *maxlength && val.size > *maxlength )
	__RO_RETURN_RUNTIME( instance, __ROx_ENC_OIDVALEXCEEDMAXLEN );


    /* Get space for oid bytes */
    if( (componentlst = (long *)malloc((unsigned)(val.size) * sizeof(*componentlst))) == NULL )
	__RO_RETURN_RUNTIME( instance, __ROx_ENC_OUTOFMEM );    
    if (val.size < 2)
       __RO_RETURN_RUNTIME(instance, __ROx_ENC_OIDLESS2);
    if (val.comp[0] > 2)
       __RO_RETURN_RUNTIME(instance, __ROx_ENC_OID1GT2);
    if (val.comp[1] > 39 && val.comp[0] < 2)
       __RO_RETURN_RUNTIME(instance, __ROx_ENC_OID2GT39FOR01);

   
					/* Find out how many bytes are needed */
    for(i = n = 0, component = (long *)val.comp, compptr = componentlst; 
		i < val.size; i++, component++ )
    {
 
	if( component == (long *)val.comp )
		/* i.e.: we're doing the first component:
			So, combine the values of the first two. */
	    j = *component++ * 40, i++, j+= *component;
	else
	    j = *component;

	m = 0;
	do 
	{
	    m++;
	    j >>= 7;
	} while ( j );
	n += (*compptr++ = m);
    }


			/* Encode the length of the oid */
    __ROr_encP_length( instance, &n );


    if( (oid_bytes = (unsigned char *)malloc( (int)n ) ) == NULL )
	__RO_RETURN_RUNTIME( instance, __ROx_ENC_OUTOFMEM );

    dataptr = oid_bytes;
    for( i = 0, component = (long *)val.comp, compptr = componentlst; 
		i < val.size; i++, component++ )
    {

 	if( component == (long *)(val.comp ) )
	    j = *component++ * 40, i++, j += *component;
	else
	    j = *component;

	endptr = dataptr + (m = *compptr++) - 1;
	for( dataptr = endptr; m-- > 0; j >>= 7 )
	    *dataptr-- = (j & 0x7f) | 0x80;
	*endptr &= ~0x80;

	dataptr = endptr + 1;
    }


    __ROr_sendP_Nelement( instance, oid_bytes, n );  


    free( (char *)oid_bytes );
    free( (char *)componentlst );


    return (SUCCESS);
}
