/*****************************************************************/
/**                                                             **/
/**             (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_octstr[] = "@(#)conv_octstr.c	1.13 10/20/92 05:36:36  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	11/6/90

	ROUTINE:	__ROr_convert_OctetString_const


PURPOSE:
	This routine converts an octal or hexadecimal string into
	a string of bytes.

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/typeinfo.h>
#include <elros/exceptions.h>
#include <elros/extend_arith.h>

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

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

{
   __RO_ByteString *ptr = (__RO_ByteString *) value;
   unsigned char *byteptr;
   int i,j;
   char *constant_string;

   constant_string = (char *)malloc(strlen(constant));
   if (!constant_string)
      return(__ROx_GENERAL_MALLOCFAILED);

   no_underscores(constant,constant_string);

   re_comp("^0x[0-9A-Fa-f_][0-9A-Fa-f_]*$");
   if (!re_exec(constant))
      return(__ROx_CONST_OCTETSTRINGERROR);

   ptr->len = (strlen(constant)-1)/2;

   if (maxlength && ptr->len > maxlength)
      return(__ROx_CONST_VALUELENGTH);

   if (maxlength)
      byteptr = &ptr->val[0];
   else {
      valuelength = ptr->len;
      byteptr = (unsigned char*)malloc(valuelength);
      if (!byteptr)
        return (__ROx_GENERAL_MALLOCFAILED);
      ((__RO_ByteString_indef*)value)->val = byteptr;
   }

   memset(byteptr,0x00,valuelength);

   for (i=2,j=0; i<strlen(constant); i++) {
      *byteptr = j%2
                 ? *byteptr + HEXTODIGIT(constant[i]) 
                 : HEXTODIGIT(constant[i])*16;
      if ((j++)%2)
         byteptr++;
   }

   free(constant_string);

   return(SUCCESS);
}


