From 097a01094eedd3b030c2c7a26ddc2ac5067ead28 Mon Sep 17 00:00:00 2001 From: cuz Date: Wed, 2 Aug 2000 13:23:06 +0000 Subject: [PATCH] Added a method to write variable sized unsigned values. Use this method for all sorts of things in the object files. This does not only make the object files smaller, but does also remove several limits (strings may be longer than 255 bytes, several counters no longer have 8 or 16 bit limits). git-svn-id: svn://svn.cc65.org/cc65/trunk@260 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/ar65/exports.c | 20 +++++----- src/ar65/fileio.c | 65 ++++++++++++++++++++++++++------ src/ar65/fileio.h | 14 +++++-- src/ar65/library.c | 72 +++++++++++++++++++++++++---------- src/ar65/objdata.c | 12 +++--- src/ca65/dbginfo.c | 16 ++++++++ src/ca65/dbginfo.h | 10 ++++- src/ca65/filetab.c | 2 +- src/ca65/objcode.c | 21 +++-------- src/ca65/objfile.c | 51 ++++++++++++++++--------- src/ca65/objfile.h | 14 ++++--- src/ca65/options.c | 16 ++++---- src/ca65/pseudo.c | 5 ++- src/ca65/symtab.c | 23 +++++------- src/common/filepos.h | 7 +--- src/common/segdefs.h | 12 ++---- src/ld65/dbgsyms.c | 29 ++++++--------- src/ld65/dbgsyms.h | 11 ++++-- src/ld65/exports.c | 30 +++++++-------- src/ld65/exports.h | 18 +++++---- src/ld65/fileio.c | 89 ++++++++++++++++++++++++++++---------------- src/ld65/fileio.h | 12 ++++-- src/ld65/library.c | 30 ++++++++------- src/ld65/objdata.h | 4 +- src/ld65/objfile.c | 15 ++++---- src/ld65/segments.c | 27 +++++--------- src/od65/dump.c | 45 +++++++++------------- src/od65/fileio.c | 46 +++++++++++++---------- src/od65/fileio.h | 6 +-- 29 files changed, 425 insertions(+), 297 deletions(-) diff --git a/src/ar65/exports.c b/src/ar65/exports.c index 01d8f0ded..34d34227f 100644 --- a/src/ar65/exports.c +++ b/src/ar65/exports.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -35,9 +35,11 @@ #include -#include "../common/hashstr.h" -#include "../common/xmalloc.h" - +/* common */ +#include "hashstr.h" +#include "xmalloc.h" + +/* ar65 */ #include "error.h" #include "objdata.h" #include "exports.h" @@ -112,7 +114,7 @@ void ExpInsert (const char* Name, unsigned Module) /* Duplicate entry */ Warning ("External symbol `%s' in module `%s' is duplicated in " "module `%s'", - Name, GetObjName (L->Module), GetObjName (Module)); + Name, GetObjName (L->Module), GetObjName (Module)); } if (L->Next == 0) { break; @@ -147,4 +149,4 @@ int ExpFind (const char* Name) - + diff --git a/src/ar65/fileio.c b/src/ar65/fileio.c index 9cf095bb7..730fc6332 100644 --- a/src/ar65/fileio.c +++ b/src/ar65/fileio.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -35,8 +35,10 @@ #include -#include "../common/xmalloc.h" +/* common */ +#include "xmalloc.h" +/* ar65 */ #include "error.h" #include "fileio.h" @@ -78,14 +80,31 @@ void Write32 (FILE* F, unsigned long Val) +void WriteVar (FILE* F, unsigned long V) +/* Write a variable sized value to the file in special encoding */ +{ + /* We will write the value to the file in 7 bit chunks. If the 8th bit + * is clear, we're done, if it is set, another chunk follows. This will + * allow us to encode smaller values with less bytes, at the expense of + * needing 5 bytes if a 32 bit value is written to file. + */ + do { + unsigned char C = (V & 0x7F); + V >>= 7; + if (V) { + C |= 0x80; + } + Write8 (F, C); + } while (V != 0); +} + + + void WriteStr (FILE* F, const char* S) /* Write a string to the file */ { unsigned Len = strlen (S); - if (Len > 255) { - Internal ("String too long"); - } - Write8 (F, (unsigned char) Len); + WriteVar (F, Len); WriteData (F, S, Len); } @@ -133,11 +152,35 @@ unsigned long Read32 (FILE* F) +unsigned long ReadVar (FILE* F) +/* Read a variable size value from the file */ +{ + /* 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* ReadStr (FILE* F) /* Read a string from the file (the memory will be malloc'ed) */ { - /* Read the length byte */ - unsigned Len = Read8 (F); + /* Read the length */ + unsigned Len = ReadVar (F); /* Allocate memory and read the string itself */ char* S = xmalloc (Len + 1); diff --git a/src/ar65/fileio.h b/src/ar65/fileio.h index e38c46082..3a636e987 100644 --- a/src/ar65/fileio.h +++ b/src/ar65/fileio.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -57,6 +57,9 @@ void Write16 (FILE* F, unsigned Val); void Write32 (FILE* F, unsigned long Val); /* Write a 32 bit value to the file */ +void WriteVar (FILE* F, unsigned long V); +/* Write a variable sized value to the file in special encoding */ + void WriteStr (FILE* F, const char* S); /* Write a string to the file */ @@ -72,6 +75,9 @@ unsigned Read16 (FILE* F); unsigned long Read32 (FILE* F); /* Read a 32 bit value from the file */ +unsigned long ReadVar (FILE* F); +/* Read a variable size value from the file */ + char* ReadStr (FILE* F); /* Read a string from the file (the memory will be malloc'ed) */ diff --git a/src/ar65/library.c b/src/ar65/library.c index 4906f57d5..5c1ba063c 100644 --- a/src/ar65/library.c +++ b/src/ar65/library.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -37,13 +37,15 @@ #include #include -#include "../common/bitops.h" -#include "../common/exprdefs.h" -#include "../common/filepos.h" -#include "../common/libdefs.h" -#include "../common/symdefs.h" -#include "../common/xmalloc.h" +/* common */ +#include "bitops.h" +#include "exprdefs.h" +#include "filepos.h" +#include "libdefs.h" +#include "symdefs.h" +#include "xmalloc.h" +/* ar65 */ #include "error.h" #include "global.h" #include "fileio.h" @@ -303,6 +305,28 @@ void LibCopyFrom (unsigned long Pos, unsigned long Bytes, FILE* F) +static unsigned long GetVar (unsigned char** Buf) +/* Get a variable sized value from Buf */ +{ + unsigned char C; + unsigned long V = 0; + unsigned Shift = 0; + do { + /* Read one byte */ + C = **Buf; + ++(*Buf); + /* Add this char to the value */ + V |= ((unsigned long)(C & 0x7F)) << Shift; + /* Next value */ + Shift += 7; + } while (C & 0x80); + + /* Return the result */ + return V; +} + + + static void SkipExpr (unsigned char** Buf) /* Skip an expression in Buf */ { @@ -339,20 +363,26 @@ static void SkipExpr (unsigned char** Buf) +static void SkipFilePos (unsigned char** Buf) +/* Skip a file position in Buf */ +{ + (void) GetVar (Buf); /* Line */ + (void) GetVar (Buf); /* Col */ + (void) GetVar (Buf); /* Name */ +} + + + static void LibCheckExports (ObjData* O) /* Insert all exports from the given object file into the global list * checking for duplicates. */ { - char Name [256]; - /* Get a pointer to the buffer */ unsigned char* Exports = O->Exports; - /* First two bytes are export count */ - unsigned Lo = *Exports++; - unsigned Hi = *Exports++; - unsigned Count = (Hi << 8) + Lo; + /* Get the export count */ + unsigned Count = GetVar (&Exports); /* Read the exports */ if (Verbose > 1) { @@ -360,13 +390,12 @@ static void LibCheckExports (ObjData* O) } while (Count--) { - unsigned char Len; - /* Get the export tag */ unsigned char Tag = *Exports++; /* Next thing is name of symbol */ - Len = *Exports++; + unsigned Len = GetVar (&Exports); + char* Name = xmalloc (Len + 1); memcpy (Name, Exports, Len); Name [Len] = '\0'; Exports += Len; @@ -381,13 +410,16 @@ static void LibCheckExports (ObjData* O) } /* Skip the position */ - Exports += POS_SIZE; + SkipFilePos (&Exports); /* Insert the name into the hash table */ if (Verbose > 1) { printf (" %s\n", Name); } ExpInsert (Name, O->Index); + + /* Free the name */ + xfree (Name); } } diff --git a/src/ar65/objdata.c b/src/ar65/objdata.c index 5c979e8f5..6b42f33a8 100644 --- a/src/ar65/objdata.c +++ b/src/ar65/objdata.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -38,7 +38,7 @@ /* common */ #include "check.h" #include "xmalloc.h" - + /* ar65 */ #include "error.h" #include "objdata.h" @@ -189,7 +189,7 @@ void MakeObjPool (void) /* Set the pool pointer */ ObjPool [Index] = O; - + /* Next object */ ++Index; O = O->Next; diff --git a/src/ca65/dbginfo.c b/src/ca65/dbginfo.c index ef1cd9752..33362aca3 100644 --- a/src/ca65/dbginfo.c +++ b/src/ca65/dbginfo.c @@ -81,3 +81,19 @@ void DbgInfoFile (void) +void DbgInfoLine (void) +/* Parse and handle LINE subcommand of the .dbg pseudo instruction */ +{ + ErrorSkip (ERR_NOT_IMPLEMENTED); +} + + + +void DbgInfoSym (void) +/* Parse and handle SYM subcommand of the .dbg pseudo instruction */ +{ + ErrorSkip (ERR_NOT_IMPLEMENTED); +} + + + diff --git a/src/ca65/dbginfo.h b/src/ca65/dbginfo.h index 7d236e1c3..77e21cdfd 100644 --- a/src/ca65/dbginfo.h +++ b/src/ca65/dbginfo.h @@ -45,7 +45,13 @@ void DbgInfoFile (void); -/* Parse and handle the .REPEAT statement */ +/* Parse and handle FILE subcommand of the .dbg pseudo instruction */ + +void DbgInfoLine (void); +/* Parse and handle LINE subcommand of the .dbg pseudo instruction */ + +void DbgInfoSym (void); +/* Parse and handle SYM subcommand of the .dbg pseudo instruction */ @@ -56,4 +62,4 @@ void DbgInfoFile (void); - + diff --git a/src/ca65/filetab.c b/src/ca65/filetab.c index c65c833ce..5b31fff3d 100644 --- a/src/ca65/filetab.c +++ b/src/ca65/filetab.c @@ -119,7 +119,7 @@ void WriteFiles (void) ObjStartFiles (); /* Write the file count */ - ObjWrite16 (FileCount); + ObjWriteVar (FileCount); /* Write the file data */ for (I = 0; I < FileCount; ++I) { diff --git a/src/ca65/objcode.c b/src/ca65/objcode.c index 5ad68f41e..b1ae6f841 100644 --- a/src/ca65/objcode.c +++ b/src/ca65/objcode.c @@ -486,19 +486,8 @@ static void WriteOneSeg (Segment* Seg) Size += F->Len; F = F->Next; } - if (Size < 0x100) { - ObjWrite8 (FRAG_LITERAL8); - ObjWrite8 (Size); - } else if (Size < 0x10000) { - ObjWrite8 (FRAG_LITERAL16); - ObjWrite16 (Size); - } else if (Size < 0x1000000) { - ObjWrite8 (FRAG_LITERAL24); - ObjWrite24 (Size); - } else { - ObjWrite8 (FRAG_LITERAL32); - ObjWrite32 (Size); - } + ObjWrite8 (FRAG_LITERAL); + ObjWriteVar (Size); /* Now write the literal data */ F = Frag; @@ -533,7 +522,7 @@ static void WriteOneSeg (Segment* Seg) case FRAG_FILL: ObjWrite8 (FRAG_FILL); - ObjWrite16 (Frag->Len); + ObjWriteVar (Frag->Len); break; default: @@ -560,7 +549,7 @@ void WriteSegments (void) ObjStartSegments (); /* First thing is segment count */ - ObjWrite8 (SegmentCount); + ObjWriteVar (SegmentCount); /* Now walk through all segments and write them to the object file */ Seg = SegmentList; @@ -578,7 +567,7 @@ void WriteSegments (void) /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ diff --git a/src/ca65/objfile.c b/src/ca65/objfile.c index 5308580dd..30f6c2c87 100644 --- a/src/ca65/objfile.c +++ b/src/ca65/objfile.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -41,7 +41,7 @@ /* common */ #include "fname.h" #include "objdefs.h" - + /* ca65 */ #include "global.h" #include "error.h" @@ -210,19 +210,36 @@ void ObjWrite32 (unsigned long V) +void ObjWriteVar (unsigned long V) +/* Write a variable sized value to the file in special encoding */ +{ + /* We will write the value to the file in 7 bit chunks. If the 8th bit + * is clear, we're done, if it is set, another chunk follows. This will + * allow us to encode smaller values with less bytes, at the expense of + * needing 5 bytes if a 32 bit value is written to file. + */ + do { + unsigned char C = (V & 0x7F); + V >>= 7; + if (V) { + C |= 0x80; + } + ObjWrite8 (C); + } while (V != 0); +} + + + void ObjWriteStr (const char* S) /* Write a string to the object file */ { unsigned Len = strlen (S); - if (Len > 255) { - Internal ("String too long in ObjWriteStr"); - } - /* Write the string with a length byte preceeded (this is easier for + /* Write the string with the length preceeded (this is easier for * the reading routine than the C format since the length is known in * advance). */ - ObjWrite8 ((unsigned char) Len); + ObjWriteVar (Len); ObjWriteData (S, Len); } @@ -241,15 +258,15 @@ void ObjWriteData (const void* Data, unsigned Size) void ObjWritePos (const FilePos* Pos) /* Write a file position to the object file */ { - /* Write the line number as 24 bit value to save one byte */ - ObjWrite24 (Pos->Line); - ObjWrite8 (Pos->Col); + /* Write the data entries */ + ObjWriteVar (Pos->Line); + ObjWriteVar (Pos->Col); if (Pos->Name == 0) { - /* Position is outside file scope, use the main file instead */ - ObjWrite8 (0); + /* Position is outside file scope, use the main file instead */ + ObjWriteVar (0); } else { - ObjWrite8 (Pos->Name - 1); - } + ObjWriteVar (Pos->Name - 1); + } } diff --git a/src/ca65/objfile.h b/src/ca65/objfile.h index 364cab97f..cf726e81d 100644 --- a/src/ca65/objfile.h +++ b/src/ca65/objfile.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -38,7 +38,8 @@ -#include "../common/filepos.h" +/* common */ +#include "filepos.h" @@ -65,6 +66,9 @@ void ObjWrite24 (unsigned long V); void ObjWrite32 (unsigned long V); /* Write a 32 bit value to the file */ + +void ObjWriteVar (unsigned long V); +/* Write a variable sized value to the file in special encoding */ void ObjWriteStr (const char* S); /* Write a string to the object file */ diff --git a/src/ca65/options.c b/src/ca65/options.c index 0af381818..ed1bd2405 100644 --- a/src/ca65/options.c +++ b/src/ca65/options.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -33,9 +33,11 @@ -#include "../common/optdefs.h" -#include "../common/xmalloc.h" +/* common */ +#include "optdefs.h" +#include "xmalloc.h" +/* ca65 */ #include "error.h" #include "objfile.h" #include "options.h" @@ -164,7 +166,7 @@ void WriteOptions (void) ObjStartOptions (); /* Write the option count */ - ObjWrite16 (OptCount); + ObjWriteVar (OptCount); /* Walk through the list and write the options */ O = OptRoot; diff --git a/src/ca65/pseudo.c b/src/ca65/pseudo.c index 94a5c3e4c..d5e76901f 100644 --- a/src/ca65/pseudo.c +++ b/src/ca65/pseudo.c @@ -370,14 +370,15 @@ static void DoDbg (void) /* Skip the subkey */ NextTok (); - + /* Parameters are separated by a comma */ ConsumeComma (); /* Check the key and dispatch to a handler */ switch (Key) { case 0: DbgInfoFile (); break; - case 1: + case 1: DbgInfoLine (); break; + case 2: DbgInfoSym (); break; default: ErrorSkip (ERR_SYNTAX); break; } } diff --git a/src/ca65/symtab.c b/src/ca65/symtab.c index 027f715dd..23ce5d121 100644 --- a/src/ca65/symtab.c +++ b/src/ca65/symtab.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -978,12 +978,12 @@ void WriteImports (void) /* Write the imports list to the object file */ { SymEntry* S; - + /* Tell the object file module that we're about to start the imports */ ObjStartImports (); /* Write the import count to the list */ - ObjWrite16 (ImportCount); + ObjWriteVar (ImportCount); /* Walk throught list and write all imports to the file */ S = SymList; @@ -1015,7 +1015,7 @@ void WriteExports (void) ObjStartExports (); /* Write the export count to the list */ - ObjWrite16 (ExportCount); + ObjWriteVar (ExportCount); /* Walk throught list and write all exports to the file */ S = SymList; @@ -1076,13 +1076,8 @@ void WriteDbgSyms (void) S = S->List; } - /* Safety check */ - if (Count > 0xFFFF) { - Fatal (FAT_TOO_MANY_SYMBOLS); - } - /* Write the symbol count to the list */ - ObjWrite16 (Count); + ObjWriteVar (Count); /* Walk through list and write all symbols to the file */ S = SymList; @@ -1118,7 +1113,7 @@ void WriteDbgSyms (void) } else { /* No debug symbols */ - ObjWrite16 (0); + ObjWriteVar (0); } diff --git a/src/common/filepos.h b/src/common/filepos.h index 8378fd4b8..fc938c674 100644 --- a/src/common/filepos.h +++ b/src/common/filepos.h @@ -44,15 +44,12 @@ -/* Size of position in file */ -#define POS_SIZE 5 - /* Type of a file position */ typedef struct FilePos_ FilePos; struct FilePos_ { unsigned long Line; /* Line */ - unsigned char Col; /* Column */ - unsigned char Name; /* File */ + unsigned Col; /* Column */ + unsigned Name; /* File */ }; diff --git a/src/common/segdefs.h b/src/common/segdefs.h index 5da311bcc..0a0b8c065 100644 --- a/src/common/segdefs.h +++ b/src/common/segdefs.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -55,10 +55,6 @@ #define FRAG_BYTEMASK 0x07 /* Mask for byte count */ #define FRAG_LITERAL 0x00 /* Literal data */ -#define FRAG_LITERAL8 0x01 /* Literal data with 8 bit length */ -#define FRAG_LITERAL16 0x02 /* Literal data with 16 bit length */ -#define FRAG_LITERAL24 0x03 /* Literal data with 24 bit length */ -#define FRAG_LITERAL32 0x04 /* Literal data with 32 bit length */ #define FRAG_EXPR 0x08 /* Expression */ #define FRAG_EXPR8 0x09 /* 8 bit expression */ diff --git a/src/ld65/dbgsyms.c b/src/ld65/dbgsyms.c index 911dedab9..5c649ce76 100644 --- a/src/ld65/dbgsyms.c +++ b/src/ld65/dbgsyms.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -39,7 +39,7 @@ #include "check.h" #include "symdefs.h" #include "xmalloc.h" - + /* ld65 */ #include "global.h" #include "error.h" @@ -69,14 +69,11 @@ static DbgSym* DbgSymPool [256]; -static DbgSym* NewDbgSym (unsigned char Type, const char* Name, ObjData* O) +static DbgSym* NewDbgSym (unsigned char Type, ObjData* O) /* Create a new DbgSym and return it */ { - /* Get the length of the symbol name */ - unsigned Len = strlen (Name); - /* Allocate memory */ - DbgSym* D = xmalloc (sizeof (DbgSym) + Len); + DbgSym* D = xmalloc (sizeof (DbgSym)); /* Initialize the fields */ D->Next = 0; @@ -84,8 +81,7 @@ static DbgSym* NewDbgSym (unsigned char Type, const char* Name, ObjData* O) D->Obj = O; D->Expr = 0; D->Type = Type; - memcpy (D->Name, Name, Len); - D->Name [Len] = '\0'; + D->Name = 0; /* Return the new entry */ return D; @@ -143,17 +139,16 @@ DbgSym* ReadDbgSym (FILE* F, ObjData* O) /* Read a debug symbol from a file, insert and return it */ { unsigned char Type; - char Name [256]; DbgSym* D; /* Read the type */ Type = Read8 (F); - /* Read the name */ - ReadStr (F, Name); + /* Create a new debug symbol */ + D = NewDbgSym (Type, O); - /* Create a new export */ - D = NewDbgSym (Type, Name, O); + /* Read and assign the name */ + D->Name = ReadStr (F); /* Read the value */ if (Type & EXP_EXPR) { diff --git a/src/ld65/dbgsyms.h b/src/ld65/dbgsyms.h index a8dc29d29..1c65a5501 100644 --- a/src/ld65/dbgsyms.h +++ b/src/ld65/dbgsyms.h @@ -40,9 +40,11 @@ #include -#include "../common/exprdefs.h" -#include "../common/filepos.h" - +/* common */ +#include "exprdefs.h" +#include "filepos.h" + +/* ld65 */ #include "objdata.h" @@ -62,7 +64,7 @@ struct DbgSym_ { FilePos Pos; /* File position of definition */ ExprNode* Expr; /* Expression (0 if not def'd) */ unsigned char Type; /* Type of symbol */ - char Name [1]; /* Name - dynamically allocated */ + char* Name; /* Name - dynamically allocated */ }; @@ -90,3 +92,4 @@ void PrintDbgSymLabels (ObjData* O, FILE* F); + diff --git a/src/ld65/exports.c b/src/ld65/exports.c index 93681465b..8f0427a6d 100644 --- a/src/ld65/exports.c +++ b/src/ld65/exports.c @@ -42,7 +42,7 @@ #include "hashstr.h" #include "symdefs.h" #include "xmalloc.h" - + /* ld65 */ #include "global.h" #include "error.h" @@ -175,7 +175,7 @@ Import* ReadImport (FILE* F, ObjData* Obj) I = NewImport (Type, Obj); /* Read the name */ - I->V.Name = ReadMallocedStr (F); + I->V.Name = ReadStr (F); /* Read the file position */ ReadFilePos (F, &I->Pos); @@ -195,22 +195,23 @@ Import* ReadImport (FILE* F, ObjData* Obj) static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj) /* Create a new export and initialize it */ { - /* Get the length of the symbol name */ - unsigned Len = strlen (Name); - /* Allocate memory */ - Export* E = xmalloc (sizeof (Export) + Len); + Export* E = xmalloc (sizeof (Export)); /* Initialize the fields */ E->Next = 0; - E->Flags = 0; + E->Flags = 0; E->Obj = Obj; E->ImpCount = 0; E->ImpList = 0; E->Expr = 0; E->Type = Type; - memcpy (E->Name, Name, Len); - E->Name [Len] = '\0'; + if (Name) { + E->Name = xstrdup (Name); + } else { + /* Name will get added later */ + E->Name = 0; + } /* Return the new entry */ return E; @@ -250,7 +251,7 @@ void InsertExport (Export* E) if (Last) { Last->Next = E; } else { - HashTab [HashVal] = E; + HashTab [HashVal] = E; } ImpOpen -= E->ImpCount; /* Decrease open imports now */ xfree (L); @@ -285,17 +286,16 @@ Export* ReadExport (FILE* F, ObjData* O) /* Read an export from a file */ { unsigned char Type; - char Name [256]; Export* E; /* Read the type */ Type = Read8 (F); - /* Read the name */ - ReadStr (F, Name); + /* Create a new export without a name */ + E = NewExport (Type, 0, O); - /* Create a new export */ - E = NewExport (Type, Name, O); + /* Read the name */ + E->Name = ReadStr (F); /* Read the value */ if (Type & EXP_EXPR) { diff --git a/src/ld65/exports.h b/src/ld65/exports.h index 46a9646b0..075c5ca48 100644 --- a/src/ld65/exports.h +++ b/src/ld65/exports.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -40,9 +40,11 @@ #include -#include "../common/exprdefs.h" -#include "../common/filepos.h" - +/* common */ +#include "exprdefs.h" +#include "filepos.h" + +/* ld65 */ #include "objdata.h" #include "config.h" @@ -80,7 +82,7 @@ struct Export_ { FilePos Pos; /* File position of definition */ ExprNode* Expr; /* Expression (0 if not def'd) */ unsigned char Type; /* Type of export */ - char Name [1]; /* Name - dynamically allocated */ + char* Name; /* Name - dynamically allocated */ }; diff --git a/src/ld65/fileio.c b/src/ld65/fileio.c index de80d5fac..c13a202f2 100644 --- a/src/ld65/fileio.c +++ b/src/ld65/fileio.c @@ -35,8 +35,10 @@ #include -#include "../common/xmalloc.h" +/* common */ +#include "xmalloc.h" +/* ld65 */ #include "error.h" #include "fileio.h" @@ -117,14 +119,31 @@ void WriteVal (FILE* F, unsigned long Val, unsigned Size) +void WriteVar (FILE* F, unsigned long V) +/* Write a variable sized value to the file in special encoding */ +{ + /* We will write the value to the file in 7 bit chunks. If the 8th bit + * is clear, we're done, if it is set, another chunk follows. This will + * allow us to encode smaller values with less bytes, at the expense of + * needing 5 bytes if a 32 bit value is written to file. + */ + do { + unsigned char C = (V & 0x7F); + V >>= 7; + if (V) { + C |= 0x80; + } + Write8 (F, C); + } while (V != 0); +} + + + void WriteStr (FILE* F, const char* S) /* Write a string to the file */ { unsigned Len = strlen (S); - if (Len > 255) { - Internal ("String too long"); - } - Write8 (F, (unsigned char) Len); + WriteVar (F, Len); WriteData (F, S, Len); } @@ -210,37 +229,43 @@ 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) -/* Read a string from the file into a malloced area */ +char* ReadStr (FILE* F) +/* Read a string from the file (the memory will be malloc'ed) */ { - /* Read the length byte */ - unsigned Len = Read8 (F); + /* Read the length */ + unsigned Len = ReadVar (F); - /* Allocate memory */ - char* Str = xmalloc (Len + 1); - - /* Read the string itself */ - ReadData (F, Str, Len); + /* Allocate memory and read the string itself */ + char* S = xmalloc (Len + 1); + ReadData (F, S, Len); /* Terminate the string and return it */ - Str [Len] = '\0'; - return Str; + S [Len] = '\0'; + return S; } @@ -248,10 +273,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; } @@ -268,4 +293,4 @@ void* ReadData (FILE* F, void* Data, unsigned Size) - + diff --git a/src/ld65/fileio.h b/src/ld65/fileio.h index 44b3e789f..3da7e86a3 100644 --- a/src/ld65/fileio.h +++ b/src/ld65/fileio.h @@ -40,7 +40,8 @@ #include -#include "../common/filepos.h" +/* common */ +#include "filepos.h" @@ -65,6 +66,9 @@ void Write32 (FILE* F, unsigned long Val); void WriteVal (FILE* F, unsigned long Val, unsigned Size); /* Write a value of the given size to the output file */ +void WriteVar (FILE* F, unsigned long V); +/* Write a variable sized value to the file in special encoding */ + void WriteStr (FILE* F, const char* S); /* Write a string to the file */ @@ -89,10 +93,10 @@ unsigned long Read32 (FILE* F); long Read32Signed (FILE* F); /* Read a 32 bit value from the file. Sign extend the value. */ -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 */ -char* ReadMallocedStr (FILE* F); +char* ReadStr (FILE* F); /* Read a string from the file into a malloced area */ FilePos* ReadFilePos (FILE* F, FilePos* Pos); diff --git a/src/ld65/library.c b/src/ld65/library.c index b73e8015b..4a9d8a7e7 100644 --- a/src/ld65/library.c +++ b/src/ld65/library.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2000 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@musoftware.de */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -37,13 +37,15 @@ #include #include -#include "../common/exprdefs.h" -#include "../common/filepos.h" -#include "../common/libdefs.h" -#include "../common/objdefs.h" -#include "../common/symdefs.h" -#include "../common/xmalloc.h" +/* common */ +#include "exprdefs.h" +#include "filepos.h" +#include "libdefs.h" +#include "objdefs.h" +#include "symdefs.h" +#include "xmalloc.h" +/* ld65 */ #include "error.h" #include "exports.h" #include "fileio.h" @@ -54,7 +56,7 @@ /*****************************************************************************/ -/* Data */ +/* Data */ /*****************************************************************************/ @@ -111,7 +113,7 @@ static ObjData* ReadIndexEntry (void) ObjData* O = NewObjData (); /* Module name/flags/MTime/Start/Size */ - O->Name = ReadMallocedStr (Lib); + O->Name = ReadStr (Lib); O->Flags = Read16 (Lib); Read32 (Lib); /* Skip MTime */ O->Start = Read32 (Lib); @@ -119,7 +121,7 @@ static ObjData* ReadIndexEntry (void) /* Skip the export size, then read the exports */ Read16 (Lib); - O->ExportCount = Read16 (Lib); + O->ExportCount = ReadVar (Lib); O->Exports = xmalloc (O->ExportCount * sizeof (Export*)); for (I = 0; I < O->ExportCount; ++I) { O->Exports [I] = ReadExport (Lib, O); @@ -127,7 +129,7 @@ static ObjData* ReadIndexEntry (void) /* Skip the import size, then read the imports */ Read16 (Lib); - O->ImportCount = Read16 (Lib); + O->ImportCount = ReadVar (Lib); O->Imports = xmalloc (O->ImportCount * sizeof (Import*)); for (I = 0; I < O->ImportCount; ++I) { O->Imports [I] = ReadImport (Lib, O); diff --git a/src/ld65/objdata.h b/src/ld65/objdata.h index 91d994a1d..5db0848d8 100644 --- a/src/ld65/objdata.h +++ b/src/ld65/objdata.h @@ -38,7 +38,8 @@ -#include "../common/objdefs.h" +/* common */ +#include "objdefs.h" @@ -104,3 +105,4 @@ void FreeObjData (ObjData* O); + diff --git a/src/ld65/objfile.c b/src/ld65/objfile.c index 6f38a038f..26d7132e7 100644 --- a/src/ld65/objfile.c +++ b/src/ld65/objfile.c @@ -40,7 +40,7 @@ /* common */ #include "xmalloc.h" - + /* ld65 */ #include "dbgsyms.h" #include "error.h" @@ -107,14 +107,14 @@ void ObjReadFiles (FILE* F, ObjData* O) { unsigned I; - O->FileCount = Read16 (F); + O->FileCount = ReadVar (F); O->Files = xmalloc (O->FileCount * sizeof (char*)); for (I = 0; I < O->FileCount; ++I) { /* Skip MTime and size */ Read32 (F); Read32 (F); /* Read the filename */ - O->Files [I] = ReadMallocedStr (F); + O->Files [I] = ReadStr (F); } } @@ -125,7 +125,7 @@ void ObjReadImports (FILE* F, ObjData* O) { unsigned I; - O->ImportCount = Read16 (F); + O->ImportCount = ReadVar (F); O->Imports = xmalloc (O->ImportCount * sizeof (Import*)); for (I = 0; I < O->ImportCount; ++I) { O->Imports [I] = ReadImport (F, O); @@ -140,7 +140,7 @@ void ObjReadExports (FILE* F, ObjData* O) { unsigned I; - O->ExportCount = Read16 (F); + O->ExportCount = ReadVar (F); O->Exports = xmalloc (O->ExportCount * sizeof (Export*)); for (I = 0; I < O->ExportCount; ++I) { O->Exports [I] = ReadExport (F, O); @@ -155,7 +155,7 @@ void ObjReadDbgSyms (FILE* F, ObjData* O) { unsigned I; - O->DbgSymCount = Read16 (F); + O->DbgSymCount = ReadVar (F); O->DbgSyms = xmalloc (O->DbgSymCount * sizeof (DbgSym*)); for (I = 0; I < O->DbgSymCount; ++I) { O->DbgSyms [I] = ReadDbgSym (F, O); @@ -169,7 +169,7 @@ void ObjReadSections (FILE* F, ObjData* O) { unsigned I; - O->SectionCount = Read8 (F); + O->SectionCount = ReadVar (F); O->Sections = xmalloc (O->SectionCount * sizeof (Section*)); for (I = 0; I < O->SectionCount; ++I) { O->Sections [I] = ReadSection (F, O); @@ -226,3 +226,4 @@ void ObjAdd (FILE* Obj, const char* Name) + diff --git a/src/ld65/segments.c b/src/ld65/segments.c index 17976e52d..775835818 100644 --- a/src/ld65/segments.c +++ b/src/ld65/segments.c @@ -215,7 +215,7 @@ Section* ReadSection (FILE* F, ObjData* O) /* Read a section from a file */ { unsigned HashVal; - char Name [256]; + char* Name; unsigned long Size; unsigned char Align; unsigned char Type; @@ -223,7 +223,7 @@ Section* ReadSection (FILE* F, ObjData* O) Section* Sec; /* Read the name */ - ReadStr (F, Name); + Name = ReadStr (F); /* Read the size */ Size = Read32 (F); @@ -237,7 +237,7 @@ Section* ReadSection (FILE* F, ObjData* O) /* Print some data */ if (Verbose > 1) { printf ("Module `%s': Found segment `%s', size = %lu, align = %u, type = %u\n", - O->Name, Name, Size, Align, Type); + O->Name, Name, Size, Align, Type); } /* Create a hash over the name and try to locate the segment in the table */ @@ -254,6 +254,9 @@ Section* ReadSection (FILE* F, ObjData* O) HashTab [HashVal] = S; } + /* We have the segment and don't need the name any longer */ + xfree (Name); + /* Allocate the section we will return later */ Sec = NewSection (S, Align, Type); @@ -281,20 +284,8 @@ Section* ReadSection (FILE* F, ObjData* O) /* Handle the different fragment types */ switch (Type) { - case FRAG_LITERAL8: - Frag = NewFragment (FRAG_LITERAL, Read8 (F), Sec); - break; - - case FRAG_LITERAL16: - Frag = NewFragment (FRAG_LITERAL, Read16 (F), Sec); - break; - - case FRAG_LITERAL24: - Frag = NewFragment (FRAG_LITERAL, Read24 (F), Sec); - break; - - case FRAG_LITERAL32: - Frag = NewFragment (FRAG_LITERAL, Read32 (F), Sec); + case FRAG_LITERAL: + Frag = NewFragment (Type, ReadVar (F), Sec); break; case FRAG_EXPR8: @@ -310,7 +301,7 @@ Section* ReadSection (FILE* F, ObjData* O) case FRAG_FILL: /* Will allocate memory, but we don't care... */ - Frag = NewFragment (FRAG_FILL, Read16 (F), Sec); + Frag = NewFragment (Type, ReadVar (F), Sec); break; default: diff --git a/src/od65/dump.c b/src/od65/dump.c index 106f5c8a8..b9bf7f122 100644 --- a/src/od65/dump.c +++ b/src/od65/dump.c @@ -143,20 +143,8 @@ static unsigned SkipFragment (FILE* F) /* Handle the different fragment types */ switch (Type) { - case FRAG_LITERAL8: - Size = Read8 (F); - break; - - case FRAG_LITERAL16: - Size = Read16 (F); - break; - - case FRAG_LITERAL24: - Size = Read24 (F); - break; - - case FRAG_LITERAL32: - Size = Read32 (F); + case FRAG_LITERAL: + Size = ReadVar (F); break; case FRAG_EXPR8: @@ -171,7 +159,7 @@ static unsigned SkipFragment (FILE* F) break; case FRAG_FILL: - Size = Read16 (F); + Size = ReadVar (F); break; default: @@ -277,7 +265,7 @@ void DumpObjOptions (FILE* F, unsigned long Offset) printf (" Options:\n"); /* Read the number of options and print it */ - Count = Read16 (F); + Count = ReadVar (F); printf (" Count:%27u\n", Count); /* Read and print all options */ @@ -313,7 +301,7 @@ void DumpObjOptions (FILE* F, unsigned long Offset) switch (ArgType) { case OPT_ARGSTR: - ArgStr = ReadMallocedStr (F); + ArgStr = ReadStr (F); ArgLen = strlen (ArgStr); printf (" Data:%*s\"%s\"\n", 24-ArgLen, "", ArgStr); xfree (ArgStr); @@ -361,7 +349,7 @@ void DumpObjFiles (FILE* F, unsigned long Offset) printf (" Files:\n"); /* Read the number of files and print it */ - Count = Read16 (F); + Count = ReadVar (F); printf (" Count:%27u\n", Count); /* Read and print all files */ @@ -370,7 +358,7 @@ void DumpObjFiles (FILE* F, unsigned long Offset) /* Read the data for one file */ unsigned long MTime = Read32 (F); unsigned long Size = Read32 (F); - char* Name = ReadMallocedStr (F); + char* Name = ReadStr (F); unsigned Len = strlen (Name); /* Print the header */ @@ -409,14 +397,14 @@ void DumpObjSegments (FILE* F, unsigned long Offset) printf (" Segments:\n"); /* Read the number of segments and print it */ - Count = Read8 (F); + Count = ReadVar (F); printf (" Count:%27u\n", Count); /* Read and print all segments */ for (I = 0; I < Count; ++I) { /* Read the data for one segments */ - char* Name = ReadMallocedStr (F); + char* Name = ReadStr (F); unsigned Len = strlen (Name); unsigned long Size = Read32 (F); unsigned Align = (1U << Read8 (F)); @@ -484,7 +472,7 @@ void DumpObjImports (FILE* F, unsigned long Offset) printf (" Imports:\n"); /* Read the number of imports and print it */ - Count = Read16 (F); + Count = ReadVar (F); printf (" Count:%27u\n", Count); /* Read and print all imports */ @@ -494,7 +482,7 @@ void DumpObjImports (FILE* F, unsigned long Offset) /* Read the data for one import */ unsigned char Type = Read8 (F); - char* Name = ReadMallocedStr (F); + char* Name = ReadStr (F); unsigned Len = strlen (Name); ReadFilePos (F, &Pos); @@ -540,7 +528,7 @@ void DumpObjExports (FILE* F, unsigned long Offset) printf (" Exports:\n"); /* Read the number of exports and print it */ - Count = Read16 (F); + Count = ReadVar (F); printf (" Count:%27u\n", Count); /* Read and print all exports */ @@ -552,7 +540,7 @@ void DumpObjExports (FILE* F, unsigned long Offset) /* Read the data for one export */ unsigned char Type = Read8 (F); - char* Name = ReadMallocedStr (F); + char* Name = ReadStr (F); unsigned Len = strlen (Name); if (Type & EXP_EXPR) { SkipExpr (F); @@ -617,19 +605,19 @@ void DumpObjDbgSyms (FILE* F, unsigned long Offset) } /* Read the number of exports and print it */ - Count = Read16 (F); + Count = ReadVar (F); printf (" Count:%27u\n", Count); /* Read and print all debug symbols */ for (I = 0; I < Count; ++I) { unsigned long Value = 0; - int HaveValue; + int HaveValue; const char* TypeDesc; /* Read the data for one symbol */ unsigned char Type = Read8 (F); - char* Name = ReadMallocedStr (F); + char* Name = ReadStr (F); unsigned Len = strlen (Name); if (Type & EXP_EXPR) { SkipExpr (F); @@ -666,3 +654,4 @@ void DumpObjDbgSyms (FILE* F, unsigned long Offset) + diff --git a/src/od65/fileio.c b/src/od65/fileio.c index c8ccc773d..f700f3186 100644 --- a/src/od65/fileio.c +++ b/src/od65/fileio.c @@ -67,7 +67,7 @@ unsigned Read8 (FILE* F) int C = getc (F); if (C == EOF) { Error ("Read error (file corrupt?)"); - } + } return C; } @@ -121,27 +121,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 +167,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; } diff --git a/src/od65/fileio.h b/src/od65/fileio.h index 06d55fcfb..862c9b206 100644 --- a/src/od65/fileio.h +++ b/src/od65/fileio.h @@ -70,10 +70,10 @@ unsigned long Read32 (FILE* F); long Read32Signed (FILE* F); /* Read a 32 bit value from the file. Sign extend the value. */ -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 */ -char* ReadMallocedStr (FILE* F); +char* ReadStr (FILE* F); /* Read a string from the file into a malloced area */ FilePos* ReadFilePos (FILE* F, FilePos* Pos); -- 2.39.5