-
Notifications
You must be signed in to change notification settings - Fork 2
/
oasis.c
472 lines (407 loc) · 15.1 KB
/
oasis.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
* Utility to list directory contents of OASIS floppy disks,
* and optionally extract the files.
*
* www.github.com/hharte/oasis-utils
*
* Copyright (c) 2021-2022, Howard M. Harte
*
* Reference: Directory Entry Block (DEB)
* http://bitsavers.org/pdf/phaseOneSystems/oasis/Macro_Assembler_Reference_Manual_2ed.pdf
* pp. 112
*
*/
#define _CRT_SECURE_NO_DEPRECATE
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "./oasis.h"
#include "./oasis_utils.h"
typedef struct oasis_args {
char image_filename[256];
char output_path[256];
char operation[8];
int dir_block_cnt;
int ascii;
int quiet;
} oasis_args_t;
/* Function prototypes */
int parse_args(int argc, char *argv[], oasis_args_t *args);
int oasis_read_dir_entries(FILE *stream,
directory_entry_block_t *dir_entries,
int dir_block_cnt,
size_t dir_entries_max);
int oasis_extract_file(directory_entry_block_t *dir_entry,
FILE *instream,
char *path,
int quiet,
int ascii);
#if defined(_WIN32)
# define strncasecmp(x, y, z) _strnicmp(x, y, z)
#endif /* if defined(_WIN32) */
int main(int argc, char *argv[]) {
FILE *instream;
directory_entry_block_t *dir_entry_list = NULL;
filesystem_block_t fs_block;
oasis_args_t args;
size_t dir_entries_max;
char label[FNAME_LEN + 1];
int positional_arg_cnt;
int dir_entry_cnt;
int i;
int result;
int extracted_file_count = 0;
int status = 0;
positional_arg_cnt = parse_args(argc, argv, &args);
if (positional_arg_cnt == 0) {
printf("OASIS File Utility [%s] (c) 2021 - Howard M. Harte\n",
VERSION);
printf("https://github.com/hharte/oasis-utils\n\n");
printf(
"usage is: %s <filename.img> [command] [<filename>|<path>] [-q] [-a]\n",
argv[0]);
printf("\t<filename.img> OASIS Disk Image in .img format.\n");
printf("\t[command] LI - List files\n");
printf("\t EX - Extract files to <path>\n");
printf("\tFlags:\n");
printf(
"\t -q Quiet: Don't list file details during extraction.\n");
printf(
"\t -a ASCII: Convert line endings and truncate output file at EOF.\n");
printf("\n\tIf no command is given, LIst is assumed.\n");
return -EINVAL;
}
if (!(instream = fopen(args.image_filename, "rb"))) {
fprintf(stderr, "Error Openening %s\n", argv[1]);
return -ENOENT;
}
if (0 != fseek(instream, 256, SEEK_SET)) {
fprintf(stderr, "Error seeking from image file.\n");
status = -EIO;
goto exit_main;
}
if (fread(&fs_block, sizeof(filesystem_block_t), 1, instream) != 1) {
fprintf(stderr, "Error reading from image file.\n");
status = -EIO;
goto exit_main;
}
dir_entries_max = (size_t)(fs_block.dir_entries_max) * 8;
if (dir_entries_max > 2048) {
fprintf(stderr, "dir_entries_max is invalid.\n");
status = -EIO;
goto exit_main;
}
snprintf(label, sizeof(label), "%s", fs_block.label);
printf("Label: %s\n", label);
printf("%d-%d-%d\n",
fs_block.num_cyl,
fs_block.num_heads >> 4,
fs_block.num_sectors);
printf("%zu directory entries\n", dir_entries_max);
printf("%dK free\n", fs_block.free_blocks);
if (0 != fseek(instream, 512, SEEK_SET)) {
printf("Error seeking input stream.\n");
fclose(instream);
return -EIO;
}
dir_entry_list =
(directory_entry_block_t *)calloc(dir_entries_max,
sizeof(directory_entry_block_t));
if (dir_entry_list == NULL) {
fprintf(stderr, "Memory allocation of %d bytes failed\n",
(int)(dir_entries_max * sizeof(directory_entry_block_t)));
status = -ENOMEM;
goto exit_main;
}
dir_entry_cnt = oasis_read_dir_entries(instream,
dir_entry_list,
args.dir_block_cnt,
dir_entries_max);
if (dir_entry_cnt == 0) {
fprintf(stderr, "File not found\n");
status = -ENOENT;
goto exit_main;
}
/* Parse the command, and perform the requested action. */
if ((positional_arg_cnt == 1) | (!strncasecmp(args.operation, "LI", 2))) {
printf(
"Fname--- Ftype-- --Date-- Time- -Recs Blks Format- -Sect Own SOw Other-\n");
for (i = 0; i < dir_entry_cnt; i++) {
oasis_list_dir_entry(&dir_entry_list[i]);
}
} else {
if (positional_arg_cnt < 2) {
fprintf(stderr, "filename required.\n");
status = -EBADF;
goto exit_main;
} else if (!strncasecmp(args.operation, "EX", 2)) {
for (i = 0; i < dir_entry_cnt; i++) {
result = oasis_extract_file(&dir_entry_list[i],
instream,
args.output_path,
args.quiet,
args.ascii);
if (result == 0) {
extracted_file_count++;
}
}
printf("Extracted %d files.\n", extracted_file_count);
}
}
exit_main:
if (dir_entry_list) free(dir_entry_list);
if (instream != NULL) fclose(instream);
return status;
}
int parse_args(int argc, char *argv[], oasis_args_t *args) {
int positional_arg_cnt = 0;
memset(args, 0, sizeof(oasis_args_t));
for (int i = 1; i < argc; i++) {
if (argv[i][0] != '-') {
switch (positional_arg_cnt) {
case 0:
snprintf(args->image_filename,
sizeof(args->image_filename),
"%s",
argv[i]);
break;
case 1:
snprintf(args->operation,
sizeof(args->operation),
"%s",
argv[i]);
break;
case 2:
snprintf(args->output_path,
sizeof(args->output_path),
"%s",
argv[i]);
break;
}
positional_arg_cnt++;
} else {
char flag = argv[i][1];
switch (flag) {
case 'a':
args->ascii = 1;
break;
case 'q':
args->quiet = 1;
break;
default:
printf("Unknown option '-%c'\n", flag);
break;
}
}
}
return positional_arg_cnt;
}
int oasis_read_dir_entries(FILE *stream,
directory_entry_block_t *dir_entries,
int dir_block_cnt,
size_t dir_entries_max) {
int dir_entry_index = 0;
for (size_t i = 0; i < dir_entries_max; i++) {
size_t readlen;
directory_entry_block_t *dir_entry = &dir_entries[dir_entry_index];
readlen = fread(dir_entry, sizeof(directory_entry_block_t), 1, stream);
if (readlen == 1) {
if (dir_entry->file_format > 0) {
dir_entry_index++;
}
}
}
return dir_entry_index;
}
int oasis_extract_file(directory_entry_block_t *dir_entry,
FILE *instream,
char *path,
int quiet,
int ascii) {
char oasis_fname[FNAME_LEN + 1];
char fname[FNAME_LEN + 1];
char oasis_ftype[FEXT_LEN + 1];
char ftype[FEXT_LEN + 1];
if (dir_entry->file_format == 0) {
return -ENOENT;
}
snprintf(oasis_fname, sizeof(oasis_fname), "%s", dir_entry->file_name);
snprintf(oasis_ftype, sizeof(oasis_ftype), "%s", dir_entry->file_type);
/* Truncate the filename if a space is encountered. */
for (unsigned int j = 0; j < strnlen(oasis_fname, sizeof(oasis_fname));
j++) {
if (oasis_fname[j] == ' ') oasis_fname[j] = '\0';
}
snprintf(fname, sizeof(fname), "%s", oasis_fname);
/* Truncate the type if a space is encountered. */
for (unsigned int j = 0; j < strnlen(oasis_ftype, sizeof(oasis_ftype));
j++) {
if (oasis_ftype[j] == ' ') oasis_ftype[j] = '\0';
}
snprintf(ftype, sizeof(fname), "%s", oasis_ftype);
FILE *ostream;
int file_offset;
uint8_t *file_buf;
uint8_t file_format;
int file_len = 0;
int current_block = 0;
char output_filename[256];
struct tm oasis_tm;
file_format = dir_entry->file_format & 0x1f;
switch (file_format) {
case FILE_FORMAT_RELOCATABLE:
file_len = dir_entry->record_count *
dir_entry->file_format_dependent1;
snprintf(output_filename,
sizeof(output_filename),
"%s%c%s.%s_R_%d",
path,
kPathSeparator,
fname,
ftype,
dir_entry->file_format_dependent1);
break;
case FILE_FORMAT_ABSOLUTE:
file_len = dir_entry->record_count *
dir_entry->file_format_dependent1;
snprintf(output_filename,
sizeof(output_filename),
"%s%c%s.%s_A_%d",
path,
kPathSeparator,
fname,
ftype,
dir_entry->file_format_dependent2);
break;
case FILE_FORMAT_SEQUENTIAL:
/* Determined by walking through the file block by block. */
file_len = BLOCK_SIZE;
snprintf(output_filename,
sizeof(output_filename),
"%s%c%s.%s_S_%d",
path,
kPathSeparator,
fname,
ftype,
dir_entry->file_format_dependent1);
break;
case FILE_FORMAT_DIRECT:
file_len = dir_entry->block_count * 1024;
snprintf(output_filename,
sizeof(output_filename),
"%s%c%s.%s_D_%d",
path,
kPathSeparator,
fname,
ftype,
dir_entry->file_format_dependent1);
break;
case FILE_FORMAT_INDEXED:
printf("Skipping INDEXED file: %s.%s\n", oasis_fname, oasis_ftype);
break;
case FILE_FORMAT_KEYED:
printf("Skipping KEYED file: %s.%s\n", oasis_fname, oasis_ftype);
break;
default:
break;
}
if (file_len == 0) {
return -ENOENT;
}
file_offset = dir_entry->start_sector * BLOCK_SIZE;
output_filename[sizeof(output_filename) - 1] = '\0';
if (!(ostream = fopen(output_filename, "wb"))) {
printf("Error Openening %s\n", output_filename);
return -ENOENT;
} else if ((file_buf = (uint8_t *)calloc(1, file_len))) {
if (0 != fseek(instream, file_offset, SEEK_SET)) {
printf("Error seeking input stream.\n");
free(file_buf);
fclose(ostream);
return -EIO;
}
if (file_format != FILE_FORMAT_SEQUENTIAL) {
/* For all file types except sequential, copy the entire file in one
shot (the file is contiguous) */
if (fread(file_buf, file_len, 1, instream) == 1) {
if (fwrite(file_buf, file_len, 1, ostream) != 1) {
printf("Error writing output stream.\n");
free(file_buf);
fclose(ostream);
return -EIO;
} else {}
} else {
printf("Error reading input stream.\n");
free(file_buf);
fclose(ostream);
return -EIO;
}
} else {
uint16_t link = 0;
/* For sequential, copy the file block by block (the file may not be
contiguous) */
do {
char text_buf[BLOCK_SIZE * 2] = { 0 };
char *text_ptr = text_buf;
int text_len;
int i;
if (fread(file_buf, BLOCK_SIZE, 1, instream) != 1) {
printf("Error reading input stream.\n");
free(file_buf);
fclose(ostream);
return -EIO;
}
link = file_buf[BLOCK_SIZE - 1] << 8;
link |= file_buf[BLOCK_SIZE - 2];
for (i = 0; i < BLOCK_SIZE - 2; i++) {
if (ascii && (file_buf[i] == SUB)) {
/* Text file EOF */
break;
}
if (ascii && (file_buf[i] == CR)) {
/* OASIS uses CR for line endings: convert CR to CR/LF
(for Windows) or LF (for UNIX) */
#if defined(_WIN32)
*text_ptr++ = file_buf[i];
#endif /* if defined(_WIN32) */
*text_ptr++ = LF; /* Add LF */
} else {
*text_ptr++ = file_buf[i];
}
}
text_len = (int)(text_ptr - text_buf);
fwrite(text_buf, text_len, 1, ostream);
current_block++;
if (current_block > dir_entry->block_count * 4) {
printf("Corrupted link: ");
break;
}
file_offset = link * BLOCK_SIZE;
if (0 != fseek(instream, file_offset, SEEK_SET)) {
free(file_buf);
fclose(ostream);
return -EIO;
}
} while (link != 0);
}
if (!quiet) {
printf("%s.%s -> %s (%ld bytes)\n",
oasis_fname,
oasis_ftype,
output_filename,
ftell(ostream));
}
free(file_buf);
fclose(ostream);
/* Preserve original file date/time */
oasis_convert_timestamp_to_tm(&dir_entry->timestamp, &oasis_tm);
set_file_time(output_filename, &oasis_tm);
return 0;
}
printf("Memory allocation of %d bytes failed\n", file_len);
fclose(ostream);
return -ENOMEM;
}