1 /*****************************************************************************/
5 /* File I/O for the ld65 linker */
9 /* (C) 1998-2008 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 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 void FileSetPos (FILE* F, unsigned long Pos)
56 /* Seek to the given absolute position, fail on errors */
58 if (fseek (F, Pos, SEEK_SET) != 0) {
59 Error ("Cannot seek: %s", strerror (errno));
65 unsigned long FileGetPos (FILE* F)
66 /* Return the current file position, fail on errors */
70 Error ("Error in ftell: %s", strerror (errno));
77 void Write8 (FILE* F, unsigned Val)
78 /* Write an 8 bit value to the file */
80 if (putc (Val, F) == EOF) {
81 Error ("Write error (disk full?)");
87 void Write16 (FILE* F, unsigned Val)
88 /* Write a 16 bit value to the file */
90 Write8 (F, (unsigned char) Val);
91 Write8 (F, (unsigned char) (Val >> 8));
96 void Write24 (FILE* F, unsigned long Val)
97 /* Write a 24 bit value to the file */
99 Write8 (F, (unsigned char) Val);
100 Write8 (F, (unsigned char) (Val >> 8));
101 Write8 (F, (unsigned char) (Val >> 16));
106 void Write32 (FILE* F, unsigned long Val)
107 /* Write a 32 bit value to the file */
109 Write8 (F, (unsigned char) Val);
110 Write8 (F, (unsigned char) (Val >> 8));
111 Write8 (F, (unsigned char) (Val >> 16));
112 Write8 (F, (unsigned char) (Val >> 24));
117 void WriteVal (FILE* F, unsigned long Val, unsigned Size)
118 /* Write a value of the given size to the output file */
139 Internal ("WriteVal: Invalid size: %u", Size);
146 void WriteVar (FILE* F, unsigned long V)
147 /* Write a variable sized value to the file in special encoding */
149 /* We will write the value to the file in 7 bit chunks. If the 8th bit
150 * is clear, we're done, if it is set, another chunk follows. This will
151 * allow us to encode smaller values with less bytes, at the expense of
152 * needing 5 bytes if a 32 bit value is written to file.
155 unsigned char C = (V & 0x7F);
166 void WriteStr (FILE* F, const char* S)
167 /* Write a string to the file */
169 unsigned Len = strlen (S);
171 WriteData (F, S, Len);
176 void WriteData (FILE* F, const void* Data, unsigned Size)
177 /* Write data to the file */
179 if (fwrite (Data, 1, Size, F) != Size) {
180 Error ("Write error (disk full?)");
186 void WriteMult (FILE* F, unsigned char Val, unsigned long Count)
187 /* Write one byte several times to the file */
196 unsigned Read8 (FILE* F)
197 /* Read an 8 bit value from the file */
201 long Pos = ftell (F);
202 Error ("Read error at position %ld (file corrupt?)", Pos);
209 unsigned Read16 (FILE* F)
210 /* Read a 16 bit value from the file */
212 unsigned Lo = Read8 (F);
213 unsigned Hi = Read8 (F);
214 return (Hi << 8) | Lo;
219 unsigned long Read24 (FILE* F)
220 /* Read a 24 bit value from the file */
222 unsigned long Lo = Read16 (F);
223 unsigned long Hi = Read8 (F);
224 return (Hi << 16) | Lo;
229 unsigned long Read32 (FILE* F)
230 /* Read a 32 bit value from the file */
232 unsigned long Lo = Read16 (F);
233 unsigned long Hi = Read16 (F);
234 return (Hi << 16) | Lo;
239 long Read32Signed (FILE* F)
240 /* Read a 32 bit value from the file. Sign extend the value. */
242 /* Read a 32 bit value */
243 unsigned long V = Read32 (F);
245 /* Sign extend the value */
246 if (V & 0x80000000UL) {
251 /* Return it as a long */
257 unsigned long ReadVar (FILE* F)
258 /* Read a variable size value from the file */
260 /* The value was written to the file in 7 bit chunks LSB first. If there
261 * are more bytes, bit 8 is set, otherwise it is clear.
269 /* Encode it into the target value */
270 V |= ((unsigned long)(C & 0x7F)) << Shift;
275 /* Return the value read */
281 unsigned ReadStr (FILE* F)
282 /* Read a string from the file, place it into the global string pool, and
283 * return its string id.
287 StrBuf Buf = STATIC_STRBUF_INITIALIZER;
289 /* Read the length */
290 unsigned Len = ReadVar (F);
292 /* Expand the string buffer memory */
293 SB_Realloc (&Buf, Len);
295 /* Read the string */
296 ReadData (F, SB_GetBuf (&Buf), Len);
299 /* Insert it into the string pool and remember the id */
300 Id = GetStrBufId (&Buf);
302 /* Free the memory buffer */
305 /* Return the string id */
311 FilePos* ReadFilePos (FILE* F, FilePos* Pos)
312 /* Read a file position from the file */
314 /* Read the data fields */
315 Pos->Line = ReadVar (F);
316 Pos->Col = ReadVar (F);
317 Pos->Name = ReadVar (F);
323 void* ReadData (FILE* F, void* Data, unsigned Size)
324 /* Read data from the file */
326 /* Explicitly allow reading zero bytes */
328 if (fread (Data, 1, Size, F) != Size) {
329 long Pos = ftell (F);
330 Error ("Read error at position %ld (file corrupt?)", Pos);