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 Error ("Read error (file corrupt?)");
208 unsigned Read16 (FILE* F)
209 /* Read a 16 bit value from the file */
211 unsigned Lo = Read8 (F);
212 unsigned Hi = Read8 (F);
213 return (Hi << 8) | Lo;
218 unsigned long Read24 (FILE* F)
219 /* Read a 24 bit value from the file */
221 unsigned long Lo = Read16 (F);
222 unsigned long Hi = Read8 (F);
223 return (Hi << 16) | Lo;
228 unsigned long Read32 (FILE* F)
229 /* Read a 32 bit value from the file */
231 unsigned long Lo = Read16 (F);
232 unsigned long Hi = Read16 (F);
233 return (Hi << 16) | Lo;
238 long Read32Signed (FILE* F)
239 /* Read a 32 bit value from the file. Sign extend the value. */
241 /* Read a 32 bit value */
242 unsigned long V = Read32 (F);
244 /* Sign extend the value */
245 if (V & 0x80000000UL) {
250 /* Return it as a long */
256 unsigned long ReadVar (FILE* F)
257 /* Read a variable size value from the file */
259 /* The value was written to the file in 7 bit chunks LSB first. If there
260 * are more bytes, bit 8 is set, otherwise it is clear.
268 /* Encode it into the target value */
269 V |= ((unsigned long)(C & 0x7F)) << Shift;
274 /* Return the value read */
280 unsigned ReadStr (FILE* F)
281 /* Read a string from the file, place it into the global string pool, and
282 * return its string id.
286 StrBuf Buf = STATIC_STRBUF_INITIALIZER;
288 /* Read the length */
289 unsigned Len = ReadVar (F);
291 /* Expand the string buffer memory */
292 SB_Realloc (&Buf, Len);
294 /* Read the string */
295 ReadData (F, SB_GetBuf (&Buf), Len);
298 /* Insert it into the string pool and remember the id */
299 Id = GetStrBufId (&Buf);
301 /* Free the memory buffer */
304 /* Return the string id */
310 FilePos* ReadFilePos (FILE* F, FilePos* Pos)
311 /* Read a file position from the file */
313 /* Read the data fields */
314 Pos->Line = ReadVar (F);
315 Pos->Col = ReadVar (F);
316 Pos->Name = ReadVar (F);
322 void* ReadData (FILE* F, void* Data, unsigned Size)
323 /* Read data from the file */
325 /* Explicitly allow reading zero bytes */
327 if (fread (Data, 1, Size, F) != Size) {
328 Error ("Read error (file corrupt?)");