-
Notifications
You must be signed in to change notification settings - Fork 4
/
io.c
272 lines (245 loc) · 5.58 KB
/
io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* file input/output
*
* $Header: io.c 1.14 95/08/01 $
* $Log: io.c,v $
* Revision 1.14 95/08/01 xx:xx:xx BB
* Fixed for Borland C/C++
*
* Revision 1.13 94/12/12 17:30:45 arb
* Added code for ArcFS outputsize checking
*
* Revision 1.12 93/08/20 12:31:20 arb
* Added code for ArcFS archive headers
*
* Revision 1.11 93/08/20 10:30:50 arb
* Added code for -C option to convert filenames to lowercase
*
* Revision 1.10 93/08/20 10:30:51 arb
* Changed read_header() to allow top-bit-set characters in RISCOS filenames
*
* Revision 1.9 93/03/05 14:44:02 arb
* Added <string.h> for RISCOS, needed for memset
*
* Revision 1.8 92/12/09 11:40:30 duplain
* Changed ret in check_stream() from type int. #ifdef'd out write_halfword()
* and write_word().
*
* Revision 1.7 92/12/07 17:18:25 duplain
* reformatted source.
*
* Revision 1.6 92/11/04 16:57:49 duplain
* Changed read_header() so it doesn't read the load/exec/attr fields if
* PC archive file.
*
* Revision 1.5 92/10/07 10:36:56 duplain
* Changed order of function so no need to include "io.h" (gcc
* complained on some platforms).
*
* Revision 1.4 92/10/05 11:01:07 duplain
* Recoded write_word() and write_halfword(). read_header() now clears the
* header structure prior to reading data into it.
*
* Revision 1.3 92/10/02 17:41:49 duplain
* Fixed read_header() so it returns immediately if comptype & 0x7f == 0.
*
* Revision 1.2 92/10/01 11:20:32 duplain
* Moved reading of STARTBYTE to unarc.c .
*
* Revision 1.1 92/09/29 18:02:19 duplain
* Initial revision
*
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "spark.h"
#include "main.h"
#include "error.h"
#include "arcfs.h"
#ifdef RISCOS /* next line RISC OS only */
#define isascii(x) 1
#endif /* RISCOS */
#include "nsparkio.h"
/*
* check for EOF or write/read errors on stream.
*/
Ferror
check_stream(FILE *fp)
{
Ferror ret = FNOERR;
if (feof(fp))
ret = FEND;
else if (ferror(fp))
ret = FRWERR;
if (ret != FNOERR)
clearerr(fp);
return (ret);
}
/*
* read a byte from the input stream.
*/
Byte
read_byte(FILE *ifp)
{
return ((Byte) getc(ifp));
}
/*
* read a little-endian 2-byte halfword from the input stream.
*/
Halfword
read_halfword(FILE *ifp)
{
union
{
Halfword h;
Byte b[sizeof(Halfword)];
}
ret;
if (fread((char *) &ret.h, 1, sizeof(Halfword), ifp)!=sizeof(Halfword)) {
error("Read error!");
}
#if defined(__MSDOS__)
/* MSDOS reads bytes in the correct order. Save a bit of time
* by returning the value */
return (ret.h);
#else
/* Ensure the data is in the correct order */
return (ret.b[0] | ret.b[1] << 8);
#endif
}
/*
* read a little-endian 4-byte word from the input stream.
*/
Word
read_word(FILE *ifp)
{
union
{
Word w;
Byte b[sizeof(Word)];
}
ret;
if (fread((char *) &ret.w, 1, sizeof(Word), ifp)!=sizeof(Word)) {
error("Read error!");
}
#if defined(__MSDOS__)
/* MSDOS reads bytes in the correct order. Save a bit of time
* by returning the value */
return (ret.w);
#else
/* Ensure the data is in the correct order */
return (ret.b[0] | (ret.b[1] << 8) | (ret.b[2] << 16) | (ret.b[3] << 24));
#endif
}
/*
* write a byte to the output stream.
*/
void
write_byte(FILE *ofp, Byte byte)
{
if (writesize-- > 0)
putc((int) byte, ofp);
}
#ifdef notyet
/*
* write a little-endian 2-byte halfword to the output stream.
*/
void
write_halfword(FILE *ofp, Halfword halfword)
{
write_byte(ofp, halfword & 0xff);
write_byte(ofp, (halfword >> 8) & 0xff);
}
/*
* write a little-endian 4-byte word to the output stream.
*/
void
write_word(FILE *ofp, Word word)
{
write_byte(ofp, word & 0xff);
write_byte(ofp, (word >> 8) & 0xff);
write_byte(ofp, (word >> 16) & 0xff);
write_byte(ofp, (word >> 24) & 0xff);
}
#endif /* notyet */
/*
* read a compression-header from the file
*/
Header *
read_header(FILE *ifp)
{
static Header header;
register int i;
register char *cptr;
Byte byte;
memset((char *) &header, '\0', sizeof(header));
if (arcfs)
return (arcfs_read_header(ifp));
header.comptype = read_byte(ifp);
if (!(header.comptype & ~ARCHPACK))
return (&header); /* EOF */
for (i = 0, cptr = header.name; i <= 12; i++, cptr++)
{
byte = read_byte(ifp);
#ifdef RISCOS
if (byte < ' ')
#else
if (byte < ' ' || byte > '~')
#endif
byte = '\0';
else if (byte == PATHSEP) /* illegal in filename */
byte = '_';
*cptr = byte;
}
*cptr = '\0'; /* null terminate */
if (singlecase)
{
for (i = 0, cptr = header.name; i <= 12; i++, cptr++)
if (isascii(*cptr) && isupper(*cptr))
*cptr = tolower(*cptr);
}
header.complen = read_word(ifp);
header.date = read_halfword(ifp);
header.time = read_halfword(ifp);
header.crc = read_halfword(ifp);
if ((header.comptype & ~ARCHPACK) > CT_NOTCOMP)
header.origlen = read_word(ifp);
else
header.origlen = header.complen;
if (header.comptype & ARCHPACK)
{
header.load = read_word(ifp);
header.exec = read_word(ifp);
header.attr = read_word(ifp);
}
if (check_stream(ifp) == FRWERR)
return (NULL);
return (&header);
}
Status
read_sqsh_header(FILE *fp, SqshHeader *header)
{
if (fread(header->magic, 1, 4, fp) != 4)
{
return RERR;
}
if (strncmp(header->magic, "SQSH", 4) != 0)
{
error("Not a RISC OS squash file");
return RERR;
}
header->origlen = read_word(fp);
header->load = read_word(fp);
header->exec = read_word(fp);
header->reserved = read_word(fp);
return NOERR;
}
void
sqsh_header_to_header(SqshHeader *sqsh_header, Header *header)
{
header->load = sqsh_header->load;
header->exec = sqsh_header->exec;
header->origlen = sqsh_header->origlen;
arcfs_fixtime(header);
}