From c3a798ea24eeb8f5f9765d4c2981be1c96dd91f1 Mon Sep 17 00:00:00 2001
From: Joerg Wunsch <j@uriah.heep.sax.de>
Date: Mon, 18 Jan 2010 11:05:36 +0000
Subject: [PATCH] bug #28660: Problem with loading intel hex rom files that
 exceed 0x10000 bytes * fileio.c: Revert the changes from r851 and r880,
 respectively.

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk@928 81a1dc3b-b13d-400b-aceb-764788c761c2
---
 avrdude/ChangeLog |  6 ++++++
 avrdude/fileio.c  | 30 ++++++++++++++----------------
 2 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/avrdude/ChangeLog b/avrdude/ChangeLog
index a94bb85e..fdc0d3a2 100644
--- a/avrdude/ChangeLog
+++ b/avrdude/ChangeLog
@@ -1,3 +1,9 @@
+2010-01-18  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
+
+	bug #28660: Problem with loading intel hex rom files that exceed
+	0x10000 bytes
+	* fileio.c: Revert the changes from r851 and r880, respectively.
+
 2010-01-15  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
 
 	Submitted by Michael biebl:
diff --git a/avrdude/fileio.c b/avrdude/fileio.c
index 421de457..11905fe3 100644
--- a/avrdude/fileio.c
+++ b/avrdude/fileio.c
@@ -261,24 +261,23 @@ static int ihex_readrec(struct ihexrec * ihex, char * rec)
  * If an error occurs, return -1.
  *
  * */
+
 static int ihex2b(char * infile, FILE * inf,
              unsigned char * outbuf, int bufsize)
 {
   char buffer [ MAX_LINE_LEN ];
   unsigned char * buf;
-  unsigned int nextaddr, baseaddr, maxaddr, offsetaddr;
+  unsigned int nextaddr, baseaddr, maxaddr;
   int i;
   int lineno;
   int len;
   struct ihexrec ihex;
   int rc;
 
-  lineno      = 0;
-  buf         = outbuf;
-  baseaddr    = 0;
-  maxaddr     = 0;
-  offsetaddr  = 0;
-  nextaddr    = 0;
+  lineno   = 0;
+  buf      = outbuf;
+  baseaddr = 0;
+  maxaddr  = 0;
 
   while (fgets((char *)buffer,MAX_LINE_LEN,inf)!=NULL) {
     lineno++;
@@ -302,28 +301,28 @@ static int ihex2b(char * infile, FILE * inf,
     }
 
     switch (ihex.rectyp) {
+
       case 0: /* data record */
         nextaddr = ihex.loadofs + baseaddr;
-        if ((nextaddr + ihex.reclen) > (bufsize+offsetaddr)) {
+        if (nextaddr + ihex.reclen > bufsize) {
           fprintf(stderr, 
                   "%s: ERROR: address 0x%04x out of range at line %d of %s\n",
                   progname, nextaddr+ihex.reclen, lineno, infile);
           return -1;
         }
         for (i=0; i<ihex.reclen; i++) {
-          buf[nextaddr+i-offsetaddr] = ihex.data[i];
+          buf[nextaddr+i] = ihex.data[i];
         }
         if (nextaddr+ihex.reclen > maxaddr)
           maxaddr = nextaddr+ihex.reclen;
         break;
 
       case 1: /* end of file record */
-        return maxaddr-offsetaddr;
+        return maxaddr;
         break;
 
       case 2: /* extended segment address record */
-        // note: shift values use to be 8 and 4 - the first had to be wrong
-        baseaddr = (ihex.data[0] << 12 | ihex.data[1]) << 4;
+        baseaddr = (ihex.data[0] << 8 | ihex.data[1]) << 4;
         break;
 
       case 3: /* start segment address record */
@@ -331,8 +330,7 @@ static int ihex2b(char * infile, FILE * inf,
         break;
 
       case 4: /* extended linear address record */
-        baseaddr = (ihex.data[0] << 24 | ihex.data[1]) << 16;
-        if(nextaddr == 0) offsetaddr = baseaddr;	// if provided before any data, then remember it
+        baseaddr = (ihex.data[0] << 8 | ihex.data[1]) << 16;
         break;
 
       case 5: /* start linear address record */
@@ -355,9 +353,10 @@ static int ihex2b(char * infile, FILE * inf,
           "file \"%s\"\n",
           progname, infile);
 
-  return maxaddr-offsetaddr;
+  return maxaddr;
 }
 
+
 static int b2srec(unsigned char * inbuf, int bufsize, 
            int recsize, int startaddr,
            char * outfile, FILE * outf)
@@ -1154,7 +1153,6 @@ int fileio(int op, char * filename, FILEFMT format,
   if (format != FMT_IMM && !using_stdio) {
     fclose(f);
   }
-
   return rc;
 }