/*****************************************************************/
/**                                                             **/
/**             (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_extend_arith_c[] = "@(#)extend_arith.c	1.10 10/20/92 05:40:35  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	11/6/90

	ROUTINES:	__ROr_arith_times10
			__ROr_arith_divide10
			__ROr_arith_add_long
			__ROr_arith_neg
			__ROr_arith_abs

			__ROr_arith_atoN
			__ROr_arith_atoI
			
*/

#include <elros/extend_arith.h>

#include <memory.h>
#include <malloc.h>

	/* This flag will include code to prevent the output conversion
	   routines (Itoa, Ntoa) from ever writing on the input bytes
	   (i.e. they make a private copy) -- if this flag is 0, they
	   may (will) destroy the input number. */

#define PREVENT_TRASHING	1

/**************************************************************

	ROUTINE:	__ROr_arith_times10

PURPOSE:
	This routine multiplies an arbitrary precision binary integer by 10.

IN:
	bytes - array of bytes to multiply by 10
	length - number of bytes

OUT:
	bytes - multiplied array of bytes 
RETURNS
	carry into next place (overflow)

CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/


int __ROr_arith_times10(bytes,length)

   unsigned char *bytes;
   unsigned long length;

{
   unsigned long carry,mult,prod,prev,eight,two;
   int i;

   for (carry=0,prev=0,i=0;i<length;i++)
   {
      mult = bytes[i];
      mult = (mult << 8) | prev;
      two = (mult >> 7) & 0xFF;
      eight = (mult >> 5) & 0xFF;
      prod = carry + eight + two;
      prev = bytes[i];
      bytes[i] = prod & 0xFF;
      carry = prod >> 8;
   }
   two = (prev >> 7) & 0xFF;
   eight = (prev >> 5) & 0xFF;
   carry += eight + two;
   return((int)carry);
}

/**************************************************************

	ROUTINE:	__ROr_arith_divide10

PURPOSE:
	This routine divides an arbitrary precision binary integer by 10.

IN:
	bytes - array of bytes to divide by 10
	length - number of bytes

OUT:
	bytes - quotient array of bytes 
RETURNS:
	remainder of the division

CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/


int __ROr_arith_divide10(bytes,length)

   unsigned char *bytes;
   unsigned long length;

{
   unsigned long remain,div;
   int i;

   for (remain=0,i=length-1; i>=0; i--)
   {
      div = bytes[i];
      div = (remain << 8) | div;
      bytes[i] = div / 10;
      remain = div % 10;
   }
   return((int)remain);
}

/**************************************************************

	ROUTINE:	__ROr_arith_leftshift

PURPOSE:
	This routine shifts an arbitrary precision binary integer left 1 bit.

IN:
	bytes - array of bytes to divide by 10
	length - number of bytes
	bit - bit to shift in

OUT:
	bytes - shifted value
RETURNS:
	bit shifted out

CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/


int __ROr_arith_leftshift(bytes,length,bit)

   unsigned char *bytes;
   unsigned long length;
   int bit;

{
   int i;
   unsigned long carry;

   for (i=0,carry=bit&0x01; i<length; i++)
   {
      unsigned long temp;

      temp = bytes[i];
      bytes[i] = ((temp << 1) | carry) & 0xFF;
      carry = (temp >> 7) & 0x01;
   }
   return((int)carry);
}

/**************************************************************

	ROUTINE:	__ROr_arith_rightshift

PURPOSE:
	This routine shifts an arbitrary precision binary integer right 1 bit.

IN:
	bytes - array of bytes to divide by 10
	length - number of bytes

OUT:
	bytes - shifted value
RETURNS:
	bit shifted out

CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/


int __ROr_arith_rightshift(bytes,length,bit)

   unsigned char *bytes;
   unsigned long length;
   int bit;

{
   int i;
   unsigned long carry;

   for (i=length-1,carry=bit&0x01; i>=0; i--)
   {
      unsigned long temp;

      temp = bytes[i];
      bytes[i] = ((temp >> 1) | (carry << 7)) & 0xFF;
      carry = temp & 0x01;
   }
   return((int)carry);
}

/**************************************************************

	ROUTINE:	__ROr_arith_add_long

PURPOSE:
	This routine increments a two's-complement binary integer.

IN:
	bytes - array of bytes to increment
	length - number of bytes
	carry - number to be added

OUT:
	bytes - incremented array of bytes 


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/
int __ROr_arith_add_long(bytes,length,carry)

   unsigned char *bytes;
   unsigned long length;
   long carry;

{
   int i;

   for (i=0; carry && i<length; i++)
   {
      carry += bytes[i];
      bytes[i] = carry & 0xFF;
      carry >>= 8;
   }
   return(carry);
}


/**************************************************************

	ROUTINE:	__ROr_arith_neg

PURPOSE:
	This routine negates a positive two's-complement binary integer.

IN:
	bytes - array of bytes to negate
	length - number of bytes

OUT:
	bytes - negated array of bytes 


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

int __ROr_arith_neg(bytes,length)

   unsigned char *bytes;
   unsigned long length;

{
   int i,sign,carryout;

   sign = bytes[length-1] & 0x80;
   for (i=0; i<length; i++)
      bytes[i] ^= 0xFF;
   carryout = add_long(bytes,length,1);
   return(sign && (bytes[length-1] & 0x80));
}

/**************************************************************

	ROUTINE:	__ROr_arith_abs

PURPOSE:
	This takes the absolute value of a two's-complement binary integer.

IN:
	bytes - array of bytes to negate
	length - number of bytes

OUT:
	bytes - negated array of bytes 


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

int __ROr_arith_abs(bytes,length)

   unsigned char *bytes;
   unsigned long length;

{
   int i,sign,carryout;

   sign = bytes[length-1] & 0x80;
   if (sign)
      negate(bytes,length);
   return(sign);
}

/**************************************************************

	ROUTINE:	__ROr_arith_zeroeq

PURPOSE:
	This routine tests if a binary integer is 0.

IN:
	bytes - array of bytes to negate
	length - number of bytes

OUT:
RETURNS	1 or 0 depending on equality


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

int __ROr_arith_zeroeq(bytes,length)

   unsigned char *bytes;
   unsigned long length;

{
   int i;

   for (i=0; i<length; i++)
      if (bytes[i] != 0x00)
         return(0);
   return(1);
}


/**************************************************************

	ROUTINE:	__ROr_arith_swapends

PURPOSE:
	This routine reverses the ordering of a byte sequence.

IN:
	bytes - array of bytes to swap
	length - number of bytes

OUT:


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

int __ROr_arith_swapends(bytes,length)

   unsigned char *bytes;
   unsigned long length;

{
   int i;

   for (i=0; i<length/2; i++)
   {
      unsigned char temp;

      temp = bytes[i];
      bytes[i] = bytes[length-i-1];
      bytes[length-i-1] = temp;
   }
      
   return(0);
}



/**************************************************************

	ROUTINE:	__ROr_arith_atoN

PURPOSE:
	This routine converts a digit string into binary form.

IN:
	digits - decimal digit string to convert.

OUT:
	bytes - array of bytes containing converted binary form.
	length - number of bytes


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

int __ROr_arith_atoN(digits,bytes,length)

   char *digits;
   unsigned char *bytes;
   unsigned long length;

{
   int i;

   memset(bytes,0x00,length);
   for (i=0; i<strlen(digits); i++)
   {
      if (times10(bytes,length))
         return(1);
      add_long(bytes,length,(unsigned char)(digits[i] - '0'));
   }
   return(0);
}

/**************************************************************

	ROUTINE:	__ROr_arith_atoI

PURPOSE:
	This routine converts a signed digit string into binary form.

IN:
	digits - decimal digit string to convert.

OUT:
	bytes - array of bytes containing converted binary form.
	length - number of bytes


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

int __ROr_arith_atoI(digits,bytes,length)

   char *digits;
   unsigned char *bytes;
   unsigned long length;

{
   int i,neg;

   neg = *digits == '-';
   if (*digits=='-' || *digits=='+')
      digits++;
   i = atoN(digits,bytes,length);
   if (i)
      return(i);
   if (bytes[length-1] & 0x80)
      return(-1);
   if (neg)
      return(negate(bytes,length));
   else
      return(0);
}

/**************************************************************

	ROUTINE:	__ROr_arith_Ntoa

PURPOSE:
	This routine converts a signed binary number into a digit string.

IN:
	bytes - array of bytes containing converted binary form.
	length - number of bytes

OUT:
	digits - decimal digit string to convert.


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

int __ROr_arith_Ntoa(bytes,length,buffer)

	unsigned char *bytes;
	unsigned long length;
	char *buffer;

{
   char *tempbuffer = malloc((int)(1+(length*8)/(1/LOG_BASE_10_OF_2)));
   unsigned char *byteptr =
#if PREVENT_TRASHING
	(unsigned char *)memcpy(malloc(length),bytes,length);
#else
	bytes;
#endif
   int i;

   for (i=0; !i || !zeroeq(byteptr,length);i++)
   {
      tempbuffer[i] = divide10(byteptr,length) + '0';
   }

   for (; i-- > 0;)
      *buffer++ = tempbuffer[i];
   *buffer = 0;

   free(tempbuffer);
#if PREVENT_TRASHING
   free(byteptr);
#endif
   return(0);
}



/**************************************************************

	ROUTINE:	__ROr_arith_Itoa

PURPOSE:
	This routine converts a signed binary number into a digit string.

IN:
	bytes - array of bytes containing converted binary form.
	length - number of bytes

OUT:
	digits - decimal digit string to convert.


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

int __ROr_arith_Itoa(bytes,length,buffer)

	unsigned char *bytes;
	unsigned long length;
	char *buffer;
{
   unsigned char *byteptr = 
#if PREVENT_TRASHING
	(unsigned char *)memcpy(malloc(length),bytes,length);
#else
	bytes;
#endif

   if (signof(byteptr,length))
      *buffer = '-';
   else
      *buffer = '+';
   absolute(byteptr,length);

   Ntoa(byteptr,length,buffer+1);
#if PREVENT_TRASHING
   free(byteptr);
#endif
   return(0);
}


/**************************************************************

	ROUTINE:	__ROr_remove_underscores

PURPOSE:
	This routine removes underscores from a string and returns the result.

IN:
	source - source string

OUT:
	dest - destination buffer


CALLED ROUTINES:

	
MODIFICATION HISTORY:


*/

int __ROr_remove_underscores(source,dest)

   char *source,*dest;

{
   int s,d;

   for (; *source; source++)
      if (*source != '_')
         *(dest++) = *source;
   *dest = 0;

}
