/*
 * TCPVIEW
 *
 * Author:	Martin Hunt
 *		Networks and Distributed Computing
 *		Computing & Communications
 *		University of Washington
 *		Administration Building, AG-44
 *		Seattle, WA  98195
 *		Internet: martinh@cac.washington.edu
 *
 *
 * Copyright 1992 by the University of Washington
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appears in all copies and that both the
 * above copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the University of Washington not be
 * used in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  This software is made
 * available "as is", and
 * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
 * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
 * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
 * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#ifndef lint
static char rcsid[] =
    "@(#) $Header: /usr/staff/martinh/tcpview/RCS/hex.c,v 1.1 1993/04/22 20:23:36 martinh Exp $ (UW)";
#endif

#include <stdio.h>

static int hatoi();
int hexread();
char *readline();
extern int PrintFrames;

/*****
main()
{
  long lw;

  while(hexread(&lw,4))
    printf("%lx",lw);
}
*****/

int hexread(ptr, len)
     void *ptr;
     int len;
{
  static char buf[4096];
  static char *bufend = buf;
  static char *bp = buf;
  char *tmp;
  int num;

  while( bufend - bp < len ) {
    num = bufend - bp;
    bcopy(bp,buf,num);
    bp = buf;
    tmp = readline(buf+num);
    if(tmp==NULL)
      return 0;
    else
      bufend = tmp;
  }
  bcopy(bp,ptr,len);
  bp += len;
  return 1;
}

char *readline(buf)
     char *buf;
{
  char str[256];
  char *s;

  do {
    if( gets(str) == NULL )
      return NULL;
    if( *str != '\t' && *str != ' ' && PrintFrames ) 
      printf("\n%s\n",str);
  } while (*str != '\t' && *str != ' ' );

  /* now we've got a hex line in str */
  s = str;
  while(*s && isspace(*s)) s++;		/* eat white space */
  while(*s && !isspace(*s)) s++;	/* eat offset */

  do {
    while( *s && isspace(*s) ) s++;
    if ( *s ) {
      if( *s == '|' )
	return( buf );
      *buf++ = hatoi( s );
      s += 2;
    }
  } while( *s );

  return( buf );
}



/* 2 digit hex ascii to int */
static int hatoi(str)
char *str;
{
  register int d;

  str[0] = tolower(str[0]);
  if(str[0]>='0'&&str[0]<='9')
    d = str[0]-'0';
  else
    d = str[0]-'a'+10;

  if(str[1]) {
    str[1] = tolower(str[1]);
    d *= 16;
    if(str[1]>='0'&&str[1]<='9')
      d+= str[1]-'0';
    else
      d+= str[1]-'a'+10;
  }
  return(d);
}


syntax highlighted by Code2HTML, v. 0.9.1