#include "config.h"
#include "packet.h"
#include "header.h"

/*
 * compare two strings up to a '\0', '\r' or n bytes
 */


Estrncmp(string1, string2, n)
register char *string1, *string2;
int n;
{
	register int i = 1;

	while ( (i < n) && (*string1 == *string2) &&
		(*string1 != 0x0D) && *string1) {

		string1++;
		string2++;
		i++;
	}
	if (*string1 == *string2)
		return(1);
	return(0);
}


jscan(string, integer, length)
char *string;
unsigned int *integer;
int length;
{
	unsigned char c;
	unsigned int i=0;
	int len=0,actual_len=0;
	
	while((c = *string++) && (len++ < length) && (c > 0x2F))
	{
		i <<= 4;
		actual_len++;

		if((c>='A') && (c<='F'))
			i += c-0x37;
		else if((c>='a') && (c<='f'))
			i += c-0x57;
		else
			i += c-0x30;
	}
	*integer = i;
	return(actual_len);
}

char bintohex(bin)
unsigned char bin;
{
	unsigned char c;
	if ((bin&0xF) < 10)
		c = (bin&0xF) + 0x30;
	else
		c = (bin&0xF) + 0x37;
	return(c);
}

addhex(buff, integer, length)
char *buff;
unsigned int integer;
int length;
{
	int i;
	unsigned char c;

	if (length != 0) {
		for (i=1; i<=length; i++) {
			*(buff++) = bintohex((integer >> (length-i)*4) & 0xF);
		}
	} else {
		for (i=1; i<=7; i++) {
			if( (c = bintohex((integer >> (8-i)*4) & 0xF)) != 0x30)
				 *(buff++) = c;
		}
		*(buff++) = bintohex(integer & 0xF);
	}
	/*
	*buff = '\0';
	*/
}

addchr(buff,c)
char *buff;
unsigned char c;
{
	while(*buff)
		buff++;
	*buff++ = c;
	*buff = '\0';
}

addstr(buff,string)
char *buff,*string;
{
	char c;
	while(*buff)
		buff++;
	while(c = *string++)
		*buff++ = c; 
	*buff = '\0';
}

putstr(str)
char *str;
{
	register char *p;

	for (p = str; *p != '\0'; p++) {
		putchar(*p);
	}
}

putstrn(str, n)
char *str;
{
	register char *p;

	for (p = str; p - str < n; p++) {
		putchar(' ');
		if ( *p < 0x20) {
			putchar('^');
			putchar(*p + 0x40);
		} else
			putchar(*p);
	}
}

/*
 * print an integer in a hex format
 */
putnum(n)
{
	int	i, j;

	for (j = 3; j >= 0; j--) {
		i = (n >> (8*j)) & 0xff;
		putchar(bintohex((i & 0xf0) >> 4));
		putchar(bintohex(i & 0xf));
	}
}



#define M_ECHO  0x4
#define M_CRNL  0x8

iscrnl()
{
	char	*p = (char *)0x1;

	return(*p & M_CRNL);
}

isecho()
{
	char	*p = (char *)0x1;

	return(*p & M_ECHO);
}

dmp(addr)
int *addr;
{
	char buf[30];

	buf[0] = '\0';
	addhex(buf, addr, 8);
	addstr(buf, " = ");
	addhex(buf + strlen(buf), *addr, 8);
	putstr(buf); putstr("          ");
}

