1 /*****************************************************************************/
5 /* File I/O for the od65 object file dump utility */
9 /* (C) 1998-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
48 /*****************************************************************************/
50 /*****************************************************************************/
54 void FileSetPos (FILE* F, unsigned long Pos)
55 /* Seek to the given absolute position, fail on errors */
57 if (fseek (F, Pos, SEEK_SET) != 0) {
58 Error ("Cannot seek: %s", strerror (errno));
64 unsigned long FileGetPos (FILE* F)
65 /* Return the current file position, fail on errors */
69 Error ("Error in ftell: %s", strerror (errno));
76 unsigned Read8 (FILE* F)
77 /* Read an 8 bit value from the file */
81 Error ("Read error (file corrupt?)");
88 unsigned Read16 (FILE* F)
89 /* Read a 16 bit value from the file */
91 unsigned Lo = Read8 (F);
92 unsigned Hi = Read8 (F);
93 return (Hi << 8) | Lo;
98 unsigned long Read24 (FILE* F)
99 /* Read a 24 bit value from the file */
101 unsigned long Lo = Read16 (F);
102 unsigned long Hi = Read8 (F);
103 return (Hi << 16) | Lo;
108 unsigned long Read32 (FILE* F)
109 /* Read a 32 bit value from the file */
111 unsigned long Lo = Read16 (F);
112 unsigned long Hi = Read16 (F);
113 return (Hi << 16) | Lo;
118 long Read32Signed (FILE* F)
119 /* Read a 32 bit value from the file. Sign extend the value. */
121 /* Read a 32 bit value */
122 unsigned long V = Read32 (F);
124 /* Sign extend the value */
125 if (V & 0x80000000UL) {
130 /* Return it as a long */
136 unsigned long ReadVar (FILE* F)
137 /* Read a variable size value from the file */
139 /* The value was written to the file in 7 bit chunks LSB first. If there
140 ** are more bytes, bit 8 is set, otherwise it is clear.
148 /* Encode it into the target value */
149 V |= ((unsigned long)(C & 0x7F)) << Shift;
154 /* Return the value read */
160 char* ReadStr (FILE* F)
161 /* Read a string from the file into a malloced area */
163 /* Read the length */
164 unsigned Len = ReadVar (F);
166 /* Allocate memory */
167 char* Str = xmalloc (Len + 1);
169 /* Read the string itself */
170 ReadData (F, Str, Len);
172 /* Terminate the string and return it */
179 FilePos* ReadFilePos (FILE* F, FilePos* Pos)
180 /* Read a file position from the file */
182 /* Read the data fields */
183 Pos->Line = ReadVar (F);
184 Pos->Col = ReadVar (F);
185 Pos->Name = ReadVar (F);
191 void* ReadData (FILE* F, void* Data, unsigned Size)
192 /* Read data from the file */
194 /* Accept zero sized reads */
196 if (fread (Data, 1, Size, F) != Size) {
197 Error ("Read error (file corrupt?)");
205 void ReadObjHeader (FILE* F, ObjHeader* H)
206 /* Read an object file header from the file */
208 /* Read all fields */
209 H->Magic = Read32 (F);
210 H->Version = Read16 (F);
211 H->Flags = Read16 (F);
212 H->OptionOffs = Read32 (F);
213 H->OptionSize = Read32 (F);
214 H->FileOffs = Read32 (F);
215 H->FileSize = Read32 (F);
216 H->SegOffs = Read32 (F);
217 H->SegSize = Read32 (F);
218 H->ImportOffs = Read32 (F);
219 H->ImportSize = Read32 (F);
220 H->ExportOffs = Read32 (F);
221 H->ExportSize = Read32 (F);
222 H->DbgSymOffs = Read32 (F);
223 H->DbgSymSize = Read32 (F);
224 H->LineInfoOffs = Read32 (F);
225 H->LineInfoSize = Read32 (F);
226 H->StrPoolOffs = Read32 (F);
227 H->StrPoolSize = Read32 (F);
228 H->AssertOffs = Read32 (F);
229 H->AssertSize = Read32 (F);
230 H->ScopeOffs = Read32 (F);
231 H->ScopeSize = Read32 (F);
232 H->SpanOffs = Read32 (F);
233 H->SpanSize = Read32 (F);
238 void ReadStrPool (FILE* F, Collection* C)
239 /* Read a string pool from the current position into C. */
241 /* The number of strings is the first item */
242 unsigned long Count = ReadVar (F);
244 /* Read all the strings into C */
246 CollAppend (C, ReadStr (F));