summaryrefslogtreecommitdiffstats
path: root/abcread.c
blob: b8bba5e9e78cb9065c1b18dbf935831232412f31 (plain)
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
/*
 * abcread.c
 *
 * Program to extract the contents from an ABC/UFD-DOS filesystem
 */
#include <ctype.h>
#include <dirent.h>
#include <limits.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wchar.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "util.h"

static void unmangle_filename(char *out, const unsigned char *in)
{
  static const wchar_t my_tolower[256] =
    L"\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
    L"\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
    L" !\"#¤%&'()*+,-./0123456789:;<=>?"
    L"éabcdefghijklmnopqrstuvwxyzäöåü_"
    L"éabcdefghijklmnopqrstuvwxyzäöåü\377"
    L"\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217"
    L"\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237"
    L"\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257"
    L"\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277"
    L"\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317"
    L"\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337"
    L"\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357"
    L"\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377";
  int i;

  wctomb(NULL, 0);

  for (i = 0; i < 8; i++) {
    if (*in != ' ')
      out += wctomb(out, my_tolower[*in]);
    in++;
  }

  if (memcmp(in, "   ", 3) && memcmp(in, "Ufd", 3)) {
    out += wctomb(out, L'.');
    for (i = 0; i < 3; i++) {
      if (*in != ' ')
	out += wctomb(out, my_tolower[*in]);
      in++;
    }
  }

  *out = '\0';
}

/* Copy files as given by tree on stdin */
static int
extract_dir_sector(struct disk *disk, const char *dirpath, int sectnr)
{
  char filename[4096], *p;
  unsigned char *sector;
  unsigned char *header;
  int i, hsect, dsect, len;
  int ufd, skip, cleft;
  FILE *f;

  /* 16 directory entries per sector */
  sector = disk->image + (sectnr << 8);

  for (i = 0; i < 16; i++, sector += 16) {
    hsect = (sector[0] << 3) + (sector[1] >> 5);
    hsect <<= disk->fmt->cluster_shift;
    if (!hsect || (sector[0] & sector[1]) == 0xff ||
	sector[4] == 0x00 || sector[4] == 0xff)
      continue;			/* Empty slot */

    header = disk->image + (hsect << 8);

    /* Length of file in sectors, including header block */
    len = sector[2] + (sector[3] << 8);

    /*
     * Early version of ABC-DOS didn't fill in the length field; those
     * versions all had cluster size == 1 so we can just read until the
     * header block runs out...
     */
    if (len == 0)
      len = disk->fmt->sectors;

    ufd = !memcmp(sector+12, "Ufd", 3);
    p = filename + sprintf(filename, "%s/", dirpath);
    unmangle_filename(p, sector + 4);

    if (ufd)
      mkdir(filename, 0777);
    else
      f = fopen(filename, "wb");

    /* Skip the header block for a file, header block and bitmap for UFD */
    skip = 1 + ufd;
    cleft = 0;
    dsect = -1;
    header += 4;

    while (len--) {
      if (cleft) {
	dsect++;
      } else {
	if ((header[0] & header[1]) == 0xff)
	  break;			/* Premature end of data */

	dsect = (header[0] << 3) + (header[1] >> 5);
	dsect <<= disk->fmt->cluster_shift;
	cleft = ((header[1] & 31) + 1) << disk->fmt->cluster_shift;

	header += 2;
      }
      cleft--;

      if (skip) {
	skip--;
      } else if (ufd) {
	  extract_dir_sector(disk, filename, dsect);
      } else {
	  fwrite(disk->image + (dsect << 8) + 3, 1, 253, f);
      }
    }

    if (!ufd)
      fclose(f);
  }

  return 0;
}

int main(int argc, char *argv[])
{
  struct disk *disk;
  const struct diskfmt *fmt;
  FILE *f;
  int i;

  setlocale(LC_ALL, "");

  program = argv[0];

  if (argc != 4 || !(fmt = get_format(argv[1]))) {
    fprintf(stderr, "Usage: %s format file outdir\n", program);
    show_formats(stderr);
    exit(1);
  }

  disk = allocate_disk(fmt);

  f = !strcmp(argv[2], "-") ? stdin : fopen(argv[2], "rb");
  fread(disk->image, 1, fmt->sectors << 8, f);
  if (f != stdin)
    fclose(f);

  /* Extract files */
  for (i = 0; i < 16; i++)
    extract_dir_sector(disk, argv[3], i+16);

  return 0;
}