1 /*****************************************************************************/
5 /* File I/O for the ld65 linker */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
47 /*****************************************************************************/
49 /*****************************************************************************/
53 void Write8 (FILE* F, unsigned Val)
54 /* Write an 8 bit value to the file */
56 if (putc (Val, F) == EOF) {
57 Error ("Write error (disk full?)");
63 void Write16 (FILE* F, unsigned Val)
64 /* Write a 16 bit value to the file */
66 Write8 (F, (unsigned char) Val);
67 Write8 (F, (unsigned char) (Val >> 8));
72 void Write24 (FILE* F, unsigned long Val)
73 /* Write a 24 bit value to the file */
75 Write8 (F, (unsigned char) Val);
76 Write8 (F, (unsigned char) (Val >> 8));
77 Write8 (F, (unsigned char) (Val >> 16));
82 void Write32 (FILE* F, unsigned long Val)
83 /* Write a 32 bit value to the file */
85 Write8 (F, (unsigned char) Val);
86 Write8 (F, (unsigned char) (Val >> 8));
87 Write8 (F, (unsigned char) (Val >> 16));
88 Write8 (F, (unsigned char) (Val >> 24));
93 void WriteVal (FILE* F, unsigned long Val, unsigned Size)
94 /* Write a value of the given size to the output file */
115 Internal ("WriteVal: Invalid size: %u", Size);
122 void WriteVar (FILE* F, unsigned long V)
123 /* Write a variable sized value to the file in special encoding */
125 /* We will write the value to the file in 7 bit chunks. If the 8th bit
126 * is clear, we're done, if it is set, another chunk follows. This will
127 * allow us to encode smaller values with less bytes, at the expense of
128 * needing 5 bytes if a 32 bit value is written to file.
131 unsigned char C = (V & 0x7F);
142 void WriteStr (FILE* F, const char* S)
143 /* Write a string to the file */
145 unsigned Len = strlen (S);
147 WriteData (F, S, Len);
152 void WriteData (FILE* F, const void* Data, unsigned Size)
153 /* Write data to the file */
155 if (fwrite (Data, 1, Size, F) != Size) {
156 Error ("Write error (disk full?)");
162 void WriteMult (FILE* F, unsigned char Val, unsigned long Count)
163 /* Write one byte several times to the file */
172 unsigned Read8 (FILE* F)
173 /* Read an 8 bit value from the file */
177 Error ("Read error (file corrupt?)");
184 unsigned Read16 (FILE* F)
185 /* Read a 16 bit value from the file */
187 unsigned Lo = Read8 (F);
188 unsigned Hi = Read8 (F);
189 return (Hi << 8) | Lo;
194 unsigned long Read24 (FILE* F)
195 /* Read a 24 bit value from the file */
197 unsigned long Lo = Read16 (F);
198 unsigned long Hi = Read8 (F);
199 return (Hi << 16) | Lo;
204 unsigned long Read32 (FILE* F)
205 /* Read a 32 bit value from the file */
207 unsigned long Lo = Read16 (F);
208 unsigned long Hi = Read16 (F);
209 return (Hi << 16) | Lo;
214 long Read32Signed (FILE* F)
215 /* Read a 32 bit value from the file. Sign extend the value. */
217 /* Read a 32 bit value */
218 unsigned long V = Read32 (F);
220 /* Sign extend the value */
221 if (V & 0x80000000UL) {
226 /* Return it as a long */
232 unsigned long ReadVar (FILE* F)
233 /* Read a variable size value from the file */
235 /* The value was written to the file in 7 bit chunks LSB first. If there
236 * are more bytes, bit 8 is set, otherwise it is clear.
244 /* Encode it into the target value */
245 V |= ((unsigned long)(C & 0x7F)) << Shift;
250 /* Return the value read */
256 char* ReadStr (FILE* F)
257 /* Read a string from the file (the memory will be malloc'ed) */
259 /* Read the length */
260 unsigned Len = ReadVar (F);
262 /* Allocate memory and read the string itself */
263 char* S = xmalloc (Len + 1);
264 ReadData (F, S, Len);
266 /* Terminate the string and return it */
273 FilePos* ReadFilePos (FILE* F, FilePos* Pos)
274 /* Read a file position from the file */
276 /* Read the data fields */
277 Pos->Line = ReadVar (F);
278 Pos->Col = ReadVar (F);
279 Pos->Name = ReadVar (F);
285 void* ReadData (FILE* F, void* Data, unsigned Size)
286 /* Read data from the file */
288 /* Explicitly allow reading zero bytes */
290 if (fread (Data, 1, Size, F) != Size) {
291 Error ("Read error (file corrupt?)");