/*****************************************************************/
/**                                                             **/
/**             (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_conv_oid[] = "@(#)conv_oid.c	1.12 12/3/92 12 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	11/6/90

	ROUTINE:	__ROr_convert_ObjectIdentifier_const


PURPOSE:
	This routine converts an ObjectIdentifier into a struct representation.

IN:
	valuelength - length in bytes of value variable
	constant - string of value expression

OUT:
	value - value specified by the constant argument


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

#include <elros/exceptions.h>
#include <elros/instance.h>

static int vfy_oid();

int __ROr_convert_ObjectIdentifier_const(value,valuelength,maxlength,maxlength2,constant)

	void *value;
	unsigned long valuelength;
	unsigned long maxlength;
	unsigned long maxlength2;
	char *constant;

{
   struct __RO_oid_skeleton *oid = (struct __RO_oid_skeleton *)value;
   int i,j;
   unsigned long *oidval;

   if (vfy_oid(constant))
      return(__ROx_CONST_OIDERROR);

   if ( maxlength && (valuelength-sizeof(int))%sizeof(unsigned long))
      return(__ROx_CONST_OIDMODNOT0);

   oid->size = 1;
   for (i=0; i<strlen(constant); i++)
      if (constant[i] == '.') oid->size++;

   if (maxlength && oid->size > maxlength)
      return (__ROx_CONST_OIDLENGTH);

   if (!maxlength) {
      oidval = (unsigned long*)malloc(oid->size * sizeof(unsigned long));
      if (!oidval)
        return (__ROx_GENERAL_MALLOCFAILED);
      ((struct __RO_oid_indef *)value)->comp = oidval;
   }
   else
     oidval = &oid->comp[0];

   oidval[0] = 0;

   for (i=0,j=0; i<strlen(constant); i++)
   {
      if (constant[i] == '.')
         j++,oidval[j] = 0;
      else
	 /* we won't worry about overflow! */
         oidval[j] = oidval[j]*10 + constant[i]-'0';
   }
   return(SUCCESS);
}

static int vfy_oid(constant)

   char *constant;

{
   if (*constant < '0' && *constant > '9')
      return(EXCEPTION);
   while (*(++constant))
   {
      if (*constant == '.')
         break;
      if (*constant < '0' && *constant > '9')
         return(EXCEPTION);
   }
   if (!*constant)
      return(EXCEPTION);

   while (*(++constant))
   {
      if (*constant < '0' && *constant > '9')
         return(EXCEPTION);
      while (*(++constant))
      {
         if (*constant == '.')
            break;
         if (*constant < '0' && *constant > '9')
            return(EXCEPTION);
      }
      if (!*constant)
         return(SUCCESS);
   }
   return(EXCEPTION);

}



