-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsec.c
437 lines (390 loc) · 6.4 KB
/
unsec.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "compress.h"
struct header
{
unsigned int check_code1;
unsigned int check_code2;
unsigned int total_size;
unsigned int zero;
unsigned int thirty_two;
unsigned int data_offset;
};
#define STATE_NULL 0
#define STATE_GOT_R 1
#define STATE_GOT_RN 2
#define STATE_GOT_RNA 3
#define STATE_GOT_RNAM 4
#define STATE_GOT_RD 5
#define STATE_GOT_RDI 6
#define STATE_GOT_RDIR 7
#define STATE_GOT_RS 8
#define STATE_GOT_RSQ 9
#define STATE_GOT_RSQS 10
#define STATE_GOT_RDA 12
#define STATE_GOT_RDAT 13
#define DATA_OFFSET_POINTER 24
static int debug = 0;
static int list_contents = 0;
static int append_filetype = 1;
static char file_name[1024];
static FILE *infp;
void
usage(char *progname)
{
fprintf(stderr, "Usage: %s [-l] [-t] sec_file\n", progname);
fprintf(stderr, " -l: list contents, don't extract\n");
fprintf(stderr, " -t: don't append RISC OS filetype\n");
exit(1);
}
unsigned int
read_word()
{
unsigned int num = 0;
num = fgetc(infp);
num |= fgetc(infp) << 8;
num |= fgetc(infp) << 16;
num |= fgetc(infp) << 24;
return num;
}
int
open_archive_file(FILE **fp, char *file_name, struct header *header)
{
*fp = fopen(file_name, "rb");
if (!*fp)
{
return 1;
}
read_word();
header->check_code1 = read_word();
header->check_code2 = read_word();
header->total_size = read_word();
header->zero = read_word();
header->thirty_two = read_word();
header->data_offset = read_word();
if (header->check_code1 != 0x79766748)
{
return 2;
}
if (header->check_code2 != 0x216c6776)
{
return 2;
}
if (header->zero != 0)
{
return 2;
}
if (header->thirty_two != 32)
{
return 2;
}
return 0;
}
int
skip_to_data(struct header *header)
{
int r;
int c;
unsigned int ptr;
r = fseek(infp, header->data_offset - DATA_OFFSET_POINTER, SEEK_SET);
if (r != 0)
{
return 1;
}
else
{
do
{
c = fgetc(infp);
} while (!feof(infp) && c != '\0');
}
if (feof(infp))
{
return 2;
}
return r;
}
void
file_name_to_unix()
{
char *bufp = &file_name[0];
while (*bufp)
{
if (*bufp == '.')
{
*bufp = '/';
}
else if (*bufp == '/')
{
*bufp = '.';
}
*bufp++;
}
}
void
handle_dir_data()
{
int c;
int chunk_size = 0;
int attributes = 0;
chunk_size = read_word();
attributes = read_word();
if (debug)
printf("Got dir %s %x\n", file_name, attributes);
if (!list_contents)
{
mkdir(file_name, 0777);
}
}
void
handle_file_name()
{
int c;
int chunk_size;
int i;
char *bufp = &file_name[0];
*bufp = '\0';
chunk_size = read_word();
bufp = &file_name[0];
do {
*bufp++ = (char)fgetc(infp);
*bufp++ = (char)fgetc(infp);
*bufp++ = (char)fgetc(infp);
*bufp++ = (char)fgetc(infp);
} while (*(bufp - 1) != '\0');
file_name_to_unix();
if (debug)
printf("Got file %x %s\n", chunk_size, file_name);
}
void
handle_sqs()
{
int chunk_size;
chunk_size = read_word();
if (debug)
printf("Got sqs %x\n", chunk_size);
}
void
extract_data()
{
int chunk_size;
int c;
unsigned int load;
unsigned int exec;
unsigned int attrs;
unsigned int cmp_size;
unsigned int orig_size;
int i;
FILE *fp;
char file_type[10];
chunk_size = read_word();
if (debug)
printf("Got data %d\n", chunk_size);
load = read_word();
if (append_filetype &&((load & 0xfff00000) == 0xfff00000))
{
if (debug)
printf("filetype = %x\n", (load >> 8) & 0xfff);
snprintf(file_type, sizeof(file_type), ",%03x", (load >> 8) & 0xfff);
strcat(file_name, file_type);
}
exec = read_word();
if (debug)
printf("exec = %x\n", exec);
attrs = read_word();
if (debug)
printf("attrs = %x\n", attrs);
cmp_size = read_word();
if (debug)
printf("compressed size = %d\n", cmp_size);
orig_size = read_word();
if (debug)
printf("original size = %d\n", orig_size);
if (!list_contents)
{
if ((fp = fopen(file_name, "wb")) == NULL)
{
printf("Failed to open file %s\n", file_name);
}
}
if (list_contents)
{
fseek(infp, chunk_size - 20, SEEK_CUR);
printf("%8d %s\n", orig_size, file_name);
}
else if (cmp_size == 0)
{
for (i = 0; i < orig_size; i++)
{
c = fgetc(infp);
fputc(c, fp);
}
}
else
{
uncompress(cmp_size, orig_size, infp, fp, UNIX_COMPRESS);
}
if (!list_contents)
{
fclose(fp);
}
// FIXME: read to end of 4 byte block
}
int
main(int argc, char *argv[])
{
int c;
int state = STATE_NULL;
int r;
struct header header;
int i;
char *file_name;
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
{
if (strcmp(argv[i], "-l") == 0)
{
list_contents = 1;
}
else if (strcmp(argv[i], "-t") == 0)
{
append_filetype = 0;
}
else if (strcmp(argv[i], "-h") == 0)
{
usage(argv[0]);
}
}
else
{
file_name = argv[i];
}
}
if (argc < 2 || file_name == NULL)
{
usage(argv[0]);
}
r = open_archive_file(&infp, file_name, &header);
if (r != 0)
{
switch(r)
{
case 1:
fprintf(stderr, "Can not open %s for reading\n", file_name);
break;
case 2:
fprintf(stderr, "Bad archive header\n");
break;
}
exit(1);
}
r = skip_to_data(&header);
if (r != 0)
{
switch (r)
{
case 1:
fprintf(stderr, "Can not find compressed data\n");
break;
case 2:
fprintf(stderr, "Unexpected end of file\n");
break;
}
exit(1);
}
while ((c = fgetc(infp)) != EOF)
{
switch (state)
{
case STATE_NULL:
if (c == 'r')
{
state = STATE_GOT_R;
}
break;
case STATE_GOT_R:
if (c == 'd')
{
state = STATE_GOT_RD;
}
else if (c == 'n')
{
state = STATE_GOT_RN;
}
else if (c == 's')
{
state = STATE_GOT_RS;
}
else
{
state = STATE_NULL;
}
break;
case STATE_GOT_RD:
if (c == 'i')
{
state = STATE_GOT_RDI;
}
else if (c == 'a')
{
state = STATE_GOT_RDA;
}
else
{
state = STATE_NULL;
}
break;
case STATE_GOT_RN:
if (c == 'a')
{
state = STATE_GOT_RNA;
}
else
{
state = STATE_NULL;
}
break;
case STATE_GOT_RS:
if (c == 'q')
{
state = STATE_GOT_RSQ;
}
else
{
state = STATE_NULL;
}
break;
case STATE_GOT_RDI:
if (c == 'r')
{
handle_dir_data();
}
state = STATE_NULL;
break;
case STATE_GOT_RNA:
if (c == 'm')
{
handle_file_name();
}
state = STATE_NULL;
break;
case STATE_GOT_RDA:
if (c == 't')
{
extract_data();
}
state = STATE_NULL;
break;
case STATE_GOT_RSQ:
if (c == 's')
{
handle_sqs();
}
state = STATE_NULL;
break;
}
}
return 0;
}