Hacked const and volatile modifiers a bit
[fw/sdcc] / packihx / packihx.c
index 8675f19038ca5a529bc4a762732cc6fcc7d58cd6..a9cf4e7a48785ed808036485f68faf3dc81f66a2 100644 (file)
@@ -1,7 +1,7 @@
 /*-----------------------------------------------------------------------
  * packihx.c:
  *
- * utility to pack an Inter HEX format file by removing redundant 
+ * utility to pack an Intel HEX format file by removing redundant 
  * extended offset records and accumulating data records up to
  * OUTPUT_CHUNK (currently 16) bytes.
  *
 #include <ctype.h>
 #include <assert.h>
 
+#if defined(_MSC_VER) || defined(__BORLANDC__)
+
+typedef unsigned char Uint8 ;
+typedef unsigned Uint16 ;
+
+#else
+
 #include "config.h"
+#endif
 
 /* A cooked line of input. */
 typedef struct _Line
 {
     Uint8              len;            /* length of data portion of record. */
-    Uint16             offset;
+    Uint16      offset;
     Uint8              type;
     Uint8              *data;          
     Uint8              checksum;
@@ -98,7 +106,7 @@ Line *readLine(FILE *inFile)
        {
            return NULL;
        }
-       ++lineno;
+        ++lineno;
     
        if (!buffer[0] || buffer[0] == '\r' || buffer[0] == '\n')
        {
@@ -144,8 +152,10 @@ Line *readLine(FILE *inFile)
        return NULL;
     }
     bp += 2;   /* Two digits consumed. */    
-    
-    line->data = (Uint8 *)malloc(line->len);
+   
+    /* Hack - always allocate something, even if len is zero.
+     * Avoids special case for len == 0. */
+    line->data = (Uint8 *)malloc(line->len ? line->len : 1);
     if (!line->data)
     {
        free(line);
@@ -153,7 +163,7 @@ Line *readLine(FILE *inFile)
         return NULL;
     }
     
-    for (i = 0; i < line->len; i++)
+    for (i = 0; i < (unsigned)line->len; i++)
     {
         if (getHexByte(bp, &(line->data[i])))
         {
@@ -175,7 +185,7 @@ Line *readLine(FILE *inFile)
        free(line);
        return NULL;
     }
-    bp += 2;   /* Two digits consumed. */   
+    /* bp += 2; */    /* Two digits consumed. */
         
     return line;
 }
@@ -184,7 +194,7 @@ Line *readLine(FILE *inFile)
 Uint16 lineChecksum(unsigned len, unsigned offset, unsigned type, 
                    const Uint8 *data)
 {
-    Uint16     checksum = 0;
+    Uint16  checksum;
     unsigned   i;
      
     checksum = len + type + (offset >> 8) + 
@@ -213,7 +223,7 @@ int validateChecksum(Line *line)
     if (checksum != line->checksum)
     {
         fprintf(stderr, "packihx: invalid checksum %X (want %X) @ line %d\n", 
-               (unsigned)checksum, (unsigned)(line->checksum),
+               (unsigned)(line->checksum), (unsigned)checksum, 
                lineno);
         return -1;     
     }
@@ -300,7 +310,7 @@ int flushPendingData(void)
 int writeLine(Line *line)
 {
     static Uint16 lastExtendedOffset = 0;
-    int                  rc = 0;
+    int       rc;
     
     if (line->type)
     {
@@ -341,7 +351,7 @@ int writeLine(Line *line)
     }
     else
     {
-        if (pendingOffset + pendingLen != line->offset)
+        if (pendingOffset + pendingLen != (unsigned)line->offset)
         {
             /* This line is not contigous with the last one. Dump pending. */
             if (flushPendingData())