]> git.sur5r.net Git - cc65/blobdiff - src/od65/fileio.c
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / src / od65 / fileio.c
index c8ccc773dc623f188ba86fe03dd567c041cf8303..9ddab17af5a498b9700ac0231a38a56c8ea8b445 100644 (file)
@@ -1,15 +1,15 @@
 /*****************************************************************************/
 /*                                                                           */
-/*                                fileio.c                                  */
+/*                                 fileio.c                                  */
 /*                                                                           */
-/*             File I/O for the od65 object file dump utility               */
+/*              File I/O for the od65 object file dump utility               */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998-2000 Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
-/* EMail:        uz@musoftware.de                                            */
+/* (C) 1998-2011, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 
 
 /*****************************************************************************/
-/*                                          Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
 
-void FileSeek (FILE* F, unsigned long Pos)
+void FileSetPos (FILE* F, unsigned long Pos)
 /* Seek to the given absolute position, fail on errors */
-{
+{                 
     if (fseek (F, Pos, SEEK_SET) != 0) {
-       Error ("Cannot seek: %s", strerror (errno));
+        Error ("Cannot seek: %s", strerror (errno));
+    }
+}
+
+
+
+unsigned long FileGetPos (FILE* F)
+/* Return the current file position, fail on errors */
+{
+    long Pos = ftell (F);
+    if (Pos < 0) {
+        Error ("Error in ftell: %s", strerror (errno));
     }
+    return Pos;
 }
 
 
@@ -66,8 +78,8 @@ unsigned Read8 (FILE* F)
 {
     int C = getc (F);
     if (C == EOF) {
-       Error ("Read error (file corrupt?)");
-    }            
+        Error ("Read error (file corrupt?)");
+    }
     return C;
 }
 
@@ -111,8 +123,8 @@ long Read32Signed (FILE* F)
 
     /* Sign extend the value */
     if (V & 0x80000000UL) {
-       /* Signed value */
-       V |= ~0xFFFFFFFFUL;
+        /* Signed value */
+        V |= ~0xFFFFFFFFUL;
     }
 
     /* Return it as a long */
@@ -121,27 +133,35 @@ long Read32Signed (FILE* F)
 
 
 
-char* ReadStr (FILE* F, char* Str)
-/* Read a string from the file. Str must hold 256 chars at max */
+unsigned long ReadVar (FILE* F)
+/* Read a variable size value from the file */
 {
-    /* Read the length byte */
-    unsigned Len = Read8 (F);
-
-    /* Read the string itself */
-    ReadData (F, Str, Len);
-
-    /* Terminate the string and return it */
-    Str [Len] = '\0';
-    return Str;
+    /* The value was written to the file in 7 bit chunks LSB first. If there
+     * are more bytes, bit 8 is set, otherwise it is clear.
+     */
+    unsigned char C;
+    unsigned long V = 0;
+    unsigned Shift = 0;
+    do {
+        /* Read one byte */
+        C = Read8 (F);
+        /* Encode it into the target value */
+        V |= ((unsigned long)(C & 0x7F)) << Shift;
+        /* Next value */
+        Shift += 7;
+    } while (C & 0x80);
+
+    /* Return the value read */
+    return V;
 }
 
 
 
-char* ReadMallocedStr (FILE* F)
+char* ReadStr (FILE* F)
 /* Read a string from the file into a malloced area */
 {
-    /* Read the length byte */
-    unsigned Len = Read8 (F);
+    /* Read the length */
+    unsigned Len = ReadVar (F);
 
     /* Allocate memory */
     char* Str = xmalloc (Len + 1);
@@ -159,10 +179,10 @@ char* ReadMallocedStr (FILE* F)
 FilePos* ReadFilePos (FILE* F, FilePos* Pos)
 /* Read a file position from the file */
 {
-    /* The line number is encoded as 24 bit value to save some space */
-    Pos->Line =        Read24 (F);
-    Pos->Col  = Read8 (F);
-    Pos->Name = Read8 (F);
+    /* Read the data fields */
+    Pos->Line = ReadVar (F);
+    Pos->Col  = ReadVar (F);
+    Pos->Name = ReadVar (F);
     return Pos;
 }
 
@@ -171,8 +191,11 @@ FilePos* ReadFilePos (FILE* F, FilePos* Pos)
 void* ReadData (FILE* F, void* Data, unsigned Size)
 /* Read data from the file */
 {
-    if (fread (Data, 1, Size, F) != Size) {
-       Error ("Read error (file corrupt?)");
+    /* Accept zero sized reads */
+    if (Size > 0) {
+        if (fread (Data, 1, Size, F) != Size) {
+            Error ("Read error (file corrupt?)");
+        }
     }
     return Data;
 }
@@ -183,21 +206,45 @@ void ReadObjHeader (FILE* F, ObjHeader* H)
 /* Read an object file header from the file */
 {
     /* Read all fields */
-    H->Magic       = Read32 (F);
-    H->Version     = Read16 (F);
-    H->Flags       = Read16 (F);
+    H->Magic        = Read32 (F);
+    H->Version      = Read16 (F);
+    H->Flags        = Read16 (F);
     H->OptionOffs   = Read32 (F);
     H->OptionSize   = Read32 (F);
-    H->FileOffs            = Read32 (F);
-    H->FileSize            = Read32 (F);
-    H->SegOffs     = Read32 (F);
-    H->SegSize     = Read32 (F);
+    H->FileOffs     = Read32 (F);
+    H->FileSize     = Read32 (F);
+    H->SegOffs      = Read32 (F);
+    H->SegSize      = Read32 (F);
     H->ImportOffs   = Read32 (F);
     H->ImportSize   = Read32 (F);
     H->ExportOffs   = Read32 (F);
     H->ExportSize   = Read32 (F);
     H->DbgSymOffs   = Read32 (F);
     H->DbgSymSize   = Read32 (F);
+    H->LineInfoOffs = Read32 (F);
+    H->LineInfoSize = Read32 (F);
+    H->StrPoolOffs  = Read32 (F);
+    H->StrPoolSize  = Read32 (F);
+    H->AssertOffs   = Read32 (F);
+    H->AssertSize   = Read32 (F);
+    H->ScopeOffs    = Read32 (F);
+    H->ScopeSize    = Read32 (F);
+    H->SpanOffs     = Read32 (F);
+    H->SpanSize     = Read32 (F);
+}
+
+
+
+void ReadStrPool (FILE* F, Collection* C)
+/* Read a string pool from the current position into C. */
+{
+    /* The number of strings is the first item */
+    unsigned long Count = ReadVar (F);
+
+    /* Read all the strings into C */
+    while (Count--) {
+        CollAppend (C, ReadStr (F));
+    }
 }