rem: buf = inbuf; nextaddr = 0; stopaddr = 0; startf = 0; stopf = 0;   for (i=startaddr; i<bufsize; i++) { if (buf[i] == 0xff) { if (startf == 0) continue; else if (stopf == 0) { stopf = 1; stopaddr = i; } } else { if (startf == 0) { startf = 1; nextaddr = i; while (nextaddr % recsize != 0) nextaddr --; } else if (stopf == 1) { stopf = 0; stopaddr = bufsize; } } } nbytes = i;  bufsize = stopaddr - nextaddr;
context: int b2srec(unsigned char * inbuf, int bufsize,            int recsize, int startaddr,           char * outfile, FILE * outf){  unsigned char * buf;  unsigned int nextaddr, stopaddr;  int n, nbytes, addr_width;  int i;  unsigned char cksum;  unsigned char startf, stopf ,emptyf;  char * tmpl=0;  if (recsize > 255) {    fprintf(stderr, "%s: ERROR: recsize=%d, must be < 256\n",            progname, recsize);    return -1;  }  buf = inbuf;  nextaddr = 0;      stopaddr = 0;  startf = 0;  stopf = 0;  /* search for ranges of 'real' data */       for (i=startaddr; i<bufsize; i++) {    if (buf[i] == 0xff) {      if (startf == 0)         continue;      else if (stopf == 0) {        stopf = 1;        stopaddr = i;      }    }    else {      if (startf == 0) {        startf = 1;        nextaddr = i;        while (nextaddr % recsize != 0)          nextaddr --;      }      else if (stopf == 1) {        stopf = 0;        stopaddr = bufsize;      }    }  }  nbytes = i;   bufsize = stopaddr - nextaddr;  addr_width = 0;  while (bufsize) {    n = recsize;    if (n > bufsize)       n = bufsize;    if (n) {      cksum = 0;      if (nextaddr + n <= 0xffff) {        addr_width = 2;        tmpl="S1%02X%04X";      }      else if (nextaddr + n <= 0xffffff) {        addr_width = 3;        tmpl="S2%02X%06X";      }      else if (nextaddr + n <= 0xffffffff) {        addr_width = 4;        tmpl="S3%02X%08X";      }      else {        fprintf(stderr, "%s: ERROR: address=%d, out of range\n",                progname, nextaddr);        return -1;      }      /* skip the lines filled with 0xff */       emptyf = 1;      for (i=nextaddr; i<nextaddr + n; i++) {        if (buf[i] != 0xff) {          emptyf=0;          break;        }      }      if (emptyf != 1) {	        fprintf(outf, tmpl, n + addr_width + 1, nextaddr);		        cksum += n + addr_width + 1;        for (i=addr_width; i>0; i--)           cksum += (nextaddr >> (i-1) * 8) & 0xff;        for (i=nextaddr; i<nextaddr + n; i++) {          fprintf(outf, "%02X", buf[i]);          cksum += buf[i];        }        cksum = 0xff - cksum;        fprintf(outf, "%02X\n", cksum);      }      nextaddr += n;    }    /* advance to next 'recsize' bytes */    bufsize -= n;  }  /*-----------------------------------------------------------------    add the end of record data line    -----------------------------------------------------------------*/  cksum = 0;  n = 0;  nextaddr = 0;  if (startaddr <= 0xffff) {    addr_width = 2;    tmpl="S9%02X%04X";  }  else if (startaddr <= 0xffffff) {    addr_width = 3;    tmpl="S9%02X%06X";  }  else if (startaddr <= 0xffffffff) {    addr_width = 4;    tmpl="S9%02X%08X";  }  fprintf(outf, tmpl, n + addr_width + 1, nextaddr);  cksum += n + addr_width +1;  for (i=addr_width; i>0; i--)     cksum += (nextaddr >> (i - 1) * 8) & 0xff;  cksum = 0xff - cksum;  fprintf(outf, "%02X\n", cksum);  return nbytes; }