rem: ignorpkt++;
add: state = sSTART; headeridx = 0;
context: static int jtagmkII_recv_frame(PROGRAMMER * pgm, unsigned char **msg,			       unsigned short * seqno) {  enum states { sSTART,		/* NB: do NOT change the sequence of the following: */		sSEQNUM1, sSEQNUM2,		sSIZE1, sSIZE2, sSIZE3, sSIZE4,		sTOKEN,		sDATA,		sCSUM1, sCSUM2,		/* end NB */		sDONE  }  state = sSTART;  unsigned long msglen = 0, l = 0;  int headeridx = 0;  int timeout = 0;  int ignorpkt = 0;  int rv;  unsigned char c, *buf = NULL, header[8];  unsigned short r_seqno = 0;  unsigned short checksum = 0;  struct timeval tv;  double timeoutval = 5;	/* seconds */  double tstart, tnow;  if (verbose >= 3)    fprintf(stderr, "%s: jtagmkII_recv():\n", progname);  gettimeofday(&tv, NULL);  tstart = tv.tv_sec;  while ( (state != sDONE ) && (!timeout) ) {    if (state == sDATA) {      rv = 0;      if (ignorpkt) {	/* skip packet's contents */	for(l = 0; l < msglen; l++)	  rv += serial_recv(pgm->fd, &c, 1);      } else {	rv += serial_recv(pgm->fd, buf + 8, msglen);      }      if (rv != 0) {	timedout:	/* timeout in receive */	if (verbose > 1)	  fprintf(stderr,		  "%s: jtagmkII_recv(): Timeout receiving packet\n",		  progname);	free(buf);	return -1;      }    } else {      if (serial_recv(pgm->fd, &c, 1) != 0)	goto timedout;    }    checksum ^= c;    if (state < sDATA)      header[headeridx++] = c;    switch (state) {      case sSTART:        if (c == MESSAGE_START) {          state = sSEQNUM1;        } else {	  headeridx = 0;	}        break;      case sSEQNUM1:      case sSEQNUM2:	r_seqno >>= 8;	r_seqno |= (c << 8);	state++;	break;      case sSIZE1:      case sSIZE2:      case sSIZE3:      case sSIZE4:	msglen >>= 8;	msglen |= (c << 24);        state++;        break;      case sTOKEN:        if (c == TOKEN) {	  state = sDATA;	  if (msglen > MAX_MESSAGE) {	    fprintf(stderr,		    "%s: jtagmkII_recv(): msglen %lu exceeds max message "		    "size %u, ignoring message\n",		    progname, msglen, MAX_MESSAGE);	    ignorpkt++;	  } else if ((buf = malloc(msglen + 10)) == NULL) {	    fprintf(stderr, "%s: jtagmkII_recv(): out of memory\n",		    progname);	    ignorpkt++;	  } else {	    memcpy(buf, header, 8);	  }	} else {	  state = sSTART;	  headeridx = 0;	}        break;      case sDATA:	/* The entire payload has been read above. */	l = msglen + 8;        state = sCSUM1;        break;      case sCSUM1:      case sCSUM2:	buf[l++] = c;	if (state == sCSUM2) {	  if (crcverify(buf, msglen + 10)) {	    if (verbose >= 3)	      fprintf(stderr, "%s: jtagmkII_recv(): CRC OK",		      progname);	    state = sDONE;	  } else {	    fprintf(stderr, "%s: jtagmkII_recv(): checksum error\n",		    progname);	    free(buf);	    return -4;	  }	} else	  state++;        break;      default:        fprintf(stderr, "%s: jtagmkII_recv(): unknown state\n",                progname);	free(buf);        return -5;     }     gettimeofday(&tv, NULL);     tnow = tv.tv_sec;     if (tnow - tstart > timeoutval) {       fprintf(stderr, "%s: jtagmkII_recv_frame(): timeout\n",               progname);       return -1;     }  }  if (verbose >= 3)fprintf(stderr, "\n");  *seqno = r_seqno;  *msg = buf;  return msglen;}