/* bintohex: convert binary to Srecord * Didier Mequignon 2005 May, e-mail: aniplay@wanadoo.fr * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include static int checksum(long addr, unsigned char *buf, long len, int size) { long i,sum; sum = 0xff - 1 - size - (len & 0xff); if(size ==4) sum -= (addr >>24) & 0xff; if(size >=3) sum -= (addr >>16) & 0xff; sum -= (addr >> 8) & 0xff; sum -= addr & 0xff; for(i=0;i0) { if(cc>size_source) cc=size_source; if(cc>0) { if((ret=put32(cc,addr,source,byte_addr,mask,handle))<0) { Fclose(handle); return(ret); } addr += cc; source += cc; size_source -= cc; } } sprintf(buf,"S%d%02x",11-byte_addr,2+1); strupr(buf); if((ret=(int)Fwrite(handle,(long)strlen(buf),(void *)buf))<0) { Fclose(handle); return(ret); } switch(byte_addr) { case 2: sprintf(buf,"%04lx",base & mask); break; case 3: sprintf(buf,"%06lx",base & mask); break; case 4: sprintf(buf,"%08lx",base & mask); break; } strupr(buf); if((ret=(int)Fwrite(handle,(long)strlen(buf),(void *)buf))<0) { Fclose(handle); return(ret); } /* kludge -> don't know why you have to add the +1 = works for byte_addr =3 at least */ sprintf(buf,"%02x\n",checksum(base,(char *)0,0,byte_addr)+1); strupr(buf); ret=(int)Fwrite(handle,(long)strlen(buf),(void *)buf); Fclose(handle); return(ret); }