old/berknet/prot.c
/* read in and decode packet */
/* as a side effect sets "tooshort" */
read in and decode packet
as a side effect sets "tooshort"
read in and decode packet as a side effect sets " tooshort "
static struct packet *decpacket()
{
# ifndef RAND
	register char *p, *q;
	register int i,j;
	int n, len, ch;
	struct packet *pp;
	static char cooked[MAXNBUF], raw[MAXNBUF];
	alarm(netd.dp_atime);
	tooshort = 0;
	if(netd.dp_pipesim){
		if(netd.dp_usehispeed)
			len = read(fileno(netd.dp_rdfile),raw,SENDLEN);
		else {
			q = raw;
			len = 0;
			for(;;){
				ch = getc(netd.dp_rdfile);
				len++;
				if(ch == '\n'){
					*q++ = '\n';
					break;
				}
				if(ch == '\\' && netd.dp_use8bit)
					ch = getc(netd.dp_rdfile);
				*q++ = ch;
			}
			if(netd.dp_use8bit)len--;
		}
	}
	else if(netd.dp_usehispeed)
		len = read(fileno(netd.dp_rdfile),raw,SENDLEN);
	else len = read(netd.dp_linefd,raw,MAXNBUF);
	alarm(0);
	if(len == 0)fprintf(stderr,"eof pip %d\n",fileno(netd.dp_rdfile));
	if(len <= 0)return(NULL);
	raw[len] = 0;
	dump.braw += len;
	dump.brawtot += len;
	if(netd.dp_usehispeed)return((struct packet *)raw);
	if(netd.dp_use8bit){
		pp = (struct packet *)raw;
		if(len != ACKLENGTH + pp->len)tooshort = 1;
		return(pp);
	}
	for(p=raw; *p; p++)
		if(*p == '}')*p = '\\';
	p = raw;
	q = cooked;
	n = (len+3) /4;
	while(n--){
		if(*p == '\n')break;
		if(*p < INCR || *p & 0200)error("bad char %o\n",*p);
		i =  *p++ - INCR;
		j =  *p++ - INCR;
		*q++ = ((j & 03) << 6) | (i & 077);
		i =  *p++ -INCR;
		*q++ = ((i & 017) << 4) | ((j >> 2) & 017);
		j =  *p++ - INCR;
		*q++ = ((j & 077) << 2) | ((i >> 4) & 03);
		}
	*q = 0;
	pp = (struct packet *)cooked;
# ifdef SWAB
	switchem(pp);
# endif
	if(len != ((ACKLENGTH + pp->len + 2)/3)*4 + 1) tooshort = 1;
# else
	if(len != ACKLENGTH + gptr->len) tooshort = 1;
# endif
	return((struct packet *)cooked);
}
static struct packet * decpacket ( ) { # ifndef RAND register char * p , * q ; register int i , j ; int n , len , ch ; struct packet * pp ; static char cooked [ MAXNBUF ] , raw [ MAXNBUF ] ; alarm ( netd . dp_atime ) ; tooshort = 0 ; if ( netd . dp_pipesim ) { if ( netd . dp_usehispeed ) len = read ( fileno ( netd . dp_rdfile ) , raw , SENDLEN ) ; else { q = raw ; len = 0 ; for ( ; ; ) { ch = getc ( netd . dp_rdfile ) ; len ++ ; if ( ch == ' \n ' ) { * q ++ = ' \n ' ; break ; } if ( ch == ' \\ ' && netd . dp_use8bit ) ch = getc ( netd . dp_rdfile ) ; * q ++ = ch ; } if ( netd . dp_use8bit ) len -- ; } } else if ( netd . dp_usehispeed ) len = read ( fileno ( netd . dp_rdfile ) , raw , SENDLEN ) ; else len = read ( netd . dp_linefd , raw , MAXNBUF ) ; alarm ( 0 ) ; if ( len == 0 ) fprintf ( stderr , " \n " , fileno ( netd . dp_rdfile ) ) ; if ( len <= 0 ) return ( NULL ) ; raw [ len ] = 0 ; dump . braw += len ; dump . brawtot += len ; if ( netd . dp_usehispeed ) return ( ( struct packet * ) raw ) ; if ( netd . dp_use8bit ) { pp = ( struct packet * ) raw ; if ( len != ACKLENGTH + pp -> len ) tooshort = 1 ; return ( pp ) ; } for ( p = raw ; * p ; p ++ ) if ( * p == ' ' ) * p = ' \\ ' ; p = raw ; q = cooked ; n = ( len + 3 ) / 4 ; while ( n -- ) { if ( * p == ' \n ' ) break ; if ( * p < INCR || * p & 0200 ) error ( " \n " , * p ) ; i = * p ++ - INCR ; j = * p ++ - INCR ; * q ++ = ( ( j & 03 ) << 6 ) | ( i & 077 ) ; i = * p ++ - INCR ; * q ++ = ( ( i & 017 ) << 4 ) | ( ( j >> 2 ) & 017 ) ; j = * p ++ - INCR ; * q ++ = ( ( j & 077 ) << 2 ) | ( ( i >> 4 ) & 03 ) ; } * q = 0 ; pp = ( struct packet * ) cooked ; # ifdef SWAB switchem ( pp ) ; # endif if ( len != ( ( ACKLENGTH + pp -> len + 2 ) / 3 ) * 4 + 1 ) tooshort = 1 ; # else if ( len != ACKLENGTH + gptr -> len ) tooshort = 1 ; # endif return ( ( struct packet * ) cooked ) ; }
read in and decode packet
as a side effect sets "tooshort"
read in and decode packet as a side effect sets " tooshort "
/* read in chars to raw, if processed then return in cooked, otherwise
	return in raw */ /* eat up the backslashes */ /*
	debug("receive %d <<%s>>",len,raw);
	*/ /* if 8 bit the all we need to do is return */ /* remove this loop later */ /* for RAND */ /* not sure of the length computation */