home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #1 / Amiga Plus 1999 #1.iso / System-Boost / Workbench / Archive / PPCxDMS / rd_src.lha / src2 / readdisk.c next >
Encoding:
C/C++ Source or Header  |  1998-02-28  |  4.3 KB  |  183 lines

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