Annotation of uae/src/readdisk.c, revision 1.1.1.2

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.