1 /*****************************************************************************/
5 /* File I/O for the ar65 archiver */
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 char 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 Write32 (FILE* F, unsigned long Val)
73 /* Write a 32 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));
78 Write8 (F, (unsigned char) (Val >> 24));
83 void WriteVar (FILE* F, unsigned long V)
84 /* Write a variable sized value to the file in special encoding */
86 /* We will write the value to the file in 7 bit chunks. If the 8th bit
87 * is clear, we're done, if it is set, another chunk follows. This will
88 * allow us to encode smaller values with less bytes, at the expense of
89 * needing 5 bytes if a 32 bit value is written to file.
92 unsigned char C = (V & 0x7F);
103 void WriteStr (FILE* F, const char* S)
104 /* Write a string to the file */
106 unsigned Len = strlen (S);
108 WriteData (F, S, Len);
113 void WriteData (FILE* F, const void* Data, unsigned Size)
114 /* Write data to the file */
116 if (fwrite (Data, 1, Size, F) != Size) {
117 Error ("Write error (disk full?)");
123 unsigned Read8 (FILE* F)
124 /* Read an 8 bit value from the file */
128 Error ("Read error (file corrupt?)");
135 unsigned Read16 (FILE* F)
136 /* Read a 16 bit value from the file */
138 unsigned Lo = Read8 (F);
139 unsigned Hi = Read8 (F);
140 return (Hi << 8) | Lo;
145 unsigned long Read32 (FILE* F)
146 /* Read a 32 bit value from the file */
148 unsigned long Lo = Read16 (F);
149 unsigned long Hi = Read16 (F);
150 return (Hi << 16) | Lo;
155 unsigned long ReadVar (FILE* F)
156 /* Read a variable size value from the file */
158 /* The value was written to the file in 7 bit chunks LSB first. If there
159 * are more bytes, bit 8 is set, otherwise it is clear.
167 /* Encode it into the target value */
168 V |= ((unsigned long)(C & 0x7F)) << Shift;
173 /* Return the value read */
179 char* ReadStr (FILE* F)
180 /* Read a string from the file (the memory will be malloc'ed) */
182 /* Read the length */
183 unsigned Len = ReadVar (F);
185 /* Allocate memory and read the string itself */
186 char* S = xmalloc (Len + 1);
187 ReadData (F, S, Len);
189 /* Terminate the string and return it */
196 void* ReadData (FILE* F, void* Data, unsigned Size)
197 /* Read data from the file */
199 if (fread (Data, 1, Size, F) != Size) {
200 Error ("Read error (file corrupt?)");