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

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	11/6/90

	ROUTINE:	__ROr_convert_BitString_const


PURPOSE:
	This routine converts a [01] string of bits into a BitString value.

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>

#include <memory.h>

static unsigned char mask[8] = 
   { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };

int __ROr_convert_BitString_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 *ptrval;
   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("^[01][01]*$");
   if (!re_exec(constant))
      return(__ROx_CONST_BITSTRINGERROR);

   ptr->len = strlen(constant);

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

   if (maxlength) {
      ptrval = ptr->val;
   }
   else {
      valuelength = ptr->len / 8 + (ptr->len%8 > 0);
      ptrval = (unsigned char *)malloc(valuelength);
      if (!ptrval)
         return(__ROx_GENERAL_MALLOCFAILED);
      ((__RO_ByteString_indef *)value)->val = ptrval;
   }

   memset(ptrval,0x00,valuelength);

   for (i=0; i< 8; i++) mask[i] = 0x80 >> (i%8);

   for (i=0,j=0; i<strlen(constant); i++,j+=(i%8==0))
   {
      if (constant[i] == '1')
         ptrval[j] |= mask[i%8];
   }

   free(constant_string);

   return(SUCCESS);
}


