|
|
1.1 root 1: /*
2: * readdisk
1.1.1.2 root 3: *
1.1 root 4: * Read files from Amiga disk files
5: *
6: * Copyright 1996 Bernd Schmidt
1.1.1.3 root 7: * Copyright 1998 Jim Cooper
1.1 root 8: */
9:
10: #include "sysconfig.h"
11: #include "sysdeps.h"
12:
1.1.1.2 root 13: #include "uae.h"
14:
1.1.1.3 root 15: void write_log (const char *s,...)
1.1.1.2 root 16: {
1.1.1.3 root 17: fprintf (stderr, "%s", s);
1.1.1.2 root 18: }
19:
1.1 root 20: unsigned char filemem[901120];
21:
1.1.1.3 root 22: typedef struct afile {
1.1 root 23: struct afile *sibling;
24: unsigned char *data;
1.1.1.2 root 25: uae_u32 size;
1.1 root 26: char name[32];
27: } afile;
28:
1.1.1.3 root 29: typedef struct directory {
1.1 root 30: struct directory *sibling;
31: struct directory *subdirs;
32: struct afile *files;
33: char name[32];
34: } directory;
35:
1.1.1.3 root 36: static int secdatasize, secdataoffset;
37:
1.1.1.2 root 38: static uae_u32 readlong (unsigned char *buffer, int pos)
1.1 root 39: {
40: return ((*(buffer + pos) << 24) + (*(buffer + pos + 1) << 16)
1.1.1.3 root 41: + (*(buffer + pos + 2) << 8) + *(buffer + pos + 3));
1.1 root 42: }
43:
44: static afile *read_file (unsigned char *filebuf)
45: {
1.1.1.3 root 46: afile *a = (afile *) xmalloc (sizeof (afile));
1.1 root 47: int sizeleft;
48: unsigned char *datapos;
1.1.1.2 root 49: uae_u32 numblocks, blockpos;
50:
1.1 root 51: /* BCPL strings... Yuk. */
52: memset (a->name, 0, 32);
1.1.1.3 root 53: strncpy (a->name, (const char *) filebuf + 0x1B1, *(filebuf + 0x1B0));
1.1 root 54: sizeleft = a->size = readlong (filebuf, 0x144);
1.1.1.3 root 55: a->data = (unsigned char *) xmalloc (a->size);
1.1 root 56:
57: numblocks = readlong (filebuf, 0x8);
58: blockpos = 0x134;
59: datapos = a->data;
1.1.1.3 root 60: while (numblocks) {
61: unsigned char *databuf = filemem + 512 * readlong (filebuf, blockpos);
62: int readsize = sizeleft > secdatasize ? secdatasize : sizeleft;
63: memcpy (datapos, databuf + secdataoffset, readsize);
1.1 root 64: datapos += readsize;
65: sizeleft -= readsize;
1.1.1.2 root 66:
1.1 root 67: blockpos -= 4;
68: numblocks--;
69: if (!numblocks) {
1.1.1.2 root 70: uae_u32 nextflb = readlong (filebuf, 0x1F8);
1.1 root 71: if (nextflb) {
1.1.1.3 root 72: filebuf = filemem + 512 * nextflb;
1.1 root 73: blockpos = 0x134;
74: numblocks = readlong (filebuf, 0x8);
75: if (!filebuf) {
1.1.1.4 ! root 76: write_log ("Disk structure corrupted. Use DISKDOCTOR to correct it.\n");
1.1 root 77: abort ();
78: }
79: }
80: }
81: }
82: return a;
83: }
84:
85: static directory *read_dir (unsigned char *dirbuf)
86: {
1.1.1.3 root 87: directory *d = (directory *) xmalloc (sizeof (directory));
1.1.1.2 root 88: uae_u32 hashsize;
89: uae_u32 i;
90:
1.1 root 91: memset (d->name, 0, 32);
1.1.1.3 root 92: strncpy (d->name, (const char *) dirbuf + 0x1B1, *(dirbuf + 0x1B0));
1.1 root 93: d->sibling = 0;
94: d->subdirs = 0;
95: d->files = 0;
96: hashsize = readlong (dirbuf, 0xc);
97: if (!hashsize)
1.1.1.2 root 98: hashsize = 72;
1.1 root 99: if (hashsize != 72)
1.1.1.4 ! root 100: write_log ("Warning: Hash table with != 72 entries.\n");
1.1 root 101: for (i = 0; i < hashsize; i++) {
1.1.1.3 root 102: uae_u32 subblock = readlong (dirbuf, 0x18 + 4 * i);
103:
1.1.1.2 root 104: while (subblock) {
1.1 root 105: directory *subdir;
106: afile *subfile;
1.1.1.3 root 107: unsigned char *subbuf = filemem + 512 * subblock;
108: long dirtype;
1.1.1.2 root 109:
1.1.1.3 root 110: dirtype = (uae_s32) readlong (subbuf, 0x1FC);
111: if (dirtype > 0) {
1.1 root 112: subdir = read_dir (subbuf);
113: subdir->sibling = d->subdirs;
114: d->subdirs = subdir;
1.1.1.3 root 115: } else if (dirtype < 0) {
1.1 root 116: subfile = read_file (subbuf);
117: subfile->sibling = d->files;
118: d->files = subfile;
1.1.1.3 root 119: } else {
1.1.1.4 ! root 120: write_log ("Disk structure corrupted. Use DISKDOCTOR to correct it.\n");
1.1 root 121: abort ();
122: }
123: subblock = readlong (subbuf, 0x1F0);
124: }
125: }
126: return d;
127: }
128:
1.1.1.3 root 129: static void writedir (directory * dir)
1.1 root 130: {
131: directory *subdir;
132: afile *f;
1.1.1.2 root 133:
134: if (mkdir (dir->name, 0777) < 0 && errno != EEXIST) {
1.1.1.4 ! root 135: write_log ("Could not create directory \"%s\". Giving up.\n", dir->name);
1.1.1.2 root 136: exit (20);
1.1 root 137: }
138: if (chdir (dir->name) < 0) {
1.1.1.4 ! root 139: write_log ("Could not enter directory \"%s\". Giving up.\n", dir->name);
1.1 root 140: exit (20);
141: }
142: for (subdir = dir->subdirs; subdir; subdir = subdir->sibling)
1.1.1.2 root 143: writedir (subdir);
1.1 root 144: for (f = dir->files; f; f = f->sibling) {
145: int fd = creat (f->name, 0666);
146: if (fd < 0) {
1.1.1.4 ! root 147: write_log ("Could not create file. Giving up.\n");
1.1 root 148: exit (20);
149: }
150: write (fd, f->data, f->size);
151: close (fd);
1.1.1.2 root 152: }
1.1 root 153: chdir ("..");
154: }
155:
1.1.1.3 root 156: int main (int argc, char **argv)
1.1 root 157: {
158: directory *root;
159: FILE *inf;
1.1.1.3 root 160:
1.1 root 161: if (argc < 2 || argc > 3) {
1.1.1.4 ! root 162: write_log ("Usage: readdisk <file> [<destdir>]\n");
1.1 root 163: exit (20);
164: }
1.1.1.3 root 165: inf = fopen (argv[1], "rb");
1.1 root 166: if (inf == NULL) {
1.1.1.4 ! root 167: write_log ("can't open file\n");
1.1 root 168: exit (20);
169: }
1.1.1.3 root 170: fread (filemem, 1, 901120, inf);
1.1.1.2 root 171:
1.1.1.3 root 172: if (strncmp ((const char *) filemem, "DOS\0", 4) == 0
173: || strncmp ((const char *) filemem, "DOS\2", 4) == 0) {
174: secdatasize = 488;
175: secdataoffset = 24;
176: } else if (strncmp ((const char *) filemem, "DOS\1", 4) == 0
177: || strncmp ((const char *) filemem, "DOS\3", 4) == 0) {
178: secdatasize = 512;
179: secdataoffset = 0;
180: } else {
1.1.1.4 ! root 181: write_log ("Not a DOS disk.\n");
1.1 root 182: exit (20);
183: }
1.1.1.3 root 184: root = read_dir (filemem + 880 * 512);
1.1 root 185:
186: if (argc == 3)
1.1.1.2 root 187: if (chdir (argv[2]) < 0) {
1.1.1.4 ! root 188: write_log ("Couldn't change to %s. Giving up.\n", argv[2]);
1.1 root 189: exit (20);
190: }
191: writedir (root);
192: return 0;
193: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.